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 create_note call wrote
Elian Doranaca08fff0ca3
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 a684dfaf5b..be3dcab2bc 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@@ -18,6 +18,12 @@ vi.mock("../../../services/utils.js", async (importOriginal) => ({...(await importOriginal<typeof import("../../../services/utils.js")>()),openInAppHelpFromUrl: mocks.openInAppHelpFromUrl}));+vi.mock("../text/ReadOnlyText.js", () => ({+ ReadOnlyTextContent: ({ html }: { html: string }) => <div className="markdown-stub">{html}</div>+}));+vi.mock("../../react/CodeBlock.js", () => ({+ default: ({ code, mimeType }: { code: string; mimeType?: string }) => <pre className="code-block-stub" data-mime={mimeType}>{code}</pre>+}));vi.mock("../../react/NoteLink.js", () => ({NewNoteLink: ({ notePath, onClick }: { notePath: string; onClick?: (e: MouseEvent) => void }) =><a className="note-link-stub" href="#" onClick={onClick}>{notePath}</a>@@ -330,4 +336,36 @@ describe("ToolCallCard", () => {expect(read?.querySelector(".llm-chat-note-card-facts")).toBeNull();expect(empty instanceof HTMLDetailsElement).toBe(false);});++ it("shows what a create_note call wrote, by the type of the note, before its result arrives", () => {+ const call = (id: string, type: string, content: string, mime?: string): ToolCall => ({+ id, toolName: "create_note", input: { parentNoteId: "p", title: "T", type, content, mime }+ });+ const target = renderCard([+ { ...call("1", "text", "# Heading\n\nSome **bold**"), result: JSON.stringify({ success: true, noteId: "n", title: "T", type: "text" }) },+ call("2", "code", "const a = 1;", "application/javascript;env=frontend"),+ call("3", "canvas", "{\"elements\":[]}"),+ call("4", "webView", "https://triliumnotes.org"),+ call("5", "search", "#book")+ ]);+ // Consecutive calls to one tool fold into a group, so the calls are the lines inside it.+ const [ text, code, canvas, webView, search ] = [ ...target.querySelectorAll(".expandable-section-body > .llm-chat-tool-call") ];++ expect(text instanceof HTMLDetailsElement).toBe(true);+ expect(text?.querySelector(".llm-chat-written .markdown-stub")?.textContent).toContain("<strong>bold</strong>");+ expect(text?.querySelector(".llm-chat-note-card-facts")).toBeNull();++ const codeBlock = code?.querySelector(".llm-chat-written .code-block-stub");+ expect(code instanceof HTMLDetailsElement).toBe(true);+ expect(codeBlock?.textContent).toBe("const a = 1;");+ expect(codeBlock?.getAttribute("data-mime")).toBe("application/javascript;env=frontend");+ expect(code?.querySelector(".llm-chat-note-card-facts")?.textContent).toBe("note_types.code");++ expect(canvas instanceof HTMLDetailsElement).toBe(false);++ const url = webView?.querySelector<HTMLAnchorElement>(".llm-chat-written a.external");+ expect(url?.getAttribute("href")).toBe("https://triliumnotes.org");++ expect(search?.querySelector(".llm-chat-written .code-block-stub")?.textContent).toBe("#book");+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.cssindex 931786f1bc..861bb5f17f 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css@@ -114,3 +114,14 @@color: var(--main-text-color);overflow-wrap: anywhere;}++/* The content a writing tool put into a note. */+.llm-chat-written {+ max-height: 300px;+ overflow: auto;+ font-size: 0.85rem;++ > .code-block {+ margin: 0;+ }+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsxindex 6e3eed64a8..2964e3c0e0 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx@@ -8,7 +8,10 @@ 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 CodeBlock from "../../react/CodeBlock.js";import { NewNoteLink } from "../../react/NoteLink.js";+import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";+import { renderMarkdown } from "./chat_markdown.js";import type { ToolCall } from "./llm_chat_types.js";const HELP_NOTE_PREFIX = "_help_";@@ -22,7 +25,17 @@ export interface ToolCallView {/** The view of a successful call to a tool that has one, or `null` to show the bare line. */export function getToolCallView(toolCall: ToolCall): ToolCallView | null {- if (!toolCall.result || toolCall.isError) return null;+ if (toolCall.isError) return null;++ // What a writing tool writes is in its input, so it shows before the result arrives.+ if (toolCall.toolName === "create_note") {+ const { type, mime, content } = toolCall.input;+ if (typeof type !== "string" || typeof content !== "string") return null;+ const body = <WrittenContent type={type} mime={typeof mime === "string" ? mime : undefined} content={content} />;+ return hasWrittenContentView(type, content) ? { body } : null;+ }++ if (!toolCall.result) return null;switch (toolCall.toolName) {case "search_notes": {@@ -220,6 +233,38 @@ 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 }) {+ 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">+ {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 />}+ {type === "search" && <CodeBlock code={content} wrap />}+ {type === "webView" && (+ <a className="tn-link external" href={content} target="_blank" rel="noopener noreferrer">{content}</a>+ )}+ </div>+ </div>+ );+}++function hasWrittenContentView(type: string, content: string): boolean {+ if (!content.trim()) return false;+ if (type === "webView") return /^https?:\/\//i.test(content.trim());+ return [ "text", "code", "mermaid", "search" ].includes(type);+}++function findNoteType(type: string, mime?: string) {+ return NOTE_TYPES.find((nt) => nt.type === type && nt.mime === mime) ?? NOTE_TYPES.find((nt) => nt.type === type);+}+/** A capped list in a `get_note` result: `totalCount` counts past the entries it lists. */interface CappedList<T> {totalCount: number;@@ -243,7 +288,7 @@ interface NoteMeta {/** 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 noteType = findNoteType(type, mime);const facts = [noteType?.title ?? type,childCount > 0 && t("llm_chat.child_count", { count: childCount }),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 ad3498bef8..cf975d1fdf 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,12 +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, 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, 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 6cdf100d5d..4ba0a4ef05 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, 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, 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).