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/dist/index.mjs CHANGED
@@ -1415,150 +1415,6 @@ import { InvalidPromptError } from "@ai-sdk/provider";
1415
1415
  import { safeValidateTypes } from "@ai-sdk/provider-utils";
1416
1416
  import { z as z7 } from "zod";
1417
1417
 
1418
- // core/prompt/message.ts
1419
- import { z as z6 } from "zod";
1420
-
1421
- // core/types/provider-metadata.ts
1422
- import { z as z3 } from "zod";
1423
-
1424
- // core/types/json-value.ts
1425
- import { z as z2 } from "zod";
1426
- var jsonValueSchema = z2.lazy(
1427
- () => z2.union([
1428
- z2.null(),
1429
- z2.string(),
1430
- z2.number(),
1431
- z2.boolean(),
1432
- z2.record(z2.string(), jsonValueSchema),
1433
- z2.array(jsonValueSchema)
1434
- ])
1435
- );
1436
-
1437
- // core/types/provider-metadata.ts
1438
- var providerMetadataSchema = z3.record(
1439
- z3.string(),
1440
- z3.record(z3.string(), jsonValueSchema)
1441
- );
1442
-
1443
- // core/prompt/content-part.ts
1444
- import { z as z5 } from "zod";
1445
-
1446
- // core/prompt/tool-result-content.ts
1447
- import { z as z4 } from "zod";
1448
- var toolResultContentSchema = z4.array(
1449
- z4.union([
1450
- z4.object({ type: z4.literal("text"), text: z4.string() }),
1451
- z4.object({
1452
- type: z4.literal("image"),
1453
- data: z4.string(),
1454
- mimeType: z4.string().optional()
1455
- })
1456
- ])
1457
- );
1458
-
1459
- // core/prompt/content-part.ts
1460
- var textPartSchema = z5.object({
1461
- type: z5.literal("text"),
1462
- text: z5.string(),
1463
- experimental_providerMetadata: providerMetadataSchema.optional()
1464
- });
1465
- var imagePartSchema = z5.object({
1466
- type: z5.literal("image"),
1467
- image: z5.union([dataContentSchema, z5.instanceof(URL)]),
1468
- mimeType: z5.string().optional(),
1469
- experimental_providerMetadata: providerMetadataSchema.optional()
1470
- });
1471
- var filePartSchema = z5.object({
1472
- type: z5.literal("file"),
1473
- data: z5.union([dataContentSchema, z5.instanceof(URL)]),
1474
- mimeType: z5.string(),
1475
- experimental_providerMetadata: providerMetadataSchema.optional()
1476
- });
1477
- var toolCallPartSchema = z5.object({
1478
- type: z5.literal("tool-call"),
1479
- toolCallId: z5.string(),
1480
- toolName: z5.string(),
1481
- args: z5.unknown()
1482
- });
1483
- var toolResultPartSchema = z5.object({
1484
- type: z5.literal("tool-result"),
1485
- toolCallId: z5.string(),
1486
- toolName: z5.string(),
1487
- result: z5.unknown(),
1488
- content: toolResultContentSchema.optional(),
1489
- isError: z5.boolean().optional(),
1490
- experimental_providerMetadata: providerMetadataSchema.optional()
1491
- });
1492
-
1493
- // core/prompt/message.ts
1494
- var coreSystemMessageSchema = z6.object({
1495
- role: z6.literal("system"),
1496
- content: z6.string(),
1497
- experimental_providerMetadata: providerMetadataSchema.optional()
1498
- });
1499
- var coreUserMessageSchema = z6.object({
1500
- role: z6.literal("user"),
1501
- content: z6.union([
1502
- z6.string(),
1503
- z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema]))
1504
- ]),
1505
- experimental_providerMetadata: providerMetadataSchema.optional()
1506
- });
1507
- var coreAssistantMessageSchema = z6.object({
1508
- role: z6.literal("assistant"),
1509
- content: z6.union([
1510
- z6.string(),
1511
- z6.array(z6.union([textPartSchema, toolCallPartSchema]))
1512
- ]),
1513
- experimental_providerMetadata: providerMetadataSchema.optional()
1514
- });
1515
- var coreToolMessageSchema = z6.object({
1516
- role: z6.literal("tool"),
1517
- content: z6.array(toolResultPartSchema),
1518
- experimental_providerMetadata: providerMetadataSchema.optional()
1519
- });
1520
- var coreMessageSchema = z6.union([
1521
- coreSystemMessageSchema,
1522
- coreUserMessageSchema,
1523
- coreAssistantMessageSchema,
1524
- coreToolMessageSchema
1525
- ]);
1526
-
1527
- // core/prompt/detect-prompt-type.ts
1528
- function detectPromptType(prompt) {
1529
- if (!Array.isArray(prompt)) {
1530
- return "other";
1531
- }
1532
- if (prompt.length === 0) {
1533
- return "messages";
1534
- }
1535
- const characteristics = prompt.map(detectSingleMessageCharacteristics);
1536
- if (characteristics.some((c) => c === "has-ui-specific-parts")) {
1537
- return "ui-messages";
1538
- } else if (characteristics.every(
1539
- (c) => c === "has-core-specific-parts" || c === "message"
1540
- )) {
1541
- return "messages";
1542
- } else {
1543
- return "other";
1544
- }
1545
- }
1546
- function detectSingleMessageCharacteristics(message) {
1547
- if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
1548
- message.role === "data" || // UI-only role
1549
- "toolInvocations" in message || // UI-specific field
1550
- "experimental_attachments" in message)) {
1551
- return "has-ui-specific-parts";
1552
- } else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
1553
- "experimental_providerMetadata" in message)) {
1554
- return "has-core-specific-parts";
1555
- } else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
1556
- return "message";
1557
- } else {
1558
- return "other";
1559
- }
1560
- }
1561
-
1562
1418
  // core/prompt/attachments-to-parts.ts
