@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
|
@@ -7,14 +7,16 @@ import { Endpoint } from "../route/endpoint.js";
|
|
|
7
7
|
import { Protocol } from "../route/protocol.js";
|
|
8
8
|
import { HttpTransport } from "../route/transport/index.js";
|
|
9
9
|
import { LLMRequest } from "../schema/index.js";
|
|
10
|
+
import { requestTTL } from "../cache-policy.js";
|
|
10
11
|
import { resolveEffortUpdates } from "../effort-updates.js";
|
|
11
12
|
import { OpenResponses } from "./open-responses.js";
|
|
12
13
|
import { OpenResponsesOptions } from "./utils/open-responses-options.js";
|
|
13
|
-
import { JsonObject, optionalArray, optionalNull, ProviderShared } from "./shared.js";
|
|
14
|
+
import { JsonObject, lenient, optionalArray, optionalNull, ProviderShared } from "./shared.js";
|
|
14
15
|
import { ResponsesHostedTools } from "./utils/responses-hosted-tools.js";
|
|
15
16
|
import { OpenResponsesChannel } from "./open-responses-channel.js";
|
|
16
17
|
import { ResponsesCompaction } from "./utils/responses-compaction.js";
|
|
17
18
|
import { ResponsesCheckpoint } from "./utils/responses-checkpoint.js";
|
|
19
|
+
import * as Cache from "./utils/cache.js";
|
|
18
20
|
const ADAPTER = "openai-responses";
|
|
19
21
|
const NAME = "OpenAI Responses";
|
|
20
22
|
const WEBSOCKET_PROTOCOL_HEADER = "responses_websockets=2026-02-06";
|
|
@@ -25,6 +27,12 @@ export const ContextManagement = Schema.Array(Schema.Struct({
|
|
|
25
27
|
type: Schema.Literal("compaction"),
|
|
26
28
|
compactThreshold: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
|
|
27
29
|
}));
|
|
30
|
+
export const ReasoningMode = ProviderShared.knownString();
|
|
31
|
+
export const ReasoningContext = ProviderShared.knownString();
|
|
32
|
+
const ReasoningOptions = Schema.Struct({
|
|
33
|
+
reasoningMode: lenient(ReasoningMode),
|
|
34
|
+
reasoningContext: lenient(ReasoningContext),
|
|
35
|
+
});
|
|
28
36
|
const OpenAIResponsesImageGenerationTool = Schema.Struct({
|
|
29
37
|
type: Schema.tag("image_generation"),
|
|
30
38
|
action: Schema.optional(Schema.Literals(["auto", "generate", "edit"])),
|
|
@@ -84,8 +92,16 @@ const OpenAIResponsesToolChoice = Schema.Union([
|
|
|
84
92
|
]);
|
|
85
93
|
const OpenAIResponsesCoreFields = {
|
|
86
94
|
...OpenResponses.coreFields,
|
|
95
|
+
prompt_cache_retention: Schema.optional(Schema.String),
|
|
96
|
+
prompt_cache_options: Schema.optional(Schema.Struct({ ttl: Schema.optional(Schema.String) })),
|
|
87
97
|
tools: optionalArray(OpenAIResponsesTools),
|
|
88
98
|
tool_choice: Schema.optional(OpenAIResponsesToolChoice),
|
|
99
|
+
reasoning: Schema.optional(Schema.Struct({
|
|
100
|
+
effort: Schema.optional(OpenResponsesOptions.ReasoningEffort),
|
|
101
|
+
summary: Schema.optional(Schema.Literals(["auto", "concise", "detailed"])),
|
|
102
|
+
mode: Schema.optional(ReasoningMode),
|
|
103
|
+
context: Schema.optional(ReasoningContext),
|
|
104
|
+
})),
|
|
89
105
|
context_management: Schema.optional(Schema.Array(Schema.Struct({
|
|
90
106
|
type: Schema.Literal("compaction"),
|
|
91
107
|
compact_threshold: Schema.optional(Schema.Int.check(Schema.isGreaterThan(0))),
|
|
@@ -111,6 +127,8 @@ const adapter = {
|
|
|
111
127
|
const supportsEffortUpdates = (request) => {
|
|
112
128
|
if (request.providerOptions?.contextManagement !== undefined)
|
|
113
129
|
return false;
|
|
130
|
+
if (request.providerOptions?.reasoningMode === "pro")
|
|
131
|
+
return false;
|
|
114
132
|
if (Schema.is(Schema.Struct({ mode: Schema.Literal("pro") }))(request.http?.body?.reasoning))
|
|
115
133
|
return false;
|
|
116
134
|
const override = request.model.compatibility?.supportsEffortUpdates;
|
|
@@ -162,15 +180,27 @@ const lowerToolChoice = (toolChoice, tools) => ProviderShared.matchToolChoice(NA
|
|
|
162
180
|
const fromRequest = Effect.fn("OpenAIResponses.fromRequest")(function* (request) {
|
|
163
181
|
const management = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(Schema.UndefinedOr(ContextManagement)))(request.providerOptions?.contextManagement);
|
|
164
182
|
const options = OpenResponsesOptions.resolve(request);
|
|
183
|
+
const reasoning = Schema.decodeUnknownSync(ReasoningOptions)(request.providerOptions ?? {});
|
|
165
184
|
const updates = resolveEffortUpdates(request, options.reasoningEffort);
|
|
185
|
+
const generation = OpenResponses.lowerGeneration(request, { ...options, reasoningEffort: updates.effort });
|
|
166
186
|
return {
|
|
167
187
|
...(yield* OpenResponses.lowerConversation(updates.request, adapter)),
|
|
168
|
-
...
|
|
188
|
+
...generation,
|
|
189
|
+
...Cache.openAIRequestTTL(requestTTL(request)),
|
|
190
|
+
...(generation.reasoning || reasoning.reasoningMode || reasoning.reasoningContext
|
|
191
|
+
? {
|
|
192
|
+
reasoning: {
|
|
193
|
+
...generation.reasoning,
|
|
194
|
+
mode: reasoning.reasoningMode,
|
|
195
|
+
context: reasoning.reasoningContext,
|
|
196
|
+
},
|
|
197
|
+
}
|
|
198
|
+
: {}),
|
|
169
199
|
context_management: management?.map((edit) => ({ type: edit.type, compact_threshold: edit.compactThreshold })),
|
|
170
200
|
tools: request.tools.length === 0 ? undefined : yield* lowerTools(request),
|
|
171
201
|
tool_choice: request.tools.length === 0
|
|
172
202
|
? undefined
|
|
173
|
-
: (OpenResponses.allowedToolChoice(request) ??
|
|
203
|
+
: (OpenResponses.allowedToolChoice(request, options) ??
|
|
174
204
|
(request.toolChoice ? yield* lowerToolChoice(request.toolChoice, request.tools) : undefined)),
|
|
175
205
|
};
|
|
176
206
|
});
|
|
@@ -16,14 +16,6 @@ interface State extends SpeechStream.Audio {
|
|
|
16
16
|
readonly usage?: MediaUsage;
|
|
17
17
|
}
|
|
18
18
|
export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
19
|
-
readonly type: "generation-queued";
|
|
20
|
-
readonly id: string;
|
|
21
|
-
readonly position?: number | undefined;
|
|
22
|
-
} | {
|
|
23
|
-
readonly type: "generation-progress";
|
|
24
|
-
readonly id: string;
|
|
25
|
-
readonly progress?: number | undefined;
|
|
26
|
-
} | {
|
|
27
19
|
readonly type: "audio-delta";
|
|
28
20
|
readonly chunk: Uint8Array<ArrayBufferLike>;
|
|
29
21
|
} | {
|
|
@@ -33,18 +25,18 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
33
25
|
readonly startSeconds: number;
|
|
34
26
|
readonly endSeconds: number;
|
|
35
27
|
}[];
|
|
28
|
+
} | {
|
|
29
|
+
readonly type: "generation-queued";
|
|
30
|
+
readonly id: string;
|
|
31
|
+
readonly position?: number | undefined;
|
|
32
|
+
} | {
|
|
33
|
+
readonly type: "generation-progress";
|
|
34
|
+
readonly id: string;
|
|
35
|
+
readonly progress?: number | undefined;
|
|
36
36
|
} | {
|
|
37
37
|
readonly type: "finish";
|
|
38
38
|
readonly audio: import("../media.js").Asset;
|
|
39
39
|
readonly usage?: {
|
|
40
|
-
readonly type: "tokens";
|
|
41
|
-
readonly input?: number | undefined;
|
|
42
|
-
readonly output?: number | undefined;
|
|
43
|
-
readonly total?: number | undefined;
|
|
44
|
-
readonly details?: {
|
|
45
|
-
readonly [x: string]: unknown;
|
|
46
|
-
} | undefined;
|
|
47
|
-
} | {
|
|
48
40
|
readonly type: "seconds";
|
|
49
41
|
readonly seconds: number;
|
|
50
42
|
} | {
|
|
@@ -56,16 +48,15 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
56
48
|
} | {
|
|
57
49
|
readonly type: "compute";
|
|
58
50
|
readonly seconds: number;
|
|
59
|
-
} |
|
|
60
|
-
|
|
61
|
-
readonly
|
|
62
|
-
readonly
|
|
63
|
-
readonly
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
};
|
|
51
|
+
} | {
|
|
52
|
+
readonly type: "tokens";
|
|
53
|
+
readonly input?: number | undefined;
|
|
54
|
+
readonly output?: number | undefined;
|
|
55
|
+
readonly total?: number | undefined;
|
|
56
|
+
readonly details?: {
|
|
57
|
+
readonly [x: string]: unknown;
|
|
67
58
|
} | undefined;
|
|
68
|
-
}
|
|
59
|
+
} | undefined;
|
|
69
60
|
readonly providerMetadata?: {
|
|
70
61
|
readonly [x: string]: {
|
|
71
62
|
readonly [x: string]: unknown;
|
|
@@ -75,14 +66,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
75
66
|
export declare const model: (input: MediaRoute.ModelInput) => SpeechModel<OpenAISpeechOptions>;
|
|
76
67
|
export declare const OpenAISpeech: {
|
|
77
68
|
readonly protocol: MediaProtocol.Streamed<Request, {
|
|
78
|
-
readonly type: "generation-queued";
|
|
79
|
-
readonly id: string;
|
|
80
|
-
readonly position?: number | undefined;
|
|
81
|
-
} | {
|
|
82
|
-
readonly type: "generation-progress";
|
|
83
|
-
readonly id: string;
|
|
84
|
-
readonly progress?: number | undefined;
|
|
85
|
-
} | {
|
|
86
69
|
readonly type: "audio-delta";
|
|
87
70
|
readonly chunk: Uint8Array<ArrayBufferLike>;
|
|
88
71
|
} | {
|
|
@@ -92,18 +75,18 @@ export declare const OpenAISpeech: {
|
|
|
92
75
|
readonly startSeconds: number;
|
|
93
76
|
readonly endSeconds: number;
|
|
94
77
|
}[];
|
|
78
|
+
} | {
|
|
79
|
+
readonly type: "generation-queued";
|
|
80
|
+
readonly id: string;
|
|
81
|
+
readonly position?: number | undefined;
|
|
82
|
+
} | {
|
|
83
|
+
readonly type: "generation-progress";
|
|
84
|
+
readonly id: string;
|
|
85
|
+
readonly progress?: number | undefined;
|
|
95
86
|
} | {
|
|
96
87
|
readonly type: "finish";
|
|
97
88
|
readonly audio: import("../media.js").Asset;
|
|
98
89
|
readonly usage?: {
|
|
99
|
-
readonly type: "tokens";
|
|
100
|
-
readonly input?: number | undefined;
|
|
101
|
-
readonly output?: number | undefined;
|
|
102
|
-
readonly total?: number | undefined;
|
|
103
|
-
readonly details?: {
|
|
104
|
-
readonly [x: string]: unknown;
|
|
105
|
-
} | undefined;
|
|
106
|
-
} | {
|
|
107
90
|
readonly type: "seconds";
|
|
108
91
|
readonly seconds: number;
|
|
109
92
|
} | {
|
|
@@ -115,16 +98,15 @@ export declare const OpenAISpeech: {
|
|
|
115
98
|
} | {
|
|
116
99
|
readonly type: "compute";
|
|
117
100
|
readonly seconds: number;
|
|
118
|
-
} |
|
|
119
|
-
|
|
120
|
-
readonly
|
|
121
|
-
readonly
|
|
122
|
-
readonly
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
};
|
|
101
|
+
} | {
|
|
102
|
+
readonly type: "tokens";
|
|
103
|
+
readonly input?: number | undefined;
|
|
104
|
+
readonly output?: number | undefined;
|
|
105
|
+
readonly total?: number | undefined;
|
|
106
|
+
readonly details?: {
|
|
107
|
+
readonly [x: string]: unknown;
|
|
126
108
|
} | undefined;
|
|
127
|
-
}
|
|
109
|
+
} | undefined;
|
|
128
110
|
readonly providerMetadata?: {
|
|
129
111
|
readonly [x: string]: {
|
|
130
112
|
readonly [x: string]: unknown;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
1
|
+
import { Effect, Schema, SchemaGetter } from "effect";
|
|
2
2
|
import { Framing } from "../route/framing.js";
|
|
3
3
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
4
|
import { MediaRoute } from "../route/media.js";
|
|
5
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
5
|
+
import { mergeJsonRecords, tokenUsage } from "../schema/index.js";
|
|
6
6
|
import { SpeechModel } from "../speech.js";
|
|
7
7
|
import { SpeechStream } from "./utils/speech-stream.js";
|
|
8
8
|
const route = MediaProtocol.identity({ id: "openai-speech", name: "OpenAI Speech", provider: "openai" });
|
|
@@ -13,6 +13,14 @@ const PCM_SAMPLE_RATE = 24000;
|
|
|
13
13
|
// ---------------------------------------------------------------------------
|
|
14
14
|
// 3. Streaming event schema
|
|
15
15
|
// ---------------------------------------------------------------------------
|
|
16
|
+
const EVENT_TYPES = new Set(["speech.audio.delta", "speech.audio.done"]);
|
|
17
|
+
/** OpenAI may add stream event types; frames outside `EVENT_TYPES` are ignored, while a malformed known type still fails decoding. */
|
|
18
|
+
const UnknownEvent = Schema.Struct({
|
|
19
|
+
type: Schema.String.check(Schema.makeFilter((type) => !EVENT_TYPES.has(type))),
|
|
20
|
+
}).pipe(Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown"), rawType: Schema.String }), {
|
|
21
|
+
decode: SchemaGetter.transform((event) => ({ type: "unknown", rawType: event.type })),
|
|
22
|
+
encode: SchemaGetter.forbidden(() => "Unknown OpenAI Speech stream events are decode-only"),
|
|
23
|
+
}));
|
|
16
24
|
const SpeechStreamEvent = Schema.Union([
|
|
17
25
|
Schema.Struct({ type: Schema.Literal("speech.audio.delta"), audio: Schema.Uint8ArrayFromBase64 }),
|
|
18
26
|
Schema.Struct({
|
|
@@ -23,6 +31,7 @@ const SpeechStreamEvent = Schema.Union([
|
|
|
23
31
|
total_tokens: Schema.optional(Schema.Number),
|
|
24
32
|
})),
|
|
25
33
|
}),
|
|
34
|
+
UnknownEvent,
|
|
26
35
|
]);
|
|
27
36
|
const decodeEvent = route.decodeFrame(SpeechStreamEvent);
|
|
28
37
|
// ---------------------------------------------------------------------------
|
|
@@ -53,6 +62,8 @@ const fromRequest = Effect.fn("OpenAISpeech.fromRequest")(function* (request) {
|
|
|
53
62
|
const isSse = (body) => body.type === "json" && body.value.stream_format === "sse";
|
|
54
63
|
const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
55
64
|
const event = yield* decodeEvent(frame);
|
|
65
|
+
if (event.type === "unknown")
|
|
66
|
+
return [state, []];
|
|
56
67
|
if (event.type === "speech.audio.delta")
|
|
57
68
|
return SpeechStream.delta(state, event.audio);
|
|
58
69
|
const usage = event.usage;
|
|
@@ -62,13 +73,12 @@ const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
|
62
73
|
done: true,
|
|
63
74
|
usage: usage === undefined
|
|
64
75
|
? undefined
|
|
65
|
-
: {
|
|
66
|
-
type: "tokens",
|
|
76
|
+
: tokenUsage({
|
|
67
77
|
input: usage.input_tokens,
|
|
68
78
|
output: usage.output_tokens,
|
|
69
79
|
total: usage.total_tokens,
|
|
70
80
|
details: { openai: usage },
|
|
71
|
-
},
|
|
81
|
+
}),
|
|
72
82
|
},
|
|
73
83
|
[],
|
|
74
84
|
];
|
|
@@ -59,14 +59,6 @@ interface State {
|
|
|
59
59
|
readonly transcript?: Transcript;
|
|
60
60
|
}
|
|
61
61
|
export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
62
|
-
readonly type: "generation-queued";
|
|
63
|
-
readonly id: string;
|
|
64
|
-
readonly position?: number | undefined;
|
|
65
|
-
} | {
|
|
66
|
-
readonly type: "generation-progress";
|
|
67
|
-
readonly id: string;
|
|
68
|
-
readonly progress?: number | undefined;
|
|
69
|
-
} | {
|
|
70
62
|
readonly type: "text-delta";
|
|
71
63
|
readonly delta: string;
|
|
72
64
|
} | {
|
|
@@ -77,6 +69,14 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
77
69
|
readonly endSeconds: number;
|
|
78
70
|
readonly speaker?: string | undefined;
|
|
79
71
|
};
|
|
72
|
+
} | {
|
|
73
|
+
readonly type: "generation-queued";
|
|
74
|
+
readonly id: string;
|
|
75
|
+
readonly position?: number | undefined;
|
|
76
|
+
} | {
|
|
77
|
+
readonly type: "generation-progress";
|
|
78
|
+
readonly id: string;
|
|
79
|
+
readonly progress?: number | undefined;
|
|
80
80
|
} | {
|
|
81
81
|
readonly text: string;
|
|
82
82
|
readonly segments?: readonly {
|
|
@@ -95,14 +95,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
95
95
|
readonly language?: string | undefined;
|
|
96
96
|
readonly durationSeconds?: number | undefined;
|
|
97
97
|
readonly usage?: {
|
|
98
|
-
readonly type: "tokens";
|
|
99
|
-
readonly input?: number | undefined;
|
|
100
|
-
readonly output?: number | undefined;
|
|
101
|
-
readonly total?: number | undefined;
|
|
102
|
-
readonly details?: {
|
|
103
|
-
readonly [x: string]: unknown;
|
|
104
|
-
} | undefined;
|
|
105
|
-
} | {
|
|
106
98
|
readonly type: "seconds";
|
|
107
99
|
readonly seconds: number;
|
|
108
100
|
} | {
|
|
@@ -114,16 +106,15 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
114
106
|
} | {
|
|
115
107
|
readonly type: "compute";
|
|
116
108
|
readonly seconds: number;
|
|
117
|
-
} |
|
|
118
|
-
|
|
119
|
-
readonly
|
|
120
|
-
readonly
|
|
121
|
-
readonly
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
};
|
|
109
|
+
} | {
|
|
110
|
+
readonly type: "tokens";
|
|
111
|
+
readonly input?: number | undefined;
|
|
112
|
+
readonly output?: number | undefined;
|
|
113
|
+
readonly total?: number | undefined;
|
|
114
|
+
readonly details?: {
|
|
115
|
+
readonly [x: string]: unknown;
|
|
125
116
|
} | undefined;
|
|
126
|
-
}
|
|
117
|
+
} | undefined;
|
|
127
118
|
readonly providerMetadata?: {
|
|
128
119
|
readonly [x: string]: {
|
|
129
120
|
readonly [x: string]: unknown;
|
|
@@ -134,14 +125,6 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
134
125
|
export declare const model: (input: MediaRoute.ModelInput) => TranscriptionModel<OpenAITranscriptionOptions>;
|
|
135
126
|
export declare const OpenAITranscription: {
|
|
136
127
|
readonly protocol: MediaProtocol.Streamed<Request, {
|
|
137
|
-
readonly type: "generation-queued";
|
|
138
|
-
readonly id: string;
|
|
139
|
-
readonly position?: number | undefined;
|
|
140
|
-
} | {
|
|
141
|
-
readonly type: "generation-progress";
|
|
142
|
-
readonly id: string;
|
|
143
|
-
readonly progress?: number | undefined;
|
|
144
|
-
} | {
|
|
145
128
|
readonly type: "text-delta";
|
|
146
129
|
readonly delta: string;
|
|
147
130
|
} | {
|
|
@@ -152,6 +135,14 @@ export declare const OpenAITranscription: {
|
|
|
152
135
|
readonly endSeconds: number;
|
|
153
136
|
readonly speaker?: string | undefined;
|
|
154
137
|
};
|
|
138
|
+
} | {
|
|
139
|
+
readonly type: "generation-queued";
|
|
140
|
+
readonly id: string;
|
|
141
|
+
readonly position?: number | undefined;
|
|
142
|
+
} | {
|
|
143
|
+
readonly type: "generation-progress";
|
|
144
|
+
readonly id: string;
|
|
145
|
+
readonly progress?: number | undefined;
|
|
155
146
|
} | {
|
|
156
147
|
readonly text: string;
|
|
157
148
|
readonly segments?: readonly {
|
|
@@ -170,14 +161,6 @@ export declare const OpenAITranscription: {
|
|
|
170
161
|
readonly language?: string | undefined;
|
|
171
162
|
readonly durationSeconds?: number | undefined;
|
|
172
163
|
readonly usage?: {
|
|
173
|
-
readonly type: "tokens";
|
|
174
|
-
readonly input?: number | undefined;
|
|
175
|
-
readonly output?: number | undefined;
|
|
176
|
-
readonly total?: number | undefined;
|
|
177
|
-
readonly details?: {
|
|
178
|
-
readonly [x: string]: unknown;
|
|
179
|
-
} | undefined;
|
|
180
|
-
} | {
|
|
181
164
|
readonly type: "seconds";
|
|
182
165
|
readonly seconds: number;
|
|
183
166
|
} | {
|
|
@@ -189,16 +172,15 @@ export declare const OpenAITranscription: {
|
|
|
189
172
|
} | {
|
|
190
173
|
readonly type: "compute";
|
|
191
174
|
readonly seconds: number;
|
|
192
|
-
} |
|
|
193
|
-
|
|
194
|
-
readonly
|
|
195
|
-
readonly
|
|
196
|
-
readonly
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
};
|
|
175
|
+
} | {
|
|
176
|
+
readonly type: "tokens";
|
|
177
|
+
readonly input?: number | undefined;
|
|
178
|
+
readonly output?: number | undefined;
|
|
179
|
+
readonly total?: number | undefined;
|
|
180
|
+
readonly details?: {
|
|
181
|
+
readonly [x: string]: unknown;
|
|
200
182
|
} | undefined;
|
|
201
|
-
}
|
|
183
|
+
} | undefined;
|
|
202
184
|
readonly providerMetadata?: {
|
|
203
185
|
readonly [x: string]: {
|
|
204
186
|
readonly [x: string]: unknown;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Effect, Schema, Stream } from "effect";
|
|
1
|
+
import { Effect, Schema, SchemaGetter, Stream } from "effect";
|
|
2
2
|
import { classifyProviderFailure } from "../provider-error.js";
|
|
3
3
|
import { Framing } from "../route/framing.js";
|
|
4
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
5
5
|
import { MediaRoute } from "../route/media.js";
|
|
6
|
-
import { AIError, mergeJsonRecords } from "../schema/index.js";
|
|
6
|
+
import { AIError, mergeJsonRecords, tokenUsage } from "../schema/index.js";
|
|
7
7
|
import { TranscriptionFinishEvent, TranscriptionModel, TranscriptionSegmentEvent, TranscriptionTextDeltaEvent, } from "../transcription.js";
|
|
8
8
|
import { mediaTypeExtension } from "../utils/media-type.js";
|
|
9
9
|
import { ProviderShared } from "./shared.js";
|
|
@@ -39,8 +39,14 @@ const transcriptFields = {
|
|
|
39
39
|
words: Schema.optional(Schema.Array(Schema.Struct({ word: Schema.String, start: Schema.Number, end: Schema.Number }))),
|
|
40
40
|
usage: Schema.optional(Usage),
|
|
41
41
|
};
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
const EVENT_TYPES = new Set(["transcript.text.delta", "transcript.text.segment", "transcript.text.done", "error"]);
|
|
43
|
+
/** OpenAI may add stream event types; frames outside `EVENT_TYPES` are ignored, while a malformed known type still fails decoding. */
|
|
44
|
+
const UnknownEvent = Schema.Struct({
|
|
45
|
+
type: Schema.String.check(Schema.makeFilter((type) => !EVENT_TYPES.has(type))),
|
|
46
|
+
}).pipe(Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown"), rawType: Schema.String }), {
|
|
47
|
+
decode: SchemaGetter.transform((event) => ({ type: "unknown", rawType: event.type })),
|
|
48
|
+
encode: SchemaGetter.forbidden(() => "Unknown OpenAI Transcription stream events are decode-only"),
|
|
49
|
+
}));
|
|
44
50
|
const Event = Schema.Union([
|
|
45
51
|
Schema.Struct({ type: Schema.Literal("transcript.text.delta"), delta: Schema.String }),
|
|
46
52
|
Schema.Struct({ type: Schema.Literal("transcript.text.segment"), ...Segment.fields }),
|
|
@@ -50,10 +56,9 @@ const Event = Schema.Union([
|
|
|
50
56
|
message: Schema.optional(Schema.String),
|
|
51
57
|
error: Schema.optional(Schema.Struct({ message: Schema.optional(Schema.String) })),
|
|
52
58
|
}),
|
|
59
|
+
UnknownEvent,
|
|
53
60
|
]);
|
|
54
|
-
const EVENT_TYPES = new Set(["transcript.text.delta", "transcript.text.segment", "transcript.text.done", "error"]);
|
|
55
61
|
const Transcript = Schema.Struct(transcriptFields);
|
|
56
|
-
const decodeEventType = route.decodeFrame(EventType);
|
|
57
62
|
const decodeEvent = route.decodeFrame(Event);
|
|
58
63
|
const decodeTranscript = route.decodeFrame(Transcript);
|
|
59
64
|
const TRANSCRIBE = { stream: true, timestamps: [], diarize: false, languageField: "language" };
|
|
@@ -132,9 +137,9 @@ const segment = (value) => ({
|
|
|
132
137
|
speaker: value.speaker,
|
|
133
138
|
});
|
|
134
139
|
const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
135
|
-
if (!EVENT_TYPES.has((yield* decodeEventType(frame)).type))
|
|
136
|
-
return [state, []];
|
|
137
140
|
const event = yield* decodeEvent(frame);
|
|
141
|
+
if (event.type === "unknown")
|
|
142
|
+
return [state, []];
|
|
138
143
|
if (event.type === "error")
|
|
139
144
|
return yield* new AIError({
|
|
140
145
|
reason: classifyProviderFailure({
|
|
@@ -158,13 +163,12 @@ const usage = (value) => {
|
|
|
158
163
|
return undefined;
|
|
159
164
|
if (value.type === "duration")
|
|
160
165
|
return { type: "seconds", seconds: value.seconds };
|
|
161
|
-
return {
|
|
162
|
-
type: "tokens",
|
|
166
|
+
return tokenUsage({
|
|
163
167
|
input: value.input_tokens,
|
|
164
168
|
output: value.output_tokens,
|
|
165
169
|
total: value.total_tokens,
|
|
166
170
|
details: { openai: value },
|
|
167
|
-
};
|
|
171
|
+
});
|
|
168
172
|
};
|
|
169
173
|
const finish = (state) => {
|
|
170
174
|
const transcript = state.transcript;
|
|
@@ -110,7 +110,7 @@ const decodeResult = Effect.fn("ReplicateImages.decodeResult")(function* (respon
|
|
|
110
110
|
// ---------------------------------------------------------------------------
|
|
111
111
|
export const protocol = MediaProtocol.queued(route, {
|
|
112
112
|
token: Token,
|
|
113
|
-
unsupported: ["images", "
|
|
113
|
+
unsupported: ["images", "n", "aspect", "resolution"],
|
|
114
114
|
start: { body: { from: fromRequest }, decode: decodeStart },
|
|
115
115
|
status: { path: (token) => token.getURL, decode: decodeStatus },
|
|
116
116
|
result: { path: (token) => token.getURL, decode: decodeResult },
|
|
@@ -11,7 +11,7 @@ export declare const IMAGE_TO_VIDEO_PATH = "/image_to_video";
|
|
|
11
11
|
export declare const VIDEO_TO_VIDEO_PATH = "/video_to_video";
|
|
12
12
|
export declare const TASKS_PATH = "/tasks";
|
|
13
13
|
/**
|
|
14
|
-
* Provider-native options. Common fields lower to Runway's names: `
|
|
14
|
+
* Provider-native options. Common fields lower to Runway's names: `aspect` → `ratio` (Runway expects pixel
|
|
15
15
|
* ratios such as `1280:720` for most models), `durationSeconds` → `duration`, `audio`, `negativePrompt`,
|
|
16
16
|
* `resolution`, `references`, and `frames` → `promptImage`.
|
|
17
17
|
*/
|
|
@@ -58,7 +58,7 @@ const fromRequest = Effect.fn("RunwayVideo.fromRequest")(function* (request) {
|
|
|
58
58
|
promptImage: promptImage.length === 0 ? undefined : promptImage,
|
|
59
59
|
videoUri,
|
|
60
60
|
references: references.length === 0 ? undefined : references,
|
|
61
|
-
ratio: request.
|
|
61
|
+
ratio: request.aspect,
|
|
62
62
|
duration: request.durationSeconds,
|
|
63
63
|
resolution: request.resolution,
|
|
64
64
|
audio: request.audio,
|
|
@@ -13,6 +13,14 @@ export declare const optionalNull: <const S extends Schema.Top>(schema: S) => Sc
|
|
|
13
13
|
export declare const optionalNullish: <const S extends Schema.Top>(schema: S) => Schema.decodeTo<Schema.optional<Schema.toType<S>>, Schema.optional<Schema.NullOr<S>>, never, never>;
|
|
14
14
|
/** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
|
|
15
15
|
export declare const lenient: <const S extends Schema.Top>(schema: S) => Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<S>, S["DecodingServices"]>>;
|
|
16
|
+
/** Billing an endpoint reports beside its token usage: `cost` is the USD it charged (OpenRouter, LiteLLM, OpenCode Zen). */
|
|
17
|
+
export declare const ReportedBilling: Schema.Struct<{
|
|
18
|
+
readonly cost: Schema.optionalKey<Schema.middlewareDecoding<Schema.UndefinedOr<Schema.Finite>, never>>;
|
|
19
|
+
}>;
|
|
20
|
+
/** Reads `ReportedBilling` from a raw usage object, which provider metadata keeps as the endpoint sent it. */
|
|
21
|
+
export declare const reportedBilling: (usage: unknown) => {} | {
|
|
22
|
+
cost: number | undefined;
|
|
23
|
+
};
|
|
16
24
|
/**
|
|
17
25
|
* Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
|
|
18
26
|
* keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
|
|
@@ -143,7 +151,7 @@ export type MediaReference = {
|
|
|
143
151
|
*/
|
|
144
152
|
export declare const mediaReference: (asset: Media.Asset, provider: ProviderID | undefined, label: string) => Effect.Effect<MediaReference, AIError>;
|
|
145
153
|
/**
|
|
146
|
-
* Lift a tool-result file into a `MediaPart`. Tool files carry either a data URL,
|
|
154
|
+
* Lift a tool-result file into a `MediaPart`. Tool files carry either a data URL, a URI, or raw base64 in
|
|
147
155
|
* `uri`; the declared `mime` wins over any data-URL prefix so tool authors control the type the model sees.
|
|
148
156
|
*/
|
|
149
157
|
export declare const toolFileMedia: (item: Tool.FileContent) => MediaPart;
|
package/dist/protocols/shared.js
CHANGED
|
@@ -17,6 +17,17 @@ export const optionalNullish = (schema) => Schema.optional(Schema.NullOr(schema)
|
|
|
17
17
|
})));
|
|
18
18
|
/** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
|
|
19
19
|
export const lenient = (schema) => Schema.optionalKey(Schema.UndefinedOr(schema).pipe(Schema.catchDecoding(() => Effect.succeed(Option.some(undefined)))));
|
|
20
|
+
/** A billed amount in USD; a malformed or negative one decodes as absent. */
|
|
21
|
+
const billedAmount = lenient(Schema.Finite.check(Schema.isGreaterThanOrEqualTo(0)));
|
|
22
|
+
/** Billing an endpoint reports beside its token usage: `cost` is the USD it charged (OpenRouter, LiteLLM, OpenCode Zen). */
|
|
23
|
+
export const ReportedBilling = Schema.Struct({
|
|
24
|
+
cost: billedAmount,
|
|
25
|
+
});
|
|
26
|
+
/** Reads `ReportedBilling` from a raw usage object, which provider metadata keeps as the endpoint sent it. */
|
|
27
|
+
export const reportedBilling = (usage) => Option.match(Schema.decodeUnknownOption(ReportedBilling)(usage), {
|
|
28
|
+
onNone: () => ({}),
|
|
29
|
+
onSome: (billing) => ({ cost: billing.cost }),
|
|
30
|
+
});
|
|
20
31
|
/**
|
|
21
32
|
* Accepts stream frames that carry no `type`, alongside the typed event schema. Gateways inject such frames as
|
|
22
33
|
* keep-alives, so they decode as an ignored event; one with an `error` still decodes as an error event.
|
|
@@ -175,14 +186,14 @@ export const mediaReference = (asset, provider, label) => {
|
|
|
175
186
|
return Effect.fail(invalidRequest(`${label} accepts inline bytes, data URLs, http(s) URLs${accepted}${got}`));
|
|
176
187
|
};
|
|
177
188
|
/**
|
|
178
|
-
* Lift a tool-result file into a `MediaPart`. Tool files carry either a data URL,
|
|
189
|
+
* Lift a tool-result file into a `MediaPart`. Tool files carry either a data URL, a URI, or raw base64 in
|
|
179
190
|
* `uri`; the declared `mime` wins over any data-URL prefix so tool authors control the type the model sees.
|
|
180
191
|
*/
|
|
181
192
|
export const toolFileMedia = (item) => {
|
|
182
193
|
const parsed = Media.parseDataUrl(item.uri);
|
|
183
194
|
const asset = parsed
|
|
184
195
|
? Media.from({ ...parsed.source, mediaType: item.mime })
|
|
185
|
-
:
|
|
196
|
+
: item.uri.includes("://")
|
|
186
197
|
? Media.url(item.uri, { mediaType: item.mime })
|
|
187
198
|
: Media.base64(item.uri, item.mime);
|
|
188
199
|
return Message.media(asset, { filename: item.name });
|
|
@@ -5,7 +5,10 @@ import { MediaRoute } from "../route/media.js";
|
|
|
5
5
|
import { type OpenString } from "../schema/index.js";
|
|
6
6
|
export declare const DEFAULT_BASE_URL = "https://api.stability.ai";
|
|
7
7
|
export type StabilityStylePreset = OpenString<"enhance" | "anime" | "photographic" | "digital-art" | "comic-book" | "fantasy-art" | "line-art" | "analog-film" | "neon-punk" | "isometric" | "low-poly" | "origami" | "modeling-compound" | "cinematic" | "3d-model" | "pixel-art" | "tile-texture">;
|
|
8
|
+
/** Native form fields; `aspect` lowers to `aspect_ratio`. */
|
|
8
9
|
export type StabilityImageOptions = {
|
|
10
|
+
readonly seed?: number;
|
|
11
|
+
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
9
12
|
readonly negative_prompt?: string;
|
|
10
13
|
readonly style_preset?: StabilityStylePreset;
|
|
11
14
|
readonly strength?: number;
|
|
@@ -13,6 +16,8 @@ export type StabilityImageOptions = {
|
|
|
13
16
|
} & Record<string, unknown>;
|
|
14
17
|
/** Creative upscale options; the one `images` source is the image to upscale. */
|
|
15
18
|
export type StabilityUpscaleOptions = {
|
|
19
|
+
readonly seed?: number;
|
|
20
|
+
readonly output_format?: OpenString<"jpeg" | "png" | "webp">;
|
|
16
21
|
readonly negative_prompt?: string;
|
|
17
22
|
readonly style_preset?: StabilityStylePreset;
|
|
18
23
|
readonly creativity?: number;
|
|
@@ -49,9 +49,7 @@ const fromRequest = Effect.fn("StabilityImages.fromRequest")(function* (request)
|
|
|
49
49
|
return yield* route.unsupported("media.images", `${route.name} core is text-to-image only; use ultra or sd3.5-*`);
|
|
50
50
|
return yield* form(route, {
|
|
51
51
|
prompt: request.prompt,
|
|
52
|
-
aspect_ratio: request.
|
|
53
|
-
seed: request.seed,
|
|
54
|
-
output_format: request.format,
|
|
52
|
+
aspect_ratio: request.aspect,
|
|
55
53
|
model: target === "sd3" ? request.model.id : undefined,
|
|
56
54
|
mode: target === "sd3" && edit ? "image-to-image" : undefined,
|
|
57
55
|
}, mergeJsonRecords(request.providerOptions, request.http?.body), request.images);
|
|
@@ -59,7 +57,7 @@ const fromRequest = Effect.fn("StabilityImages.fromRequest")(function* (request)
|
|
|
59
57
|
const fromUpscaleRequest = Effect.fn("StabilityImages.fromUpscaleRequest")(function* (request) {
|
|
60
58
|
if ((request.images?.length ?? 0) === 0)
|
|
61
59
|
return yield* ProviderShared.invalidRequest(`${upscaleRoute.name} upscale requires the source image in images`);
|
|
62
|
-
return yield* form(upscaleRoute, { prompt: request.prompt
|
|
60
|
+
return yield* form(upscaleRoute, { prompt: request.prompt }, mergeJsonRecords(request.providerOptions, request.http?.body), request.images);
|
|
63
61
|
});
|
|
64
62
|
// ---------------------------------------------------------------------------
|
|
65
63
|
// 6. Response decoding
|
|
@@ -75,9 +73,6 @@ const decodeImageDocument = (identity) => {
|
|
|
75
73
|
const image = yield* MediaInput.decodedAsset(output.invalid, `${identity.name} result`, data, undefined);
|
|
76
74
|
return new ImageResponse({
|
|
77
75
|
images: [image],
|
|
78
|
-
notices: document.finish_reason === "CONTENT_FILTERED"
|
|
79
|
-
? [{ type: "moderated", message: `${identity.name} blurred the image for violating its content policy` }]
|
|
80
|
-
: undefined,
|
|
81
76
|
providerMetadata: { stability: { seed: document.seed, finishReason: document.finish_reason } },
|
|
82
77
|
});
|
|
83
78
|
});
|
|
@@ -104,13 +99,13 @@ const decodeUpscaleResult = Effect.fn("StabilityImages.decodeUpscaleResult")(fun
|
|
|
104
99
|
// 7. Protocol and route
|
|
105
100
|
// ---------------------------------------------------------------------------
|
|
106
101
|
export const protocol = MediaProtocol.inline(route, {
|
|
107
|
-
unsupported: ["
|
|
102
|
+
unsupported: ["resolution"],
|
|
108
103
|
body: { from: fromRequest },
|
|
109
104
|
response: { decode: decodeResponse },
|
|
110
105
|
});
|
|
111
106
|
export const upscaleProtocol = MediaProtocol.queued(upscaleRoute, {
|
|
112
107
|
token: Token,
|
|
113
|
-
unsupported: ["n", "
|
|
108
|
+
unsupported: ["n", "aspect", "resolution"],
|
|
114
109
|
start: { body: { from: fromUpscaleRequest }, decode: decodeStart },
|
|
115
110
|
status: { path: (token) => `${RESULTS_PATH}/${token.id}`, decode: decodeStatus },
|
|
116
111
|
result: { path: (token) => `${RESULTS_PATH}/${token.id}`, decode: decodeUpscaleResult },
|