@alumbwe/anvil 1.0.41 → 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 (241) 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/entry.ts +5 -0
  5. package/src/index.tsx +27 -4
  6. package/src/login/plain-login.ts +60 -0
  7. package/src/login/supabase-auth.ts +346 -0
  8. package/src/pre-init/client-env.ts +17 -0
  9. package/src/utils/anvil-client.ts +4 -0
  10. package/src/utils/auth.ts +16 -4
  11. package/src/utils/client-env-defaults.ts +28 -0
  12. package/src/utils/config-dir.ts +1 -1
  13. package/vendor/@anvil/common/src/constants/deepseek-direct.ts +30 -1
  14. package/vendor/@anvil/common/src/util/project-ignore.ts +4 -3
  15. package/vendor/@anvil/sdk/dist/index.cjs +46 -10
  16. package/vendor/@anvil/sdk/dist/index.mjs +46 -10
  17. package/src/commands/__tests__/anvil-command-aliases.test.ts +0 -56
  18. package/src/commands/__tests__/bash-command.test.ts +0 -445
  19. package/src/commands/__tests__/command-args.test.ts +0 -349
  20. package/src/commands/__tests__/copy-conversation-clipboard.test.ts +0 -108
  21. package/src/commands/__tests__/copy-conversation.test.ts +0 -226
  22. package/src/commands/__tests__/diagnostics-command.test.ts +0 -35
  23. package/src/commands/__tests__/image.test.ts +0 -99
  24. package/src/commands/__tests__/init.test.ts +0 -456
  25. package/src/commands/__tests__/new-command-rotates-chat.test.ts +0 -91
  26. package/src/commands/__tests__/process-diagnostics.test.ts +0 -54
  27. package/src/commands/__tests__/prompt-builders.test.ts +0 -28
  28. package/src/commands/__tests__/router-input.test.ts +0 -298
  29. package/src/components/__tests__/ad-banner.test.tsx +0 -83
  30. package/src/components/__tests__/anvil-landing-heading-row.test.tsx +0 -91
  31. package/src/components/__tests__/anvil-model-selector.test.tsx +0 -519
  32. package/src/components/__tests__/anvil-offer-invariants.test.ts +0 -54
  33. package/src/components/__tests__/anvil-takeover-prompt.test.tsx +0 -212
  34. package/src/components/__tests__/chat-input-bar.test.tsx +0 -130
  35. package/src/components/__tests__/grid-layout.integration.test.tsx +0 -304
  36. package/src/components/__tests__/grid-layout.test.tsx +0 -1051
  37. package/src/components/__tests__/message-block.completion.test.tsx +0 -104
  38. package/src/components/__tests__/message-block.streaming.test.tsx +0 -78
  39. package/src/components/__tests__/message-with-agents.test.tsx +0 -579
  40. package/src/components/__tests__/multiline-input.test.tsx +0 -1132
  41. package/src/components/__tests__/queue-panel.test.tsx +0 -398
  42. package/src/components/__tests__/selectable-list.test.ts +0 -232
  43. package/src/components/__tests__/status-bar.test.tsx +0 -44
  44. package/src/components/__tests__/status-indicator.test.tsx +0 -204
  45. package/src/components/__tests__/terminal-thinking-orb.test.tsx +0 -111
  46. package/src/components/__tests__/user-error-banner.test.tsx +0 -102
  47. package/src/components/ask-user/__tests__/multiple-choice-form.test.ts +0 -432
  48. package/src/components/ask-user/__tests__/validation.test.ts +0 -213
  49. package/src/components/tools/__tests__/apply-patch.test.tsx +0 -84
  50. package/src/components/tools/__tests__/code-search.test.tsx +0 -45
  51. package/src/components/tools/__tests__/gravity-index.test.ts +0 -76
  52. package/src/components/tools/__tests__/render-ui.test.tsx +0 -92
  53. package/src/components/tools/__tests__/run-terminal-command.test.ts +0 -233
  54. package/src/contexts/__tests__/chat-runtime-context.test.tsx +0 -149
  55. package/src/data/slash-commands.test.ts +0 -24
  56. package/src/hooks/__tests__/holds-live-anvil-slot.test.ts +0 -49
  57. package/src/hooks/__tests__/session-fetch-signal.test.ts +0 -63
  58. package/src/hooks/__tests__/use-activity-query.test.ts +0 -1000
  59. package/src/hooks/__tests__/use-ask-user-bridge.test.ts +0 -176
  60. package/src/hooks/__tests__/use-auth-query.test.ts +0 -21
  61. package/src/hooks/__tests__/use-chat-input-remount.test.tsx +0 -66
  62. package/src/hooks/__tests__/use-clipboard.test.tsx +0 -73
  63. package/src/hooks/__tests__/use-connection-status.test.ts +0 -112
  64. package/src/hooks/__tests__/use-directory-browser.test.ts +0 -203
  65. package/src/hooks/__tests__/use-gravity-ad.test.ts +0 -73
  66. package/src/hooks/__tests__/use-grid-layout.test.ts +0 -349
  67. package/src/hooks/__tests__/use-input-history.test.ts +0 -699
  68. package/src/hooks/__tests__/use-message-queue-editing.test.tsx +0 -209
  69. package/src/hooks/__tests__/use-message-queue.test.tsx +0 -71
  70. package/src/hooks/__tests__/use-path-tab-completion.test.ts +0 -315
  71. package/src/hooks/__tests__/use-queue-controls.test.ts +0 -68
  72. package/src/hooks/__tests__/use-queue-ui.test.tsx +0 -83
  73. package/src/hooks/__tests__/use-searchable-list.test.ts +0 -359
  74. package/src/hooks/__tests__/use-send-message-timer.test.ts +0 -146
  75. package/src/hooks/__tests__/use-suggestion-engine-mention.test.ts +0 -361
  76. package/src/hooks/__tests__/use-suggestion-engine.test.ts +0 -485
  77. package/src/hooks/__tests__/use-terminal-focus.test.ts +0 -66
  78. package/src/hooks/__tests__/use-terminal-layout.test.ts +0 -675
  79. package/src/hooks/__tests__/use-timeout.test.ts +0 -330
  80. package/src/hooks/__tests__/use-usage-query.test.ts +0 -502
  81. package/src/hooks/__tests__/use-user-details-query.test.ts +0 -195
  82. package/src/hooks/helpers/__tests__/send-message.test.ts +0 -1918
  83. package/src/init/__tests__/init-direnv.test.ts +0 -526
  84. package/src/state/__tests__/chat-store-focus.test.ts +0 -22
  85. package/src/state/__tests__/feedback-store.test.ts +0 -257
  86. package/src/utils/__tests__/active-run.test.ts +0 -140
  87. package/src/utils/__tests__/activity-tracker.test.ts +0 -440
  88. package/src/utils/__tests__/agent-display.test.ts +0 -139
  89. package/src/utils/__tests__/analytics-client.test.ts +0 -262
  90. package/src/utils/__tests__/anonymous-id.test.ts +0 -74
  91. package/src/utils/__tests__/anvil-api.test.ts +0 -578
  92. package/src/utils/__tests__/anvil-instance-owner.test.ts +0 -69
  93. package/src/utils/__tests__/anvil-model-navigation.test.ts +0 -102
  94. package/src/utils/__tests__/anvil-premium-reset.test.ts +0 -79
  95. package/src/utils/__tests__/anvil-referral-cache.test.ts +0 -116
  96. package/src/utils/__tests__/anvil-session-api.test.ts +0 -215
  97. package/src/utils/__tests__/anvil-session-display.test.ts +0 -23
  98. package/src/utils/__tests__/anvil-streak-line.test.ts +0 -217
  99. package/src/utils/__tests__/arrays.test.ts +0 -264
  100. package/src/utils/__tests__/bash-context-processor.test.ts +0 -38
  101. package/src/utils/__tests__/block-processor.test.ts +0 -810
  102. package/src/utils/__tests__/chat-history.test.ts +0 -203
  103. package/src/utils/__tests__/chat-input-key-intercept.test.ts +0 -40
  104. package/src/utils/__tests__/chat-meta.test.ts +0 -130
  105. package/src/utils/__tests__/clipboard-linux.test.ts +0 -359
  106. package/src/utils/__tests__/clipboard.test.ts +0 -910
  107. package/src/utils/__tests__/code-search-summary.test.ts +0 -84
  108. package/src/utils/__tests__/collapse-helpers.test.ts +0 -1109
  109. package/src/utils/__tests__/error-handling.test.ts +0 -658
  110. package/src/utils/__tests__/exit-cleanly.test.ts +0 -110
  111. package/src/utils/__tests__/feedback-helpers.test.ts +0 -444
  112. package/src/utils/__tests__/feedback-submission.test.ts +0 -26
  113. package/src/utils/__tests__/fetch-usage.test.ts +0 -355
  114. package/src/utils/__tests__/fingerprint.test.ts +0 -144
  115. package/src/utils/__tests__/fixtures/terminal-command-broker-child.ts +0 -7
  116. package/src/utils/__tests__/fixtures/terminal-command-broker-entry.ts +0 -3
  117. package/src/utils/__tests__/fixtures/terminal-command-broker-owner.ts +0 -33
  118. package/src/utils/__tests__/format-elapsed-time.test.ts +0 -103
  119. package/src/utils/__tests__/format-timeout.test.ts +0 -87
  120. package/src/utils/__tests__/image-dimensions.test.ts +0 -249
  121. package/src/utils/__tests__/image-processor.test.ts +0 -209
  122. package/src/utils/__tests__/implementor-helpers.test.ts +0 -1458
  123. package/src/utils/__tests__/keyboard-actions.test.ts +0 -660
  124. package/src/utils/__tests__/layout-helpers.test.ts +0 -36
  125. package/src/utils/__tests__/lazy-response-ads.test.ts +0 -167
  126. package/src/utils/__tests__/log-shipper.test.ts +0 -53
  127. package/src/utils/__tests__/markdown-renderer.test.tsx +0 -421
  128. package/src/utils/__tests__/message-block-helpers.test.ts +0 -1263
  129. package/src/utils/__tests__/message-updater.test.ts +0 -645
  130. package/src/utils/__tests__/osc-timeout-scenarios.test.ts +0 -172
  131. package/src/utils/__tests__/pending-attachments.test.ts +0 -277
  132. package/src/utils/__tests__/polling-backoff.test.ts +0 -51
  133. package/src/utils/__tests__/queue-panel-actions.test.ts +0 -148
  134. package/src/utils/__tests__/run-state-storage.test.ts +0 -919
  135. package/src/utils/__tests__/safe-json.test.ts +0 -77
  136. package/src/utils/__tests__/sdk-event-handlers.test.ts +0 -685
  137. package/src/utils/__tests__/send-message-helpers.test.ts +0 -1898
  138. package/src/utils/__tests__/send-message-timer.test.ts +0 -64
  139. package/src/utils/__tests__/settings.test.ts +0 -99
  140. package/src/utils/__tests__/strings.test.ts +0 -192
  141. package/src/utils/__tests__/terminal-color-detection.test.ts +0 -425
  142. package/src/utils/__tests__/terminal-command-broker.test.ts +0 -528
  143. package/src/utils/__tests__/terminal-enter-detection.test.ts +0 -77
  144. package/src/utils/__tests__/terminal-io.test.ts +0 -137
  145. package/src/utils/__tests__/terminal-protocol-controller.test.ts +0 -119
  146. package/src/utils/__tests__/text-layout.test.ts +0 -82
  147. package/src/utils/__tests__/theme-platform-detection.test.ts +0 -125
  148. package/src/utils/__tests__/think-tag-parser.test.ts +0 -159
  149. package/src/utils/__tests__/trace-writer.test.ts +0 -223
  150. package/src/utils/__tests__/trim-chat-logs.test.ts +0 -78
  151. package/src/utils/__tests__/usage-banner-state.test.ts +0 -298
  152. package/src/utils/__tests__/validation-error-formatting.test.ts +0 -126
  153. package/src/utils/__tests__/windows-terminal-health.test.ts +0 -101
  154. package/src/utils/__tests__/write-file-atomic.test.ts +0 -127
  155. package/vendor/@anvil/common/src/__tests__/agent-validation.test.ts +0 -846
  156. package/vendor/@anvil/common/src/__tests__/anvil-model-availability.test.ts +0 -101
  157. package/vendor/@anvil/common/src/__tests__/anvil-models.test.ts +0 -1408
  158. package/vendor/@anvil/common/src/__tests__/anvil-public-data-use-copy.test.ts +0 -258
  159. package/vendor/@anvil/common/src/__tests__/anvil-referral-tiers.test.ts +0 -48
  160. package/vendor/@anvil/common/src/__tests__/anvil-spend-ceilings.test.ts +0 -282
  161. package/vendor/@anvil/common/src/__tests__/anvil-trust.test.ts +0 -526
  162. package/vendor/@anvil/common/src/__tests__/deepseek-direct.test.ts +0 -68
  163. package/vendor/@anvil/common/src/__tests__/disposable-email.test.ts +0 -132
  164. package/vendor/@anvil/common/src/__tests__/dynamic-agent-template-schema.test.ts +0 -422
  165. package/vendor/@anvil/common/src/__tests__/env-ci.test.ts +0 -167
  166. package/vendor/@anvil/common/src/__tests__/env-process.test.ts +0 -145
  167. package/vendor/@anvil/common/src/__tests__/foreign-client-shipped-agents.test.ts +0 -234
  168. package/vendor/@anvil/common/src/__tests__/foreign-client-signals.test.ts +0 -379
  169. package/vendor/@anvil/common/src/__tests__/free-agents.test.ts +0 -807
  170. package/vendor/@anvil/common/src/__tests__/gravity-capi.test.ts +0 -154
  171. package/vendor/@anvil/common/src/__tests__/handlesteps-parsing.test.ts +0 -246
  172. package/vendor/@anvil/common/src/__tests__/kimi-k3-god-only.test.ts +0 -77
  173. package/vendor/@anvil/common/src/__tests__/model-config.test.ts +0 -89
  174. package/vendor/@anvil/common/src/__tests__/project-file-tree.test.ts +0 -298
  175. package/vendor/@anvil/common/src/__tests__/provisioned-model-tiers.test.ts +0 -116
  176. package/vendor/@anvil/common/src/__tests__/reasoning-effort.test.ts +0 -167
  177. package/vendor/@anvil/common/src/__tests__/reddit-capi.test.ts +0 -132
  178. package/vendor/@anvil/common/src/__tests__/response-ad-positions.test.ts +0 -72
  179. package/vendor/@anvil/common/src/__tests__/user-state.test.ts +0 -30
  180. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding-gate.test.ts +0 -63
  181. package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding.test.ts +0 -246
  182. package/vendor/@anvil/common/src/constants/__tests__/cf-worker-signals.test.ts +0 -106
  183. package/vendor/@anvil/common/src/testing/TESTING_PATTERNS.md +0 -351
  184. package/vendor/@anvil/common/src/testing/anvil-offer-invariants.ts +0 -169
  185. package/vendor/@anvil/common/src/testing/errors.ts +0 -33
  186. package/vendor/@anvil/common/src/testing/fixtures/agent-runtime.ts +0 -334
  187. package/vendor/@anvil/common/src/testing/impl/agent-runtime.ts +0 -6
  188. package/vendor/@anvil/common/src/testing/index.ts +0 -84
  189. package/vendor/@anvil/common/src/testing/mock-modules.ts +0 -53
  190. package/vendor/@anvil/common/src/testing/mock-types.ts +0 -123
  191. package/vendor/@anvil/common/src/testing/mocks/analytics.ts +0 -261
  192. package/vendor/@anvil/common/src/testing/mocks/child-process.ts +0 -93
  193. package/vendor/@anvil/common/src/testing/mocks/crypto.ts +0 -218
  194. package/vendor/@anvil/common/src/testing/mocks/database.ts +0 -337
  195. package/vendor/@anvil/common/src/testing/mocks/fetch.ts +0 -219
  196. package/vendor/@anvil/common/src/testing/mocks/filesystem.ts +0 -166
  197. package/vendor/@anvil/common/src/testing/mocks/index.ts +0 -101
  198. package/vendor/@anvil/common/src/testing/mocks/logger.ts +0 -135
  199. package/vendor/@anvil/common/src/testing/mocks/stream.ts +0 -313
  200. package/vendor/@anvil/common/src/testing/mocks/timers.ts +0 -132
  201. package/vendor/@anvil/common/src/testing/mocks/tree-sitter.ts +0 -127
  202. package/vendor/@anvil/common/src/testing/setup.ts +0 -282
  203. package/vendor/@anvil/common/src/tools/__tests__/compile-tool-definitions.test.ts +0 -34
  204. package/vendor/@anvil/common/src/tools/params/__tests__/coerce-to-array.test.ts +0 -213
  205. package/vendor/@anvil/common/src/tools/params/__tests__/read-docs.test.ts +0 -47
  206. package/vendor/@anvil/common/src/tools/params/__tests__/render-ui.test.ts +0 -65
  207. package/vendor/@anvil/common/src/tools/params/tool/__tests__/run-terminal-command-timeout.test.ts +0 -49
  208. package/vendor/@anvil/common/src/types/__tests__/dynamic-agent-template.test.ts +0 -20
  209. package/vendor/@anvil/common/src/util/__tests__/ad-user-agent.test.ts +0 -26
  210. package/vendor/@anvil/common/src/util/__tests__/analytics-dispatcher.test.ts +0 -122
  211. package/vendor/@anvil/common/src/util/__tests__/analytics-log.test.ts +0 -102
  212. package/vendor/@anvil/common/src/util/__tests__/analytics-sampling.test.ts +0 -143
  213. package/vendor/@anvil/common/src/util/__tests__/anvil-streak.test.ts +0 -202
  214. package/vendor/@anvil/common/src/util/__tests__/anvil-usage-summary.test.ts +0 -193
  215. package/vendor/@anvil/common/src/util/__tests__/axiom-only-log.test.ts +0 -140
  216. package/vendor/@anvil/common/src/util/__tests__/client-user-agent.test.ts +0 -148
  217. package/vendor/@anvil/common/src/util/__tests__/engagement-tracker.test.ts +0 -115
  218. package/vendor/@anvil/common/src/util/__tests__/env-file-path.test.ts +0 -57
  219. package/vendor/@anvil/common/src/util/__tests__/error-abort.test.ts +0 -774
  220. package/vendor/@anvil/common/src/util/__tests__/error-api-details.test.ts +0 -206
  221. package/vendor/@anvil/common/src/util/__tests__/file-read-limits.test.ts +0 -173
  222. package/vendor/@anvil/common/src/util/__tests__/format-code-search.test.ts +0 -60
  223. package/vendor/@anvil/common/src/util/__tests__/log-mirror.test.ts +0 -31
  224. package/vendor/@anvil/common/src/util/__tests__/messages.test.ts +0 -1252
  225. package/vendor/@anvil/common/src/util/__tests__/partial-json-delta.test.ts +0 -505
  226. package/vendor/@anvil/common/src/util/__tests__/path.test.ts +0 -22
  227. package/vendor/@anvil/common/src/util/__tests__/project-ignore.test.ts +0 -90
  228. package/vendor/@anvil/common/src/util/__tests__/promise.test.ts +0 -325
  229. package/vendor/@anvil/common/src/util/__tests__/rate-limit.test.ts +0 -28
  230. package/vendor/@anvil/common/src/util/__tests__/reddit-anvil-retention.test.ts +0 -130
  231. package/vendor/@anvil/common/src/util/__tests__/saxy.test.ts +0 -1008
  232. package/vendor/@anvil/common/src/util/__tests__/split-data.test.ts +0 -289
  233. package/vendor/@anvil/common/src/util/__tests__/string.test.ts +0 -239
  234. package/vendor/@anvil/common/src/util/__tests__/thread-title.test.ts +0 -70
  235. package/vendor/@anvil/common/src/util/__tests__/tool-result-media-order.test.ts +0 -108
  236. package/vendor/@anvil/common/src/util/__tests__/ttft-histogram.test.ts +0 -180
  237. package/vendor/@anvil/common/src/util/__tests__/with-timeout.test.ts +0 -24
  238. package/vendor/@anvil/common/src/util/__tests__/zoned-time.test.ts +0 -88
  239. package/vendor/@anvil/common/src/util/skills.test.ts +0 -44
  240. package/vendor/@anvil/sdk/dist/index.cjs.map +0 -440
  241. package/vendor/@anvil/sdk/dist/index.mjs.map +0 -440
