@radheim/ai 0.0.0-stage → 2.4.3
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/README.md +1890 -3
- package/dist/ai.d.ts +36 -0
- package/dist/ai.js +46 -0
- package/dist/cache-policy.d.ts +6 -0
- package/dist/cache-policy.js +205 -0
- package/dist/decision-client.d.ts +10 -0
- package/dist/decision-client.js +42 -0
- package/dist/decision.d.ts +378 -0
- package/dist/decision.js +171 -0
- package/dist/effect.d.ts +59 -0
- package/dist/effect.js +33 -0
- package/dist/effort-updates.d.ts +12 -0
- package/dist/effort-updates.js +23 -0
- package/dist/embedding-client.d.ts +10 -0
- package/dist/embedding-client.js +17 -0
- package/dist/embedding.d.ts +113 -0
- package/dist/embedding.js +76 -0
- package/dist/experimental/cloudflare-run-extraction.d.ts +17 -0
- package/dist/experimental/cloudflare-run-extraction.js +85 -0
- package/dist/experimental/extraction.d.ts +153 -0
- package/dist/experimental/extraction.js +103 -0
- package/dist/experimental/realtime.d.ts +80 -0
- package/dist/experimental/realtime.js +96 -0
- package/dist/experimental.d.ts +10 -0
- package/dist/experimental.js +6 -0
- package/dist/fetch-middleware.d.ts +7 -0
- package/dist/fetch-middleware.js +41 -0
- package/dist/generation.d.ts +77 -0
- package/dist/generation.js +109 -0
- package/dist/image-client.d.ts +14 -0
- package/dist/image-client.js +39 -0
- package/dist/image.d.ts +323 -0
- package/dist/image.js +103 -0
- package/dist/index.d.ts +300 -0
- package/dist/index.js +179 -0
- package/dist/llm.d.ts +109 -0
- package/dist/llm.js +177 -0
- package/dist/loop.d.ts +38 -0
- package/dist/loop.js +139 -0
- package/dist/media-client.d.ts +30 -0
- package/dist/media-client.js +44 -0
- package/dist/media-model.d.ts +49 -0
- package/dist/media-model.js +55 -0
- package/dist/media.d.ts +237 -0
- package/dist/media.js +285 -0
- package/dist/promise-media.d.ts +20 -0
- package/dist/promise-media.js +16 -0
- package/dist/protocols/alibaba-chat.d.ts +315 -0
- package/dist/protocols/alibaba-chat.js +78 -0
- package/dist/protocols/alibaba-images.d.ts +22 -0
- package/dist/protocols/alibaba-images.js +77 -0
- package/dist/protocols/alibaba-messages.d.ts +413 -0
- package/dist/protocols/alibaba-messages.js +44 -0
- package/dist/protocols/alibaba-responses.d.ts +298 -0
- package/dist/protocols/alibaba-responses.js +67 -0
- package/dist/protocols/anthropic-messages.d.ts +1213 -0
- package/dist/protocols/anthropic-messages.js +1412 -0
- package/dist/protocols/assemblyai-transcription.d.ts +41 -0
- package/dist/protocols/assemblyai-transcription.js +137 -0
- package/dist/protocols/bedrock-converse.d.ts +548 -0
- package/dist/protocols/bedrock-converse.js +801 -0
- package/dist/protocols/bedrock-event-stream.d.ts +14 -0
- package/dist/protocols/bedrock-event-stream.js +100 -0
- package/dist/protocols/cartesia-speech.d.ts +117 -0
- package/dist/protocols/cartesia-speech.js +120 -0
- package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
- package/dist/protocols/cloudflare-run-decisions.js +36 -0
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
- package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
- package/dist/protocols/cohere-chat.d.ts +184 -0
- package/dist/protocols/cohere-chat.js +317 -0
- package/dist/protocols/deepgram-speech.d.ts +109 -0
- package/dist/protocols/deepgram-speech.js +93 -0
- package/dist/protocols/deepgram-transcription.d.ts +26 -0
- package/dist/protocols/deepgram-transcription.js +119 -0
- package/dist/protocols/elevenlabs-speech.d.ts +112 -0
- package/dist/protocols/elevenlabs-speech.js +111 -0
- package/dist/protocols/elevenlabs-transcription.d.ts +28 -0
- package/dist/protocols/elevenlabs-transcription.js +137 -0
- package/dist/protocols/gemini.d.ts +388 -0
- package/dist/protocols/gemini.js +741 -0
- package/dist/protocols/google-decisions.d.ts +13 -0
- package/dist/protocols/google-decisions.js +133 -0
- package/dist/protocols/google-embed-content.d.ts +34 -0
- package/dist/protocols/google-embed-content.js +110 -0
- package/dist/protocols/google-images.d.ts +18 -0
- package/dist/protocols/google-images.js +169 -0
- package/dist/protocols/google-interactions-video.d.ts +38 -0
- package/dist/protocols/google-interactions-video.js +185 -0
- package/dist/protocols/google-interactions.d.ts +554 -0
- package/dist/protocols/google-interactions.js +756 -0
- package/dist/protocols/google-speech.d.ts +120 -0
- package/dist/protocols/google-speech.js +94 -0
- package/dist/protocols/google-transcription.d.ts +148 -0
- package/dist/protocols/google-transcription.js +129 -0
- package/dist/protocols/google-vertex-predict-embeddings.d.ts +18 -0
- package/dist/protocols/google-vertex-predict-embeddings.js +74 -0
- package/dist/protocols/index.d.ts +21 -0
- package/dist/protocols/index.js +21 -0
- package/dist/protocols/meta-images.d.ts +26 -0
- package/dist/protocols/meta-images.js +101 -0
- package/dist/protocols/meta-messages.d.ts +423 -0
- package/dist/protocols/meta-messages.js +63 -0
- package/dist/protocols/meta-responses.d.ts +533 -0
- package/dist/protocols/meta-responses.js +189 -0
- package/dist/protocols/minimax-images.d.ts +25 -0
- package/dist/protocols/minimax-images.js +150 -0
- package/dist/protocols/minimax-video.d.ts +45 -0
- package/dist/protocols/minimax-video.js +296 -0
- package/dist/protocols/mistral-chat.d.ts +565 -0
- package/dist/protocols/mistral-chat.js +606 -0
- package/dist/protocols/open-responses-channel.d.ts +25 -0
- package/dist/protocols/open-responses-channel.js +137 -0
- package/dist/protocols/open-responses-continuation.d.ts +18 -0
- package/dist/protocols/open-responses-continuation.js +172 -0
- package/dist/protocols/open-responses.d.ts +2509 -0
- package/dist/protocols/open-responses.js +1335 -0
- package/dist/protocols/openai-chat.d.ts +2571 -0
- package/dist/protocols/openai-chat.js +1140 -0
- package/dist/protocols/openai-compatible-chat.d.ts +184 -0
- package/dist/protocols/openai-compatible-chat.js +19 -0
- package/dist/protocols/openai-compatible-responses.d.ts +231 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +141 -0
- package/dist/protocols/openai-embeddings.d.ts +16 -0
- package/dist/protocols/openai-embeddings.js +76 -0
- package/dist/protocols/openai-freeform.d.ts +78 -0
- package/dist/protocols/openai-freeform.js +222 -0
- package/dist/protocols/openai-images.d.ts +148 -0
- package/dist/protocols/openai-images.js +303 -0
- package/dist/protocols/openai-responses.d.ts +1600 -0
- package/dist/protocols/openai-responses.js +360 -0
- package/dist/protocols/openai-speech.d.ts +110 -0
- package/dist/protocols/openai-speech.js +112 -0
- package/dist/protocols/openai-transcription.d.ts +185 -0
- package/dist/protocols/openai-transcription.js +206 -0
- package/dist/protocols/openrouter-video.d.ts +36 -0
- package/dist/protocols/openrouter-video.js +139 -0
- package/dist/protocols/seedance-video.d.ts +45 -0
- package/dist/protocols/seedance-video.js +217 -0
- package/dist/protocols/shared.d.ts +249 -0
- package/dist/protocols/shared.js +446 -0
- package/dist/protocols/system-one.d.ts +145 -0
- package/dist/protocols/system-one.js +272 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +60 -0
- package/dist/protocols/utils/bedrock-auth.js +86 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +19 -0
- package/dist/protocols/utils/bedrock-cache.js +49 -0
- package/dist/protocols/utils/bedrock-media.d.ts +53 -0
- package/dist/protocols/utils/bedrock-media.js +98 -0
- package/dist/protocols/utils/cache.d.ts +25 -0
- package/dist/protocols/utils/cache.js +33 -0
- package/dist/protocols/utils/claude-model.d.ts +8 -0
- package/dist/protocols/utils/claude-model.js +15 -0
- package/dist/protocols/utils/gateway-protocol.d.ts +17 -0
- package/dist/protocols/utils/gateway-protocol.js +59 -0
- package/dist/protocols/utils/gemini-generate-content.d.ts +75 -0
- package/dist/protocols/utils/gemini-generate-content.js +96 -0
- package/dist/protocols/utils/gemini-json-schema.d.ts +8 -0
- package/dist/protocols/utils/gemini-json-schema.js +76 -0
- package/dist/protocols/utils/image-sizing.d.ts +31 -0
- package/dist/protocols/utils/image-sizing.js +73 -0
- package/dist/protocols/utils/lifecycle.d.ts +24 -0
- package/dist/protocols/utils/lifecycle.js +62 -0
- package/dist/protocols/utils/media-input.d.ts +32 -0
- package/dist/protocols/utils/media-input.js +62 -0
- package/dist/protocols/utils/mistral-tool-id.d.ts +5 -0
- package/dist/protocols/utils/mistral-tool-id.js +31 -0
- package/dist/protocols/utils/open-responses-options.d.ts +54 -0
- package/dist/protocols/utils/open-responses-options.js +59 -0
- package/dist/protocols/utils/partial-json-options.d.ts +61 -0
- package/dist/protocols/utils/partial-json-options.js +51 -0
- package/dist/protocols/utils/partial-json.d.ts +8 -0
- package/dist/protocols/utils/partial-json.js +280 -0
- package/dist/protocols/utils/responses-checkpoint.d.ts +4 -0
- package/dist/protocols/utils/responses-checkpoint.js +103 -0
- package/dist/protocols/utils/responses-compaction.d.ts +6 -0
- package/dist/protocols/utils/responses-compaction.js +162 -0
- package/dist/protocols/utils/responses-hosted-tools.d.ts +122 -0
- package/dist/protocols/utils/responses-hosted-tools.js +63 -0
- package/dist/protocols/utils/speaker-turns.d.ts +3 -0
- package/dist/protocols/utils/speaker-turns.js +9 -0
- package/dist/protocols/utils/speech-stream.d.ts +49 -0
- package/dist/protocols/utils/speech-stream.js +64 -0
- package/dist/protocols/utils/tool-schema.d.ts +10 -0
- package/dist/protocols/utils/tool-schema.js +109 -0
- package/dist/protocols/utils/tool-stream.d.ts +478 -0
- package/dist/protocols/utils/tool-stream.js +153 -0
- package/dist/protocols/venice-chat.d.ts +340 -0
- package/dist/protocols/venice-chat.js +283 -0
- package/dist/protocols/xai-images.d.ts +25 -0
- package/dist/protocols/xai-images.js +87 -0
- package/dist/protocols/xai-responses.d.ts +294 -0
- package/dist/protocols/xai-responses.js +86 -0
- package/dist/protocols/xai-video.d.ts +35 -0
- package/dist/protocols/xai-video.js +159 -0
- package/dist/protocols/zai-chat.d.ts +296 -0
- package/dist/protocols/zai-chat.js +56 -0
- package/dist/protocols/zai-images.d.ts +20 -0
- package/dist/protocols/zai-images.js +79 -0
- package/dist/protocols/zai-messages.d.ts +409 -0
- package/dist/protocols/zai-messages.js +30 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +22 -0
- package/dist/provider-error.js +228 -0
- package/dist/provider-package.d.ts +17 -0
- package/dist/provider-package.js +1 -0
- package/dist/providers/alibaba/chat.d.ts +1 -0
- package/dist/providers/alibaba/chat.js +1 -0
- package/dist/providers/alibaba/embeddings.d.ts +2 -0
- package/dist/providers/alibaba/embeddings.js +1 -0
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba/messages.d.ts +3 -0
- package/dist/providers/alibaba/messages.js +1 -0
- package/dist/providers/alibaba/responses.d.ts +3 -0
- package/dist/providers/alibaba/responses.js +1 -0
- package/dist/providers/alibaba.d.ts +905 -0
- package/dist/providers/alibaba.js +119 -0
- package/dist/providers/amazon-bedrock/mantle/chat.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/messages.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/messages.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/responses.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/responses.js +1 -0
- package/dist/providers/amazon-bedrock/mantle.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle.js +1 -0
- package/dist/providers/amazon-bedrock-mantle.d.ts +789 -0
- package/dist/providers/amazon-bedrock-mantle.js +107 -0
- package/dist/providers/amazon-bedrock.d.ts +190 -0
- package/dist/providers/amazon-bedrock.js +44 -0
- package/dist/providers/anthropic-compatible.d.ts +364 -0
- package/dist/providers/anthropic-compatible.js +47 -0
- package/dist/providers/anthropic.d.ts +362 -0
- package/dist/providers/anthropic.js +36 -0
- package/dist/providers/assemblyai.d.ts +19 -0
- package/dist/providers/assemblyai.js +19 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/embeddings.d.ts +2 -0
- package/dist/providers/azure/embeddings.js +1 -0
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure/messages.d.ts +2 -0
- package/dist/providers/azure/messages.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +1074 -0
- package/dist/providers/azure.js +150 -0
- package/dist/providers/baseten/decisions.d.ts +2 -0
- package/dist/providers/baseten/decisions.js +1 -0
- package/dist/providers/baseten.d.ts +384 -0
- package/dist/providers/baseten.js +56 -0
- package/dist/providers/byteplus/videos.d.ts +2 -0
- package/dist/providers/byteplus/videos.js +1 -0
- package/dist/providers/byteplus.d.ts +24 -0
- package/dist/providers/byteplus.js +21 -0
- package/dist/providers/cartesia.d.ts +18 -0
- package/dist/providers/cartesia.js +17 -0
- package/dist/providers/cerebras.d.ts +375 -0
- package/dist/providers/cerebras.js +33 -0
- package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +1581 -0
- package/dist/providers/cloudflare-ai-gateway.js +120 -0
- package/dist/providers/cloudflare-shared.d.ts +9 -0
- package/dist/providers/cloudflare-shared.js +25 -0
- package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
- package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/images.js +1 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +391 -0
- package/dist/providers/cloudflare-workers-ai.js +69 -0
- package/dist/providers/cohere/chat.d.ts +4 -0
- package/dist/providers/cohere/chat.js +3 -0
- package/dist/providers/cohere/embeddings.d.ts +2 -0
- package/dist/providers/cohere/embeddings.js +1 -0
- package/dist/providers/cohere.d.ts +502 -0
- package/dist/providers/cohere.js +55 -0
- package/dist/providers/deepgram.d.ts +22 -0
- package/dist/providers/deepgram.js +20 -0
- package/dist/providers/deepinfra/embeddings.d.ts +2 -0
- package/dist/providers/deepinfra/embeddings.js +1 -0
- package/dist/providers/deepinfra.d.ts +378 -0
- package/dist/providers/deepinfra.js +52 -0
- package/dist/providers/deepseek.d.ts +376 -0
- package/dist/providers/deepseek.js +30 -0
- package/dist/providers/digitalocean/decisions.d.ts +2 -0
- package/dist/providers/digitalocean/decisions.js +1 -0
- package/dist/providers/digitalocean/embeddings.d.ts +2 -0
- package/dist/providers/digitalocean/embeddings.js +1 -0
- package/dist/providers/digitalocean/images.d.ts +2 -0
- package/dist/providers/digitalocean/images.js +1 -0
- package/dist/providers/digitalocean/responses.d.ts +4 -0
- package/dist/providers/digitalocean/responses.js +1 -0
- package/dist/providers/digitalocean.d.ts +874 -0
- package/dist/providers/digitalocean.js +106 -0
- package/dist/providers/elevenlabs.d.ts +22 -0
- package/dist/providers/elevenlabs.js +22 -0
- package/dist/providers/fireworks/embeddings.d.ts +2 -0
- package/dist/providers/fireworks/embeddings.js +1 -0
- package/dist/providers/fireworks.d.ts +379 -0
- package/dist/providers/fireworks.js +40 -0
- package/dist/providers/google/decisions.d.ts +2 -0
- package/dist/providers/google/decisions.js +1 -0
- package/dist/providers/google/embeddings.d.ts +2 -0
- package/dist/providers/google/embeddings.js +1 -0
- package/dist/providers/google/images.d.ts +2 -0
- package/dist/providers/google/images.js +1 -0
- package/dist/providers/google/interactions.d.ts +7 -0
- package/dist/providers/google/interactions.js +3 -0
- package/dist/providers/google/videos.d.ts +2 -0
- package/dist/providers/google/videos.js +1 -0
- package/dist/providers/google-vertex/chat.d.ts +2 -0
- package/dist/providers/google-vertex/chat.js +1 -0
- package/dist/providers/google-vertex/embeddings.d.ts +2 -0
- package/dist/providers/google-vertex/embeddings.js +1 -0
- package/dist/providers/google-vertex/gemini.d.ts +2 -0
- package/dist/providers/google-vertex/gemini.js +1 -0
- package/dist/providers/google-vertex/images.d.ts +2 -0
- package/dist/providers/google-vertex/images.js +1 -0
- package/dist/providers/google-vertex/interactions.d.ts +2 -0
- package/dist/providers/google-vertex/interactions.js +1 -0
- package/dist/providers/google-vertex/messages.d.ts +2 -0
- package/dist/providers/google-vertex/messages.js +1 -0
- package/dist/providers/google-vertex/mistral.d.ts +2 -0
- package/dist/providers/google-vertex/mistral.js +1 -0
- package/dist/providers/google-vertex/responses.d.ts +2 -0
- package/dist/providers/google-vertex/responses.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +204 -0
- package/dist/providers/google-vertex-chat.js +29 -0
- package/dist/providers/google-vertex-interactions.d.ts +111 -0
- package/dist/providers/google-vertex-interactions.js +62 -0
- package/dist/providers/google-vertex-messages.d.ts +362 -0
- package/dist/providers/google-vertex-messages.js +51 -0
- package/dist/providers/google-vertex-mistral.d.ts +120 -0
- package/dist/providers/google-vertex-mistral.js +43 -0
- package/dist/providers/google-vertex-responses.d.ts +253 -0
- package/dist/providers/google-vertex-responses.js +30 -0
- package/dist/providers/google-vertex-shared.d.ts +58 -0
- package/dist/providers/google-vertex-shared.js +128 -0
- package/dist/providers/google-vertex.d.ts +132 -0
- package/dist/providers/google-vertex.js +161 -0
- package/dist/providers/google.d.ts +246 -0
- package/dist/providers/google.js +80 -0
- package/dist/providers/groq.d.ts +470 -0
- package/dist/providers/groq.js +63 -0
- package/dist/providers/index.d.ts +45 -0
- package/dist/providers/index.js +45 -0
- package/dist/providers/meta/chat.d.ts +2 -0
- package/dist/providers/meta/chat.js +1 -0
- package/dist/providers/meta/images.d.ts +2 -0
- package/dist/providers/meta/images.js +1 -0
- package/dist/providers/meta/messages.d.ts +2 -0
- package/dist/providers/meta/messages.js +1 -0
- package/dist/providers/meta/responses.d.ts +2 -0
- package/dist/providers/meta/responses.js +1 -0
- package/dist/providers/meta.d.ts +804 -0
- package/dist/providers/meta.js +96 -0
- package/dist/providers/minimax/chat.d.ts +4 -0
- package/dist/providers/minimax/chat.js +3 -0
- package/dist/providers/minimax/images.d.ts +3 -0
- package/dist/providers/minimax/images.js +1 -0
- package/dist/providers/minimax/messages.d.ts +1 -0
- package/dist/providers/minimax/messages.js +1 -0
- package/dist/providers/minimax/responses.d.ts +4 -0
- package/dist/providers/minimax/responses.js +3 -0
- package/dist/providers/minimax/videos.d.ts +3 -0
- package/dist/providers/minimax/videos.js +1 -0
- package/dist/providers/minimax.d.ts +798 -0
- package/dist/providers/minimax.js +99 -0
- package/dist/providers/mistral.d.ts +208 -0
- package/dist/providers/mistral.js +17 -0
- package/dist/providers/moonshot/chat.d.ts +1 -0
- package/dist/providers/moonshot/chat.js +1 -0
- package/dist/providers/moonshot/messages.d.ts +4 -0
- package/dist/providers/moonshot/messages.js +3 -0
- package/dist/providers/moonshot/responses.d.ts +4 -0
- package/dist/providers/moonshot/responses.js +3 -0
- package/dist/providers/moonshot.d.ts +785 -0
- package/dist/providers/moonshot.js +78 -0
- package/dist/providers/open-responses-options.d.ts +5 -0
- package/dist/providers/open-responses-options.js +1 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/embeddings.d.ts +2 -0
- package/dist/providers/openai/embeddings.js +1 -0
- package/dist/providers/openai/images.d.ts +2 -0
- package/dist/providers/openai/images.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/decisions.d.ts +2 -0
- package/dist/providers/openai-compatible/decisions.js +1 -0
- package/dist/providers/openai-compatible/embeddings.d.ts +2 -0
- package/dist/providers/openai-compatible/embeddings.js +1 -0
- package/dist/providers/openai-compatible/images.d.ts +2 -0
- package/dist/providers/openai-compatible/images.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +250 -0
- package/dist/providers/openai-compatible-responses.js +26 -0
- package/dist/providers/openai-compatible.d.ts +208 -0
- package/dist/providers/openai-compatible.js +71 -0
- package/dist/providers/openai-options.d.ts +21 -0
- package/dist/providers/openai-options.js +37 -0
- package/dist/providers/openai.d.ts +522 -0
- package/dist/providers/openai.js +118 -0
- package/dist/providers/opencode/decisions.d.ts +2 -0
- package/dist/providers/opencode/decisions.js +1 -0
- package/dist/providers/opencode.d.ts +23 -0
- package/dist/providers/opencode.js +25 -0
- package/dist/providers/openrouter/decisions.d.ts +2 -0
- package/dist/providers/openrouter/decisions.js +1 -0
- package/dist/providers/openrouter/embeddings.d.ts +2 -0
- package/dist/providers/openrouter/embeddings.js +1 -0
- package/dist/providers/openrouter/images.d.ts +2 -0
- package/dist/providers/openrouter/images.js +1 -0
- package/dist/providers/openrouter/responses.d.ts +3 -0
- package/dist/providers/openrouter/responses.js +1 -0
- package/dist/providers/openrouter/videos.d.ts +2 -0
- package/dist/providers/openrouter/videos.js +1 -0
- package/dist/providers/openrouter.d.ts +1204 -0
- package/dist/providers/openrouter.js +210 -0
- package/dist/providers/settings.d.ts +40 -0
- package/dist/providers/settings.js +18 -0
- package/dist/providers/togetherai.d.ts +375 -0
- package/dist/providers/togetherai.js +30 -0
- package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
- package/dist/providers/typesafe-ai/decisions.js +1 -0
- package/dist/providers/typesafe-ai.d.ts +23 -0
- package/dist/providers/typesafe-ai.js +25 -0
- package/dist/providers/venice/responses.d.ts +4 -0
- package/dist/providers/venice/responses.js +1 -0
- package/dist/providers/venice.d.ts +632 -0
- package/dist/providers/venice.js +57 -0
- package/dist/providers/vercel-ai-gateway/chat.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/chat.js +1 -0
- package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
- package/dist/providers/vercel-ai-gateway/embeddings.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/embeddings.js +1 -0
- package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/images.js +1 -0
- package/dist/providers/vercel-ai-gateway/messages.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/messages.js +1 -0
- package/dist/providers/vercel-ai-gateway/responses.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/responses.js +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +111 -0
- package/dist/providers/vercel-ai-gateway.js +181 -0
- package/dist/providers/volcengine/videos.d.ts +2 -0
- package/dist/providers/volcengine/videos.js +1 -0
- package/dist/providers/volcengine.d.ts +24 -0
- package/dist/providers/volcengine.js +21 -0
- package/dist/providers/xai/images.d.ts +2 -0
- package/dist/providers/xai/images.js +1 -0
- package/dist/providers/xai/videos.d.ts +2 -0
- package/dist/providers/xai/videos.js +1 -0
- package/dist/providers/xai.d.ts +235 -0
- package/dist/providers/xai.js +89 -0
- package/dist/providers/zai/chat.d.ts +1 -0
- package/dist/providers/zai/chat.js +1 -0
- package/dist/providers/zai/images.d.ts +2 -0
- package/dist/providers/zai/images.js +1 -0
- package/dist/providers/zai-coding-plan/chat.d.ts +1 -0
- package/dist/providers/zai-coding-plan/chat.js +1 -0
- package/dist/providers/zai-coding-plan/messages.d.ts +4 -0
- package/dist/providers/zai-coding-plan/messages.js +3 -0
- package/dist/providers/zai-coding-plan/responses.d.ts +4 -0
- package/dist/providers/zai-coding-plan/responses.js +3 -0
- package/dist/providers/zai-coding-plan.d.ts +846 -0
- package/dist/providers/zai-coding-plan.js +56 -0
- package/dist/providers/zai.d.ts +285 -0
- package/dist/providers/zai.js +44 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/public.d.ts +18 -0
- package/dist/public.js +7 -0
- package/dist/route/auth-options.d.ts +35 -0
- package/dist/route/auth-options.js +17 -0
- package/dist/route/auth.d.ts +52 -0
- package/dist/route/auth.js +92 -0
- package/dist/route/client.d.ts +229 -0
- package/dist/route/client.js +293 -0
- package/dist/route/decision.d.ts +55 -0
- package/dist/route/decision.js +73 -0
- package/dist/route/embedding.d.ts +58 -0
- package/dist/route/embedding.js +85 -0
- package/dist/route/endpoint.d.ts +29 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor-service.d.ts +54 -0
- package/dist/route/executor-service.js +115 -0
- package/dist/route/executor.d.ts +37 -0
- package/dist/route/executor.js +225 -0
- package/dist/route/framing.d.ts +44 -0
- package/dist/route/framing.js +63 -0
- package/dist/route/index.d.ts +21 -0
- package/dist/route/index.js +18 -0
- package/dist/route/media-protocol.d.ts +187 -0
- package/dist/route/media-protocol.js +117 -0
- package/dist/route/media.d.ts +91 -0
- package/dist/route/media.js +251 -0
- package/dist/route/protocol.d.ts +85 -0
- package/dist/route/protocol.js +19 -0
- package/dist/route/transport/http.d.ts +34 -0
- package/dist/route/transport/http.js +67 -0
- package/dist/route/transport/index.d.ts +42 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket-channel.d.ts +56 -0
- package/dist/route/transport/websocket-channel.js +1 -0
- package/dist/route/transport/websocket.d.ts +32 -0
- package/dist/route/transport/websocket.js +315 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +194 -0
- package/dist/schema/errors.js +217 -0
- package/dist/schema/events.d.ts +11001 -0
- package/dist/schema/events.js +542 -0
- package/dist/schema/ids.d.ts +17 -0
- package/dist/schema/ids.js +10 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +2706 -0
- package/dist/schema/messages.js +344 -0
- package/dist/schema/options.d.ts +270 -0
- package/dist/schema/options.js +198 -0
- package/dist/speech.d.ts +266 -0
- package/dist/speech.js +114 -0
- package/dist/test-ai.d.ts +15 -0
- package/dist/test-ai.js +49 -0
- package/dist/testing.d.ts +1585 -0
- package/dist/testing.js +108 -0
- package/dist/tool-history.d.ts +2 -0
- package/dist/tool-history.js +91 -0
- package/dist/tool-runtime.d.ts +22 -0
- package/dist/tool-runtime.js +55 -0
- package/dist/tool-updates.d.ts +34 -0
- package/dist/tool-updates.js +93 -0
- package/dist/tool.d.ts +150 -0
- package/dist/tool.js +159 -0
- package/dist/transcription.d.ts +254 -0
- package/dist/transcription.js +119 -0
- package/dist/utils/bytes.d.ts +1 -0
- package/dist/utils/bytes.js +10 -0
- package/dist/utils/json.d.ts +4 -0
- package/dist/utils/json.js +4 -0
- package/dist/utils/media-type.d.ts +8 -0
- package/dist/utils/media-type.js +93 -0
- package/dist/utils/provider-codes.d.ts +3 -0
- package/dist/utils/provider-codes.js +41 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/dist/utils/sanitize.d.ts +5 -0
- package/dist/utils/sanitize.js +31 -0
- package/dist/video-client.d.ts +12 -0
- package/dist/video-client.js +24 -0
- package/dist/video.d.ts +178 -0
- package/dist/video.js +79 -0
- package/package.json +648 -4
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Base64 } from "effect/encoding";
|
|
3
|
+
import { Route } from "../route/client.js";
|
|
4
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
5
|
+
import { Protocol } from "../route/protocol.js";
|
|
6
|
+
import { AIError, StreamEvent, Usage, } from "../schema/index.js";
|
|
7
|
+
import { BedrockEventStream } from "./bedrock-event-stream.js";
|
|
8
|
+
import { classifyProviderFailure } from "../provider-error.js";
|
|
9
|
+
import { JsonObject, optionalArray, ProviderShared } from "./shared.js";
|
|
10
|
+
import { BedrockAuth } from "./utils/bedrock-auth.js";
|
|
11
|
+
import { BedrockCache } from "./utils/bedrock-cache.js";
|
|
12
|
+
import { warnDropped } from "./utils/cache.js";
|
|
13
|
+
import { BedrockMedia } from "./utils/bedrock-media.js";
|
|
14
|
+
import { claudeVersion, supportsThinkingBlockBinding, THINKING_BINDING_BETA } from "./utils/claude-model.js";
|
|
15
|
+
import { Lifecycle } from "./utils/lifecycle.js";
|
|
16
|
+
import { MistralToolID } from "./utils/mistral-tool-id.js";
|
|
17
|
+
import { ToolStream } from "./utils/tool-stream.js";
|
|
18
|
+
import { concatBytes } from "../utils/bytes.js";
|
|
19
|
+
const ADAPTER = "bedrock-converse";
|
|
20
|
+
// =============================================================================
|
|
21
|
+
// Request Body Schema
|
|
22
|
+
// =============================================================================
|
|
23
|
+
const BedrockTextBlock = Schema.Struct({
|
|
24
|
+
text: Schema.String,
|
|
25
|
+
});
|
|
26
|
+
const BedrockToolUseBlock = Schema.Struct({
|
|
27
|
+
toolUse: Schema.Struct({
|
|
28
|
+
toolUseId: Schema.String,
|
|
29
|
+
name: Schema.String,
|
|
30
|
+
input: Schema.Unknown,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
const BedrockToolResultContentItem = Schema.Union([
|
|
34
|
+
Schema.Struct({ text: Schema.String }),
|
|
35
|
+
Schema.Struct({ json: Schema.Unknown }),
|
|
36
|
+
BedrockMedia.ImageBlock,
|
|
37
|
+
BedrockMedia.DocumentBlock,
|
|
38
|
+
]);
|
|
39
|
+
const BedrockToolResultBlock = Schema.Struct({
|
|
40
|
+
toolResult: Schema.Struct({
|
|
41
|
+
toolUseId: Schema.String,
|
|
42
|
+
content: Schema.Array(BedrockToolResultContentItem),
|
|
43
|
+
status: Schema.optional(Schema.Literals(["success", "error"])),
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
const BedrockReasoningBlock = Schema.Struct({
|
|
47
|
+
reasoningContent: Schema.Union([
|
|
48
|
+
Schema.Struct({
|
|
49
|
+
reasoningText: Schema.Struct({
|
|
50
|
+
text: Schema.String,
|
|
51
|
+
signature: Schema.optional(Schema.String),
|
|
52
|
+
}),
|
|
53
|
+
}),
|
|
54
|
+
Schema.Struct({ redactedContent: Schema.String }),
|
|
55
|
+
]),
|
|
56
|
+
});
|
|
57
|
+
const BedrockUserBlock = Schema.Union([
|
|
58
|
+
BedrockTextBlock,
|
|
59
|
+
BedrockMedia.ImageBlock,
|
|
60
|
+
BedrockMedia.DocumentBlock,
|
|
61
|
+
BedrockToolResultBlock,
|
|
62
|
+
BedrockCache.CachePointBlock,
|
|
63
|
+
]);
|
|
64
|
+
const BedrockAssistantBlock = Schema.Union([
|
|
65
|
+
BedrockTextBlock,
|
|
66
|
+
BedrockReasoningBlock,
|
|
67
|
+
BedrockToolUseBlock,
|
|
68
|
+
BedrockCache.CachePointBlock,
|
|
69
|
+
]);
|
|
70
|
+
const BedrockMessage = Schema.Union([
|
|
71
|
+
Schema.Struct({ role: Schema.Literal("user"), content: Schema.Array(BedrockUserBlock) }),
|
|
72
|
+
Schema.Struct({ role: Schema.Literal("assistant"), content: Schema.Array(BedrockAssistantBlock) }),
|
|
73
|
+
]).pipe(Schema.toTaggedUnion("role"));
|
|
74
|
+
const BedrockSystemBlock = Schema.Union([BedrockTextBlock, BedrockCache.CachePointBlock]);
|
|
75
|
+
const BedrockToolSpec = Schema.Struct({
|
|
76
|
+
toolSpec: Schema.Struct({
|
|
77
|
+
name: Schema.String,
|
|
78
|
+
description: Schema.optional(Schema.String),
|
|
79
|
+
inputSchema: Schema.Struct({
|
|
80
|
+
json: JsonObject,
|
|
81
|
+
}),
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
const BedrockTool = Schema.Union([BedrockToolSpec, BedrockCache.CachePointBlock]);
|
|
85
|
+
const BedrockToolChoice = Schema.Union([
|
|
86
|
+
Schema.Struct({ auto: Schema.Struct({}) }),
|
|
87
|
+
Schema.Struct({ any: Schema.Struct({}) }),
|
|
88
|
+
Schema.Struct({ tool: Schema.Struct({ name: Schema.String }) }),
|
|
89
|
+
]);
|
|
90
|
+
// Value sets from the Converse API reference. Unknown values pass through for forward compatibility.
|
|
91
|
+
const ServiceTierType = ProviderShared.knownString();
|
|
92
|
+
const GuardrailTrace = ProviderShared.knownString();
|
|
93
|
+
const GuardrailStreamProcessingMode = ProviderShared.knownString();
|
|
94
|
+
const TextFormatType = ProviderShared.knownString();
|
|
95
|
+
const BedrockBodyFields = {
|
|
96
|
+
modelId: Schema.String,
|
|
97
|
+
messages: Schema.Array(BedrockMessage),
|
|
98
|
+
system: optionalArray(BedrockSystemBlock),
|
|
99
|
+
inferenceConfig: Schema.optional(Schema.Struct({
|
|
100
|
+
maxTokens: Schema.optional(Schema.Number),
|
|
101
|
+
temperature: Schema.optional(Schema.Number),
|
|
102
|
+
topP: Schema.optional(Schema.Number),
|
|
103
|
+
stopSequences: optionalArray(Schema.String),
|
|
104
|
+
})),
|
|
105
|
+
toolConfig: Schema.optional(Schema.Struct({
|
|
106
|
+
tools: Schema.Array(BedrockTool),
|
|
107
|
+
toolChoice: Schema.optional(BedrockToolChoice),
|
|
108
|
+
})),
|
|
109
|
+
additionalModelRequestFields: Schema.optional(JsonObject),
|
|
110
|
+
serviceTier: Schema.optional(Schema.Struct({ type: ServiceTierType })),
|
|
111
|
+
guardrailConfig: Schema.optional(Schema.Struct({
|
|
112
|
+
guardrailIdentifier: Schema.String,
|
|
113
|
+
guardrailVersion: Schema.String,
|
|
114
|
+
trace: Schema.optional(GuardrailTrace),
|
|
115
|
+
streamProcessingMode: Schema.optional(GuardrailStreamProcessingMode),
|
|
116
|
+
})),
|
|
117
|
+
outputConfig: Schema.optional(Schema.Struct({
|
|
118
|
+
textFormat: Schema.Struct({
|
|
119
|
+
type: TextFormatType,
|
|
120
|
+
structure: Schema.Struct({
|
|
121
|
+
jsonSchema: Schema.Struct({
|
|
122
|
+
schema: Schema.String,
|
|
123
|
+
name: Schema.optional(Schema.String),
|
|
124
|
+
description: Schema.optional(Schema.String),
|
|
125
|
+
}),
|
|
126
|
+
}),
|
|
127
|
+
}),
|
|
128
|
+
})),
|
|
129
|
+
requestMetadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
130
|
+
};
|
|
131
|
+
const BedrockConverseBody = Schema.Struct(BedrockBodyFields);
|
|
132
|
+
const BedrockUsageSchema = Schema.Struct({
|
|
133
|
+
inputTokens: Schema.optional(Schema.Number),
|
|
134
|
+
outputTokens: Schema.optional(Schema.Number),
|
|
135
|
+
totalTokens: Schema.optional(Schema.Number),
|
|
136
|
+
cacheReadInputTokens: Schema.optional(Schema.Number),
|
|
137
|
+
cacheWriteInputTokens: Schema.optional(Schema.Number),
|
|
138
|
+
});
|
|
139
|
+
const BedrockStreamException = Schema.Struct({
|
|
140
|
+
message: Schema.optional(Schema.String),
|
|
141
|
+
originalMessage: Schema.optional(Schema.String),
|
|
142
|
+
originalStatusCode: Schema.optional(Schema.Number),
|
|
143
|
+
});
|
|
144
|
+
// Streaming event shape — the AWS event stream wraps each JSON payload by its
|
|
145
|
+
// `:event-type` header (e.g. `messageStart`, `contentBlockDelta`). We
|
|
146
|
+
// reconstruct that wrapping in `decodeFrames` below so the event schema can
|
|
147
|
+
// stay a plain discriminated record.
|
|
148
|
+
const BedrockEvent = Schema.Struct({
|
|
149
|
+
messageStart: Schema.optional(Schema.Struct({ role: Schema.String })),
|
|
150
|
+
contentBlockStart: Schema.optional(Schema.Struct({
|
|
151
|
+
contentBlockIndex: Schema.Number,
|
|
152
|
+
start: Schema.optional(Schema.Struct({
|
|
153
|
+
toolUse: Schema.optional(Schema.Struct({ toolUseId: Schema.String, name: Schema.String })),
|
|
154
|
+
})),
|
|
155
|
+
})),
|
|
156
|
+
contentBlockDelta: Schema.optional(Schema.Struct({
|
|
157
|
+
contentBlockIndex: Schema.Number,
|
|
158
|
+
delta: Schema.optional(Schema.Struct({
|
|
159
|
+
text: Schema.optional(Schema.String),
|
|
160
|
+
toolUse: Schema.optional(Schema.Struct({ input: Schema.String })),
|
|
161
|
+
reasoningContent: Schema.optional(Schema.Struct({
|
|
162
|
+
text: Schema.optional(Schema.String),
|
|
163
|
+
signature: Schema.optional(Schema.String),
|
|
164
|
+
// Blob fields in Bedrock's JSON event stream are base64 strings.
|
|
165
|
+
redactedContent: Schema.optional(Schema.String),
|
|
166
|
+
// Vercel's Bedrock provider exposes the same delta under
|
|
167
|
+
// Anthropic's shorter `data` spelling.
|
|
168
|
+
data: Schema.optional(Schema.String),
|
|
169
|
+
})),
|
|
170
|
+
})),
|
|
171
|
+
})),
|
|
172
|
+
contentBlockStop: Schema.optional(Schema.Struct({ contentBlockIndex: Schema.Number })),
|
|
173
|
+
messageStop: Schema.optional(Schema.Struct({
|
|
174
|
+
stopReason: Schema.String,
|
|
175
|
+
additionalModelResponseFields: Schema.optional(Schema.Unknown),
|
|
176
|
+
})),
|
|
177
|
+
metadata: Schema.optional(Schema.Struct({
|
|
178
|
+
usage: Schema.optional(BedrockUsageSchema),
|
|
179
|
+
metrics: Schema.optional(Schema.Unknown),
|
|
180
|
+
})),
|
|
181
|
+
exception: Schema.optional(Schema.Struct({ type: Schema.String, details: BedrockStreamException })),
|
|
182
|
+
});
|
|
183
|
+
// =============================================================================
|
|
184
|
+
// Request Lowering
|
|
185
|
+
// =============================================================================
|
|
186
|
+
const lowerToolSpec = (tool) => ({
|
|
187
|
+
toolSpec: {
|
|
188
|
+
name: tool.name,
|
|
189
|
+
...(tool.description.trim().length > 0 ? { description: tool.description } : {}),
|
|
190
|
+
inputSchema: { json: tool.inputSchema },
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
const lowerTools = (breakpoints, tools) => {
|
|
194
|
+
const result = [];
|
|
195
|
+
for (const tool of tools) {
|
|
196
|
+
result.push(lowerToolSpec(tool));
|
|
197
|
+
const cachePoint = BedrockCache.block(breakpoints, tool.cache);
|
|
198
|
+
if (cachePoint)
|
|
199
|
+
result.push(cachePoint);
|
|
200
|
+
}
|
|
201
|
+
return result;
|
|
202
|
+
};
|
|
203
|
+
const textWithCache = (breakpoints, text, cache) => {
|
|
204
|
+
const cachePoint = BedrockCache.block(breakpoints, cache);
|
|
205
|
+
return cachePoint ? [{ text }, cachePoint] : [{ text }];
|
|
206
|
+
};
|
|
207
|
+
const lowerToolChoice = (toolChoice) => ProviderShared.matchToolChoice(toolChoice, {
|
|
208
|
+
auto: () => ({ auto: {} }),
|
|
209
|
+
none: () => undefined,
|
|
210
|
+
required: () => ({ any: {} }),
|
|
211
|
+
tool: (name) => ({ tool: { name } }),
|
|
212
|
+
});
|
|
213
|
+
const providerMetadata = (key, metadata) => ({ [key]: metadata });
|
|
214
|
+
const reasoningSignature = (part, providerMetadataKey) => {
|
|
215
|
+
const metadata = part.providerMetadata?.[providerMetadataKey];
|
|
216
|
+
if (part.encrypted !== undefined)
|
|
217
|
+
return part.encrypted;
|
|
218
|
+
if (typeof metadata?.signature === "string")
|
|
219
|
+
return metadata.signature;
|
|
220
|
+
};
|
|
221
|
+
const reasoningRedactedData = (part, providerMetadataKey) => {
|
|
222
|
+
const metadata = part.providerMetadata?.[providerMetadataKey];
|
|
223
|
+
if (typeof metadata?.redactedData === "string")
|
|
224
|
+
return metadata.redactedData;
|
|
225
|
+
};
|
|
226
|
+
const removeEmptyToolInputKeys = (input) => {
|
|
227
|
+
if (Array.isArray(input))
|
|
228
|
+
return input.map(removeEmptyToolInputKeys);
|
|
229
|
+
if (!ProviderShared.isRecord(input))
|
|
230
|
+
return input;
|
|
231
|
+
return Object.fromEntries(Object.entries(input).flatMap(([key, value]) => (key === "" ? [] : [[key, removeEmptyToolInputKeys(value)]])));
|
|
232
|
+
};
|
|
233
|
+
const lowerToolCall = (part, normalizeID) => ({
|
|
234
|
+
toolUse: {
|
|
235
|
+
toolUseId: normalizeID(part.id),
|
|
236
|
+
// Converse rejects replayed names over 64 characters; flattenRequest already fixed invalid characters.
|
|
237
|
+
name: part.name.slice(0, 64),
|
|
238
|
+
input: removeEmptyToolInputKeys(part.input),
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
const lowerToolResultContent = Effect.fnUntraced(function* (part, documentNames) {
|
|
242
|
+
if (part.result.type === "text" || part.result.type === "error")
|
|
243
|
+
return [{ text: ProviderShared.toolResultText(part) }];
|
|
244
|
+
if (part.result.type === "json")
|
|
245
|
+
return [{ json: part.result.value }];
|
|
246
|
+
const content = [];
|
|
247
|
+
for (const item of part.result.value) {
|
|
248
|
+
if (item.type === "text") {
|
|
249
|
+
content.push({ text: item.text });
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const media = yield* BedrockMedia.lower(ProviderShared.toolFileMedia(item), documentNames);
|
|
253
|
+
content.push(...media);
|
|
254
|
+
}
|
|
255
|
+
return content;
|
|
256
|
+
});
|
|
257
|
+
const lowerToolResult = Effect.fnUntraced(function* (part, documentNames, normalizeID) {
|
|
258
|
+
return {
|
|
259
|
+
toolResult: {
|
|
260
|
+
toolUseId: normalizeID(part.id),
|
|
261
|
+
content: yield* lowerToolResultContent(part, documentNames),
|
|
262
|
+
status: part.result.type === "error" ? "error" : "success",
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
});
|
|
266
|
+
// Keep Claude and Nova tool-result images inline; put other models' images beside the result.
|
|
267
|
+
const keepToolImagesInline = (id) => id.includes("anthropic.claude-") || id.includes("amazon.nova-");
|
|
268
|
+
const lowerMessages = Effect.fnUntraced(function* (request, breakpoints) {
|
|
269
|
+
const messages = [];
|
|
270
|
+
const documentNames = new Set();
|
|
271
|
+
// Mistral can reject replay IDs even when they satisfy Converse's broader ID syntax.
|
|
272
|
+
const normalizeID = request.model.id.includes("mistral.") ? MistralToolID.normalizer(request) : (id) => id;
|
|
273
|
+
const providerMetadataKey = request.model.route.providerMetadataKey ?? request.model.provider;
|
|
274
|
+
const hoistImages = !keepToolImagesInline(request.model.id);
|
|
275
|
+
// Bedrock expects parallel tool results before any images hoisted beside them.
|
|
276
|
+
const pendingImages = [];
|
|
277
|
+
const flushImages = () => {
|
|
278
|
+
if (pendingImages.length === 0)
|
|
279
|
+
return;
|
|
280
|
+
const previous = messages.at(-1);
|
|
281
|
+
if (previous?.role === "user")
|
|
282
|
+
messages[messages.length - 1] = { role: "user", content: [...previous.content, ...pendingImages] };
|
|
283
|
+
pendingImages.length = 0;
|
|
284
|
+
};
|
|
285
|
+
for (const message of request.messages) {
|
|
286
|
+
if (message.role !== "tool")
|
|
287
|
+
flushImages();
|
|
288
|
+
if (message.role === "system") {
|
|
289
|
+
const part = yield* ProviderShared.wrappedSystemUpdate("Bedrock Converse", message);
|
|
290
|
+
const content = textWithCache(breakpoints, part.text, part.cache);
|
|
291
|
+
const previous = messages.at(-1);
|
|
292
|
+
if (previous?.role === "user")
|
|
293
|
+
messages[messages.length - 1] = { role: "user", content: [...previous.content, ...content] };
|
|
294
|
+
else
|
|
295
|
+
messages.push({ role: "user", content });
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (message.role === "user") {
|
|
299
|
+
const content = [];
|
|
300
|
+
for (const part of message.content) {
|
|
301
|
+
if (part.type === "text") {
|
|
302
|
+
content.push(...textWithCache(breakpoints, part.text, part.cache));
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (part.type === "media") {
|
|
306
|
+
content.push(...(yield* BedrockMedia.lower(part, documentNames)));
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const previous = messages.at(-1);
|
|
311
|
+
if (previous?.role === "user")
|
|
312
|
+
messages[messages.length - 1] = { role: "user", content: [...previous.content, ...content] };
|
|
313
|
+
else
|
|
314
|
+
messages.push({ role: "user", content });
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (message.role === "assistant") {
|
|
318
|
+
const content = [];
|
|
319
|
+
for (const part of message.content) {
|
|
320
|
+
if (!ProviderShared.supportsContent(part, ["text", "reasoning", "tool-call"]))
|
|
321
|
+
return yield* ProviderShared.unsupportedContent("Bedrock Converse", "assistant", [
|
|
322
|
+
"text",
|
|
323
|
+
"reasoning",
|
|
324
|
+
"tool-call",
|
|
325
|
+
]);
|
|
326
|
+
if (part.type === "text") {
|
|
327
|
+
content.push(...textWithCache(breakpoints, part.text, part.cache));
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (part.type === "reasoning") {
|
|
331
|
+
const signature = reasoningSignature(part, providerMetadataKey);
|
|
332
|
+
const redactedData = reasoningRedactedData(part, providerMetadataKey);
|
|
333
|
+
if (signature === undefined && redactedData !== undefined) {
|
|
334
|
+
content.push({ reasoningContent: { redactedContent: redactedData } });
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
if (signature === undefined || signature.trim().length === 0)
|
|
338
|
+
continue;
|
|
339
|
+
content.push({ reasoningContent: { reasoningText: { text: part.text, signature } } });
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (part.type === "tool-call") {
|
|
343
|
+
content.push(lowerToolCall(part, normalizeID));
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (content.length > 0)
|
|
348
|
+
messages.push({ role: "assistant", content });
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const content = [];
|
|
352
|
+
for (const part of message.content) {
|
|
353
|
+
const result = yield* lowerToolResult(part, documentNames, normalizeID);
|
|
354
|
+
const images = hoistImages
|
|
355
|
+
? result.toolResult.content.filter((item) => "image" in item)
|
|
356
|
+
: [];
|
|
357
|
+
const nonImageContent = result.toolResult.content.filter((item) => !("image" in item));
|
|
358
|
+
content.push(images.length === 0
|
|
359
|
+
? result
|
|
360
|
+
: {
|
|
361
|
+
toolResult: {
|
|
362
|
+
...result.toolResult,
|
|
363
|
+
content: nonImageContent.length > 0 ? nonImageContent : [{ text: "See attached image." }],
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
pendingImages.push(...images);
|
|
367
|
+
const cachePoint = BedrockCache.block(breakpoints, part.cache);
|
|
368
|
+
if (cachePoint)
|
|
369
|
+
content.push(cachePoint);
|
|
370
|
+
}
|
|
371
|
+
const previous = messages.at(-1);
|
|
372
|
+
if (previous?.role === "user")
|
|
373
|
+
messages[messages.length - 1] = { role: "user", content: [...previous.content, ...content] };
|
|
374
|
+
else
|
|
375
|
+
messages.push({ role: "user", content });
|
|
376
|
+
}
|
|
377
|
+
flushImages();
|
|
378
|
+
return messages;
|
|
379
|
+
});
|
|
380
|
+
// System prompts share the cache-point convention: emit the text block, then
|
|
381
|
+
// optionally a positional `cachePoint` marker.
|
|
382
|
+
const lowerSystem = (breakpoints, system) => {
|
|
383
|
+
const content = system
|
|
384
|
+
.filter((part) => part.text.length > 0)
|
|
385
|
+
.flatMap((part) => textWithCache(breakpoints, part.text, part.cache));
|
|
386
|
+
return content.length === 0 ? undefined : content;
|
|
387
|
+
};
|
|
388
|
+
// Nova 2 rejects `maxTokens` at high reasoning effort, where its output can exceed the field's maximum. Other models
|
|
389
|
+
// that take `reasoningConfig`, such as Grok on Bedrock, accept it.
|
|
390
|
+
const isNova2 = (model) => /\bamazon\.nova-2-/.test(model.id);
|
|
391
|
+
const isHighReasoningEffort = Schema.is(Schema.Struct({
|
|
392
|
+
additionalModelRequestFields: Schema.Struct({
|
|
393
|
+
reasoningConfig: Schema.Struct({ maxReasoningEffort: Schema.Literal("high") }),
|
|
394
|
+
}),
|
|
395
|
+
}));
|
|
396
|
+
// Claude 4.6+ pairs effort with adaptive thinking; earlier Claude models take effort beside a manual budget.
|
|
397
|
+
const supportsAdaptiveThinking = (model) => {
|
|
398
|
+
const version = claudeVersion(model.id);
|
|
399
|
+
return version !== undefined && (version.major > 4 || (version.major === 4 && version.minor >= 6));
|
|
400
|
+
};
|
|
401
|
+
const effortFields = (model, effort) => {
|
|
402
|
+
if (model.id.includes("anthropic"))
|
|
403
|
+
return { output_config: { effort } };
|
|
404
|
+
// gpt-oss takes the flat chat-completions spelling; newer OpenAI and xAI models take the Responses one.
|
|
405
|
+
if (model.id.includes("openai.gpt-oss"))
|
|
406
|
+
return { reasoning_effort: effort };
|
|
407
|
+
if (model.id.includes("openai.") || model.id.includes("xai."))
|
|
408
|
+
return { reasoning: { effort } };
|
|
409
|
+
return { reasoningConfig: { type: "enabled", maxReasoningEffort: effort } };
|
|
410
|
+
};
|
|
411
|
+
const Options = Schema.Struct({
|
|
412
|
+
/** Claude thinking. `adaptive` and `disabled` apply to Claude models only. */
|
|
413
|
+
thinking: Schema.optional(Schema.Union([
|
|
414
|
+
Schema.Struct({ type: Schema.Literal("enabled"), budgetTokens: Schema.Number }),
|
|
415
|
+
Schema.Struct({
|
|
416
|
+
type: Schema.Literal("adaptive"),
|
|
417
|
+
display: Schema.optional(ProviderShared.knownString()),
|
|
418
|
+
}),
|
|
419
|
+
Schema.Struct({ type: Schema.Literal("disabled") }),
|
|
420
|
+
])),
|
|
421
|
+
/** Reasoning effort, sent in the field each model family reads. */
|
|
422
|
+
effort: Schema.optional(ProviderShared.knownString()),
|
|
423
|
+
serviceTier: Schema.optional(ServiceTierType),
|
|
424
|
+
/** Constrains the response text to a JSON schema. */
|
|
425
|
+
outputFormat: Schema.optional(Schema.Struct({
|
|
426
|
+
type: TextFormatType,
|
|
427
|
+
schema: JsonObject,
|
|
428
|
+
name: Schema.optional(Schema.String),
|
|
429
|
+
description: Schema.optional(Schema.String),
|
|
430
|
+
})),
|
|
431
|
+
guardrail: Schema.optional(Schema.Struct({
|
|
432
|
+
identifier: Schema.String,
|
|
433
|
+
version: Schema.String,
|
|
434
|
+
trace: Schema.optional(GuardrailTrace),
|
|
435
|
+
streamProcessingMode: Schema.optional(GuardrailStreamProcessingMode),
|
|
436
|
+
})),
|
|
437
|
+
/** Key-value pairs for filtering invocation logs. */
|
|
438
|
+
requestMetadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
439
|
+
});
|
|
440
|
+
const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options));
|
|
441
|
+
// Claude on Bedrock requires the thinking budget below `maxTokens`, with a minimum of 1,024.
|
|
442
|
+
const MIN_THINKING_BUDGET = 1_024;
|
|
443
|
+
const isThinkingDisabled = Schema.is(Schema.Struct({
|
|
444
|
+
additionalModelRequestFields: Schema.Struct({
|
|
445
|
+
thinking: Schema.Struct({ type: Schema.Literals(["disabled", "between_tools"]) }),
|
|
446
|
+
}),
|
|
447
|
+
}));
|
|
448
|
+
// Claude 5.1+ binds each thinking signature to the prefix above it. Ask Bedrock to drop the affected blocks instead of
|
|
449
|
+
// failing when that prefix changes. `http.body` overlays this field by field, so callers can still override it.
|
|
450
|
+
const applyThinkingBindingDefault = (request, thinking) => {
|
|
451
|
+
if (thinking?.type === "disabled" || isThinkingDisabled(request.http?.body))
|
|
452
|
+
return thinking;
|
|
453
|
+
if (!supportsThinkingBlockBinding(request.model))
|
|
454
|
+
return thinking;
|
|
455
|
+
return {
|
|
456
|
+
...(thinking ?? { type: "adaptive" }),
|
|
457
|
+
block_binding: { prefix_mismatch_behavior: "drop_block" },
|
|
458
|
+
};
|
|
459
|
+
};
|
|
460
|
+
const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request) {
|
|
461
|
+
const toolChoice = request.toolChoice ? lowerToolChoice(request.toolChoice) : undefined;
|
|
462
|
+
const definitions = ProviderShared.flattenTools(request.tools);
|
|
463
|
+
const options = yield* decodeOptions(request.providerOptions ?? {});
|
|
464
|
+
const maxTokens = isNova2(request.model) && (options.effort === "high" || isHighReasoningEffort(request.http?.body))
|
|
465
|
+
? undefined
|
|
466
|
+
: request.maxTokens;
|
|
467
|
+
const thinking = applyThinkingBindingDefault(request, (() => {
|
|
468
|
+
if (options.thinking?.type === "enabled")
|
|
469
|
+
return {
|
|
470
|
+
type: "enabled",
|
|
471
|
+
budget_tokens: ProviderShared.fitThinkingBudget(options.thinking.budgetTokens, maxTokens, MIN_THINKING_BUDGET),
|
|
472
|
+
};
|
|
473
|
+
if (options.thinking !== undefined)
|
|
474
|
+
return options.thinking;
|
|
475
|
+
if (options.effort !== undefined && supportsAdaptiveThinking(request.model))
|
|
476
|
+
return { type: "adaptive" };
|
|
477
|
+
})());
|
|
478
|
+
const additionalModelRequestFields = {
|
|
479
|
+
...(request.topK === undefined ? {} : { top_k: request.topK }),
|
|
480
|
+
...(thinking === undefined ? {} : { thinking }),
|
|
481
|
+
...(options.effort === undefined ? {} : effortFields(request.model, options.effort)),
|
|
482
|
+
// Converse takes Anthropic betas in the body, and Bedrock rejects `block_binding` without this one.
|
|
483
|
+
...(thinking?.block_binding === undefined ? {} : { anthropic_beta: [THINKING_BINDING_BETA] }),
|
|
484
|
+
};
|
|
485
|
+
// Bedrock-Claude shares Anthropic's 4-breakpoint cap. Spend the budget in
|
|
486
|
+
// tools → system → messages order to favour the highest-impact prefixes.
|
|
487
|
+
const breakpoints = BedrockCache.breakpoints(request.model);
|
|
488
|
+
const toolConfig = (() => {
|
|
489
|
+
if (definitions.length === 0)
|
|
490
|
+
return undefined;
|
|
491
|
+
return {
|
|
492
|
+
tools: lowerTools(breakpoints, definitions),
|
|
493
|
+
// Converse has no native "none". Keep definitions stable for prompt
|
|
494
|
+
// caching and omit only the unsupported choice.
|
|
495
|
+
toolChoice,
|
|
496
|
+
};
|
|
497
|
+
})();
|
|
498
|
+
const system = lowerSystem(breakpoints, request.system);
|
|
499
|
+
const messages = yield* lowerMessages(request, breakpoints);
|
|
500
|
+
yield* warnDropped(breakpoints, "Bedrock Converse", BedrockCache.BEDROCK_BREAKPOINT_CAP);
|
|
501
|
+
const inferenceConfig = (() => {
|
|
502
|
+
if (maxTokens === undefined &&
|
|
503
|
+
request.temperature === undefined &&
|
|
504
|
+
request.topP === undefined &&
|
|
505
|
+
(request.stop === undefined || request.stop.length === 0))
|
|
506
|
+
return undefined;
|
|
507
|
+
return {
|
|
508
|
+
maxTokens,
|
|
509
|
+
temperature: request.temperature,
|
|
510
|
+
topP: request.topP,
|
|
511
|
+
stopSequences: request.stop,
|
|
512
|
+
};
|
|
513
|
+
})();
|
|
514
|
+
return {
|
|
515
|
+
modelId: request.model.id,
|
|
516
|
+
messages,
|
|
517
|
+
system,
|
|
518
|
+
inferenceConfig,
|
|
519
|
+
toolConfig,
|
|
520
|
+
// Converse's base inferenceConfig has no topK, thinking or effort; models accept them
|
|
521
|
+
// as model-specific fields, so they go through additionalModelRequestFields.
|
|
522
|
+
additionalModelRequestFields: Object.keys(additionalModelRequestFields).length === 0 ? undefined : additionalModelRequestFields,
|
|
523
|
+
serviceTier: options.serviceTier === undefined ? undefined : { type: options.serviceTier },
|
|
524
|
+
guardrailConfig: options.guardrail === undefined
|
|
525
|
+
? undefined
|
|
526
|
+
: {
|
|
527
|
+
guardrailIdentifier: options.guardrail.identifier,
|
|
528
|
+
guardrailVersion: options.guardrail.version,
|
|
529
|
+
trace: options.guardrail.trace,
|
|
530
|
+
streamProcessingMode: options.guardrail.streamProcessingMode,
|
|
531
|
+
},
|
|
532
|
+
outputConfig: options.outputFormat === undefined
|
|
533
|
+
? undefined
|
|
534
|
+
: {
|
|
535
|
+
textFormat: {
|
|
536
|
+
type: options.outputFormat.type,
|
|
537
|
+
structure: {
|
|
538
|
+
jsonSchema: {
|
|
539
|
+
schema: ProviderShared.encodeJson(options.outputFormat.schema),
|
|
540
|
+
name: options.outputFormat.name,
|
|
541
|
+
description: options.outputFormat.description,
|
|
542
|
+
},
|
|
543
|
+
},
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
requestMetadata: options.requestMetadata,
|
|
547
|
+
};
|
|
548
|
+
});
|
|
549
|
+
// =============================================================================
|
|
550
|
+
// Stream Parsing
|
|
551
|
+
// =============================================================================
|
|
552
|
+
const mapFinishReason = (reason) => {
|
|
553
|
+
if (reason === "end_turn" || reason === "stop_sequence")
|
|
554
|
+
return "stop";
|
|
555
|
+
if (reason === "max_tokens" || reason === "model_context_window_exceeded")
|
|
556
|
+
return "length";
|
|
557
|
+
if (reason === "tool_use")
|
|
558
|
+
return "tool-calls";
|
|
559
|
+
if (reason === "content_filtered" || reason === "guardrail_intervened")
|
|
560
|
+
return "content-filter";
|
|
561
|
+
return "unknown";
|
|
562
|
+
};
|
|
563
|
+
// AWS reports inputTokens separately from cache reads and writes.
|
|
564
|
+
// Bedrock does not break reasoning out of outputTokens for current models.
|
|
565
|
+
const mapUsage = (usage, providerMetadataKey) => {
|
|
566
|
+
if (!usage)
|
|
567
|
+
return undefined;
|
|
568
|
+
const inputTokens = ProviderShared.sumTokens(usage.inputTokens, usage.cacheReadInputTokens, usage.cacheWriteInputTokens);
|
|
569
|
+
return new Usage({
|
|
570
|
+
inputTokens,
|
|
571
|
+
outputTokens: usage.outputTokens,
|
|
572
|
+
nonCachedInputTokens: usage.inputTokens,
|
|
573
|
+
cacheReadInputTokens: usage.cacheReadInputTokens,
|
|
574
|
+
cacheWriteInputTokens: usage.cacheWriteInputTokens,
|
|
575
|
+
totalTokens: ProviderShared.totalTokens(inputTokens, usage.outputTokens, usage.totalTokens),
|
|
576
|
+
providerMetadata: { [providerMetadataKey]: usage },
|
|
577
|
+
});
|
|
578
|
+
};
|
|
579
|
+
const encodeRedactedContent = (chunks) => Base64.encode(concatBytes(chunks));
|
|
580
|
+
const step = (state, event) => Effect.gen(function* () {
|
|
581
|
+
if (event.contentBlockStart?.start?.toolUse) {
|
|
582
|
+
const index = event.contentBlockStart.contentBlockIndex;
|
|
583
|
+
const events = [];
|
|
584
|
+
const lifecycle = Lifecycle.start(state.lifecycle);
|
|
585
|
+
return [
|
|
586
|
+
{
|
|
587
|
+
...state,
|
|
588
|
+
lifecycle,
|
|
589
|
+
tools: ToolStream.start(state.tools, index, {
|
|
590
|
+
id: event.contentBlockStart.start.toolUse.toolUseId,
|
|
591
|
+
name: event.contentBlockStart.start.toolUse.name,
|
|
592
|
+
}),
|
|
593
|
+
},
|
|
594
|
+
[
|
|
595
|
+
...events,
|
|
596
|
+
StreamEvent.toolInputStart({
|
|
597
|
+
id: event.contentBlockStart.start.toolUse.toolUseId,
|
|
598
|
+
name: event.contentBlockStart.start.toolUse.name,
|
|
599
|
+
}),
|
|
600
|
+
],
|
|
601
|
+
];
|
|
602
|
+
}
|
|
603
|
+
if (event.contentBlockDelta?.delta?.text) {
|
|
604
|
+
const events = [];
|
|
605
|
+
return [
|
|
606
|
+
{
|
|
607
|
+
...state,
|
|
608
|
+
lifecycle: Lifecycle.textDelta(state.lifecycle, events, `text-${event.contentBlockDelta.contentBlockIndex}`, event.contentBlockDelta.delta.text),
|
|
609
|
+
},
|
|
610
|
+
events,
|
|
611
|
+
];
|
|
612
|
+
}
|
|
613
|
+
if (event.contentBlockDelta?.delta?.reasoningContent) {
|
|
614
|
+
const index = event.contentBlockDelta.contentBlockIndex;
|
|
615
|
+
const reasoning = event.contentBlockDelta.delta.reasoningContent;
|
|
616
|
+
const events = [];
|
|
617
|
+
const redactedChunk = yield* (() => {
|
|
618
|
+
if (reasoning.redactedContent === undefined)
|
|
619
|
+
return Effect.succeed(undefined);
|
|
620
|
+
return Effect.fromResult(Base64.decode(reasoning.redactedContent)).pipe(Effect.mapError((cause) => ProviderShared.eventError(ADAPTER, "Bedrock Converse reasoningContent.redactedContent contains invalid base64 data", undefined, cause)));
|
|
621
|
+
})();
|
|
622
|
+
const redactedChunks = state.reasoningRedactedContent[index] ?? [];
|
|
623
|
+
if (redactedChunk !== undefined)
|
|
624
|
+
redactedChunks.push(redactedChunk);
|
|
625
|
+
const metadata = (() => {
|
|
626
|
+
if (reasoning.signature)
|
|
627
|
+
return providerMetadata(state.providerMetadataKey, { signature: reasoning.signature });
|
|
628
|
+
if (redactedChunk === undefined && reasoning.data !== undefined)
|
|
629
|
+
return providerMetadata(state.providerMetadataKey, { redactedData: reasoning.data });
|
|
630
|
+
})();
|
|
631
|
+
const lifecycle = (() => {
|
|
632
|
+
if (reasoning.text !== undefined || metadata !== undefined)
|
|
633
|
+
return Lifecycle.reasoningDelta(state.lifecycle, events, `reasoning-${index}`, reasoning.text ?? "", metadata);
|
|
634
|
+
if (redactedChunk !== undefined)
|
|
635
|
+
return Lifecycle.reasoningStart(state.lifecycle, events, `reasoning-${index}`);
|
|
636
|
+
return state.lifecycle;
|
|
637
|
+
})();
|
|
638
|
+
const reasoningRedactedContent = (() => {
|
|
639
|
+
if (redactedChunk !== undefined)
|
|
640
|
+
return { ...state.reasoningRedactedContent, [index]: redactedChunks };
|
|
641
|
+
if (reasoning.data === undefined)
|
|
642
|
+
return state.reasoningRedactedContent;
|
|
643
|
+
return Object.fromEntries(Object.entries(state.reasoningRedactedContent).filter(([key]) => key !== String(index)));
|
|
644
|
+
})();
|
|
645
|
+
const reasoningSignatures = (() => {
|
|
646
|
+
if (!reasoning.signature)
|
|
647
|
+
return state.reasoningSignatures;
|
|
648
|
+
return { ...state.reasoningSignatures, [index]: reasoning.signature };
|
|
649
|
+
})();
|
|
650
|
+
return [
|
|
651
|
+
{
|
|
652
|
+
...state,
|
|
653
|
+
lifecycle,
|
|
654
|
+
reasoningSignatures,
|
|
655
|
+
reasoningRedactedContent,
|
|
656
|
+
},
|
|
657
|
+
events,
|
|
658
|
+
];
|
|
659
|
+
}
|
|
660
|
+
if (event.contentBlockDelta?.delta?.toolUse) {
|
|
661
|
+
// A delta for a block that is not open, whether it already stopped or never
|
|
662
|
+
// started, has nothing to attach to and is dropped.
|
|
663
|
+
const result = ToolStream.append(state.tools, event.contentBlockDelta.contentBlockIndex, event.contentBlockDelta.delta.toolUse.input);
|
|
664
|
+
if (!result)
|
|
665
|
+
return [state, []];
|
|
666
|
+
const events = [];
|
|
667
|
+
const lifecycle = result.events.length ? Lifecycle.start(state.lifecycle) : state.lifecycle;
|
|
668
|
+
events.push(...result.events);
|
|
669
|
+
return [{ ...state, lifecycle, tools: result.tools }, events];
|
|
670
|
+
}
|
|
671
|
+
if (event.contentBlockStop) {
|
|
672
|
+
const index = event.contentBlockStop.contentBlockIndex;
|
|
673
|
+
const result = yield* ToolStream.finish(ADAPTER, state.tools, index);
|
|
674
|
+
const events = [];
|
|
675
|
+
const resultEvents = result.events ?? [];
|
|
676
|
+
const lifecycle = (() => {
|
|
677
|
+
if (resultEvents.length)
|
|
678
|
+
return Lifecycle.start(state.lifecycle);
|
|
679
|
+
const metadata = (() => {
|
|
680
|
+
const signature = state.reasoningSignatures[index];
|
|
681
|
+
if (signature)
|
|
682
|
+
return providerMetadata(state.providerMetadataKey, { signature });
|
|
683
|
+
const redactedContent = state.reasoningRedactedContent[index];
|
|
684
|
+
if (redactedContent)
|
|
685
|
+
return providerMetadata(state.providerMetadataKey, {
|
|
686
|
+
redactedData: encodeRedactedContent(redactedContent),
|
|
687
|
+
});
|
|
688
|
+
})();
|
|
689
|
+
return Lifecycle.reasoningEnd(Lifecycle.textEnd(state.lifecycle, events, `text-${index}`), events, `reasoning-${index}`, metadata);
|
|
690
|
+
})();
|
|
691
|
+
events.push(...resultEvents);
|
|
692
|
+
return [
|
|
693
|
+
{
|
|
694
|
+
...state,
|
|
695
|
+
hasToolCalls: resultEvents.some((event) => event.type === "tool-call" || event.type === "tool-input-error") ||
|
|
696
|
+
state.hasToolCalls,
|
|
697
|
+
lifecycle,
|
|
698
|
+
tools: result.tools,
|
|
699
|
+
reasoningSignatures: Object.fromEntries(Object.entries(state.reasoningSignatures).filter(([key]) => key !== String(index))),
|
|
700
|
+
reasoningRedactedContent: Object.fromEntries(Object.entries(state.reasoningRedactedContent).filter(([key]) => key !== String(index))),
|
|
701
|
+
},
|
|
702
|
+
events,
|
|
703
|
+
];
|
|
704
|
+
}
|
|
705
|
+
if (event.messageStop) {
|
|
706
|
+
if (event.messageStop.stopReason === "malformed_model_output" ||
|
|
707
|
+
event.messageStop.stopReason === "malformed_tool_use")
|
|
708
|
+
return yield* ProviderShared.eventError(ADAPTER, `Bedrock Converse stopped with ${event.messageStop.stopReason}`, ProviderShared.encodeJson(event));
|
|
709
|
+
return [
|
|
710
|
+
{
|
|
711
|
+
...state,
|
|
712
|
+
finish: {
|
|
713
|
+
reason: mapFinishReason(event.messageStop.stopReason),
|
|
714
|
+
raw: event.messageStop.stopReason,
|
|
715
|
+
},
|
|
716
|
+
},
|
|
717
|
+
[],
|
|
718
|
+
];
|
|
719
|
+
}
|
|
720
|
+
if (event.metadata) {
|
|
721
|
+
const usage = mapUsage(event.metadata.usage, state.providerMetadataKey) ?? state.usage;
|
|
722
|
+
return [
|
|
723
|
+
{
|
|
724
|
+
...state,
|
|
725
|
+
usage,
|
|
726
|
+
},
|
|
727
|
+
[],
|
|
728
|
+
];
|
|
729
|
+
}
|
|
730
|
+
if (event.exception) {
|
|
731
|
+
const message = event.exception.details.message ?? event.exception.details.originalMessage ?? "Bedrock Converse stream error";
|
|
732
|
+
const body = ProviderShared.encodeJson(event);
|
|
733
|
+
return yield* new AIError({
|
|
734
|
+
reason: classifyProviderFailure({
|
|
735
|
+
message,
|
|
736
|
+
status: event.exception.details.originalStatusCode,
|
|
737
|
+
rawBody: body,
|
|
738
|
+
}),
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
return [state, []];
|
|
742
|
+
});
|
|
743
|
+
const framing = BedrockEventStream.framing(ADAPTER);
|
|
744
|
+
const onHalt = (state) => {
|
|
745
|
+
if (!state.finish)
|
|
746
|
+
return [];
|
|
747
|
+
const reason = state.finish.reason === "stop" && state.hasToolCalls ? "tool-calls" : state.finish.reason;
|
|
748
|
+
const events = [];
|
|
749
|
+
const lifecycle = Object.entries(state.reasoningRedactedContent).reduce((current, [index, chunks]) => {
|
|
750
|
+
const signature = state.reasoningSignatures[Number(index)];
|
|
751
|
+
return Lifecycle.reasoningEnd(current, events, `reasoning-${index}`, providerMetadata(state.providerMetadataKey, signature ? { signature } : { redactedData: encodeRedactedContent(chunks) }));
|
|
752
|
+
}, state.lifecycle);
|
|
753
|
+
Lifecycle.finish(lifecycle, events, {
|
|
754
|
+
...state.finish,
|
|
755
|
+
reason,
|
|
756
|
+
usage: state.usage,
|
|
757
|
+
});
|
|
758
|
+
return events;
|
|
759
|
+
};
|
|
760
|
+
// =============================================================================
|
|
761
|
+
// Protocol And Bedrock Route
|
|
762
|
+
// =============================================================================
|
|
763
|
+
/**
|
|
764
|
+
* The Bedrock Converse protocol — request body construction, body schema, and
|
|
765
|
+
* the streaming-event state machine.
|
|
766
|
+
*/
|
|
767
|
+
export const protocol = Protocol.make({
|
|
768
|
+
id: ADAPTER,
|
|
769
|
+
body: {
|
|
770
|
+
schema: BedrockConverseBody,
|
|
771
|
+
from: fromRequest,
|
|
772
|
+
},
|
|
773
|
+
stream: {
|
|
774
|
+
event: BedrockEventStream.Frame.pipe(Schema.decodeTo(BedrockEvent)),
|
|
775
|
+
initial: (request) => ({
|
|
776
|
+
providerMetadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
777
|
+
tools: ToolStream.empty(),
|
|
778
|
+
finish: undefined,
|
|
779
|
+
usage: undefined,
|
|
780
|
+
hasToolCalls: false,
|
|
781
|
+
lifecycle: Lifecycle.initial(),
|
|
782
|
+
reasoningSignatures: {},
|
|
783
|
+
reasoningRedactedContent: {},
|
|
784
|
+
}),
|
|
785
|
+
step,
|
|
786
|
+
onHalt: (state) => Effect.succeed(onHalt(state)),
|
|
787
|
+
},
|
|
788
|
+
});
|
|
789
|
+
export const route = Route.make({
|
|
790
|
+
id: ADAPTER,
|
|
791
|
+
provider: "bedrock",
|
|
792
|
+
providerMetadataKey: "bedrock",
|
|
793
|
+
protocol,
|
|
794
|
+
// Bedrock's URL embeds the region in the route endpoint host and the
|
|
795
|
+
// validated modelId in the path. We read the validated body so the URL
|
|
796
|
+
// matches the body that gets signed.
|
|
797
|
+
endpoint: Endpoint.path(({ body }) => `/model/${encodeURIComponent(body.modelId)}/converse-stream`),
|
|
798
|
+
auth: BedrockAuth.auth,
|
|
799
|
+
framing,
|
|
800
|
+
});
|
|
801
|
+
export * as BedrockConverse from "./bedrock-converse.js";
|