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
refactor(llm): search the guide with the application's own search
Elian Doran853aa2aa2a4d
diff --git a/packages/trilium-core/src/services/llm/tools/help_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/help_tools.spec.tsindex d4709d3817..d129e42321 100644--- a/packages/trilium-core/src/services/llm/tools/help_tools.spec.ts+++ b/packages/trilium-core/src/services/llm/tools/help_tools.spec.ts@@ -1,10 +1,23 @@-import { beforeEach, describe, expect, it } from "vitest";+import { beforeEach, describe, expect, it, vi } from "vitest";import becca from "../../../becca/becca.js";import { buildNote } from "../../../test/becca_easy_mocking.js";-import { helpTools, resetHelpIndex } from "./help_tools.js";+import type SearchContext from "../../search/search_context.js";++// The searching itself is the application's, and it wants a database; what belongs to these tools+// is which notes they put in scope and what they make of the results. Those are asserted here, and+// the matching end to end against the shipped guide in `apps/server/spec/in_app_help.spec.ts`.+vi.mock("../../search/services/search.js", () => ({+ default: {+ findResultsWithQuery: (query: string, context: SearchContext) => search(query, context)+ }+}));++import { helpTools } from "./help_tools.js";import type { ToolDefinition } from "./tool_registry.js";+let search = vi.fn((_query: string, _context: SearchContext) => [] as { noteId: string }[]);+function getTool(name: string): ToolDefinition {for (const [n, def] of helpTools) {if (n === name) return def;@@ -52,12 +65,27 @@ interface SearchHelpResult {describe("help_tools", () => {beforeEach(() => {becca.reset();- resetHelpIndex();+ search = vi.fn(() => []);});describe("search_help", () => {- it("matches page bodies and returns breadcrumb paths", () => {+ it("searches the guide and nothing else, passing the query through as written", () => {+ buildHelpTree();++ getTool("search_help").execute({ query: "multiple locations?" });++ expect(search).toHaveBeenCalledOnce();+ const [ query, context ] = search.mock.calls[0];+ expect(query).toBe("multiple locations?");+ // Naming an ancestor is also what puts the hidden subtree in scope, so the guide is+ // reachable without the search opening up to everything else hidden.+ expect(context.ancestorNoteId).toBe("_help");+ expect(context.includeHiddenNotes).toBe(false);+ });++ it("describes each hit by its title, its place in the guide and a preview of its text", () => {buildHelpTree();+ search = vi.fn(() => [ { noteId: "_help_cloning" } ]);const result = getTool("search_help").execute({ query: "multiple locations" }) as SearchHelpResult;@@ -70,55 +98,34 @@ describe("help_tools", () => {expect(result.results[0].contentPreview).toContain("multiple locations");});- it("requires every query word (AND semantics)", () => {+ it("reports how many pages matched even when only some are returned", () => {buildHelpTree();-- const result = getTool("search_help").execute({ query: "cloning fnordhotzenplotz" }) as SearchHelpResult;-- expect(result.totalResults).toBe(0);- expect(result.results).toEqual([]);- });-- it("ranks title matches above body matches and respects the limit", () => {- buildHelpTree();- // "installation" occurs in the Installation page title AND in the Cloning page body.-- const all = getTool("search_help").execute({ query: "installation" }) as SearchHelpResult;- expect(all.totalResults).toBe(2);- expect(all.results.map(r => r.noteId)).toEqual(["_help_install", "_help_cloning"]);+ search = vi.fn(() => [ { noteId: "_help_install" }, { noteId: "_help_cloning" } ]);const limited = getTool("search_help").execute({ query: "installation", limit: 1 }) as SearchHelpResult;+expect(limited.totalResults).toBe(2);- expect(limited.results).toHaveLength(1);+ expect(limited.results.map((r) => r.noteId)).toEqual([ "_help_install" ]);});- it("strips punctuation and deduplicates query words", () => {+ it("drops a hit whose note has since gone from becca", () => {buildHelpTree();+ search = vi.fn(() => [ { noteId: "_help_cloning" }, { noteId: "_help_vanished" } ]);- // "locations?" would never match as-is; repeated words must not double-count.- const punctuated = getTool("search_help").execute({ query: "multiple locations?" }) as SearchHelpResult;- expect(punctuated.results.map(r => r.noteId)).toEqual(["_help_cloning"]);+ const result = getTool("search_help").execute({ query: "anything" }) as SearchHelpResult;- const deduped = getTool("search_help").execute({ query: "installation installation" }) as SearchHelpResult;- // Title match still outranks body match — the duplicate word doesn't skew scoring.- expect(deduped.results.map(r => r.noteId)).toEqual(["_help_install", "_help_cloning"]);+ expect(result.results.map((r) => r.noteId)).toEqual([ "_help_cloning" ]);});- it("rejects an empty query", () => {- buildHelpTree();- expect(getTool("search_help").execute({ query: " " }))- .toEqual({ error: "Empty search query." });- expect(getTool("search_help").execute({ query: "?!" }))- .toEqual({ error: "Empty search query." });- });-- it("returns an error when the help subtree is absent or empty", () => {+ it("returns an error when the help subtree is absent or empty, without searching", () => {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", content: "" }); // present but not yet populatedexpect(getTool("search_help").execute({ query: "anything" })).toEqual({ error: "The built-in User Guide is not available in this installation." });++ expect(search).not.toHaveBeenCalled();});});diff --git a/packages/trilium-core/src/services/llm/tools/help_tools.ts b/packages/trilium-core/src/services/llm/tools/help_tools.tsindex e869fc964f..c77a321a86 100644--- a/packages/trilium-core/src/services/llm/tools/help_tools.ts+++ b/packages/trilium-core/src/services/llm/tools/help_tools.ts@@ -4,16 +4,17 @@* "how do I…?" questions about Trilium itself, grounded in the documentation* that ships with the running version instead of possibly stale training data.*- * Help pages are virtual notes, so the regular search reaches their bodies too.- * search_help stays separate for the ranking it does: it is scoped to the guide- * and weighs a title match above a body one, over a lazily built in-memory index- * of the page texts (~1 MB for the whole guide, built once per process).+ * The searching itself is the application's own, scoped to the guide. What these add is that they+ * are here to be reached for at all: a tool the assistant can see is what points it at the+ * documentation of the version in front of it.*/import { z } from "zod";import becca from "../../../becca/becca.js";import type BNote from "../../../becca/entities/bnote.js";+import SearchContext from "../../search/search_context.js";+import searchService from "../../search/services/search.js";import { getContentPreview } from "./helpers.js";import { defineTools } from "./tool_registry.js";@@ -21,15 +22,13 @@ const HELP_ROOT_NOTE_ID = "_help";const DEFAULT_SEARCH_LIMIT = 10;/** The User Guide is only a few levels deep; bound traversal defensively. */const MAX_HELP_DEPTH = 10;-/** A word occurrence in the title outweighs one in the body. */-const TITLE_MATCH_WEIGHT = 10;export const helpTools = defineTools({search_help: {description: ["Search Trilium's built-in User Guide — the documentation for Trilium itself.","Use this to answer questions about how to use Trilium: features, settings, keyboard shortcuts, sync, scripting, themes, etc.",- "Full-text keyword search over the help pages; keep queries to a few keywords (e.g. 'keyboard shortcuts', 'protected notes').",+ "Takes the same query as search_notes, over the help pages alone; keep it to a few keywords (e.g. 'keyboard shortcuts', 'protected notes').","If a query finds nothing, retry with synonyms or browse get_help_toc — the guide may name the concept differently (e.g. placing a note in two locations is 'cloning').","Read a found page with get_note_content."].join(" "),@@ -42,27 +41,19 @@ export const helpTools = defineTools({return { error: "The built-in User Guide is not available in this installation." };}- // Strip punctuation (models happily send "cloning?" despite the- // schema hint) and deduplicate so repeated words don't skew scoring.- const words = query.toLowerCase()- .replace(/[^\p{L}\p{N}\s]/gu, " ")- .split(/\s+/)- .filter(Boolean)- .filter((word, index, all) => all.indexOf(word) === index);- if (words.length === 0) {- return { error: "Empty search query." };- }-- const matches = getHelpIndex()- .map((page) => ({ page, score: scorePage(page, words) }))- .filter((m) => m.score > 0)- .sort((a, b) => b.score - a.score);+ // Naming an ancestor is also what lets the guide be found at all: it takes the place+ // of the filter that keeps the hidden subtree out of ordinary results (see+ // `getAncestorExp`), so the pages are in scope without opening up everything else.+ const matches = searchService.findResultsWithQuery(+ query,+ new SearchContext({ ancestorNoteId: HELP_ROOT_NOTE_ID })+ );- const results = matches.slice(0, limit).map(({ page }) => {- const note = becca.notes[page.noteId];+ const results = matches.slice(0, limit).map(({ noteId }) => {+ const note = becca.notes[noteId];if (!note) return null;return {- noteId: page.noteId,+ noteId,title: note.getTitleOrProtected(),path: getHelpPath(note),contentPreview: getContentPreview(note)@@ -100,99 +91,6 @@ export const helpTools = defineTools({}});-/** One help page in the search index; title and text are lowercased for matching. */-interface HelpPageIndexEntry {- noteId: string;- titleLower: string;- textLower: string;-}--/**- * Lazily built index of all help pages. The guide only changes on upgrade- * (which restarts the server), so it is cached for the process lifetime.- */-let helpIndex: HelpPageIndexEntry[] | null = null;--/** Drop the cached search index (for tests). */-export function resetHelpIndex(): void {- helpIndex = null;-}--function getHelpIndex(): HelpPageIndexEntry[] {- if (helpIndex === null) {- helpIndex = [];- collectIndexEntries(becca.getNoteOrThrow(HELP_ROOT_NOTE_ID), 0, helpIndex);- }- return helpIndex;-}--function collectIndexEntries(note: BNote, depth: number, entries: HelpPageIndexEntry[]): void {- if (depth >= MAX_HELP_DEPTH) {- return;- }- for (const child of note.getChildNotes()) {- entries.push({- noteId: child.noteId,- titleLower: child.getTitleOrProtected().toLowerCase(),- textLower: getPageText(child).toLowerCase()- });- collectIndexEntries(child, depth + 1, entries);- }-}--/** The text of a help page, which carries its content like any other note. */-function getPageText(note: BNote): string {- 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.- * Returns 0 when any word is missing.- */-function scorePage(page: HelpPageIndexEntry, words: string[]): number {- let score = 0;- for (const word of words) {- const titleHits = countOccurrences(page.titleLower, word);- const bodyHits = countOccurrences(page.textLower, word);- if (titleHits + bodyHits === 0) {- return 0;- }- score += titleHits * TITLE_MATCH_WEIGHT + bodyHits;- }- return score;-}--function countOccurrences(haystack: string, needle: string): number {- let count = 0;- let index = haystack.indexOf(needle);- while (index !== -1) {- count++;- index = haystack.indexOf(needle, index + needle.length);- }- return count;-}--/** Crude tag stripping — good enough for keyword matching, not for display. */-function htmlToPlainText(html: string): string {- return html- .replace(/<[^>]+>/g, " ")- .replace(/ /g, " ")- .replace(/</g, "<")- .replace(/>/g, ">")- .replace(/"/g, "\"")- .replace(/'/g, "'")- .replace(/&/g, "&")- .replace(/\s+/g, " ");-}-/** The help subtree exists only once the in-app help has been imported. */function isHelpAvailable(): boolean {const helpRoot = becca.getNote(HELP_ROOT_NOTE_ID);