ai 5.0.0-canary.9 → 5.0.1
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 +1312 -0
- package/README.md +5 -5
- package/dist/index.d.mts +3049 -3827
- package/dist/index.d.ts +3049 -3827
- package/dist/index.js +8598 -8499
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +8703 -8554
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +49 -613
- package/dist/internal/index.d.ts +49 -613
- package/dist/internal/index.js +702 -1133
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +688 -1104
- package/dist/internal/index.mjs.map +1 -1
- package/dist/mcp-stdio/index.d.mts +16 -96
- package/dist/mcp-stdio/index.d.ts +16 -96
- package/dist/mcp-stdio/index.js +90 -93
- package/dist/mcp-stdio/index.js.map +1 -1
- package/dist/mcp-stdio/index.mjs +33 -48
- package/dist/mcp-stdio/index.mjs.map +1 -1
- package/dist/test/index.d.mts +64 -12
- package/dist/test/index.d.ts +64 -12
- package/dist/test/index.js +137 -13
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +135 -13
- package/dist/test/index.mjs.map +1 -1
- package/internal.d.ts +1 -0
- package/mcp-stdio.d.ts +1 -0
- package/package.json +30 -18
- package/test.d.ts +1 -0
package/dist/internal/index.js
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __create = Object.create;
|
3
2
|
var __defProp = Object.defineProperty;
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
6
|
var __export = (target, all) => {
|
9
|
-
for (var
|
10
|
-
__defProp(target,
|
7
|
+
for (var name5 in all)
|
8
|
+
__defProp(target, name5, { get: all[name5], enumerable: true });
|
11
9
|
};
|
12
10
|
var __copyProps = (to, from, except, desc) => {
|
13
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
@@ -17,21 +15,12 @@ var __copyProps = (to, from, except, desc) => {
|
|
17
15
|
}
|
18
16
|
return to;
|
19
17
|
};
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
-
mod
|
27
|
-
));
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
19
|
|
30
20
|
// internal/index.ts
|
31
21
|
var internal_exports = {};
|
32
22
|
__export(internal_exports, {
|
33
|
-
|
34
|
-
calculateLanguageModelUsage: () => calculateLanguageModelUsage,
|
23
|
+
convertAsyncIteratorToReadableStream: () => import_provider_utils7.convertAsyncIteratorToReadableStream,
|
35
24
|
convertToLanguageModelPrompt: () => convertToLanguageModelPrompt,
|
36
25
|
prepareCallSettings: () => prepareCallSettings,
|
37
26
|
prepareRetries: () => prepareRetries,
|
@@ -39,32 +28,131 @@ __export(internal_exports, {
|
|
39
28
|
standardizePrompt: () => standardizePrompt
|
40
29
|
});
|
41
30
|
module.exports = __toCommonJS(internal_exports);
|
31
|
+
var import_provider_utils7 = require("@ai-sdk/provider-utils");
|
42
32
|
|
43
|
-
//
|
44
|
-
var
|
45
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
46
|
-
var import_zod7 = require("zod");
|
33
|
+
// src/prompt/convert-to-language-model-prompt.ts
|
34
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
47
35
|
|
48
|
-
//
|
49
|
-
var import_provider2 = require("@ai-sdk/provider");
|
36
|
+
// src/util/detect-media-type.ts
|
50
37
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
51
|
-
var
|
38
|
+
var imageMediaTypeSignatures = [
|
39
|
+
{
|
40
|
+
mediaType: "image/gif",
|
41
|
+
bytesPrefix: [71, 73, 70],
|
42
|
+
base64Prefix: "R0lG"
|
43
|
+
},
|
44
|
+
{
|
45
|
+
mediaType: "image/png",
|
46
|
+
bytesPrefix: [137, 80, 78, 71],
|
47
|
+
base64Prefix: "iVBORw"
|
48
|
+
},
|
49
|
+
{
|
50
|
+
mediaType: "image/jpeg",
|
51
|
+
bytesPrefix: [255, 216],
|
52
|
+
base64Prefix: "/9j/"
|
53
|
+
},
|
54
|
+
{
|
55
|
+
mediaType: "image/webp",
|
56
|
+
bytesPrefix: [82, 73, 70, 70],
|
57
|
+
base64Prefix: "UklGRg"
|
58
|
+
},
|
59
|
+
{
|
60
|
+
mediaType: "image/bmp",
|
61
|
+
bytesPrefix: [66, 77],
|
62
|
+
base64Prefix: "Qk"
|
63
|
+
},
|
64
|
+
{
|
65
|
+
mediaType: "image/tiff",
|
66
|
+
bytesPrefix: [73, 73, 42, 0],
|
67
|
+
base64Prefix: "SUkqAA"
|
68
|
+
},
|
69
|
+
{
|
70
|
+
mediaType: "image/tiff",
|
71
|
+
bytesPrefix: [77, 77, 0, 42],
|
72
|
+
base64Prefix: "TU0AKg"
|
73
|
+
},
|
74
|
+
{
|
75
|
+
mediaType: "image/avif",
|
76
|
+
bytesPrefix: [
|
77
|
+
0,
|
78
|
+
0,
|
79
|
+
0,
|
80
|
+
32,
|
81
|
+
102,
|
82
|
+
116,
|
83
|
+
121,
|
84
|
+
112,
|
85
|
+
97,
|
86
|
+
118,
|
87
|
+
105,
|
88
|
+
102
|
89
|
+
],
|
90
|
+
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
91
|
+
},
|
92
|
+
{
|
93
|
+
mediaType: "image/heic",
|
94
|
+
bytesPrefix: [
|
95
|
+
0,
|
96
|
+
0,
|
97
|
+
0,
|
98
|
+
32,
|
99
|
+
102,
|
100
|
+
116,
|
101
|
+
121,
|
102
|
+
112,
|
103
|
+
104,
|
104
|
+
101,
|
105
|
+
105,
|
106
|
+
99
|
107
|
+
],
|
108
|
+
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
109
|
+
}
|
110
|
+
];
|
111
|
+
var stripID3 = (data) => {
|
112
|
+
const bytes = typeof data === "string" ? (0, import_provider_utils.convertBase64ToUint8Array)(data) : data;
|
113
|
+
const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
|
114
|
+
return bytes.slice(id3Size + 10);
|
115
|
+
};
|
116
|
+
function stripID3TagsIfPresent(data) {
|
117
|
+
const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
|
118
|
+
data[1] === 68 && // 'D'
|
119
|
+
data[2] === 51;
|
120
|
+
return hasId3 ? stripID3(data) : data;
|
121
|
+
}
|
122
|
+
function detectMediaType({
|
123
|
+
data,
|
124
|
+
signatures
|
125
|
+
}) {
|
126
|
+
const processedData = stripID3TagsIfPresent(data);
|
127
|
+
for (const signature of signatures) {
|
128
|
+
if (typeof processedData === "string" ? processedData.startsWith(signature.base64Prefix) : processedData.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
|
129
|
+
(byte, index) => processedData[index] === byte
|
130
|
+
)) {
|
131
|
+
return signature.mediaType;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
return void 0;
|
135
|
+
}
|
52
136
|
|
53
|
-
//
|
137
|
+
// src/util/download-error.ts
|
54
138
|
var import_provider = require("@ai-sdk/provider");
|
55
|
-
var name = "
|
139
|
+
var name = "AI_DownloadError";
|
56
140
|
var marker = `vercel.ai.error.${name}`;
|
57
141
|
var symbol = Symbol.for(marker);
|
58
142
|
var _a;
|
59
|
-
var
|
143
|
+
var DownloadError = class extends import_provider.AISDKError {
|
60
144
|
constructor({
|
61
|
-
|
145
|
+
url,
|
146
|
+
statusCode,
|
147
|
+
statusText,
|
62
148
|
cause,
|
63
|
-
message =
|
149
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
64
150
|
}) {
|
65
151
|
super({ name, message, cause });
|
66
152
|
this[_a] = true;
|
67
|
-
this.
|
153
|
+
this.url = url;
|
154
|
+
this.statusCode = statusCode;
|
155
|
+
this.statusText = statusText;
|
68
156
|
}
|
69
157
|
static isInstance(error) {
|
70
158
|
return import_provider.AISDKError.hasMarker(error, marker);
|
@@ -72,7 +160,37 @@ var InvalidDataContentError = class extends import_provider.AISDKError {
|
|
72
160
|
};
|
73
161
|
_a = symbol;
|
74
162
|
|
75
|
-
//
|
163
|
+
// src/util/download.ts
|
164
|
+
async function download({ url }) {
|
165
|
+
var _a5;
|
166
|
+
const urlText = url.toString();
|
167
|
+
try {
|
168
|
+
const response = await fetch(urlText);
|
169
|
+
if (!response.ok) {
|
170
|
+
throw new DownloadError({
|
171
|
+
url: urlText,
|
172
|
+
statusCode: response.status,
|
173
|
+
statusText: response.statusText
|
174
|
+
});
|
175
|
+
}
|
176
|
+
return {
|
177
|
+
data: new Uint8Array(await response.arrayBuffer()),
|
178
|
+
mediaType: (_a5 = response.headers.get("content-type")) != null ? _a5 : void 0
|
179
|
+
};
|
180
|
+
} catch (error) {
|
181
|
+
if (DownloadError.isInstance(error)) {
|
182
|
+
throw error;
|
183
|
+
}
|
184
|
+
throw new DownloadError({ url: urlText, cause: error });
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
// src/prompt/data-content.ts
|
189
|
+
var import_provider2 = require("@ai-sdk/provider");
|
190
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
191
|
+
var import_v4 = require("zod/v4");
|
192
|
+
|
193
|
+
// src/prompt/split-data-url.ts
|
76
194
|
function splitDataUrl(dataUrl) {
|
77
195
|
try {
|
78
196
|
const [header, base64Content] = dataUrl.split(",");
|
@@ -88,16 +206,16 @@ function splitDataUrl(dataUrl) {
|
|
88
206
|
}
|
89
207
|
}
|
90
208
|
|
91
|
-
//
|
92
|
-
var dataContentSchema =
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
209
|
+
// src/prompt/data-content.ts
|
210
|
+
var dataContentSchema = import_v4.z.union([
|
211
|
+
import_v4.z.string(),
|
212
|
+
import_v4.z.instanceof(Uint8Array),
|
213
|
+
import_v4.z.instanceof(ArrayBuffer),
|
214
|
+
import_v4.z.custom(
|
97
215
|
// Buffer might not be available in some environments such as CloudFlare:
|
98
216
|
(value) => {
|
99
|
-
var
|
100
|
-
return (_b = (
|
217
|
+
var _a5, _b;
|
218
|
+
return (_b = (_a5 = globalThis.Buffer) == null ? void 0 : _a5.isBuffer(value)) != null ? _b : false;
|
101
219
|
},
|
102
220
|
{ message: "Must be a Buffer" }
|
103
221
|
)
|
@@ -129,125 +247,21 @@ function convertToLanguageModelV2DataContent(content) {
|
|
129
247
|
}
|
130
248
|
return { data: content, mediaType: void 0 };
|
131
249
|
}
|
132
|
-
function convertDataContentToUint8Array(content) {
|
133
|
-
if (content instanceof Uint8Array) {
|
134
|
-
return content;
|
135
|
-
}
|
136
|
-
if (typeof content === "string") {
|
137
|
-
try {
|
138
|
-
return (0, import_provider_utils.convertBase64ToUint8Array)(content);
|
139
|
-
} catch (error) {
|
140
|
-
throw new InvalidDataContentError({
|
141
|
-
message: "Invalid data content. Content string is not a base64-encoded media.",
|
142
|
-
content,
|
143
|
-
cause: error
|
144
|
-
});
|
145
|
-
}
|
146
|
-
}
|
147
|
-
if (content instanceof ArrayBuffer) {
|
148
|
-
return new Uint8Array(content);
|
149
|
-
}
|
150
|
-
throw new InvalidDataContentError({ content });
|
151
|
-
}
|
152
|
-
function convertUint8ArrayToText(uint8Array) {
|
153
|
-
try {
|
154
|
-
return new TextDecoder().decode(uint8Array);
|
155
|
-
} catch (error) {
|
156
|
-
throw new Error("Error decoding Uint8Array to text");
|
157
|
-
}
|
158
|
-
}
|
159
|
-
|
160
|
-
// core/prompt/attachments-to-parts.ts
|
161
|
-
function attachmentsToParts(attachments) {
|
162
|
-
var _a7, _b, _c;
|
163
|
-
const parts = [];
|
164
|
-
for (const attachment of attachments) {
|
165
|
-
let url;
|
166
|
-
try {
|
167
|
-
url = new URL(attachment.url);
|
168
|
-
} catch (error) {
|
169
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
170
|
-
}
|
171
|
-
switch (url.protocol) {
|
172
|
-
case "http:":
|
173
|
-
case "https:": {
|
174
|
-
if ((_a7 = attachment.contentType) == null ? void 0 : _a7.startsWith("image/")) {
|
175
|
-
parts.push({ type: "image", image: url });
|
176
|
-
} else {
|
177
|
-
if (!attachment.contentType) {
|
178
|
-
throw new Error(
|
179
|
-
"If the attachment is not an image, it must specify a content type"
|
180
|
-
);
|
181
|
-
}
|
182
|
-
parts.push({
|
183
|
-
type: "file",
|
184
|
-
data: url,
|
185
|
-
mediaType: attachment.contentType
|
186
|
-
});
|
187
|
-
}
|
188
|
-
break;
|
189
|
-
}
|
190
|
-
case "data:": {
|
191
|
-
let header;
|
192
|
-
let base64Content;
|
193
|
-
let mediaType;
|
194
|
-
try {
|
195
|
-
[header, base64Content] = attachment.url.split(",");
|
196
|
-
mediaType = header.split(";")[0].split(":")[1];
|
197
|
-
} catch (error) {
|
198
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
199
|
-
}
|
200
|
-
if (mediaType == null || base64Content == null) {
|
201
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
202
|
-
}
|
203
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
204
|
-
parts.push({
|
205
|
-
type: "image",
|
206
|
-
image: convertDataContentToUint8Array(base64Content)
|
207
|
-
});
|
208
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
209
|
-
parts.push({
|
210
|
-
type: "text",
|
211
|
-
text: convertUint8ArrayToText(
|
212
|
-
convertDataContentToUint8Array(base64Content)
|
213
|
-
)
|
214
|
-
});
|
215
|
-
} else {
|
216
|
-
if (!attachment.contentType) {
|
217
|
-
throw new Error(
|
218
|
-
"If the attachment is not an image or text, it must specify a content type"
|
219
|
-
);
|
220
|
-
}
|
221
|
-
parts.push({
|
222
|
-
type: "file",
|
223
|
-
data: base64Content,
|
224
|
-
mediaType: attachment.contentType
|
225
|
-
});
|
226
|
-
}
|
227
|
-
break;
|
228
|
-
}
|
229
|
-
default: {
|
230
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
231
|
-
}
|
232
|
-
}
|
233
|
-
}
|
234
|
-
return parts;
|
235
|
-
}
|
236
250
|
|
237
|
-
//
|
251
|
+
// src/prompt/invalid-message-role-error.ts
|
238
252
|
var import_provider3 = require("@ai-sdk/provider");
|
239
|
-
var name2 = "
|
253
|
+
var name2 = "AI_InvalidMessageRoleError";
|
240
254
|
var marker2 = `vercel.ai.error.${name2}`;
|
241
255
|
var symbol2 = Symbol.for(marker2);
|
242
256
|
var _a2;
|
243
|
-
var
|
257
|
+
var InvalidMessageRoleError = class extends import_provider3.AISDKError {
|
244
258
|
constructor({
|
245
|
-
|
246
|
-
message
|
259
|
+
role,
|
260
|
+
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
247
261
|
}) {
|
248
262
|
super({ name: name2, message });
|
249
263
|
this[_a2] = true;
|
250
|
-
this.
|
264
|
+
this.role = role;
|
251
265
|
}
|
252
266
|
static isInstance(error) {
|
253
267
|
return import_provider3.AISDKError.hasMarker(error, marker2);
|
@@ -255,420 +269,434 @@ var MessageConversionError = class extends import_provider3.AISDKError {
|
|
255
269
|
};
|
256
270
|
_a2 = symbol2;
|
257
271
|
|
258
|
-
//
|
259
|
-
function
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
272
|
+
// src/prompt/convert-to-language-model-prompt.ts
|
273
|
+
async function convertToLanguageModelPrompt({
|
274
|
+
prompt,
|
275
|
+
supportedUrls,
|
276
|
+
downloadImplementation = download
|
277
|
+
}) {
|
278
|
+
const downloadedAssets = await downloadAssets(
|
279
|
+
prompt.messages,
|
280
|
+
downloadImplementation,
|
281
|
+
supportedUrls
|
282
|
+
);
|
283
|
+
return [
|
284
|
+
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
285
|
+
...prompt.messages.map(
|
286
|
+
(message) => convertToLanguageModelMessage({ message, downloadedAssets })
|
287
|
+
)
|
288
|
+
];
|
289
|
+
}
|
290
|
+
function convertToLanguageModelMessage({
|
291
|
+
message,
|
292
|
+
downloadedAssets
|
293
|
+
}) {
|
294
|
+
const role = message.role;
|
295
|
+
switch (role) {
|
296
|
+
case "system": {
|
297
|
+
return {
|
298
|
+
role: "system",
|
299
|
+
content: message.content,
|
300
|
+
providerOptions: message.providerOptions
|
301
|
+
};
|
302
|
+
}
|
303
|
+
case "user": {
|
304
|
+
if (typeof message.content === "string") {
|
305
|
+
return {
|
306
|
+
role: "user",
|
307
|
+
content: [{ type: "text", text: message.content }],
|
308
|
+
providerOptions: message.providerOptions
|
309
|
+
};
|
274
310
|
}
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
}));
|
289
|
-
coreMessages.push({
|
290
|
-
role: "user",
|
291
|
-
content: experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts
|
292
|
-
});
|
293
|
-
}
|
294
|
-
break;
|
311
|
+
return {
|
312
|
+
role: "user",
|
313
|
+
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
|
314
|
+
providerOptions: message.providerOptions
|
315
|
+
};
|
316
|
+
}
|
317
|
+
case "assistant": {
|
318
|
+
if (typeof message.content === "string") {
|
319
|
+
return {
|
320
|
+
role: "assistant",
|
321
|
+
content: [{ type: "text", text: message.content }],
|
322
|
+
providerOptions: message.providerOptions
|
323
|
+
};
|
295
324
|
}
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
}
|
316
|
-
case "reasoning": {
|
317
|
-
for (const detail of part.details) {
|
318
|
-
switch (detail.type) {
|
319
|
-
case "text":
|
320
|
-
content2.push({
|
321
|
-
type: "reasoning",
|
322
|
-
text: detail.text,
|
323
|
-
signature: detail.signature
|
324
|
-
});
|
325
|
-
break;
|
326
|
-
case "redacted":
|
327
|
-
content2.push({
|
328
|
-
type: "redacted-reasoning",
|
329
|
-
data: detail.data
|
330
|
-
});
|
331
|
-
break;
|
332
|
-
}
|
333
|
-
}
|
334
|
-
break;
|
335
|
-
}
|
336
|
-
case "tool-invocation":
|
337
|
-
content2.push({
|
338
|
-
type: "tool-call",
|
339
|
-
toolCallId: part.toolInvocation.toolCallId,
|
340
|
-
toolName: part.toolInvocation.toolName,
|
341
|
-
args: part.toolInvocation.args
|
342
|
-
});
|
343
|
-
break;
|
344
|
-
default: {
|
345
|
-
const _exhaustiveCheck = part;
|
346
|
-
throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
|
347
|
-
}
|
348
|
-
}
|
325
|
+
return {
|
326
|
+
role: "assistant",
|
327
|
+
content: message.content.filter(
|
328
|
+
// remove empty text parts:
|
329
|
+
(part) => part.type !== "text" || part.text !== ""
|
330
|
+
).map((part) => {
|
331
|
+
const providerOptions = part.providerOptions;
|
332
|
+
switch (part.type) {
|
333
|
+
case "file": {
|
334
|
+
const { data, mediaType } = convertToLanguageModelV2DataContent(
|
335
|
+
part.data
|
336
|
+
);
|
337
|
+
return {
|
338
|
+
type: "file",
|
339
|
+
data,
|
340
|
+
filename: part.filename,
|
341
|
+
mediaType: mediaType != null ? mediaType : part.mediaType,
|
342
|
+
providerOptions
|
343
|
+
};
|
349
344
|
}
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
).map((part) => part.toolInvocation);
|
357
|
-
if (stepInvocations.length > 0) {
|
358
|
-
coreMessages.push({
|
359
|
-
role: "tool",
|
360
|
-
content: stepInvocations.map(
|
361
|
-
(toolInvocation) => {
|
362
|
-
if (!("result" in toolInvocation)) {
|
363
|
-
throw new MessageConversionError({
|
364
|
-
originalMessage: message,
|
365
|
-
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
366
|
-
});
|
367
|
-
}
|
368
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
369
|
-
const tool = tools[toolName];
|
370
|
-
return (tool == null ? void 0 : tool.experimental_toToolResultContent) != null ? {
|
371
|
-
type: "tool-result",
|
372
|
-
toolCallId,
|
373
|
-
toolName,
|
374
|
-
result: tool.experimental_toToolResultContent(result),
|
375
|
-
experimental_content: tool.experimental_toToolResultContent(result)
|
376
|
-
} : {
|
377
|
-
type: "tool-result",
|
378
|
-
toolCallId,
|
379
|
-
toolName,
|
380
|
-
result
|
381
|
-
};
|
382
|
-
}
|
383
|
-
)
|
384
|
-
});
|
345
|
+
case "reasoning": {
|
346
|
+
return {
|
347
|
+
type: "reasoning",
|
348
|
+
text: part.text,
|
349
|
+
providerOptions
|
350
|
+
};
|
385
351
|
}
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
let blockHasToolInvocations = false;
|
393
|
-
let block = [];
|
394
|
-
for (const part of message.parts) {
|
395
|
-
switch (part.type) {
|
396
|
-
case "text": {
|
397
|
-
if (blockHasToolInvocations) {
|
398
|
-
processBlock2();
|
399
|
-
}
|
400
|
-
block.push(part);
|
401
|
-
break;
|
402
|
-
}
|
403
|
-
case "file":
|
404
|
-
case "reasoning": {
|
405
|
-
block.push(part);
|
406
|
-
break;
|
407
|
-
}
|
408
|
-
case "tool-invocation": {
|
409
|
-
if (((_b = part.toolInvocation.step) != null ? _b : 0) !== currentStep) {
|
410
|
-
processBlock2();
|
411
|
-
}
|
412
|
-
block.push(part);
|
413
|
-
blockHasToolInvocations = true;
|
414
|
-
break;
|
415
|
-
}
|
352
|
+
case "text": {
|
353
|
+
return {
|
354
|
+
type: "text",
|
355
|
+
text: part.text,
|
356
|
+
providerOptions
|
357
|
+
};
|
416
358
|
}
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
const maxStep = toolInvocations.reduce((max, toolInvocation) => {
|
427
|
-
var _a8;
|
428
|
-
return Math.max(max, (_a8 = toolInvocation.step) != null ? _a8 : 0);
|
429
|
-
}, 0);
|
430
|
-
for (let i2 = 0; i2 <= maxStep; i2++) {
|
431
|
-
const stepInvocations = toolInvocations.filter(
|
432
|
-
(toolInvocation) => {
|
433
|
-
var _a8;
|
434
|
-
return ((_a8 = toolInvocation.step) != null ? _a8 : 0) === i2;
|
359
|
+
case "tool-call": {
|
360
|
+
return {
|
361
|
+
type: "tool-call",
|
362
|
+
toolCallId: part.toolCallId,
|
363
|
+
toolName: part.toolName,
|
364
|
+
input: part.input,
|
365
|
+
providerExecuted: part.providerExecuted,
|
366
|
+
providerOptions
|
367
|
+
};
|
435
368
|
}
|
436
|
-
|
437
|
-
|
438
|
-
continue;
|
439
|
-
}
|
440
|
-
coreMessages.push({
|
441
|
-
role: "assistant",
|
442
|
-
content: [
|
443
|
-
...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
|
444
|
-
...stepInvocations.map(
|
445
|
-
({ toolCallId, toolName, args }) => ({
|
446
|
-
type: "tool-call",
|
447
|
-
toolCallId,
|
448
|
-
toolName,
|
449
|
-
args
|
450
|
-
})
|
451
|
-
)
|
452
|
-
]
|
453
|
-
});
|
454
|
-
coreMessages.push({
|
455
|
-
role: "tool",
|
456
|
-
content: stepInvocations.map((toolInvocation) => {
|
457
|
-
if (!("result" in toolInvocation)) {
|
458
|
-
throw new MessageConversionError({
|
459
|
-
originalMessage: message,
|
460
|
-
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
461
|
-
});
|
462
|
-
}
|
463
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
464
|
-
const tool = tools[toolName];
|
465
|
-
return (tool == null ? void 0 : tool.experimental_toToolResultContent) != null ? {
|
466
|
-
type: "tool-result",
|
467
|
-
toolCallId,
|
468
|
-
toolName,
|
469
|
-
result: tool.experimental_toToolResultContent(result),
|
470
|
-
experimental_content: tool.experimental_toToolResultContent(result)
|
471
|
-
} : {
|
369
|
+
case "tool-result": {
|
370
|
+
return {
|
472
371
|
type: "tool-result",
|
473
|
-
toolCallId,
|
474
|
-
toolName,
|
475
|
-
|
372
|
+
toolCallId: part.toolCallId,
|
373
|
+
toolName: part.toolName,
|
374
|
+
output: part.output,
|
375
|
+
providerOptions
|
476
376
|
};
|
477
|
-
}
|
478
|
-
}
|
479
|
-
}
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
}
|
495
|
-
}
|
496
|
-
|
497
|
-
|
498
|
-
}
|
499
|
-
|
500
|
-
// core/prompt/detect-prompt-type.ts
|
501
|
-
function detectPromptType(prompt) {
|
502
|
-
if (!Array.isArray(prompt)) {
|
503
|
-
return "other";
|
377
|
+
}
|
378
|
+
}
|
379
|
+
}),
|
380
|
+
providerOptions: message.providerOptions
|
381
|
+
};
|
382
|
+
}
|
383
|
+
case "tool": {
|
384
|
+
return {
|
385
|
+
role: "tool",
|
386
|
+
content: message.content.map((part) => ({
|
387
|
+
type: "tool-result",
|
388
|
+
toolCallId: part.toolCallId,
|
389
|
+
toolName: part.toolName,
|
390
|
+
output: part.output,
|
391
|
+
providerOptions: part.providerOptions
|
392
|
+
})),
|
393
|
+
providerOptions: message.providerOptions
|
394
|
+
};
|
395
|
+
}
|
396
|
+
default: {
|
397
|
+
const _exhaustiveCheck = role;
|
398
|
+
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
399
|
+
}
|
504
400
|
}
|
505
|
-
|
506
|
-
|
401
|
+
}
|
402
|
+
async function downloadAssets(messages, downloadImplementation, supportedUrls) {
|
403
|
+
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
|
404
|
+
(content) => Array.isArray(content)
|
405
|
+
).flat().filter(
|
406
|
+
(part) => part.type === "image" || part.type === "file"
|
407
|
+
).map((part) => {
|
408
|
+
var _a5;
|
409
|
+
const mediaType = (_a5 = part.mediaType) != null ? _a5 : part.type === "image" ? "image/*" : void 0;
|
410
|
+
let data = part.type === "image" ? part.image : part.data;
|
411
|
+
if (typeof data === "string") {
|
412
|
+
try {
|
413
|
+
data = new URL(data);
|
414
|
+
} catch (ignored) {
|
415
|
+
}
|
416
|
+
}
|
417
|
+
return { mediaType, data };
|
418
|
+
}).filter(
|
419
|
+
(part) => part.data instanceof URL && part.mediaType != null && !(0, import_provider_utils3.isUrlSupported)({
|
420
|
+
url: part.data.toString(),
|
421
|
+
mediaType: part.mediaType,
|
422
|
+
supportedUrls
|
423
|
+
})
|
424
|
+
).map((part) => part.data);
|
425
|
+
const downloadedImages = await Promise.all(
|
426
|
+
urls.map(async (url) => ({
|
427
|
+
url,
|
428
|
+
data: await downloadImplementation({ url })
|
429
|
+
}))
|
430
|
+
);
|
431
|
+
return Object.fromEntries(
|
432
|
+
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
433
|
+
);
|
434
|
+
}
|
435
|
+
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
436
|
+
var _a5;
|
437
|
+
if (part.type === "text") {
|
438
|
+
return {
|
439
|
+
type: "text",
|
440
|
+
text: part.text,
|
441
|
+
providerOptions: part.providerOptions
|
442
|
+
};
|
507
443
|
}
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
444
|
+
let originalData;
|
445
|
+
const type = part.type;
|
446
|
+
switch (type) {
|
447
|
+
case "image":
|
448
|
+
originalData = part.image;
|
449
|
+
break;
|
450
|
+
case "file":
|
451
|
+
originalData = part.data;
|
452
|
+
break;
|
453
|
+
default:
|
454
|
+
throw new Error(`Unsupported part type: ${type}`);
|
517
455
|
}
|
518
|
-
}
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
456
|
+
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
457
|
+
let mediaType = convertedMediaType != null ? convertedMediaType : part.mediaType;
|
458
|
+
let data = convertedData;
|
459
|
+
if (data instanceof URL) {
|
460
|
+
const downloadedFile = downloadedAssets[data.toString()];
|
461
|
+
if (downloadedFile) {
|
462
|
+
data = downloadedFile.data;
|
463
|
+
mediaType != null ? mediaType : mediaType = downloadedFile.mediaType;
|
464
|
+
}
|
465
|
+
}
|
466
|
+
switch (type) {
|
467
|
+
case "image": {
|
468
|
+
if (data instanceof Uint8Array || typeof data === "string") {
|
469
|
+
mediaType = (_a5 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a5 : mediaType;
|
470
|
+
}
|
471
|
+
return {
|
472
|
+
type: "file",
|
473
|
+
mediaType: mediaType != null ? mediaType : "image/*",
|
474
|
+
// any image
|
475
|
+
filename: void 0,
|
476
|
+
data,
|
477
|
+
providerOptions: part.providerOptions
|
478
|
+
};
|
479
|
+
}
|
480
|
+
case "file": {
|
481
|
+
if (mediaType == null) {
|
482
|
+
throw new Error(`Media type is missing for file part`);
|
483
|
+
}
|
484
|
+
return {
|
485
|
+
type: "file",
|
486
|
+
mediaType,
|
487
|
+
filename: part.filename,
|
488
|
+
data,
|
489
|
+
providerOptions: part.providerOptions
|
490
|
+
};
|
491
|
+
}
|
533
492
|
}
|
534
493
|
}
|
535
494
|
|
536
|
-
//
|
537
|
-
var
|
495
|
+
// src/prompt/prepare-tools-and-tool-choice.ts
|
496
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
538
497
|
|
539
|
-
//
|
540
|
-
|
498
|
+
// src/util/is-non-empty-object.ts
|
499
|
+
function isNonEmptyObject(object) {
|
500
|
+
return object != null && Object.keys(object).length > 0;
|
501
|
+
}
|
541
502
|
|
542
|
-
//
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
503
|
+
// src/prompt/prepare-tools-and-tool-choice.ts
|
504
|
+
function prepareToolsAndToolChoice({
|
505
|
+
tools,
|
506
|
+
toolChoice,
|
507
|
+
activeTools
|
508
|
+
}) {
|
509
|
+
if (!isNonEmptyObject(tools)) {
|
510
|
+
return {
|
511
|
+
tools: void 0,
|
512
|
+
toolChoice: void 0
|
513
|
+
};
|
514
|
+
}
|
515
|
+
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
516
|
+
([name5]) => activeTools.includes(name5)
|
517
|
+
) : Object.entries(tools);
|
518
|
+
return {
|
519
|
+
tools: filteredTools.map(([name5, tool]) => {
|
520
|
+
const toolType = tool.type;
|
521
|
+
switch (toolType) {
|
522
|
+
case void 0:
|
523
|
+
case "dynamic":
|
524
|
+
case "function":
|
525
|
+
return {
|
526
|
+
type: "function",
|
527
|
+
name: name5,
|
528
|
+
description: tool.description,
|
529
|
+
inputSchema: (0, import_provider_utils4.asSchema)(tool.inputSchema).jsonSchema,
|
530
|
+
providerOptions: tool.providerOptions
|
531
|
+
};
|
532
|
+
case "provider-defined":
|
533
|
+
return {
|
534
|
+
type: "provider-defined",
|
535
|
+
name: name5,
|
536
|
+
id: tool.id,
|
537
|
+
args: tool.args
|
538
|
+
};
|
539
|
+
default: {
|
540
|
+
const exhaustiveCheck = toolType;
|
541
|
+
throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
|
542
|
+
}
|
543
|
+
}
|
544
|
+
}),
|
545
|
+
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
546
|
+
};
|
547
|
+
}
|
554
548
|
|
555
|
-
//
|
556
|
-
var
|
557
|
-
|
558
|
-
|
559
|
-
);
|
549
|
+
// src/prompt/standardize-prompt.ts
|
550
|
+
var import_provider4 = require("@ai-sdk/provider");
|
551
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
552
|
+
var import_v46 = require("zod/v4");
|
560
553
|
|
561
|
-
//
|
562
|
-
var
|
554
|
+
// src/prompt/message.ts
|
555
|
+
var import_v45 = require("zod/v4");
|
563
556
|
|
564
|
-
//
|
565
|
-
var
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
557
|
+
// src/types/provider-metadata.ts
|
558
|
+
var import_v43 = require("zod/v4");
|
559
|
+
|
560
|
+
// src/types/json-value.ts
|
561
|
+
var import_v42 = require("zod/v4");
|
562
|
+
var jsonValueSchema = import_v42.z.lazy(
|
563
|
+
() => import_v42.z.union([
|
564
|
+
import_v42.z.null(),
|
565
|
+
import_v42.z.string(),
|
566
|
+
import_v42.z.number(),
|
567
|
+
import_v42.z.boolean(),
|
568
|
+
import_v42.z.record(import_v42.z.string(), jsonValueSchema),
|
569
|
+
import_v42.z.array(jsonValueSchema)
|
574
570
|
])
|
575
571
|
);
|
576
572
|
|
577
|
-
//
|
578
|
-
var
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
573
|
+
// src/types/provider-metadata.ts
|
574
|
+
var providerMetadataSchema = import_v43.z.record(
|
575
|
+
import_v43.z.string(),
|
576
|
+
import_v43.z.record(import_v43.z.string(), jsonValueSchema)
|
577
|
+
);
|
578
|
+
|
579
|
+
// src/prompt/content-part.ts
|
580
|
+
var import_v44 = require("zod/v4");
|
581
|
+
var textPartSchema = import_v44.z.object({
|
582
|
+
type: import_v44.z.literal("text"),
|
583
|
+
text: import_v44.z.string(),
|
588
584
|
providerOptions: providerMetadataSchema.optional()
|
589
585
|
});
|
590
|
-
var
|
591
|
-
type:
|
592
|
-
|
593
|
-
|
594
|
-
mediaType: import_zod5.z.string(),
|
595
|
-
mimeType: import_zod5.z.string().optional(),
|
586
|
+
var imagePartSchema = import_v44.z.object({
|
587
|
+
type: import_v44.z.literal("image"),
|
588
|
+
image: import_v44.z.union([dataContentSchema, import_v44.z.instanceof(URL)]),
|
589
|
+
mediaType: import_v44.z.string().optional(),
|
596
590
|
providerOptions: providerMetadataSchema.optional()
|
597
591
|
});
|
598
|
-
var
|
599
|
-
type:
|
600
|
-
|
592
|
+
var filePartSchema = import_v44.z.object({
|
593
|
+
type: import_v44.z.literal("file"),
|
594
|
+
data: import_v44.z.union([dataContentSchema, import_v44.z.instanceof(URL)]),
|
595
|
+
filename: import_v44.z.string().optional(),
|
596
|
+
mediaType: import_v44.z.string(),
|
601
597
|
providerOptions: providerMetadataSchema.optional()
|
602
598
|
});
|
603
|
-
var
|
604
|
-
type:
|
605
|
-
|
599
|
+
var reasoningPartSchema = import_v44.z.object({
|
600
|
+
type: import_v44.z.literal("reasoning"),
|
601
|
+
text: import_v44.z.string(),
|
606
602
|
providerOptions: providerMetadataSchema.optional()
|
607
603
|
});
|
608
|
-
var toolCallPartSchema =
|
609
|
-
type:
|
610
|
-
toolCallId:
|
611
|
-
toolName:
|
612
|
-
|
613
|
-
providerOptions: providerMetadataSchema.optional()
|
604
|
+
var toolCallPartSchema = import_v44.z.object({
|
605
|
+
type: import_v44.z.literal("tool-call"),
|
606
|
+
toolCallId: import_v44.z.string(),
|
607
|
+
toolName: import_v44.z.string(),
|
608
|
+
input: import_v44.z.unknown(),
|
609
|
+
providerOptions: providerMetadataSchema.optional(),
|
610
|
+
providerExecuted: import_v44.z.boolean().optional()
|
614
611
|
});
|
615
|
-
var
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
612
|
+
var outputSchema = import_v44.z.discriminatedUnion("type", [
|
613
|
+
import_v44.z.object({
|
614
|
+
type: import_v44.z.literal("text"),
|
615
|
+
value: import_v44.z.string()
|
616
|
+
}),
|
617
|
+
import_v44.z.object({
|
618
|
+
type: import_v44.z.literal("json"),
|
619
|
+
value: jsonValueSchema
|
620
|
+
}),
|
621
|
+
import_v44.z.object({
|
622
|
+
type: import_v44.z.literal("error-text"),
|
623
|
+
value: import_v44.z.string()
|
624
|
+
}),
|
625
|
+
import_v44.z.object({
|
626
|
+
type: import_v44.z.literal("error-json"),
|
627
|
+
value: jsonValueSchema
|
628
|
+
}),
|
629
|
+
import_v44.z.object({
|
630
|
+
type: import_v44.z.literal("content"),
|
631
|
+
value: import_v44.z.array(
|
632
|
+
import_v44.z.union([
|
633
|
+
import_v44.z.object({
|
634
|
+
type: import_v44.z.literal("text"),
|
635
|
+
text: import_v44.z.string()
|
636
|
+
}),
|
637
|
+
import_v44.z.object({
|
638
|
+
type: import_v44.z.literal("media"),
|
639
|
+
data: import_v44.z.string(),
|
640
|
+
mediaType: import_v44.z.string()
|
641
|
+
})
|
642
|
+
])
|
643
|
+
)
|
644
|
+
})
|
645
|
+
]);
|
646
|
+
var toolResultPartSchema = import_v44.z.object({
|
647
|
+
type: import_v44.z.literal("tool-result"),
|
648
|
+
toolCallId: import_v44.z.string(),
|
649
|
+
toolName: import_v44.z.string(),
|
650
|
+
output: outputSchema,
|
622
651
|
providerOptions: providerMetadataSchema.optional()
|
623
652
|
});
|
624
653
|
|
625
|
-
//
|
626
|
-
var
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
654
|
+
// src/prompt/message.ts
|
655
|
+
var systemModelMessageSchema = import_v45.z.object(
|
656
|
+
{
|
657
|
+
role: import_v45.z.literal("system"),
|
658
|
+
content: import_v45.z.string(),
|
659
|
+
providerOptions: providerMetadataSchema.optional()
|
660
|
+
}
|
661
|
+
);
|
662
|
+
var userModelMessageSchema = import_v45.z.object({
|
663
|
+
role: import_v45.z.literal("user"),
|
664
|
+
content: import_v45.z.union([
|
665
|
+
import_v45.z.string(),
|
666
|
+
import_v45.z.array(import_v45.z.union([textPartSchema, imagePartSchema, filePartSchema]))
|
636
667
|
]),
|
637
668
|
providerOptions: providerMetadataSchema.optional()
|
638
669
|
});
|
639
|
-
var
|
640
|
-
role:
|
641
|
-
content:
|
642
|
-
|
643
|
-
|
644
|
-
|
670
|
+
var assistantModelMessageSchema = import_v45.z.object({
|
671
|
+
role: import_v45.z.literal("assistant"),
|
672
|
+
content: import_v45.z.union([
|
673
|
+
import_v45.z.string(),
|
674
|
+
import_v45.z.array(
|
675
|
+
import_v45.z.union([
|
645
676
|
textPartSchema,
|
646
677
|
filePartSchema,
|
647
678
|
reasoningPartSchema,
|
648
|
-
|
649
|
-
|
679
|
+
toolCallPartSchema,
|
680
|
+
toolResultPartSchema
|
650
681
|
])
|
651
682
|
)
|
652
683
|
]),
|
653
684
|
providerOptions: providerMetadataSchema.optional()
|
654
685
|
});
|
655
|
-
var
|
656
|
-
role:
|
657
|
-
content:
|
686
|
+
var toolModelMessageSchema = import_v45.z.object({
|
687
|
+
role: import_v45.z.literal("tool"),
|
688
|
+
content: import_v45.z.array(toolResultPartSchema),
|
658
689
|
providerOptions: providerMetadataSchema.optional()
|
659
690
|
});
|
660
|
-
var
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
691
|
+
var modelMessageSchema = import_v45.z.union([
|
692
|
+
systemModelMessageSchema,
|
693
|
+
userModelMessageSchema,
|
694
|
+
assistantModelMessageSchema,
|
695
|
+
toolModelMessageSchema
|
665
696
|
]);
|
666
697
|
|
667
|
-
//
|
668
|
-
function standardizePrompt({
|
669
|
-
prompt,
|
670
|
-
tools
|
671
|
-
}) {
|
698
|
+
// src/prompt/standardize-prompt.ts
|
699
|
+
async function standardizePrompt(prompt) {
|
672
700
|
if (prompt.prompt == null && prompt.messages == null) {
|
673
701
|
throw new import_provider4.InvalidPromptError({
|
674
702
|
prompt,
|
@@ -687,161 +715,43 @@ function standardizePrompt({
|
|
687
715
|
message: "system must be a string"
|
688
716
|
});
|
689
717
|
}
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
role: "user",
|
703
|
-
content: prompt.prompt
|
704
|
-
}
|
705
|
-
]
|
706
|
-
};
|
718
|
+
let messages;
|
719
|
+
if (prompt.prompt != null && typeof prompt.prompt === "string") {
|
720
|
+
messages = [{ role: "user", content: prompt.prompt }];
|
721
|
+
} else if (prompt.prompt != null && Array.isArray(prompt.prompt)) {
|
722
|
+
messages = prompt.prompt;
|
723
|
+
} else if (prompt.messages != null) {
|
724
|
+
messages = prompt.messages;
|
725
|
+
} else {
|
726
|
+
throw new import_provider4.InvalidPromptError({
|
727
|
+
prompt,
|
728
|
+
message: "prompt or messages must be defined"
|
729
|
+
});
|
707
730
|
}
|
708
|
-
if (
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
prompt,
|
713
|
-
message: "messages must be an array of CoreMessage or UIMessage"
|
714
|
-
});
|
715
|
-
}
|
716
|
-
const messages = promptType === "ui-messages" ? convertToCoreMessages(prompt.messages, {
|
717
|
-
tools
|
718
|
-
}) : prompt.messages;
|
719
|
-
if (messages.length === 0) {
|
720
|
-
throw new import_provider4.InvalidPromptError({
|
721
|
-
prompt,
|
722
|
-
message: "messages must not be empty"
|
723
|
-
});
|
724
|
-
}
|
725
|
-
const validationResult = (0, import_provider_utils2.safeValidateTypes)({
|
726
|
-
value: messages,
|
727
|
-
schema: import_zod7.z.array(coreMessageSchema)
|
731
|
+
if (messages.length === 0) {
|
732
|
+
throw new import_provider4.InvalidPromptError({
|
733
|
+
prompt,
|
734
|
+
message: "messages must not be empty"
|
728
735
|
});
|
729
|
-
if (!validationResult.success) {
|
730
|
-
throw new import_provider4.InvalidPromptError({
|
731
|
-
prompt,
|
732
|
-
message: "messages must be an array of CoreMessage or UIMessage",
|
733
|
-
cause: validationResult.error
|
734
|
-
});
|
735
|
-
}
|
736
|
-
return {
|
737
|
-
type: "messages",
|
738
|
-
messages,
|
739
|
-
system: prompt.system
|
740
|
-
};
|
741
736
|
}
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
var import_zod_to_json_schema = __toESM(require("zod-to-json-schema"));
|
753
|
-
function zodSchema(zodSchema2, options) {
|
754
|
-
var _a7;
|
755
|
-
const useReferences = (_a7 = options == null ? void 0 : options.useReferences) != null ? _a7 : false;
|
756
|
-
return jsonSchema(
|
757
|
-
(0, import_zod_to_json_schema.default)(zodSchema2, {
|
758
|
-
$refStrategy: useReferences ? "root" : "none",
|
759
|
-
target: "jsonSchema7"
|
760
|
-
// note: openai mode breaks various gemini conversions
|
761
|
-
}),
|
762
|
-
{
|
763
|
-
validate: (value) => {
|
764
|
-
const result = zodSchema2.safeParse(value);
|
765
|
-
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
766
|
-
}
|
767
|
-
}
|
768
|
-
);
|
769
|
-
}
|
770
|
-
|
771
|
-
// core/util/schema.ts
|
772
|
-
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
773
|
-
function jsonSchema(jsonSchema2, {
|
774
|
-
validate
|
775
|
-
} = {}) {
|
776
|
-
return {
|
777
|
-
[schemaSymbol]: true,
|
778
|
-
_type: void 0,
|
779
|
-
// should never be used directly
|
780
|
-
[import_provider_utils3.validatorSymbol]: true,
|
781
|
-
jsonSchema: jsonSchema2,
|
782
|
-
validate
|
783
|
-
};
|
784
|
-
}
|
785
|
-
function isSchema(value) {
|
786
|
-
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
787
|
-
}
|
788
|
-
function asSchema(schema) {
|
789
|
-
return schema == null ? jsonSchema({
|
790
|
-
properties: {},
|
791
|
-
additionalProperties: false
|
792
|
-
}) : isSchema(schema) ? schema : zodSchema(schema);
|
793
|
-
}
|
794
|
-
|
795
|
-
// core/util/is-non-empty-object.ts
|
796
|
-
function isNonEmptyObject(object) {
|
797
|
-
return object != null && Object.keys(object).length > 0;
|
798
|
-
}
|
799
|
-
|
800
|
-
// core/prompt/prepare-tools-and-tool-choice.ts
|
801
|
-
function prepareToolsAndToolChoice({
|
802
|
-
tools,
|
803
|
-
toolChoice,
|
804
|
-
activeTools
|
805
|
-
}) {
|
806
|
-
if (!isNonEmptyObject(tools)) {
|
807
|
-
return {
|
808
|
-
tools: void 0,
|
809
|
-
toolChoice: void 0
|
810
|
-
};
|
737
|
+
const validationResult = await (0, import_provider_utils5.safeValidateTypes)({
|
738
|
+
value: messages,
|
739
|
+
schema: import_v46.z.array(modelMessageSchema)
|
740
|
+
});
|
741
|
+
if (!validationResult.success) {
|
742
|
+
throw new import_provider4.InvalidPromptError({
|
743
|
+
prompt,
|
744
|
+
message: "The messages must be a ModelMessage[]. If you have passed a UIMessage[], you can use convertToModelMessages to convert them.",
|
745
|
+
cause: validationResult.error
|
746
|
+
});
|
811
747
|
}
|
812
|
-
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
813
|
-
([name7]) => activeTools.includes(name7)
|
814
|
-
) : Object.entries(tools);
|
815
748
|
return {
|
816
|
-
|
817
|
-
|
818
|
-
switch (toolType) {
|
819
|
-
case void 0:
|
820
|
-
case "function":
|
821
|
-
return {
|
822
|
-
type: "function",
|
823
|
-
name: name7,
|
824
|
-
description: tool.description,
|
825
|
-
parameters: asSchema(tool.parameters).jsonSchema
|
826
|
-
};
|
827
|
-
case "provider-defined":
|
828
|
-
return {
|
829
|
-
type: "provider-defined",
|
830
|
-
name: name7,
|
831
|
-
id: tool.id,
|
832
|
-
args: tool.args
|
833
|
-
};
|
834
|
-
default: {
|
835
|
-
const exhaustiveCheck = toolType;
|
836
|
-
throw new Error(`Unsupported tool type: ${exhaustiveCheck}`);
|
837
|
-
}
|
838
|
-
}
|
839
|
-
}),
|
840
|
-
toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
|
749
|
+
messages,
|
750
|
+
system: prompt.system
|
841
751
|
};
|
842
752
|
}
|
843
753
|
|
844
|
-
//
|
754
|
+
// src/error/invalid-argument-error.ts
|
845
755
|
var import_provider5 = require("@ai-sdk/provider");
|
846
756
|
var name3 = "AI_InvalidArgumentError";
|
847
757
|
var marker3 = `vercel.ai.error.${name3}`;
|
@@ -867,115 +777,7 @@ var InvalidArgumentError = class extends import_provider5.AISDKError {
|
|
867
777
|
};
|
868
778
|
_a3 = symbol3;
|
869
779
|
|
870
|
-
//
|
871
|
-
var import_provider7 = require("@ai-sdk/provider");
|
872
|
-
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
873
|
-
|
874
|
-
// util/retry-error.ts
|
875
|
-
var import_provider6 = require("@ai-sdk/provider");
|
876
|
-
var name4 = "AI_RetryError";
|
877
|
-
var marker4 = `vercel.ai.error.${name4}`;
|
878
|
-
var symbol4 = Symbol.for(marker4);
|
879
|
-
var _a4;
|
880
|
-
var RetryError = class extends import_provider6.AISDKError {
|
881
|
-
constructor({
|
882
|
-
message,
|
883
|
-
reason,
|
884
|
-
errors
|
885
|
-
}) {
|
886
|
-
super({ name: name4, message });
|
887
|
-
this[_a4] = true;
|
888
|
-
this.reason = reason;
|
889
|
-
this.errors = errors;
|
890
|
-
this.lastError = errors[errors.length - 1];
|
891
|
-
}
|
892
|
-
static isInstance(error) {
|
893
|
-
return import_provider6.AISDKError.hasMarker(error, marker4);
|
894
|
-
}
|
895
|
-
};
|
896
|
-
_a4 = symbol4;
|
897
|
-
|
898
|
-
// util/retry-with-exponential-backoff.ts
|
899
|
-
var retryWithExponentialBackoff = ({
|
900
|
-
maxRetries = 2,
|
901
|
-
initialDelayInMs = 2e3,
|
902
|
-
backoffFactor = 2
|
903
|
-
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
904
|
-
maxRetries,
|
905
|
-
delayInMs: initialDelayInMs,
|
906
|
-
backoffFactor
|
907
|
-
});
|
908
|
-
async function _retryWithExponentialBackoff(f, {
|
909
|
-
maxRetries,
|
910
|
-
delayInMs,
|
911
|
-
backoffFactor
|
912
|
-
}, errors = []) {
|
913
|
-
try {
|
914
|
-
return await f();
|
915
|
-
} catch (error) {
|
916
|
-
if ((0, import_provider_utils5.isAbortError)(error)) {
|
917
|
-
throw error;
|
918
|
-
}
|
919
|
-
if (maxRetries === 0) {
|
920
|
-
throw error;
|
921
|
-
}
|
922
|
-
const errorMessage = (0, import_provider_utils5.getErrorMessage)(error);
|
923
|
-
const newErrors = [...errors, error];
|
924
|
-
const tryNumber = newErrors.length;
|
925
|
-
if (tryNumber > maxRetries) {
|
926
|
-
throw new RetryError({
|
927
|
-
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
928
|
-
reason: "maxRetriesExceeded",
|
929
|
-
errors: newErrors
|
930
|
-
});
|
931
|
-
}
|
932
|
-
if (error instanceof Error && import_provider7.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
933
|
-
await (0, import_provider_utils5.delay)(delayInMs);
|
934
|
-
return _retryWithExponentialBackoff(
|
935
|
-
f,
|
936
|
-
{ maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
|
937
|
-
newErrors
|
938
|
-
);
|
939
|
-
}
|
940
|
-
if (tryNumber === 1) {
|
941
|
-
throw error;
|
942
|
-
}
|
943
|
-
throw new RetryError({
|
944
|
-
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
945
|
-
reason: "errorNotRetryable",
|
946
|
-
errors: newErrors
|
947
|
-
});
|
948
|
-
}
|
949
|
-
}
|
950
|
-
|
951
|
-
// core/prompt/prepare-retries.ts
|
952
|
-
function prepareRetries({
|
953
|
-
maxRetries
|
954
|
-
}) {
|
955
|
-
if (maxRetries != null) {
|
956
|
-
if (!Number.isInteger(maxRetries)) {
|
957
|
-
throw new InvalidArgumentError({
|
958
|
-
parameter: "maxRetries",
|
959
|
-
value: maxRetries,
|
960
|
-
message: "maxRetries must be an integer"
|
961
|
-
});
|
962
|
-
}
|
963
|
-
if (maxRetries < 0) {
|
964
|
-
throw new InvalidArgumentError({
|
965
|
-
parameter: "maxRetries",
|
966
|
-
value: maxRetries,
|
967
|
-
message: "maxRetries must be >= 0"
|
968
|
-
});
|
969
|
-
}
|
970
|
-
}
|
971
|
-
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
|
972
|
-
return {
|
973
|
-
maxRetries: maxRetriesResult,
|
974
|
-
retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
|
975
|
-
};
|
976
|
-
}
|
977
|
-
|
978
|
-
// core/prompt/prepare-call-settings.ts
|
780
|
+
// src/prompt/prepare-call-settings.ts
|
979
781
|
function prepareCallSettings({
|
980
782
|
maxOutputTokens,
|
981
783
|
temperature,
|
@@ -983,8 +785,8 @@ function prepareCallSettings({
|
|
983
785
|
topK,
|
984
786
|
presencePenalty,
|
985
787
|
frequencyPenalty,
|
986
|
-
|
987
|
-
|
788
|
+
seed,
|
789
|
+
stopSequences
|
988
790
|
}) {
|
989
791
|
if (maxOutputTokens != null) {
|
990
792
|
if (!Number.isInteger(maxOutputTokens)) {
|
@@ -1058,406 +860,173 @@ function prepareCallSettings({
|
|
1058
860
|
}
|
1059
861
|
return {
|
1060
862
|
maxOutputTokens,
|
1061
|
-
|
1062
|
-
temperature: temperature != null ? temperature : 0,
|
863
|
+
temperature,
|
1063
864
|
topP,
|
1064
865
|
topK,
|
1065
866
|
presencePenalty,
|
1066
867
|
frequencyPenalty,
|
1067
|
-
stopSequences
|
868
|
+
stopSequences,
|
1068
869
|
seed
|
1069
870
|
};
|
1070
871
|
}
|
1071
872
|
|
1072
|
-
// util/
|
1073
|
-
var
|
1074
|
-
var
|
1075
|
-
|
1076
|
-
|
1077
|
-
var
|
1078
|
-
var
|
873
|
+
// src/util/retry-with-exponential-backoff.ts
|
874
|
+
var import_provider7 = require("@ai-sdk/provider");
|
875
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
876
|
+
|
877
|
+
// src/util/retry-error.ts
|
878
|
+
var import_provider6 = require("@ai-sdk/provider");
|
879
|
+
var name4 = "AI_RetryError";
|
880
|
+
var marker4 = `vercel.ai.error.${name4}`;
|
881
|
+
var symbol4 = Symbol.for(marker4);
|
882
|
+
var _a4;
|
883
|
+
var RetryError = class extends import_provider6.AISDKError {
|
1079
884
|
constructor({
|
1080
|
-
|
1081
|
-
|
1082
|
-
|
1083
|
-
cause,
|
1084
|
-
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
885
|
+
message,
|
886
|
+
reason,
|
887
|
+
errors
|
1085
888
|
}) {
|
1086
|
-
super({ name:
|
1087
|
-
this[
|
1088
|
-
this.
|
1089
|
-
this.
|
1090
|
-
this.
|
889
|
+
super({ name: name4, message });
|
890
|
+
this[_a4] = true;
|
891
|
+
this.reason = reason;
|
892
|
+
this.errors = errors;
|
893
|
+
this.lastError = errors[errors.length - 1];
|
1091
894
|
}
|
1092
895
|
static isInstance(error) {
|
1093
|
-
return
|
896
|
+
return import_provider6.AISDKError.hasMarker(error, marker4);
|
1094
897
|
}
|
1095
898
|
};
|
1096
|
-
|
1097
|
-
|
1098
|
-
// util/download.ts
|
1099
|
-
async function download({ url }) {
|
1100
|
-
var _a7;
|
1101
|
-
const urlText = url.toString();
|
1102
|
-
try {
|
1103
|
-
const response = await fetch(urlText);
|
1104
|
-
if (!response.ok) {
|
1105
|
-
throw new DownloadError({
|
1106
|
-
url: urlText,
|
1107
|
-
statusCode: response.status,
|
1108
|
-
statusText: response.statusText
|
1109
|
-
});
|
1110
|
-
}
|
1111
|
-
return {
|
1112
|
-
data: new Uint8Array(await response.arrayBuffer()),
|
1113
|
-
mediaType: (_a7 = response.headers.get("content-type")) != null ? _a7 : void 0
|
1114
|
-
};
|
1115
|
-
} catch (error) {
|
1116
|
-
if (DownloadError.isInstance(error)) {
|
1117
|
-
throw error;
|
1118
|
-
}
|
1119
|
-
throw new DownloadError({ url: urlText, cause: error });
|
1120
|
-
}
|
1121
|
-
}
|
899
|
+
_a4 = symbol4;
|
1122
900
|
|
1123
|
-
//
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
bytesPrefix: [71, 73, 70],
|
1128
|
-
base64Prefix: "R0lG"
|
1129
|
-
},
|
1130
|
-
{
|
1131
|
-
mediaType: "image/png",
|
1132
|
-
bytesPrefix: [137, 80, 78, 71],
|
1133
|
-
base64Prefix: "iVBORw"
|
1134
|
-
},
|
1135
|
-
{
|
1136
|
-
mediaType: "image/jpeg",
|
1137
|
-
bytesPrefix: [255, 216],
|
1138
|
-
base64Prefix: "/9j/"
|
1139
|
-
},
|
1140
|
-
{
|
1141
|
-
mediaType: "image/webp",
|
1142
|
-
bytesPrefix: [82, 73, 70, 70],
|
1143
|
-
base64Prefix: "UklGRg"
|
1144
|
-
},
|
1145
|
-
{
|
1146
|
-
mediaType: "image/bmp",
|
1147
|
-
bytesPrefix: [66, 77],
|
1148
|
-
base64Prefix: "Qk"
|
1149
|
-
},
|
1150
|
-
{
|
1151
|
-
mediaType: "image/tiff",
|
1152
|
-
bytesPrefix: [73, 73, 42, 0],
|
1153
|
-
base64Prefix: "SUkqAA"
|
1154
|
-
},
|
1155
|
-
{
|
1156
|
-
mediaType: "image/tiff",
|
1157
|
-
bytesPrefix: [77, 77, 0, 42],
|
1158
|
-
base64Prefix: "TU0AKg"
|
1159
|
-
},
|
1160
|
-
{
|
1161
|
-
mediaType: "image/avif",
|
1162
|
-
bytesPrefix: [
|
1163
|
-
0,
|
1164
|
-
0,
|
1165
|
-
0,
|
1166
|
-
32,
|
1167
|
-
102,
|
1168
|
-
116,
|
1169
|
-
121,
|
1170
|
-
112,
|
1171
|
-
97,
|
1172
|
-
118,
|
1173
|
-
105,
|
1174
|
-
102
|
1175
|
-
],
|
1176
|
-
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
1177
|
-
},
|
1178
|
-
{
|
1179
|
-
mediaType: "image/heic",
|
1180
|
-
bytesPrefix: [
|
1181
|
-
0,
|
1182
|
-
0,
|
1183
|
-
0,
|
1184
|
-
32,
|
1185
|
-
102,
|
1186
|
-
116,
|
1187
|
-
121,
|
1188
|
-
112,
|
1189
|
-
104,
|
1190
|
-
101,
|
1191
|
-
105,
|
1192
|
-
99
|
1193
|
-
],
|
1194
|
-
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
1195
|
-
}
|
1196
|
-
];
|
1197
|
-
function detectMediaType({
|
1198
|
-
data,
|
1199
|
-
signatures
|
901
|
+
// src/util/retry-with-exponential-backoff.ts
|
902
|
+
function getRetryDelayInMs({
|
903
|
+
error,
|
904
|
+
exponentialBackoffDelay
|
1200
905
|
}) {
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
906
|
+
const headers = error.responseHeaders;
|
907
|
+
if (!headers)
|
908
|
+
return exponentialBackoffDelay;
|
909
|
+
let ms;
|
910
|
+
const retryAfterMs = headers["retry-after-ms"];
|
911
|
+
if (retryAfterMs) {
|
912
|
+
const timeoutMs = parseFloat(retryAfterMs);
|
913
|
+
if (!Number.isNaN(timeoutMs)) {
|
914
|
+
ms = timeoutMs;
|
1204
915
|
}
|
1205
916
|
}
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
var _a6;
|
1215
|
-
var InvalidMessageRoleError = class extends import_provider9.AISDKError {
|
1216
|
-
constructor({
|
1217
|
-
role,
|
1218
|
-
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
1219
|
-
}) {
|
1220
|
-
super({ name: name6, message });
|
1221
|
-
this[_a6] = true;
|
1222
|
-
this.role = role;
|
917
|
+
const retryAfter = headers["retry-after"];
|
918
|
+
if (retryAfter && ms === void 0) {
|
919
|
+
const timeoutSeconds = parseFloat(retryAfter);
|
920
|
+
if (!Number.isNaN(timeoutSeconds)) {
|
921
|
+
ms = timeoutSeconds * 1e3;
|
922
|
+
} else {
|
923
|
+
ms = Date.parse(retryAfter) - Date.now();
|
924
|
+
}
|
1223
925
|
}
|
1224
|
-
|
1225
|
-
return
|
926
|
+
if (ms != null && !Number.isNaN(ms) && 0 <= ms && (ms < 60 * 1e3 || ms < exponentialBackoffDelay)) {
|
927
|
+
return ms;
|
1226
928
|
}
|
1227
|
-
|
1228
|
-
_a6 = symbol6;
|
1229
|
-
|
1230
|
-
// core/prompt/convert-to-language-model-prompt.ts
|
1231
|
-
async function convertToLanguageModelPrompt({
|
1232
|
-
prompt,
|
1233
|
-
modelSupportsImageUrls = true,
|
1234
|
-
modelSupportsUrl = () => false,
|
1235
|
-
downloadImplementation = download
|
1236
|
-
}) {
|
1237
|
-
const downloadedAssets = await downloadAssets(
|
1238
|
-
prompt.messages,
|
1239
|
-
downloadImplementation,
|
1240
|
-
modelSupportsImageUrls,
|
1241
|
-
modelSupportsUrl
|
1242
|
-
);
|
1243
|
-
return [
|
1244
|
-
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
1245
|
-
...prompt.messages.map(
|
1246
|
-
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
1247
|
-
)
|
1248
|
-
];
|
929
|
+
return exponentialBackoffDelay;
|
1249
930
|
}
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
931
|
+
var retryWithExponentialBackoffRespectingRetryHeaders = ({
|
932
|
+
maxRetries = 2,
|
933
|
+
initialDelayInMs = 2e3,
|
934
|
+
backoffFactor = 2,
|
935
|
+
abortSignal
|
936
|
+
} = {}) => async (f) => _retryWithExponentialBackoff(f, {
|
937
|
+
maxRetries,
|
938
|
+
delayInMs: initialDelayInMs,
|
939
|
+
backoffFactor,
|
940
|
+
abortSignal
|
941
|
+
});
|
942
|
+
async function _retryWithExponentialBackoff(f, {
|
943
|
+
maxRetries,
|
944
|
+
delayInMs,
|
945
|
+
backoffFactor,
|
946
|
+
abortSignal
|
947
|
+
}, errors = []) {
|
948
|
+
try {
|
949
|
+
return await f();
|
950
|
+
} catch (error) {
|
951
|
+
if ((0, import_provider_utils6.isAbortError)(error)) {
|
952
|
+
throw error;
|
1259
953
|
}
|
1260
|
-
|
1261
|
-
|
1262
|
-
return {
|
1263
|
-
role: "user",
|
1264
|
-
content: [{ type: "text", text: message.content }],
|
1265
|
-
providerOptions: message.providerOptions
|
1266
|
-
};
|
1267
|
-
}
|
1268
|
-
return {
|
1269
|
-
role: "user",
|
1270
|
-
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
|
1271
|
-
providerOptions: message.providerOptions
|
1272
|
-
};
|
954
|
+
if (maxRetries === 0) {
|
955
|
+
throw error;
|
1273
956
|
}
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
role: "assistant",
|
1284
|
-
content: message.content.filter(
|
1285
|
-
// remove empty text parts:
|
1286
|
-
(part) => part.type !== "text" || part.text !== ""
|
1287
|
-
).map((part) => {
|
1288
|
-
var _a7;
|
1289
|
-
const providerOptions = part.providerOptions;
|
1290
|
-
switch (part.type) {
|
1291
|
-
case "file": {
|
1292
|
-
const { data, mediaType } = convertToLanguageModelV2DataContent(
|
1293
|
-
part.data
|
1294
|
-
);
|
1295
|
-
return {
|
1296
|
-
type: "file",
|
1297
|
-
data,
|
1298
|
-
filename: part.filename,
|
1299
|
-
mediaType: (_a7 = mediaType != null ? mediaType : part.mediaType) != null ? _a7 : part.mimeType,
|
1300
|
-
providerOptions
|
1301
|
-
};
|
1302
|
-
}
|
1303
|
-
case "reasoning": {
|
1304
|
-
return {
|
1305
|
-
type: "reasoning",
|
1306
|
-
text: part.text,
|
1307
|
-
signature: part.signature,
|
1308
|
-
providerOptions
|
1309
|
-
};
|
1310
|
-
}
|
1311
|
-
case "redacted-reasoning": {
|
1312
|
-
return {
|
1313
|
-
type: "redacted-reasoning",
|
1314
|
-
data: part.data,
|
1315
|
-
providerOptions
|
1316
|
-
};
|
1317
|
-
}
|
1318
|
-
case "text": {
|
1319
|
-
return {
|
1320
|
-
type: "text",
|
1321
|
-
text: part.text,
|
1322
|
-
providerOptions
|
1323
|
-
};
|
1324
|
-
}
|
1325
|
-
case "tool-call": {
|
1326
|
-
return {
|
1327
|
-
type: "tool-call",
|
1328
|
-
toolCallId: part.toolCallId,
|
1329
|
-
toolName: part.toolName,
|
1330
|
-
args: part.args,
|
1331
|
-
providerOptions
|
1332
|
-
};
|
1333
|
-
}
|
1334
|
-
}
|
1335
|
-
}),
|
1336
|
-
providerOptions: message.providerOptions
|
1337
|
-
};
|
957
|
+
const errorMessage = (0, import_provider_utils6.getErrorMessage)(error);
|
958
|
+
const newErrors = [...errors, error];
|
959
|
+
const tryNumber = newErrors.length;
|
960
|
+
if (tryNumber > maxRetries) {
|
961
|
+
throw new RetryError({
|
962
|
+
message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
|
963
|
+
reason: "maxRetriesExceeded",
|
964
|
+
errors: newErrors
|
965
|
+
});
|
1338
966
|
}
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
967
|
+
if (error instanceof Error && import_provider7.APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
|
968
|
+
await (0, import_provider_utils6.delay)(
|
969
|
+
getRetryDelayInMs({
|
970
|
+
error,
|
971
|
+
exponentialBackoffDelay: delayInMs
|
972
|
+
}),
|
973
|
+
{ abortSignal }
|
974
|
+
);
|
975
|
+
return _retryWithExponentialBackoff(
|
976
|
+
f,
|
977
|
+
{
|
978
|
+
maxRetries,
|
979
|
+
delayInMs: backoffFactor * delayInMs,
|
980
|
+
backoffFactor,
|
981
|
+
abortSignal
|
982
|
+
},
|
983
|
+
newErrors
|
984
|
+
);
|
1353
985
|
}
|
1354
|
-
|
1355
|
-
|
1356
|
-
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
986
|
+
if (tryNumber === 1) {
|
987
|
+
throw error;
|
1357
988
|
}
|
989
|
+
throw new RetryError({
|
990
|
+
message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
|
991
|
+
reason: "errorNotRetryable",
|
992
|
+
errors: newErrors
|
993
|
+
});
|
1358
994
|
}
|
1359
995
|
}
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1363
|
-
|
1364
|
-
|
1365
|
-
|
1366
|
-
|
1367
|
-
|
1368
|
-
|
1369
|
-
|
1370
|
-
|
1371
|
-
|
1372
|
-
|
1373
|
-
const downloadedImages = await Promise.all(
|
1374
|
-
urls.map(async (url) => ({
|
1375
|
-
url,
|
1376
|
-
data: await downloadImplementation({ url })
|
1377
|
-
}))
|
1378
|
-
);
|
1379
|
-
return Object.fromEntries(
|
1380
|
-
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
1381
|
-
);
|
1382
|
-
}
|
1383
|
-
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
1384
|
-
var _a7, _b, _c;
|
1385
|
-
if (part.type === "text") {
|
1386
|
-
return {
|
1387
|
-
type: "text",
|
1388
|
-
text: part.text,
|
1389
|
-
providerOptions: part.providerOptions
|
1390
|
-
};
|
1391
|
-
}
|
1392
|
-
let originalData;
|
1393
|
-
const type = part.type;
|
1394
|
-
switch (type) {
|
1395
|
-
case "image":
|
1396
|
-
originalData = part.image;
|
1397
|
-
break;
|
1398
|
-
case "file":
|
1399
|
-
originalData = part.data;
|
1400
|
-
break;
|
1401
|
-
default:
|
1402
|
-
throw new Error(`Unsupported part type: ${type}`);
|
1403
|
-
}
|
1404
|
-
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
1405
|
-
let mediaType = (_a7 = convertedMediaType != null ? convertedMediaType : part.mediaType) != null ? _a7 : part.mimeType;
|
1406
|
-
let data = convertedData;
|
1407
|
-
if (data instanceof URL) {
|
1408
|
-
const downloadedFile = downloadedAssets[data.toString()];
|
1409
|
-
if (downloadedFile) {
|
1410
|
-
data = downloadedFile.data;
|
1411
|
-
mediaType = (_b = downloadedFile.mediaType) != null ? _b : mediaType;
|
1412
|
-
}
|
1413
|
-
}
|
1414
|
-
switch (type) {
|
1415
|
-
case "image": {
|
1416
|
-
if (data instanceof Uint8Array || typeof data === "string") {
|
1417
|
-
mediaType = (_c = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _c : mediaType;
|
1418
|
-
}
|
1419
|
-
return {
|
1420
|
-
type: "file",
|
1421
|
-
mediaType: mediaType != null ? mediaType : "image/*",
|
1422
|
-
// any image
|
1423
|
-
filename: void 0,
|
1424
|
-
data,
|
1425
|
-
providerOptions: part.providerOptions
|
1426
|
-
};
|
996
|
+
|
997
|
+
// src/util/prepare-retries.ts
|
998
|
+
function prepareRetries({
|
999
|
+
maxRetries,
|
1000
|
+
abortSignal
|
1001
|
+
}) {
|
1002
|
+
if (maxRetries != null) {
|
1003
|
+
if (!Number.isInteger(maxRetries)) {
|
1004
|
+
throw new InvalidArgumentError({
|
1005
|
+
parameter: "maxRetries",
|
1006
|
+
value: maxRetries,
|
1007
|
+
message: "maxRetries must be an integer"
|
1008
|
+
});
|
1427
1009
|
}
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
|
1434
|
-
mediaType,
|
1435
|
-
filename: part.filename,
|
1436
|
-
data,
|
1437
|
-
providerOptions: part.providerOptions
|
1438
|
-
};
|
1010
|
+
if (maxRetries < 0) {
|
1011
|
+
throw new InvalidArgumentError({
|
1012
|
+
parameter: "maxRetries",
|
1013
|
+
value: maxRetries,
|
1014
|
+
message: "maxRetries must be >= 0"
|
1015
|
+
});
|
1439
1016
|
}
|
1440
1017
|
}
|
1441
|
-
|
1442
|
-
|
1443
|
-
// core/types/usage.ts
|
1444
|
-
function calculateLanguageModelUsage({
|
1445
|
-
inputTokens,
|
1446
|
-
outputTokens
|
1447
|
-
}) {
|
1018
|
+
const maxRetriesResult = maxRetries != null ? maxRetries : 2;
|
1448
1019
|
return {
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1020
|
+
maxRetries: maxRetriesResult,
|
1021
|
+
retry: retryWithExponentialBackoffRespectingRetryHeaders({
|
1022
|
+
maxRetries: maxRetriesResult,
|
1023
|
+
abortSignal
|
1024
|
+
})
|
1452
1025
|
};
|
1453
1026
|
}
|
1454
|
-
|
1455
|
-
// util/constants.ts
|
1456
|
-
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
1457
1027
|
// Annotate the CommonJS export names for ESM import in node:
|
1458
1028
|
0 && (module.exports = {
|
1459
|
-
|
1460
|
-
calculateLanguageModelUsage,
|
1029
|
+
convertAsyncIteratorToReadableStream,
|
1461
1030
|
convertToLanguageModelPrompt,
|
1462
1031
|
prepareCallSettings,
|
1463
1032
|
prepareRetries,
|