@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.
Files changed (288) hide show
  1. package/README.md +283 -114
  2. package/dist/ai-client.d.ts +2 -2
  3. package/dist/ai-client.js +2 -1
  4. package/dist/cache-policy.d.ts +4 -0
  5. package/dist/cache-policy.js +20 -6
  6. package/dist/decision-client.d.ts +21 -0
  7. package/dist/decision-client.js +55 -0
  8. package/dist/decision.d.ts +472 -0
  9. package/dist/decision.js +158 -0
  10. package/dist/effect.d.ts +38 -0
  11. package/dist/effect.js +27 -0
  12. package/dist/effort-updates.d.ts +2 -2
  13. package/dist/experimental/cloudflare-run-extraction.d.ts +13 -0
  14. package/dist/experimental/cloudflare-run-extraction.js +83 -0
  15. package/dist/experimental/extraction.d.ts +183 -0
  16. package/dist/experimental/extraction.js +102 -0
  17. package/dist/experimental/realtime.d.ts +7 -2
  18. package/dist/experimental/realtime.js +5 -5
  19. package/dist/experimental.d.ts +2 -3
  20. package/dist/experimental.js +1 -2
  21. package/dist/image-client.d.ts +13 -22
  22. package/dist/image-client.js +0 -1
  23. package/dist/image.d.ts +154 -324
  24. package/dist/image.js +4 -11
  25. package/dist/index.d.ts +1426 -34
  26. package/dist/index.js +127 -24
  27. package/dist/llm.d.ts +16 -1
  28. package/dist/llm.js +34 -0
  29. package/dist/media-client.d.ts +2 -1
  30. package/dist/media-model.d.ts +3 -0
  31. package/dist/media-model.js +4 -1
  32. package/dist/media.d.ts +0 -7
  33. package/dist/media.js +0 -6
  34. package/dist/protocols/alibaba-chat.d.ts +32 -12
  35. package/dist/protocols/alibaba-images.d.ts +22 -0
  36. package/dist/protocols/alibaba-images.js +77 -0
  37. package/dist/protocols/alibaba-messages.d.ts +62 -27
  38. package/dist/protocols/alibaba-responses.d.ts +41 -38
  39. package/dist/protocols/anthropic-messages.d.ts +196 -94
  40. package/dist/protocols/anthropic-messages.js +47 -16
  41. package/dist/protocols/bedrock-converse.d.ts +140 -47
  42. package/dist/protocols/bedrock-converse.js +114 -18
  43. package/dist/protocols/bedrock-event-stream.js +1 -0
  44. package/dist/protocols/bfl-images.d.ts +9 -0
  45. package/dist/protocols/bfl-images.js +17 -15
  46. package/dist/protocols/cartesia-speech.d.ts +32 -50
  47. package/dist/protocols/cloudflare-run-decisions.d.ts +9 -0
  48. package/dist/protocols/cloudflare-run-decisions.js +36 -0
  49. package/dist/protocols/cloudflare-workers-ai-images.d.ts +23 -0
  50. package/dist/protocols/cloudflare-workers-ai-images.js +80 -0
  51. package/dist/protocols/cohere-chat.d.ts +7 -8
  52. package/dist/protocols/deepgram-speech.d.ts +32 -50
  53. package/dist/protocols/elevenlabs-speech.d.ts +32 -50
  54. package/dist/protocols/fal-images.d.ts +10 -1
  55. package/dist/protocols/fal-images.js +16 -25
  56. package/dist/protocols/fal-video.js +1 -1
  57. package/dist/protocols/gemini.d.ts +37 -34
  58. package/dist/protocols/gemini.js +13 -8
  59. package/dist/protocols/google-images.d.ts +2 -2
  60. package/dist/protocols/google-images.js +8 -37
  61. package/dist/protocols/google-interactions.d.ts +18 -14
  62. package/dist/protocols/google-interactions.js +14 -7
  63. package/dist/protocols/google-speech.d.ts +32 -50
  64. package/dist/protocols/google-speech.js +0 -1
  65. package/dist/protocols/google-transcription.d.ts +32 -50
  66. package/dist/protocols/google-transcription.js +0 -1
  67. package/dist/protocols/google-video.d.ts +1 -1
  68. package/dist/protocols/google-video.js +2 -7
  69. package/dist/protocols/index.d.ts +3 -0
  70. package/dist/protocols/index.js +3 -0
  71. package/dist/protocols/meta-images.d.ts +7 -1
  72. package/dist/protocols/meta-images.js +10 -8
  73. package/dist/protocols/meta-messages.d.ts +73 -38
  74. package/dist/protocols/meta-messages.js +14 -1
  75. package/dist/protocols/meta-responses.d.ts +84 -78
  76. package/dist/protocols/meta-responses.js +29 -1
  77. package/dist/protocols/mistral-chat.d.ts +81 -78
  78. package/dist/protocols/mistral-chat.js +2 -1
  79. package/dist/protocols/open-responses-channel.js +47 -46
  80. package/dist/protocols/open-responses-continuation.d.ts +8 -2
  81. package/dist/protocols/open-responses-continuation.js +3 -1
  82. package/dist/protocols/open-responses.d.ts +426 -396
  83. package/dist/protocols/open-responses.js +16 -25
  84. package/dist/protocols/openai-chat.d.ts +764 -52
  85. package/dist/protocols/openai-chat.js +38 -25
  86. package/dist/protocols/openai-compatible-chat.d.ts +35 -15
  87. package/dist/protocols/openai-compatible-responses.d.ts +44 -42
  88. package/dist/protocols/openai-decisions.d.ts +22 -0
  89. package/dist/protocols/openai-decisions.js +128 -0
  90. package/dist/protocols/openai-images.d.ts +53 -56
  91. package/dist/protocols/openai-images.js +100 -34
  92. package/dist/protocols/openai-responses.d.ts +322 -264
  93. package/dist/protocols/openai-responses.js +33 -3
  94. package/dist/protocols/openai-speech.d.ts +32 -50
  95. package/dist/protocols/openai-speech.js +15 -5
  96. package/dist/protocols/openai-transcription.d.ts +32 -50
  97. package/dist/protocols/openai-transcription.js +15 -11
  98. package/dist/protocols/replicate-images.js +1 -1
  99. package/dist/protocols/runway-video.d.ts +1 -1
  100. package/dist/protocols/runway-video.js +1 -1
  101. package/dist/protocols/shared.d.ts +9 -1
  102. package/dist/protocols/shared.js +13 -2
  103. package/dist/protocols/stability-images.d.ts +5 -0
  104. package/dist/protocols/stability-images.js +4 -9
  105. package/dist/protocols/system-one.d.ts +101 -0
  106. package/dist/protocols/system-one.js +230 -0
  107. package/dist/protocols/utils/bedrock-cache.d.ts +2 -2
  108. package/dist/protocols/utils/bedrock-cache.js +7 -6
  109. package/dist/protocols/utils/cache.d.ts +14 -3
  110. package/dist/protocols/utils/cache.js +11 -4
  111. package/dist/protocols/utils/gemini-generate-content.d.ts +3 -3
  112. package/dist/protocols/utils/gemini-generate-content.js +10 -16
  113. package/dist/protocols/utils/gemini-json-schema.d.ts +5 -0
  114. package/dist/protocols/utils/gemini-json-schema.js +3 -3
  115. package/dist/protocols/utils/image-sizing.d.ts +31 -0
  116. package/dist/protocols/utils/image-sizing.js +73 -0
  117. package/dist/protocols/utils/media-input.d.ts +4 -5
  118. package/dist/protocols/utils/media-input.js +8 -5
  119. package/dist/protocols/utils/partial-json-options.d.ts +0 -1
  120. package/dist/protocols/utils/partial-json-options.js +0 -1
  121. package/dist/protocols/utils/speech-stream.d.ts +0 -1
  122. package/dist/protocols/utils/speech-stream.js +0 -1
  123. package/dist/protocols/utils/tool-schema.js +15 -5
  124. package/dist/protocols/utils/tool-stream.d.ts +10 -1
  125. package/dist/protocols/venice-chat.d.ts +71 -53
  126. package/dist/protocols/venice-chat.js +7 -6
  127. package/dist/protocols/xai-images.d.ts +1 -2
  128. package/dist/protocols/xai-images.js +9 -2
  129. package/dist/protocols/xai-responses.d.ts +88 -78
  130. package/dist/protocols/xai-responses.js +28 -2
  131. package/dist/protocols/xai-video.d.ts +1 -1
  132. package/dist/protocols/xai-video.js +11 -8
  133. package/dist/protocols/zai-chat.d.ts +54 -30
  134. package/dist/protocols/zai-chat.js +8 -3
  135. package/dist/protocols/zai-images.d.ts +3 -1
  136. package/dist/protocols/zai-images.js +12 -11
  137. package/dist/protocols/zai-messages.d.ts +72 -35
  138. package/dist/protocols/zai-messages.js +3 -2
  139. package/dist/provider-error.js +24 -7
  140. package/dist/providers/alibaba/images.d.ts +2 -0
  141. package/dist/providers/alibaba/images.js +1 -0
  142. package/dist/providers/alibaba.d.ts +146 -85
  143. package/dist/providers/alibaba.js +16 -8
  144. package/dist/providers/amazon-bedrock-mantle.d.ts +277 -218
  145. package/dist/providers/amazon-bedrock.d.ts +48 -28
  146. package/dist/providers/amazon-bedrock.js +11 -11
  147. package/dist/providers/anthropic-compatible.d.ts +64 -29
  148. package/dist/providers/anthropic.d.ts +64 -29
  149. package/dist/providers/azure/images.d.ts +2 -0
  150. package/dist/providers/azure/images.js +1 -0
  151. package/dist/providers/azure.d.ts +156 -115
  152. package/dist/providers/azure.js +19 -1
  153. package/dist/providers/baseten/decisions.d.ts +2 -0
  154. package/dist/providers/baseten/decisions.js +1 -0
  155. package/dist/providers/baseten.d.ts +75 -25
  156. package/dist/providers/baseten.js +41 -8
  157. package/dist/providers/cerebras.d.ts +66 -24
  158. package/dist/providers/cloudflare-ai-gateway/decisions.d.ts +2 -0
  159. package/dist/providers/cloudflare-ai-gateway/decisions.js +1 -0
  160. package/dist/providers/cloudflare-ai-gateway.d.ts +409 -272
  161. package/dist/providers/cloudflare-ai-gateway.js +33 -6
  162. package/dist/providers/cloudflare-workers-ai/decisions.d.ts +2 -0
  163. package/dist/providers/cloudflare-workers-ai/decisions.js +1 -0
  164. package/dist/providers/cloudflare-workers-ai/images.d.ts +2 -0
  165. package/dist/providers/cloudflare-workers-ai/images.js +1 -0
  166. package/dist/providers/cloudflare-workers-ai.d.ts +79 -25
  167. package/dist/providers/cloudflare-workers-ai.js +45 -9
  168. package/dist/providers/cohere.d.ts +117 -75
  169. package/dist/providers/deepinfra.d.ts +66 -24
  170. package/dist/providers/deepseek.d.ts +66 -24
  171. package/dist/providers/digitalocean/decisions.d.ts +2 -0
  172. package/dist/providers/digitalocean/decisions.js +1 -0
  173. package/dist/providers/digitalocean/images.d.ts +2 -0
  174. package/dist/providers/digitalocean/images.js +1 -0
  175. package/dist/providers/digitalocean.d.ts +108 -37
  176. package/dist/providers/digitalocean.js +39 -8
  177. package/dist/providers/fireworks.d.ts +66 -24
  178. package/dist/providers/google/images.d.ts +2 -0
  179. package/dist/providers/google/images.js +1 -0
  180. package/dist/providers/google-vertex/images.d.ts +2 -0
  181. package/dist/providers/google-vertex/images.js +1 -0
  182. package/dist/providers/google-vertex-chat.d.ts +33 -12
  183. package/dist/providers/google-vertex-messages.d.ts +64 -29
  184. package/dist/providers/google-vertex-mistral.d.ts +17 -17
  185. package/dist/providers/google-vertex-responses.d.ts +38 -35
  186. package/dist/providers/google-vertex.d.ts +20 -18
  187. package/dist/providers/google-vertex.js +32 -15
  188. package/dist/providers/google.d.ts +22 -21
  189. package/dist/providers/google.js +15 -8
  190. package/dist/providers/groq.d.ts +66 -28
  191. package/dist/providers/groq.js +0 -6
  192. package/dist/providers/meta.d.ts +144 -85
  193. package/dist/providers/minimax.d.ts +145 -86
  194. package/dist/providers/mistral.d.ts +28 -28
  195. package/dist/providers/moonshot.d.ts +277 -218
  196. package/dist/providers/open-responses-options.d.ts +0 -1
  197. package/dist/providers/open-responses-options.js +1 -1
  198. package/dist/providers/openai/images.d.ts +2 -0
  199. package/dist/providers/openai/images.js +1 -0
  200. package/dist/providers/openai-compatible/images.d.ts +2 -0
  201. package/dist/providers/openai-compatible/images.js +1 -0
  202. package/dist/providers/openai-compatible-responses.d.ts +38 -35
  203. package/dist/providers/openai-compatible.d.ts +36 -13
  204. package/dist/providers/openai-compatible.js +22 -10
  205. package/dist/providers/openai-options.d.ts +3 -2
  206. package/dist/providers/openai-options.js +0 -1
  207. package/dist/providers/openai.d.ts +146 -109
  208. package/dist/providers/openai.js +20 -4
  209. package/dist/providers/opencode/decisions.d.ts +2 -0
  210. package/dist/providers/opencode/decisions.js +1 -0
  211. package/dist/providers/opencode.d.ts +9 -10
  212. package/dist/providers/opencode.js +10 -8
  213. package/dist/providers/openrouter/decisions.d.ts +2 -0
  214. package/dist/providers/openrouter/decisions.js +1 -0
  215. package/dist/providers/openrouter/images.d.ts +2 -0
  216. package/dist/providers/openrouter/images.js +1 -0
  217. package/dist/providers/openrouter.d.ts +128 -47
  218. package/dist/providers/openrouter.js +38 -16
  219. package/dist/providers/togetherai.d.ts +66 -24
  220. package/dist/providers/typesafe-ai/decisions.d.ts +2 -0
  221. package/dist/providers/typesafe-ai/decisions.js +1 -0
  222. package/dist/providers/typesafe-ai.d.ts +9 -10
  223. package/dist/providers/typesafe-ai.js +10 -8
  224. package/dist/providers/venice.d.ts +214 -20
  225. package/dist/providers/vercel-ai-gateway/decisions.d.ts +2 -0
  226. package/dist/providers/vercel-ai-gateway/decisions.js +1 -0
  227. package/dist/providers/vercel-ai-gateway/images.d.ts +2 -0
  228. package/dist/providers/vercel-ai-gateway/images.js +1 -0
  229. package/dist/providers/vercel-ai-gateway.d.ts +10 -16
  230. package/dist/providers/vercel-ai-gateway.js +31 -85
  231. package/dist/providers/xai/images.d.ts +2 -0
  232. package/dist/providers/xai/images.js +1 -0
  233. package/dist/providers/xai.d.ts +40 -17
  234. package/dist/providers/xai.js +15 -8
  235. package/dist/providers/zai/images.d.ts +2 -0
  236. package/dist/providers/zai/images.js +1 -0
  237. package/dist/providers/zai-coding-plan.d.ts +224 -85
  238. package/dist/providers/zai.d.ts +110 -23
  239. package/dist/providers/zai.js +15 -8
  240. package/dist/route/auth-options.d.ts +0 -3
  241. package/dist/route/auth.d.ts +0 -2
  242. package/dist/route/auth.js +0 -2
  243. package/dist/route/client.d.ts +2 -4
  244. package/dist/route/client.js +29 -8
  245. package/dist/route/decision.d.ts +58 -0
  246. package/dist/route/decision.js +75 -0
  247. package/dist/route/executor-service.js +1 -1
  248. package/dist/route/executor.d.ts +1 -1
  249. package/dist/route/framing.d.ts +2 -0
  250. package/dist/route/index.d.ts +3 -0
  251. package/dist/route/index.js +3 -0
  252. package/dist/route/media-protocol.d.ts +1 -0
  253. package/dist/route/media-protocol.js +12 -8
  254. package/dist/route/media.d.ts +5 -1
  255. package/dist/route/media.js +6 -2
  256. package/dist/route/transport/http.js +1 -0
  257. package/dist/route/transport/index.d.ts +1 -0
  258. package/dist/schema/events.d.ts +148 -6
  259. package/dist/schema/events.js +20 -0
  260. package/dist/schema/messages.d.ts +6 -2
  261. package/dist/schema/messages.js +3 -0
  262. package/dist/schema/options.d.ts +5 -3
  263. package/dist/schema/options.js +8 -3
  264. package/dist/speech-client.d.ts +13 -22
  265. package/dist/speech-client.js +0 -1
  266. package/dist/speech.d.ts +148 -283
  267. package/dist/speech.js +2 -4
  268. package/dist/testing.d.ts +25 -24
  269. package/dist/testing.js +3 -21
  270. package/dist/transcription-client.d.ts +13 -22
  271. package/dist/transcription.d.ts +148 -283
  272. package/dist/transcription.js +1 -1
  273. package/dist/utils/media-type.js +22 -1
  274. package/dist/video-client.d.ts +13 -22
  275. package/dist/video-client.js +0 -1
  276. package/dist/video.d.ts +115 -250
  277. package/dist/video.js +3 -5
  278. package/package.json +516 -13
  279. package/dist/experimental/evaluation-client.d.ts +0 -21
  280. package/dist/experimental/evaluation-client.js +0 -64
  281. package/dist/experimental/evaluation.d.ts +0 -242
  282. package/dist/experimental/evaluation.js +0 -115
  283. package/dist/experimental/system-one.d.ts +0 -16
  284. package/dist/experimental/system-one.js +0 -135
  285. package/dist/promise.d.ts +0 -1021
  286. package/dist/promise.js +0 -101
  287. package/dist/provider.d.ts +0 -23
  288. 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 MediaUsage, type OpenString } from "../schema/index.js";
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`, `size`, `format`, `images`, `mask`) live on the request. */
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
- /** `generate` reads the whole JSON response as one frame, with the requested format for responses that omit it. */
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?: MediaUsage;
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
- } | undefined;
71
- readonly notices?: readonly {
72
- readonly type: "filtered" | "moderated" | "other";
73
- readonly message: string;
74
- readonly providerMetadata?: {
75
- readonly [x: string]: {
76
- readonly [x: string]: unknown;
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
- }[] | undefined;
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
- export declare const model: (input: MediaRoute.ModelInput) => ImageModel<OpenAIImageOptions>;
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
- } | undefined;
127
- readonly notices?: readonly {
128
- readonly type: "filtered" | "moderated" | "other";
129
- readonly message: string;
130
- readonly providerMetadata?: {
131
- readonly [x: string]: {
132
- readonly [x: string]: unknown;
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
- }[] | undefined;
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
- data: Schema.Array(Schema.Struct({ b64_json: Schema.String })),
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, route.provider, route.name).pipe(Effect.map((item) => (item.type === "ref" ? { file_id: item.value } : { image_url: item.value })));
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
- const fields = mergeJsonRecords({ n: request.n, size: request.size, output_format: request.format, ...(yield* streamOptions(request)) }, nativeOptions(request.providerOptions), request.http?.body);
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 requestedFormat = (body) => {
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: { openai: value },
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: usage(event.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
- const format = decoded.output_format ?? frame.requested ?? "png";
153
- const images = yield* Effect.forEach(decoded.data, (item, index) => MediaInput.decodedAsset(invalid, `${route.name} result ${index}`, item.b64_json, `image/${format}`, {
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: usage(decoded.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
- openai: {
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
- export const model = (input) => ImageModel.fromRoute({ protocol, baseURL: DEFAULT_BASE_URL, path: ({ request }) => (isEdit(request) ? EDIT_PATH : PATH) }, input);
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,