@zyro-sec/ai 0.0.0-stage → 1.0.1
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/AGENTS.md +23 -0
- package/README.md +1810 -2
- package/example/tutorial.ts +284 -0
- package/package.json +40 -4
- package/script/pack.ts +5 -0
- package/script/recording-cost-report.ts +250 -0
- package/script/setup-recording-env.ts +602 -0
- package/src/ai-client.ts +28 -0
- package/src/cache-policy.ts +249 -0
- package/src/decision-client.ts +88 -0
- package/src/decision.ts +335 -0
- package/src/effect.ts +44 -0
- package/src/effort-updates.ts +25 -0
- package/src/embedding-client.ts +46 -0
- package/src/embedding.ts +167 -0
- package/src/experimental/cloudflare-run-extraction.ts +112 -0
- package/src/experimental/extraction.ts +190 -0
- package/src/experimental/realtime.ts +211 -0
- package/src/experimental.ts +51 -0
- package/src/generation.ts +210 -0
- package/src/image-client.ts +89 -0
- package/src/image.ts +236 -0
- package/src/index.ts +363 -0
- package/src/llm.ts +364 -0
- package/src/loop.ts +214 -0
- package/src/media-client.ts +94 -0
- package/src/media-model.ts +129 -0
- package/src/media.ts +408 -0
- package/src/protocols/alibaba-chat.ts +95 -0
- package/src/protocols/alibaba-images.ts +126 -0
- package/src/protocols/alibaba-messages.ts +49 -0
- package/src/protocols/alibaba-responses.ts +86 -0
- package/src/protocols/anthropic-messages.ts +1664 -0
- package/src/protocols/assemblyai-transcription.ts +209 -0
- package/src/protocols/bedrock-converse.ts +984 -0
- package/src/protocols/bedrock-event-stream.ts +147 -0
- package/src/protocols/cartesia-speech.ts +182 -0
- package/src/protocols/cloudflare-run-decisions.ts +55 -0
- package/src/protocols/cloudflare-workers-ai-images.ts +124 -0
- package/src/protocols/cohere-chat.ts +353 -0
- package/src/protocols/deepgram-speech.ts +148 -0
- package/src/protocols/deepgram-transcription.ts +179 -0
- package/src/protocols/elevenlabs-speech.ts +207 -0
- package/src/protocols/elevenlabs-transcription.ts +211 -0
- package/src/protocols/gemini.ts +908 -0
- package/src/protocols/google-decisions.ts +179 -0
- package/src/protocols/google-embed-content.ts +165 -0
- package/src/protocols/google-images.ts +255 -0
- package/src/protocols/google-interactions-video.ts +264 -0
- package/src/protocols/google-interactions.ts +904 -0
- package/src/protocols/google-speech.ts +157 -0
- package/src/protocols/google-transcription.ts +206 -0
- package/src/protocols/google-vertex-predict-embeddings.ts +104 -0
- package/src/protocols/index.ts +21 -0
- package/src/protocols/meta-images.ts +162 -0
- package/src/protocols/meta-messages.ts +67 -0
- package/src/protocols/meta-responses.ts +249 -0
- package/src/protocols/minimax-images.ts +215 -0
- package/src/protocols/minimax-video.ts +406 -0
- package/src/protocols/mistral-chat.ts +768 -0
- package/src/protocols/open-responses-channel.ts +198 -0
- package/src/protocols/open-responses-continuation.ts +218 -0
- package/src/protocols/open-responses.ts +1636 -0
- package/src/protocols/openai-chat.ts +1431 -0
- package/src/protocols/openai-compatible-chat.ts +22 -0
- package/src/protocols/openai-compatible-responses.ts +21 -0
- package/src/protocols/openai-decisions.ts +174 -0
- package/src/protocols/openai-embeddings.ts +105 -0
- package/src/protocols/openai-freeform.ts +287 -0
- package/src/protocols/openai-images.ts +442 -0
- package/src/protocols/openai-responses.ts +442 -0
- package/src/protocols/openai-speech.ts +164 -0
- package/src/protocols/openai-transcription.ts +295 -0
- package/src/protocols/openrouter-video.ts +215 -0
- package/src/protocols/seedance-video.ts +301 -0
- package/src/protocols/shared.ts +552 -0
- package/src/protocols/system-one.ts +335 -0
- package/src/protocols/utils/bedrock-auth.ts +147 -0
- package/src/protocols/utils/bedrock-cache.ts +56 -0
- package/src/protocols/utils/bedrock-media.ts +121 -0
- package/src/protocols/utils/cache.ts +44 -0
- package/src/protocols/utils/claude-model.ts +19 -0
- package/src/protocols/utils/gateway-protocol.ts +92 -0
- package/src/protocols/utils/gemini-generate-content.ts +172 -0
- package/src/protocols/utils/gemini-json-schema.ts +77 -0
- package/src/protocols/utils/image-sizing.ts +117 -0
- package/src/protocols/utils/lifecycle.ts +120 -0
- package/src/protocols/utils/media-input.ts +98 -0
- package/src/protocols/utils/mistral-tool-id.ts +34 -0
- package/src/protocols/utils/open-responses-options.ts +82 -0
- package/src/protocols/utils/partial-json-options.ts +62 -0
- package/src/protocols/utils/partial-json.ts +283 -0
- package/src/protocols/utils/responses-checkpoint.ts +115 -0
- package/src/protocols/utils/responses-compaction.ts +210 -0
- package/src/protocols/utils/responses-hosted-tools.ts +104 -0
- package/src/protocols/utils/speaker-turns.ts +10 -0
- package/src/protocols/utils/speech-stream.ts +111 -0
- package/src/protocols/utils/tool-schema.ts +115 -0
- package/src/protocols/utils/tool-stream.ts +240 -0
- package/src/protocols/venice-chat.ts +356 -0
- package/src/protocols/xai-images.ts +137 -0
- package/src/protocols/xai-responses.ts +106 -0
- package/src/protocols/xai-video.ts +232 -0
- package/src/protocols/zai-chat.ts +71 -0
- package/src/protocols/zai-images.ts +120 -0
- package/src/protocols/zai-messages.ts +34 -0
- package/src/protocols.ts +1 -0
- package/src/provider-error.ts +291 -0
- package/src/provider-package.ts +32 -0
- package/src/providers/alibaba/chat.ts +1 -0
- package/src/providers/alibaba/embeddings.ts +2 -0
- package/src/providers/alibaba/images.ts +2 -0
- package/src/providers/alibaba/messages.ts +3 -0
- package/src/providers/alibaba/responses.ts +3 -0
- package/src/providers/alibaba.ts +177 -0
- package/src/providers/amazon-bedrock/mantle/chat.ts +2 -0
- package/src/providers/amazon-bedrock/mantle/messages.ts +2 -0
- package/src/providers/amazon-bedrock/mantle/responses.ts +2 -0
- package/src/providers/amazon-bedrock/mantle.ts +2 -0
- package/src/providers/amazon-bedrock-mantle.ts +192 -0
- package/src/providers/amazon-bedrock.ts +81 -0
- package/src/providers/anthropic-compatible.ts +80 -0
- package/src/providers/anthropic.ts +66 -0
- package/src/providers/assemblyai.ts +32 -0
- package/src/providers/azure/chat.ts +2 -0
- package/src/providers/azure/embeddings.ts +2 -0
- package/src/providers/azure/images.ts +2 -0
- package/src/providers/azure/messages.ts +2 -0
- package/src/providers/azure/responses.ts +2 -0
- package/src/providers/azure.ts +240 -0
- package/src/providers/baseten/decisions.ts +2 -0
- package/src/providers/baseten.ts +88 -0
- package/src/providers/byteplus/videos.ts +2 -0
- package/src/providers/byteplus.ts +39 -0
- package/src/providers/cartesia.ts +28 -0
- package/src/providers/cerebras.ts +56 -0
- package/src/providers/cloudflare-ai-gateway/decisions.ts +2 -0
- package/src/providers/cloudflare-ai-gateway.ts +199 -0
- package/src/providers/cloudflare-shared.ts +29 -0
- package/src/providers/cloudflare-workers-ai/decisions.ts +2 -0
- package/src/providers/cloudflare-workers-ai/images.ts +2 -0
- package/src/providers/cloudflare-workers-ai.ts +104 -0
- package/src/providers/cohere/chat.ts +8 -0
- package/src/providers/cohere/embeddings.ts +2 -0
- package/src/providers/cohere.ts +72 -0
- package/src/providers/deepgram.ts +33 -0
- package/src/providers/deepinfra/embeddings.ts +2 -0
- package/src/providers/deepinfra.ts +76 -0
- package/src/providers/deepseek.ts +54 -0
- package/src/providers/digitalocean/decisions.ts +2 -0
- package/src/providers/digitalocean/embeddings.ts +2 -0
- package/src/providers/digitalocean/images.ts +2 -0
- package/src/providers/digitalocean/responses.ts +4 -0
- package/src/providers/digitalocean.ts +144 -0
- package/src/providers/elevenlabs.ts +35 -0
- package/src/providers/fireworks/embeddings.ts +2 -0
- package/src/providers/fireworks.ts +66 -0
- package/src/providers/google/decisions.ts +2 -0
- package/src/providers/google/embeddings.ts +2 -0
- package/src/providers/google/images.ts +2 -0
- package/src/providers/google/interactions.ts +13 -0
- package/src/providers/google/videos.ts +2 -0
- package/src/providers/google-vertex/chat.ts +2 -0
- package/src/providers/google-vertex/embeddings.ts +2 -0
- package/src/providers/google-vertex/gemini.ts +2 -0
- package/src/providers/google-vertex/images.ts +2 -0
- package/src/providers/google-vertex/interactions.ts +2 -0
- package/src/providers/google-vertex/messages.ts +2 -0
- package/src/providers/google-vertex/mistral.ts +2 -0
- package/src/providers/google-vertex/responses.ts +2 -0
- package/src/providers/google-vertex-chat.ts +63 -0
- package/src/providers/google-vertex-interactions.ts +104 -0
- package/src/providers/google-vertex-messages.ts +99 -0
- package/src/providers/google-vertex-mistral.ts +79 -0
- package/src/providers/google-vertex-responses.ts +64 -0
- package/src/providers/google-vertex-shared.ts +176 -0
- package/src/providers/google-vertex.ts +228 -0
- package/src/providers/google.ts +121 -0
- package/src/providers/groq.ts +98 -0
- package/src/providers/index.ts +45 -0
- package/src/providers/meta/chat.ts +2 -0
- package/src/providers/meta/images.ts +2 -0
- package/src/providers/meta/messages.ts +2 -0
- package/src/providers/meta/responses.ts +2 -0
- package/src/providers/meta.ts +161 -0
- package/src/providers/minimax/chat.ts +8 -0
- package/src/providers/minimax/images.ts +4 -0
- package/src/providers/minimax/messages.ts +1 -0
- package/src/providers/minimax/responses.ts +8 -0
- package/src/providers/minimax/videos.ts +4 -0
- package/src/providers/minimax.ts +162 -0
- package/src/providers/mistral.ts +40 -0
- package/src/providers/moonshot/chat.ts +1 -0
- package/src/providers/moonshot/messages.ts +8 -0
- package/src/providers/moonshot/responses.ts +8 -0
- package/src/providers/moonshot.ts +134 -0
- package/src/providers/open-responses-options.ts +4 -0
- package/src/providers/openai/chat.ts +2 -0
- package/src/providers/openai/embeddings.ts +2 -0
- package/src/providers/openai/images.ts +2 -0
- package/src/providers/openai/responses.ts +2 -0
- package/src/providers/openai-compatible/decisions.ts +2 -0
- package/src/providers/openai-compatible/embeddings.ts +2 -0
- package/src/providers/openai-compatible/images.ts +2 -0
- package/src/providers/openai-compatible/responses.ts +1 -0
- package/src/providers/openai-compatible-responses.ts +52 -0
- package/src/providers/openai-compatible.ts +108 -0
- package/src/providers/openai-options.ts +66 -0
- package/src/providers/openai.ts +207 -0
- package/src/providers/opencode/decisions.ts +2 -0
- package/src/providers/opencode.ts +45 -0
- package/src/providers/openrouter/decisions.ts +2 -0
- package/src/providers/openrouter/embeddings.ts +2 -0
- package/src/providers/openrouter/images.ts +2 -0
- package/src/providers/openrouter/responses.ts +3 -0
- package/src/providers/openrouter/videos.ts +2 -0
- package/src/providers/openrouter.ts +347 -0
- package/src/providers/settings.ts +54 -0
- package/src/providers/togetherai.ts +53 -0
- package/src/providers/typesafe-ai/decisions.ts +2 -0
- package/src/providers/typesafe-ai.ts +45 -0
- package/src/providers/venice/responses.ts +4 -0
- package/src/providers/venice.ts +93 -0
- package/src/providers/vercel-ai-gateway/chat.ts +2 -0
- package/src/providers/vercel-ai-gateway/decisions.ts +2 -0
- package/src/providers/vercel-ai-gateway/embeddings.ts +2 -0
- package/src/providers/vercel-ai-gateway/images.ts +2 -0
- package/src/providers/vercel-ai-gateway/messages.ts +2 -0
- package/src/providers/vercel-ai-gateway/responses.ts +2 -0
- package/src/providers/vercel-ai-gateway.ts +300 -0
- package/src/providers/volcengine/videos.ts +2 -0
- package/src/providers/volcengine.ts +39 -0
- package/src/providers/xai/images.ts +2 -0
- package/src/providers/xai/videos.ts +2 -0
- package/src/providers/xai.ts +125 -0
- package/src/providers/zai/chat.ts +1 -0
- package/src/providers/zai/images.ts +2 -0
- package/src/providers/zai-coding-plan/chat.ts +1 -0
- package/src/providers/zai-coding-plan/messages.ts +8 -0
- package/src/providers/zai-coding-plan/responses.ts +8 -0
- package/src/providers/zai-coding-plan.ts +86 -0
- package/src/providers/zai.ts +73 -0
- package/src/providers.ts +1 -0
- package/src/public.ts +204 -0
- package/src/route/auth-options.ts +62 -0
- package/src/route/auth.ts +164 -0
- package/src/route/client.ts +700 -0
- package/src/route/decision.ts +150 -0
- package/src/route/embedding.ts +175 -0
- package/src/route/endpoint.ts +63 -0
- package/src/route/executor-service.ts +191 -0
- package/src/route/executor.ts +361 -0
- package/src/route/framing.ts +112 -0
- package/src/route/index.ts +47 -0
- package/src/route/media-protocol.ts +321 -0
- package/src/route/media.ts +507 -0
- package/src/route/protocol.ts +97 -0
- package/src/route/transport/http.ts +103 -0
- package/src/route/transport/index.ts +59 -0
- package/src/route/transport/websocket-channel.ts +51 -0
- package/src/route/transport/websocket.ts +443 -0
- package/src/route.ts +1 -0
- package/src/schema/errors.ts +246 -0
- package/src/schema/events.ts +744 -0
- package/src/schema/ids.ts +24 -0
- package/src/schema/index.ts +5 -0
- package/src/schema/messages.ts +529 -0
- package/src/schema/options.ts +445 -0
- package/src/speech-client.ts +25 -0
- package/src/speech.ts +211 -0
- package/src/testing.ts +218 -0
- package/src/tool-history.ts +91 -0
- package/src/tool-runtime.ts +107 -0
- package/src/tool-updates.ts +139 -0
- package/src/tool.ts +281 -0
- package/src/transcription-client.ts +19 -0
- package/src/transcription.ts +234 -0
- package/src/utils/bytes.ts +9 -0
- package/src/utils/json.ts +5 -0
- package/src/utils/media-type.ts +93 -0
- package/src/utils/provider-codes.ts +43 -0
- package/src/utils/record.ts +3 -0
- package/src/utils/sanitize.ts +33 -0
- package/src/video-client.ts +66 -0
- package/src/video.ts +181 -0
- package/test/adapter.test.ts +180 -0
- package/test/auth-options.types.ts +283 -0
- package/test/auth.test.ts +112 -0
- package/test/cache-policy.test.ts +578 -0
- package/test/compaction-http.test.ts +96 -0
- package/test/compaction.test.ts +107 -0
- package/test/compile.test.ts +600 -0
- package/test/continuation-scenarios.ts +115 -0
- package/test/decision.test.ts +983 -0
- package/test/decision.types.ts +117 -0
- package/test/effort-updates.test.ts +574 -0
- package/test/embedding.test.ts +455 -0
- package/test/endpoint.test.ts +58 -0
- package/test/error-partial.test.ts +140 -0
- package/test/executor.test.ts +1095 -0
- package/test/exports.test.ts +222 -0
- package/test/extraction.test.ts +227 -0
- package/test/fixtures/audio/dialog.mp3 +0 -0
- package/test/fixtures/audio/hello.mp3 +0 -0
- package/test/fixtures/images/edit-source.jpg +0 -0
- package/test/fixtures/media/restroom.png +0 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-flash-reads-image-bytes.json +34 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-returns-a-json-object.json +34 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-default-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-high-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-low-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-max-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-medium-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-minimal-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-none-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-xhigh-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-images/edits-an-image.json +33 -0
- package/test/fixtures/recordings/alibaba-images/generates-qwen-image-images.json +33 -0
- package/test/fixtures/recordings/alibaba-images/generates-wan-images.json +33 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-flash-reads-image-bytes.json +34 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-follows-a-json-schema.json +34 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-default-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-high-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-low-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-max-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-medium-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-xhigh-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-flash-reads-image-bytes.json +34 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-continues-a-stored-response.json +53 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-default-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-high-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-low-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-max-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-medium-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-minimal-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-none-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-xhigh-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-uses-hosted-code-interpreter.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-uses-hosted-web-search-and-extraction.json +36 -0
- package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-text.json +40 -0
- package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-tool-call.json +41 -0
- package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-tool-loop.json +60 -0
- package/test/fixtures/recordings/anthropic-messages/anthropic-opus-4-7-image-tool-result.json +43 -0
- package/test/fixtures/recordings/anthropic-messages-cache/keeps-a-long-tool-turn-inside-the-cache-lookback.json +48 -0
- package/test/fixtures/recordings/anthropic-messages-cache/writes-then-reads-cache-control-on-identical-second-call.json +48 -0
- package/test/fixtures/recordings/assemblyai-transcription/uploads-submits-and-polls-a-transcript.json +98 -0
- package/test/fixtures/recordings/azure/chat-streams-text-from-a-foundry-models-url.json +31 -0
- package/test/fixtures/recordings/azure/chat-streams-text.json +28 -0
- package/test/fixtures/recordings/azure/messages-calls-a-tool.json +31 -0
- package/test/fixtures/recordings/azure/messages-streams-text.json +31 -0
- package/test/fixtures/recordings/azure/responses-calls-a-tool.json +31 -0
- package/test/fixtures/recordings/azure/responses-continues-after-a-tool-result.json +31 -0
- package/test/fixtures/recordings/azure/responses-streams-text.json +31 -0
- package/test/fixtures/recordings/baseten-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/bedrock-converse/continues-after-parallel-tool-results.json +36 -0
- package/test/fixtures/recordings/bedrock-converse/drives-a-tool-loop.json +55 -0
- package/test/fixtures/recordings/bedrock-converse/streams-a-tool-call.json +29 -0
- package/test/fixtures/recordings/bedrock-converse/streams-text.json +29 -0
- package/test/fixtures/recordings/bedrock-converse-cache/writes-then-reads-cachepoint-on-identical-second-call.json +48 -0
- package/test/fixtures/recordings/bedrock-converse-options/constrains-claude-output-to-a-json-schema.json +33 -0
- package/test/fixtures/recordings/bedrock-converse-options/sends-claude-effort-with-adaptive-thinking-service-tier-and-request-metadata.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-options/sends-gpt-oss-reasoning-effort-with-the-priority-tier.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-options/sends-nova-2-reasoning-effort.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-thinking-binding/accepts-the-default-binding-with-no-thinking-configured.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-thinking-binding/keeps-a-session-working-after-the-system-prompt-changes.json +71 -0
- package/test/fixtures/recordings/bedrock-mantle/streams-text.json +33 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/applies-mid-conversation-effort-updates-and-thinking-block-binding-on-opus-5-5.json +57 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/lowers-system-updates-to-wrapped-user-text-on-haiku-4-5-with-budget-thinking.json +37 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/replays-signed-thinking-through-a-tool-loop-and-native-system-update.json +59 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/strips-unsupported-mid-conversation-effort-updates-on-opus-5-0.json +37 -0
- package/test/fixtures/recordings/cartesia-speech/generates-speech.json +34 -0
- package/test/fixtures/recordings/cartesia-speech/streams-speech-with-timestamps.json +33 -0
- package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-text.json +32 -0
- package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-tool-call.json +32 -0
- package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-tool-loop.json +50 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-claude-haiku-4-5-text.json +38 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-claude-haiku-4-5-tool-loop.json +58 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-openai-gpt-5-nano-text.json +37 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-openai-gpt-5-nano-tool-loop.json +56 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-llama-3-1-8b-text.json +32 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-llama-3-1-8b-text.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-extraction/extracts-invoice-fields.json +33 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-images/generates-a-flux-2-image-from-multipart-fields.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-images/generates-a-raw-binary-image.json +33 -0
- package/test/fixtures/recordings/cohere/continues-a-native-tool-call.json +49 -0
- package/test/fixtures/recordings/cohere/streams-compatible-chat-reasoning.json +31 -0
- package/test/fixtures/recordings/cohere/streams-native-text-and-usage.json +31 -0
- package/test/fixtures/recordings/cohere/streams-native-thinking-with-a-budget.json +31 -0
- package/test/fixtures/recordings/cohere-embedding/embeds-text-through-the-compatibility-api.json +33 -0
- package/test/fixtures/recordings/deepgram-speech/generates-speech.json +36 -0
- package/test/fixtures/recordings/deepgram-speech/streams-speech.json +36 -0
- package/test/fixtures/recordings/deepgram-transcription/splits-utterances-at-speaker-changes.json +32 -0
- package/test/fixtures/recordings/deepgram-transcription/transcribes-raw-audio-with-speakers-and-word-timestamps.json +50 -0
- package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-text.json +32 -0
- package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-tool-call.json +32 -0
- package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-tool-loop.json +50 -0
- package/test/fixtures/recordings/digitalocean-chat/gpt-nano-continues-a-tool-call-with-cache-markers.json +54 -0
- package/test/fixtures/recordings/digitalocean-chat/gpt-nano-reuses-a-cached-prompt.json +53 -0
- package/test/fixtures/recordings/digitalocean-chat/haiku-continues-a-tool-call-with-cache-markers.json +54 -0
- package/test/fixtures/recordings/digitalocean-chat/haiku-reuses-a-cached-prompt.json +53 -0
- package/test/fixtures/recordings/digitalocean-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/digitalocean-responses/calls-a-freeform-text-tool.json +35 -0
- package/test/fixtures/recordings/digitalocean-responses/continues-a-tool-call.json +53 -0
- package/test/fixtures/recordings/digitalocean-responses/streams-text.json +35 -0
- package/test/fixtures/recordings/elevenlabs-speech/generates-speech.json +35 -0
- package/test/fixtures/recordings/elevenlabs-speech/streams-speech-with-timestamps.json +34 -0
- package/test/fixtures/recordings/elevenlabs-transcription/groups-diarized-words-into-speaker-turns.json +32 -0
- package/test/fixtures/recordings/elevenlabs-transcription/transcribes-audio-with-word-timestamps.json +50 -0
- package/test/fixtures/recordings/gemini/gemini-2-5-flash-image.json +32 -0
- package/test/fixtures/recordings/gemini-cache/reports-cachedcontenttokencount-on-identical-second-call.json +46 -0
- package/test/fixtures/recordings/google-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/google-embedding/embeds-an-image-and-text-into-one-vector-each.json +32 -0
- package/test/fixtures/recordings/google-embedding/embeds-text-through-batchembedcontents.json +32 -0
- package/test/fixtures/recordings/google-embedding/maps-inputtype-to-a-task-type-for-gemini-embedding-001.json +50 -0
- package/test/fixtures/recordings/google-images/edits-an-image.json +28 -0
- package/test/fixtures/recordings/google-images/generates-an-image.json +28 -0
- package/test/fixtures/recordings/google-interactions/continues-tool-results-with-previous-interaction-id.json +66 -0
- package/test/fixtures/recordings/google-interactions/replays-native-tool-results-and-signatures-statelessly.json +50 -0
- package/test/fixtures/recordings/google-interactions/streams-reasoning-and-retains-thought-signatures.json +32 -0
- package/test/fixtures/recordings/google-interactions/streams-text-and-reports-usage.json +32 -0
- package/test/fixtures/recordings/google-interactions-video/generates-a-video.json +96 -0
- package/test/fixtures/recordings/google-speech/generates-speech.json +32 -0
- package/test/fixtures/recordings/google-speech/streams-speech.json +32 -0
- package/test/fixtures/recordings/google-transcription/streams-speaker-turns.json +32 -0
- package/test/fixtures/recordings/google-transcription/transcribes-with-speakers-and-word-timestamps.json +32 -0
- package/test/fixtures/recordings/google-vertex/calls-a-tool.json +32 -0
- package/test/fixtures/recordings/google-vertex/continues-after-a-tool-result.json +32 -0
- package/test/fixtures/recordings/google-vertex/streams-text.json +32 -0
- package/test/fixtures/recordings/google-vertex-embedding/embeds-gemini-embedding-001-one-text-per-predict-request.json +50 -0
- package/test/fixtures/recordings/google-vertex-embedding/embeds-text-through-predict.json +32 -0
- package/test/fixtures/recordings/groq-chat/continues-qwen-parallel-tool-calls.json +47 -0
- package/test/fixtures/recordings/groq-chat/replays-gpt-oss-reasoning-through-a-tool-loop.json +47 -0
- package/test/fixtures/recordings/groq-chat/streams-qwen-parsed-reasoning.json +29 -0
- package/test/fixtures/recordings/groq-chat/streams-text-with-usage.json +29 -0
- package/test/fixtures/recordings/meta-chat/continues-a-generated-tool-call.json +56 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-default-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-high-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-low-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-max-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-medium-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-minimal-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-xhigh-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-images/edits-image-bytes-and-returns-png.json +29 -0
- package/test/fixtures/recordings/meta-images/generates-default-webp-bytes.json +29 -0
- package/test/fixtures/recordings/meta-images/replays-a-signed-image-handle-for-a-responses-edit.json +55 -0
- package/test/fixtures/recordings/meta-messages/replays-encrypted-thinking-through-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/meta-messages/streams-text-with-adaptive-thinking.json +36 -0
- package/test/fixtures/recordings/meta-messages/streams-text-with-enabled-thinking.json +36 -0
- package/test/fixtures/recordings/meta-responses/replays-encrypted-reasoning-through-a-tool-loop.json +56 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-default-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-high-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-low-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-max-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-medium-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-minimal-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-xhigh-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-search/searches-and-replays-grounded-messages.json +56 -0
- package/test/fixtures/recordings/meta-search/searches-and-replays-grounded-responses.json +56 -0
- package/test/fixtures/recordings/minimax-chat/m3-continues-a-tool-loop-with-default-thinking.json +56 -0
- package/test/fixtures/recordings/minimax-chat/m3-streams-text-with-thinking-disabled.json +36 -0
- package/test/fixtures/recordings/minimax-images/generates-an-image.json +32 -0
- package/test/fixtures/recordings/minimax-images/generates-from-a-subject-reference.json +32 -0
- package/test/fixtures/recordings/minimax-messages/m2-7-streams-default-thinking.json +36 -0
- package/test/fixtures/recordings/minimax-messages/m3-continues-a-tool-loop-with-adaptive-thinking.json +56 -0
- package/test/fixtures/recordings/minimax-messages/m3-generates-a-named-tool-call-with-default-thinking-off.json +36 -0
- package/test/fixtures/recordings/minimax-messages/m3-streams-adaptive-thinking.json +36 -0
- package/test/fixtures/recordings/minimax-messages/m3-streams-text-with-thinking-disabled.json +36 -0
- package/test/fixtures/recordings/minimax-responses/m3-continues-a-tool-loop-with-effort-minimal.json +56 -0
- package/test/fixtures/recordings/minimax-responses/m3-streams-text-with-effort-none.json +36 -0
- package/test/fixtures/recordings/minimax-video/generates-a-hailuo-video-from-a-first-frame.json +176 -0
- package/test/fixtures/recordings/minimax-video/generates-an-h3-video.json +96 -0
- package/test/fixtures/recordings/mistral-chat/drives-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/mistral-chat/replays-native-reasoning.json +47 -0
- package/test/fixtures/recordings/mistral-chat/streams-text-with-usage.json +29 -0
- package/test/fixtures/recordings/mistral-chat-glm/streams-an-indexed-tool-call.json +36 -0
- package/test/fixtures/recordings/moonshot-chat/k3-reads-image-bytes.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-respects-tool-choice-none.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-respects-tool-choice-required.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-returns-native-structured-output.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-default-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-high-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-low-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-max-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-default-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-disabled-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-enabled-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-highspeed-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-highspeed-streams-text-with-default-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-streams-text-with-default-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-messages/k3-reads-image-bytes.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-respects-tool-choice-none.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-respects-tool-choice-required.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-returns-native-structured-output.json +35 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-default-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-high-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-low-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-max-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-responses/k3-reads-image-bytes.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-rejects-unsupported-tool-choice-none.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-rejects-unsupported-tool-choice-required.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-returns-native-structured-output.json +35 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-default-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-high-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-low-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-max-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/openai-compatible-chat/gemini-parallel-tool-signatures.json +54 -0
- package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-decision/decides-choice-score-and-predicate-questions-about-json-state.json +33 -0
- package/test/fixtures/recordings/openai-embedding/embeds-text-with-requested-dimensions.json +33 -0
- package/test/fixtures/recordings/openai-images/edits-an-image.json +28 -0
- package/test/fixtures/recordings/openai-images/generates-an-image.json +28 -0
- package/test/fixtures/recordings/openai-images/streams-a-partial-image-before-the-final-image.json +32 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-image-tool-result.json +42 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning-continuation.json +58 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning.json +38 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-tool-loop.json +57 -0
- package/test/fixtures/recordings/openai-responses-cache/caches-the-system-prompt-on-gpt-6-2-sol.json +51 -0
- package/test/fixtures/recordings/openai-responses-cache/reports-cached-tokens-on-identical-second-call.json +51 -0
- package/test/fixtures/recordings/openai-responses-images/generates-and-edits-an-image-with-the-hosted-tool.json +50 -0
- package/test/fixtures/recordings/openai-responses-websocket/continues-a-tool-call-over-one-socket.json +588 -0
- package/test/fixtures/recordings/openai-responses-websocket/reconstructs-full-context-after-reconnect.json +167 -0
- package/test/fixtures/recordings/openai-responses-websocket/recovers-from-explicit-continuation-rejection.json +167 -0
- package/test/fixtures/recordings/openai-speech/generates-speech.json +33 -0
- package/test/fixtures/recordings/openai-speech/streams-speech.json +32 -0
- package/test/fixtures/recordings/openai-transcription/diarizes-speakers-into-segments.json +32 -0
- package/test/fixtures/recordings/openai-transcription/returns-whisper-word-timestamps.json +32 -0
- package/test/fixtures/recordings/openai-transcription/streams-text-deltas.json +32 -0
- package/test/fixtures/recordings/openai-transcription/transcribes-with-a-language-hint.json +32 -0
- package/test/fixtures/recordings/opencode-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/openrouter-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/openrouter-images/edits-an-image-through-input-references.json +32 -0
- package/test/fixtures/recordings/openrouter-images/renders-flux-3-from-an-aspect-ratio-and-tier.json +32 -0
- package/test/fixtures/recordings/openrouter-images/renders-gemini-at-the-requested-2k-tier.json +32 -0
- package/test/fixtures/recordings/openrouter-reasoning-tool-loop.json +55 -0
- package/test/fixtures/recordings/openrouter-reasoning.json +29 -0
- package/test/fixtures/recordings/openrouter-responses/calls-a-freeform-text-tool.json +35 -0
- package/test/fixtures/recordings/openrouter-responses/continues-a-tool-call.json +53 -0
- package/test/fixtures/recordings/openrouter-responses/streams-text.json +35 -0
- package/test/fixtures/recordings/openrouter-video/generates-a-video.json +113 -0
- package/test/fixtures/recordings/pdf/anthropic-tool-result.json +28 -0
- package/test/fixtures/recordings/pdf/anthropic-user-input.json +28 -0
- package/test/fixtures/recordings/pdf/bedrock-tool-result.json +29 -0
- package/test/fixtures/recordings/pdf/bedrock-user-input.json +29 -0
- package/test/fixtures/recordings/pdf/gemini-tool-result.json +53 -0
- package/test/fixtures/recordings/pdf/gemini-user-input.json +28 -0
- package/test/fixtures/recordings/pdf/openai-tool-result.json +35 -0
- package/test/fixtures/recordings/pdf/openai-user-input.json +34 -0
- package/test/fixtures/recordings/pdf/xai-tool-result.json +35 -0
- package/test/fixtures/recordings/pdf/xai-user-input.json +34 -0
- package/test/fixtures/recordings/typesafe-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/venice-chat/claude-opus-4-8-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/deepseek-v4-flash-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/gemini-3-1-pro-preview-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/gemini-preserves-per-call-signatures-through-parallel-tools.json +71 -0
- package/test/fixtures/recordings/venice-chat/openai-gpt-54-mini-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/qwen-disables-reasoning.json +35 -0
- package/test/fixtures/recordings/venice-chat/surfaces-venice-model-errors.json +33 -0
- package/test/fixtures/recordings/venice-responses/calls-a-freeform-text-tool.json +35 -0
- package/test/fixtures/recordings/venice-responses/continues-a-tool-call.json +53 -0
- package/test/fixtures/recordings/venice-responses/streams-text.json +35 -0
- package/test/fixtures/recordings/vercel-ai-gateway-images/generates-an-image.json +32 -0
- package/test/fixtures/recordings/vercel-ai-gateway-images/sizes-grok-through-xai-provider-options.json +32 -0
- package/test/fixtures/recordings/vercel-ai-gateway-reasoning-tool-loop.json +55 -0
- package/test/fixtures/recordings/vercel-ai-gateway-reasoning.json +29 -0
- package/test/fixtures/recordings/vercel-native/automatically-caches-distinct-claude-system-blocks.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-chat-claude-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-chat-muse-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-messages-claude-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-messages-deepseek-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-messages-gemini-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-responses-gpt-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-responses-grok-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/rejects-invalid-credentials-without-retryable-classification.json +31 -0
- package/test/fixtures/recordings/xai-images/edits-an-image.json +28 -0
- package/test/fixtures/recordings/xai-images/generates-an-image.json +28 -0
- package/test/fixtures/recordings/xai-video/generates-a-video.json +129 -0
- package/test/fixtures/recordings/zai-chat/glm-4-5-calls-a-tool-without-streaming-arguments.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-4-7-streams-thinking-disabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-4-7-streams-thinking-enabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-high-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-low-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-max-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-medium-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-minimal-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-none-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-thinking-disabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-thinking-enabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-xhigh-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-flash-reads-image-bytes.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +64 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-rejects-disabled-thinking.json +27 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-returns-a-json-object.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-default-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-high-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-low-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-max-effort.json +28 -0
- package/test/fixtures/recordings/zai-images/generates-an-image.json +28 -0
- package/test/generate-object.test.ts +281 -0
- package/test/generation.test.ts +191 -0
- package/test/google-interactions-video.test.ts +351 -0
- package/test/http-hooks.test.ts +137 -0
- package/test/http-timeout.test.ts +287 -0
- package/test/image-job.test.ts +199 -0
- package/test/image-sizing.test.ts +47 -0
- package/test/image.test.ts +1114 -0
- package/test/image.types.ts +203 -0
- package/test/keyless-auth.test.ts +85 -0
- package/test/lib/effect.ts +50 -0
- package/test/lib/env.ts +30 -0
- package/test/lib/http.ts +141 -0
- package/test/lib/image.ts +29 -0
- package/test/lib/openai-chunks.ts +27 -0
- package/test/lib/sse.ts +20 -0
- package/test/lib/tool-runtime.ts +152 -0
- package/test/llm-option-types.types.ts +153 -0
- package/test/llm.test.ts +286 -0
- package/test/loop-steps.test.ts +77 -0
- package/test/loop-tools.types.ts +79 -0
- package/test/loop.test.ts +425 -0
- package/test/media-input.test.ts +98 -0
- package/test/media-usage.test.ts +13 -0
- package/test/media.test.ts +340 -0
- package/test/minimax-video.test.ts +553 -0
- package/test/openai-freeform.test.ts +252 -0
- package/test/openrouter-video.test.ts +294 -0
- package/test/partial-json.test.ts +58 -0
- package/test/promise-parity.types.ts +83 -0
- package/test/promise.test.ts +632 -0
- package/test/provider/alibaba-capabilities.recorded.test.ts +253 -0
- package/test/provider/alibaba-images.recorded.test.ts +71 -0
- package/test/provider/alibaba-images.test.ts +199 -0
- package/test/provider/alibaba.recorded.test.ts +165 -0
- package/test/provider/alibaba.test.ts +419 -0
- package/test/provider/anthropic-compaction.test.ts +171 -0
- package/test/provider/anthropic-messages-cache.recorded.test.ts +111 -0
- package/test/provider/anthropic-messages.recorded.test.ts +45 -0
- package/test/provider/anthropic-messages.test.ts +2609 -0
- package/test/provider/anthropic-thinking-binding.test.ts +65 -0
- package/test/provider/assemblyai-transcription.recorded.test.ts +39 -0
- package/test/provider/azure.recorded.test.ts +139 -0
- package/test/provider/azure.test.ts +48 -0
- package/test/provider/bedrock-converse-cache.recorded.test.ts +61 -0
- package/test/provider/bedrock-converse-cache.test.ts +168 -0
- package/test/provider/bedrock-converse-options.recorded.test.ts +119 -0
- package/test/provider/bedrock-converse-options.test.ts +149 -0
- package/test/provider/bedrock-converse-thinking-binding.recorded.test.ts +103 -0
- package/test/provider/bedrock-converse-thinking-binding.test.ts +107 -0
- package/test/provider/bedrock-converse.test.ts +2548 -0
- package/test/provider/bedrock-mantle.test.ts +444 -0
- package/test/provider/bedrock-mistral-ids.test.ts +86 -0
- package/test/provider/cartesia-speech.recorded.test.ts +47 -0
- package/test/provider/checkpoint.test.ts +324 -0
- package/test/provider/cloudflare-workers-ai-images.recorded.test.ts +57 -0
- package/test/provider/cloudflare-workers-ai-images.test.ts +155 -0
- package/test/provider/cloudflare.test.ts +330 -0
- package/test/provider/cohere.recorded.test.ts +94 -0
- package/test/provider/cohere.test.ts +301 -0
- package/test/provider/compaction-websocket.test.ts +50 -0
- package/test/provider/compaction.recorded.test.ts +89 -0
- package/test/provider/compaction.test.ts +178 -0
- package/test/provider/conversation-lowering.test.ts +31 -0
- package/test/provider/decision.recorded.test.ts +211 -0
- package/test/provider/deepgram-speech.recorded.test.ts +41 -0
- package/test/provider/deepgram-transcription.recorded.test.ts +47 -0
- package/test/provider/digitalocean.recorded.test.ts +78 -0
- package/test/provider/digitalocean.test.ts +123 -0
- package/test/provider/elevenlabs-speech.recorded.test.ts +54 -0
- package/test/provider/elevenlabs-transcription.recorded.test.ts +50 -0
- package/test/provider/embedding.recorded.test.ts +109 -0
- package/test/provider/empty-tool-choice.test.ts +80 -0
- package/test/provider/error-retention.test.ts +158 -0
- package/test/provider/explicit-compaction.test.ts +503 -0
- package/test/provider/extraction.recorded.test.ts +46 -0
- package/test/provider/freeform-tools.test.ts +446 -0
- package/test/provider/gemini-cache.recorded.test.ts +49 -0
- package/test/provider/gemini-openai-chat.recorded.test.ts +69 -0
- package/test/provider/gemini.test.ts +2336 -0
- package/test/provider/golden.recorded.test.ts +287 -0
- package/test/provider/google-images.recorded.test.ts +53 -0
- package/test/provider/google-interactions-video.recorded.test.ts +48 -0
- package/test/provider/google-interactions.recorded.test.ts +169 -0
- package/test/provider/google-interactions.test.ts +593 -0
- package/test/provider/google-speech.recorded.test.ts +45 -0
- package/test/provider/google-transcription.recorded.test.ts +54 -0
- package/test/provider/google-vertex-interactions.test.ts +307 -0
- package/test/provider/google-vertex.recorded.test.ts +75 -0
- package/test/provider/google-vertex.test.ts +595 -0
- package/test/provider/groq.recorded.test.ts +187 -0
- package/test/provider/groq.test.ts +134 -0
- package/test/provider/image-detail.test.ts +58 -0
- package/test/provider/media-entrypoints.test.ts +618 -0
- package/test/provider/meta-capabilities.test.ts +216 -0
- package/test/provider/meta-images.recorded.test.ts +119 -0
- package/test/provider/meta-messages.recorded.test.ts +121 -0
- package/test/provider/meta-search.recorded.test.ts +97 -0
- package/test/provider/meta.recorded.test.ts +182 -0
- package/test/provider/meta.test.ts +140 -0
- package/test/provider/minimax-images.recorded.test.ts +52 -0
- package/test/provider/minimax-images.test.ts +236 -0
- package/test/provider/minimax-video.recorded.test.ts +83 -0
- package/test/provider/minimax.recorded.test.ts +257 -0
- package/test/provider/minimax.test.ts +110 -0
- package/test/provider/missing-tool-input.test.ts +73 -0
- package/test/provider/mistral-chat.test.ts +757 -0
- package/test/provider/mistral.recorded.test.ts +170 -0
- package/test/provider/moonshot-capabilities.recorded.test.ts +131 -0
- package/test/provider/moonshot.recorded.test.ts +191 -0
- package/test/provider/moonshot.test.ts +218 -0
- package/test/provider/native-providers.test.ts +299 -0
- package/test/provider/open-responses-errors.test.ts +141 -0
- package/test/provider/open-responses-finals.test.ts +209 -0
- package/test/provider/open-responses-lifecycle.test.ts +526 -0
- package/test/provider/open-responses-replay.test.ts +120 -0
- package/test/provider/openai-chat-reasoning.recorded.test.ts +154 -0
- package/test/provider/openai-chat.test.ts +2505 -0
- package/test/provider/openai-compatible-chat.test.ts +819 -0
- package/test/provider/openai-compatible-responses.test.ts +1139 -0
- package/test/provider/openai-images.recorded.test.ts +91 -0
- package/test/provider/openai-responses-cache.recorded.test.ts +68 -0
- package/test/provider/openai-responses-images.recorded.test.ts +66 -0
- package/test/provider/openai-responses-websocket.recorded.test.ts +221 -0
- package/test/provider/openai-responses.test.ts +5414 -0
- package/test/provider/openai-speech.recorded.test.ts +38 -0
- package/test/provider/openai-transcription.recorded.test.ts +91 -0
- package/test/provider/openrouter-images.recorded.test.ts +76 -0
- package/test/provider/openrouter-video.recorded.test.ts +53 -0
- package/test/provider/openrouter.test.ts +656 -0
- package/test/provider/pdf.recorded.test.ts +211 -0
- package/test/provider/queued-recording.ts +10 -0
- package/test/provider/responses-routes.recorded.test.ts +91 -0
- package/test/provider/seedance-video.recorded.test.ts +52 -0
- package/test/provider/speech-recording.ts +19 -0
- package/test/provider/transcription-recording.ts +41 -0
- package/test/provider/venice.recorded.test.ts +237 -0
- package/test/provider/venice.test.ts +373 -0
- package/test/provider/vercel-ai-gateway-images.recorded.test.ts +55 -0
- package/test/provider/vercel-ai-gateway.recorded.test.ts +125 -0
- package/test/provider/vercel-ai-gateway.test.ts +303 -0
- package/test/provider/xai-images.recorded.test.ts +54 -0
- package/test/provider/xai-images.test.ts +109 -0
- package/test/provider/xai-responses.test.ts +339 -0
- package/test/provider/xai-video.recorded.test.ts +54 -0
- package/test/provider/zai-capabilities.recorded.test.ts +156 -0
- package/test/provider/zai-images.recorded.test.ts +33 -0
- package/test/provider/zai-images.test.ts +163 -0
- package/test/provider/zai.recorded.test.ts +115 -0
- package/test/provider/zai.test.ts +374 -0
- package/test/provider-api-methods.types.ts +8 -0
- package/test/provider-error.test.ts +800 -0
- package/test/provider-model.types.ts +37 -0
- package/test/provider-options/alibaba.types.ts +46 -0
- package/test/provider-options/amazon-bedrock.types.ts +33 -0
- package/test/provider-options/anthropic-compatible.types.ts +13 -0
- package/test/provider-options/anthropic.types.ts +30 -0
- package/test/provider-options/azure.types.ts +13 -0
- package/test/provider-options/checkpoint.types.ts +150 -0
- package/test/provider-options/cloudflare.types.ts +13 -0
- package/test/provider-options/compaction.types.ts +146 -0
- package/test/provider-options/google-vertex-chat.types.ts +7 -0
- package/test/provider-options/google-vertex-interactions.types.ts +29 -0
- package/test/provider-options/google-vertex-messages.types.ts +13 -0
- package/test/provider-options/google-vertex-responses.types.ts +14 -0
- package/test/provider-options/google-vertex.types.ts +17 -0
- package/test/provider-options/google.types.ts +43 -0
- package/test/provider-options/minimax.types.ts +33 -0
- package/test/provider-options/mistral.types.ts +24 -0
- package/test/provider-options/moonshot.types.ts +45 -0
- package/test/provider-options/openai-compatible-responses.types.ts +17 -0
- package/test/provider-options/openai-compatible.types.ts +13 -0
- package/test/provider-options/openai.types.ts +27 -0
- package/test/provider-options/openrouter.types.ts +55 -0
- package/test/provider-options/venice.types.ts +39 -0
- package/test/provider-options/vercel-ai-gateway.types.ts +30 -0
- package/test/provider-options/xai.types.ts +14 -0
- package/test/provider-options/zai.types.ts +42 -0
- package/test/provider-package.test.ts +586 -0
- package/test/raw-events.test.ts +75 -0
- package/test/realtime.test.ts +172 -0
- package/test/recorded-golden.ts +104 -0
- package/test/recorded-runner.ts +101 -0
- package/test/recorded-scenarios.ts +468 -0
- package/test/recorded-test.ts +127 -0
- package/test/recorded-utils.ts +56 -0
- package/test/request-encoding.test.ts +57 -0
- package/test/request-transport.test.ts +74 -0
- package/test/response.test.ts +203 -0
- package/test/route.test.ts +225 -0
- package/test/sanitize.test.ts +80 -0
- package/test/schema.test.ts +412 -0
- package/test/seedance-video.test.ts +418 -0
- package/test/speech.test.ts +297 -0
- package/test/speech.types.ts +33 -0
- package/test/testing.test.ts +372 -0
- package/test/tool-history.test.ts +162 -0
- package/test/tool-runtime.test.ts +926 -0
- package/test/tool-schema-projection.test.ts +427 -0
- package/test/tool-stream.test.ts +252 -0
- package/test/tool-updates.test.ts +849 -0
- package/test/tool.types.ts +131 -0
- package/test/transcription.test.ts +618 -0
- package/test/transcription.types.ts +44 -0
- package/test/video.test.ts +447 -0
- package/test/video.types.ts +110 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +17 -0
- package/tsconfig.types.json +16 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// Apply an `LLMRequest.cache` policy by injecting `CacheHint`s onto the parts
|
|
2
|
+
// the policy designates. Runs once at compile time, before the per-protocol
|
|
3
|
+
// body builder, so the existing inline-hint lowering path handles the rest.
|
|
4
|
+
//
|
|
5
|
+
// The default `"auto"` shape places breakpoints at the last tool definition,
|
|
6
|
+
// the first and last distinct system parts, and the conversation tail. This
|
|
7
|
+
// exposes reusable tool, base-agent, project, and session prefixes while
|
|
8
|
+
// advancing the tail after each tool result keeps recent conversation prefixes
|
|
9
|
+
// reusable during long agent runs.
|
|
10
|
+
//
|
|
11
|
+
// Manual `cache: CacheHint` placements on individual parts are preserved and
|
|
12
|
+
// count against the four-breakpoint budget; auto only fills remaining slots.
|
|
13
|
+
import { CacheHint, type CachePolicy, type CachePolicyObject, type CacheTTL } from "./schema/options.js"
|
|
14
|
+
import {
|
|
15
|
+
AssistantMessage,
|
|
16
|
+
type LLMRequest,
|
|
17
|
+
type Message,
|
|
18
|
+
SystemMessage,
|
|
19
|
+
ToolDefinition,
|
|
20
|
+
ToolMessage,
|
|
21
|
+
UserMessage,
|
|
22
|
+
type ContentPart,
|
|
23
|
+
type ToolEntry,
|
|
24
|
+
} from "./schema/messages.js"
|
|
25
|
+
import { effortUpdate } from "./effort-updates.js"
|
|
26
|
+
import { toolsUpdate } from "./tool-updates.js"
|
|
27
|
+
|
|
28
|
+
const AUTO: CachePolicyObject = {
|
|
29
|
+
tools: true,
|
|
30
|
+
system: true,
|
|
31
|
+
messages: { tail: 1 },
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const NONE: CachePolicyObject = {}
|
|
35
|
+
const BREAKPOINT_CAP = 4
|
|
36
|
+
|
|
37
|
+
// Resolution rules:
|
|
38
|
+
// - undefined → "auto" — caching is on by default.
|
|
39
|
+
// - "auto" → tools + first/last system + final message boundary.
|
|
40
|
+
// - "none" → no auto placement; manual `CacheHint`s still flow.
|
|
41
|
+
// - `{ ttl }` → "auto" with that lifetime.
|
|
42
|
+
// - object form → exactly what the caller asked for.
|
|
43
|
+
const resolve = (policy: CachePolicy | undefined): CachePolicyObject => {
|
|
44
|
+
if (policy === "none") return NONE
|
|
45
|
+
if (typeof policy === "object" && !isAuto(policy)) return policy
|
|
46
|
+
return AUTO
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const isAuto = (policy: CachePolicy | undefined) =>
|
|
50
|
+
policy === undefined ||
|
|
51
|
+
policy === "auto" ||
|
|
52
|
+
(typeof policy === "object" &&
|
|
53
|
+
policy.tools === undefined &&
|
|
54
|
+
policy.system === undefined &&
|
|
55
|
+
policy.messages === undefined)
|
|
56
|
+
|
|
57
|
+
/** The requested cache lifetime, for routes that send it as a request-level option instead of on markers. */
|
|
58
|
+
export const requestTTL = (request: LLMRequest) => (typeof request.cache === "object" ? request.cache.ttl : undefined)
|
|
59
|
+
|
|
60
|
+
// Protocols whose wire format ignores inline cache markers (OpenAI's implicit
|
|
61
|
+
// prefix caching, Gemini's implicit + out-of-band CachedContent). Skip the
|
|
62
|
+
// whole policy pass for these — emitting hints would be harmless but pointless.
|
|
63
|
+
const RESPECTS_INLINE_HINTS = new Set([
|
|
64
|
+
"alibaba-chat",
|
|
65
|
+
"alibaba-messages",
|
|
66
|
+
"anthropic-messages",
|
|
67
|
+
"anthropic-compatible-messages",
|
|
68
|
+
"bedrock-mantle-messages",
|
|
69
|
+
"cloudflare-ai-gateway-messages",
|
|
70
|
+
"google-vertex-messages",
|
|
71
|
+
"meta-messages",
|
|
72
|
+
"minimax-messages",
|
|
73
|
+
"moonshot-messages",
|
|
74
|
+
"vercel-ai-gateway-messages",
|
|
75
|
+
"zai-coding-messages",
|
|
76
|
+
"bedrock-converse",
|
|
77
|
+
"openrouter",
|
|
78
|
+
"digitalocean",
|
|
79
|
+
])
|
|
80
|
+
|
|
81
|
+
// OpenRouter and Vercel AI Gateway upstreams other than Anthropic and Alibaba Qwen cache without breakpoints.
|
|
82
|
+
// Gemini uses only the last breakpoint, so a conversation-tail breakpoint writes a new cache every step and costs
|
|
83
|
+
// more than none. Qwen ignores breakpoints on tool definitions and caches tools with the system prompt.
|
|
84
|
+
const QWEN: CachePolicyObject = { system: true, messages: { tail: 1 } }
|
|
85
|
+
const gatewayPolicy = (modelID: string): CachePolicyObject => {
|
|
86
|
+
// `~anthropic/claude-sonnet-latest` style IDs are OpenRouter aliases for the latest model in a family.
|
|
87
|
+
const id = modelID.replace(/^~/, "")
|
|
88
|
+
if (id.startsWith("anthropic/")) return AUTO
|
|
89
|
+
if (id.startsWith("qwen/") || id.startsWith("alibaba/qwen")) return QWEN
|
|
90
|
+
return NONE
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const makeHint = (ttl: CacheTTL | undefined): CacheHint =>
|
|
94
|
+
ttl !== undefined ? new CacheHint({ type: "ephemeral", ttl }) : new CacheHint({ type: "ephemeral" })
|
|
95
|
+
|
|
96
|
+
interface Budget {
|
|
97
|
+
remaining: number
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const markLastTool = (tools: ReadonlyArray<ToolEntry>, hint: CacheHint, budget: Budget): ReadonlyArray<ToolEntry> => {
|
|
101
|
+
const target = tools.at(-1)
|
|
102
|
+
if (target === undefined) return tools
|
|
103
|
+
if (target.type === "namespace") {
|
|
104
|
+
const nested = markLastTool(target.tools, hint, budget)
|
|
105
|
+
return nested === target.tools ? tools : [...tools.slice(0, -1), { ...target, tools: nested }]
|
|
106
|
+
}
|
|
107
|
+
if (target.cache || budget.remaining === 0) return tools
|
|
108
|
+
budget.remaining -= 1
|
|
109
|
+
return [...tools.slice(0, -1), new ToolDefinition({ ...target, cache: hint })]
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const countToolHints = (tools: ReadonlyArray<ToolEntry>): number =>
|
|
113
|
+
tools.reduce(
|
|
114
|
+
(count, tool) => count + (tool.type === "tool" ? (tool.cache === undefined ? 0 : 1) : countToolHints(tool.tools)),
|
|
115
|
+
0,
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
const markSystemBoundaries = (system: LLMRequest["system"], hint: CacheHint, budget: Budget): LLMRequest["system"] => {
|
|
119
|
+
if (system.length === 0) return system
|
|
120
|
+
let changed = false
|
|
121
|
+
const next = system.map((part, index) => {
|
|
122
|
+
if ((index !== 0 && index !== system.length - 1) || part.cache || budget.remaining === 0) return part
|
|
123
|
+
budget.remaining -= 1
|
|
124
|
+
changed = true
|
|
125
|
+
return { ...part, cache: hint }
|
|
126
|
+
})
|
|
127
|
+
return changed ? next : system
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const lastIndexOfRole = (messages: ReadonlyArray<Message>, role: Message["role"]): number =>
|
|
131
|
+
messages.findLastIndex((m) => m.role === role)
|
|
132
|
+
|
|
133
|
+
type CacheablePart = Exclude<ContentPart, { readonly type: "compaction" | "effort" }>
|
|
134
|
+
|
|
135
|
+
const isCacheablePart = (part: ContentPart): part is CacheablePart =>
|
|
136
|
+
part.type !== "compaction" && part.type !== "effort"
|
|
137
|
+
|
|
138
|
+
// Mark the last text part of `messages[index]`. If no text part exists, mark
|
|
139
|
+
// the last cacheable content part — that's the breakpoint position
|
|
140
|
+
// in tool-result-only messages too.
|
|
141
|
+
const markMessageAt = (
|
|
142
|
+
messages: ReadonlyArray<Message>,
|
|
143
|
+
index: number,
|
|
144
|
+
hint: CacheHint,
|
|
145
|
+
budget: Budget,
|
|
146
|
+
): ReadonlyArray<Message> => {
|
|
147
|
+
if (index < 0 || index >= messages.length) return messages
|
|
148
|
+
const target = messages[index]!
|
|
149
|
+
if (target.content.length === 0) return messages
|
|
150
|
+
const lastTextIndex = target.content.findLastIndex((part) => part.type === "text")
|
|
151
|
+
const markAt = lastTextIndex >= 0 ? lastTextIndex : target.content.findLastIndex(isCacheablePart)
|
|
152
|
+
const existing = target.content[markAt]
|
|
153
|
+
if (!existing || !isCacheablePart(existing) || existing.cache || budget.remaining === 0) return messages
|
|
154
|
+
budget.remaining -= 1
|
|
155
|
+
const next = cachePartAt(target, markAt, hint)
|
|
156
|
+
// Single pass over `messages`, substituting the one updated entry. Long
|
|
157
|
+
// conversations call this on every request, so avoid `.map()` here — its
|
|
158
|
+
// closure dispatch and identity copies show up in profiling.
|
|
159
|
+
const result = messages.slice()
|
|
160
|
+
result[index] = next
|
|
161
|
+
return result
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const cachePartAt = (message: Message, index: number, hint: CacheHint): Message => {
|
|
165
|
+
switch (message.role) {
|
|
166
|
+
case "system":
|
|
167
|
+
return new SystemMessage({ ...message, content: cacheContentAt(message.content, index, hint) })
|
|
168
|
+
case "user":
|
|
169
|
+
return new UserMessage({ ...message, content: cacheContentAt(message.content, index, hint) })
|
|
170
|
+
case "assistant":
|
|
171
|
+
return new AssistantMessage({ ...message, content: cacheContentAt(message.content, index, hint) })
|
|
172
|
+
case "tool":
|
|
173
|
+
return new ToolMessage({ ...message, content: cacheContentAt(message.content, index, hint) })
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const cacheContentAt = <Part extends ContentPart>(content: ReadonlyArray<Part>, index: number, hint: CacheHint) =>
|
|
178
|
+
content.map((part, i) => (i === index ? { ...part, cache: hint } : part))
|
|
179
|
+
|
|
180
|
+
const markMessages = (
|
|
181
|
+
messages: ReadonlyArray<Message>,
|
|
182
|
+
strategy: NonNullable<CachePolicyObject["messages"]>,
|
|
183
|
+
hint: CacheHint,
|
|
184
|
+
budget: Budget,
|
|
185
|
+
): ReadonlyArray<Message> => {
|
|
186
|
+
if (messages.length === 0) return messages
|
|
187
|
+
if (strategy === "latest-user-message")
|
|
188
|
+
return markMessageAt(messages, lastIndexOfRole(messages, "user"), hint, budget)
|
|
189
|
+
if (strategy === "latest-assistant")
|
|
190
|
+
return markMessageAt(messages, lastIndexOfRole(messages, "assistant"), hint, budget)
|
|
191
|
+
let start = messages.length
|
|
192
|
+
let remaining = strategy.tail
|
|
193
|
+
while (remaining > 0 && start > 0) {
|
|
194
|
+
start -= 1
|
|
195
|
+
if (!isMarker(messages[start]!)) remaining -= 1
|
|
196
|
+
}
|
|
197
|
+
let next = messages
|
|
198
|
+
for (let i = start; i < messages.length; i++) if (!isMarker(messages[i]!)) next = markMessageAt(next, i, hint, budget)
|
|
199
|
+
return next
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const isMarker = (message: Message) => effortUpdate(message) !== undefined || toolsUpdate(message) !== undefined
|
|
203
|
+
|
|
204
|
+
const countHints = (request: LLMRequest) =>
|
|
205
|
+
countToolHints(request.tools) +
|
|
206
|
+
request.system.reduce((count, part) => count + (part.cache === undefined ? 0 : 1), 0) +
|
|
207
|
+
request.messages.reduce(
|
|
208
|
+
(count, message) =>
|
|
209
|
+
count +
|
|
210
|
+
message.content.reduce(
|
|
211
|
+
(contentCount, part) => contentCount + (isCacheablePart(part) && part.cache !== undefined ? 1 : 0),
|
|
212
|
+
0,
|
|
213
|
+
),
|
|
214
|
+
0,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
export const applyCachePolicy = (request: LLMRequest): LLMRequest => {
|
|
218
|
+
const policy = selectPolicy(request)
|
|
219
|
+
if (!policy.tools && !policy.system && !policy.messages) return request
|
|
220
|
+
|
|
221
|
+
const hint = makeHint(requestTTL(request))
|
|
222
|
+
const budget = { remaining: Math.max(0, BREAKPOINT_CAP - countHints(request)) }
|
|
223
|
+
const tools = policy.tools ? markLastTool(request.tools, hint, budget) : request.tools
|
|
224
|
+
const system = policy.system ? markSystemBoundaries(request.system, hint, budget) : request.system
|
|
225
|
+
const messages = policy.messages ? markMessages(request.messages, policy.messages, hint, budget) : request.messages
|
|
226
|
+
|
|
227
|
+
if (tools === request.tools && system === request.system && messages === request.messages) return request
|
|
228
|
+
return { ...request, tools, system, messages }
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** Whether the cache policy places inline markers, which then carry the requested lifetime. */
|
|
232
|
+
export const placesMarkers = (request: LLMRequest) => {
|
|
233
|
+
const policy = selectPolicy(request)
|
|
234
|
+
return Boolean(policy.tools || policy.system || policy.messages)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// A declared cache format overrides the route allowlist and its model-ID heuristics.
|
|
238
|
+
const selectPolicy = (request: LLMRequest): CachePolicyObject => {
|
|
239
|
+
const format = request.model.compatibility?.cacheFormat
|
|
240
|
+
const route = request.model.route.id
|
|
241
|
+
const auto = isAuto(request.cache)
|
|
242
|
+
if (format === "alibaba") return auto ? QWEN : { ...resolve(request.cache), tools: false }
|
|
243
|
+
if (format === "anthropic") return resolve(request.cache)
|
|
244
|
+
if (!RESPECTS_INLINE_HINTS.has(route)) return NONE
|
|
245
|
+
if (!auto) return resolve(request.cache)
|
|
246
|
+
if (route === "openrouter" || route === "vercel-ai-gateway-messages") return gatewayPolicy(request.model.id)
|
|
247
|
+
if (route === "alibaba-chat") return request.model.id.toLowerCase().startsWith("qwen") ? QWEN : NONE
|
|
248
|
+
return AUTO
|
|
249
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Context, Effect, Layer } from "effect"
|
|
2
|
+
import type {
|
|
3
|
+
DecisionAnswer,
|
|
4
|
+
DecisionModel,
|
|
5
|
+
DecisionOptions,
|
|
6
|
+
DecisionQuestion,
|
|
7
|
+
DecisionQuestions,
|
|
8
|
+
DecisionRequest,
|
|
9
|
+
DecisionResponseFor,
|
|
10
|
+
} from "./decision.js"
|
|
11
|
+
import { RequestExecutor } from "./route/executor.js"
|
|
12
|
+
import { AIError, InvalidProviderOutputError } from "./schema/index.js"
|
|
13
|
+
|
|
14
|
+
export type Execute = RequestExecutor.Interface["execute"]
|
|
15
|
+
|
|
16
|
+
export interface Interface {
|
|
17
|
+
readonly generate: <Options extends DecisionOptions, const Questions extends DecisionQuestions>(
|
|
18
|
+
request: DecisionRequest<DecisionModel<Options>, Questions>,
|
|
19
|
+
) => Effect.Effect<DecisionResponseFor<Questions>, AIError>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class Service extends Context.Service<Service, Interface>()("@zyro-sec/AI/DecisionClient") {}
|
|
23
|
+
|
|
24
|
+
export const generate = <Options extends DecisionOptions, const Questions extends DecisionQuestions>(
|
|
25
|
+
request: DecisionRequest<DecisionModel<Options>, Questions>,
|
|
26
|
+
): Effect.Effect<DecisionResponseFor<Questions>, AIError, Service> =>
|
|
27
|
+
Effect.flatMap(Service, (client) => client.generate(request))
|
|
28
|
+
|
|
29
|
+
export const layer: Layer.Layer<Service, never, RequestExecutor.Service> = Layer.effect(
|
|
30
|
+
Service,
|
|
31
|
+
Effect.gen(function* () {
|
|
32
|
+
const executor = yield* RequestExecutor.Service
|
|
33
|
+
return Service.of({
|
|
34
|
+
generate: Effect.fnUntraced(function* (request) {
|
|
35
|
+
const route = request.model.route
|
|
36
|
+
const response = yield* route.generate(request, executor.execute)
|
|
37
|
+
if (answersMatch(request.questions, response.answers))
|
|
38
|
+
return response as DecisionResponseFor<typeof request.questions>
|
|
39
|
+
return yield* new AIError({
|
|
40
|
+
reason: new InvalidProviderOutputError({
|
|
41
|
+
route: route.id,
|
|
42
|
+
message: "Decision answers do not match the requested questions",
|
|
43
|
+
cause: response.answers,
|
|
44
|
+
}),
|
|
45
|
+
})
|
|
46
|
+
}),
|
|
47
|
+
})
|
|
48
|
+
}),
|
|
49
|
+
)
|
|
50
|
+
export const fetchLayer = layer.pipe(Layer.provideMerge(RequestExecutor.fetchLayer))
|
|
51
|
+
|
|
52
|
+
/** Host bugs (a missing or extra answer, an option that wasn't offered, a score off the scale) fail the whole call. */
|
|
53
|
+
const answersMatch = (questions: ReadonlyArray<DecisionQuestion>, answers: ReadonlyArray<DecisionAnswer>) =>
|
|
54
|
+
questions.length === answers.length &&
|
|
55
|
+
questions.every((question, index) => {
|
|
56
|
+
const answer = answers[index]
|
|
57
|
+
if (answer.name !== question.name) return false
|
|
58
|
+
if (answer.type === "error") return true
|
|
59
|
+
if (question.type === "predicate") return answer.type === "predicate"
|
|
60
|
+
if (question.type === "choice") {
|
|
61
|
+
if (answer.type !== "choice") return false
|
|
62
|
+
const values = question.choices.map((choice) => choice.value)
|
|
63
|
+
return (
|
|
64
|
+
values.includes(answer.choice) &&
|
|
65
|
+
sameMembers(
|
|
66
|
+
values,
|
|
67
|
+
answer.probabilities.map((item) => item.value),
|
|
68
|
+
)
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
if (answer.type !== "score" || answer.score < 0 || answer.score > question.levels.length - 1) return false
|
|
72
|
+
return sameMembers(
|
|
73
|
+
question.levels.map((_, level) => level),
|
|
74
|
+
answer.probabilities.map((item) => item.value),
|
|
75
|
+
)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const sameMembers = <A>(expected: ReadonlyArray<A>, received: ReadonlyArray<A>) =>
|
|
79
|
+
expected.length === received.length &&
|
|
80
|
+
new Set(received).size === received.length &&
|
|
81
|
+
received.every((item) => expected.includes(item))
|
|
82
|
+
|
|
83
|
+
export const DecisionClient = {
|
|
84
|
+
Service,
|
|
85
|
+
layer,
|
|
86
|
+
fetchLayer,
|
|
87
|
+
generate,
|
|
88
|
+
} as const
|
package/src/decision.ts
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect"
|
|
2
|
+
import { DecisionClient, Service, type Execute } from "./decision-client.js"
|
|
3
|
+
import { Media } from "./media.js"
|
|
4
|
+
import { tryRequest } from "./media-model.js"
|
|
5
|
+
import {
|
|
6
|
+
AIError,
|
|
7
|
+
type BaseRequest,
|
|
8
|
+
type TransportOptions,
|
|
9
|
+
ModelID,
|
|
10
|
+
ProviderID,
|
|
11
|
+
ProviderMetadata,
|
|
12
|
+
ProviderOptions,
|
|
13
|
+
type OpenString,
|
|
14
|
+
} from "./schema/index.js"
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Input
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
export const DecisionTextPart = Schema.Struct({ type: Schema.Literal("text"), text: Schema.String })
|
|
21
|
+
export const DecisionJsonPart = Schema.Struct({ type: Schema.Literal("json"), value: Schema.Json })
|
|
22
|
+
export const DecisionMediaPart = Schema.Struct({
|
|
23
|
+
type: Schema.Literal("media"),
|
|
24
|
+
media: Media.AssetSchema,
|
|
25
|
+
filename: Schema.optional(Schema.String),
|
|
26
|
+
})
|
|
27
|
+
export const DecisionPart = Schema.Union([DecisionTextPart, DecisionJsonPart, DecisionMediaPart]).pipe(
|
|
28
|
+
Schema.toTaggedUnion("type"),
|
|
29
|
+
)
|
|
30
|
+
export type DecisionPart = Schema.Schema.Type<typeof DecisionPart>
|
|
31
|
+
|
|
32
|
+
/** A media part as input: `media` may be plain data, with `mediaType` naming its type when the content can't. */
|
|
33
|
+
export type DecisionMediaPartInput = Omit<typeof DecisionMediaPart.Type, "media"> & {
|
|
34
|
+
readonly media: Media.Input
|
|
35
|
+
readonly mediaType?: string
|
|
36
|
+
}
|
|
37
|
+
export type DecisionPartInput = Exclude<DecisionPart, { readonly type: "media" }> | DecisionMediaPartInput
|
|
38
|
+
|
|
39
|
+
/** A plain object is one `json` part. */
|
|
40
|
+
export type DecisionInput = typeof Schema.JsonObject.Type | ReadonlyArray<DecisionPartInput>
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Questions
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
/** Choice values are typed: `true` and `"true"` are different values. */
|
|
47
|
+
export const ChoiceValue = Schema.Union([Schema.String, Schema.Boolean])
|
|
48
|
+
export type ChoiceValue = Schema.Schema.Type<typeof ChoiceValue>
|
|
49
|
+
|
|
50
|
+
export const PredicateQuestion = Schema.Struct({
|
|
51
|
+
name: Schema.String,
|
|
52
|
+
type: Schema.Literal("predicate"),
|
|
53
|
+
instructions: Schema.String,
|
|
54
|
+
/** What counts as true and as false, for statements the instructions alone leave ambiguous. */
|
|
55
|
+
criteria: Schema.optional(
|
|
56
|
+
Schema.Struct({ true: Schema.optional(Schema.String), false: Schema.optional(Schema.String) }),
|
|
57
|
+
),
|
|
58
|
+
})
|
|
59
|
+
export type PredicateQuestion = Schema.Schema.Type<typeof PredicateQuestion>
|
|
60
|
+
|
|
61
|
+
export const ChoiceQuestion = Schema.Struct({
|
|
62
|
+
name: Schema.String,
|
|
63
|
+
type: Schema.Literal("choice"),
|
|
64
|
+
instructions: Schema.String,
|
|
65
|
+
choices: Schema.Array(Schema.Struct({ value: ChoiceValue, description: Schema.optional(Schema.String) })),
|
|
66
|
+
})
|
|
67
|
+
export type ChoiceQuestion = Schema.Schema.Type<typeof ChoiceQuestion>
|
|
68
|
+
|
|
69
|
+
export const ScoreQuestion = Schema.Struct({
|
|
70
|
+
name: Schema.String,
|
|
71
|
+
type: Schema.Literal("score"),
|
|
72
|
+
instructions: Schema.String,
|
|
73
|
+
levels: Schema.Array(Schema.Struct({ label: Schema.String, description: Schema.optional(Schema.String) })),
|
|
74
|
+
})
|
|
75
|
+
export type ScoreQuestion = Schema.Schema.Type<typeof ScoreQuestion>
|
|
76
|
+
|
|
77
|
+
export const DecisionQuestion = Schema.Union([PredicateQuestion, ChoiceQuestion, ScoreQuestion]).pipe(
|
|
78
|
+
Schema.toTaggedUnion("type"),
|
|
79
|
+
)
|
|
80
|
+
export type DecisionQuestion = Schema.Schema.Type<typeof DecisionQuestion>
|
|
81
|
+
export type DecisionQuestions = ReadonlyArray<DecisionQuestion>
|
|
82
|
+
|
|
83
|
+
// Only what makes a request meaningless is checked here; counts and sizes are the host's to enforce.
|
|
84
|
+
const DecisionQuestionList = Schema.Array(DecisionQuestion).pipe(
|
|
85
|
+
Schema.refine((x): x is typeof x => x.length > 0, { message: "Decision questions must not be empty" }),
|
|
86
|
+
Schema.refine((x): x is typeof x => new Set(x.map((question) => question.name)).size === x.length, {
|
|
87
|
+
message: "Decision question names must be unique",
|
|
88
|
+
}),
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// Answers
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }))
|
|
96
|
+
|
|
97
|
+
export const PredicateAnswer = Schema.Struct({
|
|
98
|
+
name: Schema.String,
|
|
99
|
+
type: Schema.Literal("predicate"),
|
|
100
|
+
probability: Probability,
|
|
101
|
+
})
|
|
102
|
+
export type PredicateAnswer = Schema.Schema.Type<typeof PredicateAnswer>
|
|
103
|
+
|
|
104
|
+
export const ChoiceAnswer = Schema.Struct({
|
|
105
|
+
name: Schema.String,
|
|
106
|
+
type: Schema.Literal("choice"),
|
|
107
|
+
choice: ChoiceValue,
|
|
108
|
+
probabilities: Schema.Array(Schema.Struct({ value: ChoiceValue, probability: Probability })),
|
|
109
|
+
confidence: Probability,
|
|
110
|
+
})
|
|
111
|
+
export type ChoiceAnswer = Schema.Schema.Type<typeof ChoiceAnswer>
|
|
112
|
+
|
|
113
|
+
/** `score` is the probability-weighted level index; `probabilities` has one entry per level. */
|
|
114
|
+
export const ScoreAnswer = Schema.Struct({
|
|
115
|
+
name: Schema.String,
|
|
116
|
+
type: Schema.Literal("score"),
|
|
117
|
+
score: Schema.Number,
|
|
118
|
+
probabilities: Schema.Array(Schema.Struct({ value: Schema.Int, label: Schema.String, probability: Probability })),
|
|
119
|
+
confidence: Probability,
|
|
120
|
+
})
|
|
121
|
+
export type ScoreAnswer = Schema.Schema.Type<typeof ScoreAnswer>
|
|
122
|
+
|
|
123
|
+
/** Why a question has no answer; hosts may report reasons beyond these. */
|
|
124
|
+
export type ErrorReason = OpenString<"refusal">
|
|
125
|
+
export const ErrorReason: Schema.Codec<ErrorReason> = Schema.Union([Schema.Literal("refusal"), Schema.String])
|
|
126
|
+
|
|
127
|
+
/** This one question has no answer; the other answers still came back. */
|
|
128
|
+
export const ErrorAnswer = Schema.Struct({
|
|
129
|
+
name: Schema.String,
|
|
130
|
+
type: Schema.Literal("error"),
|
|
131
|
+
reason: ErrorReason,
|
|
132
|
+
message: Schema.optional(Schema.String),
|
|
133
|
+
})
|
|
134
|
+
export type ErrorAnswer = Schema.Schema.Type<typeof ErrorAnswer>
|
|
135
|
+
|
|
136
|
+
export const DecisionAnswer = Schema.Union([PredicateAnswer, ChoiceAnswer, ScoreAnswer, ErrorAnswer]).pipe(
|
|
137
|
+
Schema.toTaggedUnion("type"),
|
|
138
|
+
)
|
|
139
|
+
export type DecisionAnswer = Schema.Schema.Type<typeof DecisionAnswer>
|
|
140
|
+
|
|
141
|
+
type ChoiceValueOf<Question> = Question extends { readonly choices: ReadonlyArray<{ readonly value: infer Value }> }
|
|
142
|
+
? Value
|
|
143
|
+
: never
|
|
144
|
+
|
|
145
|
+
export type AnswerFor<Question extends DecisionQuestion> =
|
|
146
|
+
| (Question extends { readonly type: "predicate" }
|
|
147
|
+
? PredicateAnswer & { readonly name: Question["name"] }
|
|
148
|
+
: Question extends { readonly type: "choice" }
|
|
149
|
+
? {
|
|
150
|
+
readonly name: Question["name"]
|
|
151
|
+
readonly type: "choice"
|
|
152
|
+
readonly choice: ChoiceValueOf<Question>
|
|
153
|
+
readonly probabilities: ReadonlyArray<{
|
|
154
|
+
readonly value: ChoiceValueOf<Question>
|
|
155
|
+
readonly probability: number
|
|
156
|
+
}>
|
|
157
|
+
readonly confidence: number
|
|
158
|
+
}
|
|
159
|
+
: ScoreAnswer & { readonly name: Question["name"] })
|
|
160
|
+
| (ErrorAnswer & { readonly name: Question["name"] })
|
|
161
|
+
|
|
162
|
+
/** One answer per question, in question order. */
|
|
163
|
+
export type AnswersFor<Questions extends DecisionQuestions> = {
|
|
164
|
+
readonly [Index in keyof Questions]: AnswerFor<Questions[Index]>
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
// Model
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
export type DecisionOptions = Record<string, unknown>
|
|
172
|
+
|
|
173
|
+
/** What a decision model's route does; `DecisionRoute.make(...).model(...)` builds one from a protocol. */
|
|
174
|
+
export interface DecisionModelRoute<Options extends DecisionOptions = DecisionOptions> {
|
|
175
|
+
readonly id: string
|
|
176
|
+
generate(request: DecisionRequest<DecisionModel<Options>>, execute: Execute): Effect.Effect<DecisionResponse, AIError>
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Host quirks a protocol adapts to, like `LanguageModelCompatibility` for language models. */
|
|
180
|
+
export class DecisionModelCompatibility extends Schema.Class<DecisionModelCompatibility>("Decision.ModelCompatibility")(
|
|
181
|
+
{
|
|
182
|
+
/**
|
|
183
|
+
* Where a System One host reads media, from models.dev `[api.systemone] compatibility.media_input`, named after
|
|
184
|
+
* where the media goes in the body: `assets` (TypeSafe's assets map, referenced from the state), `state` (items
|
|
185
|
+
* inside a state list) or `root` (one top-level list per kind: `images`, `audio`, `videos`).
|
|
186
|
+
*/
|
|
187
|
+
mediaInput: Schema.optional(Schema.Literals(["assets", "state", "root"])),
|
|
188
|
+
},
|
|
189
|
+
) {}
|
|
190
|
+
|
|
191
|
+
export namespace DecisionModelCompatibility {
|
|
192
|
+
export type Input = DecisionModelCompatibility | ConstructorParameters<typeof DecisionModelCompatibility>[0]
|
|
193
|
+
|
|
194
|
+
export const make = (input: Input) =>
|
|
195
|
+
input instanceof DecisionModelCompatibility ? input : new DecisionModelCompatibility(input)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export class DecisionModel<Options extends DecisionOptions = DecisionOptions> {
|
|
199
|
+
declare protected readonly _Options: Options
|
|
200
|
+
readonly id: ModelID
|
|
201
|
+
readonly provider: ProviderID
|
|
202
|
+
readonly route: DecisionModelRoute<Options>
|
|
203
|
+
/** Deployment headers, timeouts and `http` overlays, below each request's own. */
|
|
204
|
+
readonly transport?: TransportOptions
|
|
205
|
+
readonly compatibility?: DecisionModelCompatibility
|
|
206
|
+
/** Defaults under each request's `providerOptions`, such as options a provider package reads from its settings. */
|
|
207
|
+
readonly providerOptions?: ProviderOptions
|
|
208
|
+
|
|
209
|
+
constructor(input: DecisionModel.Input<Options>) {
|
|
210
|
+
this.id = input.id
|
|
211
|
+
this.provider = input.provider
|
|
212
|
+
this.route = input.route
|
|
213
|
+
this.transport = input.transport
|
|
214
|
+
this.compatibility = input.compatibility
|
|
215
|
+
this.providerOptions = input.providerOptions
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static make<Options extends DecisionOptions = DecisionOptions>(input: DecisionModel.MakeInput<Options>) {
|
|
219
|
+
return new DecisionModel<Options>({
|
|
220
|
+
...input,
|
|
221
|
+
id: ModelID.make(input.id),
|
|
222
|
+
provider: ProviderID.make(input.provider),
|
|
223
|
+
})
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export namespace DecisionModel {
|
|
228
|
+
export interface Input<Options extends DecisionOptions = DecisionOptions> {
|
|
229
|
+
readonly id: ModelID
|
|
230
|
+
readonly provider: ProviderID
|
|
231
|
+
readonly route: DecisionModelRoute<Options>
|
|
232
|
+
readonly transport?: TransportOptions
|
|
233
|
+
readonly compatibility?: DecisionModelCompatibility
|
|
234
|
+
readonly providerOptions?: ProviderOptions
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export interface MakeInput<Options extends DecisionOptions = DecisionOptions>
|
|
238
|
+
extends Omit<Input<Options>, "id" | "provider"> {
|
|
239
|
+
readonly id: string | ModelID
|
|
240
|
+
readonly provider: string | ProviderID
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export const DecisionModelSchema = Schema.declare((value): value is DecisionModel => value instanceof DecisionModel, {
|
|
245
|
+
expected: "Decision.Model",
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
export type DecisionModelOptions<Model> = Model extends DecisionModel<infer Options> ? Options : never
|
|
249
|
+
|
|
250
|
+
// ---------------------------------------------------------------------------
|
|
251
|
+
// Request and response
|
|
252
|
+
// ---------------------------------------------------------------------------
|
|
253
|
+
|
|
254
|
+
const DecisionRequestSchema = Schema.Struct({
|
|
255
|
+
model: DecisionModelSchema,
|
|
256
|
+
input: Schema.Array(DecisionPart),
|
|
257
|
+
questions: DecisionQuestionList,
|
|
258
|
+
providerOptions: Schema.optional(ProviderOptions),
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
/** A normalized decision request, the plain object `ai.decision.request(...)` returns. */
|
|
262
|
+
export interface DecisionRequest<
|
|
263
|
+
Model extends DecisionModel = DecisionModel,
|
|
264
|
+
Questions extends DecisionQuestions = DecisionQuestions,
|
|
265
|
+
> extends BaseRequest<Model, DecisionModelOptions<Model>> {
|
|
266
|
+
readonly input: ReadonlyArray<DecisionPart>
|
|
267
|
+
readonly questions: Questions
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Input accepted by `ai.decision.request` and `generate`. A `DecisionRequest` is also a valid input. */
|
|
271
|
+
export interface DecisionRequestInput<
|
|
272
|
+
Model extends DecisionModel = DecisionModel,
|
|
273
|
+
Questions extends DecisionQuestions = DecisionQuestions,
|
|
274
|
+
> extends BaseRequest<Model, NoInfer<DecisionModelOptions<Model>>> {
|
|
275
|
+
readonly input: DecisionInput
|
|
276
|
+
readonly questions: Questions
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** What decision hosts report; their raw usage stays in the response's `providerMetadata`. */
|
|
280
|
+
export class DecisionUsage extends Schema.Class<DecisionUsage>("Decision.Usage")({
|
|
281
|
+
inputTokens: Schema.optional(Schema.Number),
|
|
282
|
+
outputTokens: Schema.optional(Schema.Number),
|
|
283
|
+
totalTokens: Schema.optional(Schema.Number),
|
|
284
|
+
/** USD the host says it charged for this request. */
|
|
285
|
+
cost: Schema.optional(Schema.Number),
|
|
286
|
+
}) {}
|
|
287
|
+
|
|
288
|
+
export class DecisionResponse extends Schema.Class<DecisionResponse>("Decision.Response")({
|
|
289
|
+
answers: Schema.Array(DecisionAnswer),
|
|
290
|
+
usage: Schema.optional(DecisionUsage),
|
|
291
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
292
|
+
}) {}
|
|
293
|
+
|
|
294
|
+
export type DecisionResponseFor<Questions extends DecisionQuestions> = Omit<DecisionResponse, "answers"> & {
|
|
295
|
+
readonly answers: AnswersFor<Questions>
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ---------------------------------------------------------------------------
|
|
299
|
+
// Call API
|
|
300
|
+
// ---------------------------------------------------------------------------
|
|
301
|
+
|
|
302
|
+
/** Normalize decision request input into a plain `DecisionRequest`; a plain object `input` becomes one `json` part. */
|
|
303
|
+
export const request = <const Model extends DecisionModel, const Questions extends DecisionQuestions>(
|
|
304
|
+
input: DecisionRequestInput<Model, Questions>,
|
|
305
|
+
): DecisionRequest<Model, Questions> => ({
|
|
306
|
+
...input,
|
|
307
|
+
input: isParts(input.input) ? input.input.map(decisionPart) : [{ type: "json", value: input.input }],
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
const validate = Schema.decodeUnknownSync(DecisionRequestSchema)
|
|
311
|
+
|
|
312
|
+
// `Array.isArray` doesn't narrow a readonly array union, so the object branch would keep the parts type.
|
|
313
|
+
const isParts = (input: DecisionInput): input is ReadonlyArray<DecisionPartInput> => Array.isArray(input)
|
|
314
|
+
|
|
315
|
+
const decisionPart = (part: DecisionPartInput): DecisionPart => {
|
|
316
|
+
if (part.type !== "media") return part
|
|
317
|
+
const { mediaType, ...rest } = part
|
|
318
|
+
return { ...rest, media: Media.make(part.media, mediaType) }
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function generate<const Model extends DecisionModel, const Questions extends DecisionQuestions>(
|
|
322
|
+
input: DecisionRequestInput<Model, Questions>,
|
|
323
|
+
): Effect.Effect<DecisionResponseFor<Questions>, AIError, Service>
|
|
324
|
+
export function generate(input: DecisionRequestInput) {
|
|
325
|
+
return tryRequest(() => {
|
|
326
|
+
const normalized = request(input)
|
|
327
|
+
validate(normalized)
|
|
328
|
+
return normalized
|
|
329
|
+
}).pipe(Effect.flatMap(DecisionClient.generate))
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export const Decision = {
|
|
333
|
+
request,
|
|
334
|
+
generate,
|
|
335
|
+
} as const
|