ai 4.1.19 → 4.1.21

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