ai 7.0.26 → 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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # ai
2
2
 
3
+ ## 7.0.28
4
+
5
+ ### Patch Changes
6
+
7
+ - 0bc8d4f: Fix chat `onFinish` handling when overlapping requests clear the active response before a resume stream finishes.
8
+
9
+ ## 7.0.27
10
+
11
+ ### Patch Changes
12
+
13
+ - ac01b79: Allow validating assistant UI messages with empty parts so persisted errored responses remain loadable.
14
+ - 2696562: `experimental_streamTranscribe` result promises now resolve without consuming `fullStream`: accessing any result promise consumes the stream internally. Previously `await result.text` alone deadlocked on transform backpressure. Because live transcription streams can be unbounded, `fullStream` is explicitly single-consumer (no replay buffering): access it once, before any result promise, when both stream parts and final results are needed.
15
+ - Updated dependencies [31c7be8]
16
+ - Updated dependencies [4d096f6]
17
+ - @ai-sdk/provider-utils@5.0.10
18
+ - @ai-sdk/gateway@4.0.20
19
+
3
20
  ## 7.0.26
4
21
 
5
22
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -8840,6 +8840,11 @@ interface StreamTranscriptionResult {
8840
8840
  readonly providerMetadata: PromiseLike<Record<string, JSONObject>>;
8841
8841
  /**
8842
8842
  * Full stream of transcription parts.
8843
+ *
8844
+ * This is a single-consumer live stream and can only be accessed once.
8845
+ * Access it before any result promise when both stream parts and final
8846
+ * results are needed; accessing a result promise first consumes the stream
8847
+ * internally and makes `fullStream` unavailable.
8843
8848
  */
8844
8849
  readonly fullStream: AsyncIterableStream<TranscriptionStreamPart>;
8845
8850
  }
package/dist/index.js CHANGED
@@ -1089,7 +1089,7 @@ import {
1089
1089
  } from "@ai-sdk/provider-utils";
1090
1090
 
1091
1091
  // src/version.ts
1092
- var VERSION = true ? "7.0.26" : "0.0.0-test";
1092
+ var VERSION = true ? "7.0.28" : "0.0.0-test";
1093
1093
 
1094
1094
  // src/util/download/download.ts
1095
1095
  var download = async ({
@@ -10891,7 +10891,19 @@ var uiMessagesSchema = lazySchema2(
10891
10891
  })
10892
10892
  })
10893
10893
  ])
10894
- ).nonempty("Message must contain at least one part")
10894
+ )
10895
+ }).superRefine((message, context) => {
10896
+ if (message.role !== "assistant" && message.parts.length === 0) {
10897
+ context.addIssue({
10898
+ origin: "array",
10899
+ code: "too_small",
10900
+ minimum: 1,
10901
+ inclusive: true,
10902
+ input: message.parts,
10903
+ path: ["parts"],
10904
+ message: "Message must contain at least one part"
10905
+ });
10906
+ }
10895
10907
  })
10896
10908
  ).nonempty("Messages array must not be empty")
10897
10909
  )
@@ -16089,35 +16101,60 @@ function streamTranscribe({
16089
16101
  transform.writable.abort(reason).catch(() => {
16090
16102
  });
16091
16103
  });
16104
+ let streamOwner = "unclaimed";
16105
+ function consumeStream2() {
16106
+ if (streamOwner === "full-stream" || streamOwner === "result-promises") {
16107
+ return;
16108
+ }
16109
+ streamOwner = "result-promises";
16110
+ const reader = transform.readable.getReader();
16111
+ void (async () => {
16112
+ while (!(await reader.read()).done) {
16113
+ }
16114
+ })().catch(() => {
16115
+ });
16116
+ }
16117
+ function getFullStream() {
16118
+ if (streamOwner !== "unclaimed") {
16119
+ throw new Error(
16120
+ streamOwner === "full-stream" ? "fullStream can only be accessed once." : "fullStream cannot be accessed after a result promise."
16121
+ );
16122
+ }
16123
+ streamOwner = "full-stream";
16124
+ return asAsyncIterableStream(transform.readable);
16125
+ }
16092
16126
  return {
16093
16127
  get text() {
16128
+ consumeStream2();
16094
16129
  return textPromise.promise;
16095
16130
  },
16096
16131
  get segments() {
16132
+ consumeStream2();
16097
16133
  return segmentsPromise.promise;
16098
16134
  },
16099
16135
  get language() {
16136
+ consumeStream2();
16100
16137
  return languagePromise.promise;
16101
16138
  },
16102
16139
  get durationInSeconds() {
16140
+ consumeStream2();
16103
16141
  return durationInSecondsPromise.promise;
16104
16142
  },
16105
16143
  get warnings() {
16144
+ consumeStream2();
16106
16145
  return warningsPromise.promise;
16107
16146
  },
16108
16147
  get responses() {
16148
+ consumeStream2();
16109
16149
  return responsesPromise.promise;
16110
16150
  },
16111
16151
  get providerMetadata() {
16152
+ consumeStream2();
16112
16153
  return providerMetadataPromise.promise;
16113
16154
  },
16114
- // `transform.readable` is fresh and exclusively owned here, so attach the
16115
- // async iterator in place rather than piping through another transform.
16116
- // The extra transform (as `createAsyncIterableStream` would add) chains two
16117
- // transforms fed by the active model pipe below and surfaces a spurious
16118
- // unhandled `undefined` rejection when the consumer cancels early on
16119
- // Node.js 26.
16120
- fullStream: asAsyncIterableStream(transform.readable)
16155
+ get fullStream() {
16156
+ return getFullStream();
16157
+ }
16121
16158
  };
