ai 3.1.6 → 3.1.7

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
@@ -1491,8 +1491,65 @@ var StreamTextResult = class {
1491
1491
 
1492
1492
  @returns an `AIStream` object.
1493
1493
  */
1494
- toAIStream(callbacks) {
1495
- return this.textStream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
1494
+ toAIStream(callbacks = {}) {
1495
+ let aggregatedResponse = "";
1496
+ const callbackTransformer = new TransformStream({
1497
+ async start() {
1498
+ if (callbacks.onStart)
1499
+ await callbacks.onStart();
1500
+ },
1501
+ async transform(chunk, controller) {
1502
+ controller.enqueue(chunk);
1503
+ if (chunk.type === "text-delta") {
1504
+ const textDelta = chunk.textDelta;
1505
+ aggregatedResponse += textDelta;
1506
+ if (callbacks.onToken)
1507
+ await callbacks.onToken(textDelta);
1508
+ if (callbacks.onText)
1509
+ await callbacks.onText(textDelta);
1510
+ }
1511
+ },
1512
+ async flush() {
1513
+ if (callbacks.onCompletion)
1514
+ await callbacks.onCompletion(aggregatedResponse);
1515
+ if (callbacks.onFinal)
1516
+ await callbacks.onFinal(aggregatedResponse);
1517
+ }
1518
+ });
1519
+ const streamDataTransformer = new TransformStream({
1520
+ transform: async (chunk, controller) => {
1521
+ switch (chunk.type) {
1522
+ case "text-delta":
1523
+ controller.enqueue(formatStreamPart("text", chunk.textDelta));
1524
+ break;
1525
+ case "tool-call":
1526
+ controller.enqueue(
1527
+ formatStreamPart("tool_call", {
1528
+ toolCallId: chunk.toolCallId,
1529
+ toolName: chunk.toolName,
1530
+ args: chunk.args
1531
+ })
1532
+ );
1533
+ break;
1534
+ case "tool-result":
1535
+ controller.enqueue(
1536
+ formatStreamPart("tool_result", {
1537
+ toolCallId: chunk.toolCallId,
1538
+ toolName: chunk.toolName,
1539
+ args: chunk.args,
1540
+ result: chunk.result
1541
+ })
1542
+ );
1543
+ break;
1544
+ case "error":
1545
+ controller.enqueue(
1546
+ formatStreamPart("error", JSON.stringify(chunk.error))
1547
+ );
1548
+ break;
1549
+ }
1550
+ }
1551
+ });
1552
+ return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamDataTransformer).pipeThrough(new TextEncoderStream());
1496
1553
  }
1497
1554
  /**
1498
1555
  Writes stream data output to a Node.js response-like object.
@@ -1598,6 +1655,55 @@ var StreamTextResult = class {
1598
1655
  };
1599
1656
  var experimental_streamText = streamText;
1600
1657
 
1658
+ // core/prompt/convert-to-core-messages.ts
1659
+ function convertToCoreMessages(messages) {
1660
+ const coreMessages = [];
1661
+ for (const { role, content, toolInvocations } of messages) {
1662
+ switch (role) {
1663
+ case "user": {
1664
+ coreMessages.push({ role: "user", content });
1665
+ break;
1666
+ }
1667
+ case "assistant": {
1668
+ if (toolInvocations == null) {
1669
+ coreMessages.push({ role: "assistant", content });
1670
+ break;
1671
+ }
1672
+ coreMessages.push({
1673
+ role: "assistant",
1674
+ content: [
1675
+ { type: "text", text: content },
1676
+ ...toolInvocations.map(({ toolCallId, toolName, args }) => ({
1677
+ type: "tool-call",
1678
+ toolCallId,
1679
+ toolName,
1680
+ args
1681
+ }))
1682
+ ]
1683
+ });
1684
+ coreMessages.push({
1685
+ role: "tool",
1686
+ content: toolInvocations.map(
1687
+ ({ toolCallId, toolName, args, result }) => ({
1688
+ type: "tool-result",
1689
+ toolCallId,
1690
+ toolName,
1691
+ args,
1692
+ result
1693
+ })
1694
+ )
1695
+ });
1696
+ break;
1697
+ }
1698
+ default: {
1699
+ const _exhaustiveCheck = role;
1700
+ throw new Error(`Unhandled role: ${_exhaustiveCheck}`);
1701
+ }
1702
+ }
1703
+ }
1704
+ return coreMessages;
1705
+ }
1706
+
1601
1707
  // core/tool/tool.ts
1602
1708
  function tool(tool2) {
1603
1709
  return tool2;
@@ -1719,7 +1825,7 @@ var dataMessageStreamPart = {
1719
1825
  };
1720
1826
  }
1721
1827
  };
1722
- var toolCallStreamPart = {
1828
+ var toolCallsStreamPart = {
1723
1829
  code: "7",
1724
1830
  name: "tool_calls",
1725
1831
  parse: (value) => {
@@ -1746,6 +1852,36 @@ var messageAnnotationsStreamPart = {
1746
1852
  return { type: "message_annotations", value };
1747
1853
  }
1748
1854
  };
1855
+ var toolCallStreamPart = {
1856
+ code: "9",
1857
+ name: "tool_call",
1858
+ parse: (value) => {
1859
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
1860
+ throw new Error(
1861
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
1862
+ );
1863
+ }
1864
+ return {
1865
+ type: "tool_call",
1866
+ value
1867
+ };
1868
+ }
1869
+ };
1870
+ var toolResultStreamPart = {
1871
+ code: "a",
1872
+ name: "tool_result",
1873
+ parse: (value) => {
1874
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
1875
+ throw new Error(
1876
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
1877
+ );
1878
+ }
1879
+ return {
1880
+ type: "tool_result",
1881
+ value
1882
+ };
1883
+ }
1884
+ };
1749
1885
  var streamParts = [
1750
1886
  textStreamPart,
1751
1887
  functionCallStreamPart,
@@ -1754,8 +1890,10 @@ var streamParts = [
1754
1890
  assistantMessageStreamPart,
1755
1891
  assistantControlDataStreamPart,
1756
1892
  dataMessageStreamPart,
1893
+ toolCallsStreamPart,
1894
+ messageAnnotationsStreamPart,
1757
1895
  toolCallStreamPart,
1758
- messageAnnotationsStreamPart
1896
+ toolResultStreamPart
1759
1897
  ];
1760
1898
  var streamPartsByCode = {
1761
1899
  [textStreamPart.code]: textStreamPart,
@@ -1765,8 +1903,10 @@ var streamPartsByCode = {
1765
1903
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
1766
1904
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
1767
1905
  [dataMessageStreamPart.code]: dataMessageStreamPart,
1906
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
1907
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
1768
1908
  [toolCallStreamPart.code]: toolCallStreamPart,
1769
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
1909
+ [toolResultStreamPart.code]: toolResultStreamPart
1770
1910
  };
1771
1911
  var StreamStringPrefixes = {
1772
1912
  [textStreamPart.name]: textStreamPart.code,
@@ -1776,8 +1916,10 @@ var StreamStringPrefixes = {
1776
1916
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
1777
1917
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
1778
1918
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
1919
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
1920
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
1779
1921
  [toolCallStreamPart.name]: toolCallStreamPart.code,
1780
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
1922
+ [toolResultStreamPart.name]: toolResultStreamPart.code
1781
1923
  };
1782
1924
  var validCodes = streamParts.map((part) => part.code);
1783
1925
  var parseStreamPart = (line) => {
@@ -2882,6 +3024,40 @@ async function parseComplexResponse({
2882
3024
  };
2883
3025
  }
2884
3026
  }
3027
+ if (type === "tool_call") {
3028
+ if (prefixMap.text == null) {
3029
+ prefixMap.text = {
3030
+ id: generateId2(),
3031
+ role: "assistant",
3032
+ content: "",
3033
+ createdAt
3034
+ };
3035
+ }
3036
+ if (prefixMap.text.toolInvocations == null) {
3037
+ prefixMap.text.toolInvocations = [];
3038
+ }
3039
+ prefixMap.text.toolInvocations.push(value);
3040
+ } else if (type === "tool_result") {
3041
+ if (prefixMap.text == null) {
3042
+ prefixMap.text = {
3043
+ id: generateId2(),
3044
+ role: "assistant",
3045
+ content: "",
3046
+ createdAt
3047
+ };
3048
+ }
3049
+ if (prefixMap.text.toolInvocations == null) {
3050
+ prefixMap.text.toolInvocations = [];
3051
+ }
3052
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
3053
+ (invocation) => invocation.toolCallId === value.toolCallId
3054
+ );
3055
+ if (toolInvocationIndex !== -1) {
3056
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
3057
+ } else {
3058
+ prefixMap.text.toolInvocations.push(value);
3059
+ }
3060
+ }
2885
3061
  let functionCallMessage = null;
2886
3062
  if (type === "function_call") {
2887
3063
  prefixMap["function_call"] = {
@@ -3075,6 +3251,7 @@ export {
3075
3251
  UnsupportedJSONSchemaError,
3076
3252
  convertDataContentToBase64String,
3077
3253
  convertDataContentToUint8Array,
3254
+ convertToCoreMessages,
3078
3255
  createCallbacksTransformer,
3079
3256
  createChunkDecoder,
3080
3257
  createEventStreamTransformer,