1563
1419
  function attachmentsToParts(attachments) {
1564
1420
  var _a15, _b, _c;
@@ -1743,6 +1599,150 @@ function convertToCoreMessages(messages, options) {
1743
1599
  return coreMessages;
1744
1600
  }
1745
1601
 
1602
+ // core/prompt/detect-prompt-type.ts
1603
+ function detectPromptType(prompt) {
1604
+ if (!Array.isArray(prompt)) {
1605
+ return "other";
1606
+ }
1607
+ if (prompt.length === 0) {
1608
+ return "messages";
1609
+ }
1610
+ const characteristics = prompt.map(detectSingleMessageCharacteristics);
1611
+ if (characteristics.some((c) => c === "has-ui-specific-parts")) {
1612
+ return "ui-messages";
1613
+ } else if (characteristics.every(
1614
+ (c) => c === "has-core-specific-parts" || c === "message"
1615
+ )) {
1616
+ return "messages";
1617
+ } else {
1618
+ return "other";
1619
+ }
1620
+ }
1621
+ function detectSingleMessageCharacteristics(message) {
1622
+ if (typeof message === "object" && message !== null && (message.role === "function" || // UI-only role
1623
+ message.role === "data" || // UI-only role
1624
+ "toolInvocations" in message || // UI-specific field
1625
+ "experimental_attachments" in message)) {
1626
+ return "has-ui-specific-parts";
1627
+ } else if (typeof message === "object" && message !== null && "content" in message && (Array.isArray(message.content) || // Core messages can have array content
1628
+ "experimental_providerMetadata" in message)) {
1629
+ return "has-core-specific-parts";
1630
+ } else if (typeof message === "object" && message !== null && "role" in message && "content" in message && typeof message.content === "string" && ["system", "user", "assistant", "tool"].includes(message.role)) {
1631
+ return "message";
1632
+ } else {
1633
+ return "other";
1634
+ }
1635
+ }
1636
+
1637
+ // core/prompt/message.ts
1638
+ import { z as z6 } from "zod";
1639
+
1640
+ // core/types/provider-metadata.ts
1641
+ import { z as z3 } from "zod";
1642
+
1643
+ // core/types/json-value.ts
1644
+ import { z as z2 } from "zod";
1645
+ var jsonValueSchema = z2.lazy(
1646
+ () => z2.union([
1647
+ z2.null(),
1648
+ z2.string(),
1649
+ z2.number(),
1650
+ z2.boolean(),
1651
+ z2.record(z2.string(), jsonValueSchema),
1652
+ z2.array(jsonValueSchema)
1653
+ ])
1654
+ );
1655
+
1656
+ // core/types/provider-metadata.ts
1657
+ var providerMetadataSchema = z3.record(
1658
+ z3.string(),
1659
+ z3.record(z3.string(), jsonValueSchema)
1660
+ );
1661
+
1662
+ // core/prompt/content-part.ts
1663
+ import { z as z5 } from "zod";
1664
+
1665
+ // core/prompt/tool-result-content.ts
1666
+ import { z as z4 } from "zod";
1667
+ var toolResultContentSchema = z4.array(
1668
+ z4.union([
1669
+ z4.object({ type: z4.literal("text"), text: z4.string() }),
1670
+ z4.object({
1671
+ type: z4.literal("image"),
1672
+ data: z4.string(),
1673
+ mimeType: z4.string().optional()
1674
+ })
1675
+ ])
1676
+ );
1677
+
1678
+ // core/prompt/content-part.ts
1679
+ var textPartSchema = z5.object({
1680
+ type: z5.literal("text"),
1681
+ text: z5.string(),
1682
+ experimental_providerMetadata: providerMetadataSchema.optional()
1683
+ });
1684
+ var imagePartSchema = z5.object({
1685
+ type: z5.literal("image"),
1686
+ image: z5.union([dataContentSchema, z5.instanceof(URL)]),
1687
+ mimeType: z5.string().optional(),
1688
+ experimental_providerMetadata: providerMetadataSchema.optional()
1689
+ });
1690
+ var filePartSchema = z5.object({
1691
+ type: z5.literal("file"),
1692
+ data: z5.union([dataContentSchema, z5.instanceof(URL)]),
1693
+ mimeType: z5.string(),
1694
+ experimental_providerMetadata: providerMetadataSchema.optional()
1695
+ });
1696
+ var toolCallPartSchema = z5.object({
1697
+ type: z5.literal("tool-call"),
1698
+ toolCallId: z5.string(),
1699
+ toolName: z5.string(),
1700
+ args: z5.unknown()
1701
+ });
1702
+ var toolResultPartSchema = z5.object({
1703
+ type: z5.literal("tool-result"),
1704
+ toolCallId: z5.string(),
1705
+ toolName: z5.string(),
1706
+ result: z5.unknown(),
1707
+ content: toolResultContentSchema.optional(),
1708
+ isError: z5.boolean().optional(),
1709
+ experimental_providerMetadata: providerMetadataSchema.optional()
1710
+ });
1711
+
1712
+ // core/prompt/message.ts
1713
+ var coreSystemMessageSchema = z6.object({
1714
+ role: z6.literal("system"),
1715
+ content: z6.string(),
1716
+ experimental_providerMetadata: providerMetadataSchema.optional()
1717
+ });
1718
+ var coreUserMessageSchema = z6.object({
1719
+ role: z6.literal("user"),
1720
+ content: z6.union([
1721
+ z6.string(),
1722
+ z6.array(z6.union([textPartSchema, imagePartSchema, filePartSchema]))
1723
+ ]),
1724
+ experimental_providerMetadata: providerMetadataSchema.optional()
1725
+ });
1726
+ var coreAssistantMessageSchema = z6.object({
1727
+ role: z6.literal("assistant"),
1728
+ content: z6.union([
1729
+ z6.string(),
1730
+ z6.array(z6.union([textPartSchema, toolCallPartSchema]))
1731
+ ]),
1732
+ experimental_providerMetadata: providerMetadataSchema.optional()
1733
+ });
1734
+ var coreToolMessageSchema = z6.object({
1735
+ role: z6.literal("tool"),
1736
+ content: z6.array(toolResultPartSchema),
1737
+ experimental_providerMetadata: providerMetadataSchema.optional()
1738
+ });
1739
+ var coreMessageSchema = z6.union([
1740
+ coreSystemMessageSchema,
1741
+ coreUserMessageSchema,
1742
+ coreAssistantMessageSchema,
1743
+ coreToolMessageSchema
1744
+ ]);
1745
+
1746
1746
  // core/prompt/standardize-prompt.ts
1747
1747
  function standardizePrompt({
1748
1748
  prompt,
@@ -5584,6 +5584,113 @@ var experimental_wrapLanguageModel = ({
5584
5584
  };
5585
5585
  };
5586
5586
 
5587
+ // core/util/get-potential-start-index.ts
5588
+ function getPotentialStartIndex(text2, searchedText) {
5589
+ if (searchedText.length === 0) {
5590
+ return null;
5591
+ }
5592
+ const directIndex = text2.indexOf(searchedText);
5593
+ if (directIndex !== -1) {
5594
+ return directIndex;
5595
+ }
5596
+ for (let i = text2.length - 1; i >= 0; i--) {
5597
+ const suffix = text2.substring(i);
5598
+ if (searchedText.startsWith(suffix)) {
5599
+ return i;
5600
+ }
5601
+ }
5602
+ return null;
5603
+ }
5604
+
5605
+ // core/middleware/extract-reasoning-middleware.ts
5606
+ function extractReasoningMiddleware({
5607
+ tagName,
5608
+ separator = "\n"
5609
+ }) {
5610
+ const openingTag = `<${tagName}>`;
5611
+ const closingTag = `</${tagName}>`;
5612
+ return {
5613
+ wrapGenerate: async ({ doGenerate }) => {
5614
+ const { text: text2, ...rest } = await doGenerate();
5615
+ if (text2 == null) {
5616
+ return { text: text2, ...rest };
5617
+ }
5618
+ const regexp = new RegExp(`${openingTag}(.*?)${closingTag}`, "gs");
5619
+ const matches = Array.from(text2.matchAll(regexp));
5620
+ if (!matches.length) {
5621
+ return { text: text2, ...rest };
5622
+ }
5623
+ const reasoning = matches.map((match) => match[1]).join(separator);
5624
+ let textWithoutReasoning = text2;
5625
+ for (let i = matches.length - 1; i >= 0; i--) {
5626
+ const match = matches[i];
5627
+ const beforeMatch = textWithoutReasoning.slice(0, match.index);
5628
+ const afterMatch = textWithoutReasoning.slice(
5629
+ match.index + match[0].length
5630
+ );
5631
+ textWithoutReasoning = beforeMatch + (beforeMatch.length > 0 && afterMatch.length > 0 ? separator : "") + afterMatch;
5632
+ }
5633
+ return { text: textWithoutReasoning, reasoning, ...rest };
5634
+ },
5635
+ wrapStream: async ({ doStream }) => {
5636
+ const { stream, ...rest } = await doStream();
5637
+ let isFirstReasoning = true;
5638
+ let isFirstText = true;
5639
+ let afterSwitch = false;
5640
+ let isReasoning = false;
5641
+ let buffer = "";
5642
+ return {
5643
+ stream: stream.pipeThrough(
5644
+ new TransformStream({
5645
+ transform: (chunk, controller) => {
5646
+ if (chunk.type !== "text-delta") {
5647
+ controller.enqueue(chunk);
5648
+ return;
5649
+ }
5650
+ buffer += chunk.textDelta;
5651
+ function publish(text2) {
5652
+ if (text2.length > 0) {
5653
+ const prefix = afterSwitch && (isReasoning ? !isFirstReasoning : !isFirstText) ? separator : "";
5654
+ controller.enqueue({
5655
+ type: isReasoning ? "reasoning" : "text-delta",
5656
+ textDelta: prefix + text2
5657
+ });
5658
+ afterSwitch = false;
5659
+ if (isReasoning) {
5660
+ isFirstReasoning = false;
5661
+ } else {
5662
+ isFirstText = false;
5663
+ }
5664
+ }
5665
+ }
5666
+ do {
5667
+ const nextTag = isReasoning ? closingTag : openingTag;
5668
+ const startIndex = getPotentialStartIndex(buffer, nextTag);
5669
+ if (startIndex == null) {
5670
+ publish(buffer);
5671
+ buffer = "";
5672
+ break;
5673
+ }
5674
+ publish(buffer.slice(0, startIndex));
5675
+ const foundFullMatch = startIndex + nextTag.length <= buffer.length;
5676
+ if (foundFullMatch) {
5677
+ buffer = buffer.slice(startIndex + nextTag.length);
5678
+ isReasoning = !isReasoning;
5679
+ afterSwitch = true;
5680
+ } else {
5681
+ buffer = buffer.slice(startIndex);
5682
+ break;
5683
+ }
5684
+ } while (true);
5685
+ }
5686
+ })
5687
+ ),
5688
+ ...rest
5689
+ };
5690
+ }
5691
+ };
5692
+ }
5693
+
5587
5694
  // core/prompt/append-client-message.ts
5588
5695
  function appendClientMessage({
5589
5696
  messages,
@@ -6219,6 +6326,7 @@ export {
6219
6326
  experimental_customProvider,
6220
6327
  generateImage as experimental_generateImage,
6221
6328
  experimental_wrapLanguageModel,
6329
+ extractReasoningMiddleware,
6222
6330
  formatAssistantStreamPart,
6223
6331
  formatDataStreamPart3 as formatDataStreamPart,
6224
6332
  generateId2 as generateId,