@bastani/atomic-ai 0.0.0-stage → 0.9.88
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/CHANGELOG.md +1629 -0
- package/LICENSE +22 -0
- package/NOTICE.md +16 -0
- package/README.md +165 -2
- package/dist/api/anthropic-assistant-content.d.ts +24 -0
- package/dist/api/anthropic-assistant-content.d.ts.map +1 -0
- package/dist/api/anthropic-assistant-content.js +99 -0
- package/dist/api/anthropic-assistant-content.js.map +1 -0
- package/dist/api/anthropic-messages.d.ts +71 -0
- package/dist/api/anthropic-messages.d.ts.map +1 -0
- package/dist/api/anthropic-messages.js +1814 -0
- package/dist/api/anthropic-messages.js.map +1 -0
- package/dist/api/anthropic-messages.lazy.d.ts +3 -0
- package/dist/api/anthropic-messages.lazy.d.ts.map +1 -0
- package/dist/api/anthropic-messages.lazy.js +3 -0
- package/dist/api/anthropic-messages.lazy.js.map +1 -0
- package/dist/api/azure-openai-config.d.ts +14 -0
- package/dist/api/azure-openai-config.d.ts.map +1 -0
- package/dist/api/azure-openai-config.js +61 -0
- package/dist/api/azure-openai-config.js.map +1 -0
- package/dist/api/azure-openai-responses.d.ts +14 -0
- package/dist/api/azure-openai-responses.d.ts.map +1 -0
- package/dist/api/azure-openai-responses.js +213 -0
- package/dist/api/azure-openai-responses.js.map +1 -0
- package/dist/api/azure-openai-responses.lazy.d.ts +3 -0
- package/dist/api/azure-openai-responses.lazy.d.ts.map +1 -0
- package/dist/api/azure-openai-responses.lazy.js +3 -0
- package/dist/api/azure-openai-responses.lazy.js.map +1 -0
- package/dist/api/bedrock-converse-stream.d.ts +38 -0
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -0
- package/dist/api/bedrock-converse-stream.js +1301 -0
- package/dist/api/bedrock-converse-stream.js.map +1 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts +16 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -0
- package/dist/api/bedrock-converse-stream.lazy.js +43 -0
- package/dist/api/bedrock-converse-stream.lazy.js.map +1 -0
- package/dist/api/classifier-shared.d.ts +11 -0
- package/dist/api/classifier-shared.d.ts.map +1 -0
- package/dist/api/classifier-shared.js +90 -0
- package/dist/api/classifier-shared.js.map +1 -0
- package/dist/api/cloudflare-gateway-binding.d.ts +58 -0
- package/dist/api/cloudflare-gateway-binding.d.ts.map +1 -0
- package/dist/api/cloudflare-gateway-binding.js +33 -0
- package/dist/api/cloudflare-gateway-binding.js.map +1 -0
- package/dist/api/cloudflare-workers-ai-system-one.d.ts +4 -0
- package/dist/api/cloudflare-workers-ai-system-one.d.ts.map +1 -0
- package/dist/api/cloudflare-workers-ai-system-one.js +57 -0
- package/dist/api/cloudflare-workers-ai-system-one.js.map +1 -0
- package/dist/api/cloudflare-workers-ai-system-one.lazy.d.ts +3 -0
- package/dist/api/cloudflare-workers-ai-system-one.lazy.d.ts.map +1 -0
- package/dist/api/cloudflare-workers-ai-system-one.lazy.js +4 -0
- package/dist/api/cloudflare-workers-ai-system-one.lazy.js.map +1 -0
- package/dist/api/cloudflare.d.ts +11 -0
- package/dist/api/cloudflare.d.ts.map +1 -0
- package/dist/api/cloudflare.js +11 -0
- package/dist/api/cloudflare.js.map +1 -0
- package/dist/api/constrained-sampling.d.ts +38 -0
- package/dist/api/constrained-sampling.d.ts.map +1 -0
- package/dist/api/constrained-sampling.js +271 -0
- package/dist/api/constrained-sampling.js.map +1 -0
- package/dist/api/github-copilot-headers.d.ts +40 -0
- package/dist/api/github-copilot-headers.d.ts.map +1 -0
- package/dist/api/github-copilot-headers.js +76 -0
- package/dist/api/github-copilot-headers.js.map +1 -0
- package/dist/api/google-generative-ai.d.ts +15 -0
- package/dist/api/google-generative-ai.d.ts.map +1 -0
- package/dist/api/google-generative-ai.js +376 -0
- package/dist/api/google-generative-ai.js.map +1 -0
- package/dist/api/google-generative-ai.lazy.d.ts +3 -0
- package/dist/api/google-generative-ai.lazy.d.ts.map +1 -0
- package/dist/api/google-generative-ai.lazy.js +3 -0
- package/dist/api/google-generative-ai.lazy.js.map +1 -0
- package/dist/api/google-shared.d.ts +103 -0
- package/dist/api/google-shared.d.ts.map +1 -0
- package/dist/api/google-shared.js +536 -0
- package/dist/api/google-shared.js.map +1 -0
- package/dist/api/google-vertex.d.ts +17 -0
- package/dist/api/google-vertex.d.ts.map +1 -0
- package/dist/api/google-vertex.js +440 -0
- package/dist/api/google-vertex.js.map +1 -0
- package/dist/api/google-vertex.lazy.d.ts +3 -0
- package/dist/api/google-vertex.lazy.d.ts.map +1 -0
- package/dist/api/google-vertex.lazy.js +3 -0
- package/dist/api/google-vertex.lazy.js.map +1 -0
- package/dist/api/lazy.d.ts +19 -0
- package/dist/api/lazy.d.ts.map +1 -0
- package/dist/api/lazy.js +72 -0
- package/dist/api/lazy.js.map +1 -0
- package/dist/api/llama-cpp-classify.d.ts +33 -0
- package/dist/api/llama-cpp-classify.d.ts.map +1 -0
- package/dist/api/llama-cpp-classify.js +368 -0
- package/dist/api/llama-cpp-classify.js.map +1 -0
- package/dist/api/llama-cpp-classify.lazy.d.ts +3 -0
- package/dist/api/llama-cpp-classify.lazy.d.ts.map +1 -0
- package/dist/api/llama-cpp-classify.lazy.js +4 -0
- package/dist/api/llama-cpp-classify.lazy.js.map +1 -0
- package/dist/api/mistral-conversations.d.ts +25 -0
- package/dist/api/mistral-conversations.d.ts.map +1 -0
- package/dist/api/mistral-conversations.js +779 -0
- package/dist/api/mistral-conversations.js.map +1 -0
- package/dist/api/mistral-conversations.lazy.d.ts +3 -0
- package/dist/api/mistral-conversations.lazy.d.ts.map +1 -0
- package/dist/api/mistral-conversations.lazy.js +3 -0
- package/dist/api/mistral-conversations.lazy.js.map +1 -0
- package/dist/api/openai-codex-responses.d.ts +32 -0
- package/dist/api/openai-codex-responses.d.ts.map +1 -0
- package/dist/api/openai-codex-responses.js +1357 -0
- package/dist/api/openai-codex-responses.js.map +1 -0
- package/dist/api/openai-codex-responses.lazy.d.ts +3 -0
- package/dist/api/openai-codex-responses.lazy.d.ts.map +1 -0
- package/dist/api/openai-codex-responses.lazy.js +3 -0
- package/dist/api/openai-codex-responses.lazy.js.map +1 -0
- package/dist/api/openai-completions.d.ts +30 -0
- package/dist/api/openai-completions.d.ts.map +1 -0
- package/dist/api/openai-completions.js +1529 -0
- package/dist/api/openai-completions.js.map +1 -0
- package/dist/api/openai-completions.lazy.d.ts +3 -0
- package/dist/api/openai-completions.lazy.d.ts.map +1 -0
- package/dist/api/openai-completions.lazy.js +3 -0
- package/dist/api/openai-completions.lazy.js.map +1 -0
- package/dist/api/openai-decisions.d.ts +3 -0
- package/dist/api/openai-decisions.d.ts.map +1 -0
- package/dist/api/openai-decisions.js +157 -0
- package/dist/api/openai-decisions.js.map +1 -0
- package/dist/api/openai-decisions.lazy.d.ts +3 -0
- package/dist/api/openai-decisions.lazy.d.ts.map +1 -0
- package/dist/api/openai-decisions.lazy.js +4 -0
- package/dist/api/openai-decisions.lazy.js.map +1 -0
- package/dist/api/openai-images-options.d.ts +120 -0
- package/dist/api/openai-images-options.d.ts.map +1 -0
- package/dist/api/openai-images-options.js +112 -0
- package/dist/api/openai-images-options.js.map +1 -0
- package/dist/api/openai-images.d.ts +9 -0
- package/dist/api/openai-images.d.ts.map +1 -0
- package/dist/api/openai-images.js +317 -0
- package/dist/api/openai-images.js.map +1 -0
- package/dist/api/openai-images.lazy.d.ts +3 -0
- package/dist/api/openai-images.lazy.d.ts.map +1 -0
- package/dist/api/openai-images.lazy.js +4 -0
- package/dist/api/openai-images.lazy.js.map +1 -0
- package/dist/api/openai-prompt-cache.d.ts +3 -0
- package/dist/api/openai-prompt-cache.d.ts.map +1 -0
- package/dist/api/openai-prompt-cache.js +10 -0
- package/dist/api/openai-prompt-cache.js.map +1 -0
- package/dist/api/openai-responses-shared.d.ts +133 -0
- package/dist/api/openai-responses-shared.d.ts.map +1 -0
- package/dist/api/openai-responses-shared.js +970 -0
- package/dist/api/openai-responses-shared.js.map +1 -0
- package/dist/api/openai-responses.d.ts +15 -0
- package/dist/api/openai-responses.d.ts.map +1 -0
- package/dist/api/openai-responses.js +444 -0
- package/dist/api/openai-responses.js.map +1 -0
- package/dist/api/openai-responses.lazy.d.ts +3 -0
- package/dist/api/openai-responses.lazy.d.ts.map +1 -0
- package/dist/api/openai-responses.lazy.js +3 -0
- package/dist/api/openai-responses.lazy.js.map +1 -0
- package/dist/api/openai-retry-hint-client.d.ts +10 -0
- package/dist/api/openai-retry-hint-client.d.ts.map +1 -0
- package/dist/api/openai-retry-hint-client.js +17 -0
- package/dist/api/openai-retry-hint-client.js.map +1 -0
- package/dist/api/openrouter-images.d.ts +4 -0
- package/dist/api/openrouter-images.d.ts.map +1 -0
- package/dist/api/openrouter-images.js +135 -0
- package/dist/api/openrouter-images.js.map +1 -0
- package/dist/api/openrouter-images.lazy.d.ts +3 -0
- package/dist/api/openrouter-images.lazy.d.ts.map +1 -0
- package/dist/api/openrouter-images.lazy.js +4 -0
- package/dist/api/openrouter-images.lazy.js.map +1 -0
- package/dist/api/pi-messages.d.ts +101 -0
- package/dist/api/pi-messages.d.ts.map +1 -0
- package/dist/api/pi-messages.js +338 -0
- package/dist/api/pi-messages.js.map +1 -0
- package/dist/api/pi-messages.lazy.d.ts +3 -0
- package/dist/api/pi-messages.lazy.d.ts.map +1 -0
- package/dist/api/pi-messages.lazy.js +3 -0
- package/dist/api/pi-messages.lazy.js.map +1 -0
- package/dist/api/simple-options.d.ts +17 -0
- package/dist/api/simple-options.d.ts.map +1 -0
- package/dist/api/simple-options.js +89 -0
- package/dist/api/simple-options.js.map +1 -0
- package/dist/api/strict-tool-limits.d.ts +13 -0
- package/dist/api/strict-tool-limits.d.ts.map +1 -0
- package/dist/api/strict-tool-limits.js +50 -0
- package/dist/api/strict-tool-limits.js.map +1 -0
- package/dist/api/system-one-shared.d.ts +48 -0
- package/dist/api/system-one-shared.d.ts.map +1 -0
- package/dist/api/system-one-shared.js +175 -0
- package/dist/api/system-one-shared.js.map +1 -0
- package/dist/api/transform-messages.d.ts +27 -0
- package/dist/api/transform-messages.d.ts.map +1 -0
- package/dist/api/transform-messages.js +353 -0
- package/dist/api/transform-messages.js.map +1 -0
- package/dist/api/typesafe-system-one.d.ts +4 -0
- package/dist/api/typesafe-system-one.d.ts.map +1 -0
- package/dist/api/typesafe-system-one.js +29 -0
- package/dist/api/typesafe-system-one.js.map +1 -0
- package/dist/api/typesafe-system-one.lazy.d.ts +3 -0
- package/dist/api/typesafe-system-one.lazy.d.ts.map +1 -0
- package/dist/api/typesafe-system-one.lazy.js +4 -0
- package/dist/api/typesafe-system-one.lazy.js.map +1 -0
- package/dist/auth/context.d.ts +7 -0
- package/dist/auth/context.d.ts.map +1 -0
- package/dist/auth/context.js +42 -0
- package/dist/auth/context.js.map +1 -0
- package/dist/auth/credential-store.d.ts +17 -0
- package/dist/auth/credential-store.d.ts.map +1 -0
- package/dist/auth/credential-store.js +52 -0
- package/dist/auth/credential-store.js.map +1 -0
- package/dist/auth/helpers.d.ts +30 -0
- package/dist/auth/helpers.d.ts.map +1 -0
- package/dist/auth/helpers.js +66 -0
- package/dist/auth/helpers.js.map +1 -0
- package/dist/auth/oauth/anthropic.d.ts +9 -0
- package/dist/auth/oauth/anthropic.d.ts.map +1 -0
- package/dist/auth/oauth/anthropic.js +266 -0
- package/dist/auth/oauth/anthropic.js.map +1 -0
- package/dist/auth/oauth/callback-server.d.ts +31 -0
- package/dist/auth/oauth/callback-server.d.ts.map +1 -0
- package/dist/auth/oauth/callback-server.js +138 -0
- package/dist/auth/oauth/callback-server.js.map +1 -0
- package/dist/auth/oauth/device-code.d.ts +24 -0
- package/dist/auth/oauth/device-code.d.ts.map +1 -0
- package/dist/auth/oauth/device-code.js +69 -0
- package/dist/auth/oauth/device-code.js.map +1 -0
- package/dist/auth/oauth/github-copilot.d.ts +6 -0
- package/dist/auth/oauth/github-copilot.d.ts.map +1 -0
- package/dist/auth/oauth/github-copilot.js +352 -0
- package/dist/auth/oauth/github-copilot.js.map +1 -0
- package/dist/auth/oauth/kimi-coding.d.ts +10 -0
- package/dist/auth/oauth/kimi-coding.d.ts.map +1 -0
- package/dist/auth/oauth/kimi-coding.js +238 -0
- package/dist/auth/oauth/kimi-coding.js.map +1 -0
- package/dist/auth/oauth/load.d.ts +23 -0
- package/dist/auth/oauth/load.d.ts.map +1 -0
- package/dist/auth/oauth/load.js +67 -0
- package/dist/auth/oauth/load.js.map +1 -0
- package/dist/auth/oauth/meta.d.ts +17 -0
- package/dist/auth/oauth/meta.d.ts.map +1 -0
- package/dist/auth/oauth/meta.js +182 -0
- package/dist/auth/oauth/meta.js.map +1 -0
- package/dist/auth/oauth/oauth-page.d.ts +3 -0
- package/dist/auth/oauth/oauth-page.d.ts.map +1 -0
- package/dist/auth/oauth/oauth-page.js +105 -0
- package/dist/auth/oauth/oauth-page.js.map +1 -0
- package/dist/auth/oauth/openai-chatgpt.d.ts +3 -0
- package/dist/auth/oauth/openai-chatgpt.d.ts.map +1 -0
- package/dist/auth/oauth/openai-chatgpt.js +271 -0
- package/dist/auth/oauth/openai-chatgpt.js.map +1 -0
- package/dist/auth/oauth/openai-codex.d.ts +9 -0
- package/dist/auth/oauth/openai-codex.d.ts.map +1 -0
- package/dist/auth/oauth/openai-codex.js +360 -0
- package/dist/auth/oauth/openai-codex.js.map +1 -0
- package/dist/auth/oauth/openrouter.d.ts +15 -0
- package/dist/auth/oauth/openrouter.d.ts.map +1 -0
- package/dist/auth/oauth/openrouter.js +159 -0
- package/dist/auth/oauth/openrouter.js.map +1 -0
- package/dist/auth/oauth/pkce.d.ts +13 -0
- package/dist/auth/oauth/pkce.d.ts.map +1 -0
- package/dist/auth/oauth/pkce.js +31 -0
- package/dist/auth/oauth/pkce.js.map +1 -0
- package/dist/auth/oauth/token-response.d.ts +25 -0
- package/dist/auth/oauth/token-response.d.ts.map +1 -0
- package/dist/auth/oauth/token-response.js +43 -0
- package/dist/auth/oauth/token-response.js.map +1 -0
- package/dist/auth/oauth/trusted-url.d.ts +7 -0
- package/dist/auth/oauth/trusted-url.d.ts.map +1 -0
- package/dist/auth/oauth/trusted-url.js +12 -0
- package/dist/auth/oauth/trusted-url.js.map +1 -0
- package/dist/auth/oauth/xai.d.ts +6 -0
- package/dist/auth/oauth/xai.d.ts.map +1 -0
- package/dist/auth/oauth/xai.js +193 -0
- package/dist/auth/oauth/xai.js.map +1 -0
- package/dist/auth/oauth-refresh-errors.d.ts +46 -0
- package/dist/auth/oauth-refresh-errors.d.ts.map +1 -0
- package/dist/auth/oauth-refresh-errors.js +150 -0
- package/dist/auth/oauth-refresh-errors.js.map +1 -0
- package/dist/auth/resolve.d.ts +62 -0
- package/dist/auth/resolve.d.ts.map +1 -0
- package/dist/auth/resolve.js +357 -0
- package/dist/auth/resolve.js.map +1 -0
- package/dist/auth/types.d.ts +267 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +2 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/bedrock-provider.d.ts +5 -0
- package/dist/bedrock-provider.d.ts.map +1 -0
- package/dist/bedrock-provider.js +6 -0
- package/dist/bedrock-provider.js.map +1 -0
- package/dist/bun-oauth.d.ts +3 -0
- package/dist/bun-oauth.d.ts.map +1 -0
- package/dist/bun-oauth.js +23 -0
- package/dist/bun-oauth.js.map +1 -0
- package/dist/cache-warming.d.ts +53 -0
- package/dist/cache-warming.d.ts.map +1 -0
- package/dist/cache-warming.js +82 -0
- package/dist/cache-warming.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +118 -0
- package/dist/cli.js.map +1 -0
- package/dist/compat/extension-oauth-types.d.ts +43 -0
- package/dist/compat/extension-oauth-types.d.ts.map +1 -0
- package/dist/compat/extension-oauth-types.js +2 -0
- package/dist/compat/extension-oauth-types.js.map +1 -0
- package/dist/compat.d.ts +67 -0
- package/dist/compat.d.ts.map +1 -0
- package/dist/compat.js +202 -0
- package/dist/compat.js.map +1 -0
- package/dist/decision-models.d.ts +21 -0
- package/dist/decision-models.d.ts.map +1 -0
- package/dist/decision-models.generated.d.ts +3 -0
- package/dist/decision-models.generated.d.ts.map +1 -0
- package/dist/decision-models.generated.js +47 -0
- package/dist/decision-models.generated.js.map +1 -0
- package/dist/decision-models.js +8 -0
- package/dist/decision-models.js.map +1 -0
- package/dist/env-api-keys.d.ts +28 -0
- package/dist/env-api-keys.d.ts.map +1 -0
- package/dist/env-api-keys.js +185 -0
- package/dist/env-api-keys.js.map +1 -0
- package/dist/image-generation-error.d.ts +210 -0
- package/dist/image-generation-error.d.ts.map +1 -0
- package/dist/image-generation-error.js +442 -0
- package/dist/image-generation-error.js.map +1 -0
- package/dist/image-models.d.ts +21 -0
- package/dist/image-models.d.ts.map +1 -0
- package/dist/image-models.js +24 -0
- package/dist/image-models.js.map +1 -0
- package/dist/images-api-registry.d.ts +14 -0
- package/dist/images-api-registry.d.ts.map +1 -0
- package/dist/images-api-registry.js +22 -0
- package/dist/images-api-registry.js.map +1 -0
- package/dist/images.d.ts +9 -0
- package/dist/images.d.ts.map +1 -0
- package/dist/images.js +19 -0
- package/dist/images.js.map +1 -0
- package/dist/index.d.ts +51 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/lazy-bedrock-provider.d.ts +10 -0
- package/dist/lazy-bedrock-provider.d.ts.map +1 -0
- package/dist/lazy-bedrock-provider.js +13 -0
- package/dist/lazy-bedrock-provider.js.map +1 -0
- package/dist/lazy-oauth.d.ts +10 -0
- package/dist/lazy-oauth.d.ts.map +1 -0
- package/dist/lazy-oauth.js +22 -0
- package/dist/lazy-oauth.js.map +1 -0
- package/dist/legacy-api-aliases.d.ts +42 -0
- package/dist/legacy-api-aliases.d.ts.map +1 -0
- package/dist/legacy-api-aliases.js +49 -0
- package/dist/legacy-api-aliases.js.map +1 -0
- package/dist/model-catalog.d.ts +33 -0
- package/dist/model-catalog.d.ts.map +1 -0
- package/dist/model-catalog.js +46 -0
- package/dist/model-catalog.js.map +1 -0
- package/dist/models-store.d.ts +30 -0
- package/dist/models-store.d.ts.map +1 -0
- package/dist/models-store.js +17 -0
- package/dist/models-store.js.map +1 -0
- package/dist/models.d.ts +297 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.generated.d.ts +183 -0
- package/dist/models.generated.d.ts.map +1 -0
- package/dist/models.generated.js +185 -0
- package/dist/models.generated.js.map +1 -0
- package/dist/models.js +817 -0
- package/dist/models.js.map +1 -0
- package/dist/oauth.d.ts +3 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/provider-id.d.ts +2 -0
- package/dist/provider-id.d.ts.map +1 -0
- package/dist/provider-id.js +4 -0
- package/dist/provider-id.js.map +1 -0
- package/dist/providers/all.d.ts +41 -0
- package/dist/providers/all.d.ts.map +1 -0
- package/dist/providers/all.js +145 -0
- package/dist/providers/all.js.map +1 -0
- package/dist/providers/amazon-bedrock.d.ts +3 -0
- package/dist/providers/amazon-bedrock.d.ts.map +1 -0
- package/dist/providers/amazon-bedrock.js +93 -0
- package/dist/providers/amazon-bedrock.js.map +1 -0
- package/dist/providers/amazon-bedrock.models.d.ts +5 -0
- package/dist/providers/amazon-bedrock.models.d.ts.map +1 -0
- package/dist/providers/amazon-bedrock.models.js +8 -0
- package/dist/providers/amazon-bedrock.models.js.map +1 -0
- package/dist/providers/ant-ling.d.ts +3 -0
- package/dist/providers/ant-ling.d.ts.map +1 -0
- package/dist/providers/ant-ling.js +15 -0
- package/dist/providers/ant-ling.js.map +1 -0
- package/dist/providers/ant-ling.models.d.ts +5 -0
- package/dist/providers/ant-ling.models.d.ts.map +1 -0
- package/dist/providers/ant-ling.models.js +8 -0
- package/dist/providers/ant-ling.models.js.map +1 -0
- package/dist/providers/anthropic.d.ts +4 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +87 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/anthropic.models.d.ts +5 -0
- package/dist/providers/anthropic.models.d.ts.map +1 -0
- package/dist/providers/anthropic.models.js +8 -0
- package/dist/providers/anthropic.models.js.map +1 -0
- package/dist/providers/azure-openai-responses.d.ts +3 -0
- package/dist/providers/azure-openai-responses.d.ts.map +1 -0
- package/dist/providers/azure-openai-responses.js +3 -0
- package/dist/providers/azure-openai-responses.js.map +1 -0
- package/dist/providers/azure-openai-responses.models.d.ts +3 -0
- package/dist/providers/azure-openai-responses.models.d.ts.map +1 -0
- package/dist/providers/azure-openai-responses.models.js +5 -0
- package/dist/providers/azure-openai-responses.models.js.map +1 -0
- package/dist/providers/azure.d.ts +3 -0
- package/dist/providers/azure.d.ts.map +1 -0
- package/dist/providers/azure.js +41 -0
- package/dist/providers/azure.js.map +1 -0
- package/dist/providers/azure.models.d.ts +5 -0
- package/dist/providers/azure.models.d.ts.map +1 -0
- package/dist/providers/azure.models.js +8 -0
- package/dist/providers/azure.models.js.map +1 -0
- package/dist/providers/baseten.d.ts +3 -0
- package/dist/providers/baseten.d.ts.map +1 -0
- package/dist/providers/baseten.js +15 -0
- package/dist/providers/baseten.js.map +1 -0
- package/dist/providers/baseten.models.d.ts +5 -0
- package/dist/providers/baseten.models.d.ts.map +1 -0
- package/dist/providers/baseten.models.js +8 -0
- package/dist/providers/baseten.models.js.map +1 -0
- package/dist/providers/cerebras.d.ts +3 -0
- package/dist/providers/cerebras.d.ts.map +1 -0
- package/dist/providers/cerebras.js +15 -0
- package/dist/providers/cerebras.js.map +1 -0
- package/dist/providers/cerebras.models.d.ts +5 -0
- package/dist/providers/cerebras.models.d.ts.map +1 -0
- package/dist/providers/cerebras.models.js +8 -0
- package/dist/providers/cerebras.models.js.map +1 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +5 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts.map +1 -0
- package/dist/providers/cloudflare-ai-gateway.js +21 -0
- package/dist/providers/cloudflare-ai-gateway.js.map +1 -0
- package/dist/providers/cloudflare-ai-gateway.models.d.ts +5 -0
- package/dist/providers/cloudflare-ai-gateway.models.d.ts.map +1 -0
- package/dist/providers/cloudflare-ai-gateway.models.js +8 -0
- package/dist/providers/cloudflare-ai-gateway.models.js.map +1 -0
- package/dist/providers/cloudflare-auth.d.ts +4 -0
- package/dist/providers/cloudflare-auth.d.ts.map +1 -0
- package/dist/providers/cloudflare-auth.js +86 -0
- package/dist/providers/cloudflare-auth.js.map +1 -0
- package/dist/providers/cloudflare-stream.d.ts +12 -0
- package/dist/providers/cloudflare-stream.d.ts.map +1 -0
- package/dist/providers/cloudflare-stream.js +27 -0
- package/dist/providers/cloudflare-stream.js.map +1 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +3 -0
- package/dist/providers/cloudflare-workers-ai.d.ts.map +1 -0
- package/dist/providers/cloudflare-workers-ai.js +22 -0
- package/dist/providers/cloudflare-workers-ai.js.map +1 -0
- package/dist/providers/cloudflare-workers-ai.models.d.ts +5 -0
- package/dist/providers/cloudflare-workers-ai.models.d.ts.map +1 -0
- package/dist/providers/cloudflare-workers-ai.models.js +8 -0
- package/dist/providers/cloudflare-workers-ai.models.js.map +1 -0
- package/dist/providers/compat-schema.d.ts +549 -0
- package/dist/providers/compat-schema.d.ts.map +1 -0
- package/dist/providers/compat-schema.js +452 -0
- package/dist/providers/compat-schema.js.map +1 -0
- package/dist/providers/data/.manifest.json +1 -0
- package/dist/providers/data/amazon-bedrock.json +1 -0
- package/dist/providers/data/ant-ling.json +1 -0
- package/dist/providers/data/anthropic.json +1 -0
- package/dist/providers/data/azure.json +1 -0
- package/dist/providers/data/baseten.json +1 -0
- package/dist/providers/data/cerebras.json +1 -0
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -0
- package/dist/providers/data/cloudflare-workers-ai.json +1 -0
- package/dist/providers/data/deepseek.json +1 -0
- package/dist/providers/data/fireworks.json +1 -0
- package/dist/providers/data/github-copilot.json +1 -0
- package/dist/providers/data/google-vertex.json +1 -0
- package/dist/providers/data/google.json +1 -0
- package/dist/providers/data/groq.json +1 -0
- package/dist/providers/data/huggingface.json +1 -0
- package/dist/providers/data/kimi-coding.json +1 -0
- package/dist/providers/data/meta.json +1 -0
- package/dist/providers/data/minimax-cn.json +1 -0
- package/dist/providers/data/minimax.json +1 -0
- package/dist/providers/data/mistral.json +1 -0
- package/dist/providers/data/moonshotai-cn.json +1 -0
- package/dist/providers/data/moonshotai.json +1 -0
- package/dist/providers/data/nvidia.json +1 -0
- package/dist/providers/data/openai-codex.json +1 -0
- package/dist/providers/data/openai-decisions.json +1 -0
- package/dist/providers/data/openai-images.json +1 -0
- package/dist/providers/data/openai.json +1 -0
- package/dist/providers/data/opencode-go.json +1 -0
- package/dist/providers/data/opencode.json +1 -0
- package/dist/providers/data/openrouter.json +1 -0
- package/dist/providers/data/qwen-token-plan-cn.json +1 -0
- package/dist/providers/data/qwen-token-plan-individual-cn.json +1 -0
- package/dist/providers/data/qwen-token-plan-individual.json +1 -0
- package/dist/providers/data/qwen-token-plan.json +1 -0
- package/dist/providers/data/together.json +1 -0
- package/dist/providers/data/typesafe.json +1 -0
- package/dist/providers/data/vercel-ai-gateway.json +1 -0
- package/dist/providers/data/xai.json +1 -0
- package/dist/providers/data/xiaomi-token-plan-ams.json +1 -0
- package/dist/providers/data/xiaomi-token-plan-cn.json +1 -0
- package/dist/providers/data/xiaomi-token-plan-sgp.json +1 -0
- package/dist/providers/data/xiaomi.json +1 -0
- package/dist/providers/data/zai-coding-cn.json +1 -0
- package/dist/providers/data/zai.json +1 -0
- package/dist/providers/deepseek.d.ts +3 -0
- package/dist/providers/deepseek.d.ts.map +1 -0
- package/dist/providers/deepseek.js +15 -0
- package/dist/providers/deepseek.js.map +1 -0
- package/dist/providers/deepseek.models.d.ts +5 -0
- package/dist/providers/deepseek.models.d.ts.map +1 -0
- package/dist/providers/deepseek.models.js +8 -0
- package/dist/providers/deepseek.models.js.map +1 -0
- package/dist/providers/faux.d.ts +103 -0
- package/dist/providers/faux.d.ts.map +1 -0
- package/dist/providers/faux.js +521 -0
- package/dist/providers/faux.js.map +1 -0
- package/dist/providers/fireworks.d.ts +3 -0
- package/dist/providers/fireworks.d.ts.map +1 -0
- package/dist/providers/fireworks.js +19 -0
- package/dist/providers/fireworks.js.map +1 -0
- package/dist/providers/fireworks.models.d.ts +5 -0
- package/dist/providers/fireworks.models.d.ts.map +1 -0
- package/dist/providers/fireworks.models.js +8 -0
- package/dist/providers/fireworks.models.js.map +1 -0
- package/dist/providers/github-copilot-account.d.ts +18 -0
- package/dist/providers/github-copilot-account.d.ts.map +1 -0
- package/dist/providers/github-copilot-account.js +57 -0
- package/dist/providers/github-copilot-account.js.map +1 -0
- package/dist/providers/github-copilot-capabilities.d.ts +67 -0
- package/dist/providers/github-copilot-capabilities.d.ts.map +1 -0
- package/dist/providers/github-copilot-capabilities.js +81 -0
- package/dist/providers/github-copilot-capabilities.js.map +1 -0
- package/dist/providers/github-copilot-env.d.ts +41 -0
- package/dist/providers/github-copilot-env.d.ts.map +1 -0
- package/dist/providers/github-copilot-env.js +108 -0
- package/dist/providers/github-copilot-env.js.map +1 -0
- package/dist/providers/github-copilot-errors.d.ts +255 -0
- package/dist/providers/github-copilot-errors.d.ts.map +1 -0
- package/dist/providers/github-copilot-errors.js +764 -0
- package/dist/providers/github-copilot-errors.js.map +1 -0
- package/dist/providers/github-copilot-stream-attempt.d.ts +27 -0
- package/dist/providers/github-copilot-stream-attempt.d.ts.map +1 -0
- package/dist/providers/github-copilot-stream-attempt.js +45 -0
- package/dist/providers/github-copilot-stream-attempt.js.map +1 -0
- package/dist/providers/github-copilot.d.ts +40 -0
- package/dist/providers/github-copilot.d.ts.map +1 -0
- package/dist/providers/github-copilot.js +363 -0
- package/dist/providers/github-copilot.js.map +1 -0
- package/dist/providers/github-copilot.models.d.ts +5 -0
- package/dist/providers/github-copilot.models.d.ts.map +1 -0
- package/dist/providers/github-copilot.models.js +8 -0
- package/dist/providers/github-copilot.models.js.map +1 -0
- package/dist/providers/google-vertex.d.ts +3 -0
- package/dist/providers/google-vertex.d.ts.map +1 -0
- package/dist/providers/google-vertex.js +94 -0
- package/dist/providers/google-vertex.js.map +1 -0
- package/dist/providers/google-vertex.models.d.ts +5 -0
- package/dist/providers/google-vertex.models.d.ts.map +1 -0
- package/dist/providers/google-vertex.models.js +8 -0
- package/dist/providers/google-vertex.models.js.map +1 -0
- package/dist/providers/google.d.ts +3 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +15 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/google.models.d.ts +5 -0
- package/dist/providers/google.models.d.ts.map +1 -0
- package/dist/providers/google.models.js +8 -0
- package/dist/providers/google.models.js.map +1 -0
- package/dist/providers/groq.d.ts +3 -0
- package/dist/providers/groq.d.ts.map +1 -0
- package/dist/providers/groq.js +15 -0
- package/dist/providers/groq.js.map +1 -0
- package/dist/providers/groq.models.d.ts +5 -0
- package/dist/providers/groq.models.d.ts.map +1 -0
- package/dist/providers/groq.models.js +8 -0
- package/dist/providers/groq.models.js.map +1 -0
- package/dist/providers/huggingface.d.ts +3 -0
- package/dist/providers/huggingface.d.ts.map +1 -0
- package/dist/providers/huggingface.js +15 -0
- package/dist/providers/huggingface.js.map +1 -0
- package/dist/providers/huggingface.models.d.ts +5 -0
- package/dist/providers/huggingface.models.d.ts.map +1 -0
- package/dist/providers/huggingface.models.js +8 -0
- package/dist/providers/huggingface.models.js.map +1 -0
- package/dist/providers/images/register-builtins.d.ts +5 -0
- package/dist/providers/images/register-builtins.d.ts.map +1 -0
- package/dist/providers/images/register-builtins.js +52 -0
- package/dist/providers/images/register-builtins.js.map +1 -0
- package/dist/providers/kimi-coding.d.ts +3 -0
- package/dist/providers/kimi-coding.d.ts.map +1 -0
- package/dist/providers/kimi-coding.js +24 -0
- package/dist/providers/kimi-coding.js.map +1 -0
- package/dist/providers/kimi-coding.models.d.ts +5 -0
- package/dist/providers/kimi-coding.models.d.ts.map +1 -0
- package/dist/providers/kimi-coding.models.js +8 -0
- package/dist/providers/kimi-coding.models.js.map +1 -0
- package/dist/providers/meta.d.ts +3 -0
- package/dist/providers/meta.d.ts.map +1 -0
- package/dist/providers/meta.js +24 -0
- package/dist/providers/meta.js.map +1 -0
- package/dist/providers/meta.models.d.ts +5 -0
- package/dist/providers/meta.models.d.ts.map +1 -0
- package/dist/providers/meta.models.js +8 -0
- package/dist/providers/meta.models.js.map +1 -0
- package/dist/providers/minimax-cn.d.ts +3 -0
- package/dist/providers/minimax-cn.d.ts.map +1 -0
- package/dist/providers/minimax-cn.js +15 -0
- package/dist/providers/minimax-cn.js.map +1 -0
- package/dist/providers/minimax-cn.models.d.ts +5 -0
- package/dist/providers/minimax-cn.models.d.ts.map +1 -0
- package/dist/providers/minimax-cn.models.js +8 -0
- package/dist/providers/minimax-cn.models.js.map +1 -0
- package/dist/providers/minimax.d.ts +3 -0
- package/dist/providers/minimax.d.ts.map +1 -0
- package/dist/providers/minimax.js +15 -0
- package/dist/providers/minimax.js.map +1 -0
- package/dist/providers/minimax.models.d.ts +5 -0
- package/dist/providers/minimax.models.d.ts.map +1 -0
- package/dist/providers/minimax.models.js +8 -0
- package/dist/providers/minimax.models.js.map +1 -0
- package/dist/providers/mistral.d.ts +3 -0
- package/dist/providers/mistral.d.ts.map +1 -0
- package/dist/providers/mistral.js +15 -0
- package/dist/providers/mistral.js.map +1 -0
- package/dist/providers/mistral.models.d.ts +5 -0
- package/dist/providers/mistral.models.d.ts.map +1 -0
- package/dist/providers/mistral.models.js +8 -0
- package/dist/providers/mistral.models.js.map +1 -0
- package/dist/providers/model-schema.d.ts +91 -0
- package/dist/providers/model-schema.d.ts.map +1 -0
- package/dist/providers/model-schema.js +57 -0
- package/dist/providers/model-schema.js.map +1 -0
- package/dist/providers/moonshotai-cn.d.ts +3 -0
- package/dist/providers/moonshotai-cn.d.ts.map +1 -0
- package/dist/providers/moonshotai-cn.js +15 -0
- package/dist/providers/moonshotai-cn.js.map +1 -0
- package/dist/providers/moonshotai-cn.models.d.ts +5 -0
- package/dist/providers/moonshotai-cn.models.d.ts.map +1 -0
- package/dist/providers/moonshotai-cn.models.js +8 -0
- package/dist/providers/moonshotai-cn.models.js.map +1 -0
- package/dist/providers/moonshotai.d.ts +3 -0
- package/dist/providers/moonshotai.d.ts.map +1 -0
- package/dist/providers/moonshotai.js +15 -0
- package/dist/providers/moonshotai.js.map +1 -0
- package/dist/providers/moonshotai.models.d.ts +5 -0
- package/dist/providers/moonshotai.models.d.ts.map +1 -0
- package/dist/providers/moonshotai.models.js +8 -0
- package/dist/providers/moonshotai.models.js.map +1 -0
- package/dist/providers/morph.d.ts +3 -0
- package/dist/providers/morph.d.ts.map +1 -0
- package/dist/providers/morph.js +22 -0
- package/dist/providers/morph.js.map +1 -0
- package/dist/providers/nvidia.d.ts +3 -0
- package/dist/providers/nvidia.d.ts.map +1 -0
- package/dist/providers/nvidia.js +15 -0
- package/dist/providers/nvidia.js.map +1 -0
- package/dist/providers/nvidia.models.d.ts +5 -0
- package/dist/providers/nvidia.models.d.ts.map +1 -0
- package/dist/providers/nvidia.models.js +8 -0
- package/dist/providers/nvidia.models.js.map +1 -0
- package/dist/providers/openai-codex.d.ts +3 -0
- package/dist/providers/openai-codex.d.ts.map +1 -0
- package/dist/providers/openai-codex.js +25 -0
- package/dist/providers/openai-codex.js.map +1 -0
- package/dist/providers/openai-codex.models.d.ts +5 -0
- package/dist/providers/openai-codex.models.d.ts.map +1 -0
- package/dist/providers/openai-codex.models.js +8 -0
- package/dist/providers/openai-codex.models.js.map +1 -0
- package/dist/providers/openai-decisions.d.ts +3 -0
- package/dist/providers/openai-decisions.d.ts.map +1 -0
- package/dist/providers/openai-decisions.js +19 -0
- package/dist/providers/openai-decisions.js.map +1 -0
- package/dist/providers/openai-decisions.models.d.ts +5 -0
- package/dist/providers/openai-decisions.models.d.ts.map +1 -0
- package/dist/providers/openai-decisions.models.js +8 -0
- package/dist/providers/openai-decisions.models.js.map +1 -0
- package/dist/providers/openai-images.d.ts +3 -0
- package/dist/providers/openai-images.d.ts.map +1 -0
- package/dist/providers/openai-images.js +19 -0
- package/dist/providers/openai-images.js.map +1 -0
- package/dist/providers/openai-images.models.d.ts +5 -0
- package/dist/providers/openai-images.models.d.ts.map +1 -0
- package/dist/providers/openai-images.models.js +8 -0
- package/dist/providers/openai-images.models.js.map +1 -0
- package/dist/providers/openai-mid-convo-effort.d.ts +2 -0
- package/dist/providers/openai-mid-convo-effort.d.ts.map +1 -0
- package/dist/providers/openai-mid-convo-effort.js +8 -0
- package/dist/providers/openai-mid-convo-effort.js.map +1 -0
- package/dist/providers/openai-subscription-models.d.ts +3 -0
- package/dist/providers/openai-subscription-models.d.ts.map +1 -0
- package/dist/providers/openai-subscription-models.js +19 -0
- package/dist/providers/openai-subscription-models.js.map +1 -0
- package/dist/providers/openai.d.ts +4 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +48 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openai.models.d.ts +5 -0
- package/dist/providers/openai.models.d.ts.map +1 -0
- package/dist/providers/openai.models.js +8 -0
- package/dist/providers/openai.models.js.map +1 -0
- package/dist/providers/opencode-go.d.ts +5 -0
- package/dist/providers/opencode-go.d.ts.map +1 -0
- package/dist/providers/opencode-go.js +22 -0
- package/dist/providers/opencode-go.js.map +1 -0
- package/dist/providers/opencode-go.models.d.ts +5 -0
- package/dist/providers/opencode-go.models.d.ts.map +1 -0
- package/dist/providers/opencode-go.models.js +8 -0
- package/dist/providers/opencode-go.models.js.map +1 -0
- package/dist/providers/opencode.d.ts +5 -0
- package/dist/providers/opencode.d.ts.map +1 -0
- package/dist/providers/opencode.js +24 -0
- package/dist/providers/opencode.js.map +1 -0
- package/dist/providers/opencode.models.d.ts +5 -0
- package/dist/providers/opencode.models.d.ts.map +1 -0
- package/dist/providers/opencode.models.js +8 -0
- package/dist/providers/opencode.models.js.map +1 -0
- package/dist/providers/openrouter-api.d.ts +40 -0
- package/dist/providers/openrouter-api.d.ts.map +1 -0
- package/dist/providers/openrouter-api.js +41 -0
- package/dist/providers/openrouter-api.js.map +1 -0
- package/dist/providers/openrouter.d.ts +13 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +88 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/providers/openrouter.models.d.ts +5 -0
- package/dist/providers/openrouter.models.d.ts.map +1 -0
- package/dist/providers/openrouter.models.js +8 -0
- package/dist/providers/openrouter.models.js.map +1 -0
- package/dist/providers/qwen-token-plan-cn.d.ts +3 -0
- package/dist/providers/qwen-token-plan-cn.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-cn.js +20 -0
- package/dist/providers/qwen-token-plan-cn.js.map +1 -0
- package/dist/providers/qwen-token-plan-cn.models.d.ts +5 -0
- package/dist/providers/qwen-token-plan-cn.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-cn.models.js +8 -0
- package/dist/providers/qwen-token-plan-cn.models.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.d.ts +3 -0
- package/dist/providers/qwen-token-plan-individual-cn.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.js +20 -0
- package/dist/providers/qwen-token-plan-individual-cn.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts +5 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js +8 -0
- package/dist/providers/qwen-token-plan-individual-cn.models.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual.d.ts +3 -0
- package/dist/providers/qwen-token-plan-individual.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual.js +20 -0
- package/dist/providers/qwen-token-plan-individual.js.map +1 -0
- package/dist/providers/qwen-token-plan-individual.models.d.ts +5 -0
- package/dist/providers/qwen-token-plan-individual.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan-individual.models.js +8 -0
- package/dist/providers/qwen-token-plan-individual.models.js.map +1 -0
- package/dist/providers/qwen-token-plan.d.ts +3 -0
- package/dist/providers/qwen-token-plan.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan.js +20 -0
- package/dist/providers/qwen-token-plan.js.map +1 -0
- package/dist/providers/qwen-token-plan.models.d.ts +5 -0
- package/dist/providers/qwen-token-plan.models.d.ts.map +1 -0
- package/dist/providers/qwen-token-plan.models.js +8 -0
- package/dist/providers/qwen-token-plan.models.js.map +1 -0
- package/dist/providers/together.d.ts +3 -0
- package/dist/providers/together.d.ts.map +1 -0
- package/dist/providers/together.js +15 -0
- package/dist/providers/together.js.map +1 -0
- package/dist/providers/together.models.d.ts +5 -0
- package/dist/providers/together.models.d.ts.map +1 -0
- package/dist/providers/together.models.js +8 -0
- package/dist/providers/together.models.js.map +1 -0
- package/dist/providers/typesafe.d.ts +3 -0
- package/dist/providers/typesafe.d.ts.map +1 -0
- package/dist/providers/typesafe.js +18 -0
- package/dist/providers/typesafe.js.map +1 -0
- package/dist/providers/typesafe.models.d.ts +5 -0
- package/dist/providers/typesafe.models.d.ts.map +1 -0
- package/dist/providers/typesafe.models.js +8 -0
- package/dist/providers/typesafe.models.js.map +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +3 -0
- package/dist/providers/vercel-ai-gateway.d.ts.map +1 -0
- package/dist/providers/vercel-ai-gateway.js +17 -0
- package/dist/providers/vercel-ai-gateway.js.map +1 -0
- package/dist/providers/vercel-ai-gateway.models.d.ts +5 -0
- package/dist/providers/vercel-ai-gateway.models.d.ts.map +1 -0
- package/dist/providers/vercel-ai-gateway.models.js +8 -0
- package/dist/providers/vercel-ai-gateway.models.js.map +1 -0
- package/dist/providers/xai.d.ts +3 -0
- package/dist/providers/xai.d.ts.map +1 -0
- package/dist/providers/xai.js +24 -0
- package/dist/providers/xai.js.map +1 -0
- package/dist/providers/xai.models.d.ts +5 -0
- package/dist/providers/xai.models.d.ts.map +1 -0
- package/dist/providers/xai.models.js +8 -0
- package/dist/providers/xai.models.js.map +1 -0
- package/dist/providers/xiaomi-token-plan-ams.d.ts +3 -0
- package/dist/providers/xiaomi-token-plan-ams.d.ts.map +1 -0
- package/dist/providers/xiaomi-token-plan-ams.js +15 -0
- package/dist/providers/xiaomi-token-plan-ams.js.map +1 -0
- package/dist/providers/xiaomi-token-plan-ams.models.d.ts +5 -0
- package/dist/providers/xiaomi-token-plan-ams.models.d.ts.map +1 -0
- package/dist/providers/xiaomi-token-plan-ams.models.js +8 -0
- package/dist/providers/xiaomi-token-plan-ams.models.js.map +1 -0
- package/dist/providers/xiaomi-token-plan-cn.d.ts +3 -0
- package/dist/providers/xiaomi-token-plan-cn.d.ts.map +1 -0
- package/dist/providers/xiaomi-token-plan-cn.js +15 -0
- package/dist/providers/xiaomi-token-plan-cn.js.map +1 -0
- package/dist/providers/xiaomi-token-plan-cn.models.d.ts +5 -0
- package/dist/providers/xiaomi-token-plan-cn.models.d.ts.map +1 -0
- package/dist/providers/xiaomi-token-plan-cn.models.js +8 -0
- package/dist/providers/xiaomi-token-plan-cn.models.js.map +1 -0
- package/dist/providers/xiaomi-token-plan-sgp.d.ts +3 -0
- package/dist/providers/xiaomi-token-plan-sgp.d.ts.map +1 -0
- package/dist/providers/xiaomi-token-plan-sgp.js +15 -0
- package/dist/providers/xiaomi-token-plan-sgp.js.map +1 -0
- package/dist/providers/xiaomi-token-plan-sgp.models.d.ts +5 -0
- package/dist/providers/xiaomi-token-plan-sgp.models.d.ts.map +1 -0
- package/dist/providers/xiaomi-token-plan-sgp.models.js +8 -0
- package/dist/providers/xiaomi-token-plan-sgp.models.js.map +1 -0
- package/dist/providers/xiaomi.d.ts +3 -0
- package/dist/providers/xiaomi.d.ts.map +1 -0
- package/dist/providers/xiaomi.js +15 -0
- package/dist/providers/xiaomi.js.map +1 -0
- package/dist/providers/xiaomi.models.d.ts +5 -0
- package/dist/providers/xiaomi.models.d.ts.map +1 -0
- package/dist/providers/xiaomi.models.js +8 -0
- package/dist/providers/xiaomi.models.js.map +1 -0
- package/dist/providers/zai-coding-cn.d.ts +3 -0
- package/dist/providers/zai-coding-cn.d.ts.map +1 -0
- package/dist/providers/zai-coding-cn.js +15 -0
- package/dist/providers/zai-coding-cn.js.map +1 -0
- package/dist/providers/zai-coding-cn.models.d.ts +5 -0
- package/dist/providers/zai-coding-cn.models.d.ts.map +1 -0
- package/dist/providers/zai-coding-cn.models.js +8 -0
- package/dist/providers/zai-coding-cn.models.js.map +1 -0
- package/dist/providers/zai.d.ts +3 -0
- package/dist/providers/zai.d.ts.map +1 -0
- package/dist/providers/zai.js +15 -0
- package/dist/providers/zai.js.map +1 -0
- package/dist/providers/zai.models.d.ts +5 -0
- package/dist/providers/zai.models.d.ts.map +1 -0
- package/dist/providers/zai.models.js +8 -0
- package/dist/providers/zai.models.js.map +1 -0
- package/dist/session-resources.d.ts +4 -0
- package/dist/session-resources.d.ts.map +1 -0
- package/dist/session-resources.js +22 -0
- package/dist/session-resources.js.map +1 -0
- package/dist/types.d.ts +845 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/abort-signals.d.ts +6 -0
- package/dist/utils/abort-signals.d.ts.map +1 -0
- package/dist/utils/abort-signals.js +34 -0
- package/dist/utils/abort-signals.js.map +1 -0
- package/dist/utils/abort.d.ts +8 -0
- package/dist/utils/abort.d.ts.map +1 -0
- package/dist/utils/abort.js +49 -0
- package/dist/utils/abort.js.map +1 -0
- package/dist/utils/azure-openai-thinking-level-map.d.ts +19 -0
- package/dist/utils/azure-openai-thinking-level-map.d.ts.map +1 -0
- package/dist/utils/azure-openai-thinking-level-map.js +45 -0
- package/dist/utils/azure-openai-thinking-level-map.js.map +1 -0
- package/dist/utils/diagnostics.d.ts +20 -0
- package/dist/utils/diagnostics.d.ts.map +1 -0
- package/dist/utils/diagnostics.js +25 -0
- package/dist/utils/diagnostics.js.map +1 -0
- package/dist/utils/document-input.d.ts +21 -0
- package/dist/utils/document-input.d.ts.map +1 -0
- package/dist/utils/document-input.js +28 -0
- package/dist/utils/document-input.js.map +1 -0
- package/dist/utils/error-body.d.ts +40 -0
- package/dist/utils/error-body.d.ts.map +1 -0
- package/dist/utils/error-body.js +177 -0
- package/dist/utils/error-body.js.map +1 -0
- package/dist/utils/estimate.d.ts +17 -0
- package/dist/utils/estimate.d.ts.map +1 -0
- package/dist/utils/estimate.js +107 -0
- package/dist/utils/estimate.js.map +1 -0
- package/dist/utils/event-stream.d.ts +24 -0
- package/dist/utils/event-stream.d.ts.map +1 -0
- package/dist/utils/event-stream.js +118 -0
- package/dist/utils/event-stream.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/headers.d.ts +4 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +20 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/identity.d.ts +67 -0
- package/dist/utils/identity.d.ts.map +1 -0
- package/dist/utils/identity.js +148 -0
- package/dist/utils/identity.js.map +1 -0
- package/dist/utils/inline-images.d.ts +24 -0
- package/dist/utils/inline-images.d.ts.map +1 -0
- package/dist/utils/inline-images.js +55 -0
- package/dist/utils/inline-images.js.map +1 -0
- package/dist/utils/json-parse.d.ts +18 -0
- package/dist/utils/json-parse.d.ts.map +1 -0
- package/dist/utils/json-parse.js +167 -0
- package/dist/utils/json-parse.js.map +1 -0
- package/dist/utils/model-operations.d.ts +12 -0
- package/dist/utils/model-operations.d.ts.map +1 -0
- package/dist/utils/model-operations.js +58 -0
- package/dist/utils/model-operations.js.map +1 -0
- package/dist/utils/models-dev-catalog.d.ts +324 -0
- package/dist/utils/models-dev-catalog.d.ts.map +1 -0
- package/dist/utils/models-dev-catalog.js +3166 -0
- package/dist/utils/models-dev-catalog.js.map +1 -0
- package/dist/utils/models-dev-reasoning-options.d.ts +18 -0
- package/dist/utils/models-dev-reasoning-options.d.ts.map +1 -0
- package/dist/utils/models-dev-reasoning-options.js +20 -0
- package/dist/utils/models-dev-reasoning-options.js.map +1 -0
- package/dist/utils/models-error.d.ts +8 -0
- package/dist/utils/models-error.d.ts.map +1 -0
- package/dist/utils/models-error.js +19 -0
- package/dist/utils/models-error.js.map +1 -0
- package/dist/utils/node-http-proxy.d.ts +4 -0
- package/dist/utils/node-http-proxy.d.ts.map +1 -0
- package/dist/utils/node-http-proxy.js +141 -0
- package/dist/utils/node-http-proxy.js.map +1 -0
- package/dist/utils/oauth-page.d.ts +2 -0
- package/dist/utils/oauth-page.d.ts.map +1 -0
- package/dist/utils/oauth-page.js +2 -0
- package/dist/utils/oauth-page.js.map +1 -0
- package/dist/utils/openrouter-cost.d.ts +24 -0
- package/dist/utils/openrouter-cost.d.ts.map +1 -0
- package/dist/utils/openrouter-cost.js +34 -0
- package/dist/utils/openrouter-cost.js.map +1 -0
- package/dist/utils/overflow.d.ts +73 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +204 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/provider-env.d.ts +12 -0
- package/dist/utils/provider-env.d.ts.map +1 -0
- package/dist/utils/provider-env.js +51 -0
- package/dist/utils/provider-env.js.map +1 -0
- package/dist/utils/provider-retry.d.ts +21 -0
- package/dist/utils/provider-retry.d.ts.map +1 -0
- package/dist/utils/provider-retry.js +100 -0
- package/dist/utils/provider-retry.js.map +1 -0
- package/dist/utils/request-admission.d.ts +12 -0
- package/dist/utils/request-admission.d.ts.map +1 -0
- package/dist/utils/request-admission.js +37 -0
- package/dist/utils/request-admission.js.map +1 -0
- package/dist/utils/retry-hints.d.ts +15 -0
- package/dist/utils/retry-hints.d.ts.map +1 -0
- package/dist/utils/retry-hints.js +130 -0
- package/dist/utils/retry-hints.js.map +1 -0
- package/dist/utils/retry.d.ts +78 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +251 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/sanitize-unicode.d.ts +22 -0
- package/dist/utils/sanitize-unicode.d.ts.map +1 -0
- package/dist/utils/sanitize-unicode.js +26 -0
- package/dist/utils/sanitize-unicode.js.map +1 -0
- package/dist/utils/sleep.d.ts +2 -0
- package/dist/utils/sleep.d.ts.map +1 -0
- package/dist/utils/sleep.js +15 -0
- package/dist/utils/sleep.js.map +1 -0
- package/dist/utils/sse.d.ts +29 -0
- package/dist/utils/sse.d.ts.map +1 -0
- package/dist/utils/sse.js +73 -0
- package/dist/utils/sse.js.map +1 -0
- package/dist/utils/stream-deadline.d.ts +78 -0
- package/dist/utils/stream-deadline.d.ts.map +1 -0
- package/dist/utils/stream-deadline.js +205 -0
- package/dist/utils/stream-deadline.js.map +1 -0
- package/dist/utils/streaming-json.d.ts +84 -0
- package/dist/utils/streaming-json.d.ts.map +1 -0
- package/dist/utils/streaming-json.js +513 -0
- package/dist/utils/streaming-json.js.map +1 -0
- package/dist/utils/text.d.ts +21 -0
- package/dist/utils/text.d.ts.map +1 -0
- package/dist/utils/text.js +45 -0
- package/dist/utils/text.js.map +1 -0
- package/dist/utils/thinking-off.d.ts +23 -0
- package/dist/utils/thinking-off.d.ts.map +1 -0
- package/dist/utils/thinking-off.js +51 -0
- package/dist/utils/thinking-off.js.map +1 -0
- package/dist/utils/transcript.d.ts +101 -0
- package/dist/utils/transcript.d.ts.map +1 -0
- package/dist/utils/transcript.js +218 -0
- package/dist/utils/transcript.js.map +1 -0
- package/dist/utils/typebox-helpers.d.ts +17 -0
- package/dist/utils/typebox-helpers.d.ts.map +1 -0
- package/dist/utils/typebox-helpers.js +21 -0
- package/dist/utils/typebox-helpers.js.map +1 -0
- package/dist/utils/uuid.d.ts +3 -0
- package/dist/utils/uuid.d.ts.map +1 -0
- package/dist/utils/uuid.js +46 -0
- package/dist/utils/uuid.js.map +1 -0
- package/dist/utils/validation.d.ts +24 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +363 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/version.d.ts +2 -0
- package/dist/utils/version.d.ts.map +1 -0
- package/dist/utils/version.js +3 -0
- package/dist/utils/version.js.map +1 -0
- package/package.json +138 -4
|
@@ -0,0 +1,1814 @@
|
|
|
1
|
+
import Anthropic, {} from "@anthropic-ai/sdk";
|
|
2
|
+
import { ANTHROPIC_FEDERATION_RULE_ID_ENV, ANTHROPIC_IDENTITY_TOKEN_FILE_ENV, ANTHROPIC_ORGANIZATION_ID_ENV, ANTHROPIC_SERVICE_ACCOUNT_ID_ENV, ANTHROPIC_WORKSPACE_ID_ENV, } from "../env-api-keys.js";
|
|
3
|
+
import { calculateCost } from "../models.js";
|
|
4
|
+
import { recordGitHubCopilotFailure } from "../providers/github-copilot-errors.js";
|
|
5
|
+
import { GitHubCopilotStreamAttempt } from "../providers/github-copilot-stream-attempt.js";
|
|
6
|
+
import { registerSessionResourceCleanup } from "../session-resources.js";
|
|
7
|
+
import { raceWithAbortSignal } from "../utils/abort.js";
|
|
8
|
+
import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
|
|
9
|
+
import { assertSupportedDocumentMimeType } from "../utils/document-input.js";
|
|
10
|
+
import { appendProviderResponseFailureDiagnostic, withCauseDetail } from "../utils/error-body.js";
|
|
11
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
12
|
+
import { shortHash } from "../utils/hash.js";
|
|
13
|
+
import { headersToRecord } from "../utils/headers.js";
|
|
14
|
+
import { getIdentityHeaders } from "../utils/identity.js";
|
|
15
|
+
import { inlineImageMimeType, unsupportedImagePlaceholder } from "../utils/inline-images.js";
|
|
16
|
+
import { parseJsonWithRepair } from "../utils/json-parse.js";
|
|
17
|
+
import { applyOpenRouterReportedCost, isOpenRouterModel, } from "../utils/openrouter-cost.js";
|
|
18
|
+
import { getAtomicEnvValue, getProviderEnvValue } from "../utils/provider-env.js";
|
|
19
|
+
import { retryProviderRequest } from "../utils/provider-retry.js";
|
|
20
|
+
import { getFetchAdmission } from "../utils/request-admission.js";
|
|
21
|
+
import { getProviderRetryAfterMs } from "../utils/retry-hints.js";
|
|
22
|
+
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
23
|
+
import { ServerSentEventDecoder } from "../utils/sse.js";
|
|
24
|
+
import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadline.js";
|
|
25
|
+
import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
|
|
26
|
+
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
|
|
27
|
+
import { appendThinkingOffFallbackDiagnostic, getThinkingOffFallbackLevel } from "../utils/thinking-off.js";
|
|
28
|
+
import { getCurrentTools, getInitialSystemMessage, resolveTranscript, } from "../utils/transcript.js";
|
|
29
|
+
import { convertAnthropicAssistantContent } from "./anthropic-assistant-content.js";
|
|
30
|
+
import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, prefersJsonSchemaStrictSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
|
|
31
|
+
import { buildCopilotDynamicHeaders, hasCopilotVisionInput, preserveCopilotIntegrationHeader, } from "./github-copilot-headers.js";
|
|
32
|
+
import { adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToContext } from "./simple-options.js";
|
|
33
|
+
import { hasExceededStrictToolLimits, isStrictToolLimitErrorMessage, markStrictToolLimitsExceeded, } from "./strict-tool-limits.js";
|
|
34
|
+
import { splitErrorToolResultImages, transformMessages } from "./transform-messages.js";
|
|
35
|
+
/**
|
|
36
|
+
* Resolve cache retention preference.
|
|
37
|
+
* Defaults to "short" and uses PI_CACHE_RETENTION for backward compatibility.
|
|
38
|
+
*/
|
|
39
|
+
function resolveCacheRetention(cacheRetention, env) {
|
|
40
|
+
if (cacheRetention) {
|
|
41
|
+
return cacheRetention;
|
|
42
|
+
}
|
|
43
|
+
if (getAtomicEnvValue("CACHE_RETENTION", env) === "long") {
|
|
44
|
+
return "long";
|
|
45
|
+
}
|
|
46
|
+
return "short";
|
|
47
|
+
}
|
|
48
|
+
function getCacheControl(model, cacheRetention, env) {
|
|
49
|
+
const retention = resolveCacheRetention(cacheRetention, env);
|
|
50
|
+
if (retention === "none") {
|
|
51
|
+
return { retention };
|
|
52
|
+
}
|
|
53
|
+
const ttl = retention === "long" && getAnthropicCompat(model).supportsLongCacheRetention ? "1h" : undefined;
|
|
54
|
+
return {
|
|
55
|
+
retention,
|
|
56
|
+
cacheControl: { type: "ephemeral", ...(ttl && { ttl }) },
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// Stealth mode: Mimic Claude Code's tool naming exactly
|
|
60
|
+
//
|
|
61
|
+
// Anthropic gates newer models on the `claude-cli/<version>` user agent alone and rejects an
|
|
62
|
+
// older one with `claude_code_version_too_old`. `claude-fable-5-1` requires >= 2.1.251 (bisected
|
|
63
|
+
// against the live API: 2.1.250 -> 400, 2.1.251 -> 200), Claude Opus 5.5 requires >= 2.1.280 and
|
|
64
|
+
// `claude-fable-5-2` requires >= 2.1.290, the floors the API names in its rejections. This tracks
|
|
65
|
+
// the latest published Claude Code release (`npm view @anthropic-ai/claude-code version`), which
|
|
66
|
+
// stays above every shipped model's floor and matches what an up-to-date Claude Code sends. A
|
|
67
|
+
// caller can override it for one client through the `headers` option, whose lowercase
|
|
68
|
+
// `user-agent` key is merged last.
|
|
69
|
+
const claudeCodeVersion = "2.1.322";
|
|
70
|
+
// Claude Code 2.x tool names (canonical casing)
|
|
71
|
+
// Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
|
|
72
|
+
// To update: https://github.com/badlogic/cchistory
|
|
73
|
+
const claudeCodeTools = [
|
|
74
|
+
"Read",
|
|
75
|
+
"Write",
|
|
76
|
+
"Edit",
|
|
77
|
+
"Bash",
|
|
78
|
+
"Grep",
|
|
79
|
+
"Glob",
|
|
80
|
+
"AskUserQuestion",
|
|
81
|
+
"EnterPlanMode",
|
|
82
|
+
"ExitPlanMode",
|
|
83
|
+
"KillShell",
|
|
84
|
+
"NotebookEdit",
|
|
85
|
+
"Skill",
|
|
86
|
+
"Task",
|
|
87
|
+
"TaskOutput",
|
|
88
|
+
"TodoWrite",
|
|
89
|
+
"WebFetch",
|
|
90
|
+
"WebSearch",
|
|
91
|
+
];
|
|
92
|
+
const ccToolLookup = new Map(claudeCodeTools.map((t) => [t.toLowerCase(), t]));
|
|
93
|
+
// Convert tool name to CC canonical casing if it matches (case-insensitive)
|
|
94
|
+
const toClaudeCodeName = (name) => ccToolLookup.get(name.toLowerCase()) ?? name;
|
|
95
|
+
const fromClaudeCodeName = (name, tools) => {
|
|
96
|
+
if (tools && tools.length > 0) {
|
|
97
|
+
const lowerName = name.toLowerCase();
|
|
98
|
+
const matchedTool = tools.find((tool) => tool.name.toLowerCase() === lowerName);
|
|
99
|
+
if (matchedTool)
|
|
100
|
+
return matchedTool.name;
|
|
101
|
+
}
|
|
102
|
+
return name;
|
|
103
|
+
};
|
|
104
|
+
/** An image block, or the placeholder text when Anthropic does not accept the image's media type. */
|
|
105
|
+
function convertImageBlock(image) {
|
|
106
|
+
const media_type = inlineImageMimeType(image.mimeType);
|
|
107
|
+
if (!media_type)
|
|
108
|
+
return { type: "text", text: unsupportedImagePlaceholder(image.mimeType) };
|
|
109
|
+
return {
|
|
110
|
+
type: "image",
|
|
111
|
+
source: { type: "base64", media_type, data: image.data },
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Convert tool-result content blocks to Anthropic API format.
|
|
116
|
+
*
|
|
117
|
+
* Tool results carry text and images only. `DocumentContent` appears exclusively in user
|
|
118
|
+
* messages, which `convertMessages` serializes on its own path.
|
|
119
|
+
*/
|
|
120
|
+
function convertContentBlocks(content) {
|
|
121
|
+
// If only text blocks, return as concatenated string for simplicity
|
|
122
|
+
const hasImages = content.some((c) => c.type === "image");
|
|
123
|
+
if (!hasImages) {
|
|
124
|
+
return sanitizeSurrogates(content.map((c) => c.text).join("\n"));
|
|
125
|
+
}
|
|
126
|
+
// If we have images, convert to content block array
|
|
127
|
+
const blocks = content.map((block) => {
|
|
128
|
+
if (block.type === "text") {
|
|
129
|
+
return {
|
|
130
|
+
type: "text",
|
|
131
|
+
text: sanitizeSurrogates(block.text),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return convertImageBlock(block);
|
|
135
|
+
});
|
|
136
|
+
// If only images (no text), add placeholder text block
|
|
137
|
+
const hasText = blocks.some((b) => b.type === "text");
|
|
138
|
+
if (!hasText) {
|
|
139
|
+
blocks.unshift({
|
|
140
|
+
type: "text",
|
|
141
|
+
text: "(see attached image)",
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return blocks;
|
|
145
|
+
}
|
|
146
|
+
const FINE_GRAINED_TOOL_STREAMING_BETA = "fine-grained-tool-streaming-2025-05-14";
|
|
147
|
+
const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
|
|
148
|
+
const SERVER_SIDE_FALLBACK_BETA = "server-side-fallback-2026-07-01";
|
|
149
|
+
const THINKING_BINDING_CONTROLS_BETA = "thinking-binding-controls-2026-08-01";
|
|
150
|
+
const MID_CONVERSATION_OUTPUT_CONFIG_BETA = "mid-conversation-output-config-2026-07-01";
|
|
151
|
+
const INLINE_TOOLS_BETA = "inline-tools-2026-09-15";
|
|
152
|
+
const MESSAGE_THREADS_BETA = "message-threads-2026-08-12";
|
|
153
|
+
const FAST_MODE_BETA = "fast-mode-2026-02-01";
|
|
154
|
+
/**
|
|
155
|
+
* Fast mode bills every token category at twice the standard rate for each supported model, across
|
|
156
|
+
* the full context window, and prompt-caching multipliers stack on top.
|
|
157
|
+
* https://platform.claude.com/docs/en/build-with-claude/fast-mode#pricing
|
|
158
|
+
*/
|
|
159
|
+
const FAST_MODE_PRICE_MULTIPLIER = 2;
|
|
160
|
+
function requestedSpeed(model) {
|
|
161
|
+
return model.fastRoute?.speed;
|
|
162
|
+
}
|
|
163
|
+
function upstreamModelId(model) {
|
|
164
|
+
return model.fastRoute?.upstreamModelId ?? model.id;
|
|
165
|
+
}
|
|
166
|
+
function reportedSpeed(usage) {
|
|
167
|
+
const speed = usage?.speed;
|
|
168
|
+
return speed === "fast" || speed === "standard" ? speed : undefined;
|
|
169
|
+
}
|
|
170
|
+
function applyFastModePricing(usage) {
|
|
171
|
+
usage.cost.input *= FAST_MODE_PRICE_MULTIPLIER;
|
|
172
|
+
usage.cost.output *= FAST_MODE_PRICE_MULTIPLIER;
|
|
173
|
+
usage.cost.cacheRead *= FAST_MODE_PRICE_MULTIPLIER;
|
|
174
|
+
usage.cost.cacheWrite *= FAST_MODE_PRICE_MULTIPLIER;
|
|
175
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
176
|
+
usage.cost.totalFromProvider = false;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Reject a payload hook that rewrote a field the model's fast route owns.
|
|
180
|
+
*
|
|
181
|
+
* A fast variant is recorded, persisted, and billed under its own `-fast` identity, so a hook that
|
|
182
|
+
* changes the upstream model or drops `speed: "fast"` would send a different request under that
|
|
183
|
+
* identity. Refusing loudly surfaces the misconfigured hook instead of silently restoring the route.
|
|
184
|
+
*/
|
|
185
|
+
function assertPayloadPreservesFastRoute(model, payload) {
|
|
186
|
+
const fastRoute = model.fastRoute;
|
|
187
|
+
if (fastRoute?.speed === undefined)
|
|
188
|
+
return;
|
|
189
|
+
const remedy = `A fast model variant must send its route's upstream model and speed, because it is recorded, persisted, ` +
|
|
190
|
+
`and billed under its own identity. Select the normal model "${model.provider}/${fastRoute.baseModelId}" ` +
|
|
191
|
+
`if the request needs different routing`;
|
|
192
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
|
|
193
|
+
throw new Error(`A request hook changed payload for fast model "${model.provider}/${model.id}" to a value that cannot carry ` +
|
|
194
|
+
`the route's upstream model and speed. ${remedy}, or return an object that preserves those fields.`);
|
|
195
|
+
}
|
|
196
|
+
const record = payload;
|
|
197
|
+
const conflicts = [];
|
|
198
|
+
if (record.model !== fastRoute.upstreamModelId) {
|
|
199
|
+
conflicts.push(`model (expected "${fastRoute.upstreamModelId}", got ${JSON.stringify(record.model)})`);
|
|
200
|
+
}
|
|
201
|
+
if (record.speed !== fastRoute.speed) {
|
|
202
|
+
conflicts.push(`speed (expected ${JSON.stringify(fastRoute.speed)}, got ${JSON.stringify(record.speed)})`);
|
|
203
|
+
}
|
|
204
|
+
if (conflicts.length === 0)
|
|
205
|
+
return;
|
|
206
|
+
throw new Error(`A request hook changed ${conflicts.join(" and ")} for fast model "${model.provider}/${model.id}". ` +
|
|
207
|
+
`${remedy}, or stop rewriting those fields.`);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Stable deferred tool declared whenever native tool changes are in use. Anthropic adds
|
|
211
|
+
* hidden prompt scaffolding for mid-conversation tool changes; declaring this placeholder
|
|
212
|
+
* from the first request keeps that scaffolding in the cached prefix. It is never activated
|
|
213
|
+
* and the model cannot see it.
|
|
214
|
+
*/
|
|
215
|
+
const DEFERRED_TOOL_PLACEHOLDER = {
|
|
216
|
+
name: "__pi_deferred_placeholder__",
|
|
217
|
+
description: "Reserved placeholder. Never available. Never call this.",
|
|
218
|
+
input_schema: { type: "object", properties: {}, required: [] },
|
|
219
|
+
defer_loading: true,
|
|
220
|
+
};
|
|
221
|
+
function shouldUseServerSideFallbackBeta(model) {
|
|
222
|
+
return (model.compat?.allowedFallbackModels?.length ?? 0) > 0;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Claude Fable 5.1 binds each thinking block to the conversation prefix that produced it and,
|
|
226
|
+
* for Anthropic accounts created on or after 2026-08-31, rejects a replay behind a changed
|
|
227
|
+
* `system` prompt, `tools` array, or earlier message with a 400 `invalid_request_error`.
|
|
228
|
+
* Atomic rebuilds those inputs between turns (dynamic system prompt, tool availability changes,
|
|
229
|
+
* model switches), so it opts into `prefix_mismatch_behavior: "drop_block"`: the API discards
|
|
230
|
+
* the affected thinking blocks and answers the turn instead of failing the session.
|
|
231
|
+
*
|
|
232
|
+
* This covers live prefix mismatches *between* compaction boundaries. It is not what handles
|
|
233
|
+
* compaction itself: Atomic's client-side `preserve_recent` tail compaction serializes the
|
|
234
|
+
* protected tail into a single boundary message, so no signed thinking block survives a
|
|
235
|
+
* boundary to be replayed behind it. That is Anthropic's documented keep-tail remedy applied
|
|
236
|
+
* structurally rather than a case `drop_block` has to catch.
|
|
237
|
+
* https://platform.claude.com/docs/en/build-with-claude/preserved-thinking
|
|
238
|
+
*/
|
|
239
|
+
function shouldUseThinkingBindingControlsBeta(model) {
|
|
240
|
+
return model.compat?.enforcesPreservedThinkingBinding === true;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Per-turn effort and preserved-thinking binding are independent capabilities.
|
|
244
|
+
* `supportsMidConvoEffort` adds provider effort markers and requires binding controls;
|
|
245
|
+
* Atomic's older `enforcesPreservedThinkingBinding` remains the broader opt-in for
|
|
246
|
+
* transports that need drop recovery without supporting effort-only system messages.
|
|
247
|
+
*/
|
|
248
|
+
function supportsMidConvoEffort(model) {
|
|
249
|
+
return model.compat?.supportsMidConvoEffort === true;
|
|
250
|
+
}
|
|
251
|
+
function getInputTransformations(message) {
|
|
252
|
+
const transformations = message
|
|
253
|
+
?.input_transformations;
|
|
254
|
+
return Array.isArray(transformations) ? transformations : undefined;
|
|
255
|
+
}
|
|
256
|
+
/** Record thinking blocks the API dropped from this request without exposing block contents. */
|
|
257
|
+
function recordInputTransformations(output, transformations) {
|
|
258
|
+
if (transformations.length === 0)
|
|
259
|
+
return;
|
|
260
|
+
appendAssistantMessageDiagnostic(output, {
|
|
261
|
+
type: "anthropic_input_transformations",
|
|
262
|
+
timestamp: Date.now(),
|
|
263
|
+
details: {
|
|
264
|
+
droppedBlockCount: transformations.length,
|
|
265
|
+
reasons: [...new Set(transformations.map((entry) => entry.reason).filter((r) => !!r))],
|
|
266
|
+
paths: transformations.map((entry) => entry.path).filter((p) => !!p),
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Bill the attempts that ran *before* the one which produced the returned message.
|
|
272
|
+
*
|
|
273
|
+
* "Every attempt that produced output, including one that declined partway through its response,
|
|
274
|
+
* is billed separately at the rates of the model that ran it. The `usage.iterations` array is the
|
|
275
|
+
* per-attempt record of what you're billed. The top-level `usage` counts describe only the attempt
|
|
276
|
+
* that produced the returned message. Tokens from different models are never summed into one
|
|
277
|
+
* field." https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback
|
|
278
|
+
*
|
|
279
|
+
* Two rules follow, and both matter for not double-counting:
|
|
280
|
+
*
|
|
281
|
+
* - Only `type: "message"` entries are added. The `fallback_message` entry *is* the serving
|
|
282
|
+
* attempt, and its tokens are already in the top-level usage that `calculateCost` just priced.
|
|
283
|
+
* - Only entries with output are added: "An attempt that declined before producing any output is
|
|
284
|
+
* not billed: its tokens are reported on its `usage.iterations` entry but not charged."
|
|
285
|
+
*
|
|
286
|
+
* Token counts are deliberately left alone — the docs forbid summing across models and `Usage` has
|
|
287
|
+
* no per-attempt shape — so this contributes to `usage.cost` only.
|
|
288
|
+
*/
|
|
289
|
+
function addEarlierAttemptCosts(output, model, servingModel, event) {
|
|
290
|
+
const iterations = event?.usage?.iterations;
|
|
291
|
+
if (!Array.isArray(iterations) || iterations.length === 0)
|
|
292
|
+
return;
|
|
293
|
+
for (const iteration of iterations) {
|
|
294
|
+
if (iteration.type !== "message")
|
|
295
|
+
continue;
|
|
296
|
+
if (!iteration.output_tokens)
|
|
297
|
+
continue;
|
|
298
|
+
// Price at the rates of the model that ran the attempt, which is not the serving model.
|
|
299
|
+
const attemptCost = iteration.model === upstreamModelId(model)
|
|
300
|
+
? model.cost
|
|
301
|
+
: iteration.model === servingModel.id
|
|
302
|
+
? servingModel.cost
|
|
303
|
+
: model.compat?.allowedFallbackModels?.find((fallback) => fallback.provider === model.provider && fallback.model === iteration.model)?.cost;
|
|
304
|
+
if (!attemptCost)
|
|
305
|
+
continue;
|
|
306
|
+
const attemptUsage = {
|
|
307
|
+
input: iteration.input_tokens ?? 0,
|
|
308
|
+
output: iteration.output_tokens,
|
|
309
|
+
cacheRead: iteration.cache_read_input_tokens ?? 0,
|
|
310
|
+
// `cacheWrite` stays the aggregate: `calculateCost` derives the 5-minute share by
|
|
311
|
+
// subtracting `cacheWrite1h` from it, so setting this to the 5-minute count instead
|
|
312
|
+
// would under-charge and adding the two together would double-charge. Without the 1h
|
|
313
|
+
// split, an hour-long write bills at the 5-minute rate rather than 2x base input.
|
|
314
|
+
cacheWrite: iteration.cache_creation_input_tokens ?? 0,
|
|
315
|
+
cacheWrite1h: iteration.cache_creation?.ephemeral_1h_input_tokens ?? 0,
|
|
316
|
+
totalTokens: 0,
|
|
317
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
318
|
+
};
|
|
319
|
+
calculateCost({ ...model, id: iteration.model ?? model.id, cost: attemptCost }, attemptUsage);
|
|
320
|
+
output.usage.cost.input += attemptUsage.cost.input;
|
|
321
|
+
output.usage.cost.output += attemptUsage.cost.output;
|
|
322
|
+
output.usage.cost.cacheRead += attemptUsage.cost.cacheRead;
|
|
323
|
+
output.usage.cost.cacheWrite += attemptUsage.cost.cacheWrite;
|
|
324
|
+
output.usage.cost.total += attemptUsage.cost.total;
|
|
325
|
+
output.usage.cost.totalFromProvider = false;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
function getAnthropicCompat(model) {
|
|
329
|
+
const isOpenRouter = model.provider === "openrouter" || model.baseUrl.includes("openrouter.ai");
|
|
330
|
+
return {
|
|
331
|
+
supportsEagerToolInputStreaming: model.compat?.supportsEagerToolInputStreaming ?? true,
|
|
332
|
+
supportsLongCacheRetention: model.compat?.supportsLongCacheRetention ?? true,
|
|
333
|
+
sendSessionAffinityHeaders: model.compat?.sendSessionAffinityHeaders ?? isOpenRouter,
|
|
334
|
+
sessionAffinityFormat: model.compat?.sessionAffinityFormat ?? (isOpenRouter ? "openrouter" : undefined),
|
|
335
|
+
supportsCacheControlOnTools: model.compat?.supportsCacheControlOnTools ?? true,
|
|
336
|
+
supportsTemperature: model.compat?.supportsTemperature ?? true,
|
|
337
|
+
allowEmptySignature: model.compat?.allowEmptySignature ?? false,
|
|
338
|
+
supportsStrictTools: model.compat?.supportsStrictTools ?? false,
|
|
339
|
+
supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
|
|
340
|
+
supportsMidConvoToolChanges: model.compat?.supportsMidConvoToolChanges ?? false,
|
|
341
|
+
// Gateways that drop unknown request fields would silently turn a `continue` into a request
|
|
342
|
+
// without the earlier conversation, so the default is limited to Anthropic's own host.
|
|
343
|
+
// Copilot's endpoint does not accept the threads beta.
|
|
344
|
+
supportsMessageThreads: model.compat?.supportsMessageThreads ?? isAnthropicApiHost(model.baseUrl),
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/** Whether `baseUrl` is Anthropic's own API host, compared by parsed hostname (not a substring). */
|
|
348
|
+
function isAnthropicApiHost(baseUrl) {
|
|
349
|
+
try {
|
|
350
|
+
return new URL(baseUrl).hostname === "api.anthropic.com";
|
|
351
|
+
}
|
|
352
|
+
catch {
|
|
353
|
+
return false;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** Per session: where the next request can continue, or "off" after the endpoint rejected threads. */
|
|
357
|
+
const threadState = new Map();
|
|
358
|
+
const MAX_THREAD_STATE_ENTRIES = 10_000;
|
|
359
|
+
/** Rejections of a `continue` that a new thread fixes: expired, raced, or a fixed setting changed. */
|
|
360
|
+
const NEW_THREAD_ERRORS = new Set([
|
|
361
|
+
"thread_not_found",
|
|
362
|
+
"thread_already_continued",
|
|
363
|
+
"thread_continue_in_flight",
|
|
364
|
+
"thread_fingerprint_mismatch",
|
|
365
|
+
]);
|
|
366
|
+
/**
|
|
367
|
+
* Sets a session's thread state, keeping at most `MAX_THREAD_STATE_ENTRIES` sessions.
|
|
368
|
+
*
|
|
369
|
+
* State is normally freed by `cleanupSessionResources(sessionId)` when a session ends. The cap
|
|
370
|
+
* bounds memory for library users that never call it, for example a server passing a new
|
|
371
|
+
* `sessionId` per conversation. Dropping the least recently updated session is cheap: its thread
|
|
372
|
+
* has most likely expired on the server already, and otherwise its next request starts a new thread.
|
|
373
|
+
*/
|
|
374
|
+
function setThreadState(sessionId, value) {
|
|
375
|
+
// Re-insert so Map order stays oldest-first for the cap.
|
|
376
|
+
threadState.delete(sessionId);
|
|
377
|
+
threadState.set(sessionId, value);
|
|
378
|
+
if (threadState.size > MAX_THREAD_STATE_ENTRIES) {
|
|
379
|
+
const oldest = threadState.keys().next().value;
|
|
380
|
+
if (oldest !== undefined)
|
|
381
|
+
threadState.delete(oldest);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
registerSessionResourceCleanup((sessionId) => {
|
|
385
|
+
if (sessionId)
|
|
386
|
+
threadState.delete(sessionId);
|
|
387
|
+
else
|
|
388
|
+
threadState.clear();
|
|
389
|
+
});
|
|
390
|
+
/**
|
|
391
|
+
* Hash of everything a `continue` builds on: the settings that must stay the same within a thread
|
|
392
|
+
* and the messages the server already holds.
|
|
393
|
+
*
|
|
394
|
+
* - Settings, as sent: the endpoint (a thread exists on one endpoint only) and the fixed request
|
|
395
|
+
* fields. A `continue` omits `system` and `tools` and the server keeps the thread's own, so only
|
|
396
|
+
* this check can notice a change to them, including their cache markers. The server rejects
|
|
397
|
+
* changes to the other fields with `thread_fingerprint_mismatch`; checking them here saves that
|
|
398
|
+
* rejected request. Atomic also checks the fast-mode `speed`.
|
|
399
|
+
* - Messages, normalized so messages that mean the same to the API hash the same.
|
|
400
|
+
*/
|
|
401
|
+
function hashThreadPrefix(model, params, messages) {
|
|
402
|
+
const { system, tools, thinking, output_config, tool_choice, betas, fallbacks } = params;
|
|
403
|
+
const settings = JSON.stringify([
|
|
404
|
+
model.provider,
|
|
405
|
+
model.baseUrl,
|
|
406
|
+
params.model,
|
|
407
|
+
system,
|
|
408
|
+
tools,
|
|
409
|
+
thinking,
|
|
410
|
+
output_config,
|
|
411
|
+
tool_choice,
|
|
412
|
+
betas,
|
|
413
|
+
fallbacks,
|
|
414
|
+
params.speed,
|
|
415
|
+
]);
|
|
416
|
+
const conversation = JSON.stringify(messages.map(normalizeThreadMessage));
|
|
417
|
+
return shortHash(settings + conversation);
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Normalizes one message so messages that mean the same to the API compare equal. Only message and
|
|
421
|
+
* content-block fields are touched; tool inputs and every other value compare exactly as sent.
|
|
422
|
+
*
|
|
423
|
+
* - Cache markers don't change what a message means, but they move: Atomic puts one on the newest
|
|
424
|
+
* message, so the message that had it in the last request has none in this one.
|
|
425
|
+
* - To attach that marker, the newest user message's string content is sent as one text block; the
|
|
426
|
+
* next request sends the same message as a string: "hi" vs [{ type: "text", text: "hi" }]. The
|
|
427
|
+
* same holds for a tool result's content.
|
|
428
|
+
*/
|
|
429
|
+
function normalizeThreadMessage(message) {
|
|
430
|
+
if (message === null || typeof message !== "object")
|
|
431
|
+
return message;
|
|
432
|
+
const { content, ...rest } = message;
|
|
433
|
+
return { ...rest, content: normalizeThreadContent(content) };
|
|
434
|
+
}
|
|
435
|
+
function normalizeThreadContent(content) {
|
|
436
|
+
if (typeof content === "string")
|
|
437
|
+
return [{ type: "text", text: content }];
|
|
438
|
+
if (!Array.isArray(content))
|
|
439
|
+
return content;
|
|
440
|
+
return content.map((block) => {
|
|
441
|
+
if (block === null || typeof block !== "object")
|
|
442
|
+
return block;
|
|
443
|
+
const { cache_control: _cacheControl, ...rest } = block;
|
|
444
|
+
if (rest.type !== "tool_result")
|
|
445
|
+
return rest;
|
|
446
|
+
const toolResult = rest;
|
|
447
|
+
return { ...toolResult, content: normalizeThreadContent(toolResult.content) };
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
function createThreadRequest(sessionId, params) {
|
|
451
|
+
return { sessionId, body: { ...params, thread: { type: "create" } } };
|
|
452
|
+
}
|
|
453
|
+
function prepareThreadRequest(model, sessionId, params) {
|
|
454
|
+
const state = threadState.get(sessionId);
|
|
455
|
+
if (state === "off")
|
|
456
|
+
return undefined;
|
|
457
|
+
if (state !== undefined &&
|
|
458
|
+
params.messages.length > state.prefixCount &&
|
|
459
|
+
hashThreadPrefix(model, params, params.messages.slice(0, state.prefixCount)) === state.prefixHash) {
|
|
460
|
+
// Each reply can be continued once; deleting also keeps a concurrent request from trying.
|
|
461
|
+
threadState.delete(sessionId);
|
|
462
|
+
const { system: _system, tools: _tools, ...rest } = params;
|
|
463
|
+
return {
|
|
464
|
+
body: {
|
|
465
|
+
...rest,
|
|
466
|
+
messages: params.messages.slice(state.prefixCount),
|
|
467
|
+
thread: { type: "continue", previous_message_id: state.replyId },
|
|
468
|
+
},
|
|
469
|
+
sessionId,
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
return createThreadRequest(sessionId, params);
|
|
473
|
+
}
|
|
474
|
+
/** The `error_code` Anthropic puts in `error.details` of thread rejections. */
|
|
475
|
+
function getThreadErrorCode(error) {
|
|
476
|
+
const body = error.error;
|
|
477
|
+
const code = body?.error?.details?.error_code;
|
|
478
|
+
return typeof code === "string" ? code : undefined;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Sends a thread request. A rejection about the thread (never a started stream) is resent once in
|
|
482
|
+
* full: as a new thread when the old one can't be continued, or without `thread` when the endpoint
|
|
483
|
+
* can't use threads, which also turns threads off for the session. Other errors are passed on.
|
|
484
|
+
* Returns the response and the thread request that produced it, if any.
|
|
485
|
+
*/
|
|
486
|
+
async function sendThreadRequest(thread, params, send, output, signal) {
|
|
487
|
+
try {
|
|
488
|
+
return { response: await send(thread.body), thread };
|
|
489
|
+
}
|
|
490
|
+
catch (error) {
|
|
491
|
+
if (signal?.aborted || !(error instanceof Anthropic.APIError))
|
|
492
|
+
throw error;
|
|
493
|
+
const errorCode = getThreadErrorCode(error);
|
|
494
|
+
const startNewThread = errorCode !== undefined && NEW_THREAD_ERRORS.has(errorCode);
|
|
495
|
+
// The endpoint can't use threads: it can't thread this request, or it doesn't know the
|
|
496
|
+
// `thread` field and rejects it without a code ("thread: Extra inputs are not permitted"),
|
|
497
|
+
// e.g. with an `anthropic-beta` header that lacks the threads beta.
|
|
498
|
+
const threadsUnsupported = errorCode === "thread_unsupported_request" ||
|
|
499
|
+
(errorCode === undefined && error.status === 400 && error.message.includes("thread"));
|
|
500
|
+
// Any other error (prompt too long, unknown model, strict tool limits, ...) is not about the
|
|
501
|
+
// thread, and resending would only fail again.
|
|
502
|
+
if (!startNewThread && !threadsUnsupported)
|
|
503
|
+
throw error;
|
|
504
|
+
// Recorded before the resend, so a failed resend keeps it.
|
|
505
|
+
appendAssistantMessageDiagnostic(output, {
|
|
506
|
+
type: "anthropic_thread_fallback",
|
|
507
|
+
timestamp: Date.now(),
|
|
508
|
+
error: { message: error.message },
|
|
509
|
+
details: { reason: errorCode ?? String(error.status) },
|
|
510
|
+
});
|
|
511
|
+
if (startNewThread) {
|
|
512
|
+
const created = createThreadRequest(thread.sessionId, params);
|
|
513
|
+
return { response: await send(created.body), thread: created };
|
|
514
|
+
}
|
|
515
|
+
// With the threads beta, a request without `thread` is a normal request.
|
|
516
|
+
const response = await send(params);
|
|
517
|
+
setThreadState(thread.sessionId, "off");
|
|
518
|
+
return { response, thread: undefined };
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Records where the session's next request can continue: after `output`, the clean reply to the
|
|
523
|
+
* thread request `thread`, whose full request was `params`.
|
|
524
|
+
*/
|
|
525
|
+
function recordThreadReply(model, thread, params, output, isOAuth, allowEmptySignature) {
|
|
526
|
+
// `responseModel` is set only when another model answered (server-side fallback); the server
|
|
527
|
+
// does not store those replies, so they cannot be continued.
|
|
528
|
+
if (output.responseId === undefined || output.responseModel !== undefined)
|
|
529
|
+
return;
|
|
530
|
+
// Convert the reply like the next request will, keeping only the assistant message
|
|
531
|
+
// (transformMessages adds placeholder results for trailing tool calls).
|
|
532
|
+
const reply = convertMessages(transformMessages([output], model, normalizeToolCallId), isOAuth, undefined, allowEmptySignature).messages.filter((message) => message.role === "assistant");
|
|
533
|
+
const prefix = [...params.messages, ...reply];
|
|
534
|
+
setThreadState(thread.sessionId, {
|
|
535
|
+
prefixCount: prefix.length,
|
|
536
|
+
prefixHash: hashThreadPrefix(model, params, prefix),
|
|
537
|
+
replyId: output.responseId,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
function mergeHeaders(...headerSources) {
|
|
541
|
+
const merged = {};
|
|
542
|
+
for (const headers of headerSources) {
|
|
543
|
+
if (headers) {
|
|
544
|
+
Object.assign(merged, headers);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return merged;
|
|
548
|
+
}
|
|
549
|
+
function hasHeader(headers, name) {
|
|
550
|
+
if (!headers)
|
|
551
|
+
return false;
|
|
552
|
+
const expected = name.toLowerCase();
|
|
553
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
554
|
+
if (key.toLowerCase() === expected && value !== null && value.trim().length > 0)
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
return false;
|
|
558
|
+
}
|
|
559
|
+
function hasRequestAuth(apiKey, headers) {
|
|
560
|
+
return (!!apiKey ||
|
|
561
|
+
hasHeader(headers, "authorization") ||
|
|
562
|
+
hasHeader(headers, "x-api-key") ||
|
|
563
|
+
hasHeader(headers, "cf-aig-authorization"));
|
|
564
|
+
}
|
|
565
|
+
function assertRequestAuth(provider, apiKey, headers) {
|
|
566
|
+
if (!hasRequestAuth(apiKey, headers))
|
|
567
|
+
throw new Error(`No API key for provider: ${provider}`);
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* Anthropic SDK client that never runs the SDK's own credential chain
|
|
571
|
+
* (ANTHROPIC_PROFILE config files, federation env vars). Without this, every
|
|
572
|
+
* client built with `apiKey: null, authToken: null` for header-owned auth would
|
|
573
|
+
* also resolve and exchange SDK credentials behind Atomic's auth resolver.
|
|
574
|
+
*/
|
|
575
|
+
class AtomicAnthropic extends Anthropic {
|
|
576
|
+
admission;
|
|
577
|
+
constructor(options) {
|
|
578
|
+
const admission = getFetchAdmission(options.fetch);
|
|
579
|
+
const tokenAdmission = async (request, next, context) => {
|
|
580
|
+
if (!context.options)
|
|
581
|
+
await admission?.beforeRequest(request.signal ?? undefined);
|
|
582
|
+
return next(request);
|
|
583
|
+
};
|
|
584
|
+
super({
|
|
585
|
+
...options,
|
|
586
|
+
fetch: admission?.fetch ?? options.fetch,
|
|
587
|
+
middleware: admission ? [tokenAdmission, ...(options.middleware ?? [])] : options.middleware,
|
|
588
|
+
});
|
|
589
|
+
this.admission = admission;
|
|
590
|
+
}
|
|
591
|
+
authHeaders(...args) {
|
|
592
|
+
const result = super.authHeaders(...args);
|
|
593
|
+
const signal = args[0].signal;
|
|
594
|
+
return signal ? raceWithAbortSignal(result, signal) : result;
|
|
595
|
+
}
|
|
596
|
+
async prepareRequest(...args) {
|
|
597
|
+
await super.prepareRequest(...args);
|
|
598
|
+
await this.admission?.beforeRequest(args[0].signal ?? undefined);
|
|
599
|
+
}
|
|
600
|
+
_shouldResolveDefaultCredentials() {
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Workload identity federation config from the ANTHROPIC_* variables the
|
|
606
|
+
* Anthropic SDK documents; the SDK performs the token exchange and refresh.
|
|
607
|
+
* Only for the anthropic provider, since the exchange is an Anthropic API
|
|
608
|
+
* endpoint, and only when no key or auth header was resolved.
|
|
609
|
+
*/
|
|
610
|
+
function getAnthropicFederation(model, apiKey, headers, env) {
|
|
611
|
+
if (model.provider !== "anthropic" || hasRequestAuth(apiKey, headers))
|
|
612
|
+
return undefined;
|
|
613
|
+
const federationRuleId = getProviderEnvValue(ANTHROPIC_FEDERATION_RULE_ID_ENV, env);
|
|
614
|
+
const organizationId = getProviderEnvValue(ANTHROPIC_ORGANIZATION_ID_ENV, env);
|
|
615
|
+
const identityTokenFile = getProviderEnvValue(ANTHROPIC_IDENTITY_TOKEN_FILE_ENV, env);
|
|
616
|
+
if (!federationRuleId || !organizationId || !identityTokenFile)
|
|
617
|
+
return undefined;
|
|
618
|
+
return {
|
|
619
|
+
organization_id: organizationId,
|
|
620
|
+
workspace_id: getProviderEnvValue(ANTHROPIC_WORKSPACE_ID_ENV, env),
|
|
621
|
+
authentication: {
|
|
622
|
+
type: "oidc_federation",
|
|
623
|
+
federation_rule_id: federationRuleId,
|
|
624
|
+
service_account_id: getProviderEnvValue(ANTHROPIC_SERVICE_ACCOUNT_ID_ENV, env),
|
|
625
|
+
identity_token: { source: "file", path: identityTokenFile },
|
|
626
|
+
},
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* The SDK caches the federated access token per client, but a client is created
|
|
631
|
+
* per request. Keep one client for the current federation config and fetch, and
|
|
632
|
+
* clone it per request with `withOptions()`, which shares the token cache.
|
|
633
|
+
*/
|
|
634
|
+
let federationClient;
|
|
635
|
+
const ANTHROPIC_MESSAGE_EVENTS = new Set([
|
|
636
|
+
"message_start",
|
|
637
|
+
"message_delta",
|
|
638
|
+
"message_stop",
|
|
639
|
+
"content_block_start",
|
|
640
|
+
"content_block_delta",
|
|
641
|
+
"content_block_stop",
|
|
642
|
+
]);
|
|
643
|
+
const ANTHROPIC_PING = { type: "ping" };
|
|
644
|
+
function isAnthropicPing(event) {
|
|
645
|
+
return event.type === "ping";
|
|
646
|
+
}
|
|
647
|
+
async function* iterateSseMessages(body, signal) {
|
|
648
|
+
const reader = body.getReader();
|
|
649
|
+
const decoder = new ServerSentEventDecoder();
|
|
650
|
+
const onAbort = () => {
|
|
651
|
+
void reader.cancel().catch(() => { });
|
|
652
|
+
};
|
|
653
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
654
|
+
try {
|
|
655
|
+
while (true) {
|
|
656
|
+
if (signal?.aborted) {
|
|
657
|
+
throw new Error("Request was aborted");
|
|
658
|
+
}
|
|
659
|
+
const { value, done } = await reader.read();
|
|
660
|
+
if (done) {
|
|
661
|
+
break;
|
|
662
|
+
}
|
|
663
|
+
yield* decoder.decode(value);
|
|
664
|
+
}
|
|
665
|
+
yield* decoder.end();
|
|
666
|
+
}
|
|
667
|
+
finally {
|
|
668
|
+
signal?.removeEventListener("abort", onAbort);
|
|
669
|
+
try {
|
|
670
|
+
await reader.cancel();
|
|
671
|
+
}
|
|
672
|
+
catch { }
|
|
673
|
+
reader.releaseLock();
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
function parseStreamedErrorBody(data) {
|
|
677
|
+
try {
|
|
678
|
+
return JSON.parse(data) ?? data;
|
|
679
|
+
}
|
|
680
|
+
catch {
|
|
681
|
+
return data;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
function streamedErrorType(body) {
|
|
685
|
+
const error = body.error;
|
|
686
|
+
const type = typeof error === "object" && error !== null ? error.type : undefined;
|
|
687
|
+
return typeof type === "string" ? type : undefined;
|
|
688
|
+
}
|
|
689
|
+
async function* iterateAnthropicEvents(response, signal) {
|
|
690
|
+
if (!response.body) {
|
|
691
|
+
throw new Error("Attempted to iterate over an Anthropic response with no body");
|
|
692
|
+
}
|
|
693
|
+
let sawMessageStart = false;
|
|
694
|
+
let sawMessageEnd = false;
|
|
695
|
+
for await (const sse of iterateSseMessages(response.body, signal)) {
|
|
696
|
+
if (sse.event === "error") {
|
|
697
|
+
const body = parseStreamedErrorBody(sse.data);
|
|
698
|
+
throw new Anthropic.APIError(undefined, body, sse.data, response.headers, streamedErrorType(body));
|
|
699
|
+
}
|
|
700
|
+
if (sse.event === "ping") {
|
|
701
|
+
yield ANTHROPIC_PING;
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
if (!ANTHROPIC_MESSAGE_EVENTS.has(sse.event ?? "")) {
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
try {
|
|
708
|
+
const event = parseJsonWithRepair(sse.data);
|
|
709
|
+
if (event.type === "message_start") {
|
|
710
|
+
sawMessageStart = true;
|
|
711
|
+
}
|
|
712
|
+
else if (event.type === "message_stop") {
|
|
713
|
+
sawMessageEnd = true;
|
|
714
|
+
}
|
|
715
|
+
yield event;
|
|
716
|
+
}
|
|
717
|
+
catch (error) {
|
|
718
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
719
|
+
throw new Error(`Could not parse Anthropic SSE event ${sse.event}: ${message}; data=${sse.data}; raw=${sse.raw.join("\\n")}`);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
if (sawMessageStart && !sawMessageEnd) {
|
|
723
|
+
throw new Error("Anthropic stream ended before message_stop");
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
export const stream = (model, context, options) => {
|
|
727
|
+
const stream = new AssistantMessageEventStream();
|
|
728
|
+
const compat = getAnthropicCompat(model);
|
|
729
|
+
const normalizedContext = resolveTranscript(context, compat.supportsMidConvoSystemMessages);
|
|
730
|
+
const currentTools = getCurrentTools(normalizedContext.messages);
|
|
731
|
+
(async () => {
|
|
732
|
+
const output = {
|
|
733
|
+
role: "assistant",
|
|
734
|
+
content: [],
|
|
735
|
+
api: model.api,
|
|
736
|
+
provider: model.provider,
|
|
737
|
+
model: model.id,
|
|
738
|
+
usage: {
|
|
739
|
+
input: 0,
|
|
740
|
+
output: 0,
|
|
741
|
+
cacheRead: 0,
|
|
742
|
+
cacheWrite: 0,
|
|
743
|
+
totalTokens: 0,
|
|
744
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
745
|
+
},
|
|
746
|
+
stopReason: "pending",
|
|
747
|
+
timestamp: Date.now(),
|
|
748
|
+
};
|
|
749
|
+
const streamDeadline = createStreamDeadline(options?.streamDeadlineMs, options?.signal);
|
|
750
|
+
const streamAttempt = new GitHubCopilotStreamAttempt();
|
|
751
|
+
let inputTransformations;
|
|
752
|
+
try {
|
|
753
|
+
const thinkingOffFallback = getAnthropicThinkingOffFallback(model, options);
|
|
754
|
+
if (thinkingOffFallback) {
|
|
755
|
+
appendThinkingOffFallbackDiagnostic(output, thinkingOffFallback.level, thinkingOffFallback.effort);
|
|
756
|
+
}
|
|
757
|
+
let client;
|
|
758
|
+
let isOAuth;
|
|
759
|
+
let usageModel = model;
|
|
760
|
+
let servedSpeed = requestedSpeed(model);
|
|
761
|
+
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
762
|
+
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
763
|
+
// Session whose requests may use message threads; undefined when threads don't apply. A
|
|
764
|
+
// caller-supplied client carries its own `anthropic-beta` header, which Atomic cannot extend
|
|
765
|
+
// with the threads beta, so it never uses threads. Sessions where the endpoint rejected threads
|
|
766
|
+
// ("off") still send the threads beta, which the resend that turned them off was accepted
|
|
767
|
+
// with; `prepareThreadRequest()` just sends no `thread`.
|
|
768
|
+
const threadSessionId = compat.supportsMessageThreads && !options?.client ? cacheSessionId : undefined;
|
|
769
|
+
if (options?.client) {
|
|
770
|
+
client = options.client;
|
|
771
|
+
isOAuth = false;
|
|
772
|
+
}
|
|
773
|
+
else {
|
|
774
|
+
const apiKey = options?.apiKey;
|
|
775
|
+
const federation = getAnthropicFederation(model, apiKey, options?.headers, options?.env);
|
|
776
|
+
if (!federation)
|
|
777
|
+
assertRequestAuth(model.provider, apiKey, options?.headers);
|
|
778
|
+
let copilotDynamicHeaders;
|
|
779
|
+
if (model.provider === "github-copilot") {
|
|
780
|
+
const hasImages = hasCopilotVisionInput(normalizedContext.messages);
|
|
781
|
+
copilotDynamicHeaders = preserveCopilotIntegrationHeader(model.headers, buildCopilotDynamicHeaders({
|
|
782
|
+
messages: normalizedContext.messages,
|
|
783
|
+
hasImages,
|
|
784
|
+
apiKey,
|
|
785
|
+
}));
|
|
786
|
+
}
|
|
787
|
+
const created = createClient(model, apiKey, model.reasoning && options?.thinkingEnabled === true && (options.interleavedThinking ?? true), shouldUseFineGrainedToolStreamingBeta(model, context), shouldUseServerSideFallbackBeta(model), options?.headers, options?.fetch, copilotDynamicHeaders, cacheSessionId, compat.supportsMidConvoToolChanges &&
|
|
788
|
+
compat.supportsMidConvoSystemMessages &&
|
|
789
|
+
(getInitialSystemMessage(normalizedContext.messages)?.toolsAdded?.length ?? 0) > 0, federation, options?.identity, threadSessionId !== undefined);
|
|
790
|
+
client = created.client;
|
|
791
|
+
isOAuth = created.isOAuthToken;
|
|
792
|
+
}
|
|
793
|
+
const requestOptions = {
|
|
794
|
+
...(streamDeadline.signal ? { signal: streamDeadline.signal } : {}),
|
|
795
|
+
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
|
|
796
|
+
maxRetries: 0,
|
|
797
|
+
};
|
|
798
|
+
const send = (body) => retryProviderRequest(() => {
|
|
799
|
+
streamAttempt.begin();
|
|
800
|
+
return client.beta.messages.create({ ...body, stream: true }, requestOptions).asResponse();
|
|
801
|
+
}, {
|
|
802
|
+
maxRetries: options?.maxRetries,
|
|
803
|
+
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
804
|
+
signal: streamDeadline.signal,
|
|
805
|
+
});
|
|
806
|
+
let disablePreferredStrictTools = hasExceededStrictToolLimits(options?.sessionId);
|
|
807
|
+
let response;
|
|
808
|
+
let params;
|
|
809
|
+
// The thread request that produced the response; undefined when no `thread` was sent.
|
|
810
|
+
let thread;
|
|
811
|
+
for (;;) {
|
|
812
|
+
streamDeadline.signal?.throwIfAborted();
|
|
813
|
+
const built = buildParams(model, normalizedContext, isOAuth, options, {
|
|
814
|
+
disablePreferredStrictTools,
|
|
815
|
+
messageThreads: threadSessionId !== undefined,
|
|
816
|
+
});
|
|
817
|
+
if (built.providerThinkingLevel)
|
|
818
|
+
output.providerThinkingLevel = built.providerThinkingLevel;
|
|
819
|
+
params = built.params;
|
|
820
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
821
|
+
if (nextParams !== undefined)
|
|
822
|
+
params = nextParams;
|
|
823
|
+
assertPayloadPreservesFastRoute(model, params);
|
|
824
|
+
const prepared = threadSessionId !== undefined && options?.disableContinuation !== true
|
|
825
|
+
? prepareThreadRequest(model, threadSessionId, params)
|
|
826
|
+
: undefined;
|
|
827
|
+
try {
|
|
828
|
+
const sent = prepared
|
|
829
|
+
? await sendThreadRequest(prepared, params, send, output, streamDeadline.signal)
|
|
830
|
+
: { response: await send(params), thread: undefined };
|
|
831
|
+
response = sent.response;
|
|
832
|
+
thread = sent.thread;
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
catch (error) {
|
|
836
|
+
if (streamDeadline.signal?.aborted ||
|
|
837
|
+
disablePreferredStrictTools ||
|
|
838
|
+
!built.hasPreferredStrictTools ||
|
|
839
|
+
!(error instanceof Anthropic.APIError) ||
|
|
840
|
+
error.status !== 400 ||
|
|
841
|
+
error.type !== "invalid_request_error" ||
|
|
842
|
+
!isStrictToolLimitErrorMessage(error.message))
|
|
843
|
+
throw error;
|
|
844
|
+
disablePreferredStrictTools = true;
|
|
845
|
+
markStrictToolLimitsExceeded(options?.sessionId);
|
|
846
|
+
appendAssistantMessageDiagnostic(output, {
|
|
847
|
+
type: "anthropic_strict_tools_fallback",
|
|
848
|
+
timestamp: Date.now(),
|
|
849
|
+
error: { message: error.message },
|
|
850
|
+
details: { reason: "strict_tool_limits_exceeded" },
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
855
|
+
stream.push({ type: "start", partial: output });
|
|
856
|
+
streamAttempt.respond(response.headers);
|
|
857
|
+
// Stream state per Anthropic block index, kept off the content blocks so it never reaches partials.
|
|
858
|
+
const openBlocks = new Map();
|
|
859
|
+
const openBlock = (anthropicIndex) => {
|
|
860
|
+
const open = openBlocks.get(anthropicIndex);
|
|
861
|
+
return open === undefined ? undefined : { ...open, block: output.content[open.contentIndex] };
|
|
862
|
+
};
|
|
863
|
+
for await (const event of streamAttempt.observe(withStreamDeadline(iterateAnthropicEvents(response, streamDeadline.signal), streamDeadline.deadlineMs, streamDeadline.abort, isAnthropicPing))) {
|
|
864
|
+
await options?.onProviderStreamEvent?.(event, model);
|
|
865
|
+
if (event.type === "message_start") {
|
|
866
|
+
output.responseId = event.message.id;
|
|
867
|
+
const responseModel = event.message.model;
|
|
868
|
+
if (responseModel !== upstreamModelId(model))
|
|
869
|
+
output.responseModel = responseModel;
|
|
870
|
+
const fallbackCost = responseModel === upstreamModelId(model)
|
|
871
|
+
? undefined
|
|
872
|
+
: model.compat?.allowedFallbackModels?.find((fallback) => fallback.provider === model.provider && fallback.model === responseModel)?.cost;
|
|
873
|
+
usageModel = fallbackCost ? { ...model, id: responseModel, cost: fallbackCost } : model;
|
|
874
|
+
// Capture initial token usage from message_start event
|
|
875
|
+
// This ensures we have input token counts even if the stream is aborted early
|
|
876
|
+
output.usage.input = event.message.usage.input_tokens || 0;
|
|
877
|
+
output.usage.output = event.message.usage.output_tokens || 0;
|
|
878
|
+
output.usage.cacheRead = event.message.usage.cache_read_input_tokens || 0;
|
|
879
|
+
output.usage.cacheWrite = event.message.usage.cache_creation_input_tokens || 0;
|
|
880
|
+
output.usage.cacheWrite1h = event.message.usage.cache_creation?.ephemeral_1h_input_tokens || 0;
|
|
881
|
+
// Anthropic doesn't provide total_tokens, compute from components
|
|
882
|
+
output.usage.totalTokens =
|
|
883
|
+
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
884
|
+
calculateCost(usageModel, output.usage);
|
|
885
|
+
servedSpeed = reportedSpeed(event.message.usage) ?? servedSpeed;
|
|
886
|
+
if (servedSpeed === "fast")
|
|
887
|
+
applyFastModePricing(output.usage);
|
|
888
|
+
inputTransformations = getInputTransformations(event.message) ?? inputTransformations;
|
|
889
|
+
}
|
|
890
|
+
else if (event.type === "content_block_start") {
|
|
891
|
+
if (event.content_block.type === "text") {
|
|
892
|
+
output.content.push({ type: "text", text: event.content_block.text ?? "" });
|
|
893
|
+
openBlocks.set(event.index, { contentIndex: output.content.length - 1 });
|
|
894
|
+
stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
|
|
895
|
+
}
|
|
896
|
+
else if (event.content_block.type === "thinking") {
|
|
897
|
+
output.content.push({
|
|
898
|
+
type: "thinking",
|
|
899
|
+
thinking: event.content_block.thinking ?? "",
|
|
900
|
+
thinkingSignature: event.content_block.signature ?? "",
|
|
901
|
+
});
|
|
902
|
+
openBlocks.set(event.index, { contentIndex: output.content.length - 1 });
|
|
903
|
+
stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
|
|
904
|
+
}
|
|
905
|
+
else if (event.content_block.type === "redacted_thinking") {
|
|
906
|
+
output.content.push({
|
|
907
|
+
type: "thinking",
|
|
908
|
+
thinking: "[Reasoning redacted]",
|
|
909
|
+
thinkingSignature: event.content_block.data,
|
|
910
|
+
redacted: true,
|
|
911
|
+
});
|
|
912
|
+
openBlocks.set(event.index, { contentIndex: output.content.length - 1 });
|
|
913
|
+
stream.push({ type: "thinking_start", contentIndex: output.content.length - 1, partial: output });
|
|
914
|
+
}
|
|
915
|
+
else if (event.content_block.type === "tool_use") {
|
|
916
|
+
output.content.push({
|
|
917
|
+
type: "toolCall",
|
|
918
|
+
id: event.content_block.id,
|
|
919
|
+
name: isOAuth
|
|
920
|
+
? fromClaudeCodeName(event.content_block.name, currentTools)
|
|
921
|
+
: event.content_block.name,
|
|
922
|
+
arguments: event.content_block.input ?? {},
|
|
923
|
+
});
|
|
924
|
+
openBlocks.set(event.index, {
|
|
925
|
+
contentIndex: output.content.length - 1,
|
|
926
|
+
arguments: new ToolCallArgumentsStream(),
|
|
927
|
+
});
|
|
928
|
+
stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
|
|
929
|
+
}
|
|
930
|
+
else if (event.content_block.type === "fallback") {
|
|
931
|
+
// Server-side fallback boundary: "the `fallback` block (an ordinary
|
|
932
|
+
// `content_block_start` and `content_block_stop` pair with no deltas) marks the
|
|
933
|
+
// boundary", and clients must "Keep it exactly where it appeared. The API uses
|
|
934
|
+
// its position to validate the thinking blocks around it".
|
|
935
|
+
// https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback
|
|
936
|
+
//
|
|
937
|
+
// The SDK types lag this block, so read it through a narrow cast.
|
|
938
|
+
const fallbackBlock = event.content_block;
|
|
939
|
+
const toModel = fallbackBlock.to?.model;
|
|
940
|
+
output.content.push({
|
|
941
|
+
type: "fallback",
|
|
942
|
+
fromModel: fallbackBlock.from?.model ?? output.model,
|
|
943
|
+
toModel: toModel ?? output.model,
|
|
944
|
+
});
|
|
945
|
+
// `message_start` named the requested model, so on a mid-output fallback the
|
|
946
|
+
// serving model is only knowable here. Re-derive pricing the same way the
|
|
947
|
+
// `message_start` branch does, so the returned message is costed at the rates
|
|
948
|
+
// of the model that actually produced it.
|
|
949
|
+
if (toModel && toModel !== output.model) {
|
|
950
|
+
output.model = toModel;
|
|
951
|
+
const fallbackCost = model.compat?.allowedFallbackModels?.find((fallback) => fallback.provider === model.provider && fallback.model === toModel)?.cost;
|
|
952
|
+
usageModel = fallbackCost ? { ...model, id: toModel, cost: fallbackCost } : model;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
else if (event.type === "content_block_delta") {
|
|
957
|
+
const open = openBlock(event.index);
|
|
958
|
+
if (event.delta.type === "text_delta") {
|
|
959
|
+
if (open?.block.type === "text") {
|
|
960
|
+
open.block.text += event.delta.text;
|
|
961
|
+
stream.push({
|
|
962
|
+
type: "text_delta",
|
|
963
|
+
contentIndex: open.contentIndex,
|
|
964
|
+
delta: event.delta.text,
|
|
965
|
+
partial: output,
|
|
966
|
+
});
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
else if (event.delta.type === "thinking_delta") {
|
|
970
|
+
if (open?.block.type === "thinking") {
|
|
971
|
+
open.block.thinking += event.delta.thinking;
|
|
972
|
+
stream.push({
|
|
973
|
+
type: "thinking_delta",
|
|
974
|
+
contentIndex: open.contentIndex,
|
|
975
|
+
delta: event.delta.thinking,
|
|
976
|
+
partial: output,
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
else if (event.delta.type === "input_json_delta") {
|
|
981
|
+
if (open?.block.type === "toolCall" && open.arguments !== undefined) {
|
|
982
|
+
open.block.arguments = open.arguments.append(event.delta.partial_json);
|
|
983
|
+
stream.push({
|
|
984
|
+
type: "toolcall_delta",
|
|
985
|
+
contentIndex: open.contentIndex,
|
|
986
|
+
delta: event.delta.partial_json,
|
|
987
|
+
partial: output,
|
|
988
|
+
});
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
else if (event.delta.type === "signature_delta") {
|
|
992
|
+
if (open?.block.type === "thinking") {
|
|
993
|
+
open.block.thinkingSignature = open.block.thinkingSignature || "";
|
|
994
|
+
open.block.thinkingSignature += event.delta.signature;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
else if (event.type === "content_block_stop") {
|
|
999
|
+
const open = openBlock(event.index);
|
|
1000
|
+
openBlocks.delete(event.index);
|
|
1001
|
+
if (open) {
|
|
1002
|
+
const { block, contentIndex: index } = open;
|
|
1003
|
+
if (block.type === "text") {
|
|
1004
|
+
stream.push({
|
|
1005
|
+
type: "text_end",
|
|
1006
|
+
contentIndex: index,
|
|
1007
|
+
content: block.text,
|
|
1008
|
+
partial: output,
|
|
1009
|
+
});
|
|
1010
|
+
}
|
|
1011
|
+
else if (block.type === "thinking") {
|
|
1012
|
+
stream.push({
|
|
1013
|
+
type: "thinking_end",
|
|
1014
|
+
contentIndex: index,
|
|
1015
|
+
content: block.thinking,
|
|
1016
|
+
partial: output,
|
|
1017
|
+
});
|
|
1018
|
+
}
|
|
1019
|
+
else if (block.type === "toolCall" && open.arguments !== undefined) {
|
|
1020
|
+
// Input sent complete in content_block_start has no deltas; keep it.
|
|
1021
|
+
if (open.arguments.text !== "") {
|
|
1022
|
+
Object.assign(block, open.arguments.finish());
|
|
1023
|
+
}
|
|
1024
|
+
stream.push({
|
|
1025
|
+
type: "toolcall_end",
|
|
1026
|
+
contentIndex: index,
|
|
1027
|
+
toolCall: block,
|
|
1028
|
+
partial: output,
|
|
1029
|
+
});
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
else if (event.type === "message_delta") {
|
|
1034
|
+
if (event.delta.stop_reason) {
|
|
1035
|
+
output.rawStopReason = event.delta.stop_reason;
|
|
1036
|
+
const stopReasonResult = mapStopReason(event.delta.stop_reason, event.delta.stop_details);
|
|
1037
|
+
output.stopReason = stopReasonResult.stopReason;
|
|
1038
|
+
if (stopReasonResult.errorMessage) {
|
|
1039
|
+
output.errorMessage = stopReasonResult.errorMessage;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
// Only update usage fields if present (not null).
|
|
1043
|
+
// Preserves input_tokens from message_start when proxies omit it in message_delta.
|
|
1044
|
+
if (event.usage) {
|
|
1045
|
+
if (event.usage.input_tokens != null) {
|
|
1046
|
+
output.usage.input = event.usage.input_tokens;
|
|
1047
|
+
}
|
|
1048
|
+
if (event.usage.output_tokens != null) {
|
|
1049
|
+
output.usage.output = event.usage.output_tokens;
|
|
1050
|
+
}
|
|
1051
|
+
if (event.usage.cache_read_input_tokens != null) {
|
|
1052
|
+
output.usage.cacheRead = event.usage.cache_read_input_tokens;
|
|
1053
|
+
}
|
|
1054
|
+
if (event.usage.cache_creation_input_tokens != null) {
|
|
1055
|
+
output.usage.cacheWrite = event.usage.cache_creation_input_tokens;
|
|
1056
|
+
}
|
|
1057
|
+
// Anthropic reports reasoning tokens in `output_tokens_details.thinking_tokens` on the
|
|
1058
|
+
// final message_delta usage (a subset of output_tokens). SDK 0.91.1 omits the field from
|
|
1059
|
+
// its Usage type, so read it through a narrow cast. Verified against the live API.
|
|
1060
|
+
const thinkingTokens = event.usage
|
|
1061
|
+
.output_tokens_details?.thinking_tokens;
|
|
1062
|
+
// Vercel AI Gateway includes the TTL breakdown in deltas, though the SDK only types it on message_start.
|
|
1063
|
+
const cacheCreation = event.usage.cache_creation;
|
|
1064
|
+
if (cacheCreation?.ephemeral_1h_input_tokens != null) {
|
|
1065
|
+
output.usage.cacheWrite1h = cacheCreation.ephemeral_1h_input_tokens;
|
|
1066
|
+
}
|
|
1067
|
+
if (thinkingTokens != null) {
|
|
1068
|
+
output.usage.reasoning = thinkingTokens;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
// Anthropic doesn't provide total_tokens, compute from components
|
|
1072
|
+
output.usage.totalTokens =
|
|
1073
|
+
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
1074
|
+
calculateCost(usageModel, output.usage);
|
|
1075
|
+
servedSpeed = reportedSpeed(event.usage) ?? servedSpeed;
|
|
1076
|
+
if (servedSpeed === "fast")
|
|
1077
|
+
applyFastModePricing(output.usage);
|
|
1078
|
+
// A non-empty final fallback report supersedes the request-start report with
|
|
1079
|
+
// the serving model's transformations. An empty report must not erase drops
|
|
1080
|
+
// already reported at message_start.
|
|
1081
|
+
const servingTransformations = getInputTransformations(event);
|
|
1082
|
+
if (servingTransformations && servingTransformations.length > 0) {
|
|
1083
|
+
inputTransformations = servingTransformations;
|
|
1084
|
+
}
|
|
1085
|
+
addEarlierAttemptCosts(output, model, usageModel, event);
|
|
1086
|
+
if (isOpenRouterModel(model) && event.usage) {
|
|
1087
|
+
// OpenRouter adds billing fields to streamed message_delta usage at runtime,
|
|
1088
|
+
// although its OpenAPI schema and the Anthropic SDK do not type those fields.
|
|
1089
|
+
applyOpenRouterReportedCost(output.usage, event.usage);
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
if (inputTransformations)
|
|
1094
|
+
recordInputTransformations(output, inputTransformations);
|
|
1095
|
+
if (options?.signal?.aborted) {
|
|
1096
|
+
throw new Error("Request was aborted");
|
|
1097
|
+
}
|
|
1098
|
+
if (output.stopReason === "pending") {
|
|
1099
|
+
throw new Error("Anthropic stream ended without a stop reason");
|
|
1100
|
+
}
|
|
1101
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
1102
|
+
throw new Error(output.errorMessage || "An unknown error occurred");
|
|
1103
|
+
}
|
|
1104
|
+
if (thread)
|
|
1105
|
+
recordThreadReply(model, thread, params, output, isOAuth, compat.allowEmptySignature);
|
|
1106
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
1107
|
+
stream.end();
|
|
1108
|
+
}
|
|
1109
|
+
catch (error) {
|
|
1110
|
+
if (inputTransformations &&
|
|
1111
|
+
!output.diagnostics?.some((diagnostic) => diagnostic.type === "anthropic_input_transformations")) {
|
|
1112
|
+
recordInputTransformations(output, inputTransformations);
|
|
1113
|
+
}
|
|
1114
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
1115
|
+
output.errorMessage = error instanceof Error ? withCauseDetail(error) : JSON.stringify(error);
|
|
1116
|
+
appendProviderResponseFailureDiagnostic(output, error);
|
|
1117
|
+
output.retryAfterMs = error instanceof Error ? getProviderRetryAfterMs(error) : undefined;
|
|
1118
|
+
if (model.provider === "github-copilot")
|
|
1119
|
+
recordGitHubCopilotFailure(output, error, streamAttempt.snapshot());
|
|
1120
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
1121
|
+
stream.end();
|
|
1122
|
+
}
|
|
1123
|
+
finally {
|
|
1124
|
+
streamDeadline.cleanup();
|
|
1125
|
+
}
|
|
1126
|
+
})();
|
|
1127
|
+
return stream;
|
|
1128
|
+
};
|
|
1129
|
+
/**
|
|
1130
|
+
* Map ThinkingLevel to Anthropic effort levels for adaptive thinking.
|
|
1131
|
+
* Note: effort "max" is available on all adaptive-thinking Claude models, while native
|
|
1132
|
+
* "xhigh" is available on Opus 4.7, Opus 4.8, Opus 5, Sonnet 5, Fable 5, and Fable 5.1 —
|
|
1133
|
+
* the models the generator merges an `xhigh` mapping onto.
|
|
1134
|
+
*/
|
|
1135
|
+
function mapThinkingLevelToEffort(model, level) {
|
|
1136
|
+
const mapped = level ? model.thinkingLevelMap?.[level] : undefined;
|
|
1137
|
+
if (typeof mapped === "string")
|
|
1138
|
+
return mapped;
|
|
1139
|
+
switch (level) {
|
|
1140
|
+
case "minimal":
|
|
1141
|
+
case "low":
|
|
1142
|
+
return "low";
|
|
1143
|
+
case "medium":
|
|
1144
|
+
return "medium";
|
|
1145
|
+
case "high":
|
|
1146
|
+
return "high";
|
|
1147
|
+
default:
|
|
1148
|
+
return "high";
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Thinking `off` on an adaptive model that cannot disable thinking runs at the model's lowest effort.
|
|
1153
|
+
* Sending no effort would leave the API's default, which is `high`.
|
|
1154
|
+
*/
|
|
1155
|
+
function getAnthropicThinkingOffFallback(model, options) {
|
|
1156
|
+
if (options?.thinkingEnabled !== false)
|
|
1157
|
+
return undefined;
|
|
1158
|
+
if (model.compat?.forceAdaptiveThinking !== true && !supportsMidConvoEffort(model))
|
|
1159
|
+
return undefined;
|
|
1160
|
+
const level = getThinkingOffFallbackLevel(model);
|
|
1161
|
+
return level === undefined ? undefined : { level, effort: mapThinkingLevelToEffort(model, level) };
|
|
1162
|
+
}
|
|
1163
|
+
export const streamSimple = (model, context, options) => {
|
|
1164
|
+
if (!getAnthropicFederation(model, options?.apiKey, options?.headers, options?.env)) {
|
|
1165
|
+
assertRequestAuth(model.provider, options?.apiKey, options?.headers);
|
|
1166
|
+
}
|
|
1167
|
+
const base = {
|
|
1168
|
+
...buildBaseOptions(model, context, options, options?.apiKey),
|
|
1169
|
+
toolChoice: options?.toolChoice,
|
|
1170
|
+
};
|
|
1171
|
+
if (!options?.reasoning) {
|
|
1172
|
+
return stream(model, context, {
|
|
1173
|
+
...base,
|
|
1174
|
+
thinkingEnabled: false,
|
|
1175
|
+
});
|
|
1176
|
+
}
|
|
1177
|
+
// For models with adaptive thinking: use an effort level.
|
|
1178
|
+
// For older models: use budget-based thinking.
|
|
1179
|
+
if (model.compat?.forceAdaptiveThinking === true) {
|
|
1180
|
+
const effort = mapThinkingLevelToEffort(model, options.reasoning);
|
|
1181
|
+
return stream(model, context, {
|
|
1182
|
+
...base,
|
|
1183
|
+
thinkingEnabled: true,
|
|
1184
|
+
effort,
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
// Undefined means the caller did not request an output cap; let the helper use the model cap.
|
|
1188
|
+
// Do not coerce to 0 here, or the thinking budget would become the entire max_tokens value.
|
|
1189
|
+
const adjusted = adjustMaxTokensForThinking(base.maxTokens, model.maxTokens, options.reasoning, options.thinkingBudgets);
|
|
1190
|
+
const maxTokens = clampMaxTokensToContext(model, context, adjusted.maxTokens);
|
|
1191
|
+
return stream(model, context, {
|
|
1192
|
+
...base,
|
|
1193
|
+
maxTokens,
|
|
1194
|
+
thinkingEnabled: true,
|
|
1195
|
+
thinkingBudgetTokens: Math.min(adjusted.thinkingBudget, Math.max(0, maxTokens - 1024)),
|
|
1196
|
+
});
|
|
1197
|
+
};
|
|
1198
|
+
function isOAuthToken(apiKey) {
|
|
1199
|
+
return apiKey.includes("sk-ant-oat");
|
|
1200
|
+
}
|
|
1201
|
+
function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId, useNativeToolChanges = false, federation, identity, useMessageThreads = false) {
|
|
1202
|
+
// Adaptive thinking models have interleaved thinking built in, so skip the beta header.
|
|
1203
|
+
const needsInterleavedBeta = interleavedThinking && model.compat?.forceAdaptiveThinking !== true;
|
|
1204
|
+
const betaFeatures = [];
|
|
1205
|
+
if (useFineGrainedToolStreamingBeta) {
|
|
1206
|
+
betaFeatures.push(FINE_GRAINED_TOOL_STREAMING_BETA);
|
|
1207
|
+
}
|
|
1208
|
+
if (needsInterleavedBeta) {
|
|
1209
|
+
betaFeatures.push(INTERLEAVED_THINKING_BETA);
|
|
1210
|
+
}
|
|
1211
|
+
if (useServerSideFallbackBeta) {
|
|
1212
|
+
betaFeatures.push(SERVER_SIDE_FALLBACK_BETA);
|
|
1213
|
+
}
|
|
1214
|
+
if (shouldUseThinkingBindingControlsBeta(model)) {
|
|
1215
|
+
betaFeatures.push(THINKING_BINDING_CONTROLS_BETA);
|
|
1216
|
+
}
|
|
1217
|
+
if (supportsMidConvoEffort(model)) {
|
|
1218
|
+
betaFeatures.push(MID_CONVERSATION_OUTPUT_CONFIG_BETA, THINKING_BINDING_CONTROLS_BETA);
|
|
1219
|
+
}
|
|
1220
|
+
if (useNativeToolChanges)
|
|
1221
|
+
betaFeatures.push(INLINE_TOOLS_BETA);
|
|
1222
|
+
if (requestedSpeed(model) === "fast")
|
|
1223
|
+
betaFeatures.push(FAST_MODE_BETA);
|
|
1224
|
+
if (useMessageThreads)
|
|
1225
|
+
betaFeatures.push(MESSAGE_THREADS_BETA);
|
|
1226
|
+
const uniqueBetaFeatures = [...new Set(betaFeatures)];
|
|
1227
|
+
// Copilot: Bearer auth, selective betas.
|
|
1228
|
+
if (model.provider === "github-copilot") {
|
|
1229
|
+
const client = new AtomicAnthropic({
|
|
1230
|
+
apiKey: null,
|
|
1231
|
+
authToken: apiKey ?? null,
|
|
1232
|
+
baseURL: model.baseUrl,
|
|
1233
|
+
dangerouslyAllowBrowser: true,
|
|
1234
|
+
fetch,
|
|
1235
|
+
defaultHeaders: mergeHeaders(getIdentityHeaders(model, identity), {
|
|
1236
|
+
accept: "application/json",
|
|
1237
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
1238
|
+
...(uniqueBetaFeatures.length > 0 ? { "anthropic-beta": uniqueBetaFeatures.join(",") } : {}),
|
|
1239
|
+
}, model.headers, dynamicHeaders, optionsHeaders),
|
|
1240
|
+
});
|
|
1241
|
+
return { client, isOAuthToken: false };
|
|
1242
|
+
}
|
|
1243
|
+
// OAuth: Bearer auth, Claude Code identity headers instead of the client identity.
|
|
1244
|
+
if (apiKey && isOAuthToken(apiKey)) {
|
|
1245
|
+
const client = new AtomicAnthropic({
|
|
1246
|
+
apiKey: null,
|
|
1247
|
+
authToken: apiKey,
|
|
1248
|
+
baseURL: model.baseUrl,
|
|
1249
|
+
dangerouslyAllowBrowser: true,
|
|
1250
|
+
fetch,
|
|
1251
|
+
defaultHeaders: mergeHeaders({
|
|
1252
|
+
accept: "application/json",
|
|
1253
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
1254
|
+
"anthropic-beta": ["claude-code-20250219", "oauth-2025-04-20", ...uniqueBetaFeatures].join(","),
|
|
1255
|
+
"user-agent": `claude-cli/${claudeCodeVersion}`,
|
|
1256
|
+
"x-app": "cli",
|
|
1257
|
+
}, model.headers, optionsHeaders),
|
|
1258
|
+
});
|
|
1259
|
+
return { client, isOAuthToken: true };
|
|
1260
|
+
}
|
|
1261
|
+
// API key, header-owned auth, or workload identity federation.
|
|
1262
|
+
const compat = getAnthropicCompat(model);
|
|
1263
|
+
const sessionAffinityHeaders = {};
|
|
1264
|
+
if (sessionId && compat.sendSessionAffinityHeaders) {
|
|
1265
|
+
const header = compat.sessionAffinityFormat === "openrouter" ? "x-session-id" : "x-session-affinity";
|
|
1266
|
+
sessionAffinityHeaders[header] = sessionId;
|
|
1267
|
+
}
|
|
1268
|
+
const defaultHeaders = mergeHeaders(getIdentityHeaders(model, identity), {
|
|
1269
|
+
accept: "application/json",
|
|
1270
|
+
"anthropic-dangerous-direct-browser-access": "true",
|
|
1271
|
+
...(uniqueBetaFeatures.length > 0 ? { "anthropic-beta": uniqueBetaFeatures.join(",") } : {}),
|
|
1272
|
+
}, sessionAffinityHeaders, model.headers, optionsHeaders);
|
|
1273
|
+
if (federation) {
|
|
1274
|
+
const key = JSON.stringify([model.baseUrl, federation]);
|
|
1275
|
+
if (federationClient?.key !== key || federationClient.fetch !== fetch) {
|
|
1276
|
+
const client = new AtomicAnthropic({
|
|
1277
|
+
apiKey: null,
|
|
1278
|
+
authToken: null,
|
|
1279
|
+
config: federation,
|
|
1280
|
+
baseURL: model.baseUrl,
|
|
1281
|
+
dangerouslyAllowBrowser: true,
|
|
1282
|
+
fetch,
|
|
1283
|
+
});
|
|
1284
|
+
federationClient = { key, fetch, client };
|
|
1285
|
+
}
|
|
1286
|
+
return { client: federationClient.client.withOptions({ defaultHeaders, fetch }), isOAuthToken: false };
|
|
1287
|
+
}
|
|
1288
|
+
const client = new AtomicAnthropic({
|
|
1289
|
+
apiKey: apiKey ?? null,
|
|
1290
|
+
authToken: null,
|
|
1291
|
+
baseURL: model.baseUrl,
|
|
1292
|
+
dangerouslyAllowBrowser: true,
|
|
1293
|
+
fetch,
|
|
1294
|
+
defaultHeaders,
|
|
1295
|
+
});
|
|
1296
|
+
return { client, isOAuthToken: false };
|
|
1297
|
+
}
|
|
1298
|
+
function getNativeToolChangeBetas(model, context, isOAuth, options, messageThreads) {
|
|
1299
|
+
let configured;
|
|
1300
|
+
for (const headers of [model.headers, options?.headers]) {
|
|
1301
|
+
for (const [name, value] of Object.entries(headers ?? {})) {
|
|
1302
|
+
if (name.toLowerCase() === "anthropic-beta")
|
|
1303
|
+
configured = value;
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
if (configured === null)
|
|
1307
|
+
return [];
|
|
1308
|
+
if (configured !== undefined)
|
|
1309
|
+
return [
|
|
1310
|
+
...new Set(configured
|
|
1311
|
+
.split(",")
|
|
1312
|
+
.map((feature) => feature.trim())
|
|
1313
|
+
.filter(Boolean)),
|
|
1314
|
+
];
|
|
1315
|
+
const features = [INLINE_TOOLS_BETA];
|
|
1316
|
+
if (isOAuth)
|
|
1317
|
+
features.push("claude-code-20250219", "oauth-2025-04-20");
|
|
1318
|
+
if (shouldUseFineGrainedToolStreamingBeta(model, context))
|
|
1319
|
+
features.push(FINE_GRAINED_TOOL_STREAMING_BETA);
|
|
1320
|
+
if (model.reasoning &&
|
|
1321
|
+
options?.thinkingEnabled &&
|
|
1322
|
+
(options.interleavedThinking ?? true) &&
|
|
1323
|
+
model.compat?.forceAdaptiveThinking !== true) {
|
|
1324
|
+
features.push(INTERLEAVED_THINKING_BETA);
|
|
1325
|
+
}
|
|
1326
|
+
if (shouldUseServerSideFallbackBeta(model))
|
|
1327
|
+
features.push(SERVER_SIDE_FALLBACK_BETA);
|
|
1328
|
+
if (shouldUseThinkingBindingControlsBeta(model))
|
|
1329
|
+
features.push(THINKING_BINDING_CONTROLS_BETA);
|
|
1330
|
+
if (supportsMidConvoEffort(model))
|
|
1331
|
+
features.push(MID_CONVERSATION_OUTPUT_CONFIG_BETA, THINKING_BINDING_CONTROLS_BETA);
|
|
1332
|
+
if (requestedSpeed(model) === "fast")
|
|
1333
|
+
features.push(FAST_MODE_BETA);
|
|
1334
|
+
if (messageThreads)
|
|
1335
|
+
features.push(MESSAGE_THREADS_BETA);
|
|
1336
|
+
return [...new Set(features)];
|
|
1337
|
+
}
|
|
1338
|
+
function buildParams(model, context, isOAuthToken, options, { disablePreferredStrictTools, messageThreads }) {
|
|
1339
|
+
const { cacheControl } = getCacheControl(model, options?.cacheRetention, options?.env);
|
|
1340
|
+
const compat = getAnthropicCompat(model);
|
|
1341
|
+
const initialSystemMessage = getInitialSystemMessage(context.messages);
|
|
1342
|
+
const initialSystemText = initialSystemMessage ? getSystemMessageText(initialSystemMessage) : "";
|
|
1343
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
1344
|
+
const conversationMessages = initialSystemMessage ? transformedMessages.slice(1) : transformedMessages;
|
|
1345
|
+
let hasPreferredStrictTools = false;
|
|
1346
|
+
const convertToolDefinitions = (tools, toolCacheControl) => {
|
|
1347
|
+
if (isOAuthToken)
|
|
1348
|
+
tools = [...new Map(tools.map((tool) => [toClaudeCodeName(tool.name), tool])).values()];
|
|
1349
|
+
const definitions = convertTools(tools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl, disablePreferredStrictTools);
|
|
1350
|
+
if (definitions.some((definition, index) => definition.strict === true && prefersJsonSchemaStrictSampling(tools[index])))
|
|
1351
|
+
hasPreferredStrictTools = true;
|
|
1352
|
+
return definitions;
|
|
1353
|
+
};
|
|
1354
|
+
const initialTools = initialSystemMessage?.toolsAdded ?? [];
|
|
1355
|
+
const nativeToolChanges = compat.supportsMidConvoSystemMessages && compat.supportsMidConvoToolChanges && initialTools.length > 0;
|
|
1356
|
+
const thinkingOffFallback = getAnthropicThinkingOffFallback(model, options);
|
|
1357
|
+
const thinkingDisabled = model.reasoning && options?.thinkingEnabled === false && model.thinkingLevelMap?.off !== null;
|
|
1358
|
+
// Effort cannot change between messages while thinking is disabled, so "off" sends no effort markers.
|
|
1359
|
+
const effortMessages = supportsMidConvoEffort(model) && !thinkingDisabled;
|
|
1360
|
+
const converted = convertMessages(conversationMessages, isOAuthToken, cacheControl, compat.allowEmptySignature, effortMessages ? model.provider : undefined, nativeToolChanges ? convertToolDefinitions : undefined);
|
|
1361
|
+
const activeEffort = options?.effort ?? thinkingOffFallback?.effort ?? "high";
|
|
1362
|
+
const params = {
|
|
1363
|
+
model: upstreamModelId(model),
|
|
1364
|
+
messages: (effortMessages
|
|
1365
|
+
? insertThinkingLevelMessages(converted, activeEffort)
|
|
1366
|
+
: converted.messages),
|
|
1367
|
+
max_tokens: options?.maxTokens ?? model.maxTokens,
|
|
1368
|
+
stream: true,
|
|
1369
|
+
};
|
|
1370
|
+
if (nativeToolChanges)
|
|
1371
|
+
params.betas = getNativeToolChangeBetas(model, context, isOAuthToken, options, messageThreads);
|
|
1372
|
+
const speed = requestedSpeed(model);
|
|
1373
|
+
if (speed !== undefined)
|
|
1374
|
+
params.speed = speed;
|
|
1375
|
+
// For OAuth tokens, we MUST include Claude Code identity
|
|
1376
|
+
if (isOAuthToken) {
|
|
1377
|
+
params.system = [
|
|
1378
|
+
{
|
|
1379
|
+
type: "text",
|
|
1380
|
+
text: "You are Claude Code, Anthropic's official CLI for Claude.",
|
|
1381
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
1382
|
+
},
|
|
1383
|
+
];
|
|
1384
|
+
if (initialSystemText) {
|
|
1385
|
+
params.system.push({
|
|
1386
|
+
type: "text",
|
|
1387
|
+
text: sanitizeSurrogates(initialSystemText),
|
|
1388
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
1389
|
+
});
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
else if (initialSystemText) {
|
|
1393
|
+
// Add cache control to system prompt for non-OAuth tokens
|
|
1394
|
+
params.system = [
|
|
1395
|
+
{
|
|
1396
|
+
type: "text",
|
|
1397
|
+
text: sanitizeSurrogates(initialSystemText),
|
|
1398
|
+
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
1399
|
+
},
|
|
1400
|
+
];
|
|
1401
|
+
}
|
|
1402
|
+
// Temperature is incompatible with extended thinking and unsupported on Claude Opus 4.7+.
|
|
1403
|
+
if (options?.temperature !== undefined &&
|
|
1404
|
+
!options?.thinkingEnabled &&
|
|
1405
|
+
!thinkingOffFallback &&
|
|
1406
|
+
!supportsMidConvoEffort(model) &&
|
|
1407
|
+
compat.supportsTemperature) {
|
|
1408
|
+
params.temperature = options.temperature;
|
|
1409
|
+
}
|
|
1410
|
+
const toolCacheControl = compat.supportsCacheControlOnTools ? cacheControl : undefined;
|
|
1411
|
+
if (nativeToolChanges) {
|
|
1412
|
+
params.tools = [...convertToolDefinitions(initialTools, toolCacheControl), DEFERRED_TOOL_PLACEHOLDER];
|
|
1413
|
+
}
|
|
1414
|
+
else {
|
|
1415
|
+
const tools = getCurrentTools(context.messages);
|
|
1416
|
+
if (tools.length > 0) {
|
|
1417
|
+
params.tools = convertToolDefinitions(tools, toolCacheControl);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
// Managed effort models always use adaptive thinking. Their per-turn markers
|
|
1421
|
+
// carry the actual effort for every reply.
|
|
1422
|
+
if (effortMessages) {
|
|
1423
|
+
params.thinking = {
|
|
1424
|
+
type: "adaptive",
|
|
1425
|
+
display: options?.thinkingDisplay ?? "summarized",
|
|
1426
|
+
block_binding: { prefix_mismatch_behavior: "drop_block" },
|
|
1427
|
+
};
|
|
1428
|
+
// The top-level effort is part of the cached prompt, so it stays fixed. It is "low" because
|
|
1429
|
+
// organizations can cap effort and reject requests containing any level above the cap.
|
|
1430
|
+
params.output_config = { effort: "low" };
|
|
1431
|
+
}
|
|
1432
|
+
else if (model.reasoning) {
|
|
1433
|
+
if (options?.thinkingEnabled || thinkingOffFallback) {
|
|
1434
|
+
// Default to "summarized" so Opus 4.7 and Mythos Preview behave like
|
|
1435
|
+
// older Claude 4 models (whose API default is also "summarized").
|
|
1436
|
+
const display = options?.thinkingDisplay ?? "summarized";
|
|
1437
|
+
const effort = options?.effort ?? thinkingOffFallback?.effort;
|
|
1438
|
+
if (model.compat?.forceAdaptiveThinking === true) {
|
|
1439
|
+
// Adaptive thinking: Claude decides when and how much to think.
|
|
1440
|
+
params.thinking = { type: "adaptive", display };
|
|
1441
|
+
if (effort) {
|
|
1442
|
+
// The Anthropic SDK types can lag newly supported effort values such as "xhigh".
|
|
1443
|
+
params.output_config =
|
|
1444
|
+
effort === "xhigh"
|
|
1445
|
+
? { effort }
|
|
1446
|
+
: { effort };
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
else {
|
|
1450
|
+
// Budget-based thinking for older models
|
|
1451
|
+
params.thinking = {
|
|
1452
|
+
type: "enabled",
|
|
1453
|
+
budget_tokens: options?.thinkingBudgetTokens || 1024,
|
|
1454
|
+
display,
|
|
1455
|
+
};
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
else if (options?.thinkingEnabled === false && model.thinkingLevelMap?.off !== null) {
|
|
1459
|
+
params.thinking = { type: "disabled" };
|
|
1460
|
+
}
|
|
1461
|
+
// `block_binding` must accompany the beta header on *every* request for a model that
|
|
1462
|
+
// enforces the conversation check, not only on reasoning turns. The header alone leaves
|
|
1463
|
+
// `prefix_mismatch_behavior` at its `"error"` default, which is the 400 this opts out of.
|
|
1464
|
+
// `streamSimple` sets `thinkingEnabled: false` whenever no reasoning level is requested,
|
|
1465
|
+
// and Claude Fable 5.1 denies thinking-off (`thinkingLevelMap.off === null`), so without
|
|
1466
|
+
// this the no-reasoning path would send the header with no field at all.
|
|
1467
|
+
if (shouldUseThinkingBindingControlsBeta(model)) {
|
|
1468
|
+
const blockBinding = { block_binding: { prefix_mismatch_behavior: "drop_block" } };
|
|
1469
|
+
// The Anthropic SDK types lag the `thinking-binding-controls-2026-08-01` beta.
|
|
1470
|
+
if (params.thinking?.type === "adaptive" || params.thinking?.type === "enabled") {
|
|
1471
|
+
// Accepted alongside both thinking types. It only changes what happens to a block
|
|
1472
|
+
// replayed behind a changed prefix; the model check always drops regardless.
|
|
1473
|
+
params.thinking = { ...params.thinking, ...blockBinding };
|
|
1474
|
+
}
|
|
1475
|
+
else if (!params.thinking && model.compat?.forceAdaptiveThinking === true) {
|
|
1476
|
+
// Adaptive thinking is always on for these models, so omitting `thinking` and
|
|
1477
|
+
// sending `{type: "adaptive"}` are equivalent. `display` stays absent to keep the
|
|
1478
|
+
// API's `"omitted"` default, which is exactly what omitting `thinking` produced.
|
|
1479
|
+
params.thinking = { type: "adaptive", ...blockBinding };
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
if (options?.metadata) {
|
|
1484
|
+
const userId = options.metadata.user_id;
|
|
1485
|
+
if (typeof userId === "string") {
|
|
1486
|
+
params.metadata = { user_id: userId };
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
if (options?.toolChoice) {
|
|
1490
|
+
const requested = options.toolChoice;
|
|
1491
|
+
const isForced = requested === "any" || (typeof requested !== "string" && requested.type === "tool");
|
|
1492
|
+
if (isForced && model.compat?.supportsForcedToolChoice === false) {
|
|
1493
|
+
// "The exceptions are Claude Fable 5.1 and Claude Mythos 5.1, which reject forced tool
|
|
1494
|
+
// use on every request with a 400 error. On those models, use
|
|
1495
|
+
// `tool_choice: {"type": "auto"}` with strict tool use or structured outputs instead."
|
|
1496
|
+
// https://platform.claude.com/docs/en/build-with-claude/thinking
|
|
1497
|
+
//
|
|
1498
|
+
// Reject the request rather than rewriting it. Silently substituting `auto` would
|
|
1499
|
+
// discard an explicit caller instruction and make `AnthropicOptions.toolChoice`'s
|
|
1500
|
+
// declared shape a lie; the caller asked the model to call a tool, and quietly asking
|
|
1501
|
+
// it to decide instead is a different request. Failing here matches how this package
|
|
1502
|
+
// handles other explicitly requested capabilities a model cannot honor, and surfaces
|
|
1503
|
+
// the remedy before a round trip that would 400 anyway. Callers that want the
|
|
1504
|
+
// substitution can make it themselves, and can branch on
|
|
1505
|
+
// `model.compat.supportsForcedToolChoice` to decide.
|
|
1506
|
+
const requestedLabel = typeof requested === "string" ? requested : `tool "${requested.name}"`;
|
|
1507
|
+
throw new Error(`Model ${model.id} does not support forced tool choice (requested: ${requestedLabel}). ` +
|
|
1508
|
+
`Use toolChoice "auto" with strict tool use or structured outputs instead.`);
|
|
1509
|
+
}
|
|
1510
|
+
if (typeof requested === "string") {
|
|
1511
|
+
params.tool_choice = { type: requested };
|
|
1512
|
+
}
|
|
1513
|
+
else {
|
|
1514
|
+
params.tool_choice = requested;
|
|
1515
|
+
}
|
|
1516
|
+
}
|
|
1517
|
+
const allowedFallbackModels = model.compat?.allowedFallbackModels;
|
|
1518
|
+
if (allowedFallbackModels && allowedFallbackModels.length > 0) {
|
|
1519
|
+
params.fallbacks = allowedFallbackModels.map((fallback) => ({ model: fallback.model }));
|
|
1520
|
+
}
|
|
1521
|
+
return { params, hasPreferredStrictTools, providerThinkingLevel: effortMessages ? activeEffort : undefined };
|
|
1522
|
+
}
|
|
1523
|
+
// Normalize tool call IDs to match Anthropic's required pattern and length
|
|
1524
|
+
function normalizeToolCallId(id) {
|
|
1525
|
+
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
1526
|
+
}
|
|
1527
|
+
function convertToolResult(msg, content = msg.content) {
|
|
1528
|
+
return {
|
|
1529
|
+
type: "tool_result",
|
|
1530
|
+
tool_use_id: msg.toolCallId,
|
|
1531
|
+
content: convertContentBlocks(content),
|
|
1532
|
+
is_error: msg.isError,
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowEmptySignature = false, managedProvider, convertToolDefinitions) {
|
|
1536
|
+
const params = [];
|
|
1537
|
+
const assistantLevels = new Map();
|
|
1538
|
+
// Later system messages are held back and emitted directly before the next assistant
|
|
1539
|
+
// message that is sent (or at the end of the transcript). Assistant messages that convert to
|
|
1540
|
+
// no blocks are dropped, so holding past them keeps an update from landing before a user
|
|
1541
|
+
// message (pi #11091). Anthropic requires `tool_result` blocks to
|
|
1542
|
+
// immediately follow their `tool_use`, so a system message between them is rejected; this
|
|
1543
|
+
// also mirrors where the managed-effort system messages are inserted. As a result an
|
|
1544
|
+
// update placed before a user message in the transcript lands after it on the wire.
|
|
1545
|
+
const pendingSystemMessages = [];
|
|
1546
|
+
const flushPendingSystemMessages = () => {
|
|
1547
|
+
params.push(...pendingSystemMessages);
|
|
1548
|
+
pendingSystemMessages.length = 0;
|
|
1549
|
+
};
|
|
1550
|
+
for (let i = 0; i < transformedMessages.length; i++) {
|
|
1551
|
+
const msg = transformedMessages[i];
|
|
1552
|
+
if (msg.role === "system") {
|
|
1553
|
+
// Later system messages only reach this point when the model accepts them natively;
|
|
1554
|
+
// otherwise the transcript was collapsed into the leading message before conversion.
|
|
1555
|
+
const text = renderSystemMessageUpdate(msg);
|
|
1556
|
+
const blocks = [];
|
|
1557
|
+
if (text.length > 0)
|
|
1558
|
+
blocks.push({ type: "text", text: sanitizeSurrogates(text) });
|
|
1559
|
+
if (convertToolDefinitions) {
|
|
1560
|
+
const added = msg.toolsAdded ?? [];
|
|
1561
|
+
const redefined = new Set(added.map((tool) => tool.name));
|
|
1562
|
+
for (const tool of msg.toolsRemoved ?? []) {
|
|
1563
|
+
if (redefined.has(tool.name))
|
|
1564
|
+
continue;
|
|
1565
|
+
blocks.push({
|
|
1566
|
+
type: "tool_removal",
|
|
1567
|
+
tool: { type: "tool_reference", name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name },
|
|
1568
|
+
});
|
|
1569
|
+
}
|
|
1570
|
+
for (const definition of convertToolDefinitions(added)) {
|
|
1571
|
+
blocks.push({ type: "tool_addition", tool: { type: "tool_definition", definition } });
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
if (blocks.length > 0)
|
|
1575
|
+
pendingSystemMessages.push({ role: "system", content: blocks });
|
|
1576
|
+
}
|
|
1577
|
+
else if (msg.role === "user") {
|
|
1578
|
+
if (typeof msg.content === "string") {
|
|
1579
|
+
if (msg.content.trim().length > 0) {
|
|
1580
|
+
params.push({
|
|
1581
|
+
role: "user",
|
|
1582
|
+
content: sanitizeSurrogates(msg.content),
|
|
1583
|
+
});
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
else {
|
|
1587
|
+
const blocks = msg.content.map((item) => {
|
|
1588
|
+
if (item.type === "text") {
|
|
1589
|
+
return {
|
|
1590
|
+
type: "text",
|
|
1591
|
+
text: sanitizeSurrogates(item.text),
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
if (item.type === "document") {
|
|
1595
|
+
// `BetaBase64PDFSource`. PDFs ride Claude's vision path, so no beta header.
|
|
1596
|
+
// The media type is a fixed literal in that SDK type, so it is hardcoded here
|
|
1597
|
+
// and the block's own field is verified rather than read.
|
|
1598
|
+
// https://platform.claude.com/docs/en/build-with-claude/pdf-support
|
|
1599
|
+
assertSupportedDocumentMimeType(item);
|
|
1600
|
+
return {
|
|
1601
|
+
type: "document",
|
|
1602
|
+
source: {
|
|
1603
|
+
type: "base64",
|
|
1604
|
+
media_type: "application/pdf",
|
|
1605
|
+
data: item.data,
|
|
1606
|
+
},
|
|
1607
|
+
...(item.name ? { title: item.name } : {}),
|
|
1608
|
+
};
|
|
1609
|
+
}
|
|
1610
|
+
return convertImageBlock(item);
|
|
1611
|
+
});
|
|
1612
|
+
const filteredBlocks = blocks.filter((b) => {
|
|
1613
|
+
if (b.type === "text") {
|
|
1614
|
+
return b.text.trim().length > 0;
|
|
1615
|
+
}
|
|
1616
|
+
return true;
|
|
1617
|
+
});
|
|
1618
|
+
if (filteredBlocks.length === 0)
|
|
1619
|
+
continue;
|
|
1620
|
+
params.push({
|
|
1621
|
+
role: "user",
|
|
1622
|
+
content: filteredBlocks,
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
else if (msg.role === "assistant") {
|
|
1627
|
+
const blocks = convertAnthropicAssistantContent(msg, {
|
|
1628
|
+
allowEmptySignature,
|
|
1629
|
+
toolName: isOAuthToken ? toClaudeCodeName : undefined,
|
|
1630
|
+
});
|
|
1631
|
+
if (blocks.length === 0)
|
|
1632
|
+
continue;
|
|
1633
|
+
flushPendingSystemMessages();
|
|
1634
|
+
const messageIndex = params.length;
|
|
1635
|
+
params.push({
|
|
1636
|
+
role: "assistant",
|
|
1637
|
+
content: blocks,
|
|
1638
|
+
});
|
|
1639
|
+
if (managedProvider !== undefined &&
|
|
1640
|
+
msg.api === "anthropic-messages" &&
|
|
1641
|
+
msg.provider === managedProvider &&
|
|
1642
|
+
isAnthropicEffort(msg.providerThinkingLevel)) {
|
|
1643
|
+
assistantLevels.set(messageIndex, msg.providerThinkingLevel);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
else if (msg.role === "toolResult") {
|
|
1647
|
+
// Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint.
|
|
1648
|
+
const toolResults = [];
|
|
1649
|
+
// Anthropic rejects error tool results that contain anything but text, so their images follow
|
|
1650
|
+
// all tool results of the turn, which must come first in the user message (#10794).
|
|
1651
|
+
const errorImages = [];
|
|
1652
|
+
let j = i;
|
|
1653
|
+
while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
|
|
1654
|
+
const toolMsg = transformedMessages[j];
|
|
1655
|
+
const split = splitErrorToolResultImages(toolMsg);
|
|
1656
|
+
toolResults.push(convertToolResult(toolMsg, split?.content));
|
|
1657
|
+
if (split) {
|
|
1658
|
+
errorImages.push({ type: "text", text: split.caption }, ...split.images.map(convertImageBlock));
|
|
1659
|
+
}
|
|
1660
|
+
j++;
|
|
1661
|
+
}
|
|
1662
|
+
// Skip the messages we've already processed.
|
|
1663
|
+
i = j - 1;
|
|
1664
|
+
params.push({
|
|
1665
|
+
role: "user",
|
|
1666
|
+
content: [...toolResults, ...errorImages],
|
|
1667
|
+
});
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
flushPendingSystemMessages();
|
|
1671
|
+
// Add cache_control to the last user or system message to cache conversation history
|
|
1672
|
+
if (cacheControl && params.length > 0) {
|
|
1673
|
+
const lastMessage = params[params.length - 1];
|
|
1674
|
+
if (lastMessage.role === "user" || lastMessage.role === "system") {
|
|
1675
|
+
if (Array.isArray(lastMessage.content)) {
|
|
1676
|
+
const lastBlock = lastMessage.content[lastMessage.content.length - 1];
|
|
1677
|
+
if (lastBlock &&
|
|
1678
|
+
(lastBlock.type === "text" ||
|
|
1679
|
+
lastBlock.type === "image" ||
|
|
1680
|
+
lastBlock.type === "tool_result" ||
|
|
1681
|
+
lastBlock.type === "tool_addition" ||
|
|
1682
|
+
lastBlock.type === "tool_removal")) {
|
|
1683
|
+
lastBlock.cache_control = cacheControl;
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
else if (typeof lastMessage.content === "string") {
|
|
1687
|
+
lastMessage.content = [
|
|
1688
|
+
{
|
|
1689
|
+
type: "text",
|
|
1690
|
+
text: lastMessage.content,
|
|
1691
|
+
cache_control: cacheControl,
|
|
1692
|
+
},
|
|
1693
|
+
];
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
}
|
|
1697
|
+
return { messages: params, assistantLevels };
|
|
1698
|
+
}
|
|
1699
|
+
function isAnthropicEffort(value) {
|
|
1700
|
+
return value === "low" || value === "medium" || value === "high" || value === "xhigh" || value === "max";
|
|
1701
|
+
}
|
|
1702
|
+
function insertThinkingLevelMessages(converted, activeEffort) {
|
|
1703
|
+
const messages = [];
|
|
1704
|
+
for (let index = 0; index < converted.messages.length; index++) {
|
|
1705
|
+
const historicalEffort = converted.assistantLevels.get(index);
|
|
1706
|
+
if (historicalEffort !== undefined) {
|
|
1707
|
+
messages.push({ role: "system", content: [], output_config: { effort: historicalEffort } });
|
|
1708
|
+
}
|
|
1709
|
+
messages.push(converted.messages[index]);
|
|
1710
|
+
}
|
|
1711
|
+
messages.push({ role: "system", content: [], output_config: { effort: activeEffort } });
|
|
1712
|
+
return messages;
|
|
1713
|
+
}
|
|
1714
|
+
function shouldUseFineGrainedToolStreamingBeta(model, context) {
|
|
1715
|
+
return getCurrentTools(context.messages).length > 0 && !getAnthropicCompat(model).supportsEagerToolInputStreaming;
|
|
1716
|
+
}
|
|
1717
|
+
function projectObjectUnionForAnthropic(schema) {
|
|
1718
|
+
if (schema.properties !== undefined || !Array.isArray(schema.anyOf) || schema.anyOf.length === 0)
|
|
1719
|
+
return undefined;
|
|
1720
|
+
const branches = schema.anyOf;
|
|
1721
|
+
if (!branches.every((branch) => (branch.type === undefined ||
|
|
1722
|
+
branch.type === "object" ||
|
|
1723
|
+
(Array.isArray(branch.type) &&
|
|
1724
|
+
branch.type.length > 0 &&
|
|
1725
|
+
branch.type.every((type) => type === "object"))) &&
|
|
1726
|
+
branch.properties !== undefined))
|
|
1727
|
+
return undefined;
|
|
1728
|
+
const variantsByKey = new Map();
|
|
1729
|
+
for (const branch of branches) {
|
|
1730
|
+
for (const [key, propertySchema] of Object.entries(branch.properties ?? {})) {
|
|
1731
|
+
const variants = variantsByKey.get(key);
|
|
1732
|
+
if (variants === undefined) {
|
|
1733
|
+
variantsByKey.set(key, [propertySchema]);
|
|
1734
|
+
continue;
|
|
1735
|
+
}
|
|
1736
|
+
const serialized = JSON.stringify(propertySchema);
|
|
1737
|
+
if (!variants.some((variant) => variant === propertySchema || JSON.stringify(variant) === serialized)) {
|
|
1738
|
+
variants.push(propertySchema);
|
|
1739
|
+
}
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
const properties = Object.fromEntries([...variantsByKey].flatMap(([key, variants]) => {
|
|
1743
|
+
const first = variants[0];
|
|
1744
|
+
return first === undefined ? [] : [[key, variants.length === 1 ? first : { anyOf: variants }]];
|
|
1745
|
+
}));
|
|
1746
|
+
return {
|
|
1747
|
+
properties,
|
|
1748
|
+
required: (branches[0]?.required ?? []).filter((key) => branches.every((branch) => (branch.required ?? []).includes(key))),
|
|
1749
|
+
};
|
|
1750
|
+
}
|
|
1751
|
+
function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supportsStrictTools, cacheControl, disablePreferredStrictTools = false) {
|
|
1752
|
+
if (!tools)
|
|
1753
|
+
return [];
|
|
1754
|
+
return tools.map((tool, index) => {
|
|
1755
|
+
const strict = disablePreferredStrictTools && prefersJsonSchemaStrictSampling(tool)
|
|
1756
|
+
? undefined
|
|
1757
|
+
: resolveJsonSchemaStrictSampling(tool, supportsStrictTools, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
|
|
1758
|
+
const parameters = getJsonSchemaToolParameters(tool, strict, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
|
|
1759
|
+
const schema = parameters;
|
|
1760
|
+
// Anthropic rejects top-level combinators, so project object-union fields for advertising only.
|
|
1761
|
+
// Local runtime validation still enforces the complete authored union without mutating it.
|
|
1762
|
+
const projectedUnion = projectObjectUnionForAnthropic(schema);
|
|
1763
|
+
const legacyInputSchema = {
|
|
1764
|
+
type: "object",
|
|
1765
|
+
properties: projectedUnion?.properties ?? schema.properties ?? {},
|
|
1766
|
+
required: projectedUnion?.required ?? schema.required ?? [],
|
|
1767
|
+
// Keep shared definitions so property $refs still resolve, and additionalProperties so the
|
|
1768
|
+
// model knows whether extra arguments are allowed. Other root keywords stay out:
|
|
1769
|
+
// Anthropic rejects root anyOf/oneOf/allOf with a 400.
|
|
1770
|
+
...(schema.additionalProperties === undefined ? {} : { additionalProperties: schema.additionalProperties }),
|
|
1771
|
+
...(schema.$defs === undefined ? {} : { $defs: schema.$defs }),
|
|
1772
|
+
...(schema.definitions === undefined ? {} : { definitions: schema.definitions }),
|
|
1773
|
+
};
|
|
1774
|
+
const inputSchema = strict === true && projectedUnion === undefined
|
|
1775
|
+
? {
|
|
1776
|
+
...parameters,
|
|
1777
|
+
...legacyInputSchema,
|
|
1778
|
+
}
|
|
1779
|
+
: legacyInputSchema;
|
|
1780
|
+
return {
|
|
1781
|
+
name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name,
|
|
1782
|
+
description: tool.description,
|
|
1783
|
+
...(supportsEagerToolInputStreaming ? { eager_input_streaming: true } : {}),
|
|
1784
|
+
...(strict === true ? { strict: true } : {}),
|
|
1785
|
+
input_schema: inputSchema,
|
|
1786
|
+
...(cacheControl && index === tools.length - 1 ? { cache_control: cacheControl } : {}),
|
|
1787
|
+
};
|
|
1788
|
+
});
|
|
1789
|
+
}
|
|
1790
|
+
function mapStopReason(reason, stopDetails) {
|
|
1791
|
+
switch (reason) {
|
|
1792
|
+
case "end_turn":
|
|
1793
|
+
return { stopReason: "stop" };
|
|
1794
|
+
case "max_tokens":
|
|
1795
|
+
return { stopReason: "length" };
|
|
1796
|
+
case "tool_use":
|
|
1797
|
+
return { stopReason: "toolUse" };
|
|
1798
|
+
case "refusal":
|
|
1799
|
+
return {
|
|
1800
|
+
stopReason: "error",
|
|
1801
|
+
errorMessage: stopDetails?.explanation || `The model refused to complete the request`,
|
|
1802
|
+
};
|
|
1803
|
+
case "pause_turn": // Stop is good enough -> resubmit
|
|
1804
|
+
return { stopReason: "stop" };
|
|
1805
|
+
case "stop_sequence":
|
|
1806
|
+
return { stopReason: "stop" }; // We don't supply stop sequences, so this should never happen
|
|
1807
|
+
case "sensitive": // Content flagged by safety filters (not yet in SDK types)
|
|
1808
|
+
return { stopReason: "error", errorMessage: "Provider stopped with: sensitive" };
|
|
1809
|
+
default:
|
|
1810
|
+
// Handle unknown stop reasons gracefully (API may add new values)
|
|
1811
|
+
throw new Error(`Unhandled stop reason: ${reason}`);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
//# sourceMappingURL=anthropic-messages.js.map
|