lsd-pi 1.3.2 → 1.3.6

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 (169) hide show
  1. package/dist/resources/extensions/browser-tools/tools/codegen.js +5 -5
  2. package/dist/resources/extensions/browser-tools/tools/navigation.js +107 -178
  3. package/dist/resources/extensions/browser-tools/tools/network-mock.js +112 -167
  4. package/dist/resources/extensions/browser-tools/tools/pages.js +182 -234
  5. package/dist/resources/extensions/browser-tools/tools/refs.js +202 -461
  6. package/dist/resources/extensions/browser-tools/tools/session.js +176 -323
  7. package/dist/resources/extensions/browser-tools/tools/state-persistence.js +91 -154
  8. package/dist/resources/extensions/browser-tools/utils.js +1 -1
  9. package/dist/resources/extensions/slash-commands/extension-manifest.json +2 -2
  10. package/dist/resources/extensions/slash-commands/fast.js +73 -0
  11. package/dist/resources/extensions/slash-commands/index.js +2 -0
  12. package/dist/resources/extensions/slash-commands/plan.js +37 -12
  13. package/dist/resources/extensions/subagent/background-job-manager.js +13 -0
  14. package/dist/resources/extensions/subagent/in-process-runner.js +387 -0
  15. package/dist/resources/extensions/subagent/index.js +278 -626
  16. package/dist/resources/extensions/subagent/legacy-runner.js +503 -0
  17. package/dist/resources/extensions/voice/index.js +96 -36
  18. package/dist/resources/extensions/voice/push-to-talk.js +26 -0
  19. package/package.json +1 -1
  20. package/packages/pi-agent-core/dist/agent.d.ts +19 -0
  21. package/packages/pi-agent-core/dist/agent.d.ts.map +1 -1
  22. package/packages/pi-agent-core/dist/agent.js +16 -0
  23. package/packages/pi-agent-core/dist/agent.js.map +1 -1
  24. package/packages/pi-agent-core/src/agent.ts +32 -2
  25. package/packages/pi-ai/dist/providers/openai-codex-responses.d.ts +34 -1
  26. package/packages/pi-ai/dist/providers/openai-codex-responses.d.ts.map +1 -1
  27. package/packages/pi-ai/dist/providers/openai-codex-responses.js +32 -4
  28. package/packages/pi-ai/dist/providers/openai-codex-responses.js.map +1 -1
  29. package/packages/pi-ai/dist/providers/openai-codex-responses.test.js +127 -16
  30. package/packages/pi-ai/dist/providers/openai-codex-responses.test.js.map +1 -1
  31. package/packages/pi-ai/dist/providers/openai-responses.d.ts +8 -1
  32. package/packages/pi-ai/dist/providers/openai-responses.d.ts.map +1 -1
  33. package/packages/pi-ai/dist/providers/openai-responses.fast-mode.test.d.ts +2 -0
  34. package/packages/pi-ai/dist/providers/openai-responses.fast-mode.test.d.ts.map +1 -0
  35. package/packages/pi-ai/dist/providers/openai-responses.fast-mode.test.js +67 -0
  36. package/packages/pi-ai/dist/providers/openai-responses.fast-mode.test.js.map +1 -0
  37. package/packages/pi-ai/dist/providers/openai-responses.js +21 -3
  38. package/packages/pi-ai/dist/providers/openai-responses.js.map +1 -1
  39. package/packages/pi-ai/dist/providers/simple-options.d.ts.map +1 -1
  40. package/packages/pi-ai/dist/providers/simple-options.js +2 -0
  41. package/packages/pi-ai/dist/providers/simple-options.js.map +1 -1
  42. package/packages/pi-ai/dist/types.d.ts +5 -0
  43. package/packages/pi-ai/dist/types.d.ts.map +1 -1
  44. package/packages/pi-ai/dist/types.js.map +1 -1
  45. package/packages/pi-ai/src/providers/openai-codex-responses.test.ts +143 -20
  46. package/packages/pi-ai/src/providers/openai-codex-responses.ts +47 -4
  47. package/packages/pi-ai/src/providers/openai-responses.fast-mode.test.ts +73 -0
  48. package/packages/pi-ai/src/providers/openai-responses.ts +26 -3
  49. package/packages/pi-ai/src/providers/simple-options.ts +2 -0
  50. package/packages/pi-ai/src/types.ts +5 -0
  51. package/packages/pi-coding-agent/dist/core/keybindings.d.ts +1 -1
  52. package/packages/pi-coding-agent/dist/core/keybindings.d.ts.map +1 -1
  53. package/packages/pi-coding-agent/dist/core/keybindings.js +2 -0
  54. package/packages/pi-coding-agent/dist/core/keybindings.js.map +1 -1
  55. package/packages/pi-coding-agent/dist/core/sdk.d.ts.map +1 -1
  56. package/packages/pi-coding-agent/dist/core/sdk.js +4 -2
  57. package/packages/pi-coding-agent/dist/core/sdk.js.map +1 -1
  58. package/packages/pi-coding-agent/dist/core/settings-manager.d.ts +6 -0
  59. package/packages/pi-coding-agent/dist/core/settings-manager.d.ts.map +1 -1
  60. package/packages/pi-coding-agent/dist/core/settings-manager.fast-mode.test.d.ts +2 -0
  61. package/packages/pi-coding-agent/dist/core/settings-manager.fast-mode.test.d.ts.map +1 -0
  62. package/packages/pi-coding-agent/dist/core/settings-manager.fast-mode.test.js +35 -0
  63. package/packages/pi-coding-agent/dist/core/settings-manager.fast-mode.test.js.map +1 -0
  64. package/packages/pi-coding-agent/dist/core/settings-manager.js +12 -0
  65. package/packages/pi-coding-agent/dist/core/settings-manager.js.map +1 -1
  66. package/packages/pi-coding-agent/dist/core/slash-commands.d.ts.map +1 -1
  67. package/packages/pi-coding-agent/dist/core/slash-commands.js +1 -0
  68. package/packages/pi-coding-agent/dist/core/slash-commands.js.map +1 -1
  69. package/packages/pi-coding-agent/dist/core/system-prompt.d.ts.map +1 -1
  70. package/packages/pi-coding-agent/dist/core/system-prompt.js +6 -1
  71. package/packages/pi-coding-agent/dist/core/system-prompt.js.map +1 -1
  72. package/packages/pi-coding-agent/dist/core/tool-priority.d.ts +4 -0
  73. package/packages/pi-coding-agent/dist/core/tool-priority.d.ts.map +1 -0
  74. package/packages/pi-coding-agent/dist/core/tool-priority.js +18 -0
  75. package/packages/pi-coding-agent/dist/core/tool-priority.js.map +1 -0
  76. package/packages/pi-coding-agent/dist/core/tool-priority.test.d.ts +2 -0
  77. package/packages/pi-coding-agent/dist/core/tool-priority.test.d.ts.map +1 -0
  78. package/packages/pi-coding-agent/dist/core/tool-priority.test.js +27 -0
  79. package/packages/pi-coding-agent/dist/core/tool-priority.test.js.map +1 -0
  80. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-summary-line.test.d.ts +2 -0
  81. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-summary-line.test.d.ts.map +1 -0
  82. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-summary-line.test.js +26 -0
  83. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-summary-line.test.js.map +1 -0
  84. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.d.ts +45 -0
  85. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.d.ts.map +1 -0
  86. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.js +314 -0
  87. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.js.map +1 -0
  88. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.test.d.ts +2 -0
  89. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.test.d.ts.map +1 -0
  90. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.test.js +122 -0
  91. package/packages/pi-coding-agent/dist/modes/interactive/components/btw-overlay.test.js.map +1 -0
  92. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.d.ts +2 -0
  93. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.d.ts.map +1 -1
  94. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js +7 -0
  95. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js.map +1 -1
  96. package/packages/pi-coding-agent/dist/modes/interactive/components/settings-selector.d.ts +4 -0
  97. package/packages/pi-coding-agent/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  98. package/packages/pi-coding-agent/dist/modes/interactive/components/settings-selector.js +26 -2
  99. package/packages/pi-coding-agent/dist/modes/interactive/components/settings-selector.js.map +1 -1
  100. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.d.ts +6 -0
  101. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  102. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.js +18 -4
  103. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.js.map +1 -1
  104. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-summary-line.d.ts +13 -0
  105. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-summary-line.d.ts.map +1 -0
  106. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-summary-line.js +49 -0
  107. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-summary-line.js.map +1 -0
  108. package/packages/pi-coding-agent/dist/modes/interactive/controllers/__tests__/chat-controller.collapsed-tool-summary.test.d.ts +2 -0
  109. package/packages/pi-coding-agent/dist/modes/interactive/controllers/__tests__/chat-controller.collapsed-tool-summary.test.d.ts.map +1 -0
  110. package/packages/pi-coding-agent/dist/modes/interactive/controllers/__tests__/chat-controller.collapsed-tool-summary.test.js +197 -0
  111. package/packages/pi-coding-agent/dist/modes/interactive/controllers/__tests__/chat-controller.collapsed-tool-summary.test.js.map +1 -0
  112. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.d.ts.map +1 -1
  113. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js +97 -0
  114. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js.map +1 -1
  115. package/packages/pi-coding-agent/dist/modes/interactive/controllers/input-controller.d.ts.map +1 -1
  116. package/packages/pi-coding-agent/dist/modes/interactive/controllers/input-controller.js +7 -0
  117. package/packages/pi-coding-agent/dist/modes/interactive/controllers/input-controller.js.map +1 -1
  118. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode-state.d.ts +3 -0
  119. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode-state.d.ts.map +1 -1
  120. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode-state.js.map +1 -1
  121. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.d.ts +4 -0
  122. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  123. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.js +35 -0
  124. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.js.map +1 -1
  125. package/packages/pi-coding-agent/dist/modes/interactive/slash-command-handlers.d.ts.map +1 -1
  126. package/packages/pi-coding-agent/dist/modes/interactive/slash-command-handlers.js +41 -0
  127. package/packages/pi-coding-agent/dist/modes/interactive/slash-command-handlers.js.map +1 -1
  128. package/packages/pi-coding-agent/package.json +1 -1
  129. package/packages/pi-coding-agent/src/core/keybindings.ts +4 -1
  130. package/packages/pi-coding-agent/src/core/sdk.ts +4 -2
  131. package/packages/pi-coding-agent/src/core/settings-manager.fast-mode.test.ts +46 -0
  132. package/packages/pi-coding-agent/src/core/settings-manager.ts +18 -0
  133. package/packages/pi-coding-agent/src/core/slash-commands.ts +1 -0
  134. package/packages/pi-coding-agent/src/core/system-prompt.ts +6 -1
  135. package/packages/pi-coding-agent/src/core/tool-priority.test.ts +30 -0
  136. package/packages/pi-coding-agent/src/core/tool-priority.ts +17 -0
  137. package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/tool-summary-line.test.ts +31 -0
  138. package/packages/pi-coding-agent/src/modes/interactive/components/btw-overlay.test.ts +172 -0
  139. package/packages/pi-coding-agent/src/modes/interactive/components/btw-overlay.ts +402 -0
  140. package/packages/pi-coding-agent/src/modes/interactive/components/footer.ts +8 -0
  141. package/packages/pi-coding-agent/src/modes/interactive/components/settings-selector.ts +32 -2
  142. package/packages/pi-coding-agent/src/modes/interactive/components/tool-execution.ts +1154 -1136
  143. package/packages/pi-coding-agent/src/modes/interactive/components/tool-summary-line.ts +64 -0
  144. package/packages/pi-coding-agent/src/modes/interactive/controllers/__tests__/chat-controller.collapsed-tool-summary.test.ts +228 -0
  145. package/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts +494 -398
  146. package/packages/pi-coding-agent/src/modes/interactive/controllers/input-controller.ts +7 -0
  147. package/packages/pi-coding-agent/src/modes/interactive/interactive-mode-state.ts +3 -0
  148. package/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts +38 -0
  149. package/packages/pi-coding-agent/src/modes/interactive/slash-command-handlers.ts +60 -1
  150. package/pkg/package.json +1 -1
  151. package/src/resources/extensions/browser-tools/tools/codegen.ts +5 -5
  152. package/src/resources/extensions/browser-tools/tools/navigation.ts +118 -196
  153. package/src/resources/extensions/browser-tools/tools/network-mock.ts +114 -205
  154. package/src/resources/extensions/browser-tools/tools/pages.ts +183 -237
  155. package/src/resources/extensions/browser-tools/tools/refs.ts +193 -507
  156. package/src/resources/extensions/browser-tools/tools/session.ts +182 -321
  157. package/src/resources/extensions/browser-tools/tools/state-persistence.ts +94 -172
  158. package/src/resources/extensions/browser-tools/utils.ts +1 -1
  159. package/src/resources/extensions/slash-commands/extension-manifest.json +2 -2
  160. package/src/resources/extensions/slash-commands/fast.ts +89 -0
  161. package/src/resources/extensions/slash-commands/index.ts +2 -0
  162. package/src/resources/extensions/slash-commands/plan.ts +42 -12
  163. package/src/resources/extensions/subagent/background-job-manager.ts +28 -0
  164. package/src/resources/extensions/subagent/in-process-runner.ts +534 -0
  165. package/src/resources/extensions/subagent/index.ts +489 -799
  166. package/src/resources/extensions/subagent/legacy-runner.ts +607 -0
  167. package/src/resources/extensions/voice/index.ts +308 -238
  168. package/src/resources/extensions/voice/push-to-talk.ts +42 -0
  169. package/src/resources/extensions/voice/tests/push-to-talk.test.ts +109 -0
