ai 5.0.0-canary.1 → 5.0.0-canary.10
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 +136 -0
- package/dist/index.d.mts +1449 -718
- package/dist/index.d.ts +1449 -718
- package/dist/index.js +2550 -760
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2423 -670
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +730 -0
- package/dist/internal/index.d.ts +730 -0
- package/dist/internal/index.js +1482 -0
- package/dist/internal/index.js.map +1 -0
- package/{rsc/dist/rsc-server.mjs → dist/internal/index.mjs} +855 -1555
- package/dist/internal/index.mjs.map +1 -0
- package/{mcp-stdio/dist → dist/mcp-stdio}/index.js +1 -1
- package/dist/mcp-stdio/index.js.map +1 -0
- package/{mcp-stdio/dist → dist/mcp-stdio}/index.mjs +1 -1
- package/dist/mcp-stdio/index.mjs.map +1 -0
- package/{test/dist → dist/test}/index.d.mts +18 -16
- package/{test/dist → dist/test}/index.d.ts +18 -16
- package/{test/dist → dist/test}/index.js +28 -8
- package/dist/test/index.js.map +1 -0
- package/{test/dist → dist/test}/index.mjs +27 -7
- package/dist/test/index.mjs.map +1 -0
- package/package.json +28 -47
- package/mcp-stdio/create-child-process.test.ts +0 -92
- package/mcp-stdio/create-child-process.ts +0 -21
- package/mcp-stdio/dist/index.js.map +0 -1
- package/mcp-stdio/dist/index.mjs.map +0 -1
- package/mcp-stdio/get-environment.ts +0 -43
- package/mcp-stdio/index.ts +0 -4
- package/mcp-stdio/mcp-stdio-transport.test.ts +0 -262
- package/mcp-stdio/mcp-stdio-transport.ts +0 -157
- package/rsc/dist/index.d.ts +0 -813
- package/rsc/dist/index.mjs +0 -18
- package/rsc/dist/rsc-client.d.mts +0 -1
- package/rsc/dist/rsc-client.mjs +0 -18
- package/rsc/dist/rsc-client.mjs.map +0 -1
- package/rsc/dist/rsc-server.d.mts +0 -748
- package/rsc/dist/rsc-server.mjs.map +0 -1
- package/rsc/dist/rsc-shared.d.mts +0 -101
- package/rsc/dist/rsc-shared.mjs +0 -308
- package/rsc/dist/rsc-shared.mjs.map +0 -1
- package/test/dist/index.js.map +0 -1
- package/test/dist/index.mjs.map +0 -1
- package/{mcp-stdio/dist → dist/mcp-stdio}/index.d.mts +6 -6
- package/{mcp-stdio/dist → dist/mcp-stdio}/index.d.ts +6 -6
@@ -0,0 +1,1482 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __export = (target, all) => {
|
9
|
+
for (var name7 in all)
|
10
|
+
__defProp(target, name7, { get: all[name7], enumerable: true });
|
11
|
+
};
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
14
|
+
for (let key of __getOwnPropNames(from))
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
17
|
+
}
|
18
|
+
return to;
|
19
|
+
};
|
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
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
29
|
+
|
30
|
+
// internal/index.ts
|
31
|
+
var internal_exports = {};
|
32
|
+
__export(internal_exports, {
|
33
|
+
HANGING_STREAM_WARNING_TIME_MS: () => HANGING_STREAM_WARNING_TIME_MS,
|
34
|
+
calculateLanguageModelUsage: () => calculateLanguageModelUsage,
|
35
|
+
convertToLanguageModelPrompt: () => convertToLanguageModelPrompt,
|
36
|
+
prepareCallSettings: () => prepareCallSettings,
|
37
|
+
prepareRetries: () => prepareRetries,
|
38
|
+
prepareToolsAndToolChoice: () => prepareToolsAndToolChoice,
|
39
|
+
standardizePrompt: () => standardizePrompt
|
40
|
+
});
|
41
|
+
module.exports = __toCommonJS(internal_exports);
|
42
|
+
|
43
|
+
// core/prompt/standardize-prompt.ts
|
44
|
+
var import_provider4 = require("@ai-sdk/provider");
|
45
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
46
|
+
var import_zod7 = require("zod");
|
47
|
+
|
48
|
+
// core/prompt/data-content.ts
|
49
|
+
var import_provider2 = require("@ai-sdk/provider");
|
50
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
51
|
+
var import_zod = require("zod");
|
52
|
+
|
53
|
+
// core/prompt/invalid-data-content-error.ts
|
54
|
+
var import_provider = require("@ai-sdk/provider");
|
55
|
+
var name = "AI_InvalidDataContentError";
|
56
|
+
var marker = `vercel.ai.error.${name}`;
|
57
|
+
var symbol = Symbol.for(marker);
|
58
|
+
var _a;
|
59
|
+
var InvalidDataContentError = class extends import_provider.AISDKError {
|
60
|
+
constructor({
|
61
|
+
content,
|
62
|
+
cause,
|
63
|
+
message = `Invalid data content. Expected a base64 string, Uint8Array, ArrayBuffer, or Buffer, but got ${typeof content}.`
|
64
|
+
}) {
|
65
|
+
super({ name, message, cause });
|
66
|
+
this[_a] = true;
|
67
|
+
this.content = content;
|
68
|
+
}
|
69
|
+
static isInstance(error) {
|
70
|
+
return import_provider.AISDKError.hasMarker(error, marker);
|
71
|
+
}
|
72
|
+
};
|
73
|
+
_a = symbol;
|
74
|
+
|
75
|
+
// core/prompt/split-data-url.ts
|
76
|
+
function splitDataUrl(dataUrl) {
|
77
|
+
try {
|
78
|
+
const [header, base64Content] = dataUrl.split(",");
|
79
|
+
return {
|
80
|
+
mediaType: header.split(";")[0].split(":")[1],
|
81
|
+
base64Content
|
82
|
+
};
|
83
|
+
} catch (error) {
|
84
|
+
return {
|
85
|
+
mediaType: void 0,
|
86
|
+
base64Content: void 0
|
87
|
+
};
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
// core/prompt/data-content.ts
|
92
|
+
var dataContentSchema = import_zod.z.union([
|
93
|
+
import_zod.z.string(),
|
94
|
+
import_zod.z.instanceof(Uint8Array),
|
95
|
+
import_zod.z.instanceof(ArrayBuffer),
|
96
|
+
import_zod.z.custom(
|
97
|
+
// Buffer might not be available in some environments such as CloudFlare:
|
98
|
+
(value) => {
|
99
|
+
var _a7, _b;
|
100
|
+
return (_b = (_a7 = globalThis.Buffer) == null ? void 0 : _a7.isBuffer(value)) != null ? _b : false;
|
101
|
+
},
|
102
|
+
{ message: "Must be a Buffer" }
|
103
|
+
)
|
104
|
+
]);
|
105
|
+
function convertToLanguageModelV2DataContent(content) {
|
106
|
+
if (content instanceof Uint8Array) {
|
107
|
+
return { data: content, mediaType: void 0 };
|
108
|
+
}
|
109
|
+
if (content instanceof ArrayBuffer) {
|
110
|
+
return { data: new Uint8Array(content), mediaType: void 0 };
|
111
|
+
}
|
112
|
+
if (typeof content === "string") {
|
113
|
+
try {
|
114
|
+
content = new URL(content);
|
115
|
+
} catch (error) {
|
116
|
+
}
|
117
|
+
}
|
118
|
+
if (content instanceof URL && content.protocol === "data:") {
|
119
|
+
const { mediaType: dataUrlMediaType, base64Content } = splitDataUrl(
|
120
|
+
content.toString()
|
121
|
+
);
|
122
|
+
if (dataUrlMediaType == null || base64Content == null) {
|
123
|
+
throw new import_provider2.AISDKError({
|
124
|
+
name: "InvalidDataContentError",
|
125
|
+
message: `Invalid data URL format in content ${content.toString()}`
|
126
|
+
});
|
127
|
+
}
|
128
|
+
return { data: base64Content, mediaType: dataUrlMediaType };
|
129
|
+
}
|
130
|
+
return { data: content, mediaType: void 0 };
|
131
|
+
}
|
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
|
+
|
237
|
+
// core/prompt/message-conversion-error.ts
|
238
|
+
var import_provider3 = require("@ai-sdk/provider");
|
239
|
+
var name2 = "AI_MessageConversionError";
|
240
|
+
var marker2 = `vercel.ai.error.${name2}`;
|
241
|
+
var symbol2 = Symbol.for(marker2);
|
242
|
+
var _a2;
|
243
|
+
var MessageConversionError = class extends import_provider3.AISDKError {
|
244
|
+
constructor({
|
245
|
+
originalMessage,
|
246
|
+
message
|
247
|
+
}) {
|
248
|
+
super({ name: name2, message });
|
249
|
+
this[_a2] = true;
|
250
|
+
this.originalMessage = originalMessage;
|
251
|
+
}
|
252
|
+
static isInstance(error) {
|
253
|
+
return import_provider3.AISDKError.hasMarker(error, marker2);
|
254
|
+
}
|
255
|
+
};
|
256
|
+
_a2 = symbol2;
|
257
|
+
|
258
|
+
// core/prompt/convert-to-core-messages.ts
|
259
|
+
function convertToCoreMessages(messages, options) {
|
260
|
+
var _a7, _b;
|
261
|
+
const tools = (_a7 = options == null ? void 0 : options.tools) != null ? _a7 : {};
|
262
|
+
const coreMessages = [];
|
263
|
+
for (let i = 0; i < messages.length; i++) {
|
264
|
+
const message = messages[i];
|
265
|
+
const isLastMessage = i === messages.length - 1;
|
266
|
+
const { role, content, experimental_attachments } = message;
|
267
|
+
switch (role) {
|
268
|
+
case "system": {
|
269
|
+
coreMessages.push({
|
270
|
+
role: "system",
|
271
|
+
content
|
272
|
+
});
|
273
|
+
break;
|
274
|
+
}
|
275
|
+
case "user": {
|
276
|
+
if (message.parts == null) {
|
277
|
+
coreMessages.push({
|
278
|
+
role: "user",
|
279
|
+
content: experimental_attachments ? [
|
280
|
+
{ type: "text", text: content },
|
281
|
+
...attachmentsToParts(experimental_attachments)
|
282
|
+
] : content
|
283
|
+
});
|
284
|
+
} else {
|
285
|
+
const textParts = message.parts.filter((part) => part.type === "text").map((part) => ({
|
286
|
+
type: "text",
|
287
|
+
text: part.text
|
288
|
+
}));
|
289
|
+
coreMessages.push({
|
290
|
+
role: "user",
|
291
|
+
content: experimental_attachments ? [...textParts, ...attachmentsToParts(experimental_attachments)] : textParts
|
292
|
+
});
|
293
|
+
}
|
294
|
+
break;
|
295
|
+
}
|
296
|
+
case "assistant": {
|
297
|
+
if (message.parts != null) {
|
298
|
+
let processBlock2 = function() {
|
299
|
+
var _a8;
|
300
|
+
const content2 = [];
|
301
|
+
for (const part of block) {
|
302
|
+
switch (part.type) {
|
303
|
+
case "text": {
|
304
|
+
content2.push(part);
|
305
|
+
break;
|
306
|
+
}
|
307
|
+
case "file": {
|
308
|
+
content2.push({
|
309
|
+
type: "file",
|
310
|
+
data: part.data,
|
311
|
+
mediaType: (_a8 = part.mediaType) != null ? _a8 : part.mimeType
|
312
|
+
// TODO migration, remove
|
313
|
+
});
|
314
|
+
break;
|
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
|
+
}
|
349
|
+
}
|
350
|
+
coreMessages.push({
|
351
|
+
role: "assistant",
|
352
|
+
content: content2
|
353
|
+
});
|
354
|
+
const stepInvocations = block.filter(
|
355
|
+
(part) => part.type === "tool-invocation"
|
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
|
+
});
|
385
|
+
}
|
386
|
+
block = [];
|
387
|
+
blockHasToolInvocations = false;
|
388
|
+
currentStep++;
|
389
|
+
};
|
390
|
+
var processBlock = processBlock2;
|
391
|
+
let currentStep = 0;
|
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
|
+
}
|
416
|
+
}
|
417
|
+
}
|
418
|
+
processBlock2();
|
419
|
+
break;
|
420
|
+
}
|
421
|
+
const toolInvocations = message.toolInvocations;
|
422
|
+
if (toolInvocations == null || toolInvocations.length === 0) {
|
423
|
+
coreMessages.push({ role: "assistant", content });
|
424
|
+
break;
|
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;
|
435
|
+
}
|
436
|
+
);
|
437
|
+
if (stepInvocations.length === 0) {
|
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
|
+
} : {
|
472
|
+
type: "tool-result",
|
473
|
+
toolCallId,
|
474
|
+
toolName,
|
475
|
+
result
|
476
|
+
};
|
477
|
+
})
|
478
|
+
});
|
479
|
+
}
|
480
|
+
if (content && !isLastMessage) {
|
481
|
+
coreMessages.push({ role: "assistant", content });
|
482
|
+
}
|
483
|
+
break;
|
484
|
+
}
|
485
|
+
case "data": {
|
486
|
+
break;
|
487
|
+
}
|
488
|
+
default: {
|
489
|
+
const _exhaustiveCheck = role;
|
490
|
+
throw new MessageConversionError({
|
491
|
+
originalMessage: message,
|
492
|
+
message: `Unsupported role: ${_exhaustiveCheck}`
|
493
|
+
});
|
494
|
+
}
|
495
|
+
}
|
496
|
+
}
|
497
|
+
return coreMessages;
|
498
|
+
}
|
499
|
+
|
500
|
+
// core/prompt/detect-prompt-type.ts
|
501
|
+
function detectPromptType(prompt) {
|
502
|
+
if (!Array.isArray(prompt)) {
|
503
|
+
return "other";
|
504
|
+
}
|
505
|
+
if (prompt.length === 0) {
|
506
|
+
return "messages";
|
507
|
+
}
|
508
|
+
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
509
|
+
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
510
|
+
return "ui-messages";
|
511
|
+
} else if (characteristics.every(
|
512
|
+
(c) => c === "has-core-specific-parts" || c === "message"
|
513
|
+
)) {
|
514
|
+
return "messages";
|
515
|
+
} else {
|
516
|
+
return "other";
|
517
|
+
}
|
518
|
+
}
|
519
|
+
function detectSingleMessageCharacteristics(message) {
|
520
|
+
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
521
|
+
message.role === "data" || // UI-only role
|
522
|
+
"toolInvocations" in message || // UI-specific field
|
523
|
+
"parts" in message || // UI-specific field
|
524
|
+
"experimental_attachments" in message)) {
|
525
|
+
return "has-ui-specific-parts";
|
526
|
+
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
527
|
+
"providerOptions" in message)) {
|
528
|
+
return "has-core-specific-parts";
|
529
|
+
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
530
|
+
return "message";
|
531
|
+
} else {
|
532
|
+
return "other";
|
533
|
+
}
|
534
|
+
}
|
535
|
+
|
536
|
+
// core/prompt/message.ts
|
537
|
+
var import_zod6 = require("zod");
|
538
|
+
|
539
|
+
// core/types/provider-metadata.ts
|
540
|
+
var import_zod3 = require("zod");
|
541
|
+
|
542
|
+
// core/types/json-value.ts
|
543
|
+
var import_zod2 = require("zod");
|
544
|
+
var jsonValueSchema = import_zod2.z.lazy(
|
545
|
+
() => import_zod2.z.union([
|
546
|
+
import_zod2.z.null(),
|
547
|
+
import_zod2.z.string(),
|
548
|
+
import_zod2.z.number(),
|
549
|
+
import_zod2.z.boolean(),
|
550
|
+
import_zod2.z.record(import_zod2.z.string(), jsonValueSchema),
|
551
|
+
import_zod2.z.array(jsonValueSchema)
|
552
|
+
])
|
553
|
+
);
|
554
|
+
|
555
|
+
// core/types/provider-metadata.ts
|
556
|
+
var providerMetadataSchema = import_zod3.z.record(
|
557
|
+
import_zod3.z.string(),
|
558
|
+
import_zod3.z.record(import_zod3.z.string(), jsonValueSchema)
|
559
|
+
);
|
560
|
+
|
561
|
+
// core/prompt/content-part.ts
|
562
|
+
var import_zod5 = require("zod");
|
563
|
+
|
564
|
+
// core/prompt/tool-result-content.ts
|
565
|
+
var import_zod4 = require("zod");
|
566
|
+
var toolResultContentSchema = import_zod4.z.array(
|
567
|
+
import_zod4.z.union([
|
568
|
+
import_zod4.z.object({ type: import_zod4.z.literal("text"), text: import_zod4.z.string() }),
|
569
|
+
import_zod4.z.object({
|
570
|
+
type: import_zod4.z.literal("image"),
|
571
|
+
data: import_zod4.z.string(),
|
572
|
+
mediaType: import_zod4.z.string().optional()
|
573
|
+
})
|
574
|
+
])
|
575
|
+
);
|
576
|
+
|
577
|
+
// core/prompt/content-part.ts
|
578
|
+
var textPartSchema = import_zod5.z.object({
|
579
|
+
type: import_zod5.z.literal("text"),
|
580
|
+
text: import_zod5.z.string(),
|
581
|
+
providerOptions: providerMetadataSchema.optional()
|
582
|
+
});
|
583
|
+
var imagePartSchema = import_zod5.z.object({
|
584
|
+
type: import_zod5.z.literal("image"),
|
585
|
+
image: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
586
|
+
mediaType: import_zod5.z.string().optional(),
|
587
|
+
mimeType: import_zod5.z.string().optional(),
|
588
|
+
providerOptions: providerMetadataSchema.optional()
|
589
|
+
});
|
590
|
+
var filePartSchema = import_zod5.z.object({
|
591
|
+
type: import_zod5.z.literal("file"),
|
592
|
+
data: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
593
|
+
filename: import_zod5.z.string().optional(),
|
594
|
+
mediaType: import_zod5.z.string(),
|
595
|
+
mimeType: import_zod5.z.string().optional(),
|
596
|
+
providerOptions: providerMetadataSchema.optional()
|
597
|
+
});
|
598
|
+
var reasoningPartSchema = import_zod5.z.object({
|
599
|
+
type: import_zod5.z.literal("reasoning"),
|
600
|
+
text: import_zod5.z.string(),
|
601
|
+
providerOptions: providerMetadataSchema.optional()
|
602
|
+
});
|
603
|
+
var redactedReasoningPartSchema = import_zod5.z.object({
|
604
|
+
type: import_zod5.z.literal("redacted-reasoning"),
|
605
|
+
data: import_zod5.z.string(),
|
606
|
+
providerOptions: providerMetadataSchema.optional()
|
607
|
+
});
|
608
|
+
var toolCallPartSchema = import_zod5.z.object({
|
609
|
+
type: import_zod5.z.literal("tool-call"),
|
610
|
+
toolCallId: import_zod5.z.string(),
|
611
|
+
toolName: import_zod5.z.string(),
|
612
|
+
args: import_zod5.z.unknown(),
|
613
|
+
providerOptions: providerMetadataSchema.optional()
|
614
|
+
});
|
615
|
+
var toolResultPartSchema = import_zod5.z.object({
|
616
|
+
type: import_zod5.z.literal("tool-result"),
|
617
|
+
toolCallId: import_zod5.z.string(),
|
618
|
+
toolName: import_zod5.z.string(),
|
619
|
+
result: import_zod5.z.unknown(),
|
620
|
+
content: toolResultContentSchema.optional(),
|
621
|
+
isError: import_zod5.z.boolean().optional(),
|
622
|
+
providerOptions: providerMetadataSchema.optional()
|
623
|
+
});
|
624
|
+
|
625
|
+
// core/prompt/message.ts
|
626
|
+
var coreSystemMessageSchema = import_zod6.z.object({
|
627
|
+
role: import_zod6.z.literal("system"),
|
628
|
+
content: import_zod6.z.string(),
|
629
|
+
providerOptions: providerMetadataSchema.optional()
|
630
|
+
});
|
631
|
+
var coreUserMessageSchema = import_zod6.z.object({
|
632
|
+
role: import_zod6.z.literal("user"),
|
633
|
+
content: import_zod6.z.union([
|
634
|
+
import_zod6.z.string(),
|
635
|
+
import_zod6.z.array(import_zod6.z.union([textPartSchema, imagePartSchema, filePartSchema]))
|
636
|
+
]),
|
637
|
+
providerOptions: providerMetadataSchema.optional()
|
638
|
+
});
|
639
|
+
var coreAssistantMessageSchema = import_zod6.z.object({
|
640
|
+
role: import_zod6.z.literal("assistant"),
|
641
|
+
content: import_zod6.z.union([
|
642
|
+
import_zod6.z.string(),
|
643
|
+
import_zod6.z.array(
|
644
|
+
import_zod6.z.union([
|
645
|
+
textPartSchema,
|
646
|
+
filePartSchema,
|
647
|
+
reasoningPartSchema,
|
648
|
+
redactedReasoningPartSchema,
|
649
|
+
toolCallPartSchema
|
650
|
+
])
|
651
|
+
)
|
652
|
+
]),
|
653
|
+
providerOptions: providerMetadataSchema.optional()
|
654
|
+
});
|
655
|
+
var coreToolMessageSchema = import_zod6.z.object({
|
656
|
+
role: import_zod6.z.literal("tool"),
|
657
|
+
content: import_zod6.z.array(toolResultPartSchema),
|
658
|
+
providerOptions: providerMetadataSchema.optional()
|
659
|
+
});
|
660
|
+
var coreMessageSchema = import_zod6.z.union([
|
661
|
+
coreSystemMessageSchema,
|
662
|
+
coreUserMessageSchema,
|
663
|
+
coreAssistantMessageSchema,
|
664
|
+
coreToolMessageSchema
|
665
|
+
]);
|
666
|
+
|
667
|
+
// core/prompt/standardize-prompt.ts
|
668
|
+
function standardizePrompt({
|
669
|
+
prompt,
|
670
|
+
tools
|
671
|
+
}) {
|
672
|
+
if (prompt.prompt == null && prompt.messages == null) {
|
673
|
+
throw new import_provider4.InvalidPromptError({
|
674
|
+
prompt,
|
675
|
+
message: "prompt or messages must be defined"
|
676
|
+
});
|
677
|
+
}
|
678
|
+
if (prompt.prompt != null && prompt.messages != null) {
|
679
|
+
throw new import_provider4.InvalidPromptError({
|
680
|
+
prompt,
|
681
|
+
message: "prompt and messages cannot be defined at the same time"
|
682
|
+
});
|
683
|
+
}
|
684
|
+
if (prompt.system != null && typeof prompt.system !== "string") {
|
685
|
+
throw new import_provider4.InvalidPromptError({
|
686
|
+
prompt,
|
687
|
+
message: "system must be a string"
|
688
|
+
});
|
689
|
+
}
|
690
|
+
if (prompt.prompt != null) {
|
691
|
+
if (typeof prompt.prompt !== "string") {
|
692
|
+
throw new import_provider4.InvalidPromptError({
|
693
|
+
prompt,
|
694
|
+
message: "prompt must be a string"
|
695
|
+
});
|
696
|
+
}
|
697
|
+
return {
|
698
|
+
type: "prompt",
|
699
|
+
system: prompt.system,
|
700
|
+
messages: [
|
701
|
+
{
|
702
|
+
role: "user",
|
703
|
+
content: prompt.prompt
|
704
|
+
}
|
705
|
+
]
|
706
|
+
};
|
707
|
+
}
|
708
|
+
if (prompt.messages != null) {
|
709
|
+
const promptType = detectPromptType(prompt.messages);
|
710
|
+
if (promptType === "other") {
|
711
|
+
throw new import_provider4.InvalidPromptError({
|
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)
|
728
|
+
});
|
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
|
+
}
|
742
|
+
throw new Error("unreachable");
|
743
|
+
}
|
744
|
+
|
745
|
+
// core/util/index.ts
|
746
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
747
|
+
|
748
|
+
// core/util/schema.ts
|
749
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
750
|
+
|
751
|
+
// core/util/zod-schema.ts
|
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
|
+
};
|
811
|
+
}
|
812
|
+
const filteredTools = activeTools != null ? Object.entries(tools).filter(
|
813
|
+
([name7]) => activeTools.includes(name7)
|
814
|
+
) : Object.entries(tools);
|
815
|
+
return {
|
816
|
+
tools: filteredTools.map(([name7, tool]) => {
|
817
|
+
const toolType = tool.type;
|
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 }
|
841
|
+
};
|
842
|
+
}
|
843
|
+
|
844
|
+
// errors/invalid-argument-error.ts
|
845
|
+
var import_provider5 = require("@ai-sdk/provider");
|
846
|
+
var name3 = "AI_InvalidArgumentError";
|
847
|
+
var marker3 = `vercel.ai.error.${name3}`;
|
848
|
+
var symbol3 = Symbol.for(marker3);
|
849
|
+
var _a3;
|
850
|
+
var InvalidArgumentError = class extends import_provider5.AISDKError {
|
851
|
+
constructor({
|
852
|
+
parameter,
|
853
|
+
value,
|
854
|
+
message
|
855
|
+
}) {
|
856
|
+
super({
|
857
|
+
name: name3,
|
858
|
+
message: `Invalid argument for parameter ${parameter}: ${message}`
|
859
|
+
});
|
860
|
+
this[_a3] = true;
|
861
|
+
this.parameter = parameter;
|
862
|
+
this.value = value;
|
863
|
+
}
|
864
|
+
static isInstance(error) {
|
865
|
+
return import_provider5.AISDKError.hasMarker(error, marker3);
|
866
|
+
}
|
867
|
+
};
|
868
|
+
_a3 = symbol3;
|
869
|
+
|
870
|
+
// util/retry-with-exponential-backoff.ts
|
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
|
979
|
+
function prepareCallSettings({
|
980
|
+
maxOutputTokens,
|
981
|
+
temperature,
|
982
|
+
topP,
|
983
|
+
topK,
|
984
|
+
presencePenalty,
|
985
|
+
frequencyPenalty,
|
986
|
+
stopSequences,
|
987
|
+
seed
|
988
|
+
}) {
|
989
|
+
if (maxOutputTokens != null) {
|
990
|
+
if (!Number.isInteger(maxOutputTokens)) {
|
991
|
+
throw new InvalidArgumentError({
|
992
|
+
parameter: "maxOutputTokens",
|
993
|
+
value: maxOutputTokens,
|
994
|
+
message: "maxOutputTokens must be an integer"
|
995
|
+
});
|
996
|
+
}
|
997
|
+
if (maxOutputTokens < 1) {
|
998
|
+
throw new InvalidArgumentError({
|
999
|
+
parameter: "maxOutputTokens",
|
1000
|
+
value: maxOutputTokens,
|
1001
|
+
message: "maxOutputTokens must be >= 1"
|
1002
|
+
});
|
1003
|
+
}
|
1004
|
+
}
|
1005
|
+
if (temperature != null) {
|
1006
|
+
if (typeof temperature !== "number") {
|
1007
|
+
throw new InvalidArgumentError({
|
1008
|
+
parameter: "temperature",
|
1009
|
+
value: temperature,
|
1010
|
+
message: "temperature must be a number"
|
1011
|
+
});
|
1012
|
+
}
|
1013
|
+
}
|
1014
|
+
if (topP != null) {
|
1015
|
+
if (typeof topP !== "number") {
|
1016
|
+
throw new InvalidArgumentError({
|
1017
|
+
parameter: "topP",
|
1018
|
+
value: topP,
|
1019
|
+
message: "topP must be a number"
|
1020
|
+
});
|
1021
|
+
}
|
1022
|
+
}
|
1023
|
+
if (topK != null) {
|
1024
|
+
if (typeof topK !== "number") {
|
1025
|
+
throw new InvalidArgumentError({
|
1026
|
+
parameter: "topK",
|
1027
|
+
value: topK,
|
1028
|
+
message: "topK must be a number"
|
1029
|
+
});
|
1030
|
+
}
|
1031
|
+
}
|
1032
|
+
if (presencePenalty != null) {
|
1033
|
+
if (typeof presencePenalty !== "number") {
|
1034
|
+
throw new InvalidArgumentError({
|
1035
|
+
parameter: "presencePenalty",
|
1036
|
+
value: presencePenalty,
|
1037
|
+
message: "presencePenalty must be a number"
|
1038
|
+
});
|
1039
|
+
}
|
1040
|
+
}
|
1041
|
+
if (frequencyPenalty != null) {
|
1042
|
+
if (typeof frequencyPenalty !== "number") {
|
1043
|
+
throw new InvalidArgumentError({
|
1044
|
+
parameter: "frequencyPenalty",
|
1045
|
+
value: frequencyPenalty,
|
1046
|
+
message: "frequencyPenalty must be a number"
|
1047
|
+
});
|
1048
|
+
}
|
1049
|
+
}
|
1050
|
+
if (seed != null) {
|
1051
|
+
if (!Number.isInteger(seed)) {
|
1052
|
+
throw new InvalidArgumentError({
|
1053
|
+
parameter: "seed",
|
1054
|
+
value: seed,
|
1055
|
+
message: "seed must be an integer"
|
1056
|
+
});
|
1057
|
+
}
|
1058
|
+
}
|
1059
|
+
return {
|
1060
|
+
maxOutputTokens,
|
1061
|
+
// TODO v5 remove default 0 for temperature
|
1062
|
+
temperature: temperature != null ? temperature : 0,
|
1063
|
+
topP,
|
1064
|
+
topK,
|
1065
|
+
presencePenalty,
|
1066
|
+
frequencyPenalty,
|
1067
|
+
stopSequences: stopSequences != null && stopSequences.length > 0 ? stopSequences : void 0,
|
1068
|
+
seed
|
1069
|
+
};
|
1070
|
+
}
|
1071
|
+
|
1072
|
+
// util/download-error.ts
|
1073
|
+
var import_provider8 = require("@ai-sdk/provider");
|
1074
|
+
var name5 = "AI_DownloadError";
|
1075
|
+
var marker5 = `vercel.ai.error.${name5}`;
|
1076
|
+
var symbol5 = Symbol.for(marker5);
|
1077
|
+
var _a5;
|
1078
|
+
var DownloadError = class extends import_provider8.AISDKError {
|
1079
|
+
constructor({
|
1080
|
+
url,
|
1081
|
+
statusCode,
|
1082
|
+
statusText,
|
1083
|
+
cause,
|
1084
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
1085
|
+
}) {
|
1086
|
+
super({ name: name5, message, cause });
|
1087
|
+
this[_a5] = true;
|
1088
|
+
this.url = url;
|
1089
|
+
this.statusCode = statusCode;
|
1090
|
+
this.statusText = statusText;
|
1091
|
+
}
|
1092
|
+
static isInstance(error) {
|
1093
|
+
return import_provider8.AISDKError.hasMarker(error, marker5);
|
1094
|
+
}
|
1095
|
+
};
|
1096
|
+
_a5 = symbol5;
|
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
|
+
}
|
1122
|
+
|
1123
|
+
// core/util/detect-media-type.ts
|
1124
|
+
var import_provider_utils6 = require("@ai-sdk/provider-utils");
|
1125
|
+
var imageMediaTypeSignatures = [
|
1126
|
+
{
|
1127
|
+
mediaType: "image/gif",
|
1128
|
+
bytesPrefix: [71, 73, 70],
|
1129
|
+
base64Prefix: "R0lG"
|
1130
|
+
},
|
1131
|
+
{
|
1132
|
+
mediaType: "image/png",
|
1133
|
+
bytesPrefix: [137, 80, 78, 71],
|
1134
|
+
base64Prefix: "iVBORw"
|
1135
|
+
},
|
1136
|
+
{
|
1137
|
+
mediaType: "image/jpeg",
|
1138
|
+
bytesPrefix: [255, 216],
|
1139
|
+
base64Prefix: "/9j/"
|
1140
|
+
},
|
1141
|
+
{
|
1142
|
+
mediaType: "image/webp",
|
1143
|
+
bytesPrefix: [82, 73, 70, 70],
|
1144
|
+
base64Prefix: "UklGRg"
|
1145
|
+
},
|
1146
|
+
{
|
1147
|
+
mediaType: "image/bmp",
|
1148
|
+
bytesPrefix: [66, 77],
|
1149
|
+
base64Prefix: "Qk"
|
1150
|
+
},
|
1151
|
+
{
|
1152
|
+
mediaType: "image/tiff",
|
1153
|
+
bytesPrefix: [73, 73, 42, 0],
|
1154
|
+
base64Prefix: "SUkqAA"
|
1155
|
+
},
|
1156
|
+
{
|
1157
|
+
mediaType: "image/tiff",
|
1158
|
+
bytesPrefix: [77, 77, 0, 42],
|
1159
|
+
base64Prefix: "TU0AKg"
|
1160
|
+
},
|
1161
|
+
{
|
1162
|
+
mediaType: "image/avif",
|
1163
|
+
bytesPrefix: [
|
1164
|
+
0,
|
1165
|
+
0,
|
1166
|
+
0,
|
1167
|
+
32,
|
1168
|
+
102,
|
1169
|
+
116,
|
1170
|
+
121,
|
1171
|
+
112,
|
1172
|
+
97,
|
1173
|
+
118,
|
1174
|
+
105,
|
1175
|
+
102
|
1176
|
+
],
|
1177
|
+
base64Prefix: "AAAAIGZ0eXBhdmlm"
|
1178
|
+
},
|
1179
|
+
{
|
1180
|
+
mediaType: "image/heic",
|
1181
|
+
bytesPrefix: [
|
1182
|
+
0,
|
1183
|
+
0,
|
1184
|
+
0,
|
1185
|
+
32,
|
1186
|
+
102,
|
1187
|
+
116,
|
1188
|
+
121,
|
1189
|
+
112,
|
1190
|
+
104,
|
1191
|
+
101,
|
1192
|
+
105,
|
1193
|
+
99
|
1194
|
+
],
|
1195
|
+
base64Prefix: "AAAAIGZ0eXBoZWlj"
|
1196
|
+
}
|
1197
|
+
];
|
1198
|
+
var stripID3 = (data) => {
|
1199
|
+
const bytes = typeof data === "string" ? (0, import_provider_utils6.convertBase64ToUint8Array)(data) : data;
|
1200
|
+
const id3Size = (bytes[6] & 127) << 21 | (bytes[7] & 127) << 14 | (bytes[8] & 127) << 7 | bytes[9] & 127;
|
1201
|
+
return bytes.slice(id3Size + 10);
|
1202
|
+
};
|
1203
|
+
function stripID3TagsIfPresent(data) {
|
1204
|
+
const hasId3 = typeof data === "string" && data.startsWith("SUQz") || typeof data !== "string" && data.length > 10 && data[0] === 73 && // 'I'
|
1205
|
+
data[1] === 68 && // 'D'
|
1206
|
+
data[2] === 51;
|
1207
|
+
return hasId3 ? stripID3(data) : data;
|
1208
|
+
}
|
1209
|
+
function detectMediaType({
|
1210
|
+
data,
|
1211
|
+
signatures
|
1212
|
+
}) {
|
1213
|
+
const processedData = stripID3TagsIfPresent(data);
|
1214
|
+
for (const signature of signatures) {
|
1215
|
+
if (typeof processedData === "string" ? processedData.startsWith(signature.base64Prefix) : processedData.length >= signature.bytesPrefix.length && signature.bytesPrefix.every(
|
1216
|
+
(byte, index) => processedData[index] === byte
|
1217
|
+
)) {
|
1218
|
+
return signature.mediaType;
|
1219
|
+
}
|
1220
|
+
}
|
1221
|
+
return void 0;
|
1222
|
+
}
|
1223
|
+
|
1224
|
+
// core/prompt/invalid-message-role-error.ts
|
1225
|
+
var import_provider9 = require("@ai-sdk/provider");
|
1226
|
+
var name6 = "AI_InvalidMessageRoleError";
|
1227
|
+
var marker6 = `vercel.ai.error.${name6}`;
|
1228
|
+
var symbol6 = Symbol.for(marker6);
|
1229
|
+
var _a6;
|
1230
|
+
var InvalidMessageRoleError = class extends import_provider9.AISDKError {
|
1231
|
+
constructor({
|
1232
|
+
role,
|
1233
|
+
message = `Invalid message role: '${role}'. Must be one of: "system", "user", "assistant", "tool".`
|
1234
|
+
}) {
|
1235
|
+
super({ name: name6, message });
|
1236
|
+
this[_a6] = true;
|
1237
|
+
this.role = role;
|
1238
|
+
}
|
1239
|
+
static isInstance(error) {
|
1240
|
+
return import_provider9.AISDKError.hasMarker(error, marker6);
|
1241
|
+
}
|
1242
|
+
};
|
1243
|
+
_a6 = symbol6;
|
1244
|
+
|
1245
|
+
// core/prompt/convert-to-language-model-prompt.ts
|
1246
|
+
async function convertToLanguageModelPrompt({
|
1247
|
+
prompt,
|
1248
|
+
modelSupportsImageUrls = true,
|
1249
|
+
modelSupportsUrl = () => false,
|
1250
|
+
downloadImplementation = download
|
1251
|
+
}) {
|
1252
|
+
const downloadedAssets = await downloadAssets(
|
1253
|
+
prompt.messages,
|
1254
|
+
downloadImplementation,
|
1255
|
+
modelSupportsImageUrls,
|
1256
|
+
modelSupportsUrl
|
1257
|
+
);
|
1258
|
+
return [
|
1259
|
+
...prompt.system != null ? [{ role: "system", content: prompt.system }] : [],
|
1260
|
+
...prompt.messages.map(
|
1261
|
+
(message) => convertToLanguageModelMessage(message, downloadedAssets)
|
1262
|
+
)
|
1263
|
+
];
|
1264
|
+
}
|
1265
|
+
function convertToLanguageModelMessage(message, downloadedAssets) {
|
1266
|
+
const role = message.role;
|
1267
|
+
switch (role) {
|
1268
|
+
case "system": {
|
1269
|
+
return {
|
1270
|
+
role: "system",
|
1271
|
+
content: message.content,
|
1272
|
+
providerOptions: message.providerOptions
|
1273
|
+
};
|
1274
|
+
}
|
1275
|
+
case "user": {
|
1276
|
+
if (typeof message.content === "string") {
|
1277
|
+
return {
|
1278
|
+
role: "user",
|
1279
|
+
content: [{ type: "text", text: message.content }],
|
1280
|
+
providerOptions: message.providerOptions
|
1281
|
+
};
|
1282
|
+
}
|
1283
|
+
return {
|
1284
|
+
role: "user",
|
1285
|
+
content: message.content.map((part) => convertPartToLanguageModelPart(part, downloadedAssets)).filter((part) => part.type !== "text" || part.text !== ""),
|
1286
|
+
providerOptions: message.providerOptions
|
1287
|
+
};
|
1288
|
+
}
|
1289
|
+
case "assistant": {
|
1290
|
+
if (typeof message.content === "string") {
|
1291
|
+
return {
|
1292
|
+
role: "assistant",
|
1293
|
+
content: [{ type: "text", text: message.content }],
|
1294
|
+
providerOptions: message.providerOptions
|
1295
|
+
};
|
1296
|
+
}
|
1297
|
+
return {
|
1298
|
+
role: "assistant",
|
1299
|
+
content: message.content.filter(
|
1300
|
+
// remove empty text parts:
|
1301
|
+
(part) => part.type !== "text" || part.text !== ""
|
1302
|
+
).map((part) => {
|
1303
|
+
var _a7;
|
1304
|
+
const providerOptions = part.providerOptions;
|
1305
|
+
switch (part.type) {
|
1306
|
+
case "file": {
|
1307
|
+
const { data, mediaType } = convertToLanguageModelV2DataContent(
|
1308
|
+
part.data
|
1309
|
+
);
|
1310
|
+
return {
|
1311
|
+
type: "file",
|
1312
|
+
data,
|
1313
|
+
filename: part.filename,
|
1314
|
+
mediaType: (_a7 = mediaType != null ? mediaType : part.mediaType) != null ? _a7 : part.mimeType,
|
1315
|
+
providerOptions
|
1316
|
+
};
|
1317
|
+
}
|
1318
|
+
case "reasoning": {
|
1319
|
+
return {
|
1320
|
+
type: "reasoning",
|
1321
|
+
text: part.text,
|
1322
|
+
signature: part.signature,
|
1323
|
+
providerOptions
|
1324
|
+
};
|
1325
|
+
}
|
1326
|
+
case "redacted-reasoning": {
|
1327
|
+
return {
|
1328
|
+
type: "redacted-reasoning",
|
1329
|
+
data: part.data,
|
1330
|
+
providerOptions
|
1331
|
+
};
|
1332
|
+
}
|
1333
|
+
case "text": {
|
1334
|
+
return {
|
1335
|
+
type: "text",
|
1336
|
+
text: part.text,
|
1337
|
+
providerOptions
|
1338
|
+
};
|
1339
|
+
}
|
1340
|
+
case "tool-call": {
|
1341
|
+
return {
|
1342
|
+
type: "tool-call",
|
1343
|
+
toolCallId: part.toolCallId,
|
1344
|
+
toolName: part.toolName,
|
1345
|
+
args: part.args,
|
1346
|
+
providerOptions
|
1347
|
+
};
|
1348
|
+
}
|
1349
|
+
}
|
1350
|
+
}),
|
1351
|
+
providerOptions: message.providerOptions
|
1352
|
+
};
|
1353
|
+
}
|
1354
|
+
case "tool": {
|
1355
|
+
return {
|
1356
|
+
role: "tool",
|
1357
|
+
content: message.content.map((part) => ({
|
1358
|
+
type: "tool-result",
|
1359
|
+
toolCallId: part.toolCallId,
|
1360
|
+
toolName: part.toolName,
|
1361
|
+
result: part.result,
|
1362
|
+
content: part.experimental_content,
|
1363
|
+
isError: part.isError,
|
1364
|
+
providerOptions: part.providerOptions
|
1365
|
+
})),
|
1366
|
+
providerOptions: message.providerOptions
|
1367
|
+
};
|
1368
|
+
}
|
1369
|
+
default: {
|
1370
|
+
const _exhaustiveCheck = role;
|
1371
|
+
throw new InvalidMessageRoleError({ role: _exhaustiveCheck });
|
1372
|
+
}
|
1373
|
+
}
|
1374
|
+
}
|
1375
|
+
async function downloadAssets(messages, downloadImplementation, modelSupportsImageUrls, modelSupportsUrl) {
|
1376
|
+
const urls = messages.filter((message) => message.role === "user").map((message) => message.content).filter(
|
1377
|
+
(content) => Array.isArray(content)
|
1378
|
+
).flat().filter(
|
1379
|
+
(part) => part.type === "image" || part.type === "file"
|
1380
|
+
).filter(
|
1381
|
+
(part) => !(part.type === "image" && modelSupportsImageUrls === true)
|
1382
|
+
).map((part) => part.type === "image" ? part.image : part.data).map(
|
1383
|
+
(part) => (
|
1384
|
+
// support string urls:
|
1385
|
+
typeof part === "string" && (part.startsWith("http:") || part.startsWith("https:")) ? new URL(part) : part
|
1386
|
+
)
|
1387
|
+
).filter((image) => image instanceof URL).filter((url) => !modelSupportsUrl(url));
|
1388
|
+
const downloadedImages = await Promise.all(
|
1389
|
+
urls.map(async (url) => ({
|
1390
|
+
url,
|
1391
|
+
data: await downloadImplementation({ url })
|
1392
|
+
}))
|
1393
|
+
);
|
1394
|
+
return Object.fromEntries(
|
1395
|
+
downloadedImages.map(({ url, data }) => [url.toString(), data])
|
1396
|
+
);
|
1397
|
+
}
|
1398
|
+
function convertPartToLanguageModelPart(part, downloadedAssets) {
|
1399
|
+
var _a7, _b, _c;
|
1400
|
+
if (part.type === "text") {
|
1401
|
+
return {
|
1402
|
+
type: "text",
|
1403
|
+
text: part.text,
|
1404
|
+
providerOptions: part.providerOptions
|
1405
|
+
};
|
1406
|
+
}
|
1407
|
+
let originalData;
|
1408
|
+
const type = part.type;
|
1409
|
+
switch (type) {
|
1410
|
+
case "image":
|
1411
|
+
originalData = part.image;
|
1412
|
+
break;
|
1413
|
+
case "file":
|
1414
|
+
originalData = part.data;
|
1415
|
+
break;
|
1416
|
+
default:
|
1417
|
+
throw new Error(`Unsupported part type: ${type}`);
|
1418
|
+
}
|
1419
|
+
const { data: convertedData, mediaType: convertedMediaType } = convertToLanguageModelV2DataContent(originalData);
|
1420
|
+
let mediaType = (_a7 = convertedMediaType != null ? convertedMediaType : part.mediaType) != null ? _a7 : part.mimeType;
|
1421
|
+
let data = convertedData;
|
1422
|
+
if (data instanceof URL) {
|
1423
|
+
const downloadedFile = downloadedAssets[data.toString()];
|
1424
|
+
if (downloadedFile) {
|
1425
|
+
data = downloadedFile.data;
|
1426
|
+
mediaType = (_b = downloadedFile.mediaType) != null ? _b : mediaType;
|
1427
|
+
}
|
1428
|
+
}
|
1429
|
+
switch (type) {
|
1430
|
+
case "image": {
|
1431
|
+
if (data instanceof Uint8Array || typeof data === "string") {
|
1432
|
+
mediaType = (_c = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _c : mediaType;
|
1433
|
+
}
|
1434
|
+
return {
|
1435
|
+
type: "file",
|
1436
|
+
mediaType: mediaType != null ? mediaType : "image/*",
|
1437
|
+
// any image
|
1438
|
+
filename: void 0,
|
1439
|
+
data,
|
1440
|
+
providerOptions: part.providerOptions
|
1441
|
+
};
|
1442
|
+
}
|
1443
|
+
case "file": {
|
1444
|
+
if (mediaType == null) {
|
1445
|
+
throw new Error(`Media type is missing for file part`);
|
1446
|
+
}
|
1447
|
+
return {
|
1448
|
+
type: "file",
|
1449
|
+
mediaType,
|
1450
|
+
filename: part.filename,
|
1451
|
+
data,
|
1452
|
+
providerOptions: part.providerOptions
|
1453
|
+
};
|
1454
|
+
}
|
1455
|
+
}
|
1456
|
+
}
|
1457
|
+
|
1458
|
+
// core/types/usage.ts
|
1459
|
+
function calculateLanguageModelUsage({
|
1460
|
+
inputTokens,
|
1461
|
+
outputTokens
|
1462
|
+
}) {
|
1463
|
+
return {
|
1464
|
+
promptTokens: inputTokens != null ? inputTokens : NaN,
|
1465
|
+
completionTokens: outputTokens != null ? outputTokens : NaN,
|
1466
|
+
totalTokens: (inputTokens != null ? inputTokens : 0) + (outputTokens != null ? outputTokens : 0)
|
1467
|
+
};
|
1468
|
+
}
|
1469
|
+
|
1470
|
+
// util/constants.ts
|
1471
|
+
var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
|
1472
|
+
// Annotate the CommonJS export names for ESM import in node:
|
1473
|
+
0 && (module.exports = {
|
1474
|
+
HANGING_STREAM_WARNING_TIME_MS,
|
1475
|
+
calculateLanguageModelUsage,
|
1476
|
+
convertToLanguageModelPrompt,
|
1477
|
+
prepareCallSettings,
|
1478
|
+
prepareRetries,
|
1479
|
+
prepareToolsAndToolChoice,
|
1480
|
+
standardizePrompt
|
1481
|
+
});
|
1482
|
+
//# sourceMappingURL=index.js.map
|