Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): list the children a get_child_notes call read

Elian Doran2cb045cf02dd
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f30207fe78..cbd6de55cf 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2665,7 +2665,11 @@
"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/>"
+ "search_notes_scope": "Searched in <Note/>",
+ "child_notes_count_one": "({{count}} child)",
+ "child_notes_count_other": "({{count}} children)",
+ "child_count_one": "{{count}} child",
+ "child_count_other": "{{count}} children"
},
"sidebar_chat": {
"title": "AI Chat",
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 c12813782a..53e5c65c1c 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
@@ -156,4 +156,38 @@ describe("ToolCallCard", () => {
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();
});
+
+ it("lists the children a call read, each with how many children it has", () => {
+ const target = renderCard([
+ {
+ id: "1",
+ toolName: "get_child_notes",
+ input: { noteId: "parent" },
+ result: JSON.stringify([
+ { noteId: "a", title: "Alpha", type: "text", childCount: 3 },
+ { noteId: "b", title: "Beta", type: "text", childCount: 0 }
+ ])
+ },
+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },
+ { id: "3", toolName: "get_child_notes", input: { noteId: "leaf" }, result: "[]" }
+ ]);
+ const [ children, , empty ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+
+ expect(children instanceof HTMLDetailsElement).toBe(true);
+ expect(children?.querySelector(".llm-chat-tool-call-note-ref .note-link-stub")?.textContent).toBe("parent");
+ expect(children?.querySelector(".llm-chat-tool-call-result-count")?.textContent)
+ .toBe("llm_chat.child_notes_count{\"count\":2}");
+ const rows = [ ...(children?.querySelectorAll(".llm-chat-note-result") ?? []) ];
+ expect(rows.map(row => ({
+ note: row.querySelector(".note-link-stub")?.textContent,
+ detail: row.querySelector(".llm-chat-note-result-detail")?.textContent ?? null
+ }))).toEqual([
+ { note: "a", detail: "llm_chat.child_count{\"count\":3}" },
+ { note: "b", detail: null }
+ ]);
+
+ expect(empty instanceof HTMLDetailsElement).toBe(false);
+ expect(empty?.querySelector(".llm-chat-tool-call-result-count")?.textContent)
+ .toBe("llm_chat.child_notes_count{\"count\":0}");
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
index d662d19c90..0a5743b6c8 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
@@ -29,7 +29,8 @@
}
}
-.llm-chat-note-result-parent {
+.llm-chat-note-result-parent,
+.llm-chat-note-result-detail {
flex-shrink: 10;
min-width: 0;
overflow: hidden;
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
index 9c478a4eba..ac7f3d5002 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -33,6 +33,14 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
body: search.results.length > 0 || search.ancestorNoteId ? <NoteSearchResults {...search} /> : undefined
};
}
+ case "get_child_notes": {
+ const children = parseChildNotesResult(toolCall.result);
+ if (!children) return null;
+ return {
+ summary: t("llm_chat.child_notes_count", { count: children.length }),
+ body: children.length > 0 ? <ChildNoteList notes={children} /> : undefined
+ };
+ }
default:
return null;
}
@@ -69,22 +77,19 @@ function NoteSearchResults({ totalResults, results, ancestorNoteId, limit }: Not
)}
{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>
- );
- })}
+ {results.map(({ noteId, parentTitle, contentPreview }) => (
+ <NoteResultRow
+ key={noteId}
+ noteId={noteId}
+ preview={contentPreview ? markdownToPlainPreview(contentPreview) : ""}
+ >
+ {parentTitle && (
+ <span className="llm-chat-note-result-parent">
+ <span className="bx bx-folder" />{parentTitle}
+ </span>
+ )}
+ </NoteResultRow>
+ ))}
</ul>
)}
{totalResults > results.length && (
@@ -96,6 +101,56 @@ function NoteSearchResults({ totalResults, results, ancestorNoteId, limit }: Not
);
}
+function ChildNoteList({ notes }: { notes: ChildNote[] }) {
+ return (
+ <div className="llm-chat-note-results">
+ <ul>
+ {notes.map(({ noteId, childCount }) => (
+ <NoteResultRow key={noteId} noteId={noteId}>
+ {childCount > 0 && (
+ <span className="llm-chat-note-result-detail">
+ {t("llm_chat.child_count", { count: childCount })}
+ </span>
+ )}
+ </NoteResultRow>
+ ))}
+ </ul>
+ </div>
+ );
+}
+
+/** One note in a list a tool returned: its link, muted details beside it, and a preview below. */
+function NoteResultRow({ noteId, preview, children }: { noteId: string; preview?: string; children?: ComponentChildren }) {
+ return (
+ <li className="llm-chat-note-result">
+ <div className="llm-chat-note-result-header">
+ <NewNoteLink notePath={noteId} showNoteIcon />
+ {children}
+ </div>
+ {preview && <div className="llm-chat-note-result-preview">{preview}</div>}
+ </li>
+ );
+}
+
+interface ChildNote {
+ noteId: string;
+ childCount: number;
+}
+
+function parseChildNotesResult(result: string): ChildNote[] | null {
+ let parsed: unknown;
+ try {
+ parsed = JSON.parse(result);
+ } catch {
+ return null;
+ }
+ if (!Array.isArray(parsed)) return null;
+ return parsed
+ .filter((item): item is { noteId: string; childCount?: unknown } =>
+ typeof item === "object" && item !== null && typeof item.noteId === "string")
+ .map(({ noteId, childCount }) => ({ noteId, childCount: typeof childCount === "number" ? childCount : 0 }));
+}
+
function parseSearchNotesResult(result: string): SearchNotesResult | null {
let parsed: unknown;
try {
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 c7b4458604..0693447735 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,13 +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 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>
+ is something to read inline: the notes a search found, the children of
+ a note, 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 b3b2cc68ae..e38cf7bca9 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 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.
+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 children of a note, 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).