Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): lay out the User Guide contents and name loaded skills

Elian Doran2627d4bc0d75
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 66f758b030..a30e5944d2 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2681,7 +2681,13 @@
"search_icons_count_one": "({{count}} icon)",
"search_icons_count_other": "({{count}} icons)",
"attachment_size": "({{size}})",
- "attachment_ocr": "Text recognized from the file"
+ "attachment_ocr": "Text recognized from the file",
+ "skills": {
+ "search_syntax": "Search syntax",
+ "backend_scripting": "Backend scripting",
+ "frontend_scripting": "Frontend scripting",
+ "dashboards": "Dashboards"
+ }
},
"sidebar_chat": {
"title": "AI Chat",
diff --git a/apps/client/src/widgets/react/LinkButton.tsx b/apps/client/src/widgets/react/LinkButton.tsx
index ffa248fb7c..2dfffe7b32 100644
--- a/apps/client/src/widgets/react/LinkButton.tsx
+++ b/apps/client/src/widgets/react/LinkButton.tsx
@@ -1,5 +1,5 @@
import clsx from "clsx";
-import { ComponentChild } from "preact";
+import { ComponentChild, HTMLAttributes } from "preact";
import { CommandNames } from "../../components/app_context";
interface LinkButtonProps {
@@ -28,7 +28,7 @@ export default function LinkButton({ onClick, text, triggerCommand, className }:
)
}
-interface PageLinkProps {
+interface PageLinkProps extends Pick<HTMLAttributes<HTMLAnchorElement>, "onClick"> {
/** Where the link goes, as a note path such as `#root/_hidden/_options/_optionsBackup`. */
href: string;
/** The words the link is read as. */
@@ -40,6 +40,6 @@ interface PageLinkProps {
* of the page it goes to. That wording is also why the note preview is left out: the text has
* already said what the page holds, so the preview would only repeat it in less readable form.
*/
-export function PageLink({ href, text }: PageLinkProps) {
- return <a className="tn-link no-tooltip-preview" href={href}>{text}</a>;
+export function PageLink({ href, text, onClick }: PageLinkProps) {
+ return <a className="tn-link no-tooltip-preview" href={href} onClick={onClick}>{text}</a>;
}
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 e678f461b6..482b015189 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
@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
vi.mock("../../../services/i18n.js", () => ({
t: (key: string, options?: { defaultValue?: string }) => {
if (key.startsWith("llm.tools.")) return key.slice("llm.tools.".length);
+ if (key === "llm_chat.skills.search_syntax") return "Search syntax";
return options?.defaultValue ?? (options ? `${key}${JSON.stringify(options)}` : key);
}
}));
@@ -624,4 +625,44 @@ describe("ToolCallCard", () => {
expect(content?.querySelector(".llm-chat-note-card-facts")?.textContent).toBe("llm_chat.attachment_ocr");
expect(content?.querySelector(".llm-chat-note-result-preview")?.textContent).toBe("Quarterly report");
});
+
+ it("names a skill the AI loaded by its display name, falling back to its ID", () => {
+ const target = renderCard([
+ { id: "1", toolName: "load_skill", input: { name: "search_syntax" }, result: JSON.stringify({ skill: "search_syntax", instructions: "…" }) },
+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },
+ { id: "3", toolName: "load_skill", input: { name: "brand_new" }, result: JSON.stringify({ skill: "brand_new", instructions: "…" }) }
+ ]);
+ const [ known, , unknown ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+ expect(known instanceof HTMLDetailsElement).toBe(false);
+ expect(known?.querySelector(".llm-chat-tool-call-detail")?.textContent).toBe("Search syntax");
+ expect(unknown?.querySelector(".llm-chat-tool-call-detail")?.textContent).toBe("brand_new");
+ });
+
+ it("lays out the User Guide contents as a tree whose pages open as contextual help", () => {
+ const target = renderCard([ {
+ id: "1",
+ toolName: "get_help_toc",
+ input: {},
+ result: JSON.stringify({ pageCount: 3, toc: "Getting started (_help_a)\n Install (_help_b)\nNotes (_help_c)" })
+ } ]);
+ const line = target.querySelector("details.llm-chat-tool-call");
+ expect(line?.querySelector(".llm-chat-tool-call-result-count")?.textContent)
+ .toBe("llm_chat.search_help_count{\"count\":3}");
+
+ const header = (row: Element | null | undefined) => row?.querySelector(":scope > .llm-chat-note-result-header");
+ const rows = [ ...(line?.querySelectorAll(".llm-chat-note-result") ?? []) ];
+ expect(rows.map(row => ({
+ page: header(row)?.querySelector("a")?.textContent,
+ parent: header(row.parentElement?.closest(".llm-chat-note-result"))?.querySelector("a")?.textContent ?? null
+ }))).toEqual([
+ { page: "Getting started", parent: null },
+ { page: "Install", parent: "Getting started" },
+ { page: "Notes", parent: null }
+ ]);
+
+ const install = header(rows[1])?.querySelector<HTMLAnchorElement>("a");
+ expect(install).not.toBeNull();
+ act(() => install?.click());
+ expect(mocks.openInAppHelpFromUrl).toHaveBeenCalledExactlyOnceWith("b");
+ });
});
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 f79f7160d2..0097855868 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -70,6 +70,11 @@ function getToolCallContext(toolCall: ToolCall): ToolCallContext {
return { noteId, parentNoteId: null, detailText: null };
}
+ if (toolCall.toolName === "load_skill" && typeof input?.name === "string") {
+ const skillTitle = t(`llm_chat.skills.${input.name}`, { defaultValue: input.name });
+ return { noteId: null, parentNoteId: null, detailText: skillTitle };
+ }
+
const detailText = (input?.name ?? input?.query ?? input?.url) as string | undefined;
return { noteId: null, parentNoteId: null, detailText: detailText || null };
}
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 7056243417..a27ca1f888 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -6,11 +6,13 @@ import { Trans } from "react-i18next";
import { isAutoLinkAttribute } from "../../../entities/fattribute.js";
import { formatValue } from "../../../services/attribute_renderer.js";
import { t } from "../../../services/i18n.js";
+import { calculateHash } from "../../../services/link.js";
import { NOTE_TYPES } from "../../../services/note_types.js";
import { formatSize, openInAppHelpFromUrl } from "../../../services/utils.js";
import CodeBlock from "../../react/CodeBlock.js";
import { useNote } from "../../react/hooks.js";
import { TooltipIcon } from "../../react/Icon.js";
+import { PageLink } from "../../react/LinkButton.js";
import { NewNoteLink } from "../../react/NoteLink.js";
import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";
import { renderMarkdown } from "./chat_markdown.js";
@@ -117,6 +119,18 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
: undefined
};
}
+ case "get_help_toc": {
+ const result = parseJson(toolCall.result);
+ if (!isRecord(result) || typeof result.toc !== "string") return null;
+ const pages = parseHelpToc(result.toc);
+ const count = typeof result.pageCount === "number" ? result.pageCount : pages.length;
+ return {
+ summary: t("llm_chat.search_help_count", { count }),
+ body: pages.length > 0
+ ? <div className="llm-chat-note-results llm-chat-subtree"><HelpTocList pages={pages} /></div>
+ : undefined
+ };
+ }
case "get_child_notes": {
const children = parseChildNotesResult(toolCall.result);
if (!children) return null;
@@ -333,13 +347,7 @@ function NoteResultRow({ noteId, preview, nested, onLinkClick, children }: {
<NewNoteLink
notePath={noteId}
showNoteIcon
- onClick={onLinkClick && ((e) => {
- // A modified or middle click goes on to `goToLink()` in `services/link.ts`.
- if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
- if (!onLinkClick(noteId)) return;
- e.preventDefault();
- e.stopPropagation();
- })}
+ onClick={onLinkClick && onPlainClick(() => onLinkClick(noteId))}
/>
{children}
</div>
@@ -600,6 +608,66 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
+/**
+ * A link click handler that gives a plain left click to `handle`, and lets a modified or middle click
+ * go on to `goToLink()` in `services/link.ts`. `handle` returns whether it took the click.
+ */
+function onPlainClick(handle: () => boolean) {
+ return (e: MouseEvent) => {
+ if (e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
+ if (!handle()) return;
+ e.preventDefault();
+ e.stopPropagation();
+ };
+}
+
+/** A page of the User Guide contents, with the pages below it. */
+interface HelpTocPage {
+ noteId: string;
+ title: string;
+ children: HelpTocPage[];
+}
+
+/**
+ * The User Guide contents a `get_help_toc` call read. Titles come from the result rather than froca,
+ * which would otherwise load every page of the guide for one line.
+ */
+function HelpTocList({ pages }: { pages: HelpTocPage[] }) {
+ return (
+ <ul>
+ {pages.map((page) => (
+ <li key={page.noteId} className="llm-chat-note-result">
+ <div className="llm-chat-note-result-header">
+ <PageLink
+ href={calculateHash({ notePath: page.noteId })}
+ text={page.title}
+ onClick={onPlainClick(() => openAsHelp(page.noteId))}
+ />
+ </div>
+ {page.children.length > 0 && <HelpTocList pages={page.children} />}
+ </li>
+ ))}
+ </ul>
+ );
+}
+
+/** Reads the `Title (noteId)` lines of a `get_help_toc` result, indented two spaces per level. */
+function parseHelpToc(toc: string): HelpTocPage[] {
+ const roots: HelpTocPage[] = [];
+ const ancestors: HelpTocPage[] = [];
+ for (const line of toc.split("\n")) {
+ const match = /^( *)(.+) \(([^()\s]+)\)$/.exec(line);
+ if (!match) continue;
+ const depth = Math.floor(match[1].length / 2);
+ const page: HelpTocPage = { noteId: match[3], title: match[2], children: [] };
+ ancestors.length = Math.min(depth, ancestors.length);
+ const parent = ancestors[ancestors.length - 1];
+ (parent ? parent.children : roots).push(page);
+ ancestors.push(page);
+ }
+ return roots;
+}
+
/** Opens a User Guide page in the contextual help split, as `HelpButton` does. */
function openAsHelp(noteId: string): boolean {
if (!noteId.startsWith(HELP_NOTE_PREFIX)) return false;
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 493f25b7a8..3b4a32ba2d 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
@@ -200,13 +200,13 @@ class="admonition note">
the notes a search found, the icons an icon search found, the type, attributes
or start of the content of a note the AI read, all the attributes of a
note, the text of an attachment, 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
+ or its table of contents (both 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 491394edac..0138eb42d2 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, a deleted note its title struck through, a moved note where it was moved from and to, an attribute the AI read, set or deleted as a pill (struck through once deleted), and an attachment by its title, note and size. A line folds open only when there is something to read inline: the notes a search found, the icons an icon search found, the type, attributes or start of the content of a note the AI read, all the attributes of a note, the text of an attachment, 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, an attribute the AI read, set or deleted as a pill (struck through once deleted), and an attachment by its title, note and size. A line folds open only when there is something to read inline: the notes a search found, the icons an icon search found, the type, attributes or start of the content of a note the AI read, all the attributes of a note, the text of an attachment, the User Guide pages the AI looked up or its table of contents (both 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).