Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): show the title a rename replaced

Elian Doranc9c4ad605ca6
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 4f6fe11501..1ddf76bfcf 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
@@ -84,3 +84,17 @@
@keyframes llm-chat-tool-call-caret {
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 {
+ font-size: 0.75rem;
+ opacity: 0.75;
+ text-decoration: line-through;
+
+ &::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.tsx
index 03fc552f0f..6858684764 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
@@ -441,4 +441,22 @@ describe("ToolCallCard", () => {
expect(line?.querySelector(".llm-chat-tool-call-error-message")?.textContent)
.toBe("edit_note_content does not support rich-text notes.");
});
+
+ it("names the title a note had before a rename, on the plain line", () => {
+ const rename = (id: string, result: object): ToolCall => ({
+ id, toolName: "rename_note", input: { noteId: "n", newTitle: "New" }, result: JSON.stringify(result)
+ });
+ const target = renderCard([
+ rename("1", { success: true, noteId: "n", title: "New", oldTitle: "Old" }),
+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },
+ rename("3", { success: true, noteId: "n", title: "New" })
+ ]);
+ const [ renamed, , older ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+
+ expect(renamed instanceof HTMLDetailsElement).toBe(false);
+ const oldTitle = renamed?.querySelector(".llm-chat-tool-call-old-title");
+ expect(oldTitle?.textContent).toBe("Old");
+ expect(oldTitle?.nextElementSibling?.querySelector(".note-link-stub")?.textContent).toBe("n");
+ expect(older?.querySelector(".llm-chat-tool-call-old-title")).toBeNull();
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
index 37cd5cf72b..8a862e9d8f 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -9,7 +9,7 @@ import { NewNoteLink } from "../../react/NoteLink.js";
import { EditNoteContentDiff, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";
import { ExpandableSection } from "./ExpandableCard.js";
import { isFailedToolCall, type ToolCall } from "./llm_chat_types.js";
-import { getToolCallView } from "./ToolCallViews.js";
+import { getToolCallView, type ToolCallView } from "./ToolCallViews.js";
interface ToolCallContext {
/** The primary note the tool operates on or created. */
@@ -88,7 +88,7 @@ function getErrorMessage(result: string): string {
}
/** Build the label content for a tool call section. */
-function ToolCallLabel({ toolCall, summary }: { toolCall: ToolCall; summary?: string }) {
+function ToolCallLabel({ toolCall, view }: { toolCall: ToolCall; view: ToolCallView | null }) {
const { noteId: refNoteId, parentNoteId: refParentId, detailText } = getToolCallContext(toolCall);
const hasError = isFailedToolCall(toolCall);
@@ -98,6 +98,7 @@ function ToolCallLabel({ toolCall, summary }: { toolCall: ToolCall; summary?: st
{detailText && (
<span className="llm-chat-tool-call-detail">{detailText}</span>
)}
+ {view?.lead}
{refNoteId && (
<span className="llm-chat-tool-call-note-ref">
{refParentId ? (
@@ -113,7 +114,7 @@ function ToolCallLabel({ toolCall, summary }: { toolCall: ToolCall; summary?: st
)}
</span>
)}
- {summary && <span className="llm-chat-tool-call-result-count">{summary}</span>}
+ {view?.summary && <span className="llm-chat-tool-call-result-count">{view.summary}</span>}
{hasError && <span className="llm-chat-tool-call-error-badge">{t("llm_chat.tool_error")}</span>}
</>
);
@@ -139,7 +140,7 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
const className = `llm-chat-tool-call ${hasError ? "llm-chat-tool-call-error" : ""}`;
const icon = toolCallIcon(toolCall);
- const label = <ToolCallLabel toolCall={toolCall} summary={view?.summary} />;
+ const label = <ToolCallLabel toolCall={toolCall} view={view} />;
const debugButton = <ToolCallDebugButton toolCall={toolCall} />;
if (!isStreamingInput && !noteContentEdits && !errorMessage && !view?.body) {
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 37a4a90341..2000b7a0b7 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -20,6 +20,8 @@ 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 {
+ /** Shown on the summary line before the note link, such as the title a rename replaced. */
+ lead?: ComponentChildren;
summary?: string;
body?: ComponentChildren;
}
@@ -84,12 +86,17 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
body: children.length > 0 ? <ChildNoteList notes={children} /> : undefined
};
}
+ case "rename_note": {
+ // The note link shows the note's title now, so the one it replaced comes from the result.
+ const oldTitle = parseStringField(toolCall.result, "oldTitle");
+ return oldTitle ? { lead: <span className="llm-chat-tool-call-old-title">{oldTitle}</span> } : null;
+ }
case "get_note": {
const meta = parseNoteMeta(toolCall.result);
return meta ? { body: <NoteMetaCard {...meta} /> } : null;
}
case "get_note_content": {
- const content = parseNoteContent(toolCall.result);
+ const content = parseStringField(toolCall.result, "content");
// 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;
@@ -370,10 +377,11 @@ function parseNoteMeta(result: string): NoteMeta | null {
};
}
-function parseNoteContent(result: string): string | null {
+/** A string field of a JSON object result, or `null` when the result has no such field. */
+function parseStringField(result: string, field: string): string | null {
try {
const parsed: unknown = JSON.parse(result);
- return isRecord(parsed) && typeof parsed.content === "string" ? parsed.content : null;
+ return isRecord(parsed) && typeof parsed[field] === "string" ? parsed[field] : null;
} catch {
return 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 35e301cbe7..36c1ea33ea 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
@@ -183,16 +183,17 @@ class="admonition note">
<li>Skills (see the dedicated section).</li>
</ul>
<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
- 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
+ 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.</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 0c8f133914..74e5ad0ddd 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 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 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/note_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/note_tools.spec.ts
index 6882f404b0..afc44e9dd9 100644
--- a/packages/trilium-core/src/services/llm/tools/note_tools.spec.ts
+++ b/packages/trilium-core/src/services/llm/tools/note_tools.spec.ts
@@ -362,8 +362,8 @@ describe("note_tools — write tools return post-write content", () => {
it("renames a note and trims the title", () => {
const note = buildNote({ id: "rn", title: "Old" });
note.save = vi.fn() as typeof note.save;
- const result = getTool("rename_note").execute({ noteId: "rn", newTitle: " New " }) as { title: string };
- expect(result.title).toBe("New");
+ const result = getTool("rename_note").execute({ noteId: "rn", newTitle: " New " });
+ expect(result).toMatchObject({ title: "New", oldTitle: "Old" });
expect(note.title).toBe("New");
expect(note.save).toHaveBeenCalledOnce();
});
diff --git a/packages/trilium-core/src/services/llm/tools/note_tools.ts b/packages/trilium-core/src/services/llm/tools/note_tools.ts
index 0cfa4a585f..0f284b3276 100644
--- a/packages/trilium-core/src/services/llm/tools/note_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/note_tools.ts
@@ -319,13 +319,15 @@ export const noteTools = defineTools({
return { error: "Title cannot be empty" };
}
+ const oldTitle = note.title;
note.title = trimmedTitle;
note.save();
return {
success: true,
noteId: note.noteId,
- title: note.getTitleOrProtected()
+ title: note.getTitleOrProtected(),
+ oldTitle
};
}
},