ai 4.1.18 → 4.1.20

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
@@ -1522,13 +1522,13 @@ _a8 = symbol8;
1522
1522
 
1523
1523
  // core/prompt/convert-to-core-messages.ts
1524
1524
  function convertToCoreMessages(messages, options) {
1525
- var _a15;
1525
+ var _a15, _b;
1526
1526
  const tools = (_a15 = options == null ? void 0 : options.tools) != null ? _a15 : {};
1527
1527
  const coreMessages = [];
1528
1528
  for (let i = 0; i < messages.length; i++) {
1529
1529
  const message = messages[i];
1530
1530
  const isLastMessage = i === messages.length - 1;
1531
- const { role, content, toolInvocations, experimental_attachments } = message;
1531
+ const { role, content, experimental_attachments } = message;
1532
1532
  switch (role) {
1533
1533
  case "system": {
1534
1534
  coreMessages.push({
@@ -1548,6 +1548,92 @@ function convertToCoreMessages(messages, options) {
1548
1548
  break;
1549
1549
  }
1550
1550
  case "assistant": {
1551
+ if (message.parts != null) {
1552
+ let processBlock2 = function() {
1553
+ coreMessages.push({
1554
+ role: "assistant",
1555
+ content: block.map((part) => {
1556
+ switch (part.type) {
1557
+ case "text":
1558
+ return {
1559
+ type: "text",
1560
+ text: part.text
1561
+ };
1562
+ default:
1563
+ return {
1564
+ type: "tool-call",
1565
+ toolCallId: part.toolInvocation.toolCallId,
1566
+ toolName: part.toolInvocation.toolName,
1567
+ args: part.toolInvocation.args
1568
+ };
1569
+ }
1570
+ })
1571
+ });
1572
+ const stepInvocations = block.filter(
1573
+ (part) => part.type === "tool-invocation"
1574
+ ).map((part) => part.toolInvocation);
1575
+ if (stepInvocations.length > 0) {
1576
+ coreMessages.push({
1577
+ role: "tool",
1578
+ content: stepInvocations.map(
1579
+ (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",
1590
+ toolCallId,
1591
+ toolName,
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
+ }
1604
+ block = [];
1605
+ blockHasToolInvocations = false;
1606
+ currentStep++;
1607
+ };
1608
+ var processBlock = processBlock2;
1609
+ let currentStep = 0;
1610
+ let blockHasToolInvocations = false;
1611
+ let block = [];
1612
+ for (const part of message.parts) {
1613
+ switch (part.type) {
1614
+ case "reasoning":
1615
+ break;
1616
+ case "text": {
1617
+ if (blockHasToolInvocations) {
1618
+ processBlock2();
1619
+ }
1620
+ block.push(part);
1621
+ break;
1622
+ }
1623
+ case "tool-invocation": {
1624
+ if (((_b = part.toolInvocation.step) != null ? _b : 0) !== currentStep) {
1625
+ processBlock2();
1626
+ }
1627
+ block.push(part);
1628
+ blockHasToolInvocations = true;
1629
+ break;
1630
+ }
1631
+ }
1632
+ }
1633
+ processBlock2();
1634
+ break;
1635
+ }
1636
+ const toolInvocations = message.toolInvocations;
1551
1637
  if (toolInvocations == null || toolInvocations.length === 0) {
1552
1638
  coreMessages.push({ role: "assistant", content });
1553
1639
  break;
@@ -5769,16 +5855,17 @@ import {
5769
5855
  } from "@ai-sdk/ui-utils";
5770
5856
  function appendResponseMessages({
5771
5857
  messages,
5772
- responseMessages
5858
+ responseMessages,
5859
+ _internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
5773
5860
  }) {
5774
- var _a15, _b;
5861
+ var _a15, _b, _c, _d;
5775
5862
  const clonedMessages = structuredClone(messages);
5776
5863
  for (const message of responseMessages) {
5777
5864
  const role = message.role;
5778
5865
  const lastMessage = clonedMessages[clonedMessages.length - 1];
5779
5866
  const isLastMessageAssistant = lastMessage.role === "assistant";
5780
5867
  switch (role) {
5781
- case "assistant": {
5868
+ case "assistant":
5782
5869
  let getToolInvocations2 = function(step) {
5783
5870
  return (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
5784
5871
  state: "call",
@@ -5794,40 +5881,71 @@ function appendResponseMessages({
5794
5881
  const maxStep = extractMaxToolInvocationStep(
5795
5882
  lastMessage.toolInvocations
5796
5883
  );
5884
+ (_a15 = lastMessage.parts) != null ? _a15 : lastMessage.parts = [];
5797
5885
  lastMessage.content = textContent;
5886
+ if (textContent.length > 0) {
5887
+ lastMessage.parts.push({
5888
+ type: "text",
5889
+ text: textContent
5890
+ });
5891
+ }
5798
5892
  lastMessage.toolInvocations = [
5799
- ...(_a15 = lastMessage.toolInvocations) != null ? _a15 : [],
5893
+ ...(_b = lastMessage.toolInvocations) != null ? _b : [],
5800
5894
  ...getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1)
5801
5895
  ];
5896
+ getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
5897
+ type: "tool-invocation",
5898
+ toolInvocation: call
5899
+ })).forEach((part) => {
5900
+ lastMessage.parts.push(part);
5901
+ });
5802
5902
  } else {
5803
5903
  clonedMessages.push({
5804
5904
  role: "assistant",
5805
5905
  id: message.id,
5806
- createdAt: /* @__PURE__ */ new Date(),
5906
+ createdAt: currentDate(),
5807
5907
  // generate a createdAt date for the message, will be overridden by the client
5808
5908
  content: textContent,
5809
- toolInvocations: getToolInvocations2(0)
5909
+ toolInvocations: getToolInvocations2(0),
5910
+ parts: [
5911
+ ...textContent.length > 0 ? [{ type: "text", text: textContent }] : [],
5912
+ ...getToolInvocations2(0).map((call) => ({
5913
+ type: "tool-invocation",
5914
+ toolInvocation: call
5915
+ }))
5916
+ ]
5810
5917
  });
5811
5918
  }
5812
5919
  break;
5813
- }
5814
5920
  case "tool": {
5815
- (_b = lastMessage.toolInvocations) != null ? _b : lastMessage.toolInvocations = [];
5921
+ (_c = lastMessage.toolInvocations) != null ? _c : lastMessage.toolInvocations = [];
5816
5922
  if (lastMessage.role !== "assistant") {
5817
5923
  throw new Error(
5818
5924
  `Tool result must follow an assistant message: ${lastMessage.role}`
5819
5925
  );
5820
5926
  }
5821
- for (const part of message.content) {
5927
+ (_d = lastMessage.parts) != null ? _d : lastMessage.parts = [];
5928
+ for (const contentPart of message.content) {
5822
5929
  const toolCall = lastMessage.toolInvocations.find(
5823
- (call) => call.toolCallId === part.toolCallId
5930
+ (call) => call.toolCallId === contentPart.toolCallId
5931
+ );
5932
+ const toolCallPart = lastMessage.parts.find(
5933
+ (part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === contentPart.toolCallId
5824
5934
  );
5825
5935
  if (!toolCall) {
5826
5936
  throw new Error("Tool call not found in previous message");
5827
5937
  }
5828
5938
  toolCall.state = "result";
5829
5939
  const toolResult = toolCall;
5830
- toolResult.result = part.result;
5940
+ toolResult.result = contentPart.result;
5941
+ if (toolCallPart) {
5942
+ toolCallPart.toolInvocation = toolResult;
5943
+ } else {
5944
+ lastMessage.parts.push({
5945
+ type: "tool-invocation",
5946
+ toolInvocation: toolResult
5947
+ });
5948
+ }
5831
5949
  }
5832
5950
  break;
5833
5951
  }