ai 3.4.23 → 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/dist/index.mjs CHANGED
@@ -3593,8 +3593,6 @@ function runToolsTransformation({
3593
3593
  telemetry,
3594
3594
  abortSignal
3595
3595
  }) {
3596
- let canClose = false;
3597
- const outstandingToolCalls = /* @__PURE__ */ new Set();
3598
3596
  let toolResultsStreamController = null;
3599
3597
  const toolResultsStream = new ReadableStream({
3600
3598
  start(controller) {
@@ -3602,6 +3600,17 @@ function runToolsTransformation({
3602
3600
  }
3603
3601
  });
3604
3602
  const activeToolCalls = {};
3603
+ const outstandingToolResults = /* @__PURE__ */ new Set();
3604
+ let canClose = false;
3605
+ let finishChunk = void 0;
3606
+ function attemptClose() {
3607
+ if (canClose && outstandingToolResults.size === 0) {
3608
+ if (finishChunk != null) {
3609
+ toolResultsStreamController.enqueue(finishChunk);
3610
+ }
3611
+ toolResultsStreamController.close();
3612
+ }
3613
+ }
3605
3614
  const forwardStream = new TransformStream({
3606
3615
  transform(chunk, controller) {
3607
3616
  const chunkType = chunk.type;
@@ -3659,7 +3668,7 @@ function runToolsTransformation({
3659
3668
  controller.enqueue(toolCall);
3660
3669
  if (tool2.execute != null) {
3661
3670
  const toolExecutionId = generateId();
3662
- outstandingToolCalls.add(toolExecutionId);
3671
+ outstandingToolResults.add(toolExecutionId);
3663
3672
  recordSpan({
3664
3673
  name: "ai.toolCall",
3665
3674
  attributes: selectTelemetryAttributes({
@@ -3684,10 +3693,8 @@ function runToolsTransformation({
3684
3693
  type: "tool-result",
3685
3694
  result
3686
3695
  });
3687
- outstandingToolCalls.delete(toolExecutionId);
3688
- if (canClose && outstandingToolCalls.size === 0) {
3689
- toolResultsStreamController.close();
3690
- }
3696
+ outstandingToolResults.delete(toolExecutionId);
3697
+ attemptClose();
3691
3698
  try {
3692
3699
  span.setAttributes(
3693
3700
  selectTelemetryAttributes({
@@ -3707,10 +3714,8 @@ function runToolsTransformation({
3707
3714
  type: "error",
3708
3715
  error
3709
3716
  });
3710
- outstandingToolCalls.delete(toolExecutionId);
3711
- if (canClose && outstandingToolCalls.size === 0) {
3712
- toolResultsStreamController.close();
3713
- }
3717
+ outstandingToolResults.delete(toolExecutionId);
3718
+ attemptClose();
3714
3719
  }
3715
3720
  )
3716
3721
  });
@@ -3724,13 +3729,13 @@ function runToolsTransformation({
3724
3729
  break;
3725
3730
  }
3726
3731
  case "finish": {
3727
- controller.enqueue({
3732
+ finishChunk = {
3728
3733
  type: "finish",
3729
3734
  finishReason: chunk.finishReason,
3730
3735
  logprobs: chunk.logprobs,
3731
3736
  usage: calculateLanguageModelUsage(chunk.usage),
3732
3737
  experimental_providerMetadata: chunk.providerMetadata
3733
- });
3738
+ };
3734
3739
  break;
3735
3740
  }
3736
3741
  default: {
@@ -3741,9 +3746,7 @@ function runToolsTransformation({
3741
3746
  },
3742
3747
  flush() {
3743
3748
  canClose = true;
3744
- if (outstandingToolCalls.size === 0) {
3745
- toolResultsStreamController.close();
3746
- }
3749
+ attemptClose();
3747
3750
  }
3748
3751
  });
3749
3752
  return new ReadableStream({