| 49 | import type { ExecutionContext, StreamingContext } from '@/lib/copilot/request/types' |
| 50 | |
| 51 | function createSseResponse(events: unknown[]): Response { |
| 52 | const payload = events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join('') |
| 53 | |
| 54 | return new Response( |
| 55 | new ReadableStream<Uint8Array>({ |
| 56 | start(controller) { |
| 57 | controller.enqueue(new TextEncoder().encode(payload)) |
| 58 | controller.close() |
| 59 | }, |
| 60 | }), |
| 61 | { |
| 62 | status: 200, |
| 63 | headers: { |
| 64 | 'Content-Type': 'text/event-stream', |
| 65 | }, |
| 66 | } |
| 67 | ) |
| 68 | } |
| 69 | |
| 70 | function createRawSseResponse(payload: string): Response { |
| 71 | return new Response( |