Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): show what a get_note call learned about the note
Elian Doran7120d68e09ad
diff --git a/apps/client/src/entities/fattribute.ts b/apps/client/src/entities/fattribute.tsindex 7d1a31e126..a8117afcae 100644--- a/apps/client/src/entities/fattribute.ts+++ b/apps/client/src/entities/fattribute.ts@@ -67,15 +67,7 @@ class FAttribute {}get isAutoLink() {- if (this.type === "relation") {- return ["internalLink", "imageLink", "relationMapLink", "includeNoteLink"].includes(this.name);- }-- if (this.type === "label") {- return this.name === "internalBookmark";- }-- return false;+ return isAutoLinkAttribute(this.type, this.name);}get toString() {@@ -112,4 +104,12 @@ export function isDefinitionName(name: string) {return DEFINITION_PREFIXES.some((prefix) => name.startsWith(prefix) && name.length > prefix.length);}+/** Whether an attribute is one Trilium maintains from a note's links rather than one the user set. */+export function isAutoLinkAttribute(type: string, name: string) {+ if (type === "relation") {+ return [ "internalLink", "imageLink", "relationMapLink", "includeNoteLink" ].includes(name);+ }+ return type === "label" && name === "internalBookmark";+}+export default FAttribute;diff --git a/apps/client/src/services/attribute_renderer.ts b/apps/client/src/services/attribute_renderer.tsindex c93620b3b2..27cc37559b 100644--- a/apps/client/src/services/attribute_renderer.ts+++ b/apps/client/src/services/attribute_renderer.ts@@ -36,7 +36,7 @@ async function renderAttribute(attribute: FAttribute, renderIsInheritable: boolereturn $attr;}-function formatValue(val: string) {+export function formatValue(val: string) {if (/^[\p{L}\p{N}\-_,.]+$/u.test(val)) {return val;} else if (!val.includes('"')) {diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 4cba7bcc30..3810209b67 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2673,7 +2673,9 @@"search_help_count_one": "({{count}} page)","search_help_count_other": "({{count}} pages)","subtree_more_one": "+{{count}} more",- "subtree_more_other": "+{{count}} more"+ "subtree_more_other": "+{{count}} more",+ "attachment_count_one": "{{count}} attachment",+ "attachment_count_other": "{{count}} attachments"},"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.tsxindex 2303929ae7..3d13c15cf8 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@@ -276,4 +276,39 @@ describe("ToolCallCard", () => {expect(more?.textContent).toBe("llm_chat.subtree_more{\"count\":3}");expect(more?.closest(".llm-chat-note-result")).toBeNull();});++ it("shows what a get_note call learned: type, counts, attributes and a preview", () => {+ const target = renderCard([ {+ id: "1",+ toolName: "get_note",+ input: { noteId: "n" },+ result: JSON.stringify({+ noteId: "n",+ title: "N",+ type: "code",+ mime: "text/x-markdown",+ childNotes: { totalCount: 12, results: [] },+ attributes: {+ totalCount: 5,+ results: [+ { type: "label", name: "book", value: "" },+ { type: "label", name: "status", value: "in progress" },+ { type: "relation", name: "author", value: "tolkien" },+ { type: "relation", name: "internalLink", value: "other" }+ ]+ },+ attachments: { totalCount: 1, results: [] },+ contentPreview: "# Title\n\nSome **text**"+ })+ } ]);+ const card = target.querySelector("details.llm-chat-tool-call .llm-chat-note-card");+ expect(card?.querySelector(".llm-chat-note-card-facts")?.textContent)+ .toBe("note_types.markdown · llm_chat.child_count{\"count\":12} · llm_chat.attachment_count{\"count\":1}");+ expect([ ...(card?.querySelectorAll(".llm-chat-attribute") ?? []) ].map(pill => pill.textContent))+ .toEqual([ "#book", "#status=\"in progress\"", "~author=tolkien" ]);+ expect(card?.querySelector(".llm-chat-attribute .note-link-stub")?.textContent).toBe("tolkien");+ expect(card?.querySelector(".llm-chat-note-card-attributes .llm-chat-note-results-more")?.textContent)+ .toBe("llm_chat.subtree_more{\"count\":1}");+ expect(card?.querySelector(".llm-chat-note-result-preview")?.textContent).toBe("Title Some text");+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.cssindex ab87811361..931786f1bc 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css@@ -86,3 +86,31 @@padding-bottom: 0;}}++/* What a `get_note` call learned about a note. */+.llm-chat-note-card {+ display: flex;+ flex-direction: column;+ gap: 0.3rem;+ padding: 0.2rem 0;+ font-size: 0.78rem;+}++.llm-chat-note-card-attributes {+ display: flex;+ flex-wrap: wrap;+ align-items: baseline;+ gap: 0.25rem;++ > .llm-chat-note-results-more {+ padding: 0;+ }+}++.llm-chat-attribute {+ padding: 0 0.35rem;+ border-radius: 4px;+ background: var(--accented-background-color);+ color: var(--main-text-color);+ overflow-wrap: anywhere;+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsxindex a84c38bffe..30ef5ed9d1 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx@@ -3,7 +3,10 @@ import "./ToolCallViews.css";import type { ComponentChildren } from "preact";import { Trans } from "react-i18next";+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 { NewNoteLink } from "../../react/NoteLink.js";import type { ToolCall } from "./llm_chat_types.js";@@ -60,6 +63,10 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {body: children.length > 0 ? <ChildNoteList notes={children} /> : undefined};}+ case "get_note": {+ const meta = parseNoteMeta(toolCall.result);+ return meta ? { body: <NoteMetaCard {...meta} /> } : null;+ }case "get_subtree": {const nodes = parseSubtreeResult(toolCall.result);if (!nodes) return null;@@ -206,6 +213,102 @@ function NoteResultRow({ noteId, preview, nested, onLinkClick, children }: {);}+/** A capped list in a `get_note` result: `totalCount` counts past the entries it lists. */+interface CappedList<T> {+ totalCount: number;+ results: T[];+}++interface NoteMetaAttribute {+ type: string;+ name: string;+ value: string;+}++interface NoteMeta {+ type: string;+ mime?: string;+ childCount: number;+ attachmentCount: number;+ attributes: CappedList<NoteMetaAttribute>;+ contentPreview?: string | null;+}++/** What a `get_note` call learned about the note its summary line links. */+function NoteMetaCard({ type, mime, childCount, attachmentCount, attributes, contentPreview }: NoteMeta) {+ const noteType = NOTE_TYPES.find((nt) => nt.type === type && nt.mime === mime) ?? NOTE_TYPES.find((nt) => nt.type === type);+ const facts = [+ noteType?.title ?? type,+ childCount > 0 && t("llm_chat.child_count", { count: childCount }),+ attachmentCount > 0 && t("llm_chat.attachment_count", { count: attachmentCount })+ ].filter(Boolean);+ const shown = attributes.results.filter(({ type, name }) => !isAutoLinkAttribute(type, name));+ const hiddenAttributes = attributes.totalCount - attributes.results.length;+ const preview = contentPreview ? markdownToPlainPreview(contentPreview) : "";++ return (+ <div className="llm-chat-note-card">+ <div className="llm-chat-note-card-facts">{facts.join(" · ")}</div>+ {(shown.length > 0 || hiddenAttributes > 0) && (+ <div className="llm-chat-note-card-attributes">+ {shown.map((attribute, idx) => <AttributePill key={idx} {...attribute} />)}+ {hiddenAttributes > 0 && (+ <span className="llm-chat-note-results-more">{t("llm_chat.subtree_more", { count: hiddenAttributes })}</span>+ )}+ </div>+ )}+ {preview && <div className="llm-chat-note-result-preview">{preview}</div>}+ </div>+ );+}++/**+ * A label or relation as the model read it, written the way `attribute_renderer` writes the attribute+ * bar's. It draws from the result rather than froca, which holds the attribute as it is now.+ */+function AttributePill({ type, name, value }: NoteMetaAttribute) {+ if (type === "relation") {+ return (+ <span className="llm-chat-attribute">+ ~{name}={value && <NewNoteLink notePath={value} showNoteIcon />}+ </span>+ );+ }+ return <span className="llm-chat-attribute">#{name}{value && `=${formatValue(value)}`}</span>;+}++function parseNoteMeta(result: string): NoteMeta | null {+ let parsed: unknown;+ try {+ parsed = JSON.parse(result);+ } catch {+ return null;+ }+ if (!isRecord(parsed) || typeof parsed.noteId !== "string" || typeof parsed.type !== "string") return null;++ const attributes = parseCappedList(parsed.attributes);+ return {+ type: parsed.type,+ mime: typeof parsed.mime === "string" ? parsed.mime : undefined,+ childCount: parseCappedList(parsed.childNotes).totalCount,+ attachmentCount: parseCappedList(parsed.attachments).totalCount,+ attributes: {+ totalCount: attributes.totalCount,+ results: attributes.results.filter((item): item is NoteMetaAttribute =>+ isRecord(item) && typeof item.type === "string" && typeof item.name === "string" && typeof item.value === "string")+ },+ contentPreview: typeof parsed.contentPreview === "string" ? parsed.contentPreview : null+ };+}++function parseCappedList(value: unknown): CappedList<unknown> {+ if (!isRecord(value) || !Array.isArray(value.results)) return { totalCount: 0, results: [] };+ return {+ totalCount: typeof value.totalCount === "number" ? value.totalCount : value.results.length,+ results: value.results+ };+}+/*** A note of a `get_subtree` result, or the marker that stands for the children past the ten a level* lists. `hiddenChildren` counts the children past the depth limit.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.htmlindex 11008378d5..a398af0535 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,14 @@ class="admonition note"></ul><p>Each tool the AI uses appears in the chat as a single line naming thetool 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 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+ 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> buttonnext to it; on touch screens the button is always shown.</p><aside class="admonition warning"><p>Currently there is <strong>no permission management</strong> implementeddiff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 3c0305caa3..639562f234 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 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 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.> [!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).