ai 5.1.0-beta.8 → 6.0.0-beta.100

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.
@@ -1,6 +1,6 @@
1
1
  import { ModelMessage, Tool } from '@ai-sdk/provider-utils';
2
2
  export { convertAsyncIteratorToReadableStream } from '@ai-sdk/provider-utils';
3
- import { LanguageModelV2Prompt, LanguageModelV2FunctionTool, LanguageModelV2ProviderDefinedTool, LanguageModelV2ToolChoice } from '@ai-sdk/provider';
3
+ import { LanguageModelV3Prompt, LanguageModelV3FunctionTool, LanguageModelV3ProviderDefinedTool, LanguageModelV3ToolChoice } from '@ai-sdk/provider';
4
4
 
5
5
  /**
6
6
  * Experimental. Can change in patch versions without warning.
@@ -80,7 +80,7 @@ declare function convertToLanguageModelPrompt({ prompt, supportedUrls, download,
80
80
  prompt: StandardizedPrompt;
81
81
  supportedUrls: Record<string, RegExp[]>;
82
82
  download: DownloadFunction | undefined;
83
- }): Promise<LanguageModelV2Prompt>;
83
+ }): Promise<LanguageModelV3Prompt>;
84
84
 
85
85
  type CallSettings = {
86
86
  /**
@@ -166,16 +166,16 @@ type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'requ
166
166
  toolName: Extract<keyof TOOLS, string>;
167
167
  };
168
168
 
169
- type ToolSet = Record<string, (Tool<never, never> | Tool<any, any> | Tool<any, never> | Tool<never, any>) & Pick<Tool<any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta'>>;
169
+ type ToolSet = Record<string, (Tool<never, never> | Tool<any, any> | Tool<any, never> | Tool<never, any>) & Pick<Tool<any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta' | 'needsApproval'>>;
170
170
 
171
171
  declare function prepareToolsAndToolChoice<TOOLS extends ToolSet>({ tools, toolChoice, activeTools, }: {
172
172
  tools: TOOLS | undefined;
173
173
  toolChoice: ToolChoice<TOOLS> | undefined;
174
174
  activeTools: Array<keyof TOOLS> | undefined;
175
- }): {
176
- tools: Array<LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool> | undefined;
177
- toolChoice: LanguageModelV2ToolChoice | undefined;
178
- };
175
+ }): Promise<{
176
+ tools: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderDefinedTool> | undefined;
177
+ toolChoice: LanguageModelV3ToolChoice | undefined;
178
+ }>;
179
179
 
180
180
  /**
181
181
  * Validates call settings and returns a new object with limited values.
@@ -1,6 +1,6 @@
1
1
  import { ModelMessage, Tool } from '@ai-sdk/provider-utils';
2
2
  export { convertAsyncIteratorToReadableStream } from '@ai-sdk/provider-utils';
3
- import { LanguageModelV2Prompt, LanguageModelV2FunctionTool, LanguageModelV2ProviderDefinedTool, LanguageModelV2ToolChoice } from '@ai-sdk/provider';
3
+ import { LanguageModelV3Prompt, LanguageModelV3FunctionTool, LanguageModelV3ProviderDefinedTool, LanguageModelV3ToolChoice } from '@ai-sdk/provider';
4
4
 
5
5
  /**
6
6
  * Experimental. Can change in patch versions without warning.
@@ -80,7 +80,7 @@ declare function convertToLanguageModelPrompt({ prompt, supportedUrls, download,
80
80
  prompt: StandardizedPrompt;
81
81
  supportedUrls: Record<string, RegExp[]>;
82
82
  download: DownloadFunction | undefined;
83
- }): Promise<LanguageModelV2Prompt>;
83
+ }): Promise<LanguageModelV3Prompt>;
84
84
 
85
85
  type CallSettings = {
86
86
  /**
@@ -166,16 +166,16 @@ type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'requ
166
166
  toolName: Extract<keyof TOOLS, string>;
167
167
  };
168
168
 
169
- type ToolSet = Record<string, (Tool<never, never> | Tool<any, any> | Tool<any, never> | Tool<never, any>) & Pick<Tool<any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta'>>;
169
+ type ToolSet = Record<string, (Tool<never, never> | Tool<any, any> | Tool<any, never> | Tool<never, any>) & Pick<Tool<any, any>, 'execute' | 'onInputAvailable' | 'onInputStart' | 'onInputDelta' | 'needsApproval'>>;
170
170
 
171
171
  declare function prepareToolsAndToolChoice<TOOLS extends ToolSet>({ tools, toolChoice, activeTools, }: {
172
172
  tools: TOOLS | undefined;
173
173
  toolChoice: ToolChoice<TOOLS> | undefined;
174
174
  activeTools: Array<keyof TOOLS> | undefined;
175
- }): {
176
- tools: Array<LanguageModelV2FunctionTool | LanguageModelV2ProviderDefinedTool> | undefined;
177
- toolChoice: LanguageModelV2ToolChoice | undefined;
178
- };
175
+ }): Promise<{
176
+ tools: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderDefinedTool> | undefined;
177
+ toolChoice: LanguageModelV3ToolChoice | undefined;
178
+ }>;
179
179
 
180
180
  /**
181
181
  * Validates call settings and returns a new object with limited values.
@@ -177,7 +177,7 @@ _a = symbol;
177
177
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
178
178
 
179
179
  // src/version.ts
180
- var VERSION = true ? "5.1.0-beta.8" : "0.0.0-test";
180
+ var VERSION = true ? "6.0.0-beta.100" : "0.0.0-test";
181
181
 
182
182
  // src/util/download/download.ts
183
183
  var download = async ({ url }) => {
@@ -252,7 +252,7 @@ var dataContentSchema = import_v4.z.union([
252
252
  { message: "Must be a Buffer" }
253
253
  )
254
254
  ]);
255
- function convertToLanguageModelV2DataContent(content) {
255
+ function convertToLanguageModelV3DataContent(content) {
256
256
  if (content instanceof Uint8Array) {
257
257
  return { data: content, mediaType: void 0 };
258
258
  }
@@ -312,12 +312,26 @@ async function convertToLanguageModelPrompt({
312
312
  download2,
313
313
  supportedUrls
314
314
  );
315
- return [
315
+ const messages = [
316
316
  ...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
317
317
  ...prompt.messages.map(
318
318
  (message) => convertToLanguageModelMessage({ message, downloadedAssets })
319
319
  )
320
320
  ];
321
+ const combinedMessages = [];
322
+ for (const message of messages) {
323
+ if (message.role !== "tool") {
324
+ combinedMessages.push(message);
325
+ continue;
326
+ }
327
+ const lastCombinedMessage = combinedMessages.at(-1);
328
+ if ((lastCombinedMessage == null ? void 0 : lastCombinedMessage.role) === "tool") {
329
+ lastCombinedMessage.content.push(...message.content);
330
+ } else {
331
+ combinedMessages.push(message);
332
+ }
333
+ }
334
+ return combinedMessages;
321
335
  }
322
336
  function convertToLanguageModelMessage({
323
337
  message,
@@ -359,11 +373,13 @@ function convertToLanguageModelMessage({
359
373
  content: message.content.filter(
360
374
  // remove empty text parts (no text, and no provider options):
361
375
  (part) => part.type !== "text" || part.text !== "" || part.providerOptions != null
376
+ ).filter(
377
+ (part) => part.type !== "tool-approval-request"
362
378
  ).map((part) => {
363
379
  const providerOptions = part.providerOptions;
364
380
  switch (part.type) {
365
381
  case "file": {
366
- const { data, mediaType } = convertToLanguageModelV2DataContent(
382
+ const { data, mediaType } = convertToLanguageModelV3DataContent(
367
383
  part.data
368
384
  );
369
385
  return {
@@ -403,7 +419,7 @@ function convertToLanguageModelMessage({
403
419
  type: "tool-result",
404
420
  toolCallId: part.toolCallId,
405
421
  toolName: part.toolName,
406
- output: part.output,
422
+ output: mapToolResultOutput(part.output),
407
423
  providerOptions
408
424
  };
409
425
  }
@@ -415,11 +431,11 @@ function convertToLanguageModelMessage({
415
431
  case "tool": {
416
432
  return {
417
433
  role: "tool",
418
- content: message.content.map((part) => ({
434
+ content: message.content.filter((part) => part.type !== "tool-approval-response").map((part) => ({
419
435
  type: "tool-result",
420
436
  toolCallId: part.toolCallId,
421
437
  toolName: part.toolName,
422
- output: part.output,
438
+ output: mapToolResultOutput(part.output),
423
439
  providerOptions: part.providerOptions
424
440
  })),
425
441
  providerOptions: message.providerOptions
@@ -459,12 +475,12 @@ async function downloadAssets(messages, download2, supportedUrls) {
459
475
  }));
460
476
  const downloadedFiles = await download2(plannedDownloads);
461
477
  return Object.fromEntries(
462
- downloadedFiles.filter(
463
- (downloadedFile) => (downloadedFile == null ? void 0 : downloadedFile.data) != null
464
- ).map(({ data, mediaType }, index) => [
465
- plannedDownloads[index].url.toString(),
466
- { data, mediaType }
467
- ])
478
+ downloadedFiles.map(
479
+ (file, index) => file == null ? null : [
480
+ plannedDownloads[index].url.toString(),
481
+ { data: file.data, mediaType: file.mediaType }
482
+ ]
483
+ ).filter((file) => file != null)
468
484
  );
469
485
  }
470
486
  function convertPartToLanguageModelPart(part, downloadedAssets) {
@@ -488,7 +504,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
488
504
  default:
489
505
  throw new Error(`Unsupported part type: ${type}`);
490
506
  }
491
- const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
507
+ const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV3DataContent(originalData);
492
508
  let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
493
509
  let data = convertedData;
494
510
  if (data instanceof URL) {
@@ -526,6 +542,31 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
526
542
  }
527
543
  }
528
544
  }
545
+ function mapToolResultOutput(output) {
546
+ if (output.type !== "content") {
547
+ return output;
548
+ }
549
+ return {
550
+ type: "content",
551
+ value: output.value.map((item) => {
552
+ if (item.type !== "media") {
553
+ return item;
554
+ }
555
+ if (item.mediaType.startsWith("image/")) {
556
+ return {
557
+ type: "image-data",
558
+ data: item.data,
559
+ mediaType: item.mediaType
560
+ };
561
+ }
562
+ return {
563
+ type: "file-data",
564
+ data: item.data,
565
+ mediaType: item.mediaType
566
+ };
567
+ })
568
+ };
569
+ }
529
570
 
530
571
  // src/prompt/prepare-tools-and-tool-choice.ts
531
572
  var import_provider_utils5 = require("@ai-sdk/provider-utils");
@@ -536,7 +577,7 @@ function isNonEmptyObject(object) {
536
577
  }
537
578
 
538
579
  // src/prompt/prepare-tools-and-tool-choice.ts
539
- function prepareToolsAndToolChoice({
580
+ async function prepareToolsAndToolChoice({
540
581
  tools,
541
582
  toolChoice,
542
583
  activeTools
@@ -550,33 +591,37 @@ function prepareToolsAndToolChoice({
550
591
  const filteredTools = activeTools != null ? Object.entries(tools).filter(
551
592
  ([name5]) => activeTools.includes(name5)
552
593
  ) : Object.entries(tools);
553
- return {
554
- tools: filteredTools.map(([name5, tool]) => {
555
- const toolType = tool.type;
556
- switch (toolType) {
557
- case void 0:
558
- case "dynamic":
559
- case "function":
560
- return {
561
- type: "function",
562
- name: name5,
563
- description: tool.description,
564
- inputSchema: (0, import_provider_utils5.asSchema)(tool.inputSchema).jsonSchema,
565
- providerOptions: tool.providerOptions
566
- };
567
- case "provider-defined":
568
- return {
569
- type: "provider-defined",
570
- name: name5,
571
- id: tool.id,
572
- args: tool.args
573
- };
574
- default: {
575
- const exhaustiveCheck = toolType;
576
- throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
577
- }
594
+ const languageModelTools = [];
595
+ for (const [name5, tool] of filteredTools) {
596
+ const toolType = tool.type;
597
+ switch (toolType) {
598
+ case void 0:
599
+ case "dynamic":
600
+ case "function":
601
+ languageModelTools.push({
602
+ type: "function",
603
+ name: name5,
604
+ description: tool.description,
605
+ inputSchema: await (0, import_provider_utils5.asSchema)(tool.inputSchema).jsonSchema,
606
+ providerOptions: tool.providerOptions
607
+ });
608
+ break;
609
+ case "provider-defined":
610
+ languageModelTools.push({
611
+ type: "provider-defined",
612
+ name: name5,
613
+ id: tool.id,
614
+ args: tool.args
615
+ });
616
+ break;
617
+ default: {
618
+ const exhaustiveCheck = toolType;
619
+ throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
578
620
  }
579
- }),
621
+ }
622
+ }
623
+ return {
624
+ tools: languageModelTools,
580
625
  toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
581
626
  };
582
627
  }
@@ -600,7 +645,7 @@ var jsonValueSchema = import_v42.z.lazy(
600
645
  import_v42.z.string(),
601
646
  import_v42.z.number(),
602
647
  import_v42.z.boolean(),
603
- import_v42.z.record(import_v42.z.string(), jsonValueSchema),
648
+ import_v42.z.record(import_v42.z.string(), jsonValueSchema.optional()),
604
649
  import_v42.z.array(jsonValueSchema)
605
650
  ])
606
651
  );
@@ -608,7 +653,7 @@ var jsonValueSchema = import_v42.z.lazy(
608
653
  // src/types/provider-metadata.ts
609
654
  var providerMetadataSchema = import_v43.z.record(
610
655
  import_v43.z.string(),
611
- import_v43.z.record(import_v43.z.string(), jsonValueSchema)
656
+ import_v43.z.record(import_v43.z.string(), jsonValueSchema.optional())
612
657
  );
613
658
 
614
659
  // src/prompt/content-part.ts
@@ -644,40 +689,90 @@ var toolCallPartSchema = import_v44.z.object({
644
689
  providerOptions: providerMetadataSchema.optional(),
645
690
  providerExecuted: import_v44.z.boolean().optional()
646
691
  });
647
- var outputSchema = import_v44.z.discriminatedUnion("type", [
648
- import_v44.z.object({
649
- type: import_v44.z.literal("text"),
650
- value: import_v44.z.string()
651
- }),
652
- import_v44.z.object({
653
- type: import_v44.z.literal("json"),
654
- value: jsonValueSchema
655
- }),
656
- import_v44.z.object({
657
- type: import_v44.z.literal("error-text"),
658
- value: import_v44.z.string()
659
- }),
660
- import_v44.z.object({
661
- type: import_v44.z.literal("error-json"),
662
- value: jsonValueSchema
663
- }),
664
- import_v44.z.object({
665
- type: import_v44.z.literal("content"),
666
- value: import_v44.z.array(
667
- import_v44.z.union([
668
- import_v44.z.object({
669
- type: import_v44.z.literal("text"),
670
- text: import_v44.z.string()
671
- }),
672
- import_v44.z.object({
673
- type: import_v44.z.literal("media"),
674
- data: import_v44.z.string(),
675
- mediaType: import_v44.z.string()
676
- })
677
- ])
678
- )
679
- })
680
- ]);
692
+ var outputSchema = import_v44.z.discriminatedUnion(
693
+ "type",
694
+ [
695
+ import_v44.z.object({
696
+ type: import_v44.z.literal("text"),
697
+ value: import_v44.z.string(),
698
+ providerOptions: providerMetadataSchema.optional()
699
+ }),
700
+ import_v44.z.object({
701
+ type: import_v44.z.literal("json"),
702
+ value: jsonValueSchema,
703
+ providerOptions: providerMetadataSchema.optional()
704
+ }),
705
+ import_v44.z.object({
706
+ type: import_v44.z.literal("execution-denied"),
707
+ reason: import_v44.z.string().optional(),
708
+ providerOptions: providerMetadataSchema.optional()
709
+ }),
710
+ import_v44.z.object({
711
+ type: import_v44.z.literal("error-text"),
712
+ value: import_v44.z.string(),
713
+ providerOptions: providerMetadataSchema.optional()
714
+ }),
715
+ import_v44.z.object({
716
+ type: import_v44.z.literal("error-json"),
717
+ value: jsonValueSchema,
718
+ providerOptions: providerMetadataSchema.optional()
719
+ }),
720
+ import_v44.z.object({
721
+ type: import_v44.z.literal("content"),
722
+ value: import_v44.z.array(
723
+ import_v44.z.union([
724
+ import_v44.z.object({
725
+ type: import_v44.z.literal("text"),
726
+ text: import_v44.z.string(),
727
+ providerOptions: providerMetadataSchema.optional()
728
+ }),
729
+ import_v44.z.object({
730
+ type: import_v44.z.literal("media"),
731
+ data: import_v44.z.string(),
732
+ mediaType: import_v44.z.string()
733
+ }),
734
+ import_v44.z.object({
735
+ type: import_v44.z.literal("file-data"),
736
+ data: import_v44.z.string(),
737
+ mediaType: import_v44.z.string(),
738
+ filename: import_v44.z.string().optional(),
739
+ providerOptions: providerMetadataSchema.optional()
740
+ }),
741
+ import_v44.z.object({
742
+ type: import_v44.z.literal("file-url"),
743
+ url: import_v44.z.string(),
744
+ providerOptions: providerMetadataSchema.optional()
745
+ }),
746
+ import_v44.z.object({
747
+ type: import_v44.z.literal("file-id"),
748
+ fileId: import_v44.z.union([import_v44.z.string(), import_v44.z.record(import_v44.z.string(), import_v44.z.string())]),
749
+ providerOptions: providerMetadataSchema.optional()
750
+ }),
751
+ import_v44.z.object({
752
+ type: import_v44.z.literal("image-data"),
753
+ data: import_v44.z.string(),
754
+ mediaType: import_v44.z.string(),
755
+ providerOptions: providerMetadataSchema.optional()
756
+ }),
757
+ import_v44.z.object({
758
+ type: import_v44.z.literal("image-url"),
759
+ url: import_v44.z.string(),
760
+ providerOptions: providerMetadataSchema.optional()
761
+ }),
762
+ import_v44.z.object({
763
+ type: import_v44.z.literal("image-file-id"),
764
+ fileId: import_v44.z.union([import_v44.z.string(), import_v44.z.record(import_v44.z.string(), import_v44.z.string())]),
765
+ providerOptions: providerMetadataSchema.optional()
766
+ }),
767
+ import_v44.z.object({
768
+ type: import_v44.z.literal("custom"),
769
+ providerOptions: providerMetadataSchema.optional()
770
+ })
771
+ ])
772
+ )
773
+ })
774
+ ]
775
+ );
681
776
  var toolResultPartSchema = import_v44.z.object({
682
777
  type: import_v44.z.literal("tool-result"),
683
778
  toolCallId: import_v44.z.string(),
@@ -685,6 +780,17 @@ var toolResultPartSchema = import_v44.z.object({
685
780
  output: outputSchema,
686
781
  providerOptions: providerMetadataSchema.optional()
687
782
  });
783
+ var toolApprovalRequestSchema = import_v44.z.object({
784
+ type: import_v44.z.literal("tool-approval-request"),
785
+ approvalId: import_v44.z.string(),
786
+ toolCallId: import_v44.z.string()
787
+ });
788
+ var toolApprovalResponseSchema = import_v44.z.object({
789
+ type: import_v44.z.literal("tool-approval-response"),
790
+ approvalId: import_v44.z.string(),
791
+ approved: import_v44.z.boolean(),
792
+ reason: import_v44.z.string().optional()
793
+ });
688
794
 
689
795
  // src/prompt/message.ts
690
796
  var systemModelMessageSchema = import_v45.z.object(
@@ -712,7 +818,8 @@ var assistantModelMessageSchema = import_v45.z.object({
712
818
  filePartSchema,
713
819
  reasoningPartSchema,
714
820
  toolCallPartSchema,
715
- toolResultPartSchema
821
+ toolResultPartSchema,
822
+ toolApprovalRequestSchema
716
823
  ])
717
824
  )
718
825
  ]),
@@ -720,7 +827,7 @@ var assistantModelMessageSchema = import_v45.z.object({
720
827
  });
721
828
  var toolModelMessageSchema = import_v45.z.object({
722
829
  role: import_v45.z.literal("tool"),
723
- content: import_v45.z.array(toolResultPartSchema),
830
+ content: import_v45.z.array(import_v45.z.union([toolResultPartSchema, toolApprovalResponseSchema])),
724
831
  providerOptions: providerMetadataSchema.optional()
725
832
  });
726
833
  var modelMessageSchema = import_v45.z.union([