@linxiraos/pi-ai 1.0.0
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 +5066 -0
- package/README.md +1195 -0
- package/dist/types/api-registry.d.ts +30 -0
- package/dist/types/auth/sqlite-credential-store.d.ts +117 -0
- package/dist/types/auth-broker/client.d.ts +93 -0
- package/dist/types/auth-broker/discover.d.ts +39 -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 +136 -0
- package/dist/types/auth-broker/server.d.ts +48 -0
- package/dist/types/auth-broker/snapshot-cache.d.ts +17 -0
- package/dist/types/auth-broker/types.d.ts +152 -0
- package/dist/types/auth-broker/wire-schema-resource.d.ts +53 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +5 -0
- package/dist/types/auth-gateway/http.d.ts +56 -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 +127 -0
- package/dist/types/auth-retry.d.ts +150 -0
- package/dist/types/auth-storage.d.ts +1258 -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 +30 -0
- package/dist/types/dialect/examples.d.ts +18 -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 +9 -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 +54 -0
- package/dist/types/dialect/thinking.d.ts +6 -0
- package/dist/types/dialect/types.d.ts +68 -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 +20 -0
- package/dist/types/error/auth.d.ts +27 -0
- package/dist/types/error/aws.d.ts +27 -0
- package/dist/types/error/classes.d.ts +106 -0
- package/dist/types/error/finalize.d.ts +39 -0
- package/dist/types/error/flags.d.ts +94 -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 +14 -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 +80 -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 +51 -0
- package/dist/types/provider-details.d.ts +24 -0
- package/dist/types/providers/amazon-bedrock.d.ts +39 -0
- package/dist/types/providers/anthropic-client.d.ts +106 -0
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +937 -0
- package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
- package/dist/types/providers/anthropic-wire.d.ts +345 -0
- package/dist/types/providers/anthropic.d.ts +261 -0
- package/dist/types/providers/aws-credentials.d.ts +48 -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/bedrock-mantle.d.ts +13 -0
- package/dist/types/providers/claude-code-fingerprint.d.ts +19 -0
- package/dist/types/providers/cowork-fetch.d.ts +3 -0
- package/dist/types/providers/cursor/exec-modern.d.ts +98 -0
- package/dist/types/providers/cursor-pi-args.d.ts +105 -0
- package/dist/types/providers/cursor.d.ts +221 -0
- package/dist/types/providers/devin.d.ts +12 -0
- package/dist/types/providers/error-message.d.ts +25 -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 +24 -0
- package/dist/types/providers/google-gemini-cli.d.ts +120 -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 +179 -0
- package/dist/types/providers/ollama.d.ts +8 -0
- package/dist/types/providers/openai-anthropic-shim.d.ts +35 -0
- package/dist/types/providers/openai-chat-server-schema.d.ts +1311 -0
- package/dist/types/providers/openai-chat-server.d.ts +16 -0
- package/dist/types/providers/openai-chat-wire.d.ts +669 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +109 -0
- package/dist/types/providers/openai-codex/response-handler.d.ts +26 -0
- package/dist/types/providers/openai-codex-responses.d.ts +238 -0
- package/dist/types/providers/openai-completions.d.ts +48 -0
- package/dist/types/providers/openai-reasoning-fallback.d.ts +25 -0
- package/dist/types/providers/openai-responses-server-schema.d.ts +1314 -0
- package/dist/types/providers/openai-responses-server.d.ts +17 -0
- package/dist/types/providers/openai-responses-wire.d.ts +6099 -0
- package/dist/types/providers/openai-responses.d.ts +131 -0
- package/dist/types/providers/openai-shared.d.ts +624 -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 +37 -0
- package/dist/types/providers/synthetic.d.ts +26 -0
- package/dist/types/providers/transform-messages.d.ts +32 -0
- package/dist/types/providers/vision-guard.d.ts +20 -0
- package/dist/types/registry/aiand.d.ts +7 -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/alibaba-token-plan.d.ts +18 -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/aws.d.ts +13 -0
- package/dist/types/registry/azure.d.ts +4 -0
- package/dist/types/registry/baseten.d.ts +7 -0
- package/dist/types/registry/bedrock-mantle.d.ts +22 -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/exa.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/gmi-cloud.d.ts +7 -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/meta.d.ts +7 -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/novita.d.ts +6 -0
- package/dist/types/registry/nvidia.d.ts +7 -0
- package/dist/types/registry/oauth/anthropic-constants.d.ts +12 -0
- package/dist/types/registry/oauth/anthropic.d.ts +24 -0
- package/dist/types/registry/oauth/callback-server.d.ts +74 -0
- package/dist/types/registry/oauth/cursor.d.ts +16 -0
- package/dist/types/registry/oauth/device-code.d.ts +25 -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 +22 -0
- package/dist/types/registry/oauth/google-oauth-shared.d.ts +56 -0
- package/dist/types/registry/oauth/index.d.ts +45 -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 +87 -0
- package/dist/types/registry/oauth/wafer.d.ts +1 -0
- package/dist/types/registry/oauth/xai-oauth.d.ts +46 -0
- package/dist/types/registry/oauth/xiaomi.d.ts +25 -0
- package/dist/types/registry/oauth/zai.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 +367 -0
- package/dist/types/registry/sakana.d.ts +7 -0
- package/dist/types/registry/siliconflow-cn.d.ts +7 -0
- package/dist/types/registry/siliconflow.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 +75 -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 +7 -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 +15 -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 +46 -0
- package/dist/types/types.d.ts +1064 -0
- package/dist/types/usage/alibaba-token-plan.d.ts +3 -0
- package/dist/types/usage/claude.d.ts +4 -0
- package/dist/types/usage/cursor.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 +3 -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 +88 -0
- package/dist/types/usage/openai-codex.d.ts +10 -0
- package/dist/types/usage/opencode-go.d.ts +2 -0
- package/dist/types/usage/shared.d.ts +1 -0
- package/dist/types/usage/synthetic.d.ts +2 -0
- package/dist/types/usage/umans.d.ts +2 -0
- package/dist/types/usage/xai-oauth.d.ts +12 -0
- package/dist/types/usage/zai.d.ts +3 -0
- package/dist/types/usage.d.ts +527 -0
- package/dist/types/utils/abort.d.ts +25 -0
- package/dist/types/utils/anthropic-auth.d.ts +35 -0
- package/dist/types/utils/aws-profile.d.ts +17 -0
- package/dist/types/utils/block-symbols.d.ts +62 -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 +39 -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 +135 -0
- package/dist/types/utils/http-inspector.d.ts +49 -0
- package/dist/types/utils/idle-iterator.d.ts +149 -0
- package/dist/types/utils/leaked-thinking-stream.d.ts +33 -0
- package/dist/types/utils/openai-http.d.ts +48 -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 +39 -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 +14 -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 +153 -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 +24 -0
- package/dist/types/utils/schema/wire.d.ts +52 -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 +52 -0
- package/dist/types/utils/validation.d.ts +28 -0
- package/dist/types/utils.d.ts +57 -0
- package/package.json +138 -0
- package/src/api-registry.ts +109 -0
- package/src/auth/sqlite-credential-store.ts +2066 -0
- package/src/auth-broker/client.ts +471 -0
- package/src/auth-broker/discover.ts +310 -0
- package/src/auth-broker/index.ts +7 -0
- package/src/auth-broker/refresher.ts +117 -0
- package/src/auth-broker/remote-store.ts +1332 -0
- package/src/auth-broker/server.ts +898 -0
- package/src/auth-broker/snapshot-cache.ts +200 -0
- package/src/auth-broker/types.ts +193 -0
- package/src/auth-broker/wire-schema-resource.ts +487 -0
- package/src/auth-broker/wire-schemas.ts +43 -0
- package/src/auth-gateway/http.ts +227 -0
- package/src/auth-gateway/index.ts +3 -0
- package/src/auth-gateway/server.ts +836 -0
- package/src/auth-gateway/types.ts +153 -0
- package/src/auth-retry.ts +401 -0
- package/src/auth-storage.ts +6540 -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 +40 -0
- package/src/dialect/examples.ts +71 -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 +583 -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 +579 -0
- package/src/dialect/harmony.md +31 -0
- package/src/dialect/harmony.ts +345 -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 +30 -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 +481 -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 +304 -0
- package/src/dialect/thinking.ts +292 -0
- package/src/dialect/types.ts +56 -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 +47 -0
- package/src/error/auth.ts +48 -0
- package/src/error/aws.ts +35 -0
- package/src/error/classes.ts +281 -0
- package/src/error/finalize.ts +69 -0
- package/src/error/flags.ts +602 -0
- package/src/error/format.ts +45 -0
- package/src/error/gateway.ts +96 -0
- package/src/error/index.ts +14 -0
- package/src/error/oauth.ts +58 -0
- package/src/error/provider.ts +63 -0
- package/src/error/rate-limit.ts +303 -0
- package/src/error/retryable.ts +70 -0
- package/src/error/validation.ts +44 -0
- package/src/index.ts +51 -0
- package/src/provider-details.ts +90 -0
- package/src/providers/amazon-bedrock.ts +1064 -0
- package/src/providers/anthropic-client.ts +317 -0
- package/src/providers/anthropic-messages-server-schema.ts +252 -0
- package/src/providers/anthropic-messages-server.ts +818 -0
- package/src/providers/anthropic-wire.ts +359 -0
- package/src/providers/anthropic.ts +4539 -0
- package/src/providers/aws-credentials.ts +772 -0
- package/src/providers/aws-eventstream.ts +181 -0
- package/src/providers/aws-sigv4.ts +218 -0
- package/src/providers/azure-openai-responses.ts +438 -0
- package/src/providers/bedrock-mantle.ts +110 -0
- package/src/providers/claude-code-fingerprint.ts +20 -0
- package/src/providers/cowork-fetch.ts +201 -0
- package/src/providers/cursor/exec-modern.ts +496 -0
- package/src/providers/cursor/proto/agent.proto +4533 -0
- package/src/providers/cursor/proto/buf.gen.yaml +6 -0
- package/src/providers/cursor/proto/buf.yaml +17 -0
- package/src/providers/cursor-pi-args.ts +165 -0
- package/src/providers/cursor.ts +4689 -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 +679 -0
- package/src/providers/error-message.ts +23 -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 +3135 -0
- package/src/providers/gitlab-duo.ts +399 -0
- package/src/providers/google-auth.ts +330 -0
- package/src/providers/google-gemini-cli.ts +1370 -0
- package/src/providers/google-shared.ts +1122 -0
- package/src/providers/google-types.ts +180 -0
- package/src/providers/google-vertex.ts +135 -0
- package/src/providers/google.ts +47 -0
- package/src/providers/grammar.ts +70 -0
- package/src/providers/kimi.ts +51 -0
- package/src/providers/mock.ts +514 -0
- package/src/providers/ollama.ts +776 -0
- package/src/providers/openai-anthropic-shim.ts +166 -0
- package/src/providers/openai-chat-server-schema.ts +243 -0
- package/src/providers/openai-chat-server.ts +752 -0
- package/src/providers/openai-chat-wire.ts +859 -0
- package/src/providers/openai-codex/request-transformer.ts +491 -0
- package/src/providers/openai-codex/response-handler.ts +102 -0
- package/src/providers/openai-codex-responses.ts +4716 -0
- package/src/providers/openai-completions.ts +2389 -0
- package/src/providers/openai-reasoning-fallback.ts +269 -0
- package/src/providers/openai-responses-server-schema.ts +397 -0
- package/src/providers/openai-responses-server.ts +1466 -0
- package/src/providers/openai-responses-wire.ts +6416 -0
- package/src/providers/openai-responses.ts +1393 -0
- package/src/providers/openai-shared.ts +3500 -0
- package/src/providers/pi-native-client.ts +275 -0
- package/src/providers/pi-native-server.ts +245 -0
- package/src/providers/register-builtins.ts +503 -0
- package/src/providers/synthetic.ts +50 -0
- package/src/providers/transform-messages.ts +1083 -0
- package/src/providers/vision-guard.ts +54 -0
- package/src/registry/aiand.ts +22 -0
- package/src/registry/aimlapi.ts +6 -0
- package/src/registry/alibaba-coding-plan.ts +104 -0
- package/src/registry/alibaba-token-plan.ts +125 -0
- package/src/registry/amazon-bedrock.ts +22 -0
- package/src/registry/anthropic.ts +26 -0
- package/src/registry/api-key-login.ts +115 -0
- package/src/registry/api-key-validation.ts +145 -0
- package/src/registry/aws.ts +57 -0
- package/src/registry/azure.ts +6 -0
- package/src/registry/baseten.ts +22 -0
- package/src/registry/bedrock-mantle.ts +34 -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/exa.ts +19 -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/gmi-cloud.ts +22 -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/meta.ts +22 -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 +28 -0
- package/src/registry/nanogpt.ts +22 -0
- package/src/registry/novita.ts +25 -0
- package/src/registry/nvidia.ts +61 -0
- package/src/registry/oauth/anthropic-constants.ts +12 -0
- package/src/registry/oauth/anthropic.ts +346 -0
- package/src/registry/oauth/callback-server.ts +438 -0
- package/src/registry/oauth/cursor.ts +187 -0
- package/src/registry/oauth/device-code.ts +92 -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 +225 -0
- package/src/registry/oauth/google-gemini-cli.ts +297 -0
- package/src/registry/oauth/google-oauth-shared.ts +211 -0
- package/src/registry/oauth/index.ts +187 -0
- package/src/registry/oauth/kimi.ts +297 -0
- package/src/registry/oauth/minimax-code.ts +53 -0
- package/src/registry/oauth/oauth.html +317 -0
- package/src/registry/oauth/openai-codex.ts +384 -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 +96 -0
- package/src/registry/oauth/wafer.ts +24 -0
- package/src/registry/oauth/xai-oauth.ts +559 -0
- package/src/registry/oauth/xiaomi.ts +211 -0
- package/src/registry/oauth/zai.ts +285 -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 +182 -0
- package/src/registry/sakana.ts +22 -0
- package/src/registry/siliconflow-cn.ts +22 -0
- package/src/registry/siliconflow.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 +86 -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 +22 -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 +41 -0
- package/src/registry/zenmux.ts +22 -0
- package/src/registry/zhipu-coding-plan.ts +27 -0
- package/src/stream.ts +1944 -0
- package/src/types.ts +1243 -0
- package/src/usage/alibaba-token-plan.ts +230 -0
- package/src/usage/claude.ts +830 -0
- package/src/usage/cursor.ts +335 -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 +277 -0
- package/src/usage/minimax-code.ts +291 -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 +205 -0
- package/src/usage/openai-codex.ts +627 -0
- package/src/usage/opencode-go.ts +89 -0
- package/src/usage/shared.ts +10 -0
- package/src/usage/synthetic.ts +180 -0
- package/src/usage/umans.ts +192 -0
- package/src/usage/xai-oauth.ts +414 -0
- package/src/usage/zai.ts +370 -0
- package/src/usage.ts +411 -0
- package/src/utils/abort.ts +67 -0
- package/src/utils/anthropic-auth.ts +93 -0
- package/src/utils/aws-profile.ts +88 -0
- package/src/utils/block-symbols.ts +78 -0
- package/src/utils/deterministic-id.ts +20 -0
- package/src/utils/empty-completion-retry.ts +161 -0
- package/src/utils/event-stream.ts +202 -0
- package/src/utils/foundry.ts +8 -0
- package/src/utils/google-validation.ts +25 -0
- package/src/utils/harmony-leak.ts +500 -0
- package/src/utils/http-inspector.ts +196 -0
- package/src/utils/idle-iterator.ts +531 -0
- package/src/utils/leaked-thinking-stream.ts +483 -0
- package/src/utils/openai-http.ts +119 -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 +314 -0
- package/src/utils/request-debug.ts +351 -0
- package/src/utils/retry-after.ts +121 -0
- package/src/utils/retry.ts +77 -0
- package/src/utils/schema/CONSTRAINTS.md +168 -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 +210 -0
- package/src/utils/schema/index.ts +14 -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 +2314 -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 +212 -0
- package/src/utils/schema/wire.ts +662 -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 +101 -0
- package/src/utils/validation.ts +1932 -0
- package/src/utils.ts +492 -0
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
import { gunzipSync, gzipSync } from "node:zlib";
|
|
2
|
+
import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
|
|
3
|
+
import {
|
|
4
|
+
ChatMessageRequestType,
|
|
5
|
+
GetChatMessageRequestSchema,
|
|
6
|
+
GetChatMessageResponseSchema,
|
|
7
|
+
} from "@linxiraos/pi-catalog/discovery/devin-gen/exa/api_server_pb/api_server_pb";
|
|
8
|
+
import {
|
|
9
|
+
GetUserJwtRequestSchema,
|
|
10
|
+
GetUserJwtResponseSchema,
|
|
11
|
+
} from "@linxiraos/pi-catalog/discovery/devin-gen/exa/auth_pb/auth_pb";
|
|
12
|
+
import {
|
|
13
|
+
CacheControlType,
|
|
14
|
+
type ChatMessagePrompt,
|
|
15
|
+
ChatMessagePromptSchema,
|
|
16
|
+
ChatToolChoiceSchema,
|
|
17
|
+
ChatToolDefinitionSchema,
|
|
18
|
+
PromptCacheOptionsSchema,
|
|
19
|
+
} from "@linxiraos/pi-catalog/discovery/devin-gen/exa/chat_pb/chat_pb";
|
|
20
|
+
import {
|
|
21
|
+
ChatMessageSource,
|
|
22
|
+
type ChatToolCall,
|
|
23
|
+
ChatToolCallSchema,
|
|
24
|
+
CompletionConfigurationSchema,
|
|
25
|
+
ConversationalPlannerMode,
|
|
26
|
+
ImageDataSchema,
|
|
27
|
+
MetadataSchema,
|
|
28
|
+
StopReason,
|
|
29
|
+
} from "@linxiraos/pi-catalog/discovery/devin-gen/exa/codeium_common_pb/codeium_common_pb";
|
|
30
|
+
import { calculateCost } from "@linxiraos/pi-catalog/models";
|
|
31
|
+
import { logger, parseStreamingJson, parseStreamingJsonThrottled } from "@linxiraos/pi-utils";
|
|
32
|
+
import * as AIError from "../error";
|
|
33
|
+
import type {
|
|
34
|
+
Api,
|
|
35
|
+
AssistantMessage,
|
|
36
|
+
Context,
|
|
37
|
+
Message,
|
|
38
|
+
Model,
|
|
39
|
+
StreamFunction,
|
|
40
|
+
StreamOptions,
|
|
41
|
+
TextContent,
|
|
42
|
+
ThinkingContent,
|
|
43
|
+
Tool,
|
|
44
|
+
ToolCall,
|
|
45
|
+
} from "../types";
|
|
46
|
+
import { normalizeSystemPrompts } from "../utils";
|
|
47
|
+
import { isDemotedThinking } from "../utils/block-symbols";
|
|
48
|
+
import { deterministicUuid } from "../utils/deterministic-id";
|
|
49
|
+
import { AssistantMessageEventStream } from "../utils/event-stream";
|
|
50
|
+
import { toolWireSchema } from "../utils/schema/wire";
|
|
51
|
+
import { transformMessages } from "./transform-messages";
|
|
52
|
+
|
|
53
|
+
/** Base host for Codeium/Windsurf's Cascade chat API (Connect protocol over HTTP/1.1). */
|
|
54
|
+
export const DEVIN_API_URL = "https://server.codeium.com";
|
|
55
|
+
|
|
56
|
+
export interface DevinOptions extends StreamOptions {
|
|
57
|
+
/** Cascade conversation id; reused as `cascade_id` so the server threads turns. */
|
|
58
|
+
conversationId?: string;
|
|
59
|
+
/** Falls back to `cascade_id` when no `conversationId` is supplied. */
|
|
60
|
+
sessionId?: string;
|
|
61
|
+
/** Wire model uid selected after thinking-effort routing. */
|
|
62
|
+
chatModelUid?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const CHAT_MESSAGE_PATH = "/exa.api_server_pb.ApiServerService/GetChatMessage";
|
|
66
|
+
const DEVIN_IDE_VERSION = "3.2.23";
|
|
67
|
+
const DEVIN_EXTENSION_VERSION = "1.48.2";
|
|
68
|
+
const DEVIN_SESSION_TOKEN_PREFIX = "devin-session-token$";
|
|
69
|
+
const DEVIN_AUTH_PATH = "/exa.auth_pb.AuthService/GetUserJwt";
|
|
70
|
+
const DEVIN_DEFAULT_STOP_PATTERNS = ["<|user|>", "<|bot|>", "<|context_request|>", "<|endoftext|>", "<|end_of_turn|>"];
|
|
71
|
+
|
|
72
|
+
/** Connect streaming framing: flag byte bit 0x01 = gzip payload, 0x02 = end-of-stream JSON trailers. */
|
|
73
|
+
const CONNECT_COMPRESSED_FLAG = 0x01;
|
|
74
|
+
const CONNECT_END_STREAM_FLAG = 0x02;
|
|
75
|
+
/**
|
|
76
|
+
* Hard upper bound on a single Connect frame payload. The 4-byte length prefix
|
|
77
|
+
* is otherwise attacker-controlled (up to `2**32 - 1`), so a malicious or buggy
|
|
78
|
+
* peer could force {@link streamDevin}'s reader to buffer gigabytes via
|
|
79
|
+
* `Buffer.concat` before the idle-timeout wrapper aborts. Well above any
|
|
80
|
+
* legitimate Cascade response but tight enough that a corrupt length prefix
|
|
81
|
+
* fails fast instead of consuming memory.
|
|
82
|
+
*/
|
|
83
|
+
const MAX_CONNECT_FRAME_PAYLOAD = 16 * 1024 * 1024;
|
|
84
|
+
/**
|
|
85
|
+
* Recovery heuristic for opaque Devin `invalid_argument` trailers. This is not
|
|
86
|
+
* asserted to be the backend's hard limit: small requests can hit the same
|
|
87
|
+
* intermittent error, while compactable message history this large is likely
|
|
88
|
+
* to benefit from the existing context-overflow maintenance path.
|
|
89
|
+
*/
|
|
90
|
+
const LARGE_HISTORY_RECOVERY_BYTES = 512 * 1024;
|
|
91
|
+
|
|
92
|
+
export const streamDevin: StreamFunction<"devin-agent"> = (
|
|
93
|
+
model: Model<"devin-agent">,
|
|
94
|
+
context: Context,
|
|
95
|
+
options?: DevinOptions,
|
|
96
|
+
): AssistantMessageEventStream => {
|
|
97
|
+
const stream = new AssistantMessageEventStream();
|
|
98
|
+
|
|
99
|
+
(async () => {
|
|
100
|
+
const startTime = performance.now();
|
|
101
|
+
let firstTokenTime: number | undefined;
|
|
102
|
+
|
|
103
|
+
const output: AssistantMessage = {
|
|
104
|
+
role: "assistant",
|
|
105
|
+
content: [],
|
|
106
|
+
api: "devin-agent" as Api,
|
|
107
|
+
provider: model.provider,
|
|
108
|
+
model: model.id,
|
|
109
|
+
usage: {
|
|
110
|
+
input: 0,
|
|
111
|
+
output: 0,
|
|
112
|
+
cacheRead: 0,
|
|
113
|
+
cacheWrite: 0,
|
|
114
|
+
totalTokens: 0,
|
|
115
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
116
|
+
},
|
|
117
|
+
stopReason: "stop",
|
|
118
|
+
timestamp: Date.now(),
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
let currentTextBlock: TextContent | null = null;
|
|
122
|
+
let currentThinkingBlock: ThinkingContent | null = null;
|
|
123
|
+
// Tool-call content blocks keyed by streamed tool-call id, plus the JSON-args text
|
|
124
|
+
// accumulated per id (kept out of the content object so finalized tool calls stay clean).
|
|
125
|
+
const toolBlocks = new Map<string, ToolCall>();
|
|
126
|
+
const toolPartialJson = new Map<string, string>();
|
|
127
|
+
// Last-parsed argument-buffer length per tool-call id — bounds the
|
|
128
|
+
// mid-stream parse work to O(N) via `parseStreamingJsonThrottled`; the
|
|
129
|
+
// authoritative final parse still runs unconditionally in the toolcall_end
|
|
130
|
+
// loop below.
|
|
131
|
+
const toolLastParseLen = new Map<string, number>();
|
|
132
|
+
let activeToolCallId: string | undefined;
|
|
133
|
+
let latestStopReason = StopReason.UNSPECIFIED;
|
|
134
|
+
|
|
135
|
+
const markFirstToken = () => {
|
|
136
|
+
if (firstTokenTime === undefined) firstTokenTime = performance.now();
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const endTextBlock = () => {
|
|
140
|
+
const block = currentTextBlock;
|
|
141
|
+
if (!block) return;
|
|
142
|
+
currentTextBlock = null;
|
|
143
|
+
stream.push({
|
|
144
|
+
type: "text_end",
|
|
145
|
+
contentIndex: output.content.indexOf(block),
|
|
146
|
+
content: block.text,
|
|
147
|
+
partial: output,
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const endThinkingBlock = () => {
|
|
152
|
+
const block = currentThinkingBlock;
|
|
153
|
+
if (!block) return;
|
|
154
|
+
currentThinkingBlock = null;
|
|
155
|
+
stream.push({
|
|
156
|
+
type: "thinking_end",
|
|
157
|
+
contentIndex: output.content.indexOf(block),
|
|
158
|
+
content: block.thinking,
|
|
159
|
+
partial: output,
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const fetchImpl = options?.fetch ?? fetch;
|
|
165
|
+
const baseUrl = (model.baseUrl || DEVIN_API_URL).replace(/\/+$/, "");
|
|
166
|
+
const apiKey = normalizeDevinSessionToken(options?.apiKey);
|
|
167
|
+
const auth = await fetchDevinAuthMetadata(apiKey, baseUrl, fetchImpl, options?.signal);
|
|
168
|
+
const chatBaseUrl = auth.baseUrl ?? baseUrl;
|
|
169
|
+
const request = buildDevinChatRequest(model, context, options, apiKey, auth.userJwt);
|
|
170
|
+
const reqBytes = toBinary(GetChatMessageRequestSchema, request);
|
|
171
|
+
const gz = gzipSync(reqBytes);
|
|
172
|
+
logger.debug("devin: sending chat request", {
|
|
173
|
+
model: model.id,
|
|
174
|
+
tools: context.tools?.length ?? 0,
|
|
175
|
+
requestBytes: reqBytes.byteLength,
|
|
176
|
+
compressedBytes: gz.byteLength,
|
|
177
|
+
});
|
|
178
|
+
const frame = Buffer.alloc(5 + gz.length);
|
|
179
|
+
frame[0] = CONNECT_COMPRESSED_FLAG;
|
|
180
|
+
frame.writeUInt32BE(gz.length, 1);
|
|
181
|
+
frame.set(gz, 5);
|
|
182
|
+
|
|
183
|
+
const response = await fetchImpl(chatBaseUrl + CHAT_MESSAGE_PATH, {
|
|
184
|
+
method: "POST",
|
|
185
|
+
headers: {
|
|
186
|
+
"content-type": "application/connect+proto",
|
|
187
|
+
"connect-protocol-version": "1",
|
|
188
|
+
"connect-content-encoding": "gzip",
|
|
189
|
+
"accept-encoding": "identity",
|
|
190
|
+
"user-agent": "connect-go/1.18.1 (go1.26.3)",
|
|
191
|
+
"connect-accept-encoding": "gzip",
|
|
192
|
+
...(options?.headers ?? {}),
|
|
193
|
+
},
|
|
194
|
+
body: frame,
|
|
195
|
+
signal: options?.signal,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
if (!response.ok) {
|
|
199
|
+
const text = await response.text();
|
|
200
|
+
throw new AIError.DevinApiError(
|
|
201
|
+
`Devin API error ${response.status} ${response.statusText}: ${text}`,
|
|
202
|
+
response.status,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
if (!response.body) {
|
|
206
|
+
throw new AIError.ProviderResponseError("Devin API error: response body is empty", {
|
|
207
|
+
provider: model.provider,
|
|
208
|
+
kind: "empty-body",
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const body = response.body;
|
|
212
|
+
|
|
213
|
+
stream.push({ type: "start", partial: output });
|
|
214
|
+
|
|
215
|
+
const reader = body.getReader();
|
|
216
|
+
let pending = Buffer.alloc(0);
|
|
217
|
+
|
|
218
|
+
for (;;) {
|
|
219
|
+
const { done, value } = await reader.read();
|
|
220
|
+
if (value && value.length > 0) {
|
|
221
|
+
// Steady state drains fully per chunk; view the fresh reader chunk
|
|
222
|
+
// instead of copying it through Buffer.concat (see aws-eventstream.ts).
|
|
223
|
+
pending =
|
|
224
|
+
pending.length === 0
|
|
225
|
+
? Buffer.from(value.buffer, value.byteOffset, value.byteLength)
|
|
226
|
+
: Buffer.concat([pending, value]);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
while (pending.length >= 5) {
|
|
230
|
+
const flag = pending[0];
|
|
231
|
+
const len = pending.readUInt32BE(1);
|
|
232
|
+
if (len > MAX_CONNECT_FRAME_PAYLOAD) {
|
|
233
|
+
throw new AIError.ProviderResponseError(
|
|
234
|
+
`Devin Connect frame length ${len} exceeds ${MAX_CONNECT_FRAME_PAYLOAD}-byte cap`,
|
|
235
|
+
{ provider: model.provider, kind: "envelope" },
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
if (pending.length < 5 + len) break;
|
|
239
|
+
const payload = pending.subarray(5, 5 + len);
|
|
240
|
+
pending = pending.subarray(5 + len);
|
|
241
|
+
|
|
242
|
+
if (flag & CONNECT_END_STREAM_FLAG) {
|
|
243
|
+
const trailerBytes = flag & CONNECT_COMPRESSED_FLAG ? gunzipSync(payload) : payload;
|
|
244
|
+
const trailerError = readConnectTrailerError(trailerBytes.toString("utf8").trim());
|
|
245
|
+
if (trailerError) {
|
|
246
|
+
const error = new AIError.ValidationError(trailerError.formatted);
|
|
247
|
+
if (
|
|
248
|
+
firstTokenTime === undefined &&
|
|
249
|
+
trailerError.code.toLowerCase() === "invalid_argument" &&
|
|
250
|
+
/\binternal error\b/i.test(trailerError.message)
|
|
251
|
+
) {
|
|
252
|
+
// The full protobuf also contains the system prompt and tool
|
|
253
|
+
// schemas, which history maintenance cannot shrink. Re-encode
|
|
254
|
+
// only the repeated history field before choosing recovery.
|
|
255
|
+
let activeTailCount = 0;
|
|
256
|
+
const lastRole = context.messages.at(-1)?.role;
|
|
257
|
+
if (lastRole === "user" || lastRole === "developer") {
|
|
258
|
+
activeTailCount = 1;
|
|
259
|
+
// A trailing developer message can accompany the current user
|
|
260
|
+
// prompt. Earlier user-role records may instead be flushed
|
|
261
|
+
// execution history and must remain eligible for compaction.
|
|
262
|
+
if (lastRole === "developer") {
|
|
263
|
+
for (let i = context.messages.length - 2; i >= 0; i--) {
|
|
264
|
+
const role = context.messages[i].role;
|
|
265
|
+
if (role !== "user" && role !== "developer") break;
|
|
266
|
+
activeTailCount++;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const shrinkablePrompts =
|
|
271
|
+
activeTailCount > 0
|
|
272
|
+
? request.chatMessagePrompts.slice(0, -activeTailCount)
|
|
273
|
+
: request.chatMessagePrompts;
|
|
274
|
+
const historyBytes = toBinary(
|
|
275
|
+
GetChatMessageRequestSchema,
|
|
276
|
+
create(GetChatMessageRequestSchema, {
|
|
277
|
+
chatMessagePrompts: shrinkablePrompts,
|
|
278
|
+
}),
|
|
279
|
+
).byteLength;
|
|
280
|
+
if (historyBytes >= LARGE_HISTORY_RECOVERY_BYTES) {
|
|
281
|
+
AIError.attach(error, AIError.create(AIError.Flag.ContextOverflow));
|
|
282
|
+
logger.warn("devin: treating large-history invalid_argument as context overflow", {
|
|
283
|
+
model: model.id,
|
|
284
|
+
historyBytes,
|
|
285
|
+
requestBytes: reqBytes.byteLength,
|
|
286
|
+
compressedBytes: gz.byteLength,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
throw error;
|
|
291
|
+
}
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const raw = flag & CONNECT_COMPRESSED_FLAG ? gunzipSync(payload) : payload;
|
|
296
|
+
const msg = fromBinary(GetChatMessageResponseSchema, raw);
|
|
297
|
+
if (msg.messageId && !output.responseId) output.responseId = msg.messageId;
|
|
298
|
+
|
|
299
|
+
if (msg.deltaThinking) {
|
|
300
|
+
markFirstToken();
|
|
301
|
+
const block: ThinkingContent = currentThinkingBlock ?? { type: "thinking", thinking: "" };
|
|
302
|
+
if (currentThinkingBlock !== block) {
|
|
303
|
+
output.content.push(block);
|
|
304
|
+
currentThinkingBlock = block;
|
|
305
|
+
stream.push({
|
|
306
|
+
type: "thinking_start",
|
|
307
|
+
contentIndex: output.content.length - 1,
|
|
308
|
+
partial: output,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
block.thinking += msg.deltaThinking;
|
|
312
|
+
if (msg.deltaSignature) block.thinkingSignature = msg.deltaSignature;
|
|
313
|
+
stream.push({
|
|
314
|
+
type: "thinking_delta",
|
|
315
|
+
contentIndex: output.content.indexOf(block),
|
|
316
|
+
delta: msg.deltaThinking,
|
|
317
|
+
partial: output,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (msg.deltaText) {
|
|
322
|
+
markFirstToken();
|
|
323
|
+
endThinkingBlock();
|
|
324
|
+
const block: TextContent = currentTextBlock ?? { type: "text", text: "" };
|
|
325
|
+
if (currentTextBlock !== block) {
|
|
326
|
+
output.content.push(block);
|
|
327
|
+
currentTextBlock = block;
|
|
328
|
+
stream.push({ type: "text_start", contentIndex: output.content.length - 1, partial: output });
|
|
329
|
+
}
|
|
330
|
+
block.text += msg.deltaText;
|
|
331
|
+
stream.push({
|
|
332
|
+
type: "text_delta",
|
|
333
|
+
contentIndex: output.content.indexOf(block),
|
|
334
|
+
delta: msg.deltaText,
|
|
335
|
+
partial: output,
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (msg.deltaToolCalls.length > 0) {
|
|
340
|
+
markFirstToken();
|
|
341
|
+
endTextBlock();
|
|
342
|
+
endThinkingBlock();
|
|
343
|
+
for (const tc of msg.deltaToolCalls) {
|
|
344
|
+
const toolCallId = tc.id || activeToolCallId;
|
|
345
|
+
if (!toolCallId) continue;
|
|
346
|
+
let block = toolBlocks.get(toolCallId);
|
|
347
|
+
if (!block) {
|
|
348
|
+
block = { type: "toolCall", id: toolCallId, name: tc.name, arguments: {} };
|
|
349
|
+
output.content.push(block);
|
|
350
|
+
toolBlocks.set(toolCallId, block);
|
|
351
|
+
toolPartialJson.set(toolCallId, "");
|
|
352
|
+
stream.push({
|
|
353
|
+
type: "toolcall_start",
|
|
354
|
+
contentIndex: output.content.length - 1,
|
|
355
|
+
partial: output,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
if (tc.name) block.name = tc.name;
|
|
359
|
+
activeToolCallId = toolCallId;
|
|
360
|
+
if (!tc.argumentsJson) continue;
|
|
361
|
+
const previousJson = toolPartialJson.get(toolCallId) ?? "";
|
|
362
|
+
const accumulated = tc.argumentsJson.startsWith(previousJson)
|
|
363
|
+
? tc.argumentsJson
|
|
364
|
+
: previousJson + tc.argumentsJson;
|
|
365
|
+
const delta = accumulated.slice(previousJson.length);
|
|
366
|
+
toolPartialJson.set(toolCallId, accumulated);
|
|
367
|
+
const throttled = parseStreamingJsonThrottled(accumulated, toolLastParseLen.get(toolCallId) ?? 0);
|
|
368
|
+
if (throttled) {
|
|
369
|
+
block.arguments = throttled.value;
|
|
370
|
+
toolLastParseLen.set(toolCallId, throttled.parsedLen);
|
|
371
|
+
}
|
|
372
|
+
stream.push({
|
|
373
|
+
type: "toolcall_delta",
|
|
374
|
+
contentIndex: output.content.indexOf(block),
|
|
375
|
+
delta,
|
|
376
|
+
partial: output,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
if (msg.stopReason !== StopReason.UNSPECIFIED) {
|
|
382
|
+
latestStopReason = msg.stopReason;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (msg.usage) {
|
|
386
|
+
output.usage.input = Number(msg.usage.inputTokens);
|
|
387
|
+
output.usage.output = Number(msg.usage.outputTokens);
|
|
388
|
+
output.usage.cacheRead = Number(msg.usage.cacheReadTokens);
|
|
389
|
+
output.usage.cacheWrite = Number(msg.usage.cacheWriteTokens);
|
|
390
|
+
output.usage.totalTokens =
|
|
391
|
+
output.usage.input + output.usage.output + output.usage.cacheRead + output.usage.cacheWrite;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (done) break;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
endTextBlock();
|
|
399
|
+
endThinkingBlock();
|
|
400
|
+
for (const [id, block] of toolBlocks) {
|
|
401
|
+
block.arguments = parseStreamingJson(toolPartialJson.get(id));
|
|
402
|
+
stream.push({
|
|
403
|
+
type: "toolcall_end",
|
|
404
|
+
contentIndex: output.content.indexOf(block),
|
|
405
|
+
toolCall: block,
|
|
406
|
+
partial: output,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const doneReason: "stop" | "length" | "toolUse" =
|
|
411
|
+
toolBlocks.size > 0 ? "toolUse" : latestStopReason === StopReason.MAX_TOKENS ? "length" : "stop";
|
|
412
|
+
output.stopReason = doneReason;
|
|
413
|
+
|
|
414
|
+
calculateCost(model, output.usage);
|
|
415
|
+
output.duration = performance.now() - startTime;
|
|
416
|
+
if (firstTokenTime) output.ttft = firstTokenTime - startTime;
|
|
417
|
+
|
|
418
|
+
stream.push({ type: "done", reason: doneReason, message: output });
|
|
419
|
+
stream.end();
|
|
420
|
+
} catch (error) {
|
|
421
|
+
logger.error("devin: stream failed", { error: String(error) });
|
|
422
|
+
const result = await AIError.finalize(error, { api: model.api, signal: options?.signal });
|
|
423
|
+
output.stopReason = result.stopReason;
|
|
424
|
+
output.errorStatus = result.status;
|
|
425
|
+
output.errorId = result.id;
|
|
426
|
+
output.errorMessage = result.message;
|
|
427
|
+
output.duration = performance.now() - startTime;
|
|
428
|
+
if (firstTokenTime) output.ttft = firstTokenTime - startTime;
|
|
429
|
+
stream.push({ type: "error", reason: result.stopReason, error: output });
|
|
430
|
+
stream.end();
|
|
431
|
+
}
|
|
432
|
+
})();
|
|
433
|
+
|
|
434
|
+
return stream;
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
function normalizeDevinSessionToken(apiKey: string | undefined): string {
|
|
438
|
+
if (!apiKey) return "";
|
|
439
|
+
return apiKey.startsWith(DEVIN_SESSION_TOKEN_PREFIX) ? apiKey : `${DEVIN_SESSION_TOKEN_PREFIX}${apiKey}`;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async function fetchDevinAuthMetadata(
|
|
443
|
+
apiKey: string,
|
|
444
|
+
baseUrl: string,
|
|
445
|
+
fetchImpl: NonNullable<StreamOptions["fetch"]>,
|
|
446
|
+
signal: AbortSignal | undefined,
|
|
447
|
+
): Promise<{ userJwt: string; baseUrl?: string }> {
|
|
448
|
+
const request = create(GetUserJwtRequestSchema, {
|
|
449
|
+
metadata: create(MetadataSchema, {
|
|
450
|
+
apiKey,
|
|
451
|
+
ideName: "windsurf",
|
|
452
|
+
ideVersion: DEVIN_IDE_VERSION,
|
|
453
|
+
extensionName: "windsurf",
|
|
454
|
+
extensionVersion: DEVIN_EXTENSION_VERSION,
|
|
455
|
+
locale: "en",
|
|
456
|
+
}),
|
|
457
|
+
});
|
|
458
|
+
const response = await fetchImpl(`${baseUrl}${DEVIN_AUTH_PATH}`, {
|
|
459
|
+
method: "POST",
|
|
460
|
+
headers: {
|
|
461
|
+
"content-type": "application/proto",
|
|
462
|
+
"connect-protocol-version": "1",
|
|
463
|
+
accept: "*/*",
|
|
464
|
+
},
|
|
465
|
+
body: toBinary(GetUserJwtRequestSchema, request),
|
|
466
|
+
signal,
|
|
467
|
+
});
|
|
468
|
+
const payload = new Uint8Array(await response.arrayBuffer());
|
|
469
|
+
if (!response.ok) {
|
|
470
|
+
throw new AIError.DevinApiError(
|
|
471
|
+
`Devin auth error ${response.status} ${response.statusText}: ${new TextDecoder().decode(payload)}`,
|
|
472
|
+
response.status,
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
const decoded = decodeDevinUserJwtResponse(payload);
|
|
476
|
+
if (!decoded.userJwt) {
|
|
477
|
+
throw new AIError.ProviderResponseError("Devin auth error: GetUserJwt returned an empty user JWT", {
|
|
478
|
+
provider: "devin",
|
|
479
|
+
kind: "runtime",
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
const customBaseUrl = decoded.customApiServerUrl.trim();
|
|
483
|
+
return { userJwt: decoded.userJwt, ...(customBaseUrl ? { baseUrl: customBaseUrl.replace(/\/+$/, "") } : undefined) };
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function decodeDevinUserJwtResponse(payload: Uint8Array) {
|
|
487
|
+
try {
|
|
488
|
+
return fromBinary(GetUserJwtResponseSchema, payload);
|
|
489
|
+
} catch {
|
|
490
|
+
return fromBinary(GetUserJwtResponseSchema, gunzipSync(payload));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Build a {@link GetChatMessageRequest} for one Cascade turn. Auth rides inside
|
|
496
|
+
* `Metadata.apiKey`; the system prompt is the flattened `prompt` string and the
|
|
497
|
+
* conversation history maps to `chatMessagePrompts`.
|
|
498
|
+
*/
|
|
499
|
+
function buildDevinChatRequest(
|
|
500
|
+
model: Model<"devin-agent">,
|
|
501
|
+
context: Context,
|
|
502
|
+
options: DevinOptions | undefined,
|
|
503
|
+
apiKey: string,
|
|
504
|
+
userJwt: string,
|
|
505
|
+
) {
|
|
506
|
+
const cascadeId = options?.conversationId ?? options?.sessionId ?? crypto.randomUUID();
|
|
507
|
+
const stopPatterns =
|
|
508
|
+
options?.stopSequences && options.stopSequences.length > 0
|
|
509
|
+
? [...DEVIN_DEFAULT_STOP_PATTERNS, ...options.stopSequences]
|
|
510
|
+
: DEVIN_DEFAULT_STOP_PATTERNS;
|
|
511
|
+
const messages = transformMessages(context.messages, model);
|
|
512
|
+
return create(GetChatMessageRequestSchema, {
|
|
513
|
+
metadata: create(MetadataSchema, {
|
|
514
|
+
apiKey,
|
|
515
|
+
userJwt,
|
|
516
|
+
ideName: "windsurf",
|
|
517
|
+
ideVersion: DEVIN_IDE_VERSION,
|
|
518
|
+
extensionName: "windsurf",
|
|
519
|
+
extensionVersion: DEVIN_EXTENSION_VERSION,
|
|
520
|
+
locale: "en",
|
|
521
|
+
}),
|
|
522
|
+
prompt: normalizeSystemPrompts(context.systemPrompt).join("\n\n"),
|
|
523
|
+
chatMessagePrompts: buildChatMessagePrompts(messages, cascadeId, model),
|
|
524
|
+
chatModelUid: options?.chatModelUid ?? model.requestModelId ?? model.id,
|
|
525
|
+
requestType: ChatMessageRequestType.CASCADE,
|
|
526
|
+
plannerMode: ConversationalPlannerMode.DEFAULT,
|
|
527
|
+
toolChoice: create(ChatToolChoiceSchema, { choice: { case: "optionName", value: "auto" } }),
|
|
528
|
+
systemPromptCacheOptions: create(PromptCacheOptionsSchema, { type: CacheControlType.EPHEMERAL }),
|
|
529
|
+
disableParallelToolCalls: true,
|
|
530
|
+
cascadeId,
|
|
531
|
+
executionId: crypto.randomUUID(),
|
|
532
|
+
configuration: create(CompletionConfigurationSchema, {
|
|
533
|
+
numCompletions: 1n,
|
|
534
|
+
maxTokens: BigInt(options?.maxTokens ?? model.maxTokens ?? 64000),
|
|
535
|
+
maxNewlines: 200n,
|
|
536
|
+
temperature: options?.temperature ?? 0.4,
|
|
537
|
+
firstTemperature: options?.temperature ?? 0.4,
|
|
538
|
+
topK: 50n,
|
|
539
|
+
topP: options?.topP ?? 1,
|
|
540
|
+
stopPatterns,
|
|
541
|
+
fimEotProbThreshold: 1,
|
|
542
|
+
}),
|
|
543
|
+
tools: (context.tools ?? []).map((tool: Tool) =>
|
|
544
|
+
create(ChatToolDefinitionSchema, {
|
|
545
|
+
name: tool.name,
|
|
546
|
+
description: tool.description,
|
|
547
|
+
jsonSchemaString: JSON.stringify(toolWireSchema(tool)),
|
|
548
|
+
strict: tool.strict ?? false,
|
|
549
|
+
}),
|
|
550
|
+
),
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/** Map omp `Message` history onto Cascade `ChatMessagePrompt`s (USER / SYSTEM / TOOL channels). */
|
|
555
|
+
function buildChatMessagePrompts(
|
|
556
|
+
messages: Message[],
|
|
557
|
+
cascadeId: string,
|
|
558
|
+
model: Model<"devin-agent">,
|
|
559
|
+
): ChatMessagePrompt[] {
|
|
560
|
+
const prompts: ChatMessagePrompt[] = [];
|
|
561
|
+
// messageId seeds are `cascadeId\0index\0role[...]` — prompt text is excluded
|
|
562
|
+
// so ids stay stable across content edits / history rebuilds.
|
|
563
|
+
for (const [index, msg] of messages.entries()) {
|
|
564
|
+
if (msg.role === "user" || msg.role === "developer") {
|
|
565
|
+
let promptText = "";
|
|
566
|
+
const images = [];
|
|
567
|
+
if (typeof msg.content === "string") {
|
|
568
|
+
promptText = msg.content;
|
|
569
|
+
} else {
|
|
570
|
+
for (const part of msg.content) {
|
|
571
|
+
if (part.type === "text") {
|
|
572
|
+
promptText += part.text;
|
|
573
|
+
} else if (part.type === "image") {
|
|
574
|
+
images.push(create(ImageDataSchema, { base64Data: part.data, mimeType: part.mimeType }));
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
prompts.push(
|
|
579
|
+
create(ChatMessagePromptSchema, {
|
|
580
|
+
messageId: deterministicUuid(`${cascadeId}\0${index}\0${msg.role}`),
|
|
581
|
+
source: ChatMessageSource.USER,
|
|
582
|
+
prompt: promptText,
|
|
583
|
+
images,
|
|
584
|
+
}),
|
|
585
|
+
);
|
|
586
|
+
} else if (msg.role === "assistant") {
|
|
587
|
+
const isNativeDevinMessage =
|
|
588
|
+
msg.api === model.api && msg.provider === model.provider && msg.model === model.id;
|
|
589
|
+
let promptText = "";
|
|
590
|
+
let thinkingText = "";
|
|
591
|
+
let signature = "";
|
|
592
|
+
const toolCalls: ChatToolCall[] = [];
|
|
593
|
+
for (const part of msg.content) {
|
|
594
|
+
if (part.type === "text") {
|
|
595
|
+
promptText += `${part.text}${isDemotedThinking(part) ? "\n" : ""}`;
|
|
596
|
+
} else if (part.type === "thinking") {
|
|
597
|
+
thinkingText += part.thinking;
|
|
598
|
+
if (isNativeDevinMessage && !signature && part.thinkingSignature) signature = part.thinkingSignature;
|
|
599
|
+
} else if (part.type === "toolCall") {
|
|
600
|
+
toolCalls.push(
|
|
601
|
+
create(ChatToolCallSchema, {
|
|
602
|
+
id: part.id,
|
|
603
|
+
name: part.name,
|
|
604
|
+
argumentsJson: JSON.stringify(part.arguments),
|
|
605
|
+
}),
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (!promptText && !thinkingText && !signature && toolCalls.length === 0) continue;
|
|
610
|
+
prompts.push(
|
|
611
|
+
create(ChatMessagePromptSchema, {
|
|
612
|
+
messageId:
|
|
613
|
+
isNativeDevinMessage && msg.responseId
|
|
614
|
+
? msg.responseId
|
|
615
|
+
: `bot-${deterministicUuid(`${cascadeId}\0${index}\0assistant`)}`,
|
|
616
|
+
source: ChatMessageSource.SYSTEM,
|
|
617
|
+
prompt: promptText,
|
|
618
|
+
thinking: thinkingText,
|
|
619
|
+
signature,
|
|
620
|
+
signatureType: "",
|
|
621
|
+
toolCalls,
|
|
622
|
+
}),
|
|
623
|
+
);
|
|
624
|
+
} else {
|
|
625
|
+
let resultText = "";
|
|
626
|
+
const images = [];
|
|
627
|
+
for (const part of msg.content) {
|
|
628
|
+
if (part.type === "text") {
|
|
629
|
+
resultText += part.text;
|
|
630
|
+
} else if (part.type === "image") {
|
|
631
|
+
images.push(create(ImageDataSchema, { base64Data: part.data, mimeType: part.mimeType }));
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
prompts.push(
|
|
635
|
+
create(ChatMessagePromptSchema, {
|
|
636
|
+
messageId: deterministicUuid(`${cascadeId}\0${index}\0tool\0${msg.toolCallId}`),
|
|
637
|
+
source: ChatMessageSource.TOOL,
|
|
638
|
+
toolCallId: msg.toolCallId,
|
|
639
|
+
toolResultIsError: msg.isError,
|
|
640
|
+
prompt: resultText,
|
|
641
|
+
images,
|
|
642
|
+
}),
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
return prompts;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
interface ConnectTrailerError {
|
|
650
|
+
code: string;
|
|
651
|
+
message: string;
|
|
652
|
+
formatted: string;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Parse a Connect end-of-stream JSON trailer and return its structured error
|
|
657
|
+
* when it carries `{ error: { code, message } }`, else `null`. The trailer is
|
|
658
|
+
* untrusted server output, so the shape is checked with guards rather than asserted.
|
|
659
|
+
*/
|
|
660
|
+
function readConnectTrailerError(text: string): ConnectTrailerError | null {
|
|
661
|
+
if (text.length === 0) return null;
|
|
662
|
+
let parsed: unknown;
|
|
663
|
+
try {
|
|
664
|
+
parsed = JSON.parse(text);
|
|
665
|
+
} catch {
|
|
666
|
+
return null;
|
|
667
|
+
}
|
|
668
|
+
if (!parsed || typeof parsed !== "object" || !("error" in parsed)) return null;
|
|
669
|
+
const err = parsed.error;
|
|
670
|
+
if (!err || typeof err !== "object") return null;
|
|
671
|
+
const code = "code" in err && typeof err.code === "string" ? err.code : "";
|
|
672
|
+
const message = "message" in err && typeof err.message === "string" ? err.message : "";
|
|
673
|
+
if (!code && !message) return null;
|
|
674
|
+
return {
|
|
675
|
+
code,
|
|
676
|
+
message,
|
|
677
|
+
formatted: `Devin stream error${code ? ` ${code}` : ""}: ${message}`,
|
|
678
|
+
};
|
|
679
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Api, Model } from "../types";
|
|
2
|
+
|
|
3
|
+
export function createProviderErrorMessage(model: Model<Api>, err: unknown) {
|
|
4
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
5
|
+
return {
|
|
6
|
+
role: "assistant" as const,
|
|
7
|
+
content: [],
|
|
8
|
+
errorMessage,
|
|
9
|
+
api: model.api,
|
|
10
|
+
provider: model.provider,
|
|
11
|
+
model: model.id,
|
|
12
|
+
usage: {
|
|
13
|
+
input: 0,
|
|
14
|
+
output: 0,
|
|
15
|
+
cacheRead: 0,
|
|
16
|
+
cacheWrite: 0,
|
|
17
|
+
totalTokens: 0,
|
|
18
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
19
|
+
},
|
|
20
|
+
stopReason: "error" as const,
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
};
|
|
23
|
+
}
|