@@ -1,1898 +0,0 @@
1
- import { describe, test, expect } from 'bun:test'
2
-
3
- import {
4
- appendTextToRootStream,
5
- appendTextToAgentBlock,
6
- appendToolToAgentBlock,
7
- isNativeReasoningBlock,
8
- closeNativeReasoningBlock,
9
- closeNativeReasoningInAgent,
10
- markAgentComplete,
11
- markRunningAgentsAsCancelled,
12
- } from '../block-operations'
13
- import {
14
- updateBlocksRecursively,
15
- scrubPlanTags,
16
- scrubPlanTagsInBlocks,
17
- extractPlanFromBuffer,
18
- createAgentBlock,
19
- getAgentBaseName,
20
- autoCollapseBlocks,
21
- updateToolBlockWithOutput,
22
- transformAskUserBlocks,
23
- appendInterruptionNotice,
24
- extractSpawnAgentResultContent,
25
- } from '../message-block-helpers'
26
- import {
27
- createModeDividerMessage,
28
- createAiMessageShell,
29
- createErrorMessage,
30
- generateAiMessageId,
31
- autoCollapsePreviousMessages,
32
- createSpawnAgentBlocks,
33
- isSpawnAgentsResult,
34
- markMessageComplete,
35
- sanitizeRestoredMessages,
36
- setMessageError,
37
- } from '../send-message-helpers'
38
-
39
- import type {
40
- ContentBlock,
41
- AgentContentBlock,
42
- AskUserContentBlock,
43
- ChatMessage,
44
- ModeDividerContentBlock,
45
- TextContentBlock,
46
- ToolContentBlock,
47
- } from '../../types/chat'
48
-
49
- // ============================================================================
50
- // Block Manipulation Helpers Tests (from message-block-helpers)
51
- // ============================================================================
52
-
53
- describe('updateBlocksRecursively', () => {
54
- test('updates a top-level agent block', () => {
55
- const blocks: ContentBlock[] = [
56
- {
57
- type: 'agent',
58
- agentId: 'agent-1',
59
- agentName: 'Test',
60
- agentType: 'test',
61
- content: '',
62
- status: 'running',
63
- },
64
- ]
65
-
66
- const result = updateBlocksRecursively(blocks, 'agent-1', (block) => ({
67
- ...block,
68
- status: 'complete' as const,
69
- }))
70
-
71
- expect(result[0].type).toBe('agent')
72
- expect((result[0] as AgentContentBlock).status).toBe('complete')
73
- })
74
-
75
- test('updates a nested agent block', () => {
76
- const blocks: ContentBlock[] = [
77
- {
78
- type: 'agent',
79
- agentId: 'parent',
80
- agentName: 'Parent',
81
- agentType: 'parent',
82
- content: '',
83
- status: 'running',
84
- blocks: [
85
- {
86
- type: 'agent',
87
- agentId: 'child',
88
- agentName: 'Child',
89
- agentType: 'child',
90
- content: '',
91
- status: 'running',
92
- },
93
- ],
94
- },
95
- ]
96
-
97
- const result = updateBlocksRecursively(blocks, 'child', (block) => ({
98
- ...block,
99
- status: 'complete' as const,
100
- }))
101
-
102
- const parent = result[0] as AgentContentBlock
103
- const child = parent.blocks![0] as AgentContentBlock
104
- expect(child.status).toBe('complete')
105
- })
106
-
107
- test('returns original array if no match found', () => {
108
- const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
109
-
110
- const result = updateBlocksRecursively(blocks, 'nonexistent', (block) => ({
111
- ...block,
112
- }))
113
-
114
- expect(result).toBe(blocks) // Same reference
115
- })
116
-
117
- test('does not create new blocks for unchanged nested structures', () => {
118
- const blocks: ContentBlock[] = [
119
- {
120
- type: 'agent',
121
- agentId: 'agent-1',
122
- agentName: 'Test',
123
- agentType: 'test',
124
- content: '',
125
- status: 'running',
126
- blocks: [{ type: 'text', content: 'Nested text' }],
127
- },
128
- ]
129
-
130
- const result = updateBlocksRecursively(blocks, 'nonexistent', (block) => ({
131
- ...block,
132
- }))
133
-
134
- expect(result).toBe(blocks)
135
- })
136
- })
137
-
138
- describe('scrubPlanTags', () => {
139
- test('removes complete PLAN tags', () => {
140
- const input = 'Before <PLAN>plan content</cb_plan> After'
141
- expect(scrubPlanTags(input)).toBe('Before After')
142
- })
143
-
144
- test('removes incomplete trailing PLAN tags', () => {
145
- const input = 'Content <PLAN>incomplete plan'
146
- expect(scrubPlanTags(input)).toBe('Content ')
147
- })
148
-
149
- test('handles string with no PLAN tags', () => {
150
- const input = 'Just regular content'
151
- expect(scrubPlanTags(input)).toBe('Just regular content')
152
- })
153
-
154
- test('handles empty string', () => {
155
- expect(scrubPlanTags('')).toBe('')
156
- })
157
- })
158
-
159
- describe('scrubPlanTagsInBlocks', () => {
160
- test('removes plan tags from text blocks', () => {
161
- const blocks: ContentBlock[] = [
162
- { type: 'text', content: 'Hello <PLAN>plan</cb_plan> World' },
163
- ]
164
-
165
- const result = scrubPlanTagsInBlocks(blocks)
166
- expect((result[0] as TextContentBlock).content).toBe('Hello World')
167
- })
168
-
169
- test('filters out empty text blocks after scrubbing', () => {
170
- const blocks: ContentBlock[] = [
171
- { type: 'text', content: '<PLAN>only plan</cb_plan>' },
172
- { type: 'text', content: 'Keep this' },
173
- ]
174
-
175
- const result = scrubPlanTagsInBlocks(blocks)
176
- expect(result).toHaveLength(1)
177
- expect((result[0] as TextContentBlock).content).toBe('Keep this')
178
- })
179
-
180
- test('preserves non-text blocks', () => {
181
- const blocks: ContentBlock[] = [
182
- {
183
- type: 'agent',
184
- agentId: '1',
185
- agentName: 'Test',
186
- agentType: 'test',
187
- content: '',
188
- status: 'running',
189
- },
190
- ]
191
-
192
- const result = scrubPlanTagsInBlocks(blocks)
193
- expect(result).toEqual(blocks)
194
- })
195
- })
196
-
197
- // ============================================================================
198
- // Message Creation Helpers Tests (from send-message-helpers)
199
- // ============================================================================
200
-
201
- describe('createModeDividerMessage', () => {
202
- test('creates a mode divider message', () => {
203
- const message = createModeDividerMessage('MAX')
204
-
205
- expect(message.variant).toBe('ai')
206
- expect(message.content).toBe('')
207
- expect(message.blocks).toHaveLength(1)
208
- expect(message.blocks![0].type).toBe('mode-divider')
209
- expect((message.blocks![0] as ModeDividerContentBlock).mode).toBe('MAX')
210
- expect(message.id).toMatch(/^divider-/)
211
- })
212
- })
213
-
214
- describe('createAiMessageShell', () => {
215
- test('creates an empty AI message shell', () => {
216
- const message = createAiMessageShell('ai-123')
217
-
218
- expect(message.id).toBe('ai-123')
219
- expect(message.variant).toBe('ai')
220
- expect(message.content).toBe('')
221
- expect(message.blocks).toEqual([])
222
- expect(message.metadata?.allowInlineAds).toBe(true)
223
- })
224
- })
225
-
226
- describe('createErrorMessage', () => {
227
- test('creates an error message', () => {
228
- const message = createErrorMessage('Something went wrong')
229
-
230
- expect(message.variant).toBe('error')
231
- expect(message.content).toBe('Something went wrong')
232
- expect(message.id).toMatch(/^error-/)
233
- })
234
- })
235
-
236
- describe('generateAiMessageId', () => {
237
- test('generates unique IDs', () => {
238
- const id1 = generateAiMessageId()
239
- const id2 = generateAiMessageId()
240
-
241
- expect(id1).toMatch(/^ai-\d+-[a-f0-9]+$/)
242
- expect(id1).not.toBe(id2)
243
- })
244
- })
245
-
246
- // ============================================================================
247
- // Auto-Collapse Logic Tests
248
- // ============================================================================
249
-
250
- describe('autoCollapseBlocks', () => {
251
- test('collapses text blocks with thinkingId', () => {
252
- const blocks: ContentBlock[] = [
253
- { type: 'text', content: 'thinking', thinkingId: 'think-1' },
254
- ]
255
-
256
- const result = autoCollapseBlocks(blocks)
257
- expect((result[0] as TextContentBlock).thinkingCollapseState).toBe('hidden')
258
- })
259
-
260
- test('does not collapse user-opened blocks', () => {
261
- const blocks: ContentBlock[] = [
262
- {
263
- type: 'text',
264
- content: 'thinking',
265
- thinkingId: 'think-1',
266
- userOpened: true,
267
- },
268
- ]
269
-
270
- const result = autoCollapseBlocks(blocks)
271
- expect((result[0] as TextContentBlock).isCollapsed).toBeUndefined()
272
- })
273
-
274
- test('collapses agent blocks', () => {
275
- const blocks: ContentBlock[] = [
276
- {
277
- type: 'agent',
278
- agentId: '1',
279
- agentName: 'Test',
280
- agentType: 'test',
281
- content: '',
282
- status: 'running',
283
- },
284
- ]
285
-
286
- const result = autoCollapseBlocks(blocks)
287
- expect((result[0] as AgentContentBlock).isCollapsed).toBe(true)
288
- })
289
-
290
- test('collapses tool blocks', () => {
291
- const blocks: ContentBlock[] = [
292
- {
293
- type: 'tool',
294
- toolCallId: 'tool-1',
295
- toolName: 'read_files',
296
- input: {},
297
- },
298
- ]
299
-
300
- const result = autoCollapseBlocks(blocks)
301
- expect((result[0] as ToolContentBlock).isCollapsed).toBe(true)
302
- })
303
-
304
- test('recursively collapses nested agent blocks', () => {
305
- const blocks: ContentBlock[] = [
306
- {
307
- type: 'agent',
308
- agentId: 'parent',
309
- agentName: 'Parent',
310
- agentType: 'parent',
311
- content: '',
312
- status: 'running',
313
- blocks: [
314
- {
315
- type: 'agent',
316
- agentId: 'child',
317
- agentName: 'Child',
318
- agentType: 'child',
319
- content: '',
320
- status: 'running',
321
- },
322
- ],
323
- },
324
- ]
325
-
326
- const result = autoCollapseBlocks(blocks)
327
- const parent = result[0] as AgentContentBlock
328
- const child = parent.blocks![0] as AgentContentBlock
329
-
330
- expect(parent.isCollapsed).toBe(true)
331
- expect(child.isCollapsed).toBe(true)
332
- })
333
- })
334
-
335
- describe('autoCollapsePreviousMessages', () => {
336
- test('does not collapse the current AI message', () => {
337
- const messages: ChatMessage[] = [
338
- {
339
- id: 'ai-123',
340
- variant: 'ai',
341
- content: '',
342
- blocks: [
343
- {
344
- type: 'agent',
345
- agentId: '1',
346
- agentName: 'Test',
347
- agentType: 'test',
348
- content: '',
349
- status: 'running',
350
- },
351
- ],
352
- timestamp: '',
353
- },
354
- ]
355
-
356
- const result = autoCollapsePreviousMessages(messages, 'ai-123')
357
- expect(
358
- (result[0].blocks![0] as AgentContentBlock).isCollapsed,
359
- ).toBeUndefined()
360
- })
361
-
362
- test('collapses previous messages', () => {
363
- const messages: ChatMessage[] = [
364
- {
365
- id: 'ai-old',
366
- variant: 'ai',
367
- content: '',
368
- blocks: [
369
- {
370
- type: 'agent',
371
- agentId: '1',
372
- agentName: 'Test',
373
- agentType: 'test',
374
- content: '',
375
- status: 'running',
376
- },
377
- ],
378
- timestamp: '',
379
- },
380
- {
381
- id: 'ai-new',
382
- variant: 'ai',
383
- content: '',
384
- blocks: [],
385
- timestamp: '',
386
- },
387
- ]
388
-
389
- const result = autoCollapsePreviousMessages(messages, 'ai-new')
390
- expect((result[0].blocks![0] as AgentContentBlock).isCollapsed).toBe(true)
391
- })
392
-
393
- test('respects user-opened agent messages', () => {
394
- const messages: ChatMessage[] = [
395
- {
396
- id: 'agent-msg',
397
- variant: 'agent',
398
- content: '',
399
- timestamp: '',
400
- metadata: { userOpened: true },
401
- },
402
- ]
403
-
404
- const result = autoCollapsePreviousMessages(messages, 'ai-new')
405
- expect(result[0].metadata?.isCollapsed).toBeUndefined()
406
- })
407
- })
408
-
409
- // ============================================================================
410
- // Stream Chunk Processing Tests (from block-operations)
411
- // ============================================================================
412
-
413
- describe('appendTextToRootStream', () => {
414
- test('creates new text block for empty blocks array', () => {
415
- const result = appendTextToRootStream([], { type: 'text', text: 'Hello' })
416
-
417
- expect(result).toHaveLength(1)
418
- expect(result[0].type).toBe('text')
419
- expect((result[0] as TextContentBlock).content).toBe('Hello')
420
- })
421
-
422
- test('appends to existing text block of same type', () => {
423
- const blocks: ContentBlock[] = [
424
- { type: 'text', content: 'Hello', textType: 'text' },
425
- ]
426
-
427
- const result = appendTextToRootStream(blocks, {
428
- type: 'text',
429
- text: ' World',
430
- })
431
-
432
- expect(result).toHaveLength(1)
433
- expect((result[0] as TextContentBlock).content).toBe('Hello World')
434
- })
435
-
436
- test('creates new block for different text type', () => {
437
- const blocks: ContentBlock[] = [
438
- { type: 'text', content: 'Hello', textType: 'text' },
439
- ]
440
-
441
- const result = appendTextToRootStream(blocks, {
442
- type: 'reasoning',
443
- text: 'Thinking...',
444
- })
445
-
446
- expect(result).toHaveLength(2)
447
- expect((result[1] as TextContentBlock).textType).toBe('reasoning')
448
- expect((result[1] as TextContentBlock).thinkingCollapseState).toBe(
449
- 'preview',
450
- )
451
- })
452
-
453
- test('returns original blocks for empty text', () => {
454
- const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
455
-
456
- const result = appendTextToRootStream(blocks, { type: 'text', text: '' })
457
-
458
- expect(result).toBe(blocks)
459
- })
460
-
461
- // Think tag parsing tests
462
- test('handles unclosed think tag', () => {
463
- const result = appendTextToRootStream([], {
464
- type: 'text',
465
- text: 'Before <think>unclosed thoughts',
466
- })
467
-
468
- expect(result).toHaveLength(2)
469
- expect((result[0] as TextContentBlock).content).toBe('Before ')
470
- expect((result[1] as TextContentBlock).content).toBe('unclosed thoughts')
471
- expect((result[1] as TextContentBlock).textType).toBe('reasoning')
472
- expect((result[1] as TextContentBlock).thinkingOpen).toBe(true)
473
- })
474
-
475
- test('continues appending to open thinking block', () => {
476
- const blocks: ContentBlock[] = [
477
- {
478
- type: 'text',
479
- content: 'initial thoughts',
480
- textType: 'reasoning',
481
- isCollapsed: true,
482
- thinkingOpen: true,
483
- },
484
- ]
485
-
486
- const result = appendTextToRootStream(blocks, {
487
- type: 'text',
488
- text: ' more thoughts',
489
- })
490
-
491
- expect(result).toHaveLength(1)
492
- expect((result[0] as TextContentBlock).content).toBe(
493
- 'initial thoughts more thoughts',
494
- )
495
- expect((result[0] as TextContentBlock).textType).toBe('reasoning')
496
- })
497
-
498
- test('closes thinking block when close tag received', () => {
499
- const blocks: ContentBlock[] = [
500
- {
501
- type: 'text',
502
- content: 'initial thoughts',
503
- textType: 'reasoning',
504
- isCollapsed: true,
505
- thinkingOpen: true,
506
- },
507
- ]
508
-
509
- const result = appendTextToRootStream(blocks, {
510
- type: 'text',
511
- text: ' final</think> regular text',
512
- })
513
-
514
- expect(result).toHaveLength(2)
515
- expect((result[0] as TextContentBlock).content).toBe(
516
- 'initial thoughts final',
517
- )
518
- expect((result[0] as TextContentBlock).textType).toBe('reasoning')
519
- expect((result[0] as TextContentBlock).thinkingOpen).toBe(false)
520
- expect((result[1] as TextContentBlock).content).toBe(' regular text')
521
- expect((result[1] as TextContentBlock).textType).toBe('text')
522
- })
523
-
524
- test('text without think tags works normally', () => {
525
- const result = appendTextToRootStream([], {
526
- type: 'text',
527
- text: 'Just regular text without tags',
528
- })
529
-
530
- expect(result).toHaveLength(1)
531
- expect((result[0] as TextContentBlock).content).toBe(
532
- 'Just regular text without tags',
533
- )
534
- expect((result[0] as TextContentBlock).textType).toBe('text')
535
- })
536
-
537
- test('closes thinking block when receiving just </think> tag', () => {
538
- const blocks: ContentBlock[] = [
539
- {
540
- type: 'text',
541
- content: 'thoughts',
542
- textType: 'reasoning',
543
- isCollapsed: true,
544
- thinkingOpen: true,
545
- },
546
- ]
547
-
548
- const result = appendTextToRootStream(blocks, {
549
- type: 'text',
550
- text: '</think>',
551
- })
552
-
553
- expect(result).toHaveLength(1)
554
- expect((result[0] as TextContentBlock).content).toBe('thoughts')
555
- expect((result[0] as TextContentBlock).textType).toBe('reasoning')
556
- expect((result[0] as TextContentBlock).thinkingOpen).toBe(false)
557
- })
558
-
559
- test('closes thinking block and adds text after </think>', () => {
560
- const blocks: ContentBlock[] = [
561
- {
562
- type: 'text',
563
- content: 'thoughts',
564
- textType: 'reasoning',
565
- isCollapsed: true,
566
- thinkingOpen: true,
567
- },
568
- ]
569
-
570
- const result = appendTextToRootStream(blocks, {
571
- type: 'text',
572
- text: '</think>after',
573
- })
574
-
575
- expect(result).toHaveLength(2)
576
- expect((result[0] as TextContentBlock).content).toBe('thoughts')
577
- expect((result[0] as TextContentBlock).textType).toBe('reasoning')
578
- expect((result[0] as TextContentBlock).thinkingOpen).toBe(false)
579
- expect((result[1] as TextContentBlock).content).toBe('after')
580
- expect((result[1] as TextContentBlock).textType).toBe('text')
581
- })
582
-
583
- // Streaming simulation tests
584
- test('streaming: does not create duplicate block when closing existing thinking block', () => {
585
- // Simulate streaming: first chunk opens thinking, second chunk closes it
586
- // First chunk: '<think>My thoughts' creates open thinking block
587
- const afterFirstChunk = appendTextToRootStream([], {
588
- type: 'text',
589
- text: '<think>My thoughts',
590
- })
591
-
592
- expect(afterFirstChunk).toHaveLength(1)
593
- expect((afterFirstChunk[0] as TextContentBlock).textType).toBe('reasoning')
594
- expect((afterFirstChunk[0] as TextContentBlock).content).toBe('My thoughts')
595
- expect((afterFirstChunk[0] as TextContentBlock).thinkingOpen).toBe(true)
596
-
597
- // Second chunk: '</think> after' should close the block, not create a duplicate
598
- const afterSecondChunk = appendTextToRootStream(afterFirstChunk, {
599
- type: 'text',
600
- text: '</think> after',
601
- })
602
-
603
- expect(afterSecondChunk).toHaveLength(2)
604
- expect((afterSecondChunk[0] as TextContentBlock).textType).toBe('reasoning')
605
- expect((afterSecondChunk[0] as TextContentBlock).content).toBe(
606
- 'My thoughts',
607
- )
608
- expect((afterSecondChunk[0] as TextContentBlock).thinkingOpen).toBe(false)
609
- expect((afterSecondChunk[1] as TextContentBlock).textType).toBe('text')
610
- expect((afterSecondChunk[1] as TextContentBlock).content).toBe(' after')
611
- })
612
-
613
- // Native reasoning tests
614
- test('closes native reasoning block when text arrives', () => {
615
- // Native reasoning block (thinkingOpen === undefined)
616
- const blocks: ContentBlock[] = [
617
- {
618
- type: 'text',
619
- content: 'Thinking...',
620
- textType: 'reasoning',
621
- isCollapsed: true,
622
- thinkingId: 'think-1',
623
- // Note: thinkingOpen is undefined for native reasoning
624
- },
625
- ]
626
-
627
- const result = appendTextToRootStream(blocks, {
628
- type: 'text',
629
- text: 'Regular text',
630
- })
631
-
632
- expect(result).toHaveLength(2)
633
- // Native reasoning block should be closed
634
- expect((result[0] as TextContentBlock).thinkingOpen).toBe(false)
635
- // New text block added
636
- expect((result[1] as TextContentBlock).content).toBe('Regular text')
637
- expect((result[1] as TextContentBlock).textType).toBe('text')
638
- })
639
-
640
- test('appends to existing native reasoning block', () => {
641
- const blocks: ContentBlock[] = [
642
- {
643
- type: 'text',
644
- content: 'First thought',
645
- textType: 'reasoning',
646
- isCollapsed: true,
647
- thinkingId: 'think-1',
648
- // thinkingOpen is undefined for native reasoning
649
- },
650
- ]
651
-
652
- const result = appendTextToRootStream(blocks, {
653
- type: 'reasoning',
654
- text: ' second thought',
655
- })
656
-
657
- expect(result).toHaveLength(1)
658
- expect((result[0] as TextContentBlock).content).toBe(
659
- 'First thought second thought',
660
- )
661
- expect((result[0] as TextContentBlock).textType).toBe('reasoning')
662
- })
663
- })
664
-
665
- // ============================================================================
666
- // Native Reasoning Block Tests (from block-operations)
667
- // ============================================================================
668
-
669
- describe('isNativeReasoningBlock', () => {
670
- test('returns true for native reasoning block (thinkingOpen undefined)', () => {
671
- const block: ContentBlock = {
672
- type: 'text',
673
- content: 'Thinking...',
674
- textType: 'reasoning',
675
- isCollapsed: true,
676
- thinkingId: 'think-1',
677
- }
678
-
679
- expect(isNativeReasoningBlock(block)).toBe(true)
680
- })
681
-
682
- test('returns false for closed native reasoning block (thinkingOpen false)', () => {
683
- const block: ContentBlock = {
684
- type: 'text',
685
- content: 'Thinking...',
686
- textType: 'reasoning',
687
- isCollapsed: true,
688
- thinkingOpen: false,
689
- thinkingId: 'think-1',
690
- }
691
-
692
- expect(isNativeReasoningBlock(block)).toBe(false)
693
- })
694
-
695
- test('returns false for <think> tag block (thinkingOpen true)', () => {
696
- const block: ContentBlock = {
697
- type: 'text',
698
- content: 'Thinking...',
699
- textType: 'reasoning',
700
- isCollapsed: true,
701
- thinkingOpen: true,
702
- thinkingId: 'think-1',
703
- }
704
-
705
- expect(isNativeReasoningBlock(block)).toBe(false)
706
- })
707
-
708
- test('returns false for regular text block', () => {
709
- const block: ContentBlock = {
710
- type: 'text',
711
- content: 'Hello',
712
- textType: 'text',
713
- }
714
-
715
- expect(isNativeReasoningBlock(block)).toBe(false)
716
- })
717
-
718
- test('returns false for non-text blocks', () => {
719
- const agentBlock: ContentBlock = {
720
- type: 'agent',
721
- agentId: 'agent-1',
722
- agentName: 'Test',
723
- agentType: 'test',
724
- content: '',
725
- status: 'running',
726
- }
727
-
728
- expect(isNativeReasoningBlock(agentBlock)).toBe(false)
729
- })
730
-
731
- test('returns false for undefined', () => {
732
- expect(isNativeReasoningBlock(undefined)).toBe(false)
733
- })
734
- })
735
-
736
- describe('closeNativeReasoningBlock', () => {
737
- test('closes native reasoning block by setting thinkingOpen to false', () => {
738
- const blocks: ContentBlock[] = [
739
- {
740
- type: 'text',
741
- content: 'Thinking...',
742
- textType: 'reasoning',
743
- isCollapsed: true,
744
- thinkingId: 'think-1',
745
- },
746
- ]
747
-
748
- const result = closeNativeReasoningBlock(blocks)
749
-
750
- expect(result).toHaveLength(1)
751
- expect((result[0] as TextContentBlock).thinkingOpen).toBe(false)
752
- expect((result[0] as TextContentBlock).content).toBe('Thinking...')
753
- })
754
-
755
- test('returns original blocks if no native reasoning block exists', () => {
756
- const blocks: ContentBlock[] = [
757
- { type: 'text', content: 'Hello', textType: 'text' },
758
- ]
759
-
760
- const result = closeNativeReasoningBlock(blocks)
761
-
762
- expect(result).toBe(blocks) // Same reference
763
- })
764
-
765
- test('does not close already-closed reasoning blocks', () => {
766
- const blocks: ContentBlock[] = [
767
- {
768
- type: 'text',
769
- content: 'Already closed',
770
- textType: 'reasoning',
771
- isCollapsed: true,
772
- thinkingOpen: false,
773
- thinkingId: 'think-1',
774
- },
775
- ]
776
-
777
- const result = closeNativeReasoningBlock(blocks)
778
-
779
- expect(result).toBe(blocks) // Same reference, no change
780
- })
781
-
782
- test('does not close <think> tag blocks (thinkingOpen true)', () => {
783
- const blocks: ContentBlock[] = [
784
- {
785
- type: 'text',
786
- content: 'Think tag block',
787
- textType: 'reasoning',
788
- isCollapsed: true,
789
- thinkingOpen: true,
790
- thinkingId: 'think-1',
791
- },
792
- ]
793
-
794
- const result = closeNativeReasoningBlock(blocks)
795
-
796
- expect(result).toBe(blocks) // Same reference, no change
797
- })
798
-
799
- test('finds native reasoning block even when not at end', () => {
800
- const blocks: ContentBlock[] = [
801
- {
802
- type: 'text',
803
- content: 'Native reasoning',
804
- textType: 'reasoning',
805
- isCollapsed: true,
806
- thinkingId: 'think-1',
807
- },
808
- {
809
- type: 'agent',
810
- agentId: 'agent-1',
811
- agentName: 'Test',
812
- agentType: 'test',
813
- content: '',
814
- status: 'running',
815
- },
816
- ]
817
-
818
- const result = closeNativeReasoningBlock(blocks)
819
-
820
- expect((result[0] as TextContentBlock).thinkingOpen).toBe(false)
821
- expect(result[1]).toEqual(blocks[1]) // Agent block unchanged
822
- })
823
- })
824
-
825
- describe('closeNativeReasoningInAgent', () => {
826
- test('closes native reasoning in specific agent', () => {
827
- const blocks: ContentBlock[] = [
828
- {
829
- type: 'agent',
830
- agentId: 'agent-1',
831
- agentName: 'Test',
832
- agentType: 'test',
833
- content: '',
834
- status: 'running',
835
- blocks: [
836
- {
837
- type: 'text',
838
- content: 'Agent thinking...',
839
- textType: 'reasoning',
840
- isCollapsed: true,
841
- thinkingId: 'think-1',
842
- },
843
- ],
844
- },
845
- ]
846
-
847
- const result = closeNativeReasoningInAgent(blocks, 'agent-1')
848
-
849
- const agentBlock = result[0] as AgentContentBlock
850
- expect((agentBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
851
- })
852
-
853
- test('does not modify other agents', () => {
854
- const blocks: ContentBlock[] = [
855
- {
856
- type: 'agent',
857
- agentId: 'agent-1',
858
- agentName: 'Test 1',
859
- agentType: 'test',
860
- content: '',
861
- status: 'running',
862
- blocks: [
863
- {
864
- type: 'text',
865
- content: 'Agent 1 thinking...',
866
- textType: 'reasoning',
867
- isCollapsed: true,
868
- thinkingId: 'think-1',
869
- },
870
- ],
871
- },
872
- {
873
- type: 'agent',
874
- agentId: 'agent-2',
875
- agentName: 'Test 2',
876
- agentType: 'test',
877
- content: '',
878
- status: 'running',
879
- blocks: [
880
- {
881
- type: 'text',
882
- content: 'Agent 2 thinking...',
883
- textType: 'reasoning',
884
- isCollapsed: true,
885
- thinkingId: 'think-2',
886
- },
887
- ],
888
- },
889
- ]
890
-
891
- const result = closeNativeReasoningInAgent(blocks, 'agent-1')
892
-
893
- const agent1 = result[0] as AgentContentBlock
894
- const agent2 = result[1] as AgentContentBlock
895
- expect((agent1.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
896
- // Agent 2 should still have undefined thinkingOpen
897
- expect((agent2.blocks![0] as TextContentBlock).thinkingOpen).toBeUndefined()
898
- })
899
-
900
- test('returns original blocks if agent not found', () => {
901
- const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
902
-
903
- const result = closeNativeReasoningInAgent(blocks, 'nonexistent')
904
-
905
- expect(result).toBe(blocks)
906
- })
907
- })
908
-
909
- describe('appendTextToAgentBlock with native reasoning', () => {
910
- test('creates native reasoning block when textType is reasoning', () => {
911
- const blocks: ContentBlock[] = [
912
- {
913
- type: 'agent',
914
- agentId: 'agent-1',
915
- agentName: 'Test',
916
- agentType: 'test',
917
- content: '',
918
- status: 'running',
919
- blocks: [],
920
- },
921
- ]
922
-
923
- const result = appendTextToAgentBlock(
924
- blocks,
925
- 'agent-1',
926
- 'Thinking...',
927
- 'reasoning',
928
- )
929
-
930
- const agentBlock = result[0] as AgentContentBlock
931
- expect(agentBlock.blocks).toHaveLength(1)
932
- expect((agentBlock.blocks![0] as TextContentBlock).textType).toBe(
933
- 'reasoning',
934
- )
935
- expect((agentBlock.blocks![0] as TextContentBlock).content).toBe(
936
- 'Thinking...',
937
- )
938
- expect(
939
- (agentBlock.blocks![0] as TextContentBlock).thinkingCollapseState,
940
- ).toBe('preview')
941
- // Native reasoning has thinkingOpen undefined
942
- expect(
943
- (agentBlock.blocks![0] as TextContentBlock).thinkingOpen,
944
- ).toBeUndefined()
945
- })
946
-
947
- test('appends to existing open native reasoning block', () => {
948
- const blocks: ContentBlock[] = [
949
- {
950
- type: 'agent',
951
- agentId: 'agent-1',
952
- agentName: 'Test',
953
- agentType: 'test',
954
- content: 'First',
955
- status: 'running',
956
- blocks: [
957
- {
958
- type: 'text',
959
- content: 'First',
960
- textType: 'reasoning',
961
- isCollapsed: true,
962
- thinkingId: 'think-1',
963
- },
964
- ],
965
- },
966
- ]
967
-
968
- const result = appendTextToAgentBlock(
969
- blocks,
970
- 'agent-1',
971
- ' second',
972
- 'reasoning',
973
- )
974
-
975
- const agentBlock = result[0] as AgentContentBlock
976
- expect(agentBlock.blocks).toHaveLength(1)
977
- expect((agentBlock.blocks![0] as TextContentBlock).content).toBe(
978
- 'First second',
979
- )
980
- })
981
-
982
- test('does NOT append to closed native reasoning block', () => {
983
- const blocks: ContentBlock[] = [
984
- {
985
- type: 'agent',
986
- agentId: 'agent-1',
987
- agentName: 'Test',
988
- agentType: 'test',
989
- content: 'Closed',
990
- status: 'running',
991
- blocks: [
992
- {
993
- type: 'text',
994
- content: 'Closed',
995
- textType: 'reasoning',
996
- isCollapsed: true,
997
- thinkingOpen: false, // Already closed
998
- thinkingId: 'think-1',
999
- },
1000
- ],
1001
- },
1002
- ]
1003
-
1004
- const result = appendTextToAgentBlock(
1005
- blocks,
1006
- 'agent-1',
1007
- 'New thought',
1008
- 'reasoning',
1009
- )
1010
-
1011
- const agentBlock = result[0] as AgentContentBlock
1012
- // Should create a NEW reasoning block, not append to closed one
1013
- expect(agentBlock.blocks).toHaveLength(2)
1014
- expect((agentBlock.blocks![0] as TextContentBlock).content).toBe('Closed')
1015
- expect((agentBlock.blocks![1] as TextContentBlock).content).toBe(
1016
- 'New thought',
1017
- )
1018
- })
1019
-
1020
- test('does NOT append to <think> tag block', () => {
1021
- const blocks: ContentBlock[] = [
1022
- {
1023
- type: 'agent',
1024
- agentId: 'agent-1',
1025
- agentName: 'Test',
1026
- agentType: 'test',
1027
- content: 'Think tag',
1028
- status: 'running',
1029
- blocks: [
1030
- {
1031
- type: 'text',
1032
- content: 'Think tag',
1033
- textType: 'reasoning',
1034
- isCollapsed: true,
1035
- thinkingOpen: true, // <think> tag block
1036
- thinkingId: 'think-1',
1037
- },
1038
- ],
1039
- },
1040
- ]
1041
-
1042
- const result = appendTextToAgentBlock(
1043
- blocks,
1044
- 'agent-1',
1045
- 'Native thought',
1046
- 'reasoning',
1047
- )
1048
-
1049
- const agentBlock = result[0] as AgentContentBlock
1050
- // Should create a NEW native reasoning block, not append to <think> block
1051
- expect(agentBlock.blocks).toHaveLength(2)
1052
- expect((agentBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(true)
1053
- expect(
1054
- (agentBlock.blocks![1] as TextContentBlock).thinkingOpen,
1055
- ).toBeUndefined()
1056
- })
1057
-
1058
- test('closes native reasoning when regular text arrives', () => {
1059
- const blocks: ContentBlock[] = [
1060
- {
1061
- type: 'agent',
1062
- agentId: 'agent-1',
1063
- agentName: 'Test',
1064
- agentType: 'test',
1065
- content: 'Thinking',
1066
- status: 'running',
1067
- blocks: [
1068
- {
1069
- type: 'text',
1070
- content: 'Thinking',
1071
- textType: 'reasoning',
1072
- isCollapsed: true,
1073
- thinkingId: 'think-1',
1074
- },
1075
- ],
1076
- },
1077
- ]
1078
-
1079
- const result = appendTextToAgentBlock(
1080
- blocks,
1081
- 'agent-1',
1082
- 'Regular text',
1083
- 'text',
1084
- )
1085
-
1086
- const agentBlock = result[0] as AgentContentBlock
1087
- expect(agentBlock.blocks).toHaveLength(2)
1088
- // Native reasoning should be closed
1089
- expect((agentBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
1090
- // New text block added
1091
- expect((agentBlock.blocks![1] as TextContentBlock).content).toBe(
1092
- 'Regular text',
1093
- )
1094
- expect((agentBlock.blocks![1] as TextContentBlock).textType).toBe('text')
1095
- })
1096
- })
1097
-
1098
- describe('appendToolToAgentBlock closes native reasoning', () => {
1099
- test('closes native reasoning when tool is appended', () => {
1100
- const blocks: ContentBlock[] = [
1101
- {
1102
- type: 'agent',
1103
- agentId: 'agent-1',
1104
- agentName: 'Test',
1105
- agentType: 'test',
1106
- content: 'Thinking',
1107
- status: 'running',
1108
- blocks: [
1109
- {
1110
- type: 'text',
1111
- content: 'Thinking',
1112
- textType: 'reasoning',
1113
- isCollapsed: true,
1114
- thinkingId: 'think-1',
1115
- },
1116
- ],
1117
- },
1118
- ]
1119
-
1120
- const toolBlock: ToolContentBlock = {
1121
- type: 'tool',
1122
- toolCallId: 'tool-1',
1123
- toolName: 'read_files',
1124
- input: { paths: ['test.ts'] },
1125
- }
1126
-
1127
- const result = appendToolToAgentBlock(blocks, 'agent-1', toolBlock)
1128
-
1129
- const agentBlock = result[0] as AgentContentBlock
1130
- expect(agentBlock.blocks).toHaveLength(2)
1131
- // Native reasoning should be closed
1132
- expect((agentBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
1133
- // Tool block added
1134
- expect(agentBlock.blocks![1].type).toBe('tool')
1135
- })
1136
- })
1137
-
1138
- describe('markAgentComplete closes native reasoning', () => {
1139
- test('closes native reasoning when agent completes', () => {
1140
- const blocks: ContentBlock[] = [
1141
- {
1142
- type: 'agent',
1143
- agentId: 'agent-1',
1144
- agentName: 'Test',
1145
- agentType: 'test',
1146
- content: 'Thinking',
1147
- status: 'running',
1148
- blocks: [
1149
- {
1150
- type: 'text',
1151
- content: 'Thinking',
1152
- textType: 'reasoning',
1153
- isCollapsed: true,
1154
- thinkingId: 'think-1',
1155
- },
1156
- ],
1157
- },
1158
- ]
1159
-
1160
- const result = markAgentComplete(blocks, 'agent-1')
1161
-
1162
- const agentBlock = result[0] as AgentContentBlock
1163
- expect(agentBlock.status).toBe('complete')
1164
- expect((agentBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
1165
- })
1166
- })
1167
-
1168
- describe('markRunningAgentsAsCancelled closes native reasoning', () => {
1169
- test('closes native reasoning in cancelled agents', () => {
1170
- const blocks: ContentBlock[] = [
1171
- {
1172
- type: 'agent',
1173
- agentId: 'agent-1',
1174
- agentName: 'Test',
1175
- agentType: 'test',
1176
- content: 'Thinking',
1177
- status: 'running',
1178
- blocks: [
1179
- {
1180
- type: 'text',
1181
- content: 'Thinking',
1182
- textType: 'reasoning',
1183
- isCollapsed: true,
1184
- thinkingId: 'think-1',
1185
- },
1186
- ],
1187
- },
1188
- ]
1189
-
1190
- const result = markRunningAgentsAsCancelled(blocks)
1191
-
1192
- const agentBlock = result[0] as AgentContentBlock
1193
- expect(agentBlock.status).toBe('cancelled')
1194
- expect((agentBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
1195
- })
1196
-
1197
- test('closes native reasoning in nested cancelled agents', () => {
1198
- const blocks: ContentBlock[] = [
1199
- {
1200
- type: 'agent',
1201
- agentId: 'parent',
1202
- agentName: 'Parent',
1203
- agentType: 'parent',
1204
- content: '',
1205
- status: 'running',
1206
- blocks: [
1207
- {
1208
- type: 'agent',
1209
- agentId: 'child',
1210
- agentName: 'Child',
1211
- agentType: 'child',
1212
- content: 'Child thinking',
1213
- status: 'running',
1214
- blocks: [
1215
- {
1216
- type: 'text',
1217
- content: 'Child thinking',
1218
- textType: 'reasoning',
1219
- isCollapsed: true,
1220
- thinkingId: 'think-child',
1221
- },
1222
- ],
1223
- },
1224
- ],
1225
- },
1226
- ]
1227
-
1228
- const result = markRunningAgentsAsCancelled(blocks)
1229
-
1230
- const parentBlock = result[0] as AgentContentBlock
1231
- const childBlock = parentBlock.blocks![0] as AgentContentBlock
1232
-
1233
- expect(parentBlock.status).toBe('cancelled')
1234
- expect(childBlock.status).toBe('cancelled')
1235
- expect((childBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
1236
- })
1237
-
1238
- test('closes native reasoning even in non-running agents during cancellation', () => {
1239
- const blocks: ContentBlock[] = [
1240
- {
1241
- type: 'agent',
1242
- agentId: 'agent-1',
1243
- agentName: 'Test',
1244
- agentType: 'test',
1245
- content: '',
1246
- status: 'complete', // Already complete
1247
- blocks: [
1248
- {
1249
- type: 'agent',
1250
- agentId: 'child',
1251
- agentName: 'Child',
1252
- agentType: 'child',
1253
- content: 'Thinking',
1254
- status: 'running',
1255
- blocks: [
1256
- {
1257
- type: 'text',
1258
- content: 'Thinking',
1259
- textType: 'reasoning',
1260
- isCollapsed: true,
1261
- thinkingId: 'think-1',
1262
- },
1263
- ],
1264
- },
1265
- ],
1266
- },
1267
- ]
1268
-
1269
- const result = markRunningAgentsAsCancelled(blocks)
1270
-
1271
- const parentBlock = result[0] as AgentContentBlock
1272
- const childBlock = parentBlock.blocks![0] as AgentContentBlock
1273
-
1274
- // Parent stays complete
1275
- expect(parentBlock.status).toBe('complete')
1276
- // Child is cancelled
1277
- expect(childBlock.status).toBe('cancelled')
1278
- // Child's reasoning is closed
1279
- expect((childBlock.blocks![0] as TextContentBlock).thinkingOpen).toBe(false)
1280
- })
1281
-
1282
- test('does not modify agents without native reasoning blocks', () => {
1283
- const blocks: ContentBlock[] = [
1284
- {
1285
- type: 'agent',
1286
- agentId: 'agent-1',
1287
- agentName: 'Test',
1288
- agentType: 'test',
1289
- content: 'Hello',
1290
- status: 'running',
1291
- blocks: [{ type: 'text', content: 'Hello', textType: 'text' }],
1292
- },
1293
- ]
1294
-
1295
- const result = markRunningAgentsAsCancelled(blocks)
1296
-
1297
- const agentBlock = result[0] as AgentContentBlock
1298
- expect(agentBlock.status).toBe('cancelled')
1299
- // Text block should be unchanged
1300
- expect(
1301
- (agentBlock.blocks![0] as TextContentBlock).thinkingOpen,
1302
- ).toBeUndefined()
1303
- })
1304
- })
1305
-
1306
- describe('extractPlanFromBuffer', () => {
1307
- test('extracts plan content from complete tags', () => {
1308
- const buffer = 'Some text <PLAN>This is the plan</PLAN> more text'
1309
-
1310
- const result = extractPlanFromBuffer(buffer)
1311
-
1312
- expect(result).toBe('This is the plan')
1313
- })
1314
-
1315
- test('returns null for incomplete plan', () => {
1316
- const buffer = 'Some text <PLAN>Incomplete plan'
1317
-
1318
- expect(extractPlanFromBuffer(buffer)).toBeNull()
1319
- })
1320
-
1321
- test('returns null when no plan tags exist', () => {
1322
- expect(extractPlanFromBuffer('No plan here')).toBeNull()
1323
- })
1324
-
1325
- test('trims whitespace from extracted plan', () => {
1326
- const buffer = '<PLAN> Trimmed plan </PLAN>'
1327
-
1328
- expect(extractPlanFromBuffer(buffer)).toBe('Trimmed plan')
1329
- })
1330
- })
1331
-
1332
- // ============================================================================
1333
- // Agent Block Helpers Tests (from message-block-helpers)
1334
- // ============================================================================
1335
-
1336
- describe('createAgentBlock', () => {
1337
- test('creates an agent block with required fields', () => {
1338
- const block = createAgentBlock({
1339
- agentId: 'agent-1',
1340
- agentType: 'file-picker',
1341
- })
1342
-
1343
- expect(block.type).toBe('agent')
1344
- expect(block.agentId).toBe('agent-1')
1345
- expect(block.agentType).toBe('file-picker')
1346
- expect(block.status).toBe('running')
1347
- expect(block.content).toBe('')
1348
- })
1349
-
1350
- test('includes optional prompt', () => {
1351
- const block = createAgentBlock({
1352
- agentId: 'agent-1',
1353
- agentType: 'file-picker',
1354
- prompt: 'Find files',
1355
- })
1356
-
1357
- expect(block.initialPrompt).toBe('Find files')
1358
- })
1359
-
1360
- test('includes optional params', () => {
1361
- const block = createAgentBlock({
1362
- agentId: 'agent-1',
1363
- agentType: 'file-picker',
1364
- params: { path: '/src' },
1365
- })
1366
-
1367
- expect(block.params).toEqual({ path: '/src' })
1368
- })
1369
- })
1370
-
1371
- describe('getAgentBaseName', () => {
1372
- test('extracts base name from scoped versioned name', () => {
1373
- expect(getAgentBaseName('anvil/file-picker@0.0.2')).toBe('file-picker')
1374
- })
1375
-
1376
- test('extracts base name from simple versioned name', () => {
1377
- expect(getAgentBaseName('file-picker@1.0.0')).toBe('file-picker')
1378
- })
1379
-
1380
- test('returns simple name unchanged', () => {
1381
- expect(getAgentBaseName('file-picker')).toBe('file-picker')
1382
- })
1383
-
1384
- test('normalizes direct tool aliases to canonical agent names', () => {
1385
- expect(getAgentBaseName('code_reviewer_lite')).toBe('code-reviewer-lite')
1386
- })
1387
- })
1388
-
1389
- describe('agentTypesMatch', () => {
1390
- test('matches same base names with different versions', () => {
1391
- expect(
1392
- getAgentBaseName('anvil/file-picker@0.0.2') ===
1393
- getAgentBaseName('file-picker@1.0.0'),
1394
- ).toBe(true)
1395
- })
1396
-
1397
- test('matches same simple names', () => {
1398
- expect(
1399
- getAgentBaseName('file-picker') === getAgentBaseName('file-picker'),
1400
- ).toBe(true)
1401
- })
1402
-
1403
- test('does not match different base names', () => {
1404
- expect(
1405
- getAgentBaseName('file-picker') === getAgentBaseName('code-searcher'),
1406
- ).toBe(false)
1407
- })
1408
- })
1409
-
1410
- // ============================================================================
1411
- // Tool Block Helpers Tests (from message-block-helpers)
1412
- // ============================================================================
1413
-
1414
- describe('updateToolBlockWithOutput', () => {
1415
- test('updates tool block with output', () => {
1416
- const blocks: ContentBlock[] = [
1417
- {
1418
- type: 'tool',
1419
- toolCallId: 'tool-1',
1420
- toolName: 'read_files',
1421
- input: {},
1422
- },
1423
- ]
1424
-
1425
- const result = updateToolBlockWithOutput(blocks, {
1426
- toolCallId: 'tool-1',
1427
- toolOutput: ['File contents'],
1428
- })
1429
-
1430
- expect((result[0] as ToolContentBlock).output).toBe('File contents')
1431
- })
1432
-
1433
- test('updates nested tool block', () => {
1434
- const blocks: ContentBlock[] = [
1435
- {
1436
- type: 'agent',
1437
- agentId: 'agent-1',
1438
- agentName: 'Test',
1439
- agentType: 'test',
1440
- content: '',
1441
- status: 'running',
1442
- blocks: [
1443
- {
1444
- type: 'tool',
1445
- toolCallId: 'tool-1',
1446
- toolName: 'read_files',
1447
- input: {},
1448
- },
1449
- ],
1450
- },
1451
- ]
1452
-
1453
- const result = updateToolBlockWithOutput(blocks, {
1454
- toolCallId: 'tool-1',
1455
- toolOutput: ['File contents'],
1456
- })
1457
- const agent = result[0] as AgentContentBlock
1458
- expect((agent.blocks![0] as ToolContentBlock).output).toBe('File contents')
1459
- })
1460
-
1461
- test('returns same reference if no match', () => {
1462
- const blocks: ContentBlock[] = [{ type: 'text', content: 'Hello' }]
1463
-
1464
- const result = updateToolBlockWithOutput(blocks, {
1465
- toolCallId: 'tool-1',
1466
- toolOutput: ['Output'],
1467
- })
1468
-
1469
- expect(result).toEqual(blocks)
1470
- })
1471
- })
1472
-
1473
- // ============================================================================
1474
- // Ask User Transformation Tests (from message-block-helpers)
1475
- // ============================================================================
1476
-
1477
- describe('transformAskUserBlocks', () => {
1478
- test('transforms ask_user tool block to ask-user block', () => {
1479
- const blocks: ContentBlock[] = [
1480
- {
1481
- type: 'tool',
1482
- toolCallId: 'tool-1',
1483
- toolName: 'ask_user',
1484
- input: { questions: [{ question: 'Choose?', options: ['A', 'B'] }] },
1485
- },
1486
- ]
1487
-
1488
- const result = transformAskUserBlocks(blocks, {
1489
- toolCallId: 'tool-1',
1490
- resultValue: { answers: [{ questionIndex: 0, selectedOption: 'A' }] },
1491
- })
1492
-
1493
- expect(result[0].type).toBe('ask-user')
1494
- expect((result[0] as AskUserContentBlock).answers).toEqual([
1495
- { questionIndex: 0, selectedOption: 'A' },
1496
- ])
1497
- })
1498
-
1499
- test('keeps tool block if no answers or skipped', () => {
1500
- const blocks: ContentBlock[] = [
1501
- {
1502
- type: 'tool',
1503
- toolCallId: 'tool-1',
1504
- toolName: 'ask_user',
1505
- input: { questions: [] },
1506
- },
1507
- ]
1508
-
1509
- const result = transformAskUserBlocks(blocks, {
1510
- toolCallId: 'tool-1',
1511
- resultValue: {},
1512
- })
1513
-
1514
- expect(result[0].type).toBe('tool')
1515
- })
1516
-
1517
- test('handles skipped state', () => {
1518
- const blocks: ContentBlock[] = [
1519
- {
1520
- type: 'tool',
1521
- toolCallId: 'tool-1',
1522
- toolName: 'ask_user',
1523
- input: { questions: [] },
1524
- },
1525
- ]
1526
-
1527
- const result = transformAskUserBlocks(blocks, {
1528
- toolCallId: 'tool-1',
1529
- resultValue: { skipped: true },
1530
- })
1531
-
1532
- expect(result[0].type).toBe('ask-user')
1533
- expect((result[0] as AskUserContentBlock).skipped).toBe(true)
1534
- })
1535
- })
1536
-
1537
- // ============================================================================
1538
- // Interruption Handling Tests (from message-block-helpers)
1539
- // ============================================================================
1540
-
1541
- describe('appendInterruptionNotice', () => {
1542
- test('appends to existing text block', () => {
1543
- const blocks: ContentBlock[] = [
1544
- { type: 'text', content: 'Partial response' },
1545
- ]
1546
-
1547
- const result = appendInterruptionNotice(blocks)
1548
-
1549
- expect((result[0] as TextContentBlock).content).toBe(
1550
- 'Partial response\n\n[response interrupted]',
1551
- )
1552
- })
1553
-
1554
- test('creates new text block if no existing text', () => {
1555
- const blocks: ContentBlock[] = []
1556
-
1557
- const result = appendInterruptionNotice(blocks)
1558
-
1559
- expect(result).toHaveLength(1)
1560
- expect((result[0] as TextContentBlock).content).toBe(
1561
- '[response interrupted]',
1562
- )
1563
- })
1564
-
1565
- test('creates new block if last block is not text', () => {
1566
- const blocks: ContentBlock[] = [
1567
- {
1568
- type: 'tool',
1569
- toolCallId: 'tool-1',
1570
- toolName: 'read_files',
1571
- input: {},
1572
- },
1573
- ]
1574
-
1575
- const result = appendInterruptionNotice(blocks)
1576
-
1577
- expect(result).toHaveLength(2)
1578
- expect(result[1].type).toBe('text')
1579
- })
1580
- })
1581
-
1582
- // ============================================================================
1583
- // Spawn Agents Helpers Tests (from send-message-helpers)
1584
- // ============================================================================
1585
-
1586
- describe('createSpawnAgentBlocks', () => {
1587
- test('creates agent blocks from spawn_agents input', () => {
1588
- const agents = [
1589
- { agent_type: 'file-picker', prompt: 'Find files' },
1590
- { agent_type: 'code-searcher', prompt: 'Search code' },
1591
- ]
1592
-
1593
- const result = createSpawnAgentBlocks('tool-1', agents)
1594
-
1595
- expect(result).toHaveLength(2)
1596
- expect(result[0].type).toBe('agent')
1597
- expect((result[0] as AgentContentBlock).agentId).toBe('tool-1-0')
1598
- expect((result[1] as AgentContentBlock).agentId).toBe('tool-1-1')
1599
- })
1600
-
1601
- test('filters out hidden agents', () => {
1602
- const agents = [
1603
- { agent_type: 'file-picker' },
1604
- { agent_type: 'context-pruner' }, // bundled id — hidden
1605
- { agent_type: 'anvil/context-pruner' }, // publisher-qualified — hidden
1606
- ]
1607
-
1608
- const result = createSpawnAgentBlocks('tool-1', agents)
1609
-
1610
- expect(result).toHaveLength(1)
1611
- expect((result[0] as AgentContentBlock).agentType).toBe('file-picker')
1612
- })
1613
- })
1614
-
1615
- describe('isSpawnAgentsResult', () => {
1616
- test('returns true for spawn_agents result structure', () => {
1617
- const output = [{ agentName: 'file-picker', value: 'result' }]
1618
-
1619
- expect(isSpawnAgentsResult(output)).toBe(true)
1620
- })
1621
-
1622
- test('returns false for non-array', () => {
1623
- expect(isSpawnAgentsResult('string')).toBe(false)
1624
- expect(isSpawnAgentsResult(null)).toBe(false)
1625
- })
1626
-
1627
- test('returns false for array without agent properties', () => {
1628
- expect(isSpawnAgentsResult([{ foo: 'bar' }])).toBe(false)
1629
- })
1630
- })
1631
-
1632
- describe('extractSpawnAgentResultContent', () => {
1633
- test('extracts string value directly', () => {
1634
- const result = extractSpawnAgentResultContent('Simple result')
1635
-
1636
- expect(result.content).toBe('Simple result')
1637
- expect(result.hasError).toBe(false)
1638
- })
1639
-
1640
- test('extracts string from value property', () => {
1641
- const result = extractSpawnAgentResultContent({ value: 'Nested string' })
1642
-
1643
- expect(result.content).toBe('Nested string')
1644
- expect(result.hasError).toBe(false)
1645
- })
1646
-
1647
- test('extracts error message', () => {
1648
- const result = extractSpawnAgentResultContent({ errorMessage: 'Failed!' })
1649
-
1650
- expect(result.content).toBe('Failed!')
1651
- expect(result.hasError).toBe(true)
1652
- })
1653
-
1654
- test('extracts nested error message', () => {
1655
- const result = extractSpawnAgentResultContent({
1656
- value: { errorMessage: 'Nested error!' },
1657
- })
1658
-
1659
- expect(result.content).toBe('Nested error!')
1660
- expect(result.hasError).toBe(true)
1661
- })
1662
-
1663
- test('extracts message property', () => {
1664
- const result = extractSpawnAgentResultContent({
1665
- message: 'Message content',
1666
- })
1667
-
1668
- expect(result.content).toBe('Message content')
1669
- expect(result.hasError).toBe(false)
1670
- })
1671
-
1672
- test('extracts nested message property', () => {
1673
- const result = extractSpawnAgentResultContent({
1674
- value: { message: 'Nested message' },
1675
- })
1676
-
1677
- expect(result.content).toBe('Nested message')
1678
- expect(result.hasError).toBe(false)
1679
- })
1680
-
1681
- test('returns empty for null/undefined', () => {
1682
- const result = extractSpawnAgentResultContent(null)
1683
-
1684
- expect(result.content).toBe('')
1685
- expect(result.hasError).toBe(false)
1686
- })
1687
-
1688
- test('returns empty for empty object', () => {
1689
- const result = extractSpawnAgentResultContent({})
1690
-
1691
- expect(result.content).toBe('')
1692
- expect(result.hasError).toBe(false)
1693
- })
1694
- })
1695
-
1696
- // ============================================================================
1697
- // Message Completion Helpers Tests (from send-message-helpers)
1698
- // ============================================================================
1699
-
1700
- describe('markMessageComplete', () => {
1701
- const baseMessage: ChatMessage = {
1702
- id: 'msg-1',
1703
- variant: 'ai',
1704
- content: 'Hello',
1705
- timestamp: '',
1706
- }
1707
-
1708
- test('marks message as complete', () => {
1709
- const result = markMessageComplete(baseMessage)
1710
-
1711
- expect(result.isComplete).toBe(true)
1712
- })
1713
-
1714
- test('adds completion time', () => {
1715
- const result = markMessageComplete(baseMessage, { completionTime: '5s' })
1716
-
1717
- expect(result.completionTime).toBe('5s')
1718
- })
1719
-
1720
- test('adds credits', () => {
1721
- const result = markMessageComplete(baseMessage, { credits: 100 })
1722
-
1723
- expect(result.credits).toBe(100)
1724
- })
1725
-
1726
- test('adds runState to metadata', () => {
1727
- const runState = { output: { type: 'text', text: 'Done' } }
1728
- const result = markMessageComplete(baseMessage, { runState })
1729
-
1730
- expect(result.metadata?.runState).toEqual(runState)
1731
- })
1732
-
1733
- test('preserves existing metadata', () => {
1734
- const message: ChatMessage = {
1735
- ...baseMessage,
1736
- metadata: { userOpened: true },
1737
- }
1738
-
1739
- const result = markMessageComplete(message, { credits: 50 })
1740
-
1741
- expect(result.metadata?.userOpened).toBe(true)
1742
- })
1743
- })
1744
-
1745
- describe('setMessageError', () => {
1746
- test('sets error content and clears blocks', () => {
1747
- const message: ChatMessage = {
1748
- id: 'msg-1',
1749
- variant: 'ai',
1750
- content: '',
1751
- blocks: [{ type: 'text', content: 'Old content' }],
1752
- timestamp: '',
1753
- }
1754
-
1755
- const result = setMessageError(message, 'Error occurred')
1756
-
1757
- expect(result.content).toBe('Error occurred')
1758
- expect(result.blocks).toBeUndefined()
1759
- expect(result.isComplete).toBe(true)
1760
- })
1761
- })
1762
-
1763
- // ============================================================================
1764
- // sanitizeRestoredMessages Tests
1765
- // ============================================================================
1766
-
1767
- describe('sanitizeRestoredMessages', () => {
1768
- const streamedShell = (
1769
- overrides: Partial<ChatMessage> = {},
1770
- ): ChatMessage => ({
1771
- id: 'ai-1750000000000-abc123',
1772
- variant: 'ai',
1773
- content: '',
1774
- timestamp: new Date().toISOString(),
1775
- blocks: [{ type: 'text', content: 'partial answer' }],
1776
- ...overrides,
1777
- })
1778
-
1779
- test('marks an interrupted AI response complete with an interruption notice', () => {
1780
- const [result] = sanitizeRestoredMessages([streamedShell()])
1781
-
1782
- expect(result.isComplete).toBe(true)
1783
- const lastBlock = result.blocks![result.blocks!.length - 1]
1784
- expect(lastBlock.type).toBe('text')
1785
- expect((lastBlock as TextContentBlock).content).toContain(
1786
- '[response interrupted]',
1787
- )
1788
- })
1789
-
1790
- test('cancels running agent blocks in an interrupted AI response', () => {
1791
- const [result] = sanitizeRestoredMessages([
1792
- streamedShell({
1793
- blocks: [
1794
- {
1795
- type: 'agent',
1796
- agentId: 'agent-1',
1797
- agentName: 'TestAgent',
1798
- agentType: 'inline',
1799
- content: '',
1800
- status: 'running',
1801
- blocks: [],
1802
- },
1803
- ],
1804
- }),
1805
- ])
1806
-
1807
- const agentBlock = result.blocks![0] as AgentContentBlock
1808
- expect(agentBlock.status).toBe('cancelled')
1809
- })
1810
-
1811
- test('adds a notice to an interrupted AI response with no blocks yet', () => {
1812
- const [result] = sanitizeRestoredMessages([streamedShell({ blocks: [] })])
1813
-
1814
- expect(result.isComplete).toBe(true)
1815
- expect(result.blocks).toHaveLength(1)
1816
- expect((result.blocks![0] as TextContentBlock).content).toBe(
1817
- '[response interrupted]',
1818
- )
1819
- })
1820
-
1821
- test('leaves completed AI responses untouched', () => {
1822
- const message = streamedShell({ isComplete: true })
1823
- const [result] = sanitizeRestoredMessages([message])
1824
-
1825
- expect(result).toBe(message)
1826
- })
1827
-
1828
- test('strips the live inline-ad marker from restored responses', () => {
1829
- const [result] = sanitizeRestoredMessages([
1830
- streamedShell({
1831
- isComplete: true,
1832
- metadata: { allowInlineAds: true, userOpened: true },
1833
- }),
1834
- ])
1835
-
1836
- expect(result.metadata).toEqual({ userOpened: true })
1837
- })
1838
-
1839
- test('drops persisted hidden agent blocks (context-pruner)', () => {
1840
- const [result] = sanitizeRestoredMessages([
1841
- streamedShell({
1842
- isComplete: true,
1843
- blocks: [
1844
- {
1845
- type: 'agent',
1846
- agentId: 'agent-1',
1847
- agentName: 'context-pruner',
1848
- agentType: 'context-pruner',
1849
- content: '',
1850
- status: 'complete',
1851
- },
1852
- { type: 'text', content: 'answer' },
1853
- ],
1854
- }),
1855
- ])
1856
-
1857
- expect(result.blocks).toHaveLength(1)
1858
- expect(result.blocks![0].type).toBe('text')
1859
- })
1860
-
1861
- test('leaves mode dividers, system messages, and user messages untouched', () => {
1862
- const divider = createModeDividerMessage('DEFAULT' as any)
1863
- const system: ChatMessage = {
1864
- id: 'sys-123',
1865
- variant: 'ai',
1866
- content: 'system notice',
1867
- timestamp: new Date().toISOString(),
1868
- }
1869
- const user: ChatMessage = {
1870
- id: 'user-123',
1871
- variant: 'user',
1872
- content: 'hello',
1873
- timestamp: new Date().toISOString(),
1874
- }
1875
-
1876
- const results = sanitizeRestoredMessages([divider, system, user])
1877
-
1878
- expect(results[0]).toBe(divider)
1879
- expect(results[1]).toBe(system)
1880
- expect(results[2]).toBe(user)
1881
- })
1882
- })
1883
-
1884
- describe('sanitizeRestoredMessages corruption tolerance', () => {
1885
- test('does not throw on null entries in a restored blocks array', () => {
1886
- const corrupted: ChatMessage = {
1887
- id: 'ai-1750000000000-abc123',
1888
- variant: 'ai',
1889
- content: '',
1890
- timestamp: new Date().toISOString(),
1891
- blocks: [null as unknown as ContentBlock, { type: 'text', content: 'x' }],
1892
- }
1893
-
1894
- const [result] = sanitizeRestoredMessages([corrupted])
1895
-
1896
- expect(result.isComplete).toBe(true)
1897
- })
1898
- })