Skip to content

Build your personal knowledge base with Trilium Notes

refactor(llm): register the chat stream routes only where they are served

Elian Dorana905cc92ded5
diff --git a/apps/standalone/src/lightweight/browser_routes.spec.ts b/apps/standalone/src/lightweight/browser_routes.spec.ts
index 9896c4a76c..c6a0e90db2 100644
--- a/apps/standalone/src/lightweight/browser_routes.spec.ts
+++ b/apps/standalone/src/lightweight/browser_routes.spec.ts
@@ -38,6 +38,20 @@ describe("registerRoutes (real wiring)", () => {
expect(text(res.body)).toContain("provider is required");
});
+ // Streaming is registered here rather than in the shared table, so nothing
+ // else would catch its loss. Rejected for a missing stream id, which the
+ // handler checks before starting anything — no completion is begun.
+ it("serves the LLM stream routes, which only this runtime has", async () => {
+ const start = await router.dispatch("POST", "http://localhost/api/llm-chat/stream-start", {});
+ expect(start.status).toBe(400);
+ expect(text(start.body)).toContain("streamId is required");
+
+ // Aborting an id that is not running is a no-op rather than an error, so
+ // reaching a 200 here is what proves the route is wired at all.
+ const abort = await router.dispatch("POST", "http://localhost/api/llm-chat/stream-abort", { streamId: "nope" });
+ expect(abort.status).toBe(200);
+ });
+
it("serves the compatibility dummy routes", async () => {
expect(parseJson((await router.dispatch("GET", "http://localhost/api/script/widgets")).body)).toEqual([]);
expect(parseJson((await router.dispatch("GET", "http://localhost/api/script/startup")).body)).toEqual([]);
diff --git a/apps/standalone/src/lightweight/browser_routes.ts b/apps/standalone/src/lightweight/browser_routes.ts
index 79ef40ef2d..ff2be3f160 100644
--- a/apps/standalone/src/lightweight/browser_routes.ts
+++ b/apps/standalone/src/lightweight/browser_routes.ts
@@ -5,6 +5,7 @@
import { BootstrapDefinition } from '@triliumnext/commons';
import { entity_changes, getContext, getPlatform, getSharedBootstrapItems, getSql, routes, sql_init } from '@triliumnext/core';
+import llmRoute from '@triliumnext/core/src/routes/api/llm.js';
import packageJson from '../../package.json' with { type: 'json' };
import { type BrowserRequest, BrowserRouter } from './browser_router';
@@ -297,6 +298,16 @@ export function registerRoutes(router: BrowserRouter): void {
});
apiRoute('get', '/bootstrap', bootstrapRoute);
+ // Streaming a chat, in the only form this runtime can serve it: the request
+ // starts the completion and returns, and the chunks arrive over the
+ // WebSocket-style channel (see core's routes/api/llm.ts). It is registered
+ // here rather than in the shared table because the server and the desktop app
+ // answer `/api/llm-chat/stream` with Server-Sent Events instead — they can
+ // hold a response open, and it delivers the chunks to the one client that
+ // asked rather than broadcasting them to every device signed in.
+ apiRoute('post', '/api/llm-chat/stream-start', llmRoute.startChatStream);
+ apiRoute('post', '/api/llm-chat/stream-abort', llmRoute.abortChatStream);
+
// Dummy routes for compatibility.
apiRoute("get", "/api/script/widgets", () => []);
apiRoute("get", "/api/script/startup", () => []);
diff --git a/packages/trilium-core/src/routes/api/llm.ts b/packages/trilium-core/src/routes/api/llm.ts
index 0c102afbb9..dc4dfa6e50 100644
--- a/packages/trilium-core/src/routes/api/llm.ts
+++ b/packages/trilium-core/src/routes/api/llm.ts
@@ -1,11 +1,14 @@
/**
- * LLM endpoints that every runtime can serve.
+ * The LLM endpoints' handlers. Where each is *registered* differs by runtime, so
+ * only {@link getProviderModels} is in the shared route table.
*
- * Streaming over Server-Sent Events is not among them — the browser build's
- * request bridge answers a request with one buffered body and cannot carry a
- * response that stays open. That route stays in `apps/server`; the equivalent
- * here starts a completion that reports itself over the WebSocket-style channel,
- * which every runtime already has.
+ * Streaming has no shared form. The server and the desktop app answer
+ * `/api/llm-chat/stream` with Server-Sent Events (that route lives in
+ * `apps/server`, and the desktop's custom-protocol bridge streams it too), which
+ * delivers to the one client that asked. The browser build cannot: its request
+ * bridge answers with a single buffered body and cannot hold a response open, so
+ * it registers {@link startChatStream} instead — see `apps/standalone`'s
+ * browser_routes.ts — and the chunks come back over the WebSocket-style channel.
*/
import type { LlmMessage } from "@triliumnext/commons";
diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.ts
index 781c2c7249..3453116c50 100644
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@ -207,9 +207,11 @@ export function buildSharedApiRoutes({ route, asyncRoute, asyncRouteWithoutTrans
apiRoute(GET, "/api/search/:searchString", searchRoute.search);
apiRoute(GET, "/api/search-templates", searchRoute.searchTemplates);
+ // Streaming a chat is not here — it has no single form every runtime can serve.
+ // The server and the desktop app answer `/api/llm-chat/stream` with Server-Sent
+ // Events; standalone, whose bridge cannot hold a response open, registers
+ // `llmRoute.startChatStream` itself. See `apps/standalone`'s browser_routes.ts.
asyncApiRoute(PST, "/api/llm-chat/provider-models", llmRoute.getProviderModels);
- apiRoute(PST, "/api/llm-chat/stream-start", llmRoute.startChatStream);
- apiRoute(PST, "/api/llm-chat/stream-abort", llmRoute.abortChatStream);
apiRoute(GET, "/api/autocomplete", autocompleteApiRoute.getAutocomplete);
apiRoute(GET, "/api/autocomplete/notesCount", autocompleteApiRoute.getNotesCount);