Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): list the notes a search found in its tool call

Elian Doranddcde9808aac
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 5daf1fd74d..c8890673c9 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2660,7 +2660,12 @@
"waiting_for_reply": "Waiting for the reply…"
},
"show_tool_call_details": "Show input and output",
- "tool_call_no_result": "No result yet."
+ "tool_call_no_result": "No result yet.",
+ "search_notes_count_one": "{{count}} note",
+ "search_notes_count_other": "{{count}} notes",
+ "search_notes_limited_one": "{{count}} total result, limited to {{limit}}",
+ "search_notes_limited_other": "{{count}} total results, limited to {{limit}}",
+ "search_notes_scope": "Searched in <Note/>"
},
"sidebar_chat": {
"title": "AI Chat",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
index 5b6dd5fa8a..bf634687dc 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
@@ -81,3 +81,8 @@
@keyframes llm-chat-tool-call-caret {
50% { opacity: 0; }
}
+
+.llm-chat-tool-call-result-count {
+ font-size: 0.75rem;
+ opacity: 0.8;
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
index b493685e18..c12813782a 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
@@ -3,7 +3,14 @@ import { act } from "preact/test-utils";
import { afterEach, describe, expect, it, vi } from "vitest";
vi.mock("../../../services/i18n.js", () => ({
- t: (key: string, options?: { defaultValue?: string }) => (key.startsWith("llm.tools.") ? key.slice("llm.tools.".length) : options?.defaultValue ?? key)
+ t: (key: string, options?: { defaultValue?: string }) => {
+ if (key.startsWith("llm.tools.")) return key.slice("llm.tools.".length);
+ return options?.defaultValue ?? (options ? `${key}${JSON.stringify(options)}` : key);
+ }
+}));
+vi.mock("react-i18next", () => ({
+ Trans: ({ i18nKey, components }: { i18nKey: string; components: Record<string, preact.ComponentChildren> }) =>
+ <>{i18nKey}{Object.values(components)}</>
}));
const mocks = vi.hoisted(() => ({ triggerEvent: vi.fn() }));
vi.mock("../../../components/app_context.js", () => ({ default: { triggerEvent: mocks.triggerEvent } }));
@@ -82,4 +89,71 @@ describe("ToolCallCard", () => {
expect(mocks.triggerEvent).toHaveBeenCalledOnce();
expect((line as HTMLDetailsElement | null)?.open).toBe(false);
});
+
+ it("lists the notes a search found, with their parents and a plain preview", () => {
+ const target = renderCard([ {
+ id: "1",
+ toolName: "search_notes",
+ input: { query: "rocket", limit: 2 },
+ result: JSON.stringify({
+ totalResults: 42,
+ results: [
+ { noteId: "a", title: "Apollo", type: "text", parentTitle: "Space", contentPreview: "## Launch\n\nThe **Saturn V** [rocket](https://x.org)" },
+ { noteId: "b", title: "Big", type: "text", parentTitle: null, contentPreview: "[12KB - use get_note_content for full text]" }
+ ]
+ })
+ } ]);
+ const line = target.querySelector("details.llm-chat-tool-call");
+ expect(line?.querySelector(".llm-chat-tool-call-result-count")?.textContent).toBe("llm_chat.search_notes_count{\"count\":42}");
+
+ const rows = [ ...(line?.querySelectorAll(".llm-chat-note-result") ?? []) ];
+ expect(rows.map(row => ({
+ note: row.querySelector(".note-link-stub")?.textContent,
+ parent: row.querySelector(".llm-chat-note-result-parent")?.textContent ?? null,
+ preview: row.querySelector(".llm-chat-note-result-preview")?.textContent ?? null
+ }))).toEqual([
+ { note: "a", parent: "Space", preview: "Launch The Saturn V rocket" },
+ { note: "b", parent: null, preview: null }
+ ]);
+ expect(line?.querySelector(".llm-chat-note-results-scope")).toBeNull();
+ expect(line?.querySelector(".llm-chat-note-results-more")?.textContent)
+ .toBe("llm_chat.search_notes_limited{\"count\":42,\"limit\":2}");
+ });
+
+ it("names the subtree a search was confined to, even when it found nothing there", () => {
+ const target = renderCard([
+ {
+ id: "1",
+ toolName: "search_notes",
+ input: { query: "a", ancestorNoteId: "scope" },
+ result: JSON.stringify({ totalResults: 12, results: [ { noteId: "a" } ] })
+ },
+ {
+ id: "2",
+ toolName: "search_notes",
+ input: { query: "b", ancestorNoteId: "scope" },
+ result: JSON.stringify({ totalResults: 0, results: [] })
+ }
+ ]);
+ const lines = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+ expect(lines.map(line => line instanceof HTMLDetailsElement)).toEqual([ true ]);
+ const groupedLines = [ ...(lines[0]?.querySelectorAll(".expandable-section-body > .llm-chat-tool-call") ?? []) ];
+ expect(groupedLines.map(line => line instanceof HTMLDetailsElement)).toEqual([ true, true ]);
+ expect(groupedLines.map(line => line.querySelector(".llm-chat-note-results-scope .note-link-stub")?.textContent))
+ .toEqual([ "scope", "scope" ]);
+ expect(groupedLines[0]?.querySelector(".llm-chat-note-results-more")?.textContent)
+ .toBe("llm_chat.search_notes_limited{\"count\":12,\"limit\":1}");
+ });
+
+ it("keeps a search that found nothing, or returned something unexpected, to a plain line", () => {
+ const target = renderCard([
+ { id: "1", toolName: "search_notes", input: { query: "a", ancestorNoteId: "root" }, result: JSON.stringify({ totalResults: 0, results: [] }) },
+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },
+ { id: "3", toolName: "search_notes", input: { query: "b" }, result: "not json" }
+ ]);
+ const lines = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+ expect(lines.map(line => line instanceof HTMLDetailsElement)).toEqual([ false, false, false ]);
+ expect(lines[0]?.querySelector(".llm-chat-tool-call-result-count")?.textContent).toBe("llm_chat.search_notes_count{\"count\":0}");
+ expect(target.querySelector(".llm-chat-note-result")).toBeNull();
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
index 8e5954e012..492895bd26 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -9,6 +9,7 @@ import { NewNoteLink } from "../../react/NoteLink.js";
import { EditNoteContentDiff, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";
import { ExpandableSection } from "./ExpandableCard.js";
import type { ToolCall } from "./llm_chat_types.js";
+import { getToolCallView } from "./ToolCallViews.js";
interface ToolCallContext {
/** The primary note the tool operates on or created. */
@@ -87,7 +88,7 @@ function getErrorMessage(result: string): string {
}
/** Build the label content for a tool call section. */
-function ToolCallLabel({ toolCall }: { toolCall: ToolCall }) {
+function ToolCallLabel({ toolCall, summary }: { toolCall: ToolCall; summary?: string }) {
const { noteId: refNoteId, parentNoteId: refParentId, detailText } = getToolCallContext(toolCall);
const hasError = toolCall.isError;
@@ -112,6 +113,7 @@ function ToolCallLabel({ toolCall }: { toolCall: ToolCall }) {
)}
</span>
)}
+ {summary && <span className="llm-chat-tool-call-result-count">{summary}</span>}
{hasError && <span className="llm-chat-tool-call-error-badge">{t("llm_chat.tool_error")}</span>}
</>
);
@@ -119,7 +121,8 @@ function ToolCallLabel({ toolCall }: { toolCall: ToolCall }) {
/**
* A single tool call. It folds open only for what is worth reading inline: the input while it
- * streams, the diff of an `edit_note_content` call, or why the call failed. The raw input and
+ * streams, the diff of an `edit_note_content` call, why the call failed, or the view
+ * `getToolCallView()` builds for the tools that have one. The raw input and
* result are in the dialog the debug button opens.
*/
function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
@@ -131,13 +134,14 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
? parseNoteContentEdits(toolCall.input?.edits)
: null;
const errorMessage = hasError && toolCall.result ? getErrorMessage(toolCall.result) : null;
+ const view = isStreamingInput ? null : getToolCallView(toolCall);
const className = `llm-chat-tool-call ${hasError ? "llm-chat-tool-call-error" : ""}`;
const icon = toolCallIcon(toolCall);
- const label = <ToolCallLabel toolCall={toolCall} />;
+ const label = <ToolCallLabel toolCall={toolCall} summary={view?.summary} />;
const debugButton = <ToolCallDebugButton toolCall={toolCall} />;
- if (!isStreamingInput && !noteContentEdits && !errorMessage) {
+ if (!isStreamingInput && !noteContentEdits && !errorMessage && !view?.body) {
return (
<div className={`expandable-line ${className}`}>
<div className="expandable-line-header">
@@ -165,6 +169,7 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
</div>
)}
{errorMessage && <p className="llm-chat-tool-call-error-message">{errorMessage}</p>}
+ {view?.body}
</ExpandableSection>
);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
new file mode 100644
index 0000000000..9590eee8f2
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
@@ -0,0 +1,87 @@
+/* The notes a `search_notes` call found. */
+.llm-chat-note-results {
+ ul {
+ display: flex;
+ flex-direction: column;
+ gap: 0.1rem;
+ margin: 0;
+ padding: 0;
+ list-style: none;
+ }
+}
+
+.llm-chat-note-result {
+ position: relative;
+ padding: 0.3rem 0.5rem;
+ border-radius: 6px;
+
+ &:hover {
+ background: var(--hover-item-background-color);
+ }
+
+ /* Stretches the note link over the row, so the whole row opens the note. */
+ .llm-chat-note-result-header > a::after {
+ content: "";
+ position: absolute;
+ inset: 0;
+ }
+}
+
+.llm-chat-note-result-header {
+ display: flex;
+ align-items: baseline;
+ gap: 0.5rem;
+ min-width: 0;
+ white-space: nowrap;
+
+ > a {
+ flex-shrink: 1;
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ color: var(--main-text-color);
+ font-weight: 500;
+ text-decoration: none;
+ }
+}
+
+.llm-chat-note-result-parent {
+ flex-shrink: 10;
+ min-width: 0;
+ overflow: hidden;
+ text-overflow: ellipsis;
+ font-size: 0.75rem;
+
+ > .bx {
+ margin-right: 0.2rem;
+ vertical-align: -0.1em;
+ }
+}
+
+.llm-chat-note-result-preview {
+ display: -webkit-box;
+ -webkit-box-orient: vertical;
+ -webkit-line-clamp: 2;
+ overflow: hidden;
+ margin-top: 0.1rem;
+ font-size: 0.78rem;
+ line-height: 1.35;
+}
+
+.llm-chat-note-results-more {
+ padding: 0.2rem 0.5rem 0;
+ font-size: 0.75rem;
+ font-style: italic;
+}
+
+.llm-chat-note-results-scope {
+ display: flex;
+ align-items: baseline;
+ gap: 0.3rem;
+ padding: 0.2rem 0.5rem;
+ font-size: 0.78rem;
+
+ > .bx {
+ align-self: center;
+ }
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
new file mode 100644
index 0000000000..9c478a4eba
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -0,0 +1,130 @@
+import "./ToolCallViews.css";
+
+import type { ComponentChildren } from "preact";
+import { Trans } from "react-i18next";
+
+import { t } from "../../../services/i18n.js";
+import { NewNoteLink } from "../../react/NoteLink.js";
+import type { ToolCall } from "./llm_chat_types.js";
+
+/** What a finished call shows: a short summary beside its label, and the view it folds open to. */
+export interface ToolCallView {
+ summary?: string;
+ body?: ComponentChildren;
+}
+
+/** The view of a successful call to a tool that has one, or `null` to show the bare line. */
+export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
+ if (!toolCall.result || toolCall.isError) return null;
+
+ switch (toolCall.toolName) {
+ case "search_notes": {
+ const result = parseSearchNotesResult(toolCall.result);
+ if (!result) return null;
+ const { ancestorNoteId, limit } = toolCall.input;
+ const search: NoteSearch = {
+ ...result,
+ // Searching under the root is the same as not narrowing the search at all.
+ ancestorNoteId: typeof ancestorNoteId === "string" && ancestorNoteId !== "root" ? ancestorNoteId : undefined,
+ limit: typeof limit === "number" ? limit : result.results.length
+ };
+ return {
+ summary: t("llm_chat.search_notes_count", { count: result.totalResults }),
+ body: search.results.length > 0 || search.ancestorNoteId ? <NoteSearchResults {...search} /> : undefined
+ };
+ }
+ default:
+ return null;
+ }
+}
+
+interface NoteSearchResult {
+ noteId: string;
+ parentTitle?: string | null;
+ contentPreview?: string | null;
+}
+
+interface SearchNotesResult {
+ totalResults: number;
+ results: NoteSearchResult[];
+}
+
+/** A `search_notes` result with the input that shaped it. */
+interface NoteSearch extends SearchNotesResult {
+ ancestorNoteId?: string;
+ limit: number;
+}
+
+function NoteSearchResults({ totalResults, results, ancestorNoteId, limit }: NoteSearch) {
+ return (
+ <div className="llm-chat-note-results">
+ {ancestorNoteId && (
+ <div className="llm-chat-note-results-scope">
+ <span className="bx bx-subdirectory-right" />
+ <Trans
+ i18nKey="llm_chat.search_notes_scope"
+ components={{ Note: <NewNoteLink notePath={ancestorNoteId} showNoteIcon /> } as any}
+ />
+ </div>
+ )}
+ {results.length > 0 && (
+ <ul>
+ {results.map(({ noteId, parentTitle, contentPreview }) => {
+ const preview = contentPreview ? markdownToPlainPreview(contentPreview) : "";
+ return (
+ <li key={noteId} className="llm-chat-note-result">
+ <div className="llm-chat-note-result-header">
+ <NewNoteLink notePath={noteId} showNoteIcon />
+ {parentTitle && (
+ <span className="llm-chat-note-result-parent">
+ <span className="bx bx-folder" />{parentTitle}
+ </span>
+ )}
+ </div>
+ {preview && <div className="llm-chat-note-result-preview">{preview}</div>}
+ </li>
+ );
+ })}
+ </ul>
+ )}
+ {totalResults > results.length && (
+ <div className="llm-chat-note-results-more">
+ {t("llm_chat.search_notes_limited", { count: totalResults, limit })}
+ </div>
+ )}
+ </div>
+ );
+}
+
+function parseSearchNotesResult(result: string): SearchNotesResult | null {
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(result);
+ } catch {
+ return null;
+ }
+ if (typeof parsed !== "object" || parsed === null) return null;
+
+ const { totalResults, results } = parsed as Partial<Record<keyof SearchNotesResult, unknown>>;
+ if (typeof totalResults !== "number" || !Array.isArray(results)) return null;
+ const notes = results.filter((item): item is NoteSearchResult =>
+ typeof item === "object" && item !== null && typeof item.noteId === "string");
+ return { totalResults, results: notes };
+}
+
+/**
+ * Flattens the Markdown of `contentPreview` into one line of plain text. The size notice that
+ * `getContentPreview()` sends for a large note in place of its text yields an empty string.
+ */
+export function markdownToPlainPreview(markdown: string): string {
+ if (/^\[\d+KB - /.test(markdown)) return "";
+
+ return markdown
+ .replace(/!\[[^\]]*\]\([^)]*\)/g, "")
+ .replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
+ .replace(/^\s*\|?[\s:|-]+\|?\s*$/gm, "")
+ .replace(/^\s{0,3}(#{1,6}|>|[-*+]|\d+\.)\s+/gm, "")
+ .replace(/\*\*|__|~~|`|\|/g, "")
+ .replace(/\s+/g, " ")
+ .trim();
+}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
index 48fe2803ff..c7b4458604 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
@@ -184,10 +184,13 @@ class="admonition note">
</ul>
<p>Each tool the AI uses appears in the chat as a single line naming the
tool and the note or query it worked on. A line folds open only when there
- is something to read inline: the changes of an edit, or the reason a tool
- failed. To see exactly what the AI sent to a tool and what it got back,
- hover the line and press the <span class="tn-icon bx bx-code-alt"></span> button
- next to it; on touch screens the button is always shown.</p>
+ is something to read inline: the notes a search found, the changes of an
+ edit, or the reason a tool failed. A search also shows how many notes it
+ found, the part of the tree it was confined to, if any, and how many of
+ the matches the AI asked for; each note it lists opens with a click. To
+ see exactly what the AI sent to a tool and what it got back, hover the
+ line and press the <span class="tn-icon bx bx-code-alt"></span> button next
+ to it; on touch screens the button is always shown.</p>
<aside class="admonition warning">
<p>Currently there is <strong>no permission management</strong> implemented
for note tools, meaning that the LLM could potentially remove existing
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index e791d4d862..b3b2cc68ae 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -131,7 +131,7 @@ Here are a few tools that Trilium provides for the LLM:
* Get the content of an attachment.
* Skills (see the dedicated section).
-Each tool the AI uses appears in the chat as a single line naming the tool and the note or query it worked on. A line folds open only when there is something to read inline: the changes of an edit, or the reason a tool failed. To see exactly what the AI sent to a tool and what it got back, hover the line and press the <span class="tn-icon bx bx-code-alt"></span> button next to it; on touch screens the button is always shown.
+Each tool the AI uses appears in the chat as a single line naming the tool and the note or query it worked on. A line folds open only when there is something to read inline: the notes a search found, the changes of an edit, or the reason a tool failed. A search also shows how many notes it found, the part of the tree it was confined to, if any, and how many of the matches the AI asked for; each note it lists opens with a click. To see exactly what the AI sent to a tool and what it got back, hover the line and press the <span class="tn-icon bx bx-code-alt"></span> button next to it; on touch screens the button is always shown.
> [!WARNING]
> Currently there is **no permission management** implemented for note tools, meaning that the LLM could potentially remove existing notes or clutter the tree with notes. Generally most actions are easily reversible (deleting the notes, restoring deleted notes, reverting modifications to a note), but there are some that are harder to revert (e.g. setting an attribute because there is no attribute history).