@ai-sdk/provider-utils 5.0.0-beta.28 → 5.0.0-beta.29

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 (82) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/index.d.ts +967 -843
  3. package/dist/index.js +340 -4
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -2
  6. package/src/add-additional-properties-to-json-schema.ts +1 -1
  7. package/src/convert-image-model-file-to-data-uri.ts +1 -1
  8. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  9. package/src/create-tool-name-mapping.ts +1 -1
  10. package/src/detect-media-type.ts +312 -0
  11. package/src/get-from-api.ts +2 -2
  12. package/src/index.ts +8 -0
  13. package/src/inject-json-instruction.ts +1 -1
  14. package/src/is-buffer.ts +9 -0
  15. package/src/is-json-serializable.ts +1 -1
  16. package/src/is-provider-reference.ts +11 -8
  17. package/src/is-url-supported.ts +17 -2
  18. package/src/map-reasoning-to-provider.ts +4 -1
  19. package/src/parse-json-event-stream.ts +3 -3
  20. package/src/parse-json.ts +2 -2
  21. package/src/parse-provider-options.ts +1 -1
  22. package/src/post-to-api.ts +2 -2
  23. package/src/provider-defined-tool-factory.ts +10 -9
  24. package/src/provider-executed-tool-factory.ts +6 -7
  25. package/src/resolve-full-media-type.ts +49 -0
  26. package/src/resolve-provider-reference.ts +1 -2
  27. package/src/resolve.ts +1 -1
  28. package/src/response-handler.ts +2 -2
  29. package/src/schema.ts +6 -3
  30. package/src/serialize-model-options.ts +2 -2
  31. package/src/streaming-tool-call-tracker.ts +2 -2
  32. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  33. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  34. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  35. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  36. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  37. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  38. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  39. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +3 -3
  40. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  41. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  42. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  43. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +4 -4
  44. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  45. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  46. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  47. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  48. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  49. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  50. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  51. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  52. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
  53. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +7 -8
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  62. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  63. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  64. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  65. package/src/types/assistant-model-message.ts +3 -3
  66. package/src/types/content-part.ts +37 -14
  67. package/src/types/executable-tool.ts +1 -1
  68. package/src/types/execute-tool.ts +6 -5
  69. package/src/types/file-data.ts +48 -0
  70. package/src/types/index.ts +16 -3
  71. package/src/types/infer-tool-context.ts +1 -1
  72. package/src/types/model-message.ts +4 -4
  73. package/src/types/never-optional.ts +7 -0
  74. package/src/types/provider-options.ts +1 -1
  75. package/src/types/provider-reference.ts +1 -1
  76. package/src/types/system-model-message.ts +1 -1
  77. package/src/types/tool-execute-function.ts +50 -0
  78. package/src/types/tool-model-message.ts +3 -3
  79. package/src/types/tool-needs-approval-function.ts +39 -0
  80. package/src/types/tool.ts +200 -271
  81. package/src/types/user-model-message.ts +2 -2
  82. package/src/validate-types.ts +5 -3
package/dist/index.js CHANGED
@@ -70,6 +70,20 @@ function convertToBase64(value) {
70
70
  return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
71
71
  }
72
72
 
73
+ // src/convert-inline-file-data-to-uint8-array.ts
74
+ function convertInlineFileDataToUint8Array(data) {
75
+ if (data.type === "text") {
76
+ return new TextEncoder().encode(data.text);
77
+ }
78
+ if (data.data instanceof Uint8Array) {
79
+ return data.data;
80
+ }
81
+ if (data.data instanceof ArrayBuffer) {
82
+ return new Uint8Array(data.data);
83
+ }
84
+ return convertBase64ToUint8Array(data.data);
85
+ }
86
+
73
87
  // src/convert-image-model-file-to-data-uri.ts
74
88
  function convertImageModelFileToDataUri(file) {
75
89
  if (file.type === "url") return file.url;
@@ -203,6 +217,278 @@ var DelayedPromise = class {
203
217
  }
204
218
  };
205
219
 
