const WHISPER_URL = process.env.WHISPER_URL ?? 'http://localhost:8080'; /** Relay the whisper /model/events SSE stream to the browser. */ export async function GET({ request }) { const { default: fetch } = await import('node-fetch'); const ac = new AbortController(); request.signal.addEventListener('abort', () => ac.abort()); const stream = new ReadableStream({ async start(controller) { try { const upstream = await fetch(`${WHISPER_URL}/model/events`, { signal: ac.signal as AbortSignal }); if (!upstream.body) { controller.close(); return; } for await (const chunk of upstream.body) { if (ac.signal.aborted) break; controller.enqueue(chunk instanceof Buffer ? chunk : Buffer.from(String(chunk))); } } catch { // upstream closed, client disconnected, or whisper unreachable — all fine } finally { controller.close(); } }, cancel() { ac.abort(); } }); return new Response(stream, { headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive', 'X-Accel-Buffering': 'no' } }); }