indusagi-coding-agent 0.50.0

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 (240) hide show
  1. package/CHANGELOG.md +2249 -0
  2. package/README.md +546 -0
  3. package/dist/cli/args.js +282 -0
  4. package/dist/cli/config-selector.js +30 -0
  5. package/dist/cli/file-processor.js +78 -0
  6. package/dist/cli/list-models.js +91 -0
  7. package/dist/cli/session-picker.js +31 -0
  8. package/dist/cli.js +10 -0
  9. package/dist/config.js +158 -0
  10. package/dist/core/agent-session.js +2097 -0
  11. package/dist/core/auth-storage.js +278 -0
  12. package/dist/core/bash-executor.js +211 -0
  13. package/dist/core/compaction/branch-summarization.js +241 -0
  14. package/dist/core/compaction/compaction.js +606 -0
  15. package/dist/core/compaction/index.js +6 -0
  16. package/dist/core/compaction/utils.js +137 -0
  17. package/dist/core/diagnostics.js +1 -0
  18. package/dist/core/event-bus.js +24 -0
  19. package/dist/core/exec.js +70 -0
  20. package/dist/core/export-html/ansi-to-html.js +248 -0
  21. package/dist/core/export-html/index.js +221 -0
  22. package/dist/core/export-html/template.css +905 -0
  23. package/dist/core/export-html/template.html +54 -0
  24. package/dist/core/export-html/template.js +1549 -0
  25. package/dist/core/export-html/tool-renderer.js +56 -0
  26. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  27. package/dist/core/export-html/vendor/marked.min.js +6 -0
  28. package/dist/core/extensions/index.js +8 -0
  29. package/dist/core/extensions/loader.js +395 -0
  30. package/dist/core/extensions/runner.js +499 -0
  31. package/dist/core/extensions/types.js +31 -0
  32. package/dist/core/extensions/wrapper.js +101 -0
  33. package/dist/core/footer-data-provider.js +133 -0
  34. package/dist/core/index.js +8 -0
  35. package/dist/core/keybindings.js +140 -0
  36. package/dist/core/messages.js +122 -0
  37. package/dist/core/model-registry.js +454 -0
  38. package/dist/core/model-resolver.js +309 -0
  39. package/dist/core/package-manager.js +1142 -0
  40. package/dist/core/prompt-templates.js +250 -0
  41. package/dist/core/resource-loader.js +569 -0
  42. package/dist/core/sdk.js +225 -0
  43. package/dist/core/session-manager.js +1078 -0
  44. package/dist/core/settings-manager.js +430 -0
  45. package/dist/core/skills.js +339 -0
  46. package/dist/core/system-prompt.js +136 -0
  47. package/dist/core/timings.js +24 -0
  48. package/dist/core/tools/bash.js +226 -0
  49. package/dist/core/tools/edit-diff.js +242 -0
  50. package/dist/core/tools/edit.js +145 -0
  51. package/dist/core/tools/find.js +205 -0
  52. package/dist/core/tools/grep.js +238 -0
  53. package/dist/core/tools/index.js +60 -0
  54. package/dist/core/tools/ls.js +117 -0
  55. package/dist/core/tools/path-utils.js +52 -0
  56. package/dist/core/tools/read.js +165 -0
  57. package/dist/core/tools/truncate.js +204 -0
  58. package/dist/core/tools/write.js +77 -0
  59. package/dist/index.js +41 -0
  60. package/dist/main.js +565 -0
  61. package/dist/migrations.js +260 -0
  62. package/dist/modes/index.js +7 -0
  63. package/dist/modes/interactive/components/armin.js +328 -0
  64. package/dist/modes/interactive/components/assistant-message.js +86 -0
  65. package/dist/modes/interactive/components/bash-execution.js +155 -0
  66. package/dist/modes/interactive/components/bordered-loader.js +47 -0
  67. package/dist/modes/interactive/components/branch-summary-message.js +41 -0
  68. package/dist/modes/interactive/components/compaction-summary-message.js +42 -0
  69. package/dist/modes/interactive/components/config-selector.js +458 -0
  70. package/dist/modes/interactive/components/countdown-timer.js +27 -0
  71. package/dist/modes/interactive/components/custom-editor.js +61 -0
  72. package/dist/modes/interactive/components/custom-message.js +80 -0
  73. package/dist/modes/interactive/components/diff.js +132 -0
  74. package/dist/modes/interactive/components/dynamic-border.js +19 -0
  75. package/dist/modes/interactive/components/extension-editor.js +96 -0
  76. package/dist/modes/interactive/components/extension-input.js +54 -0
  77. package/dist/modes/interactive/components/extension-selector.js +70 -0
  78. package/dist/modes/interactive/components/footer.js +213 -0
  79. package/dist/modes/interactive/components/index.js +31 -0
  80. package/dist/modes/interactive/components/keybinding-hints.js +60 -0
  81. package/dist/modes/interactive/components/login-dialog.js +138 -0
  82. package/dist/modes/interactive/components/model-selector.js +253 -0
  83. package/dist/modes/interactive/components/oauth-selector.js +91 -0
  84. package/dist/modes/interactive/components/scoped-models-selector.js +262 -0
  85. package/dist/modes/interactive/components/session-selector-search.js +145 -0
  86. package/dist/modes/interactive/components/session-selector.js +698 -0
  87. package/dist/modes/interactive/components/settings-selector.js +250 -0
  88. package/dist/modes/interactive/components/show-images-selector.js +33 -0
  89. package/dist/modes/interactive/components/skill-invocation-message.js +44 -0
  90. package/dist/modes/interactive/components/theme-selector.js +43 -0
  91. package/dist/modes/interactive/components/thinking-selector.js +45 -0
  92. package/dist/modes/interactive/components/tool-execution.js +608 -0
  93. package/dist/modes/interactive/components/tree-selector.js +892 -0
  94. package/dist/modes/interactive/components/user-message-selector.js +109 -0
  95. package/dist/modes/interactive/components/user-message.js +15 -0
  96. package/dist/modes/interactive/components/visual-truncate.js +32 -0
  97. package/dist/modes/interactive/interactive-mode.js +3576 -0
  98. package/dist/modes/interactive/theme/dark.json +85 -0
  99. package/dist/modes/interactive/theme/light.json +84 -0
  100. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  101. package/dist/modes/interactive/theme/theme.js +938 -0
  102. package/dist/modes/print-mode.js +96 -0
  103. package/dist/modes/rpc/rpc-client.js +390 -0
  104. package/dist/modes/rpc/rpc-mode.js +448 -0
  105. package/dist/modes/rpc/rpc-types.js +7 -0
  106. package/dist/utils/changelog.js +86 -0
  107. package/dist/utils/clipboard-image.js +116 -0
  108. package/dist/utils/clipboard.js +58 -0
  109. package/dist/utils/frontmatter.js +25 -0
  110. package/dist/utils/git.js +5 -0
  111. package/dist/utils/image-convert.js +34 -0
  112. package/dist/utils/image-resize.js +180 -0
  113. package/dist/utils/mime.js +25 -0
  114. package/dist/utils/photon.js +120 -0
  115. package/dist/utils/shell.js +164 -0
  116. package/dist/utils/sleep.js +16 -0
  117. package/dist/utils/tools-manager.js +186 -0
  118. package/docs/compaction.md +390 -0
  119. package/docs/custom-provider.md +538 -0
  120. package/docs/development.md +69 -0
  121. package/docs/extensions.md +1733 -0
  122. package/docs/images/doom-extension.png +0 -0
  123. package/docs/images/interactive-mode.png +0 -0
  124. package/docs/images/tree-view.png +0 -0
  125. package/docs/json.md +79 -0
  126. package/docs/keybindings.md +162 -0
  127. package/docs/models.md +193 -0
  128. package/docs/packages.md +163 -0
  129. package/docs/prompt-templates.md +67 -0
  130. package/docs/providers.md +147 -0
  131. package/docs/rpc.md +1048 -0
  132. package/docs/sdk.md +957 -0
  133. package/docs/session.md +412 -0
  134. package/docs/settings.md +216 -0
  135. package/docs/shell-aliases.md +13 -0
  136. package/docs/skills.md +226 -0
  137. package/docs/terminal-setup.md +65 -0
  138. package/docs/themes.md +295 -0
  139. package/docs/tree.md +219 -0
  140. package/docs/tui.md +887 -0
  141. package/docs/windows.md +17 -0
  142. package/examples/README.md +25 -0
  143. package/examples/extensions/README.md +192 -0
  144. package/examples/extensions/antigravity-image-gen.ts +414 -0
  145. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  146. package/examples/extensions/bookmark.ts +50 -0
  147. package/examples/extensions/claude-rules.ts +86 -0
  148. package/examples/extensions/confirm-destructive.ts +59 -0
  149. package/examples/extensions/custom-compaction.ts +115 -0
  150. package/examples/extensions/custom-footer.ts +65 -0
  151. package/examples/extensions/custom-header.ts +73 -0
  152. package/examples/extensions/custom-provider-anthropic/index.ts +605 -0
  153. package/examples/extensions/custom-provider-anthropic/package-lock.json +24 -0
  154. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  155. package/examples/extensions/custom-provider-gitlab-duo/index.ts +350 -0
  156. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  157. package/examples/extensions/custom-provider-gitlab-duo/test.ts +83 -0
  158. package/examples/extensions/dirty-repo-guard.ts +56 -0
  159. package/examples/extensions/doom-overlay/README.md +46 -0
  160. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  161. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  162. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  163. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  164. package/examples/extensions/doom-overlay/doom-component.ts +133 -0
  165. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  166. package/examples/extensions/doom-overlay/doom-keys.ts +105 -0
  167. package/examples/extensions/doom-overlay/index.ts +74 -0
  168. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  169. package/examples/extensions/event-bus.ts +43 -0
  170. package/examples/extensions/file-trigger.ts +41 -0
  171. package/examples/extensions/git-checkpoint.ts +53 -0
  172. package/examples/extensions/handoff.ts +151 -0
  173. package/examples/extensions/hello.ts +25 -0
  174. package/examples/extensions/inline-bash.ts +94 -0
  175. package/examples/extensions/input-transform.ts +43 -0
  176. package/examples/extensions/interactive-shell.ts +196 -0
  177. package/examples/extensions/mac-system-theme.ts +47 -0
  178. package/examples/extensions/message-renderer.ts +60 -0
  179. package/examples/extensions/modal-editor.ts +86 -0
  180. package/examples/extensions/model-status.ts +31 -0
  181. package/examples/extensions/notify.ts +25 -0
  182. package/examples/extensions/overlay-qa-tests.ts +882 -0
  183. package/examples/extensions/overlay-test.ts +151 -0
  184. package/examples/extensions/permission-gate.ts +34 -0
  185. package/examples/extensions/pirate.ts +47 -0
  186. package/examples/extensions/plan-mode/README.md +65 -0
  187. package/examples/extensions/plan-mode/index.ts +341 -0
  188. package/examples/extensions/plan-mode/utils.ts +168 -0
  189. package/examples/extensions/preset.ts +399 -0
  190. package/examples/extensions/protected-paths.ts +30 -0
  191. package/examples/extensions/qna.ts +120 -0
  192. package/examples/extensions/question.ts +265 -0
  193. package/examples/extensions/questionnaire.ts +428 -0
  194. package/examples/extensions/rainbow-editor.ts +88 -0
  195. package/examples/extensions/sandbox/index.ts +318 -0
  196. package/examples/extensions/sandbox/package-lock.json +92 -0
  197. package/examples/extensions/sandbox/package.json +19 -0
  198. package/examples/extensions/send-user-message.ts +97 -0
  199. package/examples/extensions/session-name.ts +27 -0
  200. package/examples/extensions/shutdown-command.ts +63 -0
  201. package/examples/extensions/snake.ts +344 -0
  202. package/examples/extensions/space-invaders.ts +561 -0
  203. package/examples/extensions/ssh.ts +220 -0
  204. package/examples/extensions/status-line.ts +40 -0
  205. package/examples/extensions/subagent/README.md +172 -0
  206. package/examples/extensions/subagent/agents/planner.md +37 -0
  207. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  208. package/examples/extensions/subagent/agents/scout.md +50 -0
  209. package/examples/extensions/subagent/agents/worker.md +24 -0
  210. package/examples/extensions/subagent/agents.ts +127 -0
  211. package/examples/extensions/subagent/index.ts +964 -0
  212. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  213. package/examples/extensions/subagent/prompts/implement.md +10 -0
  214. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  215. package/examples/extensions/summarize.ts +196 -0
  216. package/examples/extensions/timed-confirm.ts +70 -0
  217. package/examples/extensions/todo.ts +300 -0
  218. package/examples/extensions/tool-override.ts +144 -0
  219. package/examples/extensions/tools.ts +147 -0
  220. package/examples/extensions/trigger-compact.ts +40 -0
  221. package/examples/extensions/truncated-tool.ts +193 -0
  222. package/examples/extensions/widget-placement.ts +17 -0
  223. package/examples/extensions/with-deps/index.ts +36 -0
  224. package/examples/extensions/with-deps/package-lock.json +31 -0
  225. package/examples/extensions/with-deps/package.json +22 -0
  226. package/examples/sdk/01-minimal.ts +22 -0
  227. package/examples/sdk/02-custom-model.ts +50 -0
  228. package/examples/sdk/03-custom-prompt.ts +55 -0
  229. package/examples/sdk/04-skills.ts +46 -0
  230. package/examples/sdk/05-tools.ts +56 -0
  231. package/examples/sdk/06-extensions.ts +88 -0
  232. package/examples/sdk/07-context-files.ts +40 -0
  233. package/examples/sdk/08-prompt-templates.ts +47 -0
  234. package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
  235. package/examples/sdk/10-settings.ts +38 -0
  236. package/examples/sdk/11-sessions.ts +48 -0
  237. package/examples/sdk/12-full-control.ts +82 -0
  238. package/examples/sdk/13-codex-oauth.ts +37 -0
  239. package/examples/sdk/README.md +144 -0
  240. package/package.json +85 -0
