From f6619a8fad01360ab7783bba6d4604319d6f1ff8 Mon Sep 17 00:00:00 2001 From: Kieran Zhang Date: Sun, 9 Aug 2026 12:07:44 +0800 Subject: [PATCH] feat: add ChatChainOfThoughtRail timeline skin for chain of thought MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Outcome of the /proto chain-of-thought exploration (4 variants judged against real part-sequence data). Decision record and follow-ups live in .scratch/cot-variants/: Baseline stays the default, Rail lands as an optional skin pending the Appearance setting (issues/01), Ledger is earmarked for the Trace page refactor (issues/02), Narrative rejected. - rounds group think→tool loops with per-round durations - thinking vs tool nodes as distinct rail markers, aligned per node kind - tool rows reuse chat-tool target extraction/duration formatting and expand inline to args/output payloads - registered on the /design Components layer in the same PR --- .scratch/cot-variants/PRD.md | 35 ++ .../issues/01-appearance-setting-cot-style.md | 24 ++ .../issues/02-trace-page-ledger-refactor.md | 20 ++ apps/desktop/src/pages/design-components.tsx | 83 +++++ .../chat/chat-chain-of-thought-rail.test.tsx | 109 +++++++ .../ui/chat/chat-chain-of-thought-rail.tsx | 227 +++++++++++++ apps/desktop/src/shared/ui/chat/chat.css | 299 ++++++++++++++++++ 7 files changed, 797 insertions(+) create mode 100644 .scratch/cot-variants/PRD.md create mode 100644 .scratch/cot-variants/issues/01-appearance-setting-cot-style.md create mode 100644 .scratch/cot-variants/issues/02-trace-page-ledger-refactor.md create mode 100644 apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.test.tsx create mode 100644 apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.tsx diff --git a/.scratch/cot-variants/PRD.md b/.scratch/cot-variants/PRD.md new file mode 100644 index 00000000..6df09131 --- /dev/null +++ b/.scratch/cot-variants/PRD.md @@ -0,0 +1,35 @@ +--- +Status: done +--- + +# 思维链(Chain of Thought)形态探索 — 决策记录 + +2026-08-09,从 `/proto` 原型跑了 4 个变体(基于真实数据形态:Pi 一条 assistant 消息 = +`thinking | tool_call | text` 的有序 part 序列,think→tool→think 多轮循环是一等公民, +见 `packages/core/src/agent-runtime-event.ts:21`),对比后决策如下。 + +## 决策 + +- **Baseline(现状,折叠一行 + 左边线步骤列表)保持默认**。ChatChainOfThought 不动。 +- **Rail(时间轨)保留为正式组件** `shared/ui/chat/chat-chain-of-thought-rail.tsx`: + 轮次分界 + 节点轨道,思考/工具是不同形态节点,每步时长右对齐(tabular-nums)。 + 暂不在 agent-workspace 接线;等 Appearance 设置页落地后作为用户可选项 + (见 issues/01)。 +- **Ledger(台账,等宽五列网格)不进聊天流**,但形态验证成功,定为 **Trace 页重构方向** + (见 issues/02)。原型实现要点:等宽字体、`kind/name/subject/duration` 网格列、 + 轮次分隔线、行内展开 payload、状态字形(✓/✕/●)。 +- **Narrative(叙事流,工具压成行内 chip)淘汰**:设计不成立——工具信息弱化过头, + chip 超过一排后挤压严重,气质上也没有带来足够收益。 + +## 关键实现记号(从原型带出) + +- 节点圆点必须按节点类型分别对齐首行中心(思考标签行中心 ≈12px,32px 工具行中心 + ≈16px),统一 top 值必然错位。 +- 说明性 `

