Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): basic skill to do search

Elian Doran8c0dacd6d7c0
diff --git a/apps/server/src/services/llm/providers/anthropic.ts b/apps/server/src/services/llm/providers/anthropic.ts
index af415dd020..50298038c1 100644
--- a/apps/server/src/services/llm/providers/anthropic.ts
+++ b/apps/server/src/services/llm/providers/anthropic.ts
@@ -3,7 +3,7 @@ import { generateText, streamText, stepCountIs, type CoreMessage, type ToolSet }
import type { LlmMessage } from "@triliumnext/commons";
import becca from "../../../becca/becca.js";
-import { noteTools, attributeTools, hierarchyTools, currentNoteTools } from "../tools/index.js";
+import { noteTools, attributeTools, hierarchyTools, skillTools, currentNoteTools } from "../tools/index.js";
import type { LlmProvider, LlmProviderConfig, ModelInfo, ModelPricing, StreamResult } from "../types.js";
const DEFAULT_MODEL = "claude-sonnet-4-6";
@@ -207,6 +207,7 @@ export class AnthropicProvider implements LlmProvider {
Object.assign(tools, noteTools);
Object.assign(tools, attributeTools);
Object.assign(tools, hierarchyTools);
+ Object.assign(tools, skillTools);
}
if (Object.keys(tools).length > 0) {
diff --git a/apps/server/src/services/llm/skills/index.ts b/apps/server/src/services/llm/skills/index.ts
new file mode 100644
index 0000000000..11f04fef7b
--- /dev/null
+++ b/apps/server/src/services/llm/skills/index.ts
@@ -0,0 +1,66 @@
+/**
+ * LLM skills — on-demand instruction sets that an LLM can load when it needs
+ * specialized knowledge (e.g. search syntax). Only names and descriptions are
+ * included in the system prompt; full content is fetched via the load_skill tool.
+ */
+
+import { tool } from "ai";
+import { readFileSync } from "fs";
+import { dirname, join } from "path";
+import { fileURLToPath } from "url";
+import { z } from "zod";
+
+const __dirname = dirname(fileURLToPath(import.meta.url));
+
+interface SkillDefinition {
+ name: string;
+ description: string;
+ file: string;
+}
+
+const SKILLS: SkillDefinition[] = [
+ {
+ name: "search_syntax",
+ description: "Trilium search query syntax reference — labels, relations, note properties, boolean logic, ordering, and more.",
+ file: "search_syntax.md"
+ }
+];
+
+function loadSkillContent(name: string): string | null {
+ const skill = SKILLS.find((s) => s.name === name);
+ if (!skill) {
+ return null;
+ }
+ return readFileSync(join(__dirname, skill.file), "utf-8");
+}
+
+/**
+ * Returns a summary of available skills for inclusion in the system prompt.
+ */
+export function getSkillsSummary(): string {
+ return SKILLS
+ .map((s) => `- **${s.name}**: ${s.description}`)
+ .join("\n");
+}
+
+/**
+ * The load_skill tool — lets the LLM fetch full instructions on demand.
+ */
+export const loadSkill = tool({
+ description: "Load a skill to get specialized instructions. Available skills:\n"
+ + SKILLS.map((s) => `- ${s.name}: ${s.description}`).join("\n"),
+ inputSchema: z.object({
+ name: z.string().describe("The skill name to load")
+ }),
+ execute: async ({ name }) => {
+ const content = loadSkillContent(name);
+ if (!content) {
+ return { error: `Unknown skill: '${name}'. Available: ${SKILLS.map((s) => s.name).join(", ")}` };
+ }
+ return { skill: name, instructions: content };
+ }
+});
+
+export const skillTools = {
+ load_skill: loadSkill
+};
diff --git a/apps/server/src/services/llm/skills/search_syntax.md b/apps/server/src/services/llm/skills/search_syntax.md
new file mode 100644
index 0000000000..a43b6b25d7
--- /dev/null
+++ b/apps/server/src/services/llm/skills/search_syntax.md
@@ -0,0 +1,50 @@
+# Trilium Search Syntax
+
+## Full-text search
+- `rings tolkien` — notes containing both words
+- `"The Lord of the Rings"` — exact phrase match
+
+## Label filters
+- `#book` — notes with the "book" label
+- `#!book` — notes WITHOUT the "book" label
+- `#publicationYear = 1954` — exact value
+- `#genre *=* fan` — contains substring
+- `#title =* The` — starts with
+- `#title *= Rings` — ends with
+- `#publicationYear >= 1950` — numeric comparison (>, >=, <, <=)
+- `#dateNote >= TODAY-30` — date keywords: NOW+-seconds, TODAY+-days, MONTH+-months, YEAR+-years
+- `#phone %= '\d{3}-\d{4}'` — regex match
+- `#title ~= trilim` — fuzzy exact match (tolerates typos, min 3 chars)
+- `#content ~* progra` — fuzzy contains match
+
+## Relation filters
+- `~author` — notes with an "author" relation
+- `~author.title *=* Tolkien` — relation target's title contains "Tolkien"
+- `~author.relations.son.title = 'Christopher Tolkien'` — deep relation traversal
+
+## Note properties
+Access via `note.` prefix: noteId, title, type, mime, text, content, rawContent, dateCreated, dateModified, isProtected, isArchived, parentCount, childrenCount, attributeCount, labelCount, relationCount, contentSize, revisionCount.
+- `note.type = code AND note.mime = 'application/json'`
+- `note.content *=* searchTerm`
+
+## Hierarchy
+- `note.parents.title = 'Books'` — parent named "Books"
+- `note.ancestors.title = 'Books'` — any ancestor named "Books"
+- `note.children.title = 'sub-note'` — child named "sub-note"
+
+## Boolean logic
+- AND: `#book AND #fantasy` (implicit between adjacent expressions)
+- OR: `#book OR #author`
+- NOT: `not(note.ancestors.title = 'Tolkien')`
+- Parentheses: `(#genre = "fantasy" AND #year >= 1950) OR #award`
+
+## Combining full-text and attributes
+- `towers #book` — full-text "towers" AND has #book label
+- `tolkien #book or #author` — full-text with OR on labels
+
+## Ordering and limiting
+- `#author=Tolkien orderBy #publicationDate desc, note.title limit 10`
+
+## Escaping
+- `\#hash` — literal # in full-text
+- Three quote types: single, double, backtick
diff --git a/apps/server/src/services/llm/tools/index.ts b/apps/server/src/services/llm/tools/index.ts
index 0540b9d617..615067674b 100644
--- a/apps/server/src/services/llm/tools/index.ts
+++ b/apps/server/src/services/llm/tools/index.ts
@@ -6,3 +6,4 @@
export { noteTools, currentNoteTools } from "./note_tools.js";
export { attributeTools } from "./attribute_tools.js";
export { hierarchyTools } from "./hierarchy_tools.js";
+export { skillTools } from "../skills/index.js";
diff --git a/apps/server/src/services/llm/tools/note_tools.ts b/apps/server/src/services/llm/tools/note_tools.ts
index 90eb475e16..c3703b5a80 100644
--- a/apps/server/src/services/llm/tools/note_tools.ts
+++ b/apps/server/src/services/llm/tools/note_tools.ts
@@ -43,15 +43,23 @@ function setNoteContentFromLlm(note: { type: string; title: string; setContent:
* Search for notes in the knowledge base.
*/
export const searchNotes = tool({
- description: "Search for notes in the user's knowledge base. Returns note metadata including title, type, and IDs.",
+ description: "Search for notes in the user's knowledge base using Trilium search syntax. Load the 'search_syntax' skill first if unsure about query format. Returns note metadata including title, type, and IDs.",
inputSchema: z.object({
- query: z.string().describe("Search query (supports Trilium search syntax)")
+ query: z.string().describe("Search query in Trilium search syntax (e.g. '#book #year >= 2000', 'tolkien #fantasy')"),
+ fastSearch: z.boolean().optional().describe("If true, skip content search (only titles and attributes). Faster for large databases."),
+ includeArchivedNotes: z.boolean().optional().describe("If true, include archived notes in results."),
+ ancestorNoteId: z.string().optional().describe("Limit search to a subtree rooted at this note ID."),
+ limit: z.number().optional().describe("Maximum number of results to return. Defaults to 10.")
}),
- execute: async ({ query }) => {
- const searchContext = new SearchContext({});
+ execute: async ({ query, fastSearch, includeArchivedNotes, ancestorNoteId, limit = 10 }) => {
+ const searchContext = new SearchContext({
+ fastSearch,
+ includeArchivedNotes,
+ ancestorNoteId
+ });
const results = searchService.findResultsWithQuery(query, searchContext);
- return results.slice(0, 10).map(sr => {
+ return results.slice(0, limit).map(sr => {
const note = becca.notes[sr.noteId];
if (!note) return null;
return {