@radheim/ai 0.0.0-stage → 2.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1890 -3
- package/dist/ai.d.ts +36 -0
- package/dist/ai.js +46 -0
- package/dist/cache-policy.d.ts +6 -0
- package/dist/cache-policy.js +205 -0
- package/dist/decision-client.d.ts +10 -0
- package/dist/decision-client.js +42 -0
- package/dist/decision.d.ts +378 -0
- package/dist/decision.js +171 -0
- package/dist/effect.d.ts +59 -0
- package/dist/effect.js +33 -0
- package/dist/effort-updates.d.ts +12 -0
- package/dist/effort-updates.js +23 -0
- package/dist/embedding-client.d.ts +10 -0
- package/dist/embedding-client.js +17 -0
- package/dist/embedding.d.ts +113 -0
- package/dist/embedding.js +76 -0
- package/dist/experimental/cloudflare-run-extraction.d.ts +17 -0
- package/dist/experimental/cloudflare-run-extraction.js +85 -0
- package/dist/experimental/extraction.d.ts +153 -0
- package/dist/experimental/extraction.js +103 -0
- package/dist/experimental/realtime.d.ts +80 -0
- package/dist/experimental/realtime.js +96 -0
- package/dist/experimental.d.ts +10 -0
- package/dist/experimental.js +6 -0
- package/dist/fetch-middleware.d.ts +7 -0
- package/dist/fetch-middleware.js +41 -0
- package/dist/generation.d.ts +77 -0
- package/dist/generation.js +109 -0
- package/dist/image-client.d.ts +14 -0
- package/dist/image-client.js +39 -0
- package/dist/image.d.ts +323 -0
- package/dist/image.js +103 -0
- package/dist/index.d.ts +300 -0
- package/dist/index.js +179 -0
- package/dist/llm.d.ts +109 -0
- package/dist/llm.js +177 -0
- package/dist/loop.d.ts +38 -0
- package/dist/loop.js +139 -0
- package/dist/media-client.d.ts +30 -0
- package/dist/media-client.js +44 -0
- package/dist/media-model.d.ts +49 -0
- package/dist/media-model.js +55 -0
- package/dist/media.d.ts +237 -0
- package/dist/media.js +285 -0
- package/dist/promise-media.d.ts +20 -0
- package/dist/promise-media.js +16 -0
- package/dist/protocols/alibaba-chat.d.ts +315 -0
- package/dist/protocols/alibaba-chat.js +78 -0
- package/dist/protocols/alibaba-images.d.ts +22 -0
- package/dist/protocols/alibaba-images.js +77 -0
- package/dist/protocols/alibaba-messages.d.ts +413 -0
- package/dist/protocols/alibaba-messages.js +44 -0
- package/dist/protocols/alibaba-responses.d.ts +298 -0
- package/dist/protocols/alibaba-responses.js +67 -0
- package/dist/protocols/anthropic-messages.d.ts +1213 -0
- package/dist/protocols/anthropic-messages.js +1412 -0
- package/dist/protocols/assemblyai-transcription.d.ts +41 -0
- package/dist/protocols/assemblyai-transcription.js +137 -0
- package/dist/protocols/bedrock-converse.d.ts +548 -0
- package/dist/protocols/bedrock-converse.js +801 -0
- package/dist/protocols/bedrock-event-stream.d.ts +14 -0
- package/dist/protocols/bedrock-event-stream.js +100 -0
- package/dist/protocols/cartesia-speech.d.ts +117 -0
- package/dist/protocols/cartesia-speech.js +120 -0
- package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
- package/dist/protocols/cloudflare-run-decisions.js +36 -0
- package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
- package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
- package/dist/protocols/cohere-chat.d.ts +184 -0
- package/dist/protocols/cohere-chat.js +317 -0
- package/dist/protocols/deepgram-speech.d.ts +109 -0
- package/dist/protocols/deepgram-speech.js +93 -0
- package/dist/protocols/deepgram-transcription.d.ts +26 -0
- package/dist/protocols/deepgram-transcription.js +119 -0
- package/dist/protocols/elevenlabs-speech.d.ts +112 -0
- package/dist/protocols/elevenlabs-speech.js +111 -0
- package/dist/protocols/elevenlabs-transcription.d.ts +28 -0
- package/dist/protocols/elevenlabs-transcription.js +137 -0
- package/dist/protocols/gemini.d.ts +388 -0
- package/dist/protocols/gemini.js +741 -0
- package/dist/protocols/google-decisions.d.ts +13 -0
- package/dist/protocols/google-decisions.js +133 -0
- package/dist/protocols/google-embed-content.d.ts +34 -0
- package/dist/protocols/google-embed-content.js +110 -0
- package/dist/protocols/google-images.d.ts +18 -0
- package/dist/protocols/google-images.js +169 -0
- package/dist/protocols/google-interactions-video.d.ts +38 -0
- package/dist/protocols/google-interactions-video.js +185 -0
- package/dist/protocols/google-interactions.d.ts +554 -0
- package/dist/protocols/google-interactions.js +756 -0
- package/dist/protocols/google-speech.d.ts +120 -0
- package/dist/protocols/google-speech.js +94 -0
- package/dist/protocols/google-transcription.d.ts +148 -0
- package/dist/protocols/google-transcription.js +129 -0
- package/dist/protocols/google-vertex-predict-embeddings.d.ts +18 -0
- package/dist/protocols/google-vertex-predict-embeddings.js +74 -0
- package/dist/protocols/index.d.ts +21 -0
- package/dist/protocols/index.js +21 -0
- package/dist/protocols/meta-images.d.ts +26 -0
- package/dist/protocols/meta-images.js +101 -0
- package/dist/protocols/meta-messages.d.ts +423 -0
- package/dist/protocols/meta-messages.js +63 -0
- package/dist/protocols/meta-responses.d.ts +533 -0
- package/dist/protocols/meta-responses.js +189 -0
- package/dist/protocols/minimax-images.d.ts +25 -0
- package/dist/protocols/minimax-images.js +150 -0
- package/dist/protocols/minimax-video.d.ts +45 -0
- package/dist/protocols/minimax-video.js +296 -0
- package/dist/protocols/mistral-chat.d.ts +565 -0
- package/dist/protocols/mistral-chat.js +606 -0
- package/dist/protocols/open-responses-channel.d.ts +25 -0
- package/dist/protocols/open-responses-channel.js +137 -0
- package/dist/protocols/open-responses-continuation.d.ts +18 -0
- package/dist/protocols/open-responses-continuation.js +172 -0
- package/dist/protocols/open-responses.d.ts +2509 -0
- package/dist/protocols/open-responses.js +1335 -0
- package/dist/protocols/openai-chat.d.ts +2571 -0
- package/dist/protocols/openai-chat.js +1140 -0
- package/dist/protocols/openai-compatible-chat.d.ts +184 -0
- package/dist/protocols/openai-compatible-chat.js +19 -0
- package/dist/protocols/openai-compatible-responses.d.ts +231 -0
- package/dist/protocols/openai-compatible-responses.js +18 -0
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +141 -0
- package/dist/protocols/openai-embeddings.d.ts +16 -0
- package/dist/protocols/openai-embeddings.js +76 -0
- package/dist/protocols/openai-freeform.d.ts +78 -0
- package/dist/protocols/openai-freeform.js +222 -0
- package/dist/protocols/openai-images.d.ts +148 -0
- package/dist/protocols/openai-images.js +303 -0
- package/dist/protocols/openai-responses.d.ts +1600 -0
- package/dist/protocols/openai-responses.js +360 -0
- package/dist/protocols/openai-speech.d.ts +110 -0
- package/dist/protocols/openai-speech.js +112 -0
- package/dist/protocols/openai-transcription.d.ts +185 -0
- package/dist/protocols/openai-transcription.js +206 -0
- package/dist/protocols/openrouter-video.d.ts +36 -0
- package/dist/protocols/openrouter-video.js +139 -0
- package/dist/protocols/seedance-video.d.ts +45 -0
- package/dist/protocols/seedance-video.js +217 -0
- package/dist/protocols/shared.d.ts +249 -0
- package/dist/protocols/shared.js +446 -0
- package/dist/protocols/system-one.d.ts +145 -0
- package/dist/protocols/system-one.js +272 -0
- package/dist/protocols/utils/bedrock-auth.d.ts +60 -0
- package/dist/protocols/utils/bedrock-auth.js +86 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +19 -0
- package/dist/protocols/utils/bedrock-cache.js +49 -0
- package/dist/protocols/utils/bedrock-media.d.ts +53 -0
- package/dist/protocols/utils/bedrock-media.js +98 -0
- package/dist/protocols/utils/cache.d.ts +25 -0
- package/dist/protocols/utils/cache.js +33 -0
- package/dist/protocols/utils/claude-model.d.ts +8 -0
- package/dist/protocols/utils/claude-model.js +15 -0
- package/dist/protocols/utils/gateway-protocol.d.ts +17 -0
- package/dist/protocols/utils/gateway-protocol.js +59 -0
- package/dist/protocols/utils/gemini-generate-content.d.ts +75 -0
- package/dist/protocols/utils/gemini-generate-content.js +96 -0
- package/dist/protocols/utils/gemini-json-schema.d.ts +8 -0
- package/dist/protocols/utils/gemini-json-schema.js +76 -0
- package/dist/protocols/utils/image-sizing.d.ts +31 -0
- package/dist/protocols/utils/image-sizing.js +73 -0
- package/dist/protocols/utils/lifecycle.d.ts +24 -0
- package/dist/protocols/utils/lifecycle.js +62 -0
- package/dist/protocols/utils/media-input.d.ts +32 -0
- package/dist/protocols/utils/media-input.js +62 -0
- package/dist/protocols/utils/mistral-tool-id.d.ts +5 -0
- package/dist/protocols/utils/mistral-tool-id.js +31 -0
- package/dist/protocols/utils/open-responses-options.d.ts +54 -0
- package/dist/protocols/utils/open-responses-options.js +59 -0
- package/dist/protocols/utils/partial-json-options.d.ts +61 -0
- package/dist/protocols/utils/partial-json-options.js +51 -0
- package/dist/protocols/utils/partial-json.d.ts +8 -0
- package/dist/protocols/utils/partial-json.js +280 -0
- package/dist/protocols/utils/responses-checkpoint.d.ts +4 -0
- package/dist/protocols/utils/responses-checkpoint.js +103 -0
- package/dist/protocols/utils/responses-compaction.d.ts +6 -0
- package/dist/protocols/utils/responses-compaction.js +162 -0
- package/dist/protocols/utils/responses-hosted-tools.d.ts +122 -0
- package/dist/protocols/utils/responses-hosted-tools.js +63 -0
- package/dist/protocols/utils/speaker-turns.d.ts +3 -0
- package/dist/protocols/utils/speaker-turns.js +9 -0
- package/dist/protocols/utils/speech-stream.d.ts +49 -0
- package/dist/protocols/utils/speech-stream.js +64 -0
- package/dist/protocols/utils/tool-schema.d.ts +10 -0
- package/dist/protocols/utils/tool-schema.js +109 -0
- package/dist/protocols/utils/tool-stream.d.ts +478 -0
- package/dist/protocols/utils/tool-stream.js +153 -0
- package/dist/protocols/venice-chat.d.ts +340 -0
- package/dist/protocols/venice-chat.js +283 -0
- package/dist/protocols/xai-images.d.ts +25 -0
- package/dist/protocols/xai-images.js +87 -0
- package/dist/protocols/xai-responses.d.ts +294 -0
- package/dist/protocols/xai-responses.js +86 -0
- package/dist/protocols/xai-video.d.ts +35 -0
- package/dist/protocols/xai-video.js +159 -0
- package/dist/protocols/zai-chat.d.ts +296 -0
- package/dist/protocols/zai-chat.js +56 -0
- package/dist/protocols/zai-images.d.ts +20 -0
- package/dist/protocols/zai-images.js +79 -0
- package/dist/protocols/zai-messages.d.ts +409 -0
- package/dist/protocols/zai-messages.js +30 -0
- package/dist/protocols.d.ts +1 -0
- package/dist/protocols.js +1 -0
- package/dist/provider-error.d.ts +22 -0
- package/dist/provider-error.js +228 -0
- package/dist/provider-package.d.ts +17 -0
- package/dist/provider-package.js +1 -0
- package/dist/providers/alibaba/chat.d.ts +1 -0
- package/dist/providers/alibaba/chat.js +1 -0
- package/dist/providers/alibaba/embeddings.d.ts +2 -0
- package/dist/providers/alibaba/embeddings.js +1 -0
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba/messages.d.ts +3 -0
- package/dist/providers/alibaba/messages.js +1 -0
- package/dist/providers/alibaba/responses.d.ts +3 -0
- package/dist/providers/alibaba/responses.js +1 -0
- package/dist/providers/alibaba.d.ts +905 -0
- package/dist/providers/alibaba.js +119 -0
- package/dist/providers/amazon-bedrock/mantle/chat.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/chat.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/messages.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/messages.js +1 -0
- package/dist/providers/amazon-bedrock/mantle/responses.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle/responses.js +1 -0
- package/dist/providers/amazon-bedrock/mantle.d.ts +2 -0
- package/dist/providers/amazon-bedrock/mantle.js +1 -0
- package/dist/providers/amazon-bedrock-mantle.d.ts +789 -0
- package/dist/providers/amazon-bedrock-mantle.js +107 -0
- package/dist/providers/amazon-bedrock.d.ts +190 -0
- package/dist/providers/amazon-bedrock.js +44 -0
- package/dist/providers/anthropic-compatible.d.ts +364 -0
- package/dist/providers/anthropic-compatible.js +47 -0
- package/dist/providers/anthropic.d.ts +362 -0
- package/dist/providers/anthropic.js +36 -0
- package/dist/providers/assemblyai.d.ts +19 -0
- package/dist/providers/assemblyai.js +19 -0
- package/dist/providers/azure/chat.d.ts +2 -0
- package/dist/providers/azure/chat.js +1 -0
- package/dist/providers/azure/embeddings.d.ts +2 -0
- package/dist/providers/azure/embeddings.js +1 -0
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure/messages.d.ts +2 -0
- package/dist/providers/azure/messages.js +1 -0
- package/dist/providers/azure/responses.d.ts +2 -0
- package/dist/providers/azure/responses.js +1 -0
- package/dist/providers/azure.d.ts +1074 -0
- package/dist/providers/azure.js +150 -0
- package/dist/providers/baseten/decisions.d.ts +2 -0
- package/dist/providers/baseten/decisions.js +1 -0
- package/dist/providers/baseten.d.ts +384 -0
- package/dist/providers/baseten.js +56 -0
- package/dist/providers/byteplus/videos.d.ts +2 -0
- package/dist/providers/byteplus/videos.js +1 -0
- package/dist/providers/byteplus.d.ts +24 -0
- package/dist/providers/byteplus.js +21 -0
- package/dist/providers/cartesia.d.ts +18 -0
- package/dist/providers/cartesia.js +17 -0
- package/dist/providers/cerebras.d.ts +375 -0
- package/dist/providers/cerebras.js +33 -0
- package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
- package/dist/providers/cloudflare-ai-gateway.d.ts +1581 -0
- package/dist/providers/cloudflare-ai-gateway.js +120 -0
- package/dist/providers/cloudflare-shared.d.ts +9 -0
- package/dist/providers/cloudflare-shared.js +25 -0
- package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
- package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
- package/dist/providers/cloudflare-workers-ai/images.js +1 -0
- package/dist/providers/cloudflare-workers-ai.d.ts +391 -0
- package/dist/providers/cloudflare-workers-ai.js +69 -0
- package/dist/providers/cohere/chat.d.ts +4 -0
- package/dist/providers/cohere/chat.js +3 -0
- package/dist/providers/cohere/embeddings.d.ts +2 -0
- package/dist/providers/cohere/embeddings.js +1 -0
- package/dist/providers/cohere.d.ts +502 -0
- package/dist/providers/cohere.js +55 -0
- package/dist/providers/deepgram.d.ts +22 -0
- package/dist/providers/deepgram.js +20 -0
- package/dist/providers/deepinfra/embeddings.d.ts +2 -0
- package/dist/providers/deepinfra/embeddings.js +1 -0
- package/dist/providers/deepinfra.d.ts +378 -0
- package/dist/providers/deepinfra.js +52 -0
- package/dist/providers/deepseek.d.ts +376 -0
- package/dist/providers/deepseek.js +30 -0
- package/dist/providers/digitalocean/decisions.d.ts +2 -0
- package/dist/providers/digitalocean/decisions.js +1 -0
- package/dist/providers/digitalocean/embeddings.d.ts +2 -0
- package/dist/providers/digitalocean/embeddings.js +1 -0
- package/dist/providers/digitalocean/images.d.ts +2 -0
- package/dist/providers/digitalocean/images.js +1 -0
- package/dist/providers/digitalocean/responses.d.ts +4 -0
- package/dist/providers/digitalocean/responses.js +1 -0
- package/dist/providers/digitalocean.d.ts +874 -0
- package/dist/providers/digitalocean.js +106 -0
- package/dist/providers/elevenlabs.d.ts +22 -0
- package/dist/providers/elevenlabs.js +22 -0
- package/dist/providers/fireworks/embeddings.d.ts +2 -0
- package/dist/providers/fireworks/embeddings.js +1 -0
- package/dist/providers/fireworks.d.ts +379 -0
- package/dist/providers/fireworks.js +40 -0
- package/dist/providers/google/decisions.d.ts +2 -0
- package/dist/providers/google/decisions.js +1 -0
- package/dist/providers/google/embeddings.d.ts +2 -0
- package/dist/providers/google/embeddings.js +1 -0
- package/dist/providers/google/images.d.ts +2 -0
- package/dist/providers/google/images.js +1 -0
- package/dist/providers/google/interactions.d.ts +7 -0
- package/dist/providers/google/interactions.js +3 -0
- package/dist/providers/google/videos.d.ts +2 -0
- package/dist/providers/google/videos.js +1 -0
- package/dist/providers/google-vertex/chat.d.ts +2 -0
- package/dist/providers/google-vertex/chat.js +1 -0
- package/dist/providers/google-vertex/embeddings.d.ts +2 -0
- package/dist/providers/google-vertex/embeddings.js +1 -0
- package/dist/providers/google-vertex/gemini.d.ts +2 -0
- package/dist/providers/google-vertex/gemini.js +1 -0
- package/dist/providers/google-vertex/images.d.ts +2 -0
- package/dist/providers/google-vertex/images.js +1 -0
- package/dist/providers/google-vertex/interactions.d.ts +2 -0
- package/dist/providers/google-vertex/interactions.js +1 -0
- package/dist/providers/google-vertex/messages.d.ts +2 -0
- package/dist/providers/google-vertex/messages.js +1 -0
- package/dist/providers/google-vertex/mistral.d.ts +2 -0
- package/dist/providers/google-vertex/mistral.js +1 -0
- package/dist/providers/google-vertex/responses.d.ts +2 -0
- package/dist/providers/google-vertex/responses.js +1 -0
- package/dist/providers/google-vertex-chat.d.ts +204 -0
- package/dist/providers/google-vertex-chat.js +29 -0
- package/dist/providers/google-vertex-interactions.d.ts +111 -0
- package/dist/providers/google-vertex-interactions.js +62 -0
- package/dist/providers/google-vertex-messages.d.ts +362 -0
- package/dist/providers/google-vertex-messages.js +51 -0
- package/dist/providers/google-vertex-mistral.d.ts +120 -0
- package/dist/providers/google-vertex-mistral.js +43 -0
- package/dist/providers/google-vertex-responses.d.ts +253 -0
- package/dist/providers/google-vertex-responses.js +30 -0
- package/dist/providers/google-vertex-shared.d.ts +58 -0
- package/dist/providers/google-vertex-shared.js +128 -0
- package/dist/providers/google-vertex.d.ts +132 -0
- package/dist/providers/google-vertex.js +161 -0
- package/dist/providers/google.d.ts +246 -0
- package/dist/providers/google.js +80 -0
- package/dist/providers/groq.d.ts +470 -0
- package/dist/providers/groq.js +63 -0
- package/dist/providers/index.d.ts +45 -0
- package/dist/providers/index.js +45 -0
- package/dist/providers/meta/chat.d.ts +2 -0
- package/dist/providers/meta/chat.js +1 -0
- package/dist/providers/meta/images.d.ts +2 -0
- package/dist/providers/meta/images.js +1 -0
- package/dist/providers/meta/messages.d.ts +2 -0
- package/dist/providers/meta/messages.js +1 -0
- package/dist/providers/meta/responses.d.ts +2 -0
- package/dist/providers/meta/responses.js +1 -0
- package/dist/providers/meta.d.ts +804 -0
- package/dist/providers/meta.js +96 -0
- package/dist/providers/minimax/chat.d.ts +4 -0
- package/dist/providers/minimax/chat.js +3 -0
- package/dist/providers/minimax/images.d.ts +3 -0
- package/dist/providers/minimax/images.js +1 -0
- package/dist/providers/minimax/messages.d.ts +1 -0
- package/dist/providers/minimax/messages.js +1 -0
- package/dist/providers/minimax/responses.d.ts +4 -0
- package/dist/providers/minimax/responses.js +3 -0
- package/dist/providers/minimax/videos.d.ts +3 -0
- package/dist/providers/minimax/videos.js +1 -0
- package/dist/providers/minimax.d.ts +798 -0
- package/dist/providers/minimax.js +99 -0
- package/dist/providers/mistral.d.ts +208 -0
- package/dist/providers/mistral.js +17 -0
- package/dist/providers/moonshot/chat.d.ts +1 -0
- package/dist/providers/moonshot/chat.js +1 -0
- package/dist/providers/moonshot/messages.d.ts +4 -0
- package/dist/providers/moonshot/messages.js +3 -0
- package/dist/providers/moonshot/responses.d.ts +4 -0
- package/dist/providers/moonshot/responses.js +3 -0
- package/dist/providers/moonshot.d.ts +785 -0
- package/dist/providers/moonshot.js +78 -0
- package/dist/providers/open-responses-options.d.ts +5 -0
- package/dist/providers/open-responses-options.js +1 -0
- package/dist/providers/openai/chat.d.ts +2 -0
- package/dist/providers/openai/chat.js +1 -0
- package/dist/providers/openai/embeddings.d.ts +2 -0
- package/dist/providers/openai/embeddings.js +1 -0
- package/dist/providers/openai/images.d.ts +2 -0
- package/dist/providers/openai/images.js +1 -0
- package/dist/providers/openai/responses.d.ts +2 -0
- package/dist/providers/openai/responses.js +1 -0
- package/dist/providers/openai-compatible/decisions.d.ts +2 -0
- package/dist/providers/openai-compatible/decisions.js +1 -0
- package/dist/providers/openai-compatible/embeddings.d.ts +2 -0
- package/dist/providers/openai-compatible/embeddings.js +1 -0
- package/dist/providers/openai-compatible/images.d.ts +2 -0
- package/dist/providers/openai-compatible/images.js +1 -0
- package/dist/providers/openai-compatible/responses.d.ts +1 -0
- package/dist/providers/openai-compatible/responses.js +1 -0
- package/dist/providers/openai-compatible-responses.d.ts +250 -0
- package/dist/providers/openai-compatible-responses.js +26 -0
- package/dist/providers/openai-compatible.d.ts +208 -0
- package/dist/providers/openai-compatible.js +71 -0
- package/dist/providers/openai-options.d.ts +21 -0
- package/dist/providers/openai-options.js +37 -0
- package/dist/providers/openai.d.ts +522 -0
- package/dist/providers/openai.js +118 -0
- package/dist/providers/opencode/decisions.d.ts +2 -0
- package/dist/providers/opencode/decisions.js +1 -0
- package/dist/providers/opencode.d.ts +23 -0
- package/dist/providers/opencode.js +25 -0
- package/dist/providers/openrouter/decisions.d.ts +2 -0
- package/dist/providers/openrouter/decisions.js +1 -0
- package/dist/providers/openrouter/embeddings.d.ts +2 -0
- package/dist/providers/openrouter/embeddings.js +1 -0
- package/dist/providers/openrouter/images.d.ts +2 -0
- package/dist/providers/openrouter/images.js +1 -0
- package/dist/providers/openrouter/responses.d.ts +3 -0
- package/dist/providers/openrouter/responses.js +1 -0
- package/dist/providers/openrouter/videos.d.ts +2 -0
- package/dist/providers/openrouter/videos.js +1 -0
- package/dist/providers/openrouter.d.ts +1204 -0
- package/dist/providers/openrouter.js +210 -0
- package/dist/providers/settings.d.ts +40 -0
- package/dist/providers/settings.js +18 -0
- package/dist/providers/togetherai.d.ts +375 -0
- package/dist/providers/togetherai.js +30 -0
- package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
- package/dist/providers/typesafe-ai/decisions.js +1 -0
- package/dist/providers/typesafe-ai.d.ts +23 -0
- package/dist/providers/typesafe-ai.js +25 -0
- package/dist/providers/venice/responses.d.ts +4 -0
- package/dist/providers/venice/responses.js +1 -0
- package/dist/providers/venice.d.ts +632 -0
- package/dist/providers/venice.js +57 -0
- package/dist/providers/vercel-ai-gateway/chat.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/chat.js +1 -0
- package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
- package/dist/providers/vercel-ai-gateway/embeddings.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/embeddings.js +1 -0
- package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/images.js +1 -0
- package/dist/providers/vercel-ai-gateway/messages.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/messages.js +1 -0
- package/dist/providers/vercel-ai-gateway/responses.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/responses.js +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +111 -0
- package/dist/providers/vercel-ai-gateway.js +181 -0
- package/dist/providers/volcengine/videos.d.ts +2 -0
- package/dist/providers/volcengine/videos.js +1 -0
- package/dist/providers/volcengine.d.ts +24 -0
- package/dist/providers/volcengine.js +21 -0
- package/dist/providers/xai/images.d.ts +2 -0
- package/dist/providers/xai/images.js +1 -0
- package/dist/providers/xai/videos.d.ts +2 -0
- package/dist/providers/xai/videos.js +1 -0
- package/dist/providers/xai.d.ts +235 -0
- package/dist/providers/xai.js +89 -0
- package/dist/providers/zai/chat.d.ts +1 -0
- package/dist/providers/zai/chat.js +1 -0
- package/dist/providers/zai/images.d.ts +2 -0
- package/dist/providers/zai/images.js +1 -0
- package/dist/providers/zai-coding-plan/chat.d.ts +1 -0
- package/dist/providers/zai-coding-plan/chat.js +1 -0
- package/dist/providers/zai-coding-plan/messages.d.ts +4 -0
- package/dist/providers/zai-coding-plan/messages.js +3 -0
- package/dist/providers/zai-coding-plan/responses.d.ts +4 -0
- package/dist/providers/zai-coding-plan/responses.js +3 -0
- package/dist/providers/zai-coding-plan.d.ts +846 -0
- package/dist/providers/zai-coding-plan.js +56 -0
- package/dist/providers/zai.d.ts +285 -0
- package/dist/providers/zai.js +44 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.js +1 -0
- package/dist/public.d.ts +18 -0
- package/dist/public.js +7 -0
- package/dist/route/auth-options.d.ts +35 -0
- package/dist/route/auth-options.js +17 -0
- package/dist/route/auth.d.ts +52 -0
- package/dist/route/auth.js +92 -0
- package/dist/route/client.d.ts +229 -0
- package/dist/route/client.js +293 -0
- package/dist/route/decision.d.ts +55 -0
- package/dist/route/decision.js +73 -0
- package/dist/route/embedding.d.ts +58 -0
- package/dist/route/embedding.js +85 -0
- package/dist/route/endpoint.d.ts +29 -0
- package/dist/route/endpoint.js +21 -0
- package/dist/route/executor-service.d.ts +54 -0
- package/dist/route/executor-service.js +115 -0
- package/dist/route/executor.d.ts +37 -0
- package/dist/route/executor.js +225 -0
- package/dist/route/framing.d.ts +44 -0
- package/dist/route/framing.js +63 -0
- package/dist/route/index.d.ts +21 -0
- package/dist/route/index.js +18 -0
- package/dist/route/media-protocol.d.ts +187 -0
- package/dist/route/media-protocol.js +117 -0
- package/dist/route/media.d.ts +91 -0
- package/dist/route/media.js +251 -0
- package/dist/route/protocol.d.ts +85 -0
- package/dist/route/protocol.js +19 -0
- package/dist/route/transport/http.d.ts +34 -0
- package/dist/route/transport/http.js +67 -0
- package/dist/route/transport/index.d.ts +42 -0
- package/dist/route/transport/index.js +4 -0
- package/dist/route/transport/websocket-channel.d.ts +56 -0
- package/dist/route/transport/websocket-channel.js +1 -0
- package/dist/route/transport/websocket.d.ts +32 -0
- package/dist/route/transport/websocket.js +315 -0
- package/dist/route.d.ts +1 -0
- package/dist/route.js +1 -0
- package/dist/schema/errors.d.ts +194 -0
- package/dist/schema/errors.js +217 -0
- package/dist/schema/events.d.ts +11001 -0
- package/dist/schema/events.js +542 -0
- package/dist/schema/ids.d.ts +17 -0
- package/dist/schema/ids.js +10 -0
- package/dist/schema/index.d.ts +5 -0
- package/dist/schema/index.js +5 -0
- package/dist/schema/messages.d.ts +2706 -0
- package/dist/schema/messages.js +344 -0
- package/dist/schema/options.d.ts +270 -0
- package/dist/schema/options.js +198 -0
- package/dist/speech.d.ts +266 -0
- package/dist/speech.js +114 -0
- package/dist/test-ai.d.ts +15 -0
- package/dist/test-ai.js +49 -0
- package/dist/testing.d.ts +1585 -0
- package/dist/testing.js +108 -0
- package/dist/tool-history.d.ts +2 -0
- package/dist/tool-history.js +91 -0
- package/dist/tool-runtime.d.ts +22 -0
- package/dist/tool-runtime.js +55 -0
- package/dist/tool-updates.d.ts +34 -0
- package/dist/tool-updates.js +93 -0
- package/dist/tool.d.ts +150 -0
- package/dist/tool.js +159 -0
- package/dist/transcription.d.ts +254 -0
- package/dist/transcription.js +119 -0
- package/dist/utils/bytes.d.ts +1 -0
- package/dist/utils/bytes.js +10 -0
- package/dist/utils/json.d.ts +4 -0
- package/dist/utils/json.js +4 -0
- package/dist/utils/media-type.d.ts +8 -0
- package/dist/utils/media-type.js +93 -0
- package/dist/utils/provider-codes.d.ts +3 -0
- package/dist/utils/provider-codes.js +41 -0
- package/dist/utils/record.d.ts +2 -0
- package/dist/utils/record.js +2 -0
- package/dist/utils/sanitize.d.ts +5 -0
- package/dist/utils/sanitize.js +31 -0
- package/dist/video-client.d.ts +12 -0
- package/dist/video-client.js +24 -0
- package/dist/video.d.ts +178 -0
- package/dist/video.js +79 -0
- package/package.json +648 -4
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Base64 } from "effect/encoding";
|
|
3
|
+
import { EmbeddingResponse, EmbeddingUsage } from "../embedding.js";
|
|
4
|
+
import { EmbeddingRoute } from "../route/embedding.js";
|
|
5
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
6
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
7
|
+
import { AIError, UnsupportedOperationError, mergeJsonRecords } from "../schema/index.js";
|
|
8
|
+
const ID = "openai-embeddings";
|
|
9
|
+
const NAME = "OpenAI Embeddings";
|
|
10
|
+
export const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
11
|
+
export const PATH = "/embeddings";
|
|
12
|
+
const Response = Schema.Struct({
|
|
13
|
+
data: Schema.Array(Schema.Struct({
|
|
14
|
+
index: Schema.optional(Schema.Int),
|
|
15
|
+
// `base64` encoding returns little-endian float32 bytes instead of a number list.
|
|
16
|
+
embedding: Schema.Union([Schema.Array(Schema.Number), Schema.String]),
|
|
17
|
+
})),
|
|
18
|
+
model: Schema.optional(Schema.String),
|
|
19
|
+
usage: Schema.optional(Schema.StructWithRest(Schema.Struct({ prompt_tokens: Schema.optional(Schema.Number), total_tokens: Schema.optional(Schema.Number) }), [Schema.Record(Schema.String, Schema.Unknown)])),
|
|
20
|
+
});
|
|
21
|
+
// `inputType` has no field here; OpenAI's models embed queries and documents the same way.
|
|
22
|
+
const fromRequest = Effect.fn("OpenAIEmbeddings.fromRequest")(function* (request) {
|
|
23
|
+
const input = request.values.filter((value) => typeof value === "string");
|
|
24
|
+
if (input.length !== request.values.length)
|
|
25
|
+
return yield* new AIError({
|
|
26
|
+
reason: new UnsupportedOperationError({
|
|
27
|
+
operation: "embedding.media",
|
|
28
|
+
provider: request.model.provider,
|
|
29
|
+
route: request.model.route.id,
|
|
30
|
+
message: `${NAME} embeds text only; pass media values to a host with a multimodal embedding API`,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
...mergeJsonRecords({ encoding_format: "float", ...request.providerOptions }, request.http?.body),
|
|
35
|
+
model: request.model.id,
|
|
36
|
+
input,
|
|
37
|
+
...(request.dimensions === undefined ? {} : { dimensions: request.dimensions }),
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
const decodeDocument = MediaProtocol.identity({ id: ID, name: NAME, provider: "openai" }).decodeJson(Response);
|
|
41
|
+
export const protocol = EmbeddingRoute.protocol({
|
|
42
|
+
id: ID,
|
|
43
|
+
name: NAME,
|
|
44
|
+
body: { from: fromRequest },
|
|
45
|
+
response: {
|
|
46
|
+
decode: Effect.fn("OpenAIEmbeddings.decodeResponse")(function* (response, context) {
|
|
47
|
+
const output = yield* decodeDocument(response);
|
|
48
|
+
const embeddings = yield* Effect.forEach(output.value.data
|
|
49
|
+
.map((item, position) => ({ index: item.index ?? position, embedding: item.embedding }))
|
|
50
|
+
.toSorted((a, b) => a.index - b.index), (item) => typeof item.embedding !== "string"
|
|
51
|
+
? Effect.succeed(item.embedding)
|
|
52
|
+
: Effect.fromResult(Base64.decode(item.embedding)).pipe(Effect.map((bytes) => Array.from(new Float32Array(bytes.slice().buffer))), Effect.mapError((cause) => output.invalid(`${NAME} returned an invalid base64 embedding`, cause))));
|
|
53
|
+
const usage = output.value.usage;
|
|
54
|
+
return new EmbeddingResponse({
|
|
55
|
+
embeddings,
|
|
56
|
+
usage: usage?.prompt_tokens === undefined ? undefined : new EmbeddingUsage({ inputTokens: usage.prompt_tokens }),
|
|
57
|
+
providerMetadata: {
|
|
58
|
+
[context.providerMetadataKey]: {
|
|
59
|
+
...(output.value.model === undefined ? {} : { model: output.value.model }),
|
|
60
|
+
...(usage === undefined ? {} : { usage }),
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}),
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
export const route = EmbeddingRoute.make({
|
|
68
|
+
id: ID,
|
|
69
|
+
provider: "openai",
|
|
70
|
+
protocol,
|
|
71
|
+
endpoint: Endpoint.path(PATH, { baseURL: DEFAULT_BASE_URL }),
|
|
72
|
+
});
|
|
73
|
+
export const OpenAIEmbeddings = {
|
|
74
|
+
protocol,
|
|
75
|
+
route,
|
|
76
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { JsonSchema, ToolDefinition, type Freeform, type StreamEvent, type LLMRequest, type ProviderMetadata } from "../schema/index.js";
|
|
2
|
+
import { ToolStream } from "./utils/tool-stream.js";
|
|
3
|
+
export interface Custom {
|
|
4
|
+
/** The tool's required string parameter, which the raw text fills. */
|
|
5
|
+
readonly field: string;
|
|
6
|
+
readonly format: Freeform;
|
|
7
|
+
/** The tool's other parameters, set on a first line. */
|
|
8
|
+
readonly options?: {
|
|
9
|
+
readonly fields: ReadonlyArray<OptionField>;
|
|
10
|
+
/** How to write the line, added to the custom tool's description. */
|
|
11
|
+
readonly description: string;
|
|
12
|
+
/** The grammar for the line and the raw text, when the model takes Lark. */
|
|
13
|
+
readonly grammar?: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
interface OptionField {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly schema: JsonSchema;
|
|
19
|
+
readonly required: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare const PREFIX = "// @options:";
|
|
22
|
+
/**
|
|
23
|
+
* How a tool goes out: a custom tool when it declares `freeform` in a format the model accepts (`formats`; none when
|
|
24
|
+
* unset), with its required string parameter. Other parameters need a grammar for their values, and a raw
|
|
25
|
+
* text body when the model can't take Lark; otherwise the tool stays a JSON tool.
|
|
26
|
+
*/
|
|
27
|
+
export declare const resolve: (tool: ToolDefinition, formats?: ReadonlyArray<Freeform["type"]>) => Custom | undefined;
|
|
28
|
+
/** Custom tools in this request, including ones added mid-conversation, by flattened tool name. */
|
|
29
|
+
export declare const tools: (request: LLMRequest) => ReadonlyMap<string, Custom>;
|
|
30
|
+
/** The grammar to send, or `undefined` for unconstrained text. */
|
|
31
|
+
export declare const grammar: (custom: Custom) => {
|
|
32
|
+
syntax: "lark" | "regex";
|
|
33
|
+
definition: string;
|
|
34
|
+
} | undefined;
|
|
35
|
+
/** The custom tool's description, with how to write the line when the tool has other parameters. */
|
|
36
|
+
export declare const description: (tool: ToolDefinition, custom: Custom) => string;
|
|
37
|
+
/**
|
|
38
|
+
* Raw text replayed for a past call: its field after a line for the other parameters, or the whole input as JSON when
|
|
39
|
+
* the field is not a string. The line the model wrote, saved as `header`, replays as is while it still says the same,
|
|
40
|
+
* so the prompt cache holds; otherwise, as for a call another model made, a fresh line is written.
|
|
41
|
+
*/
|
|
42
|
+
export declare const toRaw: (custom: Custom, input: unknown, header?: unknown) => string;
|
|
43
|
+
/**
|
|
44
|
+
* JSON arguments for a raw call, with its line as `header`. A call to an undeclared custom tool fills `input`. A line
|
|
45
|
+
* that isn't a JSON object stays raw text, which fails as malformed JSON; the tool's schema checks the rest.
|
|
46
|
+
*/
|
|
47
|
+
export declare const parse: (custom: Custom | undefined, raw: string) => {
|
|
48
|
+
input: string;
|
|
49
|
+
header?: undefined;
|
|
50
|
+
} | {
|
|
51
|
+
input: string;
|
|
52
|
+
header: string;
|
|
53
|
+
};
|
|
54
|
+
/** The opening fragment, `{"field":"`, followed by the escaped first text. */
|
|
55
|
+
export declare const streamStart: (custom: Custom | undefined, raw?: string) => string;
|
|
56
|
+
export declare const streamDelta: (raw: string) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Close the JSON fragment of each open custom call, and set its input from the raw text. With a `final` raw input
|
|
59
|
+
* that the streamed fragments don't decode to, the final one sets the input and the streamed one stays unclosed. A
|
|
60
|
+
* line the model wrote goes in the call's provider metadata as `header`, so it replays exactly.
|
|
61
|
+
*/
|
|
62
|
+
export declare const close: <K extends string | number>(tools: ToolStream.State<K>, keys: Iterable<K>, custom: ReadonlyMap<string, Custom>, metadataKey: string, final?: string) => {
|
|
63
|
+
readonly tools: ToolStream.State<K>;
|
|
64
|
+
readonly events: ReadonlyArray<StreamEvent>;
|
|
65
|
+
};
|
|
66
|
+
/** Provider metadata marking a call that arrived as a custom tool call. */
|
|
67
|
+
export declare const metadata: (itemId: string, custom: boolean) => {
|
|
68
|
+
itemId: string;
|
|
69
|
+
custom: boolean;
|
|
70
|
+
} | {
|
|
71
|
+
itemId: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Whether a replayed call keeps its item id: only when it goes out as the type it arrived as, since OpenAI item ids
|
|
75
|
+
* name their type (`fc_`, `ctc_`). The `call_id` always survives.
|
|
76
|
+
*/
|
|
77
|
+
export declare const keepsItemID: (providerMetadata: ProviderMetadata | undefined, key: string, custom: boolean) => boolean;
|
|
78
|
+
export * as OpenAIFreeform from "./openai-freeform.js";
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { JsonSchema, ToolDefinition, } from "../schema/index.js";
|
|
3
|
+
import { encodeJson, flattenTools, isRecord } from "./shared.js";
|
|
4
|
+
import { ToolStream } from "./utils/tool-stream.js";
|
|
5
|
+
import { toolsUpdate } from "../tool-updates.js";
|
|
6
|
+
// The other parameters go on a first line as a JSON object, `// @options: {"timeout": 5000}`, then the field's text.
|
|
7
|
+
// It is a JavaScript comment, so the line reads naturally before code.
|
|
8
|
+
export const PREFIX = "// @options:";
|
|
9
|
+
/**
|
|
10
|
+
* How a tool goes out: a custom tool when it declares `freeform` in a format the model accepts (`formats`; none when
|
|
11
|
+
* unset), with its required string parameter. Other parameters need a grammar for their values, and a raw
|
|
12
|
+
* text body when the model can't take Lark; otherwise the tool stays a JSON tool.
|
|
13
|
+
*/
|
|
14
|
+
export const resolve = (tool, formats = []) => {
|
|
15
|
+
const field = ToolDefinition.freeformField(tool.inputSchema, tool.freeform);
|
|
16
|
+
if (tool.freeform === undefined || tool.native !== undefined || field === undefined)
|
|
17
|
+
return undefined;
|
|
18
|
+
if (!formats.includes(tool.freeform.type))
|
|
19
|
+
return undefined;
|
|
20
|
+
const lark = formats.includes("lark");
|
|
21
|
+
const required = Array.isArray(tool.inputSchema.required) ? tool.inputSchema.required : [];
|
|
22
|
+
const fields = Object.entries(Schema.is(JsonSchema)(tool.inputSchema.properties) ? tool.inputSchema.properties : {})
|
|
23
|
+
.filter(([name]) => name !== field)
|
|
24
|
+
.map(([name, schema]) => ({
|
|
25
|
+
name,
|
|
26
|
+
schema: Schema.is(JsonSchema)(schema) ? schema : {},
|
|
27
|
+
required: required.includes(name),
|
|
28
|
+
}));
|
|
29
|
+
if (fields.length === 0)
|
|
30
|
+
return { field, format: tool.freeform };
|
|
31
|
+
const rules = fields.flatMap((item) => valueRule(item.schema) ?? []);
|
|
32
|
+
if (rules.length !== fields.length || (!lark && tool.freeform.type !== "text"))
|
|
33
|
+
return undefined;
|
|
34
|
+
return {
|
|
35
|
+
field,
|
|
36
|
+
format: tool.freeform,
|
|
37
|
+
options: {
|
|
38
|
+
fields,
|
|
39
|
+
description: describe(field, fields),
|
|
40
|
+
grammar: lark ? lineGrammar(fields, rules, tool.freeform) : undefined,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
// Keys in any order, with spaces after `{`, `:` and `,` and before `}`. The tool's own schema checks the values it
|
|
45
|
+
// gets, as it does for a JSON call. Without required parameters a call may leave the line out, so plain raw text
|
|
46
|
+
// can't start with the prefix. A tool's own grammar becomes the body, its `start` renamed to `fb_start`.
|
|
47
|
+
const lineGrammar = (fields, rules, format) => {
|
|
48
|
+
const body = format.type === "text" ? "ff_body" : "fb_start";
|
|
49
|
+
const plain = format.type === "text" ? "ff_plain" : body;
|
|
50
|
+
return [
|
|
51
|
+
`start: ff_line ${body}${fields.some((item) => item.required) ? "" : ` | ${plain}`}`,
|
|
52
|
+
`ff_line: ${larkString(PREFIX)} " "* "{" " "* ff_pair (" "* "," " "* ff_pair)* " "* "}" "\\n"`,
|
|
53
|
+
`ff_pair: ${fields.map((item, index) => `${larkString(encodeJson(item.name))} " "* ":" " "* ${rules[index]}`).join(" | ")}`,
|
|
54
|
+
...(format.type === "text"
|
|
55
|
+
? ["ff_body: /[\\s\\S]+/", `ff_plain: /${notStartingWith(PREFIX)}/`]
|
|
56
|
+
: format.type === "regex"
|
|
57
|
+
? [`fb_start: /${format.pattern.replace(/\\?\//g, "\\/")}/`]
|
|
58
|
+
: [format.grammar.replace(/^([ \t]*)start([ \t]*:)/m, "$1fb_start$2")]),
|
|
59
|
+
].join("\n");
|
|
60
|
+
};
|
|
61
|
+
const valueRule = (schema) => {
|
|
62
|
+
if (Array.isArray(schema.enum))
|
|
63
|
+
return schema.enum.length === 0
|
|
64
|
+
? undefined
|
|
65
|
+
: `(${schema.enum.map((item) => larkString(encodeJson(item))).join(" | ")})`;
|
|
66
|
+
if (schema.type === "boolean")
|
|
67
|
+
return '("true" | "false")';
|
|
68
|
+
if (schema.type === "number")
|
|
69
|
+
return "/-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/";
|
|
70
|
+
if (schema.type === "string")
|
|
71
|
+
return '/"(?:[^"\\\\\\x00-\\x1f]|\\\\["\\\\\\/bfnrt]|\\\\u[0-9a-fA-F]{4})*"/';
|
|
72
|
+
if (schema.type !== "integer")
|
|
73
|
+
return undefined;
|
|
74
|
+
const lowest = minimum(schema);
|
|
75
|
+
if (lowest >= 1)
|
|
76
|
+
return "/[1-9][0-9]*/";
|
|
77
|
+
return lowest >= 0 ? "/0|[1-9][0-9]*/" : "/-?(?:0|[1-9][0-9]*)/";
|
|
78
|
+
};
|
|
79
|
+
const minimum = (schema) => Math.max(typeof schema.minimum === "number" ? schema.minimum : -Infinity, typeof schema.exclusiveMinimum === "number" ? schema.exclusiveMinimum + 1 : -Infinity);
|
|
80
|
+
const describe = (field, fields) => {
|
|
81
|
+
const required = fields.filter((item) => item.required);
|
|
82
|
+
const example = required.length > 0 ? required : [fields.find((item) => item.schema.type === "boolean") ?? fields[0]];
|
|
83
|
+
return [
|
|
84
|
+
required.length === 0
|
|
85
|
+
? `To set the parameters below, start with a \`${PREFIX}\` line holding a JSON object of them, then write \`${field}\` from the next line. Leave the line out to use the defaults.`
|
|
86
|
+
: `Start with a \`${PREFIX}\` line holding a JSON object of the parameters below, then write \`${field}\` from the next line.`,
|
|
87
|
+
...fields.map((item) => `- \`${item.name}\` (${Array.isArray(item.schema.enum) ? `one of ${item.schema.enum.map((value) => encodeJson(value)).join(", ")}` : String(item.schema.type)}${item.required ? ", required" : ""})${typeof item.schema.description === "string" ? `: ${item.schema.description}` : ""}`),
|
|
88
|
+
"For example:",
|
|
89
|
+
`${PREFIX} {${example.map((item) => `${encodeJson(item.name)}: ${encodeJson(sample(item.schema))}`).join(", ")}}`,
|
|
90
|
+
`<${field}>`,
|
|
91
|
+
].join("\n");
|
|
92
|
+
};
|
|
93
|
+
const sample = (schema) => {
|
|
94
|
+
if (Array.isArray(schema.enum))
|
|
95
|
+
return schema.enum[0];
|
|
96
|
+
if (schema.type === "boolean")
|
|
97
|
+
return true;
|
|
98
|
+
if (schema.type === "string")
|
|
99
|
+
return "...";
|
|
100
|
+
return Math.max(1, minimum(schema));
|
|
101
|
+
};
|
|
102
|
+
const larkString = (text) => JSON.stringify(text);
|
|
103
|
+
// A regex for text that does not start with `prefix`: it differs from the prefix at some character, or is a shorter
|
|
104
|
+
// prefix of it.
|
|
105
|
+
const notStartingWith = (prefix) => {
|
|
106
|
+
const chars = [...prefix];
|
|
107
|
+
const head = (end) => chars.slice(0, end).map(escapeRegex).join("");
|
|
108
|
+
const differs = chars.map((char, index) => `${head(index)}[^${escapeClass(char)}]`);
|
|
109
|
+
const shorter = chars.slice(1).map((_, index) => head(index + 1));
|
|
110
|
+
return [`(?:${differs.join("|")})[\\s\\S]*`, ...shorter].join("|");
|
|
111
|
+
};
|
|
112
|
+
const escapeRegex = (char) => char.replace(/[\\^$.*+?()[\]{}|/]/g, "\\$&");
|
|
113
|
+
const escapeClass = (char) => char.replace(/[\\\]^\-/]/g, "\\$&");
|
|
114
|
+
/** Custom tools in this request, including ones added mid-conversation, by flattened tool name. */
|
|
115
|
+
export const tools = (request) => {
|
|
116
|
+
const formats = request.model.capabilities?.tools?.freeform;
|
|
117
|
+
const added = request.messages.flatMap((message) => toolsUpdate(message)?.add ?? []);
|
|
118
|
+
return new Map(flattenTools([...request.tools, ...added]).flatMap((tool) => {
|
|
119
|
+
const custom = resolve(tool, formats);
|
|
120
|
+
return custom ? [[tool.name, custom]] : [];
|
|
121
|
+
}));
|
|
122
|
+
};
|
|
123
|
+
/** The grammar to send, or `undefined` for unconstrained text. */
|
|
124
|
+
export const grammar = (custom) => {
|
|
125
|
+
if (custom.options)
|
|
126
|
+
return custom.options.grammar === undefined
|
|
127
|
+
? undefined
|
|
128
|
+
: { syntax: "lark", definition: custom.options.grammar };
|
|
129
|
+
const format = custom.format;
|
|
130
|
+
if (format.type === "text")
|
|
131
|
+
return undefined;
|
|
132
|
+
return { syntax: format.type, definition: format.type === "regex" ? format.pattern : format.grammar };
|
|
133
|
+
};
|
|
134
|
+
/** The custom tool's description, with how to write the line when the tool has other parameters. */
|
|
135
|
+
export const description = (tool, custom) => custom.options ? `${tool.description}\n\n${custom.options.description}` : tool.description;
|
|
136
|
+
/**
|
|
137
|
+
* Raw text replayed for a past call: its field after a line for the other parameters, or the whole input as JSON when
|
|
138
|
+
* the field is not a string. The line the model wrote, saved as `header`, replays as is while it still says the same,
|
|
139
|
+
* so the prompt cache holds; otherwise, as for a call another model made, a fresh line is written.
|
|
140
|
+
*/
|
|
141
|
+
export const toRaw = (custom, input, header) => {
|
|
142
|
+
const value = isRecord(input) ? input[custom.field] : undefined;
|
|
143
|
+
if (typeof value !== "string" || !isRecord(input))
|
|
144
|
+
return encodeJson(input ?? {});
|
|
145
|
+
const fields = custom.options?.fields ?? [];
|
|
146
|
+
const options = encodeJson(pick(fields, input));
|
|
147
|
+
if (options === "{}")
|
|
148
|
+
return value;
|
|
149
|
+
const saved = typeof header === "string" ? Option.getOrUndefined(decodeLine(header)) : undefined;
|
|
150
|
+
const same = saved !== undefined && encodeJson(pick(fields, saved)) === options;
|
|
151
|
+
return `${same ? header : `${PREFIX} ${options}`}\n${value}`;
|
|
152
|
+
};
|
|
153
|
+
const pick = (fields, input) => Object.fromEntries(fields.flatMap((item) => (input[item.name] === undefined ? [] : [[item.name, input[item.name]]])));
|
|
154
|
+
/**
|
|
155
|
+
* JSON arguments for a raw call, with its line as `header`. A call to an undeclared custom tool fills `input`. A line
|
|
156
|
+
* that isn't a JSON object stays raw text, which fails as malformed JSON; the tool's schema checks the rest.
|
|
157
|
+
*/
|
|
158
|
+
export const parse = (custom, raw) => {
|
|
159
|
+
const end = raw.indexOf("\n");
|
|
160
|
+
if (!custom?.options || !raw.startsWith(PREFIX) || end === -1)
|
|
161
|
+
return { input: encodeJson({ [field(custom)]: raw }) };
|
|
162
|
+
const header = raw.slice(0, end);
|
|
163
|
+
const options = Option.getOrUndefined(decodeLine(header));
|
|
164
|
+
if (options === undefined)
|
|
165
|
+
return { input: raw };
|
|
166
|
+
return { input: encodeJson({ ...options, [custom.field]: raw.slice(end + 1) }), header };
|
|
167
|
+
};
|
|
168
|
+
const decodeLine = (header) => decodeArguments(header.slice(PREFIX.length));
|
|
169
|
+
const field = (custom) => custom?.field ?? "input";
|
|
170
|
+
// Streamed raw text is re-encoded as JSON-argument fragments, so the tool-input events match a JSON tool's.
|
|
171
|
+
/** The opening fragment, `{"field":"`, followed by the escaped first text. */
|
|
172
|
+
export const streamStart = (custom, raw = "") => `{${encodeJson(field(custom))}:"${streamDelta(raw)}`;
|
|
173
|
+
export const streamDelta = (raw) => encodeJson(raw).slice(1, -1);
|
|
174
|
+
const STREAM_END = '"}';
|
|
175
|
+
const decodeArguments = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)));
|
|
176
|
+
/**
|
|
177
|
+
* Close the JSON fragment of each open custom call, and set its input from the raw text. With a `final` raw input
|
|
178
|
+
* that the streamed fragments don't decode to, the final one sets the input and the streamed one stays unclosed. A
|
|
179
|
+
* line the model wrote goes in the call's provider metadata as `header`, so it replays exactly.
|
|
180
|
+
*/
|
|
181
|
+
export const close = (tools, keys, custom, metadataKey, final) => [...keys].reduce((result, key) => {
|
|
182
|
+
const pending = result.tools[key];
|
|
183
|
+
if (!pending)
|
|
184
|
+
return result;
|
|
185
|
+
const definition = custom.get(pending.name);
|
|
186
|
+
const streamed = Option.getOrUndefined(decodeArguments(`${pending.input}${STREAM_END}`))?.[field(definition)];
|
|
187
|
+
if (final !== undefined && streamed !== final)
|
|
188
|
+
return {
|
|
189
|
+
tools: { ...result.tools, [key]: settle(pending, definition, metadataKey, final) },
|
|
190
|
+
events: result.events,
|
|
191
|
+
};
|
|
192
|
+
const appended = ToolStream.append(result.tools, key, STREAM_END);
|
|
193
|
+
if (!appended)
|
|
194
|
+
return result;
|
|
195
|
+
return {
|
|
196
|
+
tools: typeof streamed === "string"
|
|
197
|
+
? { ...appended.tools, [key]: settle(appended.tool, definition, metadataKey, streamed) }
|
|
198
|
+
: appended.tools,
|
|
199
|
+
events: [...result.events, ...appended.events],
|
|
200
|
+
};
|
|
201
|
+
}, { tools, events: [] });
|
|
202
|
+
const settle = (tool, custom, metadataKey, raw) => {
|
|
203
|
+
const parsed = parse(custom, raw);
|
|
204
|
+
if (parsed.header === undefined)
|
|
205
|
+
return { ...tool, input: parsed.input };
|
|
206
|
+
return {
|
|
207
|
+
...tool,
|
|
208
|
+
input: parsed.input,
|
|
209
|
+
providerMetadata: {
|
|
210
|
+
...tool.providerMetadata,
|
|
211
|
+
[metadataKey]: { ...tool.providerMetadata?.[metadataKey], header: parsed.header },
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
/** Provider metadata marking a call that arrived as a custom tool call. */
|
|
216
|
+
export const metadata = (itemId, custom) => (custom ? { itemId, custom: true } : { itemId });
|
|
217
|
+
/**
|
|
218
|
+
* Whether a replayed call keeps its item id: only when it goes out as the type it arrived as, since OpenAI item ids
|
|
219
|
+
* name their type (`fc_`, `ctc_`). The `call_id` always survives.
|
|
220
|
+
*/
|
|
221
|
+
export const keepsItemID = (providerMetadata, key, custom) => (providerMetadata?.[key]?.custom === true) === custom;
|
|
222
|
+
export * as OpenAIFreeform from "./openai-freeform.js";
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { ImageModel, type ImageRequest } from "../image.js";
|
|
3
|
+
import { Media } from "../media.js";
|
|
4
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
|
+
import { MediaRoute } from "../route/media.js";
|
|
6
|
+
import { type OpenString } from "../schema/index.js";
|
|
7
|
+
export declare const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
8
|
+
export declare const PATH = "/images/generations";
|
|
9
|
+
export declare const EDIT_PATH = "/images/edits";
|
|
10
|
+
/** Provider-native options. Common fields (`n`, `aspect`, `resolution`, `images`) live on the request. */
|
|
11
|
+
export type OpenAIImageOptions = {
|
|
12
|
+
/** Exact `WIDTHxHEIGHT` or `auto`; it replaces `aspect` and `resolution`, so setting both fails. */
|
|
13
|
+
readonly size?: OpenString<"auto" | "1024x1024" | "1536x1024" | "1024x1536">;
|
|
14
|
+
readonly outputFormat?: OpenString<"png" | "jpeg" | "webp">;
|
|
15
|
+
/** Inpainting mask for the first input image. */
|
|
16
|
+
readonly mask?: Media.Asset;
|
|
17
|
+
readonly quality?: OpenString<"auto" | "low" | "medium" | "high" | "standard" | "hd">;
|
|
18
|
+
readonly background?: OpenString<"auto" | "opaque" | "transparent">;
|
|
19
|
+
readonly moderation?: OpenString<"auto" | "low">;
|
|
20
|
+
readonly outputCompression?: number;
|
|
21
|
+
/** Previews sent before the final image when streaming (default 2); ignored by `Image.generate`. */
|
|
22
|
+
readonly partialImages?: number;
|
|
23
|
+
} & Record<string, unknown>;
|
|
24
|
+
export type Request = ImageRequest<ImageModel<OpenAIImageOptions>>;
|
|
25
|
+
declare const Usage: Schema.Struct<{
|
|
26
|
+
readonly input_tokens: Schema.optional<Schema.Number>;
|
|
27
|
+
readonly output_tokens: Schema.optional<Schema.Number>;
|
|
28
|
+
readonly total_tokens: Schema.optional<Schema.Number>;
|
|
29
|
+
readonly input_tokens_details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
30
|
+
readonly output_tokens_details: Schema.optional<Schema.$Record<Schema.String, Schema.Unknown>>;
|
|
31
|
+
}>;
|
|
32
|
+
/** `generate` reads the whole JSON response as one frame. */
|
|
33
|
+
type Frame = string | {
|
|
34
|
+
readonly document: string;
|
|
35
|
+
};
|
|
36
|
+
interface State {
|
|
37
|
+
readonly completed: number;
|
|
38
|
+
readonly format?: string;
|
|
39
|
+
readonly size?: string;
|
|
40
|
+
readonly quality?: string;
|
|
41
|
+
readonly background?: string;
|
|
42
|
+
readonly usage?: Schema.Schema.Type<typeof Usage>;
|
|
43
|
+
}
|
|
44
|
+
/** The body fields `aspect` and `resolution` lower to; gateways that read other fields than `size` pass their own. */
|
|
45
|
+
export type OpenAIImageSizing = (request: Request) => Record<string, unknown> | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* How input images travel. `edits` is OpenAI's `/images/edits`, uploading owned bytes as multipart; `json-edits` sends
|
|
48
|
+
* every edit there as JSON with data URLs, for hosts that reject multipart (the ChatGPT backend); `references` keeps
|
|
49
|
+
* every request on `/images/generations` and lists them as `input_references`, for gateways without an edits endpoint
|
|
50
|
+
* (OpenRouter).
|
|
51
|
+
*/
|
|
52
|
+
export type OpenAIImageInput = "edits" | "json-edits" | "references";
|
|
53
|
+
export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
54
|
+
readonly type: "image-partial";
|
|
55
|
+
readonly index: number;
|
|
56
|
+
readonly image: Media.Asset;
|
|
57
|
+
} | {
|
|
58
|
+
readonly type: "image";
|
|
59
|
+
readonly index: number;
|
|
60
|
+
readonly image: Media.Asset;
|
|
61
|
+
} | {
|
|
62
|
+
readonly type: "generation-progress";
|
|
63
|
+
readonly id: string;
|
|
64
|
+
readonly progress?: number | undefined;
|
|
65
|
+
} | {
|
|
66
|
+
readonly type: "finish";
|
|
67
|
+
readonly usage?: {
|
|
68
|
+
readonly type: "seconds";
|
|
69
|
+
readonly seconds: number;
|
|
70
|
+
} | {
|
|
71
|
+
readonly type: "characters";
|
|
72
|
+
readonly characters: number;
|
|
73
|
+
} | {
|
|
74
|
+
readonly type: "credits";
|
|
75
|
+
readonly credits: number;
|
|
76
|
+
} | {
|
|
77
|
+
readonly type: "compute";
|
|
78
|
+
readonly seconds: number;
|
|
79
|
+
} | {
|
|
80
|
+
readonly type: "tokens";
|
|
81
|
+
readonly input?: number | undefined;
|
|
82
|
+
readonly output?: number | undefined;
|
|
83
|
+
readonly total?: number | undefined;
|
|
84
|
+
readonly details?: {
|
|
85
|
+
readonly [x: string]: unknown;
|
|
86
|
+
} | undefined;
|
|
87
|
+
} | undefined;
|
|
88
|
+
readonly providerMetadata?: {
|
|
89
|
+
readonly [x: string]: {
|
|
90
|
+
readonly [x: string]: unknown;
|
|
91
|
+
};
|
|
92
|
+
} | undefined;
|
|
93
|
+
}, Frame, State>;
|
|
94
|
+
/**
|
|
95
|
+
* `provider` attributes models, refs, and metadata to an OpenAI-compatible host; it defaults to `openai`. `sizing`
|
|
96
|
+
* replaces the `size` that `aspect` and `resolution` lower to, and `imageInput` how input images are sent.
|
|
97
|
+
*/
|
|
98
|
+
export declare const model: (input: MediaRoute.ModelInput & {
|
|
99
|
+
readonly provider?: string;
|
|
100
|
+
readonly sizing?: OpenAIImageSizing;
|
|
101
|
+
readonly imageInput?: OpenAIImageInput;
|
|
102
|
+
}) => ImageModel<OpenAIImageOptions>;
|
|
103
|
+
export declare const OpenAIImages: {
|
|
104
|
+
readonly protocol: MediaProtocol.Streamed<Request, {
|
|
105
|
+
readonly type: "image-partial";
|
|
106
|
+
readonly index: number;
|
|
107
|
+
readonly image: Media.Asset;
|
|
108
|
+
} | {
|
|
109
|
+
readonly type: "image";
|
|
110
|
+
readonly index: number;
|
|
111
|
+
readonly image: Media.Asset;
|
|
112
|
+
} | {
|
|
113
|
+
readonly type: "generation-progress";
|
|
114
|
+
readonly id: string;
|
|
115
|
+
readonly progress?: number | undefined;
|
|
116
|
+
} | {
|
|
117
|
+
readonly type: "finish";
|
|
118
|
+
readonly usage?: {
|
|
119
|
+
readonly type: "seconds";
|
|
120
|
+
readonly seconds: number;
|
|
121
|
+
} | {
|
|
122
|
+
readonly type: "characters";
|
|
123
|
+
readonly characters: number;
|
|
124
|
+
} | {
|
|
125
|
+
readonly type: "credits";
|
|
126
|
+
readonly credits: number;
|
|
127
|
+
} | {
|
|
128
|
+
readonly type: "compute";
|
|
129
|
+
readonly seconds: number;
|
|
130
|
+
} | {
|
|
131
|
+
readonly type: "tokens";
|
|
132
|
+
readonly input?: number | undefined;
|
|
133
|
+
readonly output?: number | undefined;
|
|
134
|
+
readonly total?: number | undefined;
|
|
135
|
+
readonly details?: {
|
|
136
|
+
readonly [x: string]: unknown;
|
|
137
|
+
} | undefined;
|
|
138
|
+
} | undefined;
|
|
139
|
+
readonly providerMetadata?: {
|
|
140
|
+
readonly [x: string]: {
|
|
141
|
+
readonly [x: string]: unknown;
|
|
142
|
+
};
|
|
143
|
+
} | undefined;
|
|
144
|
+
}, Frame, State>;
|
|
145
|
+
readonly model: typeof model;
|
|
146
|
+
readonly pixelSize: OpenAIImageSizing;
|
|
147
|
+
};
|
|
148
|
+
export {};
|