From 857b175a74db80efa91392d427b58745b651a555 Mon Sep 17 00:00:00 2001 From: iris Date: Mon, 16 Nov 2026 20:35:03 +0100 Subject: [PATCH] swarm ui: show thinking is a toggle of its own, apart from the developer details --- docs/4-implementation/web-ui/swarm-ui.md | 6 ++- .../web-ui/terminal-rendering.md | 21 +++++--- frontend/packages/shared/package.json | 1 + frontend/packages/shared/src/prefs.ts | 46 ++++++++++++++-- .../src/settings/ShowThinkingSetting.tsx | 24 +++++++++ frontend/packages/shared/src/terminal/Row.tsx | 36 ++++++++----- .../shared/src/terminal/row-visibility.ts | 24 +++++++++ .../packages/shared/src/terminal/termMsg.ts | 4 ++ .../packages/swarm-ui/src/shell/Shell.tsx | 2 + .../swarm-ui/test/row-visibility.test.mjs | 53 +++++++++++++++++++ hive-sh4re/src/stream_enrich.rs | 31 +++++++++-- hive-sh4re/src/term_msg.rs | 20 +++++++ 12 files changed, 243 insertions(+), 25 deletions(-) create mode 100644 frontend/packages/shared/src/settings/ShowThinkingSetting.tsx create mode 100644 frontend/packages/shared/src/terminal/row-visibility.ts create mode 100644 frontend/packages/swarm-ui/test/row-visibility.test.mjs diff --git a/docs/4-implementation/web-ui/swarm-ui.md b/docs/4-implementation/web-ui/swarm-ui.md index 05db4cb43..ea0e57c0b 100644 --- a/docs/4-implementation/web-ui/swarm-ui.md +++ b/docs/4-implementation/web-ui/swarm-ui.md @@ -171,12 +171,16 @@ stream with an error status, the browser gives up and the badge reads the stream replays the rows. The messages page works the same way. The settings menu in the header has a **hide developer details** toggle. It drops -the muted debug rows (thinking, coalesced ticks, harness chatter) from every +the muted debug rows (coalesced ticks, harness chatter) from every terminal, the preview and the tabs, and rows already on screen hide or show as you flip it. It also hides the **components** tab, a page for developers, which still answers at `/components`. The browser keeps the choice for the swarm UI, apart from the hive dashboard's. +A **show thinking** toggle, on by default, shows or hides claude's thinking in +the same terminals, apart from the toggle above: thinking can be on with the +developer details hidden, or off with them shown. + The menu also has a **solid backgrounds** toggle. It turns the frosted, see-through surfaces opaque: the header bar, the badges and the terminal panes. It stays off until you turn it on, and the browser keeps the choice. diff --git a/docs/4-implementation/web-ui/terminal-rendering.md b/docs/4-implementation/web-ui/terminal-rendering.md index 34c62e921..bd074055f 100644 --- a/docs/4-implementation/web-ui/terminal-rendering.md +++ b/docs/4-implementation/web-ui/terminal-rendering.md @@ -2,7 +2,7 @@ An agent's terminal in the swarm UI renders one row per `TermMsg` (`hive-agent/src/term_msg.rs`): `{ts, icon?, level: debug|info|warn|error, -summary, body?, body_format?: markdown|diff, coalesce_key?}`. Classification +summary, body?, body_format?: markdown|diff, coalesce_key?, kind?: thinking}`. Classification (icon, summary text, whether a tool call gets an expandable body) happens server-side, once — `term_msg.rs` + `stream_enrich.rs` — and the swarm queue's terminal rows and `GET /events/stream` carry it identically. The stream wraps @@ -53,11 +53,20 @@ that motivated the change. The settings menu's "hide developer details" toggle (`getHideDebugPref()`, `@hive/shared/prefs.js`) skips `debug`-level rows entirely rather than -muting them further — client-side only, no reload. The swarm UI and the hive -dashboard both have the toggle, as one shared row -(`@hive/shared/hide-debug-setting.js`). Each browser origin keeps its own -setting. A `debug` row follows the toggle as it changes, so rows already -on screen hide or show at once, not only the rows that arrive after it. +muting them further — client-side only, no reload. The swarm UI has the +toggle as a shared row (`@hive/shared/hide-debug-setting.js`). Each browser +origin keeps its own setting. A `debug` row follows the toggle as it changes, +so rows already on screen hide or show at once, not only the rows that arrive +after it. + +Thinking has its own toggle, "show thinking" (`getShowThinkingPref()`, on by +default, `@hive/shared/show-thinking-setting.js`). The harness marks the two +thinking rows, the thinking text and the live token count, with +`kind: "thinking"`; they stay `debug` level on the wire. A thinking row follows +"show thinking" alone and every other `debug` row follows "hide developer +details" alone (`terminal/row-visibility.ts` is the rule). A row from before the +harness sent `kind` has none, so it follows "hide developer details" until the +agent is rebuilt. Per-tool icon + summary formatting (what a `Read`/`Edit`/`send` call's row actually says) lives in `stream_enrich.rs`'s `fmt_tool_use()` family — diff --git a/frontend/packages/shared/package.json b/frontend/packages/shared/package.json index 252c52970..dc365ca7f 100644 --- a/frontend/packages/shared/package.json +++ b/frontend/packages/shared/package.json @@ -53,6 +53,7 @@ "./motion-apply.js": "./src/settings/motion-apply.ts", "./settings-menu.js": "./src/settings/SettingsMenu.tsx", "./hide-debug-setting.js": "./src/settings/HideDebugSetting.tsx", + "./show-thinking-setting.js": "./src/settings/ShowThinkingSetting.tsx", "./expand-details-setting.js": "./src/settings/ExpandDetailsSetting.tsx", "./settings-menu.css": "./src/settings/SettingsMenu.css", "./solid-backgrounds.js": "./src/settings/solid-backgrounds.tsx", diff --git a/frontend/packages/shared/src/prefs.ts b/frontend/packages/shared/src/prefs.ts index 1b14dfb5d..f36a70807 100644 --- a/frontend/packages/shared/src/prefs.ts +++ b/frontend/packages/shared/src/prefs.ts @@ -2,9 +2,9 @@ // so a future second consumer (e.g. swarm-ui's own terminal) gets the // exact same key name for free via the browser's shared per-origin // storage, rather than an independent copy that could drift. -// `ExpandDetailsSetting` / `HideDebugSetting` (the settings popover rows -// of the per-agent page and of the swarm UI) are the only writers today; -// `Row.tsx` is the only reader of either. +// `ExpandDetailsSetting` / `HideDebugSetting` / `ShowThinkingSetting` (the +// settings popover rows of the per-agent page and of the swarm UI) are the only +// writers today; `Row.tsx` is the only reader of any of them. const EXPAND_DETAILS_KEY = "hive-agent-expand-details"; @@ -74,3 +74,43 @@ export function subscribeHideDebugPref(listener: () => void): () => void { window.removeEventListener("storage", onStorage); }; } + +const HIDE_THINKING_KEY = "hive-agent-hide-thinking"; + +// Whether a terminal shows claude's thinking (its thinking text and the live +// count of thinking tokens). On unless turned off, and apart from the "hide +// developer details" preference above. The key records the off state, so a +// browser that never touched the setting shows thinking. Same shape as the +// others: client-side only, read live. +export function getShowThinkingPref(): boolean { + try { + return localStorage.getItem(HIDE_THINKING_KEY) !== "1"; + } catch { + return true; + } +} +export function setShowThinkingPref(v: boolean): void { + try { + if (v) localStorage.removeItem(HIDE_THINKING_KEY); + else localStorage.setItem(HIDE_THINKING_KEY, "1"); + } catch { + /* localStorage unavailable — preference is session-only */ + } + for (const listener of showThinkingListeners) listener(); +} + +const showThinkingListeners = new Set<() => void>(); + +// Calls `listener` whenever the preference changes, here or in another tab of +// the same origin, as `subscribeHideDebugPref` does. Returns the unsubscribe. +export function subscribeShowThinkingPref(listener: () => void): () => void { + showThinkingListeners.add(listener); + const onStorage = (e: StorageEvent) => { + if (e.key === HIDE_THINKING_KEY || e.key === null) listener(); + }; + window.addEventListener("storage", onStorage); + return () => { + showThinkingListeners.delete(listener); + window.removeEventListener("storage", onStorage); + }; +} diff --git a/frontend/packages/shared/src/settings/ShowThinkingSetting.tsx b/frontend/packages/shared/src/settings/ShowThinkingSetting.tsx new file mode 100644 index 000000000..330865872 --- /dev/null +++ b/frontend/packages/shared/src/settings/ShowThinkingSetting.tsx @@ -0,0 +1,24 @@ +// Settings-menu row: "show thinking". A `` child, like +// `HideDebugSetting`, and apart from it: thinking shows or hides on this row +// alone, whatever "hide developer details" says. `../terminal/Row.tsx` follows +// the preference as it changes. +import { useState } from "preact/hooks"; +import { getShowThinkingPref, setShowThinkingPref } from "../prefs.js"; + +export function ShowThinkingSetting() { + const [show, setShow] = useState(() => getShowThinkingPref()); + return ( + + ); +} diff --git a/frontend/packages/shared/src/terminal/Row.tsx b/frontend/packages/shared/src/terminal/Row.tsx index bdcdc9a93..e0785927b 100644 --- a/frontend/packages/shared/src/terminal/Row.tsx +++ b/frontend/packages/shared/src/terminal/Row.tsx @@ -17,8 +17,11 @@ import { renderMarkdown } from "./markdown.js"; import { getExpandDetailsPref, getHideDebugPref, + getShowThinkingPref, subscribeHideDebugPref, + subscribeShowThinkingPref, } from "../prefs.js"; +import { rowHidden, rowIsFilterable } from "./row-visibility.js"; function MarkdownBody({ text }: { text: string }) { const ref = useRef(null); @@ -58,24 +61,33 @@ function DiffBody({ text }: { text: string }) { ); } -// A `debug` row, which follows the "hide developer details" preference as it -// changes. Only debug rows subscribe, so a long terminal does not hold a -// listener per row. -function DebugRow({ row }: { row: TermRow }) { - const [hidden, setHidden] = useState(() => getHideDebugPref()); +// A row that a preference can hide: a `debug` row ("hide developer details") +// or a thinking row ("show thinking"). It follows them as they change. Only +// these rows subscribe, so a long terminal does not hold a listener per row. +function FilterableRow({ row }: { row: TermRow }) { + const [hideDebug, setHideDebug] = useState(() => getHideDebugPref()); + const [showThinking, setShowThinking] = useState(() => getShowThinkingPref()); useEffect( - () => subscribeHideDebugPref(() => setHidden(getHideDebugPref())), + () => subscribeHideDebugPref(() => setHideDebug(getHideDebugPref())), [], ); - // "hide developer details" is a skip, not a dimmer — a hidden debug row - // still occupies no DOM node at all, same as it never streamed, rather - // than a CSS `display: none` that would keep it in the layout/DOM for - // no benefit. - return hidden ? null : ; + useEffect( + () => + subscribeShowThinkingPref(() => setShowThinking(getShowThinkingPref())), + [], + ); + // A hidden row is a skip, not a dimmer — it occupies no DOM node at all, same + // as it never streamed, rather than a CSS `display: none` that would keep it + // in the layout/DOM for no benefit. + return rowHidden(row, hideDebug, showThinking) ? null : ; } export function Row({ row }: { row: TermRow }) { - return row.level === "debug" ? : ; + return rowIsFilterable(row) ? ( + + ) : ( + + ); } function RowBody({ row }: { row: TermRow }) { diff --git a/frontend/packages/shared/src/terminal/row-visibility.ts b/frontend/packages/shared/src/terminal/row-visibility.ts new file mode 100644 index 000000000..58721d841 --- /dev/null +++ b/frontend/packages/shared/src/terminal/row-visibility.ts @@ -0,0 +1,24 @@ +// Whether a terminal row is shown, given the two preferences that can hide it. +// Pure, so the rule is tested without a DOM: `Row.tsx` asks it for every +// `debug` row and every thinking row. +// +// A thinking row follows "show thinking" alone, so it can be on while the +// developer details are hidden, or off while they are shown. Every other +// `debug` row follows "hide developer details" alone. A row of any other level +// is always shown. +import type { TermMsg } from "./termMsg.js"; + +export function rowHidden( + row: Pick, + hideDebug: boolean, + showThinking: boolean, +): boolean { + if (row.kind === "thinking") return !showThinking; + return row.level === "debug" && hideDebug; +} + +/** Whether a preference can hide the row at all, so that only those rows + * subscribe to the preferences. */ +export function rowIsFilterable(row: Pick): boolean { + return row.level === "debug" || row.kind === "thinking"; +} diff --git a/frontend/packages/shared/src/terminal/termMsg.ts b/frontend/packages/shared/src/terminal/termMsg.ts index 77fca2ad2..3faaf7224 100644 --- a/frontend/packages/shared/src/terminal/termMsg.ts +++ b/frontend/packages/shared/src/terminal/termMsg.ts @@ -21,6 +21,10 @@ export interface TermMsg { body?: string; body_format?: "markdown" | "diff"; coalesce_key?: string; + /** What the row is about, for the kinds a client treats apart from its level: + * claude's thinking, which "show thinking" controls. Absent on every other + * row, and on rows from before the harness sent it. */ + kind?: "thinking"; } /** One SSE frame / history array entry — `hive-agent`'s `TermEnvelope`. diff --git a/frontend/packages/swarm-ui/src/shell/Shell.tsx b/frontend/packages/swarm-ui/src/shell/Shell.tsx index 90fcb422b..94d43eac1 100644 --- a/frontend/packages/swarm-ui/src/shell/Shell.tsx +++ b/frontend/packages/swarm-ui/src/shell/Shell.tsx @@ -34,6 +34,7 @@ import { UserMenu } from "./UserMenu.js"; import { useHideDebug } from "./useHideDebug.js"; import { SettingsMenu } from "@hive/shared/settings-menu.js"; import { HideDebugSetting } from "@hive/shared/hide-debug-setting.js"; +import { ShowThinkingSetting } from "@hive/shared/show-thinking-setting.js"; import { ExpandDetailsSetting } from "@hive/shared/expand-details-setting.js"; import { useApplyThemeOverride } from "@hive/shared/theme-apply.js"; import { useApplyMotionOverride } from "@hive/shared/motion-apply.js"; @@ -495,6 +496,7 @@ export function Shell({ children }: { children: ComponentChildren }) { instead of sitting flush together at the right edge. */}
+ diff --git a/frontend/packages/swarm-ui/test/row-visibility.test.mjs b/frontend/packages/swarm-ui/test/row-visibility.test.mjs new file mode 100644 index 000000000..95a7303db --- /dev/null +++ b/frontend/packages/swarm-ui/test/row-visibility.test.mjs @@ -0,0 +1,53 @@ +// Which terminal rows the two preferences hide. Run by `npm test`, which +// imports the `.ts` module as it is. +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { + rowHidden, + rowIsFilterable, +} from "../../shared/src/terminal/row-visibility.ts"; + +const thinking = { level: "debug", kind: "thinking" }; +const chatter = { level: "debug" }; +const info = { level: "info" }; + +test("thinking follows show thinking alone", () => { + assert.equal(rowHidden(thinking, false, true), false); + assert.equal( + rowHidden(thinking, true, true), + false, + "developer details are hidden, thinking is not", + ); + assert.equal( + rowHidden(thinking, false, false), + true, + "developer details are shown, thinking is not", + ); + assert.equal(rowHidden(thinking, true, false), true); +}); + +test("other debug rows follow hide developer details alone", () => { + assert.equal(rowHidden(chatter, false, true), false); + assert.equal(rowHidden(chatter, true, true), true); + assert.equal(rowHidden(chatter, true, false), true); + assert.equal( + rowHidden(chatter, false, false), + false, + "hiding thinking leaves the rest", + ); +}); + +test("a row of any other level is always shown", () => { + for (const hide of [true, false]) + for (const show of [true, false]) { + assert.equal(rowHidden(info, hide, show), false); + assert.equal(rowHidden({ level: "error" }, hide, show), false); + } +}); + +test("only debug and thinking rows can be hidden", () => { + assert.equal(rowIsFilterable(thinking), true); + assert.equal(rowIsFilterable(chatter), true); + assert.equal(rowIsFilterable(info), false); + assert.equal(rowIsFilterable({ level: "warn" }), false); +}); diff --git a/hive-sh4re/src/stream_enrich.rs b/hive-sh4re/src/stream_enrich.rs index eac4b2a16..afba07ee2 100644 --- a/hive-sh4re/src/stream_enrich.rs +++ b/hive-sh4re/src/stream_enrich.rs @@ -15,7 +15,7 @@ //! `mcp__hyperhive__send`'s row say, which tools get an expandable body) //! didn't change; only the shape it gets packed into did. -use crate::term_msg::{BodyFormat, ClassifyCtx, Level, TermMsg}; +use crate::term_msg::{BodyFormat, ClassifyCtx, Kind, Level, TermMsg}; use hive_types::term_msg::{SEND_ICON, SEND_SUMMARY_PREFIX}; use serde_json::{Value, json}; @@ -71,7 +71,8 @@ fn classify_system(v: &Value) -> Vec { summary.unwrap_or_else(|| "thinking…".to_owned()), ) .icon("🧠") - .coalesce("thinking-tok"), + .coalesce("thinking-tok") + .kind(Kind::Thinking), ], "details" => { let mut m = TermMsg::new(Level::Debug, summary.unwrap_or_default()); @@ -254,7 +255,11 @@ fn classify_assistant_content(content: &[Value], ctx: &mut ClassifyCtx) -> Vec { if let (Some(id), Some(name)) = ( @@ -1043,6 +1048,23 @@ mod tests { assert_eq!(bare.coalesce_key.as_deref(), Some("thinking-tok")); } + /// A client shows or hides thinking apart from the other `debug` rows, so + /// both the thinking text and the live token count say what they are. + #[test] + fn the_thinking_rows_are_marked_and_the_other_debug_rows_are_not() { + let count = one(&json!({ "type": "system", "subtype": "thinking_tokens" })); + assert_eq!(count.kind, Some(Kind::Thinking)); + assert_eq!( + serde_json::to_value(&count).unwrap()["kind"], + json!("thinking") + ); + + let tick = one(&json!({ "type": "system", "subtype": "status" })); + assert_eq!(tick.level, Level::Debug); + assert_eq!(tick.kind, None); + assert!(serde_json::to_value(&tick).unwrap().get("kind").is_none()); + } + #[test] fn commands_changed_expands_into_the_command_list() { let m = one(&json!({ @@ -1285,6 +1307,9 @@ mod tests { assert_eq!(rows.len(), 3, "the unknown content block is skipped"); assert_eq!(rows[0].icon.as_deref(), Some("💬")); assert_eq!(rows[1].icon.as_deref(), Some("💭")); + assert_eq!(rows[1].kind, Some(Kind::Thinking)); + assert_eq!(rows[0].kind, None); + assert_eq!(rows[2].kind, None); // The tool_use was recorded for later correlation as a side effect. assert_eq!(ctx.tool_name("t1"), Some("Bash")); } diff --git a/hive-sh4re/src/term_msg.rs b/hive-sh4re/src/term_msg.rs index d896934cd..b1f8fda0a 100644 --- a/hive-sh4re/src/term_msg.rs +++ b/hive-sh4re/src/term_msg.rs @@ -39,6 +39,17 @@ pub enum Level { Error, } +/// What a row is about, for the few kinds a client treats on their own. Most +/// rows have none; their `level` and `icon` say what they need to. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub enum Kind { + /// What claude is thinking: its thinking text and the live count of + /// thinking tokens. A client can show or hide these apart from the other + /// `debug` rows. + Thinking, +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "snake_case")] pub enum BodyFormat { @@ -68,6 +79,8 @@ pub struct TermMsg { pub body_format: Option, #[serde(skip_serializing_if = "Option::is_none")] pub coalesce_key: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub kind: Option, } impl TermMsg { @@ -80,6 +93,7 @@ impl TermMsg { body: None, body_format: None, coalesce_key: None, + kind: None, } } @@ -102,6 +116,12 @@ impl TermMsg { self } + #[must_use] + pub fn kind(mut self, kind: Kind) -> Self { + self.kind = Some(kind); + self + } + /// Stamp the row with the time of the event it came from. #[must_use] pub fn at(mut self, ts: impl Into) -> Self {