pi-openai-toolkit 0.21.1 → 0.22.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 (42) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +83 -83
  3. package/README.md +8 -0
  4. package/README.zh.md +8 -0
  5. package/config.schema.json +278 -278
  6. package/docs/configuration.md +298 -298
  7. package/docs/internals.md +181 -166
  8. package/package.json +5 -1
  9. package/src/codex-fast/extension.ts +119 -0
  10. package/src/codex-fast/payload.ts +36 -0
  11. package/src/codex-fast/state.ts +25 -0
  12. package/src/compact-client.ts +412 -412
  13. package/src/compaction-projection.ts +35 -35
  14. package/src/config/migration.ts +103 -103
  15. package/src/config.ts +310 -310
  16. package/src/context-management/codex-provider.ts +270 -270
  17. package/src/context-management/history-notes.ts +357 -357
  18. package/src/context-management/tools.ts +371 -371
  19. package/src/context-management/types.ts +168 -168
  20. package/src/context-management/window-manager.ts +801 -801
  21. package/src/debug.ts +238 -238
  22. package/src/deferred-tool-carryover.ts +282 -282
  23. package/src/details-store.ts +151 -151
  24. package/src/extension-runtime.ts +1662 -1662
  25. package/src/image-generation/extension.ts +189 -189
  26. package/src/image-generation/protocol.ts +579 -579
  27. package/src/image-generation/types.ts +300 -300
  28. package/src/payload-rewrite.ts +515 -515
  29. package/src/pi-context-hook.ts +117 -117
  30. package/src/remote-v2-client.ts +811 -811
  31. package/src/request-context-cache.ts +155 -155
  32. package/src/responses-headers.ts +180 -180
  33. package/src/runtime.ts +865 -865
  34. package/src/serializer.ts +882 -882
  35. package/src/supported-environment.ts +27 -27
  36. package/src/types.ts +565 -565
  37. package/src/web-search/alpha-client.ts +876 -876
  38. package/src/web-search/extension.ts +530 -530
  39. package/src/web-search/payload.ts +299 -299
  40. package/src/web-search/prompt.ts +60 -60
  41. package/src/web-search/types.ts +166 -166
  42. package/tsconfig.check.json +50 -50
