ai 3.4.26 → 3.4.27
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 +6 -0
- package/dist/index.d.mts +13 -12
- package/dist/index.d.ts +13 -12
- package/dist/index.js +332 -275
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +313 -256
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
- package/rsc/dist/index.d.ts +9 -1
- package/rsc/dist/rsc-server.d.mts +9 -1
- package/rsc/dist/rsc-server.mjs +313 -69
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/dist/index.mjs
CHANGED
@@ -1285,8 +1285,232 @@ var coreMessageSchema = z6.union([
|
|
1285
1285
|
coreToolMessageSchema
|
1286
1286
|
]);
|
1287
1287
|
|
1288
|
+
// core/prompt/detect-prompt-type.ts
|
1289
|
+
function detectPromptType(prompt) {
|
1290
|
+
if (!Array.isArray(prompt)) {
|
1291
|
+
return "other";
|
1292
|
+
}
|
1293
|
+
if (prompt.length === 0) {
|
1294
|
+
return "messages";
|
1295
|
+
}
|
1296
|
+
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
1297
|
+
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
1298
|
+
return "ui-messages";
|
1299
|
+
} else if (characteristics.every(
|
1300
|
+
(c) => c === "has-core-specific-parts" || c === "message"
|
1301
|
+
)) {
|
1302
|
+
return "messages";
|
1303
|
+
} else {
|
1304
|
+
return "other";
|
1305
|
+
}
|
1306
|
+
}
|
1307
|
+
function detectSingleMessageCharacteristics(message) {
|
1308
|
+
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
1309
|
+
message.role === "data" || // UI-only role
|
1310
|
+
"toolInvocations" in message || // UI-specific field
|
1311
|
+
"experimental_attachments" in message)) {
|
1312
|
+
return "has-ui-specific-parts";
|
1313
|
+
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
1314
|
+
"experimental_providerMetadata" in message)) {
|
1315
|
+
return "has-core-specific-parts";
|
1316
|
+
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
1317
|
+
return "message";
|
1318
|
+
} else {
|
1319
|
+
return "other";
|
1320
|
+
}
|
1321
|
+
}
|
1322
|
+
|
1323
|
+
// core/prompt/attachments-to-parts.ts
|
1324
|
+
function attachmentsToParts(attachments) {
|
1325
|
+
var _a11, _b, _c;
|
1326
|
+
const parts = [];
|
1327
|
+
for (const attachment of attachments) {
|
1328
|
+
let url;
|
1329
|
+
try {
|
1330
|
+
url = new URL(attachment.url);
|
1331
|
+
} catch (error) {
|
1332
|
+
throw new Error(`Invalid URL: ${attachment.url}`);
|
1333
|
+
}
|
1334
|
+
switch (url.protocol) {
|
1335
|
+
case "http:":
|
1336
|
+
case "https:": {
|
1337
|
+
if ((_a11 = attachment.contentType) == null ? void 0 : _a11.startsWith("image/")) {
|
1338
|
+
parts.push({ type: "image", image: url });
|
1339
|
+
} else {
|
1340
|
+
if (!attachment.contentType) {
|
1341
|
+
throw new Error(
|
1342
|
+
"If the attachment is not an image, it must specify a content type"
|
1343
|
+
);
|
1344
|
+
}
|
1345
|
+
parts.push({
|
1346
|
+
type: "file",
|
1347
|
+
data: url,
|
1348
|
+
mimeType: attachment.contentType
|
1349
|
+
});
|
1350
|
+
}
|
1351
|
+
break;
|
1352
|
+
}
|
1353
|
+
case "data:": {
|
1354
|
+
let header;
|
1355
|
+
let base64Content;
|
1356
|
+
let mimeType;
|
1357
|
+
try {
|
1358
|
+
[header, base64Content] = attachment.url.split(",");
|
1359
|
+
mimeType = header.split(";")[0].split(":")[1];
|
1360
|
+
} catch (error) {
|
1361
|
+
throw new Error(`Error processing data URL: ${attachment.url}`);
|
1362
|
+
}
|
1363
|
+
if (mimeType == null || base64Content == null) {
|
1364
|
+
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
1365
|
+
}
|
1366
|
+
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
1367
|
+
parts.push({
|
1368
|
+
type: "image",
|
1369
|
+
image: convertDataContentToUint8Array(base64Content)
|
1370
|
+
});
|
1371
|
+
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
1372
|
+
parts.push({
|
1373
|
+
type: "text",
|
1374
|
+
text: convertUint8ArrayToText(
|
1375
|
+
convertDataContentToUint8Array(base64Content)
|
1376
|
+
)
|
1377
|
+
});
|
1378
|
+
} else {
|
1379
|
+
if (!attachment.contentType) {
|
1380
|
+
throw new Error(
|
1381
|
+
"If the attachment is not an image or text, it must specify a content type"
|
1382
|
+
);
|
1383
|
+
}
|
1384
|
+
parts.push({
|
1385
|
+
type: "file",
|
1386
|
+
data: base64Content,
|
1387
|
+
mimeType: attachment.contentType
|
1388
|
+
});
|
1389
|
+
}
|
1390
|
+
break;
|
1391
|
+
}
|
1392
|
+
default: {
|
1393
|
+
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
1394
|
+
}
|
1395
|
+
}
|
1396
|
+
}
|
1397
|
+
return parts;
|
1398
|
+
}
|
1399
|
+
|
1400
|
+
// core/prompt/message-conversion-error.ts
|
1401
|
+
import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
|
1402
|
+
var name6 = "AI_MessageConversionError";
|
1403
|
+
var marker6 = `vercel.ai.error.${name6}`;
|
1404
|
+
var symbol6 = Symbol.for(marker6);
|
1405
|
+
var _a6;
|
1406
|
+
var MessageConversionError = class extends AISDKError6 {
|
1407
|
+
constructor({
|
1408
|
+
originalMessage,
|
1409
|
+
message
|
1410
|
+
}) {
|
1411
|
+
super({ name: name6, message });
|
1412
|
+
this[_a6] = true;
|
1413
|
+
this.originalMessage = originalMessage;
|
1414
|
+
}
|
1415
|
+
static isInstance(error) {
|
1416
|
+
return AISDKError6.hasMarker(error, marker6);
|
1417
|
+
}
|
1418
|
+
};
|
1419
|
+
_a6 = symbol6;
|
1420
|
+
|
1421
|
+
// core/prompt/convert-to-core-messages.ts
|
1422
|
+
function convertToCoreMessages(messages, options) {
|
1423
|
+
var _a11;
|
1424
|
+
const tools = (_a11 = options == null ? void 0 : options.tools) != null ? _a11 : {};
|
1425
|
+
const coreMessages = [];
|
1426
|
+
for (const message of messages) {
|
1427
|
+
const { role, content, toolInvocations, experimental_attachments } = message;
|
1428
|
+
switch (role) {
|
1429
|
+
case "system": {
|
1430
|
+
coreMessages.push({
|
1431
|
+
role: "system",
|
1432
|
+
content
|
1433
|
+
});
|
1434
|
+
break;
|
1435
|
+
}
|
1436
|
+
case "user": {
|
1437
|
+
coreMessages.push({
|
1438
|
+
role: "user",
|
1439
|
+
content: experimental_attachments ? [
|
1440
|
+
{ type: "text", text: content },
|
1441
|
+
...attachmentsToParts(experimental_attachments)
|
1442
|
+
] : content
|
1443
|
+
});
|
1444
|
+
break;
|
1445
|
+
}
|
1446
|
+
case "assistant": {
|
1447
|
+
if (toolInvocations == null) {
|
1448
|
+
coreMessages.push({ role: "assistant", content });
|
1449
|
+
break;
|
1450
|
+
}
|
1451
|
+
coreMessages.push({
|
1452
|
+
role: "assistant",
|
1453
|
+
content: [
|
1454
|
+
{ type: "text", text: content },
|
1455
|
+
...toolInvocations.map(
|
1456
|
+
({ toolCallId, toolName, args }) => ({
|
1457
|
+
type: "tool-call",
|
1458
|
+
toolCallId,
|
1459
|
+
toolName,
|
1460
|
+
args
|
1461
|
+
})
|
1462
|
+
)
|
1463
|
+
]
|
1464
|
+
});
|
1465
|
+
coreMessages.push({
|
1466
|
+
role: "tool",
|
1467
|
+
content: toolInvocations.map((toolInvocation) => {
|
1468
|
+
if (!("result" in toolInvocation)) {
|
1469
|
+
throw new MessageConversionError({
|
1470
|
+
originalMessage: message,
|
1471
|
+
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
1472
|
+
});
|
1473
|
+
}
|
1474
|
+
const { toolCallId, toolName, result } = toolInvocation;
|
1475
|
+
const tool2 = tools[toolName];
|
1476
|
+
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
1477
|
+
type: "tool-result",
|
1478
|
+
toolCallId,
|
1479
|
+
toolName,
|
1480
|
+
result: tool2.experimental_toToolResultContent(result),
|
1481
|
+
experimental_content: tool2.experimental_toToolResultContent(result)
|
1482
|
+
} : {
|
1483
|
+
type: "tool-result",
|
1484
|
+
toolCallId,
|
1485
|
+
toolName,
|
1486
|
+
result
|
1487
|
+
};
|
1488
|
+
})
|
1489
|
+
});
|
1490
|
+
break;
|
1491
|
+
}
|
1492
|
+
case "function":
|
1493
|
+
case "data":
|
1494
|
+
case "tool": {
|
1495
|
+
break;
|
1496
|
+
}
|
1497
|
+
default: {
|
1498
|
+
const _exhaustiveCheck = role;
|
1499
|
+
throw new MessageConversionError({
|
1500
|
+
originalMessage: message,
|
1501
|
+
message: `Unsupported role: ${_exhaustiveCheck}`
|
1502
|
+
});
|
1503
|
+
}
|
1504
|
+
}
|
1505
|
+
}
|
1506
|
+
return coreMessages;
|
1507
|
+
}
|
1508
|
+
|
1288
1509
|
// core/prompt/standardize-prompt.ts
|
1289
|
-
function standardizePrompt(
|
1510
|
+
function standardizePrompt({
|
1511
|
+
prompt,
|
1512
|
+
tools
|
1513
|
+
}) {
|
1290
1514
|
if (prompt.prompt == null && prompt.messages == null) {
|
1291
1515
|
throw new InvalidPromptError({
|
1292
1516
|
prompt,
|
@@ -1324,21 +1548,30 @@ function standardizePrompt(prompt) {
|
|
1324
1548
|
};
|
1325
1549
|
}
|
1326
1550
|
if (prompt.messages != null) {
|
1551
|
+
const promptType = detectPromptType(prompt.messages);
|
1552
|
+
if (promptType === "other") {
|
1553
|
+
throw new InvalidPromptError({
|
1554
|
+
prompt,
|
1555
|
+
message: "messages must be an array of CoreMessage or UIMessage"
|
1556
|
+
});
|
1557
|
+
}
|
1558
|
+
const messages = promptType === "ui-messages" ? convertToCoreMessages(prompt.messages, {
|
1559
|
+
tools
|
1560
|
+
}) : prompt.messages;
|
1327
1561
|
const validationResult = safeValidateTypes({
|
1328
|
-
value:
|
1562
|
+
value: messages,
|
1329
1563
|
schema: z7.array(coreMessageSchema)
|
1330
1564
|
});
|
1331
1565
|
if (!validationResult.success) {
|
1332
1566
|
throw new InvalidPromptError({
|
1333
1567
|
prompt,
|
1334
|
-
message: "messages must be an array of CoreMessage",
|
1568
|
+
message: "messages must be an array of CoreMessage or UIMessage",
|
1335
1569
|
cause: validationResult.error
|
1336
1570
|
});
|
1337
1571
|
}
|
1338
1572
|
return {
|
1339
1573
|
type: "messages",
|
1340
|
-
messages
|
1341
|
-
// only possible case bc of checks above
|
1574
|
+
messages,
|
1342
1575
|
system: prompt.system
|
1343
1576
|
};
|
1344
1577
|
}
|
@@ -1391,25 +1624,25 @@ function injectJsonInstruction({
|
|
1391
1624
|
}
|
1392
1625
|
|
1393
1626
|
// core/generate-object/no-object-generated-error.ts
|
1394
|
-
import { AISDKError as
|
1395
|
-
var
|
1396
|
-
var
|
1397
|
-
var
|
1398
|
-
var
|
1399
|
-
var NoObjectGeneratedError = class extends
|
1627
|
+
import { AISDKError as AISDKError7 } from "@ai-sdk/provider";
|
1628
|
+
var name7 = "AI_NoObjectGeneratedError";
|
1629
|
+
var marker7 = `vercel.ai.error.${name7}`;
|
1630
|
+
var symbol7 = Symbol.for(marker7);
|
1631
|
+
var _a7;
|
1632
|
+
var NoObjectGeneratedError = class extends AISDKError7 {
|
1400
1633
|
// used in isInstance
|
1401
1634
|
constructor({ message = "No object generated." } = {}) {
|
1402
|
-
super({ name:
|
1403
|
-
this[
|
1635
|
+
super({ name: name7, message });
|
1636
|
+
this[_a7] = true;
|
1404
1637
|
}
|
1405
1638
|
static isInstance(error) {
|
1406
|
-
return
|
1639
|
+
return AISDKError7.hasMarker(error, marker7);
|
1407
1640
|
}
|
1408
1641
|
/**
|
1409
1642
|
* @deprecated Use isInstance instead.
|
1410
1643
|
*/
|
1411
1644
|
static isNoObjectGeneratedError(error) {
|
1412
|
-
return error instanceof Error && error.name ===
|
1645
|
+
return error instanceof Error && error.name === name7;
|
1413
1646
|
}
|
1414
1647
|
/**
|
1415
1648
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -1423,7 +1656,7 @@ var NoObjectGeneratedError = class extends AISDKError6 {
|
|
1423
1656
|
};
|
1424
1657
|
}
|
1425
1658
|
};
|
1426
|
-
|
1659
|
+
_a7 = symbol7;
|
1427
1660
|
|
1428
1661
|
// core/generate-object/output-strategy.ts
|
1429
1662
|
import {
|
@@ -1877,16 +2110,19 @@ async function generateObject({
|
|
1877
2110
|
let resultProviderMetadata;
|
1878
2111
|
switch (mode) {
|
1879
2112
|
case "json": {
|
1880
|
-
const
|
1881
|
-
|
1882
|
-
prompt: system
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1886
|
-
|
2113
|
+
const standardizedPrompt = standardizePrompt({
|
2114
|
+
prompt: {
|
2115
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
2116
|
+
prompt: system,
|
2117
|
+
schema: outputStrategy.jsonSchema
|
2118
|
+
}),
|
2119
|
+
prompt,
|
2120
|
+
messages
|
2121
|
+
},
|
2122
|
+
tools: void 0
|
1887
2123
|
});
|
1888
2124
|
const promptMessages = await convertToLanguageModelPrompt({
|
1889
|
-
prompt:
|
2125
|
+
prompt: standardizedPrompt,
|
1890
2126
|
modelSupportsImageUrls: model.supportsImageUrls,
|
1891
2127
|
modelSupportsUrl: model.supportsUrl
|
1892
2128
|
});
|
@@ -1902,7 +2138,7 @@ async function generateObject({
|
|
1902
2138
|
}),
|
1903
2139
|
...baseTelemetryAttributes,
|
1904
2140
|
"ai.prompt.format": {
|
1905
|
-
input: () =>
|
2141
|
+
input: () => standardizedPrompt.type
|
1906
2142
|
},
|
1907
2143
|
"ai.prompt.messages": {
|
1908
2144
|
input: () => JSON.stringify(promptMessages)
|
@@ -1930,7 +2166,7 @@ async function generateObject({
|
|
1930
2166
|
description: schemaDescription
|
1931
2167
|
},
|
1932
2168
|
...prepareCallSettings(settings),
|
1933
|
-
inputFormat:
|
2169
|
+
inputFormat: standardizedPrompt.type,
|
1934
2170
|
prompt: promptMessages,
|
1935
2171
|
providerMetadata,
|
1936
2172
|
abortSignal,
|
@@ -1983,17 +2219,16 @@ async function generateObject({
|
|
1983
2219
|
break;
|
1984
2220
|
}
|
1985
2221
|
case "tool": {
|
1986
|
-
const
|
1987
|
-
system,
|
1988
|
-
|
1989
|
-
messages
|
2222
|
+
const standardizedPrompt = standardizePrompt({
|
2223
|
+
prompt: { system, prompt, messages },
|
2224
|
+
tools: void 0
|
1990
2225
|
});
|
1991
2226
|
const promptMessages = await convertToLanguageModelPrompt({
|
1992
|
-
prompt:
|
2227
|
+
prompt: standardizedPrompt,
|
1993
2228
|
modelSupportsImageUrls: model.supportsImageUrls,
|
1994
2229
|
modelSupportsUrl: model.supportsUrl
|
1995
2230
|
});
|
1996
|
-
const inputFormat =
|
2231
|
+
const inputFormat = standardizedPrompt.type;
|
1997
2232
|
const generateResult = await retry(
|
1998
2233
|
() => recordSpan({
|
1999
2234
|
name: "ai.generateObject.doGenerate",
|
@@ -2358,13 +2593,16 @@ async function streamObject({
|
|
2358
2593
|
let transformer;
|
2359
2594
|
switch (mode) {
|
2360
2595
|
case "json": {
|
2361
|
-
const
|
2362
|
-
|
2363
|
-
prompt: system
|
2364
|
-
|
2365
|
-
|
2366
|
-
|
2367
|
-
|
2596
|
+
const standardizedPrompt = standardizePrompt({
|
2597
|
+
prompt: {
|
2598
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
2599
|
+
prompt: system,
|
2600
|
+
schema: outputStrategy.jsonSchema
|
2601
|
+
}),
|
2602
|
+
prompt,
|
2603
|
+
messages
|
2604
|
+
},
|
2605
|
+
tools: void 0
|
2368
2606
|
});
|
2369
2607
|
callOptions = {
|
2370
2608
|
mode: {
|
@@ -2374,9 +2612,9 @@ async function streamObject({
|
|
2374
2612
|
description: schemaDescription
|
2375
2613
|
},
|
2376
2614
|
...prepareCallSettings(settings),
|
2377
|
-
inputFormat:
|
2615
|
+
inputFormat: standardizedPrompt.type,
|
2378
2616
|
prompt: await convertToLanguageModelPrompt({
|
2379
|
-
prompt:
|
2617
|
+
prompt: standardizedPrompt,
|
2380
2618
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2381
2619
|
modelSupportsUrl: model.supportsUrl
|
2382
2620
|
}),
|
@@ -2401,10 +2639,9 @@ async function streamObject({
|
|
2401
2639
|
break;
|
2402
2640
|
}
|
2403
2641
|
case "tool": {
|
2404
|
-
const
|
2405
|
-
system,
|
2406
|
-
|
2407
|
-
messages
|
2642
|
+
const standardizedPrompt = standardizePrompt({
|
2643
|
+
prompt: { system, prompt, messages },
|
2644
|
+
tools: void 0
|
2408
2645
|
});
|
2409
2646
|
callOptions = {
|
2410
2647
|
mode: {
|
@@ -2417,9 +2654,9 @@ async function streamObject({
|
|
2417
2654
|
}
|
2418
2655
|
},
|
2419
2656
|
...prepareCallSettings(settings),
|
2420
|
-
inputFormat:
|
2657
|
+
inputFormat: standardizedPrompt.type,
|
2421
2658
|
prompt: await convertToLanguageModelPrompt({
|
2422
|
-
prompt:
|
2659
|
+
prompt: standardizedPrompt,
|
2423
2660
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2424
2661
|
modelSupportsUrl: model.supportsUrl
|
2425
2662
|
}),
|
@@ -2810,12 +3047,12 @@ import {
|
|
2810
3047
|
} from "@ai-sdk/provider";
|
2811
3048
|
|
2812
3049
|
// errors/invalid-tool-arguments-error.ts
|
2813
|
-
import { AISDKError as
|
2814
|
-
var
|
2815
|
-
var
|
2816
|
-
var
|
2817
|
-
var
|
2818
|
-
var InvalidToolArgumentsError = class extends
|
3050
|
+
import { AISDKError as AISDKError8, getErrorMessage as getErrorMessage2 } from "@ai-sdk/provider";
|
3051
|
+
var name8 = "AI_InvalidToolArgumentsError";
|
3052
|
+
var marker8 = `vercel.ai.error.${name8}`;
|
3053
|
+
var symbol8 = Symbol.for(marker8);
|
3054
|
+
var _a8;
|
3055
|
+
var InvalidToolArgumentsError = class extends AISDKError8 {
|
2819
3056
|
constructor({
|
2820
3057
|
toolArgs,
|
2821
3058
|
toolName,
|
@@ -2824,19 +3061,19 @@ var InvalidToolArgumentsError = class extends AISDKError7 {
|
|
2824
3061
|
cause
|
2825
3062
|
)}`
|
2826
3063
|
}) {
|
2827
|
-
super({ name:
|
2828
|
-
this[
|
3064
|
+
super({ name: name8, message, cause });
|
3065
|
+
this[_a8] = true;
|
2829
3066
|
this.toolArgs = toolArgs;
|
2830
3067
|
this.toolName = toolName;
|
2831
3068
|
}
|
2832
3069
|
static isInstance(error) {
|
2833
|
-
return
|
3070
|
+
return AISDKError8.hasMarker(error, marker8);
|
2834
3071
|
}
|
2835
3072
|
/**
|
2836
3073
|
* @deprecated use `isInstance` instead
|
2837
3074
|
*/
|
2838
3075
|
static isInvalidToolArgumentsError(error) {
|
2839
|
-
return error instanceof Error && error.name ===
|
3076
|
+
return error instanceof Error && error.name === name8 && typeof error.toolName === "string" && typeof error.toolArgs === "string";
|
2840
3077
|
}
|
2841
3078
|
/**
|
2842
3079
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -2852,33 +3089,33 @@ var InvalidToolArgumentsError = class extends AISDKError7 {
|
|
2852
3089
|
};
|
2853
3090
|
}
|
2854
3091
|
};
|
2855
|
-
|
3092
|
+
_a8 = symbol8;
|
2856
3093
|
|
2857
3094
|
// errors/no-such-tool-error.ts
|
2858
|
-
import { AISDKError as
|
2859
|
-
var
|
2860
|
-
var
|
2861
|
-
var
|
2862
|
-
var
|
2863
|
-
var NoSuchToolError = class extends
|
3095
|
+
import { AISDKError as AISDKError9 } from "@ai-sdk/provider";
|
3096
|
+
var name9 = "AI_NoSuchToolError";
|
3097
|
+
var marker9 = `vercel.ai.error.${name9}`;
|
3098
|
+
var symbol9 = Symbol.for(marker9);
|
3099
|
+
var _a9;
|
3100
|
+
var NoSuchToolError = class extends AISDKError9 {
|
2864
3101
|
constructor({
|
2865
3102
|
toolName,
|
2866
3103
|
availableTools = void 0,
|
2867
3104
|
message = `Model tried to call unavailable tool '${toolName}'. ${availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`}`
|
2868
3105
|
}) {
|
2869
|
-
super({ name:
|
2870
|
-
this[
|
3106
|
+
super({ name: name9, message });
|
3107
|
+
this[_a9] = true;
|
2871
3108
|
this.toolName = toolName;
|
2872
3109
|
this.availableTools = availableTools;
|
2873
3110
|
}
|
2874
3111
|
static isInstance(error) {
|
2875
|
-
return
|
3112
|
+
return AISDKError9.hasMarker(error, marker9);
|
2876
3113
|
}
|
2877
3114
|
/**
|
2878
3115
|
* @deprecated use `isInstance` instead
|
2879
3116
|
*/
|
2880
3117
|
static isNoSuchToolError(error) {
|
2881
|
-
return error instanceof Error && error.name ===
|
3118
|
+
return error instanceof Error && error.name === name9 && "toolName" in error && error.toolName != void 0 && typeof error.name === "string";
|
2882
3119
|
}
|
2883
3120
|
/**
|
2884
3121
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -2893,27 +3130,6 @@ var NoSuchToolError = class extends AISDKError8 {
|
|
2893
3130
|
};
|
2894
3131
|
}
|
2895
3132
|
};
|
2896
|
-
_a8 = symbol8;
|
2897
|
-
|
2898
|
-
// core/prompt/message-conversion-error.ts
|
2899
|
-
import { AISDKError as AISDKError9 } from "@ai-sdk/provider";
|
2900
|
-
var name9 = "AI_MessageConversionError";
|
2901
|
-
var marker9 = `vercel.ai.error.${name9}`;
|
2902
|
-
var symbol9 = Symbol.for(marker9);
|
2903
|
-
var _a9;
|
2904
|
-
var MessageConversionError = class extends AISDKError9 {
|
2905
|
-
constructor({
|
2906
|
-
originalMessage,
|
2907
|
-
message
|
2908
|
-
}) {
|
2909
|
-
super({ name: name9, message });
|
2910
|
-
this[_a9] = true;
|
2911
|
-
this.originalMessage = originalMessage;
|
2912
|
-
}
|
2913
|
-
static isInstance(error) {
|
2914
|
-
return AISDKError9.hasMarker(error, marker9);
|
2915
|
-
}
|
2916
|
-
};
|
2917
3133
|
_a9 = symbol9;
|
2918
3134
|
|
2919
3135
|
// core/prompt/prepare-tools-and-tool-choice.ts
|
@@ -3093,7 +3309,10 @@ async function generateText({
|
|
3093
3309
|
headers,
|
3094
3310
|
settings: { ...settings, maxRetries }
|
3095
3311
|
});
|
3096
|
-
const initialPrompt = standardizePrompt({
|
3312
|
+
const initialPrompt = standardizePrompt({
|
3313
|
+
prompt: { system, prompt, messages },
|
3314
|
+
tools
|
3315
|
+
});
|
3097
3316
|
const tracer = getTracer(telemetry);
|
3098
3317
|
return recordSpan({
|
3099
3318
|
name: "ai.generateText",
|
@@ -3819,7 +4038,10 @@ async function streamText({
|
|
3819
4038
|
settings: { ...settings, maxRetries }
|
3820
4039
|
});
|
3821
4040
|
const tracer = getTracer(telemetry);
|
3822
|
-
const initialPrompt = standardizePrompt({
|
4041
|
+
const initialPrompt = standardizePrompt({
|
4042
|
+
prompt: { system, prompt, messages },
|
4043
|
+
tools
|
4044
|
+
});
|
3823
4045
|
return recordSpan({
|
3824
4046
|
name: "ai.streamText",
|
3825
4047
|
attributes: selectTelemetryAttributes({
|
@@ -4643,171 +4865,6 @@ var experimental_wrapLanguageModel = ({
|
|
4643
4865
|
};
|
4644
4866
|
};
|
4645
4867
|
|
4646
|
-
// core/prompt/attachments-to-parts.ts
|
4647
|
-
function attachmentsToParts(attachments) {
|
4648
|
-
var _a11, _b, _c;
|
4649
|
-
const parts = [];
|
4650
|
-
for (const attachment of attachments) {
|
4651
|
-
let url;
|
4652
|
-
try {
|
4653
|
-
url = new URL(attachment.url);
|
4654
|
-
} catch (error) {
|
4655
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
4656
|
-
}
|
4657
|
-
switch (url.protocol) {
|
4658
|
-
case "http:":
|
4659
|
-
case "https:": {
|
4660
|
-
if ((_a11 = attachment.contentType) == null ? void 0 : _a11.startsWith("image/")) {
|
4661
|
-
parts.push({ type: "image", image: url });
|
4662
|
-
} else {
|
4663
|
-
if (!attachment.contentType) {
|
4664
|
-
throw new Error(
|
4665
|
-
"If the attachment is not an image, it must specify a content type"
|
4666
|
-
);
|
4667
|
-
}
|
4668
|
-
parts.push({
|
4669
|
-
type: "file",
|
4670
|
-
data: url,
|
4671
|
-
mimeType: attachment.contentType
|
4672
|
-
});
|
4673
|
-
}
|
4674
|
-
break;
|
4675
|
-
}
|
4676
|
-
case "data:": {
|
4677
|
-
let header;
|
4678
|
-
let base64Content;
|
4679
|
-
let mimeType;
|
4680
|
-
try {
|
4681
|
-
[header, base64Content] = attachment.url.split(",");
|
4682
|
-
mimeType = header.split(";")[0].split(":")[1];
|
4683
|
-
} catch (error) {
|
4684
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
4685
|
-
}
|
4686
|
-
if (mimeType == null || base64Content == null) {
|
4687
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
4688
|
-
}
|
4689
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
4690
|
-
parts.push({
|
4691
|
-
type: "image",
|
4692
|
-
image: convertDataContentToUint8Array(base64Content)
|
4693
|
-
});
|
4694
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
4695
|
-
parts.push({
|
4696
|
-
type: "text",
|
4697
|
-
text: convertUint8ArrayToText(
|
4698
|
-
convertDataContentToUint8Array(base64Content)
|
4699
|
-
)
|
4700
|
-
});
|
4701
|
-
} else {
|
4702
|
-
if (!attachment.contentType) {
|
4703
|
-
throw new Error(
|
4704
|
-
"If the attachment is not an image or text, it must specify a content type"
|
4705
|
-
);
|
4706
|
-
}
|
4707
|
-
parts.push({
|
4708
|
-
type: "file",
|
4709
|
-
data: base64Content,
|
4710
|
-
mimeType: attachment.contentType
|
4711
|
-
});
|
4712
|
-
}
|
4713
|
-
break;
|
4714
|
-
}
|
4715
|
-
default: {
|
4716
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
4717
|
-
}
|
4718
|
-
}
|
4719
|
-
}
|
4720
|
-
return parts;
|
4721
|
-
}
|
4722
|
-
|
4723
|
-
// core/prompt/convert-to-core-messages.ts
|
4724
|
-
function convertToCoreMessages(messages, options) {
|
4725
|
-
var _a11;
|
4726
|
-
const tools = (_a11 = options == null ? void 0 : options.tools) != null ? _a11 : {};
|
4727
|
-
const coreMessages = [];
|
4728
|
-
for (const message of messages) {
|
4729
|
-
const { role, content, toolInvocations, experimental_attachments } = message;
|
4730
|
-
switch (role) {
|
4731
|
-
case "system": {
|
4732
|
-
coreMessages.push({
|
4733
|
-
role: "system",
|
4734
|
-
content
|
4735
|
-
});
|
4736
|
-
break;
|
4737
|
-
}
|
4738
|
-
case "user": {
|
4739
|
-
coreMessages.push({
|
4740
|
-
role: "user",
|
4741
|
-
content: experimental_attachments ? [
|
4742
|
-
{ type: "text", text: content },
|
4743
|
-
...attachmentsToParts(experimental_attachments)
|
4744
|
-
] : content
|
4745
|
-
});
|
4746
|
-
break;
|
4747
|
-
}
|
4748
|
-
case "assistant": {
|
4749
|
-
if (toolInvocations == null) {
|
4750
|
-
coreMessages.push({ role: "assistant", content });
|
4751
|
-
break;
|
4752
|
-
}
|
4753
|
-
coreMessages.push({
|
4754
|
-
role: "assistant",
|
4755
|
-
content: [
|
4756
|
-
{ type: "text", text: content },
|
4757
|
-
...toolInvocations.map(
|
4758
|
-
({ toolCallId, toolName, args }) => ({
|
4759
|
-
type: "tool-call",
|
4760
|
-
toolCallId,
|
4761
|
-
toolName,
|
4762
|
-
args
|
4763
|
-
})
|
4764
|
-
)
|
4765
|
-
]
|
4766
|
-
});
|
4767
|
-
coreMessages.push({
|
4768
|
-
role: "tool",
|
4769
|
-
content: toolInvocations.map((toolInvocation) => {
|
4770
|
-
if (!("result" in toolInvocation)) {
|
4771
|
-
throw new MessageConversionError({
|
4772
|
-
originalMessage: message,
|
4773
|
-
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
4774
|
-
});
|
4775
|
-
}
|
4776
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
4777
|
-
const tool2 = tools[toolName];
|
4778
|
-
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
4779
|
-
type: "tool-result",
|
4780
|
-
toolCallId,
|
4781
|
-
toolName,
|
4782
|
-
result: tool2.experimental_toToolResultContent(result),
|
4783
|
-
experimental_content: tool2.experimental_toToolResultContent(result)
|
4784
|
-
} : {
|
4785
|
-
type: "tool-result",
|
4786
|
-
toolCallId,
|
4787
|
-
toolName,
|
4788
|
-
result
|
4789
|
-
};
|
4790
|
-
})
|
4791
|
-
});
|
4792
|
-
break;
|
4793
|
-
}
|
4794
|
-
case "function":
|
4795
|
-
case "data":
|
4796
|
-
case "tool": {
|
4797
|
-
break;
|
4798
|
-
}
|
4799
|
-
default: {
|
4800
|
-
const _exhaustiveCheck = role;
|
4801
|
-
throw new MessageConversionError({
|
4802
|
-
originalMessage: message,
|
4803
|
-
message: `Unsupported role: ${_exhaustiveCheck}`
|
4804
|
-
});
|
4805
|
-
}
|
4806
|
-
}
|
4807
|
-
}
|
4808
|
-
return coreMessages;
|
4809
|
-
}
|
4810
|
-
|
4811
4868
|
// core/registry/custom-provider.ts
|
4812
4869
|
import { NoSuchModelError as NoSuchModelError2 } from "@ai-sdk/provider";
|
4813
4870
|
function experimental_customProvider({
|