220
+ // src/detect-media-type.ts
221
+ var imageMediaTypeSignatures = [
222
+ {
223
+ mediaType: "image/gif",
224
+ bytesPrefix: [71, 73, 70]
225
+ // GIF
226
+ },
227
+ {
228
+ mediaType: "image/png",
229
+ bytesPrefix: [137, 80, 78, 71]
230
+ // PNG
231
+ },
232
+ {
233
+ mediaType: "image/jpeg",
234
+ bytesPrefix: [255, 216]
235
+ // JPEG
236
+ },
237
+ {
238
+ mediaType: "image/webp",
239
+ bytesPrefix: [
240
+ 82,
241
+ 73,
242
+ 70,
243
+ 70,
244
+ // "RIFF"
245
+ null,
246
+ null,
247
+ null,
248
+ null,
249
+ // file size (variable)
250
+ 87,
251
+ 69,
252
+ 66,
253
+ 80
254
+ // "WEBP"
255
+ ]
256
+ },
257
+ {
258
+ mediaType: "image/bmp",
259
+ bytesPrefix: [66, 77]
260
+ },
261
+ {
262
+ mediaType: "image/tiff",
263
+ bytesPrefix: [73, 73, 42, 0]
264
+ },
265
+ {
266
+ mediaType: "image/tiff",
267
+ bytesPrefix: [77, 77, 0, 42]
268
+ },
269
+ {
270
+ mediaType: "image/avif",
271
+ bytesPrefix: [
272
+ 0,
273
+ 0,
274
+ 0,
275
+ 32,
276
+ 102,
277
+ 116,
278
+ 121,
279
+ 112,
280
+ 97,
281
+ 118,
282
+ 105,
283
+ 102
284
+ ]
285
+ },
286
+ {
287
+ mediaType: "image/heic",
288
+ bytesPrefix: [
289
+ 0,
290
+ 0,
291
+ 0,
292
+ 32,
293
+ 102,
294
+ 116,
295
+ 121,
296
+ 112,
297
+ 104,
298
+ 101,
299
+ 105,
300
+ 99
301
+ ]
302
+ }
303
+ ];
304
+ var documentMediaTypeSignatures = [
305
+ {
306
+ mediaType: "application/pdf",
307
+ bytesPrefix: [37, 80, 68, 70]
308
+ // %PDF
309
+ }
310
+ ];
311
+ var audioMediaTypeSignatures = [
312
+ {
313
+ mediaType: "audio/mpeg",
314
+ bytesPrefix: [255, 251]
315
+ },
316
+ {
317
+ mediaType: "audio/mpeg",
318
+ bytesPrefix: [255, 250]
319
+ },
320
+ {
321
+ mediaType: "audio/mpeg",
322
+ bytesPrefix: [255, 243]
323
+ },
324
+ {
325
+ mediaType: "audio/mpeg",
326
+ bytesPrefix: [255, 242]
327
+ },
328
+ {
329
+ mediaType: "audio/mpeg",
330
+ bytesPrefix: [255, 227]
331
+ },
332
+ {
333
+ mediaType: "audio/mpeg",
334
+ bytesPrefix: [255, 226]
335
+ },
336
+ {
337
+ mediaType: "audio/wav",
338
+ bytesPrefix: [
339
+ 82,
340
+ // R
341
+ 73,
342
+ // I
343
+ 70,
344
+ // F
345
+ 70,
346
+ // F
347
+ null,
348
+ null,
349
+ null,
350
+ null,
351
+ 87,
352
+ // W
353
+ 65,
354
+ // A
355
+ 86,
356
+ // V
357
+ 69
358
+ // E
359
+ ]
360
+ },
361
+ {
362
+ mediaType: "audio/ogg",
363
+ bytesPrefix: [79, 103, 103, 83]
364
+ },
365
+ {
366
+ mediaType: "audio/flac",
367
+ bytesPrefix: [102, 76, 97, 67]
368
+ },
369
+ {
370
+ mediaType: "audio/aac",
371
+ bytesPrefix: [64, 21, 0, 0]
372
+ },
373
+ {
374
+ mediaType: "audio/mp4",
375
+ bytesPrefix: [102, 116, 121, 112]
376
+ },
377
+ {
378
+ mediaType: "audio/webm",
379
+ bytesPrefix: [26, 69, 223, 163]
380
+ }
381
+ ];
382
+ var videoMediaTypeSignatures = [
383
+ {
384
+ mediaType: "video/mp4",
385
+ bytesPrefix: [
386
+ 0,
387
+ 0,
388
+ 0,
389
+ null,
390
+ 102,
391
+ 116,
392
+ 121,
393
+ 112
394
+ // ftyp
395
+ ]
396
+ },
397
+ {
398
+ mediaType: "video/webm",
399
+ bytesPrefix: [26, 69, 223, 163]
400
+ // EBML
401
+ },
402
+ {
403
+ mediaType: "video/quicktime",
404
+ bytesPrefix: [
405
+ 0,
406
+ 0,
407
+ 0,
408
+ 20,
409
+ 102,
410
+ 116,
411
+ 121,
412
+ 112,
413
+ 113,
414
+ 116
415
+ // ftypqt
416
+ ]
417
+ },
418
+ {
419
+ mediaType: "video/x-msvideo",
420
+ bytesPrefix: [82, 73, 70, 70]
421
+ // RIFF (AVI)
422
+ }
423
+ ];
424
+ var stripID3 = (data) => {
425
+ const bytes = typeof data === "string" ? convertBase64ToUint8Array(data) : data;
426
+ const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
427
+ return bytes.slice(id3Size + 10);
428
+ };
429
+ function stripID3TagsIfPresent(data) {
430
+ const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
431
+ data[1] === 68 && // 'D'
432
+ data[2] === 51;
433
+ return hasId3 ? stripID3(data) : data;
434
+ }
435
+ function detectMediaTypeBySignatures({
436
+ data,
437
+ signatures
438
+ }) {
439
+ const processedData = stripID3TagsIfPresent(data);
440
+ const bytes = typeof processedData === "string" ? convertBase64ToUint8Array(
441
+ processedData.substring(0, Math.min(processedData.length, 24))
442
+ ) : processedData;
443
+ for (const signature of signatures) {
444
+ if (bytes.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
445
+ (byte, index) => byte === null || bytes[index] === byte
446
+ )) {
447
+ return signature.mediaType;
448
+ }
449
+ }
450
+ return void 0;
451
+ }
452
+ var topLevelSignatureTables = {
453
+ image: imageMediaTypeSignatures,
454
+ audio: audioMediaTypeSignatures,
455
+ video: videoMediaTypeSignatures,
456
+ application: documentMediaTypeSignatures
457
+ };
458
+ function detectMediaType({
459
+ data,
460
+ topLevelType
461
+ }) {
462
+ if (topLevelType === void 0) {
463
+ return detectMediaTypeBySignatures({
464
+ data,
465
+ signatures: [
466
+ ...imageMediaTypeSignatures,
467
+ ...documentMediaTypeSignatures,
468
+ ...audioMediaTypeSignatures,
469
+ ...videoMediaTypeSignatures
470
+ ]
471
+ });
472
+ }
473
+ const signatures = topLevelSignatureTables[topLevelType];
474
+ if (signatures === void 0) {
475
+ return void 0;
476
+ }
477
+ return detectMediaTypeBySignatures({ data, signatures });
478
+ }
479
+ function getTopLevelMediaType(mediaType) {
480
+ const slashIndex = mediaType.indexOf("/");
481
+ return slashIndex === -1 ? mediaType : mediaType.substring(0, slashIndex);
482
+ }
483
+ function isFullMediaType(mediaType) {
484
+ const slashIndex = mediaType.indexOf("/");
485
+ if (slashIndex === -1) {
486
+ return false;
487
+ }
488
+ const subtype = mediaType.substring(slashIndex + 1);
489
+ return subtype.length > 0 && subtype !== "*";
490
+ }
491
+
206
492
  // src/download-error.ts
