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