Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): preview the content a get_note_content call read

Elian Doran48e0a4cb75ea
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 3d13c15cf8..a684dfaf5b 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
@@ -311,4 +311,23 @@ describe("ToolCallCard", () => {
.toBe("llm_chat.subtree_more{\"count\":1}");
expect(card?.querySelector(".llm-chat-note-result-preview")?.textContent).toBe("Title Some text");
});
+
+ it("previews the content a get_note_content call read, and keeps an empty note to a plain line", () => {
+ const target = renderCard([
+ {
+ id: "1",
+ toolName: "get_note_content",
+ input: { noteId: "n" },
+ result: JSON.stringify({ noteId: "n", content: `# Title\n\nSome **text**${" more".repeat(400)}` })
+ },
+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },
+ { id: "3", toolName: "get_note_content", input: { noteId: "e" }, result: JSON.stringify({ noteId: "e", content: "" }) }
+ ]);
+ const [ read, , empty ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+ const preview = read?.querySelector(".llm-chat-note-card .llm-chat-note-result-preview")?.textContent;
+ expect(preview?.startsWith("Title Some text more more")).toBe(true);
+ expect(preview?.length).toBeLessThan(1000);
+ expect(read?.querySelector(".llm-chat-note-card-facts")).toBeNull();
+ expect(empty instanceof HTMLDetailsElement).toBe(false);
+ });
});
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 30ef5ed9d1..6e3eed64a8 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -12,6 +12,7 @@ import { NewNoteLink } from "../../react/NoteLink.js";
import type { ToolCall } from "./llm_chat_types.js";
const HELP_NOTE_PREFIX = "_help_";
+const CONTENT_PREVIEW_SOURCE_LENGTH = 1000;
/** What a finished call shows: a short summary beside its label, and the view it folds open to. */
export interface ToolCallView {
@@ -67,6 +68,12 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
const meta = parseNoteMeta(toolCall.result);
return meta ? { body: <NoteMetaCard {...meta} /> } : null;
}
+ case "get_note_content": {
+ const content = parseNoteContent(toolCall.result);
+ // A preview shows two lines, so the start of a long note is enough to flatten.
+ const preview = content ? markdownToPlainPreview(content.slice(0, CONTENT_PREVIEW_SOURCE_LENGTH)) : "";
+ return preview ? { body: <div className="llm-chat-note-card"><div className="llm-chat-note-result-preview">{preview}</div></div> } : null;
+ }
case "get_subtree": {
const nodes = parseSubtreeResult(toolCall.result);
if (!nodes) return null;
@@ -301,6 +308,15 @@ function parseNoteMeta(result: string): NoteMeta | null {
};
}
+function parseNoteContent(result: string): string | null {
+ try {
+ const parsed: unknown = JSON.parse(result);
+ return isRecord(parsed) && typeof parsed.content === "string" ? parsed.content : null;
+ } catch {
+ return null;
+ }
+}
+
function parseCappedList(value: unknown): CappedList<unknown> {
if (!isRecord(value) || !Array.isArray(value.results)) return { totalCount: 0, results: [] };
return {
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 a398af0535..ad3498bef8 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
@@ -185,13 +185,13 @@ class="admonition note">
<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 type, attributes
- and start of a note the AI read, the User Guide pages the AI looked up
- (which open in the help panel), the children or the whole subtree 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
+ or start of the content of a note the AI read, the User Guide pages the
+ AI looked up (which open in the help panel), the children or the whole
+ subtree 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
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index 639562f234..6cdf100d5d 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 type, attributes and start of a note the AI read, the User Guide pages the AI looked up (which open in the help panel), the children or the whole subtree 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.
+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 type, attributes or start of the content of a note the AI read, the User Guide pages the AI looked up (which open in the help panel), the children or the whole subtree 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).