MCPcopy Create free account
hub / github.com/CopilotKit/aimock / writeResponsesSSEStream

Function writeResponsesSSEStream

src/responses.ts:1008–1042  ·  view source on GitHub ↗
(
  res: http.ServerResponse,
  events: ResponsesSSEEvent[],
  optionsOrLatency?: number | ResponsesStreamOptions,
)

Source from the content-addressed store, hash-verified

1006}
1007
1008async function writeResponsesSSEStream(
1009 res: http.ServerResponse,
1010 events: ResponsesSSEEvent[],
1011 optionsOrLatency?: number | ResponsesStreamOptions,
1012): Promise<boolean> {
1013 const opts: ResponsesStreamOptions =
1014 typeof optionsOrLatency === "number" ? { latency: optionsOrLatency } : (optionsOrLatency ?? {});
1015 const latency = opts.latency ?? 0;
1016 const profile = opts.streamingProfile;
1017 const { recordedTimings, replaySpeed } = opts;
1018 const signal = opts.signal;
1019 const onChunkSent = opts.onChunkSent;
1020
1021 if (res.writableEnded) return true;
1022 res.setHeader("Content-Type", "text/event-stream");
1023 res.setHeader("Cache-Control", "no-cache");
1024 res.setHeader("Connection", "keep-alive");
1025
1026 let chunkIndex = 0;
1027 for (const event of events) {
1028 const chunkDelay = calculateDelay(chunkIndex, profile, latency, recordedTimings, replaySpeed);
1029 if (chunkDelay > 0) await delay(chunkDelay, signal);
1030 if (signal?.aborted) return false;
1031 if (res.writableEnded) return true;
1032 res.write(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`);
1033 onChunkSent?.();
1034 if (signal?.aborted) return false;
1035 chunkIndex++;
1036 }
1037
1038 if (!res.writableEnded) {
1039 res.end();
1040 }
1041 return true;
1042}
1043
1044// ─── Request handler ────────────────────────────────────────────────────────
1045

Callers 1

handleResponsesFunction · 0.85

Calls 4

calculateDelayFunction · 0.85
writeMethod · 0.80
endMethod · 0.80
delayFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…