ai 4.1.11 → 4.1.12

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