Skip to content

Build your personal knowledge base with Trilium Notes

refactor(llm): make the server's LLM registrations a startup call

Elian Doran2218d68fadf7
diff --git a/apps/server/src/routes/api/llm_chat.spec.ts b/apps/server/src/routes/api/llm_chat.spec.ts
index 04414c9d2f..1f5669ed22 100644
--- a/apps/server/src/routes/api/llm_chat.spec.ts
+++ b/apps/server/src/routes/api/llm_chat.spec.ts
@@ -9,7 +9,7 @@ const state = vi.hoisted(() => ({
// The completion itself is core's and tested there; what this route adds is the
// SSE framing around it, so only the chunk source is faked.
-vi.mock("../../services/llm/index.js", () => ({
+vi.mock("@triliumnext/core/src/services/llm/chat.js", () => ({
async *runChat(_messages: unknown, _config: unknown, signal?: AbortSignal) {
state.signal = signal;
for (const c of state.chunks) yield c;
diff --git a/apps/server/src/routes/api/llm_chat.ts b/apps/server/src/routes/api/llm_chat.ts
index 577179bac4..b4937a6880 100644
--- a/apps/server/src/routes/api/llm_chat.ts
+++ b/apps/server/src/routes/api/llm_chat.ts
@@ -1,8 +1,8 @@
import type { LlmMessage } from "@triliumnext/commons";
+import { runChat } from "@triliumnext/core/src/services/llm/chat.js";
+import type { LlmProviderConfig } from "@triliumnext/core/src/services/llm/types.js";
import type { Request, Response } from "express";
-import { type LlmProviderConfig, runChat } from "../../services/llm/index.js";
-
interface ChatRequest {
messages: LlmMessage[];
config?: LlmProviderConfig;
diff --git a/apps/server/src/services/llm/index.spec.ts b/apps/server/src/services/llm/index.spec.ts
new file mode 100644
index 0000000000..f458eda7a2
--- /dev/null
+++ b/apps/server/src/services/llm/index.spec.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it, vi } from "vitest";
+
+const registrations = vi.hoisted(() => ({
+ claudeAgentFactory: undefined as (() => unknown) | undefined,
+ docNoteReader: undefined as ((note: unknown) => string | null) | undefined,
+ toolRegistries: [] as unknown[]
+}));
+
+vi.mock("@triliumnext/core/src/services/llm/index.js", () => ({
+ registerClaudeAgentProvider: (factory: () => unknown) => { registrations.claudeAgentFactory = factory; }
+}));
+vi.mock("@triliumnext/core/src/services/llm/tools/helpers.js", () => ({
+ registerDocNoteHtmlReader: (reader: (note: unknown) => string | null) => { registrations.docNoteReader = reader; }
+}));
+vi.mock("@triliumnext/core/src/services/llm/tools/index.js", () => ({
+ registerToolRegistry: (registry: unknown) => { registrations.toolRegistries.push(registry); }
+}));
+
+import { registerServerLlmExtensions } from "./index.js";
+import { ClaudeAgentProvider } from "./providers/claude_agent.js";
+import { skillTools } from "./skills/index.js";
+import { helpTools } from "./tools/help_tools.js";
+
+describe("registerServerLlmExtensions", () => {
+ it("hands core every piece of the stack that needs Node", () => {
+ registerServerLlmExtensions();
+
+ // The subscription provider, which shells out to the Claude Code CLI.
+ // Registered as a factory, so nothing is constructed until a chat asks for it.
+ expect(registrations.claudeAgentFactory?.()).toBeInstanceOf(ClaudeAgentProvider);
+ // The User Guide reader, which core's note-content helper calls for doc notes.
+ expect(registrations.docNoteReader).toBeTypeOf("function");
+ // The two tool registries backed by files on disk.
+ expect(registrations.toolRegistries).toEqual([helpTools, skillTools]);
+ });
+});
diff --git a/apps/server/src/services/llm/index.ts b/apps/server/src/services/llm/index.ts
index e108b26a9b..98470ba623 100644
--- a/apps/server/src/services/llm/index.ts
+++ b/apps/server/src/services/llm/index.ts
@@ -1,12 +1,11 @@
/**
- * The server's entry point into the LLM stack.
+ * The server's Node-only contributions to the LLM stack.
*
- * The stack itself lives in `@triliumnext/core` so the browser-hosted
- * (standalone) build can run it too. What stays here is everything that needs
- * Node: the Claude Agent provider, which spawns the Claude Code CLI, and the
- * two tool registries that read off disk (the User Guide and the skill sheets).
- * Importing this module contributes them to core; it is the only supported way
- * in, so nothing can reach a half-registered stack.
+ * The stack itself lives in `@triliumnext/core`, so the browser-hosted
+ * (standalone) build can run it too. What could not follow it there is anything
+ * that needs Node: the Claude Agent provider, which spawns the Claude Code CLI,
+ * and the two tool registries that read off disk (the User Guide and the skill
+ * sheets). Core exposes a seam for each; this is where the server fills them in.
*/
import { registerClaudeAgentProvider } from "@triliumnext/core/src/services/llm/index.js";
@@ -18,24 +17,15 @@ import { skillTools } from "./skills/index.js";
import { getDocNoteHtml } from "./tools/doc_notes.js";
import { helpTools } from "./tools/help_tools.js";
-export { runChat } from "@triliumnext/core/src/services/llm/chat.js";
-export {
- clearProviderCache,
- getProvider,
- getProviderByType,
- getSelectedModel,
- hasConfiguredProviders,
- listProviderModels,
- type LlmProvider,
- type LlmProviderConfig,
- type LlmProviderSetup,
- type ModelInfo,
- type ModelPricing
-} from "@triliumnext/core/src/services/llm/index.js";
-export { allToolRegistries } from "@triliumnext/core/src/services/llm/tools/index.js";
-export type { ToolDefinition } from "@triliumnext/core/src/services/llm/tools/tool_registry.js";
-
-registerClaudeAgentProvider(() => new ClaudeAgentProvider());
-registerDocNoteHtmlReader(getDocNoteHtml);
-registerToolRegistry(helpTools);
-registerToolRegistry(skillTools);
+/**
+ * Contribute those pieces to core. Called once from startup, beside the other
+ * registrations there, rather than run as an import side effect: core's own LLM
+ * routes reach the stack directly, so what a chat can use must not depend on
+ * whether some module that happens to import this one was loaded first.
+ */
+export function registerServerLlmExtensions() {
+ registerClaudeAgentProvider(() => new ClaudeAgentProvider());
+ registerDocNoteHtmlReader(getDocNoteHtml);
+ registerToolRegistry(helpTools);
+ registerToolRegistry(skillTools);
+}
diff --git a/apps/server/src/services/mcp/mcp_server.spec.ts b/apps/server/src/services/mcp/mcp_server.spec.ts
index 9fe978be0a..116569c9a2 100644
--- a/apps/server/src/services/mcp/mcp_server.spec.ts
+++ b/apps/server/src/services/mcp/mcp_server.spec.ts
@@ -52,7 +52,7 @@ const mutateRegistry: any = [
["mutate_tool", { description: "m", inputSchema: {}, mutates: true, execute: mutateExecute }]
];
-vi.mock("../llm/index.js", () => ({
+vi.mock("@triliumnext/core/src/services/llm/tools/index.js", () => ({
allToolRegistries: [readRegistry, mutateRegistry]
}));
diff --git a/apps/server/src/services/mcp/mcp_server.ts b/apps/server/src/services/mcp/mcp_server.ts
index 1e33e5d9f3..c87eeee78c 100644
--- a/apps/server/src/services/mcp/mcp_server.ts
+++ b/apps/server/src/services/mcp/mcp_server.ts
@@ -10,7 +10,9 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { app_info as appInfo } from "@triliumnext/core";
import { cls } from "@triliumnext/core";
-import { allToolRegistries, type ToolDefinition } from "../llm/index.js";
+import { allToolRegistries } from "@triliumnext/core/src/services/llm/tools/index.js";
+import type { ToolDefinition } from "@triliumnext/core/src/services/llm/tools/tool_registry.js";
+
import sql from "../sql.js";
/**
diff --git a/apps/server/src/www.ts b/apps/server/src/www.ts
index 8d51e417fe..93bb78bf9a 100644
--- a/apps/server/src/www.ts
+++ b/apps/server/src/www.ts
@@ -10,6 +10,7 @@ import config from "./services/config.js";
import { startCpuProfiler, writeCpuProfile } from "./services/cpu_profiler.js";
import { registerOcrHandlers } from "./services/handlers.js";
import host from "./services/host.js";
+import { registerServerLlmExtensions } from "./services/llm/index.js";
import port from "./services/port.js";
import { installProcessErrorHandlers, markAppReady } from "./services/process_errors.js";
import { isScriptingEnabled } from "./services/scripting_guard.js";
@@ -89,6 +90,7 @@ export default async function startTriliumServer(): Promise<Express> {
ws.init();
registerOcrHandlers();
+ registerServerLlmExtensions();
// Everything the application needs in order to be usable is now up, so from here on an escaped error
// is a contained failure rather than a broken startup, and stops being fatal.