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): say where a move took a note from and to
Elian Doranb3822cbc4e82
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 3810209b67..ce1cf15575 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4151,7 +4151,9 @@"search_icons": "Search icons","search_help": "Search User Guide","get_help_toc": "Get User Guide contents",- "read_web_page": "Read web page"+ "read_web_page": "Read web page",+ "note_moved_from": "<Note/> from <From/> to <To/>",+ "note_moved": "<Note/> to <To/>"}},"common": {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 6858684764..2d126c9d1f 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@@ -459,4 +459,20 @@ describe("ToolCallCard", () => {expect(oldTitle?.nextElementSibling?.querySelector(".note-link-stub")?.textContent).toBe("n");expect(older?.querySelector(".llm-chat-tool-call-old-title")).toBeNull();});++ it("says where a move took a note from and to", () => {+ const move = (id: string, result: object): ToolCall => ({+ id, toolName: "move_note", input: { noteId: "n", newParentNoteId: "to" }, result: JSON.stringify(result)+ });+ const target = renderCard([+ move("1", { success: true, noteId: "n", newParentNoteId: "to", oldParentNoteId: "from" }),+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },+ move("3", { success: true, noteId: "n", newParentNoteId: "to" })+ ]);+ const [ moved, , older ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];+ const ref = (line: Element | undefined) => line?.querySelector(".llm-chat-tool-call-note-ref");++ expect(ref(moved)?.textContent).toBe("llm.tools.note_moved_fromnfromto");+ expect(ref(older)?.textContent).toBe("llm.tools.note_movednto");+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex 8a862e9d8f..fab87f4ba6 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -16,18 +16,19 @@ interface ToolCallContext {noteId: string | null;/** The parent note, shown as "in <parent>" for creation tools. */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 };/** Plain-text detail (e.g. skill name, search query) when no note ref is available. */detailText: string | null;}-/** Try to extract a noteId from the tool call's result JSON. */-function parseResultNoteId(toolCall: ToolCall): string | null {+/** Try to extract a string field from the tool call's result JSON. */+function parseResultField(toolCall: ToolCall, field: string): string | null {if (!toolCall.result) return null;try {- const result = typeof toolCall.result === "string"- ? JSON.parse(toolCall.result)- : toolCall.result;- return result?.noteId || null;+ const result = JSON.parse(toolCall.result);+ const value = result?.[field];+ return typeof value === "string" && value ? value : null;} catch {return null;}@@ -38,15 +39,24 @@ function getToolCallContext(toolCall: ToolCall): ToolCallContext {const input = toolCall.input;const parentNoteId = (input?.parentNoteId as string) || null;+ if (toolCall.toolName === "move_note"+ && typeof input?.noteId === "string" && typeof input.newParentNoteId === "string") {+ const move = {+ toNoteId: input.newParentNoteId,+ fromNoteId: parseResultField(toolCall, "oldParentNoteId")+ };+ return { noteId: input.noteId, parentNoteId: null, move, detailText: null };+ }+// For creation tools, the created note ID is in the result.if (parentNoteId) {- const createdNoteId = parseResultNoteId(toolCall);+ const createdNoteId = parseResultField(toolCall, "noteId");if (createdNoteId) {return { noteId: createdNoteId, parentNoteId, detailText: null };}}- const noteId = (input?.noteId as string) || parentNoteId || parseResultNoteId(toolCall);+ const noteId = (input?.noteId as string) || parentNoteId || parseResultField(toolCall, "noteId");if (noteId) {return { noteId, parentNoteId: null, detailText: null };}@@ -89,7 +99,7 @@ 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, detailText } = getToolCallContext(toolCall);+ const { noteId: refNoteId, parentNoteId: refParentId, move, detailText } = getToolCallContext(toolCall);const hasError = isFailedToolCall(toolCall);return (@@ -101,17 +111,7 @@ function ToolCallLabel({ toolCall, view }: { toolCall: ToolCall; view: ToolCallV{view?.lead}{refNoteId && (<span className="llm-chat-tool-call-note-ref">- {refParentId ? (- <Trans- i18nKey="llm.tools.note_in_parent"- components={{- Note: <NewNoteLink notePath={refNoteId} showNoteIcon noPreview />,- Parent: <NewNoteLink notePath={refParentId} showNoteIcon noPreview />- } as any}- />- ) : (- <NewNoteLink notePath={refNoteId} showNoteIcon noPreview />- )}+ <NoteRef noteId={refNoteId} parentNoteId={refParentId} move={move} /></span>)}{view?.summary && <span className="llm-chat-tool-call-result-count">{view.summary}</span>}@@ -120,6 +120,28 @@ function ToolCallLabel({ toolCall, view }: { toolCall: ToolCall; view: ToolCallV);}+/** The notes a call worked on: the note alone, the note in its parent, or where a move took it. */+function NoteRef({ noteId, parentNoteId, move }: {+ noteId: string;+ parentNoteId: string | null;+ move?: ToolCallContext["move"];+}) {+ const link = (notePath: string) => <NewNoteLink notePath={notePath} showNoteIcon noPreview />;++ if (move) {+ const components = move.fromNoteId+ ? { Note: link(noteId), From: link(move.fromNoteId), To: link(move.toNoteId) }+ : { Note: link(noteId), To: link(move.toNoteId) };+ const key = move.fromNoteId ? "llm.tools.note_moved_from" : "llm.tools.note_moved";+ return <Trans i18nKey={key} components={components as any} />;+ }+ if (parentNoteId) {+ const components = { Note: link(noteId), Parent: link(parentNoteId) };+ return <Trans i18nKey="llm.tools.note_in_parent" components={components as any} />;+ }+ return link(noteId);+}+/*** A single tool call. It folds open only for what is worth reading inline: the input while it* streams, the diff of an `edit_note_content` call, why the call failed, or the viewdiff --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 36c1ea33ea..58dcd425b4 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,17 +184,18 @@ 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. 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.</p>+ 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.</p><aside class="admonition warning"><p>Currently there is <strong>no permission management</strong> implementedfor note tools, meaning that the LLM could potentially remove existingdiff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 74e5ad0ddd..37b93c04f8 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 renamed note shows its previous title struck through. 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, 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.> [!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/hierarchy_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.spec.tsindex 26bd5a470c..1b8da6f38f 100644--- a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.spec.ts+++ b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.spec.ts@@ -105,7 +105,8 @@ describe("hierarchy_tools", () => {success: true,noteId: note.noteId,newParentNoteId: newParent.noteId,- newParentTitle: "New parent"+ newParentTitle: "New parent",+ oldParentNoteId: oldParent.noteId});expect(note.getParentNotes().map((p) => p.noteId)).toContain(newParent.noteId);});diff --git a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.tsindex e6cf4c6450..a600bff2a6 100644--- a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts+++ b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts@@ -131,6 +131,7 @@ export const hierarchyTools = defineTools({return { error: "Note has no parent branches" };}+ const oldParentNoteId = branches[0].parentNoteId;const result = branchService.moveBranchToNote(branches[0], newParentNoteId);if (Array.isArray(result)) {// Validation error: [statusCode, { success: false, message }]@@ -148,7 +149,8 @@ export const hierarchyTools = defineTools({noteId: note.noteId,title: note.getTitleOrProtected(),newParentNoteId,- newParentTitle: targetParent.getTitleOrProtected()+ newParentTitle: targetParent.getTitleOrProtected(),+ oldParentNoteId};}},