@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,80 @@
|
|
|
1
|
+
import { Effect, Redacted } from "effect";
|
|
2
|
+
import { RequestExecutor } from "../route/executor.js";
|
|
3
|
+
import { AIError, type RequestTimeout } from "../schema/index.js";
|
|
4
|
+
export type RealtimeTransportInput = {
|
|
5
|
+
readonly type: "webrtc";
|
|
6
|
+
readonly sdp: string;
|
|
7
|
+
readonly dcid?: number;
|
|
8
|
+
} | {
|
|
9
|
+
readonly type: "websocket";
|
|
10
|
+
};
|
|
11
|
+
export type RealtimeTransportType = RealtimeTransportInput["type"];
|
|
12
|
+
export type RealtimeTransport = {
|
|
13
|
+
readonly type: "webrtc";
|
|
14
|
+
readonly sdp: string;
|
|
15
|
+
} | {
|
|
16
|
+
readonly type: "websocket";
|
|
17
|
+
readonly url: string;
|
|
18
|
+
readonly token: string;
|
|
19
|
+
readonly expiresAt: number;
|
|
20
|
+
};
|
|
21
|
+
export type RealtimeSession = Readonly<Record<string, unknown>>;
|
|
22
|
+
export interface RealtimeConnectInput {
|
|
23
|
+
readonly engine: RealtimeEngine;
|
|
24
|
+
readonly model: string;
|
|
25
|
+
readonly apiKey: Redacted.Redacted;
|
|
26
|
+
/** Where a GPT-Live session starts, OpenAI by default: another host of its API, such as OpenCode's. */
|
|
27
|
+
readonly baseURL?: string;
|
|
28
|
+
/** Headers that host needs besides the API key, such as OpenCode's workspace. */
|
|
29
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
30
|
+
readonly timeout?: RequestTimeout;
|
|
31
|
+
readonly session?: RealtimeSession;
|
|
32
|
+
readonly transport: RealtimeTransportInput;
|
|
33
|
+
}
|
|
34
|
+
export declare const engines: readonly [{
|
|
35
|
+
readonly id: "openai-live";
|
|
36
|
+
readonly provider: "openai";
|
|
37
|
+
readonly api: "live";
|
|
38
|
+
readonly transports: readonly ["webrtc"];
|
|
39
|
+
}, {
|
|
40
|
+
readonly id: "openai-realtime";
|
|
41
|
+
readonly provider: "openai";
|
|
42
|
+
readonly api: "realtime";
|
|
43
|
+
readonly transports: readonly ["webrtc", "websocket"];
|
|
44
|
+
}, {
|
|
45
|
+
readonly id: "xai-realtime";
|
|
46
|
+
readonly provider: "xai";
|
|
47
|
+
readonly api: "realtime";
|
|
48
|
+
readonly transports: readonly ["websocket"];
|
|
49
|
+
}, {
|
|
50
|
+
readonly id: "gemini-live";
|
|
51
|
+
readonly provider: "google";
|
|
52
|
+
readonly api: "bidi-generate-content";
|
|
53
|
+
readonly transports: readonly ["websocket"];
|
|
54
|
+
}];
|
|
55
|
+
export type RealtimeEngine = (typeof engines)[number];
|
|
56
|
+
export declare const connect: (input: RealtimeConnectInput) => Effect.Effect<RealtimeTransport, AIError, RequestExecutor.RequestExecutorService>;
|
|
57
|
+
export declare const Realtime: {
|
|
58
|
+
readonly engines: readonly [{
|
|
59
|
+
readonly id: "openai-live";
|
|
60
|
+
readonly provider: "openai";
|
|
61
|
+
readonly api: "live";
|
|
62
|
+
readonly transports: readonly ["webrtc"];
|
|
63
|
+
}, {
|
|
64
|
+
readonly id: "openai-realtime";
|
|
65
|
+
readonly provider: "openai";
|
|
66
|
+
readonly api: "realtime";
|
|
67
|
+
readonly transports: readonly ["webrtc", "websocket"];
|
|
68
|
+
}, {
|
|
69
|
+
readonly id: "xai-realtime";
|
|
70
|
+
readonly provider: "xai";
|
|
71
|
+
readonly api: "realtime";
|
|
72
|
+
readonly transports: readonly ["websocket"];
|
|
73
|
+
}, {
|
|
74
|
+
readonly id: "gemini-live";
|
|
75
|
+
readonly provider: "google";
|
|
76
|
+
readonly api: "bidi-generate-content";
|
|
77
|
+
readonly transports: readonly ["websocket"];
|
|
78
|
+
}];
|
|
79
|
+
readonly connect: (input: RealtimeConnectInput) => Effect.Effect<RealtimeTransport, AIError, RequestExecutor.RequestExecutorService>;
|
|
80
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Clock, Effect, Redacted, Schema } from "effect";
|
|
2
|
+
import { HttpClientRequest } from "effect/http";
|
|
3
|
+
import { RequestExecutor } from "../route/executor.js";
|
|
4
|
+
import { AIError, InvalidProviderOutputError, InvalidRequestError } from "../schema/index.js";
|
|
5
|
+
import { encodeJson } from "../utils/json.js";
|
|
6
|
+
const OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
7
|
+
const XAI_BASE_URL = "https://api.x.ai/v1";
|
|
8
|
+
const GOOGLE_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
|
9
|
+
// The voice APIs this package speaks, by provider and models.dev API. Which models they start comes from models.dev.
|
|
10
|
+
export const engines = [
|
|
11
|
+
{ id: "openai-live", provider: "openai", api: "live", transports: ["webrtc"] },
|
|
12
|
+
{ id: "openai-realtime", provider: "openai", api: "realtime", transports: ["webrtc", "websocket"] },
|
|
13
|
+
{ id: "xai-realtime", provider: "xai", api: "realtime", transports: ["websocket"] },
|
|
14
|
+
{ id: "gemini-live", provider: "google", api: "bidi-generate-content", transports: ["websocket"] },
|
|
15
|
+
];
|
|
16
|
+
export const connect = Effect.fn("Realtime.connect")(function* (input) {
|
|
17
|
+
const resolved = input.engine;
|
|
18
|
+
if (!resolved.transports.some((type) => type === input.transport.type))
|
|
19
|
+
return yield* invalidRequest(`${input.model} only supports ${resolved.transports.join(", ")} transport`);
|
|
20
|
+
const session = { ...input.session, model: input.model };
|
|
21
|
+
switch (resolved.id) {
|
|
22
|
+
case "openai-live": {
|
|
23
|
+
const data = yield* post(resolved.id, `${input.baseURL ?? OPENAI_BASE_URL}/live/sessions`, input.apiKey, { session, transport: input.transport }, LiveSessionResponse, { headers: input.headers, timeout: input.timeout });
|
|
24
|
+
return { type: "webrtc", sdp: data.transport.sdp };
|
|
25
|
+
}
|
|
26
|
+
case "openai-realtime": {
|
|
27
|
+
if (input.transport.type === "websocket")
|
|
28
|
+
return yield* clientSecret(resolved.id, OPENAI_BASE_URL, input.model, input.apiKey, {
|
|
29
|
+
expires_after: { anchor: "created_at", seconds: 60 },
|
|
30
|
+
session,
|
|
31
|
+
}, input.timeout);
|
|
32
|
+
const form = new FormData();
|
|
33
|
+
form.append("sdp", input.transport.sdp);
|
|
34
|
+
form.append("session", encodeJson(session));
|
|
35
|
+
const query = input.transport.dcid === undefined ? "" : `?dcid=${input.transport.dcid}`;
|
|
36
|
+
const sdp = yield* post(resolved.id, `${OPENAI_BASE_URL}/realtime/calls${query}`, input.apiKey, form, Schema.String, { timeout: input.timeout });
|
|
37
|
+
return { type: "webrtc", sdp };
|
|
38
|
+
}
|
|
39
|
+
case "xai-realtime":
|
|
40
|
+
return yield* clientSecret(resolved.id, XAI_BASE_URL, input.model, input.apiKey, {
|
|
41
|
+
expires_after: { seconds: 60 },
|
|
42
|
+
}, input.timeout);
|
|
43
|
+
case "gemini-live": {
|
|
44
|
+
const now = yield* Clock.currentTimeMillis;
|
|
45
|
+
const expiresAt = now + 60_000;
|
|
46
|
+
const data = yield* post(resolved.id, `${GOOGLE_BASE_URL}/auth_tokens`, input.apiKey, {
|
|
47
|
+
uses: 1,
|
|
48
|
+
expireTime: new Date(now + 30 * 60_000).toISOString(),
|
|
49
|
+
newSessionExpireTime: new Date(expiresAt).toISOString(),
|
|
50
|
+
bidiGenerateContentSetup: { ...input.session, model: `models/${input.model}` },
|
|
51
|
+
}, AuthTokenResponse, { authHeader: "x-goog-api-key", timeout: input.timeout });
|
|
52
|
+
return {
|
|
53
|
+
type: "websocket",
|
|
54
|
+
url: "wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained",
|
|
55
|
+
token: data.name,
|
|
56
|
+
expiresAt: Math.floor(expiresAt / 1000),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
export const Realtime = {
|
|
62
|
+
engines,
|
|
63
|
+
connect,
|
|
64
|
+
};
|
|
65
|
+
const LiveSessionResponse = Schema.fromJsonString(Schema.Struct({
|
|
66
|
+
transport: Schema.Struct({ sdp: Schema.String }),
|
|
67
|
+
}));
|
|
68
|
+
// xAI's ephemeral-token docs (https://docs.x.ai/developers/model-capabilities/audio/ephemeral-tokens)
|
|
69
|
+
// document the request body (`{ expires_after: { seconds } }`, no `anchor` or `session`) but not the response payload; we assume
|
|
70
|
+
// it mirrors OpenAI's `{ value, expires_at }` response shape.
|
|
71
|
+
const ClientSecretResponse = Schema.fromJsonString(Schema.Struct({
|
|
72
|
+
value: Schema.String,
|
|
73
|
+
expires_at: Schema.Number,
|
|
74
|
+
}));
|
|
75
|
+
const AuthTokenResponse = Schema.fromJsonString(Schema.Struct({
|
|
76
|
+
name: Schema.String,
|
|
77
|
+
}));
|
|
78
|
+
const invalidRequest = (message) => new AIError({ reason: new InvalidRequestError({ message }) });
|
|
79
|
+
const post = (route, url, apiKey, body, response, options = {}) => Effect.gen(function* () {
|
|
80
|
+
const executor = yield* RequestExecutor.Service;
|
|
81
|
+
const res = yield* RequestExecutor.withTimeout(executor.execute, options.timeout)(HttpClientRequest.post(url).pipe(HttpClientRequest.setHeaders(options.headers ?? {}), options.authHeader
|
|
82
|
+
? HttpClientRequest.setHeader(options.authHeader, Redacted.value(apiKey))
|
|
83
|
+
: HttpClientRequest.bearerToken(apiKey), body instanceof FormData
|
|
84
|
+
? HttpClientRequest.bodyFormData(body)
|
|
85
|
+
: HttpClientRequest.bodyText(encodeJson(body), "application/json")));
|
|
86
|
+
const http = RequestExecutor.responseHttp(res);
|
|
87
|
+
const fail = (message, cause, raw) => new AIError({ reason: new InvalidProviderOutputError({ route, message, body: raw, http, cause }) });
|
|
88
|
+
const text = yield* RequestExecutor.responseText(res);
|
|
89
|
+
return yield* Schema.decodeEffect(response)(text).pipe(Effect.mapError((cause) => fail(`${route} returned an invalid response`, cause, text)));
|
|
90
|
+
});
|
|
91
|
+
const clientSecret = (route, baseURL, model, apiKey, body, timeout) => post(route, `${baseURL}/realtime/client_secrets`, apiKey, body, ClientSecretResponse, { timeout }).pipe(Effect.map((data) => ({
|
|
92
|
+
type: "websocket",
|
|
93
|
+
url: `${baseURL.replace(/^http/, "ws")}/realtime?model=${encodeURIComponent(model)}`,
|
|
94
|
+
token: data.value,
|
|
95
|
+
expiresAt: data.expires_at,
|
|
96
|
+
})));
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechResponse, SpeechTimestamp, SpeechVoice } from "./speech.js";
|
|
2
|
+
export type { SpeechFormat, SpeechModelOptions, SpeechOptions, SpeechRequest, SpeechRequestInput, SpeechRoute, } from "./speech.js";
|
|
3
|
+
export { Speech } from "./speech.js";
|
|
4
|
+
export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
|
|
5
|
+
export type { TranscriptionModelOptions, TranscriptionOptions, TranscriptionRequest, TranscriptionRequestInput, TranscriptionRoute, } from "./transcription.js";
|
|
6
|
+
export { Transcription } from "./transcription.js";
|
|
7
|
+
export { Extraction, ExtractionModel, ExtractionResponse } from "./experimental/extraction.js";
|
|
8
|
+
export type { ExtractionAnswerFor, ExtractionAnswersFor, ExtractionField, ExtractionFields, ExtractionModelOptions, ExtractionOptions, ExtractionRequest, ExtractionRequestInput, ExtractionResponseFor, ExtractionRoute, } from "./experimental/extraction.js";
|
|
9
|
+
export { Realtime } from "./experimental/realtime.js";
|
|
10
|
+
export type { RealtimeConnectInput, RealtimeEngine, RealtimeSession, RealtimeTransport, RealtimeTransportInput, RealtimeTransportType, } from "./experimental/realtime.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { SpeechEvent, SpeechModel, SpeechModelSchema, SpeechResponse, SpeechTimestamp, SpeechVoice } from "./speech.js";
|
|
2
|
+
export { Speech } from "./speech.js";
|
|
3
|
+
export { TranscriptionEvent, TranscriptionModel, TranscriptionModelSchema, TranscriptionResponse, TranscriptionSegment, TranscriptionTimestamps, TranscriptionWord, } from "./transcription.js";
|
|
4
|
+
export { Transcription } from "./transcription.js";
|
|
5
|
+
export { Extraction, ExtractionModel, ExtractionResponse } from "./experimental/extraction.js";
|
|
6
|
+
export { Realtime } from "./experimental/realtime.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { HttpMiddleware } from "./route/executor-service.js";
|
|
2
|
+
/**
|
|
3
|
+
* Wraps a provider request with standard fetch types: change the `Request`, inspect or replace the `Response`, or answer
|
|
4
|
+
* without calling `next`. Response bodies stream through unread, and `request.signal` aborts when the call does.
|
|
5
|
+
*/
|
|
6
|
+
export type FetchMiddleware = (request: Request, next: (request: Request) => Promise<Response>) => Promise<Response>;
|
|
7
|
+
export declare const fromFetch: (middleware: FetchMiddleware) => HttpMiddleware;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Cause, Context, Effect, Exit, Option, Result, Stream } from "effect";
|
|
2
|
+
import { HttpBody, HttpClientRequest, HttpClientResponse } from "effect/http";
|
|
3
|
+
// Hands `middleware` a web `Request` that aborts with the call, and maps each `Request` it forwards and the `Response`
|
|
4
|
+
// it returns back to the executor's types. Response bodies are converted as streams, never buffered.
|
|
5
|
+
export const fromFetch = (middleware) => (request, handler) => Effect.contextWith((context) => {
|
|
6
|
+
const sent = new WeakMap();
|
|
7
|
+
return Effect.tryPromise({
|
|
8
|
+
try: async (signal) => {
|
|
9
|
+
const web = Result.getOrThrow(HttpClientRequest.toWebResult(request, { signal, context }));
|
|
10
|
+
const response = await middleware(web, async (forwarded) => {
|
|
11
|
+
const next = await fromWebRequest(forwarded, web.body === forwarded.body ? request.body : undefined);
|
|
12
|
+
const exit = await Effect.runPromiseExitWith(context)(handler(next), {
|
|
13
|
+
signal: AbortSignal.any([signal, forwarded.signal]),
|
|
14
|
+
});
|
|
15
|
+
if (Exit.isFailure(exit))
|
|
16
|
+
throw Cause.squash(exit.cause);
|
|
17
|
+
const result = toWebResponse(exit.value, context);
|
|
18
|
+
sent.set(result, next);
|
|
19
|
+
return result;
|
|
20
|
+
});
|
|
21
|
+
return HttpClientResponse.fromWeb(sent.get(response) ?? request, response);
|
|
22
|
+
},
|
|
23
|
+
catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
// A forwarded request whose body is still the one it was handed keeps the original body; a replaced body is read into
|
|
27
|
+
// bytes so the executor can send it more than once.
|
|
28
|
+
const fromWebRequest = async (request, body) => {
|
|
29
|
+
const converted = HttpClientRequest.fromWeb(request);
|
|
30
|
+
if (body !== undefined)
|
|
31
|
+
return HttpClientRequest.setBody(converted, body);
|
|
32
|
+
if (converted.body._tag === "Empty")
|
|
33
|
+
return converted;
|
|
34
|
+
return HttpClientRequest.bodyUint8Array(converted, new Uint8Array(await request.arrayBuffer()), request.headers.get("content-type") ?? undefined);
|
|
35
|
+
};
|
|
36
|
+
// A null-body status can't be given a body, even an empty one.
|
|
37
|
+
const NULL_BODY = new Set([204, 205, 304]);
|
|
38
|
+
const toWebResponse = (response, context) => Option.getOrElse(HttpClientResponse.toWeb(response), () => new Response(NULL_BODY.has(response.status) ? null : Stream.toReadableStreamWith(response.stream, context), {
|
|
39
|
+
status: response.status,
|
|
40
|
+
headers: response.headers,
|
|
41
|
+
}));
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Duration, Effect, Schema, Stream } from "effect";
|
|
2
|
+
import { AIError } from "./schema/errors.js";
|
|
3
|
+
export declare const Status: Schema.Literals<readonly ["running", "completed", "error"]>;
|
|
4
|
+
export type Status = Schema.Schema.Type<typeof Status>;
|
|
5
|
+
/** Why a generation ended in `error`. */
|
|
6
|
+
export declare const Reason: Schema.Literals<readonly ["failed", "cancelled", "expired"]>;
|
|
7
|
+
export type Reason = Schema.Schema.Type<typeof Reason>;
|
|
8
|
+
/** Provider-neutral view of one generation observation. */
|
|
9
|
+
export interface Snapshot {
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly status: Status;
|
|
12
|
+
/** Set when `status` is `error`. */
|
|
13
|
+
readonly reason?: Reason;
|
|
14
|
+
/** Normalized 0..1 when the provider reports progress. */
|
|
15
|
+
readonly progress?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Route-owned generation operations for one generation. The media route decodes its serializable token once (from the
|
|
19
|
+
* submission response or a `resume` input) and closes over it, so `Generation` never sees the token's shape.
|
|
20
|
+
*/
|
|
21
|
+
export interface Route<Response> {
|
|
22
|
+
readonly status: Effect.Effect<Snapshot, AIError>;
|
|
23
|
+
readonly result: Effect.Effect<Response, AIError>;
|
|
24
|
+
readonly cancel?: Effect.Effect<void, AIError>;
|
|
25
|
+
}
|
|
26
|
+
export interface Poll {
|
|
27
|
+
readonly interval?: Duration.Input;
|
|
28
|
+
readonly timeout?: Duration.Input;
|
|
29
|
+
}
|
|
30
|
+
export interface AwaitOptions {
|
|
31
|
+
readonly poll?: Poll;
|
|
32
|
+
}
|
|
33
|
+
export declare const ProgressEvent: Schema.Struct<{
|
|
34
|
+
readonly type: Schema.tag<"generation-progress">;
|
|
35
|
+
readonly id: Schema.String;
|
|
36
|
+
readonly progress: Schema.optional<Schema.Number>;
|
|
37
|
+
}>;
|
|
38
|
+
export type Observation = Schema.Schema.Type<typeof ProgressEvent>;
|
|
39
|
+
export type Event = Observation | {
|
|
40
|
+
readonly type: "generation-finished";
|
|
41
|
+
readonly id: string;
|
|
42
|
+
readonly status: Status;
|
|
43
|
+
readonly reason?: Reason;
|
|
44
|
+
};
|
|
45
|
+
export declare const isTerminal: (status: Status) => status is "completed" | "error";
|
|
46
|
+
export declare class Generation<Response> {
|
|
47
|
+
readonly route: Route<Response>;
|
|
48
|
+
/** Route-owned serializable JSON; pass it to the modality's `resume` from another process. */
|
|
49
|
+
readonly token: unknown;
|
|
50
|
+
readonly id: string;
|
|
51
|
+
readonly status: Status;
|
|
52
|
+
readonly reason?: Reason;
|
|
53
|
+
readonly progress?: number;
|
|
54
|
+
constructor(route: Route<Response>,
|
|
55
|
+
/** Route-owned serializable JSON; pass it to the modality's `resume` from another process. */
|
|
56
|
+
token: unknown, snapshot: Snapshot);
|
|
57
|
+
get snapshot(): Snapshot;
|
|
58
|
+
get terminal(): boolean;
|
|
59
|
+
refresh(): Effect.Effect<Generation<Response>, AIError>;
|
|
60
|
+
/** Fetch the result without polling; non-completed terminal generations fail with the provider's terminal body. */
|
|
61
|
+
result(): Effect.Effect<Response, AIError>;
|
|
62
|
+
/** Poll until the generation reaches a terminal status, then fetch the result. Fails with a `Timeout` reason on deadline. */
|
|
63
|
+
await(options?: AwaitOptions): Effect.Effect<Response, AIError>;
|
|
64
|
+
cancel(): Effect.Effect<void, AIError>;
|
|
65
|
+
/**
|
|
66
|
+
* Status observations as a stream, ending after the first terminal observation. Each poll and each sleep between polls
|
|
67
|
+
* is bounded by the time remaining until `poll.timeout`, so a hung status request or a long interval fails the stream at
|
|
68
|
+
* the deadline instead of stalling it. (`Stream.interruptWhen` would express this directly but deadlocks under
|
|
69
|
+
* `TestClock` when the source completes while the timer sleeps.)
|
|
70
|
+
*/
|
|
71
|
+
events(options?: AwaitOptions): Stream.Stream<Event, AIError>;
|
|
72
|
+
private event;
|
|
73
|
+
private poll;
|
|
74
|
+
private schedule;
|
|
75
|
+
}
|
|
76
|
+
/** `events` followed by the expanded result, with the result fetch bounded by the same `poll.timeout` deadline. */
|
|
77
|
+
export declare const resultEvents: <Response, A>(generation: Generation<Response>, expand: (response: Response) => ReadonlyArray<A>, options?: AwaitOptions) => Stream.Stream<Observation | A, AIError>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Clock, Duration, Effect, Schedule, Schema, Stream } from "effect";
|
|
2
|
+
import { AIError, TimeoutError } from "./schema/errors.js";
|
|
3
|
+
export const Status = Schema.Literals(["running", "completed", "error"]);
|
|
4
|
+
/** Why a generation ended in `error`. */
|
|
5
|
+
export const Reason = Schema.Literals(["failed", "cancelled", "expired"]);
|
|
6
|
+
const DEFAULT_POLL_INTERVAL = Duration.seconds(5);
|
|
7
|
+
const DEFAULT_POLL_TIMEOUT = Duration.minutes(10);
|
|
8
|
+
export const ProgressEvent = Schema.Struct({
|
|
9
|
+
type: Schema.tag("generation-progress"),
|
|
10
|
+
id: Schema.String,
|
|
11
|
+
progress: Schema.optional(Schema.Number),
|
|
12
|
+
}).annotate({ identifier: "Generation.Event.Progress" });
|
|
13
|
+
export const isTerminal = (status) => status !== "running";
|
|
14
|
+
export class Generation {
|
|
15
|
+
route;
|
|
16
|
+
token;
|
|
17
|
+
id;
|
|
18
|
+
status;
|
|
19
|
+
reason;
|
|
20
|
+
progress;
|
|
21
|
+
constructor(route,
|
|
22
|
+
/** Route-owned serializable JSON; pass it to the modality's `resume` from another process. */
|
|
23
|
+
token, snapshot) {
|
|
24
|
+
this.route = route;
|
|
25
|
+
this.token = token;
|
|
26
|
+
this.id = snapshot.id;
|
|
27
|
+
this.status = snapshot.status;
|
|
28
|
+
this.reason = snapshot.reason;
|
|
29
|
+
this.progress = snapshot.progress;
|
|
30
|
+
}
|
|
31
|
+
get snapshot() {
|
|
32
|
+
return {
|
|
33
|
+
id: this.id,
|
|
34
|
+
status: this.status,
|
|
35
|
+
reason: this.reason,
|
|
36
|
+
progress: this.progress,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
get terminal() {
|
|
40
|
+
return isTerminal(this.status);
|
|
41
|
+
}
|
|
42
|
+
refresh() {
|
|
43
|
+
return this.route.status.pipe(Effect.map((snapshot) => new Generation(this.route, this.token, snapshot)));
|
|
44
|
+
}
|
|
45
|
+
/** Fetch the result without polling; non-completed terminal generations fail with the provider's terminal body. */
|
|
46
|
+
result() {
|
|
47
|
+
return this.route.result;
|
|
48
|
+
}
|
|
49
|
+
/** Poll until the generation reaches a terminal status, then fetch the result. Fails with a `Timeout` reason on deadline. */
|
|
50
|
+
await(options) {
|
|
51
|
+
const timeout = Duration.fromInputUnsafe(options?.poll?.timeout ?? DEFAULT_POLL_TIMEOUT);
|
|
52
|
+
const settled = this.terminal ? Effect.succeed(this) : this.poll(options?.poll);
|
|
53
|
+
return settled.pipe(
|
|
54
|
+
// Non-completed terminal states also go through `result` so the route can surface its provider failure body.
|
|
55
|
+
Effect.flatMap((generation) => generation.result()), Effect.timeoutOrElse({ duration: timeout, orElse: () => timeoutError(this.id, timeout) }));
|
|
56
|
+
}
|
|
57
|
+
cancel() {
|
|
58
|
+
return this.route.cancel ?? Effect.void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Status observations as a stream, ending after the first terminal observation. Each poll and each sleep between polls
|
|
62
|
+
* is bounded by the time remaining until `poll.timeout`, so a hung status request or a long interval fails the stream at
|
|
63
|
+
* the deadline instead of stalling it. (`Stream.interruptWhen` would express this directly but deadlocks under
|
|
64
|
+
* `TestClock` when the source completes while the timer sleeps.)
|
|
65
|
+
*/
|
|
66
|
+
events(options) {
|
|
67
|
+
if (this.terminal)
|
|
68
|
+
return Stream.make(this.event());
|
|
69
|
+
const timeout = Duration.fromInputUnsafe(options?.poll?.timeout ?? DEFAULT_POLL_TIMEOUT);
|
|
70
|
+
return Stream.unwrap(Clock.currentTimeMillis.pipe(Effect.map((start) => {
|
|
71
|
+
const deadline = start + Duration.toMillis(timeout);
|
|
72
|
+
const refresh = within(this.refresh(), this.id, timeout, deadline);
|
|
73
|
+
const schedule = this.schedule(options?.poll).pipe(Schedule.modifyDelay((meta) => Effect.succeed(Duration.min(meta.duration, Duration.millis(Math.max(0, deadline - meta.now))))));
|
|
74
|
+
return Stream.fromEffectSchedule(refresh, schedule).pipe(Stream.takeUntil((generation) => generation.terminal), Stream.map((generation) => generation.event()));
|
|
75
|
+
})));
|
|
76
|
+
}
|
|
77
|
+
event() {
|
|
78
|
+
if (this.terminal)
|
|
79
|
+
return { type: "generation-finished", id: this.id, status: this.status, reason: this.reason };
|
|
80
|
+
return { type: "generation-progress", id: this.id, progress: this.progress };
|
|
81
|
+
}
|
|
82
|
+
poll(poll) {
|
|
83
|
+
return this.refresh().pipe(Effect.repeat({ schedule: this.schedule(poll), until: (generation) => generation.terminal }));
|
|
84
|
+
}
|
|
85
|
+
schedule(poll) {
|
|
86
|
+
return Schedule.spaced(poll?.interval ?? DEFAULT_POLL_INTERVAL);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** `events` followed by the expanded result, with the result fetch bounded by the same `poll.timeout` deadline. */
|
|
90
|
+
export const resultEvents = (generation, expand, options) => {
|
|
91
|
+
const timeout = Duration.fromInputUnsafe(options?.poll?.timeout ?? DEFAULT_POLL_TIMEOUT);
|
|
92
|
+
return Stream.unwrap(Clock.currentTimeMillis.pipe(Effect.map((start) => generation.events(options).pipe(Stream.filter((event) => event.type !== "generation-finished"), Stream.concat(Stream.fromIterableEffect(within(generation.result(), generation.id, timeout, start + Duration.toMillis(timeout)).pipe(Effect.map(expand))))))));
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Run `effect` within the time left until `deadline`. Fails before starting once the deadline has passed: a fast
|
|
96
|
+
* request could otherwise win the zero-budget race and schedule another zero-delay poll.
|
|
97
|
+
*/
|
|
98
|
+
const within = (effect, id, timeout, deadline) => Clock.currentTimeMillis.pipe(Effect.flatMap((now) => now >= deadline
|
|
99
|
+
? Effect.fail(timeoutError(id, timeout))
|
|
100
|
+
: effect.pipe(Effect.timeoutOrElse({
|
|
101
|
+
duration: Duration.millis(deadline - now),
|
|
102
|
+
orElse: () => Effect.fail(timeoutError(id, timeout)),
|
|
103
|
+
}))));
|
|
104
|
+
const timeoutError = (id, timeout) => new AIError({
|
|
105
|
+
reason: new TimeoutError({
|
|
106
|
+
message: `Generation ${id} did not finish within ${Duration.format(timeout)}`,
|
|
107
|
+
timeoutMs: Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, Math.ceil(Duration.toMillis(timeout)))),
|
|
108
|
+
}),
|
|
109
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { MediaClient } from "./media-client.js";
|
|
3
|
+
import type { MediaRoute } from "./route/media.js";
|
|
4
|
+
import { AIError } from "./schema/index.js";
|
|
5
|
+
import type { ImageEvent, ImageJob, ImageModel, ImageRequest, ImageResponse, ImageStatus } from "./image.js";
|
|
6
|
+
/** `start`, `status`, `result` and `cancel` fail with `UnsupportedOperation` on inline and stream routes. */
|
|
7
|
+
export interface Interface extends Pick<MediaClient.Interface<ImageRequest, ImageEvent, ImageResponse>, "generate" | "stream"> {
|
|
8
|
+
readonly start: (request: ImageRequest) => Effect.Effect<ImageJob, AIError>;
|
|
9
|
+
readonly status: (model: ImageModel, id: string) => Effect.Effect<ImageStatus, AIError>;
|
|
10
|
+
readonly result: (model: ImageModel, id: string) => Effect.Effect<ImageResponse, AIError>;
|
|
11
|
+
readonly cancel: (model: ImageModel, id: string) => Effect.Effect<void, AIError>;
|
|
12
|
+
}
|
|
13
|
+
export declare const make: (execute: MediaRoute.Execute) => Interface;
|
|
14
|
+
export * as ImageClient from "./image-client.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { MediaClient } from "./media-client.js";
|
|
3
|
+
import { AIError, UnsupportedOperationError } from "./schema/index.js";
|
|
4
|
+
const responseEvents = (response) => [
|
|
5
|
+
...response.images.map((image, index) => ({ type: "image", index, image })),
|
|
6
|
+
{ type: "finish", usage: response.usage, providerMetadata: response.providerMetadata },
|
|
7
|
+
];
|
|
8
|
+
// A queued image route whose token isn't a string is a programmer defect, not a provider error.
|
|
9
|
+
const decodeID = (token) => Schema.decodeUnknownEffect(Schema.String)(token).pipe(Effect.orDie);
|
|
10
|
+
export const make = (execute) => {
|
|
11
|
+
const media = MediaClient.operations(execute, {
|
|
12
|
+
modality: "image",
|
|
13
|
+
responseEvents,
|
|
14
|
+
});
|
|
15
|
+
const attach = (model, id, operation) => {
|
|
16
|
+
if (model.route.kind !== "queued")
|
|
17
|
+
return Effect.fail(MediaClient.notQueued("image", model.route, operation));
|
|
18
|
+
return model.route.attach(model, id, execute);
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
generate: media.generate,
|
|
22
|
+
stream: media.stream,
|
|
23
|
+
start: (request) => media.start(request).pipe(
|
|
24
|
+
// The token, not the snapshot id, is what `status` and `result` decode; `ImageModel.fromRoute` makes it a string.
|
|
25
|
+
Effect.flatMap((generation) => decodeID(generation.token)), Effect.map((id) => ({ id }))),
|
|
26
|
+
status: (model, id) => attach(model, id, "status").pipe(Effect.flatMap((route) => route.status), Effect.map((snapshot) => ({ status: snapshot.status, reason: snapshot.reason, progress: snapshot.progress }))),
|
|
27
|
+
result: (model, id) => attach(model, id, "result").pipe(Effect.flatMap((route) => route.result)),
|
|
28
|
+
cancel: (model, id) => attach(model, id, "cancel").pipe(Effect.flatMap((route) => route.cancel ??
|
|
29
|
+
Effect.fail(new AIError({
|
|
30
|
+
reason: new UnsupportedOperationError({
|
|
31
|
+
operation: "image.cancel",
|
|
32
|
+
provider: model.provider,
|
|
33
|
+
route: model.route.id,
|
|
34
|
+
message: `${model.provider}/${model.route.id} has no way to cancel an image generation`,
|
|
35
|
+
}),
|
|
36
|
+
})))),
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export * as ImageClient from "./image-client.js";
|