Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(llm-chat): check for the end chunk after the SSE read loop (closes #10883)
Elian Dorana1aee1047f2c
diff --git a/apps/client/src/services/llm_chat.spec.ts b/apps/client/src/services/llm_chat.spec.tsindex 6be5a3b315..e1ecf0ef12 100644--- a/apps/client/src/services/llm_chat.spec.ts+++ b/apps/client/src/services/llm_chat.spec.ts@@ -233,9 +233,7 @@ describe("streamChatCompletion", () => {expect(cb.onChunk).toHaveBeenCalledWith("hi");});- it("does not invoke onDone when the stream ends without an explicit done event", async () => {- // onDone() fires only for a `{type:"done"}` SSE event (source 126-128), NOT when the- // reader simply returns {done:true}. Lock in that a clean stream end leaves onDone uncalled.+ it("reports an interruption when the body ends without a done or error event (#10883)", async () => {const chunks = [`data: ${JSON.stringify({ type: "text", content: "only" })}\n`];vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));@@ -244,6 +242,9 @@ describe("streamChatCompletion", () => {expect(cb.onChunk).toHaveBeenCalledWith("only");expect(cb.onDone).not.toHaveBeenCalled();+ expect(cb.onError.mock.calls).toEqual([[+ "LLM stream ended without completing — the connection closed before the response finished (network or proxy interruption)."+ ]]);});it("ignores citation/usage events that carry no payload", async () => {@@ -275,34 +276,6 @@ describe("streamChatCompletion", () => {]);});-- it("reports an interruption when the body ends without a terminal chunk (#10883)", async () => {- // A proxy can close an SSE body cleanly: end-of-stream without the- // server's done chunk is a dropped response, not a completion.- const chunks = [`data: ${JSON.stringify({ type: "text", content: "partial" })}-`];- vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));-- const cb = makeCallbacks();- await streamChatCompletion(messages, config, cb);- expect(cb.onChunk).toHaveBeenCalledWith("partial");- expect(cb.onDone).not.toHaveBeenCalled();- expect(cb.onError).toHaveBeenCalledWith(- "LLM stream ended without completing — the connection closed before the response finished (network or proxy interruption)."- );- });-- it("does not stack the missing-terminal error on top of a stream error chunk", async () => {- const chunks = [`data: ${JSON.stringify({ type: "error", error: "model failed" })}-`];- vi.stubGlobal("fetch", vi.fn(async () => makeStreamResponse(chunks)));-- const cb = makeCallbacks();- await streamChatCompletion(messages, config, cb);- expect(cb.onError).toHaveBeenCalledTimes(1);- expect(cb.onError).toHaveBeenCalledWith("model failed", undefined);- });-it("skips optional callbacks that are not provided", async () => {const events = [{ type: "thinking", content: "TH" },@@ -353,7 +326,7 @@ describe("streamChatCompletion", () => {const cb = makeCallbacks();await streamChatCompletion(messages, config, cb);- expect(cb.onError).toHaveBeenCalledWith("LLM stream interrupted: read failed");+ expect(cb.onError.mock.calls).toEqual([["LLM stream interrupted: read failed"]]);expect(reader.releaseLock).toHaveBeenCalledTimes(1);});@@ -364,7 +337,7 @@ describe("streamChatCompletion", () => {const cb = makeCallbacks();await streamChatCompletion(messages, config, cb);- expect(cb.onError).toHaveBeenCalledWith("LLM stream interrupted: kaput");+ expect(cb.onError.mock.calls).toEqual([["LLM stream interrupted: kaput"]]);});it("passes the abort signal through to fetch", async () => {diff --git a/apps/client/src/services/llm_chat.ts b/apps/client/src/services/llm_chat.tsindex f63b1381af..835d3f8fc9 100644--- a/apps/client/src/services/llm_chat.ts+++ b/apps/client/src/services/llm_chat.ts@@ -166,13 +166,9 @@ async function streamChatOverSse(const decoder = new TextDecoder();let buffer = "";- // Why (#10883): a proxy or flaky VPN can close the SSE body *cleanly*, which- // reads as end-of-stream here even though the server never sent its terminal- // chunk. Treating a bare body end as completion returns the chat to idle with- // no error anywhere — output silently dropped. Only an explicit done/error- // chunk completes the stream; anything else is an interruption.- let terminalChunk: "done" | "error" | null = null;- let aborted = false;+ // Only a `done` or `error` chunk ends a turn: a proxy can close the body cleanly+ // before the server sends either.+ let completed = false;try {while (true) {@@ -186,32 +182,30 @@ async function streamChatOverSse(for (const line of lines) {if (line.startsWith("data: ")) {try {- const terminal = await handleChunk(streamId, JSON.parse(line.slice(6)), callbacks);- if (terminal) {- terminalChunk = terminal;+ const chunk: LlmStreamChunk = JSON.parse(line.slice(6));+ if (chunk.type === "done" || chunk.type === "error") {+ completed = true;}+ await handleChunk(streamId, chunk, callbacks);} catch (e) {console.error("Failed to parse SSE data line:", line, e);}}}}++ if (!completed) {+ callbacks.onError(+ "LLM stream ended without completing — the connection closed before the response finished (network or proxy interruption)."+ );+ }} catch (e) {if (e instanceof DOMException && e.name === "AbortError") {- aborted = true;throw e;}const message = e instanceof Error ? e.message : String(e);callbacks.onError(`LLM stream interrupted: ${message}`);- // An interruption error was already reported; don't stack the- // missing-terminal message on top of it.- terminalChunk = "error";} finally {- if (!aborted && terminalChunk === null) {- callbacks.onError(- "LLM stream ended without completing — the connection closed before the response finished (network or proxy interruption)."- );- }reader.releaseLock();}}@@ -300,12 +294,11 @@ function rejectWhenAborted(signal?: AbortSignal): Promise<never> {* Dispatch one chunk to the callbacks. Shared by both transports, so a chunk means* the same thing however it arrived.*/-/** Returns the stream's terminal chunk type when this chunk ends it, else null. */async function handleChunk(streamId: string,chunk: LlmStreamChunk,callbacks: StreamCallbacks-): Promise<"done" | "error" | null> {+): Promise<void> {switch (chunk.type) {case "status":callbacks.onStatus?.(chunk.status);@@ -356,10 +349,9 @@ async function handleChunk(break;case "error":callbacks.onError(chunk.error, chunk.errorDetails);- return "error";+ break;case "done":callbacks.onDone();- return "done";+ break;}- return null;}diff --git a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.tsindex 1490209d7a..6c3b756a59 100644--- a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts+++ b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts@@ -75,7 +75,7 @@ export default function buildAiAssistantStream(getNoteLocation?: AiNoteLocationPonError: (error) => reject(new Error(error)),onDone: () => resolve(reported)}, signal).then(- // A stream that ends without a "done" event (connection dropped) still settles.+ // Settles even when the stream returns without calling `onDone` or `onError`.() => resolve(reported),reject);