@bastani/pi-ai 0.9.71 → 0.9.72-alpha.1

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 (164) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +78 -8
  3. package/dist/api/anthropic-messages.d.ts.map +1 -1
  4. package/dist/api/anthropic-messages.js +50 -10
  5. package/dist/api/anthropic-messages.js.map +1 -1
  6. package/dist/api/azure-openai-responses.d.ts.map +1 -1
  7. package/dist/api/azure-openai-responses.js +7 -2
  8. package/dist/api/azure-openai-responses.js.map +1 -1
  9. package/dist/api/bedrock-converse-stream.d.ts.map +1 -1
  10. package/dist/api/bedrock-converse-stream.js +28 -1
  11. package/dist/api/bedrock-converse-stream.js.map +1 -1
  12. package/dist/api/bedrock-converse-stream.lazy.d.ts +7 -0
  13. package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -1
  14. package/dist/api/bedrock-converse-stream.lazy.js +18 -5
  15. package/dist/api/bedrock-converse-stream.lazy.js.map +1 -1
  16. package/dist/api/google-generative-ai.d.ts +3 -1
  17. package/dist/api/google-generative-ai.d.ts.map +1 -1
  18. package/dist/api/google-generative-ai.js +4 -2
  19. package/dist/api/google-generative-ai.js.map +1 -1
  20. package/dist/api/google-shared.d.ts +10 -2
  21. package/dist/api/google-shared.d.ts.map +1 -1
  22. package/dist/api/google-shared.js +31 -9
  23. package/dist/api/google-shared.js.map +1 -1
  24. package/dist/api/google-vertex.d.ts +3 -1
  25. package/dist/api/google-vertex.d.ts.map +1 -1
  26. package/dist/api/google-vertex.js +4 -2
  27. package/dist/api/google-vertex.js.map +1 -1
  28. package/dist/api/lazy.d.ts +2 -2
  29. package/dist/api/lazy.d.ts.map +1 -1
  30. package/dist/api/lazy.js +10 -9
  31. package/dist/api/lazy.js.map +1 -1
  32. package/dist/api/mistral-conversations.d.ts.map +1 -1
  33. package/dist/api/mistral-conversations.js +12 -1
  34. package/dist/api/mistral-conversations.js.map +1 -1
  35. package/dist/api/openai-codex-responses.d.ts.map +1 -1
  36. package/dist/api/openai-codex-responses.js +6 -1
  37. package/dist/api/openai-codex-responses.js.map +1 -1
  38. package/dist/api/openai-completions.d.ts +2 -1
  39. package/dist/api/openai-completions.d.ts.map +1 -1
  40. package/dist/api/openai-completions.js +112 -15
  41. package/dist/api/openai-completions.js.map +1 -1
  42. package/dist/api/openai-images-options.d.ts +107 -0
  43. package/dist/api/openai-images-options.d.ts.map +1 -0
  44. package/dist/api/openai-images-options.js +106 -0
  45. package/dist/api/openai-images-options.js.map +1 -0
  46. package/dist/api/openai-images.d.ts +3 -11
  47. package/dist/api/openai-images.d.ts.map +1 -1
  48. package/dist/api/openai-images.js +117 -16
  49. package/dist/api/openai-images.js.map +1 -1
  50. package/dist/api/openai-responses-shared.js +6 -2
  51. package/dist/api/openai-responses-shared.js.map +1 -1
  52. package/dist/api/openai-responses.d.ts.map +1 -1
  53. package/dist/api/openai-responses.js +29 -7
  54. package/dist/api/openai-responses.js.map +1 -1
  55. package/dist/auth/oauth/load.d.ts.map +1 -1
  56. package/dist/auth/oauth/load.js +5 -2
  57. package/dist/auth/oauth/load.js.map +1 -1
  58. package/dist/env-api-keys.d.ts.map +1 -1
  59. package/dist/env-api-keys.js +1 -0
  60. package/dist/env-api-keys.js.map +1 -1
  61. package/dist/image-generation-error.d.ts +203 -0
  62. package/dist/image-generation-error.d.ts.map +1 -0
  63. package/dist/image-generation-error.js +441 -0
  64. package/dist/image-generation-error.js.map +1 -0
  65. package/dist/index.d.ts +4 -1
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +4 -0
  68. package/dist/index.js.map +1 -1
  69. package/dist/lazy-bedrock-provider.d.ts +10 -0
  70. package/dist/lazy-bedrock-provider.d.ts.map +1 -0
  71. package/dist/lazy-bedrock-provider.js +13 -0
  72. package/dist/lazy-bedrock-provider.js.map +1 -0
  73. package/dist/lazy-oauth.d.ts +10 -0
  74. package/dist/lazy-oauth.d.ts.map +1 -0
  75. package/dist/lazy-oauth.js +23 -0
  76. package/dist/lazy-oauth.js.map +1 -0
  77. package/dist/models.d.ts.map +1 -1
  78. package/dist/models.generated.d.ts +4 -0
  79. package/dist/models.generated.d.ts.map +1 -1
  80. package/dist/models.generated.js +4 -0
  81. package/dist/models.generated.js.map +1 -1
  82. package/dist/models.js +8 -8
  83. package/dist/models.js.map +1 -1
  84. package/dist/providers/all.d.ts.map +1 -1
  85. package/dist/providers/all.js +2 -0
  86. package/dist/providers/all.js.map +1 -1
  87. package/dist/providers/azure.d.ts.map +1 -1
  88. package/dist/providers/azure.js +2 -2
  89. package/dist/providers/azure.js.map +1 -1
  90. package/dist/providers/compat-schema.d.ts +15 -0
  91. package/dist/providers/compat-schema.d.ts.map +1 -1
  92. package/dist/providers/compat-schema.js +19 -1
  93. package/dist/providers/compat-schema.js.map +1 -1
  94. package/dist/providers/data/.manifest.json +1 -1
  95. package/dist/providers/data/anthropic.json +1 -1
  96. package/dist/providers/data/azure.json +1 -1
  97. package/dist/providers/data/baseten.json +1 -1
  98. package/dist/providers/data/cloudflare-ai-gateway.json +1 -1
  99. package/dist/providers/data/fireworks.json +1 -1
  100. package/dist/providers/data/github-copilot.json +1 -1
  101. package/dist/providers/data/google-vertex.json +1 -1
  102. package/dist/providers/data/google.json +1 -1
  103. package/dist/providers/data/nvidia.json +1 -1
  104. package/dist/providers/data/openai-images.json +1 -1
  105. package/dist/providers/data/opencode-go.json +1 -1
  106. package/dist/providers/data/opencode.json +1 -1
  107. package/dist/providers/data/openrouter.json +1 -1
  108. package/dist/providers/data/qwen-token-plan-individual-cn.json +1 -0
  109. package/dist/providers/data/qwen-token-plan-individual.json +1 -1
  110. package/dist/providers/data/together.json +1 -1
  111. package/dist/providers/data/vercel-ai-gateway.json +1 -1
  112. package/dist/providers/github-copilot-capabilities.d.ts +67 -0
  113. package/dist/providers/github-copilot-capabilities.d.ts.map +1 -0
  114. package/dist/providers/github-copilot-capabilities.js +81 -0
  115. package/dist/providers/github-copilot-capabilities.js.map +1 -0
  116. package/dist/providers/github-copilot-errors.d.ts +243 -0
  117. package/dist/providers/github-copilot-errors.d.ts.map +1 -0
  118. package/dist/providers/github-copilot-errors.js +720 -0
  119. package/dist/providers/github-copilot-errors.js.map +1 -0
  120. package/dist/providers/github-copilot.d.ts +1 -2
  121. package/dist/providers/github-copilot.d.ts.map +1 -1
  122. package/dist/providers/github-copilot.js +29 -4
  123. package/dist/providers/github-copilot.js.map +1 -1
  124. package/dist/providers/openai-codex.js +1 -1
  125. package/dist/providers/openai-codex.js.map +1 -1
  126. package/dist/providers/qwen-token-plan-individual-cn.d.ts +3 -0
  127. package/dist/providers/qwen-token-plan-individual-cn.d.ts.map +1 -0
  128. package/dist/providers/qwen-token-plan-individual-cn.js +15 -0
  129. package/dist/providers/qwen-token-plan-individual-cn.js.map +1 -0
  130. package/dist/providers/qwen-token-plan-individual-cn.models.d.ts +5 -0
  131. package/dist/providers/qwen-token-plan-individual-cn.models.d.ts.map +1 -0
  132. package/dist/providers/qwen-token-plan-individual-cn.models.js +8 -0
  133. package/dist/providers/qwen-token-plan-individual-cn.models.js.map +1 -0
  134. package/dist/types.d.ts +29 -3
  135. package/dist/types.d.ts.map +1 -1
  136. package/dist/types.js.map +1 -1
  137. package/dist/utils/error-body.d.ts +8 -0
  138. package/dist/utils/error-body.d.ts.map +1 -1
  139. package/dist/utils/error-body.js +28 -1
  140. package/dist/utils/error-body.js.map +1 -1
  141. package/dist/utils/model-operations.d.ts.map +1 -1
  142. package/dist/utils/model-operations.js +6 -0
  143. package/dist/utils/model-operations.js.map +1 -1
  144. package/dist/utils/overflow.d.ts +4 -0
  145. package/dist/utils/overflow.d.ts.map +1 -1
  146. package/dist/utils/overflow.js +16 -3
  147. package/dist/utils/overflow.js.map +1 -1
  148. package/dist/utils/provider-retry.d.ts +2 -0
  149. package/dist/utils/provider-retry.d.ts.map +1 -1
  150. package/dist/utils/provider-retry.js +2 -0
  151. package/dist/utils/provider-retry.js.map +1 -1
  152. package/dist/utils/retry.d.ts +3 -1
  153. package/dist/utils/retry.d.ts.map +1 -1
  154. package/dist/utils/retry.js +9 -0
  155. package/dist/utils/retry.js.map +1 -1
  156. package/dist/utils/thinking-off.d.ts +23 -0
  157. package/dist/utils/thinking-off.d.ts.map +1 -0
  158. package/dist/utils/thinking-off.js +51 -0
  159. package/dist/utils/thinking-off.js.map +1 -0
  160. package/dist/utils/version.d.ts +1 -1
  161. package/dist/utils/version.d.ts.map +1 -1
  162. package/dist/utils/version.js +1 -1
  163. package/dist/utils/version.js.map +1 -1
  164. package/package.json +9 -1
