MCPcopy Create free account
hub / github.com/CommandCodeAI/BaseAI / toReadableStream

Method toReadableStream

packages/core/src/common/stream.ts:180–205  ·  view source on GitHub ↗

* Converts this stream to a newline-separated ReadableStream of * JSON stringified values in the stream which can be turned back into a Stream with `Stream.fromReadableStream()`.

()

Source from the content-addressed store, hash-verified

178 * JSON stringified values in the stream which can be turned back into a Stream with `Stream.fromReadableStream()`.
179 */
180 toReadableStream(): ReadableStream {
181 const self = this;
182 let iter: AsyncIterator<Item>;
183 const encoder = new TextEncoder();
184
185 return new ReadableStream({
186 async start() {
187 iter = self[Symbol.asyncIterator]();
188 },
189 async pull(ctrl: any) {
190 try {
191 const {value, done} = await iter.next();
192 if (done) return ctrl.close();
193
194 const bytes = encoder.encode(JSON.stringify(value) + '\n');
195
196 ctrl.enqueue(bytes);
197 } catch (err) {
198 ctrl.error(err);
199 }
200 },
201 async cancel() {
202 await iter.return?.();
203 },
204 });
205 }
206}
207
208/**

Callers 1

handleResponseStreamFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected