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.
- package/CHANGELOG.md +11 -0
- package/dist/index.d.mts +23 -13
- package/dist/index.d.ts +23 -13
- package/dist/index.js +450 -355
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +381 -287
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +138 -45
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +127 -34
- package/dist/internal/index.mjs.map +1 -1
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +1 -1
- package/docs/09-troubleshooting/21-missing-tool-results-error.mdx +82 -0
- package/package.json +1 -1
- package/src/error/index.ts +1 -1
- package/src/error/missing-tool-result-error.ts +28 -0
- package/src/prompt/__snapshots__/convert-to-language-model-prompt.validation.test.ts.snap +76 -0
- package/src/prompt/convert-to-language-model-prompt.ts +85 -1
- package/src/prompt/convert-to-language-model-prompt.validation.test.ts +138 -0
package/dist/internal/index.js
CHANGED
|
@@ -4,8 +4,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
6
|
var __export = (target, all) => {
|
|
7
|
-
for (var
|
|
8
|
-
__defProp(target,
|
|
7
|
+
for (var name5 in all)
|
|
8
|
+
__defProp(target, name5, { get: all[name5], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -153,11 +153,11 @@ var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
|
153
153
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
154
154
|
|
|
155
155
|
// src/version.ts
|
|
156
|
-
var VERSION = true ? "6.0.
|
|
156
|
+
var VERSION = true ? "6.0.43" : "0.0.0-test";
|
|
157
157
|
|
|
158
158
|
// src/util/download/download.ts
|
|
159
159
|
var download = async ({ url }) => {
|
|
160
|
-
var
|
|
160
|
+
var _a5;
|
|
161
161
|
const urlText = url.toString();
|
|
162
162
|
try {
|
|
163
163
|
const response = await fetch(urlText, {
|
|
@@ -176,7 +176,7 @@ var download = async ({ url }) => {
|
|
|
176
176
|
}
|
|
177
177
|
return {
|
|
178
178
|
data: new Uint8Array(await response.arrayBuffer()),
|
|
179
|
-
mediaType: (
|
|
179
|
+
mediaType: (_a5 = response.headers.get("content-type")) != null ? _a5 : void 0
|
|
180
180
|
};
|
|
181
181
|
} catch (error) {
|
|
182
182
|
if (import_provider_utils2.DownloadError.isInstance(error)) {
|
|
@@ -222,8 +222,8 @@ var dataContentSchema = import_v4.z.union([
|
|
|
222
222
|
import_v4.z.custom(
|
|
223
223
|
// Buffer might not be available in some environments such as CloudFlare:
|
|
224
224
|
(value) => {
|
|
225
|
-
var
|
|
226
|
-
return (_b = (
|
|
225
|
+
var _a5, _b;
|
|
226
|
+
return (_b = (_a5 = globalThis.Buffer) == null ? void 0 : _a5.isBuffer(value)) != null ? _b : false;
|
|
227
227
|
},
|
|
228
228
|
{ message: "Must be a Buffer" }
|
|
229
229
|
)
|
|
@@ -282,6 +282,29 @@ function asArray(value) {
|
|
|
282
282
|
return value === void 0 ? [] : Array.isArray(value) ? value : [value];
|
|
283
283
|
}
|
|
284
284
|
|
|
285
|
+
// src/error/missing-tool-result-error.ts
|
|
286
|
+
var import_provider3 = require("@ai-sdk/provider");
|
|
287
|
+
var name2 = "AI_MissingToolResultsError";
|
|
288
|
+
var marker2 = `vercel.ai.error.${name2}`;
|
|
289
|
+
var symbol2 = Symbol.for(marker2);
|
|
290
|
+
var _a2;
|
|
291
|
+
var MissingToolResultsError = class extends import_provider3.AISDKError {
|
|
292
|
+
constructor({ toolCallIds }) {
|
|
293
|
+
super({
|
|
294
|
+
name: name2,
|
|
295
|
+
message: `Tool result${toolCallIds.length > 1 ? "s are" : " is"} missing for tool call${toolCallIds.length > 1 ? "s" : ""} ${toolCallIds.join(
|
|
296
|
+
", "
|
|
297
|
+
)}.`
|
|
298
|
+
});
|
|
299
|
+
this[_a2] = true;
|
|
300
|
+
this.toolCallIds = toolCallIds;
|
|
301
|
+
}
|
|
302
|
+
static isInstance(error) {
|
|
303
|
+
return import_provider3.AISDKError.hasMarker(error, marker2);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
_a2 = symbol2;
|
|
307
|
+
|
|
285
308
|
// src/prompt/convert-to-language-model-prompt.ts
|
|
286
309
|
async function convertToLanguageModelPrompt({
|
|
287
310
|
prompt,
|
|
@@ -293,6 +316,32 @@ async function convertToLanguageModelPrompt({
|
|
|
293
316
|
download2,
|
|
294
317
|
supportedUrls
|
|
295
318
|
);
|
|
319
|
+
const approvalIdToToolCallId = /* @__PURE__ */ new Map();
|
|
320
|
+
for (const message of prompt.messages) {
|
|
321
|
+
if (message.role === "assistant" && Array.isArray(message.content)) {
|
|
322
|
+
for (const part of message.content) {
|
|
323
|
+
if (part.type === "tool-approval-request" && "approvalId" in part && "toolCallId" in part) {
|
|
324
|
+
approvalIdToToolCallId.set(
|
|
325
|
+
part.approvalId,
|
|
326
|
+
part.toolCallId
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const approvedToolCallIds = /* @__PURE__ */ new Set();
|
|
333
|
+
for (const message of prompt.messages) {
|
|
334
|
+
if (message.role === "tool") {
|
|
335
|
+
for (const part of message.content) {
|
|
336
|
+
if (part.type === "tool-approval-response") {
|
|
337
|
+
const toolCallId = approvalIdToToolCallId.get(part.approvalId);
|
|
338
|
+
if (toolCallId) {
|
|
339
|
+
approvedToolCallIds.add(toolCallId);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
296
345
|
const messages = [
|
|
297
346
|
...prompt.system != null ? typeof prompt.system === "string" ? [{ role: "system", content: prompt.system }] : asArray(prompt.system).map((message) => ({
|
|
298
347
|
role: "system",
|
|
@@ -316,7 +365,51 @@ async function convertToLanguageModelPrompt({
|
|
|
316
365
|
combinedMessages.push(message);
|
|
317
366
|
}
|
|
318
367
|
}
|
|
319
|
-
|
|
368
|
+
const toolCallIds = /* @__PURE__ */ new Set();
|
|
369
|
+
for (const message of combinedMessages) {
|
|
370
|
+
switch (message.role) {
|
|
371
|
+
case "assistant": {
|
|
372
|
+
for (const content of message.content) {
|
|
373
|
+
if (content.type === "tool-call" && !content.providerExecuted) {
|
|
374
|
+
toolCallIds.add(content.toolCallId);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
case "tool": {
|
|
380
|
+
for (const content of message.content) {
|
|
381
|
+
if (content.type === "tool-result") {
|
|
382
|
+
toolCallIds.delete(content.toolCallId);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
case "user":
|
|
388
|
+
case "system":
|
|
389
|
+
for (const id of approvedToolCallIds) {
|
|
390
|
+
toolCallIds.delete(id);
|
|
391
|
+
}
|
|
392
|
+
if (toolCallIds.size > 0) {
|
|
393
|
+
throw new MissingToolResultsError({
|
|
394
|
+
toolCallIds: Array.from(toolCallIds)
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
for (const id of approvedToolCallIds) {
|
|
401
|
+
toolCallIds.delete(id);
|
|
402
|
+
}
|
|
403
|
+
if (toolCallIds.size > 0) {
|
|
404
|
+
throw new MissingToolResultsError({ toolCallIds: Array.from(toolCallIds) });
|
|
405
|
+
}
|
|
406
|
+
return combinedMessages.filter(
|
|
407
|
+
// Filter out empty tool messages (e.g. if they only contained
|
|
408
|
+
// tool-approval-response parts that were removed).
|
|
409
|
+
// This prevents sending invalid empty messages to the provider.
|
|
410
|
+
// Note: provider-executed tool-approval-response parts are preserved.
|
|
411
|
+
(message) => message.role !== "tool" || message.content.length > 0
|
|
412
|
+
);
|
|
320
413
|
}
|
|
321
414
|
function convertToLanguageModelMessage({
|
|
322
415
|
message,
|
|
@@ -455,8 +548,8 @@ async function downloadAssets(messages, download2, supportedUrls) {
|
|
|
455
548
|
).flat().filter(
|
|
456
549
|
(part) => part.type === "image" || part.type === "file"
|
|
457
550
|
).map((part) => {
|
|
458
|
-
var
|
|
459
|
-
const mediaType = (
|
|
551
|
+
var _a5;
|
|
552
|
+
const mediaType = (_a5 = part.mediaType) != null ? _a5 : part.type === "image" ? "image/*" : void 0;
|
|
460
553
|
let data = part.type === "image" ? part.image : part.data;
|
|
461
554
|
if (typeof data === "string") {
|
|
462
555
|
try {
|
|
@@ -486,7 +579,7 @@ async function downloadAssets(messages, download2, supportedUrls) {
|
|
|
486
579
|
);
|
|
487
580
|
}
|
|
488
581
|
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
489
|
-
var
|
|
582
|
+
var _a5;
|
|
490
583
|
if (part.type === "text") {
|
|
491
584
|
return {
|
|
492
585
|
type: "text",
|
|
@@ -519,7 +612,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
519
612
|
switch (type) {
|
|
520
613
|
case "image": {
|
|
521
614
|
if (data instanceof Uint8Array || typeof data === "string") {
|
|
522
|
-
mediaType = (
|
|
615
|
+
mediaType = (_a5 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a5 : mediaType;
|
|
523
616
|
}
|
|
524
617
|
return {
|
|
525
618
|
type: "file",
|
|
@@ -591,10 +684,10 @@ async function prepareToolsAndToolChoice({
|
|
|
591
684
|
};
|
|
592
685
|
}
|
|
593
686
|
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
|
594
|
-
([
|
|
687
|
+
([name5]) => activeTools.includes(name5)
|
|
595
688
|
) : Object.entries(tools);
|
|
596
689
|
const languageModelTools = [];
|
|
597
|
-
for (const [
|
|
690
|
+
for (const [name5, tool] of filteredTools) {
|
|
598
691
|
const toolType = tool.type;
|
|
599
692
|
switch (toolType) {
|
|
600
693
|
case void 0:
|
|
@@ -602,7 +695,7 @@ async function prepareToolsAndToolChoice({
|
|
|
602
695
|
case "function":
|
|
603
696
|
languageModelTools.push({
|
|
604
697
|
type: "function",
|
|
605
|
-
name:
|
|
698
|
+
name: name5,
|
|
606
699
|
description: tool.description,
|
|
607
700
|
inputSchema: await (0, import_provider_utils6.asSchema)(tool.inputSchema).jsonSchema,
|
|
608
701
|
...tool.inputExamples != null ? { inputExamples: tool.inputExamples } : {},
|
|
@@ -613,7 +706,7 @@ async function prepareToolsAndToolChoice({
|
|
|
613
706
|
case "provider":
|
|
614
707
|
languageModelTools.push({
|
|
615
708
|
type: "provider",
|
|
616
|
-
name:
|
|
709
|
+
name: name5,
|
|
617
710
|
id: tool.id,
|
|
618
711
|
args: tool.args
|
|
619
712
|
});
|
|
@@ -631,7 +724,7 @@ async function prepareToolsAndToolChoice({
|
|
|
631
724
|
}
|
|
632
725
|
|
|
633
726
|
// src/prompt/standardize-prompt.ts
|
|
634
|
-
var
|
|
727
|
+
var import_provider4 = require("@ai-sdk/provider");
|
|
635
728
|
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
|
636
729
|
var import_v46 = require("zod/v4");
|
|
637
730
|
|
|
@@ -844,13 +937,13 @@ var modelMessageSchema = import_v45.z.union([
|
|
|
844
937
|
// src/prompt/standardize-prompt.ts
|
|
845
938
|
async function standardizePrompt(prompt) {
|
|
846
939
|
if (prompt.prompt == null && prompt.messages == null) {
|
|
847
|
-
throw new
|
|
940
|
+
throw new import_provider4.InvalidPromptError({
|
|
848
941
|
prompt,
|
|
849
942
|
message: "prompt or messages must be defined"
|
|
850
943
|
});
|
|
851
944
|
}
|
|
852
945
|
if (prompt.prompt != null && prompt.messages != null) {
|
|
853
|
-
throw new
|
|
946
|
+
throw new import_provider4.InvalidPromptError({
|
|
854
947
|
prompt,
|
|
855
948
|
message: "prompt and messages cannot be defined at the same time"
|
|
856
949
|
});
|
|
@@ -858,7 +951,7 @@ async function standardizePrompt(prompt) {
|
|
|
858
951
|
if (prompt.system != null && typeof prompt.system !== "string" && !asArray(prompt.system).every(
|
|
859
952
|
(message) => typeof message === "object" && message !== null && "role" in message && message.role === "system"
|
|
860
953
|
)) {
|
|
861
|
-
throw new
|
|
954
|
+
throw new import_provider4.InvalidPromptError({
|
|
862
955
|
prompt,
|
|
863
956
|
message: "system must be a string, SystemModelMessage, or array of SystemModelMessage"
|
|
864
957
|
});
|
|
@@ -871,13 +964,13 @@ async function standardizePrompt(prompt) {
|
|
|
871
964
|
} else if (prompt.messages != null) {
|
|
872
965
|
messages = prompt.messages;
|
|
873
966
|
} else {
|
|
874
|
-
throw new
|
|
967
|
+
throw new import_provider4.InvalidPromptError({
|
|
875
968
|
prompt,
|
|
876
969
|
message: "prompt or messages must be defined"
|
|
877
970
|
});
|
|
878
971
|
}
|
|
879
972
|
if (messages.length === 0) {
|
|
880
|
-
throw new
|
|
973
|
+
throw new import_provider4.InvalidPromptError({
|
|
881
974
|
prompt,
|
|
882
975
|
message: "messages must not be empty"
|
|
883
976
|
});
|
|
@@ -887,7 +980,7 @@ async function standardizePrompt(prompt) {
|
|
|
887
980
|
schema: import_v46.z.array(modelMessageSchema)
|
|
888
981
|
});
|
|
889
982
|
if (!validationResult.success) {
|
|
890
|
-
throw new
|
|
983
|
+
throw new import_provider4.InvalidPromptError({
|
|
891
984
|
prompt,
|
|
892
985
|
message: "The messages do not match the ModelMessage[] schema.",
|
|
893
986
|
cause: validationResult.error
|
|
@@ -900,30 +993,30 @@ async function standardizePrompt(prompt) {
|
|
|
900
993
|
}
|
|
901
994
|
|
|
902
995
|
// src/error/invalid-argument-error.ts
|
|
903
|
-
var
|
|
904
|
-
var
|
|
905
|
-
var
|
|
906
|
-
var
|
|
907
|
-
var
|
|
908
|
-
var InvalidArgumentError = class extends
|
|
996
|
+
var import_provider5 = require("@ai-sdk/provider");
|
|
997
|
+
var name3 = "AI_InvalidArgumentError";
|
|
998
|
+
var marker3 = `vercel.ai.error.${name3}`;
|
|
999
|
+
var symbol3 = Symbol.for(marker3);
|
|
1000
|
+
var _a3;
|
|
1001
|
+
var InvalidArgumentError = class extends import_provider5.AISDKError {
|
|
909
1002
|
constructor({
|
|
910
1003
|
parameter,
|
|
911
1004
|
value,
|
|
912
1005
|
message
|
|
913
1006
|
}) {
|
|
914
1007
|
super({
|
|
915
|
-
name:
|
|
1008
|
+
name: name3,
|
|
916
1009
|
message: `Invalid argument for parameter ${parameter}: ${message}`
|
|
917
1010
|
});
|
|
918
|
-
this[
|
|
1011
|
+
this[_a3] = true;
|
|
919
1012
|
this.parameter = parameter;
|
|
920
1013
|
this.value = value;
|
|
921
1014
|
}
|
|
922
1015
|
static isInstance(error) {
|
|
923
|
-
return
|
|
1016
|
+
return import_provider5.AISDKError.hasMarker(error, marker3);
|
|
924
1017
|
}
|
|
925
1018
|
};
|
|
926
|
-
|
|
1019
|
+
_a3 = symbol3;
|
|
927
1020
|
|
|
928
1021
|
// src/prompt/prepare-call-settings.ts
|
|
929
1022
|
function prepareCallSettings({
|
|
@@ -1019,32 +1112,32 @@ function prepareCallSettings({
|
|
|
1019
1112
|
}
|
|
1020
1113
|
|
|
1021
1114
|
// src/util/retry-with-exponential-backoff.ts
|
|
1022
|
-
var
|
|
1115
|
+
var import_provider7 = require("@ai-sdk/provider");
|
|
1023
1116
|
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
1024
1117
|
|
|
1025
1118
|
// src/util/retry-error.ts
|
|
1026
|
-
var
|
|
1027
|
-
var
|
|
1028
|
-
var
|
|
1029
|
-
var
|
|
1030
|
-
var
|
|
1031
|
-
var RetryError = class extends
|
|
1119
|
+
var import_provider6 = require("@ai-sdk/provider");
|
|
1120
|
+
var name4 = "AI_RetryError";
|
|
1121
|
+
var marker4 = `vercel.ai.error.${name4}`;
|
|
1122
|
+
var symbol4 = Symbol.for(marker4);
|
|
1123
|
+
var _a4;
|
|
1124
|
+
var RetryError = class extends import_provider6.AISDKError {
|
|
1032
1125
|
constructor({
|
|
1033
1126
|
message,
|
|
1034
1127
|
reason,
|
|
1035
1128
|
errors
|
|
1036
1129
|
}) {
|
|
1037
|
-
super({ name:
|
|
1038
|
-
this[
|
|
1130
|
+
super({ name: name4, message });
|
|
1131
|
+
this[_a4] = true;
|
|
1039
1132
|
this.reason = reason;
|
|
1040
1133
|
this.errors = errors;
|
|
1041
1134
|
this.lastError = errors[errors.length - 1];
|
|
1042
1135
|
}
|
|
1043
1136
|
static isInstance(error) {
|
|
1044
|
-
return
|
|
1137
|
+
return import_provider6.AISDKError.hasMarker(error, marker4);
|
|
1045
1138
|
}
|
|
1046
1139
|
};
|
|
1047
|
-
|
|
1140
|
+
_a4 = symbol4;
|
|
1048
1141
|
|
|
1049
1142
|
// src/util/retry-with-exponential-backoff.ts
|
|
1050
1143
|
function getRetryDelayInMs({
|
|
@@ -1112,7 +1205,7 @@ async function _retryWithExponentialBackoff(f, {
|
|
|
1112
1205
|
errors: newErrors
|
|
1113
1206
|
});
|
|
1114
1207
|
}
|
|
1115
|
-
if (error instanceof Error &&
|
|
1208
|
+
if (error instanceof Error && import_provider7.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
|
1116
1209
|
await (0, import_provider_utils8.delay)(
|
|
1117
1210
|
getRetryDelayInMs({
|
|
1118
1211
|
error,
|