@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,229 @@
|
|
|
1
|
+
import { Effect, Schema, Stream } from "effect";
|
|
2
|
+
import { Auth } from "./auth.js";
|
|
3
|
+
import { Endpoint, type EndpointPatch } from "./endpoint.js";
|
|
4
|
+
import { RequestExecutor } from "./executor.js";
|
|
5
|
+
import { Framing } from "./framing.js";
|
|
6
|
+
import { HttpTransport } from "./transport/index.js";
|
|
7
|
+
import type { HttpMiddleware, Transport, TransportRuntime, WebSocketChannelExecutor } from "./transport/index.js";
|
|
8
|
+
import type { Protocol } from "./protocol.js";
|
|
9
|
+
import { type ToolUpdateEncoding } from "../tool-updates.js";
|
|
10
|
+
import type { LanguageModelSanitizerCompatibility, ProtocolID, ProviderOptions } from "../schema/index.js";
|
|
11
|
+
import type { LLMRequestInput } from "../llm.js";
|
|
12
|
+
import { AIError, CompactionResponse, CompactionCheckpointResponse, type TextResponse, LanguageModel, StreamEvent, ProviderID, type LLMRequest, type RequestOptions } from "../schema/index.js";
|
|
13
|
+
export interface RouteBody<Body> {
|
|
14
|
+
/** Schema for the validated provider-native body sent as the JSON request. */
|
|
15
|
+
readonly schema: Schema.Codec<Body, unknown>;
|
|
16
|
+
/** Build the provider-native body from a common `LLMRequest`. */
|
|
17
|
+
readonly from: (request: LLMRequest) => Effect.Effect<Body, AIError>;
|
|
18
|
+
}
|
|
19
|
+
export interface Route<Body, Prepared = unknown, Compact extends CompactionOperations | undefined = CompactionOperations | undefined> {
|
|
20
|
+
readonly compact: Compact;
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly provider?: ProviderID;
|
|
23
|
+
/** ProviderMetadata namespace emitted and consumed by this route. */
|
|
24
|
+
readonly providerMetadataKey?: string;
|
|
25
|
+
readonly protocol: ProtocolID;
|
|
26
|
+
readonly endpoint: Endpoint.Definition<Body>;
|
|
27
|
+
readonly auth: Auth.Definition;
|
|
28
|
+
/** Deployment headers resolved once for every operation, before transport authentication. */
|
|
29
|
+
readonly headers?: (input: {
|
|
30
|
+
readonly request: LLMRequest;
|
|
31
|
+
}) => Record<string, string>;
|
|
32
|
+
readonly transport: Transport<Body, Prepared, unknown>;
|
|
33
|
+
/** Options every model built on this route starts from. */
|
|
34
|
+
readonly options?: RequestOptions;
|
|
35
|
+
readonly body: RouteBody<Body>;
|
|
36
|
+
readonly supportsEffortUpdates?: (request: LLMRequest) => boolean;
|
|
37
|
+
readonly toolUpdates?: ToolUpdateEncoding;
|
|
38
|
+
readonly sanitizer?: LanguageModelSanitizerCompatibility;
|
|
39
|
+
readonly with: {
|
|
40
|
+
<Next extends CompactionOperations | undefined>(patch: RoutePatch<Body, Prepared> & {
|
|
41
|
+
readonly compact: Next;
|
|
42
|
+
}): Route<Body, Prepared, Next>;
|
|
43
|
+
(patch: Omit<RoutePatch<Body, Prepared>, "compact"> & {
|
|
44
|
+
readonly compact?: undefined;
|
|
45
|
+
}): Route<Body, Prepared, Compact>;
|
|
46
|
+
(patch: RoutePatch<Body, Prepared>): Route<Body, Prepared>;
|
|
47
|
+
};
|
|
48
|
+
readonly model: <Options extends ProviderOptions = ProviderOptions>(input: RouteMappedLanguageModelInput) => LanguageModel<Options, Compact>;
|
|
49
|
+
readonly prepareTransport: (body: Body, request: LLMRequest, options?: StreamOptions) => Effect.Effect<Prepared, AIError>;
|
|
50
|
+
readonly streamPrepared: (prepared: Prepared, request: LLMRequest, runtime: TransportRuntime, options?: StreamOptions) => Stream.Stream<StreamEvent, AIError>;
|
|
51
|
+
}
|
|
52
|
+
export type AnyRoute<Compact extends CompactionOperations | undefined = CompactionOperations | undefined> = Route<any, any, Compact>;
|
|
53
|
+
export type RouteLanguageModelInput = Omit<LanguageModel.Input, "provider" | "route">;
|
|
54
|
+
export type RouteRoutedLanguageModelInput = Omit<LanguageModel.Input, "route">;
|
|
55
|
+
export interface RoutePatch<Body, Prepared> extends RequestOptions {
|
|
56
|
+
readonly compact?: CompactionOperations;
|
|
57
|
+
readonly id?: string;
|
|
58
|
+
readonly provider?: string | ProviderID;
|
|
59
|
+
readonly providerMetadataKey?: string;
|
|
60
|
+
readonly auth?: Auth.Definition;
|
|
61
|
+
readonly transport?: Transport<Body, Prepared, unknown>;
|
|
62
|
+
readonly endpoint?: EndpointPatch<Body>;
|
|
63
|
+
}
|
|
64
|
+
type RouteMappedLanguageModelInput = RouteLanguageModelInput | RouteRoutedLanguageModelInput;
|
|
65
|
+
/** Text operations over a request executor; the `AI` service exposes them beside its media groups. */
|
|
66
|
+
export interface TextOperations {
|
|
67
|
+
readonly compact: CompactMethod;
|
|
68
|
+
readonly stream: StreamMethod;
|
|
69
|
+
readonly generate: GenerateMethod;
|
|
70
|
+
}
|
|
71
|
+
export interface StreamOptions {
|
|
72
|
+
readonly http?: HttpMiddleware;
|
|
73
|
+
readonly webSocket?: WebSocketChannelExecutor;
|
|
74
|
+
}
|
|
75
|
+
export interface StreamMethod {
|
|
76
|
+
(request: LLMRequest, options?: StreamOptions): Stream.Stream<StreamEvent, AIError>;
|
|
77
|
+
}
|
|
78
|
+
export interface GenerateMethod {
|
|
79
|
+
(request: LLMRequest, options?: StreamOptions): Effect.Effect<TextResponse, AIError>;
|
|
80
|
+
}
|
|
81
|
+
export type CompactOperation = (request: LLMRequest, executor: RequestExecutor.Interface, options?: Pick<StreamOptions, "http">) => Effect.Effect<CompactionResponse, AIError>;
|
|
82
|
+
export type TriggerCompactOperation = (request: LLMRequest, executor: RequestExecutor.Interface, options: TriggerCompactOptions) => Effect.Effect<CompactionCheckpointResponse, AIError>;
|
|
83
|
+
/** Protocol capabilities, not deployment/model eligibility. */
|
|
84
|
+
export interface CompactionOperations {
|
|
85
|
+
readonly endpoint?: CompactOperation;
|
|
86
|
+
readonly trigger?: TriggerCompactOperation;
|
|
87
|
+
}
|
|
88
|
+
export interface EndpointCompactOptions extends Pick<StreamOptions, "http"> {
|
|
89
|
+
readonly mechanism?: "endpoint";
|
|
90
|
+
readonly webSocket?: never;
|
|
91
|
+
}
|
|
92
|
+
export interface TriggerCompactOptions extends StreamOptions {
|
|
93
|
+
readonly mechanism: "trigger";
|
|
94
|
+
}
|
|
95
|
+
type EndpointModel = LanguageModel<ProviderOptions, {
|
|
96
|
+
readonly endpoint: CompactOperation;
|
|
97
|
+
}>;
|
|
98
|
+
type TriggerModel = LanguageModel<ProviderOptions, {
|
|
99
|
+
readonly trigger: TriggerCompactOperation;
|
|
100
|
+
}>;
|
|
101
|
+
/** A text request whose model supports explicit compaction. */
|
|
102
|
+
export type CompactionRequest = LLMRequestInput & {
|
|
103
|
+
readonly model: EndpointModel;
|
|
104
|
+
};
|
|
105
|
+
/** A text request whose model supports trigger compaction checkpoints. */
|
|
106
|
+
export type CheckpointRequest = LLMRequestInput & {
|
|
107
|
+
readonly model: TriggerModel;
|
|
108
|
+
};
|
|
109
|
+
export interface CompactMethod<R = never> {
|
|
110
|
+
(request: LLMRequest & {
|
|
111
|
+
readonly model: TriggerModel;
|
|
112
|
+
}, options: TriggerCompactOptions): Effect.Effect<CompactionCheckpointResponse, AIError, R>;
|
|
113
|
+
(request: LLMRequest & {
|
|
114
|
+
readonly model: EndpointModel;
|
|
115
|
+
}, options?: EndpointCompactOptions): Effect.Effect<CompactionResponse, AIError, R>;
|
|
116
|
+
}
|
|
117
|
+
/** Whether a model, or a text request's model, supports compaction through the given mechanism. */
|
|
118
|
+
export declare function canCompact<Model extends LanguageModel>(model: Model, options: {
|
|
119
|
+
readonly mechanism: "trigger";
|
|
120
|
+
}): model is Model & TriggerModel;
|
|
121
|
+
export declare function canCompact<Request extends {
|
|
122
|
+
readonly model: LanguageModel;
|
|
123
|
+
}>(request: Request, options: {
|
|
124
|
+
readonly mechanism: "trigger";
|
|
125
|
+
}): request is Request & {
|
|
126
|
+
readonly model: TriggerModel;
|
|
127
|
+
};
|
|
128
|
+
export declare function canCompact<Model extends LanguageModel>(model: Model, options?: {
|
|
129
|
+
readonly mechanism?: "endpoint";
|
|
130
|
+
}): model is Model & EndpointModel;
|
|
131
|
+
export declare function canCompact<Request extends {
|
|
132
|
+
readonly model: LanguageModel;
|
|
133
|
+
}>(request: Request, options?: {
|
|
134
|
+
readonly mechanism?: "endpoint";
|
|
135
|
+
}): request is Request & {
|
|
136
|
+
readonly model: EndpointModel;
|
|
137
|
+
};
|
|
138
|
+
export declare const unsupportedCompaction: (model: LanguageModel, mechanism: string | undefined) => AIError;
|
|
139
|
+
export interface MakeInput<Body, Frame, Event, State> {
|
|
140
|
+
readonly compact?: CompactionOperations;
|
|
141
|
+
/** Route id used in diagnostics and prepared request metadata. */
|
|
142
|
+
readonly id: string;
|
|
143
|
+
/** Provider identity for route-owned model construction. */
|
|
144
|
+
readonly provider?: string | ProviderID;
|
|
145
|
+
/** ProviderMetadata namespace emitted and consumed by this route. */
|
|
146
|
+
readonly providerMetadataKey?: string;
|
|
147
|
+
/** Semantic API contract — owns body construction, body schema, and parsing. */
|
|
148
|
+
readonly protocol: Protocol<Body, Frame, Event, State>;
|
|
149
|
+
/** Where the request is sent. */
|
|
150
|
+
readonly endpoint: Endpoint.Definition<Body>;
|
|
151
|
+
/** Per-request transport auth. Provider facades override this via `route.with(...)`. */
|
|
152
|
+
readonly auth?: Auth.Definition;
|
|
153
|
+
/** Stream framing — bytes -> frames before `protocol.stream.event` decoding. */
|
|
154
|
+
readonly framing: Framing.Definition<Frame>;
|
|
155
|
+
/** Static / per-request headers added before `auth` runs. */
|
|
156
|
+
readonly headers?: (input: {
|
|
157
|
+
readonly request: LLMRequest;
|
|
158
|
+
}) => Record<string, string>;
|
|
159
|
+
/** Options every model built on this route starts from. */
|
|
160
|
+
readonly options?: RequestOptions;
|
|
161
|
+
}
|
|
162
|
+
export interface MakeTransportInput<Body, Prepared, Frame, Event, State> {
|
|
163
|
+
readonly compact?: CompactionOperations;
|
|
164
|
+
/** Route id used in diagnostics and prepared request metadata. */
|
|
165
|
+
readonly id: string;
|
|
166
|
+
/** Provider identity for route-owned model construction. */
|
|
167
|
+
readonly provider?: string | ProviderID;
|
|
168
|
+
/** ProviderMetadata namespace emitted and consumed by this route. */
|
|
169
|
+
readonly providerMetadataKey?: string;
|
|
170
|
+
/** Semantic API contract — owns body construction, body schema, and parsing. */
|
|
171
|
+
readonly protocol: Protocol<Body, Frame, Event, State>;
|
|
172
|
+
/** Where the request is sent. */
|
|
173
|
+
readonly endpoint: Endpoint.Definition<Body>;
|
|
174
|
+
/** Per-request transport auth. Provider facades override this via `route.with(...)`. */
|
|
175
|
+
readonly auth?: Auth.Definition;
|
|
176
|
+
/** Static / per-request headers added before `auth` runs. */
|
|
177
|
+
readonly headers?: (input: {
|
|
178
|
+
readonly request: LLMRequest;
|
|
179
|
+
}) => Record<string, string>;
|
|
180
|
+
/** Runnable transport route. */
|
|
181
|
+
readonly transport: Transport<Body, Prepared, Frame>;
|
|
182
|
+
/** Options every model built on this route starts from. */
|
|
183
|
+
readonly options?: RequestOptions;
|
|
184
|
+
}
|
|
185
|
+
export declare function make<Body, Prepared, Frame, Event, State, Compact extends CompactionOperations>(input: MakeTransportInput<Body, Prepared, Frame, Event, State> & {
|
|
186
|
+
readonly compact: Compact;
|
|
187
|
+
}): Route<Body, Prepared, Compact>;
|
|
188
|
+
export declare function make<Body, Frame, Event, State, Compact extends CompactionOperations>(input: MakeInput<Body, Frame, Event, State> & {
|
|
189
|
+
readonly compact: Compact;
|
|
190
|
+
}): Route<Body, HttpTransport.HttpPrepared<Frame>, Compact>;
|
|
191
|
+
export declare function make<Body, Prepared, Frame, Event, State>(input: MakeTransportInput<Body, Prepared, Frame, Event, State>): Route<Body, Prepared>;
|
|
192
|
+
/**
|
|
193
|
+
* Build a `Route` by composing the four orthogonal pieces of a deployment:
|
|
194
|
+
*
|
|
195
|
+
* - `Protocol` — what is the API I'm speaking?
|
|
196
|
+
* - `Endpoint` — where do I send the request?
|
|
197
|
+
* - `Auth` — how do I authenticate it?
|
|
198
|
+
* - `Framing` — how do I cut the response stream into protocol frames?
|
|
199
|
+
*
|
|
200
|
+
* Plus optional `headers` for cross-cutting deployment concerns (provider
|
|
201
|
+
* version pins, per-deployment quirks).
|
|
202
|
+
*
|
|
203
|
+
* This is the canonical route constructor. If a new route does not fit
|
|
204
|
+
* this four-axis model, add a purpose-built constructor rather than widening
|
|
205
|
+
* the public surface preemptively.
|
|
206
|
+
*/
|
|
207
|
+
export declare function make<Body, Frame, Event, State>(input: MakeInput<Body, Frame, Event, State>): Route<Body, HttpTransport.HttpPrepared<Frame>>;
|
|
208
|
+
/** Test-only projection of the execution compiler; exported from `@radheim/ai/testing`. */
|
|
209
|
+
export declare const compileRequest: (request: LLMRequest<LanguageModel<{
|
|
210
|
+
readonly [x: string]: unknown;
|
|
211
|
+
}, CompactionOperations | undefined>>) => Effect.Effect<{
|
|
212
|
+
route: string;
|
|
213
|
+
protocol: string;
|
|
214
|
+
model: LanguageModel<{
|
|
215
|
+
readonly [x: string]: unknown;
|
|
216
|
+
}, CompactionOperations | undefined>;
|
|
217
|
+
body: any;
|
|
218
|
+
metadata: {
|
|
219
|
+
transport: string;
|
|
220
|
+
};
|
|
221
|
+
}, AIError, never>;
|
|
222
|
+
export declare const generateWith: (stream: TextOperations["stream"]) => (request: LLMRequest<LanguageModel<{
|
|
223
|
+
readonly [x: string]: unknown;
|
|
224
|
+
}, CompactionOperations | undefined>>, options?: StreamOptions | undefined) => Effect.Effect<TextResponse, AIError, never>;
|
|
225
|
+
export declare const textOperations: (executor: RequestExecutor.Interface) => TextOperations;
|
|
226
|
+
export declare const Route: {
|
|
227
|
+
readonly make: typeof make;
|
|
228
|
+
};
|
|
229
|
+
export {};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { Cause, Effect, Option, Schema, Stream } from "effect";
|
|
2
|
+
import { Auth } from "./auth.js";
|
|
3
|
+
import { Endpoint } from "./endpoint.js";
|
|
4
|
+
import { RequestExecutor } from "./executor.js";
|
|
5
|
+
import { Framing } from "./framing.js";
|
|
6
|
+
import { HttpTransport } from "./transport/index.js";
|
|
7
|
+
import { applyCachePolicy } from "../cache-policy.js";
|
|
8
|
+
import { applyEffortUpdates } from "../effort-updates.js";
|
|
9
|
+
import { mapToolUpdates, planToolUpdates } from "../tool-updates.js";
|
|
10
|
+
import { normalizeToolHistory } from "../tool-history.js";
|
|
11
|
+
import { sanitizeSurrogates } from "../utils/sanitize.js";
|
|
12
|
+
import * as ProviderShared from "../protocols/shared.js";
|
|
13
|
+
import { ToolSchemaProjection } from "../protocols/utils/tool-schema.js";
|
|
14
|
+
import { AIError, CompactionResponse, CompactionCheckpointResponse, AIErrorReason, ResponseFold, LanguageModel, Message, StreamEvent, InvalidProviderOutputError, ProviderConfigurationError, ProviderID, ToolEntry, mergeGeneration, mergeRequestOptions, mergeProviderOptions, mergeTransport, withPartial, } from "../schema/index.js";
|
|
15
|
+
const makeRouteLanguageModel = (route, mapped) => {
|
|
16
|
+
const provider = route.provider ?? ("provider" in mapped ? mapped.provider : undefined);
|
|
17
|
+
if (!provider)
|
|
18
|
+
throw new Error(`Route.model(${route.id}) requires a provider`);
|
|
19
|
+
if (!route.endpoint.baseURL)
|
|
20
|
+
throw new ProviderConfigurationError({
|
|
21
|
+
provider: ProviderID.make(provider),
|
|
22
|
+
message: `Route.model(${route.id}) requires an endpoint baseURL — configure it on the route first`,
|
|
23
|
+
});
|
|
24
|
+
return LanguageModel.make({
|
|
25
|
+
...mapped,
|
|
26
|
+
provider,
|
|
27
|
+
route,
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
export function canCompact(target, options) {
|
|
31
|
+
const model = target instanceof LanguageModel ? target : target.model;
|
|
32
|
+
if (options?.mechanism === "trigger")
|
|
33
|
+
return model.route.compact?.trigger !== undefined;
|
|
34
|
+
if (options?.mechanism !== undefined && options.mechanism !== "endpoint")
|
|
35
|
+
return false;
|
|
36
|
+
return model.route.compact?.endpoint !== undefined;
|
|
37
|
+
}
|
|
38
|
+
export const unsupportedCompaction = (model, mechanism) => {
|
|
39
|
+
if (mechanism !== undefined && mechanism !== "endpoint" && mechanism !== "trigger")
|
|
40
|
+
return ProviderShared.invalidRequest(`Unknown compaction mechanism: ${mechanism}`);
|
|
41
|
+
return ProviderShared.unsupportedOperation({
|
|
42
|
+
operation: mechanism === "trigger" ? "compact.trigger" : "compact",
|
|
43
|
+
provider: model.provider,
|
|
44
|
+
route: model.route.id,
|
|
45
|
+
message: `${model.provider}/${model.route.id} does not support ${mechanism === "trigger" ? "trigger" : "explicit"} compaction`,
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
const resolveRequestOptions = (request) => {
|
|
49
|
+
const messages = normalizeToolHistory(request.messages);
|
|
50
|
+
const normalized = messages === request.messages ? request : { ...request, messages };
|
|
51
|
+
const options = normalized.model.options;
|
|
52
|
+
const http = mergeTransport(options, normalized).http;
|
|
53
|
+
return {
|
|
54
|
+
...normalized,
|
|
55
|
+
...mergeGeneration(options, normalized),
|
|
56
|
+
providerOptions: mergeProviderOptions(options?.providerOptions, normalized.providerOptions),
|
|
57
|
+
// Only a request carries `fetch`; the merge keeps the settings models and routes share.
|
|
58
|
+
http: normalized.http?.fetch === undefined ? http : { ...http, fetch: normalized.http.fetch },
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
const streamError = (route, message, cause) => {
|
|
62
|
+
const failed = cause.reasons.find(Cause.isFailReason)?.error;
|
|
63
|
+
if (failed instanceof AIError)
|
|
64
|
+
return failed;
|
|
65
|
+
return ProviderShared.eventError(route, message, undefined, cause);
|
|
66
|
+
};
|
|
67
|
+
const incompleteStreamError = (route) => new AIError({
|
|
68
|
+
reason: new InvalidProviderOutputError({
|
|
69
|
+
message: "The provider response ended unexpectedly.",
|
|
70
|
+
classification: "incomplete-stream",
|
|
71
|
+
route,
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
const requireTerminalEvent = (route) => (events) => Stream.suspend(() => {
|
|
75
|
+
let terminal = false;
|
|
76
|
+
return events.pipe(Stream.mapEffect((event) => {
|
|
77
|
+
// Raw events carry provider frames that may trail the terminal event.
|
|
78
|
+
if (terminal && event.type !== "raw")
|
|
79
|
+
return Effect.fail(ProviderShared.eventError(route, `Provider emitted ${event.type} after the terminal event`));
|
|
80
|
+
if (event.type === "finish")
|
|
81
|
+
terminal = true;
|
|
82
|
+
return Effect.succeed(event);
|
|
83
|
+
}), Stream.onEnd(Effect.suspend(() => (terminal ? Effect.void : Effect.fail(incompleteStreamError(route))))));
|
|
84
|
+
});
|
|
85
|
+
const decodeJsonOption = Schema.decodeUnknownOption(ProviderShared.Json);
|
|
86
|
+
// JSON text frames are parsed; non-JSON sentinels such as `[DONE]` stay strings.
|
|
87
|
+
const rawFrame = (frame) => typeof frame === "string" ? Option.getOrElse(decodeJsonOption(frame), () => frame) : frame;
|
|
88
|
+
// Transports may interleave their own marker objects with provider frames, e.g. Gemini's continuation boundary.
|
|
89
|
+
const isProviderFrame = (frame) => typeof frame !== "object" ||
|
|
90
|
+
frame === null ||
|
|
91
|
+
Array.isArray(frame) ||
|
|
92
|
+
Object.getPrototypeOf(frame) === Object.prototype;
|
|
93
|
+
function makeFromTransport(input) {
|
|
94
|
+
const protocol = input.protocol;
|
|
95
|
+
const decodeEventEffect = Schema.decodeUnknownEffect(protocol.stream.event);
|
|
96
|
+
const decodeEvent = (route) => (frame) => decodeEventEffect(frame).pipe(Effect.mapError((cause) => ProviderShared.eventError(input.id, `Invalid ${route} stream event`, typeof frame === "string" ? frame : ProviderShared.encodeJson(frame), cause)));
|
|
97
|
+
const build = (routeInput) => {
|
|
98
|
+
const route = {
|
|
99
|
+
compact: routeInput.compact,
|
|
100
|
+
id: routeInput.id,
|
|
101
|
+
provider: routeInput.provider === undefined ? undefined : ProviderID.make(routeInput.provider),
|
|
102
|
+
providerMetadataKey: routeInput.providerMetadataKey,
|
|
103
|
+
protocol: protocol.id,
|
|
104
|
+
endpoint: routeInput.endpoint,
|
|
105
|
+
auth: routeInput.auth ?? Auth.none,
|
|
106
|
+
headers: routeInput.headers,
|
|
107
|
+
transport: routeInput.transport,
|
|
108
|
+
options: routeInput.options,
|
|
109
|
+
body: protocol.body,
|
|
110
|
+
supportsEffortUpdates: protocol.supportsEffortUpdates,
|
|
111
|
+
toolUpdates: protocol.toolUpdates,
|
|
112
|
+
sanitizer: protocol.sanitizer,
|
|
113
|
+
with: (patch) => {
|
|
114
|
+
const { compact, id, provider, providerMetadataKey, auth, transport, endpoint, ...options } = patch;
|
|
115
|
+
return build({
|
|
116
|
+
...routeInput,
|
|
117
|
+
compact: "compact" in patch ? compact : routeInput.compact,
|
|
118
|
+
id: id ?? routeInput.id,
|
|
119
|
+
provider: provider ?? routeInput.provider,
|
|
120
|
+
providerMetadataKey: providerMetadataKey ??
|
|
121
|
+
(provider !== undefined && provider !== routeInput.provider ? provider : routeInput.providerMetadataKey),
|
|
122
|
+
auth: auth ?? routeInput.auth,
|
|
123
|
+
endpoint: endpoint ? Endpoint.merge(routeInput.endpoint, endpoint) : routeInput.endpoint,
|
|
124
|
+
transport: transport ?? routeInput.transport,
|
|
125
|
+
options: mergeRequestOptions(route.options, options),
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
model: (input) => makeRouteLanguageModel(route, input),
|
|
129
|
+
prepareTransport: (body, request, options) => routeInput.transport.prepare({
|
|
130
|
+
body,
|
|
131
|
+
request,
|
|
132
|
+
endpoint: routeInput.endpoint,
|
|
133
|
+
auth: routeInput.auth ?? Auth.none,
|
|
134
|
+
encodeBody: ProviderShared.encodeJson,
|
|
135
|
+
middleware: RequestExecutor.withFetch(request.http?.fetch, options?.http),
|
|
136
|
+
webSocket: options?.webSocket,
|
|
137
|
+
}),
|
|
138
|
+
streamPrepared: (prepared, request, runtime, options) => {
|
|
139
|
+
const route = `${request.model.provider}/${request.model.route.id}`;
|
|
140
|
+
return Stream.unwrap(routeInput.transport.execute(prepared, request, runtime, options).pipe(Effect.map((execution) => {
|
|
141
|
+
const terminal = protocol.stream.terminal;
|
|
142
|
+
// Preserve assembled inputs; replace only serialized event fallbacks with their original wire data.
|
|
143
|
+
const frameError = (frame, event = frame) => (error) => new AIError({
|
|
144
|
+
reason: AIErrorReason.make({
|
|
145
|
+
...error.reason,
|
|
146
|
+
message: error.reason.message,
|
|
147
|
+
cause: error.reason.cause,
|
|
148
|
+
body: error.reason.body !== undefined && error.reason.body !== ProviderShared.encodeJson(event)
|
|
149
|
+
? error.reason.body
|
|
150
|
+
: (execution.body?.(frame) ??
|
|
151
|
+
(typeof frame === "string" ? frame : ProviderShared.encodeJson(frame))),
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
const events = execution.frames.pipe(Stream.mapEffect((frame) => decodeEvent(route)(frame).pipe(Effect.catchCause((cause) => Effect.fail(streamError(route, `Failed to decode ${route} event`, cause))), Effect.map((event) => ({ event, frame })), Effect.mapError(frameError(frame)))), terminal ? Stream.takeUntil(({ event }) => terminal(event)) : (stream) => stream);
|
|
155
|
+
const stream = Stream.suspend(() => {
|
|
156
|
+
let state = protocol.stream.initial(request);
|
|
157
|
+
const parsed = events.pipe(Stream.mapEffect(({ event, frame }) => protocol.stream.step(state, event).pipe(Effect.catchCause((cause) => Effect.fail(streamError(route, `Failed to parse ${route} event`, cause))), Effect.map(([next, output]) => {
|
|
158
|
+
state = next;
|
|
159
|
+
return request.includeRaw && isProviderFrame(frame)
|
|
160
|
+
? [StreamEvent.raw({ data: execution.raw?.(frame) ?? rawFrame(frame) }), ...output]
|
|
161
|
+
: output;
|
|
162
|
+
}), Effect.mapError(frameError(frame, event)))), Stream.flatMap(Stream.fromIterable));
|
|
163
|
+
const onHalt = protocol.stream.onHalt;
|
|
164
|
+
return onHalt
|
|
165
|
+
? parsed.pipe(Stream.concat(Stream.suspend(() => Stream.unwrap(onHalt(state).pipe(Effect.map(Stream.fromIterable))))))
|
|
166
|
+
: parsed;
|
|
167
|
+
}).pipe(Stream.catchCause((cause) => Stream.fail(streamError(route, `Failed to read ${route} stream`, cause))), requireTerminalEvent(route), Stream.mapError((error) => new AIError({
|
|
168
|
+
reason: AIErrorReason.make({
|
|
169
|
+
...error.reason,
|
|
170
|
+
message: error.reason.message,
|
|
171
|
+
cause: error.reason.cause,
|
|
172
|
+
http: error.reason.http ?? execution.http,
|
|
173
|
+
}),
|
|
174
|
+
})));
|
|
175
|
+
return execution.complete ? stream.pipe(Stream.onEnd(execution.complete)) : stream;
|
|
176
|
+
})));
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
return route;
|
|
180
|
+
};
|
|
181
|
+
return build({ ...input, options: mergeRequestOptions(input.options) });
|
|
182
|
+
}
|
|
183
|
+
export function make(input) {
|
|
184
|
+
if ("transport" in input)
|
|
185
|
+
return makeFromTransport(input);
|
|
186
|
+
const protocol = input.protocol;
|
|
187
|
+
return makeFromTransport({
|
|
188
|
+
compact: input.compact,
|
|
189
|
+
id: input.id,
|
|
190
|
+
provider: input.provider,
|
|
191
|
+
providerMetadataKey: input.providerMetadataKey,
|
|
192
|
+
protocol,
|
|
193
|
+
endpoint: input.endpoint,
|
|
194
|
+
auth: input.auth,
|
|
195
|
+
headers: input.headers,
|
|
196
|
+
transport: HttpTransport.httpJson({ framing: input.framing }),
|
|
197
|
+
options: input.options,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
const prepareRequest = (request) => {
|
|
201
|
+
const original = resolveRequestOptions(request);
|
|
202
|
+
const sanitized = restoreClasses({ ...sanitizeSurrogates({ ...original, model: undefined }), model: original.model });
|
|
203
|
+
// Deduplicate per sibling level; a tool and a namespace may share a name.
|
|
204
|
+
const dedupe = (tools) => [...new Map(tools.map((tool) => [`${tool.type}:${tool.name}`, tool])).values()].map((tool) => tool.type === "tool" ? tool : { ...tool, tools: dedupe(tool.tools) });
|
|
205
|
+
const project = (tools) => ToolSchemaProjection.tools(dedupe(tools), sanitized.model);
|
|
206
|
+
const updated = planToolUpdates(sanitized);
|
|
207
|
+
const resolved = applyCachePolicy(applyEffortUpdates(mapToolUpdates({ ...updated, tools: project(updated.tools) }, project)));
|
|
208
|
+
const headers = resolved.model.route.headers?.({ request: resolved });
|
|
209
|
+
return headers === undefined
|
|
210
|
+
? resolved
|
|
211
|
+
: { ...resolved, http: { ...resolved.http, headers: { ...headers, ...resolved.http?.headers } } };
|
|
212
|
+
};
|
|
213
|
+
// Sanitizing copies changed class instances into plain records; rebuild them so protocols see the canonical classes.
|
|
214
|
+
const restoreClasses = (request) => ({
|
|
215
|
+
...request,
|
|
216
|
+
messages: request.messages.map(Message.make),
|
|
217
|
+
tools: request.tools.map(ToolEntry.make),
|
|
218
|
+
});
|
|
219
|
+
const compile = Effect.fn("LLM.compile")(function* (request, options) {
|
|
220
|
+
const references = request.messages.flatMap((message) => message.content.flatMap((part) => {
|
|
221
|
+
if (part.type === "media") {
|
|
222
|
+
const source = part.media.source;
|
|
223
|
+
return source.type === "url" ? [source.url] : source.type === "ref" ? [source.id] : [];
|
|
224
|
+
}
|
|
225
|
+
if (part.type !== "tool-result" || part.result.type !== "content")
|
|
226
|
+
return [];
|
|
227
|
+
return part.result.value.flatMap((item) => (item.type === "file" ? [item.uri] : []));
|
|
228
|
+
}));
|
|
229
|
+
if (references.some((reference) => /^\s*file:/i.test(reference)))
|
|
230
|
+
return yield* ProviderShared.invalidRequest("Local file references must be read with Media.file(path) before sending");
|
|
231
|
+
const forced = request.toolChoice?.type === "tool" ? request.toolChoice.name : undefined;
|
|
232
|
+
if (forced !== undefined && !forceableNames(request).has(forced))
|
|
233
|
+
return yield* ProviderShared.invalidRequest(`Tool choice names "${forced}", which is not one of the request's tools`);
|
|
234
|
+
const resolved = prepareRequest(request);
|
|
235
|
+
const route = resolved.model.route;
|
|
236
|
+
const body = yield* route.body.from(resolved);
|
|
237
|
+
const prepared = yield* route.prepareTransport(body, resolved, options);
|
|
238
|
+
return {
|
|
239
|
+
request: resolved,
|
|
240
|
+
route,
|
|
241
|
+
body,
|
|
242
|
+
prepared,
|
|
243
|
+
};
|
|
244
|
+
});
|
|
245
|
+
// A forced choice may name a top-level tool, a tool a system message adds, or a namespaced tool by its path
|
|
246
|
+
// (`crm.lookup`) or flattened name (`crm_lookup`).
|
|
247
|
+
const forceableNames = (request) => {
|
|
248
|
+
const names = (tools, path) => tools.flatMap((tool) => tool.type === "namespace"
|
|
249
|
+
? names(tool.tools, [...path, tool.name])
|
|
250
|
+
: [[...path, tool.name].join("."), [...path, tool.name].join("_")]);
|
|
251
|
+
const added = request.messages.flatMap((message) => message.role === "system" && message.tools !== undefined ? message.tools.add : []);
|
|
252
|
+
return new Set(names([...request.tools, ...added], []));
|
|
253
|
+
};
|
|
254
|
+
/** Test-only projection of the execution compiler; exported from `@radheim/ai/testing`. */
|
|
255
|
+
export const compileRequest = Effect.fn("LLM.compileRequest")(function* (request) {
|
|
256
|
+
const compiled = yield* compile(request);
|
|
257
|
+
return {
|
|
258
|
+
route: compiled.route.id,
|
|
259
|
+
protocol: compiled.route.protocol,
|
|
260
|
+
model: compiled.request.model,
|
|
261
|
+
body: compiled.body,
|
|
262
|
+
metadata: { transport: compiled.route.transport.id },
|
|
263
|
+
};
|
|
264
|
+
});
|
|
265
|
+
const streamRequestWith = (runtime) => (request, options) => Stream.unwrap(Effect.gen(function* () {
|
|
266
|
+
const compiled = yield* compile(request, options);
|
|
267
|
+
return compiled.route.streamPrepared(compiled.prepared, compiled.request, runtime, options);
|
|
268
|
+
}));
|
|
269
|
+
export const generateWith = (stream) => Effect.fn("LLM.generate")(function* (request, options) {
|
|
270
|
+
let state = ResponseFold.empty();
|
|
271
|
+
yield* stream(request, options).pipe(Stream.runForEach((event) => Effect.sync(() => (state = ResponseFold.reduce(state, event)))), Effect.mapError((error) => withPartial(error, ResponseFold.partial(state))));
|
|
272
|
+
if (state.finish)
|
|
273
|
+
return ResponseFold.result(state.message, state.usage, state.finish);
|
|
274
|
+
return yield* withPartial(incompleteStreamError(`${request.model.provider}/${request.model.route.id}`), ResponseFold.partial(state));
|
|
275
|
+
});
|
|
276
|
+
export const textOperations = (executor) => {
|
|
277
|
+
const stream = streamRequestWith({ http: executor });
|
|
278
|
+
function compact(request, options) {
|
|
279
|
+
return Effect.suspend(() => {
|
|
280
|
+
if (options?.mechanism === "trigger" && canCompact(request, options))
|
|
281
|
+
return request.model.route.compact.trigger(prepareRequest(request), executor, options);
|
|
282
|
+
if ((options?.mechanism === undefined || options.mechanism === "endpoint") && canCompact(request))
|
|
283
|
+
return request.model.route.compact.endpoint(prepareRequest(request), executor, options);
|
|
284
|
+
return unsupportedCompaction(request.model, options?.mechanism);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
stream,
|
|
289
|
+
generate: generateWith(stream),
|
|
290
|
+
compact,
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
export const Route = { make };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { HttpClientResponse } from "effect/http";
|
|
3
|
+
import { DecisionModel, DecisionModelCompatibility, type DecisionOptions, type DecisionRequest, type DecisionResponse } from "../decision.js";
|
|
4
|
+
import { ProviderID, type AIError, type ProviderOptions, type TransportOptions } from "../schema/index.js";
|
|
5
|
+
import { Auth } from "./auth.js";
|
|
6
|
+
import { Endpoint } from "./endpoint.js";
|
|
7
|
+
import { MediaProtocol } from "./media-protocol.js";
|
|
8
|
+
/** One decision API: how a request becomes its JSON body and how its response becomes a `DecisionResponse`. */
|
|
9
|
+
export interface Protocol {
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** The host only takes inline data, so URL media is downloaded before `body.from`. */
|
|
13
|
+
readonly inlineMedia?: boolean;
|
|
14
|
+
readonly body: {
|
|
15
|
+
readonly from: (request: DecisionRequest) => Effect.Effect<Record<string, unknown>, AIError>;
|
|
16
|
+
};
|
|
17
|
+
readonly response: {
|
|
18
|
+
readonly decode: (response: HttpClientResponse.HttpClientResponse, context: {
|
|
19
|
+
readonly request: DecisionRequest;
|
|
20
|
+
readonly providerMetadataKey: string;
|
|
21
|
+
}) => Effect.Effect<DecisionResponse, AIError>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export declare const protocol: (input: Protocol) => Protocol;
|
|
25
|
+
/** Headers, timeouts and `http` overlays merge into the route's; every model of the route starts from them. */
|
|
26
|
+
export interface Patch extends TransportOptions {
|
|
27
|
+
readonly id?: string;
|
|
28
|
+
readonly provider?: string | ProviderID;
|
|
29
|
+
/** ProviderMetadata namespace for responses; defaults to the provider. */
|
|
30
|
+
readonly providerMetadataKey?: string;
|
|
31
|
+
readonly endpoint?: Endpoint.EndpointPatch<MediaProtocol.Body, DecisionRequest>;
|
|
32
|
+
readonly auth?: Auth.Definition;
|
|
33
|
+
}
|
|
34
|
+
export interface Route extends TransportOptions {
|
|
35
|
+
readonly id: string;
|
|
36
|
+
readonly provider: ProviderID;
|
|
37
|
+
readonly providerMetadataKey: string;
|
|
38
|
+
readonly protocol: Protocol;
|
|
39
|
+
readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
|
|
40
|
+
readonly auth: Auth.Definition;
|
|
41
|
+
readonly with: (patch: Patch) => Route;
|
|
42
|
+
readonly model: <Options extends DecisionOptions = DecisionOptions>(input: {
|
|
43
|
+
readonly id: string;
|
|
44
|
+
readonly compatibility?: DecisionModelCompatibility.Input;
|
|
45
|
+
readonly providerOptions?: ProviderOptions;
|
|
46
|
+
}) => DecisionModel<Options>;
|
|
47
|
+
}
|
|
48
|
+
export interface MakeInput extends Omit<Patch, "endpoint"> {
|
|
49
|
+
readonly id: string;
|
|
50
|
+
readonly provider: string | ProviderID;
|
|
51
|
+
readonly protocol: Protocol;
|
|
52
|
+
readonly endpoint: Endpoint.Definition<MediaProtocol.Body, DecisionRequest>;
|
|
53
|
+
}
|
|
54
|
+
export declare const make: (input: MakeInput) => Route;
|
|
55
|
+
export * as DecisionRoute from "./decision.js";
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { DecisionModel, DecisionModelCompatibility, } from "../decision.js";
|
|
3
|
+
import { mergeTransport, ProviderID, } from "../schema/index.js";
|
|
4
|
+
import { Auth } from "./auth.js";
|
|
5
|
+
import { Endpoint } from "./endpoint.js";
|
|
6
|
+
import { RequestExecutor } from "./executor.js";
|
|
7
|
+
import { MediaProtocol } from "./media-protocol.js";
|
|
8
|
+
import { MediaRoute } from "./media.js";
|
|
9
|
+
export const protocol = (input) => input;
|
|
10
|
+
export const make = (input) => {
|
|
11
|
+
const provider = ProviderID.make(input.provider);
|
|
12
|
+
const route = {
|
|
13
|
+
id: input.id,
|
|
14
|
+
provider,
|
|
15
|
+
providerMetadataKey: input.providerMetadataKey ?? provider,
|
|
16
|
+
protocol: input.protocol,
|
|
17
|
+
endpoint: input.endpoint,
|
|
18
|
+
auth: input.auth ?? Auth.none,
|
|
19
|
+
...mergeTransport(input),
|
|
20
|
+
};
|
|
21
|
+
return {
|
|
22
|
+
...route,
|
|
23
|
+
with: (patch) => make({
|
|
24
|
+
...route,
|
|
25
|
+
...patch,
|
|
26
|
+
// A new provider gets its own metadata key unless the patch names one.
|
|
27
|
+
providerMetadataKey: patch.providerMetadataKey ?? (patch.provider === undefined ? route.providerMetadataKey : undefined),
|
|
28
|
+
endpoint: patch.endpoint === undefined ? route.endpoint : Endpoint.merge(route.endpoint, patch.endpoint),
|
|
29
|
+
...mergeTransport(route, patch),
|
|
30
|
+
}),
|
|
31
|
+
model: (model) => {
|
|
32
|
+
const send = transport(route);
|
|
33
|
+
return DecisionModel.make({
|
|
34
|
+
id: model.id,
|
|
35
|
+
provider,
|
|
36
|
+
transport: mergeTransport(route),
|
|
37
|
+
providerOptions: model.providerOptions,
|
|
38
|
+
compatibility: model.compatibility === undefined ? undefined : DecisionModelCompatibility.make(model.compatibility),
|
|
39
|
+
route: { id: route.id, generate: send },
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
// The HTTP plumbing (URL, query, auth, headers, `http` overlays, surrogate sanitizing) is the one media routes use.
|
|
45
|
+
const transport = (route) => {
|
|
46
|
+
const identity = MediaProtocol.identity({ id: route.id, name: route.protocol.name, provider: route.provider });
|
|
47
|
+
const composed = MediaRoute.inline({
|
|
48
|
+
protocol: MediaProtocol.inline(identity, {
|
|
49
|
+
body: {
|
|
50
|
+
from: (request) => route.protocol.body.from(request).pipe(Effect.map((body) => MediaProtocol.json(body))),
|
|
51
|
+
},
|
|
52
|
+
response: {
|
|
53
|
+
decode: (response, context) => route.protocol.response.decode(response, {
|
|
54
|
+
request: context.request,
|
|
55
|
+
providerMetadataKey: route.providerMetadataKey,
|
|
56
|
+
}),
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
endpoint: route.endpoint,
|
|
60
|
+
auth: route.auth,
|
|
61
|
+
});
|
|
62
|
+
return Effect.fnUntraced(function* (request, execute) {
|
|
63
|
+
if (!route.protocol.inlineMedia)
|
|
64
|
+
return yield* composed.generate(request, execute);
|
|
65
|
+
const timed = RequestExecutor.withTimeout(execute, mergeTransport(request.model.transport, request).http?.timeout);
|
|
66
|
+
const input = yield* Effect.forEach(request.input, (part) => materialize(part, timed));
|
|
67
|
+
return yield* composed.generate({ ...request, input }, execute);
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
const materialize = (part, execute) => part.type === "media" && part.media.source.type === "url"
|
|
71
|
+
? part.media.materialize().pipe(Effect.provideService(RequestExecutor.Service, { execute }), Effect.map((media) => ({ ...part, media })))
|
|
72
|
+
: Effect.succeed(part);
|
|
73
|
+
export * as DecisionRoute from "./decision.js";
|