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

Function writeCohereSSEStream

src/cohere.ts:865–899  ·  view source on GitHub ↗
(
  res: http.ServerResponse,
  events: CohereSSEEvent[],
  optionsOrLatency?: number | CohereStreamOptions,
)

Source from the content-addressed store, hash-verified

863}
864
865async function writeCohereSSEStream(
866 res: http.ServerResponse,
867 events: CohereSSEEvent[],
868 optionsOrLatency?: number | CohereStreamOptions,
869): Promise<boolean> {
870 const opts: CohereStreamOptions =
871 typeof optionsOrLatency === "number" ? { latency: optionsOrLatency } : (optionsOrLatency ?? {});
872 const latency = opts.latency ?? 0;
873 const profile = opts.streamingProfile;
874 const { recordedTimings, replaySpeed } = opts;
875 const signal = opts.signal;
876 const onChunkSent = opts.onChunkSent;
877
878 if (res.writableEnded) return true;
879 res.setHeader("Content-Type", "text/event-stream");
880 res.setHeader("Cache-Control", "no-cache");
881 res.setHeader("Connection", "keep-alive");
882
883 let chunkIndex = 0;
884 for (const event of events) {
885 const chunkDelay = calculateDelay(chunkIndex, profile, latency, recordedTimings, replaySpeed);
886 if (chunkDelay > 0) await delay(chunkDelay, signal);
887 if (signal?.aborted) return false;
888 if (res.writableEnded) return true;
889 res.write(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`);
890 onChunkSent?.();
891 if (signal?.aborted) return false;
892 chunkIndex++;
893 }
894
895 if (!res.writableEnded) {
896 res.end();
897 }
898 return true;
899}
900
901// ─── Request handler ────────────────────────────────────────────────────────
902

Callers 1

handleCohereFunction · 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…