ai 4.1.11 → 4.1.13

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
@@ -38,6 +38,9 @@ function createDataStream({
38
38
  }
39
39
  try {
40
40
  const result = execute({
41
+ write(data) {
42
+ safeEnqueue(data);
43
+ },
41
44
  writeData(data) {
42
45
  safeEnqueue(formatDataStreamPart("data", [data]));
43
46
  },
@@ -1547,45 +1550,62 @@ function convertToCoreMessages(messages, options) {
1547
1550
  coreMessages.push({ role: "assistant", content });
1548
1551
  break;
1549
1552
  }
1550
- coreMessages.push({
1551
- role: "assistant",
1552
- content: [
1553
- { type: "text", text: content },
1554
- ...toolInvocations.map(
1555
- ({ toolCallId, toolName, args }) => ({
1556
- type: "tool-call",
1553
+ const maxStep = toolInvocations.reduce((max, toolInvocation) => {
1554
+ var _a16;
1555
+ return Math.max(max, (_a16 = toolInvocation.step) != null ? _a16 : 0);
1556
+ }, 0);
1557
+ for (let i = 0; i <= maxStep; i++) {
1558
+ const stepInvocations = toolInvocations.filter(
1559
+ (toolInvocation) => {
1560
+ var _a16;
1561
+ return ((_a16 = toolInvocation.step) != null ? _a16 : 0) === i;
1562
+ }
1563
+ );
1564
+ if (stepInvocations.length === 0) {
1565
+ continue;
1566
+ }
1567
+ coreMessages.push({
1568
+ role: "assistant",
1569
+ content: [
1570
+ ...stepInvocations.map(
1571
+ ({ toolCallId, toolName, args }) => ({
1572
+ type: "tool-call",
1573
+ toolCallId,
1574
+ toolName,
1575
+ args
1576
+ })
1577
+ )
1578
+ ]
1579
+ });
1580
+ coreMessages.push({
1581
+ role: "tool",
1582
+ content: stepInvocations.map((toolInvocation) => {
1583
+ if (!("result" in toolInvocation)) {
1584
+ throw new MessageConversionError({
1585
+ originalMessage: message,
1586
+ message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
1587
+ });
1588
+ }
1589
+ const { toolCallId, toolName, result } = toolInvocation;
1590
+ const tool2 = tools[toolName];
1591
+ return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
1592
+ type: "tool-result",
1557
1593
  toolCallId,
1558
1594
  toolName,
1559
- args
1560
- })
1561
- )
1562
- ]
1563
- });
1564
- coreMessages.push({
1565
- role: "tool",
1566
- content: toolInvocations.map((toolInvocation) => {
1567
- if (!("result" in toolInvocation)) {
1568
- throw new MessageConversionError({
1569
- originalMessage: message,
1570
- message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
1571
- });
1572
- }
1573
- const { toolCallId, toolName, result } = toolInvocation;
1574
- const tool2 = tools[toolName];
1575
- return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
1576
- type: "tool-result",
1577
- toolCallId,
1578
- toolName,
1579
- result: tool2.experimental_toToolResultContent(result),
1580
- experimental_content: tool2.experimental_toToolResultContent(result)
1581
- } : {
1582
- type: "tool-result",
1583
- toolCallId,
1584
- toolName,
1585
- result
1586
- };
1587
- })
1588
- });
1595
+ result: tool2.experimental_toToolResultContent(result),
1596
+ experimental_content: tool2.experimental_toToolResultContent(result)
1597
+ } : {
1598
+ type: "tool-result",
1599
+ toolCallId,
1600
+ toolName,
1601
+ result
1602
+ };
1603
+ })
1604
+ });
1605
+ }
1606
+ if (content) {
1607
+ coreMessages.push({ role: "assistant", content });
1608
+ }
1589
1609
  break;
1590
1610
  }
1591
1611
  case "data": {
@@ -5723,41 +5743,62 @@ function appendClientMessage({
5723
5743
  }
5724
5744
 
5725
5745
  // core/prompt/append-response-messages.ts
5746
+ import {
5747
+ extractMaxToolInvocationStep
5748
+ } from "@ai-sdk/ui-utils";
5726
5749
  function appendResponseMessages({
5727
5750
  messages,
5728
5751
  responseMessages
5729
5752
  }) {
5730
- var _a15;
5753
+ var _a15, _b;
5731
5754
  const clonedMessages = structuredClone(messages);
5732
5755
  for (const message of responseMessages) {
5733
5756
  const role = message.role;
5757
+ const lastMessage = clonedMessages[clonedMessages.length - 1];
5758
+ const isLastMessageAssistant = lastMessage.role === "assistant";
5734
5759
  switch (role) {
5735
5760
  case "assistant": {
5736
- clonedMessages.push({
5737
- role: "assistant",
5738
- id: message.id,
5739
- createdAt: /* @__PURE__ */ new Date(),
5740
- // generate a createdAt date for the message, will be overridden by the client
5741
- // only include text in the content:
5742
- content: typeof message.content === "string" ? message.content : message.content.filter((part) => part.type === "text").map((part) => part.text).join(""),
5743
- // separate tool calls from the content:
5744
- toolInvocations: (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
5761
+ let getToolInvocations2 = function(step) {
5762
+ return (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
5745
5763
  state: "call",
5746
- ...call
5747
- }))
5748
- });
5764
+ step,
5765
+ args: call.args,
5766
+ toolCallId: call.toolCallId,
5767
+ toolName: call.toolName
5768
+ }));
5769
+ };
5770
+ var getToolInvocations = getToolInvocations2;
5771
+ const textContent = typeof message.content === "string" ? message.content : message.content.filter((part) => part.type === "text").map((part) => part.text).join("");
5772
+ if (isLastMessageAssistant) {
5773
+ const maxStep = extractMaxToolInvocationStep(
5774
+ lastMessage.toolInvocations
5775
+ );
5776
+ lastMessage.content = textContent;
5777
+ lastMessage.toolInvocations = [
5778
+ ...(_a15 = lastMessage.toolInvocations) != null ? _a15 : [],
5779
+ ...getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1)
5780
+ ];
5781
+ } else {
5782
+ clonedMessages.push({
5783
+ role: "assistant",
5784
+ id: message.id,
5785
+ createdAt: /* @__PURE__ */ new Date(),
5786
+ // generate a createdAt date for the message, will be overridden by the client
5787
+ content: textContent,
5788
+ toolInvocations: getToolInvocations2(0)
5789
+ });
5790
+ }
5749
5791
  break;
5750
5792
  }
5751
5793
  case "tool": {
5752
- const previousMessage = clonedMessages[clonedMessages.length - 1];
5753
- (_a15 = previousMessage.toolInvocations) != null ? _a15 : previousMessage.toolInvocations = [];
5754
- if (previousMessage.role !== "assistant") {
5794
+ (_b = lastMessage.toolInvocations) != null ? _b : lastMessage.toolInvocations = [];
5795
+ if (lastMessage.role !== "assistant") {
5755
5796
  throw new Error(
5756
- `Tool result must follow an assistant message: ${previousMessage.role}`
5797
+ `Tool result must follow an assistant message: ${lastMessage.role}`
5757
5798
  );
5758
5799
  }
5759
5800
  for (const part of message.content) {
5760
- const toolCall = previousMessage.toolInvocations.find(
5801
+ const toolCall = lastMessage.toolInvocations.find(
5761
5802
  (call) => call.toolCallId === part.toolCallId
5762
5803
  );
5763
5804
  if (!toolCall) {