@alumbwe/anvil 1.0.19 → 1.0.20

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 (494) hide show
  1. package/README.md +28 -30
  2. package/bin/anvil +2 -0
  3. package/package.json +71 -75
  4. package/src/agents/bundled-agents.generated.d.ts +14 -0
  5. package/src/agents/bundled-agents.generated.ts +3760 -0
  6. package/src/app.tsx +399 -0
  7. package/src/chat.tsx +1698 -0
  8. package/src/cli-args.ts +134 -0
  9. package/src/commands/__tests__/anvil-command-aliases.test.ts +56 -0
  10. package/src/commands/__tests__/bash-command.test.ts +445 -0
  11. package/src/commands/__tests__/command-args.test.ts +349 -0
  12. package/src/commands/__tests__/copy-conversation-clipboard.test.ts +108 -0
  13. package/src/commands/__tests__/copy-conversation.test.ts +226 -0
  14. package/src/commands/__tests__/diagnostics-command.test.ts +35 -0
  15. package/src/commands/__tests__/image.test.ts +99 -0
  16. package/src/commands/__tests__/init.test.ts +456 -0
  17. package/src/commands/__tests__/new-command-rotates-chat.test.ts +91 -0
  18. package/src/commands/__tests__/process-diagnostics.test.ts +54 -0
  19. package/src/commands/__tests__/prompt-builders.test.ts +28 -0
  20. package/src/commands/__tests__/router-input.test.ts +298 -0
  21. package/src/commands/ads.ts +44 -0
  22. package/src/commands/command-registry.ts +726 -0
  23. package/src/commands/copy-conversation.ts +392 -0
  24. package/src/commands/help.ts +13 -0
  25. package/src/commands/image.ts +20 -0
  26. package/src/commands/init.ts +121 -0
  27. package/src/commands/process-diagnostics.ts +120 -0
  28. package/src/commands/prompt-builders.ts +100 -0
  29. package/src/commands/publish.ts +203 -0
  30. package/src/commands/router-utils.ts +80 -0
  31. package/src/commands/router.ts +487 -0
  32. package/src/commands/usage.ts +26 -0
  33. package/src/components/__tests__/ad-banner.test.tsx +83 -0
  34. package/src/components/__tests__/anvil-landing-heading-row.test.tsx +91 -0
  35. package/src/components/__tests__/anvil-model-selector.test.tsx +519 -0
  36. package/src/components/__tests__/anvil-offer-invariants.test.ts +54 -0
  37. package/src/components/__tests__/anvil-takeover-prompt.test.tsx +212 -0
  38. package/src/components/__tests__/chat-input-bar.test.tsx +130 -0
  39. package/src/components/__tests__/grid-layout.integration.test.tsx +304 -0
  40. package/src/components/__tests__/grid-layout.test.tsx +1051 -0
  41. package/src/components/__tests__/message-block.completion.test.tsx +104 -0
  42. package/src/components/__tests__/message-block.streaming.test.tsx +78 -0
  43. package/src/components/__tests__/message-with-agents.test.tsx +579 -0
  44. package/src/components/__tests__/multiline-input.test.tsx +1132 -0
  45. package/src/components/__tests__/queue-panel.test.tsx +398 -0
  46. package/src/components/__tests__/selectable-list.test.ts +232 -0
  47. package/src/components/__tests__/status-bar.test.tsx +44 -0
  48. package/src/components/__tests__/status-indicator.test.tsx +204 -0
  49. package/src/components/__tests__/terminal-thinking-orb.test.tsx +111 -0
  50. package/src/components/__tests__/user-error-banner.test.tsx +102 -0
  51. package/src/components/ad-banner.tsx +323 -0
  52. package/src/components/agent-checklist.tsx +350 -0
  53. package/src/components/agent-mode-toggle.tsx +241 -0
  54. package/src/components/anvil-active-session-summary.tsx +63 -0
  55. package/src/components/anvil-landing-screen.tsx +865 -0
  56. package/src/components/anvil-mark.tsx +52 -0
  57. package/src/components/anvil-model-selector.tsx +1087 -0
  58. package/src/components/anvil-referral-banner.tsx +604 -0
  59. package/src/components/anvil-superseded-screen.tsx +62 -0
  60. package/src/components/ask-user/__tests__/multiple-choice-form.test.ts +432 -0
  61. package/src/components/ask-user/__tests__/validation.test.ts +213 -0
  62. package/src/components/ask-user/components/accordion-question.tsx +153 -0
  63. package/src/components/ask-user/components/custom-answer-input.tsx +66 -0
  64. package/src/components/ask-user/components/options-list.tsx +133 -0
  65. package/src/components/ask-user/components/question-header.tsx +80 -0
  66. package/src/components/ask-user/components/question-option.tsx +89 -0
  67. package/src/components/ask-user/constants.ts +38 -0
  68. package/src/components/ask-user/index.tsx +631 -0
  69. package/src/components/ask-user/utils/validation.ts +85 -0
  70. package/src/components/attachment-card.tsx +65 -0
  71. package/src/components/blocks/agent-block-grid.tsx +58 -0
  72. package/src/components/blocks/agent-branch-item.tsx +311 -0
  73. package/src/components/blocks/agent-branch-wrapper.tsx +493 -0
  74. package/src/components/blocks/agent-list-branch.tsx +77 -0
  75. package/src/components/blocks/ask-user-branch.tsx +81 -0
  76. package/src/components/blocks/block-helpers.ts +11 -0
  77. package/src/components/blocks/blocks-renderer.tsx +289 -0
  78. package/src/components/blocks/content-with-markdown.tsx +33 -0
  79. package/src/components/blocks/image-block.tsx +128 -0
  80. package/src/components/blocks/implementor-row.tsx +429 -0
  81. package/src/components/blocks/single-block.tsx +201 -0
  82. package/src/components/blocks/thinking-block.tsx +67 -0
  83. package/src/components/blocks/tool-block-group.tsx +57 -0
  84. package/src/components/blocks/tool-branch.tsx +206 -0
  85. package/src/components/blocks/user-content-copy.tsx +157 -0
  86. package/src/components/bottom-banner.tsx +136 -0
  87. package/src/components/build-mode-buttons.tsx +110 -0
  88. package/src/components/button.tsx +66 -0
  89. package/src/components/chat-header.tsx +77 -0
  90. package/src/components/chat-history-screen.tsx +413 -0
  91. package/src/components/chat-input-bar.tsx +523 -0
  92. package/src/components/clickable-title-box.tsx +66 -0
  93. package/src/components/clickable.tsx +119 -0
  94. package/src/components/collapse-button.tsx +34 -0
  95. package/src/components/copy-button.tsx +203 -0
  96. package/src/components/elapsed-timer.tsx +55 -0
  97. package/src/components/error-boundary.tsx +55 -0
  98. package/src/components/feedback-container.tsx +181 -0
  99. package/src/components/feedback-icon-button.tsx +89 -0
  100. package/src/components/feedback-input-mode.tsx +344 -0
  101. package/src/components/file-attachment-card.tsx +98 -0
  102. package/src/components/grid-layout.tsx +80 -0
  103. package/src/components/help-banner.tsx +127 -0
  104. package/src/components/highlighted-text.tsx +52 -0
  105. package/src/components/image-card.tsx +167 -0
  106. package/src/components/image-thumbnail.tsx +114 -0
  107. package/src/components/input-cursor.tsx +77 -0
  108. package/src/components/input-mode-banner.tsx +57 -0
  109. package/src/components/load-previous-button.tsx +48 -0
  110. package/src/components/login-modal.tsx +475 -0
  111. package/src/components/message-block.tsx +346 -0
  112. package/src/components/message-footer.tsx +258 -0
  113. package/src/components/message-with-agents.tsx +506 -0
  114. package/src/components/mode-divider.tsx +43 -0
  115. package/src/components/multiline-input.tsx +1241 -0
  116. package/src/components/out-of-credits-banner.tsx +160 -0
  117. package/src/components/pending-attachments-banner.tsx +116 -0
  118. package/src/components/pending-bash-message.tsx +59 -0
  119. package/src/components/progress-bar.tsx +81 -0
  120. package/src/components/project-picker-screen.tsx +485 -0
  121. package/src/components/publish-confirmation.tsx +493 -0
  122. package/src/components/publish-container.tsx +687 -0
  123. package/src/components/queue-panel.tsx +310 -0
  124. package/src/components/raised-pill.tsx +95 -0
  125. package/src/components/renderers/plan-box.tsx +59 -0
  126. package/src/components/review-screen.tsx +112 -0
  127. package/src/components/scroll-to-bottom-button.tsx +34 -0
  128. package/src/components/segmented-control.tsx +187 -0
  129. package/src/components/selectable-list.tsx +248 -0
  130. package/src/components/selected-chips.tsx +80 -0
  131. package/src/components/separator.tsx +40 -0
  132. package/src/components/session-ended-banner.tsx +226 -0
  133. package/src/components/shimmer-text.tsx +248 -0
  134. package/src/components/status-bar.tsx +313 -0
  135. package/src/components/subscription-limit-banner.tsx +206 -0
  136. package/src/components/suggested-prompts.tsx +184 -0
  137. package/src/components/suggestion-menu.tsx +215 -0
  138. package/src/components/terminal-command-display.tsx +151 -0
  139. package/src/components/terminal-link.tsx +97 -0
  140. package/src/components/terminal-thinking-orb.tsx +95 -0
  141. package/src/components/text-attachment-card.tsx +77 -0
  142. package/src/components/thinking.tsx +109 -0
  143. package/src/components/tools/__tests__/apply-patch.test.tsx +84 -0
  144. package/src/components/tools/__tests__/code-search.test.tsx +45 -0
  145. package/src/components/tools/__tests__/gravity-index.test.ts +76 -0
  146. package/src/components/tools/__tests__/render-ui.test.tsx +92 -0
  147. package/src/components/tools/__tests__/run-terminal-command.test.ts +233 -0
  148. package/src/components/tools/apply-patch.tsx +95 -0
  149. package/src/components/tools/code-search.tsx +45 -0
  150. package/src/components/tools/composio.tsx +152 -0
  151. package/src/components/tools/diff-viewer.tsx +76 -0
  152. package/src/components/tools/glob.tsx +61 -0
  153. package/src/components/tools/gravity-index.tsx +77 -0
  154. package/src/components/tools/list-directory.tsx +61 -0
  155. package/src/components/tools/read-docs.tsx +37 -0
  156. package/src/components/tools/read-files.tsx +91 -0
  157. package/src/components/tools/read-subtree.tsx +44 -0
  158. package/src/components/tools/read-url.tsx +33 -0
  159. package/src/components/tools/registry.ts +126 -0
  160. package/src/components/tools/render-ui.tsx +122 -0
  161. package/src/components/tools/run-terminal-command.tsx +79 -0
  162. package/src/components/tools/skill.tsx +29 -0
  163. package/src/components/tools/str-replace.tsx +77 -0
  164. package/src/components/tools/suggest-followups.tsx +369 -0
  165. package/src/components/tools/task-completed.tsx +16 -0
  166. package/src/components/tools/tool-call-item.tsx +273 -0
  167. package/src/components/tools/types.ts +55 -0
  168. package/src/components/tools/web-search.tsx +33 -0
  169. package/src/components/tools/write-file.tsx +16 -0
  170. package/src/components/tools/write-todos.tsx +95 -0
  171. package/src/components/top-banner.tsx +178 -0
  172. package/src/components/usage-banner.tsx +244 -0
  173. package/src/components/user-error-banner.tsx +56 -0
  174. package/src/components/validation-error-popover.tsx +192 -0
  175. package/src/contexts/__tests__/chat-runtime-context.test.tsx +149 -0
  176. package/src/contexts/chat-runtime-context.tsx +240 -0
  177. package/src/data/slash-commands.test.ts +24 -0
  178. package/src/data/slash-commands.ts +249 -0
  179. package/src/entry.ts +12 -0
  180. package/src/hooks/__tests__/holds-live-anvil-slot.test.ts +49 -0
  181. package/src/hooks/__tests__/session-fetch-signal.test.ts +63 -0
  182. package/src/hooks/__tests__/use-activity-query.test.ts +1000 -0
  183. package/src/hooks/__tests__/use-ask-user-bridge.test.ts +176 -0
  184. package/src/hooks/__tests__/use-auth-query.test.ts +21 -0
  185. package/src/hooks/__tests__/use-chat-input-remount.test.tsx +66 -0
  186. package/src/hooks/__tests__/use-clipboard.test.tsx +73 -0
  187. package/src/hooks/__tests__/use-connection-status.test.ts +112 -0
  188. package/src/hooks/__tests__/use-directory-browser.test.ts +203 -0
  189. package/src/hooks/__tests__/use-gravity-ad.test.ts +73 -0
  190. package/src/hooks/__tests__/use-grid-layout.test.ts +349 -0
  191. package/src/hooks/__tests__/use-input-history.test.ts +699 -0
  192. package/src/hooks/__tests__/use-message-queue-editing.test.tsx +209 -0
  193. package/src/hooks/__tests__/use-message-queue.test.tsx +71 -0
  194. package/src/hooks/__tests__/use-path-tab-completion.test.ts +315 -0
  195. package/src/hooks/__tests__/use-queue-controls.test.ts +68 -0
  196. package/src/hooks/__tests__/use-queue-ui.test.tsx +83 -0
  197. package/src/hooks/__tests__/use-searchable-list.test.ts +359 -0
  198. package/src/hooks/__tests__/use-send-message-timer.test.ts +146 -0
  199. package/src/hooks/__tests__/use-suggestion-engine-mention.test.ts +361 -0
  200. package/src/hooks/__tests__/use-suggestion-engine.test.ts +485 -0
  201. package/src/hooks/__tests__/use-terminal-focus.test.ts +66 -0
  202. package/src/hooks/__tests__/use-terminal-layout.test.ts +675 -0
  203. package/src/hooks/__tests__/use-timeout.test.ts +330 -0
  204. package/src/hooks/__tests__/use-usage-query.test.ts +502 -0
  205. package/src/hooks/__tests__/use-user-details-query.test.ts +195 -0
  206. package/src/hooks/helpers/__tests__/send-message.test.ts +1918 -0
  207. package/src/hooks/helpers/send-message.ts +654 -0
  208. package/src/hooks/stream-state.ts +83 -0
  209. package/src/hooks/use-activity-query.ts +668 -0
  210. package/src/hooks/use-agent-validation.ts +74 -0
  211. package/src/hooks/use-anvil-ctrl-c-exit.ts +23 -0
  212. package/src/hooks/use-anvil-session-progress.ts +34 -0
  213. package/src/hooks/use-anvil-session.ts +806 -0
  214. package/src/hooks/use-anvil-streak-query.ts +61 -0
  215. package/src/hooks/use-ask-user-bridge.ts +92 -0
  216. package/src/hooks/use-auth-query.ts +262 -0
  217. package/src/hooks/use-auth-state.ts +152 -0
  218. package/src/hooks/use-chat-input.ts +105 -0
  219. package/src/hooks/use-chat-keyboard.ts +321 -0
  220. package/src/hooks/use-chat-messages.ts +254 -0
  221. package/src/hooks/use-chat-state.ts +165 -0
  222. package/src/hooks/use-chat-streaming.ts +180 -0
  223. package/src/hooks/use-chat-ui.ts +131 -0
  224. package/src/hooks/use-clipboard.ts +151 -0
  225. package/src/hooks/use-connection-status.ts +142 -0
  226. package/src/hooks/use-directory-browser.ts +88 -0
  227. package/src/hooks/use-elapsed-time.ts +116 -0
  228. package/src/hooks/use-event.ts +34 -0
  229. package/src/hooks/use-exit-handler.ts +66 -0
  230. package/src/hooks/use-fetch-login-url.ts +65 -0
  231. package/src/hooks/use-fingerprint.ts +61 -0
  232. package/src/hooks/use-gravity-ad.ts +656 -0
  233. package/src/hooks/use-grid-layout.ts +73 -0
  234. package/src/hooks/use-input-history.ts +173 -0
  235. package/src/hooks/use-login-keyboard-handlers.ts +75 -0
  236. package/src/hooks/use-login-polling.ts +114 -0
  237. package/src/hooks/use-logo.tsx +161 -0
  238. package/src/hooks/use-message-queue.ts +387 -0
  239. package/src/hooks/use-now.ts +20 -0
  240. package/src/hooks/use-path-tab-completion.ts +88 -0
  241. package/src/hooks/use-publish-mutation.ts +60 -0
  242. package/src/hooks/use-queue-controls.ts +68 -0
  243. package/src/hooks/use-queue-ui.ts +74 -0
  244. package/src/hooks/use-scroll-management.ts +184 -0
  245. package/src/hooks/use-searchable-list.ts +98 -0
  246. package/src/hooks/use-send-message.ts +773 -0
  247. package/src/hooks/use-sheen-animation.tsx +88 -0
  248. package/src/hooks/use-subscription-query.ts +71 -0
  249. package/src/hooks/use-suggestion-engine.ts +783 -0
  250. package/src/hooks/use-terminal-breakpoints.ts +77 -0
  251. package/src/hooks/use-terminal-dimensions.ts +46 -0
  252. package/src/hooks/use-terminal-focus.ts +29 -0
  253. package/src/hooks/use-terminal-layout.ts +189 -0
  254. package/src/hooks/use-theme.tsx +140 -0
  255. package/src/hooks/use-timeout.ts +85 -0
  256. package/src/hooks/use-update-preference.ts +66 -0
  257. package/src/hooks/use-usage-monitor.ts +55 -0
  258. package/src/hooks/use-usage-query.ts +124 -0
  259. package/src/hooks/use-user-details-query.ts +93 -0
  260. package/src/hooks/use-why-did-you-update.ts +177 -0
  261. package/src/index.tsx +473 -0
  262. package/src/init/__tests__/init-direnv.test.ts +526 -0
  263. package/src/init/init-app.ts +34 -0
  264. package/src/init/init-direnv.ts +133 -0
  265. package/src/login/constants.ts +60 -0
  266. package/src/login/login-flow.ts +204 -0
  267. package/src/login/plain-login.ts +108 -0
  268. package/src/login/utils.ts +183 -0
  269. package/src/native/ripgrep.ts +80 -0
  270. package/src/polyfills/bun-strip-ansi.ts +12 -0
  271. package/src/pre-init/tree-sitter-wasm.ts +96 -0
  272. package/src/project-files.ts +102 -0
  273. package/src/setup/key-setup.ts +295 -0
  274. package/src/setup/load-env.ts +106 -0
  275. package/src/state/__tests__/chat-store-focus.test.ts +22 -0
  276. package/src/state/__tests__/feedback-store.test.ts +257 -0
  277. package/src/state/anvil-model-store.ts +42 -0
  278. package/src/state/anvil-session-store.ts +53 -0
  279. package/src/state/chat-history-store.ts +30 -0
  280. package/src/state/chat-store.ts +544 -0
  281. package/src/state/feedback-store.ts +160 -0
  282. package/src/state/login-store.ts +150 -0
  283. package/src/state/message-block-store.ts +149 -0
  284. package/src/state/publish-store.ts +146 -0
  285. package/src/state/queue-panel-store.ts +26 -0
  286. package/src/state/review-store.ts +24 -0
  287. package/src/types/anvil-session.ts +17 -0
  288. package/src/types/chat-state.ts +15 -0
  289. package/src/types/chat.ts +241 -0
  290. package/src/types/contracts/send-message.ts +12 -0
  291. package/src/types/env.ts +98 -0
  292. package/src/types/function-params.ts +28 -0
  293. package/src/types/react19-compat.d.ts +19 -0
  294. package/src/types/store.ts +111 -0
  295. package/src/types/theme-system.ts +144 -0
  296. package/src/types/utils.ts +3 -0
  297. package/src/utils/__tests__/active-run.test.ts +140 -0
  298. package/src/utils/__tests__/activity-tracker.test.ts +440 -0
  299. package/src/utils/__tests__/agent-display.test.ts +139 -0
  300. package/src/utils/__tests__/analytics-client.test.ts +262 -0
  301. package/src/utils/__tests__/anonymous-id.test.ts +74 -0
  302. package/src/utils/__tests__/anvil-api.test.ts +578 -0
  303. package/src/utils/__tests__/anvil-instance-owner.test.ts +69 -0
  304. package/src/utils/__tests__/anvil-model-navigation.test.ts +102 -0
  305. package/src/utils/__tests__/anvil-premium-reset.test.ts +79 -0
  306. package/src/utils/__tests__/anvil-referral-cache.test.ts +116 -0
  307. package/src/utils/__tests__/anvil-session-api.test.ts +215 -0
  308. package/src/utils/__tests__/anvil-session-display.test.ts +23 -0
  309. package/src/utils/__tests__/anvil-streak-line.test.ts +217 -0
  310. package/src/utils/__tests__/arrays.test.ts +264 -0
  311. package/src/utils/__tests__/bash-context-processor.test.ts +38 -0
  312. package/src/utils/__tests__/block-processor.test.ts +810 -0
  313. package/src/utils/__tests__/chat-history.test.ts +203 -0
  314. package/src/utils/__tests__/chat-input-key-intercept.test.ts +40 -0
  315. package/src/utils/__tests__/chat-meta.test.ts +130 -0
  316. package/src/utils/__tests__/clipboard-linux.test.ts +359 -0
  317. package/src/utils/__tests__/clipboard.test.ts +910 -0
  318. package/src/utils/__tests__/code-search-summary.test.ts +84 -0
  319. package/src/utils/__tests__/collapse-helpers.test.ts +1109 -0
  320. package/src/utils/__tests__/error-handling.test.ts +658 -0
  321. package/src/utils/__tests__/exit-cleanly.test.ts +110 -0
  322. package/src/utils/__tests__/feedback-helpers.test.ts +444 -0
  323. package/src/utils/__tests__/feedback-submission.test.ts +26 -0
  324. package/src/utils/__tests__/fetch-usage.test.ts +355 -0
  325. package/src/utils/__tests__/fingerprint.test.ts +144 -0
  326. package/src/utils/__tests__/fixtures/terminal-command-broker-child.ts +7 -0
  327. package/src/utils/__tests__/fixtures/terminal-command-broker-entry.ts +3 -0
  328. package/src/utils/__tests__/fixtures/terminal-command-broker-owner.ts +33 -0
  329. package/src/utils/__tests__/format-elapsed-time.test.ts +103 -0
  330. package/src/utils/__tests__/format-timeout.test.ts +87 -0
  331. package/src/utils/__tests__/image-dimensions.test.ts +249 -0
  332. package/src/utils/__tests__/image-processor.test.ts +209 -0
  333. package/src/utils/__tests__/implementor-helpers.test.ts +1458 -0
  334. package/src/utils/__tests__/keyboard-actions.test.ts +660 -0
  335. package/src/utils/__tests__/layout-helpers.test.ts +36 -0
  336. package/src/utils/__tests__/lazy-response-ads.test.ts +167 -0
  337. package/src/utils/__tests__/log-shipper.test.ts +53 -0
  338. package/src/utils/__tests__/markdown-renderer.test.tsx +421 -0
  339. package/src/utils/__tests__/message-block-helpers.test.ts +1263 -0
  340. package/src/utils/__tests__/message-updater.test.ts +645 -0
  341. package/src/utils/__tests__/osc-timeout-scenarios.test.ts +172 -0
  342. package/src/utils/__tests__/pending-attachments.test.ts +277 -0
  343. package/src/utils/__tests__/polling-backoff.test.ts +51 -0
  344. package/src/utils/__tests__/queue-panel-actions.test.ts +148 -0
  345. package/src/utils/__tests__/run-state-storage.test.ts +919 -0
  346. package/src/utils/__tests__/safe-json.test.ts +77 -0
  347. package/src/utils/__tests__/sdk-event-handlers.test.ts +685 -0
  348. package/src/utils/__tests__/send-message-helpers.test.ts +1898 -0
  349. package/src/utils/__tests__/send-message-timer.test.ts +64 -0
  350. package/src/utils/__tests__/settings.test.ts +99 -0
  351. package/src/utils/__tests__/strings.test.ts +192 -0
  352. package/src/utils/__tests__/terminal-color-detection.test.ts +425 -0
  353. package/src/utils/__tests__/terminal-command-broker.test.ts +528 -0
  354. package/src/utils/__tests__/terminal-enter-detection.test.ts +77 -0
  355. package/src/utils/__tests__/terminal-io.test.ts +137 -0
  356. package/src/utils/__tests__/terminal-protocol-controller.test.ts +119 -0
  357. package/src/utils/__tests__/text-layout.test.ts +82 -0
  358. package/src/utils/__tests__/theme-platform-detection.test.ts +125 -0
  359. package/src/utils/__tests__/think-tag-parser.test.ts +159 -0
  360. package/src/utils/__tests__/trace-writer.test.ts +223 -0
  361. package/src/utils/__tests__/trim-chat-logs.test.ts +78 -0
  362. package/src/utils/__tests__/usage-banner-state.test.ts +298 -0
  363. package/src/utils/__tests__/validation-error-formatting.test.ts +126 -0
  364. package/src/utils/__tests__/windows-terminal-health.test.ts +101 -0
  365. package/src/utils/__tests__/write-file-atomic.test.ts +127 -0
  366. package/src/utils/active-run.ts +131 -0
  367. package/src/utils/activity-tracker.ts +66 -0
  368. package/src/utils/agent-display.ts +87 -0
  369. package/src/utils/agent-helpers.ts +27 -0
  370. package/src/utils/agent-id-utils.ts +14 -0
  371. package/src/utils/ai-message-id.ts +12 -0
  372. package/src/utils/analytics.ts +361 -0
  373. package/src/utils/anonymous-id.ts +87 -0
  374. package/src/utils/anvil-agent-selection.ts +33 -0
  375. package/src/utils/anvil-api.ts +612 -0
  376. package/src/utils/anvil-client.ts +217 -0
  377. package/src/utils/anvil-instance-owner.ts +66 -0
  378. package/src/utils/anvil-model-navigation.ts +50 -0
  379. package/src/utils/anvil-premium-reset.ts +55 -0
  380. package/src/utils/anvil-referral-cache.ts +51 -0
  381. package/src/utils/anvil-session-api.ts +243 -0
  382. package/src/utils/anvil-session-display.ts +21 -0
  383. package/src/utils/anvil-streak-line.ts +93 -0
  384. package/src/utils/arrays.ts +117 -0
  385. package/src/utils/auth.ts +258 -0
  386. package/src/utils/bash-context-processor.ts +47 -0
  387. package/src/utils/bash-messages.ts +108 -0
  388. package/src/utils/block-margins.ts +35 -0
  389. package/src/utils/block-operations.ts +523 -0
  390. package/src/utils/block-processor.ts +213 -0
  391. package/src/utils/chat-history.ts +253 -0
  392. package/src/utils/chat-input-key-intercept.ts +52 -0
  393. package/src/utils/chat-meta.ts +90 -0
  394. package/src/utils/chat-scroll-accel.ts +151 -0
  395. package/src/utils/clipboard-image.ts +588 -0
  396. package/src/utils/clipboard.ts +397 -0
  397. package/src/utils/code-search-summary.ts +70 -0
  398. package/src/utils/collapse-helpers.ts +256 -0
  399. package/src/utils/config-dir.ts +24 -0
  400. package/src/utils/constants.ts +190 -0
  401. package/src/utils/create-event-handler-state.ts +73 -0
  402. package/src/utils/create-run-config.ts +121 -0
  403. package/src/utils/detect-shell.ts +116 -0
  404. package/src/utils/directory-browser.ts +72 -0
  405. package/src/utils/engagement.ts +55 -0
  406. package/src/utils/env.ts +92 -0
  407. package/src/utils/error-handling.ts +255 -0
  408. package/src/utils/error-messages.ts +62 -0
  409. package/src/utils/exit-cleanly.ts +86 -0
  410. package/src/utils/feedback-helpers.ts +103 -0
  411. package/src/utils/feedback-submission.ts +22 -0
  412. package/src/utils/fetch-usage.ts +76 -0
  413. package/src/utils/fingerprint.ts +245 -0
  414. package/src/utils/format-elapsed-time.ts +33 -0
  415. package/src/utils/format-session-units.ts +6 -0
  416. package/src/utils/format-timeout.ts +28 -0
  417. package/src/utils/format-validation-errors-for-message.ts +84 -0
  418. package/src/utils/git.ts +17 -0
  419. package/src/utils/helpers.ts +47 -0
  420. package/src/utils/image-display.ts +67 -0
  421. package/src/utils/image-handler.ts +325 -0
  422. package/src/utils/image-processor.ts +133 -0
  423. package/src/utils/image-thumbnail.ts +78 -0
  424. package/src/utils/implementor-helpers.ts +787 -0
  425. package/src/utils/input-modes.ts +174 -0
  426. package/src/utils/keyboard-actions.ts +392 -0
  427. package/src/utils/keypad-keys.ts +47 -0
  428. package/src/utils/layout-helpers.ts +33 -0
  429. package/src/utils/lazy-response-ads.ts +1 -0
  430. package/src/utils/local-agent-registry.ts +474 -0
  431. package/src/utils/log-shipper.ts +126 -0
  432. package/src/utils/logger.ts +327 -0
  433. package/src/utils/markdown-renderer.tsx +1090 -0
  434. package/src/utils/math.ts +3 -0
  435. package/src/utils/message-block-helpers.ts +684 -0
  436. package/src/utils/message-history.ts +143 -0
  437. package/src/utils/message-tree-utils.ts +67 -0
  438. package/src/utils/message-updater.ts +279 -0
  439. package/src/utils/open-file.ts +146 -0
  440. package/src/utils/open-url.ts +63 -0
  441. package/src/utils/path-completion.ts +111 -0
  442. package/src/utils/path-helpers.ts +38 -0
  443. package/src/utils/pending-attachments.ts +360 -0
  444. package/src/utils/polling-backoff.ts +59 -0
  445. package/src/utils/project-picker.ts +12 -0
  446. package/src/utils/queue-panel-actions.ts +70 -0
  447. package/src/utils/recent-projects.ts +156 -0
  448. package/src/utils/renderer-cleanup.ts +233 -0
  449. package/src/utils/response-ad-positions.ts +1 -0
  450. package/src/utils/run-state-storage.ts +603 -0
  451. package/src/utils/safe-json.ts +153 -0
  452. package/src/utils/sdk-event-handlers.ts +571 -0
  453. package/src/utils/send-message-helpers.ts +210 -0
  454. package/src/utils/send-message-timer.ts +110 -0
  455. package/src/utils/settings.ts +226 -0
  456. package/src/utils/skill-registry.ts +94 -0
  457. package/src/utils/spawn-agent-matcher.ts +55 -0
  458. package/src/utils/status-indicator-state.ts +107 -0
  459. package/src/utils/stream-chunk-processor.ts +57 -0
  460. package/src/utils/strings.ts +271 -0
  461. package/src/utils/subscription.ts +31 -0
  462. package/src/utils/syntax-highlighter.tsx +19 -0
  463. package/src/utils/terminal-color-detection.ts +474 -0
  464. package/src/utils/terminal-command-broker.ts +467 -0
  465. package/src/utils/terminal-enter-detection.ts +63 -0
  466. package/src/utils/terminal-images.ts +229 -0
  467. package/src/utils/terminal-io.ts +85 -0
  468. package/src/utils/terminal-protocol-controller.ts +173 -0
  469. package/src/utils/terminal-reset-sequences.ts +33 -0
  470. package/src/utils/terminal-title.ts +84 -0
  471. package/src/utils/terminal-watchdog.ts +366 -0
  472. package/src/utils/text-layout.ts +149 -0
  473. package/src/utils/theme-config.ts +141 -0
  474. package/src/utils/theme-system.ts +1206 -0
  475. package/src/utils/think-tag-parser.ts +95 -0
  476. package/src/utils/time-format.ts +21 -0
  477. package/src/utils/trace-writer.ts +162 -0
  478. package/src/utils/ui-constants.ts +69 -0
  479. package/src/utils/update-checker.ts +110 -0
  480. package/src/utils/usage-banner-state.ts +146 -0
  481. package/src/utils/validation-error-formatting.ts +86 -0
  482. package/src/utils/validation-error-helpers.ts +19 -0
  483. package/src/utils/windows-terminal-health.ts +124 -0
  484. package/src/utils/word-wrap-utils.ts +53 -0
  485. package/src/utils/write-file-atomic.ts +54 -0
  486. package/src/utils/yield-to-event-loop.ts +9 -0
  487. package/bin/anvil.cjs +0 -40
  488. package/dist/bundled.js +0 -123508
  489. package/dist/bundled.js.map +0 -828
  490. package/dist/vendor/ripgrep/arm64-darwin/rg +0 -0
  491. package/dist/vendor/ripgrep/arm64-linux/rg +0 -0
  492. package/dist/vendor/ripgrep/x64-darwin/rg +0 -0
  493. package/dist/vendor/ripgrep/x64-linux/rg +0 -0
  494. package/dist/vendor/ripgrep/x64-win32/rg.exe +0 -0
