ai 6.0.226 → 6.0.227

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
+ ## 6.0.227
4
+
5
+ ### Patch Changes
6
+
7
+ - 6ace546: Fix chat `onFinish` handling when overlapping requests clear the active response before a resume stream finishes.
8
+
3
9
  ## 6.0.226
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1282,7 +1282,7 @@ function detectMediaType({
1282
1282
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1283
1283
 
1284
1284
  // src/version.ts
1285
- var VERSION = true ? "6.0.226" : "0.0.0-test";
1285
+ var VERSION = true ? "6.0.227" : "0.0.0-test";
1286
1286
 
1287
1287
  // src/util/download/download.ts
1288
1288
  var download = async ({
@@ -13837,7 +13837,7 @@ var AbstractChat = class {
13837
13837
  body,
13838
13838
  messageId
13839
13839
  }) {
13840
- var _a22, _b, _c;
13840
+ var _a22, _b;
13841
13841
  let resumeStream;
13842
13842
  if (trigger === "resume-stream") {
13843
13843
  try {
@@ -13864,18 +13864,20 @@ var AbstractChat = class {
13864
13864
  let isAbort = false;
13865
13865
  let isDisconnect = false;
13866
13866
  let isError = false;
13867
+ let activeResponse;
13867
13868
  try {
13868
- const activeResponse = {
13869
+ const response = {
13869
13870
  state: createStreamingUIMessageState({
13870
13871
  lastMessage: this.state.snapshot(lastMessage),
13871
13872
  messageId: this.generateId()
13872
13873
  }),
13873
13874
  abortController: new AbortController()
13874
13875
  };
13875
- activeResponse.abortController.signal.addEventListener("abort", () => {
13876
+ activeResponse = response;
13877
+ response.abortController.signal.addEventListener("abort", () => {
13876
13878
  isAbort = true;
13877
13879
  });
13878
- this.activeResponse = activeResponse;
13880
+ this.activeResponse = response;
13879
13881
  let stream;
13880
13882
  if (trigger === "resume-stream") {
13881
13883
  stream = resumeStream;
@@ -13883,7 +13885,7 @@ var AbstractChat = class {
13883
13885
  stream = await this.transport.sendMessages({
13884
13886
  chatId: this.id,
13885
13887
  messages: this.state.messages,
13886
- abortSignal: activeResponse.abortController.signal,
13888
+ abortSignal: response.abortController.signal,
13887
13889
  metadata,
13888
13890
  headers,
13889
13891
  body,
@@ -13895,18 +13897,18 @@ var AbstractChat = class {
13895
13897
  // serialize the job execution to avoid race conditions:
13896
13898
  this.jobExecutor.run(
13897
13899
  () => job({
13898
- state: activeResponse.state,
13900
+ state: response.state,
13899
13901
  write: () => {
13900
13902
  var _a23;
13901
13903
  this.setStatus({ status: "streaming" });
13902
- const replaceLastMessage = activeResponse.state.message.id === ((_a23 = this.lastMessage) == null ? void 0 : _a23.id);
13904
+ const replaceLastMessage = response.state.message.id === ((_a23 = this.lastMessage) == null ? void 0 : _a23.id);
13903
13905
  if (replaceLastMessage) {
13904
13906
  this.state.replaceMessage(
13905
13907
  this.state.messages.length - 1,
13906
- activeResponse.state.message
13908
+ response.state.message
13907
13909
  );
13908
13910
  } else {
13909
- this.state.pushMessage(activeResponse.state.message);
13911
+ this.state.pushMessage(response.state.message);
13910
13912
  }
13911
13913
  }
13912
13914
  })
@@ -13945,23 +13947,27 @@ var AbstractChat = class {
13945
13947
  this.setStatus({ status: "error", error: err });
13946
13948
  } finally {
13947
13949
  try {
13948
- (_b = this.onFinish) == null ? void 0 : _b.call(this, {
13949
- message: this.activeResponse.state.message,
13950
- messages: this.state.messages,
13951
- isAbort,
13952
- isDisconnect,
13953
- isError,
13954
- finishReason: (_a22 = this.activeResponse) == null ? void 0 : _a22.state.finishReason
13955
- });
13950
+ if (activeResponse) {
13951
+ (_a22 = this.onFinish) == null ? void 0 : _a22.call(this, {
13952
+ message: activeResponse.state.message,
13953
+ messages: this.state.messages,
13954
+ isAbort,
13955
+ isDisconnect,
13956
+ isError,
13957
+ finishReason: activeResponse.state.finishReason
13958
+ });
13959
+ }
13956
13960
  } catch (err) {
13957
13961
  console.error(err);
13958
13962
  }
13959
- this.activeResponse = void 0;
13963
+ if (this.activeResponse === activeResponse) {
13964
+ this.activeResponse = void 0;
13965
+ }
13960
13966
  }
13961
13967
  if (!isError && await this.shouldSendAutomatically()) {
13962
13968
  await this.makeRequest({
13963
13969
  trigger: "submit-message",
13964
- messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
13970
+ messageId: (_b = this.lastMessage) == null ? void 0 : _b.id,
13965
13971
  metadata,
13966
13972
  headers,
13967
13973
  body