( res: http.ServerResponse, events: CohereSSEEvent[], optionsOrLatency?: number | CohereStreamOptions, )
| 863 | } |
| 864 | |
| 865 | async 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 |
no test coverage detected
searching dependent graphs…