tidaldb/applications/iknowyou/lib/sse.ts
jordan 51ac377376 refactor: extract shared SSE parser and add eslint to iknowyou
Deduplicate SSE chunk parsing from input-bar.tsx and vllm.ts into a
shared lib/sse.ts consumeSSEChunk helper. Add eslint + next lint config.
Silence monorepo lockfile warning via outputFileTracingRoot.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-25 16:51:48 -07:00

28 lines
900 B
TypeScript

/**
* Append a decoded SSE chunk to the running buffer and extract complete data lines.
*
* Returns the raw JSON strings from each `data: {...}` line and the updated
* buffer (partial-line remainder). Skips the `[DONE]` sentinel.
*
* Both the vLLM server-side reader and the client-side chat reader share
* this logic — keeping buffer management in one place prevents drift when
* the SSE wire format changes.
*/
export function consumeSSEChunk(
buffer: string,
chunk: string
): { jsonLines: string[]; buffer: string } {
const full = buffer + chunk;
const parts = full.split("\n");
const newBuffer = parts.pop()!;
const jsonLines: string[] = [];
for (const part of parts) {
const trimmed = part.trim();
if (!trimmed.startsWith("data: ") || trimmed === "data: [DONE]") continue;
jsonLines.push(trimmed.slice(6));
}
return { jsonLines, buffer: newBuffer };
}