gsd-pi 2.74.0-dev.14c45ac → 2.74.0-dev.28a6415

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/dist/web/standalone/.next/BUILD_ID +1 -1
  2. package/dist/web/standalone/.next/app-path-routes-manifest.json +13 -13
  3. package/dist/web/standalone/.next/build-manifest.json +2 -2
  4. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  5. package/dist/web/standalone/.next/required-server-files.json +1 -1
  6. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  7. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  8. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  9. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  10. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  11. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  12. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  13. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  14. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  15. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  16. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  17. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  18. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  19. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  20. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  21. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  22. package/dist/web/standalone/.next/server/app/index.html +1 -1
  23. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  24. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  25. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  26. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  27. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  28. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  29. package/dist/web/standalone/.next/server/app-paths-manifest.json +13 -13
  30. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  31. package/dist/web/standalone/.next/server/middleware-manifest.json +5 -5
  32. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  33. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  34. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  35. package/dist/web/standalone/server.js +1 -1
  36. package/package.json +1 -1
  37. package/packages/pi-coding-agent/dist/core/chat-controller-ordering.test.js +70 -10
  38. package/packages/pi-coding-agent/dist/core/chat-controller-ordering.test.js.map +1 -1
  39. package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  40. package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.js +5 -1
  41. package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.js.map +1 -1
  42. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.d.ts.map +1 -1
  43. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js +57 -21
  44. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js.map +1 -1
  45. package/packages/pi-coding-agent/src/core/chat-controller-ordering.test.ts +84 -10
  46. package/packages/pi-coding-agent/src/modes/interactive/components/assistant-message.ts +5 -1
  47. package/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts +72 -23
  48. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  49. /package/dist/web/standalone/.next/static/{ZMKM0OI0CrTgzKWbgfPOg → fMaWScj7m6EsI3DbaNv2_}/_buildManifest.js +0 -0
  50. /package/dist/web/standalone/.next/static/{ZMKM0OI0CrTgzKWbgfPOg → fMaWScj7m6EsI3DbaNv2_}/_ssgManifest.js +0 -0
@@ -268,42 +268,70 @@ export async function handleAgentEvent(host, event) {
268
268
  }
269
269
  return false;
270
270
  });
271
- const shouldDropPreToolText = isClaudeCodeProvider && hasMcpToolBlock;
272
271
  const firstToolIdx = blocks.findIndex((b) => b.type === "toolCall" || b.type === "serverToolUse");
272
+ const hasPostToolText = firstToolIdx >= 0
273
+ && blocks.some((b, idx) => (idx > firstToolIdx
274
+ && b?.type === "text"
275
+ && typeof b?.text === "string"
276
+ && b.text.trim().length > 0));
277
+ // Only prune provisional pre-tool prose after post-tool prose exists,
278
+ // so MCP tool-only windows do not blank the assistant content.
279
+ const shouldDropPreToolProse = isClaudeCodeProvider && hasMcpToolBlock && hasPostToolText;
273
280
  const desired = [];
274
281
  let runStart = -1;
