@alumbwe/anvil 1.0.20 → 1.0.22
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.
- package/package.json +74 -70
- package/src/agents/bundled-agents.generated.ts +4 -4
- package/src/chat.tsx +1703 -1698
- package/src/cli-args.ts +134 -134
- package/src/components/status-bar.tsx +331 -313
- package/src/hooks/helpers/send-message.ts +663 -654
- package/src/hooks/use-send-message.ts +806 -773
- package/src/index.tsx +473 -473
- package/src/state/chat-store.ts +553 -544
- package/src/utils/create-run-config.ts +121 -121
- package/src/utils/format-compact-number.ts +37 -0
- package/vendor/@anvil/common/package.json +27 -0
- package/vendor/@anvil/common/src/__tests__/agent-validation.test.ts +846 -0
- package/vendor/@anvil/common/src/__tests__/anvil-model-availability.test.ts +101 -0
- package/vendor/@anvil/common/src/__tests__/anvil-models.test.ts +1408 -0
- package/vendor/@anvil/common/src/__tests__/anvil-public-data-use-copy.test.ts +258 -0
- package/vendor/@anvil/common/src/__tests__/anvil-referral-tiers.test.ts +48 -0
- package/vendor/@anvil/common/src/__tests__/anvil-spend-ceilings.test.ts +282 -0
- package/vendor/@anvil/common/src/__tests__/anvil-trust.test.ts +526 -0
- package/vendor/@anvil/common/src/__tests__/deepseek-direct.test.ts +68 -0
- package/vendor/@anvil/common/src/__tests__/disposable-email.test.ts +132 -0
- package/vendor/@anvil/common/src/__tests__/dynamic-agent-template-schema.test.ts +422 -0
- package/vendor/@anvil/common/src/__tests__/env-ci.test.ts +167 -0
- package/vendor/@anvil/common/src/__tests__/env-process.test.ts +145 -0
- package/vendor/@anvil/common/src/__tests__/foreign-client-shipped-agents.test.ts +234 -0
- package/vendor/@anvil/common/src/__tests__/foreign-client-signals.test.ts +379 -0
- package/vendor/@anvil/common/src/__tests__/free-agents.test.ts +807 -0
- package/vendor/@anvil/common/src/__tests__/gravity-capi.test.ts +154 -0
- package/vendor/@anvil/common/src/__tests__/handlesteps-parsing.test.ts +246 -0
- package/vendor/@anvil/common/src/__tests__/kimi-k3-god-only.test.ts +77 -0
- package/vendor/@anvil/common/src/__tests__/model-config.test.ts +89 -0
- package/vendor/@anvil/common/src/__tests__/project-file-tree.test.ts +298 -0
- package/vendor/@anvil/common/src/__tests__/provisioned-model-tiers.test.ts +116 -0
- package/vendor/@anvil/common/src/__tests__/reasoning-effort.test.ts +167 -0
- package/vendor/@anvil/common/src/__tests__/reddit-capi.test.ts +132 -0
- package/vendor/@anvil/common/src/__tests__/response-ad-positions.test.ts +72 -0
- package/vendor/@anvil/common/src/__tests__/user-state.test.ts +30 -0
- package/vendor/@anvil/common/src/actions.ts +215 -0
- package/vendor/@anvil/common/src/analytics-core.ts +69 -0
- package/vendor/@anvil/common/src/analytics.ts +93 -0
- package/vendor/@anvil/common/src/api-keys/constants.ts +26 -0
- package/vendor/@anvil/common/src/browser-actions.ts +413 -0
- package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding-gate.test.ts +63 -0
- package/vendor/@anvil/common/src/constants/__tests__/anvil-onboarding.test.ts +246 -0
- package/vendor/@anvil/common/src/constants/__tests__/cf-worker-signals.test.ts +106 -0
- package/vendor/@anvil/common/src/constants/agents.ts +99 -0
- package/vendor/@anvil/common/src/constants/analytics-events.ts +435 -0
- package/vendor/@anvil/common/src/constants/anthropic.ts +73 -0
- package/vendor/@anvil/common/src/constants/anvil-data-use.ts +81 -0
- package/vendor/@anvil/common/src/constants/anvil-errors.ts +7 -0
- package/vendor/@anvil/common/src/constants/anvil-gemini-thinker.ts +21 -0
- package/vendor/@anvil/common/src/constants/anvil-model-ids.ts +11 -0
- package/vendor/@anvil/common/src/constants/anvil-models.ts +2130 -0
- package/vendor/@anvil/common/src/constants/anvil-onboarding-gate.ts +55 -0
- package/vendor/@anvil/common/src/constants/anvil-onboarding.ts +307 -0
- package/vendor/@anvil/common/src/constants/anvil-referral-tiers.ts +87 -0
- package/vendor/@anvil/common/src/constants/anvil-signup-block.ts +68 -0
- package/vendor/@anvil/common/src/constants/anvil-spend-ceilings.ts +450 -0
- package/vendor/@anvil/common/src/constants/anvil-trust.ts +976 -0
- package/vendor/@anvil/common/src/constants/auth.ts +18 -0
- package/vendor/@anvil/common/src/constants/byok.ts +2 -0
- package/vendor/@anvil/common/src/constants/cf-worker-signals.ts +132 -0
- package/vendor/@anvil/common/src/constants/composio.ts +34 -0
- package/vendor/@anvil/common/src/constants/deepseek-direct.ts +55 -0
- package/vendor/@anvil/common/src/constants/feedback.ts +13 -0
- package/vendor/@anvil/common/src/constants/foreign-client-signals.ts +272 -0
- package/vendor/@anvil/common/src/constants/free-agents.ts +777 -0
- package/vendor/@anvil/common/src/constants/gemini.ts +15 -0
- package/vendor/@anvil/common/src/constants/grant-priorities.ts +13 -0
- package/vendor/@anvil/common/src/constants/hosts.ts +6 -0
- package/vendor/@anvil/common/src/constants/images.ts +51 -0
- package/vendor/@anvil/common/src/constants/index.ts +7 -0
- package/vendor/@anvil/common/src/constants/knowledge.ts +35 -0
- package/vendor/@anvil/common/src/constants/limits.ts +23 -0
- package/vendor/@anvil/common/src/constants/model-config.ts +277 -0
- package/vendor/@anvil/common/src/constants/openrouter-attribution.ts +8 -0
- package/vendor/@anvil/common/src/constants/paths.ts +69 -0
- package/vendor/@anvil/common/src/constants/provider-routes.ts +317 -0
- package/vendor/@anvil/common/src/constants/reasoning-effort.ts +79 -0
- package/vendor/@anvil/common/src/constants/skills.ts +60 -0
- package/vendor/@anvil/common/src/constants/subscription-plans.ts +49 -0
- package/vendor/@anvil/common/src/constants/ui.ts +25 -0
- package/vendor/@anvil/common/src/env-ci.ts +36 -0
- package/vendor/@anvil/common/src/env-process.ts +95 -0
- package/vendor/@anvil/common/src/env-schema.ts +95 -0
- package/vendor/@anvil/common/src/env.ts +24 -0
- package/vendor/@anvil/common/src/gravity-capi.ts +207 -0
- package/vendor/@anvil/common/src/mcp/client.ts +233 -0
- package/vendor/@anvil/common/src/old-constants.ts +10 -0
- package/vendor/@anvil/common/src/project-file-tree.ts +355 -0
- package/vendor/@anvil/common/src/reddit-capi.ts +254 -0
- package/vendor/@anvil/common/src/schemas/feedback.ts +52 -0
- package/vendor/@anvil/common/src/schemas/logs.ts +66 -0
- package/vendor/@anvil/common/src/templates/agent-validation.ts +392 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/LICENSE +202 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/README.md +294 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/examples/01-basic-diff-reviewer.ts +17 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/examples/02-intermediate-git-committer.ts +78 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/examples/03-advanced-file-explorer.ts +73 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/my-custom-agent.ts +40 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/package.json +6 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/skills/README.md +65 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/skills/example-skill/SKILL.md +29 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/types/agent-definition.ts +497 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/types/tools.ts +444 -0
- package/vendor/@anvil/common/src/templates/initial-agents-dir/types/util-types.ts +178 -0
- package/vendor/@anvil/common/src/testing/TESTING_PATTERNS.md +351 -0
- package/vendor/@anvil/common/src/testing/anvil-offer-invariants.ts +169 -0
- package/vendor/@anvil/common/src/testing/errors.ts +33 -0
- package/vendor/@anvil/common/src/testing/fixtures/agent-runtime.ts +334 -0
- package/vendor/@anvil/common/src/testing/impl/agent-runtime.ts +6 -0
- package/vendor/@anvil/common/src/testing/index.ts +84 -0
- package/vendor/@anvil/common/src/testing/mock-modules.ts +53 -0
- package/vendor/@anvil/common/src/testing/mock-types.ts +123 -0
- package/vendor/@anvil/common/src/testing/mocks/analytics.ts +261 -0
- package/vendor/@anvil/common/src/testing/mocks/child-process.ts +93 -0
- package/vendor/@anvil/common/src/testing/mocks/crypto.ts +218 -0
- package/vendor/@anvil/common/src/testing/mocks/database.ts +337 -0
- package/vendor/@anvil/common/src/testing/mocks/fetch.ts +219 -0
- package/vendor/@anvil/common/src/testing/mocks/filesystem.ts +166 -0
- package/vendor/@anvil/common/src/testing/mocks/index.ts +101 -0
- package/vendor/@anvil/common/src/testing/mocks/logger.ts +135 -0
- package/vendor/@anvil/common/src/testing/mocks/stream.ts +313 -0
- package/vendor/@anvil/common/src/testing/mocks/timers.ts +132 -0
- package/vendor/@anvil/common/src/testing/mocks/tree-sitter.ts +127 -0
- package/vendor/@anvil/common/src/testing/setup.ts +282 -0
- package/vendor/@anvil/common/src/testing-env-ci.ts +15 -0
- package/vendor/@anvil/common/src/testing-env-process.ts +78 -0
- package/vendor/@anvil/common/src/tools/__tests__/compile-tool-definitions.test.ts +34 -0
- package/vendor/@anvil/common/src/tools/compile-tool-definitions.ts +157 -0
- package/vendor/@anvil/common/src/tools/constants.ts +117 -0
- package/vendor/@anvil/common/src/tools/list.ts +190 -0
- package/vendor/@anvil/common/src/tools/params/__tests__/coerce-to-array.test.ts +213 -0
- package/vendor/@anvil/common/src/tools/params/__tests__/read-docs.test.ts +47 -0
- package/vendor/@anvil/common/src/tools/params/__tests__/render-ui.test.ts +65 -0
- package/vendor/@anvil/common/src/tools/params/tool/__tests__/run-terminal-command-timeout.test.ts +49 -0
- package/vendor/@anvil/common/src/tools/params/tool/add-message.ts +39 -0
- package/vendor/@anvil/common/src/tools/params/tool/add-subgoal.ts +57 -0
- package/vendor/@anvil/common/src/tools/params/tool/apply-patch.ts +110 -0
- package/vendor/@anvil/common/src/tools/params/tool/ask-user.ts +181 -0
- package/vendor/@anvil/common/src/tools/params/tool/browser-logs.ts +85 -0
- package/vendor/@anvil/common/src/tools/params/tool/cloud-plan-ready.ts +94 -0
- package/vendor/@anvil/common/src/tools/params/tool/code-search.ts +159 -0
- package/vendor/@anvil/common/src/tools/params/tool/composio.ts +131 -0
- package/vendor/@anvil/common/src/tools/params/tool/create-plan.ts +80 -0
- package/vendor/@anvil/common/src/tools/params/tool/end-turn.ts +57 -0
- package/vendor/@anvil/common/src/tools/params/tool/find-files.ts +60 -0
- package/vendor/@anvil/common/src/tools/params/tool/glob.ts +80 -0
- package/vendor/@anvil/common/src/tools/params/tool/gravity-index.ts +93 -0
- package/vendor/@anvil/common/src/tools/params/tool/list-directory.ts +58 -0
- package/vendor/@anvil/common/src/tools/params/tool/lookup-agent-info.ts +37 -0
- package/vendor/@anvil/common/src/tools/params/tool/propose-str-replace.ts +103 -0
- package/vendor/@anvil/common/src/tools/params/tool/propose-write-file.ts +71 -0
- package/vendor/@anvil/common/src/tools/params/tool/read-docs.ts +90 -0
- package/vendor/@anvil/common/src/tools/params/tool/read-files.ts +109 -0
- package/vendor/@anvil/common/src/tools/params/tool/read-subtree.ts +79 -0
- package/vendor/@anvil/common/src/tools/params/tool/read-url.ts +81 -0
- package/vendor/@anvil/common/src/tools/params/tool/render-ui.ts +168 -0
- package/vendor/@anvil/common/src/tools/params/tool/run-file-change-hooks.ts +57 -0
- package/vendor/@anvil/common/src/tools/params/tool/run-terminal-command.ts +211 -0
- package/vendor/@anvil/common/src/tools/params/tool/screenshot.ts +97 -0
- package/vendor/@anvil/common/src/tools/params/tool/set-messages.ts +44 -0
- package/vendor/@anvil/common/src/tools/params/tool/set-output.ts +61 -0
- package/vendor/@anvil/common/src/tools/params/tool/skill.ts +59 -0
- package/vendor/@anvil/common/src/tools/params/tool/spawn-agent-inline.ts +56 -0
- package/vendor/@anvil/common/src/tools/params/tool/spawn-agents.ts +154 -0
- package/vendor/@anvil/common/src/tools/params/tool/str-replace.ts +107 -0
- package/vendor/@anvil/common/src/tools/params/tool/suggest-followups.ts +97 -0
- package/vendor/@anvil/common/src/tools/params/tool/task-completed.ts +61 -0
- package/vendor/@anvil/common/src/tools/params/tool/think-deeply.ts +56 -0
- package/vendor/@anvil/common/src/tools/params/tool/update-subgoal.ts +89 -0
- package/vendor/@anvil/common/src/tools/params/tool/vision-analyze.ts +82 -0
- package/vendor/@anvil/common/src/tools/params/tool/web-search.ts +73 -0
- package/vendor/@anvil/common/src/tools/params/tool/write-file.ts +71 -0
- package/vendor/@anvil/common/src/tools/params/tool/write-todos.ts +67 -0
- package/vendor/@anvil/common/src/tools/params/utils.ts +150 -0
- package/vendor/@anvil/common/src/tools/utils.ts +24 -0
- package/vendor/@anvil/common/src/types/__tests__/dynamic-agent-template.test.ts +20 -0
- package/vendor/@anvil/common/src/types/agent-template.ts +215 -0
- package/vendor/@anvil/common/src/types/anvil-session.ts +535 -0
- package/vendor/@anvil/common/src/types/anvil-streak.ts +6 -0
- package/vendor/@anvil/common/src/types/anvil-usage.ts +64 -0
- package/vendor/@anvil/common/src/types/api/agents/publish.ts +61 -0
- package/vendor/@anvil/common/src/types/bun-test.d.ts +5 -0
- package/vendor/@anvil/common/src/types/contracts/agent-runtime.ts +75 -0
- package/vendor/@anvil/common/src/types/contracts/analytics.ts +9 -0
- package/vendor/@anvil/common/src/types/contracts/bigquery.ts +55 -0
- package/vendor/@anvil/common/src/types/contracts/billing.ts +46 -0
- package/vendor/@anvil/common/src/types/contracts/client.ts +53 -0
- package/vendor/@anvil/common/src/types/contracts/database.ts +112 -0
- package/vendor/@anvil/common/src/types/contracts/env.ts +203 -0
- package/vendor/@anvil/common/src/types/contracts/llm.ts +191 -0
- package/vendor/@anvil/common/src/types/contracts/logger.ts +14 -0
- package/vendor/@anvil/common/src/types/contracts/logs.ts +34 -0
- package/vendor/@anvil/common/src/types/contracts/trace.ts +21 -0
- package/vendor/@anvil/common/src/types/dynamic-agent-template.ts +328 -0
- package/vendor/@anvil/common/src/types/filesystem.ts +10 -0
- package/vendor/@anvil/common/src/types/function-params.ts +33 -0
- package/vendor/@anvil/common/src/types/grant.ts +20 -0
- package/vendor/@anvil/common/src/types/gravity-index.ts +170 -0
- package/vendor/@anvil/common/src/types/json.ts +29 -0
- package/vendor/@anvil/common/src/types/mcp.ts +24 -0
- package/vendor/@anvil/common/src/types/messages/anvil-message.ts +58 -0
- package/vendor/@anvil/common/src/types/messages/content-part.ts +59 -0
- package/vendor/@anvil/common/src/types/messages/data-content.ts +14 -0
- package/vendor/@anvil/common/src/types/messages/provider-metadata.ts +13 -0
- package/vendor/@anvil/common/src/types/organization.ts +118 -0
- package/vendor/@anvil/common/src/types/print-mode.ts +121 -0
- package/vendor/@anvil/common/src/types/publisher.ts +67 -0
- package/vendor/@anvil/common/src/types/session-state.ts +149 -0
- package/vendor/@anvil/common/src/types/skill.ts +86 -0
- package/vendor/@anvil/common/src/types/source.ts +11 -0
- package/vendor/@anvil/common/src/types/spawn.ts +13 -0
- package/vendor/@anvil/common/src/types/subscription.ts +67 -0
- package/vendor/@anvil/common/src/types/usage.ts +16 -0
- package/vendor/@anvil/common/src/types/util.ts +3 -0
- package/vendor/@anvil/common/src/util/__tests__/ad-user-agent.test.ts +26 -0
- package/vendor/@anvil/common/src/util/__tests__/analytics-dispatcher.test.ts +122 -0
- package/vendor/@anvil/common/src/util/__tests__/analytics-log.test.ts +102 -0
- package/vendor/@anvil/common/src/util/__tests__/analytics-sampling.test.ts +143 -0
- package/vendor/@anvil/common/src/util/__tests__/anvil-streak.test.ts +202 -0
- package/vendor/@anvil/common/src/util/__tests__/anvil-usage-summary.test.ts +193 -0
- package/vendor/@anvil/common/src/util/__tests__/axiom-only-log.test.ts +140 -0
- package/vendor/@anvil/common/src/util/__tests__/client-user-agent.test.ts +148 -0
- package/vendor/@anvil/common/src/util/__tests__/engagement-tracker.test.ts +115 -0
- package/vendor/@anvil/common/src/util/__tests__/env-file-path.test.ts +57 -0
- package/vendor/@anvil/common/src/util/__tests__/error-abort.test.ts +774 -0
- package/vendor/@anvil/common/src/util/__tests__/error-api-details.test.ts +206 -0
- package/vendor/@anvil/common/src/util/__tests__/file-read-limits.test.ts +173 -0
- package/vendor/@anvil/common/src/util/__tests__/format-code-search.test.ts +60 -0
- package/vendor/@anvil/common/src/util/__tests__/log-mirror.test.ts +31 -0
- package/vendor/@anvil/common/src/util/__tests__/messages.test.ts +1252 -0
- package/vendor/@anvil/common/src/util/__tests__/partial-json-delta.test.ts +505 -0
- package/vendor/@anvil/common/src/util/__tests__/path.test.ts +22 -0
- package/vendor/@anvil/common/src/util/__tests__/project-ignore.test.ts +90 -0
- package/vendor/@anvil/common/src/util/__tests__/promise.test.ts +325 -0
- package/vendor/@anvil/common/src/util/__tests__/rate-limit.test.ts +28 -0
- package/vendor/@anvil/common/src/util/__tests__/reddit-anvil-retention.test.ts +130 -0
- package/vendor/@anvil/common/src/util/__tests__/saxy.test.ts +1008 -0
- package/vendor/@anvil/common/src/util/__tests__/split-data.test.ts +289 -0
- package/vendor/@anvil/common/src/util/__tests__/string.test.ts +239 -0
- package/vendor/@anvil/common/src/util/__tests__/thread-title.test.ts +70 -0
- package/vendor/@anvil/common/src/util/__tests__/tool-result-media-order.test.ts +108 -0
- package/vendor/@anvil/common/src/util/__tests__/ttft-histogram.test.ts +180 -0
- package/vendor/@anvil/common/src/util/__tests__/with-timeout.test.ts +24 -0
- package/vendor/@anvil/common/src/util/__tests__/zoned-time.test.ts +88 -0
- package/vendor/@anvil/common/src/util/ad-user-agent.ts +25 -0
- package/vendor/@anvil/common/src/util/agent-file-utils.ts +110 -0
- package/vendor/@anvil/common/src/util/agent-id-parsing.ts +136 -0
- package/vendor/@anvil/common/src/util/agent-name-normalization.ts +38 -0
- package/vendor/@anvil/common/src/util/agent-name-resolver.ts +86 -0
- package/vendor/@anvil/common/src/util/analytics-dispatcher.ts +82 -0
- package/vendor/@anvil/common/src/util/analytics-log.ts +78 -0
- package/vendor/@anvil/common/src/util/analytics-sampling.ts +255 -0
- package/vendor/@anvil/common/src/util/anvil-model-availability.ts +102 -0
- package/vendor/@anvil/common/src/util/anvil-privacy.ts +82 -0
- package/vendor/@anvil/common/src/util/anvil-streak-line.ts +103 -0
- package/vendor/@anvil/common/src/util/anvil-streak.ts +156 -0
- package/vendor/@anvil/common/src/util/anvil-usage-summary.ts +125 -0
- package/vendor/@anvil/common/src/util/array.ts +31 -0
- package/vendor/@anvil/common/src/util/axiom-only-log.ts +124 -0
- package/vendor/@anvil/common/src/util/cache-debug.ts +171 -0
- package/vendor/@anvil/common/src/util/client-user-agent.ts +114 -0
- package/vendor/@anvil/common/src/util/credentials.ts +26 -0
- package/vendor/@anvil/common/src/util/currency.ts +25 -0
- package/vendor/@anvil/common/src/util/dates.ts +81 -0
- package/vendor/@anvil/common/src/util/disposable-email.ts +277 -0
- package/vendor/@anvil/common/src/util/engagement-tracker.ts +129 -0
- package/vendor/@anvil/common/src/util/env-file-path.ts +40 -0
- package/vendor/@anvil/common/src/util/error.ts +562 -0
- package/vendor/@anvil/common/src/util/file-read-limits.ts +236 -0
- package/vendor/@anvil/common/src/util/file.ts +339 -0
- package/vendor/@anvil/common/src/util/format-code-search.ts +115 -0
- package/vendor/@anvil/common/src/util/lazy-response-ads.ts +92 -0
- package/vendor/@anvil/common/src/util/log-data.ts +57 -0
- package/vendor/@anvil/common/src/util/log-ingest.ts +58 -0
- package/vendor/@anvil/common/src/util/log-mirror.ts +34 -0
- package/vendor/@anvil/common/src/util/lru-cache.ts +67 -0
- package/vendor/@anvil/common/src/util/messages.ts +685 -0
- package/vendor/@anvil/common/src/util/min-heap.ts +87 -0
- package/vendor/@anvil/common/src/util/model-utils.ts +4 -0
- package/vendor/@anvil/common/src/util/object.ts +128 -0
- package/vendor/@anvil/common/src/util/partial-json-delta.ts +89 -0
- package/vendor/@anvil/common/src/util/path.ts +15 -0
- package/vendor/@anvil/common/src/util/project-ignore.ts +119 -0
- package/vendor/@anvil/common/src/util/promise.ts +73 -0
- package/vendor/@anvil/common/src/util/random.ts +15 -0
- package/vendor/@anvil/common/src/util/rate-limit.ts +56 -0
- package/vendor/@anvil/common/src/util/reddit-anvil-retention.ts +60 -0
- package/vendor/@anvil/common/src/util/reddit-capi-events.ts +36 -0
- package/vendor/@anvil/common/src/util/response-ad-positions.ts +54 -0
- package/vendor/@anvil/common/src/util/saxy.ts +741 -0
- package/vendor/@anvil/common/src/util/skills.test.ts +44 -0
- package/vendor/@anvil/common/src/util/skills.ts +36 -0
- package/vendor/@anvil/common/src/util/split-data.ts +259 -0
- package/vendor/@anvil/common/src/util/stop-sequence.ts +61 -0
- package/vendor/@anvil/common/src/util/string.ts +426 -0
- package/vendor/@anvil/common/src/util/system-info.ts +53 -0
- package/vendor/@anvil/common/src/util/thread-title.ts +46 -0
- package/vendor/@anvil/common/src/util/ttft-histogram.ts +73 -0
- package/vendor/@anvil/common/src/util/xml-parser.ts +27 -0
- package/vendor/@anvil/common/src/util/xml.ts +17 -0
- package/vendor/@anvil/common/src/util/zod-schema.ts +25 -0
- package/vendor/@anvil/common/src/util/zoned-time.ts +136 -0
- package/vendor/@anvil/common/src/utils/ask-user-bridge.ts +44 -0
- package/vendor/@anvil/sdk/dist/index.cjs +52297 -0
- package/vendor/@anvil/sdk/dist/index.cjs.map +440 -0
- package/vendor/@anvil/sdk/dist/index.d.ts +4277 -0
- package/vendor/@anvil/sdk/dist/index.mjs +52290 -0
- package/vendor/@anvil/sdk/dist/index.mjs.map +440 -0
- package/vendor/@anvil/sdk/dist/vendor/ai.cjs +30886 -0
- package/vendor/@anvil/sdk/dist/vendor/ripgrep/arm64-darwin/rg +0 -0
- package/vendor/@anvil/sdk/dist/vendor/ripgrep/arm64-linux/rg +0 -0
- package/vendor/@anvil/sdk/dist/vendor/ripgrep/x64-darwin/rg +0 -0
- package/vendor/@anvil/sdk/dist/vendor/ripgrep/x64-linux/rg +0 -0
- package/vendor/@anvil/sdk/dist/vendor/ripgrep/x64-win32/rg.exe +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-c-sharp.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-cpp.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-go.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-java.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-javascript.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-python.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-ruby.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-rust.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-tsx.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter-typescript.wasm +0 -0
- package/vendor/@anvil/sdk/dist/wasm/tree-sitter.wasm +0 -0
- package/vendor/@anvil/sdk/package.json +21 -0
|
@@ -0,0 +1,1252 @@
|
|
|
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
|
+
})
|