Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): basic tool use

Elian Doran246c561b64ef
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 5f03de87ab..b8c2ed8811 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1618,6 +1618,7 @@
"empty_state": "Start a conversation by typing a message below.",
"searching_web": "Searching the web...",
"web_search": "Web search",
+ "note_tools": "Note access",
"sources": "Sources",
"extended_thinking": "Extended thinking",
"thinking": "Thinking...",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
index bab59fef71..7ecf2eab09 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/LlmChat.tsx
@@ -24,6 +24,7 @@ interface LlmChatContent {
version: 1;
messages: StoredMessage[];
enableWebSearch?: boolean;
+ enableNoteTools?: boolean;
enableExtendedThinking?: boolean;
}
@@ -36,6 +37,7 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
const [toolActivity, setToolActivity] = useState<string | null>(null);
const [pendingCitations, setPendingCitations] = useState<LlmCitation[]>([]);
const [enableWebSearch, setEnableWebSearch] = useState(true);
+ const [enableNoteTools, setEnableNoteTools] = useState(false);
const [enableExtendedThinking, setEnableExtendedThinking] = useState(false);
const [error, setError] = useState<string | null>(null);
const [shouldSave, setShouldSave] = useState(false);
@@ -57,6 +59,9 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
const enableWebSearchRef = useRef(enableWebSearch);
enableWebSearchRef.current = enableWebSearch;
+ const enableNoteToolsRef = useRef(enableNoteTools);
+ enableNoteToolsRef.current = enableNoteTools;
+
const enableExtendedThinkingRef = useRef(enableExtendedThinking);
enableExtendedThinkingRef.current = enableExtendedThinking;
@@ -70,6 +75,7 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
version: 1,
messages: messagesRef.current,
enableWebSearch: enableWebSearchRef.current,
+ enableNoteTools: enableNoteToolsRef.current,
enableExtendedThinking: enableExtendedThinkingRef.current
};
return { content: JSON.stringify(content) };
@@ -85,6 +91,9 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
if (typeof parsed.enableWebSearch === "boolean") {
setEnableWebSearch(parsed.enableWebSearch);
}
+ if (typeof parsed.enableNoteTools === "boolean") {
+ setEnableNoteTools(parsed.enableNoteTools);
+ }
if (typeof parsed.enableExtendedThinking === "boolean") {
setEnableExtendedThinking(parsed.enableExtendedThinking);
}
@@ -136,7 +145,7 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
await streamChatCompletion(
apiMessages,
- { enableWebSearch, enableExtendedThinking },
+ { enableWebSearch, enableNoteTools, enableExtendedThinking },
{
onChunk: (text) => {
assistantContent += text;
@@ -227,6 +236,11 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
setShouldSave(true);
}, []);
+ const toggleNoteTools = useCallback(() => {
+ setEnableNoteTools(prev => !prev);
+ setShouldSave(true);
+ }, []);
+
const toggleExtendedThinking = useCallback(() => {
setEnableExtendedThinking(prev => !prev);
setShouldSave(true);
@@ -311,6 +325,16 @@ export default function LlmChat({ note, ntxId, noteContext }: TypeWidgetProps) {
<span className="bx bx-globe" />
{t("llm_chat.web_search")}
</label>
+ <label className="llm-chat-toggle">
+ <input
+ type="checkbox"
+ checked={enableNoteTools}
+ onChange={toggleNoteTools}
+ disabled={isStreaming}
+ />
+ <span className="bx bx-note" />
+ {t("llm_chat.note_tools")}
+ </label>
<label className="llm-chat-toggle">
<input
type="checkbox"
diff --git a/apps/server/src/services/llm/providers/anthropic.ts b/apps/server/src/services/llm/providers/anthropic.ts
index 9e6db7e380..03e0bc91b6 100644
--- a/apps/server/src/services/llm/providers/anthropic.ts
+++ b/apps/server/src/services/llm/providers/anthropic.ts
@@ -1,7 +1,8 @@
import { anthropic } from "@ai-sdk/anthropic";
-import { streamText, type CoreMessage } from "ai";
+import { streamText, stepCountIs, type CoreMessage } from "ai";
import type { LlmMessage } from "@triliumnext/commons";
+import { noteTools } from "../tools.js";
import type { LlmProvider, LlmProviderConfig, StreamResult } from "../types.js";
const DEFAULT_MODEL = "claude-sonnet-4-20250514";
@@ -55,14 +56,27 @@ export class AnthropicProvider implements LlmProvider {
);
}
- // Enable web search if configured
+ // Build tools object
+ const tools: Record<string, unknown> = {};
+
if (config.enableWebSearch) {
- const webSearchTool = anthropic.tools.webSearch_20250305({
+ tools.web_search = anthropic.tools.webSearch_20250305({
maxUses: 5
});
- streamOptions.tools = {
- web_search: webSearchTool
- };
+ }
+
+ if (config.enableNoteTools) {
+ Object.assign(tools, noteTools);
+ }
+
+ if (Object.keys(tools).length > 0) {
+ streamOptions.tools = tools;
+ // Allow multiple tool use cycles before final response
+ streamOptions.maxSteps = 5;
+ // Override default stopWhen which stops after 1 step
+ streamOptions.stopWhen = stepCountIs(5);
+ // Let model decide when to use tools vs respond with text
+ streamOptions.toolChoice = "auto";
}
return streamText(streamOptions);
diff --git a/apps/server/src/services/llm/tools.ts b/apps/server/src/services/llm/tools.ts
new file mode 100644
index 0000000000..5434ebdcfa
--- /dev/null
+++ b/apps/server/src/services/llm/tools.ts
@@ -0,0 +1,70 @@
+/**
+ * LLM tools that wrap existing Trilium services.
+ * These reuse the same logic as ETAPI without any HTTP overhead.
+ */
+
+import { tool } from "ai";
+import { z } from "zod";
+
+import becca from "../../becca/becca.js";
+import SearchContext from "../search/search_context.js";
+import searchService from "../search/services/search.js";
+
+/**
+ * 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.",
+ inputSchema: z.object({
+ query: z.string().describe("Search query (supports Trilium search syntax)")
+ }),
+ execute: async ({ query }) => {
+ const searchContext = new SearchContext({});
+ const results = searchService.findResultsWithQuery(query, searchContext);
+
+ return results.slice(0, 10).map(sr => {
+ const note = becca.notes[sr.noteId];
+ if (!note) return null;
+ return {
+ noteId: note.noteId,
+ title: note.title,
+ type: note.type
+ };
+ }).filter(Boolean);
+ }
+});
+
+/**
+ * Read the content of a specific note.
+ */
+export const readNote = tool({
+ description: "Read the full content of a note by its ID. Use search_notes first to find relevant note IDs.",
+ inputSchema: z.object({
+ noteId: z.string().describe("The ID of the note to read")
+ }),
+ execute: async ({ noteId }) => {
+ const note = becca.getNote(noteId);
+ if (!note) {
+ return { error: "Note not found" };
+ }
+ if (note.isProtected) {
+ return { error: "Note is protected" };
+ }
+
+ const content = note.getContent();
+ return {
+ noteId: note.noteId,
+ title: note.title,
+ type: note.type,
+ content: typeof content === "string" ? content : "[binary content]"
+ };
+ }
+});
+
+/**
+ * All available note tools.
+ */
+export const noteTools = {
+ search_notes: searchNotes,
+ read_note: readNote
+};
diff --git a/packages/commons/src/lib/llm_api.ts b/packages/commons/src/lib/llm_api.ts
index 6197f184ac..881cfd2ecc 100644
--- a/packages/commons/src/lib/llm_api.ts
+++ b/packages/commons/src/lib/llm_api.ts
@@ -33,6 +33,8 @@ export interface LlmChatConfig {
systemPrompt?: string;
/** Enable web search tool */
enableWebSearch?: boolean;
+ /** Enable note tools (search and read notes) */
+ enableNoteTools?: boolean;
/** Enable extended thinking for deeper reasoning */
enableExtendedThinking?: boolean;
/** Token budget for extended thinking (default: 10000) */