@@ -0,0 +1,107 @@
1
+ import type { ImageEditParamsNonStreaming } from "openai/resources/images";
2
+ import type { ImageApi, ImageModel, ImagesInputContent, ImagesInputImage, ImagesOptions } from "../types.ts";
3
+ /**
4
+ * Bounds the OpenAI Images API accepts for GPT Image 2.6 request fields. Requests outside them fail
5
+ * before anything is sent. The image generation guide documents 2-64 GIF frames and the API
6
+ * reference documents 2-16. A live request with 17 frames was rejected ("Use an integer from 2 to
7
+ * 16", #4332), so the API reference is right and the maximum is 16.
8
+ */
9
+ export declare const OPENAI_IMAGES_LIMITS: {
10
+ readonly gifFrameCount: {
11
+ readonly min: 2;
12
+ readonly max: 16;
13
+ };
14
+ readonly gifFrameDurationMs: {
15
+ readonly min: 20;
16
+ readonly max: 2000;
17
+ };
18
+ readonly outputCompression: {
19
+ readonly min: 0;
20
+ readonly max: 100;
21
+ };
22
+ readonly safetyIdentifierMaxLength: 128;
23
+ };
24
+ export type OpenAIImagesOutputFormat = "png" | "jpeg" | "webp" | "svg" | "gif";
25
+ /** The model that writes the SVG for `outputFormat: "svg"`, and its reasoning effort. */
26
+ export interface OpenAIImagesSvgOptions {
27
+ /** Required by the API when SVG model selection is enabled for the account. */
28
+ model: string;
29
+ reasoning?: {
30
+ effort?: "none" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
31
+ };
32
+ }
33
+ /**
34
+ * Request options for OpenAI's Images API. Each field is sent only when set.
35
+ *
36
+ * Image blocks in the context go to `/images/edits`: the first image without a `role` is the base
37
+ * image and every other image without a `role` is a reference. An image block with `role: "markup"`
38
+ * is sent as the `markup` field and one with `role: "mask"` as the `mask` field; both apply to the
39
+ * first image, and they cannot be combined.
40
+ */
41
+ export interface OpenAIImagesOptions extends ImagesOptions {
42
+ /** `auto`, a listed size, or a custom `WIDTHxHEIGHT` where the model supports one. */
43
+ size?: "auto" | "1024x1024" | "1536x1024" | "1024x1536" | (string & {});
44
+ quality?: "low" | "medium" | "high" | "xhigh" | "max" | "auto";
45
+ /** Number of images to generate. SVG output requires 1. */
46
+ n?: number;
47
+ /** `transparent` needs a PNG or WebP output format. */
48
+ background?: "transparent" | "opaque" | "auto";
49
+ /**
50
+ * `svg` and `gif` are generation-only and need a model that supports them (GPT Image 2.6).
51
+ * `svg` returns UTF-8 XML as `image/svg+xml`; `gif` returns an animation as `image/gif`.
52
+ */
53
+ outputFormat?: OpenAIImagesOutputFormat;
54
+ /**
55
+ * Selects the model that authors the SVG. Needs `outputFormat: "svg"`; the API requires `svg.model`
56
+ * when SVG model selection is enabled for the account and rejects the field otherwise.
57
+ */
58
+ svg?: OpenAIImagesSvgOptions;
59
+ /** 0-100, for `jpeg` and `webp` output only. */
60
+ outputCompression?: number;
61
+ /** GIF frames, an integer within {@link OPENAI_IMAGES_LIMITS}. The API defaults to 9. Needs `outputFormat: "gif"`. */
62
+ frameCount?: number;
63
+ /** Milliseconds per GIF frame, an integer within {@link OPENAI_IMAGES_LIMITS}; the API rounds to 10 ms and defaults to 200. Needs `outputFormat: "gif"`. */
64
+ frameDurationMs?: number;
65
+ /** How much the model plans before rendering. GPT Image 2.6 only; the API defaults to `high`. */
66
+ complexity?: "low" | "medium" | "high";
67
+ moderation?: "auto" | "low";
68
+ /** A stable, privacy-preserving end-user identifier of at most 128 characters; sent as `safety_identifier`. */
69
+ safetyIdentifier?: string;
70
+ }
71
+ /** The fields of an Images API request this provider validates. Upload fields are opaque here. */
72
+ export interface OpenAIImagesWireParams {
73
+ model: string;
74
+ prompt: string;
75
+ size?: string;
76
+ quality?: string;
77
+ n?: number;
78
+ background?: string;
79
+ output_format?: string;
80
+ output_compression?: number;
81
+ frame_count?: number;
82
+ frame_duration_ms?: number;
83
+ complexity?: string;
84
+ moderation?: string;
85
+ safety_identifier?: string;
86
+ svg?: OpenAIImagesSvgOptions;
87
+ stream?: boolean;
88
+ partial_images?: number;
89
+ image?: ImageEditParamsNonStreaming["image"];
90
+ markup?: object;
91
+ mask?: object;
92
+ }
93
+ /**
94
+ * Check a request against the Images API rules before it is sent, and throw an
95
+ * {@link ImageGenerationConfigError} naming the first offending option. Runs on the request as it
96
+ * will be sent, so it also covers a request an `onPayload` hook replaced.
97
+ */
98
+ export declare function validateOpenAIImagesRequest(model: ImageModel<ImageApi>, params: OpenAIImagesWireParams): void;
99
+ export interface PartitionedInputImages {
100
+ /** Images without a role in order: the first is the base image and the rest are references. */
101
+ images: ImagesInputImage[];
102
+ markup?: ImagesInputImage;
103
+ mask?: ImagesInputImage;
104
+ }
105
+ /** Split the image blocks of a request by role, rejecting roles the API cannot express. */
106
+ export declare function partitionInputImages(model: ImageModel<ImageApi>, input: readonly ImagesInputContent[]): PartitionedInputImages;
107
+ //# sourceMappingURL=openai-images-options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-images-options.d.ts","sourceRoot":"","sources":["../../src/api/openai-images-options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,yBAAyB,CAAC;AAE3E,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE7G;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB;aAChC,aAAa;iBAAI,GAAG,EAAE,CAAC;iBAAE,GAAG,EAAE,EAAE;;aAChC,kBAAkB;iBAAI,GAAG,EAAE,EAAE;iBAAE,GAAG,EAAE,IAAI;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AsB,CA+BxB"}
@@ -0,0 +1,106 @@
1
+ import { ImageGenerationConfigError } from "../image-generation-error.js";
2
+ /**
3
+ * Bounds the OpenAI Images API accepts for GPT Image 2.6 request fields. Requests outside them fail
4
+ * before anything is sent. The image generation guide documents 2-64 GIF frames and the API
5
+ * reference documents 2-16. A live request with 17 frames was rejected ("Use an integer from 2 to
6
+ * 16", #4332), so the API reference is right and the maximum is 16.
7
+ */
8
+ export const OPENAI_IMAGES_LIMITS = {
9
+ gifFrameCount: { min: 2, max: 16 },
10
+ gifFrameDurationMs: { min: 20, max: 2000 },
11
+ outputCompression: { min: 0, max: 100 },
12
+ safetyIdentifierMaxLength: 128,
13
+ };
14
+ function fail(model, field, reason, detail, hint, related) {
15
+ throw new ImageGenerationConfigError({ model: model.id, field, reason, related, detail, hint });
16
+ }
17
+ function checkIntegerRange(model, field, value, range) {
18
+ if (value === undefined)
19
+ return;
20
+ if (Number.isInteger(value) && value >= range.min && value <= range.max)
21
+ return;
22
+ fail(model, field, "out-of-range", `${field} must be an integer from ${range.min} to ${range.max}`, `Set ${field} to an integer from ${range.min} to ${range.max}.`);
23
+ }
24
+ /**
25
+ * Check a request against the Images API rules before it is sent, and throw an
26
+ * {@link ImageGenerationConfigError} naming the first offending option. Runs on the request as it
27
+ * will be sent, so it also covers a request an `onPayload` hook replaced.
28
+ */
29
+ export function validateOpenAIImagesRequest(model, params) {
30
+ const format = params.output_format;
31
+ const isEdit = params.image !== undefined;
32
+ const isGif = format === "gif";
33
+ const isSvg = format === "svg";
34
+ checkIntegerRange(model, "frameCount", params.frame_count, OPENAI_IMAGES_LIMITS.gifFrameCount);
35
+ checkIntegerRange(model, "frameDurationMs", params.frame_duration_ms, OPENAI_IMAGES_LIMITS.gifFrameDurationMs);
36
+ checkIntegerRange(model, "outputCompression", params.output_compression, OPENAI_IMAGES_LIMITS.outputCompression);
37
+ if (params.safety_identifier !== undefined &&
38
+ params.safety_identifier.length > OPENAI_IMAGES_LIMITS.safetyIdentifierMaxLength) {
39
+ fail(model, "safetyIdentifier", "out-of-range", `safetyIdentifier must be at most ${OPENAI_IMAGES_LIMITS.safetyIdentifierMaxLength} characters`, "Hash the user name or email address and send the digest.");
40
+ }
41
+ for (const [field, value] of [
42
+ ["frameCount", params.frame_count],
43
+ ["frameDurationMs", params.frame_duration_ms],
44
+ ]) {
45
+ if (value !== undefined && !isGif) {
46
+ fail(model, field, "requires-option", `${field} applies to animated GIF output only`, 'Set outputFormat to "gif" or remove the option.', "outputFormat");
47
+ }
48
+ }
49
+ if (params.svg !== undefined && !isSvg) {
50
+ fail(model, "svg", "requires-option", "svg applies to SVG output only", 'Set outputFormat to "svg" or remove the option.', "outputFormat");
51
+ }
52
+ if ((isGif || isSvg) && isEdit) {
53
+ fail(model, "outputFormat", "generation-only", `${format} output is available on generations only, not on edits`, "Remove the input images, or choose png, jpeg or webp.", "image");
54
+ }
55
+ if (isSvg && params.n !== undefined && params.n !== 1) {
56
+ fail(model, "n", "conflicts-with-option", "svg output requires n to be 1", "Set n to 1 or remove it.", "outputFormat");
57
+ }
58
+ if ((isGif || isSvg) && (params.stream === true || params.partial_images !== undefined)) {
59
+ const field = params.stream === true ? "stream" : "partialImages";
60
+ fail(model, field, "conflicts-with-option", `${format} output does not support streaming or partial images`, `Remove ${field} from the request.`, "outputFormat");
61
+ }
62
+ if (params.background === "transparent" && format !== undefined && format !== "png" && format !== "webp") {
63
+ fail(model, "background", "conflicts-with-option", "transparent backgrounds require png or webp output", 'Set outputFormat to "png" or "webp", or remove the transparent background.', "outputFormat");
64
+ }
65
+ if (params.output_compression !== undefined && format !== "jpeg" && format !== "webp") {
66
+ fail(model, "outputCompression", "conflicts-with-option", "outputCompression applies to jpeg and webp output only", 'Set outputFormat to "jpeg" or "webp", or remove outputCompression.', "outputFormat");
67
+ }
68
+ const hasBaseImage = Array.isArray(params.image) ? params.image.length > 0 : params.image != null;
69
+ for (const role of ["markup", "mask"]) {
70
+ if (params[role] !== undefined && !hasBaseImage) {
71
+ fail(model, "input", "requires-base-image", `an image with role "${role}" needs a base image to apply to`, "Add an image without a role before it; the first image is the base.", role);
72
+ }
73
+ }
74
+ if (params.markup !== undefined && params.mask !== undefined) {
75
+ fail(model, "markup", "conflicts-with-option", "a markup image cannot be combined with a mask image", 'Remove the image with role "markup" or the one with role "mask".', "mask");
76
+ }
77
+ const requireFeature = (field, used, feature) => {
78
+ if (!used || model.capabilities?.[feature] === true)
79
+ return;
80
+ fail(model, field, "unsupported-by-model", `${model.id} does not support ${field}`, "Use a GPT Image 2.6 model (gpt-image-2.6-sunburst or gpt-image-2.6-flare).");
81
+ };
82
+ requireFeature("complexity", params.complexity !== undefined, "complexity");
83
+ requireFeature("outputFormat", isGif, "gif");
84
+ requireFeature("outputFormat", isSvg, "svg");
85
+ requireFeature("markup", params.markup !== undefined, "markup");
86
+ }
87
+ /** Split the image blocks of a request by role, rejecting roles the API cannot express. */
88
+ export function partitionInputImages(model, input) {
89
+ const blocks = input.filter((item) => item.type === "image");
90
+ const images = blocks.filter((block) => block.role === undefined);
91
+ const markups = blocks.filter((block) => block.role === "markup");
92
+ const masks = blocks.filter((block) => block.role === "mask");
93
+ for (const [role, found] of [
94
+ ["markup", markups],
95
+ ["mask", masks],
96
+ ]) {
97
+ if (found.length > 1) {
98
+ fail(model, "input", "duplicate-role", `at most one image can have role "${role}"`, `Keep one image with role "${role}".`, role);
99
+ }
100
+ if (found.length === 1 && images.length === 0) {
101
+ fail(model, "input", "requires-base-image", `an image with role "${role}" needs a base image to apply to`, "Add an image without a role before it; the first image is the base.", role);
102
+ }
103
+ }
104
+ return { images, markup: markups[0], mask: masks[0] };
105
+ }
106
+ //# sourceMappingURL=openai-images-options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"openai-images-options.js","sourceRoot":"","sources":["../../src/api/openai-images-options.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0BAA0B,EAAoC,MAAM,8BAA8B,CAAC;AAG5G;;;;;GAKG;AACH,MAAM,CAAC,MAAM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C","sourcesContent":["import type { ImageEditParamsNonStreaming } from \"openai/resources/images\";\nimport { ImageGenerationConfigError, type ImageGenerationConfigReason } from \"../image-generation-error.ts\";\nimport type { ImageApi, ImageModel, ImagesInputContent, ImagesInputImage, ImagesOptions } from \"../types.ts\";\n\n/**\n * Bounds the OpenAI Images API accepts for GPT Image 2.6 request fields. Requests outside them fail\n * before anything is sent. The image generation guide documents 2-64 GIF frames and the API\n * reference documents 2-16. A live request with 17 frames was rejected (\"Use an integer from 2 to\n * 16\", #4332), so the API reference is right and the maximum is 16.\n */\nexport const OPENAI_IMAGES_LIMITS = {\n\tgifFrameCount: { min: 2, max: 16 },\n\tgifFrameDurationMs: { min: 20, max: 2000 },\n\toutputCompression: { min: 0, max: 100 },\n\tsafetyIdentifierMaxLength: 128,\n} as const;\n\nexport type OpenAIImagesOutputFormat = \"png\" | \"jpeg\" | \"webp\" | \"svg\" | \"gif\";\n\n/** The model that writes the SVG for `outputFormat: \"svg\"`, and its reasoning effort. */\nexport interface OpenAIImagesSvgOptions {\n\t/** Required by the API when SVG model selection is enabled for the account. */\n\tmodel: string;\n\treasoning?: { effort?: \"none\" | \"minimal\" | \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\" };\n}\n\n/**\n * Request options for OpenAI's Images API. Each field is sent only when set.\n *\n * Image blocks in the context go to `/images/edits`: the first image without a `role` is the base\n * image and every other image without a `role` is a reference. An image block with `role: \"markup\"`\n * is sent as the `markup` field and one with `role: \"mask\"` as the `mask` field; both apply to the\n * first image, and they cannot be combined.\n */\nexport interface OpenAIImagesOptions extends ImagesOptions {\n\t/** `auto`, a listed size, or a custom `WIDTHxHEIGHT` where the model supports one. */\n\tsize?: \"auto\" | \"1024x1024\" | \"1536x1024\" | \"1024x1536\" | (string & {});\n\tquality?: \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\" | \"auto\";\n\t/** Number of images to generate. SVG output requires 1. */\n\tn?: number;\n\t/** `transparent` needs a PNG or WebP output format. */\n\tbackground?: \"transparent\" | \"opaque\" | \"auto\";\n\t/**\n\t * `svg` and `gif` are generation-only and need a model that supports them (GPT Image 2.6).\n\t * `svg` returns UTF-8 XML as `image/svg+xml`; `gif` returns an animation as `image/gif`.\n\t */\n\toutputFormat?: OpenAIImagesOutputFormat;\n\t/**\n\t * Selects the model that authors the SVG. Needs `outputFormat: \"svg\"`; the API requires `svg.model`\n\t * when SVG model selection is enabled for the account and rejects the field otherwise.\n\t */\n\tsvg?: OpenAIImagesSvgOptions;\n\t/** 0-100, for `jpeg` and `webp` output only. */\n\toutputCompression?: number;\n\t/** GIF frames, an integer within {@link OPENAI_IMAGES_LIMITS}. The API defaults to 9. Needs `outputFormat: \"gif\"`. */\n\tframeCount?: number;\n\t/** Milliseconds per GIF frame, an integer within {@link OPENAI_IMAGES_LIMITS}; the API rounds to 10 ms and defaults to 200. Needs `outputFormat: \"gif\"`. */\n\tframeDurationMs?: number;\n\t/** How much the model plans before rendering. GPT Image 2.6 only; the API defaults to `high`. */\n\tcomplexity?: \"low\" | \"medium\" | \"high\";\n\tmoderation?: \"auto\" | \"low\";\n\t/** A stable, privacy-preserving end-user identifier of at most 128 characters; sent as `safety_identifier`. */\n\tsafetyIdentifier?: string;\n}\n\n/** The fields of an Images API request this provider validates. Upload fields are opaque here. */\nexport interface OpenAIImagesWireParams {\n\tmodel: string;\n\tprompt: string;\n\tsize?: string;\n\tquality?: string;\n\tn?: number;\n\tbackground?: string;\n\toutput_format?: string;\n\toutput_compression?: number;\n\tframe_count?: number;\n\tframe_duration_ms?: number;\n\tcomplexity?: string;\n\tmoderation?: string;\n\tsafety_identifier?: string;\n\tsvg?: OpenAIImagesSvgOptions;\n\tstream?: boolean;\n\tpartial_images?: number;\n\timage?: ImageEditParamsNonStreaming[\"image\"];\n\tmarkup?: object;\n\tmask?: object;\n}\n\nfunction fail(\n\tmodel: ImageModel<ImageApi>,\n\tfield: string,\n\treason: ImageGenerationConfigReason,\n\tdetail: string,\n\thint: string,\n\trelated?: string,\n): never {\n\tthrow new ImageGenerationConfigError({ model: model.id, field, reason, related, detail, hint });\n}\n\nfunction checkIntegerRange(\n\tmodel: ImageModel<ImageApi>,\n\tfield: string,\n\tvalue: number | undefined,\n\trange: { min: number; max: number },\n): void {\n\tif (value === undefined) return;\n\tif (Number.isInteger(value) && value >= range.min && value <= range.max) return;\n\tfail(\n\t\tmodel,\n\t\tfield,\n\t\t\"out-of-range\",\n\t\t`${field} must be an integer from ${range.min} to ${range.max}`,\n\t\t`Set ${field} to an integer from ${range.min} to ${range.max}.`,\n\t);\n}\n\n/**\n * Check a request against the Images API rules before it is sent, and throw an\n * {@link ImageGenerationConfigError} naming the first offending option. Runs on the request as it\n * will be sent, so it also covers a request an `onPayload` hook replaced.\n */\nexport function validateOpenAIImagesRequest(model: ImageModel<ImageApi>, params: OpenAIImagesWireParams): void {\n\tconst format = params.output_format;\n\tconst isEdit = params.image !== undefined;\n\tconst isGif = format === \"gif\";\n\tconst isSvg = format === \"svg\";\n\n\tcheckIntegerRange(model, \"frameCount\", params.frame_count, OPENAI_IMAGES_LIMITS.gifFrameCount);\n\tcheckIntegerRange(model, \"frameDurationMs\", params.frame_duration_ms, OPENAI_IMAGES_LIMITS.gifFrameDurationMs);\n\tcheckIntegerRange(model, \"outputCompression\", params.output_compression, OPENAI_IMAGES_LIMITS.outputCompression);\n\tif (\n\t\tparams.safety_identifier !== undefined &&\n\t\tparams.safety_identifier.length > OPENAI_IMAGES_LIMITS.safetyIdentifierMaxLength\n\t) {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"safetyIdentifier\",\n\t\t\t\"out-of-range\",\n\t\t\t`safetyIdentifier must be at most ${OPENAI_IMAGES_LIMITS.safetyIdentifierMaxLength} characters`,\n\t\t\t\"Hash the user name or email address and send the digest.\",\n\t\t);\n\t}\n\n\tfor (const [field, value] of [\n\t\t[\"frameCount\", params.frame_count],\n\t\t[\"frameDurationMs\", params.frame_duration_ms],\n\t] as const) {\n\t\tif (value !== undefined && !isGif) {\n\t\t\tfail(\n\t\t\t\tmodel,\n\t\t\t\tfield,\n\t\t\t\t\"requires-option\",\n\t\t\t\t`${field} applies to animated GIF output only`,\n\t\t\t\t'Set outputFormat to \"gif\" or remove the option.',\n\t\t\t\t\"outputFormat\",\n\t\t\t);\n\t\t}\n\t}\n\n\tif (params.svg !== undefined && !isSvg) {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"svg\",\n\t\t\t\"requires-option\",\n\t\t\t\"svg applies to SVG output only\",\n\t\t\t'Set outputFormat to \"svg\" or remove the option.',\n\t\t\t\"outputFormat\",\n\t\t);\n\t}\n\tif ((isGif || isSvg) && isEdit) {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"outputFormat\",\n\t\t\t\"generation-only\",\n\t\t\t`${format} output is available on generations only, not on edits`,\n\t\t\t\"Remove the input images, or choose png, jpeg or webp.\",\n\t\t\t\"image\",\n\t\t);\n\t}\n\tif (isSvg && params.n !== undefined && params.n !== 1) {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"n\",\n\t\t\t\"conflicts-with-option\",\n\t\t\t\"svg output requires n to be 1\",\n\t\t\t\"Set n to 1 or remove it.\",\n\t\t\t\"outputFormat\",\n\t\t);\n\t}\n\tif ((isGif || isSvg) && (params.stream === true || params.partial_images !== undefined)) {\n\t\tconst field = params.stream === true ? \"stream\" : \"partialImages\";\n\t\tfail(\n\t\t\tmodel,\n\t\t\tfield,\n\t\t\t\"conflicts-with-option\",\n\t\t\t`${format} output does not support streaming or partial images`,\n\t\t\t`Remove ${field} from the request.`,\n\t\t\t\"outputFormat\",\n\t\t);\n\t}\n\tif (params.background === \"transparent\" && format !== undefined && format !== \"png\" && format !== \"webp\") {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"background\",\n\t\t\t\"conflicts-with-option\",\n\t\t\t\"transparent backgrounds require png or webp output\",\n\t\t\t'Set outputFormat to \"png\" or \"webp\", or remove the transparent background.',\n\t\t\t\"outputFormat\",\n\t\t);\n\t}\n\tif (params.output_compression !== undefined && format !== \"jpeg\" && format !== \"webp\") {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"outputCompression\",\n\t\t\t\"conflicts-with-option\",\n\t\t\t\"outputCompression applies to jpeg and webp output only\",\n\t\t\t'Set outputFormat to \"jpeg\" or \"webp\", or remove outputCompression.',\n\t\t\t\"outputFormat\",\n\t\t);\n\t}\n\tconst hasBaseImage = Array.isArray(params.image) ? params.image.length > 0 : params.image != null;\n\tfor (const role of [\"markup\", \"mask\"] as const) {\n\t\tif (params[role] !== undefined && !hasBaseImage) {\n\t\t\tfail(\n\t\t\t\tmodel,\n\t\t\t\t\"input\",\n\t\t\t\t\"requires-base-image\",\n\t\t\t\t`an image with role \"${role}\" needs a base image to apply to`,\n\t\t\t\t\"Add an image without a role before it; the first image is the base.\",\n\t\t\t\trole,\n\t\t\t);\n\t\t}\n\t}\n\tif (params.markup !== undefined && params.mask !== undefined) {\n\t\tfail(\n\t\t\tmodel,\n\t\t\t\"markup\",\n\t\t\t\"conflicts-with-option\",\n\t\t\t\"a markup image cannot be combined with a mask image\",\n\t\t\t'Remove the image with role \"markup\" or the one with role \"mask\".',\n\t\t\t\"mask\",\n\t\t);\n\t}\n\n\tconst requireFeature = (\n\t\tfield: string,\n\t\tused: boolean,\n\t\tfeature: keyof NonNullable<ImageModel<ImageApi>[\"capabilities\"]>,\n\t): void => {\n\t\tif (!used || model.capabilities?.[feature] === true) return;\n\t\tfail(\n\t\t\tmodel,\n\t\t\tfield,\n\t\t\t\"unsupported-by-model\",\n\t\t\t`${model.id} does not support ${field}`,\n\t\t\t\"Use a GPT Image 2.6 model (gpt-image-2.6-sunburst or gpt-image-2.6-flare).\",\n\t\t);\n\t};\n\trequireFeature(\"complexity\", params.complexity !== undefined, \"complexity\");\n\trequireFeature(\"outputFormat\", isGif, \"gif\");\n\trequireFeature(\"outputFormat\", isSvg, \"svg\");\n\trequireFeature(\"markup\", params.markup !== undefined, \"markup\");\n}\n\nexport interface PartitionedInputImages {\n\t/** Images without a role in order: the first is the base image and the rest are references. */\n\timages: ImagesInputImage[];\n\tmarkup?: ImagesInputImage;\n\tmask?: ImagesInputImage;\n}\n\n/** Split the image blocks of a request by role, rejecting roles the API cannot express. */\nexport function partitionInputImages(\n\tmodel: ImageModel<ImageApi>,\n\tinput: readonly ImagesInputContent[],\n): PartitionedInputImages {\n\tconst blocks = input.filter((item): item is ImagesInputImage => item.type === \"image\");\n\tconst images = blocks.filter((block) => block.role === undefined);\n\tconst markups = blocks.filter((block) => block.role === \"markup\");\n\tconst masks = blocks.filter((block) => block.role === \"mask\");\n\tfor (const [role, found] of [\n\t\t[\"markup\", markups],\n\t\t[\"mask\", masks],\n\t] as const) {\n\t\tif (found.length > 1) {\n\t\t\tfail(\n\t\t\t\tmodel,\n\t\t\t\t\"input\",\n\t\t\t\t\"duplicate-role\",\n\t\t\t\t`at most one image can have role \"${role}\"`,\n\t\t\t\t`Keep one image with role \"${role}\".`,\n\t\t\t\trole,\n\t\t\t);\n\t\t}\n\t\tif (found.length === 1 && images.length === 0) {\n\t\t\tfail(\n\t\t\t\tmodel,\n\t\t\t\t\"input\",\n\t\t\t\t\"requires-base-image\",\n\t\t\t\t`an image with role \"${role}\" needs a base image to apply to`,\n\t\t\t\t\"Add an image without a role before it; the first image is the base.\",\n\t\t\t\trole,\n\t\t\t);\n\t\t}\n\t}\n\treturn { images, markup: markups[0], mask: masks[0] };\n}\n"]}
@@ -1,14 +1,6 @@
1
- import type { ImagesFunction, ImagesOptions } from "../types.ts";
2
- /** Request options for OpenAI's Images API. Each field is sent only when set. */
3
- export interface OpenAIImagesOptions extends ImagesOptions {
4
- /** `auto`, a listed size, or a custom `WIDTHxHEIGHT` where the model supports one. */
5
- size?: "auto" | "1024x1024" | "1536x1024" | "1024x1536" | (string & {});
6
- quality?: "low" | "medium" | "high" | "xhigh" | "max" | "auto";
7
- /** Number of images to generate. */
8
- n?: number;
9
- /** `transparent` needs a PNG or WebP output format. */
10
- background?: "transparent" | "opaque" | "auto";
11
- }
1
+ import type { ImagesFunction } from "../types.ts";
2
+ import { type OpenAIImagesOptions } from "./openai-images-options.ts";
3
+ export type { OpenAIImagesOptions, OpenAIImagesOutputFormat, OpenAIImagesSvgOptions } from "./openai-images-options.ts";
12
4
  /**
13
5
  * Image generation over OpenAI's Images API: `/images/generations` for text-only
14
6
  * input and `/images/edits` when the context includes image blocks.
@@ -1 +1 @@
1
- {"version":3,"file":"openai-images.d.ts","sourceRoot":"","sources":["../../src/api/openai-images.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAMX,cAAc,EACd,aAAa,EAGb,MAAM,aAAa,CAAC;AAMrB,iFAAiF;AACjF,MAAM,WAAW,mBAAoB,SAAQ,aAAa;IACzD,sFAAsF;IACtF,IAAI,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,WAAW,GAAG,WAAW,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;IACxE,OAAO,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,GAAG,MAAM,CAAC;IAC/D,oCAAoC;IACpC,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,uDAAuD;IACvD,UAAU,CAAC,EAAE,aAAa,GAAG,QAAQ,GAAG,MAAM,CAAC;CAC/C;AAUD;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,cAAc,CAAC,mBAAmB,CA0D9D,CAAC"}
1
+ {"version":3,"file":"openai-images.d.ts","sourceRoot":"","sources":["../../src/api/openai-images.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAMX,cAAc,EAGd,MAAM,aAAa,CAAC;AAKrB,OAAO,EACN,KAAK,mBAAmB,EAIxB,MAAM,4BAA4B,CAAC;AAEpC,YAAY,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AA4BxH;;;GAGG;AACH,eAAO,MAAM,cAAc,EAAE,cAAc,CAAC,mBAAmB,CAkE9D,CAAC"}
@@ -1,14 +1,34 @@
1
- import OpenAI, { toFile } from "openai";
1
+ import OpenAI, { APIConnectionError, APIConnectionTimeoutError, APIError, APIUserAbortError, toFile, } from "openai";
2
+ import { findImageGenerationError, ImageGenerationApiError, ImageGenerationQuotaError, ImageGenerationRateLimitError, ImageGenerationServerError, ImageGenerationTimeoutError, ImageGenerationUserError, ImageModerationBlockedError, } from "../image-generation-error.js";
2
3
  import { calculateCost } from "../models.js";
3
4
  import { formatProviderError, normalizeProviderError } from "../utils/error-body.js";
4
5
  import { headersToRecord, providerHeadersToRecord } from "../utils/headers.js";
5
6
  import { retryProviderRequest } from "../utils/provider-retry.js";
6
7
  import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
8
+ import { partitionInputImages, validateOpenAIImagesRequest, } from "./openai-images-options.js";
7
9
  const OUTPUT_FORMAT_MIME_TYPES = {
8
10
  png: "image/png",
9
11
  jpeg: "image/jpeg",
10
12
  webp: "image/webp",
13
+ gif: "image/gif",
14
+ svg: "image/svg+xml",
11
15
  };
16
+ const DEFAULT_REQUEST_TIMEOUT_MS = 600_000;
17
+ /** Request options and the API fields they are sent as. */
18
+ const OPTION_WIRE_FIELDS = [
19
+ ["size", "size"],
20
+ ["quality", "quality"],
21
+ ["n", "n"],
22
+ ["background", "background"],
23
+ ["outputFormat", "output_format"],
24
+ ["svg", "svg"],
25
+ ["outputCompression", "output_compression"],
26
+ ["frameCount", "frame_count"],
27
+ ["frameDurationMs", "frame_duration_ms"],
28
+ ["complexity", "complexity"],
29
+ ["moderation", "moderation"],
30
+ ["safetyIdentifier", "safety_identifier"],
31
+ ];
12
32
  /**
13
33
  * Image generation over OpenAI's Images API: `/images/generations` for text-only
14
34
  * input and `/images/edits` when the context includes image blocks.
@@ -22,28 +42,30 @@ export const generateImages = async (model, context, options) => {
22
42
  stopReason: "stop",
23
43
  timestamp: Date.now(),
24
44
  };
45
+ const startedAt = Date.now();
25
46
  try {
26
- const apiKey = options?.apiKey;
27
- if (!apiKey) {
47
+ if (!options?.apiKey) {
28
48
  throw new Error(`No API key for provider: ${model.provider}`);
29
49
  }
30
- const client = createClient(model, apiKey, options?.headers, options?.fetch);
50
+ const client = createClient(model, options.apiKey, options.headers, options.fetch);
31
51
  let params = await buildParams(model, context, options);
52
+ validateOpenAIImagesRequest(model, params);
32
53
  const nextParams = await options?.onPayload?.(params, model);
33
- if (nextParams !== undefined) {
54
+ if (nextParams !== undefined)
34
55
  params = nextParams;
35
- }
56
+ validateOpenAIImagesRequest(model, params);
36
57
  const requestOptions = {
37
58
  ...(options?.signal ? { signal: options.signal } : {}),
38
59
  ...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
39
60
  maxRetries: 0,
40
61
  };
41
- const { data: response, response: rawResponse } = await retryProviderRequest(() => "image" in params
62
+ const { data: response, response: rawResponse } = await retryProviderRequest(() => params.image !== undefined
42
63
  ? client.images.edit(params, requestOptions).withResponse()
43
64
  : client.images.generate(params, requestOptions).withResponse(), {
44
65
  maxRetries: options?.maxRetries,
45
66
  maxRetryDelayMs: options?.maxRetryDelayMs,
46
67
  signal: options?.signal,
68
+ noRetry: (error) => classifyImageApiError(error)?.retryable === false,
47
69
  });
48
70
  await options?.onResponse?.({ status: rawResponse.status, headers: headersToRecord(rawResponse.headers) }, model);
49
71
  if (response.usage) {
@@ -58,11 +80,88 @@ export const generateImages = async (model, context, options) => {
58
80
  return output;
59
81
  }
60
82
  catch (error) {
61
- output.stopReason = options?.signal?.aborted ? "aborted" : "error";
83
+ const aborted = options?.signal?.aborted === true;
84
+ output.stopReason = aborted ? "aborted" : "error";
62
85
  output.errorMessage = formatProviderError(normalizeProviderError(error));
86
+ if (!aborted) {
87
+ const typed = findImageGenerationError(error) ??
88
+ classifyImageApiError(error, { timeoutMs: options?.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS, startedAt });
89
+ if (typed)
90
+ output.error = typed.toData();
91
+ }
63
92
  return output;
64
93
  }
65
94
  };
95
+ /** Provider types, codes, request IDs and moderation categories are short identifiers. */
96
+ const SAFE_TOKEN = /^[A-Za-z0-9][A-Za-z0-9_./:-]{0,127}$/;
97
+ function safeToken(value) {
98
+ return typeof value === "string" && SAFE_TOKEN.test(value) ? value : undefined;
99
+ }
100
+ /**
101
+ * The provider's error message can quote the prompt, so typed errors carry only the HTTP status and
102
+ * identifier-shaped fields, and a cause rebuilt from them instead of the SDK error.
103
+ */
104
+ function safeSummary(status, apiType, apiCode, requestId) {
105
+ return [
106
+ status === undefined ? "no HTTP response" : `HTTP ${status}`,
107
+ ...(apiType ? [`type ${apiType}`] : []),
108
+ ...(apiCode ? [`code ${apiCode}`] : []),
109
+ ...(requestId ? [`request ${requestId}`] : []),
110
+ ].join(", ");
111
+ }
112
+ function safeCause(sdkError, summary) {
113
+ const cause = new Error(summary);
114
+ cause.name =
115
+ sdkError instanceof APIConnectionTimeoutError
116
+ ? "APIConnectionTimeoutError"
117
+ : sdkError instanceof APIConnectionError
118
+ ? "APIConnectionError"
119
+ : "APIError";
120
+ return cause;
121
+ }
122
+ /** Translate an OpenAI SDK failure, also when wrapped in a retry-delay error, into the typed family. */
123
+ function classifyImageApiError(error, timing) {
124
+ const cause = error instanceof Error ? error.cause : undefined;
125
+ const sdkError = [error, cause].find((candidate) => candidate instanceof APIError);
126
+ if (!sdkError || sdkError instanceof APIUserAbortError)
127
+ return undefined;
128
+ if (sdkError instanceof APIConnectionTimeoutError) {
129
+ const timeoutMs = timing?.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
130
+ return new ImageGenerationTimeoutError({
131
+ timeoutMs,
132
+ elapsedMs: timing ? Date.now() - timing.startedAt : timeoutMs,
133
+ cause: safeCause(sdkError, "no HTTP response, the request timed out"),
134
+ });
135
+ }
136
+ const status = sdkError.status;
137
+ const apiType = safeToken(sdkError.type);
138
+ const apiCode = safeToken(sdkError.code);
139
+ const requestId = safeToken(sdkError.requestID);
140
+ const detail = safeSummary(status, apiType, apiCode, requestId);
141
+ const init = { detail, status, apiCode, apiType, requestId, cause: safeCause(sdkError, detail) };
142
+ if (apiType === "image_generation_user_error") {
143
+ if (apiCode === "moderation_blocked") {
144
+ const details = sdkError.error.moderation_details;
145
+ const stage = details?.moderation_stage;
146
+ return new ImageModerationBlockedError({
147
+ ...init,
148
+ moderationStage: stage === "input" || stage === "output" || stage === "unknown" ? stage : undefined,
149
+ categories: Array.isArray(details?.categories)
150
+ ? details.categories.filter((category) => safeToken(category) !== undefined)
151
+ : [],
152
+ });
153
+ }
154
+ return new ImageGenerationUserError(init);
155
+ }
156
+ if (apiCode === "insufficient_quota" || apiType === "insufficient_quota") {
157
+ return new ImageGenerationQuotaError(init);
158
+ }
159
+ if (status === 429)
160
+ return new ImageGenerationRateLimitError(init);
161
+ if ((status ?? 0) >= 500)
162
+ return new ImageGenerationServerError(init);
163
+ return new ImageGenerationApiError({ ...init, retryable: status === undefined || status === 408 || status === 409 });
164
+ }
66
165
  function createClient(model, apiKey, optionsHeaders, fetch) {
67
166
  return new OpenAI({
68
167
  apiKey,
@@ -79,21 +178,23 @@ async function buildParams(model, context, options) {
79
178
  const params = {
80
179
  model: model.id,
81
180
  prompt,
82
- ...(options?.size !== undefined ? { size: options.size } : {}),
83
- ...(options?.quality !== undefined ? { quality: options.quality } : {}),
84
- ...(options?.n !== undefined ? { n: options.n } : {}),
85
- ...(options?.background !== undefined ? { background: options.background } : {}),
181
+ ...Object.fromEntries(OPTION_WIRE_FIELDS.flatMap(([option, field]) => options[option] === undefined ? [] : [[field, options[option]]])),
86
182
  };
87
- const images = context.input.filter((item) => item.type === "image");
183
+ const { images, markup, mask } = partitionInputImages(model, context.input);
88
184
  if (images.length === 0)
89
185
  return params;
90
186
  return {
91
187
  ...params,
92
- image: await Promise.all(images.map((image, index) => toFile(base64ToBytes(image.data), `image-${index + 1}.${image.mimeType.split("/")[1] ?? "png"}`, {
93
- type: image.mimeType,
94
- }))),
188
+ image: await Promise.all(images.map((image, index) => toUpload(image, `image-${index + 1}`))),
189
+ ...(markup ? { markup: await toUpload(markup, "markup") } : {}),
190
+ ...(mask ? { mask: await toUpload(mask, "mask") } : {}),
95
191
  };
96
192
  }
193
+ function toUpload(image, name) {
194
+ return toFile(base64ToBytes(image.data), `${name}.${image.mimeType.split("/")[1] ?? "png"}`, {
195
+ type: image.mimeType,
196
+ });
197
+ }
97
198
  function base64ToBytes(data) {
98
199
  return Uint8Array.from(atob(data), (char) => char.charCodeAt(0));
99
200
  }
@@ -1 +1 @@
1
- {"version":3,"file":"openai-images.js","sourceRoot":"","sources":["../../src/api/openai-images.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAY7C,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAelE,MAAM,wBAAwB,GAA2B;IACxD,GAAG,EAAE,WAAW;IAChB,IAAI,EAAE,YAAY;IAClB,IAAI,EAAE,YAAY;CAClB,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAwC,KAAK,EACvE,KAA2B,EAC3B,OAAsB,EACtB,OAA6B,EAC5B,EAAE;IACH,MAAM,MAAM,GAAoB;QAC/B,GAAG,EAAE,KAAK,CAAC,GAAG;QACd,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,KAAK,EAAE,KAAK,CAAC,EAAE;QACf,MAAM,EAAE,EAAE;QACV,UAAU,EAAE,MAAM;QAClB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;KACrB,CAAC;IAEF,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,OAAO,EAAE,MAAM,CAA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import OpenAI, { toFile } from \"openai\";\nimport type { ImageEditParamsNonStreaming, ImageGenerateParamsNonStreaming, ImagesResponse } from \"openai/resources\";\nimport { calculateCost } from \"../models.ts\";\nimport type {\n\tAssistantImages,\n\tImageApi,\n\tImageContent,\n\tImageModel,\n\tImagesContext,\n\tImagesFunction,\n\tImagesOptions,\n\tProviderHeaders,\n\tUsage,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { headersToRecord, providerHeadersToRecord } from \"../utils/headers.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\n\n/** Request options for OpenAI's Images API. Each field is sent only when set. */\nexport interface OpenAIImagesOptions extends ImagesOptions {\n\t/** `auto`, a listed size, or a custom `WIDTHxHEIGHT` where the model supports one. */\n\tsize?: \"auto\" | \"1024x1024\" | \"1536x1024\" | \"1024x1536\" | (string & {});\n\tquality?: \"low\" | \"medium\" | \"high\" | \"xhigh\" | \"max\" | \"auto\";\n\t/** Number of images to generate. */\n\tn?: number;\n\t/** `transparent` needs a PNG or WebP output format. */\n\tbackground?: \"transparent\" | \"opaque\" | \"auto\";\n}\n\ntype OpenAIImagesRequestParams = ImageGenerateParamsNonStreaming | ImageEditParamsNonStreaming;\n\nconst OUTPUT_FORMAT_MIME_TYPES: Record<string, string> = {\n\tpng: \"image/png\",\n\tjpeg: \"image/jpeg\",\n\twebp: \"image/webp\",\n};\n\n/**\n * Image generation over OpenAI's Images API: `/images/generations` for text-only\n * input and `/images/edits` when the context includes image blocks.\n */\nexport const generateImages: ImagesFunction<OpenAIImagesOptions> = async (\n\tmodel: ImageModel<ImageApi>,\n\tcontext: ImagesContext,\n\toptions?: OpenAIImagesOptions,\n) => {\n\tconst output: AssistantImages = {\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\toutput: [],\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n\n\ttry {\n\t\tconst apiKey = options?.apiKey;\n\t\tif (!apiKey) {\n\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t}\n\t\tconst client = createClient(model, apiKey, options?.headers, options?.fetch);\n\t\tlet params = await buildParams(model, context, options);\n\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\tif (nextParams !== undefined) {\n\t\t\tparams = nextParams as OpenAIImagesRequestParams;\n\t\t}\n\t\tconst requestOptions = {\n\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\tmaxRetries: 0,\n\t\t};\n\t\tconst { data: response, response: rawResponse } = await retryProviderRequest(\n\t\t\t() =>\n\t\t\t\t\"image\" in params\n\t\t\t\t\t? client.images.edit(params, requestOptions).withResponse()\n\t\t\t\t\t: client.images.generate(params, requestOptions).withResponse(),\n\t\t\t{\n\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\tsignal: options?.signal,\n\t\t\t},\n\t\t);\n\t\tawait options?.onResponse?.({ status: rawResponse.status, headers: headersToRecord(rawResponse.headers) }, model);\n\n\t\tif (response.usage) {\n\t\t\toutput.usage = parseUsage(response.usage, model);\n\t\t}\n\t\tconst mimeType = OUTPUT_FORMAT_MIME_TYPES[response.output_format ?? params.output_format ?? \"png\"] ?? \"image/png\";\n\t\tfor (const image of response.data ?? []) {\n\t\t\tif (!image.b64_json) continue;\n\t\t\toutput.output.push({ type: \"image\", mimeType, data: image.b64_json } satisfies ImageContent);\n\t\t}\n\n\t\treturn output;\n\t} catch (error) {\n\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\toutput.errorMessage = formatProviderError(normalizeProviderError(error));\n\t\treturn output;\n\t}\n};\n\nfunction createClient(\n\tmodel: ImageModel<ImageApi>,\n\tapiKey: string,\n\toptionsHeaders?: ProviderHeaders,\n\tfetch?: typeof globalThis.fetch,\n): OpenAI {\n\treturn new OpenAI({\n\t\tapiKey,\n\t\tbaseURL: model.baseUrl,\n\t\tdangerouslyAllowBrowser: true,\n\t\tfetch,\n\t\tdefaultHeaders: providerHeadersToRecord({ ...model.headers, ...optionsHeaders }),\n\t});\n}\n\nasync function buildParams(\n\tmodel: ImageModel<ImageApi>,\n\tcontext: ImagesContext,\n\toptions: OpenAIImagesOptions | undefined,\n): Promise<OpenAIImagesRequestParams> {\n\tconst prompt = context.input\n\t\t.flatMap((item) => (item.type === \"text\" ? [sanitizeSurrogates(item.text)] : []))\n\t\t.join(\"\\n\\n\");\n\tconst params = {\n\t\tmodel: model.id,\n\t\tprompt,\n\t\t...(options?.size !== undefined ? { size: options.size } : {}),\n\t\t...(options?.quality !== undefined ? { quality: options.quality } : {}),\n\t\t...(options?.n !== undefined ? { n: options.n } : {}),\n\t\t...(options?.background !== undefined ? { background: options.background } : {}),\n\t};\n\n\tconst images = context.input.filter((item): item is ImageContent => item.type === \"image\");\n\tif (images.length === 0) return params satisfies ImageGenerateParamsNonStreaming;\n\treturn {\n\t\t...params,\n\t\timage: await Promise.all(\n\t\t\timages.map((image, index) =>\n\t\t\t\ttoFile(base64ToBytes(image.data), `image-${index + 1}.${image.mimeType.split(\"/\")[1] ?? \"png\"}`, {\n\t\t\t\t\ttype: image.mimeType,\n\t\t\t\t}),\n\t\t\t),\n\t\t),\n\t} satisfies ImageEditParamsNonStreaming;\n}\n\nfunction base64ToBytes(data: string): Uint8Array<ArrayBuffer> {\n\treturn Uint8Array.from(atob(data), (char) => char.charCodeAt(0));\n}\n\nfunction parseUsage(rawUsage: ImagesResponse.Usage, model: ImageModel<ImageApi>): Usage {\n\tconst input = rawUsage.input_tokens || 0;\n\tconst output = rawUsage.output_tokens || 0;\n\tconst usage: Usage = {\n\t\tinput,\n\t\toutput,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\ttotalTokens: rawUsage.total_tokens || input + output,\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t};\n\tcalculateCost(model, usage);\n\treturn usage;\n}\n"]}
1
+ {"version":3,"file":"openai-images.js","sourceRoot":"","sources":["../../src/api/openai-images.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,EAAE,EACd,kBAAkB,EAClB,yBAAyB,EACzB,QAAQ,EACR,iBAAiB,EACjB,MAAM,GAEN,MAAM,QAAQ,CAAC;AAEhB,OAAO,EACN,wBAAwB,EACxB,uBAAuB,EAEvB,yBAAyB,EACzB,6BAA6B,EAC7B,0BAA0B,EAC1B,2BAA2B,EAC3B,wBAAwB,EACxB,2BAA2B,GAC3B,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAW7C,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAGN,oBAAoB,EACpB,2BAA2B,GAC3B,MAAM,4BAA4B,CAAC;AAIpC,MAAM,wBAAwB,GAA2B;IACxD,GAAG,EAAE,WAAW;IAChB,IAAI,EAAE,YAAY;IAClB,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,WAAW;IAChB,GAAG,EAAE,eAAe;CACpB,CAAC;AAEF,MAAM,0BAA0B,GAAG,OAAO,CAAC;AAE3C,2DAA2D;AAC3D,MAAM,kBAAkB,GAAG;IAC1B,CAAC,MAAM,EAAE,MAAM,CAAC;IAChB,CAAC,SAAS,EAAE,SAAS,CAAC;IACtB,CAAC,GAAG,EAAE,GAAG,CAAC;IACV,CAAC,YAAY,EAAE,YAAY,CAAC;IAC5B,CAAC,cAAc,EAAE,eAAe,CAAC;IACjC,CAAC,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IUserAbortError,\n\ttoFile,\n\ttype Uploadable,\n} from \"openai\";\nimport type { ImageEditParamsNonStreaming, ImageGenerateParamsNonStreaming, ImagesResponse } from \"openai/resources\";\nimport {\n\tfindImageGenerationError,\n\tImageGenerationApiError,\n\ttype ImageGenerationError,\n\tImageGenerationQuotaError,\n\tImageGenerationRateLimitError,\n\tImageGenerationServerError,\n\tImageGenerationTimeoutError,\n\tImageGenerationUserError,\n\tImageModerationBlockedError,\n} from \"../image-generation-error.ts\";\nimport { calculateCost } from \"../models.ts\";\nimport type {\n\tAssistantImages,\n\tImageApi,\n\tImageContent,\n\tImageModel,\n\tImagesContext,\n\tImagesFunction,\n\tProviderHeaders,\n\tUsage,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { headersToRecord, providerHeadersToRecord } from \"../utils/headers.ts\";\nimport { retryProviderRequest } from \"../utils/provider-retry.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\nimport {\n\ttype OpenAIImagesOptions,\n\ttype OpenAIImagesWireParams,\n\tpartitionInputImages,\n\tvalidateOpenAIImagesRequest,\n} from \"./openai-images-options.ts\";\n\nexport type { OpenAIImagesOptions, OpenAIImagesOutputFormat, OpenAIImagesSvgOptions } from \"./openai-images-options.ts\";\n\nconst OUTPUT_FORMAT_MIME_TYPES: Record<string, string> = {\n\tpng: \"image/png\",\n\tjpeg: \"image/jpeg\",\n\twebp: \"image/webp\",\n\tgif: \"image/gif\",\n\tsvg: \"image/svg+xml\",\n};\n\nconst DEFAULT_REQUEST_TIMEOUT_MS = 600_000;\n\n/** Request options and the API fields they are sent as. */\nconst OPTION_WIRE_FIELDS = [\n\t[\"size\", \"size\"],\n\t[\"quality\", \"quality\"],\n\t[\"n\", \"n\"],\n\t[\"background\", \"background\"],\n\t[\"outputFormat\", \"output_format\"],\n\t[\"svg\", \"svg\"],\n\t[\"outputCompression\", \"output_compression\"],\n\t[\"frameCount\", \"frame_count\"],\n\t[\"frameDurationMs\", \"frame_duration_ms\"],\n\t[\"complexity\", \"complexity\"],\n\t[\"moderation\", \"moderation\"],\n\t[\"safetyIdentifier\", \"safety_identifier\"],\n] as const satisfies readonly (readonly [keyof OpenAIImagesOptions, keyof OpenAIImagesWireParams])[];\n\n/**\n * Image generation over OpenAI's Images API: `/images/generations` for text-only\n * input and `/images/edits` when the context includes image blocks.\n */\nexport const generateImages: ImagesFunction<OpenAIImagesOptions> = async (\n\tmodel: ImageModel<ImageApi>,\n\tcontext: ImagesContext,\n\toptions?: OpenAIImagesOptions,\n) => {\n\tconst output: AssistantImages = {\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\toutput: [],\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n\tconst startedAt = Date.now();\n\n\ttry {\n\t\tif (!options?.apiKey) {\n\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t}\n\t\tconst client = createClient(model, options.apiKey, options.headers, options.fetch);\n\t\tlet params = await buildParams(model, context, options);\n\t\tvalidateOpenAIImagesRequest(model, params);\n\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\tif (nextParams !== undefined) params = nextParams as OpenAIImagesWireParams;\n\t\tvalidateOpenAIImagesRequest(model, params);\n\t\tconst requestOptions = {\n\t\t\t...(options?.signal ? { signal: options.signal } : {}),\n\t\t\t...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),\n\t\t\tmaxRetries: 0,\n\t\t};\n\t\tconst { data: response, response: rawResponse } = await retryProviderRequest(\n\t\t\t() =>\n\t\t\t\tparams.image !== undefined\n\t\t\t\t\t? client.images.edit(params as ImageEditParamsNonStreaming, requestOptions).withResponse()\n\t\t\t\t\t: client.images.generate(params as ImageGenerateParamsNonStreaming, requestOptions).withResponse(),\n\t\t\t{\n\t\t\t\tmaxRetries: options?.maxRetries,\n\t\t\t\tmaxRetryDelayMs: options?.maxRetryDelayMs,\n\t\t\t\tsignal: options?.signal,\n\t\t\t\tnoRetry: (error) => classifyImageApiError(error)?.retryable === false,\n\t\t\t},\n\t\t);\n\t\tawait options?.onResponse?.({ status: rawResponse.status, headers: headersToRecord(rawResponse.headers) }, model);\n\n\t\tif (response.usage) {\n\t\t\toutput.usage = parseUsage(response.usage, model);\n\t\t}\n\t\tconst mimeType = OUTPUT_FORMAT_MIME_TYPES[response.output_format ?? params.output_format ?? \"png\"] ?? \"image/png\";\n\t\tfor (const image of response.data ?? []) {\n\t\t\tif (!image.b64_json) continue;\n\t\t\toutput.output.push({ type: \"image\", mimeType, data: image.b64_json } satisfies ImageContent);\n\t\t}\n\n\t\treturn output;\n\t} catch (error) {\n\t\tconst aborted = options?.signal?.aborted === true;\n\t\toutput.stopReason = aborted ? \"aborted\" : \"error\";\n\t\toutput.errorMessage = formatProviderError(normalizeProviderError(error));\n\t\tif (!aborted) {\n\t\t\tconst typed =\n\t\t\t\tfindImageGenerationError(error) ??\n\t\t\t\tclassifyImageApiError(error, { timeoutMs: options?.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS, startedAt });\n\t\t\tif (typed) output.error = typed.toData();\n\t\t}\n\t\treturn output;\n\t}\n};\n\ninterface ImageErrorTiming {\n\ttimeoutMs: number;\n\tstartedAt: number;\n}\n\n/** Provider types, codes, request IDs and moderation categories are short identifiers. */\nconst SAFE_TOKEN = /^[A-Za-z0-9][A-Za-z0-9_./:-]{0,127}$/;\n\nfunction safeToken(value: unknown): string | undefined {\n\treturn typeof value === \"string\" && SAFE_TOKEN.test(value) ? value : undefined;\n}\n\n/**\n * The provider's error message can quote the prompt, so typed errors carry only the HTTP status and\n * identifier-shaped fields, and a cause rebuilt from them instead of the SDK error.\n */\nfunction safeSummary(status: number | undefined, apiType?: string, apiCode?: string, requestId?: string): string {\n\treturn [\n\t\tstatus === undefined ? \"no HTTP response\" : `HTTP ${status}`,\n\t\t...(apiType ? [`type ${apiType}`] : []),\n\t\t...(apiCode ? [`code ${apiCode}`] : []),\n\t\t...(requestId ? [`request ${requestId}`] : []),\n\t].join(\", \");\n}\n\nfunction safeCause(sdkError: APIError, summary: string): Error {\n\tconst cause = new Error(summary);\n\tcause.name =\n\t\tsdkError instanceof APIConnectionTimeoutError\n\t\t\t? \"APIConnectionTimeoutError\"\n\t\t\t: sdkError instanceof APIConnectionError\n\t\t\t\t? \"APIConnectionError\"\n\t\t\t\t: \"APIError\";\n\treturn cause;\n}\n\n/** Translate an OpenAI SDK failure, also when wrapped in a retry-delay error, into the typed family. */\nfunction classifyImageApiError(error: unknown, timing?: ImageErrorTiming): ImageGenerationError | undefined {\n\tconst cause = error instanceof Error ? error.cause : undefined;\n\tconst sdkError = [error, cause].find((candidate): candidate is APIError => candidate instanceof APIError);\n\tif (!sdkError || sdkError instanceof APIUserAbortError) return undefined;\n\tif (sdkError instanceof APIConnectionTimeoutError) {\n\t\tconst timeoutMs = timing?.timeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;\n\t\treturn new ImageGenerationTimeoutError({\n\t\t\ttimeoutMs,\n\t\t\telapsedMs: timing ? Date.now() - timing.startedAt : timeoutMs,\n\t\t\tcause: safeCause(sdkError, \"no HTTP response, the request timed out\"),\n\t\t});\n\t}\n\n\tconst status = sdkError.status;\n\tconst apiType = safeToken(sdkError.type);\n\tconst apiCode = safeToken(sdkError.code);\n\tconst requestId = safeToken(sdkError.requestID);\n\tconst detail = safeSummary(status, apiType, apiCode, requestId);\n\tconst init = { detail, status, apiCode, apiType, requestId, cause: safeCause(sdkError, detail) };\n\tif (apiType === \"image_generation_user_error\") {\n\t\tif (apiCode === \"moderation_blocked\") {\n\t\t\tconst details = (sdkError.error as ImageApiErrorBody).moderation_details;\n\t\t\tconst stage = details?.moderation_stage;\n\t\t\treturn new ImageModerationBlockedError({\n\t\t\t\t...init,\n\t\t\t\tmoderationStage: stage === \"input\" || stage === \"output\" || stage === \"unknown\" ? stage : undefined,\n\t\t\t\tcategories: Array.isArray(details?.categories)\n\t\t\t\t\t? details.categories.filter((category): category is string => safeToken(category) !== undefined)\n\t\t\t\t\t: [],\n\t\t\t});\n\t\t}\n\t\treturn new ImageGenerationUserError(init);\n\t}\n\tif (apiCode === \"insufficient_quota\" || apiType === \"insufficient_quota\") {\n\t\treturn new ImageGenerationQuotaError(init);\n\t}\n\tif (status === 429) return new ImageGenerationRateLimitError(init);\n\tif ((status ?? 0) >= 500) return new ImageGenerationServerError(init);\n\treturn new ImageGenerationApiError({ ...init, retryable: status === undefined || status === 408 || status === 409 });\n}\n\ninterface ImageApiErrorBody {\n\tmoderation_details?: { moderation_stage?: string; categories?: unknown[] };\n}\n\nfunction createClient(\n\tmodel: ImageModel<ImageApi>,\n\tapiKey: string,\n\toptionsHeaders?: ProviderHeaders,\n\tfetch?: typeof globalThis.fetch,\n): OpenAI {\n\treturn new OpenAI({\n\t\tapiKey,\n\t\tbaseURL: model.baseUrl,\n\t\tdangerouslyAllowBrowser: true,\n\t\tfetch,\n\t\tdefaultHeaders: providerHeadersToRecord({ ...model.headers, ...optionsHeaders }),\n\t});\n}\n\nasync function buildParams(\n\tmodel: ImageModel<ImageApi>,\n\tcontext: ImagesContext,\n\toptions: OpenAIImagesOptions,\n): Promise<OpenAIImagesWireParams> {\n\tconst prompt = context.input\n\t\t.flatMap((item) => (item.type === \"text\" ? [sanitizeSurrogates(item.text)] : []))\n\t\t.join(\"\\n\\n\");\n\tconst params: OpenAIImagesWireParams = {\n\t\tmodel: model.id,\n\t\tprompt,\n\t\t...Object.fromEntries(\n\t\t\tOPTION_WIRE_FIELDS.flatMap(([option, field]) =>\n\t\t\t\toptions[option] === undefined ? [] : [[field, options[option]]],\n\t\t\t),\n\t\t),\n\t};\n\n\tconst { images, markup, mask } = partitionInputImages(model, context.input);\n\tif (images.length === 0) return params;\n\treturn {\n\t\t...params,\n\t\timage: await Promise.all(images.map((image, index) => toUpload(image, `image-${index + 1}`))),\n\t\t...(markup ? { markup: await toUpload(markup, \"markup\") } : {}),\n\t\t...(mask ? { mask: await toUpload(mask, \"mask\") } : {}),\n\t};\n}\n\nfunction toUpload(image: ImageContent, name: string): Promise<Uploadable> {\n\treturn toFile(base64ToBytes(image.data), `${name}.${image.mimeType.split(\"/\")[1] ?? \"png\"}`, {\n\t\ttype: image.mimeType,\n\t});\n}\n\nfunction base64ToBytes(data: string): Uint8Array<ArrayBuffer> {\n\treturn Uint8Array.from(atob(data), (char) => char.charCodeAt(0));\n}\n\nfunction parseUsage(rawUsage: ImagesResponse.Usage, model: ImageModel<ImageApi>): Usage {\n\tconst input = rawUsage.input_tokens || 0;\n\tconst output = rawUsage.output_tokens || 0;\n\tconst usage: Usage = {\n\t\tinput,\n\t\toutput,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\ttotalTokens: rawUsage.total_tokens || input + output,\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t};\n\tcalculateCost(model, usage);\n\treturn usage;\n}\n"]}
@@ -881,7 +881,7 @@ export async function processResponsesStream(openaiStream, output, stream, model
881
881
  finalizeResponse(event.response);
882
882
  }
883
883
  else if (event.type === "error") {
884
- throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
884
+ throw responsesStreamError(`Error Code ${event.code}: ${event.message}`, event.code);
885
885
  }
886
886
  else if (event.type === "response.failed") {
887
887
  sawTerminalResponseEvent = true;
@@ -893,7 +893,7 @@ export async function processResponsesStream(openaiStream, output, stream, model
893
893
  : details?.reason
894
894
  ? `incomplete: ${details.reason}`
895
895
  : "Unknown error (no error details in response)";
896
- throw new Error(msg);
896
+ throw responsesStreamError(msg, error?.code);
897
897
  }
898
898
  }
899
899
  if (!sawTerminalResponseEvent) {
@@ -908,6 +908,10 @@ export async function processResponsesStream(openaiStream, output, stream, model
908
908
  }
909
909
  }
910
910
  }
911
+ /** A failure the stream reported in an event, keeping the provider's error code for typed classification. */
912
+ function responsesStreamError(message, code) {
913
+ return code ? Object.assign(new Error(message), { code }) : new Error(message);
914
+ }
911
915
  /** Stream state of a custom tool call that starts with `initialInput`. */
912
916
  function customInput(property, initialInput) {
913
917
  const buffer = { input: "", started: false, closed: false };