@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
|
@@ -1,13 +1,19 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
1
2
|
import { ImageModel, type ImageRequestFor } from "../image.js";
|
|
2
3
|
import { Media } from "../media.js";
|
|
3
4
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
4
5
|
import { MediaRoute } from "../route/media.js";
|
|
5
|
-
import { type
|
|
6
|
+
import { type OpenString } from "../schema/index.js";
|
|
6
7
|
export declare const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
7
8
|
export declare const PATH = "/images/generations";
|
|
8
9
|
export declare const EDIT_PATH = "/images/edits";
|
|
9
|
-
/** Provider-native options. Common fields (`n`, `
|
|
10
|
+
/** Provider-native options. Common fields (`n`, `aspect`, `resolution`, `images`) live on the request. */
|
|
10
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;
|
|
11
17
|
readonly quality?: OpenString<"auto" | "low" | "medium" | "high" | "standard" | "hd">;
|
|
12
18
|
readonly background?: OpenString<"auto" | "opaque" | "transparent">;
|
|
13
19
|
readonly moderation?: OpenString<"auto" | "low">;
|
|
@@ -16,10 +22,16 @@ export type OpenAIImageOptions = {
|
|
|
16
22
|
readonly partialImages?: number;
|
|
17
23
|
} & Record<string, unknown>;
|
|
18
24
|
export type Request = ImageRequestFor<OpenAIImageOptions>;
|
|
19
|
-
|
|
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. */
|
|
20
33
|
type Frame = string | {
|
|
21
34
|
readonly document: string;
|
|
22
|
-
readonly requested: string | undefined;
|
|
23
35
|
};
|
|
24
36
|
interface State {
|
|
25
37
|
readonly completed: number;
|
|
@@ -27,17 +39,9 @@ interface State {
|
|
|
27
39
|
readonly size?: string;
|
|
28
40
|
readonly quality?: string;
|
|
29
41
|
readonly background?: string;
|
|
30
|
-
readonly usage?:
|
|
42
|
+
readonly usage?: Schema.Schema.Type<typeof Usage>;
|
|
31
43
|
}
|
|
32
44
|
export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
33
|
-
readonly type: "generation-queued";
|
|
34
|
-
readonly id: string;
|
|
35
|
-
readonly position?: number | undefined;
|
|
36
|
-
} | {
|
|
37
|
-
readonly type: "generation-progress";
|
|
38
|
-
readonly id: string;
|
|
39
|
-
readonly progress?: number | undefined;
|
|
40
|
-
} | {
|
|
41
45
|
readonly type: "image-partial";
|
|
42
46
|
readonly index: number;
|
|
43
47
|
readonly image: Media.Asset;
|
|
@@ -45,17 +49,17 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
45
49
|
readonly type: "image";
|
|
46
50
|
readonly index: number;
|
|
47
51
|
readonly image: Media.Asset;
|
|
52
|
+
} | {
|
|
53
|
+
readonly type: "generation-queued";
|
|
54
|
+
readonly id: string;
|
|
55
|
+
readonly position?: number | undefined;
|
|
56
|
+
} | {
|
|
57
|
+
readonly type: "generation-progress";
|
|
58
|
+
readonly id: string;
|
|
59
|
+
readonly progress?: number | undefined;
|
|
48
60
|
} | {
|
|
49
61
|
readonly type: "finish";
|
|
50
62
|
readonly usage?: {
|
|
51
|
-
readonly type: "tokens";
|
|
52
|
-
readonly input?: number | undefined;
|
|
53
|
-
readonly output?: number | undefined;
|
|
54
|
-
readonly total?: number | undefined;
|
|
55
|
-
readonly details?: {
|
|
56
|
-
readonly [x: string]: unknown;
|
|
57
|
-
} | undefined;
|
|
58
|
-
} | {
|
|
59
63
|
readonly type: "seconds";
|
|
60
64
|
readonly seconds: number;
|
|
61
65
|
} | {
|
|
@@ -67,33 +71,27 @@ export declare const protocol: MediaProtocol.Streamed<Request, {
|
|
|
67
71
|
} | {
|
|
68
72
|
readonly type: "compute";
|
|
69
73
|
readonly seconds: number;
|
|
70
|
-
} |
|
|
71
|
-
|
|
72
|
-
readonly
|
|
73
|
-
readonly
|
|
74
|
-
readonly
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
};
|
|
74
|
+
} | {
|
|
75
|
+
readonly type: "tokens";
|
|
76
|
+
readonly input?: number | undefined;
|
|
77
|
+
readonly output?: number | undefined;
|
|
78
|
+
readonly total?: number | undefined;
|
|
79
|
+
readonly details?: {
|
|
80
|
+
readonly [x: string]: unknown;
|
|
78
81
|
} | undefined;
|
|
79
|
-
}
|
|
82
|
+
} | undefined;
|
|
80
83
|
readonly providerMetadata?: {
|
|
81
84
|
readonly [x: string]: {
|
|
82
85
|
readonly [x: string]: unknown;
|
|
83
86
|
};
|
|
84
87
|
} | undefined;
|
|
85
88
|
}, Frame, State>;
|
|
86
|
-
|
|
89
|
+
/** `provider` attributes models, refs, and metadata to an OpenAI-compatible host; it defaults to `openai`. */
|
|
90
|
+
export declare const model: (input: MediaRoute.ModelInput & {
|
|
91
|
+
readonly provider?: string;
|
|
92
|
+
}) => ImageModel<OpenAIImageOptions>;
|
|
87
93
|
export declare const OpenAIImages: {
|
|
88
94
|
readonly protocol: MediaProtocol.Streamed<Request, {
|
|
89
|
-
readonly type: "generation-queued";
|
|
90
|
-
readonly id: string;
|
|
91
|
-
readonly position?: number | undefined;
|
|
92
|
-
} | {
|
|
93
|
-
readonly type: "generation-progress";
|
|
94
|
-
readonly id: string;
|
|
95
|
-
readonly progress?: number | undefined;
|
|
96
|
-
} | {
|
|
97
95
|
readonly type: "image-partial";
|
|
98
96
|
readonly index: number;
|
|
99
97
|
readonly image: Media.Asset;
|
|
@@ -101,17 +99,17 @@ export declare const OpenAIImages: {
|
|
|
101
99
|
readonly type: "image";
|
|
102
100
|
readonly index: number;
|
|
103
101
|
readonly image: Media.Asset;
|
|
102
|
+
} | {
|
|
103
|
+
readonly type: "generation-queued";
|
|
104
|
+
readonly id: string;
|
|
105
|
+
readonly position?: number | undefined;
|
|
106
|
+
} | {
|
|
107
|
+
readonly type: "generation-progress";
|
|
108
|
+
readonly id: string;
|
|
109
|
+
readonly progress?: number | undefined;
|
|
104
110
|
} | {
|
|
105
111
|
readonly type: "finish";
|
|
106
112
|
readonly usage?: {
|
|
107
|
-
readonly type: "tokens";
|
|
108
|
-
readonly input?: number | undefined;
|
|
109
|
-
readonly output?: number | undefined;
|
|
110
|
-
readonly total?: number | undefined;
|
|
111
|
-
readonly details?: {
|
|
112
|
-
readonly [x: string]: unknown;
|
|
113
|
-
} | undefined;
|
|
114
|
-
} | {
|
|
115
113
|
readonly type: "seconds";
|
|
116
114
|
readonly seconds: number;
|
|
117
115
|
} | {
|
|
@@ -123,16 +121,15 @@ export declare const OpenAIImages: {
|
|
|
123
121
|
} | {
|
|
124
122
|
readonly type: "compute";
|
|
125
123
|
readonly seconds: number;
|
|
126
|
-
} |
|
|
127
|
-
|
|
128
|
-
readonly
|
|
129
|
-
readonly
|
|
130
|
-
readonly
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
};
|
|
124
|
+
} | {
|
|
125
|
+
readonly type: "tokens";
|
|
126
|
+
readonly input?: number | undefined;
|
|
127
|
+
readonly output?: number | undefined;
|
|
128
|
+
readonly total?: number | undefined;
|
|
129
|
+
readonly details?: {
|
|
130
|
+
readonly [x: string]: unknown;
|
|
134
131
|
} | undefined;
|
|
135
|
-
}
|
|
132
|
+
} | undefined;
|
|
136
133
|
readonly providerMetadata?: {
|
|
137
134
|
readonly [x: string]: {
|
|
138
135
|
readonly [x: string]: unknown;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { Effect, Schema, Stream } from "effect";
|
|
1
|
+
import { Effect, Schema, SchemaGetter, Stream } from "effect";
|
|
2
2
|
import { ImageFinishEvent, ImageModel, ImageOutputEvent, ImagePartialEvent, } from "../image.js";
|
|
3
3
|
import { Media } from "../media.js";
|
|
4
|
+
import { classifyProviderFailure } from "../provider-error.js";
|
|
4
5
|
import { Framing } from "../route/framing.js";
|
|
5
6
|
import { MediaProtocol } from "../route/media-protocol.js";
|
|
6
7
|
import { MediaRoute } from "../route/media.js";
|
|
7
|
-
import { mergeJsonRecords } from "../schema/index.js";
|
|
8
|
-
import { ProviderShared } from "./shared.js";
|
|
8
|
+
import { AIError, mergeJsonRecords, ProviderID, tokenUsage } from "../schema/index.js";
|
|
9
|
+
import { optionalNull, ProviderShared } from "./shared.js";
|
|
10
|
+
import { ImageSizing } from "./utils/image-sizing.js";
|
|
9
11
|
import { MediaInput } from "./utils/media-input.js";
|
|
10
12
|
const route = MediaProtocol.identity({ id: "openai-images", name: "OpenAI Images", provider: "openai" });
|
|
11
13
|
export const DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
@@ -29,13 +31,28 @@ const Settings = {
|
|
|
29
31
|
background: Schema.optional(Schema.String),
|
|
30
32
|
};
|
|
31
33
|
const OpenAIImageResponse = Schema.Struct({
|
|
32
|
-
|
|
34
|
+
// `response_format` picks `b64_json` or `url`; DALL·E and many compatible hosts default to `url`.
|
|
35
|
+
data: Schema.Array(Schema.Struct({ b64_json: optionalNull(Schema.String), url: optionalNull(Schema.String) })),
|
|
33
36
|
...Settings,
|
|
34
37
|
usage: Schema.optional(Usage),
|
|
35
38
|
});
|
|
36
39
|
// ---------------------------------------------------------------------------
|
|
37
40
|
// 3. Streaming event schema
|
|
38
41
|
// ---------------------------------------------------------------------------
|
|
42
|
+
const EVENT_TYPES = new Set([
|
|
43
|
+
"image_generation.partial_image",
|
|
44
|
+
"image_edit.partial_image",
|
|
45
|
+
"image_generation.completed",
|
|
46
|
+
"image_edit.completed",
|
|
47
|
+
"error",
|
|
48
|
+
]);
|
|
49
|
+
/** OpenAI may add stream event types; frames outside `EVENT_TYPES` are ignored, while a malformed known type still fails decoding. */
|
|
50
|
+
const UnknownEvent = Schema.Struct({
|
|
51
|
+
type: Schema.String.check(Schema.makeFilter((type) => !EVENT_TYPES.has(type))),
|
|
52
|
+
}).pipe(Schema.decodeTo(Schema.Struct({ type: Schema.Literal("unknown"), rawType: Schema.String }), {
|
|
53
|
+
decode: SchemaGetter.transform((event) => ({ type: "unknown", rawType: event.type })),
|
|
54
|
+
encode: SchemaGetter.forbidden(() => "Unknown OpenAI Images stream events are decode-only"),
|
|
55
|
+
}));
|
|
39
56
|
const StreamEvent = Schema.Union([
|
|
40
57
|
Schema.Struct({
|
|
41
58
|
type: Schema.Literals(["image_generation.partial_image", "image_edit.partial_image"]),
|
|
@@ -51,6 +68,11 @@ const StreamEvent = Schema.Union([
|
|
|
51
68
|
output_format: Schema.String,
|
|
52
69
|
usage: Schema.optional(Usage),
|
|
53
70
|
}),
|
|
71
|
+
Schema.Struct({
|
|
72
|
+
type: Schema.Literal("error"),
|
|
73
|
+
error: Schema.optional(Schema.Struct({ message: Schema.optional(Schema.String) })),
|
|
74
|
+
}),
|
|
75
|
+
UnknownEvent,
|
|
54
76
|
]);
|
|
55
77
|
const decodeEvent = route.decodeFrame(StreamEvent);
|
|
56
78
|
const decodeDocument = Schema.decodeUnknownEffect(Schema.fromJsonString(OpenAIImageResponse));
|
|
@@ -62,8 +84,38 @@ const RESERVED_FORM_FIELDS = new Set(["model", "prompt", "image", "image[]", "im
|
|
|
62
84
|
const nativeOptions = (options) => {
|
|
63
85
|
if (!options)
|
|
64
86
|
return undefined;
|
|
65
|
-
const { outputCompression, partialImages: _, ...native } = options;
|
|
66
|
-
return { output_compression: outputCompression, ...native };
|
|
87
|
+
const { outputCompression, outputFormat, partialImages: _, mask: __, ...native } = options;
|
|
88
|
+
return { output_compression: outputCompression, output_format: outputFormat, ...native };
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* `gpt-image-1`, `chatgpt-image` and DALL·E take a fixed list, `gpt-image-2` and later any size within limits. Other
|
|
92
|
+
* models are gateway-hosted (`bfl/flux-2-pro`); the gateway passes `size` through to them as is.
|
|
93
|
+
*/
|
|
94
|
+
const sizes = (model) => {
|
|
95
|
+
const id = model.slice(model.lastIndexOf("/") + 1);
|
|
96
|
+
if (id.startsWith("dall-e-2"))
|
|
97
|
+
return [256, 512, 1024].map((edge) => ({ width: edge, height: edge }));
|
|
98
|
+
if (id.startsWith("dall-e-3"))
|
|
99
|
+
return [
|
|
100
|
+
{ width: 1024, height: 1024 },
|
|
101
|
+
{ width: 1792, height: 1024 },
|
|
102
|
+
{ width: 1024, height: 1792 },
|
|
103
|
+
];
|
|
104
|
+
if (/^(gpt-image-1|chatgpt-image)/.test(id))
|
|
105
|
+
return [
|
|
106
|
+
{ width: 1024, height: 1024 },
|
|
107
|
+
{ width: 1536, height: 1024 },
|
|
108
|
+
{ width: 1024, height: 1536 },
|
|
109
|
+
];
|
|
110
|
+
if (id.startsWith("gpt-image-"))
|
|
111
|
+
return { multiple: 16, maxEdge: 3840, minPixels: 655_360, maxPixels: 8_294_400, maxRatio: 3 };
|
|
112
|
+
return { multiple: 16 };
|
|
113
|
+
};
|
|
114
|
+
const size = (request) => {
|
|
115
|
+
if (!ImageSizing.isRequested(request))
|
|
116
|
+
return undefined;
|
|
117
|
+
const options = sizes(request.model.id);
|
|
118
|
+
return "multiple" in options ? ImageSizing.fit(request, options) : ImageSizing.nearest(request, options);
|
|
67
119
|
};
|
|
68
120
|
const streamOptions = (request) => {
|
|
69
121
|
if (request.mode !== "stream")
|
|
@@ -74,13 +126,20 @@ const streamOptions = (request) => {
|
|
|
74
126
|
};
|
|
75
127
|
const isEdit = (request) => (request.images?.length ?? 0) > 0;
|
|
76
128
|
const isInline = (asset) => asset.source.type === "bytes" || asset.source.type === "base64";
|
|
77
|
-
const reference = (asset) => ProviderShared.mediaReference(asset,
|
|
129
|
+
const reference = (asset, provider) => ProviderShared.mediaReference(asset, provider, route.name).pipe(Effect.map((item) => (item.type === "ref" ? { file_id: item.value } : { image_url: item.value })));
|
|
78
130
|
const fromRequest = Effect.fn("OpenAIImages.fromRequest")(function* (request) {
|
|
79
131
|
const images = request.images ?? [];
|
|
80
|
-
const mask = request.mask;
|
|
132
|
+
const mask = request.providerOptions?.mask;
|
|
81
133
|
if (mask !== undefined && images.length === 0)
|
|
82
134
|
return yield* ProviderShared.invalidRequest("An OpenAI image mask requires at least one input image");
|
|
83
|
-
|
|
135
|
+
if (request.providerOptions?.size !== undefined && ImageSizing.isRequested(request))
|
|
136
|
+
return yield* ProviderShared.invalidRequest("OpenAI providerOptions.size replaces aspect and resolution; set one or the other");
|
|
137
|
+
const chosen = size(request);
|
|
138
|
+
const fields = mergeJsonRecords({
|
|
139
|
+
n: request.n,
|
|
140
|
+
size: chosen === undefined ? undefined : `${chosen.width}x${chosen.height}`,
|
|
141
|
+
...(yield* streamOptions(request)),
|
|
142
|
+
}, nativeOptions(request.providerOptions), request.http?.body);
|
|
84
143
|
// Owned bytes go through multipart edits; remote URLs and file IDs use the JSON edits body instead.
|
|
85
144
|
if (images.length > 0 && images.every(isInline) && (mask === undefined || isInline(mask))) {
|
|
86
145
|
const form = new FormData();
|
|
@@ -91,8 +150,8 @@ const fromRequest = Effect.fn("OpenAIImages.fromRequest")(function* (request) {
|
|
|
91
150
|
form.append("mask", MediaInput.blob(yield* MediaInput.inlineBytes(route.id, mask), mask.mediaType), "mask");
|
|
92
151
|
return MediaProtocol.multipart(form);
|
|
93
152
|
}
|
|
94
|
-
const references = yield* Effect.forEach(images, reference);
|
|
95
|
-
const maskReference = mask === undefined ? undefined : yield* reference(mask);
|
|
153
|
+
const references = yield* Effect.forEach(images, (image) => reference(image, request.model.provider));
|
|
154
|
+
const maskReference = mask === undefined ? undefined : yield* reference(mask, request.model.provider);
|
|
96
155
|
return MediaProtocol.json(mergeJsonRecords({
|
|
97
156
|
model: request.model.id,
|
|
98
157
|
prompt: request.prompt,
|
|
@@ -103,21 +162,14 @@ const fromRequest = Effect.fn("OpenAIImages.fromRequest")(function* (request) {
|
|
|
103
162
|
// ---------------------------------------------------------------------------
|
|
104
163
|
// 6. Stream parsing
|
|
105
164
|
// ---------------------------------------------------------------------------
|
|
106
|
-
const
|
|
107
|
-
if (body.type === "binary")
|
|
108
|
-
return undefined;
|
|
109
|
-
const value = body.type === "json" ? body.value.output_format : body.value.get("output_format");
|
|
110
|
-
return typeof value === "string" ? value : undefined;
|
|
111
|
-
};
|
|
112
|
-
const usage = (value) => value === undefined
|
|
165
|
+
const usage = (value, provider) => value === undefined
|
|
113
166
|
? undefined
|
|
114
|
-
: {
|
|
115
|
-
type: "tokens",
|
|
167
|
+
: tokenUsage({
|
|
116
168
|
input: value.input_tokens,
|
|
117
169
|
output: value.output_tokens,
|
|
118
170
|
total: value.total_tokens,
|
|
119
|
-
details: {
|
|
120
|
-
};
|
|
171
|
+
details: { [provider]: value },
|
|
172
|
+
});
|
|
121
173
|
/** `size` echoes the rendered `WIDTHxHEIGHT`; `auto` or any other value leaves the dimensions unknown. */
|
|
122
174
|
const info = (format, size) => {
|
|
123
175
|
const match = size?.match(/^(\d+)x(\d+)$/);
|
|
@@ -128,6 +180,15 @@ const eventImage = (frame, label, data, format, size) => MediaInput.decodedAsset
|
|
|
128
180
|
});
|
|
129
181
|
const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
130
182
|
const event = yield* decodeEvent(frame);
|
|
183
|
+
if (event.type === "unknown")
|
|
184
|
+
return [state, []];
|
|
185
|
+
if (event.type === "error")
|
|
186
|
+
return yield* new AIError({
|
|
187
|
+
reason: classifyProviderFailure({
|
|
188
|
+
message: `${route.name} stream failed: ${event.error?.message ?? "unknown error"}`,
|
|
189
|
+
rawBody: frame,
|
|
190
|
+
}),
|
|
191
|
+
});
|
|
131
192
|
const format = event.output_format;
|
|
132
193
|
if ("partial_image_index" in event) {
|
|
133
194
|
const image = yield* eventImage(frame, `${route.name} partial image`, event.b64_json, format, event.size);
|
|
@@ -141,7 +202,7 @@ const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
|
141
202
|
size: event.size,
|
|
142
203
|
quality: event.quality,
|
|
143
204
|
background: event.background,
|
|
144
|
-
usage:
|
|
205
|
+
usage: event.usage,
|
|
145
206
|
},
|
|
146
207
|
[ImageOutputEvent.make({ index: state.completed, image })],
|
|
147
208
|
];
|
|
@@ -149,8 +210,10 @@ const onEvent = Effect.fnUntraced(function* (state, frame) {
|
|
|
149
210
|
const onDocument = Effect.fnUntraced(function* (frame) {
|
|
150
211
|
const invalid = (message, cause) => route.frameError(message, frame.document, cause);
|
|
151
212
|
const decoded = yield* decodeDocument(frame.document).pipe(Effect.mapError((cause) => invalid(`${route.name} returned an invalid response`, cause)));
|
|
152
|
-
|
|
153
|
-
const
|
|
213
|
+
// Compatible hosts may omit `output_format` and ignore the requested one, so the bytes decide unreported types.
|
|
214
|
+
const format = decoded.output_format;
|
|
215
|
+
const mediaType = format === undefined ? undefined : `image/${format}`;
|
|
216
|
+
const images = yield* Effect.forEach(decoded.data, (item, index) => MediaInput.imageOutput(invalid, `${route.name} result ${index}`, item, mediaType, {
|
|
154
217
|
info: info(format, decoded.size),
|
|
155
218
|
}));
|
|
156
219
|
if (images.length === 0)
|
|
@@ -161,19 +224,20 @@ const onDocument = Effect.fnUntraced(function* (frame) {
|
|
|
161
224
|
size: decoded.size,
|
|
162
225
|
quality: decoded.quality,
|
|
163
226
|
background: decoded.background,
|
|
164
|
-
usage:
|
|
227
|
+
usage: decoded.usage,
|
|
165
228
|
};
|
|
166
229
|
return [state, images.map((image, index) => ImageOutputEvent.make({ index, image }))];
|
|
167
230
|
});
|
|
168
231
|
const step = (state, frame) => (typeof frame === "string" ? onEvent(state, frame) : onDocument(frame));
|
|
169
|
-
const finish = (state) => {
|
|
232
|
+
const finish = (state, context) => {
|
|
170
233
|
if (state.completed === 0)
|
|
171
234
|
return Effect.fail(route.incomplete());
|
|
235
|
+
const provider = context.request.model.provider;
|
|
172
236
|
return Effect.succeed([
|
|
173
237
|
ImageFinishEvent.make({
|
|
174
|
-
usage: state.usage,
|
|
238
|
+
usage: usage(state.usage, provider),
|
|
175
239
|
providerMetadata: {
|
|
176
|
-
|
|
240
|
+
[provider]: {
|
|
177
241
|
outputFormat: state.format,
|
|
178
242
|
size: state.size,
|
|
179
243
|
quality: state.quality,
|
|
@@ -187,18 +251,20 @@ const finish = (state) => {
|
|
|
187
251
|
// 7. Protocol and route
|
|
188
252
|
// ---------------------------------------------------------------------------
|
|
189
253
|
export const protocol = MediaProtocol.stream(route, {
|
|
190
|
-
unsupported: ["aspectRatio", "seed"],
|
|
191
254
|
body: { from: fromRequest },
|
|
192
255
|
frames: (bytes, context) => context.request.mode === "stream"
|
|
193
256
|
? Framing.sse.frame(bytes)
|
|
194
|
-
: Framing.document
|
|
195
|
-
.frame(bytes)
|
|
196
|
-
.pipe(Stream.map((document) => ({ document, requested: requestedFormat(context.body) }))),
|
|
257
|
+
: Framing.document.frame(bytes).pipe(Stream.map((document) => ({ document }))),
|
|
197
258
|
initial: () => ({ completed: 0 }),
|
|
198
259
|
step,
|
|
199
260
|
finish,
|
|
200
261
|
});
|
|
201
|
-
|
|
262
|
+
/** `provider` attributes models, refs, and metadata to an OpenAI-compatible host; it defaults to `openai`. */
|
|
263
|
+
export const model = (input) => ImageModel.fromRoute({
|
|
264
|
+
protocol: input.provider === undefined ? protocol : { ...protocol, provider: ProviderID.make(input.provider) },
|
|
265
|
+
baseURL: DEFAULT_BASE_URL,
|
|
266
|
+
path: ({ request }) => (isEdit(request) ? EDIT_PATH : PATH),
|
|
267
|
+
}, input);
|
|
202
268
|
export const OpenAIImages = {
|
|
203
269
|
protocol,
|
|
204
270
|
model,
|