@@ -1,18 +1,19 @@
1
1
  import {
2
- Box,
3
- Container,
4
- getCapabilities,
5
- Image,
6
- imageFallback,
7
- Spacer,
8
- Text,
9
- type TUI,
10
- truncateToWidth,
2
+ Box,
3
+ Container,
4
+ getCapabilities,
5
+ Image,
6
+ imageFallback,
7
+ Spacer,
8
+ Text,
9
+ type TUI,
10
+ truncateToWidth,
11
11
  } from "@gsd/pi-tui";
12
12
  import stripAnsi from "strip-ansi";
13
13
  import type { ToolDefinition } from "../../../core/extensions/types.js";
14
14
  import { computeEditDiff, type EditDiffError, type EditDiffResult } from "../../../core/tools/edit-diff.js";
15
15
  import { allTools } from "../../../core/tools/index.js";
16
+ import { shouldCollapse } from "../../../core/tool-priority.js";
16
17
  import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize } from "../../../core/tools/truncate.js";
17
18
  import { convertToPng } from "../../../utils/image-convert.js";
18
19
  import { sanitizeBinaryOutput } from "../../../utils/shell.js";
@@ -41,7 +42,7 @@ const WRITE_PARTIAL_FULL_HIGHLIGHT_LINES = 50;
41
42
  * Replace tabs with spaces for consistent rendering
42
43
  */
43
44
  function replaceTabs(text: string): string {
44
- return text.replace(/\t/g, " ");
45
+ return text.replace(/\t/g, " ");
45
46
  }
46
47
 
47
48
  /**
@@ -49,14 +50,14 @@ function replaceTabs(text: string): string {
49
50
  * Keep tool arguments unchanged, sanitize only display text.
50
51
  */
51
52
  function normalizeDisplayText(text: string): string {
52
- return text.replace(/\r/g, "");
53
+ return text.replace(/\r/g, "");
53
54
  }
54
55
 
55
56
  /** Safely coerce value to string for display. Returns null if invalid type. */
56
57
  function str(value: unknown): string | null {
57
- if (typeof value === "string") return value;
58
- if (value == null) return "";
59
- return null; // Invalid type
58
+ if (typeof value === "string") return value;
59
+ if (value == null) return "";
60
+ return null; // Invalid type
60
61
  }
61
62
 
62
63
  /**
@@ -64,1138 +65,1155 @@ function str(value: unknown): string | null {
64
65
  * that the agent process cannot resolve (sudo blocked, EPERM on root-owned files, etc.).
65
66
  */
66
67
  function isBashPrivilegeError(output: string, command: string): boolean {
67
- // Already handled by OS sandbox-policy path
68
- if (/sandbox blocked this operation/i.test(output)) return false;
69
- // sudo explicitly rejected by OS
70
- if (/sudo.*operation not permitted/i.test(output)) return true;
71
- if (/command exited with code 126/i.test(output) && /sudo/i.test(output)) return true;
72
- // npm / other tools hitting root-owned cache files
73
- if (/cache folder contains root-owned files/i.test(output)) return true;
74
- // Generic EPERM when command involves sudo
75
- if (/EPERM|operation not permitted/i.test(output) && /sudo/i.test(command)) return true;
76
- // Permission denied when command involves sudo
77
- if (/permission denied/i.test(output) && /sudo/i.test(command)) return true;
78
- return false;
68
+ // Already handled by OS sandbox-policy path
69
+ if (/sandbox blocked this operation/i.test(output)) return false;
70
+ // sudo explicitly rejected by OS
71
+ if (/sudo.*operation not permitted/i.test(output)) return true;
72
+ if (/command exited with code 126/i.test(output) && /sudo/i.test(output)) return true;
73
+ // npm / other tools hitting root-owned cache files
74
+ if (/cache folder contains root-owned files/i.test(output)) return true;
75
+ // Generic EPERM when command involves sudo
76
+ if (/EPERM|operation not permitted/i.test(output) && /sudo/i.test(command)) return true;
77
+ // Permission denied when command involves sudo
78
+ if (/permission denied/i.test(output) && /sudo/i.test(command)) return true;
79
+ return false;
79
80
  }
80
81
 
81
82
  export interface ToolExecutionOptions {
82
- showImages?: boolean; // default: true (only used if terminal supports images)
83
- renderMode?: "minimal" | "normal";
84
- editorScheme?: EditorScheme; // URI scheme for Cmd+click file links (default: "auto")
83
+ showImages?: boolean; // default: true (only used if terminal supports images)
84
+ renderMode?: "minimal" | "normal";
85
+ editorScheme?: EditorScheme; // URI scheme for Cmd+click file links (default: "auto")
85
86
  }
86
87
 
87
88
  type WriteHighlightCache = {
88
- rawPath: string | null;
89
- lang: string;
90
- rawContent: string;
91
- normalizedLines: string[];
92
- highlightedLines: string[];
89
+ rawPath: string | null;
90
+ lang: string;
91
+ rawContent: string;
92
+ normalizedLines: string[];
93
+ highlightedLines: string[];
93
94
  };
94
95
 
95
96
  /**
96
97
  * Component that renders a tool call with its result (updateable)
97
98
  */
