ai 3.1.10 → 3.1.11

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.d.mts CHANGED
@@ -2197,12 +2197,9 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
2197
2197
  declare class StreamData {
2198
2198
  private encoder;
2199
2199
  private controller;
2200
- stream: TransformStream<Uint8Array, Uint8Array>;
2201
- private isClosedPromise;
2202
- private isClosedPromiseResolver;
2200
+ stream: ReadableStream<Uint8Array>;
2203
2201
  private isClosed;
2204
- private data;
2205
- private messageAnnotations;
2202
+ private warningTimeout;
2206
2203
  constructor();
2207
2204
  close(): Promise<void>;
2208
2205
  append(value: JSONValue): void;
package/dist/index.d.ts CHANGED
@@ -2197,12 +2197,9 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
2197
2197
  declare class StreamData {
2198
2198
  private encoder;
2199
2199
  private controller;
2200
- stream: TransformStream<Uint8Array, Uint8Array>;
2201
- private isClosedPromise;
2202
- private isClosedPromiseResolver;
2200
+ stream: ReadableStream<Uint8Array>;
2203
2201
  private isClosed;
2204
- private data;
2205
- private messageAnnotations;
2202
+ private warningTimeout;
2206
2203
  constructor();
2207
2204
  close(): Promise<void>;
2208
2205
  append(value: JSONValue): void;
package/dist/index.js CHANGED
@@ -2273,86 +2273,61 @@ var StreamData = class {
2273
2273
  constructor() {
2274
2274
  this.encoder = new TextEncoder();
2275
2275
  this.controller = null;
2276
- // closing the stream is synchronous, but we want to return a promise
2277
- // in case we're doing async work
2278
- this.isClosedPromise = null;
2279
- this.isClosedPromiseResolver = void 0;
2280
2276
  this.isClosed = false;
2281
- // array to store appended data
2282
- this.data = [];
2283
- this.messageAnnotations = [];
2284
- this.isClosedPromise = new Promise((resolve) => {
2285
- this.isClosedPromiseResolver = resolve;
2286
- });
2277
+ this.warningTimeout = null;
2287
2278
  const self = this;
2288
- this.stream = new TransformStream({
2279
+ this.stream = new ReadableStream({
2289
2280
  start: async (controller) => {
2290
2281
  self.controller = controller;
2291
- },
2292
- transform: async (chunk, controller) => {
2293
- if (self.data.length > 0) {
2294
- const encodedData = self.encoder.encode(
2295
- formatStreamPart("data", self.data)
2296
- );
2297
- self.data = [];
2298
- controller.enqueue(encodedData);
2299
- }
2300
- if (self.messageAnnotations.length) {
2301
- const encodedMessageAnnotations = self.encoder.encode(
2302
- formatStreamPart("message_annotations", self.messageAnnotations)
2303
- );
2304
- self.messageAnnotations = [];
2305
- controller.enqueue(encodedMessageAnnotations);
2282
+ if (process.env.NODE_ENV === "development") {
2283
+ self.warningTimeout = setTimeout(() => {
2284
+ console.warn(
2285
+ "The data stream is hanging. Did you forget to close it with `data.close()`?"
2286
+ );
2287
+ }, 3e3);
2306
2288
  }
2307
- controller.enqueue(chunk);
2308
2289
  },
2309
- async flush(controller) {
2310
- const warningTimeout = process.env.NODE_ENV === "development" ? setTimeout(() => {
2311
- console.warn(
2312
- "The data stream is hanging. Did you forget to close it with `data.close()`?"
2313
- );
2314
- }, 3e3) : null;
2315
- await self.isClosedPromise;
2316
- if (warningTimeout !== null) {
2317
- clearTimeout(warningTimeout);
2318
- }
2319
- if (self.data.length) {
2320
- const encodedData = self.encoder.encode(
2321
- formatStreamPart("data", self.data)
2322
- );
2323
- controller.enqueue(encodedData);
2324
- }
2325
- if (self.messageAnnotations.length) {
2326
- const encodedData = self.encoder.encode(
2327
- formatStreamPart("message_annotations", self.messageAnnotations)
2328
- );
2329
- controller.enqueue(encodedData);
2330
- }
2290
+ pull: (controller) => {
2291
+ },
2292
+ cancel: (reason) => {
2293
+ this.isClosed = true;
2331
2294
  }
2332
2295
  });
2333
2296
  }
2334
2297
  async close() {
2335
- var _a;
2336
2298
  if (this.isClosed) {
2337
2299
  throw new Error("Data Stream has already been closed.");
2338
2300
  }
2339
2301
  if (!this.controller) {
2340
2302
  throw new Error("Stream controller is not initialized.");
2341
2303
  }
2342
- (_a = this.isClosedPromiseResolver) == null ? void 0 : _a.call(this);
2304
+ this.controller.close();
2343
2305
  this.isClosed = true;
2306
+ if (this.warningTimeout) {
2307
+ clearTimeout(this.warningTimeout);
2308
+ }
2344
2309
  }
2345
2310
  append(value) {
2346
2311
  if (this.isClosed) {
2347
2312
  throw new Error("Data Stream has already been closed.");
2348
2313
  }
2349
- this.data.push(value);
2314
+ if (!this.controller) {
2315
+ throw new Error("Stream controller is not initialized.");
2316
+ }
2317
+ this.controller.enqueue(
2318
+ this.encoder.encode(formatStreamPart("data", [value]))
2319
+ );
2350
2320
  }
2351
2321
  appendMessageAnnotation(value) {
2352
2322
  if (this.isClosed) {
2353
2323
  throw new Error("Data Stream has already been closed.");
2354
2324
  }
2355
- this.messageAnnotations.push(value);
2325
+ if (!this.controller) {
2326
+ throw new Error("Stream controller is not initialized.");
2327
+ }
2328
+ this.controller.enqueue(
2329
+ this.encoder.encode(formatStreamPart("message_annotations", [value]))
2330
+ );
2356
2331
  }
2357
2332
  };
2358
2333
  function createStreamDataTransformer() {
@@ -3126,6 +3101,94 @@ async function ReplicateStream(res, cb, options) {
3126
3101
  );
3127
3102
  }
3128
3103
 
3104
+ // core/util/merge-streams.ts
3105
+ function mergeStreams(stream1, stream2) {
3106
+ const reader1 = stream1.getReader();
3107
+ const reader2 = stream2.getReader();
3108
+ let lastRead1 = void 0;
3109
+ let lastRead2 = void 0;
3110
+ let stream1Done = false;
3111
+ let stream2Done = false;
3112
+ async function readStream1(controller) {
3113
+ try {
3114
+ if (lastRead1 == null) {
3115
+ lastRead1 = reader1.read();
3116
+ }
3117
+ const result = await lastRead1;
3118
+ lastRead1 = void 0;
3119
+ if (!result.done) {
3120
+ controller.enqueue(result.value);
3121
+ } else {
3122
+ controller.close();
3123
+ }
3124
+ } catch (error) {
3125
+ controller.error(error);
3126
+ }
3127
+ }
3128
+ async function readStream2(controller) {
3129
+ try {
3130
+ if (lastRead2 == null) {
3131
+ lastRead2 = reader2.read();
3132
+ }
3133
+ const result = await lastRead2;
3134
+ lastRead2 = void 0;
3135
+ if (!result.done) {
3136
+ controller.enqueue(result.value);
3137
+ } else {
3138
+ controller.close();
3139
+ }
3140
+ } catch (error) {
3141
+ controller.error(error);
3142
+ }
3143
+ }
3144
+ return new ReadableStream({
3145
+ async pull(controller) {
3146
+ try {
3147
+ if (stream1Done) {
3148
+ readStream2(controller);
3149
+ return;
3150
+ }
3151
+ if (stream2Done) {
3152
+ readStream1(controller);
3153
+ return;
3154
+ }
3155
+ if (lastRead1 == null) {
3156
+ lastRead1 = reader1.read();
3157
+ }
3158
+ if (lastRead2 == null) {
3159
+ lastRead2 = reader2.read();
3160
+ }
3161
+ const { result, reader } = await Promise.race([
3162
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
3163
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
3164
+ ]);
3165
+ if (!result.done) {
3166
+ controller.enqueue(result.value);
3167
+ }
3168
+ if (reader === reader1) {
3169
+ lastRead1 = void 0;
3170
+ if (result.done) {
3171
+ readStream2(controller);
3172
+ stream1Done = true;
3173
+ }
3174
+ } else {
3175
+ lastRead2 = void 0;
3176
+ if (result.done) {
3177
+ stream2Done = true;
3178
+ readStream1(controller);
3179
+ }
3180
+ }
3181
+ } catch (error) {
3182
+ controller.error(error);
3183
+ }
3184
+ },
3185
+ cancel() {
3186
+ reader1.cancel();
3187
+ reader2.cancel();
3188
+ }
3189
+ });
3190
+ }
3191
+
3129
3192
  // shared/parse-complex-response.ts
3130
3193
  function assignAnnotationsToMessage(message, annotations) {
3131
3194
  if (!message || !annotations || !annotations.length)
@@ -3280,7 +3343,7 @@ var experimental_StreamingReactResponse = class {
3280
3343
  let next = new Promise((resolve) => {
3281
3344
  resolveFunc = resolve;
3282
3345
  });
3283
- const processedStream = (options == null ? void 0 : options.data) != null ? res.pipeThrough((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream) : res;
3346
+ const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
3284
3347
  let lastPayload = void 0;
3285
3348
  parseComplexResponse({
3286
3349
  reader: processedStream.getReader(),
@@ -3318,7 +3381,7 @@ var StreamingTextResponse = class extends Response {
3318
3381
  constructor(res, init, data) {
3319
3382
  let processedStream = res;
3320
3383
  if (data) {
3321
- processedStream = res.pipeThrough(data.stream);
3384
+ processedStream = mergeStreams(data.stream, res);
3322
3385
  }
3323
3386
  super(processedStream, {
3324
3387
  ...init,