@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,1252 +0,0 @@
1
- import { describe, expect, it, test } from 'bun:test'
2
- import { cloneDeep } from 'lodash'
3
-
4
- import {
5
- withCacheControl,
6
- withoutCacheControl,
7
- convertCbToModelMessages,
8
- dropUnansweredToolCalls,
9
- systemMessage,
10
- userMessage,
11
- assistantMessage,
12
- jsonToolResult,
13
- mediaToolResult,
14
- } from '../messages'
15
-
16
- import type { Message } from '../../types/messages/anvil-message'
17
- import type { ToolResultPart } from 'ai'
18
-
19
- // Test helper types for provider options with cache control
20
- type CacheControlValue = { type: string }
21
- type ProviderWithCacheControl = Record<string, unknown> & {
22
- cache_control?: CacheControlValue
23
- }
24
-
25
- describe('withCacheControl', () => {
26
- it('should add cache control to object without providerOptions', () => {
27
- const obj = {} as Parameters<typeof withCacheControl>[0]
28
- const result = withCacheControl(obj)
29
-
30
- expect(result.providerOptions).toBeDefined()
31
- const resultOptions = result.providerOptions as Record<string, ProviderWithCacheControl>
32
- expect(resultOptions.anthropic?.cache_control).toEqual({
33
- type: 'ephemeral',
34
- })
35
- expect(resultOptions.openrouter?.cache_control).toEqual({
36
- type: 'ephemeral',
37
- })
38
- expect(resultOptions.openaiCompatible?.cache_control).toEqual({
39
- type: 'ephemeral',
40
- })
41
- })
42
-
43
- it('should add cache control to existing providerOptions', () => {
44
- const obj = {
45
- providerOptions: {
46
- anthropic: { someOtherOption: 'value' },
47
- },
48
- } as Parameters<typeof withCacheControl>[0]
49
- const result = withCacheControl(obj)
50
-
51
- const resultAnthropicOptions = result.providerOptions?.anthropic as ProviderWithCacheControl
52
- expect(resultAnthropicOptions.cache_control).toEqual({
53
- type: 'ephemeral',
54
- })
55
- expect(resultAnthropicOptions.someOtherOption).toBe(
56
- 'value',
57
- )
58
- })
59
-
60
- it('should not mutate original object', () => {
61
- const original = {} as Parameters<typeof withCacheControl>[0]
62
- const result = withCacheControl(original)
63
-
64
- expect(original.providerOptions).toBeUndefined()
65
- expect(result.providerOptions).toBeDefined()
66
- })
67
-
68
- it('should handle all three providers', () => {
69
- const obj = {} as Parameters<typeof withCacheControl>[0]
70
- const result = withCacheControl(obj)
71
-
72
- const resultOptions = result.providerOptions as Record<string, ProviderWithCacheControl>
73
- expect(resultOptions.anthropic?.cache_control?.type).toBe('ephemeral')
74
- expect(resultOptions.openrouter?.cache_control?.type).toBe('ephemeral')
75
- expect(resultOptions.openaiCompatible?.cache_control?.type).toBe('ephemeral')
76
- })
77
- })
78
-
79
- describe('withoutCacheControl', () => {
80
- it('should remove cache control from all providers', () => {
81
- const obj = {
82
- id: 'test',
83
- providerOptions: {
84
- anthropic: { cache_control: { type: 'ephemeral' } },
85
- openrouter: { cache_control: { type: 'ephemeral' } },
86
- openaiCompatible: { cache_control: { type: 'ephemeral' } },
87
- },
88
- }
89
- const result = withoutCacheControl(obj)
90
-
91
- expect(result.providerOptions).toBeUndefined()
92
- })
93
-
94
- it('should preserve other provider options', () => {
95
- const obj = {
96
- id: 'test',
97
- providerOptions: {
98
- anthropic: {
99
- cache_control: { type: 'ephemeral' },
100
- otherOption: 'value',
101
- },
102
- },
103
- }
104
- const result = withoutCacheControl(obj)
105
-
106
- expect(result.providerOptions?.anthropic?.cache_control).toBeUndefined()
107
- expect(result.providerOptions?.anthropic?.otherOption).toBe('value')
108
- })
109
-
110
- it('should not mutate original object', () => {
111
- const original = {
112
- id: 'test',
113
- providerOptions: {
114
- anthropic: { cache_control: { type: 'ephemeral' } },
115
- },
116
- }
117
- const result = withoutCacheControl(original)
118
-
119
- expect(original.providerOptions?.anthropic?.cache_control).toBeDefined()
120
- expect(result.providerOptions?.anthropic?.cache_control).toBeUndefined()
121
- })
122
-
123
- it('should handle object with no cache control', () => {
124
- const obj = {} as Parameters<typeof withoutCacheControl>[0]
125
- const result = withoutCacheControl(obj)
126
-
127
- expect(result.providerOptions).toBeUndefined()
128
- })
129
-
130
- it('should clean up empty provider objects', () => {
131
- const obj = {
132
- id: 'test',
133
- providerOptions: {
134
- anthropic: { cache_control: { type: 'ephemeral' } },
135
- },
136
- }
137
- const result = withoutCacheControl(obj)
138
-
139
- expect(result.providerOptions).toBeUndefined()
140
- })
141
- })
142
-
143
- describe('dropUnansweredToolCalls', () => {
144
- const toolCall = (toolCallId: string) =>
145
- assistantMessage({
146
- type: 'tool-call',
147
- toolCallId,
148
- toolName: 'read_files',
149
- input: { paths: ['README.md'] },
150
- })
151
-
152
- const toolResult = (toolCallId: string): Message => ({
153
- role: 'tool',
154
- toolCallId,
155
- toolName: 'read_files',
156
- content: jsonToolResult({ files: [] }),
157
- })
158
-
159
- it('returns valid history unchanged', () => {
160
- const messages: Message[] = [
161
- userMessage('start'),
162
- toolCall('complete'),
163
- toolResult('complete'),
164
- assistantMessage('done'),
165
- ]
166
-
167
- expect(dropUnansweredToolCalls(messages)).toBe(messages)
168
- })
169
-
170
- it('repairs a partial run of consecutive assistant calls', () => {
171
- const messages: Message[] = [
172
- userMessage('start'),
173
- toolCall('complete'),
174
- toolCall('interrupted'),
175
- toolResult('complete'),
176
- ]
177
-
178
- expect(dropUnansweredToolCalls(messages)).toEqual([
179
- messages[0],
180
- messages[1],
181
- messages[3],
182
- ])
183
- })
184
-
185
- it('removes only unanswered parts without mutating assistant metadata', () => {
186
- const assistant = {
187
- ...assistantMessage('working'),
188
- content: [
189
- { type: 'text' as const, text: 'working' },
190
- toolCall('complete').content[0],
191
- toolCall('interrupted').content[0],
192
- ],
193
- tags: ['important'],
194
- }
195
- const messages: Message[] = [assistant, toolResult('complete')]
196
-
197
- expect(dropUnansweredToolCalls(messages)).toEqual([
198
- {
199
- ...assistant,
200
- content: [assistant.content[0], assistant.content[1]],
201
- },
202
- messages[1],
203
- ])
204
- expect(assistant.content).toHaveLength(3)
205
- })
206
-
207
- it('preserves freestanding tool results', () => {
208
- const messages: Message[] = [toolResult('orphan'), userMessage('next')]
209
-
210
- expect(dropUnansweredToolCalls(messages)).toBe(messages)
211
- })
212
-
213
- it('preserves provider-executed calls without local results', () => {
214
- const messages: Message[] = [
215
- assistantMessage({
216
- type: 'tool-call',
217
- toolCallId: 'provider-call',
218
- toolName: 'web_search',
219
- input: { query: 'Anvil' },
220
- providerExecuted: true,
221
- }),
222
- ]
223
-
224
- expect(dropUnansweredToolCalls(messages)).toBe(messages)
225
- })
226
- })
227
-
228
- describe('convertCbToModelMessages', () => {
229
- describe('unfinished tool-call repair', () => {
230
- it('drops a call whose result arrives after a user boundary', () => {
231
- const result = convertCbToModelMessages({
232
- messages: [
233
- userMessage('start'),
234
- assistantMessage({
235
- type: 'tool-call',
236
- toolCallId: 'interrupted-call',
237
- toolName: 'read_files',
238
- input: { paths: ['README.md'] },
239
- }),
240
- userMessage('continue'),
241
- {
242
- role: 'tool',
243
- toolCallId: 'interrupted-call',
244
- toolName: 'read_files',
245
- content: jsonToolResult({ files: [] }),
246
- },
247
- ],
248
- includeCacheControl: false,
249
- })
250
-
251
- // The surviving user messages aggregate after the invalid assistant call
252
- // is removed. A result later in history cannot answer across the user
253
- // boundary, but freestanding tool results remain supported.
254
- expect(result.map((message) => message.role)).toEqual(['user', 'tool'])
255
- expect(result[0].content).toHaveLength(2)
256
- })
257
-
258
- // A media result rides a user message, and the AI SDK treats a user message
259
- // as a boundary where every tool call issued so far must already be
260
- // answered. Emitted inline it splits a batch of parallel calls and the
261
- // sibling still pending throws MissingToolResultsError client-side, which
262
- // wedges every later turn in the thread that replays the history.
263
- it('keeps a media result from splitting a batch of parallel tool calls', () => {
264
- const screenshot = (toolCallId: string): Message => ({
265
- role: 'tool',
266
- toolCallId,
267
- toolName: 'preview_screenshot',
268
- content: [
269
- ...mediaToolResult({ data: 'base64data', mediaType: 'image/png' }),
270
- ...jsonToolResult({ ok: true }),
271
- ],
272
- })
273
- const readFiles = (toolCallId: string): Message => ({
274
- role: 'tool',
275
- toolCallId,
276
- toolName: 'read_files',
277
- content: jsonToolResult({ files: [] }),
278
- })
279
-
280
- const result = convertCbToModelMessages({
281
- messages: [
282
- userMessage('screenshot the preview and read the file'),
283
- {
284
- ...assistantMessage('working'),
285
- content: [
286
- {
287
- type: 'tool-call',
288
- toolCallId: 'call_shot',
289
- toolName: 'preview_screenshot',
290
- input: {},
291
- },
292
- {
293
- type: 'tool-call',
294
- toolCallId: 'call_read',
295
- toolName: 'read_files',
296
- input: {},
297
- },
298
- ],
299
- },
300
- screenshot('call_shot'),
301
- readFiles('call_read'),
302
- ],
303
- includeCacheControl: false,
304
- })
305
-
306
- // Both results land before the image's user message, so no call is still
307
- // pending at the boundary.
308
- expect(result.map((message) => message.role)).toEqual([
309
- 'user',
310
- 'assistant',
311
- 'tool',
312
- 'tool',
313
- 'user',
314
- ])
315
- expect(result.at(-1)!.content).toEqual([
316
- expect.objectContaining({ type: 'file' }),
317
- ])
318
- })
319
-
320
- // Waiting on the answers, not merely on the next non-tool message: anything
321
- // interleaved between a batch's results must not let the image out early.
322
- it('holds media until a sibling result arrives after an interleaved message', () => {
323
- const result = convertCbToModelMessages({
324
- messages: [
325
- userMessage('go'),
326
- {
327
- ...assistantMessage('working'),
328
- content: [
329
- {
330
- type: 'tool-call',
331
- toolCallId: 'call_shot',
332
- toolName: 'preview_screenshot',
333
- input: {},
334
- },
335
- {
336
- type: 'tool-call',
337
- toolCallId: 'call_read',
338
- toolName: 'read_files',
339
- input: {},
340
- },
341
- ],
342
- },
343
- {
344
- role: 'tool',
345
- toolCallId: 'call_shot',
346
- toolName: 'preview_screenshot',
347
- content: [
348
- ...mediaToolResult({ data: 'base64data', mediaType: 'image/png' }),
349
- ...jsonToolResult({ ok: true }),
350
- ],
351
- },
352
- assistantMessage('still thinking'),
353
- {
354
- role: 'tool',
355
- toolCallId: 'call_read',
356
- toolName: 'read_files',
357
- content: jsonToolResult({ files: [] }),
358
- },
359
- ],
360
- includeCacheControl: false,
361
- })
362
-
363
- expect(result.map((message) => message.role)).toEqual([
364
- 'user',
365
- 'assistant',
366
- 'tool',
367
- 'assistant',
368
- 'tool',
369
- 'user',
370
- ])
371
- })
372
-
373
- it('flushes buffered media before the next assistant message', () => {
374
- const result = convertCbToModelMessages({
375
- messages: [
376
- userMessage('go'),
377
- {
378
- ...assistantMessage('working'),
379
- content: [
380
- {
381
- type: 'tool-call',
382
- toolCallId: 'call_shot',
383
- toolName: 'preview_screenshot',
384
- input: {},
385
- },
386
- ],
387
- },
388
- {
389
- role: 'tool',
390
- toolCallId: 'call_shot',
391
- toolName: 'preview_screenshot',
392
- content: [
393
- ...mediaToolResult({ data: 'base64data', mediaType: 'image/png' }),
394
- ...jsonToolResult({ ok: true }),
395
- ],
396
- },
397
- assistantMessage('here is what I saw'),
398
- ],
399
- includeCacheControl: false,
400
- })
401
-
402
- expect(result.map((message) => message.role)).toEqual([
403
- 'user',
404
- 'assistant',
405
- 'tool',
406
- 'user',
407
- 'assistant',
408
- ])
409
- })
410
- })
411
-
412
- describe('basic message conversion', () => {
413
- it('should convert system messages', () => {
414
- const messages: Message[] = [systemMessage('You are a helpful assistant')]
415
-
416
- const result = convertCbToModelMessages({
417
- messages,
418
- includeCacheControl: false,
419
- })
420
-
421
- expect(result).toEqual([
422
- {
423
- role: 'system',
424
- content: 'You are a helpful assistant',
425
- },
426
- ])
427
- })
428
-
429
- it('should convert user messages with array content', () => {
430
- const messages: Message[] = [
431
- {
432
- role: 'user',
433
- content: [
434
- { type: 'text', text: 'First part' },
435
- { type: 'text', text: 'Second part' },
436
- ],
437
- },
438
- ]
439
-
440
- const result = convertCbToModelMessages({
441
- messages,
442
- includeCacheControl: false,
443
- })
444
-
445
- expect(result).toEqual([
446
- {
447
- role: 'user',
448
- content: [
449
- {
450
- type: 'text',
451
- text: 'First part',
452
- },
453
- {
454
- type: 'text',
455
- text: 'Second part',
456
- },
457
- ],
458
- },
459
- ])
460
- })
461
- })
462
-
463
- describe('lone surrogate sanitization', () => {
464
- // A lone (unpaired) UTF-16 surrogate — e.g. produced by slicing a file read
465
- // in the middle of an emoji — serializes to a syntactically-valid \uXXXX
466
- // escape that JS JSON.parse accepts but strict server-side parsers
467
- // (serde_json) reject with "unexpected end of hex escape", fatally aborting
468
- // the agent on every subsequent request. The converter must neutralize it.
469
- const LONE_HIGH = '\uD83D' // high surrogate of 😀 with the low half dropped
470
- const REPLACEMENT = '�'
471
-
472
- const assertWellFormed = (value: unknown): void => {
473
- const json = JSON.stringify(value)
474
- // matchAll over lone surrogates: there should be none left.
475
- const lone = json.match(
476
- /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g,
477
- )
478
- expect(lone).toBeNull()
479
- }
480
-
481
- it('sanitizes a lone surrogate in a user text part', () => {
482
- const result = convertCbToModelMessages({
483
- messages: [userMessage(`hello ${LONE_HIGH} world`)],
484
- includeCacheControl: false,
485
- })
486
- assertWellFormed(result)
487
- expect(JSON.stringify(result)).toContain(`hello ${REPLACEMENT} world`)
488
- })
489
-
490
- it('sanitizes a lone surrogate nested in a tool result value', () => {
491
- const result = convertCbToModelMessages({
492
- messages: [
493
- {
494
- role: 'tool',
495
- toolName: 'read_files',
496
- toolCallId: 'call_1',
497
- content: jsonToolResult([
498
- { path: 'big.ts', content: `truncated${LONE_HIGH}` },
499
- ]),
500
- },
501
- ],
502
- includeCacheControl: false,
503
- })
504
- assertWellFormed(result)
505
- })
506
-
507
- it('sanitizes a lone surrogate in a system message string', () => {
508
- const result = convertCbToModelMessages({
509
- messages: [systemMessage(`prompt ${LONE_HIGH}`)],
510
- includeCacheControl: false,
511
- })
512
- assertWellFormed(result)
513
- })
514
-
515
- it('leaves valid surrogate pairs (emoji) intact', () => {
516
- const result = convertCbToModelMessages({
517
- messages: [userMessage('keep the 😀 emoji')],
518
- includeCacheControl: false,
519
- })
520
- assertWellFormed(result)
521
- expect(JSON.stringify(result)).toContain('keep the 😀 emoji')
522
- })
523
- })
524
-
525
- describe('tool message conversion', () => {
526
- it('should convert tool messages with JSON output', () => {
527
- const messages: Message[] = [
528
- {
529
- role: 'tool',
530
- toolName: 'test_tool',
531
- toolCallId: 'call_123',
532
- content: jsonToolResult({ result: 'success' }),
533
- },
534
- ]
535
-
536
- const result = convertCbToModelMessages({
537
- messages,
538
- includeCacheControl: false,
539
- })
540
-
541
- expect(result).toEqual([
542
- expect.objectContaining({
543
- role: 'tool',
544
- content: [
545
- expect.objectContaining({
546
- type: 'tool-result',
547
- toolCallId: 'call_123',
548
- toolName: 'test_tool',
549
- output: { type: 'json', value: { result: 'success' } },
550
- } satisfies ToolResultPart),
551
- ],
552
- }),
553
- ])
554
- })
555
-
556
- it('should convert tool messages with media output', () => {
557
- const messages: Message[] = [
558
- {
559
- role: 'tool',
560
- toolName: 'test_tool',
561
- toolCallId: 'call_123',
562
- content: mediaToolResult({
563
- data: 'base64data',
564
- mediaType: 'image/png',
565
- }),
566
- },
567
- ]
568
-
569
- const result = convertCbToModelMessages({
570
- messages,
571
- includeCacheControl: false,
572
- })
573
-
574
- // The image rides a user message (providers reject images inside a tool
575
- // message), but the call still has to be answered or it is an unpaired
576
- // tool call — which providers reject and the server's repair then strips.
577
- expect(result).toEqual([
578
- expect.objectContaining({
579
- role: 'tool',
580
- content: [
581
- expect.objectContaining({
582
- toolCallId: 'call_123',
583
- type: 'tool-result',
584
- }),
585
- ],
586
- }),
587
- expect.objectContaining({
588
- role: 'user',
589
- content: [
590
- expect.objectContaining({
591
- type: 'file',
592
- }),
593
- ],
594
- }),
595
- ])
596
- })
597
-
598
- it('should convert tool messages with empty content', () => {
599
- const messages: Message[] = [
600
- {
601
- role: 'tool',
602
- toolName: 'scraper_page_to_markdown',
603
- toolCallId: 'call_empty',
604
- content: [],
605
- },
606
- ]
607
-
608
- const result = convertCbToModelMessages({
609
- messages,
610
- includeCacheControl: false,
611
- })
612
-
613
- expect(result).toEqual([
614
- expect.objectContaining({
615
- role: 'tool',
616
- toolCallId: 'call_empty',
617
- toolName: 'scraper_page_to_markdown',
618
- content: [
619
- expect.objectContaining({
620
- type: 'tool-result',
621
- toolCallId: 'call_empty',
622
- toolName: 'scraper_page_to_markdown',
623
- output: { type: 'json', value: '' },
624
- } satisfies ToolResultPart),
625
- ],
626
- }),
627
- ])
628
- })
629
-
630
- it('should handle multiple tool outputs', () => {
631
- const messages: Message[] = [
632
- {
633
- role: 'tool',
634
- toolName: 'test_tool',
635
- toolCallId: 'call_123',
636
- content: [
637
- { type: 'json', value: { result1: 'success' } },
638
- { type: 'json', value: { result2: 'also success' } },
639
- ],
640
- },
641
- ]
642
-
643
- const result = convertCbToModelMessages({
644
- messages,
645
- includeCacheControl: false,
646
- })
647
-
648
- // Multiple tool outputs are aggregated into one user message
649
- expect(result).toEqual([
650
- expect.objectContaining({
651
- role: 'tool',
652
- }),
653
- expect.objectContaining({
654
- role: 'tool',
655
- }),
656
- ])
657
- })
658
- })
659
-
660
- describe('message aggregation', () => {
661
- it('should aggregate consecutive system messages', () => {
662
- const messages: Message[] = [
663
- systemMessage({ content: 'First system message' }),
664
- systemMessage({ content: 'Second system message' }),
665
- ]
666
-
667
- const result = convertCbToModelMessages({
668
- messages,
669
- includeCacheControl: false,
670
- })
671
-
672
- expect(result).toEqual([
673
- {
674
- role: 'system',
675
- content: 'First system message\n\nSecond system message',
676
- },
677
- ])
678
- })
679
-
680
- it('should aggregate consecutive user messages', () => {
681
- const messages: Message[] = [
682
- userMessage('First user message'),
683
- userMessage('Second user message'),
684
- ]
685
-
686
- const result = convertCbToModelMessages({
687
- messages,
688
- includeCacheControl: false,
689
- })
690
-
691
- expect(result).toEqual([
692
- {
693
- role: 'user',
694
- content: [
695
- {
696
- type: 'text',
697
- text: 'First user message',
698
- },
699
- {
700
- type: 'text',
701
- text: 'Second user message',
702
- },
703
- ],
704
- sentAt: expect.any(Number),
705
- },
706
- ])
707
- })
708
-
709
- it('should aggregate consecutive assistant messages', () => {
710
- const messages: Message[] = [
711
- assistantMessage('First assistant message'),
712
- assistantMessage('Second assistant message'),
713
- ]
714
-
715
- const result = convertCbToModelMessages({
716
- messages,
717
- includeCacheControl: false,
718
- })
719
-
720
- expect(result).toEqual([
721
- {
722
- role: 'assistant',
723
- content: [
724
- {
725
- type: 'text',
726
- text: 'First assistant message',
727
- },
728
- {
729
- type: 'text',
730
- text: 'Second assistant message',
731
- },
732
- ],
733
- sentAt: expect.any(Number),
734
- },
735
- ])
736
- })
737
-
738
- it('should not aggregate messages with different timeToLive', () => {
739
- const messages: Message[] = [
740
- {
741
- role: 'user',
742
- content: [{ type: 'text', text: 'First' }],
743
- timeToLive: 'agentStep',
744
- },
745
-
746
- {
747
- role: 'user',
748
- content: [{ type: 'text', text: 'Second' }],
749
- timeToLive: 'userPrompt',
750
- },
751
- ]
752
-
753
- const result = convertCbToModelMessages({
754
- messages,
755
- includeCacheControl: false,
756
- })
757
-
758
- expect(result).toEqual([
759
- {
760
- role: 'user',
761
- content: [{ type: 'text', text: 'First' }],
762
- timeToLive: 'agentStep',
763
- },
764
- {
765
- role: 'user',
766
- content: [{ type: 'text', text: 'Second' }],
767
- timeToLive: 'userPrompt',
768
- },
769
- ])
770
- })
771
-
772
- it('should not aggregate messages with different providerOptions', () => {
773
- const messages: Message[] = [
774
- {
775
- role: 'user',
776
- content: [{ type: 'text', text: 'First' }],
777
- providerOptions: { anthropic: { option1: 'value1' } },
778
- },
779
- {
780
- role: 'user',
781
- content: [{ type: 'text', text: 'Second' }],
782
- providerOptions: { anthropic: { option1: 'value2' } },
783
- },
784
- ]
785
-
786
- const result = convertCbToModelMessages({
787
- messages,
788
- includeCacheControl: false,
789
- })
790
-
791
- expect(result).toEqual([
792
- {
793
- role: 'user',
794
- content: [{ type: 'text', text: 'First' }],
795
- providerOptions: { anthropic: { option1: 'value1' } },
796
- },
797
- {
798
- role: 'user',
799
- content: [{ type: 'text', text: 'Second' }],
800
- providerOptions: { anthropic: { option1: 'value2' } },
801
- },
802
- ])
803
- })
804
-
805
- it('should not aggregate messages with different tags', () => {
806
- const messages: Message[] = [
807
- {
808
- role: 'user',
809
- content: [{ type: 'text', text: 'First' }],
810
- tags: ['tag1'],
811
- },
812
- {
813
- role: 'user',
814
- content: [{ type: 'text', text: 'Second' }],
815
- tags: ['tag2'],
816
- },
817
- ]
818
-
819
- const result = convertCbToModelMessages({
820
- messages,
821
- includeCacheControl: false,
822
- })
823
-
824
- expect(result).toEqual([
825
- {
826
- role: 'user',
827
- content: [{ type: 'text', text: 'First' }],
828
- tags: ['tag1'],
829
- },
830
- {
831
- role: 'user',
832
- content: [{ type: 'text', text: 'Second' }],
833
- tags: ['tag2'],
834
- },
835
- ])
836
- })
837
- })
838
-
839
- describe('cache control', () => {
840
- // Note: Cache control is applied to content parts within messages, not to the messages themselves.
841
- // The implementation splits text content and adds cache control to specific parts based on tagged prompts.
842
- test('should add cache control when includeCacheControl is true', () => {
843
- const messages: Message[] = [
844
- systemMessage('System message'),
845
- userMessage('Context message'),
846
- assistantMessage('Response'),
847
- userMessage({
848
- content: 'User message',
849
- tags: ['USER_PROMPT'],
850
- }),
851
- ]
852
-
853
- const result = convertCbToModelMessages({
854
- messages,
855
- includeCacheControl: true,
856
- })
857
-
858
- // Cache control is on content parts of the assistant message (result[2])
859
- if (
860
- typeof result[2].content !== 'string' &&
861
- result[2].content.length > 0
862
- ) {
863
- const lastContentPart = result[2].content[result[2].content.length - 1] as { providerOptions?: Record<string, ProviderWithCacheControl> }
864
- expect(
865
- lastContentPart.providerOptions?.anthropic?.cache_control,
866
- ).toEqual({
867
- type: 'ephemeral',
868
- })
869
- }
870
- })
871
-
872
- it('should not add cache control when includeCacheControl is false', () => {
873
- const messages: Message[] = [
874
- systemMessage('System message'),
875
- userMessage({
876
- content: 'User message',
877
- tags: ['USER_PROMPT'],
878
- }),
879
- ]
880
-
881
- const result = convertCbToModelMessages({
882
- messages,
883
- includeCacheControl: false,
884
- })
885
-
886
- expect(result[0].providerOptions).toBeUndefined()
887
- })
888
-
889
- test('should add cache control before USER_PROMPT tag', () => {
890
- const messages: Message[] = [
891
- systemMessage('System'),
892
- userMessage('Context'),
893
- assistantMessage('Response'),
894
- userMessage('More context'),
895
- userMessage({
896
- content: 'User prompt',
897
- tags: ['USER_PROMPT'],
898
- }),
899
- ]
900
-
901
- const result = convertCbToModelMessages({
902
- messages,
903
- includeCacheControl: true,
904
- })
905
-
906
- // Cache control should be on content part before USER_PROMPT
907
- expect(result).toEqual([
908
- expect.objectContaining({ role: 'system' }),
909
- expect.objectContaining({ role: 'user' }),
910
- expect.objectContaining({ role: 'assistant' }),
911
- {
912
- role: 'user',
913
- sentAt: expect.any(Number),
914
- content: [
915
- {
916
- type: 'text',
917
- text: 'More context',
918
- providerOptions: expect.objectContaining({
919
- openaiCompatible: {
920
- cache_control: {
921
- type: 'ephemeral',
922
- },
923
- },
924
- }),
925
- },
926
- ],
927
- },
928
- expect.objectContaining({ role: 'user' }),
929
- ])
930
- })
931
-
932
- test('should add cache control before LAST_ASSISTANT_MESSAGE tag', () => {
933
- const messages: Message[] = [
934
- systemMessage('System'),
935
- userMessage('Context'),
936
- assistantMessage('Response'),
937
- userMessage('Instructions'),
938
- assistantMessage({
939
- content: 'Second response',
940
- tags: ['LAST_ASSISTANT_MESSAGE'],
941
- }),
942
- ]
943
-
944
- const result = convertCbToModelMessages({
945
- messages,
946
- includeCacheControl: true,
947
- })
948
-
949
- expect(result).toEqual([
950
- expect.objectContaining({ role: 'system' }),
951
- expect.objectContaining({ role: 'user' }),
952
- expect.objectContaining({ role: 'assistant' }),
953
- {
954
- role: 'user',
955
- sentAt: expect.any(Number),
956
- content: [
957
- {
958
- type: 'text',
959
- text: 'Instructions',
960
- providerOptions: expect.objectContaining({
961
- openaiCompatible: {
962
- cache_control: {
963
- type: 'ephemeral',
964
- },
965
- },
966
- }),
967
- },
968
- ],
969
- },
970
- expect.objectContaining({ role: 'assistant' }),
971
- ])
972
- })
973
-
974
- test('should add cache control before STEP_PROMPT tag', () => {
975
- const messages: Message[] = [
976
- systemMessage('System'),
977
- userMessage('Context'),
978
- assistantMessage('Response'),
979
- userMessage('More context'),
980
- userMessage({ content: 'Step', tags: ['STEP_PROMPT'] }),
981
- ]
982
-
983
- const result = convertCbToModelMessages({
984
- messages,
985
- includeCacheControl: true,
986
- })
987
-
988
- expect(result).toEqual([
989
- expect.objectContaining({ role: 'system' }),
990
- expect.objectContaining({ role: 'user' }),
991
- expect.objectContaining({ role: 'assistant' }),
992
- {
993
- role: 'user',
994
- sentAt: expect.any(Number),
995
- content: [
996
- {
997
- type: 'text',
998
- text: 'More context',
999
- providerOptions: expect.objectContaining({
1000
- openaiCompatible: {
1001
- cache_control: {
1002
- type: 'ephemeral',
1003
- },
1004
- },
1005
- }),
1006
- },
1007
- ],
1008
- },
1009
- expect.objectContaining({ role: 'user' }),
1010
- ])
1011
- })
1012
-
1013
- test('should add cache control to last message', () => {
1014
- const messages: Message[] = [
1015
- systemMessage('System'),
1016
- userMessage('Context'),
1017
- assistantMessage('Response'),
1018
- userMessage('More context'),
1019
- userMessage('User message'),
1020
- ]
1021
-
1022
- const result = convertCbToModelMessages({
1023
- messages,
1024
- includeCacheControl: true,
1025
- })
1026
-
1027
- // Cache control is on content parts in the assistant message
1028
- expect(result).toEqual([
1029
- expect.objectContaining({ role: 'system' }),
1030
- expect.objectContaining({ role: 'user' }),
1031
- expect.objectContaining({ role: 'assistant' }),
1032
- {
1033
- role: 'user',
1034
- sentAt: expect.any(Number),
1035
- content: [
1036
- { type: 'text', text: 'More context' },
1037
- {
1038
- type: 'text',
1039
- text: 'User message',
1040
- providerOptions: expect.objectContaining({
1041
- openaiCompatible: {
1042
- cache_control: {
1043
- type: 'ephemeral',
1044
- },
1045
- },
1046
- }),
1047
- },
1048
- ],
1049
- },
1050
- ])
1051
- })
1052
-
1053
- test('should handle system messages with cache control', () => {
1054
- const messages: Message[] = [
1055
- systemMessage('Long system prompt'),
1056
- userMessage({ content: 'User', tags: ['USER_PROMPT'] }),
1057
- assistantMessage('Response'),
1058
- userMessage('User 2'),
1059
- ]
1060
-
1061
- const result = convertCbToModelMessages({
1062
- messages,
1063
- includeCacheControl: true,
1064
- })
1065
-
1066
- expect(result).toEqual([
1067
- {
1068
- role: 'system',
1069
- content: 'Long system prompt',
1070
- providerOptions: expect.objectContaining({
1071
- openaiCompatible: {
1072
- cache_control: {
1073
- type: 'ephemeral',
1074
- },
1075
- },
1076
- }),
1077
- },
1078
- expect.objectContaining({ role: 'user' }),
1079
- expect.objectContaining({ role: 'assistant' }),
1080
- expect.objectContaining({ role: 'user' }),
1081
- ])
1082
- })
1083
-
1084
- it('should handle array content with cache control on non-text parts', () => {
1085
- const messages: Message[] = [
1086
- systemMessage('System'),
1087
- userMessage([
1088
- { type: 'text', text: 'Context' },
1089
- { type: 'file', data: 'base64', mediaType: 'image/png' },
1090
- ]),
1091
- userMessage({ content: 'Next', tags: ['USER_PROMPT'] }),
1092
- ]
1093
-
1094
- const result = convertCbToModelMessages({
1095
- messages,
1096
- includeCacheControl: true,
1097
- })
1098
-
1099
- // Should add cache control to the file part (last non-text part)
1100
- expect(result).toEqual([
1101
- expect.objectContaining({ role: 'system' }),
1102
- {
1103
- role: 'user',
1104
- sentAt: expect.any(Number),
1105
- content: [
1106
- {
1107
- type: 'text',
1108
- text: 'Context',
1109
- },
1110
- {
1111
- type: 'file',
1112
- data: 'base64',
1113
- mediaType: 'image/png',
1114
- providerOptions: expect.objectContaining({
1115
- openaiCompatible: {
1116
- cache_control: {
1117
- type: 'ephemeral',
1118
- },
1119
- },
1120
- }),
1121
- },
1122
- ],
1123
- },
1124
- expect.objectContaining({ role: 'user' }),
1125
- ])
1126
- })
1127
-
1128
- it('should handle very short text content when finding cache control location', () => {
1129
- const messages: Message[] = [
1130
- systemMessage('System'),
1131
- userMessage([
1132
- { type: 'text', text: 'Longer text' },
1133
- { type: 'text', text: 'X' }, // Short
1134
- ]),
1135
- userMessage({ content: 'Next', tags: ['USER_PROMPT'] }),
1136
- ]
1137
-
1138
- const result = convertCbToModelMessages({
1139
- messages,
1140
- includeCacheControl: true,
1141
- })
1142
-
1143
- expect(result).toEqual([
1144
- expect.objectContaining({ role: 'system' }),
1145
- {
1146
- role: 'user',
1147
- sentAt: expect.any(Number),
1148
- content: [
1149
- { type: 'text', text: 'Longer text' },
1150
- {
1151
- type: 'text',
1152
- text: 'X',
1153
- providerOptions: expect.objectContaining({
1154
- openaiCompatible: {
1155
- cache_control: {
1156
- type: 'ephemeral',
1157
- },
1158
- },
1159
- }),
1160
- },
1161
- ],
1162
- },
1163
- expect.objectContaining({ role: 'user' }),
1164
- ])
1165
- })
1166
- })
1167
-
1168
- describe('edge cases', () => {
1169
- it('should handle empty messages array', () => {
1170
- const result = convertCbToModelMessages({
1171
- messages: [],
1172
- includeCacheControl: false,
1173
- })
1174
-
1175
- expect(result).toHaveLength(0)
1176
- })
1177
-
1178
- it('should handle tool-call content in assistant messages', () => {
1179
- const messages: Message[] = [
1180
- assistantMessage({
1181
- type: 'tool-call',
1182
- toolCallId: 'call_123',
1183
- toolName: 'test_tool',
1184
- input: { param: 'value' },
1185
- }),
1186
- {
1187
- role: 'tool',
1188
- toolCallId: 'call_123',
1189
- toolName: 'test_tool',
1190
- content: jsonToolResult({ ok: true }),
1191
- },
1192
- ]
1193
-
1194
- const result = convertCbToModelMessages({
1195
- messages,
1196
- includeCacheControl: false,
1197
- })
1198
-
1199
- expect(result[0]).toEqual({
1200
- role: 'assistant',
1201
- sentAt: expect.any(Number),
1202
- content: [
1203
- {
1204
- type: 'tool-call',
1205
- toolCallId: 'call_123',
1206
- toolName: 'test_tool',
1207
- input: { param: 'value' },
1208
- },
1209
- ],
1210
- })
1211
- expect(result[1].role).toBe('tool')
1212
- })
1213
-
1214
- it('should preserve message metadata during conversion', () => {
1215
- const messages: Message[] = [
1216
- userMessage({
1217
- content: 'Test',
1218
- tags: ['custom_tag'],
1219
- timeToLive: 'agentStep',
1220
- providerOptions: { anthropic: { someOption: 'value' } },
1221
- }),
1222
- ]
1223
-
1224
- const result = convertCbToModelMessages({
1225
- messages,
1226
- includeCacheControl: false,
1227
- })
1228
-
1229
- const resultMessage = result[0] as { tags?: string[]; timeToLive?: string; providerOptions?: Record<string, ProviderWithCacheControl> }
1230
- expect(resultMessage.tags).toEqual(['custom_tag'])
1231
- expect(resultMessage.timeToLive).toBe('agentStep')
1232
- expect((resultMessage.providerOptions?.anthropic as ProviderWithCacheControl)?.someOption).toBe(
1233
- 'value',
1234
- )
1235
- })
1236
-
1237
- it('should not mutate original messages', () => {
1238
- const originalMessages: Message[] = [
1239
- systemMessage('Original'),
1240
- userMessage('User message'),
1241
- ]
1242
- const messagesCopy = cloneDeep(originalMessages)
1243
-
1244
- convertCbToModelMessages({
1245
- messages: originalMessages,
1246
- includeCacheControl: true,
1247
- })
1248
-
1249
- expect(originalMessages).toEqual(messagesCopy)
1250
- })
1251
- })
1252
- })