feat(chat): show provider thinking traces #11784
Mergedmaria-rcksmerged 21 commits into
main fromrefs/pull/11784/headmerged by maria-rcks View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/mobile/src/features/threads/ThreadFeed.tsx line 2653OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2602,3 +2648,6 @@26022648 [suspendEndScrollMaintenanceForDisclosure],26032649 );260426502651+ const onToggleReasoning = useCallback(2652+ (messageId: string) => {2653+ suspendEndScrollMaintenanceForDisclosure(`reasoning:${messageId}`);apps/mobile/src/features/threads/ThreadFeed.tsx line 1488Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1469,3 +1476,11 @@1469147614701477 if (entry.type === "message") {14711478 const { message } = entry;1479+ if (message.role === "reasoning") {1480+ // An empty trace would render as a bare label; the live "Thinking" row1481+ // already covers that window.1482+ if (message.text.trim().length === 0) {1483+ return null;1484+ }1485+ return (1486+ <ThreadReasoningRowapps/web/src/components/chat/MessagesTimeline.tsx line 648OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -635,3 +643,6 @@635643 },636644 [expandedWorkGroupIds, suspendEndScrollMaintenanceForDisclosure],637645 );646+ const onToggleReasoning = useCallback(647+ (messageId: string, expanded: boolean) => {648+ suspendEndScrollMaintenanceForDisclosure(`reasoning:${messageId}`, !expanded);apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 1930OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1760,6 +1914,17 @@17601914 event.type === "content.delta" && event.payload.streamKind === "assistant_text"17611915 ? event.payload.delta17621916 : undefined;1917+ const reasoningDelta =1918+ event.type === "content.delta" &&1919+ (event.payload.streamKind === "reasoning_text" ||1920+ event.payload.streamKind === "reasoning_summary_text")1921+ ? {1922+ streamKind: event.payload.streamKind,1923+ delta: event.payload.delta,1924+ summaryIndex: event.payload.summaryIndex,1925+ }1926+ : undefined;17631927 const proposedPlanDelta =17641928 event.type === "turn.proposed.delta" ? event.payload.delta : undefined;176519291930+ if (reasoningDelta && reasoningDelta.delta.length > 0) {apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2157OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1858,0 +2137,21 @@2137+2138+ if (event.type === "item.completed" && event.payload.itemType === "reasoning") {2139+ const turnId = toTurnId(event.turnId);2140+ if (turnId) {2141+ const activeReasoningMessageId = yield* getActiveAssistantMessageIdForTurn(2142+ thread.id,2143+ turnId,2144+ "reasoning",2145+ );2146+ // The item detail is a whole-block snapshot, so it may only stand in2147+ // for deltas that never arrived. Appending it to a streamed block2148+ // would print the reasoning twice.2149+ const existingReasoningMessage = Option.isSome(activeReasoningMessageId)2150+ ? yield* getThreadMessageById(thread.id, activeReasoningMessageId.value)2151+ : undefined;2152+ const fallbackText =2153+ event.payload.detail !== undefined &&2154+ (existingReasoningMessage === undefined || existingReasoningMessage.text.length === 0)2155+ ? event.payload.detail2156+ : undefined;2157+ yield* finalizeActiveSegmentForTurn({
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts lines 1291 to 1296Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1170,0 +1264,21 @@1264+ if (Option.getOrUndefined(state)?.baseKey === input.baseKey) {1265+ return activeMessageId.value;1266+ }1267+ yield* finalizeActiveSegmentForTurn({1268+ event: input.event,1269+ threadId: input.threadId,1270+ turnId: input.turnId,1271+ createdAt: input.createdAt,1272+ commandTag: "reasoning-complete-on-new-block",1273+ finalDeltaCommandTag: "reasoning-delta-finalize-on-new-block",1274+ hasProjectedMessage: false,1275+ role: "reasoning",1276+ });1277+ }1278+1279+ return yield* startAssistantSegmentForTurn({1280+ threadId: input.threadId,1281+ turnId: input.turnId,1282+ baseKey: input.baseKey,1283+ role: "reasoning",1284+ });apps/web/src/components/chat/MessagesTimeline.tsx line 648OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -635,3 +643,6 @@635643 },636644 [expandedWorkGroupIds, suspendEndScrollMaintenanceForDisclosure],637645 );646+ const onToggleReasoning = useCallback(647+ (messageId: string, expanded: boolean) => {648+ suspendEndScrollMaintenanceForDisclosure(`reasoning:${messageId}`, !expanded);apps/web/src/components/chat/MessagesTimeline.tsx lines 2186 to 2195OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2128,0 +2175,21 @@2175+ }, [expanded, message.id, onToggleReasoning]);2176+ const elapsed = streaming ? null : formatWorkingTimer(message.createdAt, message.updatedAt);2177+ const label = streaming ? "Thinking" : elapsed ? `Thought for ${elapsed}` : "Thought";2178+2179+ return (2180+ <div2181+ className={cn(2182+ "flex flex-col rounded-md px-0.5 py-0.5 transition-colors",2183+ expanded && "mb-1",2184+ "cursor-pointer hover:bg-accent/20 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70",2185+ )}2186+ role="button"2187+ tabIndex={0}2188+ aria-expanded={expanded}2189+ onClick={toggle}2190+ onKeyDown={(event: KeyboardEvent<HTMLDivElement>) => {2191+ if (event.key === "Enter" || event.key === " ") {2192+ event.preventDefault();2193+ toggle();2194+ }2195+ }}
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts lines 2155 to 2161OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1858,0 +2141,21 @@2141+ // The item detail is a whole-block snapshot, so it may only stand in2142+ // for deltas that never arrived. Appending it to a streamed block2143+ // would print the reasoning twice.2144+ const existingReasoningMessage = Option.isSome(activeReasoningMessageId)2145+ ? yield* getThreadMessageById(thread.id, activeReasoningMessageId.value)2146+ : undefined;2147+ const fallbackText =2148+ event.payload.detail !== undefined &&2149+ (existingReasoningMessage === undefined || existingReasoningMessage.text.length === 0)2150+ ? event.payload.detail2151+ : undefined;2152+2153+ // A provider can report a whole block at once without streaming it.2154+ // Open a segment so the snapshot has somewhere to land.2155+ if (Option.isNone(activeReasoningMessageId) && fallbackText !== undefined) {2156+ yield* startAssistantSegmentForTurn({2157+ threadId: thread.id,2158+ turnId,2159+ baseKey: reasoningSegmentBaseKeyFromEvent(event, "reasoning_text"),2160+ role: "reasoning",2161+ });
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts line 2164Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1858,0 +2131,21 @@2131+ if (event.type === "item.completed" && event.payload.itemType === "reasoning") {2132+ const turnId = toTurnId(event.turnId);2133+ if (turnId) {2134+ const activeReasoningMessageId = yield* getActiveAssistantMessageIdForTurn(2135+ thread.id,2136+ turnId,2137+ "reasoning",2138+ );2139+ // The item detail is a whole-block snapshot, so it may only stand in2140+ // for deltas that never arrived. Appending it to a streamed block2141+ // would print the reasoning twice.2142+ const existingReasoningMessage = Option.isSome(activeReasoningMessageId)2143+ ? yield* getThreadMessageById(thread.id, activeReasoningMessageId.value)2144+ : undefined;2145+ const fallbackText =2146+ event.payload.detail !== undefined &&2147+ (existingReasoningMessage === undefined || existingReasoningMessage.text.length === 0)2148+ ? event.payload.detail2149+ : undefined;2150+2151+ if (Option.isNone(activeReasoningMessageId)) {
- apps/web/src/components/chat/MessagesTimeline.logic.ts lines 634 to 636Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -609,6 +611,11 @@609611 pendingUserBoundary = entry.message.createdAt;610612 continue;611613 }614+ // Thinking is work, so it folds with the rest of it. A provider that615+ // interleaves a block with every tool call would otherwise leave dozens of616+ // "Thought for ..." rows standing beside the "Worked for ..." summary.617+ // Nothing folds while the turn is live, which is when traces are watched.612618 const turnId =613- entry.kind === "message" && entry.message.role === "assistant"619+ entry.kind === "message" &&620+ (entry.message.role === "assistant" || entry.message.role === "reasoning")614621 ? (entry.message.turnId ?? null)
- apps/web/src/components/chat/MessagesTimeline.logic.ts lines 634 to 635Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -609,5 +611,10 @@609611 pendingUserBoundary = entry.message.createdAt;610612 continue;611613 }614+ // Thinking is work, so it folds with the rest of it. A provider that615+ // interleaves a block with every tool call would otherwise leave dozens of616+ // "Thought for ..." rows standing beside the "Worked for ..." summary.617+ // Nothing folds while the turn is live, which is when traces are watched.612618 const turnId =613- entry.kind === "message" && entry.message.role === "assistant"619+ entry.kind === "message" &&620+ (entry.message.role === "assistant" || entry.message.role === "reasoning")
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/provider/Layers/ClaudeAdapter.ts line 3371OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -3290,3 +3369,3 @@32903369 context.turnState.compactedSinceLatestAssistantUsage = false;32913370 }32923371 yield* backfillAssistantTextBlocksFromSnapshot(context, message);apps/server/src/provider/Layers/ClaudeAdapter.ts line 2337OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2271,0 +2317,21 @@2317+ turnId: turnState.turnId,2318+ payload: {2319+ streamKind: "reasoning_summary_text",2320+ delta: input.delta,2321+ ...(input.contentIndex !== undefined ? { contentIndex: input.contentIndex } : {}),2322+ },2323+ providerRefs: nativeProviderRefs(context),2324+ raw: {2325+ source: "claude.sdk.message",2326+ method: input.rawMethod,2327+ payload: input.rawPayload,2328+ },2329+ });2330+ });2331+2332+ const backfillThinkingFromSnapshot = Effect.fn("backfillThinkingFromSnapshot")(function* (2333+ context: ClaudeSessionContext,2334+ message: SDKMessage,2335+ ) {2336+ const turnState = context.turnState;2337+ if (!turnState || turnState.emittedThinkingText) {
- @@ -1778,2 +1989,21 @@1989+ reasoningMode,1990+ yield* Clock.currentTimeMillis,1991+ );1992+ if (spillChunk.length > 0) {1993+ yield* orchestrationEngine.dispatch({1994+ type: "thread.message.reasoning.delta",1995+ commandId: yield* providerCommandId(event, "reasoning-delta-buffer-spill"),1996+ threadId: thread.id,1997+ messageId: reasoningMessageId,1998+ delta: spillChunk,1999+ turnId,2000+ createdAt: yield* reasoningStartedAt(reasoningMessageId, now),2001+ });2002+ }2003+ }2004+17782005 if (assistantDelta && assistantDelta.length > 0) {17792006 const turnId = toTurnId(event.turnId);2007+ // Visible text ends the thinking block that preceded it, so the next2008+ // block does not swallow this answer.2009+ if (turnId) {
- apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts lines 2173 to 2173Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1870,0 +2152,21 @@2152+ // for deltas that never arrived. Appending it to a streamed block2153+ // would print the reasoning twice.2154+ const existingReasoningMessage = Option.isSome(activeReasoningMessageId)2155+ ? yield* getThreadMessageById(thread.id, activeReasoningMessageId.value)2156+ : undefined;2157+ const fallbackText =2158+ event.payload.detail !== undefined &&2159+ (existingReasoningMessage === undefined || existingReasoningMessage.text.length === 0)2160+ ? event.payload.detail2161+ : undefined;2162+2163+ if (Option.isNone(activeReasoningMessageId)) {2164+ // Segment state outlives a closed block, so its presence means this2165+ // turn already streamed a trace and the snapshot would duplicate it.2166+ const turnAlreadyStreamedReasoning = Option.isSome(2167+ yield* getAssistantSegmentStateForTurn(thread.id, turnId, "reasoning"),2168+ );2169+ // A provider can report a whole block at once without streaming it.2170+ // The id is derived from the item rather than the segment counter so2171+ // a repeated completion rewrites that row instead of adding a copy.2172+ if (fallbackText !== undefined && !turnAlreadyStreamedReasoning) {
- @@ -2365,0 +2427,21 @@2427+ message.turnId === unsettledTurnId;2428+ const expanded = ctx.expandedReasoningMessageIds.has(message.id);2429+ const { onToggleReasoning } = ctx;2430+ const toggle = useCallback(() => {2431+ onToggleReasoning(message.id, !expanded);2432+ }, [expanded, message.id, onToggleReasoning]);2433+ const label = streaming ? "Thinking" : "Thought";2434+2435+ if (message.text.trim().length === 0 && !streaming) {2436+ return null;2437+ }2438+2439+ return (2440+ <div className={cn("flex flex-col", expanded && "mb-1")}>2441+ <button2442+ type="button"2443+ aria-expanded={expanded}2444+ onClick={toggle}2445+ className="flex cursor-pointer select-none items-center gap-1.5 rounded-md px-0.5 py-0.5 text-start transition-colors hover:bg-accent/20 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"2446+ >2447+ <span className="flex size-6 shrink-0 items-center justify-center text-icon-muted">
- apps/web/src/components/chat/MessagesTimeline.tsx line 2441Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2365,0 +2421,21 @@2421+ // its completion. Only the live turn may claim to still be thinking, so a2422+ // settled turn cannot shimmer "Thinking" at the user forever.2423+ const streaming =2424+ Boolean(message.streaming) &&2425+ isWorking &&2426+ message.turnId !== null &&2427+ message.turnId === unsettledTurnId;2428+ const expanded = ctx.expandedReasoningMessageIds.has(message.id);2429+ const { onToggleReasoning } = ctx;2430+ const toggle = useCallback(() => {2431+ onToggleReasoning(message.id, !expanded);2432+ }, [expanded, message.id, onToggleReasoning]);2433+ const label = streaming ? "Thinking" : "Thought";2434+2435+ if (message.text.trim().length === 0 && !streaming) {2436+ return null;2437+ }2438+2439+ return (2440+ <div className={cn("flex flex-col", expanded && "mb-1")}>2441+ <button
maria-rcks merged this052c7ae
maria-rcks closed this
Sign in to join this conversation.








macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed