ai 7.0.27 → 7.0.28

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.
@@ -85,7 +85,7 @@ import {
85
85
  } from "@ai-sdk/provider-utils";
86
86
 
87
87
  // src/version.ts
88
- var VERSION = true ? "7.0.27" : "0.0.0-test";
88
+ var VERSION = true ? "7.0.28" : "0.0.0-test";
89
89
 
90
90
  // src/util/download/download.ts
91
91
  var download = async ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "7.0.27",
3
+ "version": "7.0.28",
4
4
  "type": "module",
5
5
  "description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
6
6
  "license": "Apache-2.0",
package/src/ui/chat.ts CHANGED
@@ -650,9 +650,10 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
650
650
  let isAbort = false;
651
651
  let isDisconnect = false;
652
652
  let isError = false;
653
+ let activeResponse: ActiveResponse<UI_MESSAGE> | undefined;
653
654
 
654
655
  try {
655
- const activeResponse = {
656
+ const response = {
656
657
  state: createStreamingUIMessageState({
657
658
  lastMessage: this.state.snapshot(lastMessage),
658
659
  messageId: this.generateId(),
@@ -660,11 +661,13 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
660
661
  abortController: new AbortController(),
661
662
  } as ActiveResponse<UI_MESSAGE>;
662
663
 
663
- activeResponse.abortController.signal.addEventListener('abort', () => {
664
+ activeResponse = response;
665
+
666
+ response.abortController.signal.addEventListener('abort', () => {
664
667
  isAbort = true;
665
668
  });
666
669
 
667
- this.activeResponse = activeResponse;
670
+ this.activeResponse = response;
668
671
 
669
672
  let stream: ReadableStream<UIMessageChunk>;
670
673
 
@@ -674,7 +677,7 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
674
677
  stream = await this.transport.sendMessages({
675
678
  chatId: this.id,
676
679
  messages: this.state.messages,
677
- abortSignal: activeResponse.abortController.signal,
680
+ abortSignal: response.abortController.signal,
678
681
  metadata,
679
682
  headers,
680
683
  body,
@@ -692,21 +695,21 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
692
695
  // serialize the job execution to avoid race conditions:
693
696
  this.jobExecutor.run(() =>
694
697
  job({
695
- state: activeResponse.state,
698
+ state: response.state,
696
699
  write: () => {
697
700
  // streaming is set on first write (before it should be "submitted")
698
701
  this.setStatus({ status: 'streaming' });
699
702
 
700
703
  const replaceLastMessage =
701
- activeResponse.state.message.id === this.lastMessage?.id;
704
+ response.state.message.id === this.lastMessage?.id;
702
705
 
703
706
  if (replaceLastMessage) {
704
707
  this.state.replaceMessage(
705
708
  this.state.messages.length - 1,
706
- activeResponse.state.message,
709
+ response.state.message,
707
710
  );
708
711
  } else {
709
- this.state.pushMessage(activeResponse.state.message);
712
+ this.state.pushMessage(response.state.message);
710
713
  }
711
714
  },
712
715
  }),
@@ -756,19 +759,23 @@ export abstract class AbstractChat<UI_MESSAGE extends UIMessage> {
756
759
  this.setStatus({ status: 'error', error: err as Error });
757
760
  } finally {
758
761
  try {
759
- this.onFinish?.({
760
- message: this.activeResponse!.state.message,
761
- messages: this.state.messages,
762
- isAbort,
763
- isDisconnect,
764
- isError,
765
- finishReason: this.activeResponse?.state.finishReason,
766
- });
762
+ if (activeResponse) {
763
+ this.onFinish?.({
764
+ message: activeResponse.state.message,
765
+ messages: this.state.messages,
766
+ isAbort,
767
+ isDisconnect,
768
+ isError,
769
+ finishReason: activeResponse.state.finishReason,
770
+ });
771
+ }
767
772
  } catch (err) {
768
773
  console.error(err);
769
774
  }
770
775
 
771
- this.activeResponse = undefined;
776
+ if (this.activeResponse === activeResponse) {
777
+ this.activeResponse = undefined;
778
+ }
772
779
  }
773
780
 
774
781
  // automatically send the message if the sendAutomaticallyWhen function returns true