16122
16159
  }
16123
16160
 
@@ -16695,7 +16732,7 @@ var AbstractChat = class {
16695
16732
  body,
16696
16733
  messageId
16697
16734
  }) {
16698
- var _a22, _b, _c;
16735
+ var _a22, _b;
16699
16736
  let resumeStream;
16700
16737
  if (trigger === "resume-stream") {
16701
16738
  try {
@@ -16722,18 +16759,20 @@ var AbstractChat = class {
16722
16759
  let isAbort = false;
16723
16760
  let isDisconnect = false;
16724
16761
  let isError = false;
16762
+ let activeResponse;
16725
16763
  try {
16726
- const activeResponse = {
16764
+ const response = {
16727
16765
  state: createStreamingUIMessageState({
16728
16766
  lastMessage: this.state.snapshot(lastMessage),
16729
16767
  messageId: this.generateId()
16730
16768
  }),
16731
16769
  abortController: new AbortController()
16732
16770
  };
16733
- activeResponse.abortController.signal.addEventListener("abort", () => {
16771
+ activeResponse = response;
16772
+ response.abortController.signal.addEventListener("abort", () => {
16734
16773
  isAbort = true;
16735
16774
  });
16736
- this.activeResponse = activeResponse;
16775
+ this.activeResponse = response;
16737
16776
  let stream;
16738
16777
  if (trigger === "resume-stream") {
16739
16778
  stream = resumeStream;
@@ -16741,7 +16780,7 @@ var AbstractChat = class {
16741
16780
  stream = await this.transport.sendMessages({
16742
16781
  chatId: this.id,
16743
16782
  messages: this.state.messages,
16744
- abortSignal: activeResponse.abortController.signal,
16783
+ abortSignal: response.abortController.signal,
16745
16784
  metadata,
16746
16785
  headers,
16747
16786
  body,
@@ -16753,18 +16792,18 @@ var AbstractChat = class {
16753
16792
  // serialize the job execution to avoid race conditions:
16754
16793
  this.jobExecutor.run(
16755
16794
  () => job({
16756
- state: activeResponse.state,
16795
+ state: response.state,
16757
16796
  write: () => {
16758
16797
  var _a23;
16759
16798
  this.setStatus({ status: "streaming" });
16760
- const replaceLastMessage = activeResponse.state.message.id === ((_a23 = this.lastMessage) == null ? void 0 : _a23.id);
16799
+ const replaceLastMessage = response.state.message.id === ((_a23 = this.lastMessage) == null ? void 0 : _a23.id);
16761
16800
  if (replaceLastMessage) {
16762
16801
  this.state.replaceMessage(
16763
16802
  this.state.messages.length - 1,
16764
- activeResponse.state.message
16803
+ response.state.message
16765
16804
  );
16766
16805
  } else {
16767
- this.state.pushMessage(activeResponse.state.message);
16806
+ this.state.pushMessage(response.state.message);
16768
16807
  }
16769
16808
  }
16770
16809
  })
@@ -16803,23 +16842,27 @@ var AbstractChat = class {
16803
16842
  this.setStatus({ status: "error", error: err });
16804
16843
  } finally {
16805
16844
  try {
16806
- (_b = this.onFinish) == null ? void 0 : _b.call(this, {
16807
- message: this.activeResponse.state.message,
16808
- messages: this.state.messages,
16809
- isAbort,
16810
- isDisconnect,
16811
- isError,
16812
- finishReason: (_a22 = this.activeResponse) == null ? void 0 : _a22.state.finishReason
16813
- });
16845
+ if (activeResponse) {
16846
+ (_a22 = this.onFinish) == null ? void 0 : _a22.call(this, {
16847
+ message: activeResponse.state.message,
16848
+ messages: this.state.messages,
16849
+ isAbort,
16850
+ isDisconnect,
16851
+ isError,
16852
+ finishReason: activeResponse.state.finishReason
16853
+ });
16854
+ }
16814
16855
  } catch (err) {
16815
16856
  console.error(err);
16816
16857
  }
16817
- this.activeResponse = void 0;
16858
+ if (this.activeResponse === activeResponse) {
16859
+ this.activeResponse = void 0;
16860
+ }
16818
16861
  }
16819
16862
  if (!isError && await this.shouldSendAutomatically()) {
16820
16863
  await this.makeRequest({
16821
16864
  trigger: "submit-message",
16822
- messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
16865
+ messageId: (_b = this.lastMessage) == null ? void 0 : _b.id,
16823
16866
  metadata,
16824
16867
  headers,
16825
16868
  body