ai 5.0.23 → 5.0.24

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,18 @@
1
1
  # ai
2
2
 
3
+ ## 5.0.24
4
+
5
+ ### Patch Changes
6
+
7
+ - f8f3682: fix: call onFinish when stream is cancelled in toUIMessageStream
8
+
9
+ Previously, onFinish was only called on normal stream completion. Now it's also called when the reader is cancelled (e.g., browser close, navigation), ensuring partial messages are persisted.
10
+
11
+ - Updated dependencies [1b5a3d3]
12
+ - Updated dependencies [c9994f9]
13
+ - @ai-sdk/provider-utils@3.0.6
14
+ - @ai-sdk/gateway@1.0.13
15
+
3
16
  ## 5.0.23
4
17
 
5
18
  ### Patch Changes
package/dist/index.js CHANGED
@@ -3838,6 +3838,23 @@ function handleUIMessageStreamFinish({
3838
3838
  await job({ state, write: () => {
3839
3839
  } });
3840
3840
  };
3841
+ let finishCalled = false;
3842
+ const callOnFinish = async () => {
3843
+ if (finishCalled || !onFinish) {
3844
+ return;
3845
+ }
3846
+ finishCalled = true;
3847
+ const isContinuation = state.message.id === (lastMessage == null ? void 0 : lastMessage.id);
3848
+ await onFinish({
3849
+ isAborted,
3850
+ isContinuation,
3851
+ responseMessage: state.message,
3852
+ messages: [
3853
+ ...isContinuation ? originalMessages.slice(0, -1) : originalMessages,
3854
+ state.message
3855
+ ]
3856
+ });
3857
+ };
3841
3858
  return processUIMessageStream({
3842
3859
  stream: idInjectedStream,
3843
3860
  runUpdateMessageJob,
@@ -3847,17 +3864,12 @@ function handleUIMessageStreamFinish({
3847
3864
  transform(chunk, controller) {
3848
3865
  controller.enqueue(chunk);
3849
3866
  },
3867
+ // @ts-expect-error cancel is still new and missing from types https://developer.mozilla.org/en-US/docs/Web/API/TransformStream#browser_compatibility
3868
+ async cancel() {
3869
+ await callOnFinish();
3870
+ },
3850
3871
  async flush() {
3851
- const isContinuation = state.message.id === (lastMessage == null ? void 0 : lastMessage.id);
3852
- await onFinish({
3853
- isAborted,
3854
- isContinuation,
3855
- responseMessage: state.message,
3856
- messages: [
3857
- ...isContinuation ? originalMessages.slice(0, -1) : originalMessages,
3858
- state.message
3859
- ]
3860
- });
3872
+ await callOnFinish();
3861
3873
  }
3862
3874
  })
3863
3875
  );
@@ -9609,12 +9621,7 @@ function convertToModelMessages(messages, options) {
9609
9621
  });
9610
9622
  } else if (part.type === "dynamic-tool") {
9611
9623
  const toolName = part.toolName;
9612
- if (part.state === "input-streaming") {
9613
- throw new MessageConversionError({
9614
- originalMessage: message,
9615
- message: `incomplete tool input is not supported: ${part.toolCallId}`
9616
- });
9617
- } else {
9624
+ if (part.state !== "input-streaming") {
9618
9625
  content.push({
9619
9626
  type: "tool-call",
9620
9627
  toolCallId: part.toolCallId,
@@ -9625,12 +9632,7 @@ function convertToModelMessages(messages, options) {
9625
9632
  }
9626
9633
  } else if (isToolUIPart(part)) {
9627
9634
  const toolName = getToolName(part);
9628
- if (part.state === "input-streaming") {
9629
- throw new MessageConversionError({
9630
- originalMessage: message,
9631
- message: `incomplete tool input is not supported: ${part.toolCallId}`
9632
- });
9633
- } else {
9635
+ if (part.state !== "input-streaming") {
9634
9636
  content.push({
9635
9637
  type: "tool-call",
9636
9638
  toolCallId: part.toolCallId,
@@ -9685,13 +9687,12 @@ function convertToModelMessages(messages, options) {
9685
9687
  };
9686
9688
  }
9687
9689
  default: {
9688
- throw new MessageConversionError({
9689
- originalMessage: message,
9690
- message: `Unsupported tool part state: ${toolPart.state}`
9691
- });
9690
+ return null;
9692
9691
  }
9693
9692
  }
9694
- })
9693
+ }).filter(
9694
+ (output) => output != null
9695
+ )
9695
9696
  });
9696
9697
  }
9697
9698
  block = [];