@bastani/pi-ai 0.9.19 → 0.9.20-alpha.10
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 +83 -1
- package/NOTICE.md +1 -1
- package/README.md +203 -30
- package/dist/api/anthropic-messages.d.ts.map +1 -1
- package/dist/api/anthropic-messages.js +255 -97
- package/dist/api/anthropic-messages.js.map +1 -1
- package/dist/api/azure-openai-responses.d.ts.map +1 -1
- package/dist/api/azure-openai-responses.js +21 -6
- package/dist/api/azure-openai-responses.js.map +1 -1
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/dist/api/bedrock-converse-stream.js +20 -15
- package/dist/api/bedrock-converse-stream.js.map +1 -1
- 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 +43 -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 +2 -0
- package/dist/api/cloudflare.d.ts.map +1 -1
- package/dist/api/cloudflare.js +2 -0
- package/dist/api/cloudflare.js.map +1 -1
- package/dist/api/constrained-sampling.d.ts +8 -3
- package/dist/api/constrained-sampling.d.ts.map +1 -1
- package/dist/api/constrained-sampling.js +29 -11
- package/dist/api/constrained-sampling.js.map +1 -1
- package/dist/api/google-generative-ai.d.ts.map +1 -1
- package/dist/api/google-generative-ai.js +23 -80
- package/dist/api/google-generative-ai.js.map +1 -1
- package/dist/api/google-shared.d.ts +13 -4
- package/dist/api/google-shared.d.ts.map +1 -1
- package/dist/api/google-shared.js +54 -4
- package/dist/api/google-shared.js.map +1 -1
- package/dist/api/google-vertex.d.ts.map +1 -1
- package/dist/api/google-vertex.js +26 -65
- package/dist/api/google-vertex.js.map +1 -1
- package/dist/api/mistral-conversations.d.ts.map +1 -1
- package/dist/api/mistral-conversations.js +18 -13
- package/dist/api/mistral-conversations.js.map +1 -1
- package/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/dist/api/openai-codex-responses.js +40 -26
- package/dist/api/openai-codex-responses.js.map +1 -1
- package/dist/api/openai-completions.d.ts +6 -4
- package/dist/api/openai-completions.d.ts.map +1 -1
- package/dist/api/openai-completions.js +44 -58
- package/dist/api/openai-completions.js.map +1 -1
- package/dist/api/openai-responses-shared.d.ts +11 -5
- package/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/dist/api/openai-responses-shared.js +65 -58
- package/dist/api/openai-responses-shared.js.map +1 -1
- package/dist/api/openai-responses.d.ts.map +1 -1
- package/dist/api/openai-responses.js +15 -19
- package/dist/api/openai-responses.js.map +1 -1
- package/dist/api/openrouter-images.d.ts +2 -1
- package/dist/api/openrouter-images.d.ts.map +1 -1
- package/dist/api/openrouter-images.js +1 -0
- package/dist/api/openrouter-images.js.map +1 -1
- package/dist/api/openrouter-images.lazy.d.ts.map +1 -1
- package/dist/api/openrouter-images.lazy.js.map +1 -1
- package/dist/api/pi-messages.d.ts +3 -3
- package/dist/api/pi-messages.d.ts.map +1 -1
- package/dist/api/pi-messages.js +3 -2
- package/dist/api/pi-messages.js.map +1 -1
- package/dist/api/simple-options.d.ts +3 -3
- package/dist/api/simple-options.d.ts.map +1 -1
- package/dist/api/simple-options.js +1 -0
- package/dist/api/simple-options.js.map +1 -1
- package/dist/api/system-one-shared.d.ts +23 -0
- package/dist/api/system-one-shared.d.ts.map +1 -0
- package/dist/api/system-one-shared.js +170 -0
- package/dist/api/system-one-shared.js.map +1 -0
- package/dist/api/transform-messages.d.ts.map +1 -1
- package/dist/api/transform-messages.js +21 -7
- package/dist/api/transform-messages.js.map +1 -1
- 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 +19 -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/credential-store.d.ts.map +1 -1
- package/dist/auth/credential-store.js +3 -2
- package/dist/auth/credential-store.js.map +1 -1
- package/dist/auth/oauth/load.d.ts +2 -0
- package/dist/auth/oauth/load.d.ts.map +1 -1
- package/dist/auth/oauth/load.js +5 -0
- package/dist/auth/oauth/load.js.map +1 -1
- 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 +190 -0
- package/dist/auth/oauth/meta.js.map +1 -0
- package/dist/auth/resolve.d.ts +6 -8
- package/dist/auth/resolve.d.ts.map +1 -1
- package/dist/auth/resolve.js +29 -26
- package/dist/auth/resolve.js.map +1 -1
- package/dist/bun-oauth.d.ts.map +1 -1
- package/dist/bun-oauth.js +2 -0
- package/dist/bun-oauth.js.map +1 -1
- package/dist/compat.d.ts +3 -3
- package/dist/compat.d.ts.map +1 -1
- package/dist/compat.js +9 -6
- package/dist/compat.js.map +1 -1
- 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 +2 -0
- package/dist/env-api-keys.d.ts.map +1 -1
- package/dist/env-api-keys.js +61 -43
- package/dist/env-api-keys.js.map +1 -1
- package/dist/image-models.d.ts +19 -8
- package/dist/image-models.d.ts.map +1 -1
- package/dist/image-models.js +14 -13
- package/dist/image-models.js.map +1 -1
- package/dist/images-api-registry.d.ts +7 -7
- package/dist/images-api-registry.d.ts.map +1 -1
- package/dist/images-api-registry.js.map +1 -1
- package/dist/images.d.ts +7 -2
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +5 -0
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/model-catalog.d.ts +25 -9
- package/dist/model-catalog.d.ts.map +1 -1
- package/dist/model-catalog.js +14 -2
- package/dist/model-catalog.js.map +1 -1
- package/dist/models-store.d.ts +3 -2
- package/dist/models-store.d.ts.map +1 -1
- package/dist/models-store.js.map +1 -1
- package/dist/models.d.ts +99 -31
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +133 -39
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +133 -39
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +161 -31
- package/dist/models.js.map +1 -1
- package/dist/providers/all.d.ts +19 -13
- package/dist/providers/all.d.ts.map +1 -1
- package/dist/providers/all.js +27 -21
- package/dist/providers/all.js.map +1 -1
- package/dist/providers/amazon-bedrock.models.d.ts +4 -3
- package/dist/providers/amazon-bedrock.models.d.ts.map +1 -1
- package/dist/providers/amazon-bedrock.models.js +4 -2
- package/dist/providers/amazon-bedrock.models.js.map +1 -1
- package/dist/providers/ant-ling.models.d.ts +4 -3
- package/dist/providers/ant-ling.models.d.ts.map +1 -1
- package/dist/providers/ant-ling.models.js +4 -2
- package/dist/providers/ant-ling.models.js.map +1 -1
- package/dist/providers/anthropic.models.d.ts +4 -3
- package/dist/providers/anthropic.models.d.ts.map +1 -1
- package/dist/providers/anthropic.models.js +4 -2
- package/dist/providers/anthropic.models.js.map +1 -1
- package/dist/providers/azure-openai-responses.models.d.ts +4 -3
- package/dist/providers/azure-openai-responses.models.d.ts.map +1 -1
- package/dist/providers/azure-openai-responses.models.js +4 -2
- package/dist/providers/azure-openai-responses.models.js.map +1 -1
- package/dist/providers/baseten.models.d.ts +4 -3
- package/dist/providers/baseten.models.d.ts.map +1 -1
- package/dist/providers/baseten.models.js +4 -2
- package/dist/providers/baseten.models.js.map +1 -1
- package/dist/providers/cerebras.models.d.ts +4 -3
- package/dist/providers/cerebras.models.d.ts.map +1 -1
- package/dist/providers/cerebras.models.js +4 -2
- package/dist/providers/cerebras.models.js.map +1 -1
- package/dist/providers/cloudflare-ai-gateway.models.d.ts +4 -3
- package/dist/providers/cloudflare-ai-gateway.models.d.ts.map +1 -1
- package/dist/providers/cloudflare-ai-gateway.models.js +4 -2
- package/dist/providers/cloudflare-ai-gateway.models.js.map +1 -1
- package/dist/providers/cloudflare-stream.d.ts +6 -2
- package/dist/providers/cloudflare-stream.d.ts.map +1 -1
- package/dist/providers/cloudflare-stream.js +6 -0
- package/dist/providers/cloudflare-stream.js.map +1 -1
- package/dist/providers/cloudflare-workers-ai.d.ts.map +1 -1
- package/dist/providers/cloudflare-workers-ai.js +10 -3
- package/dist/providers/cloudflare-workers-ai.js.map +1 -1
- package/dist/providers/cloudflare-workers-ai.models.d.ts +4 -3
- package/dist/providers/cloudflare-workers-ai.models.d.ts.map +1 -1
- package/dist/providers/cloudflare-workers-ai.models.js +4 -2
- package/dist/providers/cloudflare-workers-ai.models.js.map +1 -1
- package/dist/providers/data/.manifest.json +1 -1
- package/dist/providers/data/amazon-bedrock.json +1 -1
- package/dist/providers/data/ant-ling.json +1 -1
- package/dist/providers/data/anthropic.json +1 -1
- package/dist/providers/data/azure-openai-responses.json +1 -1
- package/dist/providers/data/baseten.json +1 -1
- package/dist/providers/data/cerebras.json +1 -1
- package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
- package/dist/providers/data/cloudflare-workers-ai.json +1 -1
- package/dist/providers/data/deepseek.json +1 -1
- package/dist/providers/data/fireworks.json +1 -1
- package/dist/providers/data/github-copilot.json +1 -1
- package/dist/providers/data/google-vertex.json +1 -1
- package/dist/providers/data/google.json +1 -1
- package/dist/providers/data/groq.json +1 -1
- package/dist/providers/data/huggingface.json +1 -1
- package/dist/providers/data/kimi-coding.json +1 -1
- package/dist/providers/data/meta.json +1 -0
- package/dist/providers/data/minimax-cn.json +1 -1
- package/dist/providers/data/minimax.json +1 -1
- package/dist/providers/data/mistral.json +1 -1
- package/dist/providers/data/moonshotai-cn.json +1 -1
- package/dist/providers/data/moonshotai.json +1 -1
- package/dist/providers/data/nvidia.json +1 -1
- package/dist/providers/data/openai-codex.json +1 -1
- package/dist/providers/data/openai.json +1 -1
- package/dist/providers/data/opencode-go.json +1 -1
- package/dist/providers/data/opencode.json +1 -1
- package/dist/providers/data/openrouter.json +1 -1
- package/dist/providers/data/qwen-token-plan-cn.json +1 -1
- package/dist/providers/data/qwen-token-plan-individual.json +1 -1
- package/dist/providers/data/qwen-token-plan.json +1 -1
- package/dist/providers/data/radius.json +1 -0
- package/dist/providers/data/together.json +1 -1
- package/dist/providers/data/typesafe.json +1 -0
- package/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/dist/providers/data/xai.json +1 -1
- package/dist/providers/data/xiaomi-token-plan-ams.json +1 -1
- package/dist/providers/data/xiaomi-token-plan-cn.json +1 -1
- package/dist/providers/data/xiaomi-token-plan-sgp.json +1 -1
- package/dist/providers/data/xiaomi.json +1 -1
- package/dist/providers/data/zai-coding-cn.json +1 -1
- package/dist/providers/data/zai.json +1 -1
- package/dist/providers/deepseek.models.d.ts +4 -3
- package/dist/providers/deepseek.models.d.ts.map +1 -1
- package/dist/providers/deepseek.models.js +4 -2
- package/dist/providers/deepseek.models.js.map +1 -1
- package/dist/providers/faux.d.ts +3 -2
- package/dist/providers/faux.d.ts.map +1 -1
- package/dist/providers/faux.js +12 -11
- package/dist/providers/faux.js.map +1 -1
- package/dist/providers/fireworks.models.d.ts +4 -3
- package/dist/providers/fireworks.models.d.ts.map +1 -1
- package/dist/providers/fireworks.models.js +4 -2
- package/dist/providers/fireworks.models.js.map +1 -1
- package/dist/providers/github-copilot.models.d.ts +4 -3
- package/dist/providers/github-copilot.models.d.ts.map +1 -1
- package/dist/providers/github-copilot.models.js +4 -2
- package/dist/providers/github-copilot.models.js.map +1 -1
- package/dist/providers/google-vertex.models.d.ts +4 -3
- package/dist/providers/google-vertex.models.d.ts.map +1 -1
- package/dist/providers/google-vertex.models.js +4 -2
- package/dist/providers/google-vertex.models.js.map +1 -1
- package/dist/providers/google.models.d.ts +4 -3
- package/dist/providers/google.models.d.ts.map +1 -1
- package/dist/providers/google.models.js +4 -2
- package/dist/providers/google.models.js.map +1 -1
- package/dist/providers/groq.models.d.ts +4 -3
- package/dist/providers/groq.models.d.ts.map +1 -1
- package/dist/providers/groq.models.js +4 -2
- package/dist/providers/groq.models.js.map +1 -1
- package/dist/providers/huggingface.models.d.ts +4 -3
- package/dist/providers/huggingface.models.d.ts.map +1 -1
- package/dist/providers/huggingface.models.js +4 -2
- package/dist/providers/huggingface.models.js.map +1 -1
- package/dist/providers/images/register-builtins.d.ts +1 -1
- package/dist/providers/images/register-builtins.d.ts.map +1 -1
- package/dist/providers/images/register-builtins.js.map +1 -1
- package/dist/providers/kimi-coding.models.d.ts +4 -3
- package/dist/providers/kimi-coding.models.d.ts.map +1 -1
- package/dist/providers/kimi-coding.models.js +4 -2
- package/dist/providers/kimi-coding.models.js.map +1 -1
- 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.models.d.ts +4 -3
- package/dist/providers/minimax-cn.models.d.ts.map +1 -1
- package/dist/providers/minimax-cn.models.js +4 -2
- package/dist/providers/minimax-cn.models.js.map +1 -1
- package/dist/providers/minimax.models.d.ts +4 -3
- package/dist/providers/minimax.models.d.ts.map +1 -1
- package/dist/providers/minimax.models.js +4 -2
- package/dist/providers/minimax.models.js.map +1 -1
- package/dist/providers/mistral.models.d.ts +4 -3
- package/dist/providers/mistral.models.d.ts.map +1 -1
- package/dist/providers/mistral.models.js +4 -2
- package/dist/providers/mistral.models.js.map +1 -1
- package/dist/providers/moonshotai-cn.models.d.ts +4 -3
- package/dist/providers/moonshotai-cn.models.d.ts.map +1 -1
- package/dist/providers/moonshotai-cn.models.js +4 -2
- package/dist/providers/moonshotai-cn.models.js.map +1 -1
- package/dist/providers/moonshotai.models.d.ts +4 -3
- package/dist/providers/moonshotai.models.d.ts.map +1 -1
- package/dist/providers/moonshotai.models.js +4 -2
- package/dist/providers/moonshotai.models.js.map +1 -1
- package/dist/providers/nvidia.models.d.ts +4 -3
- package/dist/providers/nvidia.models.d.ts.map +1 -1
- package/dist/providers/nvidia.models.js +4 -2
- package/dist/providers/nvidia.models.js.map +1 -1
- package/dist/providers/openai-codex.models.d.ts +4 -3
- package/dist/providers/openai-codex.models.d.ts.map +1 -1
- package/dist/providers/openai-codex.models.js +4 -2
- package/dist/providers/openai-codex.models.js.map +1 -1
- package/dist/providers/openai.models.d.ts +4 -3
- package/dist/providers/openai.models.d.ts.map +1 -1
- package/dist/providers/openai.models.js +4 -2
- package/dist/providers/openai.models.js.map +1 -1
- package/dist/providers/opencode-go.models.d.ts +4 -3
- package/dist/providers/opencode-go.models.d.ts.map +1 -1
- package/dist/providers/opencode-go.models.js +4 -2
- package/dist/providers/opencode-go.models.js.map +1 -1
- package/dist/providers/opencode.models.d.ts +4 -3
- package/dist/providers/opencode.models.d.ts.map +1 -1
- package/dist/providers/opencode.models.js +4 -2
- package/dist/providers/opencode.models.js.map +1 -1
- package/dist/providers/openrouter.d.ts.map +1 -1
- package/dist/providers/openrouter.js +11 -2
- package/dist/providers/openrouter.js.map +1 -1
- package/dist/providers/openrouter.models.d.ts +4 -3
- package/dist/providers/openrouter.models.d.ts.map +1 -1
- package/dist/providers/openrouter.models.js +4 -2
- package/dist/providers/openrouter.models.js.map +1 -1
- package/dist/providers/qwen-token-plan-cn.models.d.ts +4 -3
- package/dist/providers/qwen-token-plan-cn.models.d.ts.map +1 -1
- package/dist/providers/qwen-token-plan-cn.models.js +4 -2
- package/dist/providers/qwen-token-plan-cn.models.js.map +1 -1
- package/dist/providers/qwen-token-plan-individual.models.d.ts +4 -3
- package/dist/providers/qwen-token-plan-individual.models.d.ts.map +1 -1
- package/dist/providers/qwen-token-plan-individual.models.js +4 -2
- package/dist/providers/qwen-token-plan-individual.models.js.map +1 -1
- package/dist/providers/qwen-token-plan.models.d.ts +4 -3
- package/dist/providers/qwen-token-plan.models.d.ts.map +1 -1
- package/dist/providers/qwen-token-plan.models.js +4 -2
- package/dist/providers/qwen-token-plan.models.js.map +1 -1
- package/dist/providers/radius.d.ts.map +1 -1
- package/dist/providers/radius.js +19 -5
- package/dist/providers/radius.js.map +1 -1
- package/dist/providers/radius.models.d.ts +5 -0
- package/dist/providers/radius.models.d.ts.map +1 -0
- package/dist/providers/radius.models.js +8 -0
- package/dist/providers/radius.models.js.map +1 -0
- package/dist/providers/together.models.d.ts +4 -3
- package/dist/providers/together.models.d.ts.map +1 -1
- package/dist/providers/together.models.js +4 -2
- package/dist/providers/together.models.js.map +1 -1
- 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.models.d.ts +4 -3
- package/dist/providers/vercel-ai-gateway.models.d.ts.map +1 -1
- package/dist/providers/vercel-ai-gateway.models.js +4 -2
- package/dist/providers/vercel-ai-gateway.models.js.map +1 -1
- package/dist/providers/xai.models.d.ts +4 -3
- package/dist/providers/xai.models.d.ts.map +1 -1
- package/dist/providers/xai.models.js +4 -2
- package/dist/providers/xai.models.js.map +1 -1
- package/dist/providers/xiaomi-token-plan-ams.models.d.ts +4 -3
- package/dist/providers/xiaomi-token-plan-ams.models.d.ts.map +1 -1
- package/dist/providers/xiaomi-token-plan-ams.models.js +4 -2
- package/dist/providers/xiaomi-token-plan-ams.models.js.map +1 -1
- package/dist/providers/xiaomi-token-plan-cn.models.d.ts +4 -3
- package/dist/providers/xiaomi-token-plan-cn.models.d.ts.map +1 -1
- package/dist/providers/xiaomi-token-plan-cn.models.js +4 -2
- package/dist/providers/xiaomi-token-plan-cn.models.js.map +1 -1
- package/dist/providers/xiaomi-token-plan-sgp.models.d.ts +4 -3
- package/dist/providers/xiaomi-token-plan-sgp.models.d.ts.map +1 -1
- package/dist/providers/xiaomi-token-plan-sgp.models.js +4 -2
- package/dist/providers/xiaomi-token-plan-sgp.models.js.map +1 -1
- package/dist/providers/xiaomi.models.d.ts +4 -3
- package/dist/providers/xiaomi.models.d.ts.map +1 -1
- package/dist/providers/xiaomi.models.js +4 -2
- package/dist/providers/xiaomi.models.js.map +1 -1
- package/dist/providers/zai-coding-cn.models.d.ts +4 -3
- package/dist/providers/zai-coding-cn.models.d.ts.map +1 -1
- package/dist/providers/zai-coding-cn.models.js +4 -2
- package/dist/providers/zai-coding-cn.models.js.map +1 -1
- package/dist/providers/zai.models.d.ts +4 -3
- package/dist/providers/zai.models.d.ts.map +1 -1
- package/dist/providers/zai.models.js +4 -2
- package/dist/providers/zai.models.js.map +1 -1
- package/dist/types.d.ts +227 -51
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/diagnostics.d.ts +3 -2
- package/dist/utils/diagnostics.d.ts.map +1 -1
- package/dist/utils/diagnostics.js.map +1 -1
- package/dist/utils/estimate.d.ts +2 -2
- package/dist/utils/estimate.d.ts.map +1 -1
- package/dist/utils/estimate.js +8 -29
- package/dist/utils/estimate.js.map +1 -1
- package/dist/utils/headers.d.ts +1 -1
- package/dist/utils/headers.d.ts.map +1 -1
- package/dist/utils/headers.js +10 -8
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/model-operations.d.ts +11 -0
- package/dist/utils/model-operations.d.ts.map +1 -0
- package/dist/utils/model-operations.js +47 -0
- package/dist/utils/model-operations.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/overflow.d.ts +1 -0
- package/dist/utils/overflow.d.ts.map +1 -1
- package/dist/utils/overflow.js +17 -5
- package/dist/utils/overflow.js.map +1 -1
- package/dist/utils/retry.d.ts.map +1 -1
- package/dist/utils/retry.js +2 -0
- package/dist/utils/retry.js.map +1 -1
- package/dist/utils/text.d.ts +9 -1
- package/dist/utils/text.d.ts.map +1 -1
- package/dist/utils/text.js +26 -0
- package/dist/utils/text.js.map +1 -1
- package/dist/utils/transcript.d.ts +84 -0
- package/dist/utils/transcript.d.ts.map +1 -0
- package/dist/utils/transcript.js +204 -0
- package/dist/utils/transcript.js.map +1 -0
- package/package.json +4 -4
- package/dist/image-models.generated.d.ts +0 -785
- package/dist/image-models.generated.d.ts.map +0 -1
- package/dist/image-models.generated.js +0 -787
- package/dist/image-models.generated.js.map +0 -1
- package/dist/images-models.d.ts +0 -95
- package/dist/images-models.d.ts.map +0 -1
- package/dist/images-models.js +0 -143
- package/dist/images-models.js.map +0 -1
- package/dist/providers/openrouter-images.d.ts +0 -3
- package/dist/providers/openrouter-images.d.ts.map +0 -1
- package/dist/providers/openrouter-images.js +0 -22
- package/dist/providers/openrouter-images.js.map +0 -1
- package/dist/utils/deferred-tools.d.ts +0 -9
- package/dist/utils/deferred-tools.d.ts.map +0 -1
- package/dist/utils/deferred-tools.js +0 -36
- package/dist/utils/deferred-tools.js.map +0 -1
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import Anthropic from "@anthropic-ai/sdk";
|
|
2
2
|
import { calculateCost } from "../models.js";
|
|
3
|
-
import { splitDeferredTools } from "../utils/deferred-tools.js";
|
|
4
3
|
import { appendAssistantMessageDiagnostic } from "../utils/diagnostics.js";
|
|
5
4
|
import { assertSupportedDocumentMimeType } from "../utils/document-input.js";
|
|
6
5
|
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
@@ -11,7 +10,9 @@ import { getProviderEnvValue } from "../utils/provider-env.js";
|
|
|
11
10
|
import { retryProviderRequest } from "../utils/provider-retry.js";
|
|
12
11
|
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
13
12
|
import { createStreamDeadline, withStreamDeadline } from "../utils/stream-deadline.js";
|
|
14
|
-
import {
|
|
13
|
+
import { getSystemMessageText, renderSystemMessageUpdate } from "../utils/text.js";
|
|
14
|
+
import { getCurrentTools, getDeclaredTools, getInitialSystemMessage, hasToolRedefinitions, resolveTranscript, } from "../utils/transcript.js";
|
|
15
|
+
import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
|
|
15
16
|
import { buildCopilotDynamicHeaders, hasCopilotVisionInput, preserveCopilotIntegrationHeader, } from "./github-copilot-headers.js";
|
|
16
17
|
import { adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToContext } from "./simple-options.js";
|
|
17
18
|
import { transformMessages } from "./transform-messages.js";
|
|
@@ -42,13 +43,13 @@ function getCacheControl(model, cacheRetention, env) {
|
|
|
42
43
|
// Stealth mode: Mimic Claude Code's tool naming exactly
|
|
43
44
|
//
|
|
44
45
|
// Anthropic gates newer models on the `claude-cli/<version>` user agent alone and rejects an
|
|
45
|
-
// older one with `claude_code_version_too_old`. `claude-fable-5-1` requires >= 2.1.251
|
|
46
|
-
// against the live API: 2.1.250 -> 400, 2.1.251 -> 200
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
// this value; a caller can override it for
|
|
50
|
-
// lowercase `user-agent` key is merged last.
|
|
51
|
-
const claudeCodeVersion = "2.1.
|
|
46
|
+
// older one with `claude_code_version_too_old`. `claude-fable-5-1` requires >= 2.1.251 (bisected
|
|
47
|
+
// against the live API: 2.1.250 -> 400, 2.1.251 -> 200) and Claude Opus 5.5 requires >= 2.1.280,
|
|
48
|
+
// the floor the API names in its rejection. This is pinned to the highest published minimum
|
|
49
|
+
// among shipped models rather than the newest release; every model this provider ships answers
|
|
50
|
+
// 200 at 2.1.280. Raise it only when a model rejects this value; a caller can override it for
|
|
51
|
+
// one client through the `headers` option, whose lowercase `user-agent` key is merged last.
|
|
52
|
+
const claudeCodeVersion = "2.1.280";
|
|
52
53
|
// Claude Code 2.x tool names (canonical casing)
|
|
53
54
|
// Source: https://cchistory.mariozechner.at/data/prompts-2.1.11.md
|
|
54
55
|
// To update: https://github.com/badlogic/cchistory
|
|
@@ -127,6 +128,75 @@ const INTERLEAVED_THINKING_BETA = "interleaved-thinking-2025-05-14";
|
|
|
127
128
|
const SERVER_SIDE_FALLBACK_BETA = "server-side-fallback-2026-07-01";
|
|
128
129
|
const THINKING_BINDING_CONTROLS_BETA = "thinking-binding-controls-2026-08-01";
|
|
129
130
|
const MID_CONVERSATION_OUTPUT_CONFIG_BETA = "mid-conversation-output-config-2026-07-01";
|
|
131
|
+
const MID_CONVERSATION_TOOL_CHANGES_BETA = "mid-conversation-tool-changes-2026-07-01";
|
|
132
|
+
const FAST_MODE_BETA = "fast-mode-2026-02-01";
|
|
133
|
+
/**
|
|
134
|
+
* Fast mode bills every token category at twice the standard rate for each supported model, across
|
|
135
|
+
* the full context window, and prompt-caching multipliers stack on top.
|
|
136
|
+
* https://platform.claude.com/docs/en/build-with-claude/fast-mode#pricing
|
|
137
|
+
*/
|
|
138
|
+
const FAST_MODE_PRICE_MULTIPLIER = 2;
|
|
139
|
+
function requestedSpeed(model) {
|
|
140
|
+
return model.fastRoute?.speed;
|
|
141
|
+
}
|
|
142
|
+
function upstreamModelId(model) {
|
|
143
|
+
return model.fastRoute?.upstreamModelId ?? model.id;
|
|
144
|
+
}
|
|
145
|
+
function reportedSpeed(usage) {
|
|
146
|
+
const speed = usage?.speed;
|
|
147
|
+
return speed === "fast" || speed === "standard" ? speed : undefined;
|
|
148
|
+
}
|
|
149
|
+
function applyFastModePricing(usage) {
|
|
150
|
+
usage.cost.input *= FAST_MODE_PRICE_MULTIPLIER;
|
|
151
|
+
usage.cost.output *= FAST_MODE_PRICE_MULTIPLIER;
|
|
152
|
+
usage.cost.cacheRead *= FAST_MODE_PRICE_MULTIPLIER;
|
|
153
|
+
usage.cost.cacheWrite *= FAST_MODE_PRICE_MULTIPLIER;
|
|
154
|
+
usage.cost.total = usage.cost.input + usage.cost.output + usage.cost.cacheRead + usage.cost.cacheWrite;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Reject a payload hook that rewrote a field the model's fast route owns.
|
|
158
|
+
*
|
|
159
|
+
* A fast variant is recorded, persisted, and billed under its own `-fast` identity, so a hook that
|
|
160
|
+
* changes the upstream model or drops `speed: "fast"` would send a different request under that
|
|
161
|
+
* identity. Refusing loudly surfaces the misconfigured hook instead of silently restoring the route.
|
|
162
|
+
*/
|
|
163
|
+
function assertPayloadPreservesFastRoute(model, payload) {
|
|
164
|
+
const fastRoute = model.fastRoute;
|
|
165
|
+
if (fastRoute?.speed === undefined)
|
|
166
|
+
return;
|
|
167
|
+
const remedy = `A fast model variant must send its route's upstream model and speed, because it is recorded, persisted, ` +
|
|
168
|
+
`and billed under its own identity. Select the normal model "${model.provider}/${fastRoute.baseModelId}" ` +
|
|
169
|
+
`if the request needs different routing`;
|
|
170
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) {
|
|
171
|
+
throw new Error(`A request hook changed payload for fast model "${model.provider}/${model.id}" to a value that cannot carry ` +
|
|
172
|
+
`the route's upstream model and speed. ${remedy}, or return an object that preserves those fields.`);
|
|
173
|
+
}
|
|
174
|
+
const record = payload;
|
|
175
|
+
const conflicts = [];
|
|
176
|
+
if (record.model !== fastRoute.upstreamModelId) {
|
|
177
|
+
conflicts.push(`model (expected "${fastRoute.upstreamModelId}", got ${JSON.stringify(record.model)})`);
|
|
178
|
+
}
|
|
179
|
+
if (record.speed !== fastRoute.speed) {
|
|
180
|
+
conflicts.push(`speed (expected ${JSON.stringify(fastRoute.speed)}, got ${JSON.stringify(record.speed)})`);
|
|
181
|
+
}
|
|
182
|
+
if (conflicts.length === 0)
|
|
183
|
+
return;
|
|
184
|
+
throw new Error(`A request hook changed ${conflicts.join(" and ")} for fast model "${model.provider}/${model.id}". ` +
|
|
185
|
+
`${remedy}, or stop rewriting those fields.`);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Stable deferred tool declared whenever native tool changes are in use. Anthropic adds
|
|
189
|
+
* hidden prompt scaffolding as soon as any tool has `defer_loading`; declaring this
|
|
190
|
+
* placeholder from the first request keeps that scaffolding in the cached prefix, so the
|
|
191
|
+
* first real late tool does not invalidate the cache (measured: full miss without it).
|
|
192
|
+
* It is never activated and the model cannot see it.
|
|
193
|
+
*/
|
|
194
|
+
const DEFERRED_TOOL_PLACEHOLDER = {
|
|
195
|
+
name: "__pi_deferred_placeholder__",
|
|
196
|
+
description: "Reserved placeholder. Never available. Never call this.",
|
|
197
|
+
input_schema: { type: "object", properties: {}, required: [] },
|
|
198
|
+
defer_loading: true,
|
|
199
|
+
};
|
|
130
200
|
function shouldUseServerSideFallbackBeta(model) {
|
|
131
201
|
return (model.compat?.allowedFallbackModels?.length ?? 0) > 0;
|
|
132
202
|
}
|
|
@@ -205,7 +275,7 @@ function addEarlierAttemptCosts(output, model, servingModel, event) {
|
|
|
205
275
|
if (!iteration.output_tokens)
|
|
206
276
|
continue;
|
|
207
277
|
// Price at the rates of the model that ran the attempt, which is not the serving model.
|
|
208
|
-
const attemptCost = iteration.model === model
|
|
278
|
+
const attemptCost = iteration.model === upstreamModelId(model)
|
|
209
279
|
? model.cost
|
|
210
280
|
: iteration.model === servingModel.id
|
|
211
281
|
? servingModel.cost
|
|
@@ -244,24 +314,10 @@ function getAnthropicCompat(model) {
|
|
|
244
314
|
supportsTemperature: model.compat?.supportsTemperature ?? true,
|
|
245
315
|
allowEmptySignature: model.compat?.allowEmptySignature ?? false,
|
|
246
316
|
supportsStrictTools: model.compat?.supportsStrictTools ?? false,
|
|
247
|
-
|
|
317
|
+
supportsMidConvoSystemMessages: model.compat?.supportsMidConvoSystemMessages ?? false,
|
|
318
|
+
supportsMidConvoToolChanges: model.compat?.supportsMidConvoToolChanges ?? false,
|
|
248
319
|
};
|
|
249
320
|
}
|
|
250
|
-
/**
|
|
251
|
-
* Default for `supportsToolReferences`: first-party Anthropic models except
|
|
252
|
-
* Haiku (rejects client-side tool_reference blocks) and models that predate
|
|
253
|
-
* tool search (Claude 3.x, Opus/Sonnet 4.0, Opus 4.1).
|
|
254
|
-
*/
|
|
255
|
-
function defaultSupportsToolReferences(model) {
|
|
256
|
-
if (model.provider !== "anthropic" || model.id.includes("haiku"))
|
|
257
|
-
return false;
|
|
258
|
-
const version = model.id.match(/^claude-(?:opus|sonnet|fable)-(\d+)(?:-(\d+))?(?:-|$)/);
|
|
259
|
-
if (!version)
|
|
260
|
-
return false;
|
|
261
|
-
const major = Number(version[1]);
|
|
262
|
-
const minor = version[2] && version[2].length < 8 ? Number(version[2]) : 0;
|
|
263
|
-
return major > 4 || (major === 4 && minor >= 5);
|
|
264
|
-
}
|
|
265
321
|
function mergeHeaders(...headerSources) {
|
|
266
322
|
const merged = {};
|
|
267
323
|
for (const headers of headerSources) {
|
|
@@ -456,6 +512,8 @@ async function* iterateAnthropicEvents(response, signal) {
|
|
|
456
512
|
}
|
|
457
513
|
export const stream = (model, context, options) => {
|
|
458
514
|
const stream = new AssistantMessageEventStream();
|
|
515
|
+
const normalizedContext = resolveTranscript(context, getAnthropicCompat(model).supportsMidConvoSystemMessages);
|
|
516
|
+
const currentTools = getCurrentTools(normalizedContext.messages);
|
|
459
517
|
(async () => {
|
|
460
518
|
const providerThinkingLevel = supportsMidConvoEffort(model) ? (options?.effort ?? "high") : undefined;
|
|
461
519
|
const output = {
|
|
@@ -482,6 +540,7 @@ export const stream = (model, context, options) => {
|
|
|
482
540
|
let client;
|
|
483
541
|
let isOAuth;
|
|
484
542
|
let usageModel = model;
|
|
543
|
+
let servedSpeed = requestedSpeed(model);
|
|
485
544
|
if (options?.client) {
|
|
486
545
|
client = options.client;
|
|
487
546
|
isOAuth = false;
|
|
@@ -491,30 +550,34 @@ export const stream = (model, context, options) => {
|
|
|
491
550
|
assertRequestAuth(model.provider, apiKey, options?.headers);
|
|
492
551
|
let copilotDynamicHeaders;
|
|
493
552
|
if (model.provider === "github-copilot") {
|
|
494
|
-
const hasImages = hasCopilotVisionInput(
|
|
553
|
+
const hasImages = hasCopilotVisionInput(normalizedContext.messages);
|
|
495
554
|
copilotDynamicHeaders = preserveCopilotIntegrationHeader(model.headers, buildCopilotDynamicHeaders({
|
|
496
|
-
messages:
|
|
555
|
+
messages: normalizedContext.messages,
|
|
497
556
|
hasImages,
|
|
498
557
|
apiKey,
|
|
499
558
|
}));
|
|
500
559
|
}
|
|
501
560
|
const cacheRetention = resolveCacheRetention(options?.cacheRetention, options?.env);
|
|
502
561
|
const cacheSessionId = cacheRetention === "none" ? undefined : options?.sessionId;
|
|
503
|
-
const created = createClient(model, apiKey, model.reasoning && options?.thinkingEnabled === true && (options.interleavedThinking ?? true), shouldUseFineGrainedToolStreamingBeta(model, context), shouldUseServerSideFallbackBeta(model), options?.headers, options?.fetch, copilotDynamicHeaders, cacheSessionId)
|
|
562
|
+
const created = createClient(model, apiKey, model.reasoning && options?.thinkingEnabled === true && (options.interleavedThinking ?? true), shouldUseFineGrainedToolStreamingBeta(model, context), shouldUseServerSideFallbackBeta(model), options?.headers, options?.fetch, copilotDynamicHeaders, cacheSessionId, getAnthropicCompat(model).supportsMidConvoToolChanges &&
|
|
563
|
+
getAnthropicCompat(model).supportsMidConvoSystemMessages &&
|
|
564
|
+
(getInitialSystemMessage(normalizedContext.messages)?.toolsAdded?.length ?? 0) > 0 &&
|
|
565
|
+
!hasToolRedefinitions(normalizedContext.messages));
|
|
504
566
|
client = created.client;
|
|
505
567
|
isOAuth = created.isOAuthToken;
|
|
506
568
|
}
|
|
507
|
-
let params = buildParams(model,
|
|
569
|
+
let params = buildParams(model, normalizedContext, isOAuth, options);
|
|
508
570
|
const nextParams = await options?.onPayload?.(params, model);
|
|
509
571
|
if (nextParams !== undefined) {
|
|
510
572
|
params = nextParams;
|
|
511
573
|
}
|
|
574
|
+
assertPayloadPreservesFastRoute(model, params);
|
|
512
575
|
const requestOptions = {
|
|
513
576
|
...(streamDeadline.signal ? { signal: streamDeadline.signal } : {}),
|
|
514
577
|
...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
|
|
515
578
|
maxRetries: 0,
|
|
516
579
|
};
|
|
517
|
-
const response = await retryProviderRequest(() => client.messages.create({ ...params, stream: true }, requestOptions).asResponse(), {
|
|
580
|
+
const response = await retryProviderRequest(() => client.beta.messages.create({ ...params, stream: true }, requestOptions).asResponse(), {
|
|
518
581
|
maxRetries: options?.maxRetries,
|
|
519
582
|
maxRetryDelayMs: options?.maxRetryDelayMs,
|
|
520
583
|
signal: streamDeadline.signal,
|
|
@@ -523,13 +586,16 @@ export const stream = (model, context, options) => {
|
|
|
523
586
|
stream.push({ type: "start", partial: output });
|
|
524
587
|
const blocks = output.content;
|
|
525
588
|
for await (const event of withStreamDeadline(iterateAnthropicEvents(response, streamDeadline.signal), streamDeadline.deadlineMs, streamDeadline.abort)) {
|
|
589
|
+
await options?.onProviderStreamEvent?.(event, model);
|
|
526
590
|
if (event.type === "message_start") {
|
|
527
591
|
output.responseId = event.message.id;
|
|
528
|
-
|
|
529
|
-
|
|
592
|
+
const responseModel = event.message.model;
|
|
593
|
+
if (responseModel !== upstreamModelId(model))
|
|
594
|
+
output.responseModel = responseModel;
|
|
595
|
+
const fallbackCost = responseModel === upstreamModelId(model)
|
|
530
596
|
? undefined
|
|
531
|
-
: model.compat?.allowedFallbackModels?.find((fallback) => fallback.provider === model.provider && fallback.model ===
|
|
532
|
-
usageModel = fallbackCost ? { ...model, id:
|
|
597
|
+
: model.compat?.allowedFallbackModels?.find((fallback) => fallback.provider === model.provider && fallback.model === responseModel)?.cost;
|
|
598
|
+
usageModel = fallbackCost ? { ...model, id: responseModel, cost: fallbackCost } : model;
|
|
533
599
|
// Capture initial token usage from message_start event
|
|
534
600
|
// This ensures we have input token counts even if the stream is aborted early
|
|
535
601
|
output.usage.input = event.message.usage.input_tokens || 0;
|
|
@@ -541,6 +607,9 @@ export const stream = (model, context, options) => {
|
|
|
541
607
|
output.usage.totalTokens =
|
|
542
608
|
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
543
609
|
calculateCost(usageModel, output.usage);
|
|
610
|
+
servedSpeed = reportedSpeed(event.message.usage) ?? servedSpeed;
|
|
611
|
+
if (servedSpeed === "fast")
|
|
612
|
+
applyFastModePricing(output.usage);
|
|
544
613
|
inputTransformations = getInputTransformations(event.message) ?? inputTransformations;
|
|
545
614
|
}
|
|
546
615
|
else if (event.type === "content_block_start") {
|
|
@@ -579,7 +648,7 @@ export const stream = (model, context, options) => {
|
|
|
579
648
|
type: "toolCall",
|
|
580
649
|
id: event.content_block.id,
|
|
581
650
|
name: isOAuth
|
|
582
|
-
? fromClaudeCodeName(event.content_block.name,
|
|
651
|
+
? fromClaudeCodeName(event.content_block.name, currentTools)
|
|
583
652
|
: event.content_block.name,
|
|
584
653
|
arguments: event.content_block.input ?? {},
|
|
585
654
|
partialJson: "",
|
|
@@ -728,6 +797,11 @@ export const stream = (model, context, options) => {
|
|
|
728
797
|
// its Usage type, so read it through a narrow cast. Verified against the live API.
|
|
729
798
|
const thinkingTokens = event.usage
|
|
730
799
|
.output_tokens_details?.thinking_tokens;
|
|
800
|
+
// Vercel AI Gateway includes the TTL breakdown in deltas, though the SDK only types it on message_start.
|
|
801
|
+
const cacheCreation = event.usage.cache_creation;
|
|
802
|
+
if (cacheCreation?.ephemeral_1h_input_tokens != null) {
|
|
803
|
+
output.usage.cacheWrite1h = cacheCreation.ephemeral_1h_input_tokens;
|
|
804
|
+
}
|
|
731
805
|
if (thinkingTokens != null) {
|
|
732
806
|
output.usage.reasoning = thinkingTokens;
|
|
733
807
|
}
|
|
@@ -736,6 +810,9 @@ export const stream = (model, context, options) => {
|
|
|
736
810
|
output.usage.totalTokens =
|
|
737
811
|
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
738
812
|
calculateCost(usageModel, output.usage);
|
|
813
|
+
servedSpeed = reportedSpeed(event.usage) ?? servedSpeed;
|
|
814
|
+
if (servedSpeed === "fast")
|
|
815
|
+
applyFastModePricing(output.usage);
|
|
739
816
|
// A non-empty final fallback report supersedes the request-start report with
|
|
740
817
|
// the serving model's transformations. An empty report must not erase drops
|
|
741
818
|
// already reported at message_start.
|
|
@@ -839,7 +916,7 @@ export const streamSimple = (model, context, options) => {
|
|
|
839
916
|
function isOAuthToken(apiKey) {
|
|
840
917
|
return apiKey.includes("sk-ant-oat");
|
|
841
918
|
}
|
|
842
|
-
function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId) {
|
|
919
|
+
function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStreamingBeta, useServerSideFallbackBeta, optionsHeaders, fetch, dynamicHeaders, sessionId, useNativeToolChanges = false) {
|
|
843
920
|
// Adaptive thinking models have interleaved thinking built in, so skip the beta header.
|
|
844
921
|
const needsInterleavedBeta = interleavedThinking && model.compat?.forceAdaptiveThinking !== true;
|
|
845
922
|
const betaFeatures = [];
|
|
@@ -858,6 +935,10 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
|
|
|
858
935
|
if (supportsMidConvoEffort(model)) {
|
|
859
936
|
betaFeatures.push(MID_CONVERSATION_OUTPUT_CONFIG_BETA, THINKING_BINDING_CONTROLS_BETA);
|
|
860
937
|
}
|
|
938
|
+
if (useNativeToolChanges)
|
|
939
|
+
betaFeatures.push(MID_CONVERSATION_TOOL_CHANGES_BETA);
|
|
940
|
+
if (requestedSpeed(model) === "fast")
|
|
941
|
+
betaFeatures.push(FAST_MODE_BETA);
|
|
861
942
|
const uniqueBetaFeatures = [...new Set(betaFeatures)];
|
|
862
943
|
// Copilot: Bearer auth, selective betas.
|
|
863
944
|
if (model.provider === "github-copilot") {
|
|
@@ -915,29 +996,74 @@ function createClient(model, apiKey, interleavedThinking, useFineGrainedToolStre
|
|
|
915
996
|
});
|
|
916
997
|
return { client, isOAuthToken: false };
|
|
917
998
|
}
|
|
999
|
+
function getNativeToolChangeBetas(model, context, isOAuth, options) {
|
|
1000
|
+
let configured;
|
|
1001
|
+
for (const headers of [model.headers, options?.headers]) {
|
|
1002
|
+
for (const [name, value] of Object.entries(headers ?? {})) {
|
|
1003
|
+
if (name.toLowerCase() === "anthropic-beta")
|
|
1004
|
+
configured = value;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
if (configured === null)
|
|
1008
|
+
return [];
|
|
1009
|
+
if (configured !== undefined)
|
|
1010
|
+
return [
|
|
1011
|
+
...new Set(configured
|
|
1012
|
+
.split(",")
|
|
1013
|
+
.map((feature) => feature.trim())
|
|
1014
|
+
.filter(Boolean)),
|
|
1015
|
+
];
|
|
1016
|
+
const features = [MID_CONVERSATION_TOOL_CHANGES_BETA];
|
|
1017
|
+
if (isOAuth)
|
|
1018
|
+
features.push("claude-code-20250219", "oauth-2025-04-20");
|
|
1019
|
+
if (shouldUseFineGrainedToolStreamingBeta(model, context))
|
|
1020
|
+
features.push(FINE_GRAINED_TOOL_STREAMING_BETA);
|
|
1021
|
+
if (model.reasoning &&
|
|
1022
|
+
options?.thinkingEnabled &&
|
|
1023
|
+
(options.interleavedThinking ?? true) &&
|
|
1024
|
+
model.compat?.forceAdaptiveThinking !== true) {
|
|
1025
|
+
features.push(INTERLEAVED_THINKING_BETA);
|
|
1026
|
+
}
|
|
1027
|
+
if (shouldUseServerSideFallbackBeta(model))
|
|
1028
|
+
features.push(SERVER_SIDE_FALLBACK_BETA);
|
|
1029
|
+
if (shouldUseThinkingBindingControlsBeta(model))
|
|
1030
|
+
features.push(THINKING_BINDING_CONTROLS_BETA);
|
|
1031
|
+
if (supportsMidConvoEffort(model))
|
|
1032
|
+
features.push(MID_CONVERSATION_OUTPUT_CONFIG_BETA, THINKING_BINDING_CONTROLS_BETA);
|
|
1033
|
+
if (requestedSpeed(model) === "fast")
|
|
1034
|
+
features.push(FAST_MODE_BETA);
|
|
1035
|
+
return [...new Set(features)];
|
|
1036
|
+
}
|
|
918
1037
|
function buildParams(model, context, isOAuthToken, options) {
|
|
919
1038
|
const { cacheControl } = getCacheControl(model, options?.cacheRetention, options?.env);
|
|
920
1039
|
const compat = getAnthropicCompat(model);
|
|
1040
|
+
const initialSystemMessage = getInitialSystemMessage(context.messages);
|
|
1041
|
+
const initialSystemText = initialSystemMessage ? getSystemMessageText(initialSystemMessage) : "";
|
|
921
1042
|
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
922
|
-
const
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
const converted = convertMessages(
|
|
1043
|
+
const conversationMessages = initialSystemMessage ? transformedMessages.slice(1) : transformedMessages;
|
|
1044
|
+
// Native tool changes reference tools by name, so a redefined name cannot be expressed,
|
|
1045
|
+
// and Anthropic rejects a tool list where every tool is deferred, so there must be an
|
|
1046
|
+
// initial active tool to anchor the deferred ones. Otherwise the current tool list is sent.
|
|
1047
|
+
const initialTools = initialSystemMessage?.toolsAdded ?? [];
|
|
1048
|
+
const nativeToolChanges = compat.supportsMidConvoSystemMessages &&
|
|
1049
|
+
compat.supportsMidConvoToolChanges &&
|
|
1050
|
+
initialTools.length > 0 &&
|
|
1051
|
+
!hasToolRedefinitions(context.messages);
|
|
1052
|
+
const converted = convertMessages(conversationMessages, isOAuthToken, cacheControl, compat.allowEmptySignature, supportsMidConvoEffort(model) ? model.provider : undefined, nativeToolChanges);
|
|
932
1053
|
const activeEffort = options?.effort ?? "high";
|
|
933
1054
|
const params = {
|
|
934
|
-
model: model
|
|
1055
|
+
model: upstreamModelId(model),
|
|
935
1056
|
messages: (supportsMidConvoEffort(model)
|
|
936
1057
|
? insertThinkingLevelMessages(converted, activeEffort)
|
|
937
1058
|
: converted.messages),
|
|
938
1059
|
max_tokens: options?.maxTokens ?? model.maxTokens,
|
|
939
1060
|
stream: true,
|
|
940
1061
|
};
|
|
1062
|
+
if (nativeToolChanges)
|
|
1063
|
+
params.betas = getNativeToolChangeBetas(model, context, isOAuthToken, options);
|
|
1064
|
+
const speed = requestedSpeed(model);
|
|
1065
|
+
if (speed !== undefined)
|
|
1066
|
+
params.speed = speed;
|
|
941
1067
|
// For OAuth tokens, we MUST include Claude Code identity
|
|
942
1068
|
if (isOAuthToken) {
|
|
943
1069
|
params.system = [
|
|
@@ -947,20 +1073,20 @@ function buildParams(model, context, isOAuthToken, options) {
|
|
|
947
1073
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
948
1074
|
},
|
|
949
1075
|
];
|
|
950
|
-
if (
|
|
1076
|
+
if (initialSystemText) {
|
|
951
1077
|
params.system.push({
|
|
952
1078
|
type: "text",
|
|
953
|
-
text: sanitizeSurrogates(
|
|
1079
|
+
text: sanitizeSurrogates(initialSystemText),
|
|
954
1080
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
955
1081
|
});
|
|
956
1082
|
}
|
|
957
1083
|
}
|
|
958
|
-
else if (
|
|
1084
|
+
else if (initialSystemText) {
|
|
959
1085
|
// Add cache control to system prompt for non-OAuth tokens
|
|
960
1086
|
params.system = [
|
|
961
1087
|
{
|
|
962
1088
|
type: "text",
|
|
963
|
-
text: sanitizeSurrogates(
|
|
1089
|
+
text: sanitizeSurrogates(initialSystemText),
|
|
964
1090
|
...(cacheControl ? { cache_control: cacheControl } : {}),
|
|
965
1091
|
},
|
|
966
1092
|
];
|
|
@@ -972,12 +1098,26 @@ function buildParams(model, context, isOAuthToken, options) {
|
|
|
972
1098
|
compat.supportsTemperature) {
|
|
973
1099
|
params.temperature = options.temperature;
|
|
974
1100
|
}
|
|
975
|
-
|
|
1101
|
+
const toolCacheControl = compat.supportsCacheControlOnTools ? cacheControl : undefined;
|
|
1102
|
+
if (nativeToolChanges) {
|
|
1103
|
+
// Initial tools stay active with the cache breakpoint on the last one. Every later
|
|
1104
|
+
// declaration is deferred and only surfaced by its `tool_addition` block; removed
|
|
1105
|
+
// tools stay declared and are withdrawn by `tool_removal`. The request-level list
|
|
1106
|
+
// therefore only grows, keeping the cached prefix intact across tool changes.
|
|
1107
|
+
const initialNames = new Set(initialTools.map((tool) => tool.name));
|
|
1108
|
+
const laterTools = getDeclaredTools(context.messages).filter((tool) => !initialNames.has(tool.name));
|
|
976
1109
|
params.tools = [
|
|
977
|
-
...convertTools(
|
|
978
|
-
|
|
1110
|
+
...convertTools(initialTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl),
|
|
1111
|
+
DEFERRED_TOOL_PLACEHOLDER,
|
|
1112
|
+
...convertTools(laterTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools).map((tool) => ({ ...tool, defer_loading: true })),
|
|
979
1113
|
];
|
|
980
1114
|
}
|
|
1115
|
+
else {
|
|
1116
|
+
const tools = getCurrentTools(context.messages);
|
|
1117
|
+
if (tools.length > 0) {
|
|
1118
|
+
params.tools = convertTools(tools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
981
1121
|
// Managed effort models always use adaptive thinking. Their per-turn markers
|
|
982
1122
|
// carry the actual effort; the top-level high value deliberately stays stable.
|
|
983
1123
|
if (supportsMidConvoEffort(model)) {
|
|
@@ -1082,41 +1222,54 @@ function buildParams(model, context, isOAuthToken, options) {
|
|
|
1082
1222
|
function normalizeToolCallId(id) {
|
|
1083
1223
|
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
1084
1224
|
}
|
|
1085
|
-
function convertToolResult(msg
|
|
1086
|
-
const references = [];
|
|
1087
|
-
for (const name of msg.addedToolNames ?? []) {
|
|
1088
|
-
const normalizedName = normalizeToolName(name);
|
|
1089
|
-
if (!deferredToolNames.has(normalizedName) || loadedToolNames.has(normalizedName))
|
|
1090
|
-
continue;
|
|
1091
|
-
loadedToolNames.add(normalizedName);
|
|
1092
|
-
references.push({
|
|
1093
|
-
type: "tool_reference",
|
|
1094
|
-
tool_name: isOAuthToken ? toClaudeCodeName(name) : name,
|
|
1095
|
-
});
|
|
1096
|
-
}
|
|
1097
|
-
const convertedContent = convertContentBlocks(msg.content);
|
|
1098
|
-
// Anthropic rejects tool references mixed with ordinary tool-result content.
|
|
1225
|
+
function convertToolResult(msg) {
|
|
1099
1226
|
return {
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
is_error: msg.isError,
|
|
1105
|
-
},
|
|
1106
|
-
siblingContent: references.length === 0
|
|
1107
|
-
? []
|
|
1108
|
-
: typeof convertedContent === "string"
|
|
1109
|
-
? [{ type: "text", text: convertedContent }]
|
|
1110
|
-
: convertedContent,
|
|
1227
|
+
type: "tool_result",
|
|
1228
|
+
tool_use_id: msg.toolCallId,
|
|
1229
|
+
content: convertContentBlocks(msg.content),
|
|
1230
|
+
is_error: msg.isError,
|
|
1111
1231
|
};
|
|
1112
1232
|
}
|
|
1113
|
-
function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowEmptySignature = false,
|
|
1233
|
+
function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowEmptySignature = false, managedProvider, nativeToolChanges = false) {
|
|
1114
1234
|
const params = [];
|
|
1115
1235
|
const assistantLevels = new Map();
|
|
1116
|
-
|
|
1236
|
+
// Later system messages are held back and emitted directly before the next assistant
|
|
1237
|
+
// message (or at the end of the transcript). Anthropic requires `tool_result` blocks to
|
|
1238
|
+
// immediately follow their `tool_use`, so a system message between them is rejected; this
|
|
1239
|
+
// also mirrors where the managed-effort system messages are inserted. As a result an
|
|
1240
|
+
// update placed before a user message in the transcript lands after it on the wire.
|
|
1241
|
+
const pendingSystemMessages = [];
|
|
1242
|
+
const flushPendingSystemMessages = () => {
|
|
1243
|
+
params.push(...pendingSystemMessages);
|
|
1244
|
+
pendingSystemMessages.length = 0;
|
|
1245
|
+
};
|
|
1117
1246
|
for (let i = 0; i < transformedMessages.length; i++) {
|
|
1118
1247
|
const msg = transformedMessages[i];
|
|
1119
|
-
if (msg.role === "
|
|
1248
|
+
if (msg.role === "system") {
|
|
1249
|
+
// Later system messages only reach this point when the model accepts them natively;
|
|
1250
|
+
// otherwise the transcript was collapsed into the leading message before conversion.
|
|
1251
|
+
const text = renderSystemMessageUpdate(msg);
|
|
1252
|
+
const blocks = [];
|
|
1253
|
+
if (text.length > 0)
|
|
1254
|
+
blocks.push({ type: "text", text: sanitizeSurrogates(text) });
|
|
1255
|
+
if (nativeToolChanges) {
|
|
1256
|
+
for (const tool of msg.toolsRemoved ?? []) {
|
|
1257
|
+
blocks.push({
|
|
1258
|
+
type: "tool_removal",
|
|
1259
|
+
tool: { type: "tool_reference", name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name },
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
for (const tool of msg.toolsAdded ?? []) {
|
|
1263
|
+
blocks.push({
|
|
1264
|
+
type: "tool_addition",
|
|
1265
|
+
tool: { type: "tool_reference", name: isOAuthToken ? toClaudeCodeName(tool.name) : tool.name },
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
if (blocks.length > 0)
|
|
1270
|
+
pendingSystemMessages.push({ role: "system", content: blocks });
|
|
1271
|
+
}
|
|
1272
|
+
else if (msg.role === "user") {
|
|
1120
1273
|
if (typeof msg.content === "string") {
|
|
1121
1274
|
if (msg.content.trim().length > 0) {
|
|
1122
1275
|
params.push({
|
|
@@ -1173,6 +1326,7 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
|
|
|
1173
1326
|
}
|
|
1174
1327
|
}
|
|
1175
1328
|
else if (msg.role === "assistant") {
|
|
1329
|
+
flushPendingSystemMessages();
|
|
1176
1330
|
const blocks = [];
|
|
1177
1331
|
for (const block of msg.content) {
|
|
1178
1332
|
if (block.type === "text") {
|
|
@@ -1261,31 +1415,32 @@ function convertMessages(transformedMessages, isOAuthToken, cacheControl, allowE
|
|
|
1261
1415
|
else if (msg.role === "toolResult") {
|
|
1262
1416
|
// Collect all consecutive toolResult messages, needed for z.ai Anthropic endpoint.
|
|
1263
1417
|
const toolResults = [];
|
|
1264
|
-
const siblingContent = [];
|
|
1265
1418
|
let j = i;
|
|
1266
1419
|
while (j < transformedMessages.length && transformedMessages[j].role === "toolResult") {
|
|
1267
|
-
|
|
1268
|
-
toolResults.push(converted.toolResult);
|
|
1269
|
-
siblingContent.push(...converted.siblingContent);
|
|
1420
|
+
toolResults.push(convertToolResult(transformedMessages[j]));
|
|
1270
1421
|
j++;
|
|
1271
1422
|
}
|
|
1272
1423
|
// Skip the messages we've already processed.
|
|
1273
1424
|
i = j - 1;
|
|
1274
|
-
// Displaced reference-bearing results must follow every tool_result block.
|
|
1275
1425
|
params.push({
|
|
1276
1426
|
role: "user",
|
|
1277
|
-
content:
|
|
1427
|
+
content: toolResults,
|
|
1278
1428
|
});
|
|
1279
1429
|
}
|
|
1280
1430
|
}
|
|
1281
|
-
|
|
1431
|
+
flushPendingSystemMessages();
|
|
1432
|
+
// Add cache_control to the last user or system message to cache conversation history
|
|
1282
1433
|
if (cacheControl && params.length > 0) {
|
|
1283
1434
|
const lastMessage = params[params.length - 1];
|
|
1284
|
-
if (lastMessage.role === "user") {
|
|
1435
|
+
if (lastMessage.role === "user" || lastMessage.role === "system") {
|
|
1285
1436
|
if (Array.isArray(lastMessage.content)) {
|
|
1286
1437
|
const lastBlock = lastMessage.content[lastMessage.content.length - 1];
|
|
1287
1438
|
if (lastBlock &&
|
|
1288
|
-
(lastBlock.type === "text" ||
|
|
1439
|
+
(lastBlock.type === "text" ||
|
|
1440
|
+
lastBlock.type === "image" ||
|
|
1441
|
+
lastBlock.type === "tool_result" ||
|
|
1442
|
+
lastBlock.type === "tool_addition" ||
|
|
1443
|
+
lastBlock.type === "tool_removal")) {
|
|
1289
1444
|
lastBlock.cache_control = cacheControl;
|
|
1290
1445
|
}
|
|
1291
1446
|
}
|
|
@@ -1318,7 +1473,7 @@ function insertThinkingLevelMessages(converted, activeEffort) {
|
|
|
1318
1473
|
return messages;
|
|
1319
1474
|
}
|
|
1320
1475
|
function shouldUseFineGrainedToolStreamingBeta(model, context) {
|
|
1321
|
-
return
|
|
1476
|
+
return getCurrentTools(context.messages).length > 0 && !getAnthropicCompat(model).supportsEagerToolInputStreaming;
|
|
1322
1477
|
}
|
|
1323
1478
|
function projectObjectUnionForAnthropic(schema) {
|
|
1324
1479
|
if (schema.properties !== undefined || !Array.isArray(schema.anyOf) || schema.anyOf.length === 0)
|
|
@@ -1354,12 +1509,16 @@ function projectObjectUnionForAnthropic(schema) {
|
|
|
1354
1509
|
required: (branches[0]?.required ?? []).filter((key) => branches.every((branch) => (branch.required ?? []).includes(key))),
|
|
1355
1510
|
};
|
|
1356
1511
|
}
|
|
1357
|
-
function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supportsStrictTools, cacheControl
|
|
1512
|
+
function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supportsStrictTools, cacheControl) {
|
|
1358
1513
|
if (!tools)
|
|
1359
1514
|
return [];
|
|
1515
|
+
// OAuth canonicalization can make distinct transcript names collide. Keep the latest
|
|
1516
|
+
// declaration, as the pre-transcript tool serializer did, without sending duplicate names.
|
|
1517
|
+
if (isOAuthToken)
|
|
1518
|
+
tools = [...new Map(tools.map((tool) => [toClaudeCodeName(tool.name), tool])).values()];
|
|
1360
1519
|
return tools.map((tool, index) => {
|
|
1361
|
-
const strict = resolveJsonSchemaStrictSampling(tool, supportsStrictTools);
|
|
1362
|
-
const parameters = getJsonSchemaToolParameters(tool, strict);
|
|
1520
|
+
const strict = resolveJsonSchemaStrictSampling(tool, supportsStrictTools, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
|
|
1521
|
+
const parameters = getJsonSchemaToolParameters(tool, strict, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
|
|
1363
1522
|
const schema = parameters;
|
|
1364
1523
|
// Anthropic rejects top-level combinators, so project object-union fields for advertising only.
|
|
1365
1524
|
// Local runtime validation still enforces the complete authored union without mutating it.
|
|
@@ -1381,7 +1540,6 @@ function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supp
|
|
|
1381
1540
|
...(supportsEagerToolInputStreaming ? { eager_input_streaming: true } : {}),
|
|
1382
1541
|
...(strict === true ? { strict: true } : {}),
|
|
1383
1542
|
input_schema: inputSchema,
|
|
1384
|
-
...(deferLoading ? { defer_loading: true } : {}),
|
|
1385
1543
|
...(cacheControl && index === tools.length - 1 ? { cache_control: cacheControl } : {}),
|
|
1386
1544
|
};
|
|
1387
1545
|
});
|