@opencode/ai 2.1.3 → 2.2.0
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 +283 -114
- package/dist/ai-client.d.ts +2 -2
- package/dist/ai-client.js +2 -1
- package/dist/cache-policy.d.ts +4 -0
- package/dist/cache-policy.js +20 -6
- package/dist/decision-client.d.ts +21 -0
- package/dist/decision-client.js +55 -0
- package/dist/decision.d.ts +472 -0
- package/dist/decision.js +158 -0
- package/dist/effect.d.ts +38 -0
- package/dist/effect.js +27 -0
- package/dist/effort-updates.d.ts +2 -2
- package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
- package/dist/experimental/cloudflare-run-extraction.js +83 -0
- package/dist/experimental/extraction.d.ts +183 -0
- package/dist/experimental/extraction.js +102 -0
- package/dist/experimental/realtime.d.ts +7 -2
- package/dist/experimental/realtime.js +5 -5
- package/dist/experimental.d.ts +2 -3
- package/dist/experimental.js +1 -2
- package/dist/image-client.d.ts +13 -22
- package/dist/image-client.js +0 -1
- package/dist/image.d.ts +154 -324
- package/dist/image.js +4 -11
- package/dist/index.d.ts +1426 -34
- package/dist/index.js +127 -24
- package/dist/llm.d.ts +16 -1
- package/dist/llm.js +34 -0
- package/dist/media-client.d.ts +2 -1
- package/dist/media-model.d.ts +3 -0
- package/dist/media-model.js +4 -1
- package/dist/media.d.ts +0 -7
- package/dist/media.js +0 -6
- package/dist/protocols/alibaba-chat.d.ts +32 -12
- package/dist/protocols/alibaba-images.d.ts +22 -0
- package/dist/protocols/alibaba-images.js +77 -0
- package/dist/protocols/alibaba-messages.d.ts +62 -27
- package/dist/protocols/alibaba-responses.d.ts +41 -38
- package/dist/protocols/anthropic-messages.d.ts +196 -94
- package/dist/protocols/anthropic-messages.js +47 -16
- package/dist/protocols/bedrock-converse.d.ts +140 -47
- package/dist/protocols/bedrock-converse.js +114 -18
- package/dist/protocols/bedrock-event-stream.js +1 -0
- package/dist/protocols/bfl-images.d.ts +9 -0
- package/dist/protocols/bfl-images.js +17 -15
- package/dist/protocols/cartesia-speech.d.ts +32 -50
- 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 +7 -8
- package/dist/protocols/deepgram-speech.d.ts +32 -50
- package/dist/protocols/elevenlabs-speech.d.ts +32 -50
- package/dist/protocols/fal-images.d.ts +10 -1
- package/dist/protocols/fal-images.js +16 -25
- package/dist/protocols/fal-video.js +1 -1
- package/dist/protocols/gemini.d.ts +37 -34
- package/dist/protocols/gemini.js +13 -8
- package/dist/protocols/google-images.d.ts +2 -2
- package/dist/protocols/google-images.js +8 -37
- package/dist/protocols/google-interactions.d.ts +18 -14
- package/dist/protocols/google-interactions.js +14 -7
- package/dist/protocols/google-speech.d.ts +32 -50
- package/dist/protocols/google-speech.js +0 -1
- package/dist/protocols/google-transcription.d.ts +32 -50
- package/dist/protocols/google-transcription.js +0 -1
- package/dist/protocols/google-video.d.ts +1 -1
- package/dist/protocols/google-video.js +2 -7
- package/dist/protocols/index.d.ts +3 -0
- package/dist/protocols/index.js +3 -0
- package/dist/protocols/meta-images.d.ts +7 -1
- package/dist/protocols/meta-images.js +10 -8
- package/dist/protocols/meta-messages.d.ts +73 -38
- package/dist/protocols/meta-messages.js +14 -1
- package/dist/protocols/meta-responses.d.ts +84 -78
- package/dist/protocols/meta-responses.js +29 -1
- package/dist/protocols/mistral-chat.d.ts +81 -78
- package/dist/protocols/mistral-chat.js +2 -1
- package/dist/protocols/open-responses-channel.js +47 -46
- package/dist/protocols/open-responses-continuation.d.ts +8 -2
- package/dist/protocols/open-responses-continuation.js +3 -1
- package/dist/protocols/open-responses.d.ts +426 -396
- package/dist/protocols/open-responses.js +16 -25
- package/dist/protocols/openai-chat.d.ts +764 -52
- package/dist/protocols/openai-chat.js +38 -25
- package/dist/protocols/openai-compatible-chat.d.ts +35 -15
- package/dist/protocols/openai-compatible-responses.d.ts +44 -42
- package/dist/protocols/openai-decisions.d.ts +22 -0
- package/dist/protocols/openai-decisions.js +128 -0
- package/dist/protocols/openai-images.d.ts +53 -56
- package/dist/protocols/openai-images.js +100 -34
- package/dist/protocols/openai-responses.d.ts +322 -264
- package/dist/protocols/openai-responses.js +33 -3
- package/dist/protocols/openai-speech.d.ts +32 -50
- package/dist/protocols/openai-speech.js +15 -5
- package/dist/protocols/openai-transcription.d.ts +32 -50
- package/dist/protocols/openai-transcription.js +15 -11
- package/dist/protocols/replicate-images.js +1 -1
- package/dist/protocols/runway-video.d.ts +1 -1
- package/dist/protocols/runway-video.js +1 -1
- package/dist/protocols/shared.d.ts +9 -1
- package/dist/protocols/shared.js +13 -2
- package/dist/protocols/stability-images.d.ts +5 -0
- package/dist/protocols/stability-images.js +4 -9
- package/dist/protocols/system-one.d.ts +101 -0
- package/dist/protocols/system-one.js +230 -0
- package/dist/protocols/utils/bedrock-cache.d.ts +2 -2
- package/dist/protocols/utils/bedrock-cache.js +7 -6
- package/dist/protocols/utils/cache.d.ts +14 -3
- package/dist/protocols/utils/cache.js +11 -4
- package/dist/protocols/utils/gemini-generate-content.d.ts +3 -3
- package/dist/protocols/utils/gemini-generate-content.js +10 -16
- package/dist/protocols/utils/gemini-json-schema.d.ts +5 -0
- package/dist/protocols/utils/gemini-json-schema.js +3 -3
- package/dist/protocols/utils/image-sizing.d.ts +31 -0
- package/dist/protocols/utils/image-sizing.js +73 -0
- package/dist/protocols/utils/media-input.d.ts +4 -5
- package/dist/protocols/utils/media-input.js +8 -5
- package/dist/protocols/utils/partial-json-options.d.ts +0 -1
- package/dist/protocols/utils/partial-json-options.js +0 -1
- package/dist/protocols/utils/speech-stream.d.ts +0 -1
- package/dist/protocols/utils/speech-stream.js +0 -1
- package/dist/protocols/utils/tool-schema.js +15 -5
- package/dist/protocols/utils/tool-stream.d.ts +10 -1
- package/dist/protocols/venice-chat.d.ts +71 -53
- package/dist/protocols/venice-chat.js +7 -6
- package/dist/protocols/xai-images.d.ts +1 -2
- package/dist/protocols/xai-images.js +9 -2
- package/dist/protocols/xai-responses.d.ts +88 -78
- package/dist/protocols/xai-responses.js +28 -2
- package/dist/protocols/xai-video.d.ts +1 -1
- package/dist/protocols/xai-video.js +11 -8
- package/dist/protocols/zai-chat.d.ts +54 -30
- package/dist/protocols/zai-chat.js +8 -3
- package/dist/protocols/zai-images.d.ts +3 -1
- package/dist/protocols/zai-images.js +12 -11
- package/dist/protocols/zai-messages.d.ts +72 -35
- package/dist/protocols/zai-messages.js +3 -2
- package/dist/provider-error.js +24 -7
- package/dist/providers/alibaba/images.d.ts +2 -0
- package/dist/providers/alibaba/images.js +1 -0
- package/dist/providers/alibaba.d.ts +146 -85
- package/dist/providers/alibaba.js +16 -8
- package/dist/providers/amazon-bedrock-mantle.d.ts +277 -218
- package/dist/providers/amazon-bedrock.d.ts +48 -28
- package/dist/providers/amazon-bedrock.js +11 -11
- package/dist/providers/anthropic-compatible.d.ts +64 -29
- package/dist/providers/anthropic.d.ts +64 -29
- package/dist/providers/azure/images.d.ts +2 -0
- package/dist/providers/azure/images.js +1 -0
- package/dist/providers/azure.d.ts +156 -115
- package/dist/providers/azure.js +19 -1
- package/dist/providers/baseten/decisions.d.ts +2 -0
- package/dist/providers/baseten/decisions.js +1 -0
- package/dist/providers/baseten.d.ts +75 -25
- package/dist/providers/baseten.js +41 -8
- package/dist/providers/cerebras.d.ts +66 -24
- 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 +409 -272
- package/dist/providers/cloudflare-ai-gateway.js +33 -6
- 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 +79 -25
- package/dist/providers/cloudflare-workers-ai.js +45 -9
- package/dist/providers/cohere.d.ts +117 -75
- package/dist/providers/deepinfra.d.ts +66 -24
- package/dist/providers/deepseek.d.ts +66 -24
- package/dist/providers/digitalocean/decisions.d.ts +2 -0
- package/dist/providers/digitalocean/decisions.js +1 -0
- package/dist/providers/digitalocean/images.d.ts +2 -0
- package/dist/providers/digitalocean/images.js +1 -0
- package/dist/providers/digitalocean.d.ts +108 -37
- package/dist/providers/digitalocean.js +39 -8
- package/dist/providers/fireworks.d.ts +66 -24
- package/dist/providers/google/images.d.ts +2 -0
- package/dist/providers/google/images.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-chat.d.ts +33 -12
- package/dist/providers/google-vertex-messages.d.ts +64 -29
- package/dist/providers/google-vertex-mistral.d.ts +17 -17
- package/dist/providers/google-vertex-responses.d.ts +38 -35
- package/dist/providers/google-vertex.d.ts +20 -18
- package/dist/providers/google-vertex.js +32 -15
- package/dist/providers/google.d.ts +22 -21
- package/dist/providers/google.js +15 -8
- package/dist/providers/groq.d.ts +66 -28
- package/dist/providers/groq.js +0 -6
- package/dist/providers/meta.d.ts +144 -85
- package/dist/providers/minimax.d.ts +145 -86
- package/dist/providers/mistral.d.ts +28 -28
- package/dist/providers/moonshot.d.ts +277 -218
- package/dist/providers/open-responses-options.d.ts +0 -1
- package/dist/providers/open-responses-options.js +1 -1
- package/dist/providers/openai/images.d.ts +2 -0
- package/dist/providers/openai/images.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 +38 -35
- package/dist/providers/openai-compatible.d.ts +36 -13
- package/dist/providers/openai-compatible.js +22 -10
- package/dist/providers/openai-options.d.ts +3 -2
- package/dist/providers/openai-options.js +0 -1
- package/dist/providers/openai.d.ts +146 -109
- package/dist/providers/openai.js +20 -4
- package/dist/providers/opencode/decisions.d.ts +2 -0
- package/dist/providers/opencode/decisions.js +1 -0
- package/dist/providers/opencode.d.ts +9 -10
- package/dist/providers/opencode.js +10 -8
- package/dist/providers/openrouter/decisions.d.ts +2 -0
- package/dist/providers/openrouter/decisions.js +1 -0
- package/dist/providers/openrouter/images.d.ts +2 -0
- package/dist/providers/openrouter/images.js +1 -0
- package/dist/providers/openrouter.d.ts +128 -47
- package/dist/providers/openrouter.js +38 -16
- package/dist/providers/togetherai.d.ts +66 -24
- 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 +9 -10
- package/dist/providers/typesafe-ai.js +10 -8
- package/dist/providers/venice.d.ts +214 -20
- 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/images.d.ts +2 -0
- package/dist/providers/vercel-ai-gateway/images.js +1 -0
- package/dist/providers/vercel-ai-gateway.d.ts +10 -16
- package/dist/providers/vercel-ai-gateway.js +31 -85
- package/dist/providers/xai/images.d.ts +2 -0
- package/dist/providers/xai/images.js +1 -0
- package/dist/providers/xai.d.ts +40 -17
- package/dist/providers/xai.js +15 -8
- package/dist/providers/zai/images.d.ts +2 -0
- package/dist/providers/zai/images.js +1 -0
- package/dist/providers/zai-coding-plan.d.ts +224 -85
- package/dist/providers/zai.d.ts +110 -23
- package/dist/providers/zai.js +15 -8
- package/dist/route/auth-options.d.ts +0 -3
- package/dist/route/auth.d.ts +0 -2
- package/dist/route/auth.js +0 -2
- package/dist/route/client.d.ts +2 -4
- package/dist/route/client.js +29 -8
- package/dist/route/decision.d.ts +58 -0
- package/dist/route/decision.js +75 -0
- package/dist/route/executor-service.js +1 -1
- package/dist/route/executor.d.ts +1 -1
- package/dist/route/framing.d.ts +2 -0
- package/dist/route/index.d.ts +3 -0
- package/dist/route/index.js +3 -0
- package/dist/route/media-protocol.d.ts +1 -0
- package/dist/route/media-protocol.js +12 -8
- package/dist/route/media.d.ts +5 -1
- package/dist/route/media.js +6 -2
- package/dist/route/transport/http.js +1 -0
- package/dist/route/transport/index.d.ts +1 -0
- package/dist/schema/events.d.ts +148 -6
- package/dist/schema/events.js +20 -0
- package/dist/schema/messages.d.ts +6 -2
- package/dist/schema/messages.js +3 -0
- package/dist/schema/options.d.ts +5 -3
- package/dist/schema/options.js +8 -3
- package/dist/speech-client.d.ts +13 -22
- package/dist/speech-client.js +0 -1
- package/dist/speech.d.ts +148 -283
- package/dist/speech.js +2 -4
- package/dist/testing.d.ts +25 -24
- package/dist/testing.js +3 -21
- package/dist/transcription-client.d.ts +13 -22
- package/dist/transcription.d.ts +148 -283
- package/dist/transcription.js +1 -1
- package/dist/utils/media-type.js +22 -1
- package/dist/video-client.d.ts +13 -22
- package/dist/video-client.js +0 -1
- package/dist/video.d.ts +115 -250
- package/dist/video.js +3 -5
- package/package.json +516 -13
- package/dist/experimental/evaluation-client.d.ts +0 -21
- package/dist/experimental/evaluation-client.js +0 -64
- package/dist/experimental/evaluation.d.ts +0 -242
- package/dist/experimental/evaluation.js +0 -115
- package/dist/experimental/system-one.d.ts +0 -16
- package/dist/experimental/system-one.js +0 -135
- package/dist/promise.d.ts +0 -1021
- package/dist/promise.js +0 -101
- package/dist/provider.d.ts +0 -23
- package/dist/provider.js +0 -2
|
@@ -9,7 +9,9 @@ import { Protocol } from "../route/protocol.js";
|
|
|
9
9
|
import { AIError, AIErrorReason, InvalidProviderOutputError, LLMEvent, ProviderInternalError, UnknownProviderError, Usage, } from "../schema/index.js";
|
|
10
10
|
import { classifyProviderFailure } from "../provider-error.js";
|
|
11
11
|
import { isRecord, JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js";
|
|
12
|
-
import {
|
|
12
|
+
import { placesMarkers, requestTTL } from "../cache-policy.js";
|
|
13
|
+
import * as Cache from "./utils/cache.js";
|
|
14
|
+
import { resolveParallelToolCalls } from "./open-responses.js";
|
|
13
15
|
import { OpenResponsesOptions } from "./utils/open-responses-options.js";
|
|
14
16
|
import { Lifecycle } from "./utils/lifecycle.js";
|
|
15
17
|
import { ToolStream } from "./utils/tool-stream.js";
|
|
@@ -90,6 +92,10 @@ const OpenAIChatUserContent = Schema.Union([
|
|
|
90
92
|
type: Schema.Literal("image_url"),
|
|
91
93
|
image_url: Schema.Struct({ url: Schema.String }),
|
|
92
94
|
}),
|
|
95
|
+
Schema.Struct({
|
|
96
|
+
type: Schema.Literal("video_url"),
|
|
97
|
+
video_url: Schema.Struct({ url: Schema.String }),
|
|
98
|
+
}),
|
|
93
99
|
Schema.Struct({
|
|
94
100
|
type: Schema.Literal("file"),
|
|
95
101
|
file: Schema.Struct({ filename: Schema.String, file_data: Schema.String }),
|
|
@@ -135,7 +141,14 @@ export const bodyFields = {
|
|
|
135
141
|
stream_options: Schema.optional(Schema.Struct({ include_usage: Schema.Boolean })),
|
|
136
142
|
store: Schema.optional(Schema.Boolean),
|
|
137
143
|
prompt_cache_key: Schema.optional(Schema.String),
|
|
144
|
+
prompt_cache_retention: Schema.optional(Schema.String),
|
|
145
|
+
prompt_cache_options: Schema.optional(Schema.Struct({ ttl: Schema.optional(Schema.String) })),
|
|
138
146
|
reasoning_effort: Schema.optional(OpenResponsesOptions.ReasoningEffort),
|
|
147
|
+
verbosity: Schema.optional(OpenResponsesOptions.TextVerbositySchema),
|
|
148
|
+
service_tier: Schema.optional(OpenResponsesOptions.ServiceTierSchema),
|
|
149
|
+
parallel_tool_calls: Schema.optional(Schema.Boolean),
|
|
150
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
151
|
+
safety_identifier: Schema.optional(Schema.String),
|
|
139
152
|
tool_stream: Schema.optional(Schema.Boolean),
|
|
140
153
|
max_completion_tokens: Schema.optional(Schema.Number),
|
|
141
154
|
max_tokens: Schema.optional(Schema.Number),
|
|
@@ -244,9 +257,11 @@ const lowerMedia = Effect.fnUntraced(function* (part) {
|
|
|
244
257
|
file_data: (yield* ProviderShared.requireInlineMedia("OpenAI Chat", part.media)).dataUrl,
|
|
245
258
|
},
|
|
246
259
|
};
|
|
247
|
-
if (part.media.kind !== "image")
|
|
260
|
+
if (part.media.kind !== "image" && part.media.kind !== "video")
|
|
248
261
|
return yield* ProviderShared.invalidRequest(`OpenAI Chat does not support media type ${part.media.mediaType}`);
|
|
249
262
|
const url = ProviderShared.mediaUrl(part.media) ?? (yield* ProviderShared.requireInlineMedia("OpenAI Chat", part.media)).dataUrl;
|
|
263
|
+
if (part.media.kind === "video")
|
|
264
|
+
return { type: "video_url", video_url: { url } };
|
|
250
265
|
return { type: "image_url", image_url: { url } };
|
|
251
266
|
});
|
|
252
267
|
const openAICompatibleReasoningContent = (native) => isRecord(native) && typeof native.reasoning_content === "string" ? native.reasoning_content : undefined;
|
|
@@ -419,7 +434,7 @@ const lowerMessages = Effect.fnUntraced(function* (request, options) {
|
|
|
419
434
|
const mistral = ["mistral", "devstral", "codestral", "pixtral", "mixtral"].some((family) => modelID.includes(family));
|
|
420
435
|
const lowering = {
|
|
421
436
|
...options,
|
|
422
|
-
providerMetadataKey: request.model.route.providerMetadataKey ??
|
|
437
|
+
providerMetadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
423
438
|
toolCallID: (id) => {
|
|
424
439
|
if (mistral)
|
|
425
440
|
return id
|
|
@@ -587,8 +602,10 @@ const detectZaiToolStream = (provider, baseURL, modelID) => {
|
|
|
587
602
|
return false;
|
|
588
603
|
return true;
|
|
589
604
|
};
|
|
590
|
-
const lowerOptions = (request, supportsStore) => {
|
|
605
|
+
const lowerOptions = (request, supportsStore, hasActiveTools) => {
|
|
591
606
|
const options = OpenResponsesOptions.resolve(request);
|
|
607
|
+
// OpenAI rejects `parallel_tool_calls` unless tools are present.
|
|
608
|
+
const parallelToolCalls = hasActiveTools ? resolveParallelToolCalls(request, options) : undefined;
|
|
592
609
|
// Default off: strict providers 400 on unknown body fields, so only send
|
|
593
610
|
// the key where compatibility explicitly allows it. Header-based affinity
|
|
594
611
|
// (x-session-affinity, x-grok-conv-id, ...) is unaffected.
|
|
@@ -596,7 +613,14 @@ const lowerOptions = (request, supportsStore) => {
|
|
|
596
613
|
return {
|
|
597
614
|
...(supportsStore ? { store: options.store ?? false } : {}),
|
|
598
615
|
...(cacheKey ? { prompt_cache_key: cacheKey } : {}),
|
|
616
|
+
// Inline markers carry the lifetime when the policy places them.
|
|
617
|
+
...(placesMarkers(request) ? {} : Cache.openAIRequestTTL(requestTTL(request))),
|
|
599
618
|
...(options.reasoningEffort ? { reasoning_effort: options.reasoningEffort } : {}),
|
|
619
|
+
...(options.textVerbosity ? { verbosity: options.textVerbosity } : {}),
|
|
620
|
+
...(options.serviceTier ? { service_tier: options.serviceTier } : {}),
|
|
621
|
+
...(parallelToolCalls !== undefined ? { parallel_tool_calls: parallelToolCalls } : {}),
|
|
622
|
+
...(options.metadata ? { metadata: options.metadata } : {}),
|
|
623
|
+
...(options.safetyIdentifier ? { safety_identifier: options.safetyIdentifier } : {}),
|
|
600
624
|
};
|
|
601
625
|
};
|
|
602
626
|
export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (request, options = {}) {
|
|
@@ -607,7 +631,7 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
|
|
|
607
631
|
return yield* ProviderShared.invalidRequest(`OpenAI Chat reasoning field conflicts with reserved field ${reasoningField}`);
|
|
608
632
|
const generation = request.generation;
|
|
609
633
|
const flattened = ProviderShared.flattenToolRequest(request);
|
|
610
|
-
const provider =
|
|
634
|
+
const provider = request.model.provider;
|
|
611
635
|
const baseURL = request.model.route.endpoint.baseURL;
|
|
612
636
|
const detectedMaxTokensField = detectMaxTokensField(provider, baseURL);
|
|
613
637
|
const maxTokensField = request.model.compatibility?.maxTokensField ?? detectedMaxTokensField;
|
|
@@ -638,11 +662,11 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
|
|
|
638
662
|
presence_penalty: generation?.presencePenalty,
|
|
639
663
|
seed: generation?.seed,
|
|
640
664
|
stop: generation?.stop,
|
|
641
|
-
...lowerOptions(request, supportsStore),
|
|
665
|
+
...lowerOptions(request, supportsStore, hasActiveTools),
|
|
642
666
|
};
|
|
643
667
|
});
|
|
644
668
|
// Provider wrappers that pass their own marker writer keep it; otherwise a declared cache format adds one.
|
|
645
|
-
const declaredCacheControl = (request) => request.model.compatibility?.cacheFormat === undefined ? undefined : cacheControl();
|
|
669
|
+
const declaredCacheControl = (request) => request.model.compatibility?.cacheFormat === undefined ? undefined : Cache.cacheControl();
|
|
646
670
|
// =============================================================================
|
|
647
671
|
// Stream Parsing
|
|
648
672
|
// =============================================================================
|
|
@@ -676,12 +700,7 @@ const mapFinishReason = Effect.fnUntraced(function* (event, reason) {
|
|
|
676
700
|
case "tool_calls":
|
|
677
701
|
return "tool-calls";
|
|
678
702
|
default:
|
|
679
|
-
return
|
|
680
|
-
reason: new UnknownProviderError({
|
|
681
|
-
message: `Provider finish_reason: ${reason}`,
|
|
682
|
-
body: ProviderShared.encodeJson(event),
|
|
683
|
-
}),
|
|
684
|
-
});
|
|
703
|
+
return "unknown";
|
|
685
704
|
}
|
|
686
705
|
});
|
|
687
706
|
// OpenAI Chat reports `prompt_tokens` (inclusive total) with a
|
|
@@ -714,6 +733,7 @@ const mapUsage = (usage, providerMetadataKey) => {
|
|
|
714
733
|
cacheWriteInputTokens: cacheWrite,
|
|
715
734
|
reasoningTokens: reasoning,
|
|
716
735
|
totalTokens: ProviderShared.totalTokens(input, output, usage.total_tokens ?? undefined),
|
|
736
|
+
...ProviderShared.reportedBilling(usage),
|
|
717
737
|
providerMetadata: { [providerMetadataKey]: usage },
|
|
718
738
|
});
|
|
719
739
|
};
|
|
@@ -839,16 +859,9 @@ const step = (state, event) => Effect.gen(function* () {
|
|
|
839
859
|
let nextToolIndex = state.nextToolIndex;
|
|
840
860
|
let lifecycle = state.lifecycle;
|
|
841
861
|
const reasoning = reasoningDelta(delta, state.reasoningField);
|
|
842
|
-
|
|
843
|
-
Boolean(
|
|
844
|
-
|
|
845
|
-
(Array.isArray(delta?.reasoning_details) && delta.reasoning_details.length > 0) ||
|
|
846
|
-
toolDeltas.some((tool) => Boolean(tool.id) || Boolean(tool.function?.name) || Boolean(tool.function?.arguments));
|
|
847
|
-
if (state.finishReason !== undefined) {
|
|
848
|
-
if (hasLateContent)
|
|
849
|
-
return yield* ProviderShared.eventError(ADAPTER, "OpenAI Chat received content after the finish reason", ProviderShared.encodeJson(event));
|
|
850
|
-
return [{ ...state, usage }, events];
|
|
851
|
-
}
|
|
862
|
+
if (state.finishReason !== undefined &&
|
|
863
|
+
toolDeltas.some((tool) => Boolean(tool.id) || Boolean(tool.function?.name) || Boolean(tool.function?.arguments)))
|
|
864
|
+
return yield* ProviderShared.eventError(ADAPTER, "OpenAI Chat received content after the finish reason", ProviderShared.encodeJson(event));
|
|
852
865
|
const reasoningField = state.reasoningField ?? reasoning?.field;
|
|
853
866
|
const reasoningTextObserved = state.reasoningTextObserved || reasoning !== undefined;
|
|
854
867
|
const detailDelta = Array.isArray(delta?.reasoning_details)
|
|
@@ -941,7 +954,7 @@ const step = (state, event) => Effect.gen(function* () {
|
|
|
941
954
|
pendingTools,
|
|
942
955
|
toolCallEvents: finished?.events ?? state.toolCallEvents,
|
|
943
956
|
usage,
|
|
944
|
-
finishReason,
|
|
957
|
+
finishReason: state.finishReason ?? finishReason,
|
|
945
958
|
lifecycle,
|
|
946
959
|
reasoningField,
|
|
947
960
|
reasoningTextObserved,
|
|
@@ -1005,7 +1018,7 @@ export const protocol = Protocol.make({
|
|
|
1005
1018
|
stream: {
|
|
1006
1019
|
event: OpenAIChatStreamEvent,
|
|
1007
1020
|
initial: (request) => ({
|
|
1008
|
-
providerMetadataKey: request.model.route.providerMetadataKey ??
|
|
1021
|
+
providerMetadataKey: request.model.route.providerMetadataKey ?? request.model.provider,
|
|
1009
1022
|
tools: ToolStream.empty(),
|
|
1010
1023
|
pendingTools: {},
|
|
1011
1024
|
toolCallEvents: [],
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Route
|
|
2
|
-
export type OpenAICompatibleChatLanguageModelInput = RouteRoutedLanguageModelInput;
|
|
1
|
+
import { Route } from "../route/client.js";
|
|
3
2
|
/**
|
|
4
3
|
* Route for non-OpenAI providers that expose an OpenAI Chat-compatible
|
|
5
4
|
* `/chat/completions` endpoint. Reuses `OpenAIChat.protocol` end-to-end and
|
|
@@ -12,44 +11,54 @@ export declare const route: Route<{
|
|
|
12
11
|
readonly messages: readonly ({
|
|
13
12
|
readonly role: "system";
|
|
14
13
|
readonly content: string | readonly ({
|
|
15
|
-
readonly type: "text";
|
|
16
|
-
readonly text: string;
|
|
17
|
-
readonly cache_control?: {
|
|
18
|
-
readonly type: "ephemeral";
|
|
19
|
-
readonly ttl?: string | undefined;
|
|
20
|
-
} | undefined;
|
|
21
|
-
} | {
|
|
22
14
|
readonly type: "image_url";
|
|
23
15
|
readonly image_url: {
|
|
24
16
|
readonly url: string;
|
|
25
17
|
};
|
|
18
|
+
} | {
|
|
19
|
+
readonly type: "video_url";
|
|
20
|
+
readonly video_url: {
|
|
21
|
+
readonly url: string;
|
|
22
|
+
};
|
|
26
23
|
} | {
|
|
27
24
|
readonly type: "file";
|
|
28
25
|
readonly file: {
|
|
29
26
|
readonly filename: string;
|
|
30
27
|
readonly file_data: string;
|
|
31
28
|
};
|
|
32
|
-
}
|
|
33
|
-
} | {
|
|
34
|
-
readonly role: "user";
|
|
35
|
-
readonly content: string | readonly ({
|
|
29
|
+
} | {
|
|
36
30
|
readonly type: "text";
|
|
37
31
|
readonly text: string;
|
|
38
32
|
readonly cache_control?: {
|
|
39
33
|
readonly type: "ephemeral";
|
|
40
34
|
readonly ttl?: string | undefined;
|
|
41
35
|
} | undefined;
|
|
42
|
-
}
|
|
36
|
+
})[];
|
|
37
|
+
} | {
|
|
38
|
+
readonly role: "user";
|
|
39
|
+
readonly content: string | readonly ({
|
|
43
40
|
readonly type: "image_url";
|
|
44
41
|
readonly image_url: {
|
|
45
42
|
readonly url: string;
|
|
46
43
|
};
|
|
44
|
+
} | {
|
|
45
|
+
readonly type: "video_url";
|
|
46
|
+
readonly video_url: {
|
|
47
|
+
readonly url: string;
|
|
48
|
+
};
|
|
47
49
|
} | {
|
|
48
50
|
readonly type: "file";
|
|
49
51
|
readonly file: {
|
|
50
52
|
readonly filename: string;
|
|
51
53
|
readonly file_data: string;
|
|
52
54
|
};
|
|
55
|
+
} | {
|
|
56
|
+
readonly type: "text";
|
|
57
|
+
readonly text: string;
|
|
58
|
+
readonly cache_control?: {
|
|
59
|
+
readonly type: "ephemeral";
|
|
60
|
+
readonly ttl?: string | undefined;
|
|
61
|
+
} | undefined;
|
|
53
62
|
})[];
|
|
54
63
|
} | {
|
|
55
64
|
readonly role: "tool";
|
|
@@ -118,7 +127,18 @@ export declare const route: Route<{
|
|
|
118
127
|
} | undefined;
|
|
119
128
|
readonly store?: boolean | undefined;
|
|
120
129
|
readonly prompt_cache_key?: string | undefined;
|
|
121
|
-
readonly
|
|
130
|
+
readonly prompt_cache_retention?: string | undefined;
|
|
131
|
+
readonly prompt_cache_options?: {
|
|
132
|
+
readonly ttl?: string | undefined;
|
|
133
|
+
} | undefined;
|
|
134
|
+
readonly reasoning_effort?: import("../effect.js").ReasoningEffort | undefined;
|
|
135
|
+
readonly verbosity?: import("../schema/options.js").OpenString<"high" | "low" | "medium"> | undefined;
|
|
136
|
+
readonly service_tier?: import("../schema/options.js").OpenString<"auto" | "default" | "flex" | "priority"> | undefined;
|
|
137
|
+
readonly parallel_tool_calls?: boolean | undefined;
|
|
138
|
+
readonly metadata?: {
|
|
139
|
+
readonly [x: string]: string;
|
|
140
|
+
} | undefined;
|
|
141
|
+
readonly safety_identifier?: string | undefined;
|
|
122
142
|
readonly tool_stream?: boolean | undefined;
|
|
123
143
|
readonly max_completion_tokens?: number | undefined;
|
|
124
144
|
readonly max_tokens?: number | undefined;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { Route
|
|
2
|
-
export type OpenAICompatibleResponsesLanguageModelInput = RouteRoutedLanguageModelInput;
|
|
1
|
+
import { Route } from "../route/client.js";
|
|
3
2
|
/**
|
|
4
3
|
* Deployment adapter for providers that expose an Open Responses-compatible
|
|
5
4
|
* `/responses` endpoint. Provider helpers configure identity, endpoint, and
|
|
@@ -8,13 +7,9 @@ export type OpenAICompatibleResponsesLanguageModelInput = RouteRoutedLanguageMod
|
|
|
8
7
|
export declare const route: Route<{
|
|
9
8
|
readonly model: string;
|
|
10
9
|
readonly input: readonly ({
|
|
11
|
-
readonly type: "compaction";
|
|
12
|
-
readonly id?: string | null | undefined;
|
|
13
|
-
readonly encrypted_content: string;
|
|
14
|
-
} | {
|
|
15
10
|
readonly type: "configuration_update";
|
|
16
11
|
readonly reasoning: {
|
|
17
|
-
readonly effort: import("../
|
|
12
|
+
readonly effort: import("../effect.js").ReasoningEffort;
|
|
18
13
|
};
|
|
19
14
|
} | {
|
|
20
15
|
readonly type: "message";
|
|
@@ -24,10 +19,48 @@ export declare const route: Route<{
|
|
|
24
19
|
readonly type: "message";
|
|
25
20
|
readonly role: "developer";
|
|
26
21
|
readonly content: string;
|
|
22
|
+
} | {
|
|
23
|
+
readonly type: "function_call_output";
|
|
24
|
+
readonly call_id: string;
|
|
25
|
+
readonly output: string | readonly ({
|
|
26
|
+
readonly type: "input_text";
|
|
27
|
+
readonly text: string;
|
|
28
|
+
} | {
|
|
29
|
+
readonly type: "input_video";
|
|
30
|
+
readonly video_url: string;
|
|
31
|
+
} | {
|
|
32
|
+
readonly type: "input_image";
|
|
33
|
+
readonly image_url: string;
|
|
34
|
+
readonly detail?: string | undefined;
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "input_file";
|
|
37
|
+
readonly filename: string;
|
|
38
|
+
readonly detail?: string | undefined;
|
|
39
|
+
readonly file_data?: string | undefined;
|
|
40
|
+
readonly file_url?: string | undefined;
|
|
41
|
+
})[];
|
|
42
|
+
} | {
|
|
43
|
+
readonly type: "reasoning";
|
|
44
|
+
readonly id?: string | undefined;
|
|
45
|
+
readonly summary: readonly {
|
|
46
|
+
readonly type: "summary_text";
|
|
47
|
+
readonly text: string;
|
|
48
|
+
}[];
|
|
49
|
+
readonly encrypted_content?: string | null | undefined;
|
|
50
|
+
} | {
|
|
51
|
+
readonly type: "compaction";
|
|
52
|
+
readonly id?: string | null | undefined;
|
|
53
|
+
readonly encrypted_content: string;
|
|
27
54
|
} | {
|
|
28
55
|
readonly type: "message";
|
|
29
56
|
readonly role: "user";
|
|
30
57
|
readonly content: readonly ({
|
|
58
|
+
readonly type: "input_text";
|
|
59
|
+
readonly text: string;
|
|
60
|
+
} | {
|
|
61
|
+
readonly type: "input_video";
|
|
62
|
+
readonly video_url: string;
|
|
63
|
+
} | {
|
|
31
64
|
readonly type: "input_image";
|
|
32
65
|
readonly image_url: string;
|
|
33
66
|
readonly detail?: string | undefined;
|
|
@@ -37,9 +70,6 @@ export declare const route: Route<{
|
|
|
37
70
|
readonly detail?: string | undefined;
|
|
38
71
|
readonly file_data?: string | undefined;
|
|
39
72
|
readonly file_url?: string | undefined;
|
|
40
|
-
} | {
|
|
41
|
-
readonly type: "input_text";
|
|
42
|
-
readonly text: string;
|
|
43
73
|
})[];
|
|
44
74
|
readonly id?: string | undefined;
|
|
45
75
|
readonly status?: string | undefined;
|
|
@@ -53,14 +83,6 @@ export declare const route: Route<{
|
|
|
53
83
|
}[];
|
|
54
84
|
readonly phase?: "commentary" | "final_answer" | null | undefined;
|
|
55
85
|
readonly status?: string | undefined;
|
|
56
|
-
} | {
|
|
57
|
-
readonly type: "reasoning";
|
|
58
|
-
readonly id?: string | undefined;
|
|
59
|
-
readonly summary: readonly {
|
|
60
|
-
readonly type: "summary_text";
|
|
61
|
-
readonly text: string;
|
|
62
|
-
}[];
|
|
63
|
-
readonly encrypted_content?: string | null | undefined;
|
|
64
86
|
} | {
|
|
65
87
|
readonly type: "function_call";
|
|
66
88
|
readonly id?: string | undefined;
|
|
@@ -68,26 +90,6 @@ export declare const route: Route<{
|
|
|
68
90
|
readonly name: string;
|
|
69
91
|
readonly namespace?: string | undefined;
|
|
70
92
|
readonly arguments: string;
|
|
71
|
-
} | {
|
|
72
|
-
readonly type: "function_call_output";
|
|
73
|
-
readonly call_id: string;
|
|
74
|
-
readonly output: string | readonly ({
|
|
75
|
-
readonly type: "input_image";
|
|
76
|
-
readonly image_url: string;
|
|
77
|
-
readonly detail?: string | undefined;
|
|
78
|
-
} | {
|
|
79
|
-
readonly type: "input_file";
|
|
80
|
-
readonly filename: string;
|
|
81
|
-
readonly detail?: string | undefined;
|
|
82
|
-
readonly file_data?: string | undefined;
|
|
83
|
-
readonly file_url?: string | undefined;
|
|
84
|
-
} | {
|
|
85
|
-
readonly type: "input_text";
|
|
86
|
-
readonly text: string;
|
|
87
|
-
} | {
|
|
88
|
-
readonly type: "input_video";
|
|
89
|
-
readonly video_url: string;
|
|
90
|
-
})[];
|
|
91
93
|
} | {
|
|
92
94
|
readonly [x: string]: unknown;
|
|
93
95
|
readonly type: "web_search_call";
|
|
@@ -164,15 +166,15 @@ export declare const route: Route<{
|
|
|
164
166
|
} | undefined;
|
|
165
167
|
readonly top_logprobs?: number | undefined;
|
|
166
168
|
readonly truncation?: "auto" | "disabled" | undefined;
|
|
167
|
-
readonly service_tier?: import("../
|
|
169
|
+
readonly service_tier?: import("../schema/options.js").OpenString<"auto" | "default" | "flex" | "priority"> | undefined;
|
|
168
170
|
readonly prompt_cache_key?: string | undefined;
|
|
169
|
-
readonly include?: readonly import("../
|
|
171
|
+
readonly include?: readonly import("../schema/options.js").OpenString<"code_interpreter_call.outputs" | "computer_call_output.output.image_url" | "file_search_call.results" | "message.input_image.image_url" | "message.output_text.logprobs" | "reasoning.encrypted_content" | "web_search_call.action.sources" | "web_search_call.results">[] | undefined;
|
|
170
172
|
readonly reasoning?: {
|
|
171
|
-
readonly effort?: import("../
|
|
173
|
+
readonly effort?: import("../effect.js").ReasoningEffort | undefined;
|
|
172
174
|
readonly summary?: "auto" | "concise" | "detailed" | undefined;
|
|
173
175
|
} | undefined;
|
|
174
176
|
readonly text?: {
|
|
175
|
-
readonly verbosity?: import("../
|
|
177
|
+
readonly verbosity?: import("../schema/options.js").OpenString<"high" | "low" | "medium"> | undefined;
|
|
176
178
|
} | undefined;
|
|
177
179
|
readonly max_output_tokens?: number | undefined;
|
|
178
180
|
readonly max_tool_calls?: number | undefined;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { DecisionRoute } from "../route/decision.js";
|
|
3
|
+
import { type ProviderOptions } from "../schema/index.js";
|
|
4
|
+
export declare const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
5
|
+
export declare const PATH = "/decisions";
|
|
6
|
+
declare const Options: Schema.Struct<{
|
|
7
|
+
readonly promptCacheKey: Schema.optional<Schema.String>;
|
|
8
|
+
readonly promptCacheOptions: Schema.optional<Schema.Struct<{
|
|
9
|
+
readonly mode: Schema.optional<Schema.String>;
|
|
10
|
+
readonly ttl: Schema.optional<Schema.String>;
|
|
11
|
+
}>>;
|
|
12
|
+
readonly safetyIdentifier: Schema.optional<Schema.String>;
|
|
13
|
+
}>;
|
|
14
|
+
/** Known fields map to their wire names; anything else is sent as given. */
|
|
15
|
+
export type OptionsInput = ProviderOptions & typeof Options.Encoded;
|
|
16
|
+
export declare const protocol: DecisionRoute.Protocol;
|
|
17
|
+
export declare const route: DecisionRoute.Route;
|
|
18
|
+
export declare const OpenAIDecisions: {
|
|
19
|
+
readonly protocol: DecisionRoute.Protocol;
|
|
20
|
+
readonly route: DecisionRoute.Route;
|
|
21
|
+
};
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { DecisionResponse, DecisionUsage } from "../decision.js";
|
|
3
|
+
import { DecisionRoute } from "../route/decision.js";
|
|
4
|
+
import { Endpoint } from "../route/endpoint.js";
|
|
5
|
+
import { MediaProtocol } from "../route/media-protocol.js";
|
|
6
|
+
import { AIError, UnsupportedOperationError, mergeJsonRecords } from "../schema/index.js";
|
|
7
|
+
import { encodeJson } from "../utils/json.js";
|
|
8
|
+
import { OpenResponses } from "./open-responses.js";
|
|
9
|
+
import { ProviderShared } from "./shared.js";
|
|
10
|
+
const ID = "openai-decisions";
|
|
11
|
+
const NAME = "OpenAI Decisions";
|
|
12
|
+
export const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
13
|
+
export const PATH = "/decisions";
|
|
14
|
+
const Options = Schema.Struct({
|
|
15
|
+
promptCacheKey: Schema.optional(Schema.String),
|
|
16
|
+
promptCacheOptions: Schema.optional(Schema.Struct({ mode: Schema.optional(Schema.String), ttl: Schema.optional(Schema.String) })),
|
|
17
|
+
safetyIdentifier: Schema.optional(Schema.String),
|
|
18
|
+
});
|
|
19
|
+
const Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
|
|
20
|
+
const ChoiceValue = Schema.Union([Schema.String, Schema.Boolean]);
|
|
21
|
+
const Answer = Schema.Union([
|
|
22
|
+
Schema.Struct({ type: Schema.Literal("predicate"), name: Schema.String, probability: Probability }),
|
|
23
|
+
Schema.Struct({
|
|
24
|
+
type: Schema.Literal("choice"),
|
|
25
|
+
name: Schema.String,
|
|
26
|
+
choice: ChoiceValue,
|
|
27
|
+
probabilities: Schema.Array(Schema.Struct({ value: ChoiceValue, probability: Probability })),
|
|
28
|
+
confidence: Probability,
|
|
29
|
+
}),
|
|
30
|
+
Schema.Struct({
|
|
31
|
+
type: Schema.Literal("score"),
|
|
32
|
+
name: Schema.String,
|
|
33
|
+
score: Schema.Number,
|
|
34
|
+
probabilities: Schema.Array(Schema.Struct({ value: Schema.Int, label: Schema.String, probability: Probability })),
|
|
35
|
+
confidence: Probability,
|
|
36
|
+
}),
|
|
37
|
+
Schema.Struct({ type: Schema.Literal("refusal"), name: Schema.String, message: Schema.optional(Schema.String) }),
|
|
38
|
+
]);
|
|
39
|
+
const Response = Schema.Struct({
|
|
40
|
+
model: Schema.String,
|
|
41
|
+
answers: Schema.Array(Answer),
|
|
42
|
+
usage: Schema.optional(OpenResponses.OpenResponsesUsage),
|
|
43
|
+
});
|
|
44
|
+
const decodeOptions = ProviderShared.validateWith(Schema.decodeUnknownEffect(Options));
|
|
45
|
+
const fromRequest = Effect.fn("OpenAIDecisions.fromRequest")(function* (request) {
|
|
46
|
+
const options = yield* decodeOptions(request.providerOptions ?? {});
|
|
47
|
+
const { promptCacheKey: _, promptCacheOptions: __, safetyIdentifier: ___, ...native } = request.providerOptions ?? {};
|
|
48
|
+
return {
|
|
49
|
+
...mergeJsonRecords({
|
|
50
|
+
prompt_cache_key: options.promptCacheKey,
|
|
51
|
+
prompt_cache_options: options.promptCacheOptions,
|
|
52
|
+
safety_identifier: options.safetyIdentifier,
|
|
53
|
+
...native,
|
|
54
|
+
}, request.http?.body),
|
|
55
|
+
model: request.model.id,
|
|
56
|
+
input: [{ role: "user", content: yield* Effect.forEach(request.input, (part) => inputPart(request, part)) }],
|
|
57
|
+
questions: request.questions,
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
// `input_json` takes an object or a list of objects; other JSON values go as text.
|
|
61
|
+
const inputPart = (request, part) => {
|
|
62
|
+
if (part.type === "text")
|
|
63
|
+
return Effect.succeed({ type: "input_text", text: part.text });
|
|
64
|
+
if (part.type === "json")
|
|
65
|
+
return Effect.succeed(isObject(part.value) || (Array.isArray(part.value) && part.value.every(isObject))
|
|
66
|
+
? { type: "input_json", data: part.value }
|
|
67
|
+
: { type: "input_text", text: encodeJson(part.value) });
|
|
68
|
+
if (part.media.kind !== "image")
|
|
69
|
+
return Effect.fail(new AIError({
|
|
70
|
+
reason: new UnsupportedOperationError({
|
|
71
|
+
operation: "decision.media",
|
|
72
|
+
provider: request.model.provider,
|
|
73
|
+
route: request.model.route.id,
|
|
74
|
+
message: `${NAME} takes images only; got ${part.media.kind} media`,
|
|
75
|
+
}),
|
|
76
|
+
}));
|
|
77
|
+
return ProviderShared.mediaReference(part.media, undefined, `${NAME} image input`).pipe(Effect.map((reference) => ({ type: "input_image", image_url: reference.value })));
|
|
78
|
+
};
|
|
79
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
80
|
+
const decodeDocument = MediaProtocol.identity({ id: ID, name: NAME, provider: "openai" }).decodeJson(Response);
|
|
81
|
+
export const protocol = DecisionRoute.protocol({
|
|
82
|
+
id: ID,
|
|
83
|
+
name: NAME,
|
|
84
|
+
body: { from: fromRequest },
|
|
85
|
+
response: {
|
|
86
|
+
decode: Effect.fn("OpenAIDecisions.decodeResponse")(function* (response, context) {
|
|
87
|
+
const output = yield* decodeDocument(response);
|
|
88
|
+
const answers = output.value.answers.map((answer) => answer.type === "refusal"
|
|
89
|
+
? {
|
|
90
|
+
name: answer.name,
|
|
91
|
+
type: "error",
|
|
92
|
+
reason: "refusal",
|
|
93
|
+
...(answer.message === undefined ? {} : { message: answer.message }),
|
|
94
|
+
}
|
|
95
|
+
: answer);
|
|
96
|
+
const byName = new Map(answers.map((answer) => [answer.name, answer]));
|
|
97
|
+
const ordered = context.request.questions.flatMap((question) => byName.get(question.name) ?? []);
|
|
98
|
+
const usage = OpenResponses.mapUsage(output.value.usage, context.providerMetadataKey);
|
|
99
|
+
return new DecisionResponse({
|
|
100
|
+
answers: ordered.length === answers.length ? ordered : answers,
|
|
101
|
+
usage: usage === undefined
|
|
102
|
+
? undefined
|
|
103
|
+
: new DecisionUsage({
|
|
104
|
+
inputTokens: usage.inputTokens,
|
|
105
|
+
outputTokens: usage.outputTokens,
|
|
106
|
+
totalTokens: usage.totalTokens,
|
|
107
|
+
cost: usage.cost,
|
|
108
|
+
}),
|
|
109
|
+
providerMetadata: {
|
|
110
|
+
[context.providerMetadataKey]: {
|
|
111
|
+
model: output.value.model,
|
|
112
|
+
...(output.value.usage === undefined ? {} : { usage: output.value.usage }),
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}),
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
export const route = DecisionRoute.make({
|
|
120
|
+
id: ID,
|
|
121
|
+
provider: "openai",
|
|
122
|
+
protocol,
|
|
123
|
+
endpoint: Endpoint.path(PATH, { baseURL: DEFAULT_BASE_URL }),
|
|
124
|
+
});
|
|
125
|
+
export const OpenAIDecisions = {
|
|
126
|
+
protocol,
|
|
127
|
+
route,
|
|
128
|
+
};
|