@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,335 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect"
|
|
2
|
+
import {
|
|
3
|
+
DecisionResponse,
|
|
4
|
+
DecisionUsage,
|
|
5
|
+
type DecisionAnswer,
|
|
6
|
+
type DecisionModelCompatibility,
|
|
7
|
+
type DecisionPart,
|
|
8
|
+
type DecisionQuestion,
|
|
9
|
+
type DecisionRequest,
|
|
10
|
+
} from "../decision.js"
|
|
11
|
+
import type { Media } from "../media.js"
|
|
12
|
+
import { DecisionRoute } from "../route/decision.js"
|
|
13
|
+
import { Endpoint } from "../route/endpoint.js"
|
|
14
|
+
import { MediaProtocol } from "../route/media-protocol.js"
|
|
15
|
+
import { AIError, UnsupportedOperationError, mergeJsonRecords } from "../schema/index.js"
|
|
16
|
+
import { encodeJson } from "../utils/json.js"
|
|
17
|
+
import { JsonObject, ProviderShared } from "./shared.js"
|
|
18
|
+
|
|
19
|
+
export const DEFAULT_PATH = "/systemone"
|
|
20
|
+
|
|
21
|
+
export const DEFAULT_BASE_URL = "https://api.typesafe.ai/v1"
|
|
22
|
+
|
|
23
|
+
/** `state.messages` is Baseten's own placement (parts in a user message inside `state.messages`), set by its provider. */
|
|
24
|
+
type MediaInput = NonNullable<DecisionModelCompatibility["mediaInput"]> | "state.messages"
|
|
25
|
+
|
|
26
|
+
const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }))
|
|
27
|
+
const Answer = Schema.Union([
|
|
28
|
+
Schema.Struct({ type: Schema.Literal("noul"), noul: Probability }),
|
|
29
|
+
Schema.Struct({
|
|
30
|
+
type: Schema.Literal("choice"),
|
|
31
|
+
choice: Schema.String,
|
|
32
|
+
probabilities: Schema.Record(Schema.String, Probability),
|
|
33
|
+
confidence: Probability,
|
|
34
|
+
}),
|
|
35
|
+
Schema.Struct({
|
|
36
|
+
type: Schema.Literal("score"),
|
|
37
|
+
score: Schema.Number,
|
|
38
|
+
probabilities: Schema.Record(Schema.String, Probability),
|
|
39
|
+
confidence: Probability,
|
|
40
|
+
}),
|
|
41
|
+
// OpenRouter's and OpenCode Zen's answer for a question the upstream model declined.
|
|
42
|
+
Schema.Struct({ type: Schema.Literal("refusal"), message: Schema.optional(Schema.String) }),
|
|
43
|
+
]).pipe(Schema.toTaggedUnion("type"))
|
|
44
|
+
const NativeUsage = Schema.StructWithRest(
|
|
45
|
+
Schema.Struct({
|
|
46
|
+
input_tokens: Schema.optional(Schema.Number),
|
|
47
|
+
output_tokens: Schema.optional(Schema.Number),
|
|
48
|
+
cost: Schema.optional(Schema.Number),
|
|
49
|
+
}),
|
|
50
|
+
[JsonObject],
|
|
51
|
+
)
|
|
52
|
+
export const Response = Schema.Struct({
|
|
53
|
+
model: Schema.String,
|
|
54
|
+
answers: Schema.Record(Schema.String, Answer),
|
|
55
|
+
usage: Schema.optional(NativeUsage),
|
|
56
|
+
id: Schema.optional(Schema.String),
|
|
57
|
+
provider: Schema.optional(Schema.String),
|
|
58
|
+
provider_metadata: Schema.optional(Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Unknown))),
|
|
59
|
+
})
|
|
60
|
+
export type Response = Schema.Schema.Type<typeof Response>
|
|
61
|
+
|
|
62
|
+
const ID = "system-one"
|
|
63
|
+
const NAME = "System One"
|
|
64
|
+
|
|
65
|
+
/** The System One body without `model`; media goes where the model's `mediaInput` says, else `fallback`. */
|
|
66
|
+
export const encodeBody = Effect.fn("SystemOne.encodeBody")(function* (request: DecisionRequest, fallback: MediaInput) {
|
|
67
|
+
const questions = yield* encodeQuestions(request.questions)
|
|
68
|
+
const placed = yield* placeMedia(request, request.model.compatibility?.mediaInput ?? fallback)
|
|
69
|
+
return { ...mergeJsonRecords(request.providerOptions, request.http?.body), ...placed, questions }
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const encodeQuestions = (questions: ReadonlyArray<DecisionQuestion>) =>
|
|
73
|
+
Effect.forEach(questions, (question): Effect.Effect<readonly [string, unknown], AIError> => {
|
|
74
|
+
if (question.type === "predicate")
|
|
75
|
+
return Effect.succeed([
|
|
76
|
+
question.name,
|
|
77
|
+
{
|
|
78
|
+
type: "noul",
|
|
79
|
+
instructions: question.instructions,
|
|
80
|
+
...(question.criteria === undefined ? {} : { criteria: question.criteria }),
|
|
81
|
+
},
|
|
82
|
+
] as const)
|
|
83
|
+
if (question.type === "score")
|
|
84
|
+
return Effect.succeed([
|
|
85
|
+
question.name,
|
|
86
|
+
{
|
|
87
|
+
type: "score",
|
|
88
|
+
instructions: question.instructions,
|
|
89
|
+
criteria: question.levels.map((level) =>
|
|
90
|
+
level.description === undefined ? level.label : `${level.label}: ${level.description}`,
|
|
91
|
+
),
|
|
92
|
+
},
|
|
93
|
+
] as const)
|
|
94
|
+
// Options are map keys, so `true` is sent as "true" and mapped back by `choiceValue`.
|
|
95
|
+
const keys = question.choices.map((choice) => String(choice.value))
|
|
96
|
+
if (new Set(keys).size !== keys.length)
|
|
97
|
+
return Effect.fail(
|
|
98
|
+
ProviderShared.invalidRequest(
|
|
99
|
+
`Decision choice options are sent as string keys; "${question.name}" has a boolean and a string with the same text`,
|
|
100
|
+
),
|
|
101
|
+
)
|
|
102
|
+
return Effect.succeed([
|
|
103
|
+
question.name,
|
|
104
|
+
{
|
|
105
|
+
type: "choice",
|
|
106
|
+
instructions: question.instructions,
|
|
107
|
+
criteria: Object.fromEntries(
|
|
108
|
+
question.choices.map((choice) => [String(choice.value), choice.description ?? null]),
|
|
109
|
+
),
|
|
110
|
+
},
|
|
111
|
+
] as const)
|
|
112
|
+
}).pipe(Effect.map(Object.fromEntries))
|
|
113
|
+
|
|
114
|
+
const placeMedia = Effect.fnUntraced(function* (request: DecisionRequest, mediaInput: MediaInput) {
|
|
115
|
+
const parts = request.input
|
|
116
|
+
const unsupported = (asset: Media.Asset, accepted: string) =>
|
|
117
|
+
new AIError({
|
|
118
|
+
reason: new UnsupportedOperationError({
|
|
119
|
+
operation: "decision.media",
|
|
120
|
+
provider: request.model.provider,
|
|
121
|
+
route: request.model.route.id,
|
|
122
|
+
message: `${NAME} takes ${accepted} here; got ${asset.mediaType}`,
|
|
123
|
+
}),
|
|
124
|
+
})
|
|
125
|
+
const media = parts.flatMap((part) => (part.type === "media" ? [part.media] : []))
|
|
126
|
+
if (media.length === 0) return { state: state(parts.map(plain)) }
|
|
127
|
+
if (mediaInput === "state")
|
|
128
|
+
return { state: yield* Effect.forEach(parts, (part) => stateItem(part, unsupported, true)) }
|
|
129
|
+
// Chat-style content in one user message: text and JSON as text parts, media as in `state`.
|
|
130
|
+
if (mediaInput === "state.messages")
|
|
131
|
+
return {
|
|
132
|
+
state: {
|
|
133
|
+
messages: [
|
|
134
|
+
{
|
|
135
|
+
role: "user",
|
|
136
|
+
content: yield* Effect.forEach(parts, (part) =>
|
|
137
|
+
part.type === "media"
|
|
138
|
+
? stateItem(part, unsupported, false)
|
|
139
|
+
: Effect.succeed({ type: "text", text: part.type === "text" ? part.text : encodeJson(part.value) }),
|
|
140
|
+
),
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
if (mediaInput === "root") {
|
|
146
|
+
const lists = { image: "images", audio: "audio", video: "videos" } as const
|
|
147
|
+
const unplaced = media.find((asset) => !Object.hasOwn(lists, asset.kind))
|
|
148
|
+
if (unplaced !== undefined) return yield* unsupported(unplaced, "images, audio and video")
|
|
149
|
+
const inline = yield* Effect.forEach(media, (asset) => ProviderShared.requireInlineMedia(NAME, asset))
|
|
150
|
+
const fields = Object.fromEntries(
|
|
151
|
+
Object.entries(lists).flatMap(([kind, field]) => {
|
|
152
|
+
const items = inline.filter((_, index) => media[index].kind === kind).map((item) => item.dataUrl)
|
|
153
|
+
return items.length === 0 ? [] : [[field, items]]
|
|
154
|
+
}),
|
|
155
|
+
)
|
|
156
|
+
return { state: state(parts.filter((part) => part.type !== "media").map(plain)), ...fields }
|
|
157
|
+
}
|
|
158
|
+
const unplaced = media.find((asset) => asset.kind !== "image")
|
|
159
|
+
if (unplaced !== undefined) return yield* unsupported(unplaced, "images")
|
|
160
|
+
const inline = yield* Effect.forEach(media, (asset) => ProviderShared.requireInlineMedia(NAME, asset))
|
|
161
|
+
// Every occurrence of a reference string in the body is replaced with its image, so it must not be ordinary text.
|
|
162
|
+
const reference = (index: number) => `<image-${index + 1}>`
|
|
163
|
+
const items = parts.reduce<{ readonly next: number; readonly values: ReadonlyArray<unknown> }>(
|
|
164
|
+
(acc, part) =>
|
|
165
|
+
part.type === "media"
|
|
166
|
+
? { next: acc.next + 1, values: [...acc.values, reference(acc.next)] }
|
|
167
|
+
: { next: acc.next, values: [...acc.values, plain(part)] },
|
|
168
|
+
{ next: 0, values: [] },
|
|
169
|
+
).values
|
|
170
|
+
return {
|
|
171
|
+
state: items,
|
|
172
|
+
assets: Object.fromEntries(
|
|
173
|
+
inline.map((item, index) => [
|
|
174
|
+
reference(index),
|
|
175
|
+
{ type: "image", format: item.mime.slice("image/".length), data_base64: item.base64 },
|
|
176
|
+
]),
|
|
177
|
+
),
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
/** A lone text or JSON part is the state itself; several are a list. */
|
|
182
|
+
const state = (items: ReadonlyArray<unknown>) => (items.length === 1 ? items[0] : items)
|
|
183
|
+
|
|
184
|
+
const plain = (part: DecisionPart) => {
|
|
185
|
+
if (part.type === "text") return part.text
|
|
186
|
+
if (part.type === "json") return part.value
|
|
187
|
+
return undefined
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** A media part as a Chat Completions content part; `files` sends other media, such as PDFs, as `file` parts. */
|
|
191
|
+
const stateItem = (
|
|
192
|
+
part: DecisionPart,
|
|
193
|
+
unsupported: (asset: Media.Asset, accepted: string) => AIError,
|
|
194
|
+
files: boolean,
|
|
195
|
+
): Effect.Effect<unknown, AIError> => {
|
|
196
|
+
if (part.type !== "media") return Effect.succeed(plain(part))
|
|
197
|
+
const asset = part.media
|
|
198
|
+
if (!files && asset.kind !== "image" && asset.kind !== "audio" && asset.kind !== "video")
|
|
199
|
+
return Effect.fail(unsupported(asset, "images, audio and video"))
|
|
200
|
+
return ProviderShared.requireInlineMedia(NAME, asset).pipe(
|
|
201
|
+
Effect.map((inline) => {
|
|
202
|
+
if (asset.kind === "image") return { type: "image_url", image_url: { url: inline.dataUrl } }
|
|
203
|
+
if (asset.kind === "video") return { type: "video_url", video_url: { url: inline.dataUrl } }
|
|
204
|
+
if (asset.kind === "audio") {
|
|
205
|
+
const format = inline.mime.slice("audio/".length).toLowerCase()
|
|
206
|
+
return { type: "input_audio", input_audio: { data: inline.base64, format: audioFormats[format] ?? format } }
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
type: "file",
|
|
210
|
+
file: { file_data: inline.dataUrl, ...(part.filename === undefined ? {} : { filename: part.filename }) },
|
|
211
|
+
}
|
|
212
|
+
}),
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** OpenRouter's `input_audio` format names for MIME subtypes that aren't one themselves. */
|
|
217
|
+
const audioFormats: Readonly<Record<string, string>> = {
|
|
218
|
+
"x-wav": "wav",
|
|
219
|
+
wave: "wav",
|
|
220
|
+
"vnd.wave": "wav",
|
|
221
|
+
"x-m4a": "m4a",
|
|
222
|
+
"x-aiff": "aiff",
|
|
223
|
+
"x-flac": "flac",
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Map System One's answers, keyed by question name, back to the request's question order. */
|
|
227
|
+
export const toResponse = (
|
|
228
|
+
data: Response,
|
|
229
|
+
request: DecisionRequest,
|
|
230
|
+
providerMetadataKey: string,
|
|
231
|
+
invalid: (message: string) => AIError,
|
|
232
|
+
) => {
|
|
233
|
+
const names = request.questions.map((question) => question.name)
|
|
234
|
+
if (Object.keys(data.answers).length !== names.length || !names.every((name) => Object.hasOwn(data.answers, name)))
|
|
235
|
+
return Effect.fail(invalid("System One answers do not match the requested questions"))
|
|
236
|
+
return Effect.succeed(
|
|
237
|
+
new DecisionResponse({
|
|
238
|
+
answers: request.questions.map((question) => toAnswer(question, data.answers[question.name])),
|
|
239
|
+
usage:
|
|
240
|
+
data.usage === undefined
|
|
241
|
+
? undefined
|
|
242
|
+
: new DecisionUsage({
|
|
243
|
+
inputTokens: data.usage.input_tokens,
|
|
244
|
+
outputTokens: data.usage.output_tokens,
|
|
245
|
+
totalTokens: ProviderShared.totalTokens(data.usage.input_tokens, data.usage.output_tokens, undefined),
|
|
246
|
+
cost: data.usage.cost,
|
|
247
|
+
}),
|
|
248
|
+
providerMetadata: {
|
|
249
|
+
[providerMetadataKey]: {
|
|
250
|
+
model: data.model,
|
|
251
|
+
...(data.usage === undefined ? {} : { usage: data.usage }),
|
|
252
|
+
...(data.id === undefined ? {} : { responseId: data.id }),
|
|
253
|
+
...(data.provider === undefined ? {} : { provider: data.provider }),
|
|
254
|
+
...data.provider_metadata?.[providerMetadataKey],
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
}),
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const toAnswer = (question: DecisionQuestion, answer: typeof Answer.Type): DecisionAnswer => {
|
|
262
|
+
if (answer.type === "refusal")
|
|
263
|
+
return {
|
|
264
|
+
name: question.name,
|
|
265
|
+
type: "error",
|
|
266
|
+
reason: "refusal",
|
|
267
|
+
...(answer.message === undefined ? {} : { message: answer.message }),
|
|
268
|
+
}
|
|
269
|
+
if (answer.type === "noul") return { name: question.name, type: "predicate", probability: answer.noul }
|
|
270
|
+
if (answer.type === "choice") {
|
|
271
|
+
const choices = question.type === "choice" ? question.choices : []
|
|
272
|
+
const value = (key: string) => choices.find((choice) => String(choice.value) === key)?.value ?? key
|
|
273
|
+
const rank = (key: string) => {
|
|
274
|
+
const index = choices.findIndex((choice) => String(choice.value) === key)
|
|
275
|
+
return index === -1 ? choices.length : index
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
name: question.name,
|
|
279
|
+
type: "choice",
|
|
280
|
+
choice: value(answer.choice),
|
|
281
|
+
probabilities: Object.entries(answer.probabilities)
|
|
282
|
+
.toSorted(([a], [b]) => rank(a) - rank(b))
|
|
283
|
+
.map(([key, probability]) => ({ value: value(key), probability })),
|
|
284
|
+
confidence: answer.confidence,
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const levels = question.type === "score" ? question.levels : []
|
|
288
|
+
return {
|
|
289
|
+
name: question.name,
|
|
290
|
+
type: "score",
|
|
291
|
+
score: answer.score,
|
|
292
|
+
probabilities: Object.entries(answer.probabilities)
|
|
293
|
+
.map(([key, probability]) => ({ value: Number(key), label: levels[Number(key)]?.label ?? key, probability }))
|
|
294
|
+
.toSorted((a, b) => a.value - b.value),
|
|
295
|
+
confidence: answer.confidence,
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const decodeDocument = MediaProtocol.identity({ id: ID, name: NAME, provider: "typesafe-ai" }).decodeJson(Response)
|
|
300
|
+
|
|
301
|
+
export const protocol = DecisionRoute.protocol({
|
|
302
|
+
id: ID,
|
|
303
|
+
name: NAME,
|
|
304
|
+
inlineMedia: true,
|
|
305
|
+
body: {
|
|
306
|
+
from: (request) => encodeBody(request, "assets").pipe(Effect.map((body) => ({ ...body, model: request.model.id }))),
|
|
307
|
+
},
|
|
308
|
+
response: {
|
|
309
|
+
decode: (response, context) =>
|
|
310
|
+
decodeDocument(response).pipe(
|
|
311
|
+
Effect.flatMap((output) =>
|
|
312
|
+
toResponse(output.value, context.request, context.providerMetadataKey, output.invalid),
|
|
313
|
+
),
|
|
314
|
+
),
|
|
315
|
+
},
|
|
316
|
+
})
|
|
317
|
+
|
|
318
|
+
/** TypeSafe's API, the reference System One host; other hosts change the endpoint and auth with `route.with(...)`. */
|
|
319
|
+
export const route = DecisionRoute.make({
|
|
320
|
+
id: ID,
|
|
321
|
+
provider: "typesafe-ai",
|
|
322
|
+
protocol,
|
|
323
|
+
endpoint: Endpoint.path("/systemone", { baseURL: DEFAULT_BASE_URL }),
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
export const SystemOne = {
|
|
327
|
+
Answer,
|
|
328
|
+
Response,
|
|
329
|
+
encodeBody,
|
|
330
|
+
encodeQuestions,
|
|
331
|
+
toAnswer,
|
|
332
|
+
toResponse,
|
|
333
|
+
protocol,
|
|
334
|
+
route,
|
|
335
|
+
} as const
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { AwsV4Signer } from "aws4fetch"
|
|
2
|
+
import { Effect } from "effect"
|
|
3
|
+
import { Headers } from "effect/http"
|
|
4
|
+
import { Auth, type AuthInput } from "../../route/auth.js"
|
|
5
|
+
import { AIError, AuthenticationError } from "../../schema/index.js"
|
|
6
|
+
import { ProviderShared } from "../shared.js"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* AWS credentials for SigV4 signing. Bedrock also supports Bearer API key auth,
|
|
10
|
+
* which provider facades configure as route auth instead of SigV4.
|
|
11
|
+
*/
|
|
12
|
+
export interface Credentials {
|
|
13
|
+
readonly region: string
|
|
14
|
+
readonly accessKeyId: string
|
|
15
|
+
readonly secretAccessKey: string
|
|
16
|
+
readonly sessionToken?: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Static credentials or an effect resolved before every request. */
|
|
20
|
+
export type CredentialSource = Credentials | Effect.Effect<Credentials, AIError>
|
|
21
|
+
|
|
22
|
+
export interface DefaultChainOptions {
|
|
23
|
+
readonly region: string
|
|
24
|
+
/** Shared config profile passed to the AWS default chain. */
|
|
25
|
+
readonly profile?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Resolve credentials through the AWS default provider chain: environment
|
|
30
|
+
* variables, shared config and SSO caches, web identity tokens, process
|
|
31
|
+
* credentials, and container or instance metadata. A fresh chain runs on every
|
|
32
|
+
* request so credentials rotated on disk without an expiration (for example
|
|
33
|
+
* shared-config keys rewritten by a corporate SSO tool) are always re-read;
|
|
34
|
+
* the SDK's own memoization would otherwise pin them for the process lifetime.
|
|
35
|
+
*/
|
|
36
|
+
export const defaultChain = (options: DefaultChainOptions): Effect.Effect<Credentials, AIError> =>
|
|
37
|
+
Effect.tryPromise({
|
|
38
|
+
try: async () => {
|
|
39
|
+
const { fromNodeProviderChain } = await import("@aws-sdk/credential-providers")
|
|
40
|
+
// ignoreCache re-reads shared config and SSO token files, so `aws sso login` takes effect without a restart.
|
|
41
|
+
const identity = await fromNodeProviderChain({ ignoreCache: true, profile: options.profile })()
|
|
42
|
+
return {
|
|
43
|
+
region: options.region,
|
|
44
|
+
accessKeyId: identity.accessKeyId,
|
|
45
|
+
secretAccessKey: identity.secretAccessKey,
|
|
46
|
+
...(identity.sessionToken === undefined ? {} : { sessionToken: identity.sessionToken }),
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
catch: (error) =>
|
|
50
|
+
new AIError({
|
|
51
|
+
reason: new AuthenticationError({
|
|
52
|
+
message: `AWS default credential chain failed: ${ProviderShared.errorText(error)}`,
|
|
53
|
+
cause: error,
|
|
54
|
+
}),
|
|
55
|
+
}),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const signRequest = (input: {
|
|
59
|
+
readonly url: string
|
|
60
|
+
readonly body: string
|
|
61
|
+
readonly headers: Headers.Headers
|
|
62
|
+
readonly credentials: Credentials
|
|
63
|
+
readonly service: string
|
|
64
|
+
readonly name: string
|
|
65
|
+
}) =>
|
|
66
|
+
Effect.tryPromise({
|
|
67
|
+
try: async () => {
|
|
68
|
+
const signed = await new AwsV4Signer({
|
|
69
|
+
url: input.url,
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: Object.entries(input.headers),
|
|
72
|
+
body: input.body,
|
|
73
|
+
region: input.credentials.region,
|
|
74
|
+
accessKeyId: input.credentials.accessKeyId,
|
|
75
|
+
secretAccessKey: input.credentials.secretAccessKey,
|
|
76
|
+
sessionToken: input.credentials.sessionToken,
|
|
77
|
+
service: input.service,
|
|
78
|
+
}).sign()
|
|
79
|
+
return Object.fromEntries(signed.headers.entries())
|
|
80
|
+
},
|
|
81
|
+
catch: (error) =>
|
|
82
|
+
ProviderShared.invalidRequest(
|
|
83
|
+
`${input.name} SigV4 signing failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
84
|
+
),
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
/** Sign the exact JSON bytes with SigV4 using credentials configured on the route. */
|
|
88
|
+
export const sigV4 = (
|
|
89
|
+
source: CredentialSource | undefined,
|
|
90
|
+
options: { readonly service?: string; readonly name?: string } = {},
|
|
91
|
+
) =>
|
|
92
|
+
Auth.custom((input: AuthInput) => {
|
|
93
|
+
return Effect.gen(function* () {
|
|
94
|
+
if (!source) {
|
|
95
|
+
return yield* ProviderShared.invalidRequest(
|
|
96
|
+
`${options.name ?? "Bedrock Converse"} requires either route bearer auth or AWS credentials configured on the route`,
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
const credentials = Effect.isEffect(source) ? yield* source : source
|
|
100
|
+
const headersForSigning = Headers.set(input.headers, "content-type", "application/json")
|
|
101
|
+
const signed = yield* signRequest({
|
|
102
|
+
url: input.url,
|
|
103
|
+
body: input.body,
|
|
104
|
+
headers: headersForSigning,
|
|
105
|
+
credentials,
|
|
106
|
+
service: options.service ?? "bedrock",
|
|
107
|
+
name: options.name ?? "Bedrock Converse",
|
|
108
|
+
})
|
|
109
|
+
return Headers.setAll(headersForSigning, signed)
|
|
110
|
+
})
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
/** Bedrock route auth defaults to SigV4 and expects credentials from route configuration. */
|
|
114
|
+
export const auth = sigV4(undefined)
|
|
115
|
+
|
|
116
|
+
export const resolveRegion = (input: {
|
|
117
|
+
readonly region?: string
|
|
118
|
+
readonly credentials?: { readonly region: string }
|
|
119
|
+
}) =>
|
|
120
|
+
input.region ?? input.credentials?.region ?? process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION ?? "us-east-1"
|
|
121
|
+
|
|
122
|
+
export interface ResolveAuthInput {
|
|
123
|
+
readonly apiKey?: string
|
|
124
|
+
readonly credentials?: Credentials
|
|
125
|
+
readonly profile?: string
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface ResolveAuthOptions {
|
|
129
|
+
readonly service?: string
|
|
130
|
+
readonly name?: string
|
|
131
|
+
/** `sigv4` ignores an ambient `AWS_BEARER_TOKEN_BEDROCK`; `bearer` is validated by the caller. */
|
|
132
|
+
readonly mode?: "bearer" | "sigv4"
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Bearer wins over SigV4 and explicit static credentials win over the default
|
|
137
|
+
* chain, matching the AWS SDK's own precedence for `AWS_BEARER_TOKEN_BEDROCK`.
|
|
138
|
+
* The region is applied to the SigV4 scope so it always matches the endpoint host.
|
|
139
|
+
*/
|
|
140
|
+
export const resolveAuth = (input: ResolveAuthInput, region: string, options: ResolveAuthOptions = {}) => {
|
|
141
|
+
const apiKey = options.mode === "sigv4" ? undefined : (input.apiKey ?? process.env.AWS_BEARER_TOKEN_BEDROCK)
|
|
142
|
+
if (apiKey !== undefined) return Auth.bearer(apiKey)
|
|
143
|
+
if (input.credentials !== undefined) return sigV4({ ...input.credentials, region }, options)
|
|
144
|
+
return sigV4(defaultChain({ region, profile: input.profile }), options)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export * as BedrockAuth from "./bedrock-auth.js"
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Schema } from "effect"
|
|
2
|
+
import { CacheTTL, type CacheHint, type LLMRequest } from "../../schema/index.js"
|
|
3
|
+
import { newBreakpoints, take } from "./cache.js"
|
|
4
|
+
|
|
5
|
+
// Bedrock cache markers are positional: emit a `cachePoint` block immediately
|
|
6
|
+
// after the content the caller wants treated as a cacheable prefix. Bedrock
|
|
7
|
+
// accepts an optional `ttl` such as "5m" or "1h" on cachePoint.
|
|
8
|
+
export const CachePointBlock = Schema.Struct({
|
|
9
|
+
cachePoint: Schema.Struct({
|
|
10
|
+
type: Schema.tag("default"),
|
|
11
|
+
ttl: Schema.optional(CacheTTL),
|
|
12
|
+
}),
|
|
13
|
+
})
|
|
14
|
+
export type CachePointBlock = Schema.Schema.Type<typeof CachePointBlock>
|
|
15
|
+
|
|
16
|
+
const LEGACY_CLAUDE = ["anthropic.claude-instant", "anthropic.claude-v1", "anthropic.claude-v2", "anthropic.claude-3-"]
|
|
17
|
+
|
|
18
|
+
// These legacy Claude releases support explicit caching, but only for five minutes.
|
|
19
|
+
const CLAUDE_5M = [
|
|
20
|
+
"anthropic.claude-3-5-sonnet-20241022-v2:0",
|
|
21
|
+
"anthropic.claude-3-5-haiku-20241022-v1:0",
|
|
22
|
+
"anthropic.claude-3-7-sonnet-20250219-v1:0",
|
|
23
|
+
"anthropic.claude-sonnet-4-20250514-v1:0",
|
|
24
|
+
"anthropic.claude-opus-4-20250514-v1:0",
|
|
25
|
+
"anthropic.claude-opus-4-1-20250805-v1:0",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
// Callers share the four-breakpoint budget across system, messages, and tools.
|
|
29
|
+
export const BEDROCK_BREAKPOINT_CAP = 4
|
|
30
|
+
|
|
31
|
+
export const breakpoints = (model: LLMRequest["model"]) => {
|
|
32
|
+
// Substring matching also handles regional prefixes and model-bearing ARNs.
|
|
33
|
+
const short = CLAUDE_5M.some((id) => model.id.includes(id))
|
|
34
|
+
return {
|
|
35
|
+
...newBreakpoints(BEDROCK_BREAKPOINT_CAP),
|
|
36
|
+
// An explicit Anthropic cache format opts opaque IDs such as application inference profile ARNs in.
|
|
37
|
+
// Assume modern Claude releases retain caching support; older generations need an explicit exception.
|
|
38
|
+
// Other model families use implicit caching where available.
|
|
39
|
+
supported:
|
|
40
|
+
model.compatibility?.cacheFormat === "anthropic" ||
|
|
41
|
+
(model.id.includes("anthropic.claude-") && (short || !LEGACY_CLAUDE.some((id) => model.id.includes(id)))),
|
|
42
|
+
ttl1h: !short,
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export type Breakpoints = ReturnType<typeof breakpoints>
|
|
46
|
+
|
|
47
|
+
export const block = (breakpoints: Breakpoints, cache: CacheHint | undefined): CachePointBlock | undefined => {
|
|
48
|
+
if (!breakpoints.supported) return undefined
|
|
49
|
+
if (cache?.type !== "ephemeral" && cache?.type !== "persistent") return undefined
|
|
50
|
+
if (!take(breakpoints)) return undefined
|
|
51
|
+
// Older Claude releases cache for five minutes only, so a one-hour request falls back to the default.
|
|
52
|
+
const ttl = cache.ttl === "1h" && !breakpoints.ttl1h ? undefined : cache.ttl
|
|
53
|
+
return { cachePoint: { type: "default", ...(ttl === undefined ? {} : { ttl }) } }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export * as BedrockCache from "./bedrock-cache.js"
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect"
|
|
2
|
+
import { Base64 } from "effect/encoding"
|
|
3
|
+
import type { MediaPart } from "../../schema/index.js"
|
|
4
|
+
import { ProviderShared } from "../shared.js"
|
|
5
|
+
|
|
6
|
+
// Bedrock Converse accepts image `format` as the file extension and
|
|
7
|
+
// `source.bytes` as base64 in the JSON wire format.
|
|
8
|
+
export const ImageFormat = Schema.Literals(["png", "jpeg", "gif", "webp"])
|
|
9
|
+
export type ImageFormat = Schema.Schema.Type<typeof ImageFormat>
|
|
10
|
+
|
|
11
|
+
export const ImageBlock = Schema.Struct({
|
|
12
|
+
image: Schema.Struct({
|
|
13
|
+
format: ImageFormat,
|
|
14
|
+
source: Schema.Struct({ bytes: Schema.String }),
|
|
15
|
+
}),
|
|
16
|
+
})
|
|
17
|
+
export type ImageBlock = Schema.Schema.Type<typeof ImageBlock>
|
|
18
|
+
|
|
19
|
+
// Bedrock document blocks require a user-facing name so the model can refer to
|
|
20
|
+
// the uploaded document.
|
|
21
|
+
export const DocumentFormat = Schema.Literals(["pdf", "csv", "doc", "docx", "xls", "xlsx", "html", "txt", "md"])
|
|
22
|
+
export type DocumentFormat = Schema.Schema.Type<typeof DocumentFormat>
|
|
23
|
+
|
|
24
|
+
export const DocumentBlock = Schema.Struct({
|
|
25
|
+
document: Schema.Struct({
|
|
26
|
+
format: DocumentFormat,
|
|
27
|
+
name: Schema.String,
|
|
28
|
+
source: Schema.Struct({ bytes: Schema.String }),
|
|
29
|
+
}),
|
|
30
|
+
})
|
|
31
|
+
export type DocumentBlock = Schema.Schema.Type<typeof DocumentBlock>
|
|
32
|
+
|
|
33
|
+
const IMAGE_FORMATS = {
|
|
34
|
+
"image/png": "png",
|
|
35
|
+
"image/jpeg": "jpeg",
|
|
36
|
+
"image/jpg": "jpeg",
|
|
37
|
+
"image/gif": "gif",
|
|
38
|
+
"image/webp": "webp",
|
|
39
|
+
} as const satisfies Record<string, ImageFormat>
|
|
40
|
+
|
|
41
|
+
const DOCUMENT_FORMATS = {
|
|
42
|
+
"application/pdf": "pdf",
|
|
43
|
+
"text/csv": "csv",
|
|
44
|
+
"application/msword": "doc",
|
|
45
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
46
|
+
"application/vnd.ms-excel": "xls",
|
|
47
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
48
|
+
"text/html": "html",
|
|
49
|
+
"text/plain": "txt",
|
|
50
|
+
"text/markdown": "md",
|
|
51
|
+
} as const satisfies Record<string, DocumentFormat>
|
|
52
|
+
|
|
53
|
+
const documentBlock = (name: string, format: DocumentFormat, bytes: string): DocumentBlock => ({
|
|
54
|
+
document: {
|
|
55
|
+
format,
|
|
56
|
+
name,
|
|
57
|
+
source: { bytes },
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
function documentName(filename: string | undefined, names: Set<string>) {
|
|
62
|
+
const base =
|
|
63
|
+
(filename ?? "")
|
|
64
|
+
.replace(/\.[^.]*$/, "")
|
|
65
|
+
.replace(/[^a-zA-Z0-9 ()[\]-]/g, " ")
|
|
66
|
+
.replace(/\s+/g, " ")
|
|
67
|
+
.trim()
|
|
68
|
+
.slice(0, 200)
|
|
69
|
+
.trim() || "document"
|
|
70
|
+
let name = base
|
|
71
|
+
// Converse requires labels to be unique across the entire request, including tool results.
|
|
72
|
+
for (let index = 2; names.has(name); index++) {
|
|
73
|
+
const suffix = ` ${index}`
|
|
74
|
+
name = `${base.slice(0, 200 - suffix.length).trimEnd()}${suffix}`
|
|
75
|
+
}
|
|
76
|
+
names.add(name)
|
|
77
|
+
return name
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const mediaBase64 = Effect.fnUntraced(function* (part: MediaPart) {
|
|
81
|
+
const media = yield* ProviderShared.requireInlineMedia("Bedrock Converse", part.media)
|
|
82
|
+
const bytes = yield* Effect.fromResult(Base64.decode(media.base64)).pipe(
|
|
83
|
+
Effect.mapError((cause) =>
|
|
84
|
+
ProviderShared.invalidRequest("Bedrock Converse media data must be valid base64", cause),
|
|
85
|
+
),
|
|
86
|
+
)
|
|
87
|
+
return Base64.encode(bytes)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// Route by MIME. Known image/document formats lower into a typed block; anything
|
|
91
|
+
// else fails with a clear error instead of silently degrading to a malformed
|
|
92
|
+
// document block. Image MIME types not in `IMAGE_FORMATS` (e.g. `image/svg+xml`)
|
|
93
|
+
// get an image-specific error so the caller knows it's a format-support issue,
|
|
94
|
+
// not a kind-detection issue.
|
|
95
|
+
export const lower = Effect.fnUntraced(function* (part: MediaPart, documentNames: Set<string>) {
|
|
96
|
+
const mime = part.media.mediaType.toLowerCase()
|
|
97
|
+
const imageFormat = IMAGE_FORMATS[mime as keyof typeof IMAGE_FORMATS]
|
|
98
|
+
if (imageFormat) {
|
|
99
|
+
return [{ image: { format: imageFormat, source: { bytes: yield* mediaBase64(part) } } } satisfies ImageBlock]
|
|
100
|
+
}
|
|
101
|
+
if (mime.startsWith("image/"))
|
|
102
|
+
return yield* ProviderShared.invalidRequest(
|
|
103
|
+
`Bedrock Converse does not support image media type ${part.media.mediaType}`,
|
|
104
|
+
)
|
|
105
|
+
const documentFormat = DOCUMENT_FORMATS[mime as keyof typeof DOCUMENT_FORMATS]
|
|
106
|
+
if (documentFormat) {
|
|
107
|
+
const name = documentName(part.filename, documentNames)
|
|
108
|
+
const block = documentBlock(name, documentFormat, yield* mediaBase64(part))
|
|
109
|
+
return part.filename !== undefined && part.filename !== name
|
|
110
|
+
? [
|
|
111
|
+
{
|
|
112
|
+
text: `Attached file ${ProviderShared.encodeJson(part.filename)} has document label ${ProviderShared.encodeJson(name)}.`,
|
|
113
|
+
},
|
|
114
|
+
block,
|
|
115
|
+
]
|
|
116
|
+
: [block]
|
|
117
|
+
}
|
|
118
|
+
return yield* ProviderShared.invalidRequest(`Bedrock Converse does not support media type ${part.media.mediaType}`)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
export * as BedrockMedia from "./bedrock-media.js"
|