Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): list the User Guide pages a help search found

Elian Doran6cdafc1c2b34
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index cbd6de55cf..77b3b54d85 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2669,7 +2669,9 @@
"child_notes_count_one": "({{count}} child)",
"child_notes_count_other": "({{count}} children)",
"child_count_one": "{{count}} child",
- "child_count_other": "{{count}} children"
+ "child_count_other": "{{count}} children",
+ "search_help_count_one": "({{count}} page)",
+ "search_help_count_other": "({{count}} pages)"
},
"sidebar_chat": {
"title": "AI Chat",
diff --git a/apps/client/src/widgets/react/NoteLink.tsx b/apps/client/src/widgets/react/NoteLink.tsx
index ce5f9ea9f9..846e8575af 100644
--- a/apps/client/src/widgets/react/NoteLink.tsx
+++ b/apps/client/src/widgets/react/NoteLink.tsx
@@ -94,7 +94,7 @@ export default function NoteLink({ className, containerClassName, notePath, show
return <span className={containerClassName} ref={ref} />;
}
-interface NewNoteLinkProps extends Pick<HTMLAttributes<HTMLAnchorElement>, "onContextMenu"> {
+interface NewNoteLinkProps extends Pick<HTMLAttributes<HTMLAnchorElement>, "onContextMenu" | "onClick" | "onDblClick"> {
className?: string;
notePath: string;
viewScope?: ViewScope;
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 53e5c65c1c..07649e5f03 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,10 +12,15 @@ 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() }));
+const mocks = vi.hoisted(() => ({ triggerEvent: vi.fn(), openInAppHelpFromUrl: vi.fn() }));
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/NoteLink.js", () => ({
- NewNoteLink: ({ notePath }: { notePath: string }) => <a className="note-link-stub">{notePath}</a>
+ NewNoteLink: ({ notePath, onClick }: { notePath: string; onClick?: (e: MouseEvent) => void }) =>
+ <a className="note-link-stub" href="#" onClick={onClick}>{notePath}</a>
}));
import ToolCallCard from "./ToolCallCard.js";
@@ -25,6 +30,7 @@ let host: HTMLElement | undefined;
afterEach(() => {
mocks.triggerEvent.mockClear();
+ mocks.openInAppHelpFromUrl.mockClear();
if (host) {
render(null, host);
host.remove();
@@ -190,4 +196,43 @@ describe("ToolCallCard", () => {
expect(empty?.querySelector(".llm-chat-tool-call-result-count")?.textContent)
.toBe("llm_chat.child_notes_count{\"count\":0}");
});
+
+ it("lists the User Guide pages a help search found, opening each as contextual help", () => {
+ const target = renderCard([ {
+ id: "1",
+ toolName: "search_help",
+ input: { query: "clone", limit: 2 },
+ result: JSON.stringify({
+ totalResults: 5,
+ results: [
+ { noteId: "_help_abc", title: "Cloning", path: "Basic Concepts > Notes", contentPreview: "**Clones** share a note" },
+ { noteId: "_help_def", title: "Tree", path: "", contentPreview: null }
+ ]
+ })
+ } ]);
+ 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\":5}");
+
+ const rows = [ ...(line?.querySelectorAll(".llm-chat-note-result") ?? []) ];
+ expect(rows.map(row => ({
+ note: row.querySelector(".note-link-stub")?.textContent,
+ path: row.querySelector(".llm-chat-note-result-parent")?.textContent ?? null,
+ preview: row.querySelector(".llm-chat-note-result-preview")?.textContent ?? null
+ }))).toEqual([
+ { note: "_help_abc", path: "Basic Concepts > Notes", preview: "Clones share a note" },
+ { note: "_help_def", path: null, preview: null }
+ ]);
+ expect(line?.querySelector(".llm-chat-note-results-more")?.textContent)
+ .toBe("llm_chat.search_notes_limited{\"count\":5,\"limit\":2}");
+
+ const link = rows[0]?.querySelector<HTMLAnchorElement>(".note-link-stub");
+ expect(link).not.toBeNull();
+ act(() => {
+ link?.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, ctrlKey: true }));
+ });
+ expect(mocks.openInAppHelpFromUrl).not.toHaveBeenCalled();
+ act(() => link?.click());
+ expect(mocks.openInAppHelpFromUrl).toHaveBeenCalledExactlyOnceWith("abc");
+ });
});
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 ac7f3d5002..1bda863551 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -4,9 +4,12 @@ import type { ComponentChildren } from "preact";
import { Trans } from "react-i18next";
import { t } from "../../../services/i18n.js";
+import { openInAppHelpFromUrl } from "../../../services/utils.js";
import { NewNoteLink } from "../../react/NoteLink.js";
import type { ToolCall } from "./llm_chat_types.js";
+const HELP_NOTE_PREFIX = "_help_";
+
/** What a finished call shows: a short summary beside its label, and the view it folds open to. */
export interface ToolCallView {
summary?: string;
@@ -33,6 +36,22 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
body: search.results.length > 0 || search.ancestorNoteId ? <NoteSearchResults {...search} /> : undefined
};
}
+ case "search_help": {
+ const result = parseSearchNotesResult(toolCall.result);
+ if (!result) return null;
+ const { limit } = toolCall.input;
+ const search: NoteSearch = {
+ totalResults: result.totalResults,
+ // The guide path stands where a note search shows the parent.
+ results: result.results.map(({ noteId, path, contentPreview }) => ({ noteId, parentTitle: path, contentPreview })),
+ limit: typeof limit === "number" ? limit : result.results.length,
+ isHelp: true
+ };
+ return {
+ summary: t("llm_chat.search_help_count", { count: result.totalResults }),
+ body: search.results.length > 0 ? <NoteSearchResults {...search} /> : undefined
+ };
+ }
case "get_child_notes": {
const children = parseChildNotesResult(toolCall.result);
if (!children) return null;
@@ -49,6 +68,8 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
interface NoteSearchResult {
noteId: string;
parentTitle?: string | null;
+ /** The chain of User Guide sections above a `search_help` result. */
+ path?: string | null;
contentPreview?: string | null;
}
@@ -61,9 +82,11 @@ interface SearchNotesResult {
interface NoteSearch extends SearchNotesResult {
ancestorNoteId?: string;
limit: number;
+ /** The results are User Guide pages, which open as contextual help. */
+ isHelp?: boolean;
}
-function NoteSearchResults({ totalResults, results, ancestorNoteId, limit }: NoteSearch) {
+function NoteSearchResults({ totalResults, results, ancestorNoteId, limit, isHelp }: NoteSearch) {
return (
<div className="llm-chat-note-results">
{ancestorNoteId && (
@@ -82,6 +105,7 @@ function NoteSearchResults({ totalResults, results, ancestorNoteId, limit }: Not
key={noteId}
noteId={noteId}
preview={contentPreview ? markdownToPlainPreview(contentPreview) : ""}
+ onLinkClick={isHelp ? openAsHelp : undefined}
>
{parentTitle && (
<span className="llm-chat-note-result-parent">
@@ -120,11 +144,27 @@ function ChildNoteList({ notes }: { notes: ChildNote[] }) {
}
/** One note in a list a tool returned: its link, muted details beside it, and a preview below. */
-function NoteResultRow({ noteId, preview, children }: { noteId: string; preview?: string; children?: ComponentChildren }) {
+function NoteResultRow({ noteId, preview, onLinkClick, children }: {
+ noteId: string;
+ preview?: string;
+ /** Takes a plain click on the link. Returns whether it handled the click. */
+ onLinkClick?: (noteId: string) => boolean;
+ children?: ComponentChildren;
+}) {
return (
<li className="llm-chat-note-result">
<div className="llm-chat-note-result-header">
- <NewNoteLink notePath={noteId} showNoteIcon />
+ <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();
+ })}
+ />
{children}
</div>
{preview && <div className="llm-chat-note-result-preview">{preview}</div>}
@@ -132,6 +172,13 @@ function NoteResultRow({ noteId, preview, children }: { noteId: string; preview?
);
}
+/** 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;
+ void openInAppHelpFromUrl(noteId.slice(HELP_NOTE_PREFIX.length));
+ return true;
+}
+
interface ChildNote {
noteId: string;
childCount: number;
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 0693447735..743708d717 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,12 +184,13 @@ class="admonition note">
</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 children 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
+ is something to read inline: the notes a search found, the User Guide pages
+ the AI looked up (which open in the help panel), the children 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>
<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 e38cf7bca9..d9bca535ea 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 children 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 User Guide pages the AI looked up (which open in the help panel), the children 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.
> [!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).