ai 6.0.42 → 6.0.43

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.
@@ -128,11 +128,11 @@ import {
128
128
  } from "@ai-sdk/provider-utils";
129
129
 
130
130
  // src/version.ts
131
- var VERSION = true ? "6.0.42" : "0.0.0-test";
131
+ var VERSION = true ? "6.0.43" : "0.0.0-test";
132
132
 
133
133
  // src/util/download/download.ts
134
134
  var download = async ({ url }) => {
135
- var _a4;
135
+ var _a5;
136
136
  const urlText = url.toString();
137
137
  try {
138
138
  const response = await fetch(urlText, {
@@ -151,7 +151,7 @@ var download = async ({ url }) => {
151
151
  }
152
152
  return {
153
153
  data: new Uint8Array(await response.arrayBuffer()),
154
- mediaType: (_a4 = response.headers.get("content-type")) != null ? _a4 : void 0
154
+ mediaType: (_a5 = response.headers.get("content-type")) != null ? _a5 : void 0
155
155
  };
156
156
  } catch (error) {
157
157
  if (DownloadError.isInstance(error)) {
@@ -200,8 +200,8 @@ var dataContentSchema = z.union([
200
200
  z.custom(
201
201
  // Buffer might not be available in some environments such as CloudFlare:
202
202
  (value) => {
203
- var _a4, _b;
204
- return (_b = (_a4 = globalThis.Buffer) == null ? void 0 : _a4.isBuffer(value)) != null ? _b : false;
203
+ var _a5, _b;
204
+ return (_b = (_a5 = globalThis.Buffer) == null ? void 0 : _a5.isBuffer(value)) != null ? _b : false;
205
205
  },
206
206
  { message: "Must be a Buffer" }
207
207
  )
@@ -260,6 +260,29 @@ function asArray(value) {
260
260
  return value === void 0 ? [] : Array.isArray(value) ? value : [value];
261
261
  }
262
262
 
263
+ // src/error/missing-tool-result-error.ts
264
+ import { AISDKError as AISDKError3 } from "@ai-sdk/provider";
265
+ var name2 = "AI_MissingToolResultsError";
266
+ var marker2 = `vercel.ai.error.${name2}`;
267
+ var symbol2 = Symbol.for(marker2);
268
+ var _a2;
269
+ var MissingToolResultsError = class extends AISDKError3 {
270
+ constructor({ toolCallIds }) {
271
+ super({
272
+ name: name2,
273
+ message: `Tool result${toolCallIds.length > 1 ? "s are" : " is"} missing for tool call${toolCallIds.length > 1 ? "s" : ""} ${toolCallIds.join(
274
+ ", "
275
+ )}.`
276
+ });
277
+ this[_a2] = true;
278
+ this.toolCallIds = toolCallIds;
279
+ }
280
+ static isInstance(error) {
281
+ return AISDKError3.hasMarker(error, marker2);
282
+ }
283
+ };
284
+ _a2 = symbol2;
285
+
263
286
  // src/prompt/convert-to-language-model-prompt.ts
264
287
  async function convertToLanguageModelPrompt({
265
288
  prompt,
@@ -271,6 +294,32 @@ async function convertToLanguageModelPrompt({
271
294
  download2,
272
295
  supportedUrls
273
296
  );
297
+ const approvalIdToToolCallId = /* @__PURE__ */ new Map();
298
+ for (const message of prompt.messages) {
299
+ if (message.role === "assistant" && Array.isArray(message.content)) {
300
+ for (const part of message.content) {
301
+ if (part.type === "tool-approval-request" && "approvalId" in part && "toolCallId" in part) {
302
+ approvalIdToToolCallId.set(
303
+ part.approvalId,
304
+ part.toolCallId
305
+ );
306
+ }
307
+ }
308
+ }
309
+ }
310
+ const approvedToolCallIds = /* @__PURE__ */ new Set();
311
+ for (const message of prompt.messages) {
312
+ if (message.role === "tool") {
313
+ for (const part of message.content) {
314
+ if (part.type === "tool-approval-response") {
315
+ const toolCallId = approvalIdToToolCallId.get(part.approvalId);
316
+ if (toolCallId) {
317
+ approvedToolCallIds.add(toolCallId);
318
+ }
319
+ }
320
+ }
321
+ }
322
+ }
274
323
  const messages = [
275
324
  ...prompt.system != null ? typeof prompt.system === "string" ? [{ role: "system", content: prompt.system }] : asArray(prompt.system).map((message) => ({
276
325
  role: "system",
@@ -294,7 +343,51 @@ async function convertToLanguageModelPrompt({
294
343
  combinedMessages.push(message);
295
344
  }
296
345
  }
297
- return combinedMessages;
346
+ const toolCallIds = /* @__PURE__ */ new Set();
347
+ for (const message of combinedMessages) {
348
+ switch (message.role) {
349
+ case "assistant": {
350
+ for (const content of message.content) {
351
+ if (content.type === "tool-call" && !content.providerExecuted) {
352
+ toolCallIds.add(content.toolCallId);
353
+ }
354
+ }
355
+ break;
356
+ }
357
+ case "tool": {
358
+ for (const content of message.content) {
359
+ if (content.type === "tool-result") {
360
+ toolCallIds.delete(content.toolCallId);
361
+ }
362
+ }
363
+ break;
364
+ }
365
+ case "user":
366
+ case "system":
367
+ for (const id of approvedToolCallIds) {
368
+ toolCallIds.delete(id);
369
+ }
370
+ if (toolCallIds.size > 0) {
371
+ throw new MissingToolResultsError({
372
+ toolCallIds: Array.from(toolCallIds)
373
+ });
374
+ }
375
+ break;
376
+ }
377
+ }
378
+ for (const id of approvedToolCallIds) {
379
+ toolCallIds.delete(id);
380
+ }
381
+ if (toolCallIds.size > 0) {
382
+ throw new MissingToolResultsError({ toolCallIds: Array.from(toolCallIds) });
383
+ }
384
+ return combinedMessages.filter(
385
+ // Filter out empty tool messages (e.g. if they only contained
386
+ // tool-approval-response parts that were removed).
387
+ // This prevents sending invalid empty messages to the provider.
388
+ // Note: provider-executed tool-approval-response parts are preserved.
389
+ (message) => message.role !== "tool" || message.content.length > 0
390
+ );
298
391
  }
299
392
  function convertToLanguageModelMessage({
300
393
  message,
@@ -433,8 +526,8 @@ async function downloadAssets(messages, download2, supportedUrls) {
433
526
  ).flat().filter(
434
527
  (part) => part.type === "image" || part.type === "file"
435
528
  ).map((part) => {
436
- var _a4;
437
- const mediaType = (_a4 = part.mediaType) != null ? _a4 : part.type === "image" ? "image/*" : void 0;
529
+ var _a5;
530
+ const mediaType = (_a5 = part.mediaType) != null ? _a5 : part.type === "image" ? "image/*" : void 0;
438
531
  let data = part.type === "image" ? part.image : part.data;
439
532
  if (typeof data === "string") {
440
533
  try {
@@ -464,7 +557,7 @@ async function downloadAssets(messages, download2, supportedUrls) {
464
557
  );
465
558
  }
466
559
  function convertPartToLanguageModelPart(part, downloadedAssets) {
467
- var _a4;
560
+ var _a5;
468
561
  if (part.type === "text") {
469
562
  return {
470
563
  type: "text",
@@ -497,7 +590,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
497
590
  switch (type) {
498
591
  case "image": {
499
592
  if (data instanceof Uint8Array || typeof data === "string") {
500
- mediaType = (_a4 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a4 : mediaType;
593
+ mediaType = (_a5 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a5 : mediaType;
501
594
  }
502
595
  return {
503
596
  type: "file",
@@ -569,10 +662,10 @@ async function prepareToolsAndToolChoice({
569
662
  };
570
663
  }
571
664
  const filteredTools = activeTools != null ? Object.entries(tools).filter(
572
- ([name4]) => activeTools.includes(name4)
665
+ ([name5]) => activeTools.includes(name5)
573
666
  ) : Object.entries(tools);
574
667
  const languageModelTools = [];
575
- for (const [name4, tool] of filteredTools) {
668
+ for (const [name5, tool] of filteredTools) {
576
669
  const toolType = tool.type;
577
670
  switch (toolType) {
578
671
  case void 0:
@@ -580,7 +673,7 @@ async function prepareToolsAndToolChoice({
580
673
  case "function":
581
674
  languageModelTools.push({
582
675
  type: "function",
583
- name: name4,
676
+ name: name5,
584
677
  description: tool.description,
585
678
  inputSchema: await asSchema(tool.inputSchema).jsonSchema,
586
679
  ...tool.inputExamples != null ? { inputExamples: tool.inputExamples } : {},
@@ -591,7 +684,7 @@ async function prepareToolsAndToolChoice({
591
684
  case "provider":
592
685
  languageModelTools.push({
593
686
  type: "provider",
594
- name: name4,
687
+ name: name5,
595
688
  id: tool.id,
596
689
  args: tool.args
597
690
  });
@@ -880,30 +973,30 @@ async function standardizePrompt(prompt) {
880
973
  }
881
974
 
882
975
  // src/error/invalid-argument-error.ts
883
- import { AISDKError as AISDKError3 } from "@ai-sdk/provider";
884
- var name2 = "AI_InvalidArgumentError";
885
- var marker2 = `vercel.ai.error.${name2}`;
886
- var symbol2 = Symbol.for(marker2);
887
- var _a2;
888
- var InvalidArgumentError = class extends AISDKError3 {
976
+ import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
977
+ var name3 = "AI_InvalidArgumentError";
978
+ var marker3 = `vercel.ai.error.${name3}`;
979
+ var symbol3 = Symbol.for(marker3);
980
+ var _a3;
981
+ var InvalidArgumentError = class extends AISDKError4 {
889
982
  constructor({
890
983
  parameter,
891
984
  value,
892
985
  message
893
986
  }) {
894
987
  super({
895
- name: name2,
988
+ name: name3,
896
989
  message: `Invalid argument for parameter ${parameter}: ${message}`
897
990
  });
898
- this[_a2] = true;
991
+ this[_a3] = true;
899
992
  this.parameter = parameter;
900
993
  this.value = value;
901
994
  }
902
995
  static isInstance(error) {
903
- return AISDKError3.hasMarker(error, marker2);
996
+ return AISDKError4.hasMarker(error, marker3);
904
997
  }
905
998
  };
906
- _a2 = symbol2;
999
+ _a3 = symbol3;
907
1000
 
908
1001
  // src/prompt/prepare-call-settings.ts
909
1002
  function prepareCallSettings({
@@ -1003,28 +1096,28 @@ import { APICallError } from "@ai-sdk/provider";
1003
1096
  import { delay, getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
1004
1097
 
1005
1098
  // src/util/retry-error.ts
1006
- import { AISDKError as AISDKError4 } from "@ai-sdk/provider";
1007
- var name3 = "AI_RetryError";
1008
- var marker3 = `vercel.ai.error.${name3}`;
1009
- var symbol3 = Symbol.for(marker3);
1010
- var _a3;
1011
- var RetryError = class extends AISDKError4 {
1099
+ import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
1100
+ var name4 = "AI_RetryError";
1101
+ var marker4 = `vercel.ai.error.${name4}`;
1102
+ var symbol4 = Symbol.for(marker4);
1103
+ var _a4;
1104
+ var RetryError = class extends AISDKError5 {
1012
1105
  constructor({
1013
1106
  message,
1014
1107
  reason,
1015
1108
  errors
1016
1109
  }) {
1017
- super({ name: name3, message });
1018
- this[_a3] = true;
1110
+ super({ name: name4, message });
1111
+ this[_a4] = true;
1019
1112
  this.reason = reason;
1020
1113
  this.errors = errors;
1021
1114
  this.lastError = errors[errors.length - 1];
1022
1115
  }
1023
1116
  static isInstance(error) {
1024
- return AISDKError4.hasMarker(error, marker3);
1117
+ return AISDKError5.hasMarker(error, marker4);
1025
1118
  }
1026
1119
  };
1027
- _a3 = symbol3;
1120
+ _a4 = symbol4;
1028
1121
 
1029
1122
  // src/util/retry-with-exponential-backoff.ts
1030
1123
  function getRetryDelayInMs({