207
493
  import { AISDKError } from "@ai-sdk/provider";
208
494
  var name = "AI_DownloadError";
@@ -576,7 +862,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
576
862
  }
577
863
 
578
864
  // src/version.ts
579
- var VERSION = true ? "5.0.0-beta.28" : "0.0.0-test";
865
+ var VERSION = true ? "5.0.0-beta.29" : "0.0.0-test";
580
866
 
581
867
  // src/get-from-api.ts
582
868
  var getOriginalFetch = () => globalThis.fetch;
@@ -687,6 +973,12 @@ function injectJsonInstructionIntoMessages({
687
973
  ];
688
974
  }
689
975
 
976
+ // src/is-buffer.ts
977
+ function isBuffer(value) {
978
+ var _a2, _b2;
979
+ return (_b2 = (_a2 = globalThis.Buffer) == null ? void 0 : _a2.isBuffer(value)) != null ? _b2 : false;
980
+ }
981
+
690
982
  // src/is-non-nullable.ts
691
983
  function isNonNullable(value) {
692
984
  return value != null;
@@ -694,7 +986,7 @@ function isNonNullable(value) {
694
986
 
695
987
  // src/is-provider-reference.ts
696
988
  function isProviderReference(data) {
697
- return typeof data === "object" && !(data instanceof Uint8Array) && !(data instanceof URL);
989
+ return typeof data === "object" && data !== null && !(data instanceof Uint8Array) && !(data instanceof URL) && !(data instanceof ArrayBuffer) && !isBuffer(data) && !("type" in data);
698
990
  }
699
991
 
700
992
  // src/is-url-supported.ts
@@ -705,10 +997,19 @@ function isUrlSupported({
705
997
  }) {
706
998
  url = url.toLowerCase();
707
999
  mediaType = mediaType.toLowerCase();
1000
+ const isTopLevelOnly = !mediaType.includes("/");
708
1001
  return Object.entries(supportedUrls).map(([key, value]) => {
709
1002
  const mediaType2 = key.toLowerCase();
710
1003
  return mediaType2 === "*" || mediaType2 === "*/*" ? { mediaTypePrefix: "", regexes: value } : { mediaTypePrefix: mediaType2.replace(/\*/, ""), regexes: value };
711
- }).filter(({ mediaTypePrefix }) => mediaType.startsWith(mediaTypePrefix)).flatMap(({ regexes }) => regexes).some((pattern) => pattern.test(url));
1004
+ }).filter(({ mediaTypePrefix }) => {
1005
+ if (mediaTypePrefix === "") {
1006
+ return true;
1007
+ }
1008
+ if (isTopLevelOnly) {
1009
+ return `${mediaType}/` === mediaTypePrefix;
1010
+ }
1011
+ return mediaType.startsWith(mediaTypePrefix);
1012
+ }).flatMap(({ regexes }) => regexes).some((pattern) => pattern.test(url));
712
1013
  }
