ai 6.0.248 → 6.0.250

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
@@ -1193,7 +1193,7 @@ import {
1193
1193
  } from "@ai-sdk/provider-utils";
1194
1194
 
1195
1195
  // src/version.ts
1196
- var VERSION = true ? "6.0.248" : "0.0.0-test";
1196
+ var VERSION = true ? "6.0.250" : "0.0.0-test";
1197
1197
 
1198
1198
  // src/util/download/download.ts
1199
1199
  var download = async ({
@@ -2429,7 +2429,8 @@ async function recordSpan({
2429
2429
  tracer,
2430
2430
  attributes,
2431
2431
  fn,
2432
- endWhenDone = true
2432
+ endWhenDone = true,
2433
+ endOnError = endWhenDone
2433
2434
  }) {
2434
2435
  return tracer.startActiveSpan(
2435
2436
  name22,
@@ -2446,7 +2447,9 @@ async function recordSpan({
2446
2447
  try {
2447
2448
  recordErrorOnSpan(span, error);
2448
2449
  } finally {
2449
- span.end();
2450
+ if (endOnError) {
2451
+ span.end();
2452
+ }
2450
2453
  }
2451
2454
  throw error;
2452
2455
  }
@@ -6517,9 +6520,19 @@ function createAsyncIterableStream(source) {
6517
6520
  // src/util/consume-stream.ts
6518
6521
  async function consumeStream({
6519
6522
  stream,
6520
- onError
6523
+ onError,
6524
+ abortSignal
6521
6525
  }) {
6522
6526
  const reader = stream.getReader();
6527
+ const cancelOnAbort = () => {
6528
+ reader.cancel().catch(() => {
6529
+ });
6530
+ };
6531
+ if (abortSignal == null ? void 0 : abortSignal.aborted) {
6532
+ cancelOnAbort();
6533
+ } else {
6534
+ abortSignal == null ? void 0 : abortSignal.addEventListener("abort", cancelOnAbort, { once: true });
6535
+ }
6523
6536
  try {
6524
6537
  while (true) {
6525
6538
  const { done } = await reader.read();
@@ -6529,6 +6542,7 @@ async function consumeStream({
6529
6542
  } catch (error) {
6530
6543
  onError == null ? void 0 : onError(error);
6531
6544
  } finally {
6545
+ abortSignal == null ? void 0 : abortSignal.removeEventListener("abort", cancelOnAbort);
6532
6546
  reader.releaseLock();
6533
6547
  }
6534
6548
  }
@@ -7818,6 +7832,7 @@ var DefaultStreamTextResult = class {
7818
7832
  }),
7819
7833
  tracer,
7820
7834
  endWhenDone: false,
7835
+ endOnError: true,
7821
7836
  fn: async (doStreamSpan2) => ({
7822
7837
  startTimestampMs: now2(),
7823
7838
  // get before the call
@@ -11347,6 +11362,7 @@ var DefaultStreamObjectResult = class {
11347
11362
  }),
11348
11363
  tracer,
11349
11364
  endWhenDone: false,
11365
+ endOnError: true,
11350
11366
  fn: async (rootSpan) => {
11351
11367
  const standardizedPrompt = await standardizePrompt({
11352
11368
  system,
@@ -11418,6 +11434,7 @@ var DefaultStreamObjectResult = class {
11418
11434
  }),
11419
11435
  tracer,
11420
11436
  endWhenDone: false,
11437
+ endOnError: true,
11421
11438
  fn: async (doStreamSpan2) => ({
11422
11439
  startTimestampMs: now2(),
11423
11440
  doStreamSpan: doStreamSpan2,
@@ -13653,7 +13670,8 @@ var HttpChatTransport = class {
13653
13670
  const response = await fetch2(api, {
13654
13671
  method: "GET",
13655
13672
  headers,
13656
- credentials
13673
+ credentials,
13674
+ signal: options.abortSignal
13657
13675
  });
13658
13676
  if (response.status === 204) {
13659
13677
  return null;
@@ -13708,6 +13726,7 @@ var AbstractChat = class {
13708
13726
  sendAutomaticallyWhen
13709
13727
  }) {
13710
13728
  this.activeResponse = void 0;
13729
+ this.activeResumeRequest = void 0;
13711
13730
  this.jobExecutor = new SerialJobExecutor();
13712
13731
  /**
13713
13732
  * Appends or replaces a user message to the chat list. This triggers the API call to fetch
@@ -13877,12 +13896,9 @@ var AbstractChat = class {
13877
13896
  * Abort the current request immediately, keep the generated tokens if any.
13878
13897
  */
13879
13898
  this.stop = async () => {
13880
- var _a22;
13881
- if (this.status !== "streaming" && this.status !== "submitted")
13882
- return;
13883
- if ((_a22 = this.activeResponse) == null ? void 0 : _a22.abortController) {
13884
- this.activeResponse.abortController.abort();
13885
- }
13899
+ var _a22, _b;
13900
+ (_a22 = this.activeResumeRequest) == null ? void 0 : _a22.abortController.abort();
13901
+ (_b = this.activeResponse) == null ? void 0 : _b.abortController.abort();
13886
13902
  };
13887
13903
  this.id = id;
13888
13904
  this.transport = transport;
@@ -13946,25 +13962,60 @@ var AbstractChat = class {
13946
13962
  body,
13947
13963
  messageId
13948
13964
  }) {
13949
- var _a22, _b;
13965
+ var _a22, _b, _c;
13966
+ const abortController = new AbortController();
13967
+ const activeResumeRequest = trigger === "resume-stream" ? { abortController } : void 0;
13968
+ if (activeResumeRequest) {
13969
+ (_a22 = this.activeResumeRequest) == null ? void 0 : _a22.abortController.abort();
13970
+ this.activeResumeRequest = activeResumeRequest;
13971
+ }
13972
+ const isCurrentRequest = () => activeResumeRequest == null || this.activeResumeRequest === activeResumeRequest;
13973
+ const clearActiveResumeRequest = () => {
13974
+ if (this.activeResumeRequest === activeResumeRequest) {
13975
+ this.activeResumeRequest = void 0;
13976
+ }
13977
+ };
13950
13978
  let resumeStream;
13951
13979
  if (trigger === "resume-stream") {
13952
13980
  try {
13953
13981
  const reconnect = await this.transport.reconnectToStream({
13954
13982
  chatId: this.id,
13983
+ abortSignal: abortController.signal,
13955
13984
  metadata,
13956
13985
  headers,
13957
13986
  body
13958
13987
  });
13988
+ if (abortController.signal.aborted || !isCurrentRequest()) {
13989
+ await (reconnect == null ? void 0 : reconnect.cancel().catch(() => {
13990
+ }));
13991
+ if (isCurrentRequest()) {
13992
+ this.setStatus({ status: "ready" });
13993
+ }
13994
+ clearActiveResumeRequest();
13995
+ return;
13996
+ }
13959
13997
  if (reconnect == null) {
13998
+ this.setStatus({ status: "ready" });
13999
+ clearActiveResumeRequest();
13960
14000
  return;
13961
14001
  }
13962
14002
  resumeStream = reconnect;
13963
14003
  } catch (err) {
14004
+ if (abortController.signal.aborted || err.name === "AbortError") {
14005
+ if (isCurrentRequest()) {
14006
+ this.setStatus({ status: "ready" });
14007
+ }
14008
+ clearActiveResumeRequest();
14009
+ return;
14010
+ }
14011
+ if (!isCurrentRequest()) {
14012
+ return;
14013
+ }
13964
14014
  if (this.onError && err instanceof Error) {
13965
14015
  this.onError(err);
13966
14016
  }
13967
14017
  this.setStatus({ status: "error", error: err });
14018
+ clearActiveResumeRequest();
13968
14019
  return;
13969
14020
  }
13970
14021
  }
@@ -13977,10 +14028,10 @@ var AbstractChat = class {
13977
14028
  try {
13978
14029
  const response = {
13979
14030
  state: createStreamingUIMessageState({
13980
- lastMessage: trigger === "regenerate-message" ? void 0 : this.state.snapshot(lastMessage),
14031
+ lastMessage: trigger === "resume-stream" || trigger === "regenerate-message" ? void 0 : this.state.snapshot(lastMessage),
13981
14032
  messageId: this.generateId()
13982
14033
  }),
13983
- abortController: new AbortController()
14034
+ abortController
13984
14035
  };
13985
14036
  activeResponse = response;
13986
14037
  response.abortController.signal.addEventListener("abort", () => {
@@ -14004,11 +14055,17 @@ var AbstractChat = class {
14004
14055
  }
14005
14056
  const runUpdateMessageJob = (job) => (
14006
14057
  // serialize the job execution to avoid race conditions:
14007
- this.jobExecutor.run(
14008
- () => job({
14058
+ this.jobExecutor.run(() => {
14059
+ if (response.abortController.signal.aborted) {
14060
+ return Promise.resolve();
14061
+ }
14062
+ return job({
14009
14063
  state: response.state,
14010
14064
  write: () => {
14011
14065
  var _a23;
14066
+ if (response.abortController.signal.aborted) {
14067
+ return;
14068
+ }
14012
14069
  this.setStatus({ status: "streaming" });
14013
14070
  const replaceLastMessage = response.state.message.id === ((_a23 = this.lastMessage) == null ? void 0 : _a23.id);
14014
14071
  if (replaceLastMessage) {
@@ -14020,8 +14077,8 @@ var AbstractChat = class {
14020
14077
  this.state.pushMessage(response.state.message);
14021
14078
  }
14022
14079
  }
14023
- })
14024
- )
14080
+ });
14081
+ })
14025
14082
  );
14026
14083
  await consumeStream({
14027
14084
  stream: processUIMessageStream({
@@ -14035,15 +14092,29 @@ var AbstractChat = class {
14035
14092
  throw error;
14036
14093
  }
14037
14094
  }),
14095
+ abortSignal: response.abortController.signal,
14038
14096
  onError: (error) => {
14039
14097
  throw error;
14040
14098
  }
14041
14099
  });
14042
- this.setStatus({ status: "ready" });
14100
+ if (isAbort) {
14101
+ if (isCurrentRequest()) {
14102
+ this.setStatus({ status: "ready" });
14103
+ }
14104
+ return null;
14105
+ }
14106
+ if (isCurrentRequest()) {
14107
+ this.setStatus({ status: "ready" });
14108
+ }
14043
14109
  } catch (err) {
14044
14110
  if (isAbort || err.name === "AbortError") {
14045
14111
  isAbort = true;
14046
- this.setStatus({ status: "ready" });
14112
+ if (isCurrentRequest()) {
14113
+ this.setStatus({ status: "ready" });
14114
+ }
14115
+ return null;
14116
+ }
14117
+ if (!isCurrentRequest()) {
14047
14118
  return null;
14048
14119
  }
14049
14120
  isError = true;
@@ -14057,7 +14128,7 @@ var AbstractChat = class {
14057
14128
  } finally {
14058
14129
  try {
14059
14130
  if (activeResponse) {
14060
- (_a22 = this.onFinish) == null ? void 0 : _a22.call(this, {
14131
+ (_b = this.onFinish) == null ? void 0 : _b.call(this, {
14061
14132
  message: activeResponse.state.message,
14062
14133
  messages: this.state.messages,
14063
14134
  isAbort,
@@ -14072,11 +14143,12 @@ var AbstractChat = class {
14072
14143
  if (this.activeResponse === activeResponse) {
14073
14144
  this.activeResponse = void 0;
14074
14145
  }
14146
+ clearActiveResumeRequest();
14075
14147
  }
14076
14148
  if (!isError && await this.shouldSendAutomatically()) {
14077
14149
  await this.makeRequest({
14078
14150
  trigger: "submit-message",
14079
- messageId: (_b = this.lastMessage) == null ? void 0 : _b.id,
14151
+ messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
14080
14152
  metadata,
14081
14153
  headers,
14082
14154
  body