From e1977bbe639b5517f770b6a1a8e48b3b184dbdf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erman=20HAVU=C3=87?= Date: Sat, 16 May 2026 17:20:16 +0300 Subject: [PATCH] feat(ui): collapsible thinking blocks with merged per-turn view and user toggle (#1273) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add collapsible reasoning traces with animated labels * feat(ui): redesign reasoning blocks with merged collapsible Thought view - Replace per-part reasoning blocks with a single merged block per turn (VSCode Copilot pattern), controlled by new `groupReasoningBlocks` store flag - `ReasoningTimelineBlock` redesigned: chevron toggle, summary preview on collapsed header, 'Thinking'/'Justification' label when expanded, BusyDots while streaming, auto-scroll to bottom during live streaming - Short texts (< 120 chars) render inline without a toggle - Summary now strips markdown and truncates at a word boundary with ellipsis - New `MergedReasoningPart` component merges all reasoning parts for a message into one block at the position of the first reasoning part - `defaultExpanded` prop lets callers override initial expand state - Remove `.thinking-dot` CSS animation (replaced by BusyDots component) - Fix reasoning markdown font-size: use `--text-markdown` instead of `--text-meta` * refactor(ui): scope working phrases inside useAssistantStatus and simplify reasoning status - Move WORKING_PHRASES array and getRandomWorkingPhrase() inside the hook so they are no longer exported (were only consumed by ReasoningPart which no longer needs them) - Change the 'reasoning' activity status text from a random working phrase to the deterministic string 'thinking' — matches the new UI label * test(ui): expand ReasoningPart tests for new collapsible and summary behavior - Update baseline test to use text long enough to trigger the collapsible path (short texts now render inline) and assert on the correct aria markup - Add test for 'Justification' label when pre-expanded via defaultExpanded - Add test for 'Thinking' label for the thinking variant when expanded - Add test verifying summary is a word-boundary-truncated excerpt ending with an ellipsis character * i18n: rename 'Reasoning Traces' to 'Thinking Blocks' and add thought key - Rename settings label from 'Show Reasoning Traces' → 'Show Thinking Blocks' across all supported locales (en, es, ko, pl, pt-BR, uk, zh-CN) - Add `chat.reasoningTrace.thought` key to all locales (used by merged reasoning block header in completed state) * feat(ui): add collapsibleThinkingBlocks setting with full persistence wiring - New boolean store field `collapsibleThinkingBlocks` (default true) with `setCollapsibleThinkingBlocks` action; persisted to localStorage - Threaded through DesktopSettings, SettingsPayload (API types), desktop persistence (sanitize + apply), web appearance persistence, appearance auto-save watcher, and server-side settings-helpers sanitize/format - Server defaults to true when the field is absent in formatSettingsResponse - MessageBody reads the flag: false → render reasoning as plain AssistantTextPart; true → existing collapsible/merged block path * feat(settings): expose Collapsible Reasoning Blocks toggle in visual settings Add a checkbox under the 'Show Thinking Blocks' row (visible only when showReasoningTraces is enabled) that toggles the collapsibleThinkingBlocks preference. Follows the existing toggle pattern: div role=button, keyboard handler for Enter/Space, Checkbox primitive, aria-pressed attribute. * i18n: revert showReasoningTraces label rename and add collapsibleThinkingBlocks strings - Revert 'Show Reasoning Traces' → 'Show Thinking Blocks' rename (the collapsibleThinkingBlocks toggle is now a separate control, so the parent label stays as 'Reasoning Traces' for clarity) - Add `collapsibleThinkingBlocks` / `collapsibleThinkingBlocksAria` strings across all seven supported locales (en, es, ko, pl, pt-BR, uk, zh-CN) * test(server): add settings-helpers coverage for collapsibleThinkingBlocks - Verify sanitizeSettingsUpdate accepts boolean true/false and rejects non-boolean values (string, number) - Verify formatSettingsResponse forwards the value correctly for both true and false, and defaults to true when the field is absent * fix(ui): respect defaultExpanded prop and remove dead alwaysShowActions from ReasoningTimelineBlock The useEffect on [isStreaming] was firing on mount and immediately calling setIsExpanded(false) (since isStreaming is false for completed blocks), overriding any defaultExpanded={true} passed by callers. The fix uses a prevIsStreamingRef so the effect only collapses the block on a true→false transition and is a no-op on initial mount. Also removes alwaysShowActions from ReasoningTimelineBlockProps — the new header design always shows the chevron, making the prop obsolete. The prop was already absent from the component destructuring (a dead type entry) and was silently ignored at runtime. Removed it from ReasoningPartProps, MergedReasoningPartProps, and the two call-sites in MessageBody as well. * chore: remove unused reasoningpresentation module and test * fix(ui): polish collapsible reasoning block UI --------- Co-authored-by: Bohdan Triapitsyn --- .../components/chat/message/MessageBody.tsx | 54 ++- .../chat/message/parts/ReasoningPart.test.tsx | 98 ++++++ .../chat/message/parts/ReasoningPart.tsx | 315 +++++++++++++----- .../openchamber/OpenChamberVisualSettings.tsx | 25 ++ .../src/components/ui/ScrollableOverlay.tsx | 3 + packages/ui/src/index.css | 2 +- packages/ui/src/lib/api/types.ts | 1 + packages/ui/src/lib/appearanceAutoSave.ts | 6 + packages/ui/src/lib/appearancePersistence.ts | 11 + packages/ui/src/lib/desktop.ts | 1 + .../ui/src/lib/i18n/messages/en.settings.ts | 2 + packages/ui/src/lib/i18n/messages/en.ts | 6 + .../ui/src/lib/i18n/messages/es.settings.ts | 2 + packages/ui/src/lib/i18n/messages/es.ts | 6 + .../ui/src/lib/i18n/messages/ko.settings.ts | 2 + packages/ui/src/lib/i18n/messages/ko.ts | 6 + .../ui/src/lib/i18n/messages/pl.settings.ts | 2 + packages/ui/src/lib/i18n/messages/pl.ts | 6 + .../src/lib/i18n/messages/pt-BR.settings.ts | 2 + packages/ui/src/lib/i18n/messages/pt-BR.ts | 6 + .../ui/src/lib/i18n/messages/uk.settings.ts | 2 + packages/ui/src/lib/i18n/messages/uk.ts | 6 + .../src/lib/i18n/messages/zh-CN.settings.ts | 2 + packages/ui/src/lib/i18n/messages/zh-CN.ts | 6 + packages/ui/src/lib/persistence.ts | 6 + packages/ui/src/stores/useUIStore.ts | 10 + .../server/lib/opencode/settings-helpers.js | 11 +- .../lib/opencode/settings-helpers.test.js | 35 ++ 28 files changed, 544 insertions(+), 90 deletions(-) create mode 100644 packages/ui/src/components/chat/message/parts/ReasoningPart.test.tsx diff --git a/packages/ui/src/components/chat/message/MessageBody.tsx b/packages/ui/src/components/chat/message/MessageBody.tsx index b7079018..67d220d7 100644 --- a/packages/ui/src/components/chat/message/MessageBody.tsx +++ b/packages/ui/src/components/chat/message/MessageBody.tsx @@ -4,7 +4,7 @@ import type { Part } from '@opencode-ai/sdk/v2'; import UserTextPart from './parts/UserTextPart'; import ToolPart from './parts/ToolPart'; import AssistantTextPart from './parts/AssistantTextPart'; -import ReasoningPart from './parts/ReasoningPart'; +import ReasoningPart, { MergedReasoningPart } from './parts/ReasoningPart'; import { MessageFilesDisplay } from '../FileAttachment'; import { TurnChangedFilesDropdown } from '../TurnChangedFilesDropdown'; import type { ToolPart as ToolPartType } from '@opencode-ai/sdk/v2'; @@ -46,6 +46,7 @@ import { useSessions } from '@/sync/sync-context'; import { useI18n } from '@/lib/i18n'; import { extractLoopbackUrls } from '@/lib/url'; + const CONTAIN_LAYOUT_STYLE = { contain: 'layout' as const, transform: 'translateZ(0)' }; const MESSAGE_FOOTER_CONTAINER_STYLE = { containerType: 'inline-size' as const, containerName: 'message-footer' }; const INLINE_MESSAGE_ACTIONS_CLASS_NAME = 'mt-2 mb-1 flex items-center justify-start gap-1.5'; @@ -1015,6 +1016,8 @@ const AssistantMessageBody = React.memo(({ const [isPlanDialogOpen, setIsPlanDialogOpen] = React.useState(false); const [isSavingPlan, setIsSavingPlan] = React.useState(false); const chatRenderMode = useUIStore((state) => state.chatRenderMode); + const collapsibleThinkingBlocks = useUIStore((state) => state.collapsibleThinkingBlocks); + const groupReasoningBlocks = useUIStore((state) => state.groupReasoningBlocks); const showSplitAssistantMessageActions = useUIStore((state) => state.showSplitAssistantMessageActions); const isSortedRenderMode = chatRenderMode === 'sorted'; const isMiniChatSurface = chatSurfaceMode === 'mini-chat'; @@ -1506,7 +1509,16 @@ const AssistantMessageBody = React.memo(({ // Flat rendering: iterate parts in natural order. // Group consecutive static tools (read, grep, glob, etc.) into compact rows. // Expandable tools (bash, edit, task) get individual rows. - // Text and reasoning render inline at their natural position. + // Text renders inline at its natural position. + // Reasoning: all reasoning parts for this message are merged into ONE block + // at the position of the first reasoning part (VSCode Copilot pattern). + const flatReasoningParts = visibleParts.filter((p) => { + if (p.type !== 'reasoning') return false; + const a = activityByPart.get(p); + return a?.kind !== 'reasoning'; + }); + let reasoningMergeRendered = false; + let i = 0; while (i < visibleParts.length) { const part = visibleParts[i]; @@ -1553,17 +1565,8 @@ const AssistantMessageBody = React.memo(({ continue; } if (showReasoningTraces) { - if (isSortedRenderMode) { - rendered.push( - - ); - } else { + if (!collapsibleThinkingBlocks) { + // Non-collapsible mode: render thinking blocks as plain text inline. rendered.push( ); + } else if (groupReasoningBlocks) { + // Merged mode (VSCode pattern): one block for all reasoning parts. + if (!reasoningMergeRendered) { + reasoningMergeRendered = true; + rendered.push( + + ); + } + } else { + // Per-part mode: each reasoning block at its natural position. + rendered.push( + + ); } } i++; @@ -1661,6 +1687,8 @@ const AssistantMessageBody = React.memo(({ animatedToolIdsLookup, animateActivityRows, chatRenderMode, + collapsibleThinkingBlocks, + groupReasoningBlocks, collapsedPreviewCount, expandedTools, isMobile, diff --git a/packages/ui/src/components/chat/message/parts/ReasoningPart.test.tsx b/packages/ui/src/components/chat/message/parts/ReasoningPart.test.tsx new file mode 100644 index 00000000..8485d181 --- /dev/null +++ b/packages/ui/src/components/chat/message/parts/ReasoningPart.test.tsx @@ -0,0 +1,98 @@ +import React from 'react'; +import { describe, expect, test } from 'bun:test'; +import { renderToStaticMarkup } from 'react-dom/server'; + +import { I18nProvider } from '@/lib/i18n'; +import { ReasoningTimelineBlock } from './ReasoningPart'; + +// A reasoning text whose summary (first 120 chars) fits in the header but +// whose expanded body content should only appear when the disclosure is open. +const LONG_REASONING = + 'First thought about the task at hand and how to approach it carefully.\n' + + 'This second line goes into much deeper detail about the internal reasoning ' + + 'process that should remain hidden in the collapsed header view.'; + +// A long text that should render the collapsible header with a label +const LONG_JUSTIFICATION = + 'Sorting by activity first because the active session needs immediate attention.\n' + + 'Secondary sort by last updated timestamp ensures a stable deterministic ordering ' + + 'when multiple sessions have the same activity state.'; + +describe('ReasoningTimelineBlock', () => { + test('renders reasoning traces behind an accessible collapsed disclosure by default', () => { + const markup = renderToStaticMarkup( + + + , + ); + + // Accessible toggle row is rendered + expect(markup).toContain('role="button"'); + expect(markup).toContain('aria-expanded="false"'); + expect(markup).toContain('aria-label="Expand reasoning trace"'); + + // Summary preview (beginning of text) is visible in the header + expect(markup).toContain('First thought'); + + // Expanded body (MarkdownRenderer) is NOT rendered while collapsed + expect(markup).not.toContain('data-message-text-export-source'); + }); + + test('renders "Justification" label for justification variant when pre-expanded and not streaming', () => { + const markup = renderToStaticMarkup( + + + , + ); + + // Label shown in expanded header should be "Justification" not "Thinking" + expect(markup).toContain('Justification'); + expect(markup).not.toContain('Thinking'); + }); + + test('renders "Thinking" label for thinking variant when pre-expanded and not streaming', () => { + const markup = renderToStaticMarkup( + + + , + ); + + // Label shown in expanded header should be "Thinking" + expect(markup).toContain('Thinking'); + }); + + test('header summary is a truncated excerpt from the beginning', () => { + const markup = renderToStaticMarkup( + + + , + ); + + // Deep body content beyond 120 chars should be cut from the summary span + expect(markup).not.toContain('remain hidden in the collapsed header view'); + // The ellipsis character marks that the text was truncated + expect(markup).toContain('…'); + }); +}); diff --git a/packages/ui/src/components/chat/message/parts/ReasoningPart.tsx b/packages/ui/src/components/chat/message/parts/ReasoningPart.tsx index 60151390..6db40dc4 100644 --- a/packages/ui/src/components/chat/message/parts/ReasoningPart.tsx +++ b/packages/ui/src/components/chat/message/parts/ReasoningPart.tsx @@ -3,10 +3,10 @@ import type { Part } from '@opencode-ai/sdk/v2'; import { cn } from '@/lib/utils'; import type { ContentChangeReason } from '@/hooks/useChatAutoFollow'; import { ScrollableOverlay } from '@/components/ui/ScrollableOverlay'; -import { Icon } from "@/components/icon/Icon"; -import type { IconName } from "@/components/icon/icons"; +import { Icon } from '@/components/icon/Icon'; +import { BusyDots } from './BusyDots'; +import { useI18n } from '@/lib/i18n'; import { useUIStore } from '@/stores/useUIStore'; -import { useDurationTickerNow } from './useDurationTicker'; import { MarkdownRenderer } from '../../MarkdownRenderer'; import { useStreamingTextThrottle } from '../../hooks/useStreamingTextThrottle'; @@ -14,14 +14,6 @@ type PartWithText = Part & { text?: string; content?: string; time?: { start?: n export type ReasoningVariant = 'thinking' | 'justification'; -const variantConfig: Record< - ReasoningVariant, - { label: string; Icon: IconName } -> = { - thinking: { label: 'Thinking', Icon: 'brain-ai-3' }, - justification: { label: 'Justification', Icon: 'chat-ai-3' }, -}; - const cleanReasoningText = (text: string): string => { if (typeof text !== 'string' || text.trim().length === 0) { return ''; @@ -35,39 +27,46 @@ const cleanReasoningText = (text: string): string => { .trim(); }; +const SUMMARY_MAX_CHARS = 80; +const INLINE_THRESHOLD = 120; + +/** Strip common markdown syntax so the header preview reads as plain text. */ +const stripMarkdown = (text: string): string => + text + // Fenced code blocks → keep inner text on one line + .replace(/```[\w]*\n?([\s\S]*?)```/g, (_, inner: string) => inner.trim()) + // Inline code + .replace(/`([^`]+)`/g, '$1') + // Bold + italic (*** / __) + .replace(/\*{1,3}([^*]+)\*{1,3}/g, '$1') + .replace(/_{1,3}([^_]+)_{1,3}/g, '$1') + // Headings (# ## ###) + .replace(/^#{1,6}\s+/gm, '') + // Links [label](url) → label + .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1') + // Blockquote markers + .replace(/^>\s?/gm, '') + // Horizontal rules + .replace(/^[-*_]{3,}\s*$/gm, '') + // Remaining leading/trailing punctuation from stripped markers + .trim(); + const getReasoningSummary = (text: string): string => { if (!text) { return ''; } - const trimmed = text.trim(); - const newlineIndex = trimmed.indexOf('\n'); - const periodIndex = trimmed.indexOf('.'); + // Strip markdown, then collapse all whitespace runs into single spaces. + const flat = stripMarkdown(text).replace(/\s+/g, ' ').trim(); - const cutoffCandidates = [ - newlineIndex >= 0 ? newlineIndex : Infinity, - periodIndex >= 0 ? periodIndex : Infinity, - ]; - const cutoff = Math.min(...cutoffCandidates); - - if (!Number.isFinite(cutoff)) { - return trimmed; + if (flat.length <= SUMMARY_MAX_CHARS) { + return flat; } - return trimmed.substring(0, cutoff).trim(); -}; - -const formatDuration = (start: number, end?: number, now: number = Date.now()): string => { - const duration = end ? end - start : now - start; - const seconds = duration / 1000; - const displaySeconds = seconds < 0.05 && end !== undefined ? 0.1 : seconds; - return `${displaySeconds.toFixed(1)}s`; -}; - -const LiveDuration: React.FC<{ start: number; end?: number; active: boolean }> = ({ start, end, active }) => { - const now = useDurationTickerNow(active, 250); - - return <>{formatDuration(start, end, now)}; + // Cut at a word boundary before the limit, then append ellipsis. + const cut = flat.lastIndexOf(' ', SUMMARY_MAX_CHARS); + const end = cut > 0 ? cut : SUMMARY_MAX_CHARS; + return `${flat.substring(0, end).trimEnd()}…`; }; type ReasoningTimelineBlockProps = { @@ -79,7 +78,8 @@ type ReasoningTimelineBlockProps = { showDuration?: boolean; isStreaming?: boolean; actions?: React.ReactNode; - alwaysShowActions?: boolean; + /** Override the initial expanded state. Defaults to `isStreaming`. */ + defaultExpanded?: boolean; }; export const ReasoningTimelineBlock: React.FC = ({ @@ -87,18 +87,44 @@ export const ReasoningTimelineBlock: React.FC = ({ variant, onContentChange, blockId, - time, - showDuration = true, isStreaming = false, actions, - alwaysShowActions = false, + defaultExpanded, }) => { - const [isExpanded, setIsExpanded] = React.useState(false); + const { t } = useI18n(); + const [isExpanded, setIsExpanded] = React.useState(defaultExpanded ?? isStreaming); + const contentId = React.useId(); + const scrollRef = React.useRef(null); + // Track previous isStreaming so the effect only collapses on true→false + // transitions and does NOT override defaultExpanded on initial mount. + const prevIsStreamingRef = React.useRef(isStreaming); const summary = React.useMemo(() => getReasoningSummary(text), [text]); - const { label, Icon: iconName } = variantConfig[variant]; - const timeStart = typeof time?.start === 'number' && Number.isFinite(time.start) ? time.start : undefined; - const timeEnd = typeof time?.end === 'number' && Number.isFinite(time.end) ? time.end : undefined; + const toggleAriaLabel = isExpanded + ? t('chat.reasoningTrace.collapseAria') + : t('chat.reasoningTrace.expandAria'); + + const handleToggle = React.useCallback(() => { + setIsExpanded((prev) => !prev); + onContentChange?.('structural'); + }, [onContentChange]); + + const handleKeyDown = React.useCallback((event: React.KeyboardEvent) => { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + handleToggle(); + } + }, [handleToggle]); + + React.useEffect(() => { + const wasStreaming = prevIsStreamingRef.current; + prevIsStreamingRef.current = isStreaming; + // Auto-collapse only when streaming ends (true → false). + // Do not fire on mount so that defaultExpanded is respected. + if (wasStreaming && !isStreaming) { + setIsExpanded(false); + } + }, [isStreaming]); React.useEffect(() => { if (text.trim().length === 0) { @@ -107,70 +133,136 @@ export const ReasoningTimelineBlock: React.FC = ({ onContentChange?.('structural'); }, [onContentChange, text]); + React.useEffect(() => { + if (isStreaming && isExpanded && scrollRef.current) { + scrollRef.current.scrollTop = scrollRef.current.scrollHeight; + } + }, [text, isStreaming, isExpanded]); + if (!text || text.trim().length === 0) { return null; } + const isShort = !isStreaming && text.trim().length < INLINE_THRESHOLD; + + // Short blocks: render content directly without a collapsible toggle. + if (isShort) { + return ( +
+
+ +
+ {actions ? ( +
+
+ {actions} +
+
+ ) : null} +
+ ); + } + return (
setIsExpanded((prev) => !prev)} + onClick={handleToggle} + onKeyDown={handleKeyDown} > -
-
+
+
- +
{isExpanded ? : }
- {label} + + {isStreaming ? ( + + {t('chat.reasoningTrace.reasoning')} + + + ) : isExpanded ? ( + + {t(variant === 'justification' ? 'chat.reasoningTrace.justification' : 'chat.reasoningTrace.thinking')} + + ) : ( + + {t(variant === 'justification' ? 'chat.reasoningTrace.justification' : 'chat.reasoningTrace.thinking')} + + )}
- {(summary || (showDuration && typeof timeStart === 'number')) ? ( -
- {summary ? {summary} : null} - {showDuration && typeof timeStart === 'number' ? ( - - - - - - ) : null} -
- ) : null} +
+ {!isStreaming && !isExpanded && summary ? ( + + {summary} + + ) : ( + + )} +
+ {/* Expanded content — left border matching ToolPart */} {isExpanded && (
+