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 attributes as pills and a deleted note by its title
Elian Doran017a706a964f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex ce1cf15575..8ec0a65bdf 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2675,7 +2675,9 @@"subtree_more_one": "+{{count}} more","subtree_more_other": "+{{count}} more","attachment_count_one": "{{count}} attachment",- "attachment_count_other": "{{count}} attachments"+ "attachment_count_other": "{{count}} attachments",+ "attribute_count_one": "({{count}} attribute)",+ "attribute_count_other": "({{count}} attributes)"},"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.cssindex 1ddf76bfcf..3b703bcfd9 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css@@ -85,16 +85,18 @@50% { opacity: 0; }}-/* The title a rename replaced, struck through and followed by an arrow to the note link. The arrow is- an inline block, which keeps the strike-through off it. */-.llm-chat-tool-call-old-title {+/* The title a note had before a rename or a deletion, struck through. After a rename an arrow leads+ to the note link; the arrow is an inline block, which keeps the strike-through off it. */+.llm-chat-tool-call-old-title,+.llm-chat-tool-call-deleted-title {font-size: 0.75rem;opacity: 0.75;text-decoration: line-through;+}- &::after {- content: "→";- display: inline-block;- margin-left: 0.3rem;- }+.llm-chat-tool-call-old-title::after {+ content: "→";+ display: inline-block;+ margin-left: 0.3rem;}+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 e422b310bf..07ee4e3a7b 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@@ -515,4 +515,65 @@ describe("ToolCallCard", () => {expect(ref(moved)?.textContent).toBe("llm.tools.note_moved_fromnfromto");expect(ref(older)?.textContent).toBe("llm.tools.note_movednto");});++ it("shows a deleted note by the title it had, since its link has nothing left to show", () => {+ const target = renderCard([+ {+ id: "1",+ toolName: "delete_note",+ input: { noteId: "d" },+ result: JSON.stringify({ success: true, noteId: "d", deletedTitle: "Doomed" })+ },+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },+ { id: "3", toolName: "delete_note", input: { noteId: "p" } }+ ]);+ const [ deleted, , pending ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];++ expect(deleted?.querySelector(".llm-chat-tool-call-deleted-title")?.textContent).toBe("Doomed");+ expect(deleted?.querySelector(".note-link-stub")).toBeNull();+ expect(pending?.querySelector(".note-link-stub")?.textContent).toBe("p");+ expect(pending?.querySelector(".llm-chat-tool-call-deleted-title")).toBeNull();+ });++ it("shows the attributes each attribute tool read, set or deleted, as pills", () => {+ const target = renderCard([+ {+ id: "1",+ toolName: "get_attributes",+ input: { noteId: "n" },+ result: JSON.stringify([+ { attributeId: "a1", type: "label", name: "book", value: "" },+ { attributeId: "a2", type: "relation", name: "author", value: "tolkien" }+ ])+ },+ {+ id: "2",+ toolName: "get_attribute",+ input: { attributeId: "a3" },+ result: JSON.stringify({ attributeId: "a3", noteId: "n", type: "label", name: "status", value: "done" })+ },+ { id: "3", toolName: "set_attribute", input: { noteId: "n", type: "label", name: "year", value: "1954" } },+ {+ id: "4",+ toolName: "delete_attribute",+ input: { noteId: "n", attributeId: "a4" },+ result: JSON.stringify({ success: true, attributeId: "a4", type: "label", name: "temp", value: "1" })+ }+ ]);+ const [ list, single, set, deleted ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];+ const pills = (line: Element | undefined) => [ ...(line?.querySelectorAll(".llm-chat-attribute") ?? []) ].map(pill => pill.textContent);++ expect(list instanceof HTMLDetailsElement).toBe(true);+ expect(list?.querySelector(".llm-chat-tool-call-result-count")?.textContent).toBe("llm_chat.attribute_count{\"count\":2}");+ expect(pills(list)).toEqual([ "#book", "~author=tolkien" ]);++ expect(single instanceof HTMLDetailsElement).toBe(false);+ expect(pills(single)).toEqual([ "#status=done" ]);+ expect(single?.querySelector(".llm-chat-tool-call-note-ref .note-link-stub")?.textContent).toBe("n");++ expect(pills(set)).toEqual([ "#year=1954" ]);++ expect(pills(deleted)).toEqual([ "#temp=1" ]);+ expect(deleted?.querySelector(".llm-chat-attribute")?.classList.contains("llm-chat-attribute-deleted")).toBe(true);+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex fab87f4ba6..f79f7160d2 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -18,6 +18,8 @@ interface ToolCallContext {parentNoteId: string | null;/** Where `move_note` took the note: the new parent, and the old one when the result names it. */move?: { toNoteId: string; fromNoteId: string | null };+ /** The title of a note `delete_note` deleted, shown in place of a link to a note that is gone. */+ deletedTitle?: string;/** Plain-text detail (e.g. skill name, search query) when no note ref is available. */detailText: string | null;}@@ -48,6 +50,13 @@ function getToolCallContext(toolCall: ToolCall): ToolCallContext {return { noteId: input.noteId, parentNoteId: null, move, detailText: null };}+ const deletedTitle = toolCall.toolName === "delete_note"+ ? parseResultField(toolCall, "deletedTitle")+ : null;+ if (deletedTitle) {+ return { noteId: null, parentNoteId: null, deletedTitle, detailText: null };+ }+// For creation tools, the created note ID is in the result.if (parentNoteId) {const createdNoteId = parseResultField(toolCall, "noteId");@@ -99,7 +108,8 @@ function getErrorMessage(result: string): string {/** Build the label content for a tool call section. */function ToolCallLabel({ toolCall, view }: { toolCall: ToolCall; view: ToolCallView | null }) {- const { noteId: refNoteId, parentNoteId: refParentId, move, detailText } = getToolCallContext(toolCall);+ const context = getToolCallContext(toolCall);+ const { noteId: refNoteId, parentNoteId: refParentId, move, deletedTitle, detailText } = context;const hasError = isFailedToolCall(toolCall);return (@@ -109,6 +119,7 @@ function ToolCallLabel({ toolCall, view }: { toolCall: ToolCall; view: ToolCallV<span className="llm-chat-tool-call-detail">{detailText}</span>)}{view?.lead}+ {deletedTitle && <span className="llm-chat-tool-call-deleted-title">{deletedTitle}</span>}{refNoteId && (<span className="llm-chat-tool-call-note-ref"><NoteRef noteId={refNoteId} parentNoteId={refParentId} move={move} />diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.cssindex be666b515a..423ee75f9f 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css@@ -138,3 +138,13 @@max-width: 100%;max-height: 280px;}++.llm-chat-attribute-deleted {+ color: var(--muted-text-color);+ text-decoration: line-through;+}++/* An attribute pill on a summary line sits at the size of the line's other details. */+.expandable-section-label > .llm-chat-attribute {+ font-size: 0.75rem;+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsxindex 82c941fb62..7c0b6f162b 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx@@ -50,9 +50,30 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {) };}+ if (toolCall.toolName === "set_attribute") {+ const attribute = parseAttribute(toolCall.input);+ return attribute ? { lead: <AttributePill {...attribute} /> } : null;+ }+if (!toolCall.result) return null;switch (toolCall.toolName) {+ case "get_attributes": {+ const attributes = parseAttributeList(toolCall.result);+ if (!attributes) return null;+ return {+ summary: t("llm_chat.attribute_count", { count: attributes.length }),+ body: attributes.length > 0+ ? <div className="llm-chat-note-card"><AttributeRow attributes={attributes} /></div>+ : undefined+ };+ }+ case "get_attribute":+ case "delete_attribute": {+ const attribute = parseAttribute(parseJson(toolCall.result));+ const deleted = toolCall.toolName === "delete_attribute";+ return attribute ? { lead: <AttributePill {...attribute} deleted={deleted} /> } : null;+ }case "search_notes": {const result = parseSearchNotesResult(toolCall.result);if (!result) return null;@@ -342,39 +363,67 @@ function NoteMetaCard({ type, mime, childCount, attachmentCount, attributes, conchildCount > 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>- )}+ <AttributeRow attributes={attributes.results} hiddenCount={attributes.totalCount - attributes.results.length} />{preview && <div className="llm-chat-note-result-preview">{preview}</div>}</div>);}+/** A note's attributes as pills, leaving out the ones Trilium keeps for links, as the attribute bar does. */+function AttributeRow({ attributes, hiddenCount = 0 }: { attributes: NoteMetaAttribute[]; hiddenCount?: number }) {+ const shown = attributes.filter(({ type, name }) => !isAutoLinkAttribute(type, name));+ if (shown.length === 0 && hiddenCount <= 0) return null;+ return (+ <div className="llm-chat-note-card-attributes">+ {shown.map((attribute, idx) => <AttributePill key={idx} {...attribute} />)}+ {hiddenCount > 0 && (+ <span className="llm-chat-note-results-more">{t("llm_chat.subtree_more", { count: hiddenCount })}</span>+ )}+ </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.+ * bar's. It draws from the call rather than froca, which holds the attribute as it is now.*/-function AttributePill({ type, name, value }: NoteMetaAttribute) {+function AttributePill({ type, name, value, deleted }: NoteMetaAttribute & { deleted?: boolean }) {+ const className = `llm-chat-attribute ${deleted ? "llm-chat-attribute-deleted" : ""}`;if (type === "relation") {return (- <span className="llm-chat-attribute">+ <span className={className}>~{name}={value && <NewNoteLink notePath={value} showNoteIcon />}</span>);}- return <span className="llm-chat-attribute">#{name}{value && `=${formatValue(value)}`}</span>;+ return <span className={className}>#{name}{value && `=${formatValue(value)}`}</span>;+}++function parseAttributeList(result: string): NoteMetaAttribute[] | null {+ const parsed = parseJson(result);+ if (!Array.isArray(parsed)) return null;+ return parsed.map(parseAttribute).filter((attribute): attribute is NoteMetaAttribute => !!attribute);+}++function parseJson(text: string): unknown {+ try {+ return JSON.parse(text);+ } catch {+ return null;+ }+}++/** A label or relation out of a tool's input or result, or `null` when it does not describe one. */+function parseAttribute(value: unknown): NoteMetaAttribute | null {+ if (!isRecord(value) || (value.type !== "label" && value.type !== "relation") || typeof value.name !== "string") {+ return null;+ }+ return { type: value.type, name: value.name, value: typeof value.value === "string" ? value.value : "" };}function parseNoteMeta(result: string): NoteMeta | 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.htmlindex 5560ab2160..df90b9875c 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@@ -193,10 +193,12 @@ 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 renamed note shows its previous- title struck through, and a moved note where it was moved from and to.- 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+ 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 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 helppanel), the children or the whole subtree of a note, what the AI wroteinto a note it created, rewrote or added to, the changes of an edit, orthe reason a tool failed. A search also shows how many notes it found,diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 1e8b5c3344..a199ac9d54 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, and a moved note where it was moved from and to. 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, 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, 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 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.> [!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).diff --git a/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.tsindex 7ab0a1070f..1df7f6ec6e 100644--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.ts+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.ts@@ -143,7 +143,9 @@ describe("attribute_tools", () => {noteId: note.noteId,attributeId: attr.attributeId}));- expect(result).toEqual({ success: true, attributeId: attr.attributeId });+ expect(result).toEqual({+ success: true, attributeId: attr.attributeId, type: "label", name: "temp", value: "1"+ });expect(becca.getAttribute(attr.attributeId)).toBeNull();});diff --git a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts b/packages/trilium-core/src/services/llm/tools/attribute_tools.tsindex 2eb4f54614..a64900ae5b 100644--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.ts@@ -116,7 +116,10 @@ export const attributeTools = defineTools({return {success: true,- attributeId+ attributeId,+ type: attribute.type,+ name: attribute.name,+ value: attribute.value};}}