ai 3.4.24 → 3.4.26

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # ai
2
2
 
3
+ ## 3.4.26
4
+
5
+ ### Patch Changes
6
+
7
+ - 57f39ea: feat (ai/core): support multi-modal tool results in convertToCoreMessages
8
+
9
+ ## 3.4.25
10
+
11
+ ### Patch Changes
12
+
13
+ - 6e0fa1c: fix (ai/core): wait for tool results to arrive before sending finish event
14
+
3
15
  ## 3.4.24
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1331,7 +1331,9 @@ type ConvertibleMessage = {
1331
1331
  Converts an array of messages from useChat into an array of CoreMessages that can be used
1332
1332
  with the AI core functions (e.g. `streamText`).
1333
1333
  */
1334
- declare function convertToCoreMessages(messages: Array<ConvertibleMessage>): CoreMessage[];
1334
+ declare function convertToCoreMessages<TOOLS extends Record<string, CoreTool> = never>(messages: Array<ConvertibleMessage>, options?: {
1335
+ tools: TOOLS;
1336
+ }): CoreMessage[];
1335
1337
 
1336
1338
  /**
1337
1339
  Create a union of the given object's values, and optionally specify which keys to get the values from.
package/dist/index.d.ts CHANGED
@@ -1331,7 +1331,9 @@ type ConvertibleMessage = {
1331
1331
  Converts an array of messages from useChat into an array of CoreMessages that can be used
1332
1332
  with the AI core functions (e.g. `streamText`).
1333
1333
  */
1334
- declare function convertToCoreMessages(messages: Array<ConvertibleMessage>): CoreMessage[];
1334
+ declare function convertToCoreMessages<TOOLS extends Record<string, CoreTool> = never>(messages: Array<ConvertibleMessage>, options?: {
1335
+ tools: TOOLS;
1336
+ }): CoreMessage[];
1335
1337
 
1336
1338
  /**
1337
1339
  Create a union of the given object's values, and optionally specify which keys to get the values from.
package/dist/index.js CHANGED
@@ -3655,8 +3655,6 @@ function runToolsTransformation({
3655
3655
  telemetry,
3656
3656
  abortSignal
3657
3657
  }) {
3658
- let canClose = false;
3659
- const outstandingToolCalls = /* @__PURE__ */ new Set();
3660
3658
  let toolResultsStreamController = null;
3661
3659
  const toolResultsStream = new ReadableStream({
3662
3660
  start(controller) {
@@ -3664,6 +3662,17 @@ function runToolsTransformation({
3664
3662
  }
3665
3663
  });
3666
3664
  const activeToolCalls = {};
3665
+ const outstandingToolResults = /* @__PURE__ */ new Set();
3666
+ let canClose = false;
3667
+ let finishChunk = void 0;
3668
+ function attemptClose() {
3669
+ if (canClose && outstandingToolResults.size === 0) {
3670
+ if (finishChunk != null) {
3671
+ toolResultsStreamController.enqueue(finishChunk);
3672
+ }
3673
+ toolResultsStreamController.close();
3674
+ }
3675
+ }
3667
3676
  const forwardStream = new TransformStream({
3668
3677
  transform(chunk, controller) {
3669
3678
  const chunkType = chunk.type;
@@ -3721,7 +3730,7 @@ function runToolsTransformation({
3721
3730
  controller.enqueue(toolCall);
3722
3731
  if (tool2.execute != null) {
3723
3732
  const toolExecutionId = (0, import_ui_utils5.generateId)();
3724
- outstandingToolCalls.add(toolExecutionId);
3733
+ outstandingToolResults.add(toolExecutionId);
3725
3734
  recordSpan({
3726
3735
  name: "ai.toolCall",
3727
3736
  attributes: selectTelemetryAttributes({
@@ -3746,10 +3755,8 @@ function runToolsTransformation({
3746
3755
  type: "tool-result",
3747
3756
  result
3748
3757
  });
3749
- outstandingToolCalls.delete(toolExecutionId);
3750
- if (canClose && outstandingToolCalls.size === 0) {
3751
- toolResultsStreamController.close();
3752
- }
3758
+ outstandingToolResults.delete(toolExecutionId);
3759
+ attemptClose();
3753
3760
  try {
3754
3761
  span.setAttributes(
3755
3762
  selectTelemetryAttributes({
@@ -3769,10 +3776,8 @@ function runToolsTransformation({
3769
3776
  type: "error",
3770
3777
  error
3771
3778
  });
3772
- outstandingToolCalls.delete(toolExecutionId);
3773
- if (canClose && outstandingToolCalls.size === 0) {
3774
- toolResultsStreamController.close();
3775
- }
3779
+ outstandingToolResults.delete(toolExecutionId);
3780
+ attemptClose();
3776
3781
  }
3777
3782
  )
3778
3783
  });
@@ -3786,13 +3791,13 @@ function runToolsTransformation({
3786
3791
  break;
3787
3792
  }
3788
3793
  case "finish": {
3789
- controller.enqueue({
3794
+ finishChunk = {
3790
3795
  type: "finish",
3791
3796
  finishReason: chunk.finishReason,
3792
3797
  logprobs: chunk.logprobs,
3793
3798
  usage: calculateLanguageModelUsage(chunk.usage),
3794
3799
  experimental_providerMetadata: chunk.providerMetadata
3795
- });
3800
+ };
3796
3801
  break;
3797
3802
  }
3798
3803
  default: {
@@ -3803,9 +3808,7 @@ function runToolsTransformation({
3803
3808
  },
3804
3809
  flush() {
3805
3810
  canClose = true;
3806
- if (outstandingToolCalls.size === 0) {
3807
- toolResultsStreamController.close();
3808
- }
3811
+ attemptClose();
3809
3812
  }
3810
3813
  });
3811
3814
  return new ReadableStream({
@@ -4780,7 +4783,9 @@ function attachmentsToParts(attachments) {
4780
4783
  }
4781
4784
 
4782
4785
  // core/prompt/convert-to-core-messages.ts
4783
- function convertToCoreMessages(messages) {
4786
+ function convertToCoreMessages(messages, options) {
4787
+ var _a11;
4788
+ const tools = (_a11 = options == null ? void 0 : options.tools) != null ? _a11 : {};
4784
4789
  const coreMessages = [];
4785
4790
  for (const message of messages) {
4786
4791
  const { role, content, toolInvocations, experimental_attachments } = message;
@@ -4811,29 +4816,37 @@ function convertToCoreMessages(messages) {
4811
4816
  role: "assistant",
4812
4817
  content: [
4813
4818
  { type: "text", text: content },
4814
- ...toolInvocations.map(({ toolCallId, toolName, args }) => ({
4815
- type: "tool-call",
4816
- toolCallId,
4817
- toolName,
4818
- args
4819
- }))
4819
+ ...toolInvocations.map(
4820
+ ({ toolCallId, toolName, args }) => ({
4821
+ type: "tool-call",
4822
+ toolCallId,
4823
+ toolName,
4824
+ args
4825
+ })
4826
+ )
4820
4827
  ]
4821
4828
  });
4822
4829
  coreMessages.push({
4823
4830
  role: "tool",
4824
- content: toolInvocations.map((ToolInvocation) => {
4825
- if (!("result" in ToolInvocation)) {
4831
+ content: toolInvocations.map((toolInvocation) => {
4832
+ if (!("result" in toolInvocation)) {
4826
4833
  throw new MessageConversionError({
4827
4834
  originalMessage: message,
4828
- message: "ToolInvocation must have a result: " + JSON.stringify(ToolInvocation)
4835
+ message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
4829
4836
  });
4830
4837
  }
4831
- const { toolCallId, toolName, args, result } = ToolInvocation;
4832
- return {
4838
+ const { toolCallId, toolName, result } = toolInvocation;
4839
+ const tool2 = tools[toolName];
4840
+ return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
4841
+ type: "tool-result",
4842
+ toolCallId,
4843
+ toolName,
4844
+ result: tool2.experimental_toToolResultContent(result),
4845
+ experimental_content: tool2.experimental_toToolResultContent(result)
4846
+ } : {
4833
4847
  type: "tool-result",
4834
4848
  toolCallId,
4835
4849
  toolName,
4836
- args,
4837
4850
  result
4838
4851
  };
4839
4852
  })