@aryee337/aery-ai 0.1.148 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2914 -0
- package/README.md +614 -813
- package/dist/types/api-registry.d.ts +30 -0
- package/dist/types/auth-broker/client.d.ts +66 -0
- package/dist/types/auth-broker/index.d.ts +5 -0
- package/dist/types/auth-broker/refresher.d.ts +25 -0
- package/dist/types/auth-broker/remote-store.d.ts +96 -0
- package/dist/types/auth-broker/server.d.ts +32 -0
- package/dist/types/auth-broker/types.d.ts +105 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +412 -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 +117 -0
- package/dist/types/auth-storage.d.ts +739 -0
- package/dist/types/index.d.ts +49 -0
- package/dist/types/model-cache.d.ts +17 -0
- package/dist/types/model-manager.d.ts +64 -0
- package/dist/types/model-thinking.d.ts +100 -0
- package/dist/types/models.d.ts +12 -0
- package/dist/types/provider-details.d.ts +24 -0
- package/dist/types/provider-models/bundled-references.d.ts +4 -0
- package/dist/types/provider-models/descriptors.d.ts +50 -0
- package/dist/types/provider-models/google.d.ts +24 -0
- package/dist/types/provider-models/index.d.ts +5 -0
- package/dist/types/provider-models/ollama.d.ts +7 -0
- package/dist/types/provider-models/openai-compat.d.ts +296 -0
- package/dist/types/provider-models/special.d.ts +16 -0
- package/dist/types/providers/aery-native-client.d.ts +13 -0
- package/dist/types/providers/aery-native-server.d.ts +68 -0
- package/dist/types/providers/amazon-bedrock.d.ts +38 -0
- package/dist/types/providers/anthropic-client.d.ts +99 -0
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +465 -0
- package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
- package/dist/types/providers/anthropic-wire.d.ts +262 -0
- package/dist/types/providers/anthropic.d.ts +206 -0
- package/dist/types/providers/aws-credentials.d.ts +43 -0
- package/dist/types/providers/aws-eventstream.d.ts +38 -0
- package/dist/types/providers/aws-sigv4.d.ts +55 -0
- package/dist/types/providers/azure-openai-responses.d.ts +15 -0
- package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
- package/dist/types/providers/cursor.d.ts +43 -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.d.ts +27 -0
- package/dist/types/providers/google-auth.d.ts +24 -0
- package/dist/types/providers/google-gemini-cli.d.ts +81 -0
- package/dist/types/providers/google-gemini-headers.d.ts +18 -0
- package/dist/types/providers/google-shared.d.ts +171 -0
- package/dist/types/providers/google-types.d.ts +138 -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 +173 -0
- package/dist/types/providers/ollama.d.ts +6 -0
- package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
- package/dist/types/providers/openai-chat-server-schema.d.ts +817 -0
- package/dist/types/providers/openai-chat-server.d.ts +16 -0
- package/dist/types/providers/openai-codex/constants.d.ts +26 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
- package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
- package/dist/types/providers/openai-codex-responses.d.ts +67 -0
- package/dist/types/providers/openai-completions-compat.d.ts +25 -0
- package/dist/types/providers/openai-completions.d.ts +54 -0
- package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
- package/dist/types/providers/openai-responses-server.d.ts +17 -0
- package/dist/types/providers/openai-responses-shared.d.ts +100 -0
- package/dist/types/providers/openai-responses.d.ts +66 -0
- package/dist/types/providers/register-builtins.d.ts +31 -0
- package/dist/types/providers/synthetic.d.ts +26 -0
- package/dist/{providers → types/providers}/transform-messages.d.ts +6 -2
- package/dist/types/providers/vision-guard.d.ts +8 -0
- package/dist/types/providers/xai-responses.d.ts +23 -0
- package/dist/types/rate-limit-utils.d.ts +19 -0
- package/dist/types/stream.d.ts +28 -0
- package/dist/types/types.d.ts +801 -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 +2 -0
- package/dist/types/usage/kimi.d.ts +2 -0
- package/dist/types/usage/minimax-code.d.ts +2 -0
- package/dist/types/usage/openai-codex.d.ts +3 -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 +260 -0
- package/dist/types/utils/abort.d.ts +19 -0
- package/dist/types/utils/abortable-iterator.d.ts +4 -0
- package/dist/types/utils/anthropic-auth.d.ts +35 -0
- package/dist/types/utils/discovery/antigravity.d.ts +61 -0
- package/dist/types/utils/discovery/codex.d.ts +38 -0
- package/dist/types/utils/discovery/cursor.d.ts +23 -0
- package/dist/types/utils/discovery/gemini.d.ts +25 -0
- package/dist/types/utils/discovery/index.d.ts +4 -0
- package/dist/types/utils/discovery/openai-compatible.d.ts +72 -0
- package/dist/types/utils/event-stream.d.ts +28 -0
- package/dist/types/utils/fireworks-model-id.d.ts +10 -0
- package/dist/types/utils/foundry.d.ts +1 -0
- package/dist/types/utils/http-inspector.d.ts +31 -0
- package/dist/types/utils/idle-iterator.d.ts +78 -0
- package/dist/types/utils/json-parse.d.ts +37 -0
- package/dist/types/utils/oauth/__tests__/xai-oauth.test.d.ts +1 -0
- package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
- package/dist/types/utils/oauth/anthropic.d.ts +22 -0
- package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
- package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
- package/dist/types/utils/oauth/callback-server.d.ts +57 -0
- package/dist/types/utils/oauth/cerebras.d.ts +1 -0
- package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/cursor.d.ts +15 -0
- package/dist/types/utils/oauth/deepseek.d.ts +10 -0
- package/dist/types/utils/oauth/firepass.d.ts +1 -0
- package/dist/types/utils/oauth/fireworks.d.ts +1 -0
- package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
- package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
- package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
- package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
- package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
- package/dist/types/utils/oauth/huggingface.d.ts +19 -0
- package/dist/types/utils/oauth/index.d.ts +38 -0
- package/dist/types/utils/oauth/kagi.d.ts +17 -0
- package/dist/types/utils/oauth/kilo.d.ts +5 -0
- package/dist/types/utils/oauth/kimi.d.ts +21 -0
- package/dist/types/utils/oauth/litellm.d.ts +18 -0
- package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
- package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
- package/dist/types/utils/oauth/moonshot.d.ts +1 -0
- package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
- package/dist/types/utils/oauth/nvidia.d.ts +18 -0
- package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
- package/dist/types/utils/oauth/ollama.d.ts +18 -0
- package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
- package/dist/types/utils/oauth/opencode.d.ts +18 -0
- package/dist/types/utils/oauth/openrouter.d.ts +1 -0
- package/dist/types/utils/oauth/parallel.d.ts +17 -0
- package/dist/types/utils/oauth/perplexity.d.ts +9 -0
- package/dist/{utils → types/utils}/oauth/pkce.d.ts +0 -5
- package/dist/types/utils/oauth/qianfan.d.ts +17 -0
- package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
- package/dist/types/utils/oauth/synthetic.d.ts +1 -0
- package/dist/types/utils/oauth/tavily.d.ts +17 -0
- package/dist/types/utils/oauth/together.d.ts +1 -0
- package/dist/types/utils/oauth/types.d.ts +44 -0
- package/dist/types/utils/oauth/venice.d.ts +18 -0
- package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/vllm.d.ts +16 -0
- package/dist/types/utils/oauth/wafer.d.ts +2 -0
- package/dist/types/utils/oauth/xai-oauth.d.ts +60 -0
- package/dist/types/utils/oauth/xiaomi.d.ts +19 -0
- package/dist/types/utils/oauth/zai.d.ts +18 -0
- package/dist/types/utils/oauth/zenmux.d.ts +1 -0
- package/dist/types/utils/oauth/zhipu.d.ts +18 -0
- package/dist/{utils → types/utils}/overflow.d.ts +9 -11
- package/dist/types/utils/parse-bind.d.ts +23 -0
- package/dist/types/utils/provider-response.d.ts +3 -0
- package/dist/types/utils/request-debug.d.ts +29 -0
- package/dist/types/utils/retry-after.d.ts +3 -0
- package/dist/types/utils/retry.d.ts +26 -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 +49 -0
- package/dist/types/utils/schema/index.d.ts +13 -0
- package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
- package/dist/types/utils/schema/meta-validator.d.ts +2 -0
- package/dist/types/utils/schema/normalize.d.ts +93 -0
- package/dist/types/utils/schema/spill.d.ts +8 -0
- package/dist/types/utils/schema/stamps.d.ts +25 -0
- package/dist/types/utils/schema/types.d.ts +4 -0
- package/dist/types/utils/schema/wire.d.ts +53 -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 +10 -0
- package/dist/types/utils/stream-markup-healing.d.ts +80 -0
- package/dist/types/utils/tool-choice.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +17 -0
- package/dist/types/utils.d.ts +28 -0
- package/package.json +139 -105
- package/src/api-registry.ts +96 -0
- package/src/auth-broker/client.ts +358 -0
- package/src/auth-broker/index.ts +5 -0
- package/src/auth-broker/refresher.ts +117 -0
- package/src/auth-broker/remote-store.ts +623 -0
- package/src/auth-broker/server.ts +644 -0
- package/src/auth-broker/types.ts +127 -0
- package/src/auth-broker/wire-schemas.ts +200 -0
- package/src/auth-gateway/http.ts +194 -0
- package/src/auth-gateway/index.ts +3 -0
- package/src/auth-gateway/server.ts +818 -0
- package/src/auth-gateway/types.ts +143 -0
- package/src/auth-storage.ts +4422 -0
- package/src/index.ts +54 -0
- package/src/model-cache.ts +129 -0
- package/src/model-manager.ts +469 -0
- package/src/model-thinking.ts +782 -0
- package/src/models.json +83530 -0
- package/src/models.json.d.ts +9 -0
- package/src/models.ts +56 -0
- package/src/prompts/turn-aborted-guidance.md +4 -0
- package/src/provider-details.ts +90 -0
- package/src/provider-models/bundled-references.ts +38 -0
- package/src/provider-models/descriptors.ts +355 -0
- package/src/provider-models/google.ts +88 -0
- package/src/provider-models/index.ts +5 -0
- package/src/provider-models/ollama.ts +153 -0
- package/src/provider-models/openai-compat.ts +2817 -0
- package/src/provider-models/special.ts +67 -0
- package/src/providers/aery-native-client.ts +228 -0
- package/src/providers/aery-native-server.ts +212 -0
- package/src/providers/amazon-bedrock.ts +873 -0
- package/src/providers/anthropic-client.ts +318 -0
- package/src/providers/anthropic-messages-server-schema.ts +243 -0
- package/src/providers/anthropic-messages-server.ts +683 -0
- package/src/providers/anthropic-wire.ts +268 -0
- package/src/providers/anthropic.ts +3094 -0
- package/src/providers/aws-credentials.ts +501 -0
- package/src/providers/aws-eventstream.ts +185 -0
- package/src/providers/aws-sigv4.ts +218 -0
- package/src/providers/azure-openai-responses.ts +361 -0
- package/src/providers/cursor/gen/agent_pb.ts +15274 -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 +2621 -0
- package/src/providers/error-message.ts +21 -0
- package/src/providers/github-copilot-headers.ts +140 -0
- package/src/providers/gitlab-duo.ts +372 -0
- package/src/providers/google-auth.ts +252 -0
- package/src/providers/google-gemini-cli.ts +809 -0
- package/src/providers/google-gemini-headers.ts +41 -0
- package/src/providers/google-shared.ts +917 -0
- package/src/providers/google-types.ts +167 -0
- package/src/providers/google-vertex.ts +91 -0
- package/src/providers/google.ts +41 -0
- package/src/providers/grammar.ts +70 -0
- package/src/providers/kimi.ts +52 -0
- package/src/providers/mock.ts +496 -0
- package/src/providers/ollama.ts +644 -0
- package/src/providers/openai-anthropic-shim.ts +138 -0
- package/src/providers/openai-chat-server-schema.ts +252 -0
- package/src/providers/openai-chat-server.ts +647 -0
- package/src/providers/openai-codex/constants.ts +43 -0
- package/src/providers/openai-codex/request-transformer.ts +161 -0
- package/src/providers/openai-codex/response-handler.ts +81 -0
- package/src/providers/openai-codex-responses.ts +3018 -0
- package/src/providers/openai-completions-compat.ts +300 -0
- package/src/providers/openai-completions.ts +1979 -0
- package/src/providers/openai-responses-server-schema.ts +290 -0
- package/src/providers/openai-responses-server.ts +1183 -0
- package/src/providers/openai-responses-shared.ts +873 -0
- package/src/providers/openai-responses.ts +679 -0
- package/src/providers/register-builtins.ts +436 -0
- package/src/providers/synthetic.ts +50 -0
- package/src/providers/transform-messages.ts +382 -0
- package/src/providers/vision-guard.ts +31 -0
- package/src/providers/xai-responses.ts +82 -0
- package/src/rate-limit-utils.ts +84 -0
- package/src/stream.ts +1065 -0
- package/src/types.ts +944 -0
- package/src/usage/claude.ts +482 -0
- package/src/usage/gemini.ts +250 -0
- package/src/usage/github-copilot.ts +421 -0
- package/src/usage/google-antigravity.ts +201 -0
- package/src/usage/kimi.ts +271 -0
- package/src/usage/minimax-code.ts +31 -0
- package/src/usage/openai-codex.ts +503 -0
- package/src/usage/shared.ts +10 -0
- package/src/usage/zai.ts +247 -0
- package/src/usage.ts +185 -0
- package/src/utils/abort.ts +51 -0
- package/src/utils/abortable-iterator.ts +69 -0
- package/src/utils/anthropic-auth.ts +93 -0
- package/src/utils/discovery/antigravity.ts +261 -0
- package/src/utils/discovery/codex.ts +371 -0
- package/src/utils/discovery/cursor.ts +306 -0
- package/src/utils/discovery/gemini.ts +248 -0
- package/src/utils/discovery/index.ts +4 -0
- package/src/utils/discovery/openai-compatible.ts +224 -0
- package/src/utils/event-stream.ts +142 -0
- package/src/utils/fireworks-model-id.ts +30 -0
- package/src/utils/foundry.ts +8 -0
- package/src/utils/http-inspector.ts +176 -0
- package/src/utils/idle-iterator.ts +267 -0
- package/src/utils/json-parse.ts +182 -0
- package/src/utils/oauth/__tests__/xai-oauth.test.ts +107 -0
- package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
- package/src/utils/oauth/anthropic.ts +273 -0
- package/src/utils/oauth/api-key-login.ts +87 -0
- package/src/utils/oauth/api-key-validation.ts +92 -0
- package/src/utils/oauth/callback-server.ts +276 -0
- package/src/utils/oauth/cerebras.ts +16 -0
- package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
- package/src/utils/oauth/cursor.ts +157 -0
- package/src/utils/oauth/deepseek.ts +53 -0
- package/src/utils/oauth/firepass.ts +24 -0
- package/src/utils/oauth/fireworks.ts +15 -0
- package/src/utils/oauth/github-copilot.ts +362 -0
- package/src/utils/oauth/gitlab-duo.ts +123 -0
- package/src/utils/oauth/google-antigravity.ts +200 -0
- package/src/utils/oauth/google-gemini-cli.ts +256 -0
- package/src/utils/oauth/google-oauth-shared.ts +110 -0
- package/src/utils/oauth/huggingface.ts +62 -0
- package/src/utils/oauth/index.ts +484 -0
- package/src/utils/oauth/kagi.ts +47 -0
- package/src/utils/oauth/kilo.ts +87 -0
- package/src/utils/oauth/kimi.ts +254 -0
- package/src/utils/oauth/litellm.ts +47 -0
- package/src/utils/oauth/lm-studio.ts +38 -0
- package/src/utils/oauth/minimax-code.ts +78 -0
- package/src/utils/oauth/moonshot.ts +23 -0
- package/src/utils/oauth/nanogpt.ts +15 -0
- package/src/utils/oauth/nvidia.ts +70 -0
- package/src/utils/oauth/oauth.html +203 -0
- package/src/utils/oauth/ollama-cloud.ts +28 -0
- package/src/utils/oauth/ollama.ts +47 -0
- package/src/utils/oauth/openai-codex.ts +299 -0
- package/src/utils/oauth/opencode.ts +49 -0
- package/src/utils/oauth/openrouter.ts +20 -0
- package/src/utils/oauth/parallel.ts +46 -0
- package/src/utils/oauth/perplexity.ts +206 -0
- package/src/utils/oauth/pkce.ts +18 -0
- package/src/utils/oauth/qianfan.ts +58 -0
- package/src/utils/oauth/qwen-portal.ts +60 -0
- package/src/utils/oauth/synthetic.ts +15 -0
- package/src/utils/oauth/tavily.ts +46 -0
- package/src/utils/oauth/together.ts +16 -0
- package/src/utils/oauth/types.ts +99 -0
- package/src/utils/oauth/venice.ts +59 -0
- package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
- package/src/utils/oauth/vllm.ts +40 -0
- package/src/utils/oauth/wafer.ts +50 -0
- package/src/utils/oauth/xai-oauth.ts +342 -0
- package/src/utils/oauth/xiaomi.ts +139 -0
- package/src/utils/oauth/zai.ts +60 -0
- package/src/utils/oauth/zenmux.ts +15 -0
- package/src/utils/oauth/zhipu.ts +60 -0
- package/src/utils/overflow.ts +137 -0
- package/src/utils/parse-bind.ts +54 -0
- package/src/utils/provider-response.ts +30 -0
- package/src/utils/request-debug.ts +336 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +54 -0
- package/src/utils/schema/CONSTRAINTS.md +164 -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 +191 -0
- package/src/utils/schema/index.ts +13 -0
- package/src/utils/schema/json-schema-validator.ts +577 -0
- package/src/utils/schema/meta-validator.ts +167 -0
- package/src/utils/schema/normalize.ts +1588 -0
- package/src/utils/schema/spill.ts +43 -0
- package/src/utils/schema/stamps.ts +97 -0
- package/src/utils/schema/types.ts +10 -0
- package/src/utils/schema/wire.ts +293 -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 +289 -0
- package/src/utils/stream-markup-healing.ts +612 -0
- package/src/utils/tool-choice.ts +99 -0
- package/src/utils/validation.ts +1024 -0
- package/src/utils.ts +166 -0
- package/dist/api-registry.d.ts +0 -20
- package/dist/api-registry.d.ts.map +0 -1
- package/dist/api-registry.js +0 -44
- package/dist/api-registry.js.map +0 -1
- package/dist/bedrock-provider.d.ts +0 -5
- package/dist/bedrock-provider.d.ts.map +0 -1
- package/dist/bedrock-provider.js +0 -6
- package/dist/bedrock-provider.js.map +0 -1
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -130
- package/dist/cli.js.map +0 -1
- package/dist/env-api-keys.d.ts +0 -18
- package/dist/env-api-keys.d.ts.map +0 -1
- package/dist/env-api-keys.js +0 -178
- package/dist/env-api-keys.js.map +0 -1
- package/dist/image-models.d.ts +0 -10
- package/dist/image-models.d.ts.map +0 -1
- package/dist/image-models.generated.d.ts +0 -440
- package/dist/image-models.generated.d.ts.map +0 -1
- package/dist/image-models.generated.js +0 -442
- package/dist/image-models.generated.js.map +0 -1
- package/dist/image-models.js +0 -23
- package/dist/image-models.js.map +0 -1
- package/dist/images-api-registry.d.ts +0 -14
- package/dist/images-api-registry.d.ts.map +0 -1
- package/dist/images-api-registry.js +0 -22
- package/dist/images-api-registry.js.map +0 -1
- package/dist/images.d.ts +0 -4
- package/dist/images.d.ts.map +0 -1
- package/dist/images.js +0 -14
- package/dist/images.js.map +0 -1
- package/dist/index.d.ts +0 -32
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -20
- package/dist/index.js.map +0 -1
- package/dist/models.d.ts +0 -18
- package/dist/models.d.ts.map +0 -1
- package/dist/models.generated.d.ts +0 -17480
- package/dist/models.generated.d.ts.map +0 -1
- package/dist/models.generated.js +0 -16339
- package/dist/models.generated.js.map +0 -1
- package/dist/models.js +0 -71
- package/dist/models.js.map +0 -1
- package/dist/oauth.d.ts +0 -2
- package/dist/oauth.d.ts.map +0 -1
- package/dist/oauth.js +0 -2
- package/dist/oauth.js.map +0 -1
- package/dist/providers/aery-error-formatting.d.ts +0 -13
- package/dist/providers/aery-error-formatting.d.ts.map +0 -1
- package/dist/providers/aery-error-formatting.js +0 -112
- package/dist/providers/aery-error-formatting.js.map +0 -1
- package/dist/providers/amazon-bedrock.d.ts +0 -38
- package/dist/providers/amazon-bedrock.d.ts.map +0 -1
- package/dist/providers/amazon-bedrock.js +0 -763
- package/dist/providers/amazon-bedrock.js.map +0 -1
- package/dist/providers/anthropic.d.ts +0 -71
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js +0 -949
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/azure-openai-responses.d.ts +0 -15
- package/dist/providers/azure-openai-responses.d.ts.map +0 -1
- package/dist/providers/azure-openai-responses.js +0 -225
- package/dist/providers/azure-openai-responses.js.map +0 -1
- package/dist/providers/cloudflare.d.ts +0 -13
- package/dist/providers/cloudflare.d.ts.map +0 -1
- package/dist/providers/cloudflare.js +0 -26
- package/dist/providers/cloudflare.js.map +0 -1
- package/dist/providers/faux.d.ts +0 -56
- package/dist/providers/faux.d.ts.map +0 -1
- package/dist/providers/faux.js +0 -368
- package/dist/providers/faux.js.map +0 -1
- package/dist/providers/github-copilot-headers.d.ts +0 -8
- package/dist/providers/github-copilot-headers.d.ts.map +0 -1
- package/dist/providers/github-copilot-headers.js +0 -29
- package/dist/providers/github-copilot-headers.js.map +0 -1
- package/dist/providers/google-gemini-cli.d.ts +0 -74
- package/dist/providers/google-gemini-cli.d.ts.map +0 -1
- package/dist/providers/google-gemini-cli.js +0 -779
- package/dist/providers/google-gemini-cli.js.map +0 -1
- package/dist/providers/google-shared.d.ts +0 -70
- package/dist/providers/google-shared.d.ts.map +0 -1
- package/dist/providers/google-shared.js +0 -329
- package/dist/providers/google-shared.js.map +0 -1
- package/dist/providers/google-vertex.d.ts +0 -15
- package/dist/providers/google-vertex.d.ts.map +0 -1
- package/dist/providers/google-vertex.js +0 -442
- package/dist/providers/google-vertex.js.map +0 -1
- package/dist/providers/google.d.ts +0 -13
- package/dist/providers/google.d.ts.map +0 -1
- package/dist/providers/google.js +0 -400
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/images/openrouter.d.ts +0 -3
- package/dist/providers/images/openrouter.d.ts.map +0 -1
- package/dist/providers/images/openrouter.js +0 -129
- package/dist/providers/images/openrouter.js.map +0 -1
- package/dist/providers/images/register-builtins.d.ts +0 -4
- package/dist/providers/images/register-builtins.d.ts.map +0 -1
- package/dist/providers/images/register-builtins.js +0 -34
- package/dist/providers/images/register-builtins.js.map +0 -1
- package/dist/providers/mistral.d.ts +0 -25
- package/dist/providers/mistral.d.ts.map +0 -1
- package/dist/providers/mistral.js +0 -535
- package/dist/providers/mistral.js.map +0 -1
- package/dist/providers/openai-codex-responses.d.ts +0 -30
- package/dist/providers/openai-codex-responses.d.ts.map +0 -1
- package/dist/providers/openai-codex-responses.js +0 -1090
- package/dist/providers/openai-codex-responses.js.map +0 -1
- package/dist/providers/openai-completions.d.ts +0 -19
- package/dist/providers/openai-completions.d.ts.map +0 -1
- package/dist/providers/openai-completions.js +0 -950
- package/dist/providers/openai-completions.js.map +0 -1
- package/dist/providers/openai-prompt-cache.d.ts +0 -3
- package/dist/providers/openai-prompt-cache.d.ts.map +0 -1
- package/dist/providers/openai-prompt-cache.js +0 -10
- package/dist/providers/openai-prompt-cache.js.map +0 -1
- package/dist/providers/openai-responses-shared.d.ts +0 -18
- package/dist/providers/openai-responses-shared.d.ts.map +0 -1
- package/dist/providers/openai-responses-shared.js +0 -492
- package/dist/providers/openai-responses-shared.js.map +0 -1
- package/dist/providers/openai-responses.d.ts +0 -13
- package/dist/providers/openai-responses.d.ts.map +0 -1
- package/dist/providers/openai-responses.js +0 -237
- package/dist/providers/openai-responses.js.map +0 -1
- package/dist/providers/register-builtins.d.ts +0 -38
- package/dist/providers/register-builtins.d.ts.map +0 -1
- package/dist/providers/register-builtins.js +0 -278
- package/dist/providers/register-builtins.js.map +0 -1
- package/dist/providers/simple-options.d.ts +0 -8
- package/dist/providers/simple-options.d.ts.map +0 -1
- package/dist/providers/simple-options.js +0 -41
- package/dist/providers/simple-options.js.map +0 -1
- package/dist/providers/transform-messages.d.ts.map +0 -1
- package/dist/providers/transform-messages.js +0 -184
- package/dist/providers/transform-messages.js.map +0 -1
- package/dist/session-resources.d.ts +0 -4
- package/dist/session-resources.d.ts.map +0 -1
- package/dist/session-resources.js +0 -22
- package/dist/session-resources.js.map +0 -1
- package/dist/stream.d.ts +0 -8
- package/dist/stream.d.ts.map +0 -1
- package/dist/stream.js +0 -27
- package/dist/stream.js.map +0 -1
- package/dist/types.d.ts +0 -498
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/utils/diagnostics.d.ts +0 -19
- package/dist/utils/diagnostics.d.ts.map +0 -1
- package/dist/utils/diagnostics.js +0 -25
- package/dist/utils/diagnostics.js.map +0 -1
- package/dist/utils/event-stream.d.ts +0 -21
- package/dist/utils/event-stream.d.ts.map +0 -1
- package/dist/utils/event-stream.js +0 -81
- package/dist/utils/event-stream.js.map +0 -1
- package/dist/utils/hash.d.ts +0 -3
- package/dist/utils/hash.d.ts.map +0 -1
- package/dist/utils/hash.js +0 -14
- package/dist/utils/hash.js.map +0 -1
- package/dist/utils/headers.d.ts +0 -2
- package/dist/utils/headers.d.ts.map +0 -1
- package/dist/utils/headers.js +0 -8
- package/dist/utils/headers.js.map +0 -1
- package/dist/utils/json-parse.d.ts +0 -16
- package/dist/utils/json-parse.d.ts.map +0 -1
- package/dist/utils/json-parse.js +0 -113
- package/dist/utils/json-parse.js.map +0 -1
- package/dist/utils/node-http-proxy.d.ts +0 -10
- package/dist/utils/node-http-proxy.d.ts.map +0 -1
- package/dist/utils/node-http-proxy.js +0 -97
- package/dist/utils/node-http-proxy.js.map +0 -1
- package/dist/utils/oauth/anthropic.d.ts +0 -25
- package/dist/utils/oauth/anthropic.d.ts.map +0 -1
- package/dist/utils/oauth/anthropic.js +0 -335
- package/dist/utils/oauth/anthropic.js.map +0 -1
- package/dist/utils/oauth/device-code.d.ts +0 -19
- package/dist/utils/oauth/device-code.d.ts.map +0 -1
- package/dist/utils/oauth/device-code.js +0 -55
- package/dist/utils/oauth/device-code.js.map +0 -1
- package/dist/utils/oauth/github-copilot.d.ts +0 -30
- package/dist/utils/oauth/github-copilot.d.ts.map +0 -1
- package/dist/utils/oauth/github-copilot.js +0 -268
- package/dist/utils/oauth/github-copilot.js.map +0 -1
- package/dist/utils/oauth/google-antigravity.d.ts +0 -26
- package/dist/utils/oauth/google-antigravity.d.ts.map +0 -1
- package/dist/utils/oauth/google-antigravity.js +0 -377
- package/dist/utils/oauth/google-antigravity.js.map +0 -1
- package/dist/utils/oauth/google-gemini-cli.d.ts +0 -26
- package/dist/utils/oauth/google-gemini-cli.d.ts.map +0 -1
- package/dist/utils/oauth/google-gemini-cli.js +0 -482
- package/dist/utils/oauth/google-gemini-cli.js.map +0 -1
- package/dist/utils/oauth/index.d.ts +0 -63
- package/dist/utils/oauth/index.d.ts.map +0 -1
- package/dist/utils/oauth/index.js +0 -131
- package/dist/utils/oauth/index.js.map +0 -1
- package/dist/utils/oauth/oauth-page.d.ts +0 -3
- package/dist/utils/oauth/oauth-page.d.ts.map +0 -1
- package/dist/utils/oauth/oauth-page.js +0 -105
- package/dist/utils/oauth/oauth-page.js.map +0 -1
- package/dist/utils/oauth/openai-codex.d.ts +0 -34
- package/dist/utils/oauth/openai-codex.d.ts.map +0 -1
- package/dist/utils/oauth/openai-codex.js +0 -385
- package/dist/utils/oauth/openai-codex.js.map +0 -1
- package/dist/utils/oauth/pkce.d.ts.map +0 -1
- package/dist/utils/oauth/pkce.js +0 -31
- package/dist/utils/oauth/pkce.js.map +0 -1
- package/dist/utils/oauth/types.d.ts +0 -64
- package/dist/utils/oauth/types.d.ts.map +0 -1
- package/dist/utils/oauth/types.js +0 -2
- package/dist/utils/oauth/types.js.map +0 -1
- package/dist/utils/overflow.d.ts.map +0 -1
- package/dist/utils/overflow.js +0 -151
- package/dist/utils/overflow.js.map +0 -1
- package/dist/utils/sanitize-unicode.d.ts +0 -22
- package/dist/utils/sanitize-unicode.d.ts.map +0 -1
- package/dist/utils/sanitize-unicode.js +0 -26
- package/dist/utils/sanitize-unicode.js.map +0 -1
- package/dist/utils/typebox-helpers.d.ts +0 -17
- package/dist/utils/typebox-helpers.d.ts.map +0 -1
- package/dist/utils/typebox-helpers.js +0 -21
- package/dist/utils/typebox-helpers.js.map +0 -1
- package/dist/utils/validation.d.ts +0 -18
- package/dist/utils/validation.d.ts.map +0 -1
- package/dist/utils/validation.js +0 -281
- package/dist/utils/validation.js.map +0 -1
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
import { extractHttpStatusFromError, fetchWithRetry } from "@aryee337/aery-utils";
|
|
2
|
+
import { getEnvApiKey } from "../stream";
|
|
3
|
+
import type {
|
|
4
|
+
Api,
|
|
5
|
+
AssistantMessage,
|
|
6
|
+
Context,
|
|
7
|
+
DeveloperMessage,
|
|
8
|
+
Message,
|
|
9
|
+
Model,
|
|
10
|
+
StreamFunction,
|
|
11
|
+
StreamOptions,
|
|
12
|
+
Tool,
|
|
13
|
+
ToolChoice,
|
|
14
|
+
ToolResultMessage,
|
|
15
|
+
UserMessage,
|
|
16
|
+
} from "../types";
|
|
17
|
+
import { normalizeSystemPrompts } from "../utils";
|
|
18
|
+
import { AssistantMessageEventStream } from "../utils/event-stream";
|
|
19
|
+
import { finalizeErrorMessage, type RawHttpRequestDump } from "../utils/http-inspector";
|
|
20
|
+
import { parseStreamingJson } from "../utils/json-parse";
|
|
21
|
+
import { toolWireSchema } from "../utils/schema/wire";
|
|
22
|
+
import {
|
|
23
|
+
getStreamMarkupHealingPattern,
|
|
24
|
+
type HealedToolCall,
|
|
25
|
+
StreamMarkupHealing,
|
|
26
|
+
type StreamMarkupHealingEvent,
|
|
27
|
+
} from "../utils/stream-markup-healing";
|
|
28
|
+
import { transformMessages } from "./transform-messages";
|
|
29
|
+
|
|
30
|
+
export interface OllamaChatOptions extends StreamOptions {
|
|
31
|
+
reasoning?: "minimal" | "low" | "medium" | "high" | "xhigh";
|
|
32
|
+
toolChoice?: ToolChoice;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type OllamaFunctionTool = {
|
|
36
|
+
type: "function";
|
|
37
|
+
function: {
|
|
38
|
+
name: string;
|
|
39
|
+
description: string;
|
|
40
|
+
parameters: Record<string, unknown>;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type OllamaMessage = {
|
|
45
|
+
role: "system" | "user" | "assistant" | "tool";
|
|
46
|
+
content: string;
|
|
47
|
+
images?: string[];
|
|
48
|
+
thinking?: string;
|
|
49
|
+
tool_calls?: Array<{
|
|
50
|
+
type: "function";
|
|
51
|
+
function: {
|
|
52
|
+
index?: number;
|
|
53
|
+
name: string;
|
|
54
|
+
arguments: Record<string, unknown>;
|
|
55
|
+
};
|
|
56
|
+
}>;
|
|
57
|
+
tool_name?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type OllamaChatChunk = {
|
|
61
|
+
message?: {
|
|
62
|
+
role?: string;
|
|
63
|
+
content?: string;
|
|
64
|
+
thinking?: string;
|
|
65
|
+
tool_calls?: Array<{
|
|
66
|
+
type?: string;
|
|
67
|
+
function?: {
|
|
68
|
+
index?: number;
|
|
69
|
+
name?: string;
|
|
70
|
+
arguments?: Record<string, unknown> | string;
|
|
71
|
+
};
|
|
72
|
+
}>;
|
|
73
|
+
};
|
|
74
|
+
done?: boolean;
|
|
75
|
+
done_reason?: string;
|
|
76
|
+
prompt_eval_count?: number;
|
|
77
|
+
eval_count?: number;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
type InternalToolCallBlock = AssistantMessage["content"][number] & {
|
|
81
|
+
type: "toolCall";
|
|
82
|
+
partialJson?: string;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
function normalizeBaseUrl(baseUrl?: string): string {
|
|
86
|
+
const value = baseUrl?.trim();
|
|
87
|
+
if (!value) {
|
|
88
|
+
return "https://ollama.com";
|
|
89
|
+
}
|
|
90
|
+
const trimmed = value.endsWith("/") ? value.slice(0, -1) : value;
|
|
91
|
+
return trimmed.endsWith("/api") ? trimmed.slice(0, -4) : trimmed;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function mapReasoning(reasoning: OllamaChatOptions["reasoning"]): boolean | "low" | "medium" | "high" | undefined {
|
|
95
|
+
switch (reasoning) {
|
|
96
|
+
case "minimal":
|
|
97
|
+
case "low":
|
|
98
|
+
return "low";
|
|
99
|
+
case "medium":
|
|
100
|
+
return "medium";
|
|
101
|
+
case "high":
|
|
102
|
+
case "xhigh":
|
|
103
|
+
return "high";
|
|
104
|
+
default:
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function mapToolChoice(toolChoice: ToolChoice | undefined): "auto" | "none" | "required" | undefined {
|
|
110
|
+
if (!toolChoice || toolChoice === "auto") {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
if (toolChoice === "none") {
|
|
114
|
+
return "none";
|
|
115
|
+
}
|
|
116
|
+
if (toolChoice === "required" || toolChoice === "any") {
|
|
117
|
+
return "required";
|
|
118
|
+
}
|
|
119
|
+
if (typeof toolChoice === "object") {
|
|
120
|
+
return "required";
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function getNamedToolChoiceName(toolChoice: ToolChoice | undefined): string | undefined {
|
|
126
|
+
if (!toolChoice || typeof toolChoice === "string") {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
if ("function" in toolChoice) {
|
|
130
|
+
return toolChoice.function.name;
|
|
131
|
+
}
|
|
132
|
+
return toolChoice.name;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function selectToolsForToolChoice(tools: Tool[] | undefined, toolChoice: ToolChoice | undefined): Tool[] | undefined {
|
|
136
|
+
const toolName = getNamedToolChoiceName(toolChoice);
|
|
137
|
+
if (!toolName || !tools) {
|
|
138
|
+
return tools;
|
|
139
|
+
}
|
|
140
|
+
for (const tool of tools) {
|
|
141
|
+
if (tool.name === toolName) {
|
|
142
|
+
return [tool];
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function toPlainContent(content: string | Array<{ type: "text" | "image"; text?: string; data?: string }>): {
|
|
149
|
+
content: string;
|
|
150
|
+
images?: string[];
|
|
151
|
+
} {
|
|
152
|
+
if (typeof content === "string") {
|
|
153
|
+
return { content };
|
|
154
|
+
}
|
|
155
|
+
const textParts: string[] = [];
|
|
156
|
+
const images: string[] = [];
|
|
157
|
+
for (const block of content) {
|
|
158
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
159
|
+
textParts.push(block.text);
|
|
160
|
+
}
|
|
161
|
+
if (block.type === "image" && typeof block.data === "string") {
|
|
162
|
+
images.push(block.data);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
content: textParts.join("\n"),
|
|
167
|
+
...(images.length > 0 ? { images } : {}),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function convertMessage(message: Message): OllamaMessage {
|
|
172
|
+
if (message.role === "user") {
|
|
173
|
+
const converted = toPlainContent(message.content as UserMessage["content"]);
|
|
174
|
+
return { role: "user", ...converted };
|
|
175
|
+
}
|
|
176
|
+
if (message.role === "developer") {
|
|
177
|
+
const converted = toPlainContent(message.content as DeveloperMessage["content"]);
|
|
178
|
+
return { role: "system", ...converted };
|
|
179
|
+
}
|
|
180
|
+
if (message.role === "toolResult") {
|
|
181
|
+
const converted = toPlainContent(message.content as ToolResultMessage["content"]);
|
|
182
|
+
return {
|
|
183
|
+
role: "tool",
|
|
184
|
+
tool_name: message.toolName,
|
|
185
|
+
...converted,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const text: string[] = [];
|
|
189
|
+
const thinking: string[] = [];
|
|
190
|
+
const toolCalls: NonNullable<OllamaMessage["tool_calls"]> = [];
|
|
191
|
+
for (const block of message.content) {
|
|
192
|
+
if (block.type === "text") {
|
|
193
|
+
text.push(block.text);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (block.type === "thinking") {
|
|
197
|
+
thinking.push(block.thinking);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (block.type === "toolCall") {
|
|
201
|
+
toolCalls.push({
|
|
202
|
+
type: "function",
|
|
203
|
+
function: {
|
|
204
|
+
name: block.name,
|
|
205
|
+
arguments: block.arguments,
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
role: "assistant",
|
|
212
|
+
content: text.join("\n"),
|
|
213
|
+
...(thinking.length > 0 ? { thinking: thinking.join("\n") } : {}),
|
|
214
|
+
...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function convertMessages(model: Model<"ollama-chat">, context: Context): OllamaMessage[] {
|
|
219
|
+
const messages: Message[] = [];
|
|
220
|
+
// Emit one developer message per ordered system prompt. The wire role is mapped to "system"
|
|
221
|
+
// by `convertMessage`, but keeping the prompts separate preserves prefix-cache stability:
|
|
222
|
+
// if only the trailing prompt changes between calls, the leading system messages keep
|
|
223
|
+
// their identical token prefix so KV-cache reuse covers them.
|
|
224
|
+
for (const systemPrompt of normalizeSystemPrompts(context.systemPrompt)) {
|
|
225
|
+
messages.push({
|
|
226
|
+
role: "developer",
|
|
227
|
+
content: systemPrompt,
|
|
228
|
+
timestamp: Date.now(),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
messages.push(...context.messages);
|
|
232
|
+
const isCloud = model.provider === "ollama-cloud";
|
|
233
|
+
return transformMessages(messages, model).map(msg => {
|
|
234
|
+
const converted = convertMessage(msg);
|
|
235
|
+
// Ollama cloud rejects requests when assistant history messages contain the `thinking`
|
|
236
|
+
// field — it's valid in model responses but not accepted as a history input. Strip it
|
|
237
|
+
// to prevent HTTP 400 errors. Local Ollama instances are unaffected.
|
|
238
|
+
if (isCloud && converted.role === "assistant" && converted.thinking) {
|
|
239
|
+
const { thinking: _t, ...rest } = converted;
|
|
240
|
+
return rest;
|
|
241
|
+
}
|
|
242
|
+
return converted;
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function convertTools(tools: Tool[] | undefined): OllamaFunctionTool[] | undefined {
|
|
247
|
+
if (!tools || tools.length === 0) {
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
return tools.map(tool => ({
|
|
251
|
+
type: "function",
|
|
252
|
+
function: {
|
|
253
|
+
name: tool.name,
|
|
254
|
+
description: tool.description,
|
|
255
|
+
parameters: toolWireSchema(tool),
|
|
256
|
+
},
|
|
257
|
+
}));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function createChatBody(model: Model<"ollama-chat">, context: Context, options: OllamaChatOptions | undefined) {
|
|
261
|
+
const think = mapReasoning(options?.reasoning);
|
|
262
|
+
const toolChoice = mapToolChoice(options?.toolChoice);
|
|
263
|
+
const selectedTools = selectToolsForToolChoice(context.tools, options?.toolChoice);
|
|
264
|
+
const tools = convertTools(selectedTools);
|
|
265
|
+
return {
|
|
266
|
+
model: model.id,
|
|
267
|
+
messages: convertMessages(model, context),
|
|
268
|
+
...(tools ? { tools } : {}),
|
|
269
|
+
...(think !== undefined ? { think } : {}),
|
|
270
|
+
...(toolChoice !== undefined ? { tool_choice: toolChoice } : {}),
|
|
271
|
+
...(options?.maxTokens !== undefined ? { options: { num_predict: options.maxTokens } } : {}),
|
|
272
|
+
stream: true,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
async function* iterateNdjson(stream: ReadableStream<Uint8Array>): AsyncGenerator<OllamaChatChunk> {
|
|
277
|
+
const reader = stream.getReader();
|
|
278
|
+
const decoder = new TextDecoder();
|
|
279
|
+
let buffer = "";
|
|
280
|
+
while (true) {
|
|
281
|
+
const { done, value } = await reader.read();
|
|
282
|
+
if (done) {
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
buffer += decoder.decode(value, { stream: true });
|
|
286
|
+
while (true) {
|
|
287
|
+
const newlineIndex = buffer.indexOf("\n");
|
|
288
|
+
if (newlineIndex < 0) {
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
const line = buffer.slice(0, newlineIndex).trim();
|
|
292
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
293
|
+
if (!line) {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
yield JSON.parse(line) as OllamaChatChunk;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
buffer += decoder.decode();
|
|
300
|
+
const tail = buffer.trim();
|
|
301
|
+
if (tail) {
|
|
302
|
+
yield JSON.parse(tail) as OllamaChatChunk;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function createEmptyOutput(model: Model<"ollama-chat">): AssistantMessage {
|
|
307
|
+
return {
|
|
308
|
+
role: "assistant",
|
|
309
|
+
content: [],
|
|
310
|
+
api: "ollama-chat" as Api,
|
|
311
|
+
provider: model.provider,
|
|
312
|
+
model: model.id,
|
|
313
|
+
usage: {
|
|
314
|
+
input: 0,
|
|
315
|
+
output: 0,
|
|
316
|
+
cacheRead: 0,
|
|
317
|
+
cacheWrite: 0,
|
|
318
|
+
totalTokens: 0,
|
|
319
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
320
|
+
},
|
|
321
|
+
stopReason: "stop",
|
|
322
|
+
timestamp: Date.now(),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function endThinkingBlock(stream: AssistantMessageEventStream, output: AssistantMessage, index: number): void {
|
|
327
|
+
const block = output.content[index];
|
|
328
|
+
if (block?.type === "thinking") {
|
|
329
|
+
stream.push({ type: "thinking_end", contentIndex: index, content: block.thinking, partial: output });
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function endTextBlock(stream: AssistantMessageEventStream, output: AssistantMessage, index: number): void {
|
|
334
|
+
const block = output.content[index];
|
|
335
|
+
if (block?.type === "text") {
|
|
336
|
+
stream.push({ type: "text_end", contentIndex: index, content: block.text, partial: output });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function endToolCallBlock(stream: AssistantMessageEventStream, output: AssistantMessage, index: number): void {
|
|
341
|
+
const block = output.content[index];
|
|
342
|
+
if (block?.type !== "toolCall") {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const toolCall = block as InternalToolCallBlock;
|
|
346
|
+
if (toolCall.partialJson) {
|
|
347
|
+
toolCall.arguments = parseStreamingJson<Record<string, unknown>>(toolCall.partialJson);
|
|
348
|
+
delete toolCall.partialJson;
|
|
349
|
+
}
|
|
350
|
+
stream.push({ type: "toolcall_end", contentIndex: index, toolCall, partial: output });
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function mapDoneReason(doneReason: string | undefined, output: AssistantMessage): AssistantMessage["stopReason"] {
|
|
354
|
+
if (doneReason === "length") {
|
|
355
|
+
return "length";
|
|
356
|
+
}
|
|
357
|
+
if (doneReason === "tool_calls") {
|
|
358
|
+
return "toolUse";
|
|
359
|
+
}
|
|
360
|
+
if (doneReason === undefined && output.content.some(block => block.type === "toolCall")) {
|
|
361
|
+
return "toolUse";
|
|
362
|
+
}
|
|
363
|
+
return "stop";
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const OLLAMA_RETRY_DELAYS_MS = [2_000, 5_000, 10_000];
|
|
367
|
+
|
|
368
|
+
export const streamOllama: StreamFunction<"ollama-chat"> = (
|
|
369
|
+
model: Model<"ollama-chat">,
|
|
370
|
+
context: Context,
|
|
371
|
+
options: OllamaChatOptions,
|
|
372
|
+
): AssistantMessageEventStream => {
|
|
373
|
+
const stream = new AssistantMessageEventStream();
|
|
374
|
+
void (async () => {
|
|
375
|
+
const startTime = Date.now();
|
|
376
|
+
let firstTokenTime: number | undefined;
|
|
377
|
+
const output = createEmptyOutput(model);
|
|
378
|
+
let rawRequestDump: RawHttpRequestDump | undefined;
|
|
379
|
+
let activeThinkingIndex: number | undefined;
|
|
380
|
+
let activeTextIndex: number | undefined;
|
|
381
|
+
const activeToolIndices = new Set<number>();
|
|
382
|
+
const streamMarkupHealingPattern = getStreamMarkupHealingPattern(model.provider, model.id);
|
|
383
|
+
const streamMarkupHealing = streamMarkupHealingPattern
|
|
384
|
+
? new StreamMarkupHealing({ pattern: streamMarkupHealingPattern })
|
|
385
|
+
: undefined;
|
|
386
|
+
let healedToolCallEmitted = false;
|
|
387
|
+
const endActiveTextBlock = (): void => {
|
|
388
|
+
if (activeTextIndex === undefined) return;
|
|
389
|
+
endTextBlock(stream, output, activeTextIndex);
|
|
390
|
+
activeTextIndex = undefined;
|
|
391
|
+
};
|
|
392
|
+
const endActiveThinkingBlock = (): void => {
|
|
393
|
+
if (activeThinkingIndex === undefined) return;
|
|
394
|
+
endThinkingBlock(stream, output, activeThinkingIndex);
|
|
395
|
+
activeThinkingIndex = undefined;
|
|
396
|
+
};
|
|
397
|
+
const appendVisibleText = (text: string): void => {
|
|
398
|
+
if (text.length === 0) return;
|
|
399
|
+
endActiveThinkingBlock();
|
|
400
|
+
if (activeTextIndex === undefined) {
|
|
401
|
+
output.content.push({ type: "text", text: "" });
|
|
402
|
+
activeTextIndex = output.content.length - 1;
|
|
403
|
+
stream.push({ type: "text_start", contentIndex: activeTextIndex, partial: output });
|
|
404
|
+
}
|
|
405
|
+
const block = output.content[activeTextIndex];
|
|
406
|
+
if (block?.type === "text") {
|
|
407
|
+
block.text += text;
|
|
408
|
+
stream.push({
|
|
409
|
+
type: "text_delta",
|
|
410
|
+
contentIndex: activeTextIndex,
|
|
411
|
+
delta: text,
|
|
412
|
+
partial: output,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
if (!firstTokenTime) firstTokenTime = Date.now();
|
|
416
|
+
};
|
|
417
|
+
const appendVisibleThinking = (thinking: string): void => {
|
|
418
|
+
if (thinking.length === 0) return;
|
|
419
|
+
endActiveTextBlock();
|
|
420
|
+
if (activeThinkingIndex === undefined) {
|
|
421
|
+
output.content.push({ type: "thinking", thinking: "" });
|
|
422
|
+
activeThinkingIndex = output.content.length - 1;
|
|
423
|
+
stream.push({ type: "thinking_start", contentIndex: activeThinkingIndex, partial: output });
|
|
424
|
+
}
|
|
425
|
+
const block = output.content[activeThinkingIndex];
|
|
426
|
+
if (block?.type === "thinking") {
|
|
427
|
+
block.thinking += thinking;
|
|
428
|
+
stream.push({
|
|
429
|
+
type: "thinking_delta",
|
|
430
|
+
contentIndex: activeThinkingIndex,
|
|
431
|
+
delta: thinking,
|
|
432
|
+
partial: output,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
if (!firstTokenTime) firstTokenTime = Date.now();
|
|
436
|
+
};
|
|
437
|
+
const emitHealedToolCall = (call: HealedToolCall): void => {
|
|
438
|
+
endActiveThinkingBlock();
|
|
439
|
+
endActiveTextBlock();
|
|
440
|
+
const toolCall: InternalToolCallBlock = {
|
|
441
|
+
type: "toolCall",
|
|
442
|
+
id: call.id,
|
|
443
|
+
name: call.name,
|
|
444
|
+
arguments: parseStreamingJson<Record<string, unknown>>(call.arguments),
|
|
445
|
+
partialJson: call.arguments,
|
|
446
|
+
};
|
|
447
|
+
output.content.push(toolCall);
|
|
448
|
+
const index = output.content.length - 1;
|
|
449
|
+
stream.push({ type: "toolcall_start", contentIndex: index, partial: output });
|
|
450
|
+
stream.push({
|
|
451
|
+
type: "toolcall_delta",
|
|
452
|
+
contentIndex: index,
|
|
453
|
+
delta: call.arguments,
|
|
454
|
+
partial: output,
|
|
455
|
+
});
|
|
456
|
+
endToolCallBlock(stream, output, index);
|
|
457
|
+
healedToolCallEmitted = true;
|
|
458
|
+
if (!firstTokenTime) firstTokenTime = Date.now();
|
|
459
|
+
};
|
|
460
|
+
const emitHealingEvent = (event: StreamMarkupHealingEvent): void => {
|
|
461
|
+
if (event.type === "text") {
|
|
462
|
+
appendVisibleText(event.text);
|
|
463
|
+
} else if (event.type === "thinking") {
|
|
464
|
+
appendVisibleThinking(event.thinking);
|
|
465
|
+
} else {
|
|
466
|
+
emitHealedToolCall(event.call);
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
const drainHealedToolCalls = (): void => {
|
|
470
|
+
if (!streamMarkupHealing) return;
|
|
471
|
+
for (const call of streamMarkupHealing.drainCompleted()) emitHealedToolCall(call);
|
|
472
|
+
};
|
|
473
|
+
try {
|
|
474
|
+
const apiKey = options.apiKey || getEnvApiKey(model.provider);
|
|
475
|
+
if (!apiKey) {
|
|
476
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
477
|
+
}
|
|
478
|
+
const baseUrl = normalizeBaseUrl(model.baseUrl);
|
|
479
|
+
let body = createChatBody(model, context, options);
|
|
480
|
+
const replacementPayload = await options.onPayload?.(body, model);
|
|
481
|
+
if (replacementPayload !== undefined) {
|
|
482
|
+
body = replacementPayload as typeof body;
|
|
483
|
+
}
|
|
484
|
+
rawRequestDump = {
|
|
485
|
+
provider: model.provider,
|
|
486
|
+
api: model.api,
|
|
487
|
+
model: model.id,
|
|
488
|
+
method: "POST",
|
|
489
|
+
url: `${baseUrl}/api/chat`,
|
|
490
|
+
body,
|
|
491
|
+
};
|
|
492
|
+
const response = await fetchWithRetry(`${baseUrl}/api/chat`, {
|
|
493
|
+
method: "POST",
|
|
494
|
+
headers: {
|
|
495
|
+
...model.headers,
|
|
496
|
+
...options.headers,
|
|
497
|
+
Authorization: `Bearer ${apiKey}`,
|
|
498
|
+
"Content-Type": "application/json",
|
|
499
|
+
},
|
|
500
|
+
body: JSON.stringify(body),
|
|
501
|
+
signal: options.signal,
|
|
502
|
+
defaultDelayMs: OLLAMA_RETRY_DELAYS_MS,
|
|
503
|
+
fetch: options.fetch,
|
|
504
|
+
});
|
|
505
|
+
if (!response.ok) {
|
|
506
|
+
throw new Error(`HTTP ${response.status} from ${baseUrl}/api/chat`);
|
|
507
|
+
}
|
|
508
|
+
if (!response.body) {
|
|
509
|
+
throw new Error("Ollama returned an empty response body");
|
|
510
|
+
}
|
|
511
|
+
stream.push({ type: "start", partial: output });
|
|
512
|
+
for await (const chunk of iterateNdjson(response.body)) {
|
|
513
|
+
if (chunk.message?.thinking) {
|
|
514
|
+
endActiveTextBlock();
|
|
515
|
+
if (activeThinkingIndex === undefined) {
|
|
516
|
+
output.content.push({ type: "thinking", thinking: "" });
|
|
517
|
+
activeThinkingIndex = output.content.length - 1;
|
|
518
|
+
stream.push({ type: "thinking_start", contentIndex: activeThinkingIndex, partial: output });
|
|
519
|
+
}
|
|
520
|
+
const block = output.content[activeThinkingIndex];
|
|
521
|
+
if (block?.type === "thinking") {
|
|
522
|
+
block.thinking += chunk.message.thinking;
|
|
523
|
+
stream.push({
|
|
524
|
+
type: "thinking_delta",
|
|
525
|
+
contentIndex: activeThinkingIndex,
|
|
526
|
+
delta: chunk.message.thinking,
|
|
527
|
+
partial: output,
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
if (!firstTokenTime) {
|
|
531
|
+
firstTokenTime = Date.now();
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
const chunkContent = chunk.message?.content;
|
|
535
|
+
const structuredCalls = chunk.message?.tool_calls?.length ? chunk.message.tool_calls : undefined;
|
|
536
|
+
if (chunkContent) {
|
|
537
|
+
if (streamMarkupHealing) {
|
|
538
|
+
if (structuredCalls) {
|
|
539
|
+
appendVisibleText(streamMarkupHealing.consumeWithoutCalls(chunkContent));
|
|
540
|
+
} else {
|
|
541
|
+
for (const event of streamMarkupHealing.feedEvents(chunkContent)) {
|
|
542
|
+
emitHealingEvent(event);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
} else {
|
|
546
|
+
appendVisibleText(chunkContent);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (structuredCalls) {
|
|
550
|
+
endActiveThinkingBlock();
|
|
551
|
+
endActiveTextBlock();
|
|
552
|
+
for (const call of structuredCalls) {
|
|
553
|
+
const name = call.function?.name ?? "unknown_tool";
|
|
554
|
+
const rawArgs = call.function?.arguments;
|
|
555
|
+
const partialJson = typeof rawArgs === "string" ? rawArgs : JSON.stringify(rawArgs ?? {});
|
|
556
|
+
const toolCall: InternalToolCallBlock = {
|
|
557
|
+
type: "toolCall",
|
|
558
|
+
id: `ollama:${output.content.length}:${name}`,
|
|
559
|
+
name,
|
|
560
|
+
arguments: parseStreamingJson<Record<string, unknown>>(partialJson),
|
|
561
|
+
partialJson,
|
|
562
|
+
};
|
|
563
|
+
output.content.push(toolCall);
|
|
564
|
+
const index = output.content.length - 1;
|
|
565
|
+
activeToolIndices.add(index);
|
|
566
|
+
stream.push({ type: "toolcall_start", contentIndex: index, partial: output });
|
|
567
|
+
stream.push({
|
|
568
|
+
type: "toolcall_delta",
|
|
569
|
+
contentIndex: index,
|
|
570
|
+
delta: partialJson,
|
|
571
|
+
partial: output,
|
|
572
|
+
});
|
|
573
|
+
if (!firstTokenTime) {
|
|
574
|
+
firstTokenTime = Date.now();
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
if (chunk.done) {
|
|
579
|
+
if (streamMarkupHealing) {
|
|
580
|
+
for (const event of streamMarkupHealing.flushEvents()) {
|
|
581
|
+
emitHealingEvent(event);
|
|
582
|
+
}
|
|
583
|
+
drainHealedToolCalls();
|
|
584
|
+
}
|
|
585
|
+
endActiveThinkingBlock();
|
|
586
|
+
endActiveTextBlock();
|
|
587
|
+
for (const index of activeToolIndices) {
|
|
588
|
+
endToolCallBlock(stream, output, index);
|
|
589
|
+
}
|
|
590
|
+
activeToolIndices.clear();
|
|
591
|
+
output.stopReason = mapDoneReason(chunk.done_reason, output);
|
|
592
|
+
if (healedToolCallEmitted && output.stopReason === "stop") {
|
|
593
|
+
output.stopReason = "toolUse";
|
|
594
|
+
}
|
|
595
|
+
output.usage.input = chunk.prompt_eval_count ?? 0;
|
|
596
|
+
output.usage.output = chunk.eval_count ?? 0;
|
|
597
|
+
output.usage.totalTokens = output.usage.input + output.usage.output;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
if (streamMarkupHealing) {
|
|
601
|
+
for (const event of streamMarkupHealing.flushEvents()) {
|
|
602
|
+
emitHealingEvent(event);
|
|
603
|
+
}
|
|
604
|
+
drainHealedToolCalls();
|
|
605
|
+
if (healedToolCallEmitted && output.stopReason === "stop") {
|
|
606
|
+
output.stopReason = "toolUse";
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
endActiveThinkingBlock();
|
|
610
|
+
endActiveTextBlock();
|
|
611
|
+
// Tool calls always mean "execute and continue" in the OpenAI/Ollama contract.
|
|
612
|
+
// If the turn produced tool-call blocks but reported a natural `stop`, promote
|
|
613
|
+
// to `toolUse` so the agent loop runs them (it gates execution on the stop
|
|
614
|
+
// reason). `length`/`aborted`/`error` are intentionally left untouched.
|
|
615
|
+
if (output.stopReason === "stop" && output.content.some(block => block.type === "toolCall")) {
|
|
616
|
+
output.stopReason = "toolUse";
|
|
617
|
+
}
|
|
618
|
+
output.duration = Date.now() - startTime;
|
|
619
|
+
if (firstTokenTime) {
|
|
620
|
+
output.ttft = firstTokenTime - startTime;
|
|
621
|
+
}
|
|
622
|
+
const doneReason =
|
|
623
|
+
output.stopReason === "length" ? "length" : output.stopReason === "toolUse" ? "toolUse" : "stop";
|
|
624
|
+
stream.push({ type: "done", reason: doneReason, message: output });
|
|
625
|
+
stream.end();
|
|
626
|
+
} catch (error) {
|
|
627
|
+
for (const block of output.content) {
|
|
628
|
+
if (block.type === "toolCall") {
|
|
629
|
+
delete (block as InternalToolCallBlock).partialJson;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
output.stopReason = options.signal?.aborted ? "aborted" : "error";
|
|
633
|
+
output.errorStatus = extractHttpStatusFromError(error);
|
|
634
|
+
output.errorMessage = await finalizeErrorMessage(error, rawRequestDump);
|
|
635
|
+
output.duration = Date.now() - startTime;
|
|
636
|
+
if (firstTokenTime) {
|
|
637
|
+
output.ttft = firstTokenTime - startTime;
|
|
638
|
+
}
|
|
639
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
640
|
+
stream.end();
|
|
641
|
+
}
|
|
642
|
+
})();
|
|
643
|
+
return stream;
|
|
644
|
+
};
|