Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): name an attachment the AI read, and preview its text

Elian Doran0ba478671bdf
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f46982750c..66f758b030 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2679,7 +2679,9 @@
"attribute_count_one": "({{count}} attribute)",
"attribute_count_other": "({{count}} attributes)",
"search_icons_count_one": "({{count}} icon)",
- "search_icons_count_other": "({{count}} icons)"
+ "search_icons_count_other": "({{count}} icons)",
+ "attachment_size": "({{size}})",
+ "attachment_ocr": "Text recognized from the file"
},
"sidebar_chat": {
"title": "AI Chat",
@@ -4157,7 +4159,8 @@
"get_help_toc": "Get User Guide contents",
"read_web_page": "Read web page",
"note_moved_from": "<Note/> from <From/> to <To/>",
- "note_moved": "<Note/> to <To/>"
+ "note_moved": "<Note/> to <To/>",
+ "attachment_in_note": "<Title/> in <Note/>"
}
},
"common": {
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 556cd2e350..a28381b3ff 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
@@ -10,6 +10,7 @@
}
.llm-chat-tool-call-detail,
+.llm-chat-tool-call-attachment-title,
.llm-chat-tool-call-count,
.llm-chat-tool-call-result-count {
font-size: 0.75rem;
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 186cb1de9d..e678f461b6 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
@@ -598,4 +598,30 @@ describe("ToolCallCard", () => {
expect(line?.querySelector(".llm-chat-note-results-more")?.textContent)
.toBe("llm_chat.search_notes_limited{\"count\":5,\"limit\":2}");
});
+
+ it("names an attachment and its note, and previews the text read from one", () => {
+ const target = renderCard([
+ {
+ id: "1",
+ toolName: "get_attachment",
+ input: { attachmentId: "a" },
+ result: JSON.stringify({ attachmentId: "a", ownerId: "n", role: "file", mime: "application/pdf", title: "report.pdf", contentLength: 2048 })
+ },
+ {
+ id: "2",
+ toolName: "get_attachment_content",
+ input: { attachmentId: "a" },
+ result: JSON.stringify({ attachmentId: "a", source: "ocr", content: "Quarterly **report**" })
+ }
+ ]);
+ const [ meta, content ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+
+ expect(meta instanceof HTMLDetailsElement).toBe(false);
+ expect(meta?.querySelector(".llm-chat-tool-call-attachment-title")?.textContent).toBe("report.pdf");
+ expect(meta?.querySelector(".note-link-stub")?.textContent).toBe("n");
+ expect(meta?.querySelector(".llm-chat-tool-call-result-count")?.textContent).toBe("llm_chat.attachment_size{\"size\":\"2 KiB\"}");
+
+ expect(content?.querySelector(".llm-chat-note-card-facts")?.textContent).toBe("llm_chat.attachment_ocr");
+ expect(content?.querySelector(".llm-chat-note-result-preview")?.textContent).toBe("Quarterly report");
+ });
});
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 5eb5a87e19..7056243417 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -7,7 +7,7 @@ import { isAutoLinkAttribute } from "../../../entities/fattribute.js";
import { formatValue } from "../../../services/attribute_renderer.js";
import { t } from "../../../services/i18n.js";
import { NOTE_TYPES } from "../../../services/note_types.js";
-import { openInAppHelpFromUrl } from "../../../services/utils.js";
+import { formatSize, openInAppHelpFromUrl } from "../../../services/utils.js";
import CodeBlock from "../../react/CodeBlock.js";
import { useNote } from "../../react/hooks.js";
import { TooltipIcon } from "../../react/Icon.js";
@@ -130,6 +130,39 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
const oldTitle = parseStringField(toolCall.result, "oldTitle");
return oldTitle ? { lead: <span className="llm-chat-tool-call-old-title">{oldTitle}</span> } : null;
}
+ case "get_attachment": {
+ // The input names only the attachment's ID, so its title and owner come from the result.
+ const result = parseJson(toolCall.result);
+ if (!isRecord(result) || typeof result.title !== "string" || typeof result.ownerId !== "string") return null;
+ const components = {
+ Title: <span className="llm-chat-tool-call-attachment-title">{result.title}</span>,
+ Note: <NewNoteLink notePath={result.ownerId} showNoteIcon noPreview />
+ };
+ return {
+ lead: (
+ <span className="llm-chat-tool-call-note-ref">
+ <Trans i18nKey="llm.tools.attachment_in_note" components={components as any} />
+ </span>
+ ),
+ summary: typeof result.contentLength === "number"
+ ? t("llm_chat.attachment_size", { size: formatSize(result.contentLength) })
+ : undefined
+ };
+ }
+ case "get_attachment_content": {
+ const content = parseStringField(toolCall.result, "content");
+ const preview = content ? markdownToPlainPreview(content.slice(0, CONTENT_PREVIEW_SOURCE_LENGTH)) : "";
+ if (!preview) return null;
+ const isOcr = parseStringField(toolCall.result, "source") === "ocr";
+ return {
+ body: (
+ <div className="llm-chat-note-card">
+ {isOcr && <div className="llm-chat-note-card-facts">{t("llm_chat.attachment_ocr")}</div>}
+ <div className="llm-chat-note-result-preview">{preview}</div>
+ </div>
+ )
+ };
+ }
case "get_note": {
const meta = parseNoteMeta(toolCall.result);
return meta ? { body: <NoteMetaCard {...meta} /> } : null;
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 6ae10be7d0..493f25b7a8 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
@@ -194,18 +194,19 @@ 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 renamed note shows its previous
title struck through, a deleted note its title struck through, a moved
- note where it was moved from and to, and an attribute the AI read, set
- or deleted as a pill (struck through once deleted). A line folds open only
- when there is something to read inline: the notes a search found, the icons
- an icon search found, the type, attributes or start of the content of a
- note the AI read, all the attributes of a note, the User Guide pages the
- AI looked up (which open in the help panel), the children or the whole
- subtree of a note, what the AI wrote into a note it created, rewrote or
- added to, 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
+ note where it was moved from and to, an attribute the AI read, set or deleted
+ as a pill (struck through once deleted), and an attachment by its title,
+ note and size. A line folds open only when there is something to read inline:
+ the notes a search found, the icons an icon search found, the type, attributes
+ or start of the content of a note the AI read, all the attributes of a
+ note, the text of an attachment, the User Guide pages the AI looked up
+ (which open in the help panel), the children or the whole subtree of a
+ note, what the AI wrote into a note it created, rewrote or added to, 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 8db0d34b97..491394edac 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -133,7 +133,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 renamed note shows its previous title struck through, a deleted note its title struck through, a moved note where it was moved from and to, and an attribute the AI read, set or deleted as a pill (struck through once deleted). A line folds open only when there is something to read inline: the notes a search found, the icons an icon search found, the type, attributes or start of the content of a note the AI read, all the attributes of a note, the User Guide pages the AI looked up (which open in the help panel), the children or the whole subtree of a note, what the AI wrote into a note it created, rewrote or added to, 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 renamed note shows its previous title struck through, a deleted note its title struck through, a moved note where it was moved from and to, an attribute the AI read, set or deleted as a pill (struck through once deleted), and an attachment by its title, note and size. A line folds open only when there is something to read inline: the notes a search found, the icons an icon search found, the type, attributes or start of the content of a note the AI read, all the attributes of a note, the text of an attachment, the User Guide pages the AI looked up (which open in the help panel), the children or the whole subtree of a note, what the AI wrote into a note it created, rewrote or added to, 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).