@ai-sdk/provider-utils 5.0.74 → 5.0.76

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.
@@ -7,6 +7,11 @@ import { DownloadError } from './download-error';
7
7
  import { extractResponseHeaders } from './extract-response-headers';
8
8
  import { handleFetchError } from './handle-fetch-error';
9
9
  import { isAbortError } from './is-abort-error';
10
+ import {
11
+ compileBinaryFieldPaths,
12
+ createJsonBinaryFieldScanner,
13
+ withBinaryFields,
14
+ } from './json-binary-field-scanner';
10
15
  import { parseJSON, safeParseJSON, type ParseResult } from './parse-json';
11
16
  import { parseJsonEventStream } from './parse-json-event-stream';
12
17
  import { readResponseWithSizeLimit } from './read-response-with-size-limit';
@@ -201,15 +206,58 @@ export const createEventSourceResponseHandler =
201
206
  };
202
207
  };
203
208
 
204
- export const createJsonResponseHandler =
205
- <T>(responseSchema: FlexibleSchema<T>): ResponseHandler<T> =>
206
- async ({ response, url, requestBodyValues }) => {
207
- const responseBody = await readResponseBodyAsText({ response, url });
209
+ export const createJsonResponseHandler = <T>(
210
+ responseSchema: FlexibleSchema<T>,
211
+ {
212
+ experimental_binaryFields,
213
+ }: {
214
+ /**
215
+ * Paths of string fields that carry large base64 data, e.g. `['images[*]']`
216
+ * or `['content[*].data.data']`. A path is a sequence of plain object keys
217
+ * joined by `.`, with `[*]` for every element of an array.
218
+ *
219
+ * By default the whole body is buffered, decoded to text, parsed, and the
220
+ * base64 string is decoded again later, which holds about four copies of
221
+ * the data at once. With this option the body is read as a stream and the
222
+ * strings at these paths are decoded to `Uint8Array` while they arrive.
223
+ *
224
+ * A string is only decoded if it is canonical base64 (standard alphabet,
225
+ * padded, no whitespace or escape sequences) and decodes to at least 64 KiB.
226
+ * Other strings, and strings at other paths, are parsed as usual, so the
227
+ * schema has to accept both a string and a `Uint8Array` at these paths.
228
+ *
229
+ * Differences to the default behavior:
230
+ * - Validation and the returned `value` contain the decoded `Uint8Array`.
231
+ * - `rawValue`, the `value` of a `TypeValidationError`, the text of a
232
+ * `JSONParseError` and `APICallError.responseBody` contain a short
233
+ * placeholder string such as `[binary data: 2097152 bytes; ref ...]` in
234
+ * place of the data.
235
+ * - `JSON.stringify` of a decoded `Uint8Array` gives the same placeholder
236
+ * as long as the validation can fail, so the message of an error that a
237
+ * validator builds from the value, or from its issues, stays short.
238
+ */
239
+ experimental_binaryFields?: readonly string[];
240
+ } = {},
241
+ ): ResponseHandler<T> => {
242
+ const binaryFieldPaths = experimental_binaryFields?.length
243
+ ? compileBinaryFieldPaths(experimental_binaryFields)
244
+ : undefined;
245
+
246
+ return async ({ response, url, requestBodyValues }) => {
247
+ let responseBody: string;
248
+ let schema = responseSchema;
249
+
250
+ if (binaryFieldPaths == null) {
251
+ responseBody = await readResponseBodyAsText({ response, url });
252
+ } else {
253
+ const scanner = createJsonBinaryFieldScanner(binaryFieldPaths);
254
+ await readResponseWithSizeLimit({ response, url, onChunk: scanner.push });
255
+ const scanned = scanner.finish();
256
+ responseBody = scanned.text;
257
+ schema = withBinaryFields(responseSchema, scanned.binaries);
258
+ }
208
259
 
209
- const parsedResult = await safeParseJSON({
210
- text: responseBody,
211
- schema: responseSchema,
212
- });
260
+ const parsedResult = await safeParseJSON({ text: responseBody, schema });
213
261
 
214
262
  const responseHeaders = extractResponseHeaders(response);
215
263
 
@@ -231,6 +279,7 @@ export const createJsonResponseHandler =
231
279
  rawValue: parsedResult.rawValue,
232
280
  };
233
281
  };
282
+ };
234
283
 
235
284
  export const createJsonLinesResponseHandler = <T>(
236
285
  responseSchema: FlexibleSchema<T>,