98
99
  export class ToolExecutionComponent extends Container {
99
- private contentBox: Box; // Used for custom tools and bash visual truncation
100
- private contentText: Text; // For built-in tools (with its own padding/bg)
101
- private imageComponents: Image[] = [];
102
- private imageSpacers: Spacer[] = [];
103
- private toolName: string;
104
- private args: any;
105
- private expanded = false;
106
- private renderMode: "minimal" | "normal";
107
- private showImages: boolean;
108
- private editorScheme: EditorScheme;
109
- private isPartial = true;
110
- private toolDefinition?: ToolDefinition;
111
- private ui: TUI;
112
- private cwd: string;
113
- private result?: {
114
- content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;
115
- isError: boolean;
116
- details?: any;
117
- };
118
- // Cached edit diff preview (computed when args arrive, before tool executes)
119
- private editDiffPreview?: EditDiffResult | EditDiffError;
120
- private editDiffArgsKey?: string; // Track which args the preview is for
121
- // Cached converted images for Kitty protocol (which requires PNG), keyed by index
122
- private convertedImages: Map<number, { data: string; mimeType: string }> = new Map();
123
- // Incremental syntax highlighting cache for write tool call args
124
- private writeHighlightCache?: WriteHighlightCache;
125
- // When true, this component intentionally renders no lines
126
- private hideComponent = false;
127
-
128
- // Tool status spinner state
129
- private spinnerTimer: NodeJS.Timeout | null = null;
130
- private spinnerFrame = 0;
131
-
132
- constructor(
133
- toolName: string,
134
- args: any,
135
- options: ToolExecutionOptions = {},
136
- toolDefinition: ToolDefinition | undefined,
137
- ui: TUI,
138
- cwd: string = process.cwd(),
139
- ) {
140
- super();
141
- this.toolName = toolName;
142
- this.args = args;
143
- this.showImages = options.showImages ?? true;
144
- this.renderMode = options.renderMode ?? "normal";
145
- this.editorScheme = options.editorScheme ?? "auto";
146
- this.toolDefinition = toolDefinition;
147
- this.ui = ui;
148
- this.cwd = cwd;
149
-
150
- // Always create both - contentBox for custom tools/bash, contentText for other built-ins
151
- this.contentBox = new Box(1, 0, (text: string) => theme.bg("toolPendingBg", text));
152
- this.contentText = new Text("", 1, 0, (text: string) => theme.bg("toolPendingBg", text));
153
-
154
- // Use contentBox for bash (visual truncation) or custom tools with custom renderers
155
- // Use contentText for built-in tools (including overrides without custom renderers)
156
- if (toolName === "bash" || (toolDefinition && !this.shouldUseBuiltInRenderer())) {
157
- this.addChild(this.contentBox);
158
- } else {
159
- this.addChild(this.contentText);
160
- }
161
-
162
- this.updateDisplay();
163
- }
164
-
165
- /**
166
- * Check if we should use built-in rendering for this tool.
167
- * Returns true if the tool name is a built-in AND either there's no toolDefinition
168
- * or the toolDefinition doesn't provide custom renderers.
169
- */
170
- private shouldUseBuiltInRenderer(): boolean {
171
- const isBuiltInName = this.toolName in allTools;
172
- const hasCustomRenderers = this.toolDefinition?.renderCall || this.toolDefinition?.renderResult;
173
- return isBuiltInName && !hasCustomRenderers;
174
- }
175
-
176
- updateArgs(args: any): void {
177
- this.args = args;
178
- if (this.toolName === "write" && this.isPartial) {
179
- this.updateWriteHighlightCacheIncremental();
180
- }
181
- this.updateDisplay();
182
- }
183
-
184
- private highlightSingleLine(line: string, lang: string): string {
185
- const highlighted = highlightCode(line, lang);
186
- return highlighted[0] ?? "";
187
- }
188
-
189
- private refreshWriteHighlightPrefix(cache: WriteHighlightCache): void {
190
- const prefixCount = Math.min(WRITE_PARTIAL_FULL_HIGHLIGHT_LINES, cache.normalizedLines.length);
191
- if (prefixCount === 0) return;
192
-
193
- const prefixSource = cache.normalizedLines.slice(0, prefixCount).join("\n");
194
- const prefixHighlighted = highlightCode(prefixSource, cache.lang);
195
- for (let i = 0; i < prefixCount; i++) {
196
- cache.highlightedLines[i] =
197
- prefixHighlighted[i] ?? this.highlightSingleLine(cache.normalizedLines[i] ?? "", cache.lang);
198
- }
199
- }
200
-
201
- private rebuildWriteHighlightCacheFull(rawPath: string | null, fileContent: string): void {
202
- const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
203
- if (!lang) {
204
- this.writeHighlightCache = undefined;
205
- return;
206
- }
207
-
208
- const displayContent = normalizeDisplayText(fileContent);
209
- const normalized = replaceTabs(displayContent);
210
- this.writeHighlightCache = {
211
- rawPath,
212
- lang,
213
- rawContent: fileContent,
214
- normalizedLines: normalized.split("\n"),
215
- highlightedLines: highlightCode(normalized, lang),
216
- };
217
- }
218
-
219
- private updateWriteHighlightCacheIncremental(): void {
220
- const rawPath = str(this.args?.file_path ?? this.args?.path);
221
- const fileContent = str(this.args?.content);
222
- if (rawPath === null || fileContent === null) {
223
- this.writeHighlightCache = undefined;
224
- return;
225
- }
226
-
227
- const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
228
- if (!lang) {
229
- this.writeHighlightCache = undefined;
230
- return;
231
- }
232
-
233
- if (!this.writeHighlightCache) {
234
- this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
235
- return;
236
- }
237
-
238
- const cache = this.writeHighlightCache;
239
- if (cache.lang !== lang || cache.rawPath !== rawPath) {
240
- this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
241
- return;
242
- }
243
-
244
- if (!fileContent.startsWith(cache.rawContent)) {
245
- this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
246
- return;
247
- }
248
-
249
- if (fileContent.length === cache.rawContent.length) {
250
- return;
251
- }
252
-
253
- const deltaRaw = fileContent.slice(cache.rawContent.length);
254
- const deltaDisplay = normalizeDisplayText(deltaRaw);
255
- const deltaNormalized = replaceTabs(deltaDisplay);
256
- cache.rawContent = fileContent;
257
-
258
- if (cache.normalizedLines.length === 0) {
259
- cache.normalizedLines.push("");
260
- cache.highlightedLines.push("");
261
- }
262
-
263
- const segments = deltaNormalized.split("\n");
264
- const lastIndex = cache.normalizedLines.length - 1;
265
- cache.normalizedLines[lastIndex] += segments[0];
266
- cache.highlightedLines[lastIndex] = this.highlightSingleLine(cache.normalizedLines[lastIndex], cache.lang);
267
-
268
- for (let i = 1; i < segments.length; i++) {
269
- cache.normalizedLines.push(segments[i]);
270
- cache.highlightedLines.push(this.highlightSingleLine(segments[i], cache.lang));
271
- }
272
-
273
- this.refreshWriteHighlightPrefix(cache);
274
- }
275
-
276
- /**
277
- * Signal that args are complete (tool is about to execute).
278
- * This triggers diff computation for edit tool.
279
- */
280
- setArgsComplete(): void {
281
- if (this.toolName === "write") {
282
- const rawPath = str(this.args?.file_path ?? this.args?.path);
283
- const fileContent = str(this.args?.content);
284
- if (rawPath !== null && fileContent !== null) {
285
- this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
286
- }
287
- }
288
- this.maybeComputeEditDiff();
289
- }
290
-
291
- /**
292
- * Compute edit diff preview when we have complete args.
293
- * This runs async and updates display when done.
294
- */
295
- private maybeComputeEditDiff(): void {
296
- if (this.toolName !== "edit") return;
297
-
298
- const path = this.args?.path;
299
- const oldText = this.args?.oldText;
300
- const newText = this.args?.newText;
301
-
302
- // Need all three params to compute diff
303
- if (!path || oldText === undefined || newText === undefined) return;
304
-
305
- // Create a key to track which args this computation is for
306
- const argsKey = JSON.stringify({ path, oldText, newText });
307
-
308
- // Skip if we already computed for these exact args
309
- if (this.editDiffArgsKey === argsKey) return;
310
-
311
- this.editDiffArgsKey = argsKey;
312
-
313
- // Compute diff async
314
- computeEditDiff(path, oldText, newText, this.cwd).then((result) => {
315
- // Only update if args haven't changed since we started
316
- if (this.editDiffArgsKey === argsKey) {
317
- this.editDiffPreview = result;
318
- this.updateDisplay();
319
- this.ui.requestRender();
320
- }
321
- });
322
- }
323
-
324
- updateResult(
325
- result: {
326
- content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;
327
- details?: any;
328
- isError: boolean;
329
- },
330
- isPartial = false,
331
- ): void {
332
- this.result = result;
333
- this.isPartial = isPartial;
334
-
335
- if (this.toolName === "write" && !isPartial) {
336
- const rawPath = str(this.args?.file_path ?? this.args?.path);
337
- const fileContent = str(this.args?.content);
338
- if (rawPath !== null && fileContent !== null) {
339
- this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
340
- }
341
- }
342
- this.updateDisplay();
343
- // Convert non-PNG images to PNG for Kitty protocol (async)
344
- this.maybeConvertImagesForKitty();
345
- }
346
-
347
- /**
348
- * Convert non-PNG images to PNG for Kitty graphics protocol.
349
- * Kitty requires PNG format (f=100), so JPEG/GIF/WebP won't display.
350
- */
351
- private maybeConvertImagesForKitty(): void {
352
- const caps = getCapabilities();
353
- // Only needed for Kitty protocol
354
- if (caps.images !== "kitty") return;
355
- if (!this.result) return;
356
-
357
- const imageBlocks = this.result.content?.filter((c: any) => c.type === "image") || [];
358
-
359
- for (let i = 0; i < imageBlocks.length; i++) {
360
- const img = imageBlocks[i];
361
- if (!img.data || !img.mimeType) continue;
362
- // Skip if already PNG or already converted
363
- if (img.mimeType === "image/png") continue;
364
- if (this.convertedImages.has(i)) continue;
365
-
366
- // Convert async
367
- const index = i;
368
- convertToPng(img.data, img.mimeType).then((converted) => {
369
- if (converted) {
370
- this.convertedImages.set(index, converted);
371
- this.updateDisplay();
372
- this.ui.requestRender();
373
- }
374
- });
375
- }
376
- }
377
-
378
- setExpanded(expanded: boolean): void {
379
- this.expanded = expanded;
380
- this.updateDisplay();
381
- }
382
-
383
- setRenderMode(mode: "minimal" | "normal"): void {
384
- if (this.renderMode !== mode) {
385
- this.renderMode = mode;
386
- this.updateDisplay();
387
- }
388
- }
389
-
390
- setEditorScheme(scheme: EditorScheme): void {
391
- this.editorScheme = scheme;
392
- this.updateDisplay();
393
- }
394
-
395
- setShowImages(show: boolean): void {
396
- this.showImages = show;
397
- this.updateDisplay();
398
- }
399
-
400
- dispose(): void {
401
- if (this.spinnerTimer) {
402
- clearInterval(this.spinnerTimer);
403
- this.spinnerTimer = null;
404
- }
405
- }
406
-
407
- override invalidate(): void {
408
- super.invalidate();
409
- this.updateDisplay();
410
- }
411
-
412
- override render(width: number): string[] {
413
- if (this.hideComponent) {
414
- return [];
415
- }
416
- return [...super.render(width), ""];
417
- }
418
-
419
- private updateDisplay(): void {
420
- // Status indicator with circle
421
- let statusIndicator = "";
422
- let statusColor = "";
423
-
424
- if (this.isPartial) {
425
- // Loading spinner - start timer on first partial render
426
- if (!this.spinnerTimer) {
427
- this.spinnerTimer = setInterval(() => {
428
- this.spinnerFrame = (this.spinnerFrame + 1) % SPINNER_FRAMES.length;
429
- this.updateDisplay();
430
- this.ui.requestRender();
431
- }, SPINNER_INTERVAL_MS);
432
- }
433
- statusIndicator = SPINNER_FRAMES[this.spinnerFrame];
434
- } else {
435
- // Stop spinner when no longer partial
436
- if (this.spinnerTimer) {
437
- clearInterval(this.spinnerTimer);
438
- this.spinnerTimer = null;
439
- }
440
- if (this.result?.isError) {
441
- // Failed - red circle
442
- statusIndicator = theme.fg("error", "●");
443
- statusColor = "error";
444
- } else {
445
- // Success - green circle
446
- statusIndicator = theme.fg("success", "●");
447
- statusColor = "success";
448
- }
449
- }
450
-
451
- const useBuiltInRenderer = this.shouldUseBuiltInRenderer();
452
- let customRendererHasContent = false;
453
- this.hideComponent = false;
454
-
455
- // Use built-in rendering for built-in tools (or overrides without custom renderers)
456
- if (useBuiltInRenderer) {
457
- if (this.toolName === "bash") {
458
- // Bash uses Box with visual line truncation - no background
459
- this.contentBox.setBgFn((text: string) => text);
460
- this.contentBox.clear();
461
- this.renderBashContent(statusIndicator);
462
- } else {
463
- // Other built-in tools: use Text directly with caching - no background
464
- this.contentText.setCustomBgFn((text: string) => text);
465
- this.contentText.setText(this.formatToolExecution(statusIndicator));
466
- }
467
- } else if (this.toolDefinition) {
468
- // Custom tools use Box for flexible component rendering - no background
469
- this.contentBox.setBgFn((text: string) => text);
470
- this.contentBox.clear();
471
-
472
- // Render call component
473
- if (this.toolDefinition.renderCall) {
474
- try {
475
- const callComponent = this.toolDefinition.renderCall(this.args, theme, { statusIndicator });
476
- if (callComponent !== undefined) {
477
- this.contentBox.addChild(callComponent);
478
- customRendererHasContent = true;
479
- }
480
- } catch {
481
- // Fall back to default on error
482
- this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0));
483
- customRendererHasContent = true;
484
- }
485
- } else {
486
- // No custom renderCall, show tool name
487
- this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0));
488
- customRendererHasContent = true;
489
- }
490
-
491
- // Render result component if we have a result
492
- if (this.result && this.toolDefinition.renderResult) {
493
- try {
494
- if (this.shouldHideCollapsedPreview()) {
495
- const output = this.getTextOutput();
496
- const hasDetails = output.trim().length > 0 || this.imageComponents.length > 0 || this.result.details !== undefined;
497
- const collapsedHint = this.collapsedHintWithPrefix("\n");
498
- if (hasDetails && collapsedHint) {
499
- this.contentBox.addChild(new Text(collapsedHint, 0, 0));
500
- customRendererHasContent = true;
501
- }
502
- } else {
503
- const resultComponent = this.toolDefinition.renderResult(
504
- { content: this.result.content as any, details: this.result.details },
505
- { expanded: this.expanded, isPartial: this.isPartial },
506
- theme,
507
- );
508
- if (resultComponent !== undefined) {
509
- this.contentBox.addChild(resultComponent);
510
- customRendererHasContent = true;
511
- // bg_shell: show /bg hint after start/list/digest actions
512
- if (this.toolName === 'bg_shell' && this.result && !this.isPartial) {
513
- const details = this.result.details as Record<string, unknown> | undefined;
514
- const action = details?.action as string | undefined;
515
- if (action === 'start' || action === 'list' || action === 'digest') {
516
- this.contentBox.addChild(new Text(
517
- '\n' + theme.fg('muted', 'Tip: run /bg to manage background processes'),
518
- 0, 0,
519
- ));
520
- }
521
- }
522
- }
523
- }
524
- } catch {
525
- // Fall back to showing raw output on error
526
- const output = this.getTextOutput();
527
- if (output) {
528
- this.contentBox.addChild(new Text(theme.fg("toolOutput", output), 0, 0));
529
- customRendererHasContent = true;
530
- }
531
- }
532
- } else if (this.result) {
533
- // Has result but no custom renderResult
534
- const output = this.getTextOutput();
535
- if (output) {
536
- if (this.shouldHideCollapsedPreview()) {
537
- const collapsedHint = this.collapsedHintWithPrefix("\n");
538
- if (collapsedHint) {
539
- this.contentBox.addChild(new Text(collapsedHint, 0, 0));
540
- }
541
- } else {
542
- this.contentBox.addChild(new Text(theme.fg("toolOutput", output), 0, 0));
543
- }
544
- customRendererHasContent = true;
545
- }
546
- }
547
- } else {
548
- // Unknown tool with no registered definition - show generic fallback
549
- this.contentText.setCustomBgFn((text: string) => text);
550
- this.contentText.setText(this.formatToolExecution(statusIndicator));
551
- }
552
-
553
- // Handle images (same for both custom and built-in)
554
- for (const img of this.imageComponents) {
555
- this.removeChild(img);
556
- }
557
- this.imageComponents = [];
558
- for (const spacer of this.imageSpacers) {
559
- this.removeChild(spacer);
560
- }
561
- this.imageSpacers = [];
562
-
563
- if (this.result) {
564
- const imageBlocks = this.result.content?.filter((c: any) => c.type === "image") || [];
565
- const caps = getCapabilities();
566
-
567
- for (let i = 0; i < imageBlocks.length; i++) {
568
- const img = imageBlocks[i];
569
- if (caps.images && this.showImages && img.data && img.mimeType) {
570
- // Use converted PNG for Kitty protocol if available
571
- const converted = this.convertedImages.get(i);
572
- const imageData = converted?.data ?? img.data;
573
- const imageMimeType = converted?.mimeType ?? img.mimeType;
574
-
575
- // For Kitty, skip non-PNG images that haven't been converted yet
576
- if (caps.images === "kitty" && imageMimeType !== "image/png") {
577
- continue;
578
- }
579
-
580
- const spacer = new Spacer(1);
581
- this.addChild(spacer);
582
- this.imageSpacers.push(spacer);
583
- const imageComponent = new Image(
584
- imageData,
585
- imageMimeType,
586
- { fallbackColor: (s: string) => theme.fg("toolOutput", s) },
587
- { maxWidthCells: 60 },
588
- );
589
- imageComponent.setOnDimensionsResolved(() => {
590
- this.updateDisplay();
591
- this.ui.requestRender();
592
- });
593
- this.imageComponents.push(imageComponent);
594
- this.addChild(imageComponent);
595
- }
596
- }
597
- }
598
-
599
- if (!useBuiltInRenderer && this.toolDefinition) {
600
- this.hideComponent = !customRendererHasContent && this.imageComponents.length === 0;
601
- }
602
- }
603
-
604
- /**
605
- * Render bash content using visual line truncation (like bash-execution.ts)
606
- */
607
- private renderBashContent(statusIndicator: string): void {
608
- const command = str(this.args?.command);
609
- const timeout = this.args?.timeout as number | undefined;
610
- const body = new Container();
611
-
612
- this.contentBox.addChild(body);
613
-
614
- const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : "";
615
- const sandboxBadge = this.result?.details?.sandboxed ? ` ${theme.fg("success", "[sandboxed]")}` : "";
616
- const toolLabel = theme.fg("toolTitle", theme.bold("bash"));
617
- const shellPrompt = theme.fg("muted", "$ ");
618
-
619
- // Always show only the first meaningful non-comment line in the header.
620
- // The full command is shown below the header when expanded.
621
- let commandDisplay: string;
622
- let commandLines: string[] = [];
623
- let isMultiLine = false;
624
- if (command === null) {
625
- commandDisplay = theme.fg("error", "[invalid arg]");
626
- } else if (!command) {
627
- commandDisplay = theme.fg("toolOutput", "...");
628
- } else {
629
- commandLines = command.split("\n");
630
- isMultiLine = commandLines.length > 1;
631
- const firstMeaningful = commandLines.find((l) => l.trim() && !l.trim().startsWith("#")) ?? commandLines[0];
632
- const suffix = isMultiLine ? theme.fg("muted", "") : "";
633
- commandDisplay = theme.fg("toolOutput", firstMeaningful ?? "") + suffix;
634
- }
635
-
636
- body.addChild(new Text(`${statusIndicator} ${toolLabel} ${shellPrompt}${commandDisplay}${timeoutSuffix}${sandboxBadge}`, 0, 0));
637
-
638
- // When expanded, show the full command below the header (only for multi-line commands)
639
- if (this.expanded && isMultiLine) {
640
- const fullCommand = commandLines.map((line) => theme.fg("toolOutput", line)).join("\n");
641
- body.addChild(new Text(`\n${fullCommand}`, 0, 0));
642
- }
643
-
644
- if (this.result) {
645
- const output = this.getTextOutput().trim();
646
-
647
- if (output) {
648
- const styledOutput = output
649
- .split("\n")
650
- .map((line) => theme.fg("toolOutput", line))
651
- .join("\n");
652
-
653
- if (this.expanded) {
654
- body.addChild(new Text(`\n${styledOutput}`, 0, 0));
655
- } else if (this.renderMode === "minimal") {
656
- const collapsedHint = this.collapsedHintWithPrefix("\n");
657
- if (collapsedHint) {
658
- body.addChild(new Text(collapsedHint, 1, 0));
659
- }
660
- } else {
661
- let cachedWidth: number | undefined;
662
- let cachedLines: string[] | undefined;
663
- let cachedSkipped: number | undefined;
664
-
665
- body.addChild({
666
- render: (width: number) => {
667
- if (cachedLines === undefined || cachedWidth !== width) {
668
- const result = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, Math.max(1, width - 1));
669
- cachedLines = result.visualLines.map((line) => ` ${line}`);
670
- cachedSkipped = result.skippedCount;
671
- cachedWidth = width;
672
- }
673
- if (cachedSkipped && cachedSkipped > 0) {
674
- const hint = theme.fg("muted", `... (${cachedSkipped} earlier lines)`);
675
- return ["", truncateToWidth(` ${hint}`, width, "..."), ...cachedLines];
676
- }
677
- return ["", ...cachedLines];
678
- },
679
- invalidate: () => {
680
- cachedWidth = undefined;
681
- cachedLines = undefined;
682
- cachedSkipped = undefined;
683
- },
684
- });
685
- }
686
- }
687
-
688
- const truncation = this.result.details?.truncation;
689
- const fullOutputPath = this.result.details?.fullOutputPath;
690
- if (truncation?.truncated || fullOutputPath) {
691
- const warnings: string[] = [];
692
- if (fullOutputPath) {
693
- warnings.push(`Full output: ${fullOutputPath}`);
694
- }
695
- if (truncation?.truncated) {
696
- if (truncation.truncatedBy === "lines") {
697
- warnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);
698
- } else {
699
- warnings.push(
700
- `Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`,
701
- );
702
- }
703
- }
704
- body.addChild(new Text(`\n${theme.fg("warning", `[${warnings.join(". ")}]`)}`, 0, 0));
705
- }
706
-
707
- // Privilege escalation error — agent cannot sudo, show manual step callout
708
- if (this.result.isError && command !== null && isBashPrivilegeError(output, command)) {
709
- body.addChild(
710
- new Text(
711
- `\n${theme.fg("warning", "⚠ This command requires elevated privileges the agent cannot use.")}\n${theme.fg("muted", `Run it manually in your terminal:\n ${command}`)}`,
712
- 0,
713
- 0,
714
- ),
715
- );
716
- }
717
- }
718
- }
719
-
720
- private getTextOutput(): string {
721
- if (!this.result) return "";
722
-
723
- const textBlocks = this.result.content?.filter((c: any) => c.type === "text") || [];
724
- const imageBlocks = this.result.content?.filter((c: any) => c.type === "image") || [];
725
-
726
- let output = textBlocks
727
- .map((c: any) => {
728
- // Use sanitizeBinaryOutput to handle binary data that crashes string-width
729
- return sanitizeBinaryOutput(c.text || "");
730
- })
731
- .join("\n");
732
-
733
- if (this.toolName === "bash") {
734
- output = renderTerminalText(output);
735
- } else {
736
- output = output.replace(/\r/g, "");
737
- output = stripAnsi(output);
738
- }
739
-
740
- const caps = getCapabilities();
741
- if (imageBlocks.length > 0 && (!caps.images || !this.showImages)) {
742
- const imageIndicators = imageBlocks
743
- .map((img: any) => {
744
- return imageFallback(img.mimeType);
745
- })
746
- .join("\n");
747
- output = output ? `${output}\n${imageIndicators}` : imageIndicators;
748
- }
749
-
750
- return output;
751
- }
752
-
753
- private shouldHideCollapsedPreview(): boolean {
754
- // Always hide preview for read tool when not expanded (like Claude Code)
755
- // For other tools, only hide in minimal mode without errors
756
- if (this.toolName === "read") {
757
- return !this.expanded && !this.result?.isError;
758
- }
759
- return !this.expanded && this.renderMode === "minimal" && !this.result?.isError;
760
- }
761
-
762
- private collapsedExpandHint(_label?: string): string {
763
- return ""; // hint is shown in editor bottom border instead
764
- }
765
-
766
- private collapsedHintWithPrefix(prefix = "\n\n"): string {
767
- const hint = this.collapsedExpandHint();
768
- return hint ? `${prefix}${hint}` : "";
769
- }
770
-
771
- private collapsedFirstLine(output: string): string | undefined {
772
- const first = output.split("\n").map((line) => line.trim()).find(Boolean);
773
- return first ? truncateToWidth(first, 120, "...") : undefined;
774
- }
775
-
776
- private formatToolExecution(statusIndicator: string): string {
777
- let text = "";
778
- const invalidArg = theme.fg("error", "[invalid arg]");
779
- const hideCollapsedPreview = this.shouldHideCollapsedPreview();
780
-
781
- if (this.toolName === "read") {
782
- const rawPath = str(this.args?.file_path ?? this.args?.path);
783
- const path = rawPath !== null ? shortenPath(rawPath) : null;
784
- const offset = this.args?.offset;
785
- const limit = this.args?.limit;
786
-
787
- const startLine = offset ?? 1;
788
- const endLine = limit !== undefined ? startLine + limit - 1 : "";
789
- const lineNum = offset !== undefined ? startLine : undefined;
790
-
791
- let styledPath = path === null ? invalidArg : path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
792
- if (rawPath && path) {
793
- styledPath = editorLink(rawPath, styledPath, { cwd: this.cwd, line: lineNum, scheme: this.editorScheme });
794
- }
795
- let pathDisplay = styledPath;
796
- if (offset !== undefined || limit !== undefined) {
797
- pathDisplay += theme.fg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
798
- }
799
-
800
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`;
801
-
802
- if (this.result) {
803
- const rawOutput = this.getTextOutput();
804
- // Strip hashline prefixes (e.g. "1#BQ:content") for TUI display
805
- const output = rawOutput.replace(/^(\s*)\d+#[ZPMQVRWSNKTXJBYH]{2}:/gm, "$1");
806
- const rawPath = str(this.args?.file_path ?? this.args?.path);
807
- const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
808
- const lines = lang ? highlightCode(replaceTabs(output), lang) : output.split("\n");
809
-
810
- if (hideCollapsedPreview) {
811
- if (output.trim()) {
812
- text += this.collapsedHintWithPrefix();
813
- }
814
- } else {
815
- const maxLines = this.expanded ? lines.length : 10;
816
- const displayLines = lines.slice(0, maxLines);
817
- const remaining = lines.length - maxLines;
818
-
819
- text +=
820
- "\n\n" +
821
- displayLines
822
- .map((line: string) => (lang ? replaceTabs(line) : theme.fg("toolOutput", replaceTabs(line))))
823
- .join("\n");
824
- if (remaining > 0) {
825
- text += theme.fg("muted", `\n... (${remaining} more lines)`);
826
- }
827
- }
828
-
829
- const truncation = this.result.details?.truncation;
830
- if (truncation?.truncated) {
831
- if (truncation.firstLineExceedsLimit) {
832
- text +=
833
- "\n" +
834
- theme.fg(
835
- "warning",
836
- `[First line exceeds ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`,
837
- );
838
- } else if (truncation.truncatedBy === "lines") {
839
- text +=
840
- "\n" +
841
- theme.fg(
842
- "warning",
843
- `[Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${truncation.maxLines ?? DEFAULT_MAX_LINES} line limit)]`,
844
- );
845
- } else {
846
- text +=
847
- "\n" +
848
- theme.fg(
849
- "warning",
850
- `[Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)]`,
851
- );
852
- }
853
- }
854
- }
855
- } else if (this.toolName === "write") {
856
- const rawPath = str(this.args?.file_path ?? this.args?.path);
857
- const fileContent = str(this.args?.content);
858
- const path = rawPath !== null ? shortenPath(rawPath) : null;
859
-
860
- let writePathDisplay = path === null ? invalidArg : path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
861
- if (rawPath && path) {
862
- writePathDisplay = editorLink(rawPath, writePathDisplay, { cwd: this.cwd, scheme: this.editorScheme });
863
- }
864
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("write"))} ${writePathDisplay}`;
865
-
866
- if (fileContent === null) {
867
- text += `\n\n${theme.fg("error", "[invalid content arg - expected string]")}`;
868
- } else if (fileContent) {
869
- const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
870
-
871
- let lines: string[];
872
- if (lang) {
873
- const cache = this.writeHighlightCache;
874
- if (cache && cache.lang === lang && cache.rawPath === rawPath && cache.rawContent === fileContent) {
875
- lines = cache.highlightedLines;
876
- } else {
877
- const displayContent = normalizeDisplayText(fileContent);
878
- const normalized = replaceTabs(displayContent);
879
- lines = highlightCode(normalized, lang);
880
- this.writeHighlightCache = {
881
- rawPath,
882
- lang,
883
- rawContent: fileContent,
884
- normalizedLines: normalized.split("\n"),
885
- highlightedLines: lines,
886
- };
887
- }
888
- } else {
889
- lines = normalizeDisplayText(fileContent).split("\n");
890
- this.writeHighlightCache = undefined;
891
- }
892
-
893
- if (hideCollapsedPreview) {
894
- text += this.collapsedHintWithPrefix();
895
- } else {
896
- const totalLines = lines.length;
897
- const maxLines = this.expanded ? lines.length : 10;
898
- const displayLines = lines.slice(0, maxLines);
899
- const remaining = lines.length - maxLines;
900
-
901
- text +=
902
- "\n\n" +
903
- displayLines.map((line: string) => (lang ? line : theme.fg("toolOutput", replaceTabs(line)))).join("\n");
904
- if (remaining > 0) {
905
- text += theme.fg("muted", `\n... (${remaining} more lines, ${totalLines} total)`);
906
- }
907
- }
908
- }
909
-
910
- // Show error if tool execution failed
911
- if (this.result?.isError) {
912
- const errorText = this.getTextOutput();
913
- if (errorText) {
914
- text += `\n\n${theme.fg("error", errorText)}`;
915
- }
916
- }
917
- } else if (this.toolName === "edit") {
918
- const rawPath = str(this.args?.file_path ?? this.args?.path);
919
- const path = rawPath !== null ? shortenPath(rawPath) : null;
920
-
921
- // Build path display, appending :line if we have diff info
922
- const firstChangedLine =
923
- (this.editDiffPreview && "firstChangedLine" in this.editDiffPreview
924
- ? this.editDiffPreview.firstChangedLine
925
- : undefined) ||
926
- (this.result && !this.result.isError ? this.result.details?.firstChangedLine : undefined);
927
-
928
- let styledEditPath = path === null ? invalidArg : path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
929
- if (rawPath && path) {
930
- styledEditPath = editorLink(rawPath, styledEditPath, {
931
- cwd: this.cwd,
932
- line: firstChangedLine ?? undefined,
933
- scheme: this.editorScheme,
934
- });
935
- }
936
- let pathDisplay = styledEditPath;
937
- if (firstChangedLine) {
938
- pathDisplay += theme.fg("warning", `:${firstChangedLine}`);
939
- }
940
-
941
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`;
942
-
943
- if (this.result?.isError) {
944
- // Show error from result
945
- const errorText = this.getTextOutput();
946
- if (errorText) {
947
- text += `\n\n${theme.fg("error", errorText)}`;
948
- }
949
- } else if (this.result?.details?.diff) {
950
- // Tool executed successfully - use the diff from result
951
- // This takes priority over editDiffPreview which may have a stale error
952
- // due to race condition (async preview computed after file was modified)
953
- text += hideCollapsedPreview
954
- ? this.collapsedHintWithPrefix()
955
- : `\n\n${renderDiff(this.result.details.diff, { filePath: rawPath ?? undefined })}`;
956
- } else if (this.editDiffPreview) {
957
- // Use cached diff preview (before tool executes)
958
- if ("error" in this.editDiffPreview) {
959
- text += `\n\n${theme.fg("error", this.editDiffPreview.error)}`;
960
- } else if (this.editDiffPreview.diff) {
961
- text += hideCollapsedPreview
962
- ? this.collapsedHintWithPrefix()
963
- : `\n\n${renderDiff(this.editDiffPreview.diff, { filePath: rawPath ?? undefined })}`;
964
- }
965
- }
966
- } else if (this.toolName === "ls") {
967
- const rawPath = str(this.args?.path);
968
- const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
969
- const limit = this.args?.limit;
970
-
971
- let lsPathDisplay = path === null ? invalidArg : theme.fg("accent", path);
972
- if (path && rawPath !== null) {
973
- lsPathDisplay = editorLink(rawPath || ".", lsPathDisplay, { cwd: this.cwd, scheme: this.editorScheme });
974
- }
975
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("ls"))} ${lsPathDisplay}`;
976
- if (limit !== undefined) {
977
- text += theme.fg("toolOutput", ` (limit ${limit})`);
978
- }
979
-
980
- if (this.result) {
981
- const output = this.getTextOutput().trim();
982
- if (output) {
983
- if (hideCollapsedPreview) {
984
- text += this.collapsedHintWithPrefix();
985
- } else {
986
- const lines = output.split("\n");
987
- const maxLines = this.expanded ? lines.length : 20;
988
- const displayLines = lines.slice(0, maxLines);
989
- const remaining = lines.length - maxLines;
990
-
991
- text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
992
- if (remaining > 0) {
993
- text += theme.fg("muted", `\n... (${remaining} more lines)`);
994
- }
995
- }
996
- }
997
-
998
- const entryLimit = this.result.details?.entryLimitReached;
999
- const truncation = this.result.details?.truncation;
1000
- if (entryLimit || truncation?.truncated) {
1001
- const warnings: string[] = [];
1002
- if (entryLimit) {
1003
- warnings.push(`${entryLimit} entries limit`);
1004
- }
1005
- if (truncation?.truncated) {
1006
- warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
1007
- }
1008
- text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
1009
- }
1010
- }
1011
- } else if (this.toolName === "find") {
1012
- const pattern = str(this.args?.pattern);
1013
- const rawPath = str(this.args?.path);
1014
- const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
1015
- const limit = this.args?.limit;
1016
-
1017
- let findPathDisplay = path === null ? invalidArg : path;
1018
- if (path && rawPath !== null) {
1019
- findPathDisplay = editorLink(rawPath || ".", theme.fg("accent", findPathDisplay), { cwd: this.cwd, scheme: this.editorScheme });
1020
- } else {
1021
- findPathDisplay = theme.fg("accent", findPathDisplay);
1022
- }
1023
- text =
1024
- `${statusIndicator} ${theme.fg("toolTitle", theme.bold("find"))}` +
1025
- " " +
1026
- (pattern === null ? invalidArg : theme.fg("accent", pattern || "")) +
1027
- theme.fg("toolOutput", " in ") +
1028
- findPathDisplay;
1029
- if (limit !== undefined) {
1030
- text += theme.fg("toolOutput", ` (limit ${limit})`);
1031
- }
1032
-
1033
- if (this.result) {
1034
- const output = this.getTextOutput().trim();
1035
- if (output) {
1036
- if (hideCollapsedPreview) {
1037
- text += this.collapsedHintWithPrefix();
1038
- } else {
1039
- const lines = output.split("\n");
1040
- const maxLines = this.expanded ? lines.length : 20;
1041
- const displayLines = lines.slice(0, maxLines);
1042
- const remaining = lines.length - maxLines;
1043
-
1044
- text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1045
- if (remaining > 0) {
1046
- text += theme.fg("muted", `\n... (${remaining} more lines)`);
1047
- }
1048
- }
1049
- }
1050
-
1051
- const resultLimit = this.result.details?.resultLimitReached;
1052
- const truncation = this.result.details?.truncation;
1053
- if (resultLimit || truncation?.truncated) {
1054
- const warnings: string[] = [];
1055
- if (resultLimit) {
1056
- warnings.push(`${resultLimit} results limit`);
1057
- }
1058
- if (truncation?.truncated) {
1059
- warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
1060
- }
1061
- text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
1062
- }
1063
- }
1064
- } else if (this.toolName === "grep") {
1065
- const pattern = str(this.args?.pattern);
1066
- const rawPath = str(this.args?.path);
1067
- const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
1068
- const glob = str(this.args?.glob);
1069
- const limit = this.args?.limit;
1070
-
1071
- let grepPathDisplay = path === null ? invalidArg : path;
1072
- if (path && rawPath !== null) {
1073
- grepPathDisplay = editorLink(rawPath || ".", theme.fg("accent", grepPathDisplay), { cwd: this.cwd, scheme: this.editorScheme });
1074
- } else {
1075
- grepPathDisplay = theme.fg("accent", grepPathDisplay);
1076
- }
1077
- text =
1078
- `${statusIndicator} ${theme.fg("toolTitle", theme.bold("grep"))}` +
1079
- " " +
1080
- (pattern === null ? invalidArg : theme.fg("accent", `/${pattern || ""}/`)) +
1081
- theme.fg("toolOutput", " in ") +
1082
- grepPathDisplay;
1083
- if (glob) {
1084
- text += theme.fg("toolOutput", ` (${glob})`);
1085
- }
1086
- if (limit !== undefined) {
1087
- text += theme.fg("toolOutput", ` limit ${limit}`);
1088
- }
1089
-
1090
- if (this.result) {
1091
- const output = this.getTextOutput().trim();
1092
- if (output) {
1093
- if (hideCollapsedPreview) {
1094
- text += this.collapsedHintWithPrefix();
1095
- } else {
1096
- const lines = output.split("\n");
1097
- const maxLines = this.expanded ? lines.length : 15;
1098
- const displayLines = lines.slice(0, maxLines);
1099
- const remaining = lines.length - maxLines;
1100
-
1101
- text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1102
- if (remaining > 0) {
1103
- text += theme.fg("muted", `\n... (${remaining} more lines)`);
1104
- }
1105
- }
1106
- }
1107
-
1108
- const matchLimit = this.result.details?.matchLimitReached;
1109
- const truncation = this.result.details?.truncation;
1110
- const linesTruncated = this.result.details?.linesTruncated;
1111
- if (matchLimit || truncation?.truncated || linesTruncated) {
1112
- const warnings: string[] = [];
1113
- if (matchLimit) {
1114
- warnings.push(`${matchLimit} matches limit`);
1115
- }
1116
- if (truncation?.truncated) {
1117
- warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
1118
- }
1119
- if (linesTruncated) {
1120
- warnings.push("some lines truncated");
1121
- }
1122
- text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
1123
- }
1124
- }
1125
- } else if (this.toolName === "web_search") {
1126
- // Server-side Anthropic web search
1127
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("web search"))}`;
1128
-
1129
- if (process.env.PI_OFFLINE === "1") {
1130
- text += "\n\n" + theme.fg("muted", "\u{1F50C} Offline \u{2014} web search unavailable");
1131
- } else if (this.result) {
1132
- const output = this.getTextOutput().trim();
1133
- if (output) {
1134
- if (hideCollapsedPreview) {
1135
- text += this.collapsedHintWithPrefix();
1136
- } else {
1137
- const lines = output.split("\n");
1138
- const maxLines = this.expanded ? lines.length : 10;
1139
- const displayLines = lines.slice(0, maxLines);
1140
- const remaining = lines.length - maxLines;
1141
-
1142
- text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1143
- if (remaining > 0) {
1144
- text += theme.fg("muted", `\n... (${remaining} more lines)`);
1145
- }
1146
- }
1147
- }
1148
- }
1149
- } else if (this.toolName === "lsp") {
1150
- const action = this.args?.action as string | undefined;
1151
- const file = this.args?.file as string | undefined;
1152
- const line = this.args?.line as number | undefined;
1153
- const symbol = this.args?.symbol as string | undefined;
1154
- const query = this.args?.query as string | undefined;
1155
- const newName = this.args?.new_name as string | undefined;
1156
-
1157
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("lsp"))}`;
1158
- if (action) text += ` ${theme.fg("accent", action)}`;
1159
- if (file) {
1160
- const shortFile = shortenPath(file);
1161
- let styledFile = theme.fg("muted", shortFile);
1162
- if (file && shortFile) {
1163
- styledFile = editorLink(file, styledFile, { cwd: this.cwd, line, scheme: this.editorScheme });
1164
- }
1165
- text += ` ${styledFile}`;
1166
- if (line !== undefined) text += theme.fg("warning", `:${line}`);
1167
- }
1168
- if (symbol) text += ` ${theme.fg("toolOutput", symbol)}`;
1169
- if (query) text += ` ${theme.fg("muted", `"${query}"`)}`;
1170
- if (newName) text += ` ${theme.fg("accent", newName)}`;
1171
-
1172
- if (this.result) {
1173
- const output = this.getTextOutput().trim();
1174
- if (output) {
1175
- if (hideCollapsedPreview) {
1176
- text += this.collapsedHintWithPrefix();
1177
- } else {
1178
- const lines = output.split("\n");
1179
- const maxLines = this.expanded ? lines.length : 10;
1180
- const displayLines = lines.slice(0, maxLines);
1181
- const remaining = lines.length - maxLines;
1182
- text += `\n\n${displayLines.map((l: string) => theme.fg("toolOutput", l)).join("\n")}`;
1183
- if (remaining > 0) text += theme.fg("muted", `\n... (${remaining} more lines)`);
1184
- }
1185
- }
1186
- }
1187
- } else {
1188
- // Generic tool (shouldn't reach here for custom tools)
1189
- text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold(this.toolName))}`;
1190
-
1191
- const content = JSON.stringify(this.args, null, 2);
1192
- text += hideCollapsedPreview ? this.collapsedHintWithPrefix() : `\n\n${content}`;
1193
- const output = this.getTextOutput();
1194
- if (output && !hideCollapsedPreview) {
1195
- text += `\n${output}`;
1196
- }
1197
- }
1198
-
1199
- return text;
1200
- }
100
+ private contentBox: Box; // Used for custom tools and bash visual truncation
101
+ private contentText: Text; // For built-in tools (with its own padding/bg)
102
+ private imageComponents: Image[] = [];
103
+ private imageSpacers: Spacer[] = [];
104
+ private toolName: string;
105
+ private args: any;
106
+ private expanded = false;
107
+ private renderMode: "minimal" | "normal";
108
+ private showImages: boolean;
109
+ private editorScheme: EditorScheme;
110
+ private isPartial = true;
111
+ private toolDefinition?: ToolDefinition;
112
+ private ui: TUI;
113
+ private cwd: string;
114
+ private result?: {
115
+ content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;
116
+ isError: boolean;
117
+ details?: any;
118
+ };
119
+ // Cached edit diff preview (computed when args arrive, before tool executes)
120
+ private editDiffPreview?: EditDiffResult | EditDiffError;
121
+ private editDiffArgsKey?: string; // Track which args the preview is for
122
+ // Cached converted images for Kitty protocol (which requires PNG), keyed by index
123
+ private convertedImages: Map<number, { data: string; mimeType: string }> = new Map();
124
+ // Incremental syntax highlighting cache for write tool call args
125
+ private writeHighlightCache?: WriteHighlightCache;
126
+ // When true, this component intentionally renders no lines
127
+ private hideComponent = false;
128
+ private manuallyHidden = false;
129
+ private startTime = Date.now();
130
+
131
+ // Tool status spinner state
132
+ private spinnerTimer: NodeJS.Timeout | null = null;
133
+ private spinnerFrame = 0;
134
+
135
+ constructor(
136
+ toolName: string,
137
+ args: any,
138
+ options: ToolExecutionOptions = {},
139
+ toolDefinition: ToolDefinition | undefined,
140
+ ui: TUI,
141
+ cwd: string = process.cwd(),
142
+ ) {
143
+ super();
144
+ this.toolName = toolName;
145
+ this.args = args;
146
+ this.showImages = options.showImages ?? true;
147
+ this.renderMode = options.renderMode ?? "normal";
148
+ this.editorScheme = options.editorScheme ?? "auto";
149
+ this.toolDefinition = toolDefinition;
150
+ this.ui = ui;
151
+ this.cwd = cwd;
152
+
153
+ // Always create both - contentBox for custom tools/bash, contentText for other built-ins
154
+ this.contentBox = new Box(1, 0, (text: string) => theme.bg("toolPendingBg", text));
155
+ this.contentText = new Text("", 1, 0, (text: string) => theme.bg("toolPendingBg", text));
156
+
157
+ // Use contentBox for bash (visual truncation) or custom tools with custom renderers
158
+ // Use contentText for built-in tools (including overrides without custom renderers)
159
+ if (toolName === "bash" || (toolDefinition && !this.shouldUseBuiltInRenderer())) {
160
+ this.addChild(this.contentBox);
161
+ } else {
162
+ this.addChild(this.contentText);
163
+ }
164
+
165
+ this.updateDisplay();
166
+ }
167
+
168
+ /**
169
+ * Check if we should use built-in rendering for this tool.
170
+ * Returns true if the tool name is a built-in AND either there's no toolDefinition
171
+ * or the toolDefinition doesn't provide custom renderers.
172
+ */
173
+ private shouldUseBuiltInRenderer(): boolean {
174
+ const isBuiltInName = this.toolName in allTools;
175
+ const hasCustomRenderers = this.toolDefinition?.renderCall || this.toolDefinition?.renderResult;
176
+ return isBuiltInName && !hasCustomRenderers;
177
+ }
178
+
179
+ updateArgs(args: any): void {
180
+ this.args = args;
181
+ if (this.toolName === "write" && this.isPartial) {
182
+ this.updateWriteHighlightCacheIncremental();
183
+ }
184
+ this.updateDisplay();
185
+ }
186
+
187
+ private highlightSingleLine(line: string, lang: string): string {
188
+ const highlighted = highlightCode(line, lang);
189
+ return highlighted[0] ?? "";
190
+ }
191
+
192
+ private refreshWriteHighlightPrefix(cache: WriteHighlightCache): void {
193
+ const prefixCount = Math.min(WRITE_PARTIAL_FULL_HIGHLIGHT_LINES, cache.normalizedLines.length);
194
+ if (prefixCount === 0) return;
195
+
196
+ const prefixSource = cache.normalizedLines.slice(0, prefixCount).join("\n");
197
+ const prefixHighlighted = highlightCode(prefixSource, cache.lang);
198
+ for (let i = 0; i < prefixCount; i++) {
199
+ cache.highlightedLines[i] =
200
+ prefixHighlighted[i] ?? this.highlightSingleLine(cache.normalizedLines[i] ?? "", cache.lang);
201
+ }
202
+ }
203
+
204
+ private rebuildWriteHighlightCacheFull(rawPath: string | null, fileContent: string): void {
205
+ const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
206
+ if (!lang) {
207
+ this.writeHighlightCache = undefined;
208
+ return;
209
+ }
210
+
211
+ const displayContent = normalizeDisplayText(fileContent);
212
+ const normalized = replaceTabs(displayContent);
213
+ this.writeHighlightCache = {
214
+ rawPath,
215
+ lang,
216
+ rawContent: fileContent,
217
+ normalizedLines: normalized.split("\n"),
218
+ highlightedLines: highlightCode(normalized, lang),
219
+ };
220
+ }
221
+
222
+ private updateWriteHighlightCacheIncremental(): void {
223
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
224
+ const fileContent = str(this.args?.content);
225
+ if (rawPath === null || fileContent === null) {
226
+ this.writeHighlightCache = undefined;
227
+ return;
228
+ }
229
+
230
+ const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
231
+ if (!lang) {
232
+ this.writeHighlightCache = undefined;
233
+ return;
234
+ }
235
+
236
+ if (!this.writeHighlightCache) {
237
+ this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
238
+ return;
239
+ }
240
+
241
+ const cache = this.writeHighlightCache;
242
+ if (cache.lang !== lang || cache.rawPath !== rawPath) {
243
+ this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
244
+ return;
245
+ }
246
+
247
+ if (!fileContent.startsWith(cache.rawContent)) {
248
+ this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
249
+ return;
250
+ }
251
+
252
+ if (fileContent.length === cache.rawContent.length) {
253
+ return;
254
+ }
255
+
256
+ const deltaRaw = fileContent.slice(cache.rawContent.length);
257
+ const deltaDisplay = normalizeDisplayText(deltaRaw);
258
+ const deltaNormalized = replaceTabs(deltaDisplay);
259
+ cache.rawContent = fileContent;
260
+
261
+ if (cache.normalizedLines.length === 0) {
262
+ cache.normalizedLines.push("");
263
+ cache.highlightedLines.push("");
264
+ }
265
+
266
+ const segments = deltaNormalized.split("\n");
267
+ const lastIndex = cache.normalizedLines.length - 1;
268
+ cache.normalizedLines[lastIndex] += segments[0];
269
+ cache.highlightedLines[lastIndex] = this.highlightSingleLine(cache.normalizedLines[lastIndex], cache.lang);
270
+
271
+ for (let i = 1; i < segments.length; i++) {
272
+ cache.normalizedLines.push(segments[i]);
273
+ cache.highlightedLines.push(this.highlightSingleLine(segments[i], cache.lang));
274
+ }
275
+
276
+ this.refreshWriteHighlightPrefix(cache);
277
+ }
278
+
279
+ /**
280
+ * Signal that args are complete (tool is about to execute).
281
+ * This triggers diff computation for edit tool.
282
+ */
283
+ setArgsComplete(): void {
284
+ if (this.toolName === "write") {
285
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
286
+ const fileContent = str(this.args?.content);
287
+ if (rawPath !== null && fileContent !== null) {
288
+ this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
289
+ }
290
+ }
291
+ this.maybeComputeEditDiff();
292
+ }
293
+
294
+ /**
295
+ * Compute edit diff preview when we have complete args.
296
+ * This runs async and updates display when done.
297
+ */
298
+ private maybeComputeEditDiff(): void {
299
+ if (this.toolName !== "edit") return;
300
+
301
+ const path = this.args?.path;
302
+ const oldText = this.args?.oldText;
303
+ const newText = this.args?.newText;
304
+
305
+ // Need all three params to compute diff
306
+ if (!path || oldText === undefined || newText === undefined) return;
307
+
308
+ // Create a key to track which args this computation is for
309
+ const argsKey = JSON.stringify({ path, oldText, newText });
310
+
311
+ // Skip if we already computed for these exact args
312
+ if (this.editDiffArgsKey === argsKey) return;
313
+
314
+ this.editDiffArgsKey = argsKey;
315
+
316
+ // Compute diff async
317
+ computeEditDiff(path, oldText, newText, this.cwd).then((result) => {
318
+ // Only update if args haven't changed since we started
319
+ if (this.editDiffArgsKey === argsKey) {
320
+ this.editDiffPreview = result;
321
+ this.updateDisplay();
322
+ this.ui.requestRender();
323
+ }
324
+ });
325
+ }
326
+
327
+ updateResult(
328
+ result: {
329
+ content: Array<{ type: string; text?: string; data?: string; mimeType?: string }>;
330
+ details?: any;
331
+ isError: boolean;
332
+ },
333
+ isPartial = false,
334
+ ): void {
335
+ this.result = result;
336
+ this.isPartial = isPartial;
337
+
338
+ if (this.toolName === "write" && !isPartial) {
339
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
340
+ const fileContent = str(this.args?.content);
341
+ if (rawPath !== null && fileContent !== null) {
342
+ this.rebuildWriteHighlightCacheFull(rawPath, fileContent);
343
+ }
344
+ }
345
+ this.updateDisplay();
346
+ // Convert non-PNG images to PNG for Kitty protocol (async)
347
+ this.maybeConvertImagesForKitty();
348
+ }
349
+
350
+ /**
351
+ * Convert non-PNG images to PNG for Kitty graphics protocol.
352
+ * Kitty requires PNG format (f=100), so JPEG/GIF/WebP won't display.
353
+ */
354
+ private maybeConvertImagesForKitty(): void {
355
+ const caps = getCapabilities();
356
+ // Only needed for Kitty protocol
357
+ if (caps.images !== "kitty") return;
358
+ if (!this.result) return;
359
+
360
+ const imageBlocks = this.result.content?.filter((c: any) => c.type === "image") || [];
361
+
362
+ for (let i = 0; i < imageBlocks.length; i++) {
363
+ const img = imageBlocks[i];
364
+ if (!img.data || !img.mimeType) continue;
365
+ // Skip if already PNG or already converted
366
+ if (img.mimeType === "image/png") continue;
367
+ if (this.convertedImages.has(i)) continue;
368
+
369
+ // Convert async
370
+ const index = i;
371
+ convertToPng(img.data, img.mimeType).then((converted) => {
372
+ if (converted) {
373
+ this.convertedImages.set(index, converted);
374
+ this.updateDisplay();
375
+ this.ui.requestRender();
376
+ }
377
+ });
378
+ }
379
+ }
380
+
381
+ setExpanded(expanded: boolean): void {
382
+ this.expanded = expanded;
383
+ this.updateDisplay();
384
+ }
385
+
386
+ setHidden(hidden: boolean): void {
387
+ this.manuallyHidden = hidden;
388
+ this.updateDisplay();
389
+ }
390
+
391
+ isHidden(): boolean {
392
+ return this.hideComponent;
393
+ }
394
+
395
+ getElapsed(): number {
396
+ return Date.now() - this.startTime;
397
+ }
398
+
399
+ shouldHideWhenCollapsed(): boolean {
400
+ return !this.isPartial && shouldCollapse(this.toolName, this.result?.isError ?? false);
401
+ }
402
+
403
+ setRenderMode(mode: "minimal" | "normal"): void {
404
+ if (this.renderMode !== mode) {
405
+ this.renderMode = mode;
406
+ this.updateDisplay();
407
+ }
408
+ }
409
+
410
+ setEditorScheme(scheme: EditorScheme): void {
411
+ this.editorScheme = scheme;
412
+ this.updateDisplay();
413
+ }
414
+
415
+ setShowImages(show: boolean): void {
416
+ this.showImages = show;
417
+ this.updateDisplay();
418
+ }
419
+
420
+ dispose(): void {
421
+ if (this.spinnerTimer) {
422
+ clearInterval(this.spinnerTimer);
423
+ this.spinnerTimer = null;
424
+ }
425
+ }
426
+
427
+ override invalidate(): void {
428
+ super.invalidate();
429
+ this.updateDisplay();
430
+ }
431
+
432
+ override render(width: number): string[] {
433
+ if (this.hideComponent) {
434
+ return [];
435
+ }
436
+ return [...super.render(width), ""];
437
+ }
438
+
439
+ private updateDisplay(): void {
440
+ // Status indicator with circle
441
+ let statusIndicator = "";
442
+ let statusColor = "";
443
+
444
+ if (this.isPartial) {
445
+ // Loading spinner - start timer on first partial render
446
+ if (!this.spinnerTimer) {
447
+ this.spinnerTimer = setInterval(() => {
448
+ this.spinnerFrame = (this.spinnerFrame + 1) % SPINNER_FRAMES.length;
449
+ this.updateDisplay();
450
+ this.ui.requestRender();
451
+ }, SPINNER_INTERVAL_MS);
452
+ }
453
+ statusIndicator = SPINNER_FRAMES[this.spinnerFrame];
454
+ } else {
455
+ // Stop spinner when no longer partial
456
+ if (this.spinnerTimer) {
457
+ clearInterval(this.spinnerTimer);
458
+ this.spinnerTimer = null;
459
+ }
460
+ if (this.result?.isError) {
461
+ // Failed - red circle
462
+ statusIndicator = theme.fg("error", "●");
463
+ statusColor = "error";
464
+ } else {
465
+ // Success - green circle
466
+ statusIndicator = theme.fg("success", "●");
467
+ statusColor = "success";
468
+ }
469
+ }
470
+
471
+ const useBuiltInRenderer = this.shouldUseBuiltInRenderer();
472
+ let customRendererHasContent = false;
473
+
474
+ // Use built-in rendering for built-in tools (or overrides without custom renderers)
475
+ if (useBuiltInRenderer) {
476
+ if (this.toolName === "bash") {
477
+ // Bash uses Box with visual line truncation - no background
478
+ this.contentBox.setBgFn((text: string) => text);
479
+ this.contentBox.clear();
480
+ this.renderBashContent(statusIndicator);
481
+ } else {
482
+ // Other built-in tools: use Text directly with caching - no background
483
+ this.contentText.setCustomBgFn((text: string) => text);
484
+ this.contentText.setText(this.formatToolExecution(statusIndicator));
485
+ }
486
+ } else if (this.toolDefinition) {
487
+ // Custom tools use Box for flexible component rendering - no background
488
+ this.contentBox.setBgFn((text: string) => text);
489
+ this.contentBox.clear();
490
+
491
+ // Render call component
492
+ if (this.toolDefinition.renderCall) {
493
+ try {
494
+ const callComponent = this.toolDefinition.renderCall(this.args, theme, { statusIndicator });
495
+ if (callComponent !== undefined) {
496
+ this.contentBox.addChild(callComponent);
497
+ customRendererHasContent = true;
498
+ }
499
+ } catch {
500
+ // Fall back to default on error
501
+ this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0));
502
+ customRendererHasContent = true;
503
+ }
504
+ } else {
505
+ // No custom renderCall, show tool name
506
+ this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0));
507
+ customRendererHasContent = true;
508
+ }
509
+
510
+ // Render result component if we have a result
511
+ if (this.result && this.toolDefinition.renderResult) {
512
+ try {
513
+ if (this.shouldHideCollapsedPreview()) {
514
+ const output = this.getTextOutput();
515
+ const hasDetails = output.trim().length > 0 || this.imageComponents.length > 0 || this.result.details !== undefined;
516
+ const collapsedHint = this.collapsedHintWithPrefix("\n");
517
+ if (hasDetails && collapsedHint) {
518
+ this.contentBox.addChild(new Text(collapsedHint, 0, 0));
519
+ customRendererHasContent = true;
520
+ }
521
+ } else {
522
+ const resultComponent = this.toolDefinition.renderResult(
523
+ { content: this.result.content as any, details: this.result.details },
524
+ { expanded: this.expanded, isPartial: this.isPartial },
525
+ theme,
526
+ );
527
+ if (resultComponent !== undefined) {
528
+ this.contentBox.addChild(resultComponent);
529
+ customRendererHasContent = true;
530
+ // bg_shell: show /bg hint after start/list/digest actions
531
+ if (this.toolName === 'bg_shell' && this.result && !this.isPartial) {
532
+ const details = this.result.details as Record<string, unknown> | undefined;
533
+ const action = details?.action as string | undefined;
534
+ if (action === 'start' || action === 'list' || action === 'digest') {
535
+ this.contentBox.addChild(new Text(
536
+ '\n' + theme.fg('muted', 'Tip: run /bg to manage background processes'),
537
+ 0, 0,
538
+ ));
539
+ }
540
+ }
541
+ }
542
+ }
543
+ } catch {
544
+ // Fall back to showing raw output on error
545
+ const output = this.getTextOutput();
546
+ if (output) {
547
+ this.contentBox.addChild(new Text(theme.fg("toolOutput", output), 0, 0));
548
+ customRendererHasContent = true;
549
+ }
550
+ }
551
+ } else if (this.result) {
552
+ // Has result but no custom renderResult
553
+ const output = this.getTextOutput();
554
+ if (output) {
555
+ if (this.shouldHideCollapsedPreview()) {
556
+ const collapsedHint = this.collapsedHintWithPrefix("\n");
557
+ if (collapsedHint) {
558
+ this.contentBox.addChild(new Text(collapsedHint, 0, 0));
559
+ }
560
+ } else {
561
+ this.contentBox.addChild(new Text(theme.fg("toolOutput", output), 0, 0));
562
+ }
563
+ customRendererHasContent = true;
564
+ }
565
+ }
566
+ } else {
567
+ // Unknown tool with no registered definition - show generic fallback
568
+ this.contentText.setCustomBgFn((text: string) => text);
569
+ this.contentText.setText(this.formatToolExecution(statusIndicator));
570
+ }
571
+
572
+ // Handle images (same for both custom and built-in)
573
+ for (const img of this.imageComponents) {
574
+ this.removeChild(img);
575
+ }
576
+ this.imageComponents = [];
577
+ for (const spacer of this.imageSpacers) {
578
+ this.removeChild(spacer);
579
+ }
580
+ this.imageSpacers = [];
581
+
582
+ if (this.result) {
583
+ const imageBlocks = this.result.content?.filter((c: any) => c.type === "image") || [];
584
+ const caps = getCapabilities();
585
+
586
+ for (let i = 0; i < imageBlocks.length; i++) {
587
+ const img = imageBlocks[i];
588
+ if (caps.images && this.showImages && img.data && img.mimeType) {
589
+ // Use converted PNG for Kitty protocol if available
590
+ const converted = this.convertedImages.get(i);
591
+ const imageData = converted?.data ?? img.data;
592
+ const imageMimeType = converted?.mimeType ?? img.mimeType;
593
+
594
+ // For Kitty, skip non-PNG images that haven't been converted yet
595
+ if (caps.images === "kitty" && imageMimeType !== "image/png") {
596
+ continue;
597
+ }
598
+
599
+ const spacer = new Spacer(1);
600
+ this.addChild(spacer);
601
+ this.imageSpacers.push(spacer);
602
+ const imageComponent = new Image(
603
+ imageData,
604
+ imageMimeType,
605
+ { fallbackColor: (s: string) => theme.fg("toolOutput", s) },
606
+ { maxWidthCells: 60 },
607
+ );
608
+ imageComponent.setOnDimensionsResolved(() => {
609
+ this.updateDisplay();
610
+ this.ui.requestRender();
611
+ });
612
+ this.imageComponents.push(imageComponent);
613
+ this.addChild(imageComponent);
614
+ }
615
+ }
616
+ }
617
+
618
+ const computedHidden = !useBuiltInRenderer && this.toolDefinition ? !customRendererHasContent && this.imageComponents.length === 0 : false;
619
+ this.hideComponent = this.manuallyHidden || computedHidden;
620
+ }
621
+
622
+ /**
623
+ * Render bash content using visual line truncation (like bash-execution.ts)
624
+ */
625
+ private renderBashContent(statusIndicator: string): void {
626
+ const command = str(this.args?.command);
627
+ const timeout = this.args?.timeout as number | undefined;
628
+ const body = new Container();
629
+
630
+ this.contentBox.addChild(body);
631
+
632
+ const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : "";
633
+ const sandboxBadge = this.result?.details?.sandboxed ? ` ${theme.fg("success", "[sandboxed]")}` : "";
634
+ const toolLabel = theme.fg("toolTitle", theme.bold("bash"));
635
+ const shellPrompt = theme.fg("muted", "$ ");
636
+
637
+ // Always show only the first meaningful non-comment line in the header.
638
+ // The full command is shown below the header when expanded.
639
+ let commandDisplay: string;
640
+ let commandLines: string[] = [];
641
+ let isMultiLine = false;
642
+ if (command === null) {
643
+ commandDisplay = theme.fg("error", "[invalid arg]");
644
+ } else if (!command) {
645
+ commandDisplay = theme.fg("toolOutput", "...");
646
+ } else {
647
+ commandLines = command.split("\n");
648
+ isMultiLine = commandLines.length > 1;
649
+ const firstMeaningful = commandLines.find((l) => l.trim() && !l.trim().startsWith("#")) ?? commandLines[0];
650
+ const suffix = isMultiLine ? theme.fg("muted", " …") : "";
651
+ commandDisplay = theme.fg("toolOutput", firstMeaningful ?? "") + suffix;
652
+ }
653
+
654
+ body.addChild(new Text(`${statusIndicator} ${toolLabel} ${shellPrompt}${commandDisplay}${timeoutSuffix}${sandboxBadge}`, 0, 0));
655
+
656
+ // When expanded, show the full command below the header (only for multi-line commands)
657
+ if (this.expanded && isMultiLine) {
658
+ const fullCommand = commandLines.map((line) => theme.fg("toolOutput", line)).join("\n");
659
+ body.addChild(new Text(`\n${fullCommand}`, 0, 0));
660
+ }
661
+
662
+ if (this.result) {
663
+ const output = this.getTextOutput().trim();
664
+
665
+ if (output) {
666
+ const styledOutput = output
667
+ .split("\n")
668
+ .map((line) => theme.fg("toolOutput", line))
669
+ .join("\n");
670
+
671
+ if (this.expanded) {
672
+ body.addChild(new Text(`\n${styledOutput}`, 0, 0));
673
+ } else if (this.renderMode === "minimal") {
674
+ const collapsedHint = this.collapsedHintWithPrefix("\n");
675
+ if (collapsedHint) {
676
+ body.addChild(new Text(collapsedHint, 1, 0));
677
+ }
678
+ } else {
679
+ let cachedWidth: number | undefined;
680
+ let cachedLines: string[] | undefined;
681
+ let cachedSkipped: number | undefined;
682
+
683
+ body.addChild({
684
+ render: (width: number) => {
685
+ if (cachedLines === undefined || cachedWidth !== width) {
686
+ const result = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, Math.max(1, width - 1));
687
+ cachedLines = result.visualLines.map((line) => ` ${line}`);
688
+ cachedSkipped = result.skippedCount;
689
+ cachedWidth = width;
690
+ }
691
+ if (cachedSkipped && cachedSkipped > 0) {
692
+ const hint = theme.fg("muted", `... (${cachedSkipped} earlier lines)`);
693
+ return ["", truncateToWidth(` ${hint}`, width, "..."), ...cachedLines];
694
+ }
695
+ return ["", ...cachedLines];
696
+ },
697
+ invalidate: () => {
698
+ cachedWidth = undefined;
699
+ cachedLines = undefined;
700
+ cachedSkipped = undefined;
701
+ },
702
+ });
703
+ }
704
+ }
705
+
706
+ const truncation = this.result.details?.truncation;
707
+ const fullOutputPath = this.result.details?.fullOutputPath;
708
+ if (truncation?.truncated || fullOutputPath) {
709
+ const warnings: string[] = [];
710
+ if (fullOutputPath) {
711
+ warnings.push(`Full output: ${fullOutputPath}`);
712
+ }
713
+ if (truncation?.truncated) {
714
+ if (truncation.truncatedBy === "lines") {
715
+ warnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);
716
+ } else {
717
+ warnings.push(
718
+ `Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`,
719
+ );
720
+ }
721
+ }
722
+ body.addChild(new Text(`\n${theme.fg("warning", `[${warnings.join(". ")}]`)}`, 0, 0));
723
+ }
724
+
725
+ // Privilege escalation error agent cannot sudo, show manual step callout
726
+ if (this.result.isError && command !== null && isBashPrivilegeError(output, command)) {
727
+ body.addChild(
728
+ new Text(
729
+ `\n${theme.fg("warning", "⚠ This command requires elevated privileges the agent cannot use.")}\n${theme.fg("muted", `Run it manually in your terminal:\n ${command}`)}`,
730
+ 0,
731
+ 0,
732
+ ),
733
+ );
734
+ }
735
+ }
736
+ }
737
+
738
+ private getTextOutput(): string {
739
+ if (!this.result) return "";
740
+
741
+ const textBlocks = this.result.content?.filter((c: any) => c.type === "text") || [];
742
+ const imageBlocks = this.result.content?.filter((c: any) => c.type === "image") || [];
743
+
744
+ let output = textBlocks
745
+ .map((c: any) => {
746
+ // Use sanitizeBinaryOutput to handle binary data that crashes string-width
747
+ return sanitizeBinaryOutput(c.text || "");
748
+ })
749
+ .join("\n");
750
+
751
+ if (this.toolName === "bash") {
752
+ output = renderTerminalText(output);
753
+ } else {
754
+ output = output.replace(/\r/g, "");
755
+ output = stripAnsi(output);
756
+ }
757
+
758
+ const caps = getCapabilities();
759
+ if (imageBlocks.length > 0 && (!caps.images || !this.showImages)) {
760
+ const imageIndicators = imageBlocks
761
+ .map((img: any) => {
762
+ return imageFallback(img.mimeType);
763
+ })
764
+ .join("\n");
765
+ output = output ? `${output}\n${imageIndicators}` : imageIndicators;
766
+ }
767
+
768
+ return output;
769
+ }
770
+
771
+ private shouldHideCollapsedPreview(): boolean {
772
+ // Always hide preview for read tool when not expanded (like Claude Code)
773
+ // For other tools, only hide in minimal mode without errors
774
+ if (this.toolName === "read") {
775
+ return !this.expanded && !this.result?.isError;
776
+ }
777
+ return !this.expanded && this.renderMode === "minimal" && !this.result?.isError;
778
+ }
779
+
780
+ private collapsedExpandHint(_label?: string): string {
781
+ return ""; // hint is shown in editor bottom border instead
782
+ }
783
+
784
+ private collapsedHintWithPrefix(prefix = "\n\n"): string {
785
+ const hint = this.collapsedExpandHint();
786
+ return hint ? `${prefix}${hint}` : "";
787
+ }
788
+
789
+ private collapsedFirstLine(output: string): string | undefined {
790
+ const first = output.split("\n").map((line) => line.trim()).find(Boolean);
791
+ return first ? truncateToWidth(first, 120, "...") : undefined;
792
+ }
793
+
794
+ private formatToolExecution(statusIndicator: string): string {
795
+ let text = "";
796
+ const invalidArg = theme.fg("error", "[invalid arg]");
797
+ const hideCollapsedPreview = this.shouldHideCollapsedPreview();
798
+
799
+ if (this.toolName === "read") {
800
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
801
+ const path = rawPath !== null ? shortenPath(rawPath) : null;
802
+ const offset = this.args?.offset;
803
+ const limit = this.args?.limit;
804
+
805
+ const startLine = offset ?? 1;
806
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
807
+ const lineNum = offset !== undefined ? startLine : undefined;
808
+
809
+ let styledPath = path === null ? invalidArg : path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
810
+ if (rawPath && path) {
811
+ styledPath = editorLink(rawPath, styledPath, { cwd: this.cwd, line: lineNum, scheme: this.editorScheme });
812
+ }
813
+ let pathDisplay = styledPath;
814
+ if (offset !== undefined || limit !== undefined) {
815
+ pathDisplay += theme.fg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
816
+ }
817
+
818
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`;
819
+
820
+ if (this.result) {
821
+ const rawOutput = this.getTextOutput();
822
+ // Strip hashline prefixes (e.g. "1#BQ:content") for TUI display
823
+ const output = rawOutput.replace(/^(\s*)\d+#[ZPMQVRWSNKTXJBYH]{2}:/gm, "$1");
824
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
825
+ const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
826
+ const lines = lang ? highlightCode(replaceTabs(output), lang) : output.split("\n");
827
+
828
+ if (hideCollapsedPreview) {
829
+ if (output.trim()) {
830
+ text += this.collapsedHintWithPrefix();
831
+ }
832
+ } else {
833
+ const maxLines = this.expanded ? lines.length : 10;
834
+ const displayLines = lines.slice(0, maxLines);
835
+ const remaining = lines.length - maxLines;
836
+
837
+ text +=
838
+ "\n\n" +
839
+ displayLines
840
+ .map((line: string) => (lang ? replaceTabs(line) : theme.fg("toolOutput", replaceTabs(line))))
841
+ .join("\n");
842
+ if (remaining > 0) {
843
+ text += theme.fg("muted", `\n... (${remaining} more lines)`);
844
+ }
845
+ }
846
+
847
+ const truncation = this.result.details?.truncation;
848
+ if (truncation?.truncated) {
849
+ if (truncation.firstLineExceedsLimit) {
850
+ text +=
851
+ "\n" +
852
+ theme.fg(
853
+ "warning",
854
+ `[First line exceeds ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`,
855
+ );
856
+ } else if (truncation.truncatedBy === "lines") {
857
+ text +=
858
+ "\n" +
859
+ theme.fg(
860
+ "warning",
861
+ `[Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${truncation.maxLines ?? DEFAULT_MAX_LINES} line limit)]`,
862
+ );
863
+ } else {
864
+ text +=
865
+ "\n" +
866
+ theme.fg(
867
+ "warning",
868
+ `[Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)]`,
869
+ );
870
+ }
871
+ }
872
+ }
873
+ } else if (this.toolName === "write") {
874
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
875
+ const fileContent = str(this.args?.content);
876
+ const path = rawPath !== null ? shortenPath(rawPath) : null;
877
+
878
+ let writePathDisplay = path === null ? invalidArg : path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
879
+ if (rawPath && path) {
880
+ writePathDisplay = editorLink(rawPath, writePathDisplay, { cwd: this.cwd, scheme: this.editorScheme });
881
+ }
882
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("write"))} ${writePathDisplay}`;
883
+
884
+ if (fileContent === null) {
885
+ text += `\n\n${theme.fg("error", "[invalid content arg - expected string]")}`;
886
+ } else if (fileContent) {
887
+ const lang = rawPath ? getLanguageFromPath(rawPath) : undefined;
888
+
889
+ let lines: string[];
890
+ if (lang) {
891
+ const cache = this.writeHighlightCache;
892
+ if (cache && cache.lang === lang && cache.rawPath === rawPath && cache.rawContent === fileContent) {
893
+ lines = cache.highlightedLines;
894
+ } else {
895
+ const displayContent = normalizeDisplayText(fileContent);
896
+ const normalized = replaceTabs(displayContent);
897
+ lines = highlightCode(normalized, lang);
898
+ this.writeHighlightCache = {
899
+ rawPath,
900
+ lang,
901
+ rawContent: fileContent,
902
+ normalizedLines: normalized.split("\n"),
903
+ highlightedLines: lines,
904
+ };
905
+ }
906
+ } else {
907
+ lines = normalizeDisplayText(fileContent).split("\n");
908
+ this.writeHighlightCache = undefined;
909
+ }
910
+
911
+ if (hideCollapsedPreview) {
912
+ text += this.collapsedHintWithPrefix();
913
+ } else {
914
+ const totalLines = lines.length;
915
+ const maxLines = this.expanded ? lines.length : 10;
916
+ const displayLines = lines.slice(0, maxLines);
917
+ const remaining = lines.length - maxLines;
918
+
919
+ text +=
920
+ "\n\n" +
921
+ displayLines.map((line: string) => (lang ? line : theme.fg("toolOutput", replaceTabs(line)))).join("\n");
922
+ if (remaining > 0) {
923
+ text += theme.fg("muted", `\n... (${remaining} more lines, ${totalLines} total)`);
924
+ }
925
+ }
926
+ }
927
+
928
+ // Show error if tool execution failed
929
+ if (this.result?.isError) {
930
+ const errorText = this.getTextOutput();
931
+ if (errorText) {
932
+ text += `\n\n${theme.fg("error", errorText)}`;
933
+ }
934
+ }
935
+ } else if (this.toolName === "edit") {
936
+ const rawPath = str(this.args?.file_path ?? this.args?.path);
937
+ const path = rawPath !== null ? shortenPath(rawPath) : null;
938
+
939
+ // Build path display, appending :line if we have diff info
940
+ const firstChangedLine =
941
+ (this.editDiffPreview && "firstChangedLine" in this.editDiffPreview
942
+ ? this.editDiffPreview.firstChangedLine
943
+ : undefined) ||
944
+ (this.result && !this.result.isError ? this.result.details?.firstChangedLine : undefined);
945
+
946
+ let styledEditPath = path === null ? invalidArg : path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
947
+ if (rawPath && path) {
948
+ styledEditPath = editorLink(rawPath, styledEditPath, {
949
+ cwd: this.cwd,
950
+ line: firstChangedLine ?? undefined,
951
+ scheme: this.editorScheme,
952
+ });
953
+ }
954
+ let pathDisplay = styledEditPath;
955
+ if (firstChangedLine) {
956
+ pathDisplay += theme.fg("warning", `:${firstChangedLine}`);
957
+ }
958
+
959
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`;
960
+
961
+ if (this.result?.isError) {
962
+ // Show error from result
963
+ const errorText = this.getTextOutput();
964
+ if (errorText) {
965
+ text += `\n\n${theme.fg("error", errorText)}`;
966
+ }
967
+ } else if (this.result?.details?.diff) {
968
+ // Tool executed successfully - use the diff from result
969
+ // This takes priority over editDiffPreview which may have a stale error
970
+ // due to race condition (async preview computed after file was modified)
971
+ text += hideCollapsedPreview
972
+ ? this.collapsedHintWithPrefix()
973
+ : `\n\n${renderDiff(this.result.details.diff, { filePath: rawPath ?? undefined })}`;
974
+ } else if (this.editDiffPreview) {
975
+ // Use cached diff preview (before tool executes)
976
+ if ("error" in this.editDiffPreview) {
977
+ text += `\n\n${theme.fg("error", this.editDiffPreview.error)}`;
978
+ } else if (this.editDiffPreview.diff) {
979
+ text += hideCollapsedPreview
980
+ ? this.collapsedHintWithPrefix()
981
+ : `\n\n${renderDiff(this.editDiffPreview.diff, { filePath: rawPath ?? undefined })}`;
982
+ }
983
+ }
984
+ } else if (this.toolName === "ls") {
985
+ const rawPath = str(this.args?.path);
986
+ const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
987
+ const limit = this.args?.limit;
988
+
989
+ let lsPathDisplay = path === null ? invalidArg : theme.fg("accent", path);
990
+ if (path && rawPath !== null) {
991
+ lsPathDisplay = editorLink(rawPath || ".", lsPathDisplay, { cwd: this.cwd, scheme: this.editorScheme });
992
+ }
993
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("ls"))} ${lsPathDisplay}`;
994
+ if (limit !== undefined) {
995
+ text += theme.fg("toolOutput", ` (limit ${limit})`);
996
+ }
997
+
998
+ if (this.result) {
999
+ const output = this.getTextOutput().trim();
1000
+ if (output) {
1001
+ if (hideCollapsedPreview) {
1002
+ text += this.collapsedHintWithPrefix();
1003
+ } else {
1004
+ const lines = output.split("\n");
1005
+ const maxLines = this.expanded ? lines.length : 20;
1006
+ const displayLines = lines.slice(0, maxLines);
1007
+ const remaining = lines.length - maxLines;
1008
+
1009
+ text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1010
+ if (remaining > 0) {
1011
+ text += theme.fg("muted", `\n... (${remaining} more lines)`);
1012
+ }
1013
+ }
1014
+ }
1015
+
1016
+ const entryLimit = this.result.details?.entryLimitReached;
1017
+ const truncation = this.result.details?.truncation;
1018
+ if (entryLimit || truncation?.truncated) {
1019
+ const warnings: string[] = [];
1020
+ if (entryLimit) {
1021
+ warnings.push(`${entryLimit} entries limit`);
1022
+ }
1023
+ if (truncation?.truncated) {
1024
+ warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
1025
+ }
1026
+ text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
1027
+ }
1028
+ }
1029
+ } else if (this.toolName === "find") {
1030
+ const pattern = str(this.args?.pattern);
1031
+ const rawPath = str(this.args?.path);
1032
+ const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
1033
+ const limit = this.args?.limit;
1034
+
1035
+ let findPathDisplay = path === null ? invalidArg : path;
1036
+ if (path && rawPath !== null) {
1037
+ findPathDisplay = editorLink(rawPath || ".", theme.fg("accent", findPathDisplay), { cwd: this.cwd, scheme: this.editorScheme });
1038
+ } else {
1039
+ findPathDisplay = theme.fg("accent", findPathDisplay);
1040
+ }
1041
+ text =
1042
+ `${statusIndicator} ${theme.fg("toolTitle", theme.bold("find"))}` +
1043
+ " " +
1044
+ (pattern === null ? invalidArg : theme.fg("accent", pattern || "")) +
1045
+ theme.fg("toolOutput", " in ") +
1046
+ findPathDisplay;
1047
+ if (limit !== undefined) {
1048
+ text += theme.fg("toolOutput", ` (limit ${limit})`);
1049
+ }
1050
+
1051
+ if (this.result) {
1052
+ const output = this.getTextOutput().trim();
1053
+ if (output) {
1054
+ if (hideCollapsedPreview) {
1055
+ text += this.collapsedHintWithPrefix();
1056
+ } else {
1057
+ const lines = output.split("\n");
1058
+ const maxLines = this.expanded ? lines.length : 20;
1059
+ const displayLines = lines.slice(0, maxLines);
1060
+ const remaining = lines.length - maxLines;
1061
+
1062
+ text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1063
+ if (remaining > 0) {
1064
+ text += theme.fg("muted", `\n... (${remaining} more lines)`);
1065
+ }
1066
+ }
1067
+ }
1068
+
1069
+ const resultLimit = this.result.details?.resultLimitReached;
1070
+ const truncation = this.result.details?.truncation;
1071
+ if (resultLimit || truncation?.truncated) {
1072
+ const warnings: string[] = [];
1073
+ if (resultLimit) {
1074
+ warnings.push(`${resultLimit} results limit`);
1075
+ }
1076
+ if (truncation?.truncated) {
1077
+ warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
1078
+ }
1079
+ text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
1080
+ }
1081
+ }
1082
+ } else if (this.toolName === "grep") {
1083
+ const pattern = str(this.args?.pattern);
1084
+ const rawPath = str(this.args?.path);
1085
+ const path = rawPath !== null ? shortenPath(rawPath || ".") : null;
1086
+ const glob = str(this.args?.glob);
1087
+ const limit = this.args?.limit;
1088
+
1089
+ let grepPathDisplay = path === null ? invalidArg : path;
1090
+ if (path && rawPath !== null) {
1091
+ grepPathDisplay = editorLink(rawPath || ".", theme.fg("accent", grepPathDisplay), { cwd: this.cwd, scheme: this.editorScheme });
1092
+ } else {
1093
+ grepPathDisplay = theme.fg("accent", grepPathDisplay);
1094
+ }
1095
+ text =
1096
+ `${statusIndicator} ${theme.fg("toolTitle", theme.bold("grep"))}` +
1097
+ " " +
1098
+ (pattern === null ? invalidArg : theme.fg("accent", `/${pattern || ""}/`)) +
1099
+ theme.fg("toolOutput", " in ") +
1100
+ grepPathDisplay;
1101
+ if (glob) {
1102
+ text += theme.fg("toolOutput", ` (${glob})`);
1103
+ }
1104
+ if (limit !== undefined) {
1105
+ text += theme.fg("toolOutput", ` limit ${limit}`);
1106
+ }
1107
+
1108
+ if (this.result) {
1109
+ const output = this.getTextOutput().trim();
1110
+ if (output) {
1111
+ if (hideCollapsedPreview) {
1112
+ text += this.collapsedHintWithPrefix();
1113
+ } else {
1114
+ const lines = output.split("\n");
1115
+ const maxLines = this.expanded ? lines.length : 15;
1116
+ const displayLines = lines.slice(0, maxLines);
1117
+ const remaining = lines.length - maxLines;
1118
+
1119
+ text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1120
+ if (remaining > 0) {
1121
+ text += theme.fg("muted", `\n... (${remaining} more lines)`);
1122
+ }
1123
+ }
1124
+ }
1125
+
1126
+ const matchLimit = this.result.details?.matchLimitReached;
1127
+ const truncation = this.result.details?.truncation;
1128
+ const linesTruncated = this.result.details?.linesTruncated;
1129
+ if (matchLimit || truncation?.truncated || linesTruncated) {
1130
+ const warnings: string[] = [];
1131
+ if (matchLimit) {
1132
+ warnings.push(`${matchLimit} matches limit`);
1133
+ }
1134
+ if (truncation?.truncated) {
1135
+ warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
1136
+ }
1137
+ if (linesTruncated) {
1138
+ warnings.push("some lines truncated");
1139
+ }
1140
+ text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
1141
+ }
1142
+ }
1143
+ } else if (this.toolName === "web_search") {
1144
+ // Server-side Anthropic web search
1145
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("web search"))}`;
1146
+
1147
+ if (process.env.PI_OFFLINE === "1") {
1148
+ text += "\n\n" + theme.fg("muted", "\u{1F50C} Offline \u{2014} web search unavailable");
1149
+ } else if (this.result) {
1150
+ const output = this.getTextOutput().trim();
1151
+ if (output) {
1152
+ if (hideCollapsedPreview) {
1153
+ text += this.collapsedHintWithPrefix();
1154
+ } else {
1155
+ const lines = output.split("\n");
1156
+ const maxLines = this.expanded ? lines.length : 10;
1157
+ const displayLines = lines.slice(0, maxLines);
1158
+ const remaining = lines.length - maxLines;
1159
+
1160
+ text += `\n\n${displayLines.map((line: string) => theme.fg("toolOutput", line)).join("\n")}`;
1161
+ if (remaining > 0) {
1162
+ text += theme.fg("muted", `\n... (${remaining} more lines)`);
1163
+ }
1164
+ }
1165
+ }
1166
+ }
1167
+ } else if (this.toolName === "lsp") {
1168
+ const action = this.args?.action as string | undefined;
1169
+ const file = this.args?.file as string | undefined;
1170
+ const line = this.args?.line as number | undefined;
1171
+ const symbol = this.args?.symbol as string | undefined;
1172
+ const query = this.args?.query as string | undefined;
1173
+ const newName = this.args?.new_name as string | undefined;
1174
+
1175
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold("lsp"))}`;
1176
+ if (action) text += ` ${theme.fg("accent", action)}`;
1177
+ if (file) {
1178
+ const shortFile = shortenPath(file);
1179
+ let styledFile = theme.fg("muted", shortFile);
1180
+ if (file && shortFile) {
1181
+ styledFile = editorLink(file, styledFile, { cwd: this.cwd, line, scheme: this.editorScheme });
1182
+ }
1183
+ text += ` ${styledFile}`;
1184
+ if (line !== undefined) text += theme.fg("warning", `:${line}`);
1185
+ }
1186
+ if (symbol) text += ` ${theme.fg("toolOutput", symbol)}`;
1187
+ if (query) text += ` ${theme.fg("muted", `"${query}"`)}`;
1188
+ if (newName) text += ` → ${theme.fg("accent", newName)}`;
1189
+
1190
+ if (this.result) {
1191
+ const output = this.getTextOutput().trim();
1192
+ if (output) {
1193
+ if (hideCollapsedPreview) {
1194
+ text += this.collapsedHintWithPrefix();
1195
+ } else {
1196
+ const lines = output.split("\n");
1197
+ const maxLines = this.expanded ? lines.length : 10;
1198
+ const displayLines = lines.slice(0, maxLines);
1199
+ const remaining = lines.length - maxLines;
1200
+ text += `\n\n${displayLines.map((l: string) => theme.fg("toolOutput", l)).join("\n")}`;
1201
+ if (remaining > 0) text += theme.fg("muted", `\n... (${remaining} more lines)`);
1202
+ }
1203
+ }
1204
+ }
1205
+ } else {
1206
+ // Generic tool (shouldn't reach here for custom tools)
1207
+ text = `${statusIndicator} ${theme.fg("toolTitle", theme.bold(this.toolName))}`;
1208
+
1209
+ const content = JSON.stringify(this.args, null, 2);
1210
+ text += hideCollapsedPreview ? this.collapsedHintWithPrefix() : `\n\n${content}`;
1211
+ const output = this.getTextOutput();
1212
+ if (output && !hideCollapsedPreview) {
1213
+ text += `\n${output}`;
1214
+ }
1215
+ }
1216
+
1217
+ return text;
1218
+ }
1201
1219
  }