` 在深色浮层里要显式设 color,应用全局段落色(近黑)会压过继承。 +- 工具行 target 提取与时长格式化直接复用 `chat-tool.tsx` 的 + `toolTargetFromArgs` / `formatToolDuration`。 + +## 原型位置(已拆除) + +原型面 `apps/desktop/src/proto/chain-of-thought/` 与 `main.tsx` 中带 +`proto:chain-of-thought` 栅栏注释的 dev-only 挂载已在决策后删除。 diff --git a/.scratch/cot-variants/issues/01-appearance-setting-cot-style.md b/.scratch/cot-variants/issues/01-appearance-setting-cot-style.md new file mode 100644 index 00000000..4604af56 --- /dev/null +++ b/.scratch/cot-variants/issues/01-appearance-setting-cot-style.md @@ -0,0 +1,24 @@ +--- +Status: needs-triage +--- + +# Appearance 设置:思维链样式可选项(Baseline / Rail) + +## 背景 + +思维链形态探索(见 ../PRD.md)决定同时保留两种渲染:默认 Baseline +(`ChatChainOfThought`),可选 Rail(`ChatChainOfThoughtRail`,已落 +`shared/ui/chat/` 并注册 /design 页)。 + +## 要做的事 + +- 等 Settings 出现 Appearance 分区时,增加 "Chain of thought style" 选项: + `Compact(默认,Baseline)` / `Timeline(Rail)`。 +- 偏好持久化(跟随现有 settings 存储机制),agent-workspace 的 + `AssistantRunTrace` 按偏好在两个组件间切换。 +- 两种样式共用同一数据(RunTimelineItem 序列),切换不改行为契约 + (data-slot 断言、折叠默认值 DF-005B 的 remount 逻辑保持)。 + +## 阻塞 + +- 依赖 Appearance 设置页立项(当前 Settings 只有 Provider 配置)。 diff --git a/.scratch/cot-variants/issues/02-trace-page-ledger-refactor.md b/.scratch/cot-variants/issues/02-trace-page-ledger-refactor.md new file mode 100644 index 00000000..386b09a1 --- /dev/null +++ b/.scratch/cot-variants/issues/02-trace-page-ledger-refactor.md @@ -0,0 +1,20 @@ +--- +Status: needs-triage +--- + +# Trace 页重构:采用台账(Ledger)形态 + +## 背景 + +思维链形态探索(见 ../PRD.md)中,Ledger 变体(等宽字体、kind/name/subject/duration +对齐网格、轮次分隔、行内展开 payload、✓/✕/● 状态字形)被验证为高密度扫读 trace 的 +最佳形态——12.4s 的失败 Bash 一眼跳出。它不适合聊天流,但正好命中 Trace 页 +"做得不好看、信息密度低"的痛点。 + +## 要做的事 + +- 以 Ledger 形态重构 Trace 页的会话事件列表(`pages/trace.tsx` / session-detail)。 +- 从原型提炼网格列:状态字形 / kind / 名称 / 对象(target 截断) / 右对齐 tabular-nums + 时长;轮次(或消息)分隔线;行内展开 args/output。 +- 组件落 `shared/ui/`(命名待定,如 `pi-trace-ledger`),同 PR 注册 /design 页。 +- 原型参考实现见 ../PRD.md 的"关键实现记号"(原型代码已拆除,形态见决策记录)。 diff --git a/apps/desktop/src/pages/design-components.tsx b/apps/desktop/src/pages/design-components.tsx index 72225633..1ee9e065 100644 --- a/apps/desktop/src/pages/design-components.tsx +++ b/apps/desktop/src/pages/design-components.tsx @@ -5,6 +5,10 @@ import { PiBarChart } from "@/shared/ui/pi-bar-chart"; import { PiKpi } from "@/shared/ui/pi-kpi"; import { PiSheet } from "@/shared/ui/pi-sheet"; import { ChatChainOfThought } from "@/shared/ui/chat/chat-chain-of-thought"; +import { + ChatChainOfThoughtRail, + type ChainOfThoughtRailPart, +} from "@/shared/ui/chat/chat-chain-of-thought-rail"; import { ChatCodeBlock } from "@/shared/ui/chat/chat-code-block"; import { ChatConversation } from "@/shared/ui/chat/chat-conversation"; import { ChatMarkdown, ChatStreamMarkdown } from "@/shared/ui/chat/chat-markdown"; @@ -513,6 +517,84 @@ function ChatChainOfThoughtGallery() { ); } +const railParts: ChainOfThoughtRailPart[] = [ + { + kind: "thinking", + id: "rail-t1", + durationMs: 8200, + text: "The failing assertion says the replay projection dropped the tool_call part after a fork — reading the projection code first.", + }, + { + kind: "tool", + id: "rail-c1", + tool: { + toolName: "Read", + toolCallId: "rail-call-1", + state: "output-available", + durationMs: 320, + argsText: JSON.stringify({ path: "packages/backend/src/workspace/fork.ts" }), + output: "77 remapEntryId(part.piEntryId)\n81 part.toolCallId // never remapped", + }, + }, + { + kind: "tool", + id: "rail-c2", + tool: { + toolName: "Bash", + toolCallId: "rail-call-2", + state: "output-error", + durationMs: 12400, + argsText: JSON.stringify({ command: "bun test packages/backend/src/workspace" }), + output: "1 tests failed:\n ✗ replays forked session [4012ms]", + }, + }, + { + kind: "thinking", + id: "rail-t2", + durationMs: 2100, + text: "Root cause confirmed: toolCallId is never remapped on fork. Fixing.", + }, + { + kind: "tool", + id: "rail-c3", + tool: { + toolName: "Grep", + toolCallId: "rail-call-3", + state: "input-streaming", + argsText: JSON.stringify({ pattern: "remapToolCallId" }), + }, + }, +]; + +function ChatChainOfThoughtRailGallery() { + return ( + +

+ + + + + + + + + +
+ + ); +} + function ChatConversationGallery() { return ( @@ -559,6 +641,7 @@ export function DesignComponentsLayer() { + diff --git a/apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.test.tsx b/apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.test.tsx new file mode 100644 index 00000000..4979434f --- /dev/null +++ b/apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.test.tsx @@ -0,0 +1,109 @@ +import { render, screen } from "@testing-library/react"; +import userEvent from "@testing-library/user-event"; +import { describe, expect, it } from "vitest"; +import { + ChatChainOfThoughtRail, + type ChainOfThoughtRailPart, +} from "@/shared/ui/chat/chat-chain-of-thought-rail"; + +const parts: ChainOfThoughtRailPart[] = [ + { kind: "thinking", id: "t1", text: "Reading the projection first.", durationMs: 8200 }, + { + kind: "tool", + id: "c1", + tool: { + toolName: "Read", + toolCallId: "call_1", + state: "output-available", + durationMs: 320, + argsText: JSON.stringify({ path: "packages/backend/src/workspace/fork.ts" }), + output: "77 remapEntryId(part.piEntryId)", + }, + }, + { + kind: "tool", + id: "c2", + tool: { + toolName: "Bash", + toolCallId: "call_2", + state: "output-error", + durationMs: 12400, + argsText: JSON.stringify({ command: "bun test packages/backend" }), + output: "1 tests failed", + }, + }, + { kind: "thinking", id: "t2", text: "Root cause confirmed, fixing.", durationMs: 2100 }, +]; + +describe("ChatChainOfThoughtRail", () => { + it("groups think→tool loops into labeled rounds with distinct node kinds", () => { + const { container } = render( + , + ); + + expect( + container.querySelector('[data-slot="chain-of-thought-rail"]'), + ).toBeInTheDocument(); + expect(screen.getByText("Round 1")).toBeInTheDocument(); + expect(screen.getByText("Round 2")).toBeInTheDocument(); + + const nodes = container.querySelectorAll('[data-slot="chain-of-thought-rail-node"]'); + expect(nodes).toHaveLength(4); + expect(nodes[0]).toHaveAttribute("data-kind", "thinking"); + expect(nodes[1]).toHaveAttribute("data-kind", "tool"); + }); + + it("shows tool name, extracted target, duration, and error state", () => { + const { container } = render( + , + ); + + expect(screen.getByText("Read")).toBeInTheDocument(); + expect(screen.getByText("packages/backend/src/workspace/fork.ts")).toBeInTheDocument(); + expect(screen.getByText("320ms")).toBeInTheDocument(); + expect(screen.getByText("failed")).toBeInTheDocument(); + expect( + container.querySelector('[data-slot="chain-of-thought-rail-node"][data-state="output-error"]'), + ).toBeInTheDocument(); + }); + + it("reveals args and output when a tool row is expanded", async () => { + const user = userEvent.setup(); + render( + , + ); + + const detail = screen.getByText("77 remapEntryId(part.piEntryId)", { exact: false }); + expect(detail.closest("details")).not.toHaveAttribute("open"); + + await user.click(screen.getByText("Read")); + + expect(detail.closest("details")).toHaveAttribute("open"); + }); + + it("collapses by default when settled and toggles from the trigger", async () => { + const user = userEvent.setup(); + render(); + + const trigger = screen.getByRole("button", { name: "Thought for 23s" }); + expect(trigger).toHaveAttribute("aria-expanded", "false"); + + await user.click(trigger); + expect(trigger).toHaveAttribute("aria-expanded", "true"); + }); + + it("streams: expands by default and shimmers the trigger label", () => { + const { container } = render( + , + ); + + const trigger = screen.getByRole("button"); + expect(trigger).toHaveAttribute("aria-expanded", "true"); + expect(trigger).toHaveTextContent("Thinking…"); + expect(container.querySelector('[data-slot="text-shimmer"]')).toBeInTheDocument(); + }); +}); diff --git a/apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.tsx b/apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.tsx new file mode 100644 index 00000000..544694e6 --- /dev/null +++ b/apps/desktop/src/shared/ui/chat/chat-chain-of-thought-rail.tsx @@ -0,0 +1,227 @@ +import { Collapsible } from "@base-ui-components/react/collapsible"; +import { TextShimmer } from "@/shared/ui/chat/text-shimmer"; +import { + formatToolDuration, + toolTargetFromArgs, + type ChatToolItem, +} from "@/shared/ui/chat/chat-tool"; + +/** + * Timeline-style chain of thought: think→tool loops render as labeled + * rounds on a vertical rail, thinking and tool calls as distinct node + * kinds, every node carrying its duration. Alternative skin to + * ChatChainOfThought over the same part sequence — selectable via the + * future Appearance setting (.scratch/cot-variants/issues/01). + */ +export type ChainOfThoughtRailPart = + | { kind: "thinking"; id: string; text: string; durationMs?: number } + | { kind: "tool"; id: string; tool: ChatToolItem }; + +type RailRound = { + id: string; + thinking?: Extract; + tools: Extract[]; +}; + +/** A thinking part opens a new round; tools attach to the current one. */ +function groupRounds(parts: ChainOfThoughtRailPart[]): RailRound[] { + const rounds: RailRound[] = []; + for (const part of parts) { + const last = rounds[rounds.length - 1]; + if (part.kind === "thinking") { + rounds.push({ id: part.id, thinking: part, tools: [] }); + } else if (last) { + last.tools.push(part); + } else { + rounds.push({ id: part.id, tools: [part] }); + } + } + return rounds; +} + +function Chevron() { + return ( + + ); +} + +function RailToolNode({ tool }: { tool: ChatToolItem }) { + const target = toolTargetFromArgs(tool.argsText); + const running = tool.state === "input-streaming" || tool.state === "input-available"; + const hasDetail = tool.argsText != null || tool.output != null; + + const row = ( + <> + {tool.toolName ?? "tool"} + {target ? ( + {target} + ) : null} + + {tool.state === "output-error" ? ( + + failed + + ) : running ? ( + + running + + ) : null} + {tool.durationMs !== undefined ? ( + + {formatToolDuration(tool.durationMs)} + + ) : null} + + + ); + + return ( +
  • +
  • + ); +} + +export function ChatChainOfThoughtRail({ + parts, + summary, + className = "", + defaultExpanded = false, + isStreaming = false, +}: { + parts: ChainOfThoughtRailPart[]; + /** Settled trigger label, e.g. "Thought for 38s · 6 tool calls". */ + summary: string; + className?: string; + defaultExpanded?: boolean; + isStreaming?: boolean; +}) { + const rounds = groupRounds(parts); + + return ( + + + + {isStreaming ? Thinking… : summary} + + +
    + {rounds.map((round, index) => { + const roundDuration = + (round.thinking?.durationMs ?? 0) + + round.tools.reduce((sum, part) => sum + (part.tool.durationMs ?? 0), 0); + return ( +
    +
    + + Round {index + 1} + + + {formatToolDuration(roundDuration)} + +
    +
      + {round.thinking ? ( +
    1. +
    2. + ) : null} + {round.tools.map((part) => ( + + ))} +
    +
    + ); + })} +
    +
    +
    + ); +} diff --git a/apps/desktop/src/shared/ui/chat/chat.css b/apps/desktop/src/shared/ui/chat/chat.css index bc438449..364f7637 100644 --- a/apps/desktop/src/shared/ui/chat/chat.css +++ b/apps/desktop/src/shared/ui/chat/chat.css @@ -339,3 +339,302 @@ align-items: center; color: var(--color-icon-secondary); } + +/* —— Chain of thought: rail skin —— */ + +.chain-of-thought-rail { + min-width: 0; + font-size: var(--font-size-base); +} + +.chain-of-thought-rail__trigger { + display: inline-flex; + align-items: center; + gap: var(--spacing-1); + min-height: var(--size-element-md); + border: none; + background: transparent; + padding: 0; + color: var(--color-text-secondary); + font-size: var(--font-size-base); + cursor: pointer; + touch-action: manipulation; + transition: color var(--duration-fast) var(--ease-standard); +} + +@media (hover: hover) and (pointer: fine) { + .chain-of-thought-rail__trigger:hover { + color: var(--color-text-primary); + } +} + +.chain-of-thought-rail__trigger-chevron { + display: inline-flex; + transition: transform var(--duration-fast) var(--ease-standard); +} + +.chain-of-thought-rail__trigger[data-panel-open] .chain-of-thought-rail__trigger-chevron { + transform: rotate(90deg); +} + +.chain-of-thought-rail__panel { + margin-block-start: var(--spacing-2); +} + +.chain-of-thought-rail__track { + display: flex; + flex-direction: column; + gap: var(--spacing-3); +} + +.chain-of-thought-rail__round-header { + display: flex; + align-items: baseline; + gap: var(--spacing-2); + margin-block-end: var(--spacing-1); +} + +.chain-of-thought-rail__round-label { + font-size: 11px; + font-weight: var(--font-weight-medium); + letter-spacing: 0.08em; + text-transform: uppercase; + color: var(--color-text-disabled); +} + +.chain-of-thought-rail__nodes { + margin: 0; + padding: 0; + list-style: none; + display: flex; + flex-direction: column; + gap: var(--spacing-1); +} + +.chain-of-thought-rail__node { + position: relative; + padding-inline-start: var(--spacing-3); + min-width: 0; +} + +/* the rail line — starts just below each node's marker */ +.chain-of-thought-rail__node::before { + content: ""; + position: absolute; + inset-inline-start: 3.5px; + top: 18px; + bottom: -8px; + width: var(--border-width); + background: var(--color-border); +} + +.chain-of-thought-rail__node[data-kind="tool"]::before { + top: 22px; +} + +.chain-of-thought-rail__node:last-child::before { + display: none; +} + +/* Markers center on their node's first text line: thinking label line + (~24px tall, center 12px) vs. tool row (32px centered, center 16px). */ +.chain-of-thought-rail__marker { + position: absolute; + inset-inline-start: 0; + top: 8px; + width: 8px; + height: 8px; + border-radius: var(--radius-full); + background: var(--color-border-emphasized); + pointer-events: none; +} + +.chain-of-thought-rail__node[data-kind="tool"] .chain-of-thought-rail__marker { + top: 12px; +} + +.chain-of-thought-rail__marker[data-kind="thinking"] { + background: transparent; + border: var(--border-width) solid var(--color-border-emphasized); +} + +.chain-of-thought-rail__marker[data-state="output-error"] { + background: var(--color-danger); +} + +.chain-of-thought-rail__marker[data-state="input-streaming"], +.chain-of-thought-rail__marker[data-state="input-available"] { + background: var(--color-text-secondary); + animation: chain-of-thought-rail-pulse 1.2s ease-in-out infinite; +} + +@keyframes chain-of-thought-rail-pulse { + 50% { + opacity: 0.35; + } +} + +.chain-of-thought-rail__node-head { + display: flex; + align-items: baseline; + gap: var(--spacing-2); +} + +.chain-of-thought-rail__thinking-label { + font-weight: var(--font-weight-medium); + color: var(--color-text-primary); +} + +.chain-of-thought-rail__thinking-text { + margin: var(--spacing-0-5) 0 0; + max-width: 65ch; + color: var(--color-text-secondary); + text-wrap: pretty; + overflow-wrap: anywhere; +} + +.chain-of-thought-rail__node-meta { + margin-inline-start: auto; + display: inline-flex; + align-items: baseline; + gap: var(--spacing-1-5); + flex-shrink: 0; +} + +.chain-of-thought-rail__duration { + font-size: var(--font-size-sm); + color: var(--color-text-disabled); + font-variant-numeric: tabular-nums; +} + +.chain-of-thought-rail__tool-row { + display: flex; + align-items: center; + gap: var(--spacing-1-5); + min-height: var(--size-element-sm); + padding-block: var(--spacing-0-5); + border-radius: var(--radius-inner); + cursor: pointer; + min-width: 0; + list-style: none; + touch-action: manipulation; +} + +.chain-of-thought-rail__tool-row::-webkit-details-marker { + display: none; +} + +.chain-of-thought-rail__tool-row[data-static="true"] { + cursor: default; +} + +@media (hover: hover) and (pointer: fine) { + details > .chain-of-thought-rail__tool-row:hover { + background: var(--color-overlay-hover); + margin-inline: calc(-1 * var(--spacing-1-5)); + padding-inline: var(--spacing-1-5); + } +} + +.chain-of-thought-rail__disclosure { + display: inline-flex; + color: var(--color-icon-secondary); + transition: transform var(--duration-fast) var(--ease-standard); + flex-shrink: 0; +} + +.chain-of-thought-rail__disclosure[data-hidden="true"] { + visibility: hidden; +} + +details[open] > .chain-of-thought-rail__tool-row .chain-of-thought-rail__disclosure { + transform: rotate(90deg); +} + +.chain-of-thought-rail__tool-name { + font-weight: var(--font-weight-medium); + color: var(--color-text-primary); + flex-shrink: 0; +} + +.chain-of-thought-rail__tool-target { + font-family: var(--font-family-code); + font-size: var(--font-size-sm); + color: var(--color-text-secondary); + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + min-width: 0; +} + +.chain-of-thought-rail__badge { + font-size: 11px; + font-weight: var(--font-weight-medium); + letter-spacing: 0.04em; + text-transform: uppercase; +} + +.chain-of-thought-rail__badge[data-tone="error"] { + color: var(--color-danger); +} + +.chain-of-thought-rail__badge[data-tone="running"] { + color: var(--color-text-secondary); +} + +.chain-of-thought-rail__tool-detail { + padding: var(--spacing-1) 0 var(--spacing-1) calc(12px + var(--spacing-1-5)); +} + +.chain-of-thought-rail__pre { + margin: 0; + padding: var(--spacing-1-5) var(--spacing-2); + font-family: var(--font-family-code); + font-size: var(--font-size-sm); + line-height: 1.5; + color: var(--color-text-secondary); + background: var(--color-background-muted); + border-radius: var(--radius-inner); + overflow-x: auto; + white-space: pre; +} + +.chain-of-thought-rail__pre::before { + content: attr(data-label); + display: block; + margin-block-end: var(--spacing-0-5); + font-size: 11px; + letter-spacing: 0.06em; + text-transform: uppercase; + color: var(--color-text-disabled); + user-select: none; +} + +.chain-of-thought-rail__pre[data-tone="error"] { + color: var(--color-danger); +} + +.chain-of-thought-rail__pre + .chain-of-thought-rail__pre { + margin-block-start: var(--spacing-1); +} + +/* Scoped scrollbar for payload blocks only (never the page). */ +.chain-of-thought-rail__pre::-webkit-scrollbar { + height: 8px; +} + +.chain-of-thought-rail__pre::-webkit-scrollbar-thumb { + background: var(--color-border-emphasized); + border-radius: 4px; +} + +@media (prefers-reduced-motion: reduce) { + .chain-of-thought-rail__trigger-chevron, + .chain-of-thought-rail__disclosure { + transition: none; + } + + .chain-of-thought-rail__marker { + animation: none !important; + } +}