@@ -0,0 +1,608 @@
1
+ import * as os from "node:os";
2
+ import { Box, Container, getCapabilities, getImageDimensions, Image, imageFallback, Spacer, Text, truncateToWidth, } from "indusagi/tui";
3
+ import stripAnsi from "strip-ansi";
4
+ import { computeEditDiff } from "../../../core/tools/edit-diff.js";
5
+ import { allTools } from "../../../core/tools/index.js";
6
+ import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize } from "../../../core/tools/truncate.js";
7
+ import { convertToPng } from "../../../utils/image-convert.js";
8
+ import { sanitizeBinaryOutput } from "../../../utils/shell.js";
9
+ import { getLanguageFromPath, highlightCode, theme } from "../theme/theme.js";
10
+ import { renderDiff } from "./diff.js";
11
+ import { keyHint } from "./keybinding-hints.js";
12
+ import { truncateToVisualLines } from "./visual-truncate.js";
13
+ // Preview line limit for bash when not expanded
14
+ const BASH_PREVIEW_LINES = 5;
15
+ /**
16
+ * Convert absolute path to tilde notation if it's in home directory
17
+ */
18
+ function shortenPath(path) {
19
+ const home = os.homedir();
20
+ if (path.startsWith(home)) {
21
+ return `~${path.slice(home.length)}`;
22
+ }
23
+ return path;
24
+ }
25
+ /**
26
+ * Replace tabs with spaces for consistent rendering
27
+ */
28
+ function replaceTabs(text) {
29
+ return text.replace(/\t/g, " ");
30
+ }
31
+ /**
32
+ * Component that renders a tool call with its result (updateable)
33
+ */
34
+ export class ToolExecutionComponent extends Container {
35
+ constructor(toolName, args, options = {}, toolDefinition, ui, cwd = process.cwd()) {
36
+ super();
37
+ this.imageComponents = [];
38
+ this.imageSpacers = [];
39
+ this.expanded = false;
40
+ this.isPartial = true;
41
+ // Cached converted images for Kitty protocol (which requires PNG), keyed by index
42
+ this.convertedImages = new Map();
43
+ this.toolName = toolName;
44
+ this.args = args;
45
+ this.showImages = options.showImages ?? true;
46
+ this.toolDefinition = toolDefinition;
47
+ this.ui = ui;
48
+ this.cwd = cwd;
49
+ this.addChild(new Spacer(1));
50
+ // Always create both - contentBox for custom tools/bash, contentText for other built-ins
51
+ this.contentBox = new Box(1, 1, (text) => theme.bg("toolPendingBg", text));
52
+ this.contentText = new Text("", 1, 1, (text) => theme.bg("toolPendingBg", text));
53
+ // Use contentBox for bash (visual truncation) or custom tools with custom renderers
54
+ // Use contentText for built-in tools (including overrides without custom renderers)
55
+ if (toolName === "bash" || (toolDefinition && !this.shouldUseBuiltInRenderer())) {
56
+ this.addChild(this.contentBox);
57
+ }
58
+ else {
59
+ this.addChild(this.contentText);
60
+ }
61
+ this.updateDisplay();
62
+ }
63
+ /**
64
+ * Check if we should use built-in rendering for this tool.
65
+ * Returns true if the tool name is a built-in AND either there's no toolDefinition
66
+ * or the toolDefinition doesn't provide custom renderers.
67
+ */
68
+ shouldUseBuiltInRenderer() {
69
+ const isBuiltInName = this.toolName in allTools;
70
+ const hasCustomRenderers = this.toolDefinition?.renderCall || this.toolDefinition?.renderResult;
71
+ return isBuiltInName && !hasCustomRenderers;
72
+ }
73
+ updateArgs(args) {
74
+ this.args = args;
75
+ this.updateDisplay();
76
+ }
77
+ /**
78
+ * Signal that args are complete (tool is about to execute).
79
+ * This triggers diff computation for edit tool.
80
+ */
81
+ setArgsComplete() {
82
+ this.maybeComputeEditDiff();
83
+ }
84
+ /**
85
+ * Compute edit diff preview when we have complete args.
86
+ * This runs async and updates display when done.
87
+ */
88
+ maybeComputeEditDiff() {
89
+ if (this.toolName !== "edit")
90
+ return;
91
+ const path = this.args?.path;
92
+ const oldText = this.args?.oldText;
93
+ const newText = this.args?.newText;
94
+ // Need all three params to compute diff
95
+ if (!path || oldText === undefined || newText === undefined)
96
+ return;
97
+ // Create a key to track which args this computation is for
98
+ const argsKey = JSON.stringify({ path, oldText, newText });
99
+ // Skip if we already computed for these exact args
100
+ if (this.editDiffArgsKey === argsKey)
101
+ return;
102
+ this.editDiffArgsKey = argsKey;
103
+ // Compute diff async
104
+ computeEditDiff(path, oldText, newText, this.cwd).then((result) => {
105
+ // Only update if args haven't changed since we started
106
+ if (this.editDiffArgsKey === argsKey) {
107
+ this.editDiffPreview = result;
108
+ this.updateDisplay();
109
+ this.ui.requestRender();
110
+ }
111
+ });
112
+ }
113
+ updateResult(result, isPartial = false) {
114
+ this.result = result;
115
+ this.isPartial = isPartial;
116
+ this.updateDisplay();
117
+ // Convert non-PNG images to PNG for Kitty protocol (async)
118
+ this.maybeConvertImagesForKitty();
119
+ }
120
+ /**
121
+ * Convert non-PNG images to PNG for Kitty graphics protocol.
122
+ * Kitty requires PNG format (f=100), so JPEG/GIF/WebP won't display.
123
+ */
124
+ maybeConvertImagesForKitty() {
125
+ const caps = getCapabilities();
126
+ // Only needed for Kitty protocol
127
+ if (caps.images !== "kitty")
128
+ return;
129
+ if (!this.result)
130
+ return;
131
+ const imageBlocks = this.result.content?.filter((c) => c.type === "image") || [];
132
+ for (let i = 0; i < imageBlocks.length; i++) {
133
+ const img = imageBlocks[i];
134
+ if (!img.data || !img.mimeType)
135
+ continue;
136
+ // Skip if already PNG or already converted
137
+ if (img.mimeType === "image/png")
138
+ continue;
139
+ if (this.convertedImages.has(i))
140
+ continue;
141
+ // Convert async
142
+ const index = i;
143
+ convertToPng(img.data, img.mimeType).then((converted) => {
144
+ if (converted) {
145
+ this.convertedImages.set(index, converted);
146
+ this.updateDisplay();
147
+ this.ui.requestRender();
148
+ }
149
+ });
150
+ }
151
+ }
152
+ setExpanded(expanded) {
153
+ this.expanded = expanded;
154
+ this.updateDisplay();
155
+ }
156
+ setShowImages(show) {
157
+ this.showImages = show;
158
+ this.updateDisplay();
159
+ }
160
+ invalidate() {
161
+ super.invalidate();
162
+ this.updateDisplay();
163
+ }
164
+ updateDisplay() {
165
+ // Set background based on state
166
+ const bgFn = this.isPartial
167
+ ? (text) => theme.bg("toolPendingBg", text)
168
+ : this.result?.isError
169
+ ? (text) => theme.bg("toolErrorBg", text)
170
+ : (text) => theme.bg("toolSuccessBg", text);
171
+ // Use built-in rendering for built-in tools (or overrides without custom renderers)
172
+ if (this.shouldUseBuiltInRenderer()) {
173
+ if (this.toolName === "bash") {
174
+ // Bash uses Box with visual line truncation
175
+ this.contentBox.setBgFn(bgFn);
176
+ this.contentBox.clear();
177
+ this.renderBashContent();
178
+ }
179
+ else {
180
+ // Other built-in tools: use Text directly with caching
181
+ this.contentText.setCustomBgFn(bgFn);
182
+ this.contentText.setText(this.formatToolExecution());
183
+ }
184
+ }
185
+ else if (this.toolDefinition) {
186
+ // Custom tools use Box for flexible component rendering
187
+ this.contentBox.setBgFn(bgFn);
188
+ this.contentBox.clear();
189
+ // Render call component
190
+ if (this.toolDefinition.renderCall) {
191
+ try {
192
+ const callComponent = this.toolDefinition.renderCall(this.args, theme);
193
+ if (callComponent) {
194
+ this.contentBox.addChild(callComponent);
195
+ }
196
+ }
197
+ catch {
198
+ // Fall back to default on error
199
+ this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0));
200
+ }
201
+ }
202
+ else {
203
+ // No custom renderCall, show tool name
204
+ this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(this.toolName)), 0, 0));
205
+ }
206
+ // Render result component if we have a result
207
+ if (this.result && this.toolDefinition.renderResult) {
208
+ try {
209
+ const resultComponent = this.toolDefinition.renderResult({ content: this.result.content, details: this.result.details }, { expanded: this.expanded, isPartial: this.isPartial }, theme);
210
+ if (resultComponent) {
211
+ this.contentBox.addChild(resultComponent);
212
+ }
213
+ }
214
+ catch {
215
+ // Fall back to showing raw output on error
216
+ const output = this.getTextOutput();
217
+ if (output) {
218
+ this.contentBox.addChild(new Text(theme.fg("toolOutput", output), 0, 0));
219
+ }
220
+ }
221
+ }
222
+ else if (this.result) {
223
+ // Has result but no custom renderResult
224
+ const output = this.getTextOutput();
225
+ if (output) {
226
+ this.contentBox.addChild(new Text(theme.fg("toolOutput", output), 0, 0));
227
+ }
228
+ }
229
+ }
230
+ // Handle images (same for both custom and built-in)
231
+ for (const img of this.imageComponents) {
232
+ this.removeChild(img);
233
+ }
234
+ this.imageComponents = [];
235
+ for (const spacer of this.imageSpacers) {
236
+ this.removeChild(spacer);
237
+ }
238
+ this.imageSpacers = [];
239
+ if (this.result) {
240
+ const imageBlocks = this.result.content?.filter((c) => c.type === "image") || [];
241
+ const caps = getCapabilities();
242
+ for (let i = 0; i < imageBlocks.length; i++) {
243
+ const img = imageBlocks[i];
244
+ if (caps.images && this.showImages && img.data && img.mimeType) {
245
+ // Use converted PNG for Kitty protocol if available
246
+ const converted = this.convertedImages.get(i);
247
+ const imageData = converted?.data ?? img.data;
248
+ const imageMimeType = converted?.mimeType ?? img.mimeType;
249
+ // For Kitty, skip non-PNG images that haven't been converted yet
250
+ if (caps.images === "kitty" && imageMimeType !== "image/png") {
251
+ continue;
252
+ }
253
+ const spacer = new Spacer(1);
254
+ this.addChild(spacer);
255
+ this.imageSpacers.push(spacer);
256
+ const imageComponent = new Image(imageData, imageMimeType, { fallbackColor: (s) => theme.fg("toolOutput", s) }, { maxWidthCells: 60 });
257
+ this.imageComponents.push(imageComponent);
258
+ this.addChild(imageComponent);
259
+ }
260
+ }
261
+ }
262
+ }
263
+ /**
264
+ * Render bash content using visual line truncation (like bash-execution.ts)
265
+ */
266
+ renderBashContent() {
267
+ const command = this.args?.command || "";
268
+ const timeout = this.args?.timeout;
269
+ // Header
270
+ const timeoutSuffix = timeout ? theme.fg("muted", ` (timeout ${timeout}s)`) : "";
271
+ this.contentBox.addChild(new Text(theme.fg("toolTitle", theme.bold(`$ ${command || theme.fg("toolOutput", "...")}`)) + timeoutSuffix, 0, 0));
272
+ if (this.result) {
273
+ const output = this.getTextOutput().trim();
274
+ if (output) {
275
+ // Style each line for the output
276
+ const styledOutput = output
277
+ .split("\n")
278
+ .map((line) => theme.fg("toolOutput", line))
279
+ .join("\n");
280
+ if (this.expanded) {
281
+ // Show all lines when expanded
282
+ this.contentBox.addChild(new Text(`\n${styledOutput}`, 0, 0));
283
+ }
284
+ else {
285
+ // Use visual line truncation when collapsed with width-aware caching
286
+ let cachedWidth;
287
+ let cachedLines;
288
+ let cachedSkipped;
289
+ this.contentBox.addChild({
290
+ render: (width) => {
291
+ if (cachedLines === undefined || cachedWidth !== width) {
292
+ const result = truncateToVisualLines(styledOutput, BASH_PREVIEW_LINES, width);
293
+ cachedLines = result.visualLines;
294
+ cachedSkipped = result.skippedCount;
295
+ cachedWidth = width;
296
+ }
297
+ if (cachedSkipped && cachedSkipped > 0) {
298
+ const hint = theme.fg("muted", `... (${cachedSkipped} earlier lines,`) +
299
+ ` ${keyHint("expandTools", "to expand")})`;
300
+ return ["", truncateToWidth(hint, width, "..."), ...cachedLines];
301
+ }
302
+ // Add blank line for spacing (matches expanded case)
303
+ return ["", ...cachedLines];
304
+ },
305
+ invalidate: () => {
306
+ cachedWidth = undefined;
307
+ cachedLines = undefined;
308
+ cachedSkipped = undefined;
309
+ },
310
+ });
311
+ }
312
+ }
313
+ // Truncation warnings
314
+ const truncation = this.result.details?.truncation;
315
+ const fullOutputPath = this.result.details?.fullOutputPath;
316
+ if (truncation?.truncated || fullOutputPath) {
317
+ const warnings = [];
318
+ if (fullOutputPath) {
319
+ warnings.push(`Full output: ${fullOutputPath}`);
320
+ }
321
+ if (truncation?.truncated) {
322
+ if (truncation.truncatedBy === "lines") {
323
+ warnings.push(`Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines`);
324
+ }
325
+ else {
326
+ warnings.push(`Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)`);
327
+ }
328
+ }
329
+ this.contentBox.addChild(new Text(`\n${theme.fg("warning", `[${warnings.join(". ")}]`)}`, 0, 0));
330
+ }
331
+ }
332
+ }
333
+ getTextOutput() {
334
+ if (!this.result)
335
+ return "";
336
+ const textBlocks = this.result.content?.filter((c) => c.type === "text") || [];
337
+ const imageBlocks = this.result.content?.filter((c) => c.type === "image") || [];
338
+ let output = textBlocks
339
+ .map((c) => {
340
+ // Use sanitizeBinaryOutput to handle binary data that crashes string-width
341
+ return sanitizeBinaryOutput(stripAnsi(c.text || "")).replace(/\r/g, "");
342
+ })
343
+ .join("\n");
344
+ const caps = getCapabilities();
345
+ if (imageBlocks.length > 0 && (!caps.images || !this.showImages)) {
346
+ const imageIndicators = imageBlocks
347
+ .map((img) => {
348
+ const dims = img.data ? (getImageDimensions(img.data, img.mimeType) ?? undefined) : undefined;
349
+ return imageFallback(img.mimeType, dims);
350
+ })
351
+ .join("\n");
352
+ output = output ? `${output}\n${imageIndicators}` : imageIndicators;
353
+ }
354
+ return output;
355
+ }
356
+ formatToolExecution() {
357
+ let text = "";
358
+ if (this.toolName === "read") {
359
+ const path = shortenPath(this.args?.file_path || this.args?.path || "");
360
+ const offset = this.args?.offset;
361
+ const limit = this.args?.limit;
362
+ let pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
363
+ if (offset !== undefined || limit !== undefined) {
364
+ const startLine = offset ?? 1;
365
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
366
+ pathDisplay += theme.fg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
367
+ }
368
+ text = `${theme.fg("toolTitle", theme.bold("read"))} ${pathDisplay}`;
369
+ if (this.result) {
370
+ const output = this.getTextOutput();
371
+ const rawPath = this.args?.file_path || this.args?.path || "";
372
+ const lang = getLanguageFromPath(rawPath);
373
+ const lines = lang ? highlightCode(replaceTabs(output), lang) : output.split("\n");
374
+ const maxLines = this.expanded ? lines.length : 10;
375
+ const displayLines = lines.slice(0, maxLines);
376
+ const remaining = lines.length - maxLines;
377
+ text +=
378
+ "\n\n" +
379
+ displayLines
380
+ .map((line) => (lang ? replaceTabs(line) : theme.fg("toolOutput", replaceTabs(line))))
381
+ .join("\n");
382
+ if (remaining > 0) {
383
+ text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("expandTools", "to expand")})`;
384
+ }
385
+ const truncation = this.result.details?.truncation;
386
+ if (truncation?.truncated) {
387
+ if (truncation.firstLineExceedsLimit) {
388
+ text +=
389
+ "\n" +
390
+ theme.fg("warning", `[First line exceeds ${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit]`);
391
+ }
392
+ else if (truncation.truncatedBy === "lines") {
393
+ text +=
394
+ "\n" +
395
+ theme.fg("warning", `[Truncated: showing ${truncation.outputLines} of ${truncation.totalLines} lines (${truncation.maxLines ?? DEFAULT_MAX_LINES} line limit)]`);
396
+ }
397
+ else {
398
+ text +=
399
+ "\n" +
400
+ theme.fg("warning", `[Truncated: ${truncation.outputLines} lines shown (${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit)]`);
401
+ }
402
+ }
403
+ }
404
+ }
405
+ else if (this.toolName === "write") {
406
+ const rawPath = this.args?.file_path || this.args?.path || "";
407
+ const path = shortenPath(rawPath);
408
+ const fileContent = this.args?.content || "";
409
+ const lang = getLanguageFromPath(rawPath);
410
+ const lines = fileContent
411
+ ? lang
412
+ ? highlightCode(replaceTabs(fileContent), lang)
413
+ : fileContent.split("\n")
414
+ : [];
415
+ const totalLines = lines.length;
416
+ text =
417
+ theme.fg("toolTitle", theme.bold("write")) +
418
+ " " +
419
+ (path ? theme.fg("accent", path) : theme.fg("toolOutput", "..."));
420
+ if (fileContent) {
421
+ const maxLines = this.expanded ? lines.length : 10;
422
+ const displayLines = lines.slice(0, maxLines);
423
+ const remaining = lines.length - maxLines;
424
+ text +=
425
+ "\n\n" +
426
+ displayLines
427
+ .map((line) => (lang ? replaceTabs(line) : theme.fg("toolOutput", replaceTabs(line))))
428
+ .join("\n");
429
+ if (remaining > 0) {
430
+ text +=
431
+ theme.fg("muted", `\n... (${remaining} more lines, ${totalLines} total,`) +
432
+ ` ${keyHint("expandTools", "to expand")})`;
433
+ }
434
+ }
435
+ // Show error if tool execution failed
436
+ if (this.result?.isError) {
437
+ const errorText = this.getTextOutput();
438
+ if (errorText) {
439
+ text += `\n\n${theme.fg("error", errorText)}`;
440
+ }
441
+ }
442
+ }
443
+ else if (this.toolName === "edit") {
444
+ const rawPath = this.args?.file_path || this.args?.path || "";
445
+ const path = shortenPath(rawPath);
446
+ // Build path display, appending :line if we have diff info
447
+ let pathDisplay = path ? theme.fg("accent", path) : theme.fg("toolOutput", "...");
448
+ const firstChangedLine = (this.editDiffPreview && "firstChangedLine" in this.editDiffPreview
449
+ ? this.editDiffPreview.firstChangedLine
450
+ : undefined) ||
451
+ (this.result && !this.result.isError ? this.result.details?.firstChangedLine : undefined);
452
+ if (firstChangedLine) {
453
+ pathDisplay += theme.fg("warning", `:${firstChangedLine}`);
454
+ }
455
+ text = `${theme.fg("toolTitle", theme.bold("edit"))} ${pathDisplay}`;
456
+ if (this.result?.isError) {
457
+ // Show error from result
458
+ const errorText = this.getTextOutput();
459
+ if (errorText) {
460
+ text += `\n\n${theme.fg("error", errorText)}`;
461
+ }
462
+ }
463
+ else if (this.result?.details?.diff) {
464
+ // Tool executed successfully - use the diff from result
465
+ // This takes priority over editDiffPreview which may have a stale error
466
+ // due to race condition (async preview computed after file was modified)
467
+ text += `\n\n${renderDiff(this.result.details.diff, { filePath: rawPath })}`;
468
+ }
469
+ else if (this.editDiffPreview) {
470
+ // Use cached diff preview (before tool executes)
471
+ if ("error" in this.editDiffPreview) {
472
+ text += `\n\n${theme.fg("error", this.editDiffPreview.error)}`;
473
+ }
474
+ else if (this.editDiffPreview.diff) {
475
+ text += `\n\n${renderDiff(this.editDiffPreview.diff, { filePath: rawPath })}`;
476
+ }
477
+ }
478
+ }
479
+ else if (this.toolName === "ls") {
480
+ const path = shortenPath(this.args?.path || ".");
481
+ const limit = this.args?.limit;
482
+ text = `${theme.fg("toolTitle", theme.bold("ls"))} ${theme.fg("accent", path)}`;
483
+ if (limit !== undefined) {
484
+ text += theme.fg("toolOutput", ` (limit ${limit})`);
485
+ }
486
+ if (this.result) {
487
+ const output = this.getTextOutput().trim();
488
+ if (output) {
489
+ const lines = output.split("\n");
490
+ const maxLines = this.expanded ? lines.length : 20;
491
+ const displayLines = lines.slice(0, maxLines);
492
+ const remaining = lines.length - maxLines;
493
+ text += `\n\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`;
494
+ if (remaining > 0) {
495
+ text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("expandTools", "to expand")})`;
496
+ }
497
+ }
498
+ const entryLimit = this.result.details?.entryLimitReached;
499
+ const truncation = this.result.details?.truncation;
500
+ if (entryLimit || truncation?.truncated) {
501
+ const warnings = [];
502
+ if (entryLimit) {
503
+ warnings.push(`${entryLimit} entries limit`);
504
+ }
505
+ if (truncation?.truncated) {
506
+ warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
507
+ }
508
+ text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
509
+ }
510
+ }
511
+ }
512
+ else if (this.toolName === "find") {
513
+ const pattern = this.args?.pattern || "";
514
+ const path = shortenPath(this.args?.path || ".");
515
+ const limit = this.args?.limit;
516
+ text =
517
+ theme.fg("toolTitle", theme.bold("find")) +
518
+ " " +
519
+ theme.fg("accent", pattern) +
520
+ theme.fg("toolOutput", ` in ${path}`);
521
+ if (limit !== undefined) {
522
+ text += theme.fg("toolOutput", ` (limit ${limit})`);
523
+ }
524
+ if (this.result) {
525
+ const output = this.getTextOutput().trim();
526
+ if (output) {
527
+ const lines = output.split("\n");
528
+ const maxLines = this.expanded ? lines.length : 20;
529
+ const displayLines = lines.slice(0, maxLines);
530
+ const remaining = lines.length - maxLines;
531
+ text += `\n\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`;
532
+ if (remaining > 0) {
533
+ text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("expandTools", "to expand")})`;
534
+ }
535
+ }
536
+ const resultLimit = this.result.details?.resultLimitReached;
537
+ const truncation = this.result.details?.truncation;
538
+ if (resultLimit || truncation?.truncated) {
539
+ const warnings = [];
540
+ if (resultLimit) {
541
+ warnings.push(`${resultLimit} results limit`);
542
+ }
543
+ if (truncation?.truncated) {
544
+ warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
545
+ }
546
+ text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
547
+ }
548
+ }
549
+ }
550
+ else if (this.toolName === "grep") {
551
+ const pattern = this.args?.pattern || "";
552
+ const path = shortenPath(this.args?.path || ".");
553
+ const glob = this.args?.glob;
554
+ const limit = this.args?.limit;
555
+ text =
556
+ theme.fg("toolTitle", theme.bold("grep")) +
557
+ " " +
558
+ theme.fg("accent", `/${pattern}/`) +
559
+ theme.fg("toolOutput", ` in ${path}`);
560
+ if (glob) {
561
+ text += theme.fg("toolOutput", ` (${glob})`);
562
+ }
563
+ if (limit !== undefined) {
564
+ text += theme.fg("toolOutput", ` limit ${limit}`);
565
+ }
566
+ if (this.result) {
567
+ const output = this.getTextOutput().trim();
568
+ if (output) {
569
+ const lines = output.split("\n");
570
+ const maxLines = this.expanded ? lines.length : 15;
571
+ const displayLines = lines.slice(0, maxLines);
572
+ const remaining = lines.length - maxLines;
573
+ text += `\n\n${displayLines.map((line) => theme.fg("toolOutput", line)).join("\n")}`;
574
+ if (remaining > 0) {
575
+ text += `${theme.fg("muted", `\n... (${remaining} more lines,`)} ${keyHint("expandTools", "to expand")})`;
576
+ }
577
+ }
578
+ const matchLimit = this.result.details?.matchLimitReached;
579
+ const truncation = this.result.details?.truncation;
580
+ const linesTruncated = this.result.details?.linesTruncated;
581
+ if (matchLimit || truncation?.truncated || linesTruncated) {
582
+ const warnings = [];
583
+ if (matchLimit) {
584
+ warnings.push(`${matchLimit} matches limit`);
585
+ }
586
+ if (truncation?.truncated) {
587
+ warnings.push(`${formatSize(truncation.maxBytes ?? DEFAULT_MAX_BYTES)} limit`);
588
+ }
589
+ if (linesTruncated) {
590
+ warnings.push("some lines truncated");
591
+ }
592
+ text += `\n${theme.fg("warning", `[Truncated: ${warnings.join(", ")}]`)}`;
593
+ }
594
+ }
595
+ }
596
+ else {
597
+ // Generic tool (shouldn't reach here for custom tools)
598
+ text = theme.fg("toolTitle", theme.bold(this.toolName));
599
+ const content = JSON.stringify(this.args, null, 2);
600
+ text += `\n\n${content}`;
601
+ const output = this.getTextOutput();
602
+ if (output) {
603
+ text += `\n${output}`;
604
+ }
605
+ }
606
+ return text;
607
+ }
608
+ }