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.js
CHANGED
@@ -1254,7 +1254,7 @@ function prepareCallSettings({
|
|
1254
1254
|
}
|
1255
1255
|
|
1256
1256
|
// core/prompt/standardize-prompt.ts
|
1257
|
-
var
|
1257
|
+
var import_provider8 = require("@ai-sdk/provider");
|
1258
1258
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
1259
1259
|
var import_zod7 = require("zod");
|
1260
1260
|
|
@@ -1367,29 +1367,253 @@ var coreMessageSchema = import_zod6.z.union([
|
|
1367
1367
|
coreToolMessageSchema
|
1368
1368
|
]);
|
1369
1369
|
|
1370
|
+
// core/prompt/detect-prompt-type.ts
|
1371
|
+
function detectPromptType(prompt) {
|
1372
|
+
if (!Array.isArray(prompt)) {
|
1373
|
+
return "other";
|
1374
|
+
}
|
1375
|
+
if (prompt.length === 0) {
|
1376
|
+
return "messages";
|
1377
|
+
}
|
1378
|
+
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
1379
|
+
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
1380
|
+
return "ui-messages";
|
1381
|
+
} else if (characteristics.every(
|
1382
|
+
(c) => c === "has-core-specific-parts" || c === "message"
|
1383
|
+
)) {
|
1384
|
+
return "messages";
|
1385
|
+
} else {
|
1386
|
+
return "other";
|
1387
|
+
}
|
1388
|
+
}
|
1389
|
+
function detectSingleMessageCharacteristics(message) {
|
1390
|
+
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
1391
|
+
message.role === "data" || // UI-only role
|
1392
|
+
"toolInvocations" in message || // UI-specific field
|
1393
|
+
"experimental_attachments" in message)) {
|
1394
|
+
return "has-ui-specific-parts";
|
1395
|
+
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
1396
|
+
"experimental_providerMetadata" in message)) {
|
1397
|
+
return "has-core-specific-parts";
|
1398
|
+
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
1399
|
+
return "message";
|
1400
|
+
} else {
|
1401
|
+
return "other";
|
1402
|
+
}
|
1403
|
+
}
|
1404
|
+
|
1405
|
+
// core/prompt/attachments-to-parts.ts
|
1406
|
+
function attachmentsToParts(attachments) {
|
1407
|
+
var _a11, _b, _c;
|
1408
|
+
const parts = [];
|
1409
|
+
for (const attachment of attachments) {
|
1410
|
+
let url;
|
1411
|
+
try {
|
1412
|
+
url = new URL(attachment.url);
|
1413
|
+
} catch (error) {
|
1414
|
+
throw new Error(`Invalid URL: ${attachment.url}`);
|
1415
|
+
}
|
1416
|
+
switch (url.protocol) {
|
1417
|
+
case "http:":
|
1418
|
+
case "https:": {
|
1419
|
+
if ((_a11 = attachment.contentType) == null ? void 0 : _a11.startsWith("image/")) {
|
1420
|
+
parts.push({ type: "image", image: url });
|
1421
|
+
} else {
|
1422
|
+
if (!attachment.contentType) {
|
1423
|
+
throw new Error(
|
1424
|
+
"If the attachment is not an image, it must specify a content type"
|
1425
|
+
);
|
1426
|
+
}
|
1427
|
+
parts.push({
|
1428
|
+
type: "file",
|
1429
|
+
data: url,
|
1430
|
+
mimeType: attachment.contentType
|
1431
|
+
});
|
1432
|
+
}
|
1433
|
+
break;
|
1434
|
+
}
|
1435
|
+
case "data:": {
|
1436
|
+
let header;
|
1437
|
+
let base64Content;
|
1438
|
+
let mimeType;
|
1439
|
+
try {
|
1440
|
+
[header, base64Content] = attachment.url.split(",");
|
1441
|
+
mimeType = header.split(";")[0].split(":")[1];
|
1442
|
+
} catch (error) {
|
1443
|
+
throw new Error(`Error processing data URL: ${attachment.url}`);
|
1444
|
+
}
|
1445
|
+
if (mimeType == null || base64Content == null) {
|
1446
|
+
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
1447
|
+
}
|
1448
|
+
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
1449
|
+
parts.push({
|
1450
|
+
type: "image",
|
1451
|
+
image: convertDataContentToUint8Array(base64Content)
|
1452
|
+
});
|
1453
|
+
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
1454
|
+
parts.push({
|
1455
|
+
type: "text",
|
1456
|
+
text: convertUint8ArrayToText(
|
1457
|
+
convertDataContentToUint8Array(base64Content)
|
1458
|
+
)
|
1459
|
+
});
|
1460
|
+
} else {
|
1461
|
+
if (!attachment.contentType) {
|
1462
|
+
throw new Error(
|
1463
|
+
"If the attachment is not an image or text, it must specify a content type"
|
1464
|
+
);
|
1465
|
+
}
|
1466
|
+
parts.push({
|
1467
|
+
type: "file",
|
1468
|
+
data: base64Content,
|
1469
|
+
mimeType: attachment.contentType
|
1470
|
+
});
|
1471
|
+
}
|
1472
|
+
break;
|
1473
|
+
}
|
1474
|
+
default: {
|
1475
|
+
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
1476
|
+
}
|
1477
|
+
}
|
1478
|
+
}
|
1479
|
+
return parts;
|
1480
|
+
}
|
1481
|
+
|
1482
|
+
// core/prompt/message-conversion-error.ts
|
1483
|
+
var import_provider7 = require("@ai-sdk/provider");
|
1484
|
+
var name6 = "AI_MessageConversionError";
|
1485
|
+
var marker6 = `vercel.ai.error.${name6}`;
|
1486
|
+
var symbol6 = Symbol.for(marker6);
|
1487
|
+
var _a6;
|
1488
|
+
var MessageConversionError = class extends import_provider7.AISDKError {
|
1489
|
+
constructor({
|
1490
|
+
originalMessage,
|
1491
|
+
message
|
1492
|
+
}) {
|
1493
|
+
super({ name: name6, message });
|
1494
|
+
this[_a6] = true;
|
1495
|
+
this.originalMessage = originalMessage;
|
1496
|
+
}
|
1497
|
+
static isInstance(error) {
|
1498
|
+
return import_provider7.AISDKError.hasMarker(error, marker6);
|
1499
|
+
}
|
1500
|
+
};
|
1501
|
+
_a6 = symbol6;
|
1502
|
+
|
1503
|
+
// core/prompt/convert-to-core-messages.ts
|
1504
|
+
function convertToCoreMessages(messages, options) {
|
1505
|
+
var _a11;
|
1506
|
+
const tools = (_a11 = options == null ? void 0 : options.tools) != null ? _a11 : {};
|
1507
|
+
const coreMessages = [];
|
1508
|
+
for (const message of messages) {
|
1509
|
+
const { role, content, toolInvocations, experimental_attachments } = message;
|
1510
|
+
switch (role) {
|
1511
|
+
case "system": {
|
1512
|
+
coreMessages.push({
|
1513
|
+
role: "system",
|
1514
|
+
content
|
1515
|
+
});
|
1516
|
+
break;
|
1517
|
+
}
|
1518
|
+
case "user": {
|
1519
|
+
coreMessages.push({
|
1520
|
+
role: "user",
|
1521
|
+
content: experimental_attachments ? [
|
1522
|
+
{ type: "text", text: content },
|
1523
|
+
...attachmentsToParts(experimental_attachments)
|
1524
|
+
] : content
|
1525
|
+
});
|
1526
|
+
break;
|
1527
|
+
}
|
1528
|
+
case "assistant": {
|
1529
|
+
if (toolInvocations == null) {
|
1530
|
+
coreMessages.push({ role: "assistant", content });
|
1531
|
+
break;
|
1532
|
+
}
|
1533
|
+
coreMessages.push({
|
1534
|
+
role: "assistant",
|
1535
|
+
content: [
|
1536
|
+
{ type: "text", text: content },
|
1537
|
+
...toolInvocations.map(
|
1538
|
+
({ toolCallId, toolName, args }) => ({
|
1539
|
+
type: "tool-call",
|
1540
|
+
toolCallId,
|
1541
|
+
toolName,
|
1542
|
+
args
|
1543
|
+
})
|
1544
|
+
)
|
1545
|
+
]
|
1546
|
+
});
|
1547
|
+
coreMessages.push({
|
1548
|
+
role: "tool",
|
1549
|
+
content: toolInvocations.map((toolInvocation) => {
|
1550
|
+
if (!("result" in toolInvocation)) {
|
1551
|
+
throw new MessageConversionError({
|
1552
|
+
originalMessage: message,
|
1553
|
+
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
1554
|
+
});
|
1555
|
+
}
|
1556
|
+
const { toolCallId, toolName, result } = toolInvocation;
|
1557
|
+
const tool2 = tools[toolName];
|
1558
|
+
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
1559
|
+
type: "tool-result",
|
1560
|
+
toolCallId,
|
1561
|
+
toolName,
|
1562
|
+
result: tool2.experimental_toToolResultContent(result),
|
1563
|
+
experimental_content: tool2.experimental_toToolResultContent(result)
|
1564
|
+
} : {
|
1565
|
+
type: "tool-result",
|
1566
|
+
toolCallId,
|
1567
|
+
toolName,
|
1568
|
+
result
|
1569
|
+
};
|
1570
|
+
})
|
1571
|
+
});
|
1572
|
+
break;
|
1573
|
+
}
|
1574
|
+
case "function":
|
1575
|
+
case "data":
|
1576
|
+
case "tool": {
|
1577
|
+
break;
|
1578
|
+
}
|
1579
|
+
default: {
|
1580
|
+
const _exhaustiveCheck = role;
|
1581
|
+
throw new MessageConversionError({
|
1582
|
+
originalMessage: message,
|
1583
|
+
message: `Unsupported role: ${_exhaustiveCheck}`
|
1584
|
+
});
|
1585
|
+
}
|
1586
|
+
}
|
1587
|
+
}
|
1588
|
+
return coreMessages;
|
1589
|
+
}
|
1590
|
+
|
1370
1591
|
// core/prompt/standardize-prompt.ts
|
1371
|
-
function standardizePrompt(
|
1592
|
+
function standardizePrompt({
|
1593
|
+
prompt,
|
1594
|
+
tools
|
1595
|
+
}) {
|
1372
1596
|
if (prompt.prompt == null && prompt.messages == null) {
|
1373
|
-
throw new
|
1597
|
+
throw new import_provider8.InvalidPromptError({
|
1374
1598
|
prompt,
|
1375
1599
|
message: "prompt or messages must be defined"
|
1376
1600
|
});
|
1377
1601
|
}
|
1378
1602
|
if (prompt.prompt != null && prompt.messages != null) {
|
1379
|
-
throw new
|
1603
|
+
throw new import_provider8.InvalidPromptError({
|
1380
1604
|
prompt,
|
1381
1605
|
message: "prompt and messages cannot be defined at the same time"
|
1382
1606
|
});
|
1383
1607
|
}
|
1384
1608
|
if (prompt.system != null && typeof prompt.system !== "string") {
|
1385
|
-
throw new
|
1609
|
+
throw new import_provider8.InvalidPromptError({
|
1386
1610
|
prompt,
|
1387
1611
|
message: "system must be a string"
|
1388
1612
|
});
|
1389
1613
|
}
|
1390
1614
|
if (prompt.prompt != null) {
|
1391
1615
|
if (typeof prompt.prompt !== "string") {
|
1392
|
-
throw new
|
1616
|
+
throw new import_provider8.InvalidPromptError({
|
1393
1617
|
prompt,
|
1394
1618
|
message: "prompt must be a string"
|
1395
1619
|
});
|
@@ -1406,21 +1630,30 @@ function standardizePrompt(prompt) {
|
|
1406
1630
|
};
|
1407
1631
|
}
|
1408
1632
|
if (prompt.messages != null) {
|
1633
|
+
const promptType = detectPromptType(prompt.messages);
|
1634
|
+
if (promptType === "other") {
|
1635
|
+
throw new import_provider8.InvalidPromptError({
|
1636
|
+
prompt,
|
1637
|
+
message: "messages must be an array of CoreMessage or UIMessage"
|
1638
|
+
});
|
1639
|
+
}
|
1640
|
+
const messages = promptType === "ui-messages" ? convertToCoreMessages(prompt.messages, {
|
1641
|
+
tools
|
1642
|
+
}) : prompt.messages;
|
1409
1643
|
const validationResult = (0, import_provider_utils3.safeValidateTypes)({
|
1410
|
-
value:
|
1644
|
+
value: messages,
|
1411
1645
|
schema: import_zod7.z.array(coreMessageSchema)
|
1412
1646
|
});
|
1413
1647
|
if (!validationResult.success) {
|
1414
|
-
throw new
|
1648
|
+
throw new import_provider8.InvalidPromptError({
|
1415
1649
|
prompt,
|
1416
|
-
message: "messages must be an array of CoreMessage",
|
1650
|
+
message: "messages must be an array of CoreMessage or UIMessage",
|
1417
1651
|
cause: validationResult.error
|
1418
1652
|
});
|
1419
1653
|
}
|
1420
1654
|
return {
|
1421
1655
|
type: "messages",
|
1422
|
-
messages
|
1423
|
-
// only possible case bc of checks above
|
1656
|
+
messages,
|
1424
1657
|
system: prompt.system
|
1425
1658
|
};
|
1426
1659
|
}
|
@@ -1473,25 +1706,25 @@ function injectJsonInstruction({
|
|
1473
1706
|
}
|
1474
1707
|
|
1475
1708
|
// core/generate-object/no-object-generated-error.ts
|
1476
|
-
var
|
1477
|
-
var
|
1478
|
-
var
|
1479
|
-
var
|
1480
|
-
var
|
1481
|
-
var NoObjectGeneratedError = class extends
|
1709
|
+
var import_provider9 = require("@ai-sdk/provider");
|
1710
|
+
var name7 = "AI_NoObjectGeneratedError";
|
1711
|
+
var marker7 = `vercel.ai.error.${name7}`;
|
1712
|
+
var symbol7 = Symbol.for(marker7);
|
1713
|
+
var _a7;
|
1714
|
+
var NoObjectGeneratedError = class extends import_provider9.AISDKError {
|
1482
1715
|
// used in isInstance
|
1483
1716
|
constructor({ message = "No object generated." } = {}) {
|
1484
|
-
super({ name:
|
1485
|
-
this[
|
1717
|
+
super({ name: name7, message });
|
1718
|
+
this[_a7] = true;
|
1486
1719
|
}
|
1487
1720
|
static isInstance(error) {
|
1488
|
-
return
|
1721
|
+
return import_provider9.AISDKError.hasMarker(error, marker7);
|
1489
1722
|
}
|
1490
1723
|
/**
|
1491
1724
|
* @deprecated Use isInstance instead.
|
1492
1725
|
*/
|
1493
1726
|
static isNoObjectGeneratedError(error) {
|
1494
|
-
return error instanceof Error && error.name ===
|
1727
|
+
return error instanceof Error && error.name === name7;
|
1495
1728
|
}
|
1496
1729
|
/**
|
1497
1730
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -1505,10 +1738,10 @@ var NoObjectGeneratedError = class extends import_provider8.AISDKError {
|
|
1505
1738
|
};
|
1506
1739
|
}
|
1507
1740
|
};
|
1508
|
-
|
1741
|
+
_a7 = symbol7;
|
1509
1742
|
|
1510
1743
|
// core/generate-object/output-strategy.ts
|
1511
|
-
var
|
1744
|
+
var import_provider10 = require("@ai-sdk/provider");
|
1512
1745
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
1513
1746
|
var import_ui_utils = require("@ai-sdk/ui-utils");
|
1514
1747
|
|
@@ -1540,7 +1773,7 @@ var noSchemaOutputStrategy = {
|
|
1540
1773
|
return value === void 0 ? { success: false, error: new NoObjectGeneratedError() } : { success: true, value };
|
1541
1774
|
},
|
1542
1775
|
createElementStream() {
|
1543
|
-
throw new
|
1776
|
+
throw new import_provider10.UnsupportedFunctionalityError({
|
1544
1777
|
functionality: "element streams in no-schema mode"
|
1545
1778
|
});
|
1546
1779
|
}
|
@@ -1562,7 +1795,7 @@ var objectOutputStrategy = (schema) => ({
|
|
1562
1795
|
return (0, import_provider_utils4.safeValidateTypes)({ value, schema });
|
1563
1796
|
},
|
1564
1797
|
createElementStream() {
|
1565
|
-
throw new
|
1798
|
+
throw new import_provider10.UnsupportedFunctionalityError({
|
1566
1799
|
functionality: "element streams in object mode"
|
1567
1800
|
});
|
1568
1801
|
}
|
@@ -1585,10 +1818,10 @@ var arrayOutputStrategy = (schema) => {
|
|
1585
1818
|
},
|
1586
1819
|
validatePartialResult({ value, latestObject, isFirstDelta, isFinalDelta }) {
|
1587
1820
|
var _a11;
|
1588
|
-
if (!(0,
|
1821
|
+
if (!(0, import_provider10.isJSONObject)(value) || !(0, import_provider10.isJSONArray)(value.elements)) {
|
1589
1822
|
return {
|
1590
1823
|
success: false,
|
1591
|
-
error: new
|
1824
|
+
error: new import_provider10.TypeValidationError({
|
1592
1825
|
value,
|
1593
1826
|
cause: "value must be an object that contains an array of elements"
|
1594
1827
|
})
|
@@ -1628,10 +1861,10 @@ var arrayOutputStrategy = (schema) => {
|
|
1628
1861
|
};
|
1629
1862
|
},
|
1630
1863
|
validateFinalResult(value) {
|
1631
|
-
if (!(0,
|
1864
|
+
if (!(0, import_provider10.isJSONObject)(value) || !(0, import_provider10.isJSONArray)(value.elements)) {
|
1632
1865
|
return {
|
1633
1866
|
success: false,
|
1634
|
-
error: new
|
1867
|
+
error: new import_provider10.TypeValidationError({
|
1635
1868
|
value,
|
1636
1869
|
cause: "value must be an object that contains an array of elements"
|
1637
1870
|
})
|
@@ -1690,10 +1923,10 @@ var enumOutputStrategy = (enumValues) => {
|
|
1690
1923
|
additionalProperties: false
|
1691
1924
|
},
|
1692
1925
|
validateFinalResult(value) {
|
1693
|
-
if (!(0,
|
1926
|
+
if (!(0, import_provider10.isJSONObject)(value) || typeof value.result !== "string") {
|
1694
1927
|
return {
|
1695
1928
|
success: false,
|
1696
|
-
error: new
|
1929
|
+
error: new import_provider10.TypeValidationError({
|
1697
1930
|
value,
|
1698
1931
|
cause: 'value must be an object that contains a string in the "result" property.'
|
1699
1932
|
})
|
@@ -1702,19 +1935,19 @@ var enumOutputStrategy = (enumValues) => {
|
|
1702
1935
|
const result = value.result;
|
1703
1936
|
return enumValues.includes(result) ? { success: true, value: result } : {
|
1704
1937
|
success: false,
|
1705
|
-
error: new
|
1938
|
+
error: new import_provider10.TypeValidationError({
|
1706
1939
|
value,
|
1707
1940
|
cause: "value must be a string in the enum"
|
1708
1941
|
})
|
1709
1942
|
};
|
1710
1943
|
},
|
1711
1944
|
validatePartialResult() {
|
1712
|
-
throw new
|
1945
|
+
throw new import_provider10.UnsupportedFunctionalityError({
|
1713
1946
|
functionality: "partial results in enum mode"
|
1714
1947
|
});
|
1715
1948
|
},
|
1716
1949
|
createElementStream() {
|
1717
|
-
throw new
|
1950
|
+
throw new import_provider10.UnsupportedFunctionalityError({
|
1718
1951
|
functionality: "element streams in enum mode"
|
1719
1952
|
});
|
1720
1953
|
}
|
@@ -1954,16 +2187,19 @@ async function generateObject({
|
|
1954
2187
|
let resultProviderMetadata;
|
1955
2188
|
switch (mode) {
|
1956
2189
|
case "json": {
|
1957
|
-
const
|
1958
|
-
|
1959
|
-
prompt: system
|
1960
|
-
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
2190
|
+
const standardizedPrompt = standardizePrompt({
|
2191
|
+
prompt: {
|
2192
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
2193
|
+
prompt: system,
|
2194
|
+
schema: outputStrategy.jsonSchema
|
2195
|
+
}),
|
2196
|
+
prompt,
|
2197
|
+
messages
|
2198
|
+
},
|
2199
|
+
tools: void 0
|
1964
2200
|
});
|
1965
2201
|
const promptMessages = await convertToLanguageModelPrompt({
|
1966
|
-
prompt:
|
2202
|
+
prompt: standardizedPrompt,
|
1967
2203
|
modelSupportsImageUrls: model.supportsImageUrls,
|
1968
2204
|
modelSupportsUrl: model.supportsUrl
|
1969
2205
|
});
|
@@ -1979,7 +2215,7 @@ async function generateObject({
|
|
1979
2215
|
}),
|
1980
2216
|
...baseTelemetryAttributes,
|
1981
2217
|
"ai.prompt.format": {
|
1982
|
-
input: () =>
|
2218
|
+
input: () => standardizedPrompt.type
|
1983
2219
|
},
|
1984
2220
|
"ai.prompt.messages": {
|
1985
2221
|
input: () => JSON.stringify(promptMessages)
|
@@ -2007,7 +2243,7 @@ async function generateObject({
|
|
2007
2243
|
description: schemaDescription
|
2008
2244
|
},
|
2009
2245
|
...prepareCallSettings(settings),
|
2010
|
-
inputFormat:
|
2246
|
+
inputFormat: standardizedPrompt.type,
|
2011
2247
|
prompt: promptMessages,
|
2012
2248
|
providerMetadata,
|
2013
2249
|
abortSignal,
|
@@ -2060,17 +2296,16 @@ async function generateObject({
|
|
2060
2296
|
break;
|
2061
2297
|
}
|
2062
2298
|
case "tool": {
|
2063
|
-
const
|
2064
|
-
system,
|
2065
|
-
|
2066
|
-
messages
|
2299
|
+
const standardizedPrompt = standardizePrompt({
|
2300
|
+
prompt: { system, prompt, messages },
|
2301
|
+
tools: void 0
|
2067
2302
|
});
|
2068
2303
|
const promptMessages = await convertToLanguageModelPrompt({
|
2069
|
-
prompt:
|
2304
|
+
prompt: standardizedPrompt,
|
2070
2305
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2071
2306
|
modelSupportsUrl: model.supportsUrl
|
2072
2307
|
});
|
2073
|
-
const inputFormat =
|
2308
|
+
const inputFormat = standardizedPrompt.type;
|
2074
2309
|
const generateResult = await retry(
|
2075
2310
|
() => recordSpan({
|
2076
2311
|
name: "ai.generateObject.doGenerate",
|
@@ -2432,13 +2667,16 @@ async function streamObject({
|
|
2432
2667
|
let transformer;
|
2433
2668
|
switch (mode) {
|
2434
2669
|
case "json": {
|
2435
|
-
const
|
2436
|
-
|
2437
|
-
prompt: system
|
2438
|
-
|
2439
|
-
|
2440
|
-
|
2441
|
-
|
2670
|
+
const standardizedPrompt = standardizePrompt({
|
2671
|
+
prompt: {
|
2672
|
+
system: outputStrategy.jsonSchema == null ? injectJsonInstruction({ prompt: system }) : model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
2673
|
+
prompt: system,
|
2674
|
+
schema: outputStrategy.jsonSchema
|
2675
|
+
}),
|
2676
|
+
prompt,
|
2677
|
+
messages
|
2678
|
+
},
|
2679
|
+
tools: void 0
|
2442
2680
|
});
|
2443
2681
|
callOptions = {
|
2444
2682
|
mode: {
|
@@ -2448,9 +2686,9 @@ async function streamObject({
|
|
2448
2686
|
description: schemaDescription
|
2449
2687
|
},
|
2450
2688
|
...prepareCallSettings(settings),
|
2451
|
-
inputFormat:
|
2689
|
+
inputFormat: standardizedPrompt.type,
|
2452
2690
|
prompt: await convertToLanguageModelPrompt({
|
2453
|
-
prompt:
|
2691
|
+
prompt: standardizedPrompt,
|
2454
2692
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2455
2693
|
modelSupportsUrl: model.supportsUrl
|
2456
2694
|
}),
|
@@ -2475,10 +2713,9 @@ async function streamObject({
|
|
2475
2713
|
break;
|
2476
2714
|
}
|
2477
2715
|
case "tool": {
|
2478
|
-
const
|
2479
|
-
system,
|
2480
|
-
|
2481
|
-
messages
|
2716
|
+
const standardizedPrompt = standardizePrompt({
|
2717
|
+
prompt: { system, prompt, messages },
|
2718
|
+
tools: void 0
|
2482
2719
|
});
|
2483
2720
|
callOptions = {
|
2484
2721
|
mode: {
|
@@ -2491,9 +2728,9 @@ async function streamObject({
|
|
2491
2728
|
}
|
2492
2729
|
},
|
2493
2730
|
...prepareCallSettings(settings),
|
2494
|
-
inputFormat:
|
2731
|
+
inputFormat: standardizedPrompt.type,
|
2495
2732
|
prompt: await convertToLanguageModelPrompt({
|
2496
|
-
prompt:
|
2733
|
+
prompt: standardizedPrompt,
|
2497
2734
|
modelSupportsImageUrls: model.supportsImageUrls,
|
2498
2735
|
modelSupportsUrl: model.supportsUrl
|
2499
2736
|
}),
|
@@ -2872,33 +3109,33 @@ var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
2872
3109
|
var import_provider13 = require("@ai-sdk/provider");
|
2873
3110
|
|
2874
3111
|
// errors/invalid-tool-arguments-error.ts
|
2875
|
-
var
|
2876
|
-
var
|
2877
|
-
var
|
2878
|
-
var
|
2879
|
-
var
|
2880
|
-
var InvalidToolArgumentsError = class extends
|
3112
|
+
var import_provider11 = require("@ai-sdk/provider");
|
3113
|
+
var name8 = "AI_InvalidToolArgumentsError";
|
3114
|
+
var marker8 = `vercel.ai.error.${name8}`;
|
3115
|
+
var symbol8 = Symbol.for(marker8);
|
3116
|
+
var _a8;
|
3117
|
+
var InvalidToolArgumentsError = class extends import_provider11.AISDKError {
|
2881
3118
|
constructor({
|
2882
3119
|
toolArgs,
|
2883
3120
|
toolName,
|
2884
3121
|
cause,
|
2885
|
-
message = `Invalid arguments for tool ${toolName}: ${(0,
|
3122
|
+
message = `Invalid arguments for tool ${toolName}: ${(0, import_provider11.getErrorMessage)(
|
2886
3123
|
cause
|
2887
3124
|
)}`
|
2888
3125
|
}) {
|
2889
|
-
super({ name:
|
2890
|
-
this[
|
3126
|
+
super({ name: name8, message, cause });
|
3127
|
+
this[_a8] = true;
|
2891
3128
|
this.toolArgs = toolArgs;
|
2892
3129
|
this.toolName = toolName;
|
2893
3130
|
}
|
2894
3131
|
static isInstance(error) {
|
2895
|
-
return
|
3132
|
+
return import_provider11.AISDKError.hasMarker(error, marker8);
|
2896
3133
|
}
|
2897
3134
|
/**
|
2898
3135
|
* @deprecated use `isInstance` instead
|
2899
3136
|
*/
|
2900
3137
|
static isInvalidToolArgumentsError(error) {
|
2901
|
-
return error instanceof Error && error.name ===
|
3138
|
+
return error instanceof Error && error.name === name8 && typeof error.toolName === "string" && typeof error.toolArgs === "string";
|
2902
3139
|
}
|
2903
3140
|
/**
|
2904
3141
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -2914,33 +3151,33 @@ var InvalidToolArgumentsError = class extends import_provider10.AISDKError {
|
|
2914
3151
|
};
|
2915
3152
|
}
|
2916
3153
|
};
|
2917
|
-
|
3154
|
+
_a8 = symbol8;
|
2918
3155
|
|
2919
3156
|
// errors/no-such-tool-error.ts
|
2920
|
-
var
|
2921
|
-
var
|
2922
|
-
var
|
2923
|
-
var
|
2924
|
-
var
|
2925
|
-
var NoSuchToolError = class extends
|
3157
|
+
var import_provider12 = require("@ai-sdk/provider");
|
3158
|
+
var name9 = "AI_NoSuchToolError";
|
3159
|
+
var marker9 = `vercel.ai.error.${name9}`;
|
3160
|
+
var symbol9 = Symbol.for(marker9);
|
3161
|
+
var _a9;
|
3162
|
+
var NoSuchToolError = class extends import_provider12.AISDKError {
|
2926
3163
|
constructor({
|
2927
3164
|
toolName,
|
2928
3165
|
availableTools = void 0,
|
2929
3166
|
message = `Model tried to call unavailable tool '${toolName}'. ${availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`}`
|
2930
3167
|
}) {
|
2931
|
-
super({ name:
|
2932
|
-
this[
|
3168
|
+
super({ name: name9, message });
|
3169
|
+
this[_a9] = true;
|
2933
3170
|
this.toolName = toolName;
|
2934
3171
|
this.availableTools = availableTools;
|
2935
3172
|
}
|
2936
3173
|
static isInstance(error) {
|
2937
|
-
return
|
3174
|
+
return import_provider12.AISDKError.hasMarker(error, marker9);
|
2938
3175
|
}
|
2939
3176
|
/**
|
2940
3177
|
* @deprecated use `isInstance` instead
|
2941
3178
|
*/
|
2942
3179
|
static isNoSuchToolError(error) {
|
2943
|
-
return error instanceof Error && error.name ===
|
3180
|
+
return error instanceof Error && error.name === name9 && "toolName" in error && error.toolName != void 0 && typeof error.name === "string";
|
2944
3181
|
}
|
2945
3182
|
/**
|
2946
3183
|
* @deprecated Do not use this method. It will be removed in the next major version.
|
@@ -2955,27 +3192,6 @@ var NoSuchToolError = class extends import_provider11.AISDKError {
|
|
2955
3192
|
};
|
2956
3193
|
}
|
2957
3194
|
};
|
2958
|
-
_a8 = symbol8;
|
2959
|
-
|
2960
|
-
// core/prompt/message-conversion-error.ts
|
2961
|
-
var import_provider12 = require("@ai-sdk/provider");
|
2962
|
-
var name9 = "AI_MessageConversionError";
|
2963
|
-
var marker9 = `vercel.ai.error.${name9}`;
|
2964
|
-
var symbol9 = Symbol.for(marker9);
|
2965
|
-
var _a9;
|
2966
|
-
var MessageConversionError = class extends import_provider12.AISDKError {
|
2967
|
-
constructor({
|
2968
|
-
originalMessage,
|
2969
|
-
message
|
2970
|
-
}) {
|
2971
|
-
super({ name: name9, message });
|
2972
|
-
this[_a9] = true;
|
2973
|
-
this.originalMessage = originalMessage;
|
2974
|
-
}
|
2975
|
-
static isInstance(error) {
|
2976
|
-
return import_provider12.AISDKError.hasMarker(error, marker9);
|
2977
|
-
}
|
2978
|
-
};
|
2979
3195
|
_a9 = symbol9;
|
2980
3196
|
|
2981
3197
|
// core/prompt/prepare-tools-and-tool-choice.ts
|
@@ -3155,7 +3371,10 @@ async function generateText({
|
|
3155
3371
|
headers,
|
3156
3372
|
settings: { ...settings, maxRetries }
|
3157
3373
|
});
|
3158
|
-
const initialPrompt = standardizePrompt({
|
3374
|
+
const initialPrompt = standardizePrompt({
|
3375
|
+
prompt: { system, prompt, messages },
|
3376
|
+
tools
|
3377
|
+
});
|
3159
3378
|
const tracer = getTracer(telemetry);
|
3160
3379
|
return recordSpan({
|
3161
3380
|
name: "ai.generateText",
|
@@ -3881,7 +4100,10 @@ async function streamText({
|
|
3881
4100
|
settings: { ...settings, maxRetries }
|
3882
4101
|
});
|
3883
4102
|
const tracer = getTracer(telemetry);
|
3884
|
-
const initialPrompt = standardizePrompt({
|
4103
|
+
const initialPrompt = standardizePrompt({
|
4104
|
+
prompt: { system, prompt, messages },
|
4105
|
+
tools
|
4106
|
+
});
|
3885
4107
|
return recordSpan({
|
3886
4108
|
name: "ai.streamText",
|
3887
4109
|
attributes: selectTelemetryAttributes({
|
@@ -4705,171 +4927,6 @@ var experimental_wrapLanguageModel = ({
|
|
4705
4927
|
};
|
4706
4928
|
};
|
4707
4929
|
|
4708
|
-
// core/prompt/attachments-to-parts.ts
|
4709
|
-
function attachmentsToParts(attachments) {
|
4710
|
-
var _a11, _b, _c;
|
4711
|
-
const parts = [];
|
4712
|
-
for (const attachment of attachments) {
|
4713
|
-
let url;
|
4714
|
-
try {
|
4715
|
-
url = new URL(attachment.url);
|
4716
|
-
} catch (error) {
|
4717
|
-
throw new Error(`Invalid URL: ${attachment.url}`);
|
4718
|
-
}
|
4719
|
-
switch (url.protocol) {
|
4720
|
-
case "http:":
|
4721
|
-
case "https:": {
|
4722
|
-
if ((_a11 = attachment.contentType) == null ? void 0 : _a11.startsWith("image/")) {
|
4723
|
-
parts.push({ type: "image", image: url });
|
4724
|
-
} else {
|
4725
|
-
if (!attachment.contentType) {
|
4726
|
-
throw new Error(
|
4727
|
-
"If the attachment is not an image, it must specify a content type"
|
4728
|
-
);
|
4729
|
-
}
|
4730
|
-
parts.push({
|
4731
|
-
type: "file",
|
4732
|
-
data: url,
|
4733
|
-
mimeType: attachment.contentType
|
4734
|
-
});
|
4735
|
-
}
|
4736
|
-
break;
|
4737
|
-
}
|
4738
|
-
case "data:": {
|
4739
|
-
let header;
|
4740
|
-
let base64Content;
|
4741
|
-
let mimeType;
|
4742
|
-
try {
|
4743
|
-
[header, base64Content] = attachment.url.split(",");
|
4744
|
-
mimeType = header.split(";")[0].split(":")[1];
|
4745
|
-
} catch (error) {
|
4746
|
-
throw new Error(`Error processing data URL: ${attachment.url}`);
|
4747
|
-
}
|
4748
|
-
if (mimeType == null || base64Content == null) {
|
4749
|
-
throw new Error(`Invalid data URL format: ${attachment.url}`);
|
4750
|
-
}
|
4751
|
-
if ((_b = attachment.contentType) == null ? void 0 : _b.startsWith("image/")) {
|
4752
|
-
parts.push({
|
4753
|
-
type: "image",
|
4754
|
-
image: convertDataContentToUint8Array(base64Content)
|
4755
|
-
});
|
4756
|
-
} else if ((_c = attachment.contentType) == null ? void 0 : _c.startsWith("text/")) {
|
4757
|
-
parts.push({
|
4758
|
-
type: "text",
|
4759
|
-
text: convertUint8ArrayToText(
|
4760
|
-
convertDataContentToUint8Array(base64Content)
|
4761
|
-
)
|
4762
|
-
});
|
4763
|
-
} else {
|
4764
|
-
if (!attachment.contentType) {
|
4765
|
-
throw new Error(
|
4766
|
-
"If the attachment is not an image or text, it must specify a content type"
|
4767
|
-
);
|
4768
|
-
}
|
4769
|
-
parts.push({
|
4770
|
-
type: "file",
|
4771
|
-
data: base64Content,
|
4772
|
-
mimeType: attachment.contentType
|
4773
|
-
});
|
4774
|
-
}
|
4775
|
-
break;
|
4776
|
-
}
|
4777
|
-
default: {
|
4778
|
-
throw new Error(`Unsupported URL protocol: ${url.protocol}`);
|
4779
|
-
}
|
4780
|
-
}
|
4781
|
-
}
|
4782
|
-
return parts;
|
4783
|
-
}
|
4784
|
-
|
4785
|
-
// core/prompt/convert-to-core-messages.ts
|
4786
|
-
function convertToCoreMessages(messages, options) {
|
4787
|
-
var _a11;
|
4788
|
-
const tools = (_a11 = options == null ? void 0 : options.tools) != null ? _a11 : {};
|
4789
|
-
const coreMessages = [];
|
4790
|
-
for (const message of messages) {
|
4791
|
-
const { role, content, toolInvocations, experimental_attachments } = message;
|
4792
|
-
switch (role) {
|
4793
|
-
case "system": {
|
4794
|
-
coreMessages.push({
|
4795
|
-
role: "system",
|
4796
|
-
content
|
4797
|
-
});
|
4798
|
-
break;
|
4799
|
-
}
|
4800
|
-
case "user": {
|
4801
|
-
coreMessages.push({
|
4802
|
-
role: "user",
|
4803
|
-
content: experimental_attachments ? [
|
4804
|
-
{ type: "text", text: content },
|
4805
|
-
...attachmentsToParts(experimental_attachments)
|
4806
|
-
] : content
|
4807
|
-
});
|
4808
|
-
break;
|
4809
|
-
}
|
4810
|
-
case "assistant": {
|
4811
|
-
if (toolInvocations == null) {
|
4812
|
-
coreMessages.push({ role: "assistant", content });
|
4813
|
-
break;
|
4814
|
-
}
|
4815
|
-
coreMessages.push({
|
4816
|
-
role: "assistant",
|
4817
|
-
content: [
|
4818
|
-
{ type: "text", text: content },
|
4819
|
-
...toolInvocations.map(
|
4820
|
-
({ toolCallId, toolName, args }) => ({
|
4821
|
-
type: "tool-call",
|
4822
|
-
toolCallId,
|
4823
|
-
toolName,
|
4824
|
-
args
|
4825
|
-
})
|
4826
|
-
)
|
4827
|
-
]
|
4828
|
-
});
|
4829
|
-
coreMessages.push({
|
4830
|
-
role: "tool",
|
4831
|
-
content: toolInvocations.map((toolInvocation) => {
|
4832
|
-
if (!("result" in toolInvocation)) {
|
4833
|
-
throw new MessageConversionError({
|
4834
|
-
originalMessage: message,
|
4835
|
-
message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
|
4836
|
-
});
|
4837
|
-
}
|
4838
|
-
const { toolCallId, toolName, result } = toolInvocation;
|
4839
|
-
const tool2 = tools[toolName];
|
4840
|
-
return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
|
4841
|
-
type: "tool-result",
|
4842
|
-
toolCallId,
|
4843
|
-
toolName,
|
4844
|
-
result: tool2.experimental_toToolResultContent(result),
|
4845
|
-
experimental_content: tool2.experimental_toToolResultContent(result)
|
4846
|
-
} : {
|
4847
|
-
type: "tool-result",
|
4848
|
-
toolCallId,
|
4849
|
-
toolName,
|
4850
|
-
result
|
4851
|
-
};
|
4852
|
-
})
|
4853
|
-
});
|
4854
|
-
break;
|
4855
|
-
}
|
4856
|
-
case "function":
|
4857
|
-
case "data":
|
4858
|
-
case "tool": {
|
4859
|
-
break;
|
4860
|
-
}
|
4861
|
-
default: {
|
4862
|
-
const _exhaustiveCheck = role;
|
4863
|
-
throw new MessageConversionError({
|
4864
|
-
originalMessage: message,
|
4865
|
-
message: `Unsupported role: ${_exhaustiveCheck}`
|
4866
|
-
});
|
4867
|
-
}
|
4868
|
-
}
|
4869
|
-
}
|
4870
|
-
return coreMessages;
|
4871
|
-
}
|
4872
|
-
|
4873
4930
|
// core/registry/custom-provider.ts
|
4874
4931
|
var import_provider14 = require("@ai-sdk/provider");
|
4875
4932
|
function experimental_customProvider({
|