ai 3.1.5 → 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
@@ -53,7 +53,15 @@ function convertDataContentToUint8Array(content) {
53
53
  return content;
54
54
  }
55
55
  if (typeof content === "string") {
56
- return convertBase64ToUint8Array(content);
56
+ try {
57
+ return convertBase64ToUint8Array(content);
58
+ } catch (error) {
59
+ throw new InvalidDataContentError({
60
+ message: "Invalid data content. Content string is not a base64-encoded image.",
61
+ content,
62
+ cause: error
63
+ });
64
+ }
57
65
  }
58
66
  if (content instanceof ArrayBuffer) {
59
67
  return new Uint8Array(content);
@@ -1483,8 +1491,65 @@ var StreamTextResult = class {
1483
1491
 
1484
1492
  @returns an `AIStream` object.
1485
1493
  */
1486
- toAIStream(callbacks) {
1487
- 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());
1488
1553
  }
1489
1554
  /**
1490
1555
  Writes stream data output to a Node.js response-like object.
@@ -1590,6 +1655,55 @@ var StreamTextResult = class {
1590
1655
  };
1591
1656
  var experimental_streamText = streamText;
1592
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
+
1593
1707
  // core/tool/tool.ts
1594
1708
  function tool(tool2) {
1595
1709
  return tool2;
@@ -1711,7 +1825,7 @@ var dataMessageStreamPart = {
1711
1825
  };
1712
1826
  }
1713
1827
  };
1714
- var toolCallStreamPart = {
1828
+ var toolCallsStreamPart = {
1715
1829
  code: "7",
1716
1830
  name: "tool_calls",
1717
1831
  parse: (value) => {
@@ -1738,6 +1852,36 @@ var messageAnnotationsStreamPart = {
1738
1852
  return { type: "message_annotations", value };
1739
1853
  }
1740
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
+ };
1741
1885
  var streamParts = [
1742
1886
  textStreamPart,
1743
1887
  functionCallStreamPart,
@@ -1746,8 +1890,10 @@ var streamParts = [
1746
1890
  assistantMessageStreamPart,
1747
1891
  assistantControlDataStreamPart,
1748
1892
  dataMessageStreamPart,
1893
+ toolCallsStreamPart,
1894
+ messageAnnotationsStreamPart,
1749
1895
  toolCallStreamPart,
1750
- messageAnnotationsStreamPart
1896
+ toolResultStreamPart
1751
1897
  ];
1752
1898
  var streamPartsByCode = {
1753
1899
  [textStreamPart.code]: textStreamPart,
@@ -1757,8 +1903,10 @@ var streamPartsByCode = {
1757
1903
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
1758
1904
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
1759
1905
  [dataMessageStreamPart.code]: dataMessageStreamPart,
1906
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
1907
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
1760
1908
  [toolCallStreamPart.code]: toolCallStreamPart,
1761
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
1909
+ [toolResultStreamPart.code]: toolResultStreamPart
1762
1910
  };
1763
1911
  var StreamStringPrefixes = {
1764
1912
  [textStreamPart.name]: textStreamPart.code,
@@ -1768,8 +1916,10 @@ var StreamStringPrefixes = {
1768
1916
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
1769
1917
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
1770
1918
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
1919
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
1920
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
1771
1921
  [toolCallStreamPart.name]: toolCallStreamPart.code,
1772
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
1922
+ [toolResultStreamPart.name]: toolResultStreamPart.code
1773
1923
  };
1774
1924
  var validCodes = streamParts.map((part) => part.code);
1775
1925
  var parseStreamPart = (line) => {
@@ -2874,6 +3024,40 @@ async function parseComplexResponse({
2874
3024
  };
2875
3025
  }
2876
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
+ }
2877
3061
  let functionCallMessage = null;
2878
3062
  if (type === "function_call") {
2879
3063
  prefixMap["function_call"] = {
@@ -3067,6 +3251,7 @@ export {
3067
3251
  UnsupportedJSONSchemaError,
3068
3252
  convertDataContentToBase64String,
3069
3253
  convertDataContentToUint8Array,
3254
+ convertToCoreMessages,
3070
3255
  createCallbacksTransformer,
3071
3256
  createChunkDecoder,
3072
3257
  createEventStreamTransformer,