@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
package/dist/tool.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { Effect, JsonSchema, Schema, SchemaIssue } from "effect";
|
|
2
|
+
import { ToolFailure } from "./schema/errors.js";
|
|
3
|
+
import { ToolDefinition, ToolEntry, ToolOutput, } from "./schema/messages.js";
|
|
4
|
+
import { isRecord } from "./utils/record.js";
|
|
5
|
+
/**
|
|
6
|
+
* Constructs a tool whose `execute` returns an Effect that fails with `ToolFailure`.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* tool({
|
|
10
|
+
* name: "weather",
|
|
11
|
+
* description: "Get current weather",
|
|
12
|
+
* input: Schema.Struct({ city: Schema.String }),
|
|
13
|
+
* output: Schema.Struct({ temperature: Schema.Number }),
|
|
14
|
+
* execute: ({ city }) => Effect.succeed({ temperature: 22 }),
|
|
15
|
+
* })
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export const tool = (config) => build(config, config.execute);
|
|
19
|
+
export function build(config, execute) {
|
|
20
|
+
const result = {
|
|
21
|
+
name: config.name,
|
|
22
|
+
description: config.description,
|
|
23
|
+
input: config.input,
|
|
24
|
+
output: config.output,
|
|
25
|
+
execute,
|
|
26
|
+
toModelOutput: config.toModelOutput,
|
|
27
|
+
freeform: config.freeform,
|
|
28
|
+
};
|
|
29
|
+
// Compiling now rejects an invalid `freeform` or a schema without JSON Schema when the tool is built.
|
|
30
|
+
compiled(result);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
const compiledTools = new WeakMap();
|
|
34
|
+
/** @internal A tool's codecs and `ToolDefinition`, derived from its fields once per tool object. */
|
|
35
|
+
export const compiled = (tool) => {
|
|
36
|
+
const cached = compiledTools.get(tool);
|
|
37
|
+
if (cached)
|
|
38
|
+
return cached;
|
|
39
|
+
const next = compile(tool);
|
|
40
|
+
compiledTools.set(tool, next);
|
|
41
|
+
return next;
|
|
42
|
+
};
|
|
43
|
+
/** @internal Whether a request or `Message.tools` entry is a `Tool` rather than a tool definition or namespace. */
|
|
44
|
+
export const isTool = (item) => "input" in item;
|
|
45
|
+
/** @internal The wire entry for a `Tool` or tool entry input. */
|
|
46
|
+
export const toEntry = (item) => isTool(item) ? compiled(item).definition : ToolEntry.make(item);
|
|
47
|
+
/** @internal The `ToolDefinition[]` for `tools`, in array order. Public callers pass `Tool` values to `LLM.request`. */
|
|
48
|
+
export const toDefinitions = (tools) => tools.map((item) => compiled(item).definition);
|
|
49
|
+
/** @internal The `ToolOutput` for a tool's encoded result: `toModelOutput`'s content, or a string result as text. */
|
|
50
|
+
export const project = (tool, input, id, output) => ToolOutput.make(output, tool.toModelOutput?.({ id, input, output }) ?? (typeof output === "string" ? [{ type: "text", text: output }] : []));
|
|
51
|
+
const compile = (tool) => {
|
|
52
|
+
const input = tool.input;
|
|
53
|
+
const output = tool.output;
|
|
54
|
+
const outputSchema = Schema.isSchema(output)
|
|
55
|
+
? toJsonSchema(output)
|
|
56
|
+
: isStandardSchema(output)
|
|
57
|
+
? standardJsonSchema(tool.name, "output", output)
|
|
58
|
+
: output;
|
|
59
|
+
if (!Schema.isSchema(input) && !isStandardSchema(input))
|
|
60
|
+
return {
|
|
61
|
+
decode: Effect.succeed,
|
|
62
|
+
encode: Effect.succeed,
|
|
63
|
+
legacyResult: tool.toModelOutput === undefined,
|
|
64
|
+
definition: definition({
|
|
65
|
+
name: tool.name,
|
|
66
|
+
description: tool.description,
|
|
67
|
+
inputSchema: input,
|
|
68
|
+
outputSchema,
|
|
69
|
+
freeform: tool.freeform,
|
|
70
|
+
}),
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
decode: Schema.isSchema(input) ? Schema.decodeUnknownEffect(input) : validate(input),
|
|
74
|
+
encode: Schema.isSchema(output)
|
|
75
|
+
? Schema.encodeEffect(output)
|
|
76
|
+
: isStandardSchema(output)
|
|
77
|
+
? validate(output)
|
|
78
|
+
: (value) => Effect.succeed(value ?? null),
|
|
79
|
+
legacyResult: false,
|
|
80
|
+
definition: definition({
|
|
81
|
+
name: tool.name,
|
|
82
|
+
description: tool.description,
|
|
83
|
+
inputSchema: Schema.isSchema(input)
|
|
84
|
+
? (emptyInputJsonSchema(input) ?? toJsonSchema(input))
|
|
85
|
+
: standardJsonSchema(tool.name, "input", input),
|
|
86
|
+
outputSchema,
|
|
87
|
+
freeform: tool.freeform,
|
|
88
|
+
}),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Describes a no-argument Effect tool input as an empty object, or returns
|
|
93
|
+
* `undefined` when the input takes arguments.
|
|
94
|
+
*
|
|
95
|
+
* Effect emits `{ not: { type: "null" } }` only for an encoded struct with no
|
|
96
|
+
* properties or index signatures, but providers require tool parameters to
|
|
97
|
+
* describe an object. Annotations are kept; any other keyword, such as a
|
|
98
|
+
* check's `minProperties`, leaves the schema as Effect emitted it.
|
|
99
|
+
*/
|
|
100
|
+
export const emptyInputJsonSchema = (schema) => {
|
|
101
|
+
const { not, ...rest } = Schema.toJsonSchemaDocument(schema, { referencePolicy: () => undefined }).schema;
|
|
102
|
+
if (!isRecord(not) || not.type !== "null" || Object.keys(not).length !== 1)
|
|
103
|
+
return undefined;
|
|
104
|
+
if (!Object.keys(rest).every((key) => annotationKeywords.has(key)))
|
|
105
|
+
return undefined;
|
|
106
|
+
return { ...rest, type: "object", properties: {}, additionalProperties: false };
|
|
107
|
+
};
|
|
108
|
+
const definition = (input) => {
|
|
109
|
+
if (input.freeform !== undefined && ToolDefinition.freeformField(input.inputSchema, input.freeform) === undefined)
|
|
110
|
+
throw new Error(input.freeform.field === undefined
|
|
111
|
+
? "A freeform tool needs exactly one parameter, a required string"
|
|
112
|
+
: `A freeform tool's \`${input.freeform.field}\` parameter must be a required string`);
|
|
113
|
+
return new ToolDefinition(input);
|
|
114
|
+
};
|
|
115
|
+
const annotationKeywords = new Set(["title", "description", "default", "examples", "readOnly", "writeOnly"]);
|
|
116
|
+
const toJsonSchema = (schema) => {
|
|
117
|
+
const document = Schema.toJsonSchemaDocument(schema, { onExcessProperty: "error" });
|
|
118
|
+
if (Object.keys(document.definitions).length === 0)
|
|
119
|
+
return document.schema;
|
|
120
|
+
return { ...document.schema, $defs: document.definitions };
|
|
121
|
+
};
|
|
122
|
+
const isStandardSchema = (value) => (typeof value === "object" || typeof value === "function") && value !== null && "~standard" in value;
|
|
123
|
+
/**
|
|
124
|
+
* Validates with a Standard Schema, failing with the same `SchemaError` an Effect Schema decode does, also when the
|
|
125
|
+
* schema's own validation throws or rejects.
|
|
126
|
+
*/
|
|
127
|
+
const validate = (schema) => (value) => Effect.try({ try: () => schema["~standard"].validate(value), catch: thrownError }).pipe(Effect.flatMap((result) => result instanceof Promise
|
|
128
|
+
? Effect.tryPromise({ try: () => result, catch: thrownError })
|
|
129
|
+
: Effect.succeed(result)), Effect.flatMap((result) => result.issues === undefined
|
|
130
|
+
? Effect.succeed(result.value)
|
|
131
|
+
: Effect.fail(new Schema.SchemaError(new SchemaIssue.Composite(Schema.Unknown.ast, toIssues(result.issues), value)))));
|
|
132
|
+
const thrownError = (error) => new Schema.SchemaError(new SchemaIssue.InvalidValue({ message: error instanceof Error ? error.message : String(error) }));
|
|
133
|
+
const toIssues = (issues) => {
|
|
134
|
+
const [first = new SchemaIssue.InvalidValue({ message: "Invalid value" }), ...rest] = issues.map(toIssue);
|
|
135
|
+
return [first, ...rest];
|
|
136
|
+
};
|
|
137
|
+
const toIssue = (issue) => {
|
|
138
|
+
const leaf = new SchemaIssue.InvalidValue({ message: issue.message });
|
|
139
|
+
if (issue.path === undefined || issue.path.length === 0)
|
|
140
|
+
return leaf;
|
|
141
|
+
return new SchemaIssue.Pointer(issue.path.map((segment) => (typeof segment === "object" ? segment.key : segment)), leaf);
|
|
142
|
+
};
|
|
143
|
+
const hasJsonSchema = (schema) => "jsonSchema" in schema["~standard"];
|
|
144
|
+
const standardJsonSchema = (name, direction, schema) => {
|
|
145
|
+
if (!hasJsonSchema(schema))
|
|
146
|
+
throw new Error(`Tool \`${name}\`'s ${schema["~standard"].vendor} ${direction} schema can't produce JSON Schema. Use a schema that implements Standard JSON Schema (https://standardschema.dev/json-schema), such as a Valibot schema wrapped with \`toStandardJsonSchema\` from @valibot/to-json-schema.`);
|
|
147
|
+
const { $schema, ...rest } = convert(name, direction, schema);
|
|
148
|
+
return rest;
|
|
149
|
+
};
|
|
150
|
+
// Libraries throw for schemas they can't represent, such as a Zod transform, so name the tool in the error.
|
|
151
|
+
const convert = (name, direction, schema) => {
|
|
152
|
+
try {
|
|
153
|
+
return schema["~standard"].jsonSchema[direction]({ target: "draft-2020-12" });
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
throw new Error(`Tool \`${name}\`'s ${schema["~standard"].vendor} ${direction} schema can't be converted to JSON Schema: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
export { ToolFailure };
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { Effect, Schema, Stream } from "effect";
|
|
2
|
+
import { Generation, type AwaitOptions } from "./generation.js";
|
|
3
|
+
import { Media } from "./media.js";
|
|
4
|
+
import { MediaModel } from "./media-model.js";
|
|
5
|
+
import { MediaRoute } from "./route/media.js";
|
|
6
|
+
import { AIError, type BaseRequest } from "./schema/index.js";
|
|
7
|
+
import { AI } from "./ai.js";
|
|
8
|
+
export type TranscriptionOptions = MediaModel.Options;
|
|
9
|
+
export type TranscriptionRoute = MediaRoute.AnyRoute<TranscriptionRequest, TranscriptionEvent, TranscriptionResponse>;
|
|
10
|
+
export declare class TranscriptionModel<Options extends TranscriptionOptions = TranscriptionOptions> extends MediaModel<TranscriptionRoute, Options> {
|
|
11
|
+
protected readonly _TranscriptionModel: void;
|
|
12
|
+
/** The number of type arguments selects the kind: `<Options>`, `<Options, Frame, State>`, or `<Options, Token>`. */
|
|
13
|
+
static fromRoute<Options extends TranscriptionOptions>(route: MediaModel.InlineRouteInput<TranscriptionRequest<TranscriptionModel<Options>>, TranscriptionResponse>, input: MediaRoute.ModelInput): TranscriptionModel<Options>;
|
|
14
|
+
static fromRoute<Options extends TranscriptionOptions, Frame, State>(route: MediaModel.StreamRouteInput<TranscriptionRequest<TranscriptionModel<Options>>, TranscriptionEvent, Frame, State>, input: MediaRoute.ModelInput): TranscriptionModel<Options>;
|
|
15
|
+
static fromRoute<Options extends TranscriptionOptions, Token>(route: MediaModel.QueuedRouteInput<TranscriptionRequest<TranscriptionModel<Options>>, TranscriptionResponse, Token>, input: MediaRoute.ModelInput): TranscriptionModel<Options>;
|
|
16
|
+
}
|
|
17
|
+
export declare const TranscriptionModelSchema: Schema.declare<TranscriptionModel<{
|
|
18
|
+
readonly [x: string]: unknown;
|
|
19
|
+
}>, TranscriptionModel<{
|
|
20
|
+
readonly [x: string]: unknown;
|
|
21
|
+
}>>;
|
|
22
|
+
export declare const TranscriptionTimestamps: Schema.Literals<readonly ["none", "segment", "word"]>;
|
|
23
|
+
export type TranscriptionTimestamps = Schema.Schema.Type<typeof TranscriptionTimestamps>;
|
|
24
|
+
declare const TranscriptionRequestSchema: Schema.Struct<{
|
|
25
|
+
readonly model: Schema.declare<TranscriptionModel<{
|
|
26
|
+
readonly [x: string]: unknown;
|
|
27
|
+
}>, TranscriptionModel<{
|
|
28
|
+
readonly [x: string]: unknown;
|
|
29
|
+
}>>;
|
|
30
|
+
readonly audio: Schema.decodeTo<Schema.declare<Media.Asset, Media.Asset>, Schema.Struct<{
|
|
31
|
+
readonly source: Schema.Union<readonly [Schema.Struct<{
|
|
32
|
+
readonly type: Schema.Literal<"bytes">;
|
|
33
|
+
readonly data: Schema.Uint8Array;
|
|
34
|
+
readonly mediaType: Schema.String;
|
|
35
|
+
}>, Schema.Struct<{
|
|
36
|
+
readonly type: Schema.Literal<"base64">;
|
|
37
|
+
readonly data: Schema.String;
|
|
38
|
+
readonly mediaType: Schema.String;
|
|
39
|
+
}>, Schema.Struct<{
|
|
40
|
+
readonly type: Schema.Literal<"url">;
|
|
41
|
+
readonly url: Schema.String;
|
|
42
|
+
readonly mediaType: Schema.optional<Schema.String>;
|
|
43
|
+
readonly expiresAt: Schema.optional<Schema.Number>;
|
|
44
|
+
}>, Schema.Struct<{
|
|
45
|
+
readonly type: Schema.Literal<"ref">;
|
|
46
|
+
readonly provider: Schema.brand<Schema.String, "AI.ProviderID">;
|
|
47
|
+
readonly id: Schema.String;
|
|
48
|
+
readonly mediaType: Schema.optional<Schema.String>;
|
|
49
|
+
}>]>;
|
|
50
|
+
readonly info: Schema.optional<Schema.Struct<{
|
|
51
|
+
readonly width: Schema.optional<Schema.Number>;
|
|
52
|
+
readonly height: Schema.optional<Schema.Number>;
|
|
53
|
+
readonly durationSeconds: Schema.optional<Schema.Number>;
|
|
54
|
+
readonly sampleRate: Schema.optional<Schema.Number>;
|
|
55
|
+
readonly channels: Schema.optional<Schema.Number>;
|
|
56
|
+
readonly encoding: Schema.optional<Schema.String>;
|
|
57
|
+
readonly format: Schema.optional<Schema.String>;
|
|
58
|
+
}>>;
|
|
59
|
+
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
60
|
+
}>, never, never>;
|
|
61
|
+
readonly language: Schema.optional<Schema.String>;
|
|
62
|
+
readonly prompt: Schema.optional<Schema.String>;
|
|
63
|
+
/** Routes that cannot produce the requested granularity fail typed; routes may return more than asked. */
|
|
64
|
+
readonly timestamps: Schema.optional<Schema.Literals<readonly ["none", "segment", "word"]>>;
|
|
65
|
+
readonly diarize: Schema.optional<Schema.Boolean>;
|
|
66
|
+
readonly speakers: Schema.optional<Schema.Int>;
|
|
67
|
+
readonly providerOptions: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
68
|
+
}>;
|
|
69
|
+
/** A normalized transcription request, the plain object `Transcription.request(...)` returns. */
|
|
70
|
+
export type TranscriptionRequest<Model extends TranscriptionModel = TranscriptionModel> = Omit<typeof TranscriptionRequestSchema.Type, "model" | "providerOptions"> & BaseRequest<Model, TranscriptionModelOptions<Model>>;
|
|
71
|
+
export type TranscriptionModelOptions<Model> = Model extends TranscriptionModel<infer Options> ? Options : never;
|
|
72
|
+
/** Input accepted by `Transcription.request` and the calls; a `TranscriptionRequest` is also a valid input. */
|
|
73
|
+
export type TranscriptionRequestInput<Model extends TranscriptionModel = TranscriptionModel> = Omit<TranscriptionRequest<Model>, "providerOptions"> & {
|
|
74
|
+
readonly providerOptions?: NoInfer<TranscriptionModelOptions<Model>>;
|
|
75
|
+
};
|
|
76
|
+
/** Speaker labels are provider-native (`A`, `0`, `spk:0`, `speaker_0`, or a known speaker name). */
|
|
77
|
+
export declare const TranscriptionSegment: Schema.Struct<{
|
|
78
|
+
readonly text: Schema.String;
|
|
79
|
+
readonly startSeconds: Schema.Number;
|
|
80
|
+
readonly endSeconds: Schema.Number;
|
|
81
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
82
|
+
}>;
|
|
83
|
+
export type TranscriptionSegment = Schema.Schema.Type<typeof TranscriptionSegment>;
|
|
84
|
+
export declare const TranscriptionWord: Schema.Struct<{
|
|
85
|
+
readonly text: Schema.String;
|
|
86
|
+
readonly startSeconds: Schema.Number;
|
|
87
|
+
readonly endSeconds: Schema.Number;
|
|
88
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
89
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
90
|
+
}>;
|
|
91
|
+
export type TranscriptionWord = Schema.Schema.Type<typeof TranscriptionWord>;
|
|
92
|
+
declare const TranscriptionResponse_base: Schema.Class<TranscriptionResponse, Schema.Struct<{
|
|
93
|
+
text: Schema.String;
|
|
94
|
+
segments: Schema.optional<Schema.$Array<Schema.Struct<{
|
|
95
|
+
readonly text: Schema.String;
|
|
96
|
+
readonly startSeconds: Schema.Number;
|
|
97
|
+
readonly endSeconds: Schema.Number;
|
|
98
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
99
|
+
}>>>;
|
|
100
|
+
words: Schema.optional<Schema.$Array<Schema.Struct<{
|
|
101
|
+
readonly text: Schema.String;
|
|
102
|
+
readonly startSeconds: Schema.Number;
|
|
103
|
+
readonly endSeconds: Schema.Number;
|
|
104
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
105
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
106
|
+
}>>>;
|
|
107
|
+
/** Provider-native language as detected or echoed (`en`, `english`, `en_us`), lowercased but not normalized. */
|
|
108
|
+
language: Schema.optional<Schema.String>;
|
|
109
|
+
durationSeconds: Schema.optional<Schema.Number>;
|
|
110
|
+
usage: Schema.optional<Schema.Union<readonly [Schema.Struct<{
|
|
111
|
+
readonly type: Schema.Literal<"tokens">;
|
|
112
|
+
readonly input: Schema.optional<Schema.Number>;
|
|
113
|
+
readonly output: Schema.optional<Schema.Number>;
|
|
114
|
+
readonly total: Schema.optional<Schema.Number>;
|
|
115
|
+
readonly details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
116
|
+
}>, Schema.Struct<{
|
|
117
|
+
readonly type: Schema.Literal<"seconds">;
|
|
118
|
+
readonly seconds: Schema.Number;
|
|
119
|
+
}>, Schema.Struct<{
|
|
120
|
+
readonly type: Schema.Literal<"characters">;
|
|
121
|
+
readonly characters: Schema.Number;
|
|
122
|
+
}>, Schema.Struct<{
|
|
123
|
+
readonly type: Schema.Literal<"credits">;
|
|
124
|
+
readonly credits: Schema.Number;
|
|
125
|
+
}>, Schema.Struct<{
|
|
126
|
+
readonly type: Schema.Literal<"compute">;
|
|
127
|
+
readonly seconds: Schema.Number;
|
|
128
|
+
}>]>>;
|
|
129
|
+
providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
130
|
+
}>, {}>;
|
|
131
|
+
export declare class TranscriptionResponse extends TranscriptionResponse_base {
|
|
132
|
+
}
|
|
133
|
+
export declare const TranscriptionTextDeltaEvent: Schema.Struct<{
|
|
134
|
+
readonly type: Schema.tag<"text-delta">;
|
|
135
|
+
readonly delta: Schema.String;
|
|
136
|
+
}>;
|
|
137
|
+
export declare const TranscriptionSegmentEvent: Schema.Struct<{
|
|
138
|
+
readonly type: Schema.tag<"segment">;
|
|
139
|
+
readonly segment: Schema.Struct<{
|
|
140
|
+
readonly text: Schema.String;
|
|
141
|
+
readonly startSeconds: Schema.Number;
|
|
142
|
+
readonly endSeconds: Schema.Number;
|
|
143
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
144
|
+
}>;
|
|
145
|
+
}>;
|
|
146
|
+
export declare const TranscriptionFinishEvent: Schema.Struct<{
|
|
147
|
+
readonly text: Schema.String;
|
|
148
|
+
readonly segments: Schema.optional<Schema.$Array<Schema.Struct<{
|
|
149
|
+
readonly text: Schema.String;
|
|
150
|
+
readonly startSeconds: Schema.Number;
|
|
151
|
+
readonly endSeconds: Schema.Number;
|
|
152
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
153
|
+
}>>>;
|
|
154
|
+
readonly words: Schema.optional<Schema.$Array<Schema.Struct<{
|
|
155
|
+
readonly text: Schema.String;
|
|
156
|
+
readonly startSeconds: Schema.Number;
|
|
157
|
+
readonly endSeconds: Schema.Number;
|
|
158
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
159
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
160
|
+
}>>>;
|
|
161
|
+
readonly language: Schema.optional<Schema.String>;
|
|
162
|
+
readonly durationSeconds: Schema.optional<Schema.Number>;
|
|
163
|
+
readonly usage: Schema.optional<Schema.Union<readonly [Schema.Struct<{
|
|
164
|
+
readonly type: Schema.Literal<"tokens">;
|
|
165
|
+
readonly input: Schema.optional<Schema.Number>;
|
|
166
|
+
readonly output: Schema.optional<Schema.Number>;
|
|
167
|
+
readonly total: Schema.optional<Schema.Number>;
|
|
168
|
+
readonly details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
169
|
+
}>, Schema.Struct<{
|
|
170
|
+
readonly type: Schema.Literal<"seconds">;
|
|
171
|
+
readonly seconds: Schema.Number;
|
|
172
|
+
}>, Schema.Struct<{
|
|
173
|
+
readonly type: Schema.Literal<"characters">;
|
|
174
|
+
readonly characters: Schema.Number;
|
|
175
|
+
}>, Schema.Struct<{
|
|
176
|
+
readonly type: Schema.Literal<"credits">;
|
|
177
|
+
readonly credits: Schema.Number;
|
|
178
|
+
}>, Schema.Struct<{
|
|
179
|
+
readonly type: Schema.Literal<"compute">;
|
|
180
|
+
readonly seconds: Schema.Number;
|
|
181
|
+
}>]>>;
|
|
182
|
+
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
183
|
+
readonly type: Schema.tag<"finish">;
|
|
184
|
+
}>;
|
|
185
|
+
export declare const TranscriptionEvent: Schema.toTaggedUnion<"type", readonly [Schema.Struct<{
|
|
186
|
+
readonly type: Schema.tag<"generation-progress">;
|
|
187
|
+
readonly id: Schema.String;
|
|
188
|
+
readonly progress: Schema.optional<Schema.Number>;
|
|
189
|
+
}>, Schema.Struct<{
|
|
190
|
+
readonly type: Schema.tag<"text-delta">;
|
|
191
|
+
readonly delta: Schema.String;
|
|
192
|
+
}>, Schema.Struct<{
|
|
193
|
+
readonly type: Schema.tag<"segment">;
|
|
194
|
+
readonly segment: Schema.Struct<{
|
|
195
|
+
readonly text: Schema.String;
|
|
196
|
+
readonly startSeconds: Schema.Number;
|
|
197
|
+
readonly endSeconds: Schema.Number;
|
|
198
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
199
|
+
}>;
|
|
200
|
+
}>, Schema.Struct<{
|
|
201
|
+
readonly text: Schema.String;
|
|
202
|
+
readonly segments: Schema.optional<Schema.$Array<Schema.Struct<{
|
|
203
|
+
readonly text: Schema.String;
|
|
204
|
+
readonly startSeconds: Schema.Number;
|
|
205
|
+
readonly endSeconds: Schema.Number;
|
|
206
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
207
|
+
}>>>;
|
|
208
|
+
readonly words: Schema.optional<Schema.$Array<Schema.Struct<{
|
|
209
|
+
readonly text: Schema.String;
|
|
210
|
+
readonly startSeconds: Schema.Number;
|
|
211
|
+
readonly endSeconds: Schema.Number;
|
|
212
|
+
readonly speaker: Schema.optional<Schema.String>;
|
|
213
|
+
readonly confidence: Schema.optional<Schema.Number>;
|
|
214
|
+
}>>>;
|
|
215
|
+
readonly language: Schema.optional<Schema.String>;
|
|
216
|
+
readonly durationSeconds: Schema.optional<Schema.Number>;
|
|
217
|
+
readonly usage: Schema.optional<Schema.Union<readonly [Schema.Struct<{
|
|
218
|
+
readonly type: Schema.Literal<"tokens">;
|
|
219
|
+
readonly input: Schema.optional<Schema.Number>;
|
|
220
|
+
readonly output: Schema.optional<Schema.Number>;
|
|
221
|
+
readonly total: Schema.optional<Schema.Number>;
|
|
222
|
+
readonly details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
223
|
+
}>, Schema.Struct<{
|
|
224
|
+
readonly type: Schema.Literal<"seconds">;
|
|
225
|
+
readonly seconds: Schema.Number;
|
|
226
|
+
}>, Schema.Struct<{
|
|
227
|
+
readonly type: Schema.Literal<"characters">;
|
|
228
|
+
readonly characters: Schema.Number;
|
|
229
|
+
}>, Schema.Struct<{
|
|
230
|
+
readonly type: Schema.Literal<"credits">;
|
|
231
|
+
readonly credits: Schema.Number;
|
|
232
|
+
}>, Schema.Struct<{
|
|
233
|
+
readonly type: Schema.Literal<"compute">;
|
|
234
|
+
readonly seconds: Schema.Number;
|
|
235
|
+
}>]>>;
|
|
236
|
+
readonly providerMetadata: Schema.optional<Schema.$Record<Schema.String, Schema.$Record<Schema.String, Schema.Unknown>>>;
|
|
237
|
+
readonly type: Schema.tag<"finish">;
|
|
238
|
+
}>]>;
|
|
239
|
+
export type TranscriptionEvent = Schema.Schema.Type<typeof TranscriptionEvent>;
|
|
240
|
+
/** Normalize transcription request input into a plain `TranscriptionRequest`. */
|
|
241
|
+
export declare const request: <const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model>) => TranscriptionRequest<Model>;
|
|
242
|
+
export declare function generate<const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model>, options?: AwaitOptions): Effect.Effect<TranscriptionResponse, AIError, AI.Service>;
|
|
243
|
+
export declare function stream<const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model>, options?: AwaitOptions): Stream.Stream<TranscriptionEvent, AIError, AI.Service>;
|
|
244
|
+
/** Inline and streaming routes fail with `UnsupportedOperation`. */
|
|
245
|
+
export declare function start<const Model extends TranscriptionModel>(input: TranscriptionRequestInput<Model>): Effect.Effect<Generation<TranscriptionResponse>, AIError, AI.Service>;
|
|
246
|
+
export declare const resume: (model: TranscriptionModel, token: unknown) => Effect.Effect<Generation<TranscriptionResponse>, AIError, AI.Service>;
|
|
247
|
+
export declare const Transcription: {
|
|
248
|
+
readonly request: typeof request;
|
|
249
|
+
readonly generate: typeof generate;
|
|
250
|
+
readonly stream: typeof stream;
|
|
251
|
+
readonly start: typeof start;
|
|
252
|
+
readonly resume: typeof resume;
|
|
253
|
+
};
|
|
254
|
+
export {};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Effect, Schema, Stream } from "effect";
|
|
2
|
+
import { Generation, ProgressEvent } from "./generation.js";
|
|
3
|
+
import { Media } from "./media.js";
|
|
4
|
+
import { MediaModel, composeRoute, tryRequest } from "./media-model.js";
|
|
5
|
+
import { MediaRoute } from "./route/media.js";
|
|
6
|
+
import { AIError, MediaUsage, ProviderMetadata, ProviderOptions } from "./schema/index.js";
|
|
7
|
+
import { AI } from "./ai.js";
|
|
8
|
+
export class TranscriptionModel extends MediaModel {
|
|
9
|
+
static fromRoute(route, input) {
|
|
10
|
+
return new TranscriptionModel({
|
|
11
|
+
id: input.id,
|
|
12
|
+
provider: route.protocol.provider,
|
|
13
|
+
transport: input.transport,
|
|
14
|
+
providerOptions: input.providerOptions,
|
|
15
|
+
route: composeRoute(route, input, collectResponse),
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export const TranscriptionModelSchema = Schema.declare((value) => value instanceof TranscriptionModel, { expected: "Transcription.Model" });
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Request
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
export const TranscriptionTimestamps = Schema.Literals(["none", "segment", "word"]);
|
|
24
|
+
const TranscriptionRequestSchema = Schema.Struct({
|
|
25
|
+
model: TranscriptionModelSchema,
|
|
26
|
+
audio: Media.AssetSchema,
|
|
27
|
+
language: Schema.optional(Schema.String),
|
|
28
|
+
prompt: Schema.optional(Schema.String),
|
|
29
|
+
/** Routes that cannot produce the requested granularity fail typed; routes may return more than asked. */
|
|
30
|
+
timestamps: Schema.optional(TranscriptionTimestamps),
|
|
31
|
+
diarize: Schema.optional(Schema.Boolean),
|
|
32
|
+
speakers: Schema.optional(Schema.Int),
|
|
33
|
+
providerOptions: Schema.optional(ProviderOptions),
|
|
34
|
+
});
|
|
35
|
+
// ---------------------------------------------------------------------------
|
|
36
|
+
// Response and events
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
/** Speaker labels are provider-native (`A`, `0`, `spk:0`, `speaker_0`, or a known speaker name). */
|
|
39
|
+
export const TranscriptionSegment = Schema.Struct({
|
|
40
|
+
text: Schema.String,
|
|
41
|
+
startSeconds: Schema.Number,
|
|
42
|
+
endSeconds: Schema.Number,
|
|
43
|
+
speaker: Schema.optional(Schema.String),
|
|
44
|
+
}).annotate({ identifier: "Transcription.Segment" });
|
|
45
|
+
export const TranscriptionWord = Schema.Struct({
|
|
46
|
+
text: Schema.String,
|
|
47
|
+
startSeconds: Schema.Number,
|
|
48
|
+
endSeconds: Schema.Number,
|
|
49
|
+
speaker: Schema.optional(Schema.String),
|
|
50
|
+
confidence: Schema.optional(Schema.Number),
|
|
51
|
+
}).annotate({ identifier: "Transcription.Word" });
|
|
52
|
+
const transcriptFields = {
|
|
53
|
+
text: Schema.String,
|
|
54
|
+
segments: Schema.optional(Schema.Array(TranscriptionSegment)),
|
|
55
|
+
words: Schema.optional(Schema.Array(TranscriptionWord)),
|
|
56
|
+
/** Provider-native language as detected or echoed (`en`, `english`, `en_us`), lowercased but not normalized. */
|
|
57
|
+
language: Schema.optional(Schema.String),
|
|
58
|
+
durationSeconds: Schema.optional(Schema.Number),
|
|
59
|
+
usage: Schema.optional(MediaUsage),
|
|
60
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
61
|
+
};
|
|
62
|
+
export class TranscriptionResponse extends Schema.Class("Transcription.Response")(transcriptFields) {
|
|
63
|
+
}
|
|
64
|
+
export const TranscriptionTextDeltaEvent = Schema.Struct({
|
|
65
|
+
type: Schema.tag("text-delta"),
|
|
66
|
+
delta: Schema.String,
|
|
67
|
+
}).annotate({ identifier: "Transcription.Event.TextDelta" });
|
|
68
|
+
export const TranscriptionSegmentEvent = Schema.Struct({
|
|
69
|
+
type: Schema.tag("segment"),
|
|
70
|
+
segment: TranscriptionSegment,
|
|
71
|
+
}).annotate({ identifier: "Transcription.Event.Segment" });
|
|
72
|
+
export const TranscriptionFinishEvent = Schema.Struct({
|
|
73
|
+
type: Schema.tag("finish"),
|
|
74
|
+
...transcriptFields,
|
|
75
|
+
}).annotate({ identifier: "Transcription.Event.Finish" });
|
|
76
|
+
export const TranscriptionEvent = Schema.Union([
|
|
77
|
+
ProgressEvent,
|
|
78
|
+
TranscriptionTextDeltaEvent,
|
|
79
|
+
TranscriptionSegmentEvent,
|
|
80
|
+
TranscriptionFinishEvent,
|
|
81
|
+
]).pipe(Schema.toTaggedUnion("type"));
|
|
82
|
+
const collectResponse = (events) => {
|
|
83
|
+
const finish = events.find((event) => event.type === "finish");
|
|
84
|
+
// Every transcription protocol's `finish` emits the terminal event or fails, so a completed stream always has one.
|
|
85
|
+
if (finish === undefined)
|
|
86
|
+
return Effect.die(new Error("The transcription stream completed without a finish event"));
|
|
87
|
+
const { type: _type, ...transcript } = finish;
|
|
88
|
+
return Effect.succeed(new TranscriptionResponse(transcript));
|
|
89
|
+
};
|
|
90
|
+
// ---------------------------------------------------------------------------
|
|
91
|
+
// Request-shaped call API
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
/** Normalize transcription request input into a plain `TranscriptionRequest`. */
|
|
94
|
+
export const request = (input) => ({
|
|
95
|
+
...input,
|
|
96
|
+
});
|
|
97
|
+
const validate = Schema.decodeUnknownSync(TranscriptionRequestSchema);
|
|
98
|
+
const requestEffect = (input) => tryRequest(() => {
|
|
99
|
+
const normalized = request(input);
|
|
100
|
+
validate(normalized);
|
|
101
|
+
return normalized;
|
|
102
|
+
});
|
|
103
|
+
export function generate(input, options) {
|
|
104
|
+
return requestEffect(input).pipe(Effect.flatMap((request) => AI.Service.use((ai) => ai.transcription.generate(request, options))));
|
|
105
|
+
}
|
|
106
|
+
export function stream(input, options) {
|
|
107
|
+
return Stream.unwrap(requestEffect(input).pipe(Effect.flatMap((request) => AI.Service.useSync((ai) => ai.transcription.stream(request, options)))));
|
|
108
|
+
}
|
|
109
|
+
export function start(input) {
|
|
110
|
+
return requestEffect(input).pipe(Effect.flatMap((request) => AI.Service.use((ai) => ai.transcription.start(request))));
|
|
111
|
+
}
|
|
112
|
+
export const resume = (model, token) => AI.Service.use((ai) => ai.transcription.resume(model, token));
|
|
113
|
+
export const Transcription = {
|
|
114
|
+
request,
|
|
115
|
+
generate,
|
|
116
|
+
stream,
|
|
117
|
+
start,
|
|
118
|
+
resume,
|
|
119
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const concatBytes: (chunks: ReadonlyArray<Uint8Array>) => Uint8Array<ArrayBufferLike>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export const concatBytes = (chunks) => {
|
|
2
|
+
if (chunks.length === 1)
|
|
3
|
+
return chunks[0];
|
|
4
|
+
const bytes = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0));
|
|
5
|
+
chunks.reduce((offset, chunk) => {
|
|
6
|
+
bytes.set(chunk, offset);
|
|
7
|
+
return offset + chunk.length;
|
|
8
|
+
}, 0);
|
|
9
|
+
return bytes;
|
|
10
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
export declare const Json: Schema.fromJsonString<Schema.Unknown>;
|
|
3
|
+
export declare const decodeJson: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => unknown;
|
|
4
|
+
export declare const encodeJson: (input: unknown, options?: import("effect/SchemaAST").ParseOptions) => string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sniff a media type from leading magic bytes. Covers the containers media routes commonly return; anything else is
|
|
3
|
+
* `undefined` so callers can fall back to a provider-declared type or `application/octet-stream`.
|
|
4
|
+
*/
|
|
5
|
+
export declare const detectMediaType: (bytes: Uint8Array) => string | undefined;
|
|
6
|
+
/** Media type of a file's contents: sniffed magic bytes, then the path's extension. */
|
|
7
|
+
export declare const fileMediaType: (bytes: Uint8Array, path: string) => string | undefined;
|
|
8
|
+
export declare const mediaTypeExtension: (mediaType: string) => string | undefined;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const ascii = (bytes, start, end) => String.fromCharCode(...bytes.subarray(start, end));
|
|
2
|
+
const startsWith = (bytes, prefix) => bytes.length >= prefix.length && prefix.every((value, index) => bytes[index] === value);
|
|
3
|
+
/**
|
|
4
|
+
* Sniff a media type from leading magic bytes. Covers the containers media routes commonly return; anything else is
|
|
5
|
+
* `undefined` so callers can fall back to a provider-declared type or `application/octet-stream`.
|
|
6
|
+
*/
|
|
7
|
+
export const detectMediaType = (bytes) => {
|
|
8
|
+
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
|
|
9
|
+
return "image/png";
|
|
10
|
+
if (startsWith(bytes, [0xff, 0xd8, 0xff]))
|
|
11
|
+
return "image/jpeg";
|
|
12
|
+
if (startsWith(bytes, [0x47, 0x49, 0x46, 0x38]))
|
|
13
|
+
return "image/gif";
|
|
14
|
+
if (bytes.length >= 12 && ascii(bytes, 0, 4) === "RIFF") {
|
|
15
|
+
const riffType = ascii(bytes, 8, 12);
|
|
16
|
+
if (riffType === "WEBP")
|
|
17
|
+
return "image/webp";
|
|
18
|
+
if (riffType === "WAVE")
|
|
19
|
+
return "audio/wav";
|
|
20
|
+
}
|
|
21
|
+
if (startsWith(bytes, [0x25, 0x50, 0x44, 0x46]))
|
|
22
|
+
return "application/pdf";
|
|
23
|
+
if (bytes.length >= 12 && ascii(bytes, 4, 8) === "ftyp") {
|
|
24
|
+
// AVIF, HEIC, QuickTime and M4A share MP4's container; the major brand tells them apart, and generic HEIF brands
|
|
25
|
+
// list `avif`/`avis` among the compatible brands that follow the minor version.
|
|
26
|
+
const brand = ascii(bytes, 8, 12);
|
|
27
|
+
const boxEnd = Math.min(new DataView(bytes.buffer, bytes.byteOffset).getUint32(0), bytes.length);
|
|
28
|
+
const compatible = Array.from({ length: Math.floor((boxEnd - 16) / 4) }, (_, index) => ascii(bytes, 16 + index * 4, 20 + index * 4));
|
|
29
|
+
const generic = brand === "mif1" || brand === "msf1";
|
|
30
|
+
if (brand === "avif" ||
|
|
31
|
+
brand === "avis" ||
|
|
32
|
+
(generic && (compatible.includes("avif") || compatible.includes("avis"))))
|
|
33
|
+
return "image/avif";
|
|
34
|
+
if (generic || ["heic", "heix", "hevc", "hevx"].includes(brand))
|
|
35
|
+
return "image/heic";
|
|
36
|
+
if (brand === "qt ")
|
|
37
|
+
return "video/quicktime";
|
|
38
|
+
if (brand === "M4A ")
|
|
39
|
+
return "audio/mp4";
|
|
40
|
+
return "video/mp4";
|
|
41
|
+
}
|
|
42
|
+
if (startsWith(bytes, [0x1a, 0x45, 0xdf, 0xa3]))
|
|
43
|
+
return "video/webm";
|
|
44
|
+
if (startsWith(bytes, [0x49, 0x44, 0x33]))
|
|
45
|
+
return "audio/mpeg";
|
|
46
|
+
// An 11-bit frame sync; layer bits `00` mark AAC ADTS, any other layer is MPEG audio.
|
|
47
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0)
|
|
48
|
+
return (bytes[1] & 0x06) === 0 ? "audio/aac" : "audio/mpeg";
|
|
49
|
+
if (startsWith(bytes, [0x4f, 0x67, 0x67, 0x53]))
|
|
50
|
+
return "audio/ogg";
|
|
51
|
+
if (startsWith(bytes, [0x66, 0x4c, 0x61, 0x43]))
|
|
52
|
+
return "audio/flac";
|
|
53
|
+
return undefined;
|
|
54
|
+
};
|
|
55
|
+
const EXTENSIONS = {
|
|
56
|
+
png: "image/png",
|
|
57
|
+
jpg: "image/jpeg",
|
|
58
|
+
jpeg: "image/jpeg",
|
|
59
|
+
gif: "image/gif",
|
|
60
|
+
webp: "image/webp",
|
|
61
|
+
avif: "image/avif",
|
|
62
|
+
heic: "image/heic",
|
|
63
|
+
heif: "image/heif",
|
|
64
|
+
pdf: "application/pdf",
|
|
65
|
+
mp4: "video/mp4",
|
|
66
|
+
mov: "video/quicktime",
|
|
67
|
+
webm: "video/webm",
|
|
68
|
+
mp3: "audio/mpeg",
|
|
69
|
+
m4a: "audio/mp4",
|
|
70
|
+
wav: "audio/wav",
|
|
71
|
+
ogg: "audio/ogg",
|
|
72
|
+
flac: "audio/flac",
|
|
73
|
+
aac: "audio/aac",
|
|
74
|
+
txt: "text/plain",
|
|
75
|
+
md: "text/markdown",
|
|
76
|
+
csv: "text/csv",
|
|
77
|
+
};
|
|
78
|
+
const extensionMediaType = (path) => EXTENSIONS[path.slice(path.lastIndexOf(".") + 1).toLowerCase()];
|
|
79
|
+
/** Media type of a file's contents: sniffed magic bytes, then the path's extension. */
|
|
80
|
+
export const fileMediaType = (bytes, path) => detectMediaType(bytes) ?? extensionMediaType(path);
|
|
81
|
+
const EXTENSION_ALIASES = {
|
|
82
|
+
"audio/mp3": "mp3",
|
|
83
|
+
"audio/m4a": "m4a",
|
|
84
|
+
"audio/x-m4a": "m4a",
|
|
85
|
+
"audio/webm": "webm",
|
|
86
|
+
"audio/wave": "wav",
|
|
87
|
+
"audio/x-wav": "wav",
|
|
88
|
+
"audio/x-flac": "flac",
|
|
89
|
+
};
|
|
90
|
+
export const mediaTypeExtension = (mediaType) => {
|
|
91
|
+
const type = mediaType.split(";", 1)[0].trim().toLowerCase();
|
|
92
|
+
return EXTENSION_ALIASES[type] ?? Object.entries(EXTENSIONS).find(([, known]) => known === type)?.[0];
|
|
93
|
+
};
|