ai 6.0.266 → 6.0.270

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.
@@ -25,9 +25,14 @@ export function createStitchableStream<T>(): {
25
25
  }> = [];
26
26
  let controller: ReadableStreamDefaultController<T> | null = null;
27
27
  let isClosed = false;
28
+ let isCancelled = false;
28
29
  let waitForNewStream = createResolvablePromise<void>();
29
30
 
30
31
  const terminate = () => {
32
+ if (isCancelled) {
33
+ return;
34
+ }
35
+
31
36
  isClosed = true;
32
37
  waitForNewStream.resolve();
33
38
 
@@ -40,6 +45,10 @@ export function createStitchableStream<T>(): {
40
45
  };
41
46
 
42
47
  const processPull = async () => {
48
+ if (isCancelled) {
49
+ return;
50
+ }
51
+
43
52
  // Case 1: Outer stream is closed and no more inner streams
44
53
  if (isClosed && innerStreams.length === 0) {
45
54
  controller?.close();
@@ -54,8 +63,14 @@ export function createStitchableStream<T>(): {
54
63
  return processPull();
55
64
  }
56
65
 
66
+ const currentStream = innerStreams[0];
67
+
57
68
  try {
58
- const { value, done } = await innerStreams[0].reader.read();
69
+ const { value, done } = await currentStream.reader.read();
70
+
71
+ if (isCancelled) {
72
+ return;
73
+ }
59
74
 
60
75
  if (done) {
61
76
  // Case 3: Current inner stream is done
@@ -73,8 +88,12 @@ export function createStitchableStream<T>(): {
73
88
  controller?.enqueue(value);
74
89
  }
75
90
  } catch (error) {
91
+ if (isCancelled) {
92
+ return;
93
+ }
94
+
76
95
  // Case 5: Current inner stream throws an error
77
- innerStreams[0].onError?.(error);
96
+ currentStream.onError?.(error);
78
97
  controller?.error(error);
79
98
  innerStreams.shift(); // Remove the errored stream
80
99
  terminate(); // we have errored, terminate all streams
@@ -88,12 +107,15 @@ export function createStitchableStream<T>(): {
88
107
  },
89
108
  pull: processPull,
90
109
  async cancel() {
110
+ isCancelled = true;
111
+ isClosed = true;
112
+ waitForNewStream.resolve();
113
+
91
114
  for (const { reader, onCancel } of innerStreams) {
92
115
  onCancel?.();
93
116
  await reader.cancel();
94
117
  }
95
118
  innerStreams = [];
96
- isClosed = true;
97
119
  },
98
120
  }),
99
121
  addStream: (
@@ -103,6 +125,12 @@ export function createStitchableStream<T>(): {
103
125
  onCancel?: () => void;
104
126
  },
105
127
  ) => {
128
+ if (isCancelled) {
129
+ callbacks?.onCancel?.();
130
+ void innerStream.cancel().catch(() => {});
131
+ return;
132
+ }
133
+
106
134
  if (isClosed) {
107
135
  throw new Error('Cannot add inner stream: outer stream is closed');
108
136
  }
@@ -119,6 +147,10 @@ export function createStitchableStream<T>(): {
119
147
  * finish processing and then close the outer stream.
120
148
  */
121
149
  close: () => {
150
+ if (isCancelled) {
151
+ return;
152
+ }
153
+
122
154
  isClosed = true;
123
155
  waitForNewStream.resolve();
124
156