@alumbwe/anvil 1.0.42 → 1.0.49

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 (238) hide show
  1. package/bin/anvil.js +18 -0
  2. package/package.json +13 -6
  3. package/src/agents/bundled-agents.generated.ts +1 -1
  4. package/src/index.tsx +27 -4
  5. package/src/login/plain-login.ts +60 -0
  6. package/src/login/supabase-auth.ts +346 -0
  7. package/src/utils/anvil-client.ts +4 -0
  8. package/src/utils/auth.ts +16 -4
  9. package/src/utils/config-dir.ts +1 -1
  10. package/vendor/@anvil/common/src/constants/deepseek-direct.ts +30 -1
  11. package/vendor/@anvil/common/src/util/project-ignore.ts +4 -3
  12. package/vendor/@anvil/sdk/dist/index.cjs +46 -10
  13. package/vendor/@anvil/sdk/dist/index.mjs +46 -10
  14. package/src/commands/__tests__/anvil-command-aliases.test.ts +0 -56
  15. package/src/commands/__tests__/bash-command.test.ts +0 -445
  16. package/src/commands/__tests__/command-args.test.ts +0 -349
  17. package/src/commands/__tests__/copy-conversation-clipboard.test.ts +0 -108
  18. package/src/commands/__tests__/copy-conversation.test.ts +0 -226
  19. package/src/commands/__tests__/diagnostics-command.test.ts +0 -35
  20. package/src/commands/__tests__/image.test.ts +0 -99
  21. package/src/commands/__tests__/init.test.ts +0 -456
  22. package/src/commands/__tests__/new-command-rotates-chat.test.ts +0 -91
  23. package/src/commands/__tests__/process-diagnostics.test.ts +0 -54
  24. package/src/commands/__tests__/prompt-builders.test.ts +0 -28
  25. package/src/commands/__tests__/router-input.test.ts +0 -298
  26. package/src/components/__tests__/ad-banner.test.tsx +0 -83
  27. package/src/components/__tests__/anvil-landing-heading-row.test.tsx +0 -91
  28. package/src/components/__tests__/anvil-model-selector.test.tsx +0 -519
  29. package/src/components/__tests__/anvil-offer-invariants.test.ts +0 -54
  30. package/src/components/__tests__/anvil-takeover-prompt.test.tsx +0 -212
  31. package/src/components/__tests__/chat-input-bar.test.tsx +0 -130
  32. package/src/components/__tests__/grid-layout.integration.test.tsx +0 -304
  33. package/src/components/__tests__/grid-layout.test.tsx +0 -1051
  34. package/src/components/__tests__/message-block.completion.test.tsx +0 -104
  35. package/src/components/__tests__/message-block.streaming.test.tsx +0 -78
  36. package/src/components/__tests__/message-with-agents.test.tsx +0 -579
  37. package/src/components/__tests__/multiline-input.test.tsx +0 -1132
  38. package/src/components/__tests__/queue-panel.test.tsx +0 -398
  39. package/src/components/__tests__/selectable-list.test.ts +0 -232
  40. package/src/components/__tests__/status-bar.test.tsx +0 -44
  41. package/src/components/__tests__/status-indicator.test.tsx +0 -204
  42. package/src/components/__tests__/terminal-thinking-orb.test.tsx +0 -111
  43. package/src/components/__tests__/user-error-banner.test.tsx +0 -102
  44. package/src/components/ask-user/__tests__/multiple-choice-form.test.ts +0 -432
  45. package/src/components/ask-user/__tests__/validation.test.ts +0 -213
  46. package/src/components/tools/__tests__/apply-patch.test.tsx +0 -84
  47. package/src/components/tools/__tests__/code-search.test.tsx +0 -45
  48. package/src/components/tools/__tests__/gravity-index.test.ts +0 -76
  49. package/src/components/tools/__tests__/render-ui.test.tsx +0 -92
  50. package/src/components/tools/__tests__/run-terminal-command.test.ts +0 -233
  51. package/src/contexts/__tests__/chat-runtime-context.test.tsx +0 -149
  52. package/src/data/slash-commands.test.ts +0 -24
  53. package/src/hooks/__tests__/holds-live-anvil-slot.test.ts +0 -49
  54. package/src/hooks/__tests__/session-fetch-signal.test.ts +0 -63
  55. package/src/hooks/__tests__/use-activity-query.test.ts +0 -1000
  56. package/src/hooks/__tests__/use-ask-user-bridge.test.ts +0 -176
  57. package/src/hooks/__tests__/use-auth-query.test.ts +0 -21
  58. package/src/hooks/__tests__/use-chat-input-remount.test.tsx +0 -66
  59. package/src/hooks/__tests__/use-clipboard.test.tsx +0 -73
  60. package/src/hooks/__tests__/use-connection-status.test.ts +0 -112
  61. package/src/hooks/__tests__/use-directory-browser.test.ts +0 -203
  62. package/src/hooks/__tests__/use-gravity-ad.test.ts +0 -73
  63. package/src/hooks/__tests__/use-grid-layout.test.ts +0 -349
  64. package/src/hooks/__tests__/use-input-history.test.ts +0 -699
  65. package/src/hooks/__tests__/use-message-queue-editing.test.tsx +0 -209
  66. package/src/hooks/__tests__/use-message-queue.test.tsx +0 -71
  67. package/src/hooks/__tests__/use-path-tab-completion.test.ts +0 -315
  68. package/src/hooks/__tests__/use-queue-controls.test.ts +0 -68
  69. package/src/hooks/__tests__/use-queue-ui.test.tsx +0 -83
  70. package/src/hooks/__tests__/use-searchable-list.test.ts +0 -359
  71. package/src/hooks/__tests__/use-send-message-timer.test.ts +0 -146
  72. package/src/hooks/__tests__/use-suggestion-engine-mention.test.ts +0 -361
  73. package/src/hooks/__tests__/use-suggestion-engine.test.ts +0 -485
  74. package/src/hooks/__tests__/use-terminal-focus.test.ts +0 -66
  75. package/src/hooks/__tests__/use-terminal-layout.test.ts +0 -675
  76. package/src/hooks/__tests__/use-timeout.test.ts +0 -330
  77. package/src/hooks/__tests__/use-usage-query.test.ts +0 -502
  78. package/src/hooks/__tests__/use-user-details-query.test.ts +0 -195
  79. package/src/hooks/helpers/__tests__/send-message.test.ts +0 -1918
  80. package/src/init/__tests__/init-direnv.test.ts +0 -526
  81. package/src/state/__tests__/chat-store-focus.test.ts +0 -22
  82. package/src/state/__tests__/feedback-store.test.ts +0 -257
  83. package/src/utils/__tests__/active-run.test.ts +0 -140
  84. package/src/utils/__tests__/activity-tracker.test.ts +0 -440
  85. package/src/utils/__tests__/agent-display.test.ts +0 -139
  86. package/src/utils/__tests__/analytics-client.test.ts +0 -262
  87. package/src/utils/__tests__/anonymous-id.test.ts +0 -74
  88. package/src/utils/__tests__/anvil-api.test.ts +0 -578
  89. package/src/utils/__tests__/anvil-instance-owner.test.ts +0 -69
  90. package/src/utils/__tests__/anvil-model-navigation.test.ts +0 -102
  91. package/src/utils/__tests__/anvil-premium-reset.test.ts +0 -79
  92. package/src/utils/__tests__/anvil-referral-cache.test.ts +0 -116
  93. package/src/utils/__tests__/anvil-session-api.test.ts +0 -215
  94. package/src/utils/__tests__/anvil-session-display.test.ts +0 -23
  95. package/src/utils/__tests__/anvil-streak-line.test.ts +0 -217
  96. package/src/utils/__tests__/arrays.test.ts +0 -264
  97. package/src/utils/__tests__/bash-context-processor.test.ts +0 -38
  98. package/src/utils/__tests__/block-processor.test.ts +0 -810
  99. package/src/utils/__tests__/chat-history.test.ts +0 -203
  100. package/src/utils/__tests__/chat-input-key-intercept.test.ts +0 -40
  101. package/src/utils/__tests__/chat-meta.test.ts +0 -130
  102. package/src/utils/__tests__/clipboard-linux.test.ts +0 -359
  103. package/src/utils/__tests__/clipboard.test.ts +0 -910
  104. package/src/utils/__tests__/code-search-summary.test.ts +0 -84
  105. package/src/utils/__tests__/collapse-helpers.test.ts +0 -1109
  106. package/src/utils/__tests__/error-handling.test.ts +0 -658
  107. package/src/utils/__tests__/exit-cleanly.test.ts +0 -110
  108. package/src/utils/__tests__/feedback-helpers.test.ts +0 -444
  109. package/src/utils/__tests__/feedback-submission.test.ts +0 -26
  110. package/src/utils/__tests__/fetch-usage.test.ts +0 -355
  111. package/src/utils/__tests__/fingerprint.test.ts +0 -144
  112. package/src/utils/__tests__/fixtures/terminal-command-broker-child.ts +0 -7
  113. package/src/utils/__tests__/fixtures/terminal-command-broker-entry.ts +0 -3
  114. package/src/utils/__tests__/fixtures/terminal-command-broker-owner.ts +0 -33
  115. package/src/utils/__tests__/format-elapsed-time.test.ts +0 -103
  116. package/src/utils/__tests__/format-timeout.test.ts +0 -87
  117. package/src/utils/__tests__/image-dimensions.test.ts +0 -249
  118. package/src/utils/__tests__/image-processor.test.ts +0 -209
  119. package/src/utils/__tests__/implementor-helpers.test.ts +0 -1458
  120. package/src/utils/__tests__/keyboard-actions.test.ts +0 -660
  121. package/src/utils/__tests__/layout-helpers.test.ts +0 -36
  122. package/src/utils/__tests__/lazy-response-ads.test.ts +0 -167
  123. package/src/utils/__tests__/log-shipper.test.ts +0 -53
  124. package/src/utils/__tests__/markdown-renderer.test.tsx +0 -421
  125. package/src/utils/__tests__/message-block-helpers.test.ts +0 -1263
  126. package/src/utils/__tests__/message-updater.test.ts +0 -645
  127. package/src/utils/__tests__/osc-timeout-scenarios.test.ts +0 -172
  128. package/src/utils/__tests__/pending-attachments.test.ts +0 -277
  129. package/src/utils/__tests__/polling-backoff.test.ts +0 -51
  130. package/src/utils/__tests__/queue-panel-actions.test.ts +0 -148
  131. package/src/utils/__tests__/run-state-storage.test.ts +0 -919
  132. package/src/utils/__tests__/safe-json.test.ts +0 -77
  133. package/src/utils/__tests__/sdk-event-handlers.test.ts +0 -685
  134. package/src/utils/__tests__/send-message-helpers.test.ts +0 -1898
  135. package/src/utils/__tests__/send-message-timer.test.ts +0 -64
  136. package/src/utils/__tests__/settings.test.ts +0 -99
  137. package/src/utils/__tests__/strings.test.ts +0 -192
  138. package/src/utils/__tests__/terminal-color-detection.test.ts +0 -425
  139. package/src/utils/__tests__/terminal-command-broker.test.ts +0 -528
  140. package/src/utils/__tests__/terminal-enter-detection.test.ts +0 -77
  141. package/src/utils/__tests__/terminal-io.test.ts +0 -137
  142. package/src/utils/__tests__/terminal-protocol-controller.test.ts +0 -119
  143. package/src/utils/__tests__/text-layout.test.ts +0 -82
  144. package/src/utils/__tests__/theme-platform-detection.test.ts +0 -125
  145. package/src/utils/__tests__/think-tag-parser.test.ts +0 -159
  146. package/src/utils/__tests__/trace-writer.test.ts +0 -223
  147. package/src/utils/__tests__/trim-chat-logs.test.ts +0 -78
  148. package/src/utils/__tests__/usage-banner-state.test.ts +0 -298
  149. package/src/utils/__tests__/validation-error-formatting.test.ts +0 -126
  150. package/src/utils/__tests__/windows-terminal-health.test.ts +0 -101
  151. package/src/utils/__tests__/write-file-atomic.test.ts +0 -127
  152. package/vendor/@anvil/common/src/__tests__/agent-validation.test.ts +0 -846
  153. package/vendor/@anvil/common/src/__tests__/anvil-model-availability.test.ts +0 -101
  154. package/vendor/@anvil/common/src/__tests__/anvil-models.test.ts +0 -1408
  155. package/vendor/@anvil/common/src/__tests__/anvil-public-data-use-copy.test.ts +0 -258
  156. package/vendor/@anvil/common/src/__tests__/anvil-referral-tiers.test.ts +0 -48
  157. package/vendor/@anvil/common/src/__tests__/anvil-spend-ceilings.test.ts +0 -282
  158. package/vendor/@anvil/common/src/__tests__/anvil-trust.test.ts +0 -526
  159. package/vendor/@anvil/common/src/__tests__/deepseek-direct.test.ts +0 -68
  160. package/vendor/@anvil/common/src/__tests__/disposable-email.test.ts +0 -132
  161. package/vendor/@anvil/common/src/__tests__/dynamic-agent-template-schema.test.ts +0 -422
  162. package/vendor/@anvil/common/src/__tests__/env-ci.test.ts +0 -167
  163. package/vendor/@anvil/common/src/__tests__/env-process.test.ts +0 -145
  164. package/vendor/@anvil/common/src/__tests__/foreign-client-shipped-agents.test.ts +0 -234
  165. package/vendor/@anvil/common/src/__tests__/foreign-client-signals.test.ts +0 -379
  166. package/vendor/@anvil/common/src/__tests__/free-agents.test.ts +0 -807
  167. package/vendor/@anvil/common/src/__tests__/gravity-capi.test.ts +0 -154
  168. package/vendor/@anvil/common/src/__tests__/handlesteps-parsing.test.ts +0 -246
  169. package/vendor/@anvil/common/src/__tests__/kimi-k3-god-only.test.ts +0 -77
  170. package/vendor/@anvil/common/src/__tests__/model-config.test.ts +0 -89
  171. package/vendor/@anvil/common/src/__tests__/project-file-tree.test.ts +0 -298
  172. package/vendor/@anvil/common/src/__tests__/provisioned-model-tiers.test.ts +0 -116
  173. package/vendor/@anvil/common/src/__tests__/reasoning-effort.test.ts +0 -167
  174. package/vendor/@anvil/common/src/__tests__/reddit-capi.test.ts +0 -132
  175. package/vendor/@anvil/common/src/__tests__/response-ad-positions.test.ts +0 -72
  176. package/vendor/@anvil/common/src/__tests__/user-state.test.ts +0 -30
  177. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding-gate.test.ts +0 -63
  178. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding.test.ts +0 -246
  179. package/vendor/@anvil/common/src/constants/__tests__/cf-worker-signals.test.ts +0 -106
  180. package/vendor/@anvil/common/src/testing/TESTING_PATTERNS.md +0 -351
  181. package/vendor/@anvil/common/src/testing/anvil-offer-invariants.ts +0 -169
  182. package/vendor/@anvil/common/src/testing/errors.ts +0 -33
  183. package/vendor/@anvil/common/src/testing/fixtures/agent-runtime.ts +0 -334
  184. package/vendor/@anvil/common/src/testing/impl/agent-runtime.ts +0 -6
  185. package/vendor/@anvil/common/src/testing/index.ts +0 -84
  186. package/vendor/@anvil/common/src/testing/mock-modules.ts +0 -53
  187. package/vendor/@anvil/common/src/testing/mock-types.ts +0 -123
  188. package/vendor/@anvil/common/src/testing/mocks/analytics.ts +0 -261
  189. package/vendor/@anvil/common/src/testing/mocks/child-process.ts +0 -93
  190. package/vendor/@anvil/common/src/testing/mocks/crypto.ts +0 -218
  191. package/vendor/@anvil/common/src/testing/mocks/database.ts +0 -337
  192. package/vendor/@anvil/common/src/testing/mocks/fetch.ts +0 -219
  193. package/vendor/@anvil/common/src/testing/mocks/filesystem.ts +0 -166
  194. package/vendor/@anvil/common/src/testing/mocks/index.ts +0 -101
  195. package/vendor/@anvil/common/src/testing/mocks/logger.ts +0 -135
  196. package/vendor/@anvil/common/src/testing/mocks/stream.ts +0 -313
  197. package/vendor/@anvil/common/src/testing/mocks/timers.ts +0 -132
  198. package/vendor/@anvil/common/src/testing/mocks/tree-sitter.ts +0 -127
  199. package/vendor/@anvil/common/src/testing/setup.ts +0 -282
  200. package/vendor/@anvil/common/src/tools/__tests__/compile-tool-definitions.test.ts +0 -34
  201. package/vendor/@anvil/common/src/tools/params/__tests__/coerce-to-array.test.ts +0 -213
  202. package/vendor/@anvil/common/src/tools/params/__tests__/read-docs.test.ts +0 -47
  203. package/vendor/@anvil/common/src/tools/params/__tests__/render-ui.test.ts +0 -65
  204. package/vendor/@anvil/common/src/tools/params/tool/__tests__/run-terminal-command-timeout.test.ts +0 -49
  205. package/vendor/@anvil/common/src/types/__tests__/dynamic-agent-template.test.ts +0 -20
  206. package/vendor/@anvil/common/src/util/__tests__/ad-user-agent.test.ts +0 -26
  207. package/vendor/@anvil/common/src/util/__tests__/analytics-dispatcher.test.ts +0 -122
  208. package/vendor/@anvil/common/src/util/__tests__/analytics-log.test.ts +0 -102
  209. package/vendor/@anvil/common/src/util/__tests__/analytics-sampling.test.ts +0 -143
  210. package/vendor/@anvil/common/src/util/__tests__/anvil-streak.test.ts +0 -202
  211. package/vendor/@anvil/common/src/util/__tests__/anvil-usage-summary.test.ts +0 -193
  212. package/vendor/@anvil/common/src/util/__tests__/axiom-only-log.test.ts +0 -140
  213. package/vendor/@anvil/common/src/util/__tests__/client-user-agent.test.ts +0 -148
  214. package/vendor/@anvil/common/src/util/__tests__/engagement-tracker.test.ts +0 -115
  215. package/vendor/@anvil/common/src/util/__tests__/env-file-path.test.ts +0 -57
  216. package/vendor/@anvil/common/src/util/__tests__/error-abort.test.ts +0 -774
  217. package/vendor/@anvil/common/src/util/__tests__/error-api-details.test.ts +0 -206
  218. package/vendor/@anvil/common/src/util/__tests__/file-read-limits.test.ts +0 -173
  219. package/vendor/@anvil/common/src/util/__tests__/format-code-search.test.ts +0 -60
  220. package/vendor/@anvil/common/src/util/__tests__/log-mirror.test.ts +0 -31
  221. package/vendor/@anvil/common/src/util/__tests__/messages.test.ts +0 -1252
  222. package/vendor/@anvil/common/src/util/__tests__/partial-json-delta.test.ts +0 -505
  223. package/vendor/@anvil/common/src/util/__tests__/path.test.ts +0 -22
  224. package/vendor/@anvil/common/src/util/__tests__/project-ignore.test.ts +0 -90
  225. package/vendor/@anvil/common/src/util/__tests__/promise.test.ts +0 -325
  226. package/vendor/@anvil/common/src/util/__tests__/rate-limit.test.ts +0 -28
  227. package/vendor/@anvil/common/src/util/__tests__/reddit-anvil-retention.test.ts +0 -130
  228. package/vendor/@anvil/common/src/util/__tests__/saxy.test.ts +0 -1008
  229. package/vendor/@anvil/common/src/util/__tests__/split-data.test.ts +0 -289
  230. package/vendor/@anvil/common/src/util/__tests__/string.test.ts +0 -239
  231. package/vendor/@anvil/common/src/util/__tests__/thread-title.test.ts +0 -70
  232. package/vendor/@anvil/common/src/util/__tests__/tool-result-media-order.test.ts +0 -108
  233. package/vendor/@anvil/common/src/util/__tests__/ttft-histogram.test.ts +0 -180
  234. package/vendor/@anvil/common/src/util/__tests__/with-timeout.test.ts +0 -24
  235. package/vendor/@anvil/common/src/util/__tests__/zoned-time.test.ts +0 -88
  236. package/vendor/@anvil/common/src/util/skills.test.ts +0 -44
  237. package/vendor/@anvil/sdk/dist/index.cjs.map +0 -440
  238. package/vendor/@anvil/sdk/dist/index.mjs.map +0 -440
@@ -1,1263 +0,0 @@
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
- })