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

Function _iterSSEMessages

packages/core/src/common/stream.ts:216–240  ·  view source on GitHub ↗
(
	response: Response,
	controller: AbortController,
)

Source from the content-addressed store, hash-verified

214 * @throws Error if the response has no body.
215 */
216export async function* _iterSSEMessages(
217 response: Response,
218 controller: AbortController,
219): AsyncGenerator<ServerSentEvent, void, unknown> {
220 if (!response.body) {
221 controller.abort();
222 throw new Error(`Attempted to iterate over a response with no body`);
223 }
224
225 const sseDecoder = new SSEDecoder();
226 const lineDecoder = new LineDecoder();
227
228 const iter = readableStreamAsyncIterable<Bytes>(response.body);
229 for await (const sseChunk of iterSSEChunks(iter)) {
230 for (const line of lineDecoder.decode(sseChunk)) {
231 const sse = sseDecoder.decode(line);
232 if (sse) yield sse;
233 }
234 }
235
236 for (const line of lineDecoder.flush()) {
237 const sse = sseDecoder.decode(line);
238 if (sse) yield sse;
239 }
240}
241
242/**
243 * Asynchronously iterates over SSE (Server-Sent Events) chunks and yields the data as Uint8Array.

Callers 1

iteratorMethod · 0.70

Calls 5

decodeMethod · 0.95
decodeMethod · 0.95
flushMethod · 0.95
iterSSEChunksFunction · 0.70

Tested by

no test coverage detected