ai 3.1.10 → 3.1.12

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
@@ -2198,86 +2198,61 @@ var StreamData = class {
2198
2198
  constructor() {
2199
2199
  this.encoder = new TextEncoder();
2200
2200
  this.controller = null;
2201
- // closing the stream is synchronous, but we want to return a promise
2202
- // in case we're doing async work
2203
- this.isClosedPromise = null;
2204
- this.isClosedPromiseResolver = void 0;
2205
2201
  this.isClosed = false;
2206
- // array to store appended data
2207
- this.data = [];
2208
- this.messageAnnotations = [];
2209
- this.isClosedPromise = new Promise((resolve) => {
2210
- this.isClosedPromiseResolver = resolve;
2211
- });
2202
+ this.warningTimeout = null;
2212
2203
  const self = this;
2213
- this.stream = new TransformStream({
2204
+ this.stream = new ReadableStream({
2214
2205
  start: async (controller) => {
2215
2206
  self.controller = controller;
2216
- },
2217
- transform: async (chunk, controller) => {
2218
- if (self.data.length > 0) {
2219
- const encodedData = self.encoder.encode(
2220
- formatStreamPart("data", self.data)
2221
- );
2222
- self.data = [];
2223
- controller.enqueue(encodedData);
2224
- }
2225
- if (self.messageAnnotations.length) {
2226
- const encodedMessageAnnotations = self.encoder.encode(
2227
- formatStreamPart("message_annotations", self.messageAnnotations)
2228
- );
2229
- self.messageAnnotations = [];
2230
- controller.enqueue(encodedMessageAnnotations);
2207
+ if (process.env.NODE_ENV === "development") {
2208
+ self.warningTimeout = setTimeout(() => {
2209
+ console.warn(
2210
+ "The data stream is hanging. Did you forget to close it with `data.close()`?"
2211
+ );
2212
+ }, 3e3);
2231
2213
  }
2232
- controller.enqueue(chunk);
2233
2214
  },
2234
- async flush(controller) {
2235
- const warningTimeout = process.env.NODE_ENV === "development" ? setTimeout(() => {
2236
- console.warn(
2237
- "The data stream is hanging. Did you forget to close it with `data.close()`?"
2238
- );
2239
- }, 3e3) : null;
2240
- await self.isClosedPromise;
2241
- if (warningTimeout !== null) {
2242
- clearTimeout(warningTimeout);
2243
- }
2244
- if (self.data.length) {
2245
- const encodedData = self.encoder.encode(
2246
- formatStreamPart("data", self.data)
2247
- );
2248
- controller.enqueue(encodedData);
2249
- }
2250
- if (self.messageAnnotations.length) {
2251
- const encodedData = self.encoder.encode(
2252
- formatStreamPart("message_annotations", self.messageAnnotations)
2253
- );
2254
- controller.enqueue(encodedData);
2255
- }
2215
+ pull: (controller) => {
2216
+ },
2217
+ cancel: (reason) => {
2218
+ this.isClosed = true;
2256
2219
  }
2257
2220
  });
2258
2221
  }
2259
2222
  async close() {
2260
- var _a;
2261
2223
  if (this.isClosed) {
2262
2224
  throw new Error("Data Stream has already been closed.");
2263
2225
  }
2264
2226
  if (!this.controller) {
2265
2227
  throw new Error("Stream controller is not initialized.");
2266
2228
  }
2267
- (_a = this.isClosedPromiseResolver) == null ? void 0 : _a.call(this);
2229
+ this.controller.close();
2268
2230
  this.isClosed = true;
2231
+ if (this.warningTimeout) {
2232
+ clearTimeout(this.warningTimeout);
2233
+ }
2269
2234
  }
2270
2235
  append(value) {
2271
2236
  if (this.isClosed) {
2272
2237
  throw new Error("Data Stream has already been closed.");
2273
2238
  }
2274
- this.data.push(value);
2239
+ if (!this.controller) {
2240
+ throw new Error("Stream controller is not initialized.");
2241
+ }
2242
+ this.controller.enqueue(
2243
+ this.encoder.encode(formatStreamPart("data", [value]))
2244
+ );
2275
2245
  }
2276
2246
  appendMessageAnnotation(value) {
2277
2247
  if (this.isClosed) {
2278
2248
  throw new Error("Data Stream has already been closed.");
2279
2249
  }
2280
- this.messageAnnotations.push(value);
2250
+ if (!this.controller) {
2251
+ throw new Error("Stream controller is not initialized.");
2252
+ }
2253
+ this.controller.enqueue(
2254
+ this.encoder.encode(formatStreamPart("message_annotations", [value]))
2255
+ );
2281
2256
  }
2282
2257
  };
2283
2258
  function createStreamDataTransformer() {
@@ -3051,6 +3026,119 @@ async function ReplicateStream(res, cb, options) {
3051
3026
  );
3052
3027
  }
3053
3028
 
3029
+ // core/util/merge-streams.ts
3030
+ function mergeStreams(stream1, stream2) {
3031
+ const reader1 = stream1.getReader();
3032
+ const reader2 = stream2.getReader();
3033
+ let lastRead1 = void 0;
3034
+ let lastRead2 = void 0;
3035
+ let stream1Done = false;
3036
+ let stream2Done = false;
3037
+ async function readStream1(controller) {
3038
+ try {
3039
+ if (lastRead1 == null) {
3040
+ lastRead1 = reader1.read();
3041
+ }
3042
+ const result = await lastRead1;
3043
+ lastRead1 = void 0;
3044
+ if (!result.done) {
3045
+ controller.enqueue(result.value);
3046
+ } else {
3047
+ controller.close();
3048
+ }
3049
+ } catch (error) {
3050
+ controller.error(error);
3051
+ }
3052
+ }
3053
+ async function readStream2(controller) {
3054
+ try {
3055
+ if (lastRead2 == null) {
3056
+ lastRead2 = reader2.read();
3057
+ }
3058
+ const result = await lastRead2;
3059
+ lastRead2 = void 0;
3060
+ if (!result.done) {
3061
+ controller.enqueue(result.value);
3062
+ } else {
3063
+ controller.close();
3064
+ }
3065
+ } catch (error) {
3066
+ controller.error(error);
3067
+ }
3068
+ }
3069
+ return new ReadableStream({
3070
+ async pull(controller) {
3071
+ try {
3072
+ if (stream1Done) {
3073
+ readStream2(controller);
3074
+ return;
3075
+ }
3076
+ if (stream2Done) {
3077
+ readStream1(controller);
3078
+ return;
3079
+ }
3080
+ if (lastRead1 == null) {
3081
+ lastRead1 = reader1.read();
3082
+ }
3083
+ if (lastRead2 == null) {
3084
+ lastRead2 = reader2.read();
3085
+ }
3086
+ const { result, reader } = await Promise.race([
3087
+ lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
3088
+ lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
3089
+ ]);
3090
+ if (!result.done) {
3091
+ controller.enqueue(result.value);
3092
+ }
3093
+ if (reader === reader1) {
3094
+ lastRead1 = void 0;
3095
+ if (result.done) {
3096
+ readStream2(controller);
3097
+ stream1Done = true;
3098
+ }
3099
+ } else {
3100
+ lastRead2 = void 0;
3101
+ if (result.done) {
3102
+ stream2Done = true;
3103
+ readStream1(controller);
3104
+ }
3105
+ }
3106
+ } catch (error) {
3107
+ controller.error(error);
3108
+ }
3109
+ },
3110
+ cancel() {
3111
+ reader1.cancel();
3112
+ reader2.cancel();
3113
+ }
3114
+ });
3115
+ }
3116
+
3117
+ // streams/stream-to-response.ts
3118
+ function streamToResponse(res, response, init, data) {
3119
+ var _a;
3120
+ response.writeHead((_a = init == null ? void 0 : init.status) != null ? _a : 200, {
3121
+ "Content-Type": "text/plain; charset=utf-8",
3122
+ ...init == null ? void 0 : init.headers
3123
+ });
3124
+ let processedStream = res;
3125
+ if (data) {
3126
+ processedStream = mergeStreams(data.stream, res);
3127
+ }
3128
+ const reader = processedStream.getReader();
3129
+ function read() {
3130
+ reader.read().then(({ done, value }) => {
3131
+ if (done) {
3132
+ response.end();
3133
+ return;
3134
+ }
3135
+ response.write(value);
3136
+ read();
3137
+ });
3138
+ }
3139
+ read();
3140
+ }
3141
+
3054
3142
  // shared/parse-complex-response.ts
3055
3143
  function assignAnnotationsToMessage(message, annotations) {
3056
3144
  if (!message || !annotations || !annotations.length)
@@ -3205,7 +3293,7 @@ var experimental_StreamingReactResponse = class {
3205
3293
  let next = new Promise((resolve) => {
3206
3294
  resolveFunc = resolve;
3207
3295
  });
3208
- const processedStream = (options == null ? void 0 : options.data) != null ? res.pipeThrough((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream) : res;
3296
+ const processedStream = (options == null ? void 0 : options.data) != null ? mergeStreams((_a = options == null ? void 0 : options.data) == null ? void 0 : _a.stream, res) : res;
3209
3297
  let lastPayload = void 0;
3210
3298
  parseComplexResponse({
3211
3299
  reader: processedStream.getReader(),
@@ -3243,7 +3331,7 @@ var StreamingTextResponse = class extends Response {
3243
3331
  constructor(res, init, data) {
3244
3332
  let processedStream = res;
3245
3333
  if (data) {
3246
- processedStream = res.pipeThrough(data.stream);
3334
+ processedStream = mergeStreams(data.stream, res);
3247
3335
  }
3248
3336
  super(processedStream, {
3249
3337
  ...init,
@@ -3255,24 +3343,6 @@ var StreamingTextResponse = class extends Response {
3255
3343
  });
3256
3344
  }
3257
3345
  };
3258
- function streamToResponse(res, response, init) {
3259
- response.writeHead((init == null ? void 0 : init.status) || 200, {
3260
- "Content-Type": "text/plain; charset=utf-8",
3261
- ...init == null ? void 0 : init.headers
3262
- });
3263
- const reader = res.getReader();
3264
- function read() {
3265
- reader.read().then(({ done, value }) => {
3266
- if (done) {
3267
- response.end();
3268
- return;
3269
- }
3270
- response.write(value);
3271
- read();
3272
- });
3273
- }
3274
- read();
3275
- }
3276
3346
  export {
3277
3347
  AIStream,
3278
3348
  APICallError2 as APICallError,