713
1014
 
714
1015
  // src/load-api-key.ts
@@ -925,7 +1226,9 @@ function secureJsonParse(text) {
925
1226
  }
926
1227
 
927
1228
  // src/validate-types.ts
928
- import { TypeValidationError as TypeValidationError2 } from "@ai-sdk/provider";
1229
+ import {
1230
+ TypeValidationError as TypeValidationError2
1231
+ } from "@ai-sdk/provider";
929
1232
 
930
1233
  // src/schema.ts
931
1234
  import { TypeValidationError } from "@ai-sdk/provider";
@@ -2600,6 +2903,33 @@ async function resolve(value) {
2600
2903
  return Promise.resolve(value);
2601
2904
  }
2602
2905
 
2906
+ // src/resolve-full-media-type.ts
2907
+ import {
2908
+ UnsupportedFunctionalityError
2909
+ } from "@ai-sdk/provider";
2910
+ function resolveFullMediaType({
2911
+ part
2912
+ }) {
2913
+ if (isFullMediaType(part.mediaType)) {
2914
+ return part.mediaType;
2915
+ }
2916
+ if (part.data.type === "data") {
2917
+ const detected = detectMediaType({
2918
+ data: part.data.data,
2919
+ topLevelType: getTopLevelMediaType(part.mediaType)
2920
+ });
2921
+ if (detected) {
2922
+ return detected;
2923
+ }
2924
+ throw new UnsupportedFunctionalityError({
2925
+ functionality: `file of media type "${part.mediaType}" must specify subtype since it could not be auto-detected`
2926
+ });
2927
+ }
2928
+ throw new UnsupportedFunctionalityError({
2929
+ functionality: `file of media type "${part.mediaType}" must specify subtype since it is not passed as inline bytes`
2930
+ });
2931
+ }
2932
+
2603
2933
  // src/resolve-provider-reference.ts
2604
2934
  import {
2605
2935
  NoSuchProviderReferenceError
@@ -2996,6 +3326,7 @@ export {
2996
3326
  convertAsyncIteratorToReadableStream,
2997
3327
  convertBase64ToUint8Array,
2998
3328
  convertImageModelFileToDataUri,
3329
+ convertInlineFileDataToUint8Array,
2999
3330
  convertToBase64,
3000
3331
  convertToFormData,
3001
3332
  convertUint8ArrayToBase64,
@@ -3010,6 +3341,7 @@ export {
3010
3341
  createStatusCodeErrorResponseHandler,
3011
3342
  createToolNameMapping,
3012
3343
  delay,
3344
+ detectMediaType,
3013
3345
  downloadBlob,
3014
3346
  dynamicTool,
3015
3347
  executeTool,
@@ -3019,10 +3351,13 @@ export {
3019
3351
  getErrorMessage,
3020
3352
  getFromApi,
3021
3353
  getRuntimeEnvironmentUserAgent,
3354
+ getTopLevelMediaType,
3022
3355
  injectJsonInstructionIntoMessages,
3023
3356
  isAbortError,
3357
+ isBuffer,
3024
3358
  isCustomReasoning,
3025
3359
  isExecutableTool,
3360
+ isFullMediaType,
3026
3361
  isNonNullable,
3027
3362
  isParsableJson,
3028
3363
  isProviderReference,
@@ -3045,6 +3380,7 @@ export {
3045
3380
  readResponseWithSizeLimit,
3046
3381
  removeUndefinedEntries,
3047
3382
  resolve,
3383
+ resolveFullMediaType,
3048
3384
  resolveProviderReference,
3049
3385
  safeParseJSON,
3050
3386
  safeValidateTypes,