jeopi-ai 16.2.13
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 +4347 -0
- package/README.md +1193 -0
- package/dist/types/api-registry.d.ts +30 -0
- package/dist/types/auth-broker/client.d.ts +73 -0
- package/dist/types/auth-broker/discover.d.ts +35 -0
- package/dist/types/auth-broker/index.d.ts +7 -0
- package/dist/types/auth-broker/refresher.d.ts +25 -0
- package/dist/types/auth-broker/remote-store.d.ts +102 -0
- package/dist/types/auth-broker/server.d.ts +43 -0
- package/dist/types/auth-broker/snapshot-cache.d.ts +17 -0
- package/dist/types/auth-broker/types.d.ts +107 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +411 -0
- package/dist/types/auth-gateway/http.d.ts +39 -0
- package/dist/types/auth-gateway/index.d.ts +3 -0
- package/dist/types/auth-gateway/server.d.ts +36 -0
- package/dist/types/auth-gateway/types.d.ts +123 -0
- package/dist/types/auth-retry.d.ts +124 -0
- package/dist/types/auth-storage.d.ts +1026 -0
- package/dist/types/dialect/anthropic.d.ts +15 -0
- package/dist/types/dialect/catalog.d.ts +3 -0
- package/dist/types/dialect/coercion.d.ts +23 -0
- package/dist/types/dialect/deepseek.d.ts +14 -0
- package/dist/types/dialect/demotion.d.ts +23 -0
- package/dist/types/dialect/examples.d.ts +2 -0
- package/dist/types/dialect/factory.d.ts +3 -0
- package/dist/types/dialect/fenced-thinking.d.ts +53 -0
- package/dist/types/dialect/gemini.d.ts +17 -0
- package/dist/types/dialect/gemma.d.ts +15 -0
- package/dist/types/dialect/glm.d.ts +9 -0
- package/dist/types/dialect/harmony.d.ts +8 -0
- package/dist/types/dialect/hermes.d.ts +9 -0
- package/dist/types/dialect/history.d.ts +3 -0
- package/dist/types/dialect/index.d.ts +11 -0
- package/dist/types/dialect/inventory.d.ts +12 -0
- package/dist/types/dialect/kimi.d.ts +14 -0
- package/dist/types/dialect/minimax.d.ts +3 -0
- package/dist/types/dialect/owned-stream.d.ts +4 -0
- package/dist/types/dialect/qwen3.d.ts +9 -0
- package/dist/types/dialect/rendering.d.ts +45 -0
- package/dist/types/dialect/thinking.d.ts +6 -0
- package/dist/types/dialect/types.d.ts +69 -0
- package/dist/types/dialect/xml.d.ts +9 -0
- package/dist/types/error/abort.d.ts +14 -0
- package/dist/types/error/auth-classify.d.ts +16 -0
- package/dist/types/error/auth.d.ts +27 -0
- package/dist/types/error/aws.d.ts +23 -0
- package/dist/types/error/classes.d.ts +102 -0
- package/dist/types/error/finalize.d.ts +39 -0
- package/dist/types/error/flags.d.ts +79 -0
- package/dist/types/error/format.d.ts +20 -0
- package/dist/types/error/gateway.d.ts +20 -0
- package/dist/types/error/index.d.ts +13 -0
- package/dist/types/error/oauth.d.ts +43 -0
- package/dist/types/error/provider.d.ts +42 -0
- package/dist/types/error/rate-limit.d.ts +59 -0
- package/dist/types/error/retryable.d.ts +27 -0
- package/dist/types/error/validation.d.ts +32 -0
- package/dist/types/index.d.ts +49 -0
- package/dist/types/provider-details.d.ts +24 -0
- package/dist/types/providers/__tests__/google-auth.test.d.ts +1 -0
- package/dist/types/providers/__tests__/kimi-code-thinking.test.d.ts +1 -0
- package/dist/types/providers/__tests__/openai-codex-error.test.d.ts +1 -0
- package/dist/types/providers/amazon-bedrock.d.ts +39 -0
- package/dist/types/providers/anthropic-client.d.ts +94 -0
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +497 -0
- package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
- package/dist/types/providers/anthropic-wire.d.ts +318 -0
- package/dist/types/providers/anthropic.d.ts +248 -0
- package/dist/types/providers/aws-credentials.d.ts +53 -0
- package/dist/types/providers/aws-eventstream.d.ts +39 -0
- package/dist/types/providers/aws-sigv4.d.ts +55 -0
- package/dist/types/providers/azure-openai-responses.d.ts +16 -0
- package/dist/types/providers/cursor.d.ts +91 -0
- package/dist/types/providers/devin.d.ts +12 -0
- package/dist/types/providers/error-message.d.ts +27 -0
- package/dist/types/providers/github-copilot-headers.d.ts +40 -0
- package/dist/types/providers/gitlab-duo-workflow.d.ts +254 -0
- package/dist/types/providers/gitlab-duo.d.ts +27 -0
- package/dist/types/providers/google-auth.d.ts +32 -0
- package/dist/types/providers/google-gemini-cli.d.ts +118 -0
- package/dist/types/providers/google-interactions.d.ts +65 -0
- package/dist/types/providers/google-shared.d.ts +203 -0
- package/dist/types/providers/google-types.d.ts +155 -0
- package/dist/types/providers/google-vertex.d.ts +7 -0
- package/dist/types/providers/google.d.ts +4 -0
- package/dist/types/providers/grammar.d.ts +1 -0
- package/dist/types/providers/kimi.d.ts +27 -0
- package/dist/types/providers/mock.d.ts +178 -0
- package/dist/types/providers/ollama.d.ts +7 -0
- package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
- package/dist/types/providers/openai-chat-server-schema.d.ts +695 -0
- package/dist/types/providers/openai-chat-server.d.ts +16 -0
- package/dist/types/providers/openai-chat-wire.d.ts +644 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +54 -0
- package/dist/types/providers/openai-codex/response-handler.d.ts +26 -0
- package/dist/types/providers/openai-codex-responses.d.ts +108 -0
- package/dist/types/providers/openai-completions.d.ts +45 -0
- package/dist/types/providers/openai-reasoning-fallback.d.ts +25 -0
- package/dist/types/providers/openai-responses-server-schema.d.ts +349 -0
- package/dist/types/providers/openai-responses-server.d.ts +17 -0
- package/dist/types/providers/openai-responses-wire.d.ts +6065 -0
- package/dist/types/providers/openai-responses.d.ts +126 -0
- package/dist/types/providers/openai-shared.d.ts +506 -0
- package/dist/types/providers/pi-native-client.d.ts +13 -0
- package/dist/types/providers/pi-native-server.d.ts +69 -0
- package/dist/types/providers/register-builtins.d.ts +32 -0
- package/dist/types/providers/synthetic.d.ts +26 -0
- package/dist/types/providers/transform-messages.d.ts +11 -0
- package/dist/types/providers/vision-guard.d.ts +20 -0
- package/dist/types/registry/aimlapi.d.ts +4 -0
- package/dist/types/registry/alibaba-coding-plan.d.ts +8 -0
- package/dist/types/registry/amazon-bedrock.d.ts +5 -0
- package/dist/types/registry/anthropic.d.ts +10 -0
- package/dist/types/registry/api-key-login.d.ts +42 -0
- package/dist/types/registry/api-key-validation.d.ts +43 -0
- package/dist/types/registry/azure.d.ts +4 -0
- package/dist/types/registry/cerebras.d.ts +7 -0
- package/dist/types/registry/cloudflare-ai-gateway.d.ts +13 -0
- package/dist/types/registry/coreweave.d.ts +7 -0
- package/dist/types/registry/cursor.d.ts +7 -0
- package/dist/types/registry/deepseek.d.ts +8 -0
- package/dist/types/registry/derived.d.ts +5 -0
- package/dist/types/registry/devin.d.ts +8 -0
- package/dist/types/registry/firepass.d.ts +16 -0
- package/dist/types/registry/fireworks.d.ts +7 -0
- package/dist/types/registry/github-copilot.d.ts +7 -0
- package/dist/types/registry/gitlab-duo-workflow.d.ts +10 -0
- package/dist/types/registry/gitlab-duo.d.ts +9 -0
- package/dist/types/registry/google-antigravity.d.ts +9 -0
- package/dist/types/registry/google-gemini-cli.d.ts +9 -0
- package/dist/types/registry/google-vertex.d.ts +5 -0
- package/dist/types/registry/google.d.ts +4 -0
- package/dist/types/registry/groq.d.ts +4 -0
- package/dist/types/registry/huggingface.d.ts +7 -0
- package/dist/types/registry/index.d.ts +4 -0
- package/dist/types/registry/kagi.d.ts +14 -0
- package/dist/types/registry/kilo.d.ts +7 -0
- package/dist/types/registry/kimi-code.d.ts +7 -0
- package/dist/types/registry/litellm.d.ts +13 -0
- package/dist/types/registry/llama-cpp.d.ts +8 -0
- package/dist/types/registry/lm-studio.d.ts +8 -0
- package/dist/types/registry/minimax-code-cn.d.ts +6 -0
- package/dist/types/registry/minimax-code.d.ts +6 -0
- package/dist/types/registry/minimax.d.ts +4 -0
- package/dist/types/registry/mistral.d.ts +4 -0
- package/dist/types/registry/moonshot.d.ts +7 -0
- package/dist/types/registry/nanogpt.d.ts +7 -0
- package/dist/types/registry/nvidia.d.ts +7 -0
- package/dist/types/registry/oauth/__tests__/xai-oauth.test.d.ts +1 -0
- package/dist/types/registry/oauth/anthropic.d.ts +23 -0
- package/dist/types/registry/oauth/callback-server.d.ts +72 -0
- package/dist/types/registry/oauth/cursor.d.ts +15 -0
- package/dist/types/registry/oauth/devin.d.ts +5 -0
- package/dist/types/registry/oauth/github-copilot.d.ts +30 -0
- package/dist/types/registry/oauth/gitlab-duo-workflow.d.ts +6 -0
- package/dist/types/registry/oauth/gitlab-duo.d.ts +3 -0
- package/dist/types/registry/oauth/google-antigravity.d.ts +11 -0
- package/dist/types/registry/oauth/google-gemini-cli.d.ts +10 -0
- package/dist/types/registry/oauth/google-oauth-shared.d.ts +22 -0
- package/dist/types/registry/oauth/index.d.ts +64 -0
- package/dist/types/registry/oauth/kimi.d.ts +21 -0
- package/dist/types/registry/oauth/minimax-code.d.ts +27 -0
- package/dist/types/registry/oauth/openai-codex.d.ts +33 -0
- package/dist/types/registry/oauth/opencode.d.ts +18 -0
- package/dist/types/registry/oauth/perplexity.d.ts +9 -0
- package/dist/types/registry/oauth/pkce.d.ts +8 -0
- package/dist/types/registry/oauth/types.d.ts +56 -0
- package/dist/types/registry/oauth/wafer.d.ts +1 -0
- package/dist/types/registry/oauth/xai-oauth.d.ts +52 -0
- package/dist/types/registry/oauth/xiaomi.d.ts +25 -0
- package/dist/types/registry/ollama-cloud.d.ts +7 -0
- package/dist/types/registry/ollama.d.ts +12 -0
- package/dist/types/registry/openai-codex-device.d.ts +8 -0
- package/dist/types/registry/openai-codex.d.ts +9 -0
- package/dist/types/registry/openai.d.ts +4 -0
- package/dist/types/registry/opencode-go.d.ts +6 -0
- package/dist/types/registry/opencode-zen.d.ts +6 -0
- package/dist/types/registry/openrouter.d.ts +13 -0
- package/dist/types/registry/parallel.d.ts +14 -0
- package/dist/types/registry/perplexity.d.ts +7 -0
- package/dist/types/registry/qianfan.d.ts +7 -0
- package/dist/types/registry/qwen-portal.d.ts +7 -0
- package/dist/types/registry/registry.d.ts +303 -0
- package/dist/types/registry/sakana.d.ts +7 -0
- package/dist/types/registry/synthetic.d.ts +6 -0
- package/dist/types/registry/tavily.d.ts +14 -0
- package/dist/types/registry/together.d.ts +6 -0
- package/dist/types/registry/types.d.ts +51 -0
- package/dist/types/registry/umans.d.ts +7 -0
- package/dist/types/registry/venice.d.ts +13 -0
- package/dist/types/registry/vercel-ai-gateway.d.ts +7 -0
- package/dist/types/registry/vllm.d.ts +7 -0
- package/dist/types/registry/wafer-serverless.d.ts +6 -0
- package/dist/types/registry/xai-oauth.d.ts +7 -0
- package/dist/types/registry/xai.d.ts +4 -0
- package/dist/types/registry/xiaomi-token-plan-ams.d.ts +6 -0
- package/dist/types/registry/xiaomi-token-plan-cn.d.ts +6 -0
- package/dist/types/registry/xiaomi-token-plan-sgp.d.ts +6 -0
- package/dist/types/registry/xiaomi.d.ts +6 -0
- package/dist/types/registry/zai.d.ts +7 -0
- package/dist/types/registry/zenmux.d.ts +7 -0
- package/dist/types/registry/zhipu-coding-plan.d.ts +7 -0
- package/dist/types/stream.d.ts +44 -0
- package/dist/types/types.d.ts +715 -0
- package/dist/types/usage/claude.d.ts +4 -0
- package/dist/types/usage/gemini.d.ts +2 -0
- package/dist/types/usage/github-copilot.d.ts +7 -0
- package/dist/types/usage/google-antigravity.d.ts +15 -0
- package/dist/types/usage/kimi.d.ts +2 -0
- package/dist/types/usage/minimax-code.d.ts +2 -0
- package/dist/types/usage/ollama.d.ts +5 -0
- package/dist/types/usage/openai-codex-base-url.d.ts +18 -0
- package/dist/types/usage/openai-codex-reset.d.ts +79 -0
- package/dist/types/usage/openai-codex.d.ts +3 -0
- package/dist/types/usage/opencode-go.d.ts +2 -0
- package/dist/types/usage/shared.d.ts +1 -0
- package/dist/types/usage/zai.d.ts +2 -0
- package/dist/types/usage.d.ts +346 -0
- package/dist/types/utils/abort.d.ts +25 -0
- package/dist/types/utils/anthropic-auth.d.ts +35 -0
- package/dist/types/utils/block-symbols.d.ts +20 -0
- package/dist/types/utils/deterministic-id.d.ts +16 -0
- package/dist/types/utils/empty-completion-retry.d.ts +21 -0
- package/dist/types/utils/event-stream.d.ts +30 -0
- package/dist/types/utils/foundry.d.ts +1 -0
- package/dist/types/utils/google-validation.d.ts +2 -0
- package/dist/types/utils/harmony-leak.d.ts +118 -0
- package/dist/types/utils/http-inspector.d.ts +30 -0
- package/dist/types/utils/idle-iterator.d.ts +137 -0
- package/dist/types/utils/leaked-thinking-stream.d.ts +29 -0
- package/dist/types/utils/openai-http.d.ts +54 -0
- package/dist/types/utils/openrouter-headers.d.ts +1 -0
- package/dist/types/utils/parse-bind.d.ts +23 -0
- package/dist/types/utils/provider-response.d.ts +3 -0
- package/dist/types/utils/proxy.d.ts +29 -0
- package/dist/types/utils/request-debug.d.ts +29 -0
- package/dist/types/utils/retry-after.d.ts +4 -0
- package/dist/types/utils/retry.d.ts +14 -0
- package/dist/types/utils/schema/adapt.d.ts +24 -0
- package/dist/types/utils/schema/compatibility.d.ts +30 -0
- package/dist/types/utils/schema/dereference.d.ts +11 -0
- package/dist/types/utils/schema/draft.d.ts +10 -0
- package/dist/types/utils/schema/equality.d.ts +4 -0
- package/dist/types/utils/schema/fields.d.ts +54 -0
- package/dist/types/utils/schema/index.d.ts +15 -0
- package/dist/types/utils/schema/json-schema-validator.d.ts +20 -0
- package/dist/types/utils/schema/meta-validator.d.ts +2 -0
- package/dist/types/utils/schema/normalize.d.ts +124 -0
- package/dist/types/utils/schema/spill.d.ts +8 -0
- package/dist/types/utils/schema/stamps.d.ts +17 -0
- package/dist/types/utils/schema/strict-tool-validation.d.ts +16 -0
- package/dist/types/utils/schema/types.d.ts +4 -0
- package/dist/types/utils/schema/typescript.d.ts +18 -0
- package/dist/types/utils/schema/wire.d.ts +92 -0
- package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
- package/dist/types/utils/sdk-stream-timeout.d.ts +33 -0
- package/dist/types/utils/sse-debug.d.ts +5 -0
- package/dist/types/utils/stream-markup-healing.d.ts +87 -0
- package/dist/types/utils/thinking-loop.d.ts +102 -0
- package/dist/types/utils/tool-call-loop-guard.d.ts +26 -0
- package/dist/types/utils/tool-choice.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +42 -0
- package/dist/types/utils.d.ts +24 -0
- package/package.json +139 -0
- package/src/api-registry.ts +109 -0
- package/src/auth-broker/client.ts +359 -0
- package/src/auth-broker/discover.ts +222 -0
- package/src/auth-broker/index.ts +7 -0
- package/src/auth-broker/refresher.ts +117 -0
- package/src/auth-broker/remote-store.ts +657 -0
- package/src/auth-broker/server.ts +646 -0
- package/src/auth-broker/snapshot-cache.ts +191 -0
- package/src/auth-broker/types.ts +130 -0
- package/src/auth-broker/wire-schemas.ts +249 -0
- package/src/auth-gateway/http.ts +194 -0
- package/src/auth-gateway/index.ts +3 -0
- package/src/auth-gateway/server.ts +802 -0
- package/src/auth-gateway/types.ts +151 -0
- package/src/auth-retry.ts +250 -0
- package/src/auth-storage.ts +5576 -0
- package/src/dialect/anthropic.md +31 -0
- package/src/dialect/anthropic.ts +608 -0
- package/src/dialect/catalog.ts +29 -0
- package/src/dialect/coercion.ts +136 -0
- package/src/dialect/deepseek.md +24 -0
- package/src/dialect/deepseek.ts +609 -0
- package/src/dialect/demotion.ts +36 -0
- package/src/dialect/examples.ts +33 -0
- package/src/dialect/factory.ts +34 -0
- package/src/dialect/fenced-thinking.ts +184 -0
- package/src/dialect/gemini.md +44 -0
- package/src/dialect/gemini.ts +597 -0
- package/src/dialect/gemma.md +33 -0
- package/src/dialect/gemma.ts +387 -0
- package/src/dialect/glm.md +32 -0
- package/src/dialect/glm.ts +456 -0
- package/src/dialect/harmony.md +31 -0
- package/src/dialect/harmony.ts +346 -0
- package/src/dialect/hermes.md +25 -0
- package/src/dialect/hermes.ts +206 -0
- package/src/dialect/history.ts +81 -0
- package/src/dialect/index.ts +15 -0
- package/src/dialect/inventory.ts +73 -0
- package/src/dialect/kimi.md +24 -0
- package/src/dialect/kimi.ts +340 -0
- package/src/dialect/minimax.md +31 -0
- package/src/dialect/minimax.ts +95 -0
- package/src/dialect/owned-stream.ts +470 -0
- package/src/dialect/prompt-template.md +12 -0
- package/src/dialect/qwen3.md +28 -0
- package/src/dialect/qwen3.ts +240 -0
- package/src/dialect/rendering.ts +249 -0
- package/src/dialect/thinking.ts +122 -0
- package/src/dialect/types.ts +57 -0
- package/src/dialect/xml.md +22 -0
- package/src/dialect/xml.ts +90 -0
- package/src/error/abort.ts +18 -0
- package/src/error/auth-classify.ts +30 -0
- package/src/error/auth.ts +48 -0
- package/src/error/aws.ts +31 -0
- package/src/error/classes.ts +186 -0
- package/src/error/finalize.ts +69 -0
- package/src/error/flags.ts +506 -0
- package/src/error/format.ts +45 -0
- package/src/error/gateway.ts +96 -0
- package/src/error/index.ts +13 -0
- package/src/error/oauth.ts +58 -0
- package/src/error/provider.ts +62 -0
- package/src/error/rate-limit.ts +161 -0
- package/src/error/retryable.ts +70 -0
- package/src/error/validation.ts +44 -0
- package/src/index.ts +49 -0
- package/src/provider-details.ts +90 -0
- package/src/providers/__tests__/google-auth.test.ts +144 -0
- package/src/providers/__tests__/kimi-code-thinking.test.ts +112 -0
- package/src/providers/__tests__/openai-codex-error.test.ts +84 -0
- package/src/providers/amazon-bedrock.ts +1042 -0
- package/src/providers/anthropic-client.ts +295 -0
- package/src/providers/anthropic-messages-server-schema.ts +252 -0
- package/src/providers/anthropic-messages-server.ts +756 -0
- package/src/providers/anthropic-wire.ts +318 -0
- package/src/providers/anthropic.ts +4078 -0
- package/src/providers/aws-credentials.ts +586 -0
- package/src/providers/aws-eventstream.ts +181 -0
- package/src/providers/aws-sigv4.ts +218 -0
- package/src/providers/azure-openai-responses.ts +382 -0
- package/src/providers/cursor/proto/agent.proto +3526 -0
- package/src/providers/cursor/proto/buf.gen.yaml +6 -0
- package/src/providers/cursor/proto/buf.yaml +17 -0
- package/src/providers/cursor.ts +2695 -0
- package/src/providers/devin/proto/buf/validate/validate.proto +468 -0
- package/src/providers/devin/proto/buf.gen.yaml +33 -0
- package/src/providers/devin/proto/buf.yaml +17 -0
- package/src/providers/devin/proto/cel/expr/checked.proto +103 -0
- package/src/providers/devin/proto/cel/expr/eval.proto +38 -0
- package/src/providers/devin/proto/cel/expr/explain.proto +15 -0
- package/src/providers/devin/proto/cel/expr/syntax.proto +113 -0
- package/src/providers/devin/proto/cel/expr/value.proto +41 -0
- package/src/providers/devin/proto/connectext/grpc/status/v1/status.proto +11 -0
- package/src/providers/devin/proto/errorspb/errors.proto +56 -0
- package/src/providers/devin/proto/errorspb/hintdetail.proto +7 -0
- package/src/providers/devin/proto/errorspb/markers.proto +10 -0
- package/src/providers/devin/proto/errorspb/tags.proto +12 -0
- package/src/providers/devin/proto/errorspb/testing.proto +6 -0
- package/src/providers/devin/proto/exa/analytics_pb/analytics.proto +188 -0
- package/src/providers/devin/proto/exa/api_server_pb/api_server.proto +2461 -0
- package/src/providers/devin/proto/exa/auth_pb/auth.proto +19 -0
- package/src/providers/devin/proto/exa/auto_cascade_common_pb/auto_cascade_common.proto +79 -0
- package/src/providers/devin/proto/exa/browser_preview_pb/browser_preview.proto +32 -0
- package/src/providers/devin/proto/exa/bug_checker_pb/bug_checker.proto +22 -0
- package/src/providers/devin/proto/exa/cascade_plugins_pb/cascade_plugins.proto +262 -0
- package/src/providers/devin/proto/exa/chat_client_server_pb/chat_client_server.proto +57 -0
- package/src/providers/devin/proto/exa/chat_pb/chat.proto +449 -0
- package/src/providers/devin/proto/exa/code_edit/code_edit_pb/code_edit.proto +186 -0
- package/src/providers/devin/proto/exa/codeium_common_pb/codeium_common.proto +4157 -0
- package/src/providers/devin/proto/exa/context_module_pb/context_module.proto +175 -0
- package/src/providers/devin/proto/exa/cortex_pb/cortex.proto +3268 -0
- package/src/providers/devin/proto/exa/dev_pb/dev.proto +26 -0
- package/src/providers/devin/proto/exa/diff_action_pb/diff_action.proto +75 -0
- package/src/providers/devin/proto/exa/eval/pr_eval/datasets_pb/datasets.proto +103 -0
- package/src/providers/devin/proto/exa/eval_pb/eval.proto +1315 -0
- package/src/providers/devin/proto/exa/extension_server_pb/extension_server.proto +556 -0
- package/src/providers/devin/proto/exa/file_system_provider_pb/file_system_provider.proto +75 -0
- package/src/providers/devin/proto/exa/index_pb/index.proto +461 -0
- package/src/providers/devin/proto/exa/knowledge_base_pb/knowledge_base.proto +144 -0
- package/src/providers/devin/proto/exa/language_server_pb/language_server.proto +2385 -0
- package/src/providers/devin/proto/exa/model_management_pb/model_management.proto +186 -0
- package/src/providers/devin/proto/exa/opensearch_clients_pb/opensearch_clients.proto +503 -0
- package/src/providers/devin/proto/exa/product_analytics_pb/product_analytics.proto +37 -0
- package/src/providers/devin/proto/exa/prompt_pb/prompt.proto +92 -0
- package/src/providers/devin/proto/exa/reactive_component_pb/reactive_component.proto +96 -0
- package/src/providers/devin/proto/exa/seat_management_pb/seat_management.proto +2680 -0
- package/src/providers/devin/proto/exa/tokenizer_pb/tokenizer.proto +37 -0
- package/src/providers/devin/proto/exa/trainer_pb/config.proto +647 -0
- package/src/providers/devin/proto/exa/tree_sitter/language_data_pb/language_data.proto +14 -0
- package/src/providers/devin/proto/exa/trust_pb/trust.proto +157 -0
- package/src/providers/devin/proto/exa/user_analytics_pb/user_analytics.proto +519 -0
- package/src/providers/devin/proto/google.golang.org/appengine/internal/base/api_base.proto +28 -0
- package/src/providers/devin/proto/google.golang.org/appengine/internal/datastore/datastore_v3.proto +484 -0
- package/src/providers/devin/proto/google.golang.org/appengine/internal/log/log_service.proto +136 -0
- package/src/providers/devin/proto/google.golang.org/appengine/internal/remote_api/remote_api.proto +42 -0
- package/src/providers/devin/proto/google.golang.org/appengine/internal/urlfetch/urlfetch_service.proto +61 -0
- package/src/providers/devin/proto/grpc/binlog/v1/binarylog.proto +84 -0
- package/src/providers/devin/proto/io/prometheus/client/metrics.proto +98 -0
- package/src/providers/devin.ts +577 -0
- package/src/providers/error-message.ts +21 -0
- package/src/providers/github-copilot-headers.ts +141 -0
- package/src/providers/gitlab-duo-workflow-chatml-note.md +1 -0
- package/src/providers/gitlab-duo-workflow.ts +3058 -0
- package/src/providers/gitlab-duo.ts +395 -0
- package/src/providers/google-auth.ts +350 -0
- package/src/providers/google-gemini-cli.ts +1362 -0
- package/src/providers/google-interactions.ts +753 -0
- package/src/providers/google-shared.ts +1103 -0
- package/src/providers/google-types.ts +180 -0
- package/src/providers/google-vertex.ts +183 -0
- package/src/providers/google.ts +87 -0
- package/src/providers/grammar.ts +70 -0
- package/src/providers/kimi.ts +52 -0
- package/src/providers/mock.ts +507 -0
- package/src/providers/ollama.ts +773 -0
- package/src/providers/openai-anthropic-shim.ts +152 -0
- package/src/providers/openai-chat-server-schema.ts +242 -0
- package/src/providers/openai-chat-server.ts +715 -0
- package/src/providers/openai-chat-wire.ts +847 -0
- package/src/providers/openai-codex/request-transformer.ts +295 -0
- package/src/providers/openai-codex/response-handler.ts +102 -0
- package/src/providers/openai-codex-responses.ts +3468 -0
- package/src/providers/openai-completions.ts +2173 -0
- package/src/providers/openai-reasoning-fallback.ts +269 -0
- package/src/providers/openai-responses-reasoning-suppression.md +1 -0
- package/src/providers/openai-responses-server-schema.ts +282 -0
- package/src/providers/openai-responses-server.ts +1280 -0
- package/src/providers/openai-responses-wire.ts +6391 -0
- package/src/providers/openai-responses.ts +1022 -0
- package/src/providers/openai-shared.ts +2648 -0
- package/src/providers/pi-native-client.ts +266 -0
- package/src/providers/pi-native-server.ts +242 -0
- package/src/providers/register-builtins.ts +475 -0
- package/src/providers/synthetic.ts +50 -0
- package/src/providers/transform-messages.ts +787 -0
- package/src/providers/vision-guard.ts +54 -0
- package/src/registry/aimlapi.ts +6 -0
- package/src/registry/alibaba-coding-plan.ts +95 -0
- package/src/registry/amazon-bedrock.ts +22 -0
- package/src/registry/anthropic.ts +26 -0
- package/src/registry/api-key-login.ts +112 -0
- package/src/registry/api-key-validation.ts +161 -0
- package/src/registry/azure.ts +6 -0
- package/src/registry/cerebras.ts +23 -0
- package/src/registry/cloudflare-ai-gateway.ts +45 -0
- package/src/registry/coreweave.ts +40 -0
- package/src/registry/cursor.ts +20 -0
- package/src/registry/deepseek.ts +46 -0
- package/src/registry/derived.ts +9 -0
- package/src/registry/devin.ts +15 -0
- package/src/registry/firepass.ts +32 -0
- package/src/registry/fireworks.ts +28 -0
- package/src/registry/github-copilot.ts +22 -0
- package/src/registry/gitlab-duo-workflow.ts +20 -0
- package/src/registry/gitlab-duo.ts +19 -0
- package/src/registry/google-antigravity.ts +22 -0
- package/src/registry/google-gemini-cli.ts +22 -0
- package/src/registry/google-vertex.ts +38 -0
- package/src/registry/google.ts +6 -0
- package/src/registry/groq.ts +6 -0
- package/src/registry/huggingface.ts +29 -0
- package/src/registry/index.ts +4 -0
- package/src/registry/kagi.ts +46 -0
- package/src/registry/kilo.ts +114 -0
- package/src/registry/kimi-code.ts +17 -0
- package/src/registry/litellm.ts +45 -0
- package/src/registry/llama-cpp.ts +35 -0
- package/src/registry/lm-studio.ts +31 -0
- package/src/registry/minimax-code-cn.ts +12 -0
- package/src/registry/minimax-code.ts +12 -0
- package/src/registry/minimax.ts +6 -0
- package/src/registry/mistral.ts +6 -0
- package/src/registry/moonshot.ts +22 -0
- package/src/registry/nanogpt.ts +22 -0
- package/src/registry/nvidia.ts +61 -0
- package/src/registry/oauth/__tests__/xai-oauth.test.ts +104 -0
- package/src/registry/oauth/anthropic.ts +311 -0
- package/src/registry/oauth/callback-server.ts +315 -0
- package/src/registry/oauth/cursor.ts +171 -0
- package/src/registry/oauth/devin.ts +124 -0
- package/src/registry/oauth/github-copilot.ts +369 -0
- package/src/registry/oauth/gitlab-duo-workflow.ts +146 -0
- package/src/registry/oauth/gitlab-duo.ts +222 -0
- package/src/registry/oauth/google-antigravity.ts +209 -0
- package/src/registry/oauth/google-gemini-cli.ts +273 -0
- package/src/registry/oauth/google-oauth-shared.ts +125 -0
- package/src/registry/oauth/index.ts +269 -0
- package/src/registry/oauth/kimi.ts +289 -0
- package/src/registry/oauth/minimax-code.ts +53 -0
- package/src/registry/oauth/oauth.html +311 -0
- package/src/registry/oauth/openai-codex.ts +364 -0
- package/src/registry/oauth/opencode.ts +50 -0
- package/src/registry/oauth/perplexity.ts +228 -0
- package/src/registry/oauth/pkce.ts +18 -0
- package/src/registry/oauth/types.ts +65 -0
- package/src/registry/oauth/wafer.ts +24 -0
- package/src/registry/oauth/xai-oauth.ts +394 -0
- package/src/registry/oauth/xiaomi.ts +211 -0
- package/src/registry/ollama-cloud.ts +36 -0
- package/src/registry/ollama.ts +43 -0
- package/src/registry/openai-codex-device.ts +18 -0
- package/src/registry/openai-codex.ts +19 -0
- package/src/registry/openai.ts +6 -0
- package/src/registry/opencode-go.ts +12 -0
- package/src/registry/opencode-zen.ts +12 -0
- package/src/registry/openrouter.ts +28 -0
- package/src/registry/parallel.ts +45 -0
- package/src/registry/perplexity.ts +13 -0
- package/src/registry/qianfan.ts +27 -0
- package/src/registry/qwen-portal.ts +50 -0
- package/src/registry/registry.ts +161 -0
- package/src/registry/sakana.ts +22 -0
- package/src/registry/synthetic.ts +21 -0
- package/src/registry/tavily.ts +45 -0
- package/src/registry/together.ts +22 -0
- package/src/registry/types.ts +56 -0
- package/src/registry/umans.ts +23 -0
- package/src/registry/venice.ts +33 -0
- package/src/registry/vercel-ai-gateway.ts +38 -0
- package/src/registry/vllm.ts +34 -0
- package/src/registry/wafer-serverless.ts +12 -0
- package/src/registry/xai-oauth.ts +17 -0
- package/src/registry/xai.ts +6 -0
- package/src/registry/xiaomi-token-plan-ams.ts +12 -0
- package/src/registry/xiaomi-token-plan-cn.ts +12 -0
- package/src/registry/xiaomi-token-plan-sgp.ts +12 -0
- package/src/registry/xiaomi.ts +12 -0
- package/src/registry/zai.ts +27 -0
- package/src/registry/zenmux.ts +22 -0
- package/src/registry/zhipu-coding-plan.ts +27 -0
- package/src/stream.ts +1778 -0
- package/src/types.ts +856 -0
- package/src/usage/claude.ts +485 -0
- package/src/usage/gemini.ts +258 -0
- package/src/usage/github-copilot.ts +424 -0
- package/src/usage/google-antigravity.ts +497 -0
- package/src/usage/kimi.ts +271 -0
- package/src/usage/minimax-code.ts +30 -0
- package/src/usage/ollama.ts +41 -0
- package/src/usage/openai-codex-base-url.ts +35 -0
- package/src/usage/openai-codex-reset.ts +174 -0
- package/src/usage/openai-codex.ts +535 -0
- package/src/usage/opencode-go.ts +89 -0
- package/src/usage/shared.ts +10 -0
- package/src/usage/zai.ts +321 -0
- package/src/usage.ts +333 -0
- package/src/utils/abort.ts +67 -0
- package/src/utils/anthropic-auth.ts +93 -0
- package/src/utils/block-symbols.ts +32 -0
- package/src/utils/deterministic-id.ts +20 -0
- package/src/utils/empty-completion-retry.ts +159 -0
- package/src/utils/event-stream.ts +171 -0
- package/src/utils/foundry.ts +8 -0
- package/src/utils/google-validation.ts +25 -0
- package/src/utils/harmony-leak.ts +456 -0
- package/src/utils/http-inspector.ts +168 -0
- package/src/utils/idle-iterator.ts +473 -0
- package/src/utils/leaked-thinking-stream.ts +294 -0
- package/src/utils/openai-http.ts +122 -0
- package/src/utils/openrouter-headers.ts +12 -0
- package/src/utils/parse-bind.ts +56 -0
- package/src/utils/provider-response.ts +30 -0
- package/src/utils/proxy.ts +240 -0
- package/src/utils/request-debug.ts +351 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +59 -0
- package/src/utils/schema/CONSTRAINTS.md +166 -0
- package/src/utils/schema/adapt.ts +36 -0
- package/src/utils/schema/compatibility.ts +435 -0
- package/src/utils/schema/dereference.ts +98 -0
- package/src/utils/schema/draft.ts +341 -0
- package/src/utils/schema/equality.ts +97 -0
- package/src/utils/schema/fields.ts +207 -0
- package/src/utils/schema/index.ts +15 -0
- package/src/utils/schema/json-schema-validator.ts +595 -0
- package/src/utils/schema/meta-validator.ts +167 -0
- package/src/utils/schema/normalize.ts +1901 -0
- package/src/utils/schema/spill.ts +43 -0
- package/src/utils/schema/stamps.ts +109 -0
- package/src/utils/schema/strict-tool-validation.ts +117 -0
- package/src/utils/schema/types.ts +10 -0
- package/src/utils/schema/typescript.ts +198 -0
- package/src/utils/schema/wire.ts +789 -0
- package/src/utils/schema/zod-decontaminate.ts +331 -0
- package/src/utils/sdk-stream-timeout.ts +43 -0
- package/src/utils/sse-debug.ts +18 -0
- package/src/utils/stream-markup-healing.ts +247 -0
- package/src/utils/thinking-loop.ts +552 -0
- package/src/utils/tool-call-loop-guard.ts +107 -0
- package/src/utils/tool-choice.ts +99 -0
- package/src/utils/validation.ts +1507 -0
- package/src/utils.ts +171 -0
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
import { type } from "arktype";
|
|
2
|
+
import { logger } from "jeopi-utils";
|
|
3
|
+
import { captureRequestHeaders, resolvePromptCacheKey } from "../auth-gateway/http";
|
|
4
|
+
import * as AIError from "../error";
|
|
5
|
+
import type {
|
|
6
|
+
AssistantMessage,
|
|
7
|
+
AssistantMessageEventStream,
|
|
8
|
+
Message,
|
|
9
|
+
RedactedThinkingContent,
|
|
10
|
+
StopReason,
|
|
11
|
+
TextContent,
|
|
12
|
+
ThinkingContent,
|
|
13
|
+
Tool,
|
|
14
|
+
ToolCall,
|
|
15
|
+
ToolResultMessage,
|
|
16
|
+
UserMessage,
|
|
17
|
+
} from "../types";
|
|
18
|
+
import {
|
|
19
|
+
type AnthropicAssistantContentBlock,
|
|
20
|
+
type AnthropicMessage,
|
|
21
|
+
type AnthropicSystem,
|
|
22
|
+
type AnthropicTool,
|
|
23
|
+
type AnthropicToolChoice,
|
|
24
|
+
type AnthropicToolResultContent,
|
|
25
|
+
type AnthropicUserContentBlock,
|
|
26
|
+
anthropicMessagesRequestSchema,
|
|
27
|
+
} from "./anthropic-messages-server-schema";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Anthropic Messages API (https://docs.anthropic.com/en/api/messages) ↔ pi-ai
|
|
31
|
+
* gateway translation. Inbound: foreign HTTP body → omp Context. Outbound:
|
|
32
|
+
* omp AssistantMessage[Stream] → Anthropic-shaped JSON / SSE.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
import type { AuthGatewayStreamControl, AuthGatewayParsedRequest as ParsedRequest } from "../auth-gateway/types";
|
|
36
|
+
|
|
37
|
+
export type { ParsedRequest };
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Inbound parsing
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
type ImageContentPart = { type: "image"; data: string; mimeType: string };
|
|
44
|
+
|
|
45
|
+
// Dedup noise from unknown-block-type warnings. Module-scoped so the warn
|
|
46
|
+
// fires once per (category, type) pair across the lifetime of the process.
|
|
47
|
+
const WARNED_UNKNOWN_BLOCK_TYPES = new Set<string>();
|
|
48
|
+
function warnUnknownBlockType(category: "user" | "assistant", blockType: string): void {
|
|
49
|
+
const key = `${category}:${blockType}`;
|
|
50
|
+
if (WARNED_UNKNOWN_BLOCK_TYPES.has(key)) return;
|
|
51
|
+
WARNED_UNKNOWN_BLOCK_TYPES.add(key);
|
|
52
|
+
logger.warn("anthropic-messages: unknown content block flattened to text placeholder", {
|
|
53
|
+
category,
|
|
54
|
+
blockType,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// pi-ai's `ImageContent` only carries base64 + mimeType. When the inbound
|
|
59
|
+
// uses `url` or `file_id` sources we surface a text placeholder so the
|
|
60
|
+
// downstream provider still sees a sane history; warn once per source kind.
|
|
61
|
+
const WARNED_NON_BASE64_IMAGE_SOURCES = new Set<string>();
|
|
62
|
+
function warnNonBase64ImageSource(sourceType: string): void {
|
|
63
|
+
if (WARNED_NON_BASE64_IMAGE_SOURCES.has(sourceType)) return;
|
|
64
|
+
WARNED_NON_BASE64_IMAGE_SOURCES.add(sourceType);
|
|
65
|
+
logger.warn("anthropic-messages: image source surfaced as text placeholder (pi-ai ImageContent lacks URL channel)", {
|
|
66
|
+
sourceType,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Compact, log-safe stringification for unknown content blocks. Keeps the
|
|
71
|
+
// placeholder informative without dumping multi-KB structures into history.
|
|
72
|
+
function describeUnknownBlock(block: { type: string }): string {
|
|
73
|
+
try {
|
|
74
|
+
const json = JSON.stringify(block);
|
|
75
|
+
if (json !== undefined && json.length <= 200) return `[${block.type}: ${json}]`;
|
|
76
|
+
} catch {
|
|
77
|
+
// fall through
|
|
78
|
+
}
|
|
79
|
+
return `[${block.type}]`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function buildSystemPrompt(raw: AnthropicSystem): string[] | undefined {
|
|
83
|
+
if (raw === undefined) return undefined;
|
|
84
|
+
if (typeof raw === "string") return raw.length > 0 ? [raw] : undefined;
|
|
85
|
+
const parts = raw.map(block => block.text).filter(text => text.length > 0);
|
|
86
|
+
return parts.length > 0 ? [parts.join("\n\n")] : undefined;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function makeUserMessage(parts: (TextContent | ImageContentPart)[], timestamp: number): UserMessage {
|
|
90
|
+
return {
|
|
91
|
+
role: "user",
|
|
92
|
+
content: parts.length === 1 && parts[0].type === "text" ? parts[0].text : parts,
|
|
93
|
+
timestamp,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function toolResultPartsFromBlocks(
|
|
98
|
+
content: AnthropicToolResultContent[] | string | undefined,
|
|
99
|
+
): (TextContent | ImageContentPart)[] {
|
|
100
|
+
if (content === undefined) return [];
|
|
101
|
+
if (typeof content === "string") return [{ type: "text", text: content }];
|
|
102
|
+
const out: (TextContent | ImageContentPart)[] = [];
|
|
103
|
+
for (const block of content) {
|
|
104
|
+
if (block.type === "text") {
|
|
105
|
+
out.push({ type: "text", text: block.text });
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
// block.type === "image" — schema only accepts base64 sources.
|
|
109
|
+
if (block.source.type === "base64") {
|
|
110
|
+
out.push({ type: "image", data: block.source.data, mimeType: block.source.media_type });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function walkUserContent(
|
|
117
|
+
blocks: string | AnthropicUserContentBlock[],
|
|
118
|
+
timestamp: number,
|
|
119
|
+
): (UserMessage | ToolResultMessage)[] {
|
|
120
|
+
const messages: (UserMessage | ToolResultMessage)[] = [];
|
|
121
|
+
const userParts: (TextContent | ImageContentPart)[] = [];
|
|
122
|
+
const flush = () => {
|
|
123
|
+
if (userParts.length === 0) return;
|
|
124
|
+
messages.push(makeUserMessage(userParts.splice(0), timestamp));
|
|
125
|
+
};
|
|
126
|
+
if (typeof blocks === "string") {
|
|
127
|
+
if (blocks.length > 0) userParts.push({ type: "text", text: blocks });
|
|
128
|
+
flush();
|
|
129
|
+
return messages;
|
|
130
|
+
}
|
|
131
|
+
for (const block of blocks) {
|
|
132
|
+
if (block.type === "text") {
|
|
133
|
+
userParts.push({ type: "text", text: block.text });
|
|
134
|
+
} else if (block.type === "image") {
|
|
135
|
+
// SDK's typed source covers base64+url; our schema also accepts the
|
|
136
|
+
// forward-compat `file` variant. Narrow against a widened shape so
|
|
137
|
+
// every variant is handled at runtime regardless of SDK lag.
|
|
138
|
+
const source = block.source as {
|
|
139
|
+
type: string;
|
|
140
|
+
data?: string;
|
|
141
|
+
media_type?: string;
|
|
142
|
+
url?: string;
|
|
143
|
+
file_id?: string;
|
|
144
|
+
};
|
|
145
|
+
if (source.type === "base64" && source.data && source.media_type) {
|
|
146
|
+
userParts.push({ type: "image", data: source.data, mimeType: source.media_type });
|
|
147
|
+
} else {
|
|
148
|
+
warnNonBase64ImageSource(source.type);
|
|
149
|
+
const ref =
|
|
150
|
+
source.type === "url" ? (source.url ?? "") : source.type === "file" ? (source.file_id ?? "") : "";
|
|
151
|
+
userParts.push({ type: "text", text: `[image: ${ref}]` });
|
|
152
|
+
}
|
|
153
|
+
} else if (block.type === "tool_result") {
|
|
154
|
+
// Anthropic permits tool_result blocks to follow plain text/image
|
|
155
|
+
// siblings in the same user message. pi-ai's history is a flat
|
|
156
|
+
// sequence of typed messages, so flush the accumulated parts as a
|
|
157
|
+
// separate UserMessage before emitting the ToolResultMessage.
|
|
158
|
+
flush();
|
|
159
|
+
messages.push({
|
|
160
|
+
role: "toolResult",
|
|
161
|
+
toolCallId: block.tool_use_id,
|
|
162
|
+
// Anthropic tool_results don't carry the tool name; downstream can rehydrate.
|
|
163
|
+
toolName: "",
|
|
164
|
+
content: toolResultPartsFromBlocks(block.content as AnthropicToolResultContent[] | string | undefined),
|
|
165
|
+
isError: block.is_error === true,
|
|
166
|
+
timestamp,
|
|
167
|
+
});
|
|
168
|
+
} else {
|
|
169
|
+
// Unknown variant (server_tool_use, mcp_*, document, web_search_tool_result,
|
|
170
|
+
// container_upload, code_execution_*, …). Flatten to a text placeholder
|
|
171
|
+
// so the downstream provider still gets a coherent transcript.
|
|
172
|
+
const unknown = block as { type: string };
|
|
173
|
+
warnUnknownBlockType("user", unknown.type);
|
|
174
|
+
userParts.push({ type: "text", text: describeUnknownBlock(unknown) });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
flush();
|
|
178
|
+
return messages;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function walkAssistantContent(
|
|
182
|
+
blocks: string | AnthropicAssistantContentBlock[],
|
|
183
|
+
): (TextContent | ThinkingContent | RedactedThinkingContent | ToolCall)[] {
|
|
184
|
+
const out: (TextContent | ThinkingContent | RedactedThinkingContent | ToolCall)[] = [];
|
|
185
|
+
if (typeof blocks === "string") {
|
|
186
|
+
if (blocks.length > 0) out.push({ type: "text", text: blocks });
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
for (const block of blocks) {
|
|
190
|
+
switch (block.type) {
|
|
191
|
+
case "text":
|
|
192
|
+
out.push({ type: "text", text: block.text });
|
|
193
|
+
break;
|
|
194
|
+
case "thinking": {
|
|
195
|
+
const tc: ThinkingContent = { type: "thinking", thinking: block.thinking };
|
|
196
|
+
if (block.signature !== undefined) tc.thinkingSignature = block.signature;
|
|
197
|
+
out.push(tc);
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
case "redacted_thinking":
|
|
201
|
+
out.push({ type: "redactedThinking", data: block.data });
|
|
202
|
+
break;
|
|
203
|
+
case "tool_use":
|
|
204
|
+
out.push({
|
|
205
|
+
type: "toolCall",
|
|
206
|
+
id: block.id,
|
|
207
|
+
name: block.name,
|
|
208
|
+
arguments:
|
|
209
|
+
block.input && typeof block.input === "object" && !Array.isArray(block.input)
|
|
210
|
+
? (block.input as Record<string, unknown>)
|
|
211
|
+
: {},
|
|
212
|
+
});
|
|
213
|
+
break;
|
|
214
|
+
default: {
|
|
215
|
+
// Unknown assistant variant (server_tool_use, mcp_tool_use, …).
|
|
216
|
+
// Flatten to a text placeholder; warn once per unknown type.
|
|
217
|
+
const unknown = block as { type: string };
|
|
218
|
+
warnUnknownBlockType("assistant", unknown.type);
|
|
219
|
+
out.push({ type: "text", text: describeUnknownBlock(unknown) });
|
|
220
|
+
break;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return out;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function walkTools(tools: AnthropicTool[] | undefined): Tool[] | undefined {
|
|
228
|
+
if (!tools) return undefined;
|
|
229
|
+
return tools.map(tool => ({
|
|
230
|
+
name: tool.name,
|
|
231
|
+
description: tool.description ?? "",
|
|
232
|
+
parameters: tool.input_schema as Record<string, unknown>,
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function mapToolChoice(choice: AnthropicToolChoice | undefined): ParsedRequest["options"]["toolChoice"] {
|
|
237
|
+
if (!choice) return undefined;
|
|
238
|
+
switch (choice.type) {
|
|
239
|
+
case "auto":
|
|
240
|
+
return "auto";
|
|
241
|
+
case "any":
|
|
242
|
+
return "required";
|
|
243
|
+
case "none":
|
|
244
|
+
return "none";
|
|
245
|
+
case "tool":
|
|
246
|
+
return { name: choice.name };
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
type AnthropicCacheControl = { type: "ephemeral"; ttl?: "1h" | "5m" };
|
|
251
|
+
type HasCacheControl = { cache_control?: AnthropicCacheControl };
|
|
252
|
+
|
|
253
|
+
function readCacheControl(value: unknown): AnthropicCacheControl | undefined {
|
|
254
|
+
if (value === null || typeof value !== "object") return undefined;
|
|
255
|
+
const cc = (value as HasCacheControl).cache_control;
|
|
256
|
+
if (!cc || typeof cc !== "object" || cc.type !== "ephemeral") return undefined;
|
|
257
|
+
return cc;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Anthropic clients annotate caching breakpoints per block via
|
|
262
|
+
* `cache_control: { type: "ephemeral", ttl?: "1h"|"5m" }`. pi-ai's
|
|
263
|
+
* `cacheRetention` is per-request, not per-block, and its anthropic provider
|
|
264
|
+
* re-applies breakpoints itself on the rebuilt outbound wire. Scan every
|
|
265
|
+
* block once and return the strongest retention requested: any `ttl: "1h"`
|
|
266
|
+
* promotes the request to "long", anything else ephemeral maps to "short".
|
|
267
|
+
*/
|
|
268
|
+
function deriveCacheRetention(data: {
|
|
269
|
+
system?: unknown;
|
|
270
|
+
messages: readonly unknown[];
|
|
271
|
+
tools?: readonly unknown[];
|
|
272
|
+
}): "short" | "long" | undefined {
|
|
273
|
+
let strongest: "short" | "long" | undefined;
|
|
274
|
+
const visit = (cc: AnthropicCacheControl | undefined): void => {
|
|
275
|
+
if (!cc) return;
|
|
276
|
+
if (cc.ttl === "1h") strongest = "long";
|
|
277
|
+
else strongest ??= "short";
|
|
278
|
+
};
|
|
279
|
+
if (Array.isArray(data.system)) {
|
|
280
|
+
for (const block of data.system) visit(readCacheControl(block));
|
|
281
|
+
}
|
|
282
|
+
for (const message of data.messages) {
|
|
283
|
+
if (message === null || typeof message !== "object") continue;
|
|
284
|
+
const content = (message as { content?: unknown }).content;
|
|
285
|
+
if (!Array.isArray(content)) continue;
|
|
286
|
+
for (const block of content) visit(readCacheControl(block));
|
|
287
|
+
}
|
|
288
|
+
if (data.tools) {
|
|
289
|
+
for (const tool of data.tools) visit(readCacheControl(tool));
|
|
290
|
+
}
|
|
291
|
+
return strongest;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function parseRequest(body: unknown, headers?: Headers): ParsedRequest {
|
|
295
|
+
const data = anthropicMessagesRequestSchema(body);
|
|
296
|
+
if (data instanceof type.errors) {
|
|
297
|
+
throw new AIError.ValidationError(`anthropic-messages: ${data.summary}`);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const now = Date.now();
|
|
301
|
+
const messages: Message[] = [];
|
|
302
|
+
for (const message of data.messages as AnthropicMessage[]) {
|
|
303
|
+
if (message.role === "user") {
|
|
304
|
+
for (const m of walkUserContent(message.content, now)) messages.push(m);
|
|
305
|
+
} else {
|
|
306
|
+
const assistant: AssistantMessage = {
|
|
307
|
+
role: "assistant",
|
|
308
|
+
content: walkAssistantContent(message.content),
|
|
309
|
+
api: "anthropic-messages",
|
|
310
|
+
provider: "anthropic",
|
|
311
|
+
model: data.model,
|
|
312
|
+
usage: emptyUsage(),
|
|
313
|
+
stopReason: "stop",
|
|
314
|
+
timestamp: now,
|
|
315
|
+
};
|
|
316
|
+
messages.push(assistant);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const options: ParsedRequest["options"] = {
|
|
321
|
+
maxOutputTokens: data.max_tokens,
|
|
322
|
+
};
|
|
323
|
+
if (data.temperature !== undefined) options.temperature = data.temperature;
|
|
324
|
+
if (data.top_p !== undefined) options.topP = data.top_p;
|
|
325
|
+
if (data.top_k !== undefined) options.topK = data.top_k;
|
|
326
|
+
if (data.stop_sequences) options.stopSequences = data.stop_sequences;
|
|
327
|
+
const toolChoice = mapToolChoice(data.tool_choice as AnthropicToolChoice | undefined);
|
|
328
|
+
if (toolChoice !== undefined) options.toolChoice = toolChoice;
|
|
329
|
+
// `disable_parallel_tool_use === true` means the client wants the model to
|
|
330
|
+
// emit at most one tool call per turn; map to pi-ai's negated boolean.
|
|
331
|
+
// Leave undefined when the field is absent or explicitly `false` so we
|
|
332
|
+
// don't override provider defaults.
|
|
333
|
+
if (data.tool_choice?.disable_parallel_tool_use === true) {
|
|
334
|
+
options.parallelToolCalls = false;
|
|
335
|
+
}
|
|
336
|
+
if (data.thinking) {
|
|
337
|
+
switch (data.thinking.type) {
|
|
338
|
+
case "enabled":
|
|
339
|
+
options.explicitThinkingBudgetTokens = data.thinking.budget_tokens;
|
|
340
|
+
break;
|
|
341
|
+
case "disabled":
|
|
342
|
+
options.disableReasoning = true;
|
|
343
|
+
break;
|
|
344
|
+
case "adaptive":
|
|
345
|
+
if (data.thinking.budget_tokens !== undefined) {
|
|
346
|
+
options.explicitThinkingBudgetTokens = data.thinking.budget_tokens;
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (data.output_config?.task_budget) {
|
|
352
|
+
options.taskBudget = data.output_config.task_budget;
|
|
353
|
+
}
|
|
354
|
+
const cacheRetention = deriveCacheRetention(data);
|
|
355
|
+
if (cacheRetention !== undefined) options.cacheRetention = cacheRetention;
|
|
356
|
+
// Anthropic clients commonly send `metadata: { user_id }`; forward verbatim
|
|
357
|
+
// so downstream providers (and our anthropic-passthrough fast-path) can
|
|
358
|
+
// preserve abuse-tracking signal.
|
|
359
|
+
if (data.metadata !== undefined) {
|
|
360
|
+
options.metadata = data.metadata as Record<string, unknown>;
|
|
361
|
+
}
|
|
362
|
+
const cacheKey = resolvePromptCacheKey(body, headers);
|
|
363
|
+
if (cacheKey !== undefined) options.promptCacheKey = cacheKey;
|
|
364
|
+
// Allow-listed header capture. The gateway's `handleFormatEndpoint`
|
|
365
|
+
// already merges its own pre-capture under whatever the parser sets, but
|
|
366
|
+
// we populate here too so direct callers of `parseRequest` (tests, custom
|
|
367
|
+
// wrappers) see the same surface. `anthropic-version` is the most
|
|
368
|
+
// load-bearing — some downstream Anthropic-API targets reject requests
|
|
369
|
+
// missing it.
|
|
370
|
+
if (headers) {
|
|
371
|
+
const captured = captureRequestHeaders(headers);
|
|
372
|
+
if (Object.keys(captured).length > 0) options.headers = captured;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return {
|
|
376
|
+
modelId: data.model,
|
|
377
|
+
context: {
|
|
378
|
+
systemPrompt: buildSystemPrompt(data.system as AnthropicSystem),
|
|
379
|
+
messages,
|
|
380
|
+
tools: walkTools(data.tools as AnthropicTool[] | undefined),
|
|
381
|
+
},
|
|
382
|
+
stream: data.stream === true,
|
|
383
|
+
options,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function emptyUsage(): AssistantMessage["usage"] {
|
|
388
|
+
return {
|
|
389
|
+
input: 0,
|
|
390
|
+
output: 0,
|
|
391
|
+
cacheRead: 0,
|
|
392
|
+
cacheWrite: 0,
|
|
393
|
+
totalTokens: 0,
|
|
394
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// ---------------------------------------------------------------------------
|
|
399
|
+
// Outbound encoding
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
|
|
402
|
+
function newMessageId(): string {
|
|
403
|
+
const hex = (globalThis.crypto?.randomUUID?.() ?? randomFallback()).replace(/-/g, "").slice(0, 24);
|
|
404
|
+
return `msg_${hex}`;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function randomFallback(): string {
|
|
408
|
+
// Sufficient for tests / environments without crypto.randomUUID
|
|
409
|
+
const buf = new Uint8Array(16);
|
|
410
|
+
for (let i = 0; i < 16; i++) buf[i] = Math.floor(Math.random() * 256);
|
|
411
|
+
const hex = Array.from(buf, b => b.toString(16).padStart(2, "0")).join("");
|
|
412
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function mapStopReasonOut(reason: StopReason): "end_turn" | "max_tokens" | "tool_use" {
|
|
416
|
+
switch (reason) {
|
|
417
|
+
case "length":
|
|
418
|
+
return "max_tokens";
|
|
419
|
+
case "toolUse":
|
|
420
|
+
return "tool_use";
|
|
421
|
+
default:
|
|
422
|
+
return "end_turn";
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function encodeContentBlocks(message: AssistantMessage): Record<string, unknown>[] {
|
|
427
|
+
const blocks: Record<string, unknown>[] = [];
|
|
428
|
+
for (const c of message.content) {
|
|
429
|
+
switch (c.type) {
|
|
430
|
+
case "text":
|
|
431
|
+
blocks.push({ type: "text", text: c.text });
|
|
432
|
+
break;
|
|
433
|
+
case "thinking": {
|
|
434
|
+
const b: Record<string, unknown> = { type: "thinking", thinking: c.thinking };
|
|
435
|
+
if (c.thinkingSignature) b.signature = c.thinkingSignature;
|
|
436
|
+
blocks.push(b);
|
|
437
|
+
break;
|
|
438
|
+
}
|
|
439
|
+
case "redactedThinking":
|
|
440
|
+
blocks.push({ type: "redacted_thinking", data: c.data });
|
|
441
|
+
break;
|
|
442
|
+
case "toolCall":
|
|
443
|
+
blocks.push({ type: "tool_use", id: c.id, name: c.name, input: c.arguments ?? {} });
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return blocks;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function encodeUsage(message: AssistantMessage): Record<string, unknown> {
|
|
451
|
+
return {
|
|
452
|
+
input_tokens: message.usage.input,
|
|
453
|
+
output_tokens: message.usage.output,
|
|
454
|
+
cache_read_input_tokens: message.usage.cacheRead,
|
|
455
|
+
cache_creation_input_tokens: message.usage.cacheWrite,
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export function encodeResponse(message: AssistantMessage, requestedModelId: string): Record<string, unknown> {
|
|
460
|
+
if (message.stopReason === "error" || message.stopReason === "aborted") {
|
|
461
|
+
throw new AIError.ProviderResponseError(
|
|
462
|
+
message.errorMessage ?? `anthropic-messages: upstream ${message.stopReason}`,
|
|
463
|
+
{
|
|
464
|
+
provider: "anthropic",
|
|
465
|
+
kind: "output",
|
|
466
|
+
},
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
return {
|
|
470
|
+
id: message.responseId ?? newMessageId(),
|
|
471
|
+
type: "message",
|
|
472
|
+
role: "assistant",
|
|
473
|
+
model: requestedModelId,
|
|
474
|
+
content: encodeContentBlocks(message),
|
|
475
|
+
stop_reason: mapStopReasonOut(message.stopReason),
|
|
476
|
+
// TODO: surface the matched stop sequence once pi-ai's
|
|
477
|
+
// `AssistantMessage.stopReason` carries the matched string. Intentionally
|
|
478
|
+
// `null` for now (Anthropic schema allows it).
|
|
479
|
+
stop_sequence: null,
|
|
480
|
+
usage: encodeUsage(message),
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// ---------------------------------------------------------------------------
|
|
485
|
+
// Streaming encoder
|
|
486
|
+
// ---------------------------------------------------------------------------
|
|
487
|
+
|
|
488
|
+
const ENCODER = new TextEncoder();
|
|
489
|
+
|
|
490
|
+
function sseFrame(event: string, data: Record<string, unknown>): Uint8Array {
|
|
491
|
+
return ENCODER.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
type BlockKind = "text" | "thinking" | "tool_use";
|
|
495
|
+
|
|
496
|
+
interface OpenBlock {
|
|
497
|
+
index: number;
|
|
498
|
+
kind: BlockKind;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
// Keepalive cadence for the SSE encoder. Anthropic's API pings periodically;
|
|
502
|
+
// without frames between message_start and the first content block (slow first
|
|
503
|
+
// token) SDK first-event/idle watchdogs classify the stream as stalled.
|
|
504
|
+
const STREAM_PING_INTERVAL_MS = 15_000;
|
|
505
|
+
|
|
506
|
+
const ZERO_WIRE_USAGE: Record<string, unknown> = {
|
|
507
|
+
input_tokens: 0,
|
|
508
|
+
output_tokens: 0,
|
|
509
|
+
cache_read_input_tokens: 0,
|
|
510
|
+
cache_creation_input_tokens: 0,
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
export function encodeStream(
|
|
514
|
+
events: AssistantMessageEventStream,
|
|
515
|
+
requestedModelId: string,
|
|
516
|
+
_options?: ParsedRequest["options"],
|
|
517
|
+
control?: AuthGatewayStreamControl,
|
|
518
|
+
): ReadableStream<Uint8Array> {
|
|
519
|
+
let pingTimer: NodeJS.Timeout | undefined;
|
|
520
|
+
let cancelled = control?.signal?.aborted === true;
|
|
521
|
+
const markCancelled = () => {
|
|
522
|
+
cancelled = true;
|
|
523
|
+
};
|
|
524
|
+
control?.signal?.addEventListener("abort", markCancelled, { once: true });
|
|
525
|
+
const stopPings = () => {
|
|
526
|
+
if (pingTimer !== undefined) {
|
|
527
|
+
clearInterval(pingTimer);
|
|
528
|
+
pingTimer = undefined;
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
return new ReadableStream<Uint8Array>({
|
|
532
|
+
async start(controller) {
|
|
533
|
+
const messageId = newMessageId();
|
|
534
|
+
let started = false;
|
|
535
|
+
let lastPartial: AssistantMessage | undefined;
|
|
536
|
+
const open = new Map<number, OpenBlock>();
|
|
537
|
+
|
|
538
|
+
const ensureStart = (partial: AssistantMessage | undefined) => {
|
|
539
|
+
if (started) return;
|
|
540
|
+
started = true;
|
|
541
|
+
controller.enqueue(
|
|
542
|
+
sseFrame("message_start", {
|
|
543
|
+
type: "message_start",
|
|
544
|
+
message: {
|
|
545
|
+
id: messageId,
|
|
546
|
+
type: "message",
|
|
547
|
+
role: "assistant",
|
|
548
|
+
model: requestedModelId,
|
|
549
|
+
content: [],
|
|
550
|
+
stop_reason: null,
|
|
551
|
+
// TODO: same as encodeResponse — surface matched stop sequence
|
|
552
|
+
// once pi-ai propagates it.
|
|
553
|
+
stop_sequence: null,
|
|
554
|
+
usage: partial ? encodeUsage(partial) : ZERO_WIRE_USAGE,
|
|
555
|
+
},
|
|
556
|
+
}),
|
|
557
|
+
);
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
const closeBlock = (index: number) => {
|
|
561
|
+
if (!open.has(index)) return;
|
|
562
|
+
controller.enqueue(sseFrame("content_block_stop", { type: "content_block_stop", index }));
|
|
563
|
+
open.delete(index);
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
pingTimer = setInterval(() => {
|
|
567
|
+
try {
|
|
568
|
+
if (cancelled) {
|
|
569
|
+
stopPings();
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
controller.enqueue(sseFrame("ping", { type: "ping" }));
|
|
573
|
+
} catch {
|
|
574
|
+
// Controller already closed/errored (client gone); stop the timer.
|
|
575
|
+
stopPings();
|
|
576
|
+
}
|
|
577
|
+
}, STREAM_PING_INTERVAL_MS);
|
|
578
|
+
|
|
579
|
+
try {
|
|
580
|
+
if (cancelled) {
|
|
581
|
+
controller.close();
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
for await (const ev of events) {
|
|
585
|
+
if (cancelled) return;
|
|
586
|
+
switch (ev.type) {
|
|
587
|
+
case "start":
|
|
588
|
+
ensureStart(ev.partial);
|
|
589
|
+
break;
|
|
590
|
+
case "text_start": {
|
|
591
|
+
ensureStart(ev.partial);
|
|
592
|
+
open.set(ev.contentIndex, { index: ev.contentIndex, kind: "text" });
|
|
593
|
+
controller.enqueue(
|
|
594
|
+
sseFrame("content_block_start", {
|
|
595
|
+
type: "content_block_start",
|
|
596
|
+
index: ev.contentIndex,
|
|
597
|
+
content_block: { type: "text", text: "" },
|
|
598
|
+
}),
|
|
599
|
+
);
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
case "text_delta":
|
|
603
|
+
controller.enqueue(
|
|
604
|
+
sseFrame("content_block_delta", {
|
|
605
|
+
type: "content_block_delta",
|
|
606
|
+
index: ev.contentIndex,
|
|
607
|
+
delta: { type: "text_delta", text: ev.delta },
|
|
608
|
+
}),
|
|
609
|
+
);
|
|
610
|
+
break;
|
|
611
|
+
case "text_end":
|
|
612
|
+
closeBlock(ev.contentIndex);
|
|
613
|
+
break;
|
|
614
|
+
case "thinking_start": {
|
|
615
|
+
ensureStart(ev.partial);
|
|
616
|
+
open.set(ev.contentIndex, { index: ev.contentIndex, kind: "thinking" });
|
|
617
|
+
controller.enqueue(
|
|
618
|
+
sseFrame("content_block_start", {
|
|
619
|
+
type: "content_block_start",
|
|
620
|
+
index: ev.contentIndex,
|
|
621
|
+
content_block: { type: "thinking", thinking: "" },
|
|
622
|
+
}),
|
|
623
|
+
);
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
case "thinking_delta":
|
|
627
|
+
controller.enqueue(
|
|
628
|
+
sseFrame("content_block_delta", {
|
|
629
|
+
type: "content_block_delta",
|
|
630
|
+
index: ev.contentIndex,
|
|
631
|
+
delta: { type: "thinking_delta", thinking: ev.delta },
|
|
632
|
+
}),
|
|
633
|
+
);
|
|
634
|
+
break;
|
|
635
|
+
case "thinking_end": {
|
|
636
|
+
const c = ev.partial.content[ev.contentIndex];
|
|
637
|
+
if (c?.type === "thinking" && c.thinkingSignature) {
|
|
638
|
+
controller.enqueue(
|
|
639
|
+
sseFrame("content_block_delta", {
|
|
640
|
+
type: "content_block_delta",
|
|
641
|
+
index: ev.contentIndex,
|
|
642
|
+
delta: { type: "signature_delta", signature: c.thinkingSignature },
|
|
643
|
+
}),
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
closeBlock(ev.contentIndex);
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
case "toolcall_start": {
|
|
650
|
+
ensureStart(ev.partial);
|
|
651
|
+
const tc = ev.partial.content[ev.contentIndex] as ToolCall | undefined;
|
|
652
|
+
open.set(ev.contentIndex, { index: ev.contentIndex, kind: "tool_use" });
|
|
653
|
+
controller.enqueue(
|
|
654
|
+
sseFrame("content_block_start", {
|
|
655
|
+
type: "content_block_start",
|
|
656
|
+
index: ev.contentIndex,
|
|
657
|
+
content_block: {
|
|
658
|
+
type: "tool_use",
|
|
659
|
+
id: tc?.id ?? "",
|
|
660
|
+
name: tc?.name ?? "",
|
|
661
|
+
input: {},
|
|
662
|
+
},
|
|
663
|
+
}),
|
|
664
|
+
);
|
|
665
|
+
break;
|
|
666
|
+
}
|
|
667
|
+
case "toolcall_delta":
|
|
668
|
+
controller.enqueue(
|
|
669
|
+
sseFrame("content_block_delta", {
|
|
670
|
+
type: "content_block_delta",
|
|
671
|
+
index: ev.contentIndex,
|
|
672
|
+
delta: { type: "input_json_delta", partial_json: ev.delta },
|
|
673
|
+
}),
|
|
674
|
+
);
|
|
675
|
+
break;
|
|
676
|
+
case "toolcall_end":
|
|
677
|
+
closeBlock(ev.contentIndex);
|
|
678
|
+
break;
|
|
679
|
+
case "done": {
|
|
680
|
+
for (const idx of [...open.keys()]) closeBlock(idx);
|
|
681
|
+
controller.enqueue(
|
|
682
|
+
sseFrame("message_delta", {
|
|
683
|
+
type: "message_delta",
|
|
684
|
+
// TODO: surface matched stop sequence once pi-ai
|
|
685
|
+
// propagates it on the `done` event.
|
|
686
|
+
delta: { stop_reason: mapStopReasonOut(ev.reason), stop_sequence: null },
|
|
687
|
+
usage: encodeUsage(ev.message),
|
|
688
|
+
}),
|
|
689
|
+
);
|
|
690
|
+
controller.enqueue(sseFrame("message_stop", { type: "message_stop" }));
|
|
691
|
+
controller.close();
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
case "error": {
|
|
695
|
+
const msg = ev.error.errorMessage ?? "stream error";
|
|
696
|
+
controller.enqueue(
|
|
697
|
+
sseFrame("error", { type: "error", error: { type: "api_error", message: msg } }),
|
|
698
|
+
);
|
|
699
|
+
controller.close();
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
// Stream ended without an explicit done: emit a complete envelope
|
|
705
|
+
// (message_start + message_delta carrying a stop_reason) so strict
|
|
706
|
+
// clients don't reject the response as a protocol error.
|
|
707
|
+
ensureStart(lastPartial);
|
|
708
|
+
for (const idx of [...open.keys()]) closeBlock(idx);
|
|
709
|
+
controller.enqueue(
|
|
710
|
+
sseFrame("message_delta", {
|
|
711
|
+
type: "message_delta",
|
|
712
|
+
delta: { stop_reason: "end_turn", stop_sequence: null },
|
|
713
|
+
usage: lastPartial ? encodeUsage(lastPartial) : ZERO_WIRE_USAGE,
|
|
714
|
+
}),
|
|
715
|
+
);
|
|
716
|
+
controller.enqueue(sseFrame("message_stop", { type: "message_stop" }));
|
|
717
|
+
controller.close();
|
|
718
|
+
} catch (err) {
|
|
719
|
+
if (!cancelled) {
|
|
720
|
+
controller.enqueue(
|
|
721
|
+
sseFrame("error", {
|
|
722
|
+
type: "error",
|
|
723
|
+
error: { type: "api_error", message: err instanceof Error ? err.message : String(err) },
|
|
724
|
+
}),
|
|
725
|
+
);
|
|
726
|
+
controller.close();
|
|
727
|
+
}
|
|
728
|
+
} finally {
|
|
729
|
+
control?.signal?.removeEventListener("abort", markCancelled);
|
|
730
|
+
stopPings();
|
|
731
|
+
}
|
|
732
|
+
},
|
|
733
|
+
cancel(reason) {
|
|
734
|
+
cancelled = true;
|
|
735
|
+
control?.signal?.removeEventListener("abort", markCancelled);
|
|
736
|
+
control?.onCancel?.(reason);
|
|
737
|
+
stopPings();
|
|
738
|
+
},
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// ---------------------------------------------------------------------------
|
|
743
|
+
// Error envelope
|
|
744
|
+
// ---------------------------------------------------------------------------
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Anthropic error envelope: `{ type: "error", error: { type, message } }`.
|
|
748
|
+
* See https://docs.anthropic.com/en/api/errors. Returned as a `Response` so
|
|
749
|
+
* the gateway can hand it straight back to the client without extra wrapping.
|
|
750
|
+
*/
|
|
751
|
+
export function formatError(status: number, type: string, message: string): Response {
|
|
752
|
+
return new Response(JSON.stringify({ type: "error", error: { type, message } }), {
|
|
753
|
+
status,
|
|
754
|
+
headers: { "Content-Type": "application/json" },
|
|
755
|
+
});
|
|
756
|
+
}
|