@@ -27,7 +27,6 @@ import type {
2727 AssistantMessage,
2828 Message,
2929 TextContent,
30- ThinkingContent,
3130 ThinkingLevel,
3231 ToolCall,
3332 UserMessage,
@@ -108,11 +107,8 @@ let dividerTick = 0;
108107 /** Divider animation timer handle. */
109108 let dividerTimer: ReturnType<typeof setInterval> | null = null;
110109
111-/** Streaming text buffer for the current assistant response. */
112-let streamingText = "";
113-
114-/** Streaming thinking buffer for the current assistant response. */
115-let streamingThinking = "";
110+/** Streaming assistant content for the current response. */
111+let streamingContent: AssistantMessage["content"] = [];
116112
117113 /** Whether we are currently streaming a response. */
118114 let isStreaming = false;
@@ -139,7 +135,7 @@ export interface PendingToolCall {
139135 done: boolean;
140136 }
141137
142-/** Render options shared by completed and streaming assistant content. */
138+/** Render options shared by completed and in-progress assistant content. */
143139 export interface ConversationRenderOpts {
144140 /** Whether reasoning blocks are visible. */
145141 showReasoning: boolean;
@@ -149,14 +145,14 @@ export interface ConversationRenderOpts {
149145 theme: Theme;
150146 }
151147
152-/** Display-only assistant content that is still streaming. */
153-export interface StreamingRenderState {
154- /** Streamed markdown text so far. */
155- text: string;
156- /** Streamed thinking text so far. */
157- thinking: string;
148+/** Display-only assistant state used while a response is still streaming. */
149+interface AssistantRenderState {
150+ /** Assistant content blocks accumulated so far. */
151+ content: AssistantMessage["content"];
158152 /** Tool calls observed in the current streaming turn. */
159- pendingToolCalls: PendingToolCall[];
153+ pendingToolCalls?: PendingToolCall[];
154+ /** Optional error text appended below the assistant content. */
155+ errorMessage?: string;
160156 }
161157
162158 /** Semantic line kinds used when rendering tool output in the log. */
@@ -211,8 +207,7 @@ export function resetUiState(): void {
211207 inputFocused = true;
212208 dividerTick = 0;
213209 stopDividerAnimation();
214- streamingText = "";
215- streamingThinking = "";
210+ streamingContent = [];
216211 isStreaming = false;
217212 pendingToolCalls = [];
218213 activeOverlay = null;
@@ -486,36 +481,64 @@ function renderMarkdownContent(content: string): Node[] {
486481 });
487482 }
488483
489-/** Render a completed assistant message from history. */
484+/** Render a completed or in-progress assistant response. */
490485 export function renderAssistantMessage(
491- msg: AssistantMessage,
486+ assistant: AssistantMessage | AssistantRenderState,
492487 opts: ConversationRenderOpts,
493488 ): Node | null {
489+ const errorMessage =
490+ "role" in assistant
491+ ? assistant.stopReason === "error"
492+ ? assistant.errorMessage
493+ : undefined
494+ : assistant.errorMessage;
495+ const pendingToolCalls =
496+ "role" in assistant ? [] : (assistant.pendingToolCalls ?? []);
494497 const children: Node[] = [];
495498
496- for (const block of msg.content) {
497- if (block.type === "text" && (block as TextContent).text) {
498- children.push(...renderMarkdownContent((block as TextContent).text));
499- } else if (block.type === "thinking" && opts.showReasoning) {
500- const thinking = (block as ThinkingContent).thinking;
501- if (thinking) {
502- children.push(
503- VStack({ padding: { x: 1 } }, [
504- Text(thinking, {
505- wrap: "word",
506- fgColor: opts.theme.mutedText,
507- italic: true,
508- }),
509- ]),
510- );
511- }
499+ for (const block of assistant.content) {
500+ if (block.type === "text" && block.text) {
501+ children.push(...renderMarkdownContent(block.text));
502+ continue;
503+ }
504+
505+ if (block.type !== "thinking" || !block.thinking) {
506+ continue;
507+ }
508+
509+ if (opts.showReasoning) {
510+ children.push(
511+ VStack({ padding: { x: 1 } }, [
512+ Text(block.thinking, {
513+ wrap: "word",
514+ fgColor: opts.theme.mutedText,
515+ italic: true,
516+ }),
517+ ]),
518+ );
519+ continue;
512520 }
521+
522+ const lineCount = block.thinking.split("\n").length;
523+ const unit = lineCount === 1 ? "line" : "lines";
524+ children.push(
525+ VStack({ padding: { x: 1 } }, [
526+ Text(`Thinking... ${lineCount} ${unit}.`, {
527+ fgColor: opts.theme.mutedText,
528+ italic: true,
529+ }),
530+ ]),
531+ );
532+ }
533+
534+ for (const toolCall of pendingToolCalls) {
535+ children.push(renderPendingToolCall(toolCall, opts));
513536 }
514537
515- if (msg.stopReason === "error" && msg.errorMessage) {
538+ if (errorMessage) {
516539 children.push(
517540 VStack({ padding: { x: 1 } }, [
518- Text(`Error: ${msg.errorMessage}`, {
541+ Text(`Error: ${errorMessage}`, {
519542 fgColor: opts.theme.error,
520543 }),
521544 ]),
@@ -782,7 +805,7 @@ export function renderToolResult(
782805 }
783806
784807 // ---------------------------------------------------------------------------
785-// Streaming response rendering
808+// Streaming tool-call rendering
786809 // ---------------------------------------------------------------------------
787810
788811 /** Render a pending tool call, including progressive output when available. */
@@ -864,57 +887,6 @@ function renderPendingToolCall(
864887 );
865888 }
866889
867-/**
868- * Render the in-progress streaming response.
869- *
870- * Shows thinking (if enabled), streamed markdown, and pending tool calls.
871- */
872-export function renderStreamingResponse(
873- streaming: StreamingRenderState,
874- opts: ConversationRenderOpts,
875-): Node | null {
876- const children: Node[] = [];
877-
878- if (streaming.thinking) {
879- if (opts.showReasoning) {
880- children.push(
881- VStack({ padding: { x: 1 } }, [
882- Text(streaming.thinking, {
883- wrap: "word",
884- fgColor: opts.theme.mutedText,
885- italic: true,
886- }),
887- ]),
888- );
889- } else {
890- const lineCount = streaming.thinking.split("\n").length;
891- const unit = lineCount === 1 ? "line" : "lines";
892- children.push(
893- VStack({ padding: { x: 1 } }, [
894- Text(`Thinking... ${lineCount} ${unit}.`, {
895- fgColor: opts.theme.mutedText,
896- italic: true,
897- }),
898- ]),
899- );
900- }
901- }
902-
903- if (streaming.text) {
904- children.push(...renderMarkdownContent(streaming.text));
905- }
906-
907- for (const toolCall of streaming.pendingToolCalls) {
908- children.push(renderPendingToolCall(toolCall, opts));
909- }
910-
911- if (children.length === 0) {
912- return null;
913- }
914-
915- return VStack({}, children);
916-}
917-
918890 // ---------------------------------------------------------------------------
919891 // Conversation log
920892 // ---------------------------------------------------------------------------
@@ -982,10 +954,9 @@ export function buildConversationLog(state: AppState): Node[] {
982954 // Append in-progress streaming response
983955 if (isStreaming) {
984956 pushConversationNode(
985- renderStreamingResponse(
957+ renderAssistantMessage(
986958 {
987- text: streamingText,
988- thinking: streamingThinking,
959+ content: streamingContent,
989960 pendingToolCalls,
990961 },
991962 renderOpts,
@@ -2065,8 +2036,7 @@ async function submitMessage(
20652036 state.running = true;
20662037 state.abortController = new AbortController();
20672038 isStreaming = true;
2068- streamingText = "";
2069- streamingThinking = "";
2039+ streamingContent = [];
20702040 pendingToolCalls = [];
20712041 startDividerAnimation();
20722042 cel.render();
@@ -2093,8 +2063,7 @@ async function submitMessage(
20932063 state.running = false;
20942064 state.abortController = null;
20952065 isStreaming = false;
2096- streamingText = "";
2097- streamingThinking = "";
2066+ streamingContent = [];
20982067 pendingToolCalls = [];
20992068 stopDividerAnimation();
21002069 cel.render();
@@ -2105,13 +2074,13 @@ async function submitMessage(
21052074 function handleAgentEvent(event: AgentEvent, state: AppState): void {
21062075 switch (event.type) {
21072076 case "text_delta":
2108- streamingText += event.delta;
2077+ streamingContent = event.content;
21092078 stickToBottom = true;
21102079 cel.render();
21112080 break;
21122081
21132082 case "thinking_delta":
2114- streamingThinking += event.delta;
2083+ streamingContent = event.content;
21152084 stickToBottom = true;
21162085 cel.render();
21172086 break;
@@ -2119,8 +2088,7 @@ function handleAgentEvent(event: AgentEvent, state: AppState): void {
21192088 case "assistant_message":
21202089 state.messages.push(event.message);
21212090 state.stats = computeStats(state.messages);
2122- streamingText = "";
2123- streamingThinking = "";
2091+ streamingContent = [];
21242092 stickToBottom = true;
21252093 cel.render();
21262094 break;
@@ -2177,8 +2145,7 @@ function handleAgentEvent(event: AgentEvent, state: AppState): void {
21772145 case "done":
21782146 case "error":
21792147 case "aborted":
2180- streamingText = "";
2181- streamingThinking = "";
2148+ streamingContent = [];
21822149 pendingToolCalls = [];
21832150 isStreaming = false;
21842151 stickToBottom = true;