ai 4.1.7 → 4.1.9
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 +16 -0
- package/dist/index.d.mts +67 -37
- package/dist/index.d.ts +67 -37
- package/dist/index.js +253 -144
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +252 -144
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/rsc/dist/index.d.ts +2 -2
- package/rsc/dist/rsc-server.d.mts +2 -2
- package/rsc/dist/rsc-server.mjs +144 -144
- package/rsc/dist/rsc-server.mjs.map +1 -1
package/dist/index.js
CHANGED
@@ -68,6 +68,7 @@ __export(streams_exports, {
|
|
68
68
|
experimental_customProvider: () => experimental_customProvider,
|
69
69
|
experimental_generateImage: () => generateImage,
|
70
70
|
experimental_wrapLanguageModel: () => experimental_wrapLanguageModel,
|
71
|
+
extractReasoningMiddleware: () => extractReasoningMiddleware,
|
71
72
|
formatAssistantStreamPart: () => import_ui_utils9.formatAssistantStreamPart,
|
72
73
|
formatDataStreamPart: () => import_ui_utils9.formatDataStreamPart,
|
73
74
|
generateId: () => import_provider_utils14.generateId,
|
@@ -1484,150 +1485,6 @@ var import_provider10 = require("@ai-sdk/provider");
|
|
1484
1485
|
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
1485
1486
|
var import_zod7 = require("zod");
|
1486
1487
|
|
1487
|
-
// core/prompt/message.ts
|
1488
|
-
var import_zod6 = require("zod");
|
1489
|
-
|
1490
|
-
// core/types/provider-metadata.ts
|
1491
|
-
var import_zod3 = require("zod");
|
1492
|
-
|
1493
|
-
// core/types/json-value.ts
|
1494
|
-
var import_zod2 = require("zod");
|
1495
|
-
var jsonValueSchema = import_zod2.z.lazy(
|
1496
|
-
() => import_zod2.z.union([
|
1497
|
-
import_zod2.z.null(),
|
1498
|
-
import_zod2.z.string(),
|
1499
|
-
import_zod2.z.number(),
|
1500
|
-
import_zod2.z.boolean(),
|
1501
|
-
import_zod2.z.record(import_zod2.z.string(), jsonValueSchema),
|
1502
|
-
import_zod2.z.array(jsonValueSchema)
|
1503
|
-
])
|
1504
|
-
);
|
1505
|
-
|
1506
|
-
// core/types/provider-metadata.ts
|
1507
|
-
var providerMetadataSchema = import_zod3.z.record(
|
1508
|
-
import_zod3.z.string(),
|
1509
|
-
import_zod3.z.record(import_zod3.z.string(), jsonValueSchema)
|
1510
|
-
);
|
1511
|
-
|
1512
|
-
// core/prompt/content-part.ts
|
1513
|
-
var import_zod5 = require("zod");
|
1514
|
-
|
1515
|
-
// core/prompt/tool-result-content.ts
|
1516
|
-
var import_zod4 = require("zod");
|
1517
|
-
var toolResultContentSchema = import_zod4.z.array(
|
1518
|
-
import_zod4.z.union([
|
1519
|
-
import_zod4.z.object({ type: import_zod4.z.literal("text"), text: import_zod4.z.string() }),
|
1520
|
-
import_zod4.z.object({
|
1521
|
-
type: import_zod4.z.literal("image"),
|
1522
|
-
data: import_zod4.z.string(),
|
1523
|
-
mimeType: import_zod4.z.string().optional()
|
1524
|
-
})
|
1525
|
-
])
|
1526
|
-
);
|
1527
|
-
|
1528
|
-
// core/prompt/content-part.ts
|
1529
|
-
var textPartSchema = import_zod5.z.object({
|
1530
|
-
type: import_zod5.z.literal("text"),
|
1531
|
-
text: import_zod5.z.string(),
|
1532
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1533
|
-
});
|
1534
|
-
var imagePartSchema = import_zod5.z.object({
|
1535
|
-
type: import_zod5.z.literal("image"),
|
1536
|
-
image: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
1537
|
-
mimeType: import_zod5.z.string().optional(),
|
1538
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1539
|
-
});
|
1540
|
-
var filePartSchema = import_zod5.z.object({
|
1541
|
-
type: import_zod5.z.literal("file"),
|
1542
|
-
data: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
1543
|
-
mimeType: import_zod5.z.string(),
|
1544
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1545
|
-
});
|
1546
|
-
var toolCallPartSchema = import_zod5.z.object({
|
1547
|
-
type: import_zod5.z.literal("tool-call"),
|
1548
|
-
toolCallId: import_zod5.z.string(),
|
1549
|
-
toolName: import_zod5.z.string(),
|
1550
|
-
args: import_zod5.z.unknown()
|
1551
|
-
});
|
1552
|
-
var toolResultPartSchema = import_zod5.z.object({
|
1553
|
-
type: import_zod5.z.literal("tool-result"),
|
1554
|
-
toolCallId: import_zod5.z.string(),
|
1555
|
-
toolName: import_zod5.z.string(),
|
1556
|
-
result: import_zod5.z.unknown(),
|
1557
|
-
content: toolResultContentSchema.optional(),
|
1558
|
-
isError: import_zod5.z.boolean().optional(),
|
1559
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1560
|
-
});
|
1561
|
-
|
1562
|
-
// core/prompt/message.ts
|
1563
|
-
var coreSystemMessageSchema = import_zod6.z.object({
|
1564
|
-
role: import_zod6.z.literal("system"),
|
1565
|
-
content: import_zod6.z.string(),
|
1566
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1567
|
-
});
|
1568
|
-
var coreUserMessageSchema = import_zod6.z.object({
|
1569
|
-
role: import_zod6.z.literal("user"),
|
1570
|
-
content: import_zod6.z.union([
|
1571
|
-
import_zod6.z.string(),
|
1572
|
-
import_zod6.z.array(import_zod6.z.union([textPartSchema, imagePartSchema, filePartSchema]))
|
1573
|
-
]),
|
1574
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1575
|
-
});
|
1576
|
-
var coreAssistantMessageSchema = import_zod6.z.object({
|
1577
|
-
role: import_zod6.z.literal("assistant"),
|
1578
|
-
content: import_zod6.z.union([
|
1579
|
-
import_zod6.z.string(),
|
1580
|
-
import_zod6.z.array(import_zod6.z.union([textPartSchema, toolCallPartSchema]))
|
1581
|
-
]),
|
1582
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1583
|
-
});
|
1584
|
-
var coreToolMessageSchema = import_zod6.z.object({
|
1585
|
-
role: import_zod6.z.literal("tool"),
|
1586
|
-
content: import_zod6.z.array(toolResultPartSchema),
|
1587
|
-
experimental_providerMetadata: providerMetadataSchema.optional()
|
1588
|
-
});
|
1589
|
-
var coreMessageSchema = import_zod6.z.union([
|
1590
|
-
coreSystemMessageSchema,
|
1591
|
-
coreUserMessageSchema,
|
1592
|
-
coreAssistantMessageSchema,
|
1593
|
-
coreToolMessageSchema
|
1594
|
-
]);
|
1595
|
-
|
1596
|
-
// core/prompt/detect-prompt-type.ts
|
1597
|
-
function detectPromptType(prompt) {
|
1598
|
-
if (!Array.isArray(prompt)) {
|
1599
|
-
return "other";
|
1600
|
-
}
|
1601
|
-
if (prompt.length === 0) {
|
1602
|
-
return "messages";
|
1603
|
-
}
|
1604
|
-
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
1605
|
-
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
1606
|
-
return "ui-messages";
|
1607
|
-
} else if (characteristics.every(
|
1608
|
-
(c) => c === "has-core-specific-parts" || c === "message"
|
1609
|
-
)) {
|
1610
|
-
return "messages";
|
1611
|
-
} else {
|
1612
|
-
return "other";
|
1613
|
-
}
|
1614
|
-
}
|
1615
|
-
function detectSingleMessageCharacteristics(message) {
|
1616
|
-
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
1617
|
-
message.role === "data" || // UI-only role
|
1618
|
-
"toolInvocations" in message || // UI-specific field
|
1619
|
-
"experimental_attachments" in message)) {
|
1620
|
-
return "has-ui-specific-parts";
|
1621
|
-
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
1622
|
-
"experimental_providerMetadata" in message)) {
|
1623
|
-
return "has-core-specific-parts";
|
1624
|
-
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
1625
|
-
return "message";
|
1626
|
-
} else {
|
1627
|
-
return "other";
|
1628
|
-
}
|
1629
|
-
}
|
1630
|
-
|
1631
1488
|
// core/prompt/attachments-to-parts.ts
|
1632
1489
|
function attachmentsToParts(attachments) {
|
1633
1490
|
var _a15, _b, _c;
|
@@ -1812,6 +1669,150 @@ function convertToCoreMessages(messages, options) {
|
|
1812
1669
|
return coreMessages;
|
1813
1670
|
}
|
1814
1671
|
|
1672
|
+
// core/prompt/detect-prompt-type.ts
|
1673
|
+
function detectPromptType(prompt) {
|
1674
|
+
if (!Array.isArray(prompt)) {
|
1675
|
+
return "other";
|
1676
|
+
}
|
1677
|
+
if (prompt.length === 0) {
|
1678
|
+
return "messages";
|
1679
|
+
}
|
1680
|
+
const characteristics = prompt.map(detectSingleMessageCharacteristics);
|
1681
|
+
if (characteristics.some((c) => c === "has-ui-specific-parts")) {
|
1682
|
+
return "ui-messages";
|
1683
|
+
} else if (characteristics.every(
|
1684
|
+
(c) => c === "has-core-specific-parts" || c === "message"
|
1685
|
+
)) {
|
1686
|
+
return "messages";
|
1687
|
+
} else {
|
1688
|
+
return "other";
|
1689
|
+
}
|
1690
|
+
}
|
1691
|
+
function detectSingleMessageCharacteristics(message) {
|
1692
|
+
if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
|
1693
|
+
message.role === "data" || // UI-only role
|
1694
|
+
"toolInvocations" in message || // UI-specific field
|
1695
|
+
"experimental_attachments" in message)) {
|
1696
|
+
return "has-ui-specific-parts";
|
1697
|
+
} else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
|
1698
|
+
"experimental_providerMetadata" in message)) {
|
1699
|
+
return "has-core-specific-parts";
|
1700
|
+
} else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
|
1701
|
+
return "message";
|
1702
|
+
} else {
|
1703
|
+
return "other";
|
1704
|
+
}
|
1705
|
+
}
|
1706
|
+
|
1707
|
+
// core/prompt/message.ts
|
1708
|
+
var import_zod6 = require("zod");
|
1709
|
+
|
1710
|
+
// core/types/provider-metadata.ts
|
1711
|
+
var import_zod3 = require("zod");
|
1712
|
+
|
1713
|
+
// core/types/json-value.ts
|
1714
|
+
var import_zod2 = require("zod");
|
1715
|
+
var jsonValueSchema = import_zod2.z.lazy(
|
1716
|
+
() => import_zod2.z.union([
|
1717
|
+
import_zod2.z.null(),
|
1718
|
+
import_zod2.z.string(),
|
1719
|
+
import_zod2.z.number(),
|
1720
|
+
import_zod2.z.boolean(),
|
1721
|
+
import_zod2.z.record(import_zod2.z.string(), jsonValueSchema),
|
1722
|
+
import_zod2.z.array(jsonValueSchema)
|
1723
|
+
])
|
1724
|
+
);
|
1725
|
+
|
1726
|
+
// core/types/provider-metadata.ts
|
1727
|
+
var providerMetadataSchema = import_zod3.z.record(
|
1728
|
+
import_zod3.z.string(),
|
1729
|
+
import_zod3.z.record(import_zod3.z.string(), jsonValueSchema)
|
1730
|
+
);
|
1731
|
+
|
1732
|
+
// core/prompt/content-part.ts
|
1733
|
+
var import_zod5 = require("zod");
|
1734
|
+
|
1735
|
+
// core/prompt/tool-result-content.ts
|
1736
|
+
var import_zod4 = require("zod");
|
1737
|
+
var toolResultContentSchema = import_zod4.z.array(
|
1738
|
+
import_zod4.z.union([
|
1739
|
+
import_zod4.z.object({ type: import_zod4.z.literal("text"), text: import_zod4.z.string() }),
|
1740
|
+
import_zod4.z.object({
|
1741
|
+
type: import_zod4.z.literal("image"),
|
1742
|
+
data: import_zod4.z.string(),
|
1743
|
+
mimeType: import_zod4.z.string().optional()
|
1744
|
+
})
|
1745
|
+
])
|
1746
|
+
);
|
1747
|
+
|
1748
|
+
// core/prompt/content-part.ts
|
1749
|
+
var textPartSchema = import_zod5.z.object({
|
1750
|
+
type: import_zod5.z.literal("text"),
|
1751
|
+
text: import_zod5.z.string(),
|
1752
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1753
|
+
});
|
1754
|
+
var imagePartSchema = import_zod5.z.object({
|
1755
|
+
type: import_zod5.z.literal("image"),
|
1756
|
+
image: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
1757
|
+
mimeType: import_zod5.z.string().optional(),
|
1758
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1759
|
+
});
|
1760
|
+
var filePartSchema = import_zod5.z.object({
|
1761
|
+
type: import_zod5.z.literal("file"),
|
1762
|
+
data: import_zod5.z.union([dataContentSchema, import_zod5.z.instanceof(URL)]),
|
1763
|
+
mimeType: import_zod5.z.string(),
|
1764
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1765
|
+
});
|
1766
|
+
var toolCallPartSchema = import_zod5.z.object({
|
1767
|
+
type: import_zod5.z.literal("tool-call"),
|
1768
|
+
toolCallId: import_zod5.z.string(),
|
1769
|
+
toolName: import_zod5.z.string(),
|
1770
|
+
args: import_zod5.z.unknown()
|
1771
|
+
});
|
1772
|
+
var toolResultPartSchema = import_zod5.z.object({
|
1773
|
+
type: import_zod5.z.literal("tool-result"),
|
1774
|
+
toolCallId: import_zod5.z.string(),
|
1775
|
+
toolName: import_zod5.z.string(),
|
1776
|
+
result: import_zod5.z.unknown(),
|
1777
|
+
content: toolResultContentSchema.optional(),
|
1778
|
+
isError: import_zod5.z.boolean().optional(),
|
1779
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1780
|
+
});
|
1781
|
+
|
1782
|
+
// core/prompt/message.ts
|
1783
|
+
var coreSystemMessageSchema = import_zod6.z.object({
|
1784
|
+
role: import_zod6.z.literal("system"),
|
1785
|
+
content: import_zod6.z.string(),
|
1786
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1787
|
+
});
|
1788
|
+
var coreUserMessageSchema = import_zod6.z.object({
|
1789
|
+
role: import_zod6.z.literal("user"),
|
1790
|
+
content: import_zod6.z.union([
|
1791
|
+
import_zod6.z.string(),
|
1792
|
+
import_zod6.z.array(import_zod6.z.union([textPartSchema, imagePartSchema, filePartSchema]))
|
1793
|
+
]),
|
1794
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1795
|
+
});
|
1796
|
+
var coreAssistantMessageSchema = import_zod6.z.object({
|
1797
|
+
role: import_zod6.z.literal("assistant"),
|
1798
|
+
content: import_zod6.z.union([
|
1799
|
+
import_zod6.z.string(),
|
1800
|
+
import_zod6.z.array(import_zod6.z.union([textPartSchema, toolCallPartSchema]))
|
1801
|
+
]),
|
1802
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1803
|
+
});
|
1804
|
+
var coreToolMessageSchema = import_zod6.z.object({
|
1805
|
+
role: import_zod6.z.literal("tool"),
|
1806
|
+
content: import_zod6.z.array(toolResultPartSchema),
|
1807
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
1808
|
+
});
|
1809
|
+
var coreMessageSchema = import_zod6.z.union([
|
1810
|
+
coreSystemMessageSchema,
|
1811
|
+
coreUserMessageSchema,
|
1812
|
+
coreAssistantMessageSchema,
|
1813
|
+
coreToolMessageSchema
|
1814
|
+
]);
|
1815
|
+
|
1815
1816
|
// core/prompt/standardize-prompt.ts
|
1816
1817
|
function standardizePrompt({
|
1817
1818
|
prompt,
|
@@ -5630,6 +5631,113 @@ var experimental_wrapLanguageModel = ({
|
|
5630
5631
|
};
|
5631
5632
|
};
|
5632
5633
|
|
5634
|
+
// core/util/get-potential-start-index.ts
|
5635
|
+
function getPotentialStartIndex(text2, searchedText) {
|
5636
|
+
if (searchedText.length === 0) {
|
5637
|
+
return null;
|
5638
|
+
}
|
5639
|
+
const directIndex = text2.indexOf(searchedText);
|
5640
|
+
if (directIndex !== -1) {
|
5641
|
+
return directIndex;
|
5642
|
+
}
|
5643
|
+
for (let i = text2.length - 1; i >= 0; i--) {
|
5644
|
+
const suffix = text2.substring(i);
|
5645
|
+
if (searchedText.startsWith(suffix)) {
|
5646
|
+
return i;
|
5647
|
+
}
|
5648
|
+
}
|
5649
|
+
return null;
|
5650
|
+
}
|
5651
|
+
|
5652
|
+
// core/middleware/extract-reasoning-middleware.ts
|
5653
|
+
function extractReasoningMiddleware({
|
5654
|
+
tagName,
|
5655
|
+
separator = "\n"
|
5656
|
+
}) {
|
5657
|
+
const openingTag = `<${tagName}>`;
|
5658
|
+
const closingTag = `</${tagName}>`;
|
5659
|
+
return {
|
5660
|
+
wrapGenerate: async ({ doGenerate }) => {
|
5661
|
+
const { text: text2, ...rest } = await doGenerate();
|
5662
|
+
if (text2 == null) {
|
5663
|
+
return { text: text2, ...rest };
|
5664
|
+
}
|
5665
|
+
const regexp = new RegExp(`${openingTag}(.*?)${closingTag}`, "gs");
|
5666
|
+
const matches = Array.from(text2.matchAll(regexp));
|
5667
|
+
if (!matches.length) {
|
5668
|
+
return { text: text2, ...rest };
|
5669
|
+
}
|
5670
|
+
const reasoning = matches.map((match) => match[1]).join(separator);
|
5671
|
+
let textWithoutReasoning = text2;
|
5672
|
+
for (let i = matches.length - 1; i >= 0; i--) {
|
5673
|
+
const match = matches[i];
|
5674
|
+
const beforeMatch = textWithoutReasoning.slice(0, match.index);
|
5675
|
+
const afterMatch = textWithoutReasoning.slice(
|
5676
|
+
match.index + match[0].length
|
5677
|
+
);
|
5678
|
+
textWithoutReasoning = beforeMatch + (beforeMatch.length > 0 && afterMatch.length > 0 ? separator : "") + afterMatch;
|
5679
|
+
}
|
5680
|
+
return { text: textWithoutReasoning, reasoning, ...rest };
|
5681
|
+
},
|
5682
|
+
wrapStream: async ({ doStream }) => {
|
5683
|
+
const { stream, ...rest } = await doStream();
|
5684
|
+
let isFirstReasoning = true;
|
5685
|
+
let isFirstText = true;
|
5686
|
+
let afterSwitch = false;
|
5687
|
+
let isReasoning = false;
|
5688
|
+
let buffer = "";
|
5689
|
+
return {
|
5690
|
+
stream: stream.pipeThrough(
|
5691
|
+
new TransformStream({
|
5692
|
+
transform: (chunk, controller) => {
|
5693
|
+
if (chunk.type !== "text-delta") {
|
5694
|
+
controller.enqueue(chunk);
|
5695
|
+
return;
|
5696
|
+
}
|
5697
|
+
buffer += chunk.textDelta;
|
5698
|
+
function publish(text2) {
|
5699
|
+
if (text2.length > 0) {
|
5700
|
+
const prefix = afterSwitch && (isReasoning ? !isFirstReasoning : !isFirstText) ? separator : "";
|
5701
|
+
controller.enqueue({
|
5702
|
+
type: isReasoning ? "reasoning" : "text-delta",
|
5703
|
+
textDelta: prefix + text2
|
5704
|
+
});
|
5705
|
+
afterSwitch = false;
|
5706
|
+
if (isReasoning) {
|
5707
|
+
isFirstReasoning = false;
|
5708
|
+
} else {
|
5709
|
+
isFirstText = false;
|
5710
|
+
}
|
5711
|
+
}
|
5712
|
+
}
|
5713
|
+
do {
|
5714
|
+
const nextTag = isReasoning ? closingTag : openingTag;
|
5715
|
+
const startIndex = getPotentialStartIndex(buffer, nextTag);
|
5716
|
+
if (startIndex == null) {
|
5717
|
+
publish(buffer);
|
5718
|
+
buffer = "";
|
5719
|
+
break;
|
5720
|
+
}
|
5721
|
+
publish(buffer.slice(0, startIndex));
|
5722
|
+
const foundFullMatch = startIndex + nextTag.length <= buffer.length;
|
5723
|
+
if (foundFullMatch) {
|
5724
|
+
buffer = buffer.slice(startIndex + nextTag.length);
|
5725
|
+
isReasoning = !isReasoning;
|
5726
|
+
afterSwitch = true;
|
5727
|
+
} else {
|
5728
|
+
buffer = buffer.slice(startIndex);
|
5729
|
+
break;
|
5730
|
+
}
|
5731
|
+
} while (true);
|
5732
|
+
}
|
5733
|
+
})
|
5734
|
+
),
|
5735
|
+
...rest
|
5736
|
+
};
|
5737
|
+
}
|
5738
|
+
};
|
5739
|
+
}
|
5740
|
+
|
5633
5741
|
// core/prompt/append-client-message.ts
|
5634
5742
|
function appendClientMessage({
|
5635
5743
|
messages,
|
@@ -6264,6 +6372,7 @@ var StreamData = class {
|
|
6264
6372
|
experimental_customProvider,
|
6265
6373
|
experimental_generateImage,
|
6266
6374
|
experimental_wrapLanguageModel,
|
6375
|
+
extractReasoningMiddleware,
|
6267
6376
|
formatAssistantStreamPart,
|
6268
6377
|
formatDataStreamPart,
|
6269
6378
|
generateId,
|