ai 3.4.5 → 3.4.7

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "3.4.5",
3
+ "version": "3.4.7",
4
4
  "description": "AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -64,13 +64,13 @@
64
64
  }
65
65
  },
66
66
  "dependencies": {
67
- "@ai-sdk/provider": "0.0.23",
68
- "@ai-sdk/provider-utils": "1.0.19",
69
- "@ai-sdk/react": "0.0.61",
70
- "@ai-sdk/solid": "0.0.48",
71
- "@ai-sdk/svelte": "0.0.50",
72
- "@ai-sdk/ui-utils": "0.0.45",
73
- "@ai-sdk/vue": "0.0.52",
67
+ "@ai-sdk/provider": "0.0.24",
68
+ "@ai-sdk/provider-utils": "1.0.20",
69
+ "@ai-sdk/react": "0.0.62",
70
+ "@ai-sdk/solid": "0.0.49",
71
+ "@ai-sdk/svelte": "0.0.51",
72
+ "@ai-sdk/ui-utils": "0.0.46",
73
+ "@ai-sdk/vue": "0.0.53",
74
74
  "@opentelemetry/api": "1.9.0",
75
75
  "eventsource-parser": "1.1.2",
76
76
  "jsondiffpatch": "0.6.0",
@@ -274,6 +274,29 @@ interface ImagePart {
274
274
  experimental_providerMetadata?: ProviderMetadata;
275
275
  }
276
276
  /**
277
+ File content part of a prompt. It contains a file.
278
+ */
279
+ interface FilePart {
280
+ type: 'file';
281
+ /**
282
+ File data. Can either be:
283
+
284
+ - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
285
+ - URL: a URL that points to the image
286
+ */
287
+ data: DataContent | URL;
288
+ /**
289
+ Mime type of the file.
290
+ */
291
+ mimeType: string;
292
+ /**
293
+ Additional provider-specific metadata. They are passed through
294
+ to the provider from the AI SDK and enable provider-specific
295
+ functionality that can be fully encapsulated in the provider.
296
+ */
297
+ experimental_providerMetadata?: ProviderMetadata;
298
+ }
299
+ /**
277
300
  Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
278
301
  */
279
302
  interface ToolCallPart {
@@ -359,7 +382,7 @@ type CoreUserMessage = {
359
382
  /**
360
383
  Content of a user message. It can be a string or an array of text and image parts.
361
384
  */
362
- type UserContent = string | Array<TextPart | ImagePart>;
385
+ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
363
386
  /**
364
387
  An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
365
388
  */
@@ -272,6 +272,29 @@ interface ImagePart {
272
272
  experimental_providerMetadata?: ProviderMetadata;
273
273
  }
274
274
  /**
275
+ File content part of a prompt. It contains a file.
276
+ */
277
+ interface FilePart {
278
+ type: 'file';
279
+ /**
280
+ File data. Can either be:
281
+
282
+ - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
283
+ - URL: a URL that points to the image
284
+ */
285
+ data: DataContent | URL;
286
+ /**
287
+ Mime type of the file.
288
+ */
289
+ mimeType: string;
290
+ /**
291
+ Additional provider-specific metadata. They are passed through
292
+ to the provider from the AI SDK and enable provider-specific
293
+ functionality that can be fully encapsulated in the provider.
294
+ */
295
+ experimental_providerMetadata?: ProviderMetadata;
296
+ }
297
+ /**
275
298
  Tool call content part of a prompt. It contains a tool call (usually generated by the AI model).
276
299
  */
277
300
  interface ToolCallPart {
@@ -357,7 +380,7 @@ type CoreUserMessage = {
357
380
  /**
358
381
  Content of a user message. It can be a string or an array of text and image parts.
359
382
  */
360
- type UserContent = string | Array<TextPart | ImagePart>;
383
+ type UserContent = string | Array<TextPart | ImagePart | FilePart>;
361
384
  /**
362
385
  An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
363
386
  */
@@ -214,7 +214,10 @@ function createAI({
214
214
  import { safeParseJSON } from "@ai-sdk/provider-utils";
215
215
 
216
216
  // core/prompt/convert-to-language-model-prompt.ts
217
- import { getErrorMessage } from "@ai-sdk/provider-utils";
217
+ import {
218
+ convertUint8ArrayToBase64 as convertUint8ArrayToBase642,
219
+ getErrorMessage
220
+ } from "@ai-sdk/provider-utils";
218
221
 
219
222
  // util/download-error.ts
220
223
  import { AISDKError } from "@ai-sdk/provider";
@@ -366,6 +369,15 @@ var dataContentSchema = z.union([
366
369
  { message: "Must be a Buffer" }
367
370
  )
368
371
  ]);
372
+ function convertDataContentToBase64String(content) {
373
+ if (typeof content === "string") {
374
+ return content;
375
+ }
376
+ if (content instanceof ArrayBuffer) {
377
+ return convertUint8ArrayToBase64(new Uint8Array(content));
378
+ }
379
+ return convertUint8ArrayToBase64(content);
380
+ }
369
381
  function convertDataContentToUint8Array(content) {
370
382
  if (content instanceof Uint8Array) {
371
383
  return content;
@@ -425,6 +437,22 @@ var InvalidMessageRoleError = class extends AISDKError3 {
425
437
  };
426
438
  _a3 = symbol3;
427
439
 
440
+ // core/prompt/split-data-url.ts
441
+ function splitDataUrl(dataUrl) {
442
+ try {
443
+ const [header, base64Content] = dataUrl.split(",");
444
+ return {
445
+ mimeType: header.split(";")[0].split(":")[1],
446
+ base64Content
447
+ };
448
+ } catch (error) {
449
+ return {
450
+ mimeType: void 0,
451
+ base64Content: void 0
452
+ };
453
+ }
454
+ }
455
+
428
456
  // core/prompt/convert-to-language-model-prompt.ts
429
457
  async function convertToLanguageModelPrompt({
430
458
  prompt,
@@ -435,7 +463,7 @@ async function convertToLanguageModelPrompt({
435
463
  if (prompt.system != null) {
436
464
  languageModelMessages.push({ role: "system", content: prompt.system });
437
465
  }
438
- const downloadedImages = modelSupportsImageUrls || prompt.messages == null ? null : await downloadImages(prompt.messages, downloadImplementation);
466
+ const downloadedAssets = modelSupportsImageUrls || prompt.messages == null ? null : await downloadAssets(prompt.messages, downloadImplementation);
439
467
  const promptType = prompt.type;
440
468
  switch (promptType) {
441
469
  case "prompt": {
@@ -448,7 +476,7 @@ async function convertToLanguageModelPrompt({
448
476
  case "messages": {
449
477
  languageModelMessages.push(
450
478
  ...prompt.messages.map(
451
- (message) => convertToLanguageModelMessage(message, downloadedImages)
479
+ (message) => convertToLanguageModelMessage(message, downloadedAssets)
452
480
  )
453
481
  );
454
482
  break;
@@ -460,7 +488,7 @@ async function convertToLanguageModelPrompt({
460
488
  }
461
489
  return languageModelMessages;
462
490
  }
463
- function convertToLanguageModelMessage(message, downloadedImages) {
491
+ function convertToLanguageModelMessage(message, downloadedAssets) {
464
492
  const role = message.role;
465
493
  switch (role) {
466
494
  case "system": {
@@ -480,98 +508,178 @@ function convertToLanguageModelMessage(message, downloadedImages) {
480
508
  }
481
509
  return {
482
510
  role: "user",
483
- content: message.content.map((part) => {
484
- var _a8, _b, _c;
485
- switch (part.type) {
486
- case "text": {
487
- return {
488
- type: "text",
489
- text: part.text,
490
- providerMetadata: part.experimental_providerMetadata
491
- };
492
- }
493
- case "image": {
494
- if (part.image instanceof URL) {
495
- if (downloadedImages == null) {
496
- return {
497
- type: "image",
498
- image: part.image,
499
- mimeType: part.mimeType,
500
- providerMetadata: part.experimental_providerMetadata
501
- };
502
- } else {
503
- const downloadedImage = downloadedImages[part.image.toString()];
504
- return {
505
- type: "image",
506
- image: downloadedImage.data,
507
- mimeType: (_a8 = part.mimeType) != null ? _a8 : downloadedImage.mimeType,
508
- providerMetadata: part.experimental_providerMetadata
509
- };
510
- }
511
+ content: message.content.map(
512
+ (part) => {
513
+ var _a8, _b, _c, _d, _e;
514
+ switch (part.type) {
515
+ case "text": {
516
+ return {
517
+ type: "text",
518
+ text: part.text,
519
+ providerMetadata: part.experimental_providerMetadata
520
+ };
511
521
  }
512
- if (typeof part.image === "string") {
513
- try {
514
- const url = new URL(part.image);
515
- switch (url.protocol) {
516
- case "http:":
517
- case "https:": {
518
- if (downloadedImages == null) {
519
- return {
520
- type: "image",
521
- image: url,
522
- mimeType: part.mimeType,
523
- providerMetadata: part.experimental_providerMetadata
524
- };
525
- } else {
526
- const downloadedImage = downloadedImages[part.image];
527
- return {
528
- type: "image",
529
- image: downloadedImage.data,
530
- mimeType: (_b = part.mimeType) != null ? _b : downloadedImage.mimeType,
531
- providerMetadata: part.experimental_providerMetadata
532
- };
522
+ case "image": {
523
+ if (part.image instanceof URL) {
524
+ if (downloadedAssets == null) {
525
+ return {
526
+ type: "image",
527
+ image: part.image,
528
+ mimeType: part.mimeType,
529
+ providerMetadata: part.experimental_providerMetadata
530
+ };
531
+ } else {
532
+ const downloadedImage = downloadedAssets[part.image.toString()];
533
+ return {
534
+ type: "image",
535
+ image: downloadedImage.data,
536
+ mimeType: (_a8 = part.mimeType) != null ? _a8 : downloadedImage.mimeType,
537
+ providerMetadata: part.experimental_providerMetadata
538
+ };
539
+ }
540
+ }
541
+ if (typeof part.image === "string") {
542
+ try {
543
+ const url = new URL(part.image);
544
+ switch (url.protocol) {
545
+ case "http:":
546
+ case "https:": {
547
+ if (downloadedAssets == null) {
548
+ return {
549
+ type: "image",
550
+ image: url,
551
+ mimeType: part.mimeType,
552
+ providerMetadata: part.experimental_providerMetadata
553
+ };
554
+ } else {
555
+ const downloadedImage = downloadedAssets[url.toString()];
556
+ return {
557
+ type: "image",
558
+ image: downloadedImage.data,
559
+ mimeType: (_b = part.mimeType) != null ? _b : downloadedImage.mimeType,
560
+ providerMetadata: part.experimental_providerMetadata
561
+ };
562
+ }
533
563
  }
534
- }
535
- case "data:": {
536
- try {
537
- const [header, base64Content] = part.image.split(",");
538
- const mimeType = header.split(";")[0].split(":")[1];
539
- if (mimeType == null || base64Content == null) {
540
- throw new Error("Invalid data URL format");
564
+ case "data:": {
565
+ try {
566
+ const { mimeType, base64Content } = splitDataUrl(
567
+ part.image
568
+ );
569
+ if (mimeType == null || base64Content == null) {
570
+ throw new Error("Invalid data URL format");
571
+ }
572
+ return {
573
+ type: "image",
574
+ image: convertDataContentToUint8Array(base64Content),
575
+ mimeType,
576
+ providerMetadata: part.experimental_providerMetadata
577
+ };
578
+ } catch (error) {
579
+ throw new Error(
580
+ `Error processing data URL: ${getErrorMessage(
581
+ message
582
+ )}`
583
+ );
541
584
  }
542
- return {
543
- type: "image",
544
- image: convertDataContentToUint8Array(base64Content),
545
- mimeType,
546
- providerMetadata: part.experimental_providerMetadata
547
- };
548
- } catch (error) {
549
- throw new Error(
550
- `Error processing data URL: ${getErrorMessage(
551
- message
552
- )}`
553
- );
554
585
  }
555
586
  }
556
- default: {
557
- throw new Error(
558
- `Unsupported URL protocol: ${url.protocol}`
559
- );
587
+ } catch (_ignored) {
588
+ }
589
+ }
590
+ const imageUint8 = convertDataContentToUint8Array(part.image);
591
+ return {
592
+ type: "image",
593
+ image: imageUint8,
594
+ mimeType: (_c = part.mimeType) != null ? _c : detectImageMimeType(imageUint8),
595
+ providerMetadata: part.experimental_providerMetadata
596
+ };
597
+ }
598
+ case "file": {
599
+ if (part.data instanceof URL) {
600
+ if (downloadedAssets == null) {
601
+ return {
602
+ type: "file",
603
+ data: part.data,
604
+ mimeType: part.mimeType,
605
+ providerMetadata: part.experimental_providerMetadata
606
+ };
607
+ } else {
608
+ const downloadedImage = downloadedAssets[part.data.toString()];
609
+ return {
610
+ type: "file",
611
+ data: convertUint8ArrayToBase642(downloadedImage.data),
612
+ mimeType: (_d = part.mimeType) != null ? _d : downloadedImage.mimeType,
613
+ providerMetadata: part.experimental_providerMetadata
614
+ };
615
+ }
616
+ }
617
+ if (typeof part.data === "string") {
618
+ try {
619
+ const url = new URL(part.data);
620
+ switch (url.protocol) {
621
+ case "http:":
622
+ case "https:": {
623
+ if (downloadedAssets == null) {
624
+ return {
625
+ type: "file",
626
+ data: url,
627
+ mimeType: part.mimeType,
628
+ providerMetadata: part.experimental_providerMetadata
629
+ };
630
+ } else {
631
+ const downloadedImage = downloadedAssets[url.toString()];
632
+ return {
633
+ type: "file",
634
+ data: convertUint8ArrayToBase642(
635
+ downloadedImage.data
636
+ ),
637
+ mimeType: (_e = part.mimeType) != null ? _e : downloadedImage.mimeType,
638
+ providerMetadata: part.experimental_providerMetadata
639
+ };
640
+ }
641
+ }
642
+ case "data:": {
643
+ try {
644
+ const { mimeType, base64Content } = splitDataUrl(
645
+ part.data
646
+ );
647
+ if (mimeType == null || base64Content == null) {
648
+ throw new Error("Invalid data URL format");
649
+ }
650
+ return {
651
+ type: "file",
652
+ data: convertDataContentToBase64String(
653
+ base64Content
654
+ ),
655
+ mimeType,
656
+ providerMetadata: part.experimental_providerMetadata
657
+ };
658
+ } catch (error) {
659
+ throw new Error(
660
+ `Error processing data URL: ${getErrorMessage(
661
+ message
662
+ )}`
663
+ );
664
+ }
665
+ }
560
666
  }
667
+ } catch (_ignored) {
561
668
  }
562
- } catch (_ignored) {
563
669
  }
670
+ const imageBase64 = convertDataContentToBase64String(
671
+ part.data
672
+ );
673
+ return {
674
+ type: "file",
675
+ data: imageBase64,
676
+ mimeType: part.mimeType,
677
+ providerMetadata: part.experimental_providerMetadata
678
+ };
564
679
  }
565
- const imageUint8 = convertDataContentToUint8Array(part.image);
566
- return {
567
- type: "image",
568
- image: imageUint8,
569
- mimeType: (_c = part.mimeType) != null ? _c : detectImageMimeType(imageUint8),
570
- providerMetadata: part.experimental_providerMetadata
571
- };
572
680
  }
573
681
  }
574
- }).filter((part) => part.type !== "text" || part.text !== ""),
682
+ ).filter((part) => part.type !== "text" || part.text !== ""),
575
683
  providerMetadata: message.experimental_providerMetadata
576
684
  };
577
685
  }
@@ -617,12 +725,14 @@ function convertToLanguageModelMessage(message, downloadedImages) {
617
725
  }
618
726
  }
619
727
  }
620
- async function downloadImages(messages, downloadImplementation) {
728
+ async function downloadAssets(messages, downloadImplementation) {
621
729
  const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
622
730
  (content) => Array.isArray(content)
623
- ).flat().filter((part) => part.type === "image").map((part) => part.image).map(
731
+ ).flat().filter(
732
+ (part) => part.type === "image" || part.type === "file"
733
+ ).map((part) => part.type === "image" ? part.image : part.data).map(
624
734
  (part) => (
625
- // support string urls in image parts:
735
+ // support string urls:
626
736
  typeof part === "string" && (part.startsWith("http:") || part.startsWith("https:")) ? new URL(part) : part
627
737
  )
628
738
  ).filter((image) => image instanceof URL);
@@ -862,6 +972,12 @@ var imagePartSchema = z4.object({
862
972
  mimeType: z4.string().optional(),
863
973
  experimental_providerMetadata: providerMetadataSchema.optional()
864
974
  });
975
+ var filePartSchema = z4.object({
976
+ type: z4.literal("file"),
977
+ data: z4.union([dataContentSchema, z4.instanceof(URL)]),
978
+ mimeType: z4.string(),
979
+ experimental_providerMetadata: providerMetadataSchema.optional()
980
+ });
865
981
  var toolCallPartSchema = z4.object({
866
982
  type: z4.literal("tool-call"),
867
983
  toolCallId: z4.string(),
@@ -887,7 +1003,7 @@ var coreUserMessageSchema = z5.object({
887
1003
  role: z5.literal("user"),
888
1004
  content: z5.union([
889
1005
  z5.string(),
890
- z5.array(z5.union([textPartSchema, imagePartSchema]))
1006
+ z5.array(z5.union([textPartSchema, imagePartSchema, filePartSchema]))
891
1007
  ]),
892
1008
  experimental_providerMetadata: providerMetadataSchema.optional()
893
1009
  });