@zyro-sec/ai 0.0.0-stage → 1.0.1
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/AGENTS.md +23 -0
- package/README.md +1810 -2
- package/example/tutorial.ts +284 -0
- package/package.json +40 -4
- package/script/pack.ts +5 -0
- package/script/recording-cost-report.ts +250 -0
- package/script/setup-recording-env.ts +602 -0
- package/src/ai-client.ts +28 -0
- package/src/cache-policy.ts +249 -0
- package/src/decision-client.ts +88 -0
- package/src/decision.ts +335 -0
- package/src/effect.ts +44 -0
- package/src/effort-updates.ts +25 -0
- package/src/embedding-client.ts +46 -0
- package/src/embedding.ts +167 -0
- package/src/experimental/cloudflare-run-extraction.ts +112 -0
- package/src/experimental/extraction.ts +190 -0
- package/src/experimental/realtime.ts +211 -0
- package/src/experimental.ts +51 -0
- package/src/generation.ts +210 -0
- package/src/image-client.ts +89 -0
- package/src/image.ts +236 -0
- package/src/index.ts +363 -0
- package/src/llm.ts +364 -0
- package/src/loop.ts +214 -0
- package/src/media-client.ts +94 -0
- package/src/media-model.ts +129 -0
- package/src/media.ts +408 -0
- package/src/protocols/alibaba-chat.ts +95 -0
- package/src/protocols/alibaba-images.ts +126 -0
- package/src/protocols/alibaba-messages.ts +49 -0
- package/src/protocols/alibaba-responses.ts +86 -0
- package/src/protocols/anthropic-messages.ts +1664 -0
- package/src/protocols/assemblyai-transcription.ts +209 -0
- package/src/protocols/bedrock-converse.ts +984 -0
- package/src/protocols/bedrock-event-stream.ts +147 -0
- package/src/protocols/cartesia-speech.ts +182 -0
- package/src/protocols/cloudflare-run-decisions.ts +55 -0
- package/src/protocols/cloudflare-workers-ai-images.ts +124 -0
- package/src/protocols/cohere-chat.ts +353 -0
- package/src/protocols/deepgram-speech.ts +148 -0
- package/src/protocols/deepgram-transcription.ts +179 -0
- package/src/protocols/elevenlabs-speech.ts +207 -0
- package/src/protocols/elevenlabs-transcription.ts +211 -0
- package/src/protocols/gemini.ts +908 -0
- package/src/protocols/google-decisions.ts +179 -0
- package/src/protocols/google-embed-content.ts +165 -0
- package/src/protocols/google-images.ts +255 -0
- package/src/protocols/google-interactions-video.ts +264 -0
- package/src/protocols/google-interactions.ts +904 -0
- package/src/protocols/google-speech.ts +157 -0
- package/src/protocols/google-transcription.ts +206 -0
- package/src/protocols/google-vertex-predict-embeddings.ts +104 -0
- package/src/protocols/index.ts +21 -0
- package/src/protocols/meta-images.ts +162 -0
- package/src/protocols/meta-messages.ts +67 -0
- package/src/protocols/meta-responses.ts +249 -0
- package/src/protocols/minimax-images.ts +215 -0
- package/src/protocols/minimax-video.ts +406 -0
- package/src/protocols/mistral-chat.ts +768 -0
- package/src/protocols/open-responses-channel.ts +198 -0
- package/src/protocols/open-responses-continuation.ts +218 -0
- package/src/protocols/open-responses.ts +1636 -0
- package/src/protocols/openai-chat.ts +1431 -0
- package/src/protocols/openai-compatible-chat.ts +22 -0
- package/src/protocols/openai-compatible-responses.ts +21 -0
- package/src/protocols/openai-decisions.ts +174 -0
- package/src/protocols/openai-embeddings.ts +105 -0
- package/src/protocols/openai-freeform.ts +287 -0
- package/src/protocols/openai-images.ts +442 -0
- package/src/protocols/openai-responses.ts +442 -0
- package/src/protocols/openai-speech.ts +164 -0
- package/src/protocols/openai-transcription.ts +295 -0
- package/src/protocols/openrouter-video.ts +215 -0
- package/src/protocols/seedance-video.ts +301 -0
- package/src/protocols/shared.ts +552 -0
- package/src/protocols/system-one.ts +335 -0
- package/src/protocols/utils/bedrock-auth.ts +147 -0
- package/src/protocols/utils/bedrock-cache.ts +56 -0
- package/src/protocols/utils/bedrock-media.ts +121 -0
- package/src/protocols/utils/cache.ts +44 -0
- package/src/protocols/utils/claude-model.ts +19 -0
- package/src/protocols/utils/gateway-protocol.ts +92 -0
- package/src/protocols/utils/gemini-generate-content.ts +172 -0
- package/src/protocols/utils/gemini-json-schema.ts +77 -0
- package/src/protocols/utils/image-sizing.ts +117 -0
- package/src/protocols/utils/lifecycle.ts +120 -0
- package/src/protocols/utils/media-input.ts +98 -0
- package/src/protocols/utils/mistral-tool-id.ts +34 -0
- package/src/protocols/utils/open-responses-options.ts +82 -0
- package/src/protocols/utils/partial-json-options.ts +62 -0
- package/src/protocols/utils/partial-json.ts +283 -0
- package/src/protocols/utils/responses-checkpoint.ts +115 -0
- package/src/protocols/utils/responses-compaction.ts +210 -0
- package/src/protocols/utils/responses-hosted-tools.ts +104 -0
- package/src/protocols/utils/speaker-turns.ts +10 -0
- package/src/protocols/utils/speech-stream.ts +111 -0
- package/src/protocols/utils/tool-schema.ts +115 -0
- package/src/protocols/utils/tool-stream.ts +240 -0
- package/src/protocols/venice-chat.ts +356 -0
- package/src/protocols/xai-images.ts +137 -0
- package/src/protocols/xai-responses.ts +106 -0
- package/src/protocols/xai-video.ts +232 -0
- package/src/protocols/zai-chat.ts +71 -0
- package/src/protocols/zai-images.ts +120 -0
- package/src/protocols/zai-messages.ts +34 -0
- package/src/protocols.ts +1 -0
- package/src/provider-error.ts +291 -0
- package/src/provider-package.ts +32 -0
- package/src/providers/alibaba/chat.ts +1 -0
- package/src/providers/alibaba/embeddings.ts +2 -0
- package/src/providers/alibaba/images.ts +2 -0
- package/src/providers/alibaba/messages.ts +3 -0
- package/src/providers/alibaba/responses.ts +3 -0
- package/src/providers/alibaba.ts +177 -0
- package/src/providers/amazon-bedrock/mantle/chat.ts +2 -0
- package/src/providers/amazon-bedrock/mantle/messages.ts +2 -0
- package/src/providers/amazon-bedrock/mantle/responses.ts +2 -0
- package/src/providers/amazon-bedrock/mantle.ts +2 -0
- package/src/providers/amazon-bedrock-mantle.ts +192 -0
- package/src/providers/amazon-bedrock.ts +81 -0
- package/src/providers/anthropic-compatible.ts +80 -0
- package/src/providers/anthropic.ts +66 -0
- package/src/providers/assemblyai.ts +32 -0
- package/src/providers/azure/chat.ts +2 -0
- package/src/providers/azure/embeddings.ts +2 -0
- package/src/providers/azure/images.ts +2 -0
- package/src/providers/azure/messages.ts +2 -0
- package/src/providers/azure/responses.ts +2 -0
- package/src/providers/azure.ts +240 -0
- package/src/providers/baseten/decisions.ts +2 -0
- package/src/providers/baseten.ts +88 -0
- package/src/providers/byteplus/videos.ts +2 -0
- package/src/providers/byteplus.ts +39 -0
- package/src/providers/cartesia.ts +28 -0
- package/src/providers/cerebras.ts +56 -0
- package/src/providers/cloudflare-ai-gateway/decisions.ts +2 -0
- package/src/providers/cloudflare-ai-gateway.ts +199 -0
- package/src/providers/cloudflare-shared.ts +29 -0
- package/src/providers/cloudflare-workers-ai/decisions.ts +2 -0
- package/src/providers/cloudflare-workers-ai/images.ts +2 -0
- package/src/providers/cloudflare-workers-ai.ts +104 -0
- package/src/providers/cohere/chat.ts +8 -0
- package/src/providers/cohere/embeddings.ts +2 -0
- package/src/providers/cohere.ts +72 -0
- package/src/providers/deepgram.ts +33 -0
- package/src/providers/deepinfra/embeddings.ts +2 -0
- package/src/providers/deepinfra.ts +76 -0
- package/src/providers/deepseek.ts +54 -0
- package/src/providers/digitalocean/decisions.ts +2 -0
- package/src/providers/digitalocean/embeddings.ts +2 -0
- package/src/providers/digitalocean/images.ts +2 -0
- package/src/providers/digitalocean/responses.ts +4 -0
- package/src/providers/digitalocean.ts +144 -0
- package/src/providers/elevenlabs.ts +35 -0
- package/src/providers/fireworks/embeddings.ts +2 -0
- package/src/providers/fireworks.ts +66 -0
- package/src/providers/google/decisions.ts +2 -0
- package/src/providers/google/embeddings.ts +2 -0
- package/src/providers/google/images.ts +2 -0
- package/src/providers/google/interactions.ts +13 -0
- package/src/providers/google/videos.ts +2 -0
- package/src/providers/google-vertex/chat.ts +2 -0
- package/src/providers/google-vertex/embeddings.ts +2 -0
- package/src/providers/google-vertex/gemini.ts +2 -0
- package/src/providers/google-vertex/images.ts +2 -0
- package/src/providers/google-vertex/interactions.ts +2 -0
- package/src/providers/google-vertex/messages.ts +2 -0
- package/src/providers/google-vertex/mistral.ts +2 -0
- package/src/providers/google-vertex/responses.ts +2 -0
- package/src/providers/google-vertex-chat.ts +63 -0
- package/src/providers/google-vertex-interactions.ts +104 -0
- package/src/providers/google-vertex-messages.ts +99 -0
- package/src/providers/google-vertex-mistral.ts +79 -0
- package/src/providers/google-vertex-responses.ts +64 -0
- package/src/providers/google-vertex-shared.ts +176 -0
- package/src/providers/google-vertex.ts +228 -0
- package/src/providers/google.ts +121 -0
- package/src/providers/groq.ts +98 -0
- package/src/providers/index.ts +45 -0
- package/src/providers/meta/chat.ts +2 -0
- package/src/providers/meta/images.ts +2 -0
- package/src/providers/meta/messages.ts +2 -0
- package/src/providers/meta/responses.ts +2 -0
- package/src/providers/meta.ts +161 -0
- package/src/providers/minimax/chat.ts +8 -0
- package/src/providers/minimax/images.ts +4 -0
- package/src/providers/minimax/messages.ts +1 -0
- package/src/providers/minimax/responses.ts +8 -0
- package/src/providers/minimax/videos.ts +4 -0
- package/src/providers/minimax.ts +162 -0
- package/src/providers/mistral.ts +40 -0
- package/src/providers/moonshot/chat.ts +1 -0
- package/src/providers/moonshot/messages.ts +8 -0
- package/src/providers/moonshot/responses.ts +8 -0
- package/src/providers/moonshot.ts +134 -0
- package/src/providers/open-responses-options.ts +4 -0
- package/src/providers/openai/chat.ts +2 -0
- package/src/providers/openai/embeddings.ts +2 -0
- package/src/providers/openai/images.ts +2 -0
- package/src/providers/openai/responses.ts +2 -0
- package/src/providers/openai-compatible/decisions.ts +2 -0
- package/src/providers/openai-compatible/embeddings.ts +2 -0
- package/src/providers/openai-compatible/images.ts +2 -0
- package/src/providers/openai-compatible/responses.ts +1 -0
- package/src/providers/openai-compatible-responses.ts +52 -0
- package/src/providers/openai-compatible.ts +108 -0
- package/src/providers/openai-options.ts +66 -0
- package/src/providers/openai.ts +207 -0
- package/src/providers/opencode/decisions.ts +2 -0
- package/src/providers/opencode.ts +45 -0
- package/src/providers/openrouter/decisions.ts +2 -0
- package/src/providers/openrouter/embeddings.ts +2 -0
- package/src/providers/openrouter/images.ts +2 -0
- package/src/providers/openrouter/responses.ts +3 -0
- package/src/providers/openrouter/videos.ts +2 -0
- package/src/providers/openrouter.ts +347 -0
- package/src/providers/settings.ts +54 -0
- package/src/providers/togetherai.ts +53 -0
- package/src/providers/typesafe-ai/decisions.ts +2 -0
- package/src/providers/typesafe-ai.ts +45 -0
- package/src/providers/venice/responses.ts +4 -0
- package/src/providers/venice.ts +93 -0
- package/src/providers/vercel-ai-gateway/chat.ts +2 -0
- package/src/providers/vercel-ai-gateway/decisions.ts +2 -0
- package/src/providers/vercel-ai-gateway/embeddings.ts +2 -0
- package/src/providers/vercel-ai-gateway/images.ts +2 -0
- package/src/providers/vercel-ai-gateway/messages.ts +2 -0
- package/src/providers/vercel-ai-gateway/responses.ts +2 -0
- package/src/providers/vercel-ai-gateway.ts +300 -0
- package/src/providers/volcengine/videos.ts +2 -0
- package/src/providers/volcengine.ts +39 -0
- package/src/providers/xai/images.ts +2 -0
- package/src/providers/xai/videos.ts +2 -0
- package/src/providers/xai.ts +125 -0
- package/src/providers/zai/chat.ts +1 -0
- package/src/providers/zai/images.ts +2 -0
- package/src/providers/zai-coding-plan/chat.ts +1 -0
- package/src/providers/zai-coding-plan/messages.ts +8 -0
- package/src/providers/zai-coding-plan/responses.ts +8 -0
- package/src/providers/zai-coding-plan.ts +86 -0
- package/src/providers/zai.ts +73 -0
- package/src/providers.ts +1 -0
- package/src/public.ts +204 -0
- package/src/route/auth-options.ts +62 -0
- package/src/route/auth.ts +164 -0
- package/src/route/client.ts +700 -0
- package/src/route/decision.ts +150 -0
- package/src/route/embedding.ts +175 -0
- package/src/route/endpoint.ts +63 -0
- package/src/route/executor-service.ts +191 -0
- package/src/route/executor.ts +361 -0
- package/src/route/framing.ts +112 -0
- package/src/route/index.ts +47 -0
- package/src/route/media-protocol.ts +321 -0
- package/src/route/media.ts +507 -0
- package/src/route/protocol.ts +97 -0
- package/src/route/transport/http.ts +103 -0
- package/src/route/transport/index.ts +59 -0
- package/src/route/transport/websocket-channel.ts +51 -0
- package/src/route/transport/websocket.ts +443 -0
- package/src/route.ts +1 -0
- package/src/schema/errors.ts +246 -0
- package/src/schema/events.ts +744 -0
- package/src/schema/ids.ts +24 -0
- package/src/schema/index.ts +5 -0
- package/src/schema/messages.ts +529 -0
- package/src/schema/options.ts +445 -0
- package/src/speech-client.ts +25 -0
- package/src/speech.ts +211 -0
- package/src/testing.ts +218 -0
- package/src/tool-history.ts +91 -0
- package/src/tool-runtime.ts +107 -0
- package/src/tool-updates.ts +139 -0
- package/src/tool.ts +281 -0
- package/src/transcription-client.ts +19 -0
- package/src/transcription.ts +234 -0
- package/src/utils/bytes.ts +9 -0
- package/src/utils/json.ts +5 -0
- package/src/utils/media-type.ts +93 -0
- package/src/utils/provider-codes.ts +43 -0
- package/src/utils/record.ts +3 -0
- package/src/utils/sanitize.ts +33 -0
- package/src/video-client.ts +66 -0
- package/src/video.ts +181 -0
- package/test/adapter.test.ts +180 -0
- package/test/auth-options.types.ts +283 -0
- package/test/auth.test.ts +112 -0
- package/test/cache-policy.test.ts +578 -0
- package/test/compaction-http.test.ts +96 -0
- package/test/compaction.test.ts +107 -0
- package/test/compile.test.ts +600 -0
- package/test/continuation-scenarios.ts +115 -0
- package/test/decision.test.ts +983 -0
- package/test/decision.types.ts +117 -0
- package/test/effort-updates.test.ts +574 -0
- package/test/embedding.test.ts +455 -0
- package/test/endpoint.test.ts +58 -0
- package/test/error-partial.test.ts +140 -0
- package/test/executor.test.ts +1095 -0
- package/test/exports.test.ts +222 -0
- package/test/extraction.test.ts +227 -0
- package/test/fixtures/audio/dialog.mp3 +0 -0
- package/test/fixtures/audio/hello.mp3 +0 -0
- package/test/fixtures/images/edit-source.jpg +0 -0
- package/test/fixtures/media/restroom.png +0 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-flash-reads-image-bytes.json +34 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-returns-a-json-object.json +34 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-default-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-high-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-low-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-max-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-medium-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-minimal-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-none-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-chat/qwen-3-8-max-streams-xhigh-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-images/edits-an-image.json +33 -0
- package/test/fixtures/recordings/alibaba-images/generates-qwen-image-images.json +33 -0
- package/test/fixtures/recordings/alibaba-images/generates-wan-images.json +33 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-flash-reads-image-bytes.json +34 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-follows-a-json-schema.json +34 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-default-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-high-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-low-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-max-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-medium-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-messages/qwen-3-8-max-streams-xhigh-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-7-plus-streams-thinking-disabled.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-7-plus-streams-thinking-enabled.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-flash-reads-image-bytes.json +34 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-continues-a-stored-response.json +53 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-obeys-named-tool-choice.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-replays-reasoning-through-a-tool-loop-and-follow-up.json +73 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-default-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-high-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-low-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-max-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-medium-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-minimal-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-none-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-streams-xhigh-effort.json +36 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-uses-hosted-code-interpreter.json +35 -0
- package/test/fixtures/recordings/alibaba-responses/qwen-3-8-max-uses-hosted-web-search-and-extraction.json +36 -0
- package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-text.json +40 -0
- package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-tool-call.json +41 -0
- package/test/fixtures/recordings/anthropic-compatible-messages/minimax-m3-anthropic-compatible-tool-loop.json +60 -0
- package/test/fixtures/recordings/anthropic-messages/anthropic-opus-4-7-image-tool-result.json +43 -0
- package/test/fixtures/recordings/anthropic-messages-cache/keeps-a-long-tool-turn-inside-the-cache-lookback.json +48 -0
- package/test/fixtures/recordings/anthropic-messages-cache/writes-then-reads-cache-control-on-identical-second-call.json +48 -0
- package/test/fixtures/recordings/assemblyai-transcription/uploads-submits-and-polls-a-transcript.json +98 -0
- package/test/fixtures/recordings/azure/chat-streams-text-from-a-foundry-models-url.json +31 -0
- package/test/fixtures/recordings/azure/chat-streams-text.json +28 -0
- package/test/fixtures/recordings/azure/messages-calls-a-tool.json +31 -0
- package/test/fixtures/recordings/azure/messages-streams-text.json +31 -0
- package/test/fixtures/recordings/azure/responses-calls-a-tool.json +31 -0
- package/test/fixtures/recordings/azure/responses-continues-after-a-tool-result.json +31 -0
- package/test/fixtures/recordings/azure/responses-streams-text.json +31 -0
- package/test/fixtures/recordings/baseten-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/bedrock-converse/continues-after-parallel-tool-results.json +36 -0
- package/test/fixtures/recordings/bedrock-converse/drives-a-tool-loop.json +55 -0
- package/test/fixtures/recordings/bedrock-converse/streams-a-tool-call.json +29 -0
- package/test/fixtures/recordings/bedrock-converse/streams-text.json +29 -0
- package/test/fixtures/recordings/bedrock-converse-cache/writes-then-reads-cachepoint-on-identical-second-call.json +48 -0
- package/test/fixtures/recordings/bedrock-converse-options/constrains-claude-output-to-a-json-schema.json +33 -0
- package/test/fixtures/recordings/bedrock-converse-options/sends-claude-effort-with-adaptive-thinking-service-tier-and-request-metadata.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-options/sends-gpt-oss-reasoning-effort-with-the-priority-tier.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-options/sends-nova-2-reasoning-effort.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-thinking-binding/accepts-the-default-binding-with-no-thinking-configured.json +34 -0
- package/test/fixtures/recordings/bedrock-converse-thinking-binding/keeps-a-session-working-after-the-system-prompt-changes.json +71 -0
- package/test/fixtures/recordings/bedrock-mantle/streams-text.json +33 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/applies-mid-conversation-effort-updates-and-thinking-block-binding-on-opus-5-5.json +57 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/lowers-system-updates-to-wrapped-user-text-on-haiku-4-5-with-budget-thinking.json +37 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/replays-signed-thinking-through-a-tool-loop-and-native-system-update.json +59 -0
- package/test/fixtures/recordings/bedrock-mantle-messages/strips-unsupported-mid-conversation-effort-updates-on-opus-5-0.json +37 -0
- package/test/fixtures/recordings/cartesia-speech/generates-speech.json +34 -0
- package/test/fixtures/recordings/cartesia-speech/streams-speech-with-timestamps.json +33 -0
- package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-text.json +32 -0
- package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-tool-call.json +32 -0
- package/test/fixtures/recordings/cerebras-chat/cerebras-gpt-oss-120b-tool-loop.json +50 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-claude-haiku-4-5-text.json +38 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-claude-haiku-4-5-tool-loop.json +58 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-openai-gpt-5-nano-text.json +37 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-openai-gpt-5-nano-tool-loop.json +56 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway/cloudflare-ai-gateway-workers-ai-llama-3-1-8b-text.json +32 -0
- package/test/fixtures/recordings/cloudflare-ai-gateway-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-gpt-oss-20b-tools-tool-call.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai/cloudflare-workers-ai-llama-3-1-8b-text.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-extraction/extracts-invoice-fields.json +33 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-images/generates-a-flux-2-image-from-multipart-fields.json +32 -0
- package/test/fixtures/recordings/cloudflare-workers-ai-images/generates-a-raw-binary-image.json +33 -0
- package/test/fixtures/recordings/cohere/continues-a-native-tool-call.json +49 -0
- package/test/fixtures/recordings/cohere/streams-compatible-chat-reasoning.json +31 -0
- package/test/fixtures/recordings/cohere/streams-native-text-and-usage.json +31 -0
- package/test/fixtures/recordings/cohere/streams-native-thinking-with-a-budget.json +31 -0
- package/test/fixtures/recordings/cohere-embedding/embeds-text-through-the-compatibility-api.json +33 -0
- package/test/fixtures/recordings/deepgram-speech/generates-speech.json +36 -0
- package/test/fixtures/recordings/deepgram-speech/streams-speech.json +36 -0
- package/test/fixtures/recordings/deepgram-transcription/splits-utterances-at-speaker-changes.json +32 -0
- package/test/fixtures/recordings/deepgram-transcription/transcribes-raw-audio-with-speakers-and-word-timestamps.json +50 -0
- package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-text.json +32 -0
- package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-tool-call.json +32 -0
- package/test/fixtures/recordings/deepinfra-chat/deepinfra-llama-3-3-70b-tool-loop.json +50 -0
- package/test/fixtures/recordings/digitalocean-chat/gpt-nano-continues-a-tool-call-with-cache-markers.json +54 -0
- package/test/fixtures/recordings/digitalocean-chat/gpt-nano-reuses-a-cached-prompt.json +53 -0
- package/test/fixtures/recordings/digitalocean-chat/haiku-continues-a-tool-call-with-cache-markers.json +54 -0
- package/test/fixtures/recordings/digitalocean-chat/haiku-reuses-a-cached-prompt.json +53 -0
- package/test/fixtures/recordings/digitalocean-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/digitalocean-responses/calls-a-freeform-text-tool.json +35 -0
- package/test/fixtures/recordings/digitalocean-responses/continues-a-tool-call.json +53 -0
- package/test/fixtures/recordings/digitalocean-responses/streams-text.json +35 -0
- package/test/fixtures/recordings/elevenlabs-speech/generates-speech.json +35 -0
- package/test/fixtures/recordings/elevenlabs-speech/streams-speech-with-timestamps.json +34 -0
- package/test/fixtures/recordings/elevenlabs-transcription/groups-diarized-words-into-speaker-turns.json +32 -0
- package/test/fixtures/recordings/elevenlabs-transcription/transcribes-audio-with-word-timestamps.json +50 -0
- package/test/fixtures/recordings/gemini/gemini-2-5-flash-image.json +32 -0
- package/test/fixtures/recordings/gemini-cache/reports-cachedcontenttokencount-on-identical-second-call.json +46 -0
- package/test/fixtures/recordings/google-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/google-embedding/embeds-an-image-and-text-into-one-vector-each.json +32 -0
- package/test/fixtures/recordings/google-embedding/embeds-text-through-batchembedcontents.json +32 -0
- package/test/fixtures/recordings/google-embedding/maps-inputtype-to-a-task-type-for-gemini-embedding-001.json +50 -0
- package/test/fixtures/recordings/google-images/edits-an-image.json +28 -0
- package/test/fixtures/recordings/google-images/generates-an-image.json +28 -0
- package/test/fixtures/recordings/google-interactions/continues-tool-results-with-previous-interaction-id.json +66 -0
- package/test/fixtures/recordings/google-interactions/replays-native-tool-results-and-signatures-statelessly.json +50 -0
- package/test/fixtures/recordings/google-interactions/streams-reasoning-and-retains-thought-signatures.json +32 -0
- package/test/fixtures/recordings/google-interactions/streams-text-and-reports-usage.json +32 -0
- package/test/fixtures/recordings/google-interactions-video/generates-a-video.json +96 -0
- package/test/fixtures/recordings/google-speech/generates-speech.json +32 -0
- package/test/fixtures/recordings/google-speech/streams-speech.json +32 -0
- package/test/fixtures/recordings/google-transcription/streams-speaker-turns.json +32 -0
- package/test/fixtures/recordings/google-transcription/transcribes-with-speakers-and-word-timestamps.json +32 -0
- package/test/fixtures/recordings/google-vertex/calls-a-tool.json +32 -0
- package/test/fixtures/recordings/google-vertex/continues-after-a-tool-result.json +32 -0
- package/test/fixtures/recordings/google-vertex/streams-text.json +32 -0
- package/test/fixtures/recordings/google-vertex-embedding/embeds-gemini-embedding-001-one-text-per-predict-request.json +50 -0
- package/test/fixtures/recordings/google-vertex-embedding/embeds-text-through-predict.json +32 -0
- package/test/fixtures/recordings/groq-chat/continues-qwen-parallel-tool-calls.json +47 -0
- package/test/fixtures/recordings/groq-chat/replays-gpt-oss-reasoning-through-a-tool-loop.json +47 -0
- package/test/fixtures/recordings/groq-chat/streams-qwen-parsed-reasoning.json +29 -0
- package/test/fixtures/recordings/groq-chat/streams-text-with-usage.json +29 -0
- package/test/fixtures/recordings/meta-chat/continues-a-generated-tool-call.json +56 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-default-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-high-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-low-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-max-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-medium-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-minimal-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-chat/streams-text-with-xhigh-reasoning.json +29 -0
- package/test/fixtures/recordings/meta-images/edits-image-bytes-and-returns-png.json +29 -0
- package/test/fixtures/recordings/meta-images/generates-default-webp-bytes.json +29 -0
- package/test/fixtures/recordings/meta-images/replays-a-signed-image-handle-for-a-responses-edit.json +55 -0
- package/test/fixtures/recordings/meta-messages/replays-encrypted-thinking-through-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/meta-messages/streams-text-with-adaptive-thinking.json +36 -0
- package/test/fixtures/recordings/meta-messages/streams-text-with-enabled-thinking.json +36 -0
- package/test/fixtures/recordings/meta-responses/replays-encrypted-reasoning-through-a-tool-loop.json +56 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-default-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-high-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-low-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-max-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-medium-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-minimal-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-responses/streams-text-with-xhigh-reasoning.json +37 -0
- package/test/fixtures/recordings/meta-search/searches-and-replays-grounded-messages.json +56 -0
- package/test/fixtures/recordings/meta-search/searches-and-replays-grounded-responses.json +56 -0
- package/test/fixtures/recordings/minimax-chat/m3-continues-a-tool-loop-with-default-thinking.json +56 -0
- package/test/fixtures/recordings/minimax-chat/m3-streams-text-with-thinking-disabled.json +36 -0
- package/test/fixtures/recordings/minimax-images/generates-an-image.json +32 -0
- package/test/fixtures/recordings/minimax-images/generates-from-a-subject-reference.json +32 -0
- package/test/fixtures/recordings/minimax-messages/m2-7-streams-default-thinking.json +36 -0
- package/test/fixtures/recordings/minimax-messages/m3-continues-a-tool-loop-with-adaptive-thinking.json +56 -0
- package/test/fixtures/recordings/minimax-messages/m3-generates-a-named-tool-call-with-default-thinking-off.json +36 -0
- package/test/fixtures/recordings/minimax-messages/m3-streams-adaptive-thinking.json +36 -0
- package/test/fixtures/recordings/minimax-messages/m3-streams-text-with-thinking-disabled.json +36 -0
- package/test/fixtures/recordings/minimax-responses/m3-continues-a-tool-loop-with-effort-minimal.json +56 -0
- package/test/fixtures/recordings/minimax-responses/m3-streams-text-with-effort-none.json +36 -0
- package/test/fixtures/recordings/minimax-video/generates-a-hailuo-video-from-a-first-frame.json +176 -0
- package/test/fixtures/recordings/minimax-video/generates-an-h3-video.json +96 -0
- package/test/fixtures/recordings/mistral-chat/drives-a-tool-loop.json +54 -0
- package/test/fixtures/recordings/mistral-chat/replays-native-reasoning.json +47 -0
- package/test/fixtures/recordings/mistral-chat/streams-text-with-usage.json +29 -0
- package/test/fixtures/recordings/mistral-chat-glm/streams-an-indexed-tool-call.json +36 -0
- package/test/fixtures/recordings/moonshot-chat/k3-reads-image-bytes.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-respects-tool-choice-none.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-respects-tool-choice-required.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-returns-native-structured-output.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-default-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-high-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-low-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/k3-streams-text-with-max-effort.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-default-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-disabled-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-6-streams-text-with-enabled-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-highspeed-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-highspeed-streams-text-with-default-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k2-7-code-streams-text-with-default-thinking.json +29 -0
- package/test/fixtures/recordings/moonshot-chat/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-messages/k3-reads-image-bytes.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-respects-tool-choice-none.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-respects-tool-choice-required.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-returns-native-structured-output.json +35 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-default-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-high-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-low-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/k3-streams-text-with-max-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-messages/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/moonshot-responses/k3-reads-image-bytes.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-rejects-unsupported-tool-choice-none.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-rejects-unsupported-tool-choice-required.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-returns-native-structured-output.json +35 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-default-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-high-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-low-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/k3-streams-text-with-max-effort.json +36 -0
- package/test/fixtures/recordings/moonshot-responses/kimi-k3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +74 -0
- package/test/fixtures/recordings/openai-compatible-chat/gemini-parallel-tool-signatures.json +54 -0
- package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-text.json +28 -0
- package/test/fixtures/recordings/openai-compatible-chat/togetherai-streams-tool-call.json +28 -0
- package/test/fixtures/recordings/openai-decision/decides-choice-score-and-predicate-questions-about-json-state.json +33 -0
- package/test/fixtures/recordings/openai-embedding/embeds-text-with-requested-dimensions.json +33 -0
- package/test/fixtures/recordings/openai-images/edits-an-image.json +28 -0
- package/test/fixtures/recordings/openai-images/generates-an-image.json +28 -0
- package/test/fixtures/recordings/openai-images/streams-a-partial-image-before-the-final-image.json +32 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-image-tool-result.json +42 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning-continuation.json +58 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-reasoning.json +38 -0
- package/test/fixtures/recordings/openai-responses/openai-responses-gpt-5-5-tool-loop.json +57 -0
- package/test/fixtures/recordings/openai-responses-cache/caches-the-system-prompt-on-gpt-6-2-sol.json +51 -0
- package/test/fixtures/recordings/openai-responses-cache/reports-cached-tokens-on-identical-second-call.json +51 -0
- package/test/fixtures/recordings/openai-responses-images/generates-and-edits-an-image-with-the-hosted-tool.json +50 -0
- package/test/fixtures/recordings/openai-responses-websocket/continues-a-tool-call-over-one-socket.json +588 -0
- package/test/fixtures/recordings/openai-responses-websocket/reconstructs-full-context-after-reconnect.json +167 -0
- package/test/fixtures/recordings/openai-responses-websocket/recovers-from-explicit-continuation-rejection.json +167 -0
- package/test/fixtures/recordings/openai-speech/generates-speech.json +33 -0
- package/test/fixtures/recordings/openai-speech/streams-speech.json +32 -0
- package/test/fixtures/recordings/openai-transcription/diarizes-speakers-into-segments.json +32 -0
- package/test/fixtures/recordings/openai-transcription/returns-whisper-word-timestamps.json +32 -0
- package/test/fixtures/recordings/openai-transcription/streams-text-deltas.json +32 -0
- package/test/fixtures/recordings/openai-transcription/transcribes-with-a-language-hint.json +32 -0
- package/test/fixtures/recordings/opencode-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/openrouter-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/openrouter-images/edits-an-image-through-input-references.json +32 -0
- package/test/fixtures/recordings/openrouter-images/renders-flux-3-from-an-aspect-ratio-and-tier.json +32 -0
- package/test/fixtures/recordings/openrouter-images/renders-gemini-at-the-requested-2k-tier.json +32 -0
- package/test/fixtures/recordings/openrouter-reasoning-tool-loop.json +55 -0
- package/test/fixtures/recordings/openrouter-reasoning.json +29 -0
- package/test/fixtures/recordings/openrouter-responses/calls-a-freeform-text-tool.json +35 -0
- package/test/fixtures/recordings/openrouter-responses/continues-a-tool-call.json +53 -0
- package/test/fixtures/recordings/openrouter-responses/streams-text.json +35 -0
- package/test/fixtures/recordings/openrouter-video/generates-a-video.json +113 -0
- package/test/fixtures/recordings/pdf/anthropic-tool-result.json +28 -0
- package/test/fixtures/recordings/pdf/anthropic-user-input.json +28 -0
- package/test/fixtures/recordings/pdf/bedrock-tool-result.json +29 -0
- package/test/fixtures/recordings/pdf/bedrock-user-input.json +29 -0
- package/test/fixtures/recordings/pdf/gemini-tool-result.json +53 -0
- package/test/fixtures/recordings/pdf/gemini-user-input.json +28 -0
- package/test/fixtures/recordings/pdf/openai-tool-result.json +35 -0
- package/test/fixtures/recordings/pdf/openai-user-input.json +34 -0
- package/test/fixtures/recordings/pdf/xai-tool-result.json +35 -0
- package/test/fixtures/recordings/pdf/xai-user-input.json +34 -0
- package/test/fixtures/recordings/typesafe-decision/decides-choice-score-and-predicate-questions.json +33 -0
- package/test/fixtures/recordings/venice-chat/claude-opus-4-8-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/deepseek-v4-flash-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/gemini-3-1-pro-preview-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/gemini-preserves-per-call-signatures-through-parallel-tools.json +71 -0
- package/test/fixtures/recordings/venice-chat/openai-gpt-54-mini-preserves-reasoning-through-a-tool-loop-and-follow-up.json +75 -0
- package/test/fixtures/recordings/venice-chat/qwen-disables-reasoning.json +35 -0
- package/test/fixtures/recordings/venice-chat/surfaces-venice-model-errors.json +33 -0
- package/test/fixtures/recordings/venice-responses/calls-a-freeform-text-tool.json +35 -0
- package/test/fixtures/recordings/venice-responses/continues-a-tool-call.json +53 -0
- package/test/fixtures/recordings/venice-responses/streams-text.json +35 -0
- package/test/fixtures/recordings/vercel-ai-gateway-images/generates-an-image.json +32 -0
- package/test/fixtures/recordings/vercel-ai-gateway-images/sizes-grok-through-xai-provider-options.json +32 -0
- package/test/fixtures/recordings/vercel-ai-gateway-reasoning-tool-loop.json +55 -0
- package/test/fixtures/recordings/vercel-ai-gateway-reasoning.json +29 -0
- package/test/fixtures/recordings/vercel-native/automatically-caches-distinct-claude-system-blocks.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-chat-claude-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-chat-muse-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-messages-claude-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-messages-deepseek-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-messages-gemini-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-responses-gpt-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/continues-responses-grok-reasoning-and-tools.json +49 -0
- package/test/fixtures/recordings/vercel-native/rejects-invalid-credentials-without-retryable-classification.json +31 -0
- package/test/fixtures/recordings/xai-images/edits-an-image.json +28 -0
- package/test/fixtures/recordings/xai-images/generates-an-image.json +28 -0
- package/test/fixtures/recordings/xai-video/generates-a-video.json +129 -0
- package/test/fixtures/recordings/zai-chat/glm-4-5-calls-a-tool-without-streaming-arguments.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-4-7-streams-thinking-disabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-4-7-streams-thinking-enabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-high-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-low-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-max-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-medium-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-minimal-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-none-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-thinking-disabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-thinking-enabled.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-2-streams-xhigh-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-flash-reads-image-bytes.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-preserves-reasoning-through-a-tool-loop-and-follow-up.json +64 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-rejects-disabled-thinking.json +27 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-returns-a-json-object.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-default-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-high-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-low-effort.json +28 -0
- package/test/fixtures/recordings/zai-chat/glm-5-3-streams-max-effort.json +28 -0
- package/test/fixtures/recordings/zai-images/generates-an-image.json +28 -0
- package/test/generate-object.test.ts +281 -0
- package/test/generation.test.ts +191 -0
- package/test/google-interactions-video.test.ts +351 -0
- package/test/http-hooks.test.ts +137 -0
- package/test/http-timeout.test.ts +287 -0
- package/test/image-job.test.ts +199 -0
- package/test/image-sizing.test.ts +47 -0
- package/test/image.test.ts +1114 -0
- package/test/image.types.ts +203 -0
- package/test/keyless-auth.test.ts +85 -0
- package/test/lib/effect.ts +50 -0
- package/test/lib/env.ts +30 -0
- package/test/lib/http.ts +141 -0
- package/test/lib/image.ts +29 -0
- package/test/lib/openai-chunks.ts +27 -0
- package/test/lib/sse.ts +20 -0
- package/test/lib/tool-runtime.ts +152 -0
- package/test/llm-option-types.types.ts +153 -0
- package/test/llm.test.ts +286 -0
- package/test/loop-steps.test.ts +77 -0
- package/test/loop-tools.types.ts +79 -0
- package/test/loop.test.ts +425 -0
- package/test/media-input.test.ts +98 -0
- package/test/media-usage.test.ts +13 -0
- package/test/media.test.ts +340 -0
- package/test/minimax-video.test.ts +553 -0
- package/test/openai-freeform.test.ts +252 -0
- package/test/openrouter-video.test.ts +294 -0
- package/test/partial-json.test.ts +58 -0
- package/test/promise-parity.types.ts +83 -0
- package/test/promise.test.ts +632 -0
- package/test/provider/alibaba-capabilities.recorded.test.ts +253 -0
- package/test/provider/alibaba-images.recorded.test.ts +71 -0
- package/test/provider/alibaba-images.test.ts +199 -0
- package/test/provider/alibaba.recorded.test.ts +165 -0
- package/test/provider/alibaba.test.ts +419 -0
- package/test/provider/anthropic-compaction.test.ts +171 -0
- package/test/provider/anthropic-messages-cache.recorded.test.ts +111 -0
- package/test/provider/anthropic-messages.recorded.test.ts +45 -0
- package/test/provider/anthropic-messages.test.ts +2609 -0
- package/test/provider/anthropic-thinking-binding.test.ts +65 -0
- package/test/provider/assemblyai-transcription.recorded.test.ts +39 -0
- package/test/provider/azure.recorded.test.ts +139 -0
- package/test/provider/azure.test.ts +48 -0
- package/test/provider/bedrock-converse-cache.recorded.test.ts +61 -0
- package/test/provider/bedrock-converse-cache.test.ts +168 -0
- package/test/provider/bedrock-converse-options.recorded.test.ts +119 -0
- package/test/provider/bedrock-converse-options.test.ts +149 -0
- package/test/provider/bedrock-converse-thinking-binding.recorded.test.ts +103 -0
- package/test/provider/bedrock-converse-thinking-binding.test.ts +107 -0
- package/test/provider/bedrock-converse.test.ts +2548 -0
- package/test/provider/bedrock-mantle.test.ts +444 -0
- package/test/provider/bedrock-mistral-ids.test.ts +86 -0
- package/test/provider/cartesia-speech.recorded.test.ts +47 -0
- package/test/provider/checkpoint.test.ts +324 -0
- package/test/provider/cloudflare-workers-ai-images.recorded.test.ts +57 -0
- package/test/provider/cloudflare-workers-ai-images.test.ts +155 -0
- package/test/provider/cloudflare.test.ts +330 -0
- package/test/provider/cohere.recorded.test.ts +94 -0
- package/test/provider/cohere.test.ts +301 -0
- package/test/provider/compaction-websocket.test.ts +50 -0
- package/test/provider/compaction.recorded.test.ts +89 -0
- package/test/provider/compaction.test.ts +178 -0
- package/test/provider/conversation-lowering.test.ts +31 -0
- package/test/provider/decision.recorded.test.ts +211 -0
- package/test/provider/deepgram-speech.recorded.test.ts +41 -0
- package/test/provider/deepgram-transcription.recorded.test.ts +47 -0
- package/test/provider/digitalocean.recorded.test.ts +78 -0
- package/test/provider/digitalocean.test.ts +123 -0
- package/test/provider/elevenlabs-speech.recorded.test.ts +54 -0
- package/test/provider/elevenlabs-transcription.recorded.test.ts +50 -0
- package/test/provider/embedding.recorded.test.ts +109 -0
- package/test/provider/empty-tool-choice.test.ts +80 -0
- package/test/provider/error-retention.test.ts +158 -0
- package/test/provider/explicit-compaction.test.ts +503 -0
- package/test/provider/extraction.recorded.test.ts +46 -0
- package/test/provider/freeform-tools.test.ts +446 -0
- package/test/provider/gemini-cache.recorded.test.ts +49 -0
- package/test/provider/gemini-openai-chat.recorded.test.ts +69 -0
- package/test/provider/gemini.test.ts +2336 -0
- package/test/provider/golden.recorded.test.ts +287 -0
- package/test/provider/google-images.recorded.test.ts +53 -0
- package/test/provider/google-interactions-video.recorded.test.ts +48 -0
- package/test/provider/google-interactions.recorded.test.ts +169 -0
- package/test/provider/google-interactions.test.ts +593 -0
- package/test/provider/google-speech.recorded.test.ts +45 -0
- package/test/provider/google-transcription.recorded.test.ts +54 -0
- package/test/provider/google-vertex-interactions.test.ts +307 -0
- package/test/provider/google-vertex.recorded.test.ts +75 -0
- package/test/provider/google-vertex.test.ts +595 -0
- package/test/provider/groq.recorded.test.ts +187 -0
- package/test/provider/groq.test.ts +134 -0
- package/test/provider/image-detail.test.ts +58 -0
- package/test/provider/media-entrypoints.test.ts +618 -0
- package/test/provider/meta-capabilities.test.ts +216 -0
- package/test/provider/meta-images.recorded.test.ts +119 -0
- package/test/provider/meta-messages.recorded.test.ts +121 -0
- package/test/provider/meta-search.recorded.test.ts +97 -0
- package/test/provider/meta.recorded.test.ts +182 -0
- package/test/provider/meta.test.ts +140 -0
- package/test/provider/minimax-images.recorded.test.ts +52 -0
- package/test/provider/minimax-images.test.ts +236 -0
- package/test/provider/minimax-video.recorded.test.ts +83 -0
- package/test/provider/minimax.recorded.test.ts +257 -0
- package/test/provider/minimax.test.ts +110 -0
- package/test/provider/missing-tool-input.test.ts +73 -0
- package/test/provider/mistral-chat.test.ts +757 -0
- package/test/provider/mistral.recorded.test.ts +170 -0
- package/test/provider/moonshot-capabilities.recorded.test.ts +131 -0
- package/test/provider/moonshot.recorded.test.ts +191 -0
- package/test/provider/moonshot.test.ts +218 -0
- package/test/provider/native-providers.test.ts +299 -0
- package/test/provider/open-responses-errors.test.ts +141 -0
- package/test/provider/open-responses-finals.test.ts +209 -0
- package/test/provider/open-responses-lifecycle.test.ts +526 -0
- package/test/provider/open-responses-replay.test.ts +120 -0
- package/test/provider/openai-chat-reasoning.recorded.test.ts +154 -0
- package/test/provider/openai-chat.test.ts +2505 -0
- package/test/provider/openai-compatible-chat.test.ts +819 -0
- package/test/provider/openai-compatible-responses.test.ts +1139 -0
- package/test/provider/openai-images.recorded.test.ts +91 -0
- package/test/provider/openai-responses-cache.recorded.test.ts +68 -0
- package/test/provider/openai-responses-images.recorded.test.ts +66 -0
- package/test/provider/openai-responses-websocket.recorded.test.ts +221 -0
- package/test/provider/openai-responses.test.ts +5414 -0
- package/test/provider/openai-speech.recorded.test.ts +38 -0
- package/test/provider/openai-transcription.recorded.test.ts +91 -0
- package/test/provider/openrouter-images.recorded.test.ts +76 -0
- package/test/provider/openrouter-video.recorded.test.ts +53 -0
- package/test/provider/openrouter.test.ts +656 -0
- package/test/provider/pdf.recorded.test.ts +211 -0
- package/test/provider/queued-recording.ts +10 -0
- package/test/provider/responses-routes.recorded.test.ts +91 -0
- package/test/provider/seedance-video.recorded.test.ts +52 -0
- package/test/provider/speech-recording.ts +19 -0
- package/test/provider/transcription-recording.ts +41 -0
- package/test/provider/venice.recorded.test.ts +237 -0
- package/test/provider/venice.test.ts +373 -0
- package/test/provider/vercel-ai-gateway-images.recorded.test.ts +55 -0
- package/test/provider/vercel-ai-gateway.recorded.test.ts +125 -0
- package/test/provider/vercel-ai-gateway.test.ts +303 -0
- package/test/provider/xai-images.recorded.test.ts +54 -0
- package/test/provider/xai-images.test.ts +109 -0
- package/test/provider/xai-responses.test.ts +339 -0
- package/test/provider/xai-video.recorded.test.ts +54 -0
- package/test/provider/zai-capabilities.recorded.test.ts +156 -0
- package/test/provider/zai-images.recorded.test.ts +33 -0
- package/test/provider/zai-images.test.ts +163 -0
- package/test/provider/zai.recorded.test.ts +115 -0
- package/test/provider/zai.test.ts +374 -0
- package/test/provider-api-methods.types.ts +8 -0
- package/test/provider-error.test.ts +800 -0
- package/test/provider-model.types.ts +37 -0
- package/test/provider-options/alibaba.types.ts +46 -0
- package/test/provider-options/amazon-bedrock.types.ts +33 -0
- package/test/provider-options/anthropic-compatible.types.ts +13 -0
- package/test/provider-options/anthropic.types.ts +30 -0
- package/test/provider-options/azure.types.ts +13 -0
- package/test/provider-options/checkpoint.types.ts +150 -0
- package/test/provider-options/cloudflare.types.ts +13 -0
- package/test/provider-options/compaction.types.ts +146 -0
- package/test/provider-options/google-vertex-chat.types.ts +7 -0
- package/test/provider-options/google-vertex-interactions.types.ts +29 -0
- package/test/provider-options/google-vertex-messages.types.ts +13 -0
- package/test/provider-options/google-vertex-responses.types.ts +14 -0
- package/test/provider-options/google-vertex.types.ts +17 -0
- package/test/provider-options/google.types.ts +43 -0
- package/test/provider-options/minimax.types.ts +33 -0
- package/test/provider-options/mistral.types.ts +24 -0
- package/test/provider-options/moonshot.types.ts +45 -0
- package/test/provider-options/openai-compatible-responses.types.ts +17 -0
- package/test/provider-options/openai-compatible.types.ts +13 -0
- package/test/provider-options/openai.types.ts +27 -0
- package/test/provider-options/openrouter.types.ts +55 -0
- package/test/provider-options/venice.types.ts +39 -0
- package/test/provider-options/vercel-ai-gateway.types.ts +30 -0
- package/test/provider-options/xai.types.ts +14 -0
- package/test/provider-options/zai.types.ts +42 -0
- package/test/provider-package.test.ts +586 -0
- package/test/raw-events.test.ts +75 -0
- package/test/realtime.test.ts +172 -0
- package/test/recorded-golden.ts +104 -0
- package/test/recorded-runner.ts +101 -0
- package/test/recorded-scenarios.ts +468 -0
- package/test/recorded-test.ts +127 -0
- package/test/recorded-utils.ts +56 -0
- package/test/request-encoding.test.ts +57 -0
- package/test/request-transport.test.ts +74 -0
- package/test/response.test.ts +203 -0
- package/test/route.test.ts +225 -0
- package/test/sanitize.test.ts +80 -0
- package/test/schema.test.ts +412 -0
- package/test/seedance-video.test.ts +418 -0
- package/test/speech.test.ts +297 -0
- package/test/speech.types.ts +33 -0
- package/test/testing.test.ts +372 -0
- package/test/tool-history.test.ts +162 -0
- package/test/tool-runtime.test.ts +926 -0
- package/test/tool-schema-projection.test.ts +427 -0
- package/test/tool-stream.test.ts +252 -0
- package/test/tool-updates.test.ts +849 -0
- package/test/tool.types.ts +131 -0
- package/test/transcription.test.ts +618 -0
- package/test/transcription.types.ts +44 -0
- package/test/video.test.ts +447 -0
- package/test/video.types.ts +110 -0
- package/tsconfig.build.json +8 -0
- package/tsconfig.json +17 -0
- package/tsconfig.types.json +16 -0
package/src/tool.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { Effect, JsonSchema, Schema } from "effect"
|
|
2
|
+
import type { Content } from "@zyro-sec/schema/tool"
|
|
3
|
+
import type {
|
|
4
|
+
Freeform,
|
|
5
|
+
ToolCallPart,
|
|
6
|
+
ToolDefinition as ToolDefinitionClass,
|
|
7
|
+
ToolOutput as ToolOutputType,
|
|
8
|
+
} from "./schema/index.js"
|
|
9
|
+
import {
|
|
10
|
+
ToolDefinition,
|
|
11
|
+
ToolFailure,
|
|
12
|
+
ToolOutput,
|
|
13
|
+
type ToolDefinitionInput,
|
|
14
|
+
type ToolEntryInput,
|
|
15
|
+
} from "./schema/index.js"
|
|
16
|
+
import { isRecord } from "./utils/record.js"
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Schema constraint for tool input / success values: no decoding or
|
|
20
|
+
* encoding services are allowed. Tools should be self-contained — anything
|
|
21
|
+
* beyond pure data conversion belongs in the handler closure.
|
|
22
|
+
*/
|
|
23
|
+
export type ToolSchema<T> = Schema.Codec<T, any, never, never>
|
|
24
|
+
|
|
25
|
+
/** A tool's `input`: an Effect Schema decodes and types it; a raw JSON Schema leaves it `unknown`. */
|
|
26
|
+
export type ToolInput = ToolSchema<any> | JsonSchema.JsonSchema
|
|
27
|
+
|
|
28
|
+
/** The value `execute` receives for a tool `input`. */
|
|
29
|
+
export type ToolInputType<Input extends ToolInput> = Input extends ToolSchema<infer T> ? T : unknown
|
|
30
|
+
|
|
31
|
+
export interface ToolExecuteContext {
|
|
32
|
+
readonly id: ToolCallPart["id"]
|
|
33
|
+
readonly name: ToolCallPart["name"]
|
|
34
|
+
readonly namespace?: ToolCallPart["namespace"]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type ToolExecute<Input extends ToolInput, Success extends ToolSchema<any>> = (
|
|
38
|
+
input: ToolInputType<Input>,
|
|
39
|
+
context?: ToolExecuteContext,
|
|
40
|
+
) => Effect.Effect<Schema.Schema.Type<Success>, ToolFailure>
|
|
41
|
+
|
|
42
|
+
export interface ToolModelOutputInput<Input, Output> {
|
|
43
|
+
readonly id: ToolCallPart["id"]
|
|
44
|
+
readonly input: Input
|
|
45
|
+
readonly output: Output
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type ToolToModelOutput<Input extends ToolInput, Success extends ToolSchema<any>> = (
|
|
49
|
+
result: ToolModelOutputInput<ToolInputType<Input>, Success["Encoded"]>,
|
|
50
|
+
) => ReadonlyArray<Content>
|
|
51
|
+
|
|
52
|
+
type FreeformField<T> = {
|
|
53
|
+
[K in keyof T]-?: [Exclude<keyof T, K>] extends [never]
|
|
54
|
+
? {} extends Pick<T, K>
|
|
55
|
+
? never
|
|
56
|
+
: T[K] extends string
|
|
57
|
+
? K
|
|
58
|
+
: never
|
|
59
|
+
: never
|
|
60
|
+
}[keyof T]
|
|
61
|
+
|
|
62
|
+
type RequiredStringField<T> = {
|
|
63
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? never : T[K] extends string ? K : never
|
|
64
|
+
}[keyof T]
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* `Freeform` for exactly one required string field or unknown ones (a JSON Schema `input`), `Freeform` naming its
|
|
68
|
+
* `field` for a required string field among others, otherwise `undefined`.
|
|
69
|
+
*/
|
|
70
|
+
export type FreeformOption<Input extends ToolInput> =
|
|
71
|
+
unknown extends ToolInputType<Input>
|
|
72
|
+
? Freeform
|
|
73
|
+
: [FreeformField<ToolInputType<Input>>] extends [never]
|
|
74
|
+
? [RequiredStringField<ToolInputType<Input>>] extends [never]
|
|
75
|
+
? undefined
|
|
76
|
+
: Freeform & { readonly field: RequiredStringField<ToolInputType<Input>> }
|
|
77
|
+
: Freeform & { readonly field?: FreeformField<ToolInputType<Input>> }
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A type-safe LLM tool. Each tool bundles its own description, input
|
|
81
|
+
* Schema and success Schema. The execute handler is optional: omit it when you
|
|
82
|
+
* only want to expose a tool schema to the model and handle tool calls outside
|
|
83
|
+
* this package.
|
|
84
|
+
*
|
|
85
|
+
* Errors must be expressed as `ToolFailure`. Unmapped errors and defects fail
|
|
86
|
+
* the stream.
|
|
87
|
+
*
|
|
88
|
+
* Internally each tool also carries memoized codecs and a precomputed
|
|
89
|
+
* `ToolDefinition` so callers do not rebuild them per invocation.
|
|
90
|
+
*/
|
|
91
|
+
export interface Tool<Input extends ToolInput = any, Success extends ToolSchema<any> = any> {
|
|
92
|
+
/** The tool name the model sees and calls. */
|
|
93
|
+
readonly name: string
|
|
94
|
+
readonly description: string
|
|
95
|
+
readonly input: Input
|
|
96
|
+
readonly success: Success
|
|
97
|
+
readonly execute?: ToolExecute<Input, Success>
|
|
98
|
+
readonly toModelOutput?: ToolToModelOutput<Input, Success>
|
|
99
|
+
readonly toStructuredOutput?: (output: Success["Encoded"]) => unknown
|
|
100
|
+
/** @internal */
|
|
101
|
+
readonly _decode: (input: unknown) => Effect.Effect<ToolInputType<Input>, Schema.SchemaError>
|
|
102
|
+
// Method syntax keeps these parameters bivariant, so a concrete tool stays assignable to `ExecutableTool`.
|
|
103
|
+
/** @internal */
|
|
104
|
+
_encode(value: Schema.Schema.Type<Success>): Effect.Effect<unknown, Schema.SchemaError>
|
|
105
|
+
/** @internal */
|
|
106
|
+
_project(input: ToolInputType<Input>, id: ToolCallPart["id"], output: unknown): ToolOutputType
|
|
107
|
+
/** @internal */
|
|
108
|
+
readonly _legacyResult: boolean
|
|
109
|
+
/** @internal */
|
|
110
|
+
readonly _definition: ToolDefinitionClass
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Tool changes for `Message.tools`: `add` takes what a request's `tools` takes, `remove` the same tools or their names. */
|
|
114
|
+
export interface ToolChangesInput {
|
|
115
|
+
readonly add?: ReadonlyArray<Tool | ToolEntryInput>
|
|
116
|
+
readonly remove?: ReadonlyArray<Tool | ToolEntryInput | string>
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A tool with an `execute` handler, which `LLM.loop` and `ToolRuntime.dispatch` can run. */
|
|
120
|
+
export type ExecutableTool<Input extends ToolInput = any, Success extends ToolSchema<any> = any> = Tool<
|
|
121
|
+
Input,
|
|
122
|
+
Success
|
|
123
|
+
> & {
|
|
124
|
+
readonly execute: ToolExecute<Input, Success>
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Options for `tool`, where `Execute` is the only part that differs between the Effect and Promise clients. Pass an
|
|
129
|
+
* Effect Schema `input` (and optionally a `success` schema that validates the result, otherwise it is sent as is with
|
|
130
|
+
* `undefined` as `null`), or a raw JSON Schema `input` known only at runtime, such as an MCP tool's, with an optional
|
|
131
|
+
* JSON Schema `outputSchema`. Omit `execute` to declare a tool the model can call without running it here.
|
|
132
|
+
*/
|
|
133
|
+
export type ToolOptions<
|
|
134
|
+
Input extends ToolInput,
|
|
135
|
+
Success extends ToolSchema<any>,
|
|
136
|
+
Execute = ToolExecute<Input, Success>,
|
|
137
|
+
> = {
|
|
138
|
+
readonly name: string
|
|
139
|
+
readonly description: string
|
|
140
|
+
readonly input: Input
|
|
141
|
+
readonly freeform?: FreeformOption<Input>
|
|
142
|
+
readonly execute?: Execute
|
|
143
|
+
readonly toModelOutput?: ToolToModelOutput<Input, Success>
|
|
144
|
+
readonly toStructuredOutput?: (output: Success["Encoded"]) => unknown
|
|
145
|
+
} & (Input extends ToolSchema<any> ? { readonly success?: Success } : { readonly outputSchema?: JsonSchema.JsonSchema })
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Constructs a tool whose `execute` returns an Effect that fails with `ToolFailure`.
|
|
149
|
+
*
|
|
150
|
+
* ```ts
|
|
151
|
+
* tool({
|
|
152
|
+
* name: "weather",
|
|
153
|
+
* description: "Get current weather",
|
|
154
|
+
* input: Schema.Struct({ city: Schema.String }),
|
|
155
|
+
* success: Schema.Struct({ temperature: Schema.Number }),
|
|
156
|
+
* execute: ({ city }) => Effect.succeed({ temperature: 22 }),
|
|
157
|
+
* })
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
export const tool = <
|
|
161
|
+
Input extends ToolInput = typeof Schema.Unknown,
|
|
162
|
+
Success extends ToolSchema<any> = typeof Schema.Unknown,
|
|
163
|
+
Result extends Effect.Effect<Schema.Schema.Type<Success>, ToolFailure> | NoExecute = NoExecute,
|
|
164
|
+
>(
|
|
165
|
+
config: ToolOptions<Input, Success, (input: ToolInputType<Input>, context?: ToolExecuteContext) => Result>,
|
|
166
|
+
) => build(config, config.execute)
|
|
167
|
+
|
|
168
|
+
declare const noExecute: unique symbol
|
|
169
|
+
/** @internal The inferred `execute` result when `execute` is omitted; no function can return it. */
|
|
170
|
+
export type NoExecute = { readonly [noExecute]: never }
|
|
171
|
+
|
|
172
|
+
/** @internal Builds a tool for both clients from its options and an Effect `execute`. */
|
|
173
|
+
export function build<Input extends ToolInput, Success extends ToolSchema<any>, Result>(
|
|
174
|
+
config: ToolOptions<Input, Success, (...args: never) => Result>,
|
|
175
|
+
execute: ((input: ToolInputType<Input>, context?: ToolExecuteContext) => unknown) | undefined,
|
|
176
|
+
): 0 extends 1 & Result // `any`
|
|
177
|
+
? ExecutableTool<Input, Success>
|
|
178
|
+
: [Result, NoExecute] extends [NoExecute, Result]
|
|
179
|
+
? Tool<Input, Success>
|
|
180
|
+
: ExecutableTool<Input, Success>
|
|
181
|
+
export function build(
|
|
182
|
+
config: Omit<ToolOptions<ToolInput, ToolSchema<any>, unknown>, "success" | "outputSchema"> & {
|
|
183
|
+
readonly success?: ToolSchema<any>
|
|
184
|
+
readonly outputSchema?: JsonSchema.JsonSchema
|
|
185
|
+
},
|
|
186
|
+
execute: ((input: any, context?: ToolExecuteContext) => any) | undefined,
|
|
187
|
+
): Tool {
|
|
188
|
+
const shared = {
|
|
189
|
+
name: config.name,
|
|
190
|
+
description: config.description,
|
|
191
|
+
execute,
|
|
192
|
+
toModelOutput: config.toModelOutput,
|
|
193
|
+
toStructuredOutput: config.toStructuredOutput,
|
|
194
|
+
_project: (input: unknown, id: ToolCallPart["id"], output: unknown) =>
|
|
195
|
+
project(config.toModelOutput, config.toStructuredOutput, input, id, output),
|
|
196
|
+
}
|
|
197
|
+
const input = config.input
|
|
198
|
+
if (!Schema.isSchema(input))
|
|
199
|
+
return {
|
|
200
|
+
...shared,
|
|
201
|
+
input,
|
|
202
|
+
success: Schema.Unknown,
|
|
203
|
+
_decode: Effect.succeed,
|
|
204
|
+
_encode: Effect.succeed,
|
|
205
|
+
_legacyResult: config.toModelOutput === undefined && config.toStructuredOutput === undefined,
|
|
206
|
+
_definition: definition({
|
|
207
|
+
name: config.name,
|
|
208
|
+
description: config.description,
|
|
209
|
+
inputSchema: input,
|
|
210
|
+
outputSchema: config.outputSchema,
|
|
211
|
+
freeform: config.freeform,
|
|
212
|
+
}),
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
...shared,
|
|
216
|
+
input,
|
|
217
|
+
success: config.success ?? Schema.Unknown,
|
|
218
|
+
_decode: Schema.decodeUnknownEffect(input),
|
|
219
|
+
_encode: config.success ? Schema.encodeEffect(config.success) : (value) => Effect.succeed(value ?? null),
|
|
220
|
+
_legacyResult: false,
|
|
221
|
+
_definition: definition({
|
|
222
|
+
name: config.name,
|
|
223
|
+
description: config.description,
|
|
224
|
+
inputSchema: emptyInputJsonSchema(input) ?? toJsonSchema(input),
|
|
225
|
+
outputSchema: config.success && toJsonSchema(config.success),
|
|
226
|
+
freeform: config.freeform,
|
|
227
|
+
}),
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** The `ToolDefinition[]` that `LLMRequest.tools` expects, in array order. */
|
|
232
|
+
export const toDefinitions = (tools: ReadonlyArray<Tool>): ReadonlyArray<ToolDefinitionClass> =>
|
|
233
|
+
tools.map((item) => item._definition)
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Describes a no-argument Effect tool input as an empty object, or returns
|
|
237
|
+
* `undefined` when the input takes arguments.
|
|
238
|
+
*
|
|
239
|
+
* Effect emits `{ not: { type: "null" } }` only for an encoded struct with no
|
|
240
|
+
* properties or index signatures, but providers require tool parameters to
|
|
241
|
+
* describe an object. Annotations are kept; any other keyword, such as a
|
|
242
|
+
* check's `minProperties`, leaves the schema as Effect emitted it.
|
|
243
|
+
*/
|
|
244
|
+
export const emptyInputJsonSchema = (schema: Schema.Top): JsonSchema.JsonSchema | undefined => {
|
|
245
|
+
const { not, ...rest } = Schema.toJsonSchemaDocument(schema, { referencePolicy: () => undefined }).schema
|
|
246
|
+
if (!isRecord(not) || not.type !== "null" || Object.keys(not).length !== 1) return undefined
|
|
247
|
+
if (!Object.keys(rest).every((key) => annotationKeywords.has(key))) return undefined
|
|
248
|
+
return { ...rest, type: "object", properties: {}, additionalProperties: false }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const definition = (input: ToolDefinitionInput) => {
|
|
252
|
+
if (input.freeform !== undefined && ToolDefinition.freeformField(input.inputSchema, input.freeform) === undefined)
|
|
253
|
+
throw new Error(
|
|
254
|
+
input.freeform.field === undefined
|
|
255
|
+
? "A freeform tool needs exactly one parameter, a required string"
|
|
256
|
+
: `A freeform tool's \`${input.freeform.field}\` parameter must be a required string`,
|
|
257
|
+
)
|
|
258
|
+
return new ToolDefinition(input)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const annotationKeywords = new Set(["title", "description", "default", "examples", "readOnly", "writeOnly"])
|
|
262
|
+
|
|
263
|
+
const toJsonSchema = (schema: Schema.Top): JsonSchema.JsonSchema => {
|
|
264
|
+
const document = Schema.toJsonSchemaDocument(schema, { onExcessProperty: "error" })
|
|
265
|
+
if (Object.keys(document.definitions).length === 0) return document.schema
|
|
266
|
+
return { ...document.schema, $defs: document.definitions }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const project = (
|
|
270
|
+
toModelOutput: ((input: ToolModelOutputInput<any, any>) => ReadonlyArray<Content>) | undefined,
|
|
271
|
+
toStructuredOutput: ((output: unknown) => unknown) | undefined,
|
|
272
|
+
input: unknown,
|
|
273
|
+
id: ToolCallPart["id"],
|
|
274
|
+
output: unknown,
|
|
275
|
+
): ToolOutputType =>
|
|
276
|
+
ToolOutput.make(
|
|
277
|
+
toStructuredOutput?.(output) ?? output,
|
|
278
|
+
toModelOutput?.({ id, input, output }) ?? (typeof output === "string" ? [{ type: "text", text: output }] : []),
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
export { ToolFailure }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Context } from "effect"
|
|
2
|
+
import { MediaClient } from "./media-client.js"
|
|
3
|
+
import type { TranscriptionEvent, TranscriptionRequest, TranscriptionResponse } from "./transcription.js"
|
|
4
|
+
|
|
5
|
+
export type Interface = MediaClient.Interface<TranscriptionRequest, TranscriptionEvent, TranscriptionResponse>
|
|
6
|
+
|
|
7
|
+
export class TranscriptionClientService extends Context.Service<TranscriptionClientService, Interface>()(
|
|
8
|
+
"@zyro-sec/TranscriptionClient",
|
|
9
|
+
) {}
|
|
10
|
+
export const Service = TranscriptionClientService
|
|
11
|
+
export type Service = TranscriptionClientService
|
|
12
|
+
|
|
13
|
+
export const TranscriptionClient = {
|
|
14
|
+
Service,
|
|
15
|
+
...MediaClient.make(Service, {
|
|
16
|
+
modality: "transcription",
|
|
17
|
+
responseEvents: (response: TranscriptionResponse) => [{ type: "finish" as const, ...response }],
|
|
18
|
+
}),
|
|
19
|
+
} as const
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { Effect, Schema, Stream } from "effect"
|
|
2
|
+
import { Generation, ProgressEvent, type AwaitOptions } 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, type BaseRequest } from "./schema/index.js"
|
|
7
|
+
import { TranscriptionClient, Service } from "./transcription-client.js"
|
|
8
|
+
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Model
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
export type TranscriptionOptions = MediaModel.Options
|
|
14
|
+
|
|
15
|
+
export type TranscriptionRoute = MediaRoute.AnyRoute<TranscriptionRequest, TranscriptionEvent, TranscriptionResponse>
|
|
16
|
+
|
|
17
|
+
export class TranscriptionModel<Options extends TranscriptionOptions = TranscriptionOptions> extends MediaModel<
|
|
18
|
+
TranscriptionRoute,
|
|
19
|
+
Options
|
|
20
|
+
> {
|
|
21
|
+
declare protected readonly _TranscriptionModel: void
|
|
22
|
+
|
|
23
|
+
/** The number of type arguments selects the kind: `<Options>`, `<Options, Frame, State>`, or `<Options, Token>`. */
|
|
24
|
+
static fromRoute<Options extends TranscriptionOptions>(
|
|
25
|
+
route: MediaModel.InlineRouteInput<TranscriptionRequest<TranscriptionModel<Options>>, TranscriptionResponse>,
|
|
26
|
+
input: MediaRoute.ModelInput,
|
|
27
|
+
): TranscriptionModel<Options>
|
|
28
|
+
static fromRoute<Options extends TranscriptionOptions, Frame, State>(
|
|
29
|
+
route: MediaModel.StreamRouteInput<
|
|
30
|
+
TranscriptionRequest<TranscriptionModel<Options>>,
|
|
31
|
+
TranscriptionEvent,
|
|
32
|
+
Frame,
|
|
33
|
+
State
|
|
34
|
+
>,
|
|
35
|
+
input: MediaRoute.ModelInput,
|
|
36
|
+
): TranscriptionModel<Options>
|
|
37
|
+
static fromRoute<Options extends TranscriptionOptions, Token>(
|
|
38
|
+
route: MediaModel.QueuedRouteInput<TranscriptionRequest<TranscriptionModel<Options>>, TranscriptionResponse, Token>,
|
|
39
|
+
input: MediaRoute.ModelInput,
|
|
40
|
+
): TranscriptionModel<Options>
|
|
41
|
+
static fromRoute<Options extends TranscriptionOptions, Frame, State, Token>(
|
|
42
|
+
route: MediaModel.AnyRouteInput<
|
|
43
|
+
TranscriptionRequest<TranscriptionModel<Options>>,
|
|
44
|
+
TranscriptionEvent,
|
|
45
|
+
TranscriptionResponse,
|
|
46
|
+
Frame,
|
|
47
|
+
State,
|
|
48
|
+
Token
|
|
49
|
+
>,
|
|
50
|
+
input: MediaRoute.ModelInput,
|
|
51
|
+
) {
|
|
52
|
+
return new TranscriptionModel<Options>({
|
|
53
|
+
id: input.id,
|
|
54
|
+
provider: route.protocol.provider,
|
|
55
|
+
transport: input.transport,
|
|
56
|
+
providerOptions: input.providerOptions,
|
|
57
|
+
route: composeRoute(route, input, collectResponse) as TranscriptionRoute,
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const TranscriptionModelSchema = Schema.declare(
|
|
63
|
+
(value): value is TranscriptionModel => value instanceof TranscriptionModel,
|
|
64
|
+
{ expected: "Transcription.Model" },
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
// ---------------------------------------------------------------------------
|
|
68
|
+
// Request
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
|
|
71
|
+
export const TranscriptionTimestamps = Schema.Literals(["none", "segment", "word"])
|
|
72
|
+
export type TranscriptionTimestamps = Schema.Schema.Type<typeof TranscriptionTimestamps>
|
|
73
|
+
|
|
74
|
+
const TranscriptionRequestSchema = Schema.Struct({
|
|
75
|
+
model: TranscriptionModelSchema,
|
|
76
|
+
audio: Media.AssetSchema,
|
|
77
|
+
language: Schema.optional(Schema.String),
|
|
78
|
+
prompt: Schema.optional(Schema.String),
|
|
79
|
+
/** Routes that cannot produce the requested granularity fail typed; routes may return more than asked. */
|
|
80
|
+
timestamps: Schema.optional(TranscriptionTimestamps),
|
|
81
|
+
diarize: Schema.optional(Schema.Boolean),
|
|
82
|
+
speakers: Schema.optional(Schema.Int),
|
|
83
|
+
providerOptions: Schema.optional(ProviderOptions),
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
/** A normalized transcription request, the plain object `Transcription.request(...)` returns. */
|
|
87
|
+
export type TranscriptionRequest<Model extends TranscriptionModel = TranscriptionModel> = Omit<
|
|
88
|
+
typeof TranscriptionRequestSchema.Type,
|
|
89
|
+
"model" | "providerOptions"
|
|
90
|
+
> &
|
|
91
|
+
BaseRequest<Model, TranscriptionModelOptions<Model>>
|
|
92
|
+
|
|
93
|
+
export type TranscriptionModelOptions<Model> = Model extends TranscriptionModel<infer Options> ? Options : never
|
|
94
|
+
|
|
95
|
+
/** Input accepted by `Transcription.request` and the calls; a `TranscriptionRequest` is also a valid input. */
|
|
96
|
+
export type TranscriptionRequestInput<Model extends TranscriptionModel = TranscriptionModel> = Omit<
|
|
97
|
+
TranscriptionRequest<Model>,
|
|
98
|
+
"providerOptions"
|
|
99
|
+
> & {
|
|
100
|
+
readonly providerOptions?: NoInfer<TranscriptionModelOptions<Model>>
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// Response and events
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
/** Speaker labels are provider-native (`A`, `0`, `spk:0`, `speaker_0`, or a known speaker name). */
|
|
108
|
+
export const TranscriptionSegment = Schema.Struct({
|
|
109
|
+
text: Schema.String,
|
|
110
|
+
startSeconds: Schema.Number,
|
|
111
|
+
endSeconds: Schema.Number,
|
|
112
|
+
speaker: Schema.optional(Schema.String),
|
|
113
|
+
}).annotate({ identifier: "Transcription.Segment" })
|
|
114
|
+
export type TranscriptionSegment = Schema.Schema.Type<typeof TranscriptionSegment>
|
|
115
|
+
|
|
116
|
+
export const TranscriptionWord = Schema.Struct({
|
|
117
|
+
text: Schema.String,
|
|
118
|
+
startSeconds: Schema.Number,
|
|
119
|
+
endSeconds: Schema.Number,
|
|
120
|
+
speaker: Schema.optional(Schema.String),
|
|
121
|
+
confidence: Schema.optional(Schema.Number),
|
|
122
|
+
}).annotate({ identifier: "Transcription.Word" })
|
|
123
|
+
export type TranscriptionWord = Schema.Schema.Type<typeof TranscriptionWord>
|
|
124
|
+
|
|
125
|
+
const transcriptFields = {
|
|
126
|
+
text: Schema.String,
|
|
127
|
+
segments: Schema.optional(Schema.Array(TranscriptionSegment)),
|
|
128
|
+
words: Schema.optional(Schema.Array(TranscriptionWord)),
|
|
129
|
+
/** Provider-native language as detected or echoed (`en`, `english`, `en_us`), lowercased but not normalized. */
|
|
130
|
+
language: Schema.optional(Schema.String),
|
|
131
|
+
durationSeconds: Schema.optional(Schema.Number),
|
|
132
|
+
usage: Schema.optional(MediaUsage),
|
|
133
|
+
providerMetadata: Schema.optional(ProviderMetadata),
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export class TranscriptionResponse extends Schema.Class<TranscriptionResponse>("Transcription.Response")(
|
|
137
|
+
transcriptFields,
|
|
138
|
+
) {}
|
|
139
|
+
|
|
140
|
+
export const TranscriptionTextDeltaEvent = Schema.Struct({
|
|
141
|
+
type: Schema.tag("text-delta"),
|
|
142
|
+
delta: Schema.String,
|
|
143
|
+
}).annotate({ identifier: "Transcription.Event.TextDelta" })
|
|
144
|
+
|
|
145
|
+
export const TranscriptionSegmentEvent = Schema.Struct({
|
|
146
|
+
type: Schema.tag("segment"),
|
|
147
|
+
segment: TranscriptionSegment,
|
|
148
|
+
}).annotate({ identifier: "Transcription.Event.Segment" })
|
|
149
|
+
|
|
150
|
+
export const TranscriptionFinishEvent = Schema.Struct({
|
|
151
|
+
type: Schema.tag("finish"),
|
|
152
|
+
...transcriptFields,
|
|
153
|
+
}).annotate({ identifier: "Transcription.Event.Finish" })
|
|
154
|
+
|
|
155
|
+
const transcriptionEventTagged = Schema.Union([
|
|
156
|
+
ProgressEvent,
|
|
157
|
+
TranscriptionTextDeltaEvent,
|
|
158
|
+
TranscriptionSegmentEvent,
|
|
159
|
+
TranscriptionFinishEvent,
|
|
160
|
+
]).pipe(Schema.toTaggedUnion("type"))
|
|
161
|
+
export const TranscriptionEvent = Object.assign(transcriptionEventTagged, {
|
|
162
|
+
is: {
|
|
163
|
+
generationProgress: transcriptionEventTagged.guards["generation-progress"],
|
|
164
|
+
textDelta: transcriptionEventTagged.guards["text-delta"],
|
|
165
|
+
segment: transcriptionEventTagged.guards.segment,
|
|
166
|
+
finish: transcriptionEventTagged.guards.finish,
|
|
167
|
+
},
|
|
168
|
+
})
|
|
169
|
+
export type TranscriptionEvent = Schema.Schema.Type<typeof transcriptionEventTagged>
|
|
170
|
+
|
|
171
|
+
const collectResponse = (events: ReadonlyArray<TranscriptionEvent>): Effect.Effect<TranscriptionResponse> => {
|
|
172
|
+
const finish = events.find(TranscriptionEvent.is.finish)
|
|
173
|
+
// Every transcription protocol's `finish` emits the terminal event or fails, so a completed stream always has one.
|
|
174
|
+
if (finish === undefined) return Effect.die(new Error("The transcription stream completed without a finish event"))
|
|
175
|
+
const { type: _type, ...transcript } = finish
|
|
176
|
+
return Effect.succeed(new TranscriptionResponse(transcript))
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
// Request-shaped call API
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
/** Normalize transcription request input into a plain `TranscriptionRequest`. */
|
|
184
|
+
export const request = <const Model extends TranscriptionModel>(
|
|
185
|
+
input: TranscriptionRequestInput<Model>,
|
|
186
|
+
): TranscriptionRequest<Model> => ({
|
|
187
|
+
...input,
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
const validate = Schema.decodeUnknownSync(TranscriptionRequestSchema)
|
|
191
|
+
|
|
192
|
+
const requestEffect = (input: TranscriptionRequestInput) =>
|
|
193
|
+
tryRequest(() => {
|
|
194
|
+
const normalized = request(input)
|
|
195
|
+
validate(normalized)
|
|
196
|
+
return normalized
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
export function generate<const Model extends TranscriptionModel>(
|
|
200
|
+
input: TranscriptionRequestInput<Model>,
|
|
201
|
+
options?: AwaitOptions,
|
|
202
|
+
): Effect.Effect<TranscriptionResponse, AIError, Service>
|
|
203
|
+
export function generate(input: TranscriptionRequestInput, options?: AwaitOptions) {
|
|
204
|
+
return requestEffect(input).pipe(Effect.flatMap((request) => TranscriptionClient.generate(request, options)))
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export function stream<const Model extends TranscriptionModel>(
|
|
208
|
+
input: TranscriptionRequestInput<Model>,
|
|
209
|
+
options?: AwaitOptions,
|
|
210
|
+
): Stream.Stream<TranscriptionEvent, AIError, Service>
|
|
211
|
+
export function stream(input: TranscriptionRequestInput, options?: AwaitOptions) {
|
|
212
|
+
return Stream.unwrap(requestEffect(input).pipe(Effect.map((request) => TranscriptionClient.stream(request, options))))
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Inline and streaming routes fail with `UnsupportedOperation`. */
|
|
216
|
+
export function start<const Model extends TranscriptionModel>(
|
|
217
|
+
input: TranscriptionRequestInput<Model>,
|
|
218
|
+
): Effect.Effect<Generation<TranscriptionResponse>, AIError, Service>
|
|
219
|
+
export function start(input: TranscriptionRequestInput) {
|
|
220
|
+
return requestEffect(input).pipe(Effect.flatMap((request) => TranscriptionClient.start(request)))
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export const resume = (
|
|
224
|
+
model: TranscriptionModel,
|
|
225
|
+
token: unknown,
|
|
226
|
+
): Effect.Effect<Generation<TranscriptionResponse>, AIError, Service> => TranscriptionClient.resume(model, token)
|
|
227
|
+
|
|
228
|
+
export const Transcription = {
|
|
229
|
+
request,
|
|
230
|
+
generate,
|
|
231
|
+
stream,
|
|
232
|
+
start,
|
|
233
|
+
resume,
|
|
234
|
+
} as const
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const concatBytes = (chunks: ReadonlyArray<Uint8Array>) => {
|
|
2
|
+
if (chunks.length === 1) return chunks[0]
|
|
3
|
+
const bytes = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.length, 0))
|
|
4
|
+
chunks.reduce((offset, chunk) => {
|
|
5
|
+
bytes.set(chunk, offset)
|
|
6
|
+
return offset + chunk.length
|
|
7
|
+
}, 0)
|
|
8
|
+
return bytes
|
|
9
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const ascii = (bytes: Uint8Array, start: number, end: number) => String.fromCharCode(...bytes.subarray(start, end))
|
|
2
|
+
|
|
3
|
+
const startsWith = (bytes: Uint8Array, prefix: ReadonlyArray<number>) =>
|
|
4
|
+
bytes.length >= prefix.length && prefix.every((value, index) => bytes[index] === value)
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Sniff a media type from leading magic bytes. Covers the containers media routes commonly return; anything else is
|
|
8
|
+
* `undefined` so callers can fall back to a provider-declared type or `application/octet-stream`.
|
|
9
|
+
*/
|
|
10
|
+
export const detectMediaType = (bytes: Uint8Array): string | undefined => {
|
|
11
|
+
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png"
|
|
12
|
+
if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg"
|
|
13
|
+
if (startsWith(bytes, [0x47, 0x49, 0x46, 0x38])) return "image/gif"
|
|
14
|
+
if (bytes.length >= 12 && ascii(bytes, 0, 4) === "RIFF") {
|
|
15
|
+
const riffType = ascii(bytes, 8, 12)
|
|
16
|
+
if (riffType === "WEBP") return "image/webp"
|
|
17
|
+
if (riffType === "WAVE") return "audio/wav"
|
|
18
|
+
}
|
|
19
|
+
if (startsWith(bytes, [0x25, 0x50, 0x44, 0x46])) return "application/pdf"
|
|
20
|
+
if (bytes.length >= 12 && ascii(bytes, 4, 8) === "ftyp") {
|
|
21
|
+
// AVIF, HEIC, QuickTime and M4A share MP4's container; the major brand tells them apart, and generic HEIF brands
|
|
22
|
+
// list `avif`/`avis` among the compatible brands that follow the minor version.
|
|
23
|
+
const brand = ascii(bytes, 8, 12)
|
|
24
|
+
const boxEnd = Math.min(new DataView(bytes.buffer, bytes.byteOffset).getUint32(0), bytes.length)
|
|
25
|
+
const compatible = Array.from({ length: Math.floor((boxEnd - 16) / 4) }, (_, index) =>
|
|
26
|
+
ascii(bytes, 16 + index * 4, 20 + index * 4),
|
|
27
|
+
)
|
|
28
|
+
const generic = brand === "mif1" || brand === "msf1"
|
|
29
|
+
if (
|
|
30
|
+
brand === "avif" ||
|
|
31
|
+
brand === "avis" ||
|
|
32
|
+
(generic && (compatible.includes("avif") || compatible.includes("avis")))
|
|
33
|
+
)
|
|
34
|
+
return "image/avif"
|
|
35
|
+
if (generic || ["heic", "heix", "hevc", "hevx"].includes(brand)) return "image/heic"
|
|
36
|
+
if (brand === "qt ") return "video/quicktime"
|
|
37
|
+
if (brand === "M4A ") return "audio/mp4"
|
|
38
|
+
return "video/mp4"
|
|
39
|
+
}
|
|
40
|
+
if (startsWith(bytes, [0x1a, 0x45, 0xdf, 0xa3])) return "video/webm"
|
|
41
|
+
if (startsWith(bytes, [0x49, 0x44, 0x33])) return "audio/mpeg"
|
|
42
|
+
// An 11-bit frame sync; layer bits `00` mark AAC ADTS, any other layer is MPEG audio.
|
|
43
|
+
if (bytes.length >= 2 && bytes[0] === 0xff && (bytes[1] & 0xe0) === 0xe0)
|
|
44
|
+
return (bytes[1] & 0x06) === 0 ? "audio/aac" : "audio/mpeg"
|
|
45
|
+
if (startsWith(bytes, [0x4f, 0x67, 0x67, 0x53])) return "audio/ogg"
|
|
46
|
+
if (startsWith(bytes, [0x66, 0x4c, 0x61, 0x43])) return "audio/flac"
|
|
47
|
+
return undefined
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const EXTENSIONS: Readonly<Record<string, string>> = {
|
|
51
|
+
png: "image/png",
|
|
52
|
+
jpg: "image/jpeg",
|
|
53
|
+
jpeg: "image/jpeg",
|
|
54
|
+
gif: "image/gif",
|
|
55
|
+
webp: "image/webp",
|
|
56
|
+
avif: "image/avif",
|
|
57
|
+
heic: "image/heic",
|
|
58
|
+
heif: "image/heif",
|
|
59
|
+
pdf: "application/pdf",
|
|
60
|
+
mp4: "video/mp4",
|
|
61
|
+
mov: "video/quicktime",
|
|
62
|
+
webm: "video/webm",
|
|
63
|
+
mp3: "audio/mpeg",
|
|
64
|
+
m4a: "audio/mp4",
|
|
65
|
+
wav: "audio/wav",
|
|
66
|
+
ogg: "audio/ogg",
|
|
67
|
+
flac: "audio/flac",
|
|
68
|
+
aac: "audio/aac",
|
|
69
|
+
txt: "text/plain",
|
|
70
|
+
md: "text/markdown",
|
|
71
|
+
csv: "text/csv",
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const extensionMediaType = (path: string): string | undefined =>
|
|
75
|
+
EXTENSIONS[path.slice(path.lastIndexOf(".") + 1).toLowerCase()]
|
|
76
|
+
|
|
77
|
+
/** Media type of a file's contents: sniffed magic bytes, then the path's extension. */
|
|
78
|
+
export const fileMediaType = (bytes: Uint8Array, path: string) => detectMediaType(bytes) ?? extensionMediaType(path)
|
|
79
|
+
|
|
80
|
+
const EXTENSION_ALIASES: Readonly<Record<string, string>> = {
|
|
81
|
+
"audio/mp3": "mp3",
|
|
82
|
+
"audio/m4a": "m4a",
|
|
83
|
+
"audio/x-m4a": "m4a",
|
|
84
|
+
"audio/webm": "webm",
|
|
85
|
+
"audio/wave": "wav",
|
|
86
|
+
"audio/x-wav": "wav",
|
|
87
|
+
"audio/x-flac": "flac",
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export const mediaTypeExtension = (mediaType: string): string | undefined => {
|
|
91
|
+
const type = mediaType.split(";", 1)[0].trim().toLowerCase()
|
|
92
|
+
return EXTENSION_ALIASES[type] ?? Object.entries(EXTENSIONS).find(([, known]) => known === type)?.[0]
|
|
93
|
+
}
|