ai 3.4.24 → 3.4.25

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,11 @@
1
1
  # ai
2
2
 
3
+ ## 3.4.25
4
+
5
+ ### Patch Changes
6
+
7
+ - 6e0fa1c: fix (ai/core): wait for tool results to arrive before sending finish event
8
+
3
9
  ## 3.4.24
4
10
 
5
11
  ### Patch Changes
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({