function parseSseField(line) {
    const colonIndex = line.indexOf(":");
    if (colonIndex === -1)
        return { name: line, value: "" };
    const value = line.slice(colonIndex + 1);
    return {
        name: line.slice(0, colonIndex),
        value: value.startsWith(" ") ? value.slice(1) : value,
    };
}
export async function* parseSseStream(body) {
    const reader = body.getReader();
    const decoder = new TextDecoder();
    let buffer = "";
    let currentEvent = "message";
    let dataLines = [];
    const flush = () => {
        const data = dataLines.join("\n");
        const event = currentEvent || "message";
        currentEvent = "message";
        dataLines = [];
        return data ? { event, data } : null;
    };
    const processLine = (line) => {
        if (line === "")
            return flush();
        if (line.startsWith(":")) {
            const value = line.slice(1);
            return {
                event: "__comment__",
                data: (value.startsWith(" ") ? value.slice(1) : value).trim(),
            };
        }
        const field = parseSseField(line);
        if (field.name === "event") {
            currentEvent = field.value.trim() || "message";
        }
        else if (field.name === "data") {
            dataLines.push(field.value);
        }
        return null;
    };
    try {
        while (true) {
            const { value, done } = await reader.read();
            if (done)
                break;
            buffer += decoder.decode(value, { stream: true });
            while (true) {
                const newlineIndex = buffer.indexOf("\n");
                if (newlineIndex === -1)
                    break;
                const line = buffer.slice(0, newlineIndex).replace(/\r$/, "");
                buffer = buffer.slice(newlineIndex + 1);
                const message = processLine(line);
                if (message)
                    yield message;
            }
        }
        buffer += decoder.decode();
        if (buffer) {
            const message = processLine(buffer.replace(/\r$/, ""));
            if (message)
                yield message;
        }
        const message = flush();
        if (message)
            yield message;
    }
    finally {
        reader.releaseLock();
    }
}
export function encodeSseEvent(event) {
    return `event: ${event.event}\ndata: ${JSON.stringify(event.data)}\n\n`;
}
export function parseSseEvent(message) {
    switch (message.event) {
        case "meta":
            return { event: "meta", data: JSON.parse(message.data) };
        case "slides":
            return { event: "slides", data: JSON.parse(message.data) };
        case "status":
            return { event: "status", data: JSON.parse(message.data) };
        case "chunk":
            return { event: "chunk", data: JSON.parse(message.data) };
        case "assistant":
            return { event: "assistant", data: JSON.parse(message.data) };
        case "metrics":
            return { event: "metrics", data: JSON.parse(message.data) };
        case "done":
            return { event: "done", data: JSON.parse(message.data) };
        case "error":
            return { event: "error", data: JSON.parse(message.data) };
        default:
            return null;
    }
}
//# sourceMappingURL=sse-events.js.map