@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,756 @@
|
|
|
1
|
+
import { Effect, Option, Schema, SchemaGetter, Stream } from "effect";
|
|
2
|
+
import { Route } from "../route/client.js";
|
|
3
|
+
import { Auth } from "../route/auth.js";
|
|
4
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
5
|
+
import { Protocol } from "../route/protocol.js";
|
|
6
|
+
import { HttpTransport, } from "../route/transport/index.js";
|
|
7
|
+
import { AIError, ContentPolicyError, InvalidProviderOutputError, StreamEvent, ProviderID, Usage, } from "../schema/index.js";
|
|
8
|
+
import { Media } from "../media.js";
|
|
9
|
+
import { classifyProviderFailure, providerErrorMessage } from "../provider-error.js";
|
|
10
|
+
import { encodeJson } from "../utils/json.js";
|
|
11
|
+
import { JsonObject, knownString, lenient, optionalNull, ProviderShared } from "./shared.js";
|
|
12
|
+
import { Lifecycle } from "./utils/lifecycle.js";
|
|
13
|
+
import { MediaInput } from "./utils/media-input.js";
|
|
14
|
+
import { GeminiGenerateContent } from "./utils/gemini-generate-content.js";
|
|
15
|
+
import { ToolStream } from "./utils/tool-stream.js";
|
|
16
|
+
const ADAPTER = "google-interactions";
|
|
17
|
+
const CONTINUATION = "requires_continuation";
|
|
18
|
+
// Matches Google's GenAI SDKs (Python, JS, and Go).
|
|
19
|
+
const MAX_STREAM_RECONNECTS_WITHOUT_PROGRESS = 3;
|
|
20
|
+
const SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
|
|
21
|
+
export const DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
22
|
+
// =============================================================================
|
|
23
|
+
// Public Model Input
|
|
24
|
+
// =============================================================================
|
|
25
|
+
const ThinkingLevel = knownString();
|
|
26
|
+
const Options = Schema.Struct({
|
|
27
|
+
previousInteractionId: lenient(Schema.String),
|
|
28
|
+
store: lenient(Schema.Boolean),
|
|
29
|
+
thinkingLevel: lenient(ThinkingLevel),
|
|
30
|
+
thinkingSummaries: lenient(knownString()),
|
|
31
|
+
serviceTier: lenient(knownString()),
|
|
32
|
+
safetySettings: lenient(Schema.Array(GeminiGenerateContent.SafetySetting)),
|
|
33
|
+
});
|
|
34
|
+
const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options));
|
|
35
|
+
// =============================================================================
|
|
36
|
+
// Request Body Schema
|
|
37
|
+
// =============================================================================
|
|
38
|
+
const Text = Schema.Struct({ type: Schema.Literal("text"), text: Schema.String });
|
|
39
|
+
const MediaContent = Schema.Struct({
|
|
40
|
+
type: Schema.Literals(["image", "audio", "video", "document"]),
|
|
41
|
+
data: Schema.optional(Schema.String),
|
|
42
|
+
uri: Schema.optional(Schema.String),
|
|
43
|
+
mime_type: Schema.String,
|
|
44
|
+
});
|
|
45
|
+
const Content = Schema.Union([Text, MediaContent]);
|
|
46
|
+
const InputStep = Schema.Union([
|
|
47
|
+
Schema.Struct({ type: Schema.Literals(["user_input", "model_output"]), content: Schema.Array(Content) }),
|
|
48
|
+
Schema.Struct({
|
|
49
|
+
type: Schema.Literal("thought"),
|
|
50
|
+
signature: Schema.optional(Schema.String),
|
|
51
|
+
summary: Schema.optional(Schema.Array(Text)),
|
|
52
|
+
}),
|
|
53
|
+
Schema.Struct({
|
|
54
|
+
type: Schema.Literal("function_call"),
|
|
55
|
+
id: Schema.String,
|
|
56
|
+
name: Schema.String,
|
|
57
|
+
arguments: Schema.Unknown,
|
|
58
|
+
signature: Schema.optional(Schema.String),
|
|
59
|
+
}),
|
|
60
|
+
Schema.Struct({
|
|
61
|
+
type: Schema.Literal("function_result"),
|
|
62
|
+
call_id: Schema.String,
|
|
63
|
+
name: Schema.String,
|
|
64
|
+
result: Schema.Unknown,
|
|
65
|
+
is_error: Schema.optional(Schema.Boolean),
|
|
66
|
+
}),
|
|
67
|
+
]);
|
|
68
|
+
const ToolChoice = Schema.Union([
|
|
69
|
+
Schema.Literals(["auto", "any", "none"]),
|
|
70
|
+
Schema.Struct({ allowed_tools: Schema.Struct({ mode: Schema.Literal("any"), tools: Schema.Array(Schema.String) }) }),
|
|
71
|
+
]);
|
|
72
|
+
const Body = Schema.Struct({
|
|
73
|
+
model: Schema.String,
|
|
74
|
+
input: Schema.optional(Schema.Array(InputStep)),
|
|
75
|
+
stream: Schema.Literal(true),
|
|
76
|
+
store: Schema.Boolean,
|
|
77
|
+
previous_interaction_id: Schema.optional(Schema.String),
|
|
78
|
+
continuation_token: Schema.optional(Schema.String),
|
|
79
|
+
system_instruction: Schema.optional(Schema.String),
|
|
80
|
+
service_tier: Schema.optional(Schema.String),
|
|
81
|
+
safety_settings: Schema.optional(Schema.Array(Schema.Struct({ type: Schema.String, threshold: Schema.String }))),
|
|
82
|
+
tools: Schema.optional(Schema.Array(Schema.Struct({
|
|
83
|
+
type: Schema.Literal("function"),
|
|
84
|
+
name: Schema.String,
|
|
85
|
+
description: Schema.String,
|
|
86
|
+
parameters: JsonObject,
|
|
87
|
+
}))),
|
|
88
|
+
generation_config: Schema.Struct({
|
|
89
|
+
max_output_tokens: Schema.optional(Schema.Number),
|
|
90
|
+
temperature: Schema.optional(Schema.Number),
|
|
91
|
+
top_p: Schema.optional(Schema.Number),
|
|
92
|
+
seed: Schema.optional(Schema.Number),
|
|
93
|
+
stop_sequences: Schema.optional(Schema.Array(Schema.String)),
|
|
94
|
+
thinking_level: Schema.optional(ThinkingLevel),
|
|
95
|
+
thinking_summaries: Schema.optional(Schema.String),
|
|
96
|
+
tool_choice: Schema.optional(ToolChoice),
|
|
97
|
+
}),
|
|
98
|
+
});
|
|
99
|
+
// =============================================================================
|
|
100
|
+
// Streaming Event Schema
|
|
101
|
+
// =============================================================================
|
|
102
|
+
const RawUsage = Schema.StructWithRest(Schema.Struct({
|
|
103
|
+
total_input_tokens: optionalNull(Schema.Number),
|
|
104
|
+
total_cached_tokens: optionalNull(Schema.Number),
|
|
105
|
+
total_output_tokens: optionalNull(Schema.Number),
|
|
106
|
+
total_thought_tokens: optionalNull(Schema.Number),
|
|
107
|
+
total_tokens: optionalNull(Schema.Number),
|
|
108
|
+
raw_prompt_token: optionalNull(Schema.Number),
|
|
109
|
+
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
110
|
+
const OutputStep = Schema.Struct({
|
|
111
|
+
type: Schema.String,
|
|
112
|
+
id: Schema.optional(Schema.String),
|
|
113
|
+
name: Schema.optional(Schema.String),
|
|
114
|
+
arguments: Schema.optional(JsonObject),
|
|
115
|
+
signature: Schema.optional(Schema.String),
|
|
116
|
+
summary: Schema.optional(Schema.Array(Text)),
|
|
117
|
+
content: Schema.optional(Schema.Array(Schema.Struct({ type: Schema.String, text: Schema.optional(Schema.String) }))),
|
|
118
|
+
});
|
|
119
|
+
const KNOWN_DELTA_TYPES = new Set([
|
|
120
|
+
"text",
|
|
121
|
+
"text_annotation_delta",
|
|
122
|
+
"arguments_delta",
|
|
123
|
+
"thought_signature",
|
|
124
|
+
"thought_summary",
|
|
125
|
+
]);
|
|
126
|
+
const UnknownDelta = Schema.Struct({
|
|
127
|
+
type: Schema.String.check(Schema.makeFilter((type) => !KNOWN_DELTA_TYPES.has(type))),
|
|
128
|
+
}).pipe(Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown"), rawType: Schema.String }), {
|
|
129
|
+
decode: SchemaGetter.transform((delta) => ({ type: "unknown", rawType: delta.type })),
|
|
130
|
+
encode: SchemaGetter.forbidden(() => "Unknown Google Interactions deltas are decode-only"),
|
|
131
|
+
}));
|
|
132
|
+
const Delta = Schema.Union([
|
|
133
|
+
Schema.Struct({ type: Schema.Literal("text"), text: Schema.String }),
|
|
134
|
+
// Citation/annotation metadata is outside this basic text surface.
|
|
135
|
+
Schema.Struct({ type: Schema.Literal("text_annotation_delta") }),
|
|
136
|
+
Schema.Struct({ type: Schema.Literal("arguments_delta"), arguments: Schema.String }),
|
|
137
|
+
Schema.Struct({ type: Schema.Literal("thought_signature"), signature: Schema.String }),
|
|
138
|
+
Schema.Struct({ type: Schema.Literal("thought_summary"), content: Text }),
|
|
139
|
+
// Unknown output modalities must fail explicitly rather than disappearing from a successful response.
|
|
140
|
+
UnknownDelta,
|
|
141
|
+
]);
|
|
142
|
+
const Interaction = Schema.StructWithRest(Schema.Struct({
|
|
143
|
+
id: Schema.optional(Schema.String),
|
|
144
|
+
status: Schema.String,
|
|
145
|
+
continuation_token: optionalNull(Schema.String),
|
|
146
|
+
usage: Schema.optional(RawUsage),
|
|
147
|
+
errors: Schema.optional(Schema.Array(Schema.Struct({ code: Schema.optional(Schema.String), message: Schema.optional(Schema.String) }))),
|
|
148
|
+
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
149
|
+
const KNOWN_EVENT_TYPES = new Set([
|
|
150
|
+
"step.start",
|
|
151
|
+
"step.delta",
|
|
152
|
+
"step.stop",
|
|
153
|
+
"interaction.created",
|
|
154
|
+
"interaction.status_update",
|
|
155
|
+
"interaction.completed",
|
|
156
|
+
"error",
|
|
157
|
+
]);
|
|
158
|
+
// Google may add stream event types. Unknown types are ignored, while a malformed known type still fails decoding.
|
|
159
|
+
const UnknownEvent = Schema.Struct({
|
|
160
|
+
event_type: Schema.String.check(Schema.makeFilter((type) => !KNOWN_EVENT_TYPES.has(type))),
|
|
161
|
+
}).pipe(Schema.decodeTo(Schema.Struct({ event_type: Schema.Literal("unknown") }), {
|
|
162
|
+
decode: SchemaGetter.transform(() => ({ event_type: "unknown" })),
|
|
163
|
+
encode: SchemaGetter.forbidden(() => "Unknown Google Interactions stream events are decode-only"),
|
|
164
|
+
}));
|
|
165
|
+
const ErrorDetail = Schema.StructWithRest(Schema.Struct({
|
|
166
|
+
code: lenient(Schema.String),
|
|
167
|
+
error_type: lenient(Schema.String),
|
|
168
|
+
message: lenient(Schema.String),
|
|
169
|
+
}), [Schema.Record(Schema.String, Schema.Unknown)]);
|
|
170
|
+
const Event = Schema.Union([
|
|
171
|
+
Schema.Struct({ event_type: Schema.Literal("step.start"), index: Schema.Number, step: OutputStep }),
|
|
172
|
+
Schema.Struct({ event_type: Schema.Literal("step.delta"), index: Schema.Number, delta: Delta }),
|
|
173
|
+
Schema.Struct({ event_type: Schema.Literal("step.stop"), index: Schema.Number }),
|
|
174
|
+
Schema.Struct({
|
|
175
|
+
event_type: Schema.Literal("interaction.created"),
|
|
176
|
+
interaction: Schema.Struct({ id: Schema.optional(Schema.String) }),
|
|
177
|
+
}),
|
|
178
|
+
Schema.Struct({
|
|
179
|
+
event_type: Schema.Literal("interaction.status_update"),
|
|
180
|
+
interaction_id: Schema.optional(Schema.String),
|
|
181
|
+
status: Schema.String,
|
|
182
|
+
continuation_token: optionalNull(Schema.String),
|
|
183
|
+
usage: Schema.optional(RawUsage),
|
|
184
|
+
}),
|
|
185
|
+
Schema.Struct({ event_type: Schema.Literal("interaction.completed"), interaction: Interaction }),
|
|
186
|
+
Schema.Struct({ event_type: Schema.Literal("error"), error: Schema.optionalKey(Schema.Unknown) }),
|
|
187
|
+
UnknownEvent,
|
|
188
|
+
]);
|
|
189
|
+
// =============================================================================
|
|
190
|
+
// Request Body Construction
|
|
191
|
+
// =============================================================================
|
|
192
|
+
// https://aistudio.google.com/docs/document-processing#office-documents
|
|
193
|
+
const OFFICE_DOCUMENT_MIME_TYPES = new Set([
|
|
194
|
+
"application/msword",
|
|
195
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
196
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.template",
|
|
197
|
+
"application/vnd.oasis.opendocument.text",
|
|
198
|
+
"application/vnd.ms-powerpoint",
|
|
199
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
200
|
+
"application/vnd.oasis.opendocument.presentation",
|
|
201
|
+
"application/vnd.ms-excel",
|
|
202
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
203
|
+
"application/vnd.oasis.opendocument.spreadsheet",
|
|
204
|
+
]);
|
|
205
|
+
export const mediaContent = Effect.fnUntraced(function* (asset) {
|
|
206
|
+
if (asset.kind !== "image" &&
|
|
207
|
+
asset.kind !== "audio" &&
|
|
208
|
+
asset.kind !== "video" &&
|
|
209
|
+
asset.mediaType !== "application/pdf" &&
|
|
210
|
+
asset.mediaType !== "text/csv" &&
|
|
211
|
+
!OFFICE_DOCUMENT_MIME_TYPES.has(asset.mediaType))
|
|
212
|
+
return yield* ProviderShared.invalidRequest(`Google Interactions does not support ${asset.mediaType} document input`);
|
|
213
|
+
const type = asset.kind === "image" || asset.kind === "audio" || asset.kind === "video" ? asset.kind : "document";
|
|
214
|
+
const uri = asset.kind === "video" && asset.source.type === "url" && /^https?:\/\//i.test(asset.source.url)
|
|
215
|
+
? asset.source.url
|
|
216
|
+
: MediaInput.refID(asset, ProviderID.make("google"));
|
|
217
|
+
if (uri !== undefined)
|
|
218
|
+
return { type, uri, mime_type: asset.mediaType };
|
|
219
|
+
const inline = yield* ProviderShared.requireInlineMedia("Google Interactions", asset);
|
|
220
|
+
return { type, data: inline.base64, mime_type: inline.mime };
|
|
221
|
+
});
|
|
222
|
+
const signature = (metadata, key) => {
|
|
223
|
+
const value = metadata?.[key];
|
|
224
|
+
return typeof value?.interactionSignature === "string" ? value.interactionSignature : undefined;
|
|
225
|
+
};
|
|
226
|
+
const lowerMessages = Effect.fnUntraced(function* (request) {
|
|
227
|
+
const steps = [];
|
|
228
|
+
const key = request.model.route.providerMetadataKey ?? request.model.provider;
|
|
229
|
+
for (const message of request.messages) {
|
|
230
|
+
if (message.role === "system") {
|
|
231
|
+
const part = yield* ProviderShared.wrappedSystemUpdate("Google Interactions", message);
|
|
232
|
+
steps.push({ type: "user_input", content: [{ type: "text", text: part.text }] });
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const start = steps.length;
|
|
236
|
+
// Consecutive ordinary content remains one native message; tools and thoughts retain their chronology.
|
|
237
|
+
const append = (content) => {
|
|
238
|
+
const type = message.role === "assistant" ? "model_output" : "user_input";
|
|
239
|
+
const last = steps.at(-1);
|
|
240
|
+
if (steps.length > start && last?.type === type)
|
|
241
|
+
steps[steps.length - 1] = { type, content: [...last.content, content] };
|
|
242
|
+
else
|
|
243
|
+
steps.push({ type, content: [content] });
|
|
244
|
+
};
|
|
245
|
+
let hasSignedStep = false;
|
|
246
|
+
for (const part of message.content) {
|
|
247
|
+
if (message.role === "tool") {
|
|
248
|
+
if (part.type !== "tool-result")
|
|
249
|
+
return yield* ProviderShared.unsupportedContent(ADAPTER, "tool", ["tool-result"]);
|
|
250
|
+
steps.push({
|
|
251
|
+
type: "function_result",
|
|
252
|
+
call_id: part.id,
|
|
253
|
+
name: part.name,
|
|
254
|
+
result: yield* lowerToolResult(part),
|
|
255
|
+
is_error: part.result.type === "error" || undefined,
|
|
256
|
+
});
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (part.type === "text") {
|
|
260
|
+
append({ type: "text", text: part.text });
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (part.type === "media") {
|
|
264
|
+
append(yield* mediaContent(part.media));
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (message.role === "assistant" && part.type === "reasoning") {
|
|
268
|
+
const thoughtSignature = signature(part.providerMetadata, key);
|
|
269
|
+
steps.push({
|
|
270
|
+
type: "thought",
|
|
271
|
+
signature: thoughtSignature,
|
|
272
|
+
summary: part.text ? [{ type: "text", text: part.text }] : undefined,
|
|
273
|
+
});
|
|
274
|
+
if (thoughtSignature !== undefined)
|
|
275
|
+
hasSignedStep = true;
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (message.role === "assistant" && part.type === "tool-call") {
|
|
279
|
+
const toolSignature = signature(part.providerMetadata, key);
|
|
280
|
+
steps.push({
|
|
281
|
+
type: "function_call",
|
|
282
|
+
id: part.id,
|
|
283
|
+
name: part.name,
|
|
284
|
+
arguments: part.input,
|
|
285
|
+
signature: toolSignature ?? (!hasSignedStep ? SKIP_THOUGHT_SIGNATURE_VALIDATOR : undefined),
|
|
286
|
+
});
|
|
287
|
+
if (toolSignature !== undefined)
|
|
288
|
+
hasSignedStep = true;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
return yield* ProviderShared.unsupportedContent(ADAPTER, message.role, message.role === "user" ? ["text", "media"] : ["text", "media", "reasoning", "tool-call"]);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return steps;
|
|
295
|
+
});
|
|
296
|
+
const lowerToolResult = Effect.fnUntraced(function* (part) {
|
|
297
|
+
if (part.result.type === "json" && ProviderShared.isRecord(part.result.value))
|
|
298
|
+
return part.result.value;
|
|
299
|
+
if (part.result.type !== "content")
|
|
300
|
+
return ProviderShared.toolResultText(part);
|
|
301
|
+
return yield* Effect.forEach(part.result.value, (item) => {
|
|
302
|
+
if (item.type === "text")
|
|
303
|
+
return Effect.succeed({ type: "text", text: item.text });
|
|
304
|
+
return mediaContent(GeminiGenerateContent.toolMedia(item));
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
const fromRequest = Effect.fn("GoogleInteractions.fromRequest")(function* (request) {
|
|
308
|
+
const options = yield* decodeOptions(request.providerOptions ?? {});
|
|
309
|
+
const definitions = ProviderShared.flattenTools(request.tools);
|
|
310
|
+
if (definitions.some((tool) => tool.native !== undefined))
|
|
311
|
+
return yield* ProviderShared.invalidRequest("Google Interactions hosted tools are not supported");
|
|
312
|
+
if (request.topK !== undefined || request.frequencyPenalty !== undefined || request.presencePenalty !== undefined)
|
|
313
|
+
return yield* ProviderShared.invalidRequest("Google Interactions does not support topK, frequencyPenalty, or presencePenalty");
|
|
314
|
+
const choice = request.toolChoice === undefined
|
|
315
|
+
? undefined
|
|
316
|
+
: ProviderShared.matchToolChoice(request.toolChoice, {
|
|
317
|
+
auto: () => "auto",
|
|
318
|
+
none: () => "none",
|
|
319
|
+
required: () => "any",
|
|
320
|
+
tool: (name) => ({ allowed_tools: { mode: "any", tools: [name] } }),
|
|
321
|
+
});
|
|
322
|
+
return {
|
|
323
|
+
model: request.model.id,
|
|
324
|
+
input: yield* lowerMessages(request),
|
|
325
|
+
stream: true,
|
|
326
|
+
// Full-history replay need not create retained provider resources. Continuation callers opt in to storage.
|
|
327
|
+
store: options.store ?? false,
|
|
328
|
+
previous_interaction_id: options.previousInteractionId,
|
|
329
|
+
system_instruction: request.system.length ? ProviderShared.joinText(request.system) : undefined,
|
|
330
|
+
service_tier: options.serviceTier,
|
|
331
|
+
safety_settings: options.safetySettings?.map((setting) => ({
|
|
332
|
+
type: setting.category.replace(/^HARM_CATEGORY_/, "").toLowerCase(),
|
|
333
|
+
threshold: setting.threshold.toLowerCase(),
|
|
334
|
+
})),
|
|
335
|
+
tools: definitions.length
|
|
336
|
+
? definitions.map((tool) => ({
|
|
337
|
+
type: "function",
|
|
338
|
+
name: tool.name,
|
|
339
|
+
description: tool.description,
|
|
340
|
+
parameters: tool.inputSchema,
|
|
341
|
+
}))
|
|
342
|
+
: undefined,
|
|
343
|
+
generation_config: {
|
|
344
|
+
max_output_tokens: request.maxTokens,
|
|
345
|
+
temperature: request.temperature,
|
|
346
|
+
top_p: request.topP,
|
|
347
|
+
seed: request.seed,
|
|
348
|
+
stop_sequences: request.stop,
|
|
349
|
+
thinking_level: options.thinkingLevel,
|
|
350
|
+
thinking_summaries: options.thinkingSummaries,
|
|
351
|
+
tool_choice: choice,
|
|
352
|
+
},
|
|
353
|
+
};
|
|
354
|
+
});
|
|
355
|
+
// =============================================================================
|
|
356
|
+
// Stream Parsing
|
|
357
|
+
// =============================================================================
|
|
358
|
+
const metadata = (state, step) => ({
|
|
359
|
+
[state.metadataKey]: { interactionSignature: step.signature },
|
|
360
|
+
});
|
|
361
|
+
const mapUsage = (usage, key, continued) => {
|
|
362
|
+
if (!usage && continued.every((hop) => !hop))
|
|
363
|
+
return undefined;
|
|
364
|
+
const first = continued.length ? continued[0] : usage;
|
|
365
|
+
const hops = [...continued, usage];
|
|
366
|
+
const sum = (field) => ProviderShared.sumTokens(...hops.map((hop) => (hop ? (field(hop) ?? undefined) : undefined)));
|
|
367
|
+
const input = first?.total_input_tokens ?? undefined;
|
|
368
|
+
const cached = first?.total_cached_tokens ?? undefined;
|
|
369
|
+
const reasoning = sum((hop) => hop.total_thought_tokens);
|
|
370
|
+
const text = sum((hop) => hop.total_output_tokens);
|
|
371
|
+
const output = continued.length
|
|
372
|
+
? ProviderShared.sumTokens(text, reasoning)
|
|
373
|
+
: text === undefined
|
|
374
|
+
? undefined
|
|
375
|
+
: text + (reasoning ?? 0);
|
|
376
|
+
return new Usage({
|
|
377
|
+
contextTokens: first?.raw_prompt_token ?? input,
|
|
378
|
+
inputTokens: input,
|
|
379
|
+
outputTokens: output,
|
|
380
|
+
nonCachedInputTokens: ProviderShared.subtractTokens(input, cached),
|
|
381
|
+
cacheReadInputTokens: cached,
|
|
382
|
+
reasoningTokens: reasoning,
|
|
383
|
+
totalTokens: continued.length
|
|
384
|
+
? ProviderShared.totalTokens(input, output, undefined)
|
|
385
|
+
: (usage?.total_tokens ?? undefined),
|
|
386
|
+
// Keep re-billed input in provider metadata, separate from the logical call's input.
|
|
387
|
+
providerMetadata: {
|
|
388
|
+
[key]: continued.length
|
|
389
|
+
? {
|
|
390
|
+
total_input_tokens: sum((hop) => hop.total_input_tokens),
|
|
391
|
+
total_cached_tokens: sum((hop) => hop.total_cached_tokens),
|
|
392
|
+
total_output_tokens: text,
|
|
393
|
+
total_thought_tokens: reasoning,
|
|
394
|
+
total_tokens: sum((hop) => hop.total_tokens),
|
|
395
|
+
requests: hops.length,
|
|
396
|
+
}
|
|
397
|
+
: (usage ?? {}),
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
};
|
|
401
|
+
const blockID = (state, index) => (state.hop === 0 ? String(index) : `${state.hop}:${index}`);
|
|
402
|
+
const onStart = Effect.fnUntraced(function* (state, index, step) {
|
|
403
|
+
const events = [];
|
|
404
|
+
let lifecycle = Lifecycle.start(state.lifecycle);
|
|
405
|
+
let tools = state.tools;
|
|
406
|
+
const id = blockID(state, index);
|
|
407
|
+
if (step.type === "thought") {
|
|
408
|
+
lifecycle = Lifecycle.reasoningStart(lifecycle, events, id, metadata(state, step));
|
|
409
|
+
for (const part of step.summary ?? [])
|
|
410
|
+
lifecycle = Lifecycle.reasoningDelta(lifecycle, events, id, part.text);
|
|
411
|
+
}
|
|
412
|
+
else if (step.type === "model_output") {
|
|
413
|
+
lifecycle = Lifecycle.textStart(lifecycle, events, id);
|
|
414
|
+
for (const part of step.content ?? []) {
|
|
415
|
+
if (part.type !== "text")
|
|
416
|
+
return yield* ProviderShared.eventError(ADAPTER, `Unsupported Interactions output: ${part.type}`, encodeJson(step));
|
|
417
|
+
if (part.text)
|
|
418
|
+
lifecycle = Lifecycle.textDelta(lifecycle, events, id, part.text);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
else if (step.type === "function_call") {
|
|
422
|
+
if (!step.id || !step.name)
|
|
423
|
+
return yield* ProviderShared.eventError(ADAPTER, "Interactions function call lacks id or name", encodeJson(step));
|
|
424
|
+
tools = ToolStream.start(tools, index, {
|
|
425
|
+
id: step.id,
|
|
426
|
+
name: step.name,
|
|
427
|
+
providerMetadata: metadata(state, step),
|
|
428
|
+
input: step.arguments && Object.keys(step.arguments).length ? encodeJson(step.arguments) : "",
|
|
429
|
+
});
|
|
430
|
+
events.push(StreamEvent.toolInputStart({ id: step.id, name: step.name, providerMetadata: metadata(state, step) }));
|
|
431
|
+
}
|
|
432
|
+
else
|
|
433
|
+
return yield* ProviderShared.eventError(ADAPTER, `Unsupported Interactions step: ${step.type}`, encodeJson(step));
|
|
434
|
+
return [
|
|
435
|
+
{
|
|
436
|
+
...state,
|
|
437
|
+
lifecycle,
|
|
438
|
+
tools,
|
|
439
|
+
hasToolCalls: state.hasToolCalls || step.type === "function_call",
|
|
440
|
+
steps: { ...state.steps, [index]: step },
|
|
441
|
+
},
|
|
442
|
+
events,
|
|
443
|
+
];
|
|
444
|
+
});
|
|
445
|
+
const onDelta = Effect.fnUntraced(function* (state, index, delta) {
|
|
446
|
+
const step = state.steps[index];
|
|
447
|
+
if (!step)
|
|
448
|
+
return yield* ProviderShared.eventError(ADAPTER, "Interactions delta without step.start", encodeJson(delta));
|
|
449
|
+
const events = [];
|
|
450
|
+
if (delta.type === "text" && step.type === "model_output")
|
|
451
|
+
return [
|
|
452
|
+
{ ...state, lifecycle: Lifecycle.textDelta(state.lifecycle, events, blockID(state, index), delta.text) },
|
|
453
|
+
events,
|
|
454
|
+
];
|
|
455
|
+
if (delta.type === "text_annotation_delta" && step.type === "model_output")
|
|
456
|
+
return [state, events];
|
|
457
|
+
if (delta.type === "thought_summary" && step.type === "thought")
|
|
458
|
+
return [
|
|
459
|
+
{
|
|
460
|
+
...state,
|
|
461
|
+
lifecycle: Lifecycle.reasoningDelta(state.lifecycle, events, blockID(state, index), delta.content.text),
|
|
462
|
+
},
|
|
463
|
+
events,
|
|
464
|
+
];
|
|
465
|
+
if (delta.type === "thought_signature") {
|
|
466
|
+
const next = { ...step, signature: delta.signature };
|
|
467
|
+
const tool = state.tools[index];
|
|
468
|
+
return [
|
|
469
|
+
{
|
|
470
|
+
...state,
|
|
471
|
+
steps: { ...state.steps, [index]: next },
|
|
472
|
+
tools: tool ? { ...state.tools, [index]: { ...tool, providerMetadata: metadata(state, next) } } : state.tools,
|
|
473
|
+
},
|
|
474
|
+
events,
|
|
475
|
+
];
|
|
476
|
+
}
|
|
477
|
+
if (delta.type === "arguments_delta" && step.type === "function_call") {
|
|
478
|
+
const result = ToolStream.appendExisting(ADAPTER, state.tools, index, delta.arguments, "Interactions arguments without function call");
|
|
479
|
+
if (ToolStream.isError(result))
|
|
480
|
+
return yield* result;
|
|
481
|
+
return [{ ...state, tools: result.tools }, result.events];
|
|
482
|
+
}
|
|
483
|
+
return yield* ProviderShared.eventError(ADAPTER, `Unsupported Interactions delta: ${delta.type === "unknown" ? delta.rawType : delta.type}`, encodeJson(delta));
|
|
484
|
+
});
|
|
485
|
+
const onStop = Effect.fnUntraced(function* (state, index) {
|
|
486
|
+
const step = state.steps[index];
|
|
487
|
+
if (!step)
|
|
488
|
+
return yield* ProviderShared.eventError(ADAPTER, "Interactions step.stop without step.start");
|
|
489
|
+
const events = [];
|
|
490
|
+
if (step.type === "thought")
|
|
491
|
+
return [
|
|
492
|
+
{
|
|
493
|
+
...state,
|
|
494
|
+
lifecycle: Lifecycle.reasoningEnd(state.lifecycle, events, blockID(state, index), metadata(state, step)),
|
|
495
|
+
},
|
|
496
|
+
events,
|
|
497
|
+
];
|
|
498
|
+
if (step.type === "model_output")
|
|
499
|
+
return [
|
|
500
|
+
{ ...state, lifecycle: Lifecycle.textEnd(state.lifecycle, events, blockID(state, index)) },
|
|
501
|
+
events,
|
|
502
|
+
];
|
|
503
|
+
const result = yield* ToolStream.finish(ADAPTER, state.tools, index);
|
|
504
|
+
return [{ ...state, tools: result.tools }, result.events ?? []];
|
|
505
|
+
});
|
|
506
|
+
const RECOVERABLE_CODES = new Set([
|
|
507
|
+
"MALFORMED_FUNCTION_CALL",
|
|
508
|
+
"MALFORMED_TOOL_CALL",
|
|
509
|
+
"UNEXPECTED_TOOL_CALL",
|
|
510
|
+
"MALFORMED_RESPONSE",
|
|
511
|
+
]);
|
|
512
|
+
const INVALID_OUTPUT_CODES = new Set(["MISSING_THOUGHT_SIGNATURE", "TOO_MANY_TOOL_CALLS", "NO_IMAGE"]);
|
|
513
|
+
const failureReason = (route, code, message, data, rawBody) => {
|
|
514
|
+
const normalized = code?.trim().toUpperCase().replaceAll("-", "_");
|
|
515
|
+
if (normalized && RECOVERABLE_CODES.has(normalized))
|
|
516
|
+
return new InvalidProviderOutputError({ route, message, body: rawBody, classification: "incomplete-stream" });
|
|
517
|
+
if (normalized && INVALID_OUTPUT_CODES.has(normalized))
|
|
518
|
+
return new InvalidProviderOutputError({ route, message, body: rawBody });
|
|
519
|
+
if (normalized && (normalized === "CONTENT_BLOCKED" || GeminiGenerateContent.contentFiltered(normalized)))
|
|
520
|
+
return new ContentPolicyError({ message, body: rawBody });
|
|
521
|
+
return classifyProviderFailure({ message, data, rawBody });
|
|
522
|
+
};
|
|
523
|
+
// Marks a request boundary without adding another logical LLM step or finish.
|
|
524
|
+
class NextRequest {
|
|
525
|
+
}
|
|
526
|
+
const step = Effect.fnUntraced(function* (state, event) {
|
|
527
|
+
if (event instanceof NextRequest) {
|
|
528
|
+
const pending = yield* ToolStream.finishAll(ADAPTER, state.tools);
|
|
529
|
+
const events = [...pending.events];
|
|
530
|
+
let lifecycle = state.lifecycle;
|
|
531
|
+
for (const [index, step] of Object.entries(state.steps)) {
|
|
532
|
+
const id = blockID(state, Number(index));
|
|
533
|
+
if (step?.type === "thought")
|
|
534
|
+
lifecycle = Lifecycle.reasoningEnd(lifecycle, events, id, metadata(state, step));
|
|
535
|
+
if (step?.type === "model_output")
|
|
536
|
+
lifecycle = Lifecycle.textEnd(lifecycle, events, id);
|
|
537
|
+
}
|
|
538
|
+
return [
|
|
539
|
+
{
|
|
540
|
+
...state,
|
|
541
|
+
hop: state.hop + 1,
|
|
542
|
+
lifecycle,
|
|
543
|
+
steps: {},
|
|
544
|
+
tools: ToolStream.empty(),
|
|
545
|
+
usage: undefined,
|
|
546
|
+
continuedUsage: [...state.continuedUsage, state.usage],
|
|
547
|
+
},
|
|
548
|
+
events,
|
|
549
|
+
];
|
|
550
|
+
}
|
|
551
|
+
switch (event.event_type) {
|
|
552
|
+
case "step.start":
|
|
553
|
+
return yield* onStart(state, event.index, event.step);
|
|
554
|
+
case "step.delta":
|
|
555
|
+
return yield* onDelta(state, event.index, event.delta);
|
|
556
|
+
case "step.stop":
|
|
557
|
+
return yield* onStop(state, event.index);
|
|
558
|
+
case "interaction.status_update":
|
|
559
|
+
return [{ ...state, usage: event.usage ?? state.usage }, []];
|
|
560
|
+
case "interaction.created":
|
|
561
|
+
case "unknown":
|
|
562
|
+
return [state, []];
|
|
563
|
+
case "error": {
|
|
564
|
+
const rawBody = encodeJson(event);
|
|
565
|
+
const detail = Option.getOrUndefined(Schema.decodeUnknownOption(ErrorDetail)(event.error));
|
|
566
|
+
return yield* new AIError({
|
|
567
|
+
reason: failureReason(state.route, detail?.code ?? detail?.error_type, detail?.message?.trim() || providerErrorMessage(rawBody) || "Google Interactions stream error", event.error, rawBody),
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
case "interaction.completed": {
|
|
571
|
+
const interaction = event.interaction;
|
|
572
|
+
if (interaction.status === CONTINUATION)
|
|
573
|
+
return [{ ...state, usage: interaction.usage ?? state.usage }, []];
|
|
574
|
+
if (interaction.status === "failed" || interaction.status === "cancelled") {
|
|
575
|
+
const messages = (interaction.errors ?? []).flatMap((error) => (error.message?.trim() ? [error.message] : []));
|
|
576
|
+
const coded = interaction.errors?.find((error) => error.code);
|
|
577
|
+
return yield* new AIError({
|
|
578
|
+
reason: failureReason(state.route, coded?.code, messages.length > 0 ? messages.join("\n") : `Google Interactions ${interaction.status}`,
|
|
579
|
+
// The first coded error decides the classification; every message stays visible.
|
|
580
|
+
coded ?? interaction, encodeJson(event)),
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
if (!["completed", "requires_action", "incomplete"].includes(interaction.status))
|
|
584
|
+
return yield* ProviderShared.eventError(ADAPTER, `Unexpected terminal Interactions status: ${interaction.status}`, encodeJson(event));
|
|
585
|
+
const pending = yield* ToolStream.finishAll(ADAPTER, state.tools);
|
|
586
|
+
const events = [...pending.events];
|
|
587
|
+
const lifecycle = Lifecycle.finish(state.lifecycle, events, {
|
|
588
|
+
reason: interaction.status === "requires_action" || (interaction.status === "completed" && state.hasToolCalls)
|
|
589
|
+
? "tool-calls"
|
|
590
|
+
: interaction.status === "incomplete"
|
|
591
|
+
? "length"
|
|
592
|
+
: "stop",
|
|
593
|
+
raw: interaction.status,
|
|
594
|
+
usage: mapUsage(interaction.usage ?? state.usage, state.metadataKey, state.continuedUsage),
|
|
595
|
+
providerMetadata: { [state.metadataKey]: { interactionId: interaction.id } },
|
|
596
|
+
});
|
|
597
|
+
return [{ ...state, lifecycle, tools: pending.tools }, events];
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
const sse = HttpTransport.sseJson.with();
|
|
602
|
+
const WireEvent = Protocol.jsonEvent(Event);
|
|
603
|
+
const decodeEvent = Schema.decodeUnknownEffect(WireEvent);
|
|
604
|
+
export const transport = {
|
|
605
|
+
id: sse.id,
|
|
606
|
+
prepare: (input) => sse.prepare(input).pipe(Effect.map((http) => ({ http, input }))),
|
|
607
|
+
execute: (prepared, request, runtime) => Effect.gen(function* () {
|
|
608
|
+
const label = `${request.model.provider}/${request.model.route.id}`;
|
|
609
|
+
const raw = new WeakMap();
|
|
610
|
+
const respond = (execution, resume) => Stream.suspend(() => {
|
|
611
|
+
let token = resume?.token;
|
|
612
|
+
let owner;
|
|
613
|
+
let status;
|
|
614
|
+
let contentSinceToken = false;
|
|
615
|
+
let terminal = false;
|
|
616
|
+
let dropped;
|
|
617
|
+
const pendingTools = new Set();
|
|
618
|
+
return execution.frames.pipe(Stream.mapEffect((frame) => decodeEvent(frame).pipe(Effect.mapError((cause) => ProviderShared.eventError(ADAPTER, `Invalid ${label} stream event`, frame, cause)), Effect.map((event) => {
|
|
619
|
+
raw.set(event, frame);
|
|
620
|
+
switch (event.event_type) {
|
|
621
|
+
case "interaction.created":
|
|
622
|
+
owner = event.interaction.id;
|
|
623
|
+
break;
|
|
624
|
+
case "interaction.status_update":
|
|
625
|
+
status = event.status;
|
|
626
|
+
owner = event.interaction_id ?? owner;
|
|
627
|
+
if (event.continuation_token && event.continuation_token !== token) {
|
|
628
|
+
token = event.continuation_token;
|
|
629
|
+
contentSinceToken = false;
|
|
630
|
+
}
|
|
631
|
+
break;
|
|
632
|
+
case "interaction.completed":
|
|
633
|
+
terminal = true;
|
|
634
|
+
status = event.interaction.status;
|
|
635
|
+
owner = event.interaction.id ?? owner;
|
|
636
|
+
if (event.interaction.continuation_token) {
|
|
637
|
+
token = event.interaction.continuation_token;
|
|
638
|
+
contentSinceToken = false;
|
|
639
|
+
}
|
|
640
|
+
break;
|
|
641
|
+
case "step.start":
|
|
642
|
+
if (event.step.type === "function_call")
|
|
643
|
+
pendingTools.add(event.index);
|
|
644
|
+
if (pendingTools.has(event.index) ||
|
|
645
|
+
event.step.content?.length ||
|
|
646
|
+
event.step.summary?.length ||
|
|
647
|
+
event.step.signature)
|
|
648
|
+
contentSinceToken = true;
|
|
649
|
+
break;
|
|
650
|
+
case "step.delta":
|
|
651
|
+
contentSinceToken = true;
|
|
652
|
+
break;
|
|
653
|
+
case "step.stop":
|
|
654
|
+
if (pendingTools.delete(event.index))
|
|
655
|
+
contentSinceToken = true;
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
return event;
|
|
659
|
+
}))), Stream.takeUntil((event) => event.event_type === "interaction.completed"), Stream.catchIf((error) => error.reason._tag === "Transport" && error.reason.code !== "Timeout", (error) => {
|
|
660
|
+
dropped = error;
|
|
661
|
+
return Stream.empty;
|
|
662
|
+
}), Stream.concat(Stream.suspend(() => {
|
|
663
|
+
const needsContinuation = status === CONTINUATION;
|
|
664
|
+
const resumable = needsContinuation || (!terminal && (status === undefined || status === "in_progress"));
|
|
665
|
+
const newToken = token !== resume?.token;
|
|
666
|
+
// An interrupted tool call may carry arguments across the checkpoint. Don't finalize it on EOF.
|
|
667
|
+
if (token &&
|
|
668
|
+
resumable &&
|
|
669
|
+
!contentSinceToken &&
|
|
670
|
+
(terminal || pendingTools.size === 0) &&
|
|
671
|
+
(!needsContinuation || newToken)) {
|
|
672
|
+
const reconnectsWithoutProgress = newToken ? 0 : (resume?.reconnectsWithoutProgress ?? 0) + 1;
|
|
673
|
+
const lastError = dropped ?? (newToken ? undefined : resume?.lastError);
|
|
674
|
+
if (reconnectsWithoutProgress > MAX_STREAM_RECONNECTS_WITHOUT_PROGRESS) {
|
|
675
|
+
if (!lastError)
|
|
676
|
+
return Stream.empty;
|
|
677
|
+
return Stream.fail(new AIError({
|
|
678
|
+
reason: new InvalidProviderOutputError({
|
|
679
|
+
route: label,
|
|
680
|
+
message: lastError.reason.message,
|
|
681
|
+
classification: "incomplete-stream",
|
|
682
|
+
cause: lastError,
|
|
683
|
+
http: lastError.reason.http,
|
|
684
|
+
}),
|
|
685
|
+
}));
|
|
686
|
+
}
|
|
687
|
+
return Stream.concat(Stream.succeed(new NextRequest()), next({ token, owner: newToken ? owner : resume?.owner, reconnectsWithoutProgress, lastError }));
|
|
688
|
+
}
|
|
689
|
+
if (dropped)
|
|
690
|
+
return Stream.fail(dropped);
|
|
691
|
+
if (terminal && needsContinuation && !token)
|
|
692
|
+
return Stream.fail(ProviderShared.eventError(label, "Google Interactions continued without a token"));
|
|
693
|
+
// No safe checkpoint. Leave EOF for the route's incomplete-stream check.
|
|
694
|
+
return Stream.empty;
|
|
695
|
+
})));
|
|
696
|
+
});
|
|
697
|
+
const next = (resume) => {
|
|
698
|
+
const body = prepared.input.body;
|
|
699
|
+
return Stream.unwrap(sse
|
|
700
|
+
.prepare({
|
|
701
|
+
...prepared.input,
|
|
702
|
+
body: {
|
|
703
|
+
...body,
|
|
704
|
+
continuation_token: resume.token,
|
|
705
|
+
...(body.store && resume.owner ? { input: undefined, previous_interaction_id: resume.owner } : {}),
|
|
706
|
+
},
|
|
707
|
+
})
|
|
708
|
+
.pipe(Effect.flatMap((continued) => sse.execute(continued, request, runtime)), Effect.map((execution) => respond(execution, resume))));
|
|
709
|
+
};
|
|
710
|
+
const first = yield* sse.execute(prepared.http, request, runtime);
|
|
711
|
+
return {
|
|
712
|
+
frames: respond(first),
|
|
713
|
+
http: first.http,
|
|
714
|
+
body: (frame) => raw.get(frame),
|
|
715
|
+
raw: (frame) => {
|
|
716
|
+
const body = raw.get(frame);
|
|
717
|
+
return body === undefined ? undefined : ProviderShared.decodeJson(body);
|
|
718
|
+
},
|
|
719
|
+
};
|
|
720
|
+
}),
|
|
721
|
+
};
|
|
722
|
+
// =============================================================================
|
|
723
|
+
// Protocol And Route
|
|
724
|
+
// =============================================================================
|
|
725
|
+
export const protocol = Protocol.make({
|
|
726
|
+
id: ADAPTER,
|
|
727
|
+
sanitizer: "gemini",
|
|
728
|
+
body: { schema: Body, from: fromRequest },
|
|
729
|
+
stream: {
|
|
730
|
+
event: Schema.Union([Schema.instanceOf(NextRequest), Schema.toType(Event), WireEvent]),
|
|
731
|
+
initial: (request) => ({
|
|
732
|
+
route: `${request.model.provider}/${request.model.route.id}`,
|
|
733
|
+
metadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
734
|
+
lifecycle: Lifecycle.initial(),
|
|
735
|
+
hop: 0,
|
|
736
|
+
hasToolCalls: false,
|
|
737
|
+
continuedUsage: [],
|
|
738
|
+
steps: {},
|
|
739
|
+
tools: ToolStream.empty(),
|
|
740
|
+
}),
|
|
741
|
+
step,
|
|
742
|
+
terminal: (event) => !(event instanceof NextRequest) &&
|
|
743
|
+
event.event_type === "interaction.completed" &&
|
|
744
|
+
event.interaction.status !== CONTINUATION,
|
|
745
|
+
},
|
|
746
|
+
});
|
|
747
|
+
export const route = Route.make({
|
|
748
|
+
id: ADAPTER,
|
|
749
|
+
provider: "google",
|
|
750
|
+
providerMetadataKey: "google",
|
|
751
|
+
protocol,
|
|
752
|
+
endpoint: Endpoint.path("/interactions", { baseURL: DEFAULT_BASE_URL }),
|
|
753
|
+
auth: Auth.none,
|
|
754
|
+
transport,
|
|
755
|
+
});
|
|
756
|
+
export * as GoogleInteractions from "./google-interactions.js";
|