Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): harden search_help query handling per review

Elian Dorand2a668d2245a
diff --git a/apps/server/src/services/llm/tools/help_tools.spec.ts b/apps/server/src/services/llm/tools/help_tools.spec.ts
index e3a4e85c52..d7ea1d78ca 100644
--- a/apps/server/src/services/llm/tools/help_tools.spec.ts
+++ b/apps/server/src/services/llm/tools/help_tools.spec.ts
@@ -110,10 +110,24 @@ describe("help_tools", () => {
expect(limited.results).toHaveLength(1);
});
+ it("strips punctuation and deduplicates query words", () => {
+ buildHelpTree();
+
+ // "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 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"]);
+ });
+
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", () => {
diff --git a/apps/server/src/services/llm/tools/help_tools.ts b/apps/server/src/services/llm/tools/help_tools.ts
index ef5dd547b9..254fe3699a 100644
--- a/apps/server/src/services/llm/tools/help_tools.ts
+++ b/apps/server/src/services/llm/tools/help_tools.ts
@@ -34,14 +34,18 @@ export const helpTools = defineTools({
].join(" "),
inputSchema: z.object({
query: z.string().describe("Keyword search query (a few plain words, not a full question)"),
- limit: z.number().optional().describe("Maximum number of results to return. Defaults to 10.")
+ limit: z.number().int().positive().optional().describe("Maximum number of results to return. Defaults to 10.")
}),
execute: ({ query, limit = DEFAULT_SEARCH_LIMIT }) => {
if (!isHelpAvailable()) {
return { error: "The built-in User Guide is not available in this installation." };
}
- const words = query.toLowerCase().split(/\s+/).filter(Boolean);
+ // Strip punctuation (models happily send "cloning?" despite the
+ // schema hint) and deduplicate so repeated words don't skew scoring.
+ const words = [...new Set(
+ query.toLowerCase().replace(/[^\p{L}\p{N}\s]/gu, " ").split(/\s+/).filter(Boolean)
+ )];
if (words.length === 0) {
return { error: "Empty search query." };
}
@@ -152,7 +156,13 @@ function scorePage(page: HelpPageIndexEntry, words: string[]): number {
}
function countOccurrences(haystack: string, needle: string): number {
- return haystack.split(needle).length - 1;
+ 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. */