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 set_note_content and append_to_note wrote
Elian Doran1044a8defa9c
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 be3dcab2bc..1341cc9964 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@@ -12,12 +12,20 @@ vi.mock("react-i18next", () => ({Trans: ({ i18nKey, components }: { i18nKey: string; components: Record<string, preact.ComponentChildren> }) =><>{i18nKey}{Object.values(components)}</>}));-const mocks = vi.hoisted(() => ({ triggerEvent: vi.fn(), openInAppHelpFromUrl: vi.fn() }));+const mocks = vi.hoisted(() => ({+ triggerEvent: vi.fn(),+ openInAppHelpFromUrl: vi.fn(),+ notes: {} as Record<string, { type: string; mime: string }>+}));vi.mock("../../../components/app_context.js", () => ({ default: { triggerEvent: mocks.triggerEvent } }));vi.mock("../../../services/utils.js", async (importOriginal) => ({...(await importOriginal<typeof import("../../../services/utils.js")>()),openInAppHelpFromUrl: mocks.openInAppHelpFromUrl}));+vi.mock("../../react/hooks.js", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../../react/hooks.js")>()),+ useNote: (noteId: string) => mocks.notes[noteId]+}));vi.mock("../text/ReadOnlyText.js", () => ({ReadOnlyTextContent: ({ html }: { html: string }) => <div className="markdown-stub">{html}</div>}));@@ -368,4 +376,28 @@ describe("ToolCallCard", () => {expect(search?.querySelector(".llm-chat-written .code-block-stub")?.textContent).toBe("#book");});++ it("shows what set_note_content and append_to_note wrote, by the type of the note they wrote to", () => {+ mocks.notes = {+ t: { type: "text", mime: "text/html" },+ c: { type: "code", mime: "text/x-python" },+ j: { type: "canvas", mime: "application/json" }+ };+ const target = renderCard([+ { id: "1", toolName: "set_note_content", input: { noteId: "t", content: "Some **bold**" } },+ { id: "2", toolName: "append_to_note", input: { noteId: "c", content: "print(1)" } },+ { id: "3", toolName: "set_note_content", input: { noteId: "j", content: "{\"elements\":[]}" } }+ ]);+ const [ set, append, json ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];++ expect(set?.querySelector(".llm-chat-written .markdown-stub")?.textContent).toContain("<strong>bold</strong>");+ expect(set?.querySelector(".llm-chat-written-appended")).toBeNull();++ const codeBlock = append?.querySelector(".llm-chat-written.llm-chat-written-appended .code-block-stub");+ expect(codeBlock?.textContent).toBe("print(1)");+ expect(codeBlock?.getAttribute("data-mime")).toBe("text/x-python");++ expect(json).not.toBeUndefined();+ expect(json?.querySelector(".llm-chat-written")).toBeNull();+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.cssindex 861bb5f17f..53d8777957 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css@@ -125,3 +125,9 @@margin: 0;}}++/* Appended content is new at the end of the note, so it wears the diff's color for added lines. */+.llm-chat-written-appended {+ padding-left: 0.5rem;+ border-left: 2px solid rgb(60, 160, 60);+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsxindex 2964e3c0e0..bff9a68608 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx@@ -9,6 +9,7 @@ import { t } from "../../../services/i18n.js";import { NOTE_TYPES } from "../../../services/note_types.js";import { openInAppHelpFromUrl } from "../../../services/utils.js";import CodeBlock from "../../react/CodeBlock.js";+import { useNote } from "../../react/hooks.js";import { NewNoteLink } from "../../react/NoteLink.js";import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";import { renderMarkdown } from "./chat_markdown.js";@@ -35,6 +36,12 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {return hasWrittenContentView(type, content) ? { body } : null;}+ if (toolCall.toolName === "set_note_content" || toolCall.toolName === "append_to_note") {+ const { noteId, content } = toolCall.input;+ if (typeof noteId !== "string" || typeof content !== "string" || !content.trim()) return null;+ return { body: <NoteWrittenContent noteId={noteId} content={content} appended={toolCall.toolName === "append_to_note"} /> };+ }+if (!toolCall.result) return null;switch (toolCall.toolName) {@@ -237,12 +244,12 @@ function NoteResultRow({ noteId, preview, nested, onLinkClick, children }: {* The content a tool wrote into a note, shown the way its type reads: text as rendered Markdown, code* and diagrams as a code block, a web view as its URL. JSON types (canvas, mind map) have no view.*/-function WrittenContent({ type, mime, content }: { type: string; mime?: string; content: string }) {+function WrittenContent({ type, mime, content, appended }: { type: string; mime?: string; content: string; appended?: boolean }) {const noteType = type !== "text" && findNoteType(type, mime);return (<div className="llm-chat-note-card">{noteType && <div className="llm-chat-note-card-facts">{noteType.title}</div>}- <div className="llm-chat-written">+ <div className={`llm-chat-written ${appended ? "llm-chat-written-appended" : ""}`}>{type === "text" && <ReadOnlyTextContent html={renderMarkdown(content)} className="llm-chat-markdown" />}{type === "code" && <CodeBlock code={content} mimeType={mime} wrap />}{type === "mermaid" && <CodeBlock code={content} mimeType="text/mermaid" wrap />}@@ -255,6 +262,16 @@ function WrittenContent({ type, mime, content }: { type: string; mime?: string;);}+/**+ * The content `set_note_content` or `append_to_note` wrote. Their input names no type, so it comes+ * from the note itself, and nothing shows until froca has it.+ */+function NoteWrittenContent({ noteId, content, appended }: { noteId: string; content: string; appended: boolean }) {+ const note = useNote(noteId);+ if (!note || !hasWrittenContentView(note.type, content)) return null;+ return <WrittenContent type={note.type} mime={note.mime} content={content} appended={appended} />;+}+function hasWrittenContentView(type: string, content: string): boolean {if (!content.trim()) return false;if (type === "webView") return /^https?:\/\//i.test(content.trim());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 cf975d1fdf..35e301cbe7 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@@ -187,13 +187,13 @@ class="admonition note">is something to read inline: the notes a search found, the type, attributesor start of the content of a note the AI read, the User Guide pages theAI 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, 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>+ 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 4ba0a4ef05..0c8f133914 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, 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 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).