import type { ChatRequestBody, ChatStreamEvent } from "@/types/llm-chat"; /** * Open a streaming chat session against `/api/v1/llm/chat` and yield each * decoded `ChatStreamEvent` via an async generator. The caller is expected * to pipe events into UI state. * * The underlying fetch is cancellable: the supplied `signal` is forwarded to * fetch, which in turn triggers server-side upstream cancellation via the * route handler's abort plumbing. */ export async function* streamChat( body: ChatRequestBody, signal?: AbortSignal, ): AsyncGenerator { const resp = await fetch("/api/v1/llm/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), signal, }); if (!resp.ok || !resp.body) { // Error envelope path (pre-stream validation failure). let message = `Chat request failed with status ${resp.status}`; let code = "STREAM_ERROR"; try { const err = await resp.json(); message = err.message ?? message; code = err.code ?? code; } catch { // non-JSON body, keep defaults } yield { type: "error", code, message }; return; } const reader = resp.body.getReader(); const decoder = new TextDecoder("utf-8"); let buffer = ""; while (true) { const { value, done } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); let terminatorIdx = buffer.indexOf("\n\n"); while (terminatorIdx !== -1) { const frame = buffer.slice(0, terminatorIdx); buffer = buffer.slice(terminatorIdx + 2); const event = parseFrame(frame); if (event) yield event; terminatorIdx = buffer.indexOf("\n\n"); } } // Flush any trailing frame without a terminator (unusual but defensive). if (buffer.trim()) { const event = parseFrame(buffer); if (event) yield event; } } function parseFrame(frame: string): ChatStreamEvent | null { const lines = frame.split("\n"); const dataParts: string[] = []; for (const line of lines) { if (!line || line.startsWith(":")) continue; if (line.startsWith("data:")) { dataParts.push(line.slice(5).trimStart()); } } if (dataParts.length === 0) return null; try { return JSON.parse(dataParts.join("\n")) as ChatStreamEvent; } catch { return null; } }