@@ -0,0 +1,1263 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+
3
+ import {
4
+ getAgentBaseName,
5
+ extractPlanFromBuffer,
6
+ autoCollapseBlocks,
7
+ extractSpawnAgentResultContent,
8
+ appendInterruptionNotice,
9
+ createAgentBlock,
10
+ updateBlocksRecursively,
11
+ nestBlockUnderParent,
12
+ extractBlockById,
13
+ transformAskUserBlocks,
14
+ updateToolBlockWithOutput,
15
+ scrubPlanTags,
16
+ scrubPlanTagsInBlocks,
17
+ insertPlanBlock,
18
+ moveSpawnAgentBlock,
19
+ stripHiddenAgentBlocks,
20
+ } from '../message-block-helpers'
21
+
22
+ import type {
23
+ ContentBlock,
24
+ AgentContentBlock,
25
+ AskUserContentBlock,
26
+ TextContentBlock,
27
+ ToolContentBlock,
28
+ } from '../../types/chat'
29
+
30
+ describe('stripHiddenAgentBlocks', () => {
31
+ const agentBlock = (
32
+ agentType: string,
33
+ blocks?: ContentBlock[],
34
+ ): AgentContentBlock => ({
35
+ type: 'agent',
36
+ agentId: `id-${agentType}`,
37
+ agentName: agentType,
38
+ agentType,
39
+ content: '',
40
+ status: 'complete',
41
+ ...(blocks ? { blocks } : {}),
42
+ })
43
+
44
+ test('removes context-pruner blocks (bundled and qualified ids)', () => {
45
+ const blocks: ContentBlock[] = [
46
+ agentBlock('context-pruner'),
47
+ agentBlock('anvil/context-pruner@1.0.0'),
48
+ agentBlock('file-picker'),
49
+ ]
50
+
51
+ const result = stripHiddenAgentBlocks(blocks)
52
+
53
+ expect(result).toHaveLength(1)
54
+ expect((result[0] as AgentContentBlock).agentType).toBe('file-picker')
55
+ })
56
+
57
+ test('removes hidden blocks nested under other agents', () => {
58
+ const blocks: ContentBlock[] = [
59
+ agentBlock('base2', [
60
+ agentBlock('context-pruner'),
61
+ agentBlock('file-picker'),
62
+ ]),
63
+ ]
64
+
65
+ const result = stripHiddenAgentBlocks(blocks)
66
+
67
+ const children = (result[0] as AgentContentBlock).blocks!
68
+ expect(children).toHaveLength(1)
69
+ expect((children[0] as AgentContentBlock).agentType).toBe('file-picker')
70
+ })
71
+
72
+ test('returns the same array when nothing is hidden', () => {
73
+ const blocks: ContentBlock[] = [
74
+ agentBlock('file-picker'),
75
+ { type: 'text', content: 'hello' },
76
+ ]
77
+
78
+ expect(stripHiddenAgentBlocks(blocks)).toBe(blocks)
79
+ })
80
+
81
+ test('tolerates corrupted persisted shapes without throwing', () => {
82
+ const corruptedEntries = [
83
+ null,
84
+ { type: 'agent', agentId: 'a1', status: 'complete' }, // missing agentType
85
+ { ...agentBlock('base2'), blocks: 'not-an-array' },
86
+ ] as unknown as ContentBlock[]
87
+
88
+ expect(stripHiddenAgentBlocks(corruptedEntries)).toBe(corruptedEntries)
89
+ expect(
90
+ stripHiddenAgentBlocks('not-an-array' as unknown as ContentBlock[]),
91
+ ).toBe('not-an-array' as unknown as ContentBlock[])
92
+ })
93
+ })
94
+
95
+ describe('getAgentBaseName', () => {
96
+ test('extracts base name from scoped versioned name', () => {
97
+ expect(getAgentBaseName('anvil/file-picker@0.0.2')).toBe('file-picker')
98
+ })
99
+
100
+ test('extracts base name from simple versioned name', () => {
101
+ expect(getAgentBaseName('file-picker@1.0.0')).toBe('file-picker')
102
+ })
103
+
104
+ test('returns simple name unchanged', () => {
105
+ expect(getAgentBaseName('file-picker')).toBe('file-picker')
106
+ })
107
+
108
+ test('normalizes direct tool aliases to canonical agent names', () => {
109
+ expect(getAgentBaseName('code_reviewer_lite')).toBe('code-reviewer-lite')
110
+ })
111
+
112
+ test('handles scoped name without version', () => {
113
+ expect(getAgentBaseName('anvil/file-picker')).toBe('file-picker')
114
+ })
115
+
116
+ test('handles empty string', () => {
117
+ expect(getAgentBaseName('')).toBe('')
118
+ })
119
+
120
+ test('handles name with multiple slashes', () => {
121
+ expect(getAgentBaseName('@scope/sub/agent@1.0.0')).toBe('agent')
122
+ })
123
+ })
124
+
125
+ describe('extractPlanFromBuffer', () => {
126
+ test('extracts plan content between tags', () => {
127
+ const buffer = 'Some text <PLAN>This is the plan</PLAN> more text'
128
+ expect(extractPlanFromBuffer(buffer)).toBe('This is the plan')
129
+ })
130
+
131
+ test('trims whitespace from extracted plan', () => {
132
+ const buffer = '<PLAN> \n Plan with whitespace \n </PLAN>'
133
+ expect(extractPlanFromBuffer(buffer)).toBe('Plan with whitespace')
134
+ })
135
+
136
+ test('returns null when no opening tag', () => {
137
+ const buffer = 'This is the plan</PLAN>'
138
+ expect(extractPlanFromBuffer(buffer)).toBeNull()
139
+ })
140
+
141
+ test('returns null when no closing tag', () => {
142
+ const buffer = '<PLAN>This is the plan'
143
+ expect(extractPlanFromBuffer(buffer)).toBeNull()
144
+ })
145
+
146
+ test('returns null when tags are in wrong order', () => {
147
+ const buffer = '</PLAN>content<PLAN>'
148
+ expect(extractPlanFromBuffer(buffer)).toBeNull()
149
+ })
150
+
151
+ test('returns null for empty buffer', () => {
152
+ expect(extractPlanFromBuffer('')).toBeNull()
153
+ })
154
+
155
+ test('handles multiline plan content', () => {
156
+ const buffer =
157
+ '<PLAN>\n1. First step\n2. Second step\n3. Third step\n</PLAN>'
158
+ expect(extractPlanFromBuffer(buffer)).toBe(
159
+ '1. First step\n2. Second step\n3. Third step',
160
+ )
161
+ })
162
+ })
163
+
164
+ describe('scrubPlanTags helpers', () => {
165
+ test('removes plan tags from text', () => {
166
+ expect(scrubPlanTags('<PLAN>Plan</PLAN> trailing')).toBe(' trailing')
167
+ })
168
+
169
+ test('scrubs plan tags inside text blocks and removes empties', () => {
170
+ const blocks: ContentBlock[] = [
171
+ { type: 'text', content: '<PLAN>Plan</PLAN>' },
172
+ { type: 'text', content: 'Keep me' },
173
+ { type: 'tool', toolCallId: 'id', toolName: 'read_files', input: {} },
174
+ ]
175
+ const result = scrubPlanTagsInBlocks(blocks)
176
+ expect(result).toHaveLength(2)
177
+ expect(result[0]).toEqual({ type: 'text', content: 'Keep me' })
178
+ expect(result[1].type).toBe('tool')
179
+ })
180
+
181
+ test('inserts plan block after scrubbing', () => {
182
+ const blocks: ContentBlock[] = [
183
+ { type: 'text', content: 'Intro <PLAN>secret</PLAN>' },
184
+ ]
185
+ const result = insertPlanBlock(blocks, 'Plan body')
186
+ expect(result).toHaveLength(2)
187
+ expect(result[0]).toEqual({ type: 'text', content: 'Intro ' })
188
+ expect(result[1]).toEqual({ type: 'plan', content: 'Plan body' })
189
+ })
190
+ })
191
+
192
+ describe('autoCollapseBlocks', () => {
193
+ test('collapses text blocks with thinkingId', () => {
194
+ const blocks: ContentBlock[] = [
195
+ { type: 'text', content: 'thinking', thinkingId: 'think-1' },
196
+ ]
197
+ const result = autoCollapseBlocks(blocks)
198
+ expect(result[0]).toHaveProperty('thinkingCollapseState', 'hidden')
199
+ })
200
+
201
+ test('preserves user-opened text blocks', () => {
202
+ const blocks: ContentBlock[] = [
203
+ {
204
+ type: 'text',
205
+ content: 'thinking',
206
+ thinkingId: 'think-1',
207
+ userOpened: true,
208
+ },
209
+ ]
210
+ const result = autoCollapseBlocks(blocks)
211
+ expect(result[0]).not.toHaveProperty('isCollapsed')
212
+ })
213
+
214
+ test('collapses agent blocks', () => {
215
+ const blocks: ContentBlock[] = [
216
+ {
217
+ type: 'agent',
218
+ agentId: 'agent-1',
219
+ agentName: 'Test Agent',
220
+ agentType: 'test',
221
+ content: '',
222
+ status: 'complete',
223
+ blocks: [],
224
+ initialPrompt: '',
225
+ },
226
+ ]
227
+ const result = autoCollapseBlocks(blocks)
228
+ expect(result[0]).toHaveProperty('isCollapsed', true)
229
+ })
230
+
231
+ test('recursively collapses nested agent blocks', () => {
232
+ const blocks: ContentBlock[] = [
233
+ {
234
+ type: 'agent',
235
+ agentId: 'parent',
236
+ agentName: 'Parent',
237
+ agentType: 'parent',
238
+ content: '',
239
+ status: 'complete',
240
+ blocks: [
241
+ {
242
+ type: 'agent',
243
+ agentId: 'child',
244
+ agentName: 'Child',
245
+ agentType: 'child',
246
+ content: '',
247
+ status: 'complete',
248
+ blocks: [],
249
+ initialPrompt: '',
250
+ },
251
+ ],
252
+ initialPrompt: '',
253
+ },
254
+ ]
255
+ const result = autoCollapseBlocks(blocks)
256
+ expect(result[0]).toHaveProperty('isCollapsed', true)
257
+ expect((result[0] as AgentContentBlock).blocks![0]).toHaveProperty(
258
+ 'isCollapsed',
259
+ true,
260
+ )
261
+ })
262
+
263
+ test('collapses tool blocks', () => {
264
+ const blocks: ContentBlock[] = [
265
+ {
266
+ type: 'tool',
267
+ toolCallId: 'tool-1',
268
+ toolName: 'read_files',
269
+ input: {},
270
+ },
271
+ ]
272
+ const result = autoCollapseBlocks(blocks)
273
+ expect(result[0]).toHaveProperty('isCollapsed', true)
274
+ })
275
+
276
+ test('preserves user-opened tool blocks', () => {
277
+ const blocks: ContentBlock[] = [
278
+ {
279
+ type: 'tool',
280
+ toolCallId: 'tool-1',
281
+ toolName: 'read_files',
282
+ input: {},
283
+ userOpened: true,
284
+ },
285
+ ]
286
+ const result = autoCollapseBlocks(blocks)
287
+ expect(result[0]).not.toHaveProperty('isCollapsed')
288
+ })
289
+
290
+ test('leaves regular text blocks unchanged', () => {
291
+ const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
292
+ const result = autoCollapseBlocks(blocks)
293
+ expect(result[0]).toEqual({ type: 'text', content: 'Hello' })
294
+ })
295
+ })
296
+
297
+ describe('extractSpawnAgentResultContent', () => {
298
+ test('returns string value directly', () => {
299
+ const result = extractSpawnAgentResultContent('Simple result')
300
+ expect(result).toEqual({ content: 'Simple result', hasError: false })
301
+ })
302
+
303
+ test('extracts error message', () => {
304
+ const result = extractSpawnAgentResultContent({
305
+ errorMessage: 'Something went wrong',
306
+ })
307
+ expect(result).toEqual({
308
+ content: 'Something went wrong',
309
+ hasError: true,
310
+ })
311
+ })
312
+
313
+ test('extracts nested value string', () => {
314
+ const result = extractSpawnAgentResultContent({
315
+ type: 'lastMessage',
316
+ value: 'Nested value',
317
+ })
318
+ expect(result).toEqual({ content: 'Nested value', hasError: false })
319
+ })
320
+
321
+ test('extracts message field', () => {
322
+ const result = extractSpawnAgentResultContent({
323
+ message: 'Message content',
324
+ })
325
+ expect(result).toEqual({ content: 'Message content', hasError: false })
326
+ })
327
+
328
+ test('falls back to formatted output for unknown structure', () => {
329
+ const result = extractSpawnAgentResultContent({ unknownField: 123 })
330
+ expect(result.hasError).toBe(false)
331
+ expect(result.content).toContain('unknownField')
332
+ })
333
+
334
+ test('handles null value', () => {
335
+ const result = extractSpawnAgentResultContent(null)
336
+ expect(result.hasError).toBe(false)
337
+ })
338
+
339
+ test('handles undefined value', () => {
340
+ const result = extractSpawnAgentResultContent(undefined)
341
+ expect(result.hasError).toBe(false)
342
+ })
343
+
344
+ test('extracts text from lastMessage output mode with Message array', () => {
345
+ // This is the format returned by agents with outputMode: 'last_message'
346
+ const result = extractSpawnAgentResultContent({
347
+ type: 'lastMessage',
348
+ value: [
349
+ {
350
+ role: 'assistant',
351
+ content: [
352
+ { type: 'text', text: 'Here are the research findings:' },
353
+ { type: 'text', text: ' Important information found.' },
354
+ ],
355
+ },
356
+ ],
357
+ })
358
+ expect(result).toEqual({
359
+ content: 'Here are the research findings: Important information found.',
360
+ hasError: false,
361
+ })
362
+ })
363
+
364
+ test('extracts text from multiple assistant messages in lastMessage output', () => {
365
+ const result = extractSpawnAgentResultContent({
366
+ type: 'lastMessage',
367
+ value: [
368
+ {
369
+ role: 'assistant',
370
+ content: [{ type: 'text', text: 'First message' }],
371
+ },
372
+ {
373
+ role: 'tool',
374
+ content: [{ type: 'json', value: {} }],
375
+ },
376
+ {
377
+ role: 'assistant',
378
+ content: [{ type: 'text', text: 'Second message' }],
379
+ },
380
+ ],
381
+ })
382
+ expect(result).toEqual({
383
+ content: 'First message\nSecond message',
384
+ hasError: false,
385
+ })
386
+ })
387
+
388
+ test('handles lastMessage with empty content array', () => {
389
+ const result = extractSpawnAgentResultContent({
390
+ type: 'lastMessage',
391
+ value: [
392
+ {
393
+ role: 'assistant',
394
+ content: [],
395
+ },
396
+ ],
397
+ })
398
+ expect(result).toEqual({ content: '', hasError: false })
399
+ })
400
+
401
+ test('handles lastMessage with no assistant messages', () => {
402
+ const result = extractSpawnAgentResultContent({
403
+ type: 'lastMessage',
404
+ value: [
405
+ {
406
+ role: 'tool',
407
+ content: [{ type: 'json', value: {} }],
408
+ },
409
+ ],
410
+ })
411
+ expect(result).toEqual({ content: '', hasError: false })
412
+ })
413
+
414
+ test('handles allMessages output mode', () => {
415
+ const result = extractSpawnAgentResultContent({
416
+ type: 'allMessages',
417
+ value: [
418
+ {
419
+ role: 'assistant',
420
+ content: [{ type: 'text', text: 'First response' }],
421
+ },
422
+ {
423
+ role: 'user',
424
+ content: [{ type: 'text', text: 'Follow up' }],
425
+ },
426
+ {
427
+ role: 'assistant',
428
+ content: [{ type: 'text', text: 'Second response' }],
429
+ },
430
+ ],
431
+ })
432
+ expect(result).toEqual({
433
+ content: 'First response\nSecond response',
434
+ hasError: false,
435
+ })
436
+ })
437
+
438
+ test('handles structuredOutput with message field', () => {
439
+ const result = extractSpawnAgentResultContent({
440
+ type: 'structuredOutput',
441
+ value: { message: 'Structured output message' },
442
+ })
443
+ expect(result).toEqual({
444
+ content: 'Structured output message',
445
+ hasError: false,
446
+ })
447
+ })
448
+
449
+ test('uses an empty structuredOutput message as no display content', () => {
450
+ const result = extractSpawnAgentResultContent({
451
+ type: 'structuredOutput',
452
+ value: {
453
+ message: '',
454
+ results: [
455
+ {
456
+ stdout: 'Found 1 match\n./file.ts:\nLine 1: needle',
457
+ message: 'Exit code: 0',
458
+ },
459
+ ],
460
+ },
461
+ })
462
+
463
+ expect(result).toEqual({ content: '', hasError: false })
464
+ })
465
+ })
466
+
467
+ describe('appendInterruptionNotice', () => {
468
+ test('appends to last text block', () => {
469
+ const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
470
+ const result = appendInterruptionNotice(blocks)
471
+ expect(result).toHaveLength(1)
472
+ expect(result[0]).toEqual({
473
+ type: 'text',
474
+ content: 'Hello\n\n[response interrupted]',
475
+ })
476
+ })
477
+
478
+ test('preserves text block fields when appending', () => {
479
+ const blocks: ContentBlock[] = [
480
+ {
481
+ type: 'text',
482
+ content: 'Hello',
483
+ color: 'blue',
484
+ status: 'running',
485
+ thinkingId: 'think-1',
486
+ userOpened: true,
487
+ thinkingCollapseState: 'hidden',
488
+ },
489
+ ]
490
+ const result = appendInterruptionNotice(blocks)
491
+ expect(result[0]).toMatchObject({
492
+ color: 'blue',
493
+ status: 'running',
494
+ thinkingId: 'think-1',
495
+ userOpened: true,
496
+ thinkingCollapseState: 'hidden',
497
+ content: 'Hello\n\n[response interrupted]',
498
+ })
499
+ })
500
+
501
+ test('adds new block when last is not text', () => {
502
+ const blocks: ContentBlock[] = [
503
+ {
504
+ type: 'tool',
505
+ toolCallId: 'tool-1',
506
+ toolName: 'read_files',
507
+ input: {},
508
+ },
509
+ ]
510
+ const result = appendInterruptionNotice(blocks)
511
+ expect(result).toHaveLength(2)
512
+ expect(result[1]).toEqual({
513
+ type: 'text',
514
+ content: '[response interrupted]',
515
+ })
516
+ })
517
+
518
+ test('adds notice to empty blocks array', () => {
519
+ const result = appendInterruptionNotice([])
520
+ expect(result).toHaveLength(1)
521
+ expect(result[0]).toEqual({
522
+ type: 'text',
523
+ content: '[response interrupted]',
524
+ })
525
+ })
526
+
527
+ test('preserves other blocks when appending to text', () => {
528
+ const blocks: ContentBlock[] = [
529
+ {
530
+ type: 'tool',
531
+ toolCallId: 'tool-1',
532
+ toolName: 'read_files',
533
+ input: {},
534
+ },
535
+ { type: 'text', content: 'Some response' },
536
+ ]
537
+ const result = appendInterruptionNotice(blocks)
538
+ expect(result).toHaveLength(2)
539
+ expect(result[0].type).toBe('tool')
540
+ expect(result[1]).toEqual({
541
+ type: 'text',
542
+ content: 'Some response\n\n[response interrupted]',
543
+ })
544
+ })
545
+ })
546
+
547
+ describe('createAgentBlock', () => {
548
+ test('creates basic agent block with required fields', () => {
549
+ const block = createAgentBlock({
550
+ agentId: 'agent-123',
551
+ agentType: 'file-picker',
552
+ })
553
+ expect(block.type).toBe('agent')
554
+ expect(block.agentId).toBe('agent-123')
555
+ expect(block.agentName).toBe('file-picker')
556
+ expect(block.agentType).toBe('file-picker')
557
+ expect(block.content).toBe('')
558
+ expect(block.status).toBe('running')
559
+ expect(block.blocks).toEqual([])
560
+ expect(block.initialPrompt).toBe('')
561
+ })
562
+
563
+ test('includes prompt when provided', () => {
564
+ const block = createAgentBlock({
565
+ agentId: 'agent-123',
566
+ agentType: 'file-picker',
567
+ prompt: 'Find relevant files',
568
+ })
569
+ expect(block.initialPrompt).toBe('Find relevant files')
570
+ })
571
+
572
+ test('includes params when provided', () => {
573
+ const block = createAgentBlock({
574
+ agentId: 'agent-123',
575
+ agentType: 'file-picker',
576
+ params: { directories: ['src'] },
577
+ })
578
+ expect(block.params).toEqual({ directories: ['src'] })
579
+ })
580
+
581
+ test('uses fallback values for empty agentType', () => {
582
+ const block = createAgentBlock({
583
+ agentId: 'agent-123',
584
+ agentType: '',
585
+ })
586
+ expect(block.agentName).toBe('Agent')
587
+ expect(block.agentType).toBe('unknown')
588
+ })
589
+ })
590
+
591
+ describe('updateBlocksRecursively', () => {
592
+ test('updates target block at top level', () => {
593
+ const blocks: ContentBlock[] = [
594
+ {
595
+ type: 'agent',
596
+ agentId: 'agent-1',
597
+ agentName: 'Test',
598
+ agentType: 'test',
599
+ content: '',
600
+ status: 'running',
601
+ blocks: [],
602
+ initialPrompt: '',
603
+ },
604
+ ]
605
+ const result = updateBlocksRecursively(blocks, 'agent-1', (block) => ({
606
+ ...block,
607
+ status: 'complete' as const,
608
+ }))
609
+ expect((result[0] as AgentContentBlock).status).toBe('complete')
610
+ })
611
+
612
+ test('updates nested block', () => {
613
+ const blocks: ContentBlock[] = [
614
+ {
615
+ type: 'agent',
616
+ agentId: 'parent',
617
+ agentName: 'Parent',
618
+ agentType: 'parent',
619
+ content: '',
620
+ status: 'running',
621
+ blocks: [
622
+ {
623
+ type: 'agent',
624
+ agentId: 'child',
625
+ agentName: 'Child',
626
+ agentType: 'child',
627
+ content: '',
628
+ status: 'running',
629
+ blocks: [],
630
+ initialPrompt: '',
631
+ },
632
+ ],
633
+ initialPrompt: '',
634
+ },
635
+ ]
636
+ const result = updateBlocksRecursively(blocks, 'child', (block) => ({
637
+ ...block,
638
+ status: 'complete' as const,
639
+ }))
640
+ expect((result[0] as AgentContentBlock).blocks![0]).toMatchObject({
641
+ status: 'complete',
642
+ })
643
+ })
644
+
645
+ test('returns original array if target not found', () => {
646
+ const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
647
+ const result = updateBlocksRecursively(
648
+ blocks,
649
+ 'nonexistent',
650
+ (block) => block,
651
+ )
652
+ expect(result).toBe(blocks)
653
+ })
654
+
655
+ test('handles deeply nested blocks', () => {
656
+ const blocks: ContentBlock[] = [
657
+ {
658
+ type: 'agent',
659
+ agentId: 'level-1',
660
+ agentName: 'L1',
661
+ agentType: 'l1',
662
+ content: '',
663
+ status: 'running',
664
+ blocks: [
665
+ {
666
+ type: 'agent',
667
+ agentId: 'level-2',
668
+ agentName: 'L2',
669
+ agentType: 'l2',
670
+ content: '',
671
+ status: 'running',
672
+ blocks: [
673
+ {
674
+ type: 'agent',
675
+ agentId: 'level-3',
676
+ agentName: 'L3',
677
+ agentType: 'l3',
678
+ content: '',
679
+ status: 'running',
680
+ blocks: [],
681
+ initialPrompt: '',
682
+ },
683
+ ],
684
+ initialPrompt: '',
685
+ },
686
+ ],
687
+ initialPrompt: '',
688
+ },
689
+ ]
690
+ const result = updateBlocksRecursively(blocks, 'level-3', (block) => ({
691
+ ...block,
692
+ content: 'updated',
693
+ }))
694
+ const level1 = result[0] as AgentContentBlock
695
+ const level2 = level1.blocks![0] as AgentContentBlock
696
+ const level3 = level2.blocks![0] as AgentContentBlock
697
+ expect(level3.content).toBe('updated')
698
+ })
699
+ })
700
+
701
+ describe('nestBlockUnderParent', () => {
702
+ test('nests block under existing parent', () => {
703
+ const blocks: ContentBlock[] = [
704
+ {
705
+ type: 'agent',
706
+ agentId: 'parent',
707
+ agentName: 'Parent',
708
+ agentType: 'parent',
709
+ content: '',
710
+ status: 'running',
711
+ blocks: [],
712
+ initialPrompt: '',
713
+ },
714
+ ]
715
+ const childBlock: ContentBlock = { type: 'text', content: 'Child content' }
716
+ const { blocks: result, parentFound } = nestBlockUnderParent(
717
+ blocks,
718
+ 'parent',
719
+ childBlock,
720
+ )
721
+ expect(parentFound).toBe(true)
722
+ expect((result[0] as AgentContentBlock).blocks).toHaveLength(1)
723
+ expect((result[0] as AgentContentBlock).blocks![0]).toEqual(childBlock)
724
+ })
725
+
726
+ test('returns parentFound false when parent not found', () => {
727
+ const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
728
+ const childBlock: ContentBlock = { type: 'text', content: 'Child' }
729
+ const { blocks: result, parentFound } = nestBlockUnderParent(
730
+ blocks,
731
+ 'nonexistent',
732
+ childBlock,
733
+ )
734
+ expect(parentFound).toBe(false)
735
+ expect(result).toBe(blocks)
736
+ })
737
+
738
+ test('appends to existing blocks in parent', () => {
739
+ const blocks: ContentBlock[] = [
740
+ {
741
+ type: 'agent',
742
+ agentId: 'parent',
743
+ agentName: 'Parent',
744
+ agentType: 'parent',
745
+ content: '',
746
+ status: 'running',
747
+ blocks: [{ type: 'text', content: 'Existing' }],
748
+ initialPrompt: '',
749
+ },
750
+ ]
751
+ const childBlock: ContentBlock = { type: 'text', content: 'New child' }
752
+ const { blocks: result, parentFound } = nestBlockUnderParent(
753
+ blocks,
754
+ 'parent',
755
+ childBlock,
756
+ )
757
+ expect(parentFound).toBe(true)
758
+ expect((result[0] as AgentContentBlock).blocks).toHaveLength(2)
759
+ expect((result[0] as AgentContentBlock).blocks![1]).toEqual(childBlock)
760
+ })
761
+
762
+ test('nests under deeply nested parent', () => {
763
+ const blocks: ContentBlock[] = [
764
+ {
765
+ type: 'agent',
766
+ agentId: 'grandparent',
767
+ agentName: 'GP',
768
+ agentType: 'gp',
769
+ content: '',
770
+ status: 'running',
771
+ blocks: [
772
+ {
773
+ type: 'agent',
774
+ agentId: 'parent',
775
+ agentName: 'Parent',
776
+ agentType: 'parent',
777
+ content: '',
778
+ status: 'running',
779
+ blocks: [],
780
+ initialPrompt: '',
781
+ },
782
+ ],
783
+ initialPrompt: '',
784
+ },
785
+ ]
786
+ const childBlock: ContentBlock = { type: 'text', content: 'Nested child' }
787
+ const { blocks: result, parentFound } = nestBlockUnderParent(
788
+ blocks,
789
+ 'parent',
790
+ childBlock,
791
+ )
792
+ expect(parentFound).toBe(true)
793
+ const grandparent = result[0] as AgentContentBlock
794
+ const parent = grandparent.blocks![0] as AgentContentBlock
795
+ expect(parent.blocks).toHaveLength(1)
796
+ expect(parent.blocks![0]).toEqual(childBlock)
797
+ })
798
+ })
799
+
800
+ describe('moveSpawnAgentBlock', () => {
801
+ test('replaces temp agent id with real id', () => {
802
+ const blocks: ContentBlock[] = [
803
+ {
804
+ type: 'agent',
805
+ agentId: 'temp',
806
+ agentName: 'Temp',
807
+ agentType: 'temp',
808
+ content: '',
809
+ status: 'running',
810
+ blocks: [],
811
+ initialPrompt: '',
812
+ },
813
+ ]
814
+ const result = moveSpawnAgentBlock(blocks, 'temp', 'real')
815
+ expect((result[0] as AgentContentBlock).agentId).toBe('real')
816
+ })
817
+
818
+ test('nests extracted block under parent when found', () => {
819
+ const blocks: ContentBlock[] = [
820
+ {
821
+ type: 'agent',
822
+ agentId: 'parent',
823
+ agentName: 'Parent',
824
+ agentType: 'parent',
825
+ content: '',
826
+ status: 'running',
827
+ blocks: [
828
+ {
829
+ type: 'agent',
830
+ agentId: 'temp',
831
+ agentName: 'Temp',
832
+ agentType: 'temp',
833
+ content: '',
834
+ status: 'running',
835
+ blocks: [],
836
+ initialPrompt: '',
837
+ },
838
+ ],
839
+ initialPrompt: '',
840
+ },
841
+ ]
842
+ const result = moveSpawnAgentBlock(blocks, 'temp', 'real', 'parent')
843
+ const parent = result[0] as AgentContentBlock
844
+ expect(parent.blocks).toHaveLength(1)
845
+ expect((parent.blocks![0] as AgentContentBlock).agentId).toBe('real')
846
+ })
847
+
848
+ test('updates in place when parent missing to preserve order', () => {
849
+ const blocks: ContentBlock[] = [
850
+ {
851
+ type: 'agent',
852
+ agentId: 'temp',
853
+ agentName: 'Temp',
854
+ agentType: 'temp',
855
+ content: '',
856
+ status: 'running',
857
+ blocks: [],
858
+ initialPrompt: '',
859
+ },
860
+ { type: 'text', content: 'other' },
861
+ ]
862
+ const result = moveSpawnAgentBlock(blocks, 'temp', 'real', 'missing')
863
+ // Block should stay in its original position (index 0), not move to end
864
+ expect(result[0]).toMatchObject({ type: 'agent', agentId: 'real' })
865
+ expect(result[1]).toMatchObject({ type: 'text', content: 'other' })
866
+ })
867
+
868
+ test('preserves block order when multiple agents resolve out of order', () => {
869
+ // Simulate spawn_agents creating 3 placeholder blocks in order
870
+ const blocks: ContentBlock[] = [
871
+ {
872
+ type: 'agent',
873
+ agentId: 'toolcall-0',
874
+ agentName: 'Agent A',
875
+ agentType: 'file-picker',
876
+ content: '',
877
+ status: 'running',
878
+ blocks: [],
879
+ initialPrompt: '',
880
+ },
881
+ {
882
+ type: 'agent',
883
+ agentId: 'toolcall-1',
884
+ agentName: 'Agent B',
885
+ agentType: 'code-searcher',
886
+ content: '',
887
+ status: 'running',
888
+ blocks: [],
889
+ initialPrompt: '',
890
+ },
891
+ {
892
+ type: 'agent',
893
+ agentId: 'toolcall-2',
894
+ agentName: 'Agent C',
895
+ agentType: 'commander',
896
+ content: '',
897
+ status: 'running',
898
+ blocks: [],
899
+ initialPrompt: '',
900
+ },
901
+ ]
902
+
903
+ // Agents resolve in different order: C first, then A, then B
904
+ let result = moveSpawnAgentBlock(blocks, 'toolcall-2', 'real-c')
905
+ result = moveSpawnAgentBlock(result, 'toolcall-0', 'real-a')
906
+ result = moveSpawnAgentBlock(result, 'toolcall-1', 'real-b')
907
+
908
+ // Order should be preserved: A, B, C
909
+ expect(result[0]).toMatchObject({ agentId: 'real-a' })
910
+ expect(result[1]).toMatchObject({ agentId: 'real-b' })
911
+ expect(result[2]).toMatchObject({ agentId: 'real-c' })
912
+ })
913
+ })
914
+
915
+ describe('extractBlockById', () => {
916
+ test('extracts block from top level', () => {
917
+ const blocks: ContentBlock[] = [
918
+ { type: 'text', content: 'Keep me' },
919
+ {
920
+ type: 'agent',
921
+ agentId: 'extract-me',
922
+ agentName: 'Extract',
923
+ agentType: 'extract',
924
+ content: '',
925
+ status: 'running',
926
+ blocks: [],
927
+ initialPrompt: '',
928
+ },
929
+ ]
930
+ const { remainingBlocks, extractedBlock } = extractBlockById(
931
+ blocks,
932
+ 'extract-me',
933
+ )
934
+ expect(remainingBlocks).toHaveLength(1)
935
+ expect(remainingBlocks[0].type).toBe('text')
936
+ expect(extractedBlock).not.toBeNull()
937
+ expect((extractedBlock as AgentContentBlock).agentId).toBe('extract-me')
938
+ })
939
+
940
+ test('returns null when block not found', () => {
941
+ const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
942
+ const { remainingBlocks, extractedBlock } = extractBlockById(
943
+ blocks,
944
+ 'nonexistent',
945
+ )
946
+ expect(remainingBlocks).toHaveLength(1)
947
+ expect(extractedBlock).toBeNull()
948
+ })
949
+
950
+ test('extracts from nested blocks', () => {
951
+ const blocks: ContentBlock[] = [
952
+ {
953
+ type: 'agent',
954
+ agentId: 'parent',
955
+ agentName: 'Parent',
956
+ agentType: 'parent',
957
+ content: '',
958
+ status: 'running',
959
+ blocks: [
960
+ {
961
+ type: 'agent',
962
+ agentId: 'nested-child',
963
+ agentName: 'Child',
964
+ agentType: 'child',
965
+ content: '',
966
+ status: 'running',
967
+ blocks: [],
968
+ initialPrompt: '',
969
+ },
970
+ ],
971
+ initialPrompt: '',
972
+ },
973
+ ]
974
+ const { remainingBlocks, extractedBlock } = extractBlockById(
975
+ blocks,
976
+ 'nested-child',
977
+ )
978
+ expect((remainingBlocks[0] as AgentContentBlock).blocks).toHaveLength(0)
979
+ expect(extractedBlock).not.toBeNull()
980
+ expect((extractedBlock as AgentContentBlock).agentId).toBe('nested-child')
981
+ })
982
+
983
+ test('handles empty blocks array', () => {
984
+ const { remainingBlocks, extractedBlock } = extractBlockById([], 'any-id')
985
+ expect(remainingBlocks).toHaveLength(0)
986
+ expect(extractedBlock).toBeNull()
987
+ })
988
+
989
+ test('preserves non-matching nested structure', () => {
990
+ const blocks: ContentBlock[] = [
991
+ {
992
+ type: 'agent',
993
+ agentId: 'parent',
994
+ agentName: 'Parent',
995
+ agentType: 'parent',
996
+ content: '',
997
+ status: 'running',
998
+ blocks: [
999
+ { type: 'text', content: 'Keep this' },
1000
+ {
1001
+ type: 'agent',
1002
+ agentId: 'extract-me',
1003
+ agentName: 'Extract',
1004
+ agentType: 'extract',
1005
+ content: '',
1006
+ status: 'running',
1007
+ blocks: [],
1008
+ initialPrompt: '',
1009
+ },
1010
+ { type: 'text', content: 'Keep this too' },
1011
+ ],
1012
+ initialPrompt: '',
1013
+ },
1014
+ ]
1015
+ const { remainingBlocks, extractedBlock } = extractBlockById(
1016
+ blocks,
1017
+ 'extract-me',
1018
+ )
1019
+ const parentBlock = remainingBlocks[0] as AgentContentBlock
1020
+ expect(parentBlock.blocks).toHaveLength(2)
1021
+ expect((parentBlock.blocks![0] as TextContentBlock).content).toBe(
1022
+ 'Keep this',
1023
+ )
1024
+ expect((parentBlock.blocks![1] as TextContentBlock).content).toBe(
1025
+ 'Keep this too',
1026
+ )
1027
+ expect(extractedBlock).not.toBeNull()
1028
+ })
1029
+ })
1030
+
1031
+ describe('transformAskUserBlocks', () => {
1032
+ test('transforms ask_user tool block to ask-user block', () => {
1033
+ const blocks: ContentBlock[] = [
1034
+ {
1035
+ type: 'tool',
1036
+ toolCallId: 'tool-123',
1037
+ toolName: 'ask_user',
1038
+ input: {
1039
+ questions: [
1040
+ { question: 'Pick one', options: [{ label: 'A' }, { label: 'B' }] },
1041
+ ],
1042
+ },
1043
+ },
1044
+ ]
1045
+ const result = transformAskUserBlocks(blocks, {
1046
+ toolCallId: 'tool-123',
1047
+ resultValue: { answers: [{ questionIndex: 0, selectedOption: 'A' }] },
1048
+ })
1049
+ expect(result[0].type).toBe('ask-user')
1050
+ const askUserBlock = result[0] as AskUserContentBlock
1051
+ expect(askUserBlock.answers).toEqual([
1052
+ { questionIndex: 0, selectedOption: 'A' },
1053
+ ])
1054
+ expect(askUserBlock.questions).toEqual([
1055
+ { question: 'Pick one', options: [{ label: 'A' }, { label: 'B' }] },
1056
+ ])
1057
+ })
1058
+
1059
+ test('transforms skipped ask_user block', () => {
1060
+ const blocks: ContentBlock[] = [
1061
+ {
1062
+ type: 'tool',
1063
+ toolCallId: 'tool-123',
1064
+ toolName: 'ask_user',
1065
+ input: {
1066
+ questions: [
1067
+ { question: 'Pick one', options: [{ label: 'A' }, { label: 'B' }] },
1068
+ ],
1069
+ },
1070
+ },
1071
+ ]
1072
+ const result = transformAskUserBlocks(blocks, {
1073
+ toolCallId: 'tool-123',
1074
+ resultValue: { skipped: true },
1075
+ })
1076
+ expect(result[0].type).toBe('ask-user')
1077
+ expect((result[0] as AskUserContentBlock).skipped).toBe(true)
1078
+ })
1079
+
1080
+ test('keeps tool block when no result data', () => {
1081
+ const blocks: ContentBlock[] = [
1082
+ {
1083
+ type: 'tool',
1084
+ toolCallId: 'tool-123',
1085
+ toolName: 'ask_user',
1086
+ input: { questions: [] },
1087
+ },
1088
+ ]
1089
+ const result = transformAskUserBlocks(blocks, {
1090
+ toolCallId: 'tool-123',
1091
+ resultValue: {},
1092
+ })
1093
+ expect(result[0].type).toBe('tool')
1094
+ })
1095
+
1096
+ test('does not transform non-matching tool', () => {
1097
+ const blocks: ContentBlock[] = [
1098
+ {
1099
+ type: 'tool',
1100
+ toolCallId: 'tool-123',
1101
+ toolName: 'ask_user',
1102
+ input: { questions: [] },
1103
+ },
1104
+ ]
1105
+ const result = transformAskUserBlocks(blocks, {
1106
+ toolCallId: 'different-id',
1107
+ resultValue: { answers: [{ questionIndex: 0, selectedOption: 'A' }] },
1108
+ })
1109
+ expect(result[0].type).toBe('tool')
1110
+ })
1111
+
1112
+ test('transforms nested ask_user in agent blocks', () => {
1113
+ const blocks: ContentBlock[] = [
1114
+ {
1115
+ type: 'agent',
1116
+ agentId: 'agent-1',
1117
+ agentName: 'Test',
1118
+ agentType: 'test',
1119
+ content: '',
1120
+ status: 'running',
1121
+ blocks: [
1122
+ {
1123
+ type: 'tool',
1124
+ toolCallId: 'tool-123',
1125
+ toolName: 'ask_user',
1126
+ input: { questions: [{ question: 'Q?' }] },
1127
+ },
1128
+ ],
1129
+ initialPrompt: '',
1130
+ },
1131
+ ]
1132
+ const result = transformAskUserBlocks(blocks, {
1133
+ toolCallId: 'tool-123',
1134
+ resultValue: { answers: ['Yes'] },
1135
+ })
1136
+ expect((result[0] as AgentContentBlock).blocks![0].type).toBe('ask-user')
1137
+ })
1138
+
1139
+ test('returns same reference when nothing changes', () => {
1140
+ const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
1141
+ const result = transformAskUserBlocks(blocks, {
1142
+ toolCallId: 'tool-123',
1143
+ resultValue: { answers: [{ questionIndex: 0, selectedOption: 'A' }] },
1144
+ })
1145
+ expect(result[0]).toBe(blocks[0])
1146
+ })
1147
+ })
1148
+
1149
+ describe('updateToolBlockWithOutput', () => {
1150
+ test('updates tool block with formatted output', () => {
1151
+ const blocks: ContentBlock[] = [
1152
+ {
1153
+ type: 'tool',
1154
+ toolCallId: 'tool-123',
1155
+ toolName: 'read_files',
1156
+ input: { paths: ['file.ts'] },
1157
+ },
1158
+ ]
1159
+ const result = updateToolBlockWithOutput(blocks, {
1160
+ toolCallId: 'tool-123',
1161
+ toolOutput: [{ type: 'text', value: 'file contents' }],
1162
+ })
1163
+ expect((result[0] as ToolContentBlock).output).toBeDefined()
1164
+ })
1165
+
1166
+ test('formats terminal command output specially', () => {
1167
+ const blocks: ContentBlock[] = [
1168
+ {
1169
+ type: 'tool',
1170
+ toolCallId: 'tool-123',
1171
+ toolName: 'run_terminal_command',
1172
+ input: { command: 'echo hi' },
1173
+ },
1174
+ ]
1175
+ const result = updateToolBlockWithOutput(blocks, {
1176
+ toolCallId: 'tool-123',
1177
+ toolOutput: [{ value: { stdout: 'hi\n', stderr: '' } }],
1178
+ })
1179
+ expect((result[0] as ToolContentBlock).output).toBe('hi\n')
1180
+ })
1181
+
1182
+ test('combines stdout and stderr for terminal commands', () => {
1183
+ const blocks: ContentBlock[] = [
1184
+ {
1185
+ type: 'tool',
1186
+ toolCallId: 'tool-123',
1187
+ toolName: 'run_terminal_command',
1188
+ input: { command: 'cmd' },
1189
+ },
1190
+ ]
1191
+ const result = updateToolBlockWithOutput(blocks, {
1192
+ toolCallId: 'tool-123',
1193
+ toolOutput: [{ value: { stdout: 'out', stderr: 'err' } }],
1194
+ })
1195
+ expect((result[0] as ToolContentBlock).output).toBe('outerr')
1196
+ })
1197
+
1198
+ test('does not update non-matching tool block', () => {
1199
+ const blocks: ContentBlock[] = [
1200
+ {
1201
+ type: 'tool',
1202
+ toolCallId: 'tool-123',
1203
+ toolName: 'read_files',
1204
+ input: {},
1205
+ },
1206
+ ]
1207
+ const result = updateToolBlockWithOutput(blocks, {
1208
+ toolCallId: 'different-id',
1209
+ toolOutput: [{ value: 'output' }],
1210
+ })
1211
+ expect((result[0] as ToolContentBlock).output).toBeUndefined()
1212
+ })
1213
+
1214
+ test('updates nested tool blocks in agent', () => {
1215
+ const blocks: ContentBlock[] = [
1216
+ {
1217
+ type: 'agent',
1218
+ agentId: 'agent-1',
1219
+ agentName: 'Test',
1220
+ agentType: 'test',
1221
+ content: '',
1222
+ status: 'running',
1223
+ blocks: [
1224
+ {
1225
+ type: 'tool',
1226
+ toolCallId: 'tool-123',
1227
+ toolName: 'read_files',
1228
+ input: {},
1229
+ },
1230
+ ],
1231
+ initialPrompt: '',
1232
+ },
1233
+ ]
1234
+ const result = updateToolBlockWithOutput(blocks, {
1235
+ toolCallId: 'tool-123',
1236
+ toolOutput: [{ type: 'text', value: 'contents' }],
1237
+ })
1238
+ expect(
1239
+ ((result[0] as AgentContentBlock).blocks![0] as ToolContentBlock).output,
1240
+ ).toBeDefined()
1241
+ })
1242
+
1243
+ test('returns same reference for unchanged nested blocks', () => {
1244
+ const nestedBlocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
1245
+ const blocks: ContentBlock[] = [
1246
+ {
1247
+ type: 'agent',
1248
+ agentId: 'agent-1',
1249
+ agentName: 'Test',
1250
+ agentType: 'test',
1251
+ content: '',
1252
+ status: 'running',
1253
+ blocks: nestedBlocks,
1254
+ initialPrompt: '',
1255
+ },
1256
+ ]
1257
+ const result = updateToolBlockWithOutput(blocks, {
1258
+ toolCallId: 'non-existent',
1259
+ toolOutput: [],
1260
+ })
1261
+ expect(result[0]).toBe(blocks[0])
1262
+ })
1263
+ })