Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm_chat): name the subtree a note search is confined to on its line

Elian Doran7a2163b1f2e1
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index a2966d65f9..ad7ba8eef5 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2655,7 +2655,6 @@
"search_notes_count_other": "({{count}} notes)",
"search_notes_limited_one": "{{count}} total result, limited to {{limit}}",
"search_notes_limited_other": "{{count}} total results, limited to {{limit}}",
- "search_notes_scope": "Searched in <Note/>",
"child_notes_count_one": "({{count}} child)",
"child_notes_count_other": "({{count}} children)",
"child_count_one": "{{count}} child",
@@ -4267,7 +4266,8 @@
"read_web_page": "Read web page",
"note_moved_from": "<Note/> from <From/> to <To/>",
"note_moved": "<Note/> to <To/>",
- "attachment_in_note": "<Title/> in <Note/>"
+ "attachment_in_note": "<Title/> in <Note/>",
+ "search_in_subtree": "<Query/> in <Subtree/>"
},
"search_providers": "Search Providers",
"no_search_providers_configured": "No search providers configured yet.",
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 1e1456be4a..fec311a15c 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css
@@ -25,6 +25,11 @@
margin-left: 0.25rem;
}
+.llm-chat-tool-call-scope {
+ font-size: 0.75rem;
+ font-weight: 400;
+}
+
/* Shown on hover, on focus, on an open line, and always where there is no hover. */
.llm-chat-tool-call-debug.icon-action {
--icon-button-size: 1.4rem;
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 75c28a2c86..28585b7b0c 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
@@ -148,34 +148,37 @@ describe("ToolCallCard", () => {
{ note: "a", parent: "Space", preview: "Launch The Saturn V rocket" },
{ note: "b", parent: null, preview: null }
]);
- expect(line?.querySelector(".llm-chat-note-results-scope")).toBeNull();
expect(line?.querySelector(".llm-chat-note-results-more")?.textContent)
.toBe("llm_chat.search_notes_limited{\"count\":42,\"limit\":2}");
});
- it("names the subtree a search was confined to, even when it found nothing there", () => {
+ it("names the subtree a search is confined to on its line, before and after it runs", () => {
+ const search = (id: string, ancestorNoteId: string, result?: object): ToolCall => ({
+ id, toolName: "search_notes", input: { query: id, ancestorNoteId }, result: result && JSON.stringify(result)
+ });
+ const other = (id: string): ToolCall => ({ id, toolName: "get_note", input: { noteId: "x" }, result: "{}" });
const target = renderCard([
- {
- id: "1",
- toolName: "search_notes",
- input: { query: "a", ancestorNoteId: "scope" },
- result: JSON.stringify({ totalResults: 12, results: [ { noteId: "a" } ] })
- },
- {
- id: "2",
- toolName: "search_notes",
- input: { query: "b", ancestorNoteId: "scope" },
- result: JSON.stringify({ totalResults: 0, results: [] })
- }
+ search("a", "scope", { totalResults: 12, results: [ { noteId: "a" } ] }),
+ other("1"),
+ search("b", "scope", { totalResults: 0, results: [] }),
+ other("2"),
+ search("c", "scope"),
+ other("3"),
+ search("d", "root", { totalResults: 0, results: [] })
]);
- const lines = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
- expect(lines.map(line => line instanceof HTMLDetailsElement)).toEqual([ true ]);
- const groupedLines = [ ...(lines[0]?.querySelectorAll(".expandable-section-body > .llm-chat-tool-call") ?? []) ];
- expect(groupedLines.map(line => line instanceof HTMLDetailsElement)).toEqual([ true, true ]);
- expect(groupedLines.map(line => line.querySelector(".llm-chat-note-results-scope .note-link-stub")?.textContent))
- .toEqual([ "scope", "scope" ]);
- expect(groupedLines[0]?.querySelector(".llm-chat-note-results-more")?.textContent)
+ const [ found, , empty, , waiting, , everywhere ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+
+ for (const line of [ found, empty, waiting ]) {
+ const scope = line?.querySelector(".llm-chat-tool-call-scope");
+ expect(scope?.textContent?.startsWith("llm.tools.search_in_subtree")).toBe(true);
+ expect(scope?.querySelector(".note-link-stub")?.textContent).toBe("scope");
+ }
+ expect(found?.querySelector(".llm-chat-tool-call-scope .llm-chat-tool-call-detail")?.textContent).toBe("a");
+ expect(found?.querySelector(".llm-chat-note-results-more")?.textContent)
.toBe("llm_chat.search_notes_limited{\"count\":12,\"limit\":1}");
+ expect(empty instanceof HTMLDetailsElement).toBe(false);
+ expect(everywhere?.querySelector(".llm-chat-tool-call-scope")).toBeNull();
+ expect(everywhere?.querySelector(".llm-chat-tool-call-detail")?.textContent).toBe("d");
});
it("keeps a search that found nothing, or returned something unexpected, to a plain line", () => {
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 be12957485..a06d8cd76d 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -34,6 +34,8 @@ interface ToolCallContext {
deletedTitle?: string;
/** The title of the note `create_note` would create, in place of a link to a note not yet there. */
createdTitle?: string;
+ /** The subtree `search_notes` is confined to, unless it searches the whole tree. */
+ scopeNoteId?: string;
/** Plain-text detail (e.g. skill name, search query) when no note ref is available. */
detailText: string | null;
}
@@ -95,7 +97,11 @@ function getToolCallContext(toolCall: ToolCall): ToolCallContext {
const detailText = (input?.name ?? input?.query ?? input?.url) as string | undefined
?? readWebSearchAction(toolCall);
- return { noteId: null, parentNoteId: null, detailText: detailText || null };
+ const { ancestorNoteId } = input;
+ // Searching under the root is the same as not narrowing the search at all.
+ const scopeNoteId = toolCall.toolName === "search_notes" && typeof ancestorNoteId === "string"
+ && ancestorNoteId !== "root" ? ancestorNoteId : undefined;
+ return { noteId: null, parentNoteId: null, scopeNoteId, detailText: detailText || null };
}
/**
@@ -178,20 +184,32 @@ function getErrorMessage(result: string): string {
function ToolCallLabel({ toolCall, view }: { toolCall: ToolCall; view: ToolCallView | null }) {
const context = getToolCallContext(toolCall);
const {
- noteId: refNoteId, parentNoteId: refParentId, move, deletedTitle, createdTitle, detailText
+ noteId: refNoteId, parentNoteId: refParentId, move, deletedTitle, createdTitle, scopeNoteId,
+ detailText
} = context;
const hasError = isFailedToolCall(toolCall);
+ const detail = detailText && (
+ <span className="llm-chat-tool-call-detail">
+ {toolCall.toolName === "read_web_page" && /^https?:\/\//i.test(detailText)
+ ? <ExternalLink url={detailText}>{detailText}</ExternalLink>
+ : detailText}
+ </span>
+ );
return (
<>
<span className="llm-chat-tool-call-name">{t(`llm.tools.${toolCall.toolName}`, { defaultValue: toolCall.toolName })}</span>
- {detailText && (
- <span className="llm-chat-tool-call-detail">
- {toolCall.toolName === "read_web_page" && /^https?:\/\//i.test(detailText)
- ? <ExternalLink url={detailText}>{detailText}</ExternalLink>
- : detailText}
+ {scopeNoteId && detail ? (
+ <span className="llm-chat-tool-call-scope">
+ <Trans
+ i18nKey="llm.tools.search_in_subtree"
+ components={{
+ Query: detail,
+ Subtree: <NewNoteLink notePath={scopeNoteId} showNoteIcon noPreview />
+ } as any}
+ />
</span>
- )}
+ ) : detail}
{view?.lead}
{deletedTitle && <span className="llm-chat-tool-call-deleted-title">{deletedTitle}</span>}
{refNoteId && (
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
index 34f48ef23a..d7a1bc4207 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css
@@ -59,18 +59,6 @@
font-style: italic;
}
-.llm-chat-note-results-scope {
- display: flex;
- align-items: baseline;
- gap: 0.3rem;
- padding: 0.2rem 0.5rem;
- font-size: 0.78rem;
-
- > .bx {
- align-self: center;
- }
-}
-
/* The notes of a `get_subtree` call, one indent and guide line per level. */
.llm-chat-subtree {
max-height: 300px;
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 4cf8c76b1e..61dc7a80c1 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -102,16 +102,14 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
case "search_notes": {
const result = parseSearchNotesResult(toolCall.result);
if (!result) return null;
- const { ancestorNoteId, limit } = toolCall.input;
+ const { limit } = toolCall.input;
const search: NoteSearch = {
...result,
- // Searching under the root is the same as not narrowing the search at all.
- ancestorNoteId: typeof ancestorNoteId === "string" && ancestorNoteId !== "root" ? ancestorNoteId : undefined,
limit: typeof limit === "number" ? limit : result.results.length
};
return {
summary: t("llm_chat.search_notes_count", { count: result.totalResults }),
- body: search.results.length > 0 || search.ancestorNoteId ? <NoteSearchResults {...search} /> : undefined
+ body: search.results.length > 0 ? <NoteSearchResults {...search} /> : undefined
};
}
case "search_help": {
@@ -254,24 +252,14 @@ interface SearchNotesResult {
/** A `search_notes` result with the input that shaped it. */
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, isHelp }: NoteSearch) {
+function NoteSearchResults({ totalResults, results, limit, isHelp }: NoteSearch) {
return (
<div className="llm-chat-note-results">
- {ancestorNoteId && (
- <div className="llm-chat-note-results-scope">
- <span className="bx bx-subdirectory-right" />
- <Trans
- i18nKey="llm_chat.search_notes_scope"
- components={{ Note: <NewNoteLink notePath={ancestorNoteId} showNoteIcon /> } as any}
- />
- </div>
- )}
{results.length > 0 && (
<ul>
{results.map(({ noteId, parentTitle, contentPreview }) => (
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 9af3e4e2a9..565aa06cfe 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
@@ -253,12 +253,12 @@
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.</p>
+ a tool failed. A search names on its line the part of the tree it is confined
+ to, if any, and also shows how many notes it found 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>
<p>When the reply stops moving for a couple of seconds while the AI is still
at work, for example while it reads what a tool returned, a <em>Still working…</em> line
appears under it until the next part of the reply arrives.</p>
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index f80f7c1493..4ced1e1242 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -152,7 +152,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), an attachment by its title, note and size, and a web page the AI read as a link. A line folds open only when there is something to read inline: the notes a search found, the pages a web 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 or of a web page, 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.
+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), an attachment by its title, note and size, and a web page the AI read as a link. A line folds open only when there is something to read inline: the notes a search found, the pages a web 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 or of a web page, 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 names on its line the part of the tree it is confined to, if any, and also shows how many notes it found 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.
When the reply stops moving for a couple of seconds while the AI is still at work, for example while it reads what a tool returned, a _Still working…_ line appears under it until the next part of the reply arrives.