Files
Netcatty/electron/bridges/ai/sseDataLine.cjs
T
bincxz 4291df8942 fix(ai): accept SSE data lines without a space after the colon
Older AxonHub 0.9 and some intranet OpenAI-compat proxies emit
`data:{json}` instead of `data: {json}`. The stream parser required
the space, so chat chunks were dropped while the model-list probe
still succeeded.

Parse `data:` per the EventSource spec (optional single space) and
cover both the line and end-of-stream flush paths.

Closes #3020
2026-08-17 17:29:09 +08:00

20 lines
703 B
JavaScript

/**
* Extract the payload of an SSE `data:` field.
*
* The WHATWG EventSource spec treats the space after the colon as optional
* and strips at most one leading U+0020. Older AxonHub (0.9) and some
* intranet OpenAI-compat proxies emit `data:{json}` with no space, which
* a `data: ` prefix check silently drops (issue #3020).
*
* @param {unknown} line
* @returns {string | null}
*/
function extractSseDataPayload(line) {
const trimmed = typeof line === "string" ? line.trim() : "";
if (!trimmed.startsWith("data:")) return null;
const payload = trimmed.slice("data:".length);
return payload.startsWith(" ") ? payload.slice(1) : payload;
}
module.exports = { extractSseDataPayload };