ai 6.0.302 → 6.0.303

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
@@ -1225,7 +1225,7 @@ import {
1225
1225
  } from "@ai-sdk/provider-utils";
1226
1226
 
1227
1227
  // src/version.ts
1228
- var VERSION = true ? "6.0.302" : "0.0.0-test";
1228
+ var VERSION = true ? "6.0.303" : "0.0.0-test";
1229
1229
 
1230
1230
  // src/util/download/download.ts
1231
1231
  var download = async ({
@@ -6051,7 +6051,8 @@ function processUIMessageStream({
6051
6051
  runUpdateMessageJob,
6052
6052
  onError,
6053
6053
  onToolCall,
6054
- onData
6054
+ onData,
6055
+ resetStateOnMessageIdChange = false
6055
6056
  }) {
6056
6057
  return stream.pipeThrough(
6057
6058
  new TransformStream({
@@ -6564,6 +6565,16 @@ function processUIMessageStream({
6564
6565
  break;
6565
6566
  }
6566
6567
  case "start": {
6568
+ if (resetStateOnMessageIdChange && chunk.messageId != null && chunk.messageId !== state.message.id) {
6569
+ Object.assign(
6570
+ state,
6571
+ createStreamingUIMessageState({
6572
+ lastMessage: void 0,
6573
+ messageId: chunk.messageId
6574
+ }),
6575
+ { finishReason: void 0 }
6576
+ );
6577
+ }
6567
6578
  if (chunk.messageId != null) {
6568
6579
  state.message.id = chunk.messageId;
6569
6580
  }
@@ -9622,6 +9633,7 @@ function readUIMessageStream({
9622
9633
  consumeStream({
9623
9634
  stream: processUIMessageStream({
9624
9635
  stream,
9636
+ resetStateOnMessageIdChange: true,
9625
9637
  runUpdateMessageJob(job) {
9626
9638
  return job({
9627
9639
  state,
@@ -11960,17 +11972,24 @@ var SerialJobExecutor = class {
11960
11972
  constructor() {
11961
11973
  this.queue = [];
11962
11974
  this.isProcessing = false;
11975
+ this.idleWaiters = [];
11963
11976
  }
11964
11977
  async processQueue() {
11965
11978
  if (this.isProcessing) {
11966
11979
  return;
11967
11980
  }
11968
11981
  this.isProcessing = true;
11969
- while (this.queue.length > 0) {
11970
- await this.queue[0]();
11971
- this.queue.shift();
11982
+ try {
11983
+ while (this.queue.length > 0) {
11984
+ await this.queue[0]();
11985
+ this.queue.shift();
11986
+ }
11987
+ } finally {
11988
+ this.isProcessing = false;
11989
+ for (const resolve3 of this.idleWaiters.splice(0)) {
11990
+ resolve3();
11991
+ }
11972
11992
  }
11973
- this.isProcessing = false;
11974
11993
  }
11975
11994
  async run(job) {
11976
11995
  return new Promise((resolve3, reject) => {
@@ -11985,6 +12004,14 @@ var SerialJobExecutor = class {
11985
12004
  void this.processQueue();
11986
12005
  });
11987
12006
  }
12007
+ async waitForIdle() {
12008
+ if (!this.isProcessing && this.queue.length === 0) {
12009
+ return;
12010
+ }
12011
+ await new Promise((resolve3) => {
12012
+ this.idleWaiters.push(resolve3);
12013
+ });
12014
+ }
11988
12015
  };
11989
12016
 
11990
12017
  // src/util/simulate-readable-stream.ts
@@ -14647,6 +14674,8 @@ var AbstractChat = class {
14647
14674
  this.activeResumeRequest = void 0;
14648
14675
  this.resumableStreamState = void 0;
14649
14676
  this.jobExecutor = new SerialJobExecutor();
14677
+ this.activeStopCount = 0;
14678
+ this.stopGeneration = 0;
14650
14679
  /**
14651
14680
  * Appends or replaces a user message to the chat list. This triggers the API call to fetch
14652
14681
  * the assistant's response.
@@ -14790,17 +14819,18 @@ var AbstractChat = class {
14790
14819
  this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
14791
14820
  }
14792
14821
  if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
14793
- this.shouldSendAutomatically().then((shouldSend) => {
14794
- var _a23;
14795
- if (shouldSend) {
14822
+ const shouldSend = await this.shouldSendAutomatically();
14823
+ if (shouldSend) {
14824
+ void this.runAutomaticRequest(() => {
14825
+ var _a23;
14796
14826
  const messageId = messageIndex === -1 ? (_a23 = this.lastMessage) == null ? void 0 : _a23.id : messages[messageIndex].id;
14797
- this.makeRequestForToolApproval({
14827
+ return this.makeRequestForToolApproval({
14798
14828
  messageId,
14799
14829
  messageIndex,
14800
14830
  ...options
14801
14831
  });
14802
- }
14803
- });
14832
+ }, shouldSend);
14833
+ }
14804
14834
  }
14805
14835
  });
14806
14836
  this.addToolOutput = async ({
@@ -14821,27 +14851,44 @@ var AbstractChat = class {
14821
14851
  this.activeResponse.state.message.parts = this.activeResponse.state.message.parts.map(updatePart);
14822
14852
  }
14823
14853
  if (this.status !== "streaming" && this.status !== "submitted" && this.sendAutomaticallyWhen) {
14824
- this.shouldSendAutomatically().then((shouldSend) => {
14825
- var _a23;
14826
- if (shouldSend) {
14827
- this.makeRequest({
14828
- trigger: "submit-message",
14829
- messageId: (_a23 = this.lastMessage) == null ? void 0 : _a23.id,
14830
- ...options
14831
- });
14832
- }
14833
- });
14854
+ const shouldSend = await this.shouldSendAutomatically();
14855
+ if (shouldSend) {
14856
+ void this.runAutomaticRequest(
14857
+ () => {
14858
+ var _a23;
14859
+ return this.makeRequest({
14860
+ trigger: "submit-message",
14861
+ messageId: (_a23 = this.lastMessage) == null ? void 0 : _a23.id,
14862
+ ...options
14863
+ });
14864
+ },
14865
+ shouldSend
14866
+ );
14867
+ }
14834
14868
  }
14835
14869
  });
14836
14870
  /** @deprecated Use addToolOutput */
14837
14871
  this.addToolResult = this.addToolOutput;
14838
14872
  /**
14839
- * Abort the current request immediately, keep the generated tokens if any.
14873
+ * Abort the current request, keep the generated tokens if any, and wait for
14874
+ * the request pipeline to finish.
14840
14875
  */
14841
14876
  this.stop = async () => {
14842
- var _a23, _b;
14843
- (_a23 = this.activeResumeRequest) == null ? void 0 : _a23.abortController.abort();
14844
- (_b = this.activeResponse) == null ? void 0 : _b.abortController.abort();
14877
+ this.activeStopCount++;
14878
+ this.stopGeneration++;
14879
+ try {
14880
+ const activeResumeRequest = this.activeResumeRequest;
14881
+ const activeResponse = this.activeResponse;
14882
+ activeResumeRequest == null ? void 0 : activeResumeRequest.abortController.abort();
14883
+ activeResponse == null ? void 0 : activeResponse.abortController.abort();
14884
+ await Promise.all([
14885
+ activeResumeRequest == null ? void 0 : activeResumeRequest.completionPromise,
14886
+ activeResponse == null ? void 0 : activeResponse.completionPromise
14887
+ ]);
14888
+ await this.jobExecutor.waitForIdle();
14889
+ } finally {
14890
+ this.activeStopCount--;
14891
+ }
14845
14892
  };
14846
14893
  this.id = id;
14847
14894
  this.transport = transport;
@@ -14902,6 +14949,17 @@ var AbstractChat = class {
14902
14949
  }
14903
14950
  return result;
14904
14951
  }
14952
+ async runAutomaticRequest(request, shouldSend) {
14953
+ const stopGeneration = this.stopGeneration;
14954
+ const startedWhileStopping = this.activeStopCount > 0;
14955
+ if (!(shouldSend != null ? shouldSend : await this.shouldSendAutomatically())) {
14956
+ return;
14957
+ }
14958
+ if (startedWhileStopping || stopGeneration !== this.stopGeneration || this.activeStopCount > 0) {
14959
+ return;
14960
+ }
14961
+ await request();
14962
+ }
14905
14963
  async makeRequestForToolApproval({
14906
14964
  messageId,
14907
14965
  messageIndex,
@@ -14921,19 +14979,31 @@ var AbstractChat = class {
14921
14979
  this.pendingApprovalMessageId = (_b = (_a23 = this.state.messages[messageIndex]) == null ? void 0 : _a23.id) != null ? _b : messageId;
14922
14980
  }
14923
14981
  }
14924
- async makeRequest({
14982
+ async makeRequest(options) {
14983
+ const completion = createResolvablePromise();
14984
+ try {
14985
+ await this.makeRequestImpl({
14986
+ ...options,
14987
+ completionPromise: completion.promise
14988
+ });
14989
+ } finally {
14990
+ completion.resolve();
14991
+ }
14992
+ }
14993
+ async makeRequestImpl({
14925
14994
  trigger,
14926
14995
  metadata,
14927
14996
  headers,
14928
14997
  body,
14929
- messageId
14998
+ messageId,
14999
+ completionPromise
14930
15000
  }) {
14931
- var _a23, _b, _c;
15001
+ var _a23, _b;
14932
15002
  if (trigger !== "resume-stream") {
14933
15003
  this.resumableStreamState = void 0;
14934
15004
  }
14935
15005
  const abortController = new AbortController();
14936
- const activeResumeRequest = trigger === "resume-stream" ? { abortController } : void 0;
15006
+ const activeResumeRequest = trigger === "resume-stream" ? { abortController, completionPromise } : void 0;
14937
15007
  if (activeResumeRequest) {
14938
15008
  (_a23 = this.activeResumeRequest) == null ? void 0 : _a23.abortController.abort();
14939
15009
  this.activeResumeRequest = activeResumeRequest;
@@ -15009,7 +15079,8 @@ var AbstractChat = class {
15009
15079
  lastMessage: trigger === "resume-stream" ? resumableResponseMessage : trigger === "regenerate-message" ? void 0 : this.state.snapshot(responseMessage),
15010
15080
  messageId: this.generateId()
15011
15081
  }),
15012
- abortController
15082
+ abortController,
15083
+ completionPromise
15013
15084
  };
15014
15085
  activeResponse = response;
15015
15086
  response.abortController.signal.addEventListener("abort", () => {
@@ -15142,14 +15213,19 @@ var AbstractChat = class {
15142
15213
  clearActiveResumeRequest();
15143
15214
  }
15144
15215
  }
15145
- if (!isError && await this.shouldSendAutomatically()) {
15146
- await this.makeRequest({
15147
- trigger: "submit-message",
15148
- messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
15149
- metadata,
15150
- headers,
15151
- body
15152
- });
15216
+ if (!isAbort && !isError) {
15217
+ await this.runAutomaticRequest(
15218
+ () => {
15219
+ var _a24;
15220
+ return this.makeRequest({
15221
+ trigger: "submit-message",
15222
+ messageId: (_a24 = this.lastMessage) == null ? void 0 : _a24.id,
15223
+ metadata,
15224
+ headers,
15225
+ body
15226
+ });
15227
+ }
15228
+ );
15153
15229
  }
15154
15230
  }
15155
15231
  };