@@ -1,300 +1,300 @@
1
- export const IMAGE_GENERATION_TOOL_NAME = "openai_generate_image";
2
- /** Image formats accepted from independent Images API responses. */
3
- export const IMAGE_GENERATION_MIME_TYPES = ["image/png", "image/jpeg", "image/webp"] as const;
4
- /** Backward-compatible default format for callers that construct PNG-only fixtures. */
5
- export const IMAGE_GENERATION_MIME_TYPE = "image/png" as const;
6
- export const IMAGE_GENERATION_CAPABLE_APIS = [
7
- "openai-responses",
8
- "openai-codex-responses",
9
- ] as const;
10
- export const IMAGE_GENERATION_SIZES = [
11
- "auto",
12
- "1024x1024",
13
- "1536x1024",
14
- "1024x1536",
15
- ] as const;
16
- export const IMAGE_GENERATION_QUALITIES = ["auto", "low", "medium", "high"] as const;
17
-
18
- export const MAX_IMAGE_PROMPT_CHARS = 20_000;
19
- export const MAX_REFERENCE_IMAGE_COUNT = 5;
20
- export const MAX_REFERENCE_IMAGE_BYTES = 20 * 1024 * 1024;
21
- export const MAX_TOTAL_REFERENCE_IMAGE_BYTES = 50 * 1024 * 1024;
22
- export const MAX_GENERATED_IMAGE_BYTES = 32 * 1024 * 1024;
23
- export const MAX_IMAGE_RESPONSE_BYTES = 48 * 1024 * 1024;
24
- export const MAX_IMAGE_ERROR_BYTES = 64 * 1024;
25
- export const IMAGE_REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
26
- export const MAX_IMAGE_PATH_CHARS = 4096;
27
- export const MAX_IMAGE_DIAGNOSTIC_CHARS = 4096;
28
- export const MAX_IMAGE_IDENTIFIER_CHARS = 256;
29
- /** Upper bound for a configured or requested Images API model id. */
30
- export const MAX_IMAGE_MODEL_ID_CHARS = 256;
31
- export const MAX_IMAGE_BATCH_SIZE = 10;
32
- export const MAX_IMAGE_DIMENSION = 100_000;
33
-
34
- export type ImageGenerationCapableApi = (typeof IMAGE_GENERATION_CAPABLE_APIS)[number];
35
- export type ImageGenerationSize = (typeof IMAGE_GENERATION_SIZES)[number];
36
- export type ImageGenerationQuality = (typeof IMAGE_GENERATION_QUALITIES)[number];
37
- export type ImageGenerationAction = "generate" | "edit";
38
- export type ImageGenerationMimeType = (typeof IMAGE_GENERATION_MIME_TYPES)[number];
39
- export type ReferenceImageMimeType = ImageGenerationMimeType;
40
-
41
- export function imageFileExtension(mimeType: ImageGenerationMimeType): "png" | "jpg" | "webp" {
42
- switch (mimeType) {
43
- case "image/png":
44
- return "png";
45
- case "image/jpeg":
46
- return "jpg";
47
- case "image/webp":
48
- return "webp";
49
- }
50
- }
51
-
52
- export type ImageGenerationModel = {
53
- api?: string;
54
- provider?: string;
55
- id?: string;
56
- };
57
-
58
- export type GenerateImageParams = {
59
- prompt: string;
60
- referenceImagePaths?: string[] | null;
61
- outputPath?: string | null;
62
- size?: ImageGenerationSize;
63
- quality?: ImageGenerationQuality;
64
- /** Optional bare image model id that must exist in the configured image model allowlist. */
65
- model?: string | null;
66
- /** Number of independent image requests to run concurrently. */
67
- batchSize?: number | null;
68
- };
69
-
70
- export type NormalizedGenerateImageParams = {
71
- prompt: string;
72
- referenceImagePaths: string[];
73
- outputPath?: string;
74
- size: ImageGenerationSize;
75
- quality: ImageGenerationQuality;
76
- action: ImageGenerationAction;
77
- batchSize: number;
78
- /** Trimmed requested model id, or undefined when the caller omitted it. */
79
- model?: string;
80
- };
81
-
82
- export type PreparedReferenceImage = {
83
- path: string;
84
- mimeType: ReferenceImageMimeType;
85
- bytes: Buffer;
86
- };
87
-
88
- export type ParsedGeneratedImage = {
89
- bytes: Buffer;
90
- mimeType: ImageGenerationMimeType;
91
- imageCallId: string;
92
- responseId?: string;
93
- revisedPrompt?: string;
94
- width: number;
95
- height: number;
96
- };
97
-
98
- export type ImageGenerationDetails = {
99
- artifactPath: string;
100
- outputPath?: string;
101
- routingModel: string;
102
- imageModel: string;
103
- imageCallId: string;
104
- responseId?: string;
105
- mimeType: ImageGenerationMimeType;
106
- byteCount: number;
107
- width: number;
108
- height: number;
109
- edited: boolean;
110
- referenceCount: number;
111
- revisedPrompt?: string;
112
- warning?: string;
113
- };
114
-
115
- export type ImageGenerationBatchFailure = {
116
- index: number;
117
- code: ImageGenerationFailureCode;
118
- message: string;
119
- };
120
-
121
- export type ImageGenerationBatchDetails = {
122
- batch: true;
123
- requestedCount: number;
124
- succeededCount: number;
125
- failedCount: number;
126
- images: ImageGenerationDetails[];
127
- failures?: ImageGenerationBatchFailure[];
128
- };
129
-
130
- export type ImageGenerationToolDetails = ImageGenerationDetails | ImageGenerationBatchDetails;
131
-
132
- export type ImageGenerationExecutionResult = {
133
- details: ImageGenerationToolDetails;
134
- text: string;
135
- };
136
-
137
- export type ImageGenerationFailureCode =
138
- | "aborted"
139
- | "timeout"
140
- | "disabled"
141
- | "unsupported-model"
142
- | "missing-runtime"
143
- | "invalid-parameters"
144
- | "reference-input-invalid"
145
- | "reference-upload-approval-required"
146
- | "reference-upload-declined"
147
- | "output-path-invalid"
148
- | "output-path-approval-required"
149
- | "output-path-declined"
150
- | "authentication"
151
- | "rate-limit"
152
- | "request-rejected"
153
- | "backend-unavailable"
154
- | "network"
155
- | "oversized-response"
156
- | "malformed-response"
157
- | "no-image"
158
- | "artifact-write-failed";
159
-
160
- function isImageGenerationFailureCode(value: unknown): value is ImageGenerationFailureCode {
161
- switch (value) {
162
- case "aborted":
163
- case "timeout":
164
- case "disabled":
165
- case "unsupported-model":
166
- case "missing-runtime":
167
- case "invalid-parameters":
168
- case "reference-input-invalid":
169
- case "reference-upload-approval-required":
170
- case "reference-upload-declined":
171
- case "output-path-invalid":
172
- case "output-path-approval-required":
173
- case "output-path-declined":
174
- case "authentication":
175
- case "rate-limit":
176
- case "request-rejected":
177
- case "backend-unavailable":
178
- case "network":
179
- case "oversized-response":
180
- case "malformed-response":
181
- case "no-image":
182
- case "artifact-write-failed":
183
- return true;
184
- default:
185
- return false;
186
- }
187
- }
188
-
189
- export class ImageGenerationError extends Error {
190
- readonly code: ImageGenerationFailureCode;
191
-
192
- constructor(code: ImageGenerationFailureCode, message: string) {
193
- super(message);
194
- this.name = "ImageGenerationError";
195
- this.code = code;
196
- }
197
- }
198
-
199
- function isRecord(value: unknown): value is Record<string, unknown> {
200
- return !!value && typeof value === "object" && !Array.isArray(value);
201
- }
202
-
203
- function isBoundedString(value: unknown, maxChars: number): value is string {
204
- return typeof value === "string" && value.trim().length > 0 && value.length <= maxChars;
205
- }
206
-
207
- function isImageGenerationMimeType(value: unknown): value is ImageGenerationMimeType {
208
- return (IMAGE_GENERATION_MIME_TYPES as readonly string[]).includes(value as string);
209
- }
210
-
211
- export function isImageGenerationDetails(value: unknown): value is ImageGenerationDetails {
212
- if (!isRecord(value)) return false;
213
- return (
214
- isBoundedString(value.artifactPath, MAX_IMAGE_PATH_CHARS) &&
215
- (value.outputPath === undefined || isBoundedString(value.outputPath, MAX_IMAGE_PATH_CHARS)) &&
216
- isBoundedString(value.routingModel, MAX_IMAGE_DIAGNOSTIC_CHARS) &&
217
- isBoundedString(value.imageModel, MAX_IMAGE_MODEL_ID_CHARS) &&
218
- isBoundedString(value.imageCallId, MAX_IMAGE_IDENTIFIER_CHARS) &&
219
- (value.responseId === undefined || isBoundedString(value.responseId, MAX_IMAGE_IDENTIFIER_CHARS)) &&
220
- isImageGenerationMimeType(value.mimeType) &&
221
- typeof value.byteCount === "number" &&
222
- Number.isInteger(value.byteCount) &&
223
- value.byteCount > 0 &&
224
- value.byteCount <= MAX_GENERATED_IMAGE_BYTES &&
225
- typeof value.width === "number" &&
226
- Number.isInteger(value.width) &&
227
- value.width > 0 &&
228
- value.width <= MAX_IMAGE_DIMENSION &&
229
- typeof value.height === "number" &&
230
- Number.isInteger(value.height) &&
231
- value.height > 0 &&
232
- value.height <= MAX_IMAGE_DIMENSION &&
233
- typeof value.edited === "boolean" &&
234
- typeof value.referenceCount === "number" &&
235
- Number.isInteger(value.referenceCount) &&
236
- value.referenceCount >= 0 &&
237
- value.referenceCount <= MAX_REFERENCE_IMAGE_COUNT &&
238
- (value.revisedPrompt === undefined || isBoundedString(value.revisedPrompt, MAX_IMAGE_DIAGNOSTIC_CHARS)) &&
239
- (value.warning === undefined || isBoundedString(value.warning, MAX_IMAGE_DIAGNOSTIC_CHARS))
240
- );
241
- }
242
-
243
- function isBoundedIndex(value: unknown, maximum: number): value is number {
244
- return typeof value === "number" && Number.isInteger(value) && value >= 0 && value < maximum;
245
- }
246
-
247
- export function isImageGenerationBatchDetails(value: unknown): value is ImageGenerationBatchDetails {
248
- if (!isRecord(value) || value.batch !== true) return false;
249
- if (
250
- typeof value.requestedCount !== "number" ||
251
- !Number.isInteger(value.requestedCount) ||
252
- value.requestedCount < 2 ||
253
- value.requestedCount > MAX_IMAGE_BATCH_SIZE ||
254
- typeof value.succeededCount !== "number" ||
255
- !Number.isInteger(value.succeededCount) ||
256
- value.succeededCount < 1 ||
257
- value.succeededCount > value.requestedCount ||
258
- typeof value.failedCount !== "number" ||
259
- !Number.isInteger(value.failedCount) ||
260
- value.failedCount !== value.requestedCount - value.succeededCount ||
261
- !Array.isArray(value.images) ||
262
- value.images.length !== value.succeededCount ||
263
- !value.images.every(isImageGenerationDetails)
264
- ) {
265
- return false;
266
- }
267
- if (value.failedCount === 0) return value.failures === undefined;
268
- if (!Array.isArray(value.failures) || value.failures.length !== value.failedCount) return false;
269
- const requestedCount = value.requestedCount;
270
- const indexes = new Set<number>();
271
- return value.failures.every((failure) => {
272
- if (!isRecord(failure) || !isBoundedIndex(failure.index, requestedCount)) return false;
273
- if (!isImageGenerationFailureCode(failure.code) || !isBoundedString(failure.message, MAX_IMAGE_DIAGNOSTIC_CHARS)) return false;
274
- if (indexes.has(failure.index)) return false;
275
- indexes.add(failure.index);
276
- return true;
277
- });
278
- }
279
-
280
- export function isImageGenerationToolDetails(value: unknown): value is ImageGenerationToolDetails {
281
- return isImageGenerationDetails(value) || isImageGenerationBatchDetails(value);
282
- }
283
-
284
- export function sanitizeImageDiagnostic(value: unknown, fallback: string): string {
285
- if (typeof value !== "string") return fallback;
286
- const normalized = value
287
- .replace(/data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/gi, "[REDACTED_IMAGE_DATA]")
288
- .replace(/[A-Za-z0-9+/]{128,}={0,2}/g, "[REDACTED_BASE64]")
289
- .replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [REDACTED]")
290
- .replace(
291
- /\b(authorization|api[-_ ]?key|access[-_ ]?token|refresh[-_ ]?token|account[-_ ]?id|cookie)\s*[:=]\s*[^\s,;]+/gi,
292
- "$1: [REDACTED]",
293
- )
294
- .replace(/\bsk-[A-Za-z0-9_-]{8,}\b/g, "[REDACTED]")
295
- .replace(/[\u0000-\u001f\u007f]+/g, " ")
296
- .replace(/\s+/g, " ")
297
- .trim();
298
- if (!normalized) return fallback;
299
- return normalized.slice(0, MAX_IMAGE_DIAGNOSTIC_CHARS);
300
- }
1
+ export const IMAGE_GENERATION_TOOL_NAME = "openai_generate_image";
2
+ /** Image formats accepted from independent Images API responses. */
3
+ export const IMAGE_GENERATION_MIME_TYPES = ["image/png", "image/jpeg", "image/webp"] as const;
4
+ /** Backward-compatible default format for callers that construct PNG-only fixtures. */
5
+ export const IMAGE_GENERATION_MIME_TYPE = "image/png" as const;
6
+ export const IMAGE_GENERATION_CAPABLE_APIS = [
7
+ "openai-responses",
8
+ "openai-codex-responses",
9
+ ] as const;
10
+ export const IMAGE_GENERATION_SIZES = [
11
+ "auto",
12
+ "1024x1024",
13
+ "1536x1024",
14
+ "1024x1536",
15
+ ] as const;
16
+ export const IMAGE_GENERATION_QUALITIES = ["auto", "low", "medium", "high"] as const;
17
+
18
+ export const MAX_IMAGE_PROMPT_CHARS = 20_000;
19
+ export const MAX_REFERENCE_IMAGE_COUNT = 5;
20
+ export const MAX_REFERENCE_IMAGE_BYTES = 20 * 1024 * 1024;
21
+ export const MAX_TOTAL_REFERENCE_IMAGE_BYTES = 50 * 1024 * 1024;
22
+ export const MAX_GENERATED_IMAGE_BYTES = 32 * 1024 * 1024;
23
+ export const MAX_IMAGE_RESPONSE_BYTES = 48 * 1024 * 1024;
24
+ export const MAX_IMAGE_ERROR_BYTES = 64 * 1024;
25
+ export const IMAGE_REQUEST_TIMEOUT_MS = 5 * 60 * 1000;
26
+ export const MAX_IMAGE_PATH_CHARS = 4096;
27
+ export const MAX_IMAGE_DIAGNOSTIC_CHARS = 4096;
28
+ export const MAX_IMAGE_IDENTIFIER_CHARS = 256;
29
+ /** Upper bound for a configured or requested Images API model id. */
30
+ export const MAX_IMAGE_MODEL_ID_CHARS = 256;
31
+ export const MAX_IMAGE_BATCH_SIZE = 10;
32
+ export const MAX_IMAGE_DIMENSION = 100_000;
33
+
34
+ export type ImageGenerationCapableApi = (typeof IMAGE_GENERATION_CAPABLE_APIS)[number];
35
+ export type ImageGenerationSize = (typeof IMAGE_GENERATION_SIZES)[number];
36
+ export type ImageGenerationQuality = (typeof IMAGE_GENERATION_QUALITIES)[number];
37
+ export type ImageGenerationAction = "generate" | "edit";
38
+ export type ImageGenerationMimeType = (typeof IMAGE_GENERATION_MIME_TYPES)[number];
39
+ export type ReferenceImageMimeType = ImageGenerationMimeType;
40
+
41
+ export function imageFileExtension(mimeType: ImageGenerationMimeType): "png" | "jpg" | "webp" {
42
+ switch (mimeType) {
43
+ case "image/png":
44
+ return "png";
45
+ case "image/jpeg":
46
+ return "jpg";
47
+ case "image/webp":
48
+ return "webp";
49
+ }
50
+ }
51
+
52
+ export type ImageGenerationModel = {
53
+ api?: string;
54
+ provider?: string;
55
+ id?: string;
56
+ };
57
+
58
+ export type GenerateImageParams = {
59
+ prompt: string;
60
+ referenceImagePaths?: string[] | null;
61
+ outputPath?: string | null;
62
+ size?: ImageGenerationSize;
63
+ quality?: ImageGenerationQuality;
64
+ /** Optional bare image model id that must exist in the configured image model allowlist. */
65
+ model?: string | null;
66
+ /** Number of independent image requests to run concurrently. */
67
+ batchSize?: number | null;
68
+ };
69
+
70
+ export type NormalizedGenerateImageParams = {
71
+ prompt: string;
72
+ referenceImagePaths: string[];
73
+ outputPath?: string;
74
+ size: ImageGenerationSize;
75
+ quality: ImageGenerationQuality;
76
+ action: ImageGenerationAction;
77
+ batchSize: number;
78
+ /** Trimmed requested model id, or undefined when the caller omitted it. */
79
+ model?: string;
80
+ };
81
+
82
+ export type PreparedReferenceImage = {
83
+ path: string;
84
+ mimeType: ReferenceImageMimeType;
85
+ bytes: Buffer;
86
+ };
87
+
88
+ export type ParsedGeneratedImage = {
89
+ bytes: Buffer;
90
+ mimeType: ImageGenerationMimeType;
91
+ imageCallId: string;
92
+ responseId?: string;
93
+ revisedPrompt?: string;
94
+ width: number;
95
+ height: number;
96
+ };
97
+
98
+ export type ImageGenerationDetails = {
99
+ artifactPath: string;
100
+ outputPath?: string;
101
+ routingModel: string;
102
+ imageModel: string;
103
+ imageCallId: string;
104
+ responseId?: string;
105
+ mimeType: ImageGenerationMimeType;
106
+ byteCount: number;
107
+ width: number;
108
+ height: number;
109
+ edited: boolean;
110
+ referenceCount: number;
111
+ revisedPrompt?: string;
112
+ warning?: string;
113
+ };
114
+
115
+ export type ImageGenerationBatchFailure = {
116
+ index: number;
117
+ code: ImageGenerationFailureCode;
118
+ message: string;
119
+ };
120
+
121
+ export type ImageGenerationBatchDetails = {
122
+ batch: true;
123
+ requestedCount: number;
124
+ succeededCount: number;
125
+ failedCount: number;
126
+ images: ImageGenerationDetails[];
127
+ failures?: ImageGenerationBatchFailure[];
128
+ };
129
+
130
+ export type ImageGenerationToolDetails = ImageGenerationDetails | ImageGenerationBatchDetails;
131
+
132
+ export type ImageGenerationExecutionResult = {
133
+ details: ImageGenerationToolDetails;
134
+ text: string;
135
+ };
136
+
137
+ export type ImageGenerationFailureCode =
138
+ | "aborted"
139
+ | "timeout"
140
+ | "disabled"
141
+ | "unsupported-model"
142
+ | "missing-runtime"
143
+ | "invalid-parameters"
144
+ | "reference-input-invalid"
145
+ | "reference-upload-approval-required"
146
+ | "reference-upload-declined"
147
+ | "output-path-invalid"
148
+ | "output-path-approval-required"
149
+ | "output-path-declined"
150
+ | "authentication"
151
+ | "rate-limit"
152
+ | "request-rejected"
153
+ | "backend-unavailable"
154
+ | "network"
155
+ | "oversized-response"
156
+ | "malformed-response"
157
+ | "no-image"
158
+ | "artifact-write-failed";
159
+
160
+ function isImageGenerationFailureCode(value: unknown): value is ImageGenerationFailureCode {
161
+ switch (value) {
162
+ case "aborted":
163
+ case "timeout":
164
+ case "disabled":
165
+ case "unsupported-model":
166
+ case "missing-runtime":
167
+ case "invalid-parameters":
168
+ case "reference-input-invalid":
169
+ case "reference-upload-approval-required":
170
+ case "reference-upload-declined":
171
+ case "output-path-invalid":
172
+ case "output-path-approval-required":
173
+ case "output-path-declined":
174
+ case "authentication":
175
+ case "rate-limit":
176
+ case "request-rejected":
177
+ case "backend-unavailable":
178
+ case "network":
179
+ case "oversized-response":
180
+ case "malformed-response":
181
+ case "no-image":
182
+ case "artifact-write-failed":
183
+ return true;
184
+ default:
185
+ return false;
186
+ }
187
+ }
188
+
189
+ export class ImageGenerationError extends Error {
190
+ readonly code: ImageGenerationFailureCode;
191
+
192
+ constructor(code: ImageGenerationFailureCode, message: string) {
193
+ super(message);
194
+ this.name = "ImageGenerationError";
195
+ this.code = code;
196
+ }
197
+ }
198
+
199
+ function isRecord(value: unknown): value is Record<string, unknown> {
200
+ return !!value && typeof value === "object" && !Array.isArray(value);
201
+ }
202
+
203
+ function isBoundedString(value: unknown, maxChars: number): value is string {
204
+ return typeof value === "string" && value.trim().length > 0 && value.length <= maxChars;
205
+ }
206
+
207
+ function isImageGenerationMimeType(value: unknown): value is ImageGenerationMimeType {
208
+ return (IMAGE_GENERATION_MIME_TYPES as readonly string[]).includes(value as string);
209
+ }
210
+
211
+ export function isImageGenerationDetails(value: unknown): value is ImageGenerationDetails {
212
+ if (!isRecord(value)) return false;
213
+ return (
214
+ isBoundedString(value.artifactPath, MAX_IMAGE_PATH_CHARS) &&
215
+ (value.outputPath === undefined || isBoundedString(value.outputPath, MAX_IMAGE_PATH_CHARS)) &&
216
+ isBoundedString(value.routingModel, MAX_IMAGE_DIAGNOSTIC_CHARS) &&
217
+ isBoundedString(value.imageModel, MAX_IMAGE_MODEL_ID_CHARS) &&
218
+ isBoundedString(value.imageCallId, MAX_IMAGE_IDENTIFIER_CHARS) &&
219
+ (value.responseId === undefined || isBoundedString(value.responseId, MAX_IMAGE_IDENTIFIER_CHARS)) &&
220
+ isImageGenerationMimeType(value.mimeType) &&
221
+ typeof value.byteCount === "number" &&
222
+ Number.isInteger(value.byteCount) &&
223
+ value.byteCount > 0 &&
224
+ value.byteCount <= MAX_GENERATED_IMAGE_BYTES &&
225
+ typeof value.width === "number" &&
226
+ Number.isInteger(value.width) &&
227
+ value.width > 0 &&
228
+ value.width <= MAX_IMAGE_DIMENSION &&
229
+ typeof value.height === "number" &&
230
+ Number.isInteger(value.height) &&
231
+ value.height > 0 &&
232
+ value.height <= MAX_IMAGE_DIMENSION &&
233
+ typeof value.edited === "boolean" &&
234
+ typeof value.referenceCount === "number" &&
235
+ Number.isInteger(value.referenceCount) &&
236
+ value.referenceCount >= 0 &&
237
+ value.referenceCount <= MAX_REFERENCE_IMAGE_COUNT &&
238
+ (value.revisedPrompt === undefined || isBoundedString(value.revisedPrompt, MAX_IMAGE_DIAGNOSTIC_CHARS)) &&
239
+ (value.warning === undefined || isBoundedString(value.warning, MAX_IMAGE_DIAGNOSTIC_CHARS))
240
+ );
241
+ }
242
+
243
+ function isBoundedIndex(value: unknown, maximum: number): value is number {
244
+ return typeof value === "number" && Number.isInteger(value) && value >= 0 && value < maximum;
245
+ }
246
+
247
+ export function isImageGenerationBatchDetails(value: unknown): value is ImageGenerationBatchDetails {
248
+ if (!isRecord(value) || value.batch !== true) return false;
249
+ if (
250
+ typeof value.requestedCount !== "number" ||
251
+ !Number.isInteger(value.requestedCount) ||
252
+ value.requestedCount < 2 ||
253
+ value.requestedCount > MAX_IMAGE_BATCH_SIZE ||
254
+ typeof value.succeededCount !== "number" ||
255
+ !Number.isInteger(value.succeededCount) ||
256
+ value.succeededCount < 1 ||
257
+ value.succeededCount > value.requestedCount ||
258
+ typeof value.failedCount !== "number" ||
259
+ !Number.isInteger(value.failedCount) ||
260
+ value.failedCount !== value.requestedCount - value.succeededCount ||
261
+ !Array.isArray(value.images) ||
262
+ value.images.length !== value.succeededCount ||
263
+ !value.images.every(isImageGenerationDetails)
264
+ ) {
265
+ return false;
266
+ }
267
+ if (value.failedCount === 0) return value.failures === undefined;
268
+ if (!Array.isArray(value.failures) || value.failures.length !== value.failedCount) return false;
269
+ const requestedCount = value.requestedCount;
270
+ const indexes = new Set<number>();
271
+ return value.failures.every((failure) => {
272
+ if (!isRecord(failure) || !isBoundedIndex(failure.index, requestedCount)) return false;
273
+ if (!isImageGenerationFailureCode(failure.code) || !isBoundedString(failure.message, MAX_IMAGE_DIAGNOSTIC_CHARS)) return false;
274
+ if (indexes.has(failure.index)) return false;
275
+ indexes.add(failure.index);
276
+ return true;
277
+ });
278
+ }
279
+
280
+ export function isImageGenerationToolDetails(value: unknown): value is ImageGenerationToolDetails {
281
+ return isImageGenerationDetails(value) || isImageGenerationBatchDetails(value);
282
+ }
283
+
284
+ export function sanitizeImageDiagnostic(value: unknown, fallback: string): string {
285
+ if (typeof value !== "string") return fallback;
286
+ const normalized = value
287
+ .replace(/data:image\/[a-z0-9.+-]+;base64,[A-Za-z0-9+/=]+/gi, "[REDACTED_IMAGE_DATA]")
288
+ .replace(/[A-Za-z0-9+/]{128,}={0,2}/g, "[REDACTED_BASE64]")
289
+ .replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [REDACTED]")
290
+ .replace(
291
+ /\b(authorization|api[-_ ]?key|access[-_ ]?token|refresh[-_ ]?token|account[-_ ]?id|cookie)\s*[:=]\s*[^\s,;]+/gi,
292
+ "$1: [REDACTED]",
293
+ )
294
+ .replace(/\bsk-[A-Za-z0-9_-]{8,}\b/g, "[REDACTED]")
295
+ .replace(/[\u0000-\u001f\u007f]+/g, " ")
296
+ .replace(/\s+/g, " ")
297
+ .trim();
298
+ if (!normalized) return fallback;
299
+ return normalized.slice(0, MAX_IMAGE_DIAGNOSTIC_CHARS);
300
+ }