282
+ let runEnd = -1;
283
+ let runType;
284
+ const closeRun = () => {
285
+ if (runStart !== -1 && runType) {
286
+ desired.push({ kind: "text-run", startIndex: runStart, endIndex: runEnd, contentType: runType });
287
+ runStart = -1;
288
+ runEnd = -1;
289
+ runType = undefined;
290
+ }
291
+ };
275
292
  for (let i = 0; i < blocks.length; i++) {
276
293
  const b = blocks[i];
277
- const isText = b.type === "text" || b.type === "thinking";
294
+ const blockType = b.type === "text" || b.type === "thinking" ? b.type : undefined;
295
+ const isTextLike = blockType === "text" || blockType === "thinking";
278
296
  const isTool = b.type === "toolCall" || b.type === "serverToolUse";
279
- if (isText) {
280
- if (shouldDropPreToolText && firstToolIdx >= 0 && i < firstToolIdx) {
281
- continue;
297
+ // For Claude Code MCP turns, prune only pre-tool prose, never thinking.
298
+ const shouldSkipProse = shouldDropPreToolProse && firstToolIdx >= 0 && i < firstToolIdx && blockType === "text";
299
+ if (shouldSkipProse) {
300
+ closeRun();
301
+ continue;
302
+ }
303
+ if (isTextLike) {
304
+ if (runStart === -1) {
305
+ runStart = i;
306
+ runEnd = i;
307
+ runType = blockType;
282
308
  }
283
- if (runStart === -1)
309
+ else if (runType !== blockType) {
310
+ closeRun();
284
311
  runStart = i;
312
+ runEnd = i;
313
+ runType = blockType;
314
+ }
315
+ else {
316
+ runEnd = i;
317
+ }
285
318
  }
286
319
  else {
287
- if (runStart !== -1) {
288
- desired.push({ kind: "text-run", startIndex: runStart, endIndex: i - 1 });
289
- runStart = -1;
290
- }
320
+ closeRun();
291
321
  if (isTool) {
292
322
  desired.push({ kind: "tool", contentIndex: i, toolId: b.id });
293
323
  }
294
324
  }
295
325
  }
296
- if (runStart !== -1) {
297
- desired.push({ kind: "text-run", startIndex: runStart, endIndex: blocks.length - 1 });
298
- }
326
+ closeRun();
299
327
  // Claude Code MCP can emit provisional pre-tool prose that gets
300
328
  // superseded by post-tool output. Prune stale text-run segments so
301
329
  // the final assistant output remains below tool output.
302
- if (shouldDropPreToolText && firstToolIdx >= 0) {
330
+ if (shouldDropPreToolProse && firstToolIdx >= 0) {
303
331
  if (orphanedSegments.length > 0) {
304
332
  const remainingOrphans = [];
305
333
  for (const orphan of orphanedSegments) {
306
- if (orphan.kind === "text-run") {
334
+ if (orphan.kind === "text-run" && orphan.contentType === "text") {
307
335
  host.chatContainer.removeChild(orphan.component);
308
336
  if (host.streamingComponent === orphan.component) {
309
337
  host.streamingComponent = undefined;
@@ -314,15 +342,17 @@ export async function handleAgentEvent(host, event) {
314
342
  }
315
343
  orphanedSegments = remainingOrphans;
316
344
  }
317
- const desiredTextStarts = new Set(desired
345
+ const desiredTextKeys = new Set(desired
318
346
  .filter((seg) => seg.kind === "text-run")
319
- .map((seg) => seg.startIndex));
347
+ .map((seg) => `${seg.contentType}:${seg.startIndex}`));
320
348
  const desiredToolIndices = new Set(desired
321
349
  .filter((seg) => seg.kind === "tool")
322
350
  .map((seg) => seg.contentIndex));
323
351
  const nextRendered = [];
324
352
  for (const seg of renderedSegments) {
325
- if (seg.kind === "text-run" && !desiredTextStarts.has(seg.startIndex)) {
353
+ if (seg.kind === "text-run"
354
+ && seg.contentType === "text"
355
+ && !desiredTextKeys.has(`${seg.contentType}:${seg.startIndex}`)) {
326
356
  host.chatContainer.removeChild(seg.component);
327
357
  if (host.streamingComponent === seg.component) {
328
358
  host.streamingComponent = undefined;
@@ -351,11 +381,17 @@ export async function handleAgentEvent(host, event) {
351
381
  }
352
382
  else {
353
383
  // text-run segment
354
- const existing = renderedSegments.find((s) => s.kind === "text-run" && s.startIndex === seg.startIndex);
384
+ const existing = renderedSegments.find((s) => s.kind === "text-run" && s.startIndex === seg.startIndex && s.contentType === seg.contentType);
355
385
  if (!existing) {
356
386
  const comp = new AssistantMessageComponent(undefined, host.hideThinkingBlock, host.getMarkdownThemeWithSettings(), host.settingsManager.getTimestampFormat(), { startIndex: seg.startIndex, endIndex: seg.endIndex });
357
387
  host.chatContainer.addChild(comp);
358
- renderedSegments.push({ kind: "text-run", startIndex: seg.startIndex, endIndex: seg.endIndex, component: comp });
388
+ renderedSegments.push({
389
+ kind: "text-run",
390
+ startIndex: seg.startIndex,
391
+ endIndex: seg.endIndex,
392
+ contentType: seg.contentType,
393
+ component: comp,
394
+ });
359
395
  host.streamingComponent = comp;
360
396
  }
361
397
  }
@@ -365,7 +401,7 @@ export async function handleAgentEvent(host, event) {
365
401
  for (const seg of renderedSegments) {
366
402
  if (seg.kind === "text-run") {
367
403
  // Find corresponding desired segment to get current endIndex
368
- const d = desired.find((ds) => ds.kind === "text-run" && ds.startIndex === seg.startIndex);
404
+ const d = desired.find((ds) => ds.kind === "text-run" && ds.startIndex === seg.startIndex && ds.contentType === seg.contentType);
369
405
  if (d && d.kind === "text-run" && d.endIndex !== seg.endIndex) {
370
406
  seg.endIndex = d.endIndex;
371
407
  seg.component.setRange({ startIndex: seg.startIndex, endIndex: seg.endIndex });
@@ -1 +1 @@
1
- {"version":3,"file":"chat-controller.js","sourceRoot":"","sources":["../../../../src/modes/interactive/controllers/chat-controller.ts"],"names":[],"mappings":"AAAA,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\"../interactive-mode-state.js\";\nimport { theme } from \"../theme/theme.js\";\nimport { AssistantMessageComponent } from \"../components/assistant-message.js\";\nimport { ToolExecutionComponent } from \"../components/tool-execution.js\";\nimport { DynamicBorder } from \"../components/dynamic-border.js\";\nimport { appKey } from \"../components/keybinding-hints.js\";\n\n// Tracks the last processed content index to avoid re-scanning all blocks on every message_update\nlet lastProcessedContentIndex = 0;\n\n// Tracks the previous content[] length so we can detect when an adapter resets\n// the assistant content array for a new provider sub-turn within one lifecycle.\nlet lastContentLength = 0;\n\n// --- Segment walker state (per streaming assistant turn) ---\ntype RenderedSegment =\n\t| { kind: \"text-run\"; startIndex: number; endIndex: number; component: AssistantMessageComponent }\n\t| { kind: \"tool\"; contentIndex: number; component: ToolExecutionComponent };\n\nlet renderedSegments: RenderedSegment[] = [];\n// When providers reuse one assistant lifecycle across internal sub-turns,\n// a content[] shrink resets renderedSegments. Keep the displaced segments so\n// claude-code MCP pruning can remove stale provisional text later.\nlet orphanedSegments: RenderedSegment[] = [];\n\nfunction hasVisibleAssistantContent(message: { content: Array<any> }): boolean {\n\treturn message.content.some(\n\t\t(c) =>\n\t\t\t(c.type === \"text\" && typeof c.text === \"string\" && c.text.trim().length > 0)\n\t\t\t|| (c.type === \"thinking\" && typeof c.thinking === \"string\" && c.thinking.trim().length > 0),\n\t);\n}\n\nfunction hasAssistantToolBlocks(message: { content: Array<any> }): boolean {\n\treturn message.content.some((c) => c.type === \"toolCall\" || c.type === \"serverToolUse\");\n}\n\n// Pick the latest non-empty text block that appears strictly before the most\n// recent tool call. Text blocks that come after the last tool call are still\n// streaming live into the chat container, so mirroring them into the pinned\n// \"Latest Output\" zone would render the same tokens twice.\nexport function findLatestPinnableText(contentBlocks: Array<any>): string {\n\tlet lastToolIdx = -1;\n\tfor (let i = contentBlocks.length - 1; i >= 0; i--) {\n\t\tconst c = contentBlocks[i];\n\t\tif (c?.type === \"toolCall\" || c?.type === \"serverToolUse\") {\n\t\t\tlastToolIdx = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tfor (let i = lastToolIdx - 1; i >= 0; i--) {\n\t\tconst c = contentBlocks[i];\n\t\tif (c?.type === \"text\" && typeof c.text === \"string\" && c.text.trim()) {\n\t\t\treturn c.text.trim();\n\t\t}\n\t}\n\treturn \"\";\n}\n\n// Tracks the latest assistant text for the pinned message zone\nlet lastPinnedText = \"\";\n// Whether any tool execution has been added in this assistant turn (triggers pinned display)\nlet hasToolsInTurn = false;\n// Reference to the pinned border so we can toggle its label between working/idle\nlet pinnedBorder: DynamicBorder | undefined;\n// Reference to the pinned markdown component below the border\nlet pinnedTextComponent: Markdown | undefined;\n\nexport async function handleAgentEvent(host: InteractiveModeStateHost & {\n\tinit: () => Promise<void>;\n\tgetMarkdownThemeWithSettings: () => any;\n\taddMessageToChat: (message: any, options?: any) => void;\n\tformatWebSearchResult: (content: unknown) => string;\n\tgetRegisteredToolDefinition: (toolName: string) => any;\n\tcheckShutdownRequested: () => Promise<void>;\n\trebuildChatFromMessages: () => void;\n\tflushCompactionQueue: (options?: { willRetry?: boolean }) => Promise<void>;\n\tshowStatus: (message: string) => void;\n\tshowError: (message: string) => void;\n\tupdatePendingMessagesDisplay: () => void;\n\tupdateTerminalTitle: () => void;\n\tupdateEditorBorderColor: () => void;\n\tpendingMessagesContainer: { clear: () => void };\n}, event: InteractiveModeEvent): Promise<void> {\n\tif (!host.isInitialized) {\n\t\tawait host.init();\n\t}\n\n\thost.footer.invalidate();\n\n\t// Reset content index tracker and pinned state when a new assistant message starts\n\tif (event.type === \"message_start\" && event.message.role === \"assistant\") {\n\t\tlastProcessedContentIndex = 0;\n\t\tlastContentLength = 0;\n\t\tlastPinnedText = \"\";\n\t\thasToolsInTurn = false;\n\t\trenderedSegments = [];\n\t\torphanedSegments = [];\n\t\tif (pinnedBorder) pinnedBorder.stopSpinner();\n\t\tpinnedBorder = undefined;\n\t\tpinnedTextComponent = undefined;\n\t\thost.pinnedMessageContainer.clear();\n\t}\n\n\tswitch (event.type) {\n\t\tcase \"session_state_changed\":\n\t\t\tswitch (event.reason) {\n\t\t\t\tcase \"new_session\":\n\t\t\t\tcase \"switch_session\":\n\t\t\t\tcase \"fork\":\n\t\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\t\thost.streamingMessage = undefined;\n\t\t\t\t\thost.pendingTools.clear();\n\t\t\t\t\thost.pendingMessagesContainer.clear();\n\t\t\t\t\thost.pinnedMessageContainer.clear();\n\t\t\t\t\tlastPinnedText = \"\";\n\t\t\t\t\thasToolsInTurn = false;\n\t\t\t\t\trenderedSegments = [];\n\t\t\t\t\torphanedSegments = [];\n\t\t\t\t\tlastContentLength = 0;\n\t\t\t\t\tif (pinnedBorder) pinnedBorder.stopSpinner();\n\t\t\t\t\tpinnedBorder = undefined;\n\t\t\t\t\tpinnedTextComponent = undefined;\n\t\t\t\t\thost.compactionQueuedMessages = [];\n\t\t\t\t\thost.rebuildChatFromMessages();\n\t\t\t\t\thost.updatePendingMessagesDisplay();\n\t\t\t\t\thost.updateTerminalTitle();\n\t\t\t\t\thost.updateEditorBorderColor();\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t\tcase \"set_session_name\":\n\t\t\t\t\thost.updateTerminalTitle();\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t\tcase \"set_model\":\n\t\t\t\tcase \"set_thinking_level\":\n\t\t\t\t\thost.updateEditorBorderColor();\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t\tdefault:\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t}\n\t\tcase \"agent_start\":\n\t\t\tif (host.retryEscapeHandler) {\n\t\t\t\thost.defaultEditor.onEscape = host.retryEscapeHandler;\n\t\t\t\thost.retryEscapeHandler = undefined;\n\t\t\t}\n\t\t\tif (host.retryLoader) {\n\t\t\t\thost.retryLoader.stop();\n\t\t\t\thost.retryLoader = undefined;\n\t\t\t}\n\t\t\tif (host.loadingAnimation) {\n\t\t\t\thost.loadingAnimation.stop();\n\t\t\t}\n\t\t\thost.statusContainer.clear();\n\t\t\thost.loadingAnimation = new Loader(\n\t\t\t\thost.ui,\n\t\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\thost.defaultWorkingMessage,\n\t\t\t);\n\t\t\thost.statusContainer.addChild(host.loadingAnimation);\n\t\t\tif (host.pendingWorkingMessage !== undefined) {\n\t\t\t\tif (host.pendingWorkingMessage) {\n\t\t\t\t\thost.loadingAnimation.setMessage(host.pendingWorkingMessage);\n\t\t\t\t}\n\t\t\t\thost.pendingWorkingMessage = undefined;\n\t\t\t}\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"message_start\":\n\t\t\tif (event.message.role === \"custom\") {\n\t\t\t\thost.addMessageToChat(event.message);\n\t\t\t\thost.ui.requestRender();\n\t\t\t} else if (event.message.role === \"user\") {\n\t\t\t\thost.addMessageToChat(event.message);\n\t\t\t\thost.updatePendingMessagesDisplay();\n\t\t\t\thost.ui.requestRender();\n\t\t\t} else if (event.message.role === \"assistant\") {\n\t\t\t\thost.streamingMessage = event.message;\n\t\t\t\t// External-tool providers can stream multiple assistant turns through\n\t\t\t\t// one response. Delay component creation until visible assistant text\n\t\t\t\t// arrives so tool outputs keep chronological ordering.\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase \"message_update\":\n\t\t\tif (event.message.role === \"assistant\") {\n\t\t\t\thost.streamingMessage = event.message;\n\t\t\t\tconst innerEvent = event.assistantMessageEvent;\n\n\t\t\t\tlet externalToolResult:\n\t\t\t\t\t| { toolCallId: string; content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; details: Record<string, unknown>; isError: boolean }\n\t\t\t\t\t| undefined;\n\t\t\t\tif (innerEvent.type === \"toolcall_end\" && innerEvent.toolCall) {\n\t\t\t\t\tconst tc = innerEvent.toolCall as any;\n\t\t\t\t\tconst ext = tc.externalResult;\n\t\t\t\t\tif (ext) {\n\t\t\t\t\t\texternalToolResult = {\n\t\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\t\tcontent: ext.content ?? [{ type: \"text\", text: \"\" }],\n\t\t\t\t\t\t\tdetails: ext.details ?? {},\n\t\t\t\t\t\t\tisError: ext.isError ?? false,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} else if (innerEvent.type === \"server_tool_use\") {\n\t\t\t\t\tconst idx = typeof innerEvent.contentIndex === \"number\" ? innerEvent.contentIndex : -1;\n\t\t\t\t\tconst block = idx >= 0 ? (host.streamingMessage.content[idx] as any) : undefined;\n\t\t\t\t\tconst ext = block?.externalResult;\n\t\t\t\t\tif (block?.id && ext) {\n\t\t\t\t\t\texternalToolResult = {\n\t\t\t\t\t\t\ttoolCallId: block.id,\n\t\t\t\t\t\t\tcontent: ext.content ?? [{ type: \"text\", text: \"\" }],\n\t\t\t\t\t\t\tdetails: ext.details ?? {},\n\t\t\t\t\t\t\tisError: ext.isError ?? false,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst contentBlocks = host.streamingMessage.content;\n\t\t\t\t// Some adapters (notably claude-code) reuse a single assistant\n\t\t\t\t// lifecycle while internally spanning multiple provider sub-turns.\n\t\t\t\t// When a new sub-turn starts, content[] length shrinks back to 0/1.\n\t\t\t\t// The scan loop needs its index reset, AND the segment walker's\n\t\t\t\t// renderedSegments map must be cleared so existing text-run\n\t\t\t\t// components don't get overwritten in place with new sub-turn\n\t\t\t\t// content (#4144 regression). Prior sub-turn children stay in\n\t\t\t\t// chatContainer as frozen history; new segments append after them.\n\t\t\t\tif (contentBlocks.length < lastContentLength) {\n\t\t\t\t\torphanedSegments = [...renderedSegments];\n\t\t\t\t\trenderedSegments = [];\n\t\t\t\t\tlastPinnedText = \"\";\n\t\t\t\t\tlastProcessedContentIndex = 0;\n\t\t\t\t} else if (lastProcessedContentIndex >= contentBlocks.length) {\n\t\t\t\t\tlastProcessedContentIndex = 0;\n\t\t\t\t}\n\t\t\t\tlastContentLength = contentBlocks.length;\n\t\t\t\tfor (let i = lastProcessedContentIndex; i < contentBlocks.length; i++) {\n\t\t\t\t\tconst content = contentBlocks[i];\n\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\tif (!host.pendingTools.has(content.id)) {\n\t\t\t\t\t\t\tconst component = new ToolExecutionComponent(\n\t\t\t\t\t\t\t\tcontent.name,\n\t\t\t\t\t\t\t\tcontent.arguments,\n\t\t\t\t\t\t\t\t{ showImages: host.settingsManager.getShowImages() },\n\t\t\t\t\t\t\t\thost.getRegisteredToolDefinition(content.name),\n\t\t\t\t\t\t\t\thost.ui,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tcomponent.setExpanded(host.toolOutputExpanded);\n\t\t\t\t\t\t\thost.chatContainer.addChild(component);\n\t\t\t\t\t\t\thost.pendingTools.set(content.id, component);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\thost.pendingTools.get(content.id)?.updateArgs(content.arguments);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (content.type === \"serverToolUse\") {\n\t\t\t\t\t\tif (!host.pendingTools.has(content.id)) {\n\t\t\t\t\t\t\tconst component = new ToolExecutionComponent(\n\t\t\t\t\t\t\t\tcontent.name,\n\t\t\t\t\t\t\t\tcontent.input ?? {},\n\t\t\t\t\t\t\t\t{ showImages: host.settingsManager.getShowImages() },\n\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t\thost.ui,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tcomponent.setExpanded(host.toolOutputExpanded);\n\t\t\t\t\t\t\thost.chatContainer.addChild(component);\n\t\t\t\t\t\t\thost.pendingTools.set(content.id, component);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (content.type === \"webSearchResult\") {\n\t\t\t\t\t\tconst component = host.pendingTools.get(content.toolUseId);\n\t\t\t\t\t\tif (component) {\n\t\t\t\t\t\t\tif (process.env.PI_OFFLINE === \"1\") {\n\t\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"Web search disabled (offline mode)\" }],\n\t\t\t\t\t\t\t\t\tisError: false,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tconst searchContent = content.content;\n\t\t\t\t\t\t\t\tconst isError = searchContent && typeof searchContent === \"object\" && \"type\" in (searchContent as any) && (searchContent as any).type === \"web_search_tool_result_error\";\n\t\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: host.formatWebSearchResult(searchContent) }],\n\t\t\t\t\t\t\t\t\tisError: !!isError,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// When the stream adapter signals a completed tool call with an\n\t\t\t\t// external result (from Claude Code SDK), update the pending\n\t\t\t\t// ToolExecutionComponent immediately so output is visible in\n\t\t\t\t// real-time instead of waiting for the session to end.\n\t\t\t\tif (externalToolResult) {\n\t\t\t\t\tconst component = host.pendingTools.get(externalToolResult.toolCallId);\n\t\t\t\t\tif (component) {\n\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\tcontent: externalToolResult.content,\n\t\t\t\t\t\t\tdetails: externalToolResult.details,\n\t\t\t\t\t\t\tisError: externalToolResult.isError,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Segment walker: render content blocks in stream order, append-only.\n\t\t\t\t// Build desired segment plan from content[].\n\t\t\t\t{\n\t\t\t\t\tconst blocks = host.streamingMessage.content;\n\t\t\t\t\tconst isClaudeCodeProvider = host.streamingMessage.provider === \"claude-code\";\n\t\t\t\t\tconst hasMcpToolBlock = blocks.some((b: any) => {\n\t\t\t\t\t\tif (b?.type === \"toolCall\") {\n\t\t\t\t\t\t\treturn typeof b?.mcpServer === \"string\" || String(b?.name ?? \"\").startsWith(\"mcp__\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (b?.type === \"serverToolUse\") {\n\t\t\t\t\t\t\treturn typeof b?.mcpServer === \"string\" || String(b?.name ?? \"\").startsWith(\"mcp__\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t});\n\t\t\t\t\tconst shouldDropPreToolText = isClaudeCodeProvider && hasMcpToolBlock;\n\t\t\t\t\tconst firstToolIdx = blocks.findIndex((b: any) => b.type === \"toolCall\" || b.type === \"serverToolUse\");\n\t\t\t\t\ttype DesiredSegment =\n\t\t\t\t\t\t| { kind: \"text-run\"; startIndex: number; endIndex: number }\n\t\t\t\t\t\t| { kind: \"tool\"; contentIndex: number; toolId: string };\n\t\t\t\t\tconst desired: DesiredSegment[] = [];\n\t\t\t\t\tlet runStart = -1;\n\t\t\t\t\tfor (let i = 0; i < blocks.length; i++) {\n\t\t\t\t\t\tconst b = blocks[i];\n\t\t\t\t\t\tconst isText = b.type === \"text\" || b.type === \"thinking\";\n\t\t\t\t\t\tconst isTool = b.type === \"toolCall\" || b.type === \"serverToolUse\";\n\t\t\t\t\t\tif (isText) {\n\t\t\t\t\t\t\tif (shouldDropPreToolText && firstToolIdx >= 0 && i < firstToolIdx) {\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (runStart === -1) runStart = i;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tif (runStart !== -1) {\n\t\t\t\t\t\t\t\tdesired.push({ kind: \"text-run\", startIndex: runStart, endIndex: i - 1 });\n\t\t\t\t\t\t\t\trunStart = -1;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (isTool) {\n\t\t\t\t\t\t\t\tdesired.push({ kind: \"tool\", contentIndex: i, toolId: b.id });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (runStart !== -1) {\n\t\t\t\t\t\tdesired.push({ kind: \"text-run\", startIndex: runStart, endIndex: blocks.length - 1 });\n\t\t\t\t\t}\n\n\t\t\t\t\t// Claude Code MCP can emit provisional pre-tool prose that gets\n\t\t\t\t\t// superseded by post-tool output. Prune stale text-run segments so\n\t\t\t\t\t// the final assistant output remains below tool output.\n\t\t\t\t\tif (shouldDropPreToolText && firstToolIdx >= 0) {\n\t\t\t\t\t\tif (orphanedSegments.length > 0) {\n\t\t\t\t\t\t\tconst remainingOrphans: RenderedSegment[] = [];\n\t\t\t\t\t\t\tfor (const orphan of orphanedSegments) {\n\t\t\t\t\t\t\t\tif (orphan.kind === \"text-run\") {\n\t\t\t\t\t\t\t\t\thost.chatContainer.removeChild(orphan.component);\n\t\t\t\t\t\t\t\t\tif (host.streamingComponent === orphan.component) {\n\t\t\t\t\t\t\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tremainingOrphans.push(orphan);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\torphanedSegments = remainingOrphans;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst desiredTextStarts = new Set(\n\t\t\t\t\t\t\tdesired\n\t\t\t\t\t\t\t\t.filter((seg): seg is Extract<DesiredSegment, { kind: \"text-run\" }> => seg.kind === \"text-run\")\n\t\t\t\t\t\t\t\t.map((seg) => seg.startIndex),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst desiredToolIndices = new Set(\n\t\t\t\t\t\t\tdesired\n\t\t\t\t\t\t\t\t.filter((seg): seg is Extract<DesiredSegment, { kind: \"tool\" }> => seg.kind === \"tool\")\n\t\t\t\t\t\t\t\t.map((seg) => seg.contentIndex),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst nextRendered: RenderedSegment[] = [];\n\t\t\t\t\t\tfor (const seg of renderedSegments) {\n\t\t\t\t\t\t\tif (seg.kind === \"text-run\" && !desiredTextStarts.has(seg.startIndex)) {\n\t\t\t\t\t\t\t\thost.chatContainer.removeChild(seg.component);\n\t\t\t\t\t\t\t\tif (host.streamingComponent === seg.component) {\n\t\t\t\t\t\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (seg.kind === \"tool\" && !desiredToolIndices.has(seg.contentIndex)) {\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnextRendered.push(seg);\n\t\t\t\t\t\t}\n\t\t\t\t\t\trenderedSegments = nextRendered;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Append any newly needed segments (never reorder existing ones).\n\t\t\t\t\tfor (const seg of desired) {\n\t\t\t\t\t\tif (seg.kind === \"tool\") {\n\t\t\t\t\t\t\t// Tool segments are already handled above via pendingTools; just\n\t\t\t\t\t\t\t// register them in renderedSegments if not yet tracked.\n\t\t\t\t\t\t\tconst existing = renderedSegments.find(\n\t\t\t\t\t\t\t\t(s) => s.kind === \"tool\" && s.contentIndex === seg.contentIndex,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tif (!existing) {\n\t\t\t\t\t\t\t\tconst comp = host.pendingTools.get(seg.toolId);\n\t\t\t\t\t\t\t\tif (comp) {\n\t\t\t\t\t\t\t\t\trenderedSegments.push({ kind: \"tool\", contentIndex: seg.contentIndex, component: comp });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// text-run segment\n\t\t\t\t\t\t\tconst existing = renderedSegments.find(\n\t\t\t\t\t\t\t\t(s) => s.kind === \"text-run\" && s.startIndex === seg.startIndex,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tif (!existing) {\n\t\t\t\t\t\t\t\tconst comp = new AssistantMessageComponent(\n\t\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t\t\thost.hideThinkingBlock,\n\t\t\t\t\t\t\t\t\thost.getMarkdownThemeWithSettings(),\n\t\t\t\t\t\t\t\t\thost.settingsManager.getTimestampFormat(),\n\t\t\t\t\t\t\t\t\t{ startIndex: seg.startIndex, endIndex: seg.endIndex },\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\thost.chatContainer.addChild(comp);\n\t\t\t\t\t\t\t\trenderedSegments.push({ kind: \"text-run\", startIndex: seg.startIndex, endIndex: seg.endIndex, component: comp });\n\t\t\t\t\t\t\t\thost.streamingComponent = comp;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Update all trailing text-run segments with the latest message so\n\t\t\t\t\t// streaming text grows in place.\n\t\t\t\t\tfor (const seg of renderedSegments) {\n\t\t\t\t\t\tif (seg.kind === \"text-run\") {\n\t\t\t\t\t\t\t// Find corresponding desired segment to get current endIndex\n\t\t\t\t\t\t\tconst d = desired.find((ds) => ds.kind === \"text-run\" && ds.startIndex === seg.startIndex);\n\t\t\t\t\t\t\tif (d && d.kind === \"text-run\" && d.endIndex !== seg.endIndex) {\n\t\t\t\t\t\t\t\tseg.endIndex = d.endIndex;\n\t\t\t\t\t\t\t\tseg.component.setRange({ startIndex: seg.startIndex, endIndex: seg.endIndex });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tseg.component.updateContent(host.streamingMessage);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Keep streamingComponent pointing at the last text-run for message_end compatibility.\n\t\t\t\t\tconst lastTextSeg = [...renderedSegments].reverse().find((s) => s.kind === \"text-run\");\n\t\t\t\t\tif (lastTextSeg && lastTextSeg.kind === \"text-run\") {\n\t\t\t\t\t\thost.streamingComponent = lastTextSeg.component;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Update index: fully processed blocks won't need re-scanning.\n\t\t\t\t// Keep the last block's index (it may still be accumulating data),\n\t\t\t\t// so we re-check it next time but skip all earlier ones.\n\t\t\t\tif (contentBlocks.length > 0) {\n\t\t\t\t\tlastProcessedContentIndex = Math.max(0, contentBlocks.length - 1);\n\t\t\t\t}\n\n\t\t\t\t// Pinned message: mirror the latest assistant text above the editor\n\t\t\t\t// when tool executions push it out of the viewport.\n\t\t\t\tconst hasTools = contentBlocks.some(\n\t\t\t\t\t(c: any) => c.type === \"toolCall\" || c.type === \"serverToolUse\",\n\t\t\t\t);\n\t\t\t\tif (hasTools) hasToolsInTurn = true;\n\n\t\t\t\tif (hasToolsInTurn) {\n\t\t\t\t\tconst latestText = findLatestPinnableText(contentBlocks);\n\n\t\t\t\t\tif (latestText && latestText !== lastPinnedText) {\n\t\t\t\t\t\tlastPinnedText = latestText;\n\n\t\t\t\t\t\tif (!pinnedBorder) {\n\t\t\t\t\t\t\t// First time: create border + text component\n\t\t\t\t\t\t\thost.pinnedMessageContainer.clear();\n\t\t\t\t\t\t\tpinnedBorder = new DynamicBorder(\n\t\t\t\t\t\t\t\t(str: string) => theme.fg(\"dim\", str),\n\t\t\t\t\t\t\t\t\"Working · Latest Output\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tpinnedBorder.startSpinner(host.ui, (str: string) => theme.fg(\"accent\", str));\n\t\t\t\t\t\t\thost.pinnedMessageContainer.addChild(pinnedBorder);\n\t\t\t\t\t\t\tpinnedTextComponent = new Markdown(latestText, 1, 0, host.getMarkdownThemeWithSettings());\n\t\t\t\t\t\t\t// Cap pinned content to ~40% of terminal height so tall output\n\t\t\t\t\t\t\t// doesn't exceed the viewport and cause render flashing.\n\t\t\t\t\t\t\tpinnedTextComponent.maxLines = Math.max(3, Math.floor(host.ui.terminal.rows * 0.4));\n\t\t\t\t\t\t\thost.pinnedMessageContainer.addChild(pinnedTextComponent);\n\t\t\t\t\t\t\t// Hide the separate status loader — the pinned zone replaces it\n\t\t\t\t\t\t\tif (host.loadingAnimation) {\n\t\t\t\t\t\t\t\thost.loadingAnimation.stop();\n\t\t\t\t\t\t\t\thost.loadingAnimation = undefined;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\thost.statusContainer.clear();\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Update existing markdown component in-place\n\t\t\t\t\t\t\tpinnedTextComponent?.setText(latestText);\n\t\t\t\t\t\t\t// Refresh maxLines in case terminal was resized\n\t\t\t\t\t\t\tif (pinnedTextComponent) {\n\t\t\t\t\t\t\t\tpinnedTextComponent.maxLines = Math.max(3, Math.floor(host.ui.terminal.rows * 0.4));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase \"message_end\":\n\t\t\tif (event.message.role === \"user\") break;\n\t\t\tif (event.message.role === \"assistant\") {\n\t\t\t\thost.streamingMessage = event.message;\n\t\t\t\tlet errorMessage: string | undefined;\n\t\t\t\tif (host.streamingMessage.stopReason === \"aborted\") {\n\t\t\t\t\tconst retryAttempt = host.session.retryAttempt;\n\t\t\t\t\terrorMessage = retryAttempt > 0\n\t\t\t\t\t\t? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? \"s\" : \"\"}`\n\t\t\t\t\t\t: \"Operation aborted\";\n\t\t\t\t\thost.streamingMessage.errorMessage = errorMessage;\n\t\t\t\t}\n\n\t\t\t\tconst shouldRenderAssistant = hasVisibleAssistantContent(host.streamingMessage)\n\t\t\t\t\t|| (\n\t\t\t\t\t\t(host.streamingMessage.stopReason === \"aborted\" || host.streamingMessage.stopReason === \"error\")\n\t\t\t\t\t\t&& !hasAssistantToolBlocks(host.streamingMessage)\n\t\t\t\t\t);\n\t\t\t\tif (!host.streamingComponent && shouldRenderAssistant) {\n\t\t\t\t\thost.streamingComponent = new AssistantMessageComponent(\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\thost.hideThinkingBlock,\n\t\t\t\t\t\thost.getMarkdownThemeWithSettings(),\n\t\t\t\t\t\thost.settingsManager.getTimestampFormat(),\n\t\t\t\t\t);\n\t\t\t\t\thost.chatContainer.addChild(host.streamingComponent);\n\t\t\t\t}\n\t\t\t\tif (host.streamingComponent) {\n\t\t\t\t\thost.streamingComponent.setShowMetadata(true);\n\t\t\t\t\thost.streamingComponent.updateContent(host.streamingMessage);\n\t\t\t\t}\n\n\t\t\t\tif (host.streamingMessage.stopReason === \"aborted\" || host.streamingMessage.stopReason === \"error\") {\n\t\t\t\t\tif (!errorMessage) {\n\t\t\t\t\t\terrorMessage = host.streamingMessage.errorMessage || \"Error\";\n\t\t\t\t\t}\n\t\t\t\t\tconst pendingComponents = Array.from(host.pendingTools.values());\n\t\t\t\t\tif (pendingComponents.length > 0) {\n\t\t\t\t\t\tconst [first, ...rest] = pendingComponents;\n\t\t\t\t\t\tfirst.completeWithError(errorMessage);\n\t\t\t\t\t\tfor (const component of rest) {\n\t\t\t\t\t\t\tcomponent.completeWithError();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\thost.pendingTools.clear();\n\t\t\t\t} else {\n\t\t\t\t\tfor (const [, component] of host.pendingTools.entries()) {\n\t\t\t\t\t\tcomponent.setArgsComplete();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\thost.streamingMessage = undefined;\n\t\t\t\trenderedSegments = [];\n\t\t\t\torphanedSegments = [];\n\t\t\t\tlastContentLength = 0;\n\t\t\t\t// Clear pinned output once the message is finalized in the chat\n\t\t\t\t// container — prevents duplicate display when the agent continues\n\t\t\t\t// (e.g. form elicitation) after the assistant message ends.\n\t\t\t\tif (pinnedBorder) pinnedBorder.stopSpinner();\n\t\t\t\thost.pinnedMessageContainer.clear();\n\t\t\t\tlastPinnedText = \"\";\n\t\t\t\thasToolsInTurn = false;\n\t\t\t\tpinnedBorder = undefined;\n\t\t\t\tpinnedTextComponent = undefined;\n\t\t\t\thost.footer.invalidate();\n\t\t\t}\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"tool_execution_start\":\n\t\t\tif (!host.pendingTools.has(event.toolCallId)) {\n\t\t\t\tconst component = new ToolExecutionComponent(\n\t\t\t\t\tevent.toolName,\n\t\t\t\t\tevent.args,\n\t\t\t\t\t{ showImages: host.settingsManager.getShowImages() },\n\t\t\t\t\thost.getRegisteredToolDefinition(event.toolName),\n\t\t\t\t\thost.ui,\n\t\t\t\t);\n\t\t\t\tcomponent.setExpanded(host.toolOutputExpanded);\n\t\t\t\thost.chatContainer.addChild(component);\n\t\t\t\thost.pendingTools.set(event.toolCallId, component);\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase \"tool_execution_update\": {\n\t\t\tconst component = host.pendingTools.get(event.toolCallId);\n\t\t\tif (component) {\n\t\t\t\tcomponent.updateResult({ ...event.partialResult, isError: false }, true);\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\tcase \"tool_execution_end\": {\n\t\t\tconst component = host.pendingTools.get(event.toolCallId);\n\t\t\tif (component) {\n\t\t\t\tcomponent.updateResult({ ...event.result, isError: event.isError });\n\t\t\t\thost.pendingTools.delete(event.toolCallId);\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\tcase \"agent_end\":\n\t\t\tif (host.loadingAnimation) {\n\t\t\t\thost.loadingAnimation.stop();\n\t\t\t\thost.loadingAnimation = undefined;\n\t\t\t\thost.statusContainer.clear();\n\t\t\t}\n\t\t\tif (host.streamingComponent && host.streamingMessage) {\n\t\t\t\thost.streamingComponent.setShowMetadata(true);\n\t\t\t\thost.streamingComponent.updateContent(host.streamingMessage);\n\t\t\t}\n\t\t\thost.streamingComponent = undefined;\n\t\t\thost.streamingMessage = undefined;\n\t\t\trenderedSegments = [];\n\t\t\torphanedSegments = [];\n\t\t\tlastContentLength = 0;\n\t\t\thost.pendingTools.clear();\n\t\t\t// Pinned output is only useful while work is actively streaming.\n\t\t\t// Keep chat history as the single source after completion.\n\t\t\tif (pinnedBorder) {\n\t\t\t\tpinnedBorder.stopSpinner();\n\t\t\t}\n\t\t\thost.pinnedMessageContainer.clear();\n\t\t\tlastPinnedText = \"\";\n\t\t\thasToolsInTurn = false;\n\t\t\tpinnedBorder = undefined;\n\t\t\tpinnedTextComponent = undefined;\n\t\t\tawait host.checkShutdownRequested();\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_compaction_start\":\n\t\t\thost.autoCompactionEscapeHandler = host.defaultEditor.onEscape;\n\t\t\thost.defaultEditor.onEscape = () => host.session.abortCompaction();\n\t\t\thost.statusContainer.clear();\n\t\t\thost.autoCompactionLoader = new Loader(\n\t\t\t\thost.ui,\n\t\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\t`${event.reason === \"overflow\" ? \"Context overflow detected, \" : \"\"}Auto-compacting... (${appKey(host.keybindings, \"interrupt\")} to cancel)`,\n\t\t\t);\n\t\t\thost.statusContainer.addChild(host.autoCompactionLoader);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_compaction_end\":\n\t\t\tif (host.autoCompactionEscapeHandler) {\n\t\t\t\thost.defaultEditor.onEscape = host.autoCompactionEscapeHandler;\n\t\t\t\thost.autoCompactionEscapeHandler = undefined;\n\t\t\t}\n\t\t\tif (host.autoCompactionLoader) {\n\t\t\t\thost.autoCompactionLoader.stop();\n\t\t\t\thost.autoCompactionLoader = undefined;\n\t\t\t\thost.statusContainer.clear();\n\t\t\t}\n\t\t\tif (event.aborted) {\n\t\t\t\thost.showStatus(\"Auto-compaction cancelled\");\n\t\t\t} else if (event.result) {\n\t\t\t\thost.chatContainer.clear();\n\t\t\t\thost.rebuildChatFromMessages();\n\t\t\t\thost.addMessageToChat({\n\t\t\t\t\trole: \"compactionSummary\",\n\t\t\t\t\ttokensBefore: event.result.tokensBefore,\n\t\t\t\t\tsummary: event.result.summary,\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t});\n\t\t\t\thost.footer.invalidate();\n\t\t\t} else if (event.errorMessage) {\n\t\t\t\thost.chatContainer.addChild(new Spacer(1));\n\t\t\t\thost.chatContainer.addChild(new Text(theme.fg(\"error\", event.errorMessage), 1, 0));\n\t\t\t}\n\t\t\tvoid host.flushCompactionQueue({ willRetry: event.willRetry });\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_retry_start\":\n\t\t\thost.retryEscapeHandler = host.defaultEditor.onEscape;\n\t\t\thost.defaultEditor.onEscape = () => host.session.abortRetry();\n\t\t\thost.statusContainer.clear();\n\t\t\thost.retryLoader = new Loader(\n\t\t\t\thost.ui,\n\t\t\t\t(spinner) => theme.fg(\"warning\", spinner),\n\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\t`Retrying (${event.attempt}/${event.maxAttempts}) in ${Math.round(event.delayMs / 1000)}s... (${appKey(host.keybindings, \"interrupt\")} to cancel)`,\n\t\t\t);\n\t\t\thost.statusContainer.addChild(host.retryLoader);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_retry_end\":\n\t\t\tif (host.retryEscapeHandler) {\n\t\t\t\thost.defaultEditor.onEscape = host.retryEscapeHandler;\n\t\t\t\thost.retryEscapeHandler = undefined;\n\t\t\t}\n\t\t\tif (host.retryLoader) {\n\t\t\t\thost.retryLoader.stop();\n\t\t\t\thost.retryLoader = undefined;\n\t\t\t\thost.statusContainer.clear();\n\t\t\t}\n\t\t\tif (!event.success) {\n\t\t\t\thost.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || \"Unknown error\"}`);\n\t\t\t}\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"fallback_provider_switch\":\n\t\t\thost.showStatus(`Switched from ${event.from} → ${event.to} (${event.reason})`);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"fallback_provider_restored\":\n\t\t\thost.showStatus(`Restored to ${event.provider}`);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"fallback_chain_exhausted\":\n\t\t\thost.showError(event.reason);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"image_overflow_recovery\":\n\t\t\thost.showStatus(\n\t\t\t\t`Removed ${event.strippedCount} older image(s) to comply with API limits. Retrying...`,\n\t\t\t);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\t}\n}\n"]}
1
+ {"version":3,"file":"chat-controller.js","sourceRoot":"","sources":["../../../../src/modes/interactive/controllers/chat-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAG7D,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,sBAAsB,EAAE,MAAM,iCAAiC,CAAC;AACzE,OAAO,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAE3D,kGAAkG;AAClG,IAAI,yBAAyB,GAAG,CAAC,CAAC;AAElC,+EAA+E;AAC/E,gFAAgF;AAChF,IAAI,iBAAiB,GAAG,CAAC,CAAC;AAa1B,IAAI,gBAAgB,GAAsB,EAAE,CAAC;AAC7C,0EAA0E;AAC1E,6EAA6E;AAC7E,mEAAmE;AACnE,IAAI,gBAAgB,GAAsB,EAAE,CAAC;AAE7C,SAAS,0BAA0B,CAAC,OAAgC;IACnE,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAC1B,CAAC,CAAC,EAAE,EAAE,CACL,CAAC,C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AC,UAAU,CAAC,eAAe,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;YACjD,IAAI,CAAC,EAAE,CAAC,aAAa,EAAE,CAAC;YACxB,MAAM;QAEP,KAAK,0BAA0B;YAC9B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YAC7B,IAAI,CAAC,EAAE,CAAC,aAAa,EAAE,CAAC;YACxB,MAAM;QAEP,KAAK,yBAAyB;YAC7B,IAAI,CAAC,UAAU,CACd,WAAW,KAAK,CAAC,aAAa,wDAAwD,CACtF,CAAC;YACF,IAAI,CAAC,EAAE,CAAC,aAAa,EAAE,CAAC;YACxB,MAAM;IACR,CAAC;AACF,CAAC","sourcesContent":["import { Loader, Markdown, Spacer, Text } from \"@gsd/pi-tui\";\n\nimport type { InteractiveModeEvent, InteractiveModeStateHost } from \"../interactive-mode-state.js\";\nimport { theme } from \"../theme/theme.js\";\nimport { AssistantMessageComponent } from \"../components/assistant-message.js\";\nimport { ToolExecutionComponent } from \"../components/tool-execution.js\";\nimport { DynamicBorder } from \"../components/dynamic-border.js\";\nimport { appKey } from \"../components/keybinding-hints.js\";\n\n// Tracks the last processed content index to avoid re-scanning all blocks on every message_update\nlet lastProcessedContentIndex = 0;\n\n// Tracks the previous content[] length so we can detect when an adapter resets\n// the assistant content array for a new provider sub-turn within one lifecycle.\nlet lastContentLength = 0;\n\n// --- Segment walker state (per streaming assistant turn) ---\ntype RenderedSegment =\n\t| {\n\t\tkind: \"text-run\";\n\t\tstartIndex: number;\n\t\tendIndex: number;\n\t\tcontentType: \"text\" | \"thinking\";\n\t\tcomponent: AssistantMessageComponent;\n\t}\n\t| { kind: \"tool\"; contentIndex: number; component: ToolExecutionComponent };\n\nlet renderedSegments: RenderedSegment[] = [];\n// When providers reuse one assistant lifecycle across internal sub-turns,\n// a content[] shrink resets renderedSegments. Keep the displaced segments so\n// claude-code MCP pruning can remove stale provisional text later.\nlet orphanedSegments: RenderedSegment[] = [];\n\nfunction hasVisibleAssistantContent(message: { content: Array<any> }): boolean {\n\treturn message.content.some(\n\t\t(c) =>\n\t\t\t(c.type === \"text\" && typeof c.text === \"string\" && c.text.trim().length > 0)\n\t\t\t|| (c.type === \"thinking\" && typeof c.thinking === \"string\" && c.thinking.trim().length > 0),\n\t);\n}\n\nfunction hasAssistantToolBlocks(message: { content: Array<any> }): boolean {\n\treturn message.content.some((c) => c.type === \"toolCall\" || c.type === \"serverToolUse\");\n}\n\n// Pick the latest non-empty text block that appears strictly before the most\n// recent tool call. Text blocks that come after the last tool call are still\n// streaming live into the chat container, so mirroring them into the pinned\n// \"Latest Output\" zone would render the same tokens twice.\nexport function findLatestPinnableText(contentBlocks: Array<any>): string {\n\tlet lastToolIdx = -1;\n\tfor (let i = contentBlocks.length - 1; i >= 0; i--) {\n\t\tconst c = contentBlocks[i];\n\t\tif (c?.type === \"toolCall\" || c?.type === \"serverToolUse\") {\n\t\t\tlastToolIdx = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\tfor (let i = lastToolIdx - 1; i >= 0; i--) {\n\t\tconst c = contentBlocks[i];\n\t\tif (c?.type === \"text\" && typeof c.text === \"string\" && c.text.trim()) {\n\t\t\treturn c.text.trim();\n\t\t}\n\t}\n\treturn \"\";\n}\n\n// Tracks the latest assistant text for the pinned message zone\nlet lastPinnedText = \"\";\n// Whether any tool execution has been added in this assistant turn (triggers pinned display)\nlet hasToolsInTurn = false;\n// Reference to the pinned border so we can toggle its label between working/idle\nlet pinnedBorder: DynamicBorder | undefined;\n// Reference to the pinned markdown component below the border\nlet pinnedTextComponent: Markdown | undefined;\n\nexport async function handleAgentEvent(host: InteractiveModeStateHost & {\n\tinit: () => Promise<void>;\n\tgetMarkdownThemeWithSettings: () => any;\n\taddMessageToChat: (message: any, options?: any) => void;\n\tformatWebSearchResult: (content: unknown) => string;\n\tgetRegisteredToolDefinition: (toolName: string) => any;\n\tcheckShutdownRequested: () => Promise<void>;\n\trebuildChatFromMessages: () => void;\n\tflushCompactionQueue: (options?: { willRetry?: boolean }) => Promise<void>;\n\tshowStatus: (message: string) => void;\n\tshowError: (message: string) => void;\n\tupdatePendingMessagesDisplay: () => void;\n\tupdateTerminalTitle: () => void;\n\tupdateEditorBorderColor: () => void;\n\tpendingMessagesContainer: { clear: () => void };\n}, event: InteractiveModeEvent): Promise<void> {\n\tif (!host.isInitialized) {\n\t\tawait host.init();\n\t}\n\n\thost.footer.invalidate();\n\n\t// Reset content index tracker and pinned state when a new assistant message starts\n\tif (event.type === \"message_start\" && event.message.role === \"assistant\") {\n\t\tlastProcessedContentIndex = 0;\n\t\tlastContentLength = 0;\n\t\tlastPinnedText = \"\";\n\t\thasToolsInTurn = false;\n\t\trenderedSegments = [];\n\t\torphanedSegments = [];\n\t\tif (pinnedBorder) pinnedBorder.stopSpinner();\n\t\tpinnedBorder = undefined;\n\t\tpinnedTextComponent = undefined;\n\t\thost.pinnedMessageContainer.clear();\n\t}\n\n\tswitch (event.type) {\n\t\tcase \"session_state_changed\":\n\t\t\tswitch (event.reason) {\n\t\t\t\tcase \"new_session\":\n\t\t\t\tcase \"switch_session\":\n\t\t\t\tcase \"fork\":\n\t\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\t\thost.streamingMessage = undefined;\n\t\t\t\t\thost.pendingTools.clear();\n\t\t\t\t\thost.pendingMessagesContainer.clear();\n\t\t\t\t\thost.pinnedMessageContainer.clear();\n\t\t\t\t\tlastPinnedText = \"\";\n\t\t\t\t\thasToolsInTurn = false;\n\t\t\t\t\trenderedSegments = [];\n\t\t\t\t\torphanedSegments = [];\n\t\t\t\t\tlastContentLength = 0;\n\t\t\t\t\tif (pinnedBorder) pinnedBorder.stopSpinner();\n\t\t\t\t\tpinnedBorder = undefined;\n\t\t\t\t\tpinnedTextComponent = undefined;\n\t\t\t\t\thost.compactionQueuedMessages = [];\n\t\t\t\t\thost.rebuildChatFromMessages();\n\t\t\t\t\thost.updatePendingMessagesDisplay();\n\t\t\t\t\thost.updateTerminalTitle();\n\t\t\t\t\thost.updateEditorBorderColor();\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t\tcase \"set_session_name\":\n\t\t\t\t\thost.updateTerminalTitle();\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t\tcase \"set_model\":\n\t\t\t\tcase \"set_thinking_level\":\n\t\t\t\t\thost.updateEditorBorderColor();\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t\tdefault:\n\t\t\t\t\thost.ui.requestRender();\n\t\t\t\t\treturn;\n\t\t\t}\n\t\tcase \"agent_start\":\n\t\t\tif (host.retryEscapeHandler) {\n\t\t\t\thost.defaultEditor.onEscape = host.retryEscapeHandler;\n\t\t\t\thost.retryEscapeHandler = undefined;\n\t\t\t}\n\t\t\tif (host.retryLoader) {\n\t\t\t\thost.retryLoader.stop();\n\t\t\t\thost.retryLoader = undefined;\n\t\t\t}\n\t\t\tif (host.loadingAnimation) {\n\t\t\t\thost.loadingAnimation.stop();\n\t\t\t}\n\t\t\thost.statusContainer.clear();\n\t\t\thost.loadingAnimation = new Loader(\n\t\t\t\thost.ui,\n\t\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\thost.defaultWorkingMessage,\n\t\t\t);\n\t\t\thost.statusContainer.addChild(host.loadingAnimation);\n\t\t\tif (host.pendingWorkingMessage !== undefined) {\n\t\t\t\tif (host.pendingWorkingMessage) {\n\t\t\t\t\thost.loadingAnimation.setMessage(host.pendingWorkingMessage);\n\t\t\t\t}\n\t\t\t\thost.pendingWorkingMessage = undefined;\n\t\t\t}\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"message_start\":\n\t\t\tif (event.message.role === \"custom\") {\n\t\t\t\thost.addMessageToChat(event.message);\n\t\t\t\thost.ui.requestRender();\n\t\t\t} else if (event.message.role === \"user\") {\n\t\t\t\thost.addMessageToChat(event.message);\n\t\t\t\thost.updatePendingMessagesDisplay();\n\t\t\t\thost.ui.requestRender();\n\t\t\t} else if (event.message.role === \"assistant\") {\n\t\t\t\thost.streamingMessage = event.message;\n\t\t\t\t// External-tool providers can stream multiple assistant turns through\n\t\t\t\t// one response. Delay component creation until visible assistant text\n\t\t\t\t// arrives so tool outputs keep chronological ordering.\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase \"message_update\":\n\t\t\tif (event.message.role === \"assistant\") {\n\t\t\t\thost.streamingMessage = event.message;\n\t\t\t\tconst innerEvent = event.assistantMessageEvent;\n\n\t\t\t\tlet externalToolResult:\n\t\t\t\t\t| { toolCallId: string; content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>; details: Record<string, unknown>; isError: boolean }\n\t\t\t\t\t| undefined;\n\t\t\t\tif (innerEvent.type === \"toolcall_end\" && innerEvent.toolCall) {\n\t\t\t\t\tconst tc = innerEvent.toolCall as any;\n\t\t\t\t\tconst ext = tc.externalResult;\n\t\t\t\t\tif (ext) {\n\t\t\t\t\t\texternalToolResult = {\n\t\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\t\tcontent: ext.content ?? [{ type: \"text\", text: \"\" }],\n\t\t\t\t\t\t\tdetails: ext.details ?? {},\n\t\t\t\t\t\t\tisError: ext.isError ?? false,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} else if (innerEvent.type === \"server_tool_use\") {\n\t\t\t\t\tconst idx = typeof innerEvent.contentIndex === \"number\" ? innerEvent.contentIndex : -1;\n\t\t\t\t\tconst block = idx >= 0 ? (host.streamingMessage.content[idx] as any) : undefined;\n\t\t\t\t\tconst ext = block?.externalResult;\n\t\t\t\t\tif (block?.id && ext) {\n\t\t\t\t\t\texternalToolResult = {\n\t\t\t\t\t\t\ttoolCallId: block.id,\n\t\t\t\t\t\t\tcontent: ext.content ?? [{ type: \"text\", text: \"\" }],\n\t\t\t\t\t\t\tdetails: ext.details ?? {},\n\t\t\t\t\t\t\tisError: ext.isError ?? false,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst contentBlocks = host.streamingMessage.content;\n\t\t\t\t// Some adapters (notably claude-code) reuse a single assistant\n\t\t\t\t// lifecycle while internally spanning multiple provider sub-turns.\n\t\t\t\t// When a new sub-turn starts, content[] length shrinks back to 0/1.\n\t\t\t\t// The scan loop needs its index reset, AND the segment walker's\n\t\t\t\t// renderedSegments map must be cleared so existing text-run\n\t\t\t\t// components don't get overwritten in place with new sub-turn\n\t\t\t\t// content (#4144 regression). Prior sub-turn children stay in\n\t\t\t\t// chatContainer as frozen history; new segments append after them.\n\t\t\t\tif (contentBlocks.length < lastContentLength) {\n\t\t\t\t\torphanedSegments = [...renderedSegments];\n\t\t\t\t\trenderedSegments = [];\n\t\t\t\t\tlastPinnedText = \"\";\n\t\t\t\t\tlastProcessedContentIndex = 0;\n\t\t\t\t} else if (lastProcessedContentIndex >= contentBlocks.length) {\n\t\t\t\t\tlastProcessedContentIndex = 0;\n\t\t\t\t}\n\t\t\t\tlastContentLength = contentBlocks.length;\n\t\t\t\tfor (let i = lastProcessedContentIndex; i < contentBlocks.length; i++) {\n\t\t\t\t\tconst content = contentBlocks[i];\n\t\t\t\t\tif (content.type === \"toolCall\") {\n\t\t\t\t\t\tif (!host.pendingTools.has(content.id)) {\n\t\t\t\t\t\t\tconst component = new ToolExecutionComponent(\n\t\t\t\t\t\t\t\tcontent.name,\n\t\t\t\t\t\t\t\tcontent.arguments,\n\t\t\t\t\t\t\t\t{ showImages: host.settingsManager.getShowImages() },\n\t\t\t\t\t\t\t\thost.getRegisteredToolDefinition(content.name),\n\t\t\t\t\t\t\t\thost.ui,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tcomponent.setExpanded(host.toolOutputExpanded);\n\t\t\t\t\t\t\thost.chatContainer.addChild(component);\n\t\t\t\t\t\t\thost.pendingTools.set(content.id, component);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\thost.pendingTools.get(content.id)?.updateArgs(content.arguments);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (content.type === \"serverToolUse\") {\n\t\t\t\t\t\tif (!host.pendingTools.has(content.id)) {\n\t\t\t\t\t\t\tconst component = new ToolExecutionComponent(\n\t\t\t\t\t\t\t\tcontent.name,\n\t\t\t\t\t\t\t\tcontent.input ?? {},\n\t\t\t\t\t\t\t\t{ showImages: host.settingsManager.getShowImages() },\n\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t\thost.ui,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tcomponent.setExpanded(host.toolOutputExpanded);\n\t\t\t\t\t\t\thost.chatContainer.addChild(component);\n\t\t\t\t\t\t\thost.pendingTools.set(content.id, component);\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (content.type === \"webSearchResult\") {\n\t\t\t\t\t\tconst component = host.pendingTools.get(content.toolUseId);\n\t\t\t\t\t\tif (component) {\n\t\t\t\t\t\t\tif (process.env.PI_OFFLINE === \"1\") {\n\t\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"Web search disabled (offline mode)\" }],\n\t\t\t\t\t\t\t\t\tisError: false,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tconst searchContent = content.content;\n\t\t\t\t\t\t\t\tconst isError = searchContent && typeof searchContent === \"object\" && \"type\" in (searchContent as any) && (searchContent as any).type === \"web_search_tool_result_error\";\n\t\t\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: host.formatWebSearchResult(searchContent) }],\n\t\t\t\t\t\t\t\t\tisError: !!isError,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// When the stream adapter signals a completed tool call with an\n\t\t\t\t// external result (from Claude Code SDK), update the pending\n\t\t\t\t// ToolExecutionComponent immediately so output is visible in\n\t\t\t\t// real-time instead of waiting for the session to end.\n\t\t\t\tif (externalToolResult) {\n\t\t\t\t\tconst component = host.pendingTools.get(externalToolResult.toolCallId);\n\t\t\t\t\tif (component) {\n\t\t\t\t\t\tcomponent.updateResult({\n\t\t\t\t\t\t\tcontent: externalToolResult.content,\n\t\t\t\t\t\t\tdetails: externalToolResult.details,\n\t\t\t\t\t\t\tisError: externalToolResult.isError,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Segment walker: render content blocks in stream order, append-only.\n\t\t\t\t// Build desired segment plan from content[].\n\t\t\t\t{\n\t\t\t\t\tconst blocks = host.streamingMessage.content;\n\t\t\t\t\tconst isClaudeCodeProvider = host.streamingMessage.provider === \"claude-code\";\n\t\t\t\t\tconst hasMcpToolBlock = blocks.some((b: any) => {\n\t\t\t\t\t\tif (b?.type === \"toolCall\") {\n\t\t\t\t\t\t\treturn typeof b?.mcpServer === \"string\" || String(b?.name ?? \"\").startsWith(\"mcp__\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (b?.type === \"serverToolUse\") {\n\t\t\t\t\t\t\treturn typeof b?.mcpServer === \"string\" || String(b?.name ?? \"\").startsWith(\"mcp__\");\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t});\n\t\t\t\t\tconst firstToolIdx = blocks.findIndex((b: any) => b.type === \"toolCall\" || b.type === \"serverToolUse\");\n\t\t\t\t\tconst hasPostToolText = firstToolIdx >= 0\n\t\t\t\t\t\t&& blocks.some(\n\t\t\t\t\t\t\t(b: any, idx: number) => (\n\t\t\t\t\t\t\t\tidx > firstToolIdx\n\t\t\t\t\t\t\t\t&& b?.type === \"text\"\n\t\t\t\t\t\t\t\t&& typeof b?.text === \"string\"\n\t\t\t\t\t\t\t\t&& b.text.trim().length > 0\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t// Only prune provisional pre-tool prose after post-tool prose exists,\n\t\t\t\t\t// so MCP tool-only windows do not blank the assistant content.\n\t\t\t\t\tconst shouldDropPreToolProse = isClaudeCodeProvider && hasMcpToolBlock && hasPostToolText;\n\t\t\t\t\ttype DesiredSegment =\n\t\t\t\t\t\t| { kind: \"text-run\"; startIndex: number; endIndex: number; contentType: \"text\" | \"thinking\" }\n\t\t\t\t\t\t| { kind: \"tool\"; contentIndex: number; toolId: string };\n\t\t\t\t\tconst desired: DesiredSegment[] = [];\n\t\t\t\t\tlet runStart = -1;\n\t\t\t\t\tlet runEnd = -1;\n\t\t\t\t\tlet runType: \"text\" | \"thinking\" | undefined;\n\t\t\t\t\tconst closeRun = () => {\n\t\t\t\t\t\tif (runStart !== -1 && runType) {\n\t\t\t\t\t\t\tdesired.push({ kind: \"text-run\", startIndex: runStart, endIndex: runEnd, contentType: runType });\n\t\t\t\t\t\t\trunStart = -1;\n\t\t\t\t\t\t\trunEnd = -1;\n\t\t\t\t\t\t\trunType = undefined;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t\tfor (let i = 0; i < blocks.length; i++) {\n\t\t\t\t\t\tconst b = blocks[i];\n\t\t\t\t\t\tconst blockType = b.type === \"text\" || b.type === \"thinking\" ? b.type : undefined;\n\t\t\t\t\t\tconst isTextLike = blockType === \"text\" || blockType === \"thinking\";\n\t\t\t\t\t\tconst isTool = b.type === \"toolCall\" || b.type === \"serverToolUse\";\n\t\t\t\t\t\t// For Claude Code MCP turns, prune only pre-tool prose, never thinking.\n\t\t\t\t\t\tconst shouldSkipProse = shouldDropPreToolProse && firstToolIdx >= 0 && i < firstToolIdx && blockType === \"text\";\n\t\t\t\t\t\tif (shouldSkipProse) {\n\t\t\t\t\t\t\tcloseRun();\n\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (isTextLike) {\n\t\t\t\t\t\t\tif (runStart === -1) {\n\t\t\t\t\t\t\t\trunStart = i;\n\t\t\t\t\t\t\t\trunEnd = i;\n\t\t\t\t\t\t\t\trunType = blockType;\n\t\t\t\t\t\t\t} else if (runType !== blockType) {\n\t\t\t\t\t\t\t\tcloseRun();\n\t\t\t\t\t\t\t\trunStart = i;\n\t\t\t\t\t\t\t\trunEnd = i;\n\t\t\t\t\t\t\t\trunType = blockType;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\trunEnd = i;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tcloseRun();\n\t\t\t\t\t\t\tif (isTool) {\n\t\t\t\t\t\t\t\tdesired.push({ kind: \"tool\", contentIndex: i, toolId: b.id });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tcloseRun();\n\n\t\t\t\t\t// Claude Code MCP can emit provisional pre-tool prose that gets\n\t\t\t\t\t// superseded by post-tool output. Prune stale text-run segments so\n\t\t\t\t\t// the final assistant output remains below tool output.\n\t\t\t\t\tif (shouldDropPreToolProse && firstToolIdx >= 0) {\n\t\t\t\t\t\tif (orphanedSegments.length > 0) {\n\t\t\t\t\t\t\tconst remainingOrphans: RenderedSegment[] = [];\n\t\t\t\t\t\t\tfor (const orphan of orphanedSegments) {\n\t\t\t\t\t\t\t\tif (orphan.kind === \"text-run\" && orphan.contentType === \"text\") {\n\t\t\t\t\t\t\t\t\thost.chatContainer.removeChild(orphan.component);\n\t\t\t\t\t\t\t\t\tif (host.streamingComponent === orphan.component) {\n\t\t\t\t\t\t\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tremainingOrphans.push(orphan);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\torphanedSegments = remainingOrphans;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst desiredTextKeys = new Set(\n\t\t\t\t\t\t\tdesired\n\t\t\t\t\t\t\t\t.filter((seg): seg is Extract<DesiredSegment, { kind: \"text-run\" }> => seg.kind === \"text-run\")\n\t\t\t\t\t\t\t\t.map((seg) => `${seg.contentType}:${seg.startIndex}`),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst desiredToolIndices = new Set(\n\t\t\t\t\t\t\tdesired\n\t\t\t\t\t\t\t\t.filter((seg): seg is Extract<DesiredSegment, { kind: \"tool\" }> => seg.kind === \"tool\")\n\t\t\t\t\t\t\t\t.map((seg) => seg.contentIndex),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tconst nextRendered: RenderedSegment[] = [];\n\t\t\t\t\t\tfor (const seg of renderedSegments) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\tseg.kind === \"text-run\"\n\t\t\t\t\t\t\t\t&& seg.contentType === \"text\"\n\t\t\t\t\t\t\t\t&& !desiredTextKeys.has(`${seg.contentType}:${seg.startIndex}`)\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\thost.chatContainer.removeChild(seg.component);\n\t\t\t\t\t\t\t\tif (host.streamingComponent === seg.component) {\n\t\t\t\t\t\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (seg.kind === \"tool\" && !desiredToolIndices.has(seg.contentIndex)) {\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tnextRendered.push(seg);\n\t\t\t\t\t\t}\n\t\t\t\t\t\trenderedSegments = nextRendered;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Append any newly needed segments (never reorder existing ones).\n\t\t\t\t\tfor (const seg of desired) {\n\t\t\t\t\t\tif (seg.kind === \"tool\") {\n\t\t\t\t\t\t\t// Tool segments are already handled above via pendingTools; just\n\t\t\t\t\t\t\t// register them in renderedSegments if not yet tracked.\n\t\t\t\t\t\t\tconst existing = renderedSegments.find(\n\t\t\t\t\t\t\t\t(s) => s.kind === \"tool\" && s.contentIndex === seg.contentIndex,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tif (!existing) {\n\t\t\t\t\t\t\t\tconst comp = host.pendingTools.get(seg.toolId);\n\t\t\t\t\t\t\t\tif (comp) {\n\t\t\t\t\t\t\t\t\trenderedSegments.push({ kind: \"tool\", contentIndex: seg.contentIndex, component: comp });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// text-run segment\n\t\t\t\t\t\t\tconst existing = renderedSegments.find(\n\t\t\t\t\t\t\t\t(s) => s.kind === \"text-run\" && s.startIndex === seg.startIndex && s.contentType === seg.contentType,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tif (!existing) {\n\t\t\t\t\t\t\t\tconst comp = new AssistantMessageComponent(\n\t\t\t\t\t\t\t\t\tundefined,\n\t\t\t\t\t\t\t\t\thost.hideThinkingBlock,\n\t\t\t\t\t\t\t\t\thost.getMarkdownThemeWithSettings(),\n\t\t\t\t\t\t\t\t\thost.settingsManager.getTimestampFormat(),\n\t\t\t\t\t\t\t\t\t{ startIndex: seg.startIndex, endIndex: seg.endIndex },\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\thost.chatContainer.addChild(comp);\n\t\t\t\t\t\t\t\trenderedSegments.push({\n\t\t\t\t\t\t\t\t\tkind: \"text-run\",\n\t\t\t\t\t\t\t\t\tstartIndex: seg.startIndex,\n\t\t\t\t\t\t\t\t\tendIndex: seg.endIndex,\n\t\t\t\t\t\t\t\t\tcontentType: seg.contentType,\n\t\t\t\t\t\t\t\t\tcomponent: comp,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\thost.streamingComponent = comp;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Update all trailing text-run segments with the latest message so\n\t\t\t\t\t// streaming text grows in place.\n\t\t\t\t\tfor (const seg of renderedSegments) {\n\t\t\t\t\t\tif (seg.kind === \"text-run\") {\n\t\t\t\t\t\t\t// Find corresponding desired segment to get current endIndex\n\t\t\t\t\t\t\tconst d = desired.find(\n\t\t\t\t\t\t\t\t(ds) => ds.kind === \"text-run\" && ds.startIndex === seg.startIndex && ds.contentType === seg.contentType,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tif (d && d.kind === \"text-run\" && d.endIndex !== seg.endIndex) {\n\t\t\t\t\t\t\t\tseg.endIndex = d.endIndex;\n\t\t\t\t\t\t\t\tseg.component.setRange({ startIndex: seg.startIndex, endIndex: seg.endIndex });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tseg.component.updateContent(host.streamingMessage);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\t// Keep streamingComponent pointing at the last text-run for message_end compatibility.\n\t\t\t\t\tconst lastTextSeg = [...renderedSegments].reverse().find((s) => s.kind === \"text-run\");\n\t\t\t\t\tif (lastTextSeg && lastTextSeg.kind === \"text-run\") {\n\t\t\t\t\t\thost.streamingComponent = lastTextSeg.component;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Update index: fully processed blocks won't need re-scanning.\n\t\t\t\t// Keep the last block's index (it may still be accumulating data),\n\t\t\t\t// so we re-check it next time but skip all earlier ones.\n\t\t\t\tif (contentBlocks.length > 0) {\n\t\t\t\t\tlastProcessedContentIndex = Math.max(0, contentBlocks.length - 1);\n\t\t\t\t}\n\n\t\t\t\t// Pinned message: mirror the latest assistant text above the editor\n\t\t\t\t// when tool executions push it out of the viewport.\n\t\t\t\tconst hasTools = contentBlocks.some(\n\t\t\t\t\t(c: any) => c.type === \"toolCall\" || c.type === \"serverToolUse\",\n\t\t\t\t);\n\t\t\t\tif (hasTools) hasToolsInTurn = true;\n\n\t\t\t\tif (hasToolsInTurn) {\n\t\t\t\t\tconst latestText = findLatestPinnableText(contentBlocks);\n\n\t\t\t\t\tif (latestText && latestText !== lastPinnedText) {\n\t\t\t\t\t\tlastPinnedText = latestText;\n\n\t\t\t\t\t\tif (!pinnedBorder) {\n\t\t\t\t\t\t\t// First time: create border + text component\n\t\t\t\t\t\t\thost.pinnedMessageContainer.clear();\n\t\t\t\t\t\t\tpinnedBorder = new DynamicBorder(\n\t\t\t\t\t\t\t\t(str: string) => theme.fg(\"dim\", str),\n\t\t\t\t\t\t\t\t\"Working · Latest Output\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tpinnedBorder.startSpinner(host.ui, (str: string) => theme.fg(\"accent\", str));\n\t\t\t\t\t\t\thost.pinnedMessageContainer.addChild(pinnedBorder);\n\t\t\t\t\t\t\tpinnedTextComponent = new Markdown(latestText, 1, 0, host.getMarkdownThemeWithSettings());\n\t\t\t\t\t\t\t// Cap pinned content to ~40% of terminal height so tall output\n\t\t\t\t\t\t\t// doesn't exceed the viewport and cause render flashing.\n\t\t\t\t\t\t\tpinnedTextComponent.maxLines = Math.max(3, Math.floor(host.ui.terminal.rows * 0.4));\n\t\t\t\t\t\t\thost.pinnedMessageContainer.addChild(pinnedTextComponent);\n\t\t\t\t\t\t\t// Hide the separate status loader — the pinned zone replaces it\n\t\t\t\t\t\t\tif (host.loadingAnimation) {\n\t\t\t\t\t\t\t\thost.loadingAnimation.stop();\n\t\t\t\t\t\t\t\thost.loadingAnimation = undefined;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\thost.statusContainer.clear();\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Update existing markdown component in-place\n\t\t\t\t\t\t\tpinnedTextComponent?.setText(latestText);\n\t\t\t\t\t\t\t// Refresh maxLines in case terminal was resized\n\t\t\t\t\t\t\tif (pinnedTextComponent) {\n\t\t\t\t\t\t\t\tpinnedTextComponent.maxLines = Math.max(3, Math.floor(host.ui.terminal.rows * 0.4));\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase \"message_end\":\n\t\t\tif (event.message.role === \"user\") break;\n\t\t\tif (event.message.role === \"assistant\") {\n\t\t\t\thost.streamingMessage = event.message;\n\t\t\t\tlet errorMessage: string | undefined;\n\t\t\t\tif (host.streamingMessage.stopReason === \"aborted\") {\n\t\t\t\t\tconst retryAttempt = host.session.retryAttempt;\n\t\t\t\t\terrorMessage = retryAttempt > 0\n\t\t\t\t\t\t? `Aborted after ${retryAttempt} retry attempt${retryAttempt > 1 ? \"s\" : \"\"}`\n\t\t\t\t\t\t: \"Operation aborted\";\n\t\t\t\t\thost.streamingMessage.errorMessage = errorMessage;\n\t\t\t\t}\n\n\t\t\t\tconst shouldRenderAssistant = hasVisibleAssistantContent(host.streamingMessage)\n\t\t\t\t\t|| (\n\t\t\t\t\t\t(host.streamingMessage.stopReason === \"aborted\" || host.streamingMessage.stopReason === \"error\")\n\t\t\t\t\t\t&& !hasAssistantToolBlocks(host.streamingMessage)\n\t\t\t\t\t);\n\t\t\t\tif (!host.streamingComponent && shouldRenderAssistant) {\n\t\t\t\t\thost.streamingComponent = new AssistantMessageComponent(\n\t\t\t\t\t\tundefined,\n\t\t\t\t\t\thost.hideThinkingBlock,\n\t\t\t\t\t\thost.getMarkdownThemeWithSettings(),\n\t\t\t\t\t\thost.settingsManager.getTimestampFormat(),\n\t\t\t\t\t);\n\t\t\t\t\thost.chatContainer.addChild(host.streamingComponent);\n\t\t\t\t}\n\t\t\t\tif (host.streamingComponent) {\n\t\t\t\t\thost.streamingComponent.setShowMetadata(true);\n\t\t\t\t\thost.streamingComponent.updateContent(host.streamingMessage);\n\t\t\t\t}\n\n\t\t\t\tif (host.streamingMessage.stopReason === \"aborted\" || host.streamingMessage.stopReason === \"error\") {\n\t\t\t\t\tif (!errorMessage) {\n\t\t\t\t\t\terrorMessage = host.streamingMessage.errorMessage || \"Error\";\n\t\t\t\t\t}\n\t\t\t\t\tconst pendingComponents = Array.from(host.pendingTools.values());\n\t\t\t\t\tif (pendingComponents.length > 0) {\n\t\t\t\t\t\tconst [first, ...rest] = pendingComponents;\n\t\t\t\t\t\tfirst.completeWithError(errorMessage);\n\t\t\t\t\t\tfor (const component of rest) {\n\t\t\t\t\t\t\tcomponent.completeWithError();\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\thost.pendingTools.clear();\n\t\t\t\t} else {\n\t\t\t\t\tfor (const [, component] of host.pendingTools.entries()) {\n\t\t\t\t\t\tcomponent.setArgsComplete();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\thost.streamingComponent = undefined;\n\t\t\t\thost.streamingMessage = undefined;\n\t\t\t\trenderedSegments = [];\n\t\t\t\torphanedSegments = [];\n\t\t\t\tlastContentLength = 0;\n\t\t\t\t// Clear pinned output once the message is finalized in the chat\n\t\t\t\t// container — prevents duplicate display when the agent continues\n\t\t\t\t// (e.g. form elicitation) after the assistant message ends.\n\t\t\t\tif (pinnedBorder) pinnedBorder.stopSpinner();\n\t\t\t\thost.pinnedMessageContainer.clear();\n\t\t\t\tlastPinnedText = \"\";\n\t\t\t\thasToolsInTurn = false;\n\t\t\t\tpinnedBorder = undefined;\n\t\t\t\tpinnedTextComponent = undefined;\n\t\t\t\thost.footer.invalidate();\n\t\t\t}\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"tool_execution_start\":\n\t\t\tif (!host.pendingTools.has(event.toolCallId)) {\n\t\t\t\tconst component = new ToolExecutionComponent(\n\t\t\t\t\tevent.toolName,\n\t\t\t\t\tevent.args,\n\t\t\t\t\t{ showImages: host.settingsManager.getShowImages() },\n\t\t\t\t\thost.getRegisteredToolDefinition(event.toolName),\n\t\t\t\t\thost.ui,\n\t\t\t\t);\n\t\t\t\tcomponent.setExpanded(host.toolOutputExpanded);\n\t\t\t\thost.chatContainer.addChild(component);\n\t\t\t\thost.pendingTools.set(event.toolCallId, component);\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase \"tool_execution_update\": {\n\t\t\tconst component = host.pendingTools.get(event.toolCallId);\n\t\t\tif (component) {\n\t\t\t\tcomponent.updateResult({ ...event.partialResult, isError: false }, true);\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\tcase \"tool_execution_end\": {\n\t\t\tconst component = host.pendingTools.get(event.toolCallId);\n\t\t\tif (component) {\n\t\t\t\tcomponent.updateResult({ ...event.result, isError: event.isError });\n\t\t\t\thost.pendingTools.delete(event.toolCallId);\n\t\t\t\thost.ui.requestRender();\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\tcase \"agent_end\":\n\t\t\tif (host.loadingAnimation) {\n\t\t\t\thost.loadingAnimation.stop();\n\t\t\t\thost.loadingAnimation = undefined;\n\t\t\t\thost.statusContainer.clear();\n\t\t\t}\n\t\t\tif (host.streamingComponent && host.streamingMessage) {\n\t\t\t\thost.streamingComponent.setShowMetadata(true);\n\t\t\t\thost.streamingComponent.updateContent(host.streamingMessage);\n\t\t\t}\n\t\t\thost.streamingComponent = undefined;\n\t\t\thost.streamingMessage = undefined;\n\t\t\trenderedSegments = [];\n\t\t\torphanedSegments = [];\n\t\t\tlastContentLength = 0;\n\t\t\thost.pendingTools.clear();\n\t\t\t// Pinned output is only useful while work is actively streaming.\n\t\t\t// Keep chat history as the single source after completion.\n\t\t\tif (pinnedBorder) {\n\t\t\t\tpinnedBorder.stopSpinner();\n\t\t\t}\n\t\t\thost.pinnedMessageContainer.clear();\n\t\t\tlastPinnedText = \"\";\n\t\t\thasToolsInTurn = false;\n\t\t\tpinnedBorder = undefined;\n\t\t\tpinnedTextComponent = undefined;\n\t\t\tawait host.checkShutdownRequested();\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_compaction_start\":\n\t\t\thost.autoCompactionEscapeHandler = host.defaultEditor.onEscape;\n\t\t\thost.defaultEditor.onEscape = () => host.session.abortCompaction();\n\t\t\thost.statusContainer.clear();\n\t\t\thost.autoCompactionLoader = new Loader(\n\t\t\t\thost.ui,\n\t\t\t\t(spinner) => theme.fg(\"accent\", spinner),\n\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\t`${event.reason === \"overflow\" ? \"Context overflow detected, \" : \"\"}Auto-compacting... (${appKey(host.keybindings, \"interrupt\")} to cancel)`,\n\t\t\t);\n\t\t\thost.statusContainer.addChild(host.autoCompactionLoader);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_compaction_end\":\n\t\t\tif (host.autoCompactionEscapeHandler) {\n\t\t\t\thost.defaultEditor.onEscape = host.autoCompactionEscapeHandler;\n\t\t\t\thost.autoCompactionEscapeHandler = undefined;\n\t\t\t}\n\t\t\tif (host.autoCompactionLoader) {\n\t\t\t\thost.autoCompactionLoader.stop();\n\t\t\t\thost.autoCompactionLoader = undefined;\n\t\t\t\thost.statusContainer.clear();\n\t\t\t}\n\t\t\tif (event.aborted) {\n\t\t\t\thost.showStatus(\"Auto-compaction cancelled\");\n\t\t\t} else if (event.result) {\n\t\t\t\thost.chatContainer.clear();\n\t\t\t\thost.rebuildChatFromMessages();\n\t\t\t\thost.addMessageToChat({\n\t\t\t\t\trole: \"compactionSummary\",\n\t\t\t\t\ttokensBefore: event.result.tokensBefore,\n\t\t\t\t\tsummary: event.result.summary,\n\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t});\n\t\t\t\thost.footer.invalidate();\n\t\t\t} else if (event.errorMessage) {\n\t\t\t\thost.chatContainer.addChild(new Spacer(1));\n\t\t\t\thost.chatContainer.addChild(new Text(theme.fg(\"error\", event.errorMessage), 1, 0));\n\t\t\t}\n\t\t\tvoid host.flushCompactionQueue({ willRetry: event.willRetry });\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_retry_start\":\n\t\t\thost.retryEscapeHandler = host.defaultEditor.onEscape;\n\t\t\thost.defaultEditor.onEscape = () => host.session.abortRetry();\n\t\t\thost.statusContainer.clear();\n\t\t\thost.retryLoader = new Loader(\n\t\t\t\thost.ui,\n\t\t\t\t(spinner) => theme.fg(\"warning\", spinner),\n\t\t\t\t(text) => theme.fg(\"muted\", text),\n\t\t\t\t`Retrying (${event.attempt}/${event.maxAttempts}) in ${Math.round(event.delayMs / 1000)}s... (${appKey(host.keybindings, \"interrupt\")} to cancel)`,\n\t\t\t);\n\t\t\thost.statusContainer.addChild(host.retryLoader);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"auto_retry_end\":\n\t\t\tif (host.retryEscapeHandler) {\n\t\t\t\thost.defaultEditor.onEscape = host.retryEscapeHandler;\n\t\t\t\thost.retryEscapeHandler = undefined;\n\t\t\t}\n\t\t\tif (host.retryLoader) {\n\t\t\t\thost.retryLoader.stop();\n\t\t\t\thost.retryLoader = undefined;\n\t\t\t\thost.statusContainer.clear();\n\t\t\t}\n\t\t\tif (!event.success) {\n\t\t\t\thost.showError(`Retry failed after ${event.attempt} attempts: ${event.finalError || \"Unknown error\"}`);\n\t\t\t}\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"fallback_provider_switch\":\n\t\t\thost.showStatus(`Switched from ${event.from} → ${event.to} (${event.reason})`);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"fallback_provider_restored\":\n\t\t\thost.showStatus(`Restored to ${event.provider}`);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"fallback_chain_exhausted\":\n\t\t\thost.showError(event.reason);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\n\t\tcase \"image_overflow_recovery\":\n\t\t\thost.showStatus(\n\t\t\t\t`Removed ${event.strippedCount} older image(s) to comply with API limits. Retrying...`,\n\t\t\t);\n\t\t\thost.ui.requestRender();\n\t\t\tbreak;\n\t}\n}\n"]}
@@ -234,7 +234,7 @@ test("chat-controller renders serverToolUse before trailing text matching conten
234
234
  assert.equal(host.chatContainer.children[1]?.constructor?.name, "AssistantMessageComponent");
235
235
  });
236
236
 
237
- test("chat-controller drops provisional pre-tool text for claude-code MCP turns", async () => {
237
+ test("chat-controller keeps pre-tool prose visible until post-tool prose arrives, then prunes it", async () => {
238
238
  (globalThis as any)[Symbol.for("@gsd/pi-coding-agent:theme")] = {
239
239
  fg: (_key: string, text: string) => text,
240
240
  bg: (_key: string, text: string) => text,
@@ -273,7 +273,7 @@ test("chat-controller drops provisional pre-tool text for claude-code MCP turns"
273
273
  assert.equal(host.chatContainer.children.length, 1);
274
274
  assert.equal(host.chatContainer.children[0]?.constructor?.name, "AssistantMessageComponent");
275
275
 
276
- // MCP tool appears; provisional text should be removed from the chat stack.
276
+ // MCP tool appears; provisional text should remain visible until post-tool prose exists.
277
277
  await handleAgentEvent(
278
278
  host,
279
279
  {
@@ -294,11 +294,16 @@ test("chat-controller drops provisional pre-tool text for claude-code MCP turns"
294
294
  },
295
295
  } as any,
296
296
  );
297
- assert.equal(host.chatContainer.children.length, 1, "provisional pre-tool text should be pruned");
298
- assert.equal(host.chatContainer.children[0]?.constructor?.name, "ToolExecutionComponent");
297
+ assert.equal(host.chatContainer.children.length, 2, "pre-tool prose should remain during tool-only window");
298
+ assert.equal(host.chatContainer.children[0]?.constructor?.name, "AssistantMessageComponent");
299
+ assert.equal(host.chatContainer.children[1]?.constructor?.name, "ToolExecutionComponent");
299
300
 
300
- // Final assistant output should render below the tool.
301
- const finalContent = [mcpTool, { type: "text", text: "Which missing feature matters most to you?" }];
301
+ // Post-tool prose arrives: pre-tool prose should now be pruned.
302
+ const finalContent = [
303
+ { type: "text", text: "Let me inspect the workspace first." },
304
+ mcpTool,
305
+ { type: "text", text: "Which missing feature matters most to you?" },
306
+ ];
302
307
  await handleAgentEvent(
303
308
  host,
304
309
  {
@@ -306,7 +311,7 @@ test("chat-controller drops provisional pre-tool text for claude-code MCP turns"
306
311
  message: makeAssistant(finalContent),
307
312
  assistantMessageEvent: {
308
313
  type: "text_delta",
309
- contentIndex: 1,
314
+ contentIndex: 2,
310
315
  delta: "Which missing feature matters most to you?",
311
316
  partial: makeAssistant(finalContent),
312
317
  },
@@ -320,6 +325,73 @@ test("chat-controller drops provisional pre-tool text for claude-code MCP turns"
320
325
  await handleAgentEvent(host, { type: "message_end", message: makeAssistant(finalContent) } as any);
321
326
  });
322
327
 
328
+ test("chat-controller keeps pre-tool thinking visible for claude-code MCP turns without post-tool prose", async () => {
329
+ (globalThis as any)[Symbol.for("@gsd/pi-coding-agent:theme")] = {
330
+ fg: (_key: string, text: string) => text,
331
+ bg: (_key: string, text: string) => text,
332
+ bold: (text: string) => text,
333
+ italic: (text: string) => text,
334
+ truncate: (text: string) => text,
335
+ };
336
+
337
+ const host = createHost();
338
+ host.getMarkdownThemeWithSettings = () => ({});
339
+
340
+ const mcpTool = {
341
+ type: "toolCall",
342
+ id: "mcp-tool-thinking-1",
343
+ name: "read",
344
+ mcpServer: "filesystem",
345
+ arguments: { filePath: "/tmp/demo.txt" },
346
+ };
347
+
348
+ await handleAgentEvent(host, { type: "message_start", message: makeAssistant([]) } as any);
349
+
350
+ const thinkingOnly = [{ type: "thinking", thinking: "I should inspect the workspace." }];
351
+ await handleAgentEvent(
352
+ host,
353
+ {
354
+ type: "message_update",
355
+ message: makeAssistant(thinkingOnly),
356
+ assistantMessageEvent: {
357
+ type: "thinking_delta",
358
+ contentIndex: 0,
359
+ delta: "I should inspect the workspace.",
360
+ partial: makeAssistant(thinkingOnly),
361
+ },
362
+ } as any,
363
+ );
364
+ assert.equal(host.chatContainer.children.length, 1);
365
+ assert.equal(host.chatContainer.children[0]?.constructor?.name, "AssistantMessageComponent");
366
+
367
+ await handleAgentEvent(
368
+ host,
369
+ {
370
+ type: "message_update",
371
+ message: makeAssistant([thinkingOnly[0], mcpTool]),
372
+ assistantMessageEvent: {
373
+ type: "toolcall_end",
374
+ contentIndex: 1,
375
+ toolCall: {
376
+ ...mcpTool,
377
+ externalResult: {
378
+ content: [{ type: "text", text: "file preview" }],
379
+ details: {},
380
+ isError: false,
381
+ },
382
+ },
383
+ partial: makeAssistant([thinkingOnly[0], mcpTool]),
384
+ },
385
+ } as any,
386
+ );
387
+
388
+ assert.equal(host.chatContainer.children.length, 2, "thinking should remain visible while only tool output is present");
389
+ assert.equal(host.chatContainer.children[0]?.constructor?.name, "AssistantMessageComponent");
390
+ assert.equal(host.chatContainer.children[1]?.constructor?.name, "ToolExecutionComponent");
391
+
392
+ await handleAgentEvent(host, { type: "message_end", message: makeAssistant([thinkingOnly[0], mcpTool]) } as any);
393
+ });
394
+
323
395
  test("chat-controller prunes orphaned provisional text after claude-code sub-turn shrink when MCP tools appear", async () => {
324
396
  (globalThis as any)[Symbol.for("@gsd/pi-coding-agent:theme")] = {
325
397
  fg: (_key: string, text: string) => text,
@@ -374,7 +446,7 @@ test("chat-controller prunes orphaned provisional text after claude-code sub-tur
374
446
  );
375
447
  assert.equal(host.chatContainer.children.length, 2, "shrink keeps prior text until MCP tool context appears");
376
448
 
377
- // MCP tool appears in sub-turn 2: both old orphaned text and current pre-tool text should be pruned.
449
+ // MCP tool appears in sub-turn 2: tool-only windows keep provisional prose visible.
378
450
  await handleAgentEvent(
379
451
  host,
380
452
  {
@@ -395,8 +467,10 @@ test("chat-controller prunes orphaned provisional text after claude-code sub-tur
395
467
  },
396
468
  } as any,
397
469
  );
398
- assert.equal(host.chatContainer.children.length, 1, "stale text runs should be removed once MCP tool is present");
399
- assert.equal(host.chatContainer.children[0]?.constructor?.name, "ToolExecutionComponent");
470
+ assert.equal(host.chatContainer.children.length, 3, "stale text runs are deferred until post-tool prose arrives");
471
+ assert.equal(host.chatContainer.children[0]?.constructor?.name, "AssistantMessageComponent");
472
+ assert.equal(host.chatContainer.children[1]?.constructor?.name, "AssistantMessageComponent");
473
+ assert.equal(host.chatContainer.children[2]?.constructor?.name, "ToolExecutionComponent");
400
474
 
401
475
  const finalContent = [mcpTool, { type: "text", text: "Final visible question?" }];
402
476
  await handleAgentEvent(
@@ -89,6 +89,10 @@ export class AssistantMessageComponent extends Container {
89
89
  );
90
90
  const hasTextContent = message.content.some((c) => c.type === "text" && c.text.trim().length > 0);
91
91
  const hasToolContent = message.content.some((c) => c.type === "toolCall" || c.type === "serverToolUse");
92
+ // Claude Code often emits long reasoning blocks ahead of user-visible text/tool
93
+ // output in the same lifecycle. Keep chat output visible without requiring a
94
+ // manual thinking toggle every turn.
95
+ const shouldCapThinking = hasTextContent || hasToolContent || message.provider === "claude-code";
92
96
 
93
97
  if (hasVisibleContent) {
94
98
  this.contentContainer.addChild(new Spacer(1));
@@ -122,7 +126,7 @@ export class AssistantMessageComponent extends Container {
122
126
  });
123
127
  // Keep visible chat output readable when thinking traces are long.
124
128
  // Tool-bearing turns can stream text in a later assistant message.
125
- if (hasTextContent || hasToolContent) {
129
+ if (shouldCapThinking) {
126
130
  thinkingMarkdown.maxLines = 8;
127
131
  }
128
132
  this.contentContainer.addChild(thinkingMarkdown);