Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): index the help pages from their content, and move the tools to core

Elian Doran8fdb02b6fa61
diff --git a/apps/server/spec/in_app_help.spec.ts b/apps/server/spec/in_app_help.spec.ts
index b0ae5741ba..abcfb20605 100644
--- a/apps/server/spec/in_app_help.spec.ts
+++ b/apps/server/spec/in_app_help.spec.ts
@@ -4,6 +4,7 @@ import { load } from "@triliumnext/core/src/becca/becca_loader.js";
import blobService from "@triliumnext/core/src/services/blob.js";
import { getContext } from "@triliumnext/core/src/services/context.js";
import { HELP_ASSET_TOKEN, initInAppHelp } from "@triliumnext/core/src/services/in_app_help.js";
+import { helpTools } from "@triliumnext/core/src/services/llm/tools/help_tools.js";
import searchService from "@triliumnext/core/src/services/search/services/search.js";
import fs from "fs";
import path from "path";
@@ -84,6 +85,20 @@ describe("in-app help (shipped artifacts)", () => {
expect(ordinary.filter((note) => note.noteId.startsWith("_help"))).toEqual([]);
});
+ it("is searchable by the assistant, by body text and not only by title", () => {
+ const searchHelp = Object.fromEntries([ ...helpTools ]).search_help;
+ const result = searchHelp.execute({ query: "docker compose" }) as {
+ totalResults: number;
+ results: { noteId: string; contentPreview: string | null }[];
+ };
+
+ // The phrase is in the body of the Docker page and in no title, so a title-only index —
+ // which is what an unresolvable content source leaves behind — scores nothing.
+ expect(result.totalResults).toBeGreaterThan(0);
+ expect(result.results[0].noteId).toBe("_help_rWX5eY045zbE");
+ expect(result.results[0].contentPreview).toBeTruthy();
+ });
+
it("makes every page read-only", () => {
for (const item of flatten(meta)) {
expect(becca.getNoteOrThrow(item.id).isLabelTruthy("readOnly")).toBe(true);
diff --git a/apps/server/src/services/llm/index.spec.ts b/apps/server/src/services/llm/index.spec.ts
index ecb14c4efc..c317ca8a00 100644
--- a/apps/server/src/services/llm/index.spec.ts
+++ b/apps/server/src/services/llm/index.spec.ts
@@ -10,19 +10,17 @@ const registrations = vi.hoisted(() => ({
vi.mock("@triliumnext/core/src/services/llm/index.js", () => ({
registerHostProvider: (type: string, factory: () => unknown) => { registrations.hostProviders.push({ type, factory }); }
}));
-vi.mock("@triliumnext/core/src/services/llm/skills.js", () => ({
+// Partial: dropping the tool-registry mock brings the real tool graph into this suite, and the
+// skills tool reads the catalog at import time.
+vi.mock("@triliumnext/core/src/services/llm/skills.js", async (importOriginal) => ({
+ ...await importOriginal<typeof import("@triliumnext/core/src/services/llm/skills.js")>(),
registerSkillReader: (reader: (file: string) => string | null) => { registrations.skillReader = reader; }
}));
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 { helpTools } from "./tools/help_tools.js";
describe("registerServerLlmExtensions", () => {
it("hands core every piece of the stack that needs Node", () => {
@@ -38,7 +36,8 @@ describe("registerServerLlmExtensions", () => {
// The skill sheets: core owns the catalog and the tool, the server only
// knows how to get a sheet off disk.
expect(registrations.skillReader?.("search_syntax.md")).toContain("#");
- // The one registry still backed by files this build alone carries.
- expect(registrations.toolRegistries).toEqual([helpTools]);
+ // No tool registry of its own: the help tools read note content like every other
+ // tool, so core carries them and standalone gets them too.
+ expect(registrations.toolRegistries).toEqual([]);
});
});
diff --git a/apps/server/src/services/llm/index.ts b/apps/server/src/services/llm/index.ts
index cd037d2259..ed7b884d9a 100644
--- a/apps/server/src/services/llm/index.ts
+++ b/apps/server/src/services/llm/index.ts
@@ -16,12 +16,10 @@
import { registerHostProvider } from "@triliumnext/core/src/services/llm/index.js";
import { registerSkillReader } from "@triliumnext/core/src/services/llm/skills.js";
import { registerDocNoteHtmlReader } from "@triliumnext/core/src/services/llm/tools/helpers.js";
-import { registerToolRegistry } from "@triliumnext/core/src/services/llm/tools/index.js";
import { loadSkillSheet } from "../../core_assets.js";
import { ClaudeAgentProvider } from "./providers/claude_agent.js";
import { getDocNoteHtml } from "./tools/doc_notes.js";
-import { helpTools } from "./tools/help_tools.js";
/**
* Contribute those pieces to core. Called once from startup, beside the other
@@ -33,5 +31,4 @@ export function registerServerLlmExtensions() {
registerHostProvider("claude-agent", () => new ClaudeAgentProvider());
registerDocNoteHtmlReader(getDocNoteHtml);
registerSkillReader(loadSkillSheet);
- registerToolRegistry(helpTools);
}
diff --git a/apps/server/src/services/llm/tools/help_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/help_tools.spec.ts
similarity index 69%
rename from apps/server/src/services/llm/tools/help_tools.spec.ts
rename to packages/trilium-core/src/services/llm/tools/help_tools.spec.ts
index 4fa7ce9464..d4709d3817 100644
--- a/apps/server/src/services/llm/tools/help_tools.spec.ts
+++ b/packages/trilium-core/src/services/llm/tools/help_tools.spec.ts
@@ -1,38 +1,9 @@
-import { becca, becca_easy_mocking } from "@triliumnext/core";
-import { registerDocNoteHtmlReader } from "@triliumnext/core/src/services/llm/tools/helpers.js";
-import type { ToolDefinition } from "@triliumnext/core/src/services/llm/tools/tool_registry.js";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeEach, describe, expect, it } from "vitest";
-import { getDocNoteHtml } from "./doc_notes.js";
+import becca from "../../../becca/becca.js";
+import { buildNote } from "../../../test/becca_easy_mocking.js";
import { helpTools, resetHelpIndex } from "./help_tools.js";
-
-// Doc-note HTML reaches core through a host-registered reader — the server's is
-// filesystem-backed. Registering it here mirrors what services/llm/index.ts
-// does in production.
-registerDocNoteHtmlReader(getDocNoteHtml);
-
-const { buildNote } = becca_easy_mocking;
-
-/** Fake on-disk help pages: path suffix → HTML content. */
-const docFiles = vi.hoisted(() => new Map<string, string>());
-
-// Help pages are doc notes whose HTML lives on disk — serve them from the
-// docFiles fixture map and pass every other path through to the real fs.
-vi.mock("fs", async (importOriginal) => {
- const actual = await importOriginal<typeof import("fs")>();
- const readFileSync = ((filePath: unknown, ...rest: unknown[]) => {
- const pathStr = String(filePath);
- for (const [suffix, content] of docFiles) {
- if (pathStr.endsWith(suffix)) return content;
- }
- if (pathStr.includes("doc_notes")) {
- throw new Error(`ENOENT: ${pathStr}`);
- }
- return (actual.readFileSync as (...args: unknown[]) => unknown)(filePath, ...rest);
- }) as typeof actual.readFileSync;
- const mocked = { ...actual, readFileSync };
- return { ...mocked, default: mocked };
-});
+import type { ToolDefinition } from "./tool_registry.js";
function getTool(name: string): ToolDefinition {
for (const [n, def] of helpTools) {
@@ -46,25 +17,31 @@ function buildHelpTree() {
buildNote({
id: "_help",
title: "User Guide",
+ content: "",
children: [
{
id: "_help_basics",
title: "Basic Concepts",
+ content: "",
children: [
{
id: "_help_notes",
title: "Notes",
+ content: "",
children: [
- { id: "_help_cloning", title: "Cloning Notes", type: "doc", "#docName": "User Guide/User Guide/Cloning Notes" }
+ {
+ id: "_help_cloning",
+ title: "Cloning Notes",
+ type: "text",
+ content: "<p>A note can be placed in multiple locations. Prefix &amp; installation hints.</p>"
+ }
]
}
]
},
- { id: "_help_install", title: "Installation & Setup", type: "doc", "#docName": "User Guide/User Guide/Installation" }
+ { id: "_help_install", title: "Installation & Setup", type: "text", content: "<p>Download the desktop app.</p>" }
]
});
- docFiles.set("Cloning Notes.html", "<p>A note can be placed in multiple locations. Prefix &amp; installation hints.</p>");
- docFiles.set("Installation.html", "<p>Download the desktop app.</p>");
}
interface SearchHelpResult {
@@ -76,12 +53,10 @@ describe("help_tools", () => {
beforeEach(() => {
becca.reset();
resetHelpIndex();
- docFiles.clear();
- vi.clearAllMocks();
});
describe("search_help", () => {
- it("matches page bodies (which live on disk, invisible to regular search) and returns breadcrumb paths", () => {
+ it("matches page bodies and returns breadcrumb paths", () => {
buildHelpTree();
const result = getTool("search_help").execute({ query: "multiple locations" }) as SearchHelpResult;
@@ -141,7 +116,7 @@ describe("help_tools", () => {
expect(getTool("search_help").execute({ query: "anything" }))
.toEqual({ error: "The built-in User Guide is not available in this installation." });
- buildNote({ id: "_help", title: "User Guide" }); // present but not yet populated
+ buildNote({ id: "_help", title: "User Guide", content: "" }); // present but not yet populated
expect(getTool("search_help").execute({ query: "anything" }))
.toEqual({ error: "The built-in User Guide is not available in this installation." });
});
diff --git a/apps/server/src/services/llm/tools/help_tools.ts b/packages/trilium-core/src/services/llm/tools/help_tools.ts
similarity index 89%
rename from apps/server/src/services/llm/tools/help_tools.ts
rename to packages/trilium-core/src/services/llm/tools/help_tools.ts
index 977e93a399..ca4acb1d2d 100644
--- a/apps/server/src/services/llm/tools/help_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/help_tools.ts
@@ -10,11 +10,13 @@
* (~1 MB for the whole guide, loaded once per process).
*/
-import { becca, type BNote } from "@triliumnext/core";
-import { getContentPreview, getDocNoteHtml } from "@triliumnext/core/src/services/llm/tools/helpers.js";
-import { defineTools } from "@triliumnext/core/src/services/llm/tools/tool_registry.js";
import { z } from "zod";
+import becca from "../../../becca/becca.js";
+import type BNote from "../../../becca/entities/bnote.js";
+import { getContentPreview, getDocNoteHtml } from "./helpers.js";
+import { defineTools } from "./tool_registry.js";
+
const HELP_ROOT_NOTE_ID = "_help";
const DEFAULT_SEARCH_LIMIT = 10;
/** The User Guide is only a few levels deep; bound traversal defensively. */
@@ -132,12 +134,31 @@ function collectIndexEntries(note: BNote, depth: number, entries: HelpPageIndexE
entries.push({
noteId: child.noteId,
titleLower: child.getTitleOrProtected().toLowerCase(),
- textLower: htmlToPlainText(getDocNoteHtml(child) ?? "").toLowerCase()
+ textLower: getPageText(child).toLowerCase()
});
collectIndexEntries(child, depth + 1, entries);
}
}
+/**
+ * The text of a help page. Pages carry their content like any other note; `doc` notes elsewhere in
+ * the hidden subtree still keep theirs on disk, so those fall back to the host's reader.
+ */
+function getPageText(note: BNote): string {
+ if (note.type === "doc") {
+ return htmlToPlainText(getDocNoteHtml(note) ?? "");
+ }
+
+ const content = note.getContent();
+
+ if (typeof content !== "string") {
+ return "";
+ }
+
+ // Code pages are shown verbatim in their own language; stripping tags would eat parts of them.
+ return note.type === "code" ? content : htmlToPlainText(content);
+}
+
/**
* Score a page against the query words: every word must occur in the title or
* body (AND semantics), title occurrences weigh more than body occurrences.
diff --git a/packages/trilium-core/src/services/llm/tools/index.ts b/packages/trilium-core/src/services/llm/tools/index.ts
index 15b56af2d3..cf0243705f 100644
--- a/packages/trilium-core/src/services/llm/tools/index.ts
+++ b/packages/trilium-core/src/services/llm/tools/index.ts
@@ -14,6 +14,7 @@ export { ToolRegistry } from "./tool_registry.js";
import { attachmentTools } from "./attachment_tools.js";
import { attributeTools } from "./attribute_tools.js";
+import { helpTools } from "./help_tools.js";
import { hierarchyTools } from "./hierarchy_tools.js";
import { iconTools } from "./icon_tools.js";
import { noteTools } from "./note_tools.js";
@@ -32,6 +33,7 @@ import type { ToolRegistry } from "./tool_registry.js";
*/
export const allToolRegistries: ToolRegistry[] = [
noteTools,
+ helpTools,
attributeTools,
attachmentTools,
hierarchyTools,