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_chat): mark a search a search provider ran with its logo
Elian Doranc069b741022c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex ba757455df..5e8ce964d3 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2727,7 +2727,8 @@"web_search_builtin": "Use the model's built-in search","manage_search_providers": "Manage search providers…","web_search_builtin_unsupported": "This model has no built-in web search. Choose a search provider instead.",- "web_search_provider_unsupported": "This model's provider searches the web with its own tools."+ "web_search_provider_unsupported": "This model's provider searches the web with its own tools.",+ "searched_with": "Searched with {{name}}"},"sidebar_chat": {"title": "AI Chat",diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsxindex 2475e7c4ff..10c5b252dc 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx@@ -3,7 +3,8 @@ import "./ExpandableCard.css";import type { ComponentChildren } from "preact";interface ExpandableSectionProps {- icon: string;+ /** The classes of an icon font glyph, or an element to stand in its place. */+ icon: string | ComponentChildren;label: ComponentChildren;className?: string;/** Whether the section is expanded on initial render. */@@ -23,7 +24,7 @@ export function ExpandableSection({ icon, label, className, open, children, actireturn (<details className={`expandable-section ${variant === "line" ? "expandable-line" : ""} ${className ?? ""}`} open={open}><summary className="expandable-section-summary">- <span className={icon} />+ {typeof icon === "string" ? <span className={icon} /> : icon}<span className="expandable-section-label">{label}</span><span className="bx bx-chevron-down expandable-section-chevron" />{actions}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.cssindex a28381b3ff..9d0de5a57b 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.css@@ -102,3 +102,14 @@margin-left: 0.3rem;}++/* Sized as the glyphs beside it in `.expandable-line` (0.9rem in a 1.4rem line). */+.llm-chat-tool-call-provider {+ display: flex;+ height: 1.4rem;+ align-items: center;++ .masked-icon {+ --masked-icon-size: 0.9rem;+ }+}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 16aba53caf..340b0cc089 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@@ -721,6 +721,26 @@ describe("ToolCallCard", () => {expect(sources(badUrl)).toEqual([ { text: "Bad", href: "https://bad host/", domain: "" } ]);});+ it("marks a search a configured search provider ran with its logo and name, a built-in one with the magnifier", () => {+ const viaTavily = (id: string): ToolCall => ({+ id, toolName: "web_search", input: { query: "trilium" },+ result: JSON.stringify({ query: "trilium", searchProvider: { type: "tavily", name: "Tavily" }, sources: [ { url: "https://triliumnotes.org", title: "Trilium" } ] })+ });+ const builtIn: ToolCall = { id: "b", toolName: "web_search", input: { query: "q" }, result: JSON.stringify([ { url: "https://a.com", title: "A" } ]) };+ /** The first element of a line's header, which is its icon. */+ const icon = (line: Element | null | undefined) => line?.querySelector(".expandable-section-summary, .expandable-line-header")?.firstElementChild;++ const single = icon(renderCard([ viaTavily("1") ]).querySelector(".llm-chat-tool-call"));+ expect(single?.getAttribute("title")).toBe("llm_chat.searched_with{\"name\":\"Tavily\"}");+ expect(single?.querySelector(".masked-icon")).not.toBeNull();++ // A run of searches is one line, marked as its searches are.+ const group = icon(renderCard([ viaTavily("1"), viaTavily("2") ]).querySelector(".llm-chat-tool-call-group"));+ expect(group?.getAttribute("title")).toBe("llm_chat.searched_with{\"name\":\"Tavily\"}");++ expect(icon(renderCard([ builtIn ]).querySelector(".llm-chat-tool-call"))?.className).toBe("bx bx-search");+ });+it("links the page a call read, and previews what it read there", () => {const target = renderCard([ {id: "1",diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex 49c54e12b6..2841666947 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -5,7 +5,9 @@ import { Trans } from "react-i18next";import appContext from "../../../components/app_context.js";import { t } from "../../../services/i18n.js";import ActionButton from "../../react/ActionButton.js";+import MaskedIcon from "../../react/MaskedIcon.js";import { NewNoteLink } from "../../react/NoteLink.js";+import { providerIconUrl } from "../options/llm/provider_icons.js";import { EditNoteContentDiff, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";import { ExpandableSection } from "./ExpandableCard.js";import { isFailedToolCall, type ToolCall } from "./llm_chat_types.js";@@ -109,10 +111,36 @@ function toolNameIcon(toolName: string): string {return "bx bx-wrench";}-function toolCallIcon(toolCall: ToolCall): string {+function toolCallIcon(toolCall: ToolCall) {if (isFailedToolCall(toolCall)) return "bx bx-error-circle";if (!toolCall.result) return "bx bx-loader-alt bx-spin";- return toolNameIcon(toolCall.toolName);+ const searchProvider = readSearchProvider(toolCall);+ return searchProvider ? <SearchProviderIcon {...searchProvider} /> : toolNameIcon(toolCall.toolName);+}++/**+ * The search provider named in a `web_search` result, which a configured search provider's+ * search carries and the model's built-in search does not.+ */+function readSearchProvider(toolCall: ToolCall): { type: string; name: string } | undefined {+ if (toolCall.toolName !== "web_search" || !toolCall.result) return undefined;+ try {+ const { searchProvider } = JSON.parse(toolCall.result) ?? {};+ return typeof searchProvider?.type === "string" && typeof searchProvider.name === "string"+ ? { type: searchProvider.type, name: searchProvider.name }+ : undefined;+ } catch {+ return undefined;+ }+}++/** The logo of the search provider that ran a search, in place of the magnifier. */+function SearchProviderIcon({ type, name }: { type: string; name: string }) {+ return (+ <span className="llm-chat-tool-call-provider" title={t("llm_chat.searched_with", { name })}>+ <MaskedIcon url={providerIconUrl(type)} />+ </span>+ );}/** The message of a failed call: the `error` field of a JSON result, or else the whole result. */@@ -206,7 +234,7 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {return (<div className={`expandable-line ${className}`}><div className="expandable-line-header">- <span className={icon} />+ {typeof icon === "string" ? <span className={icon} /> : icon}<span className="expandable-section-label">{label}</span>{debugButton}</div>@@ -257,7 +285,11 @@ function ToolCallGroupSection({ toolCalls }: { toolCalls: ToolCall[] }) {const anyPending = toolCalls.some(tc => !tc.result);const anyError = toolCalls.some(isFailedToolCall);- const icon = anyPending ? "bx bx-loader-alt bx-spin" : toolNameIcon(first.toolName);+ const searchProvider = readSearchProvider(first);+ const icon = anyPending ? "bx bx-loader-alt bx-spin"+ : searchProvider && toolCalls.every(tc => readSearchProvider(tc)?.type === searchProvider.type)+ ? <SearchProviderIcon {...searchProvider} />+ : toolNameIcon(first.toolName);const friendlyName = t(`llm.tools.${first.toolName}`, { defaultValue: first.toolName });const label = (<>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 6c504f4fd4..c158e4c028 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@@ -133,6 +133,9 @@hover the <span class="tn-icon bx bx-info-circle"></span> icon next to itto see why. Claude Code, GitHub Copilot, Google Antigravity and OpenAICodex run their own tools, so they can't use a search provider.</p>+<p>In the chat, a search run through a search provider shows that provider's+ logo at the start of its line instead of the <span class="tn-icon bx bx-search"></span> magnifier;+ hover the logo to see the provider's name.</p><h3>Thinking</h3><p>Some models reason before they answer. While the model is thinking, itsreasoning is shown in full under a spinner; once it is done, it folds intodiff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 357c50b5c3..a69c4a7b52 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -93,6 +93,8 @@ Where the search comes from is chosen under _Web search_ in the <span class="tn-Whether web search is on is kept per chat; the search source is shared by all chats. A choice the current model can't use stays in the menu, disabled; hover the <span class="tn-icon bx bx-info-circle"></span> icon next to it to see why. Claude Code, GitHub Copilot, Google Antigravity and OpenAI Codex run their own tools, so they can't use a search provider.+In the chat, a search run through a search provider shows that provider's logo at the start of its line instead of the <span class="tn-icon bx bx-search"></span> magnifier; hover the logo to see the provider's name.+### ThinkingSome models reason before they answer. While the model is thinking, its reasoning is shown in full under a spinner; once it is done, it folds into a collapsible _Thought process_ line above the reply.diff --git a/packages/trilium-core/src/services/llm/web_search.spec.ts b/packages/trilium-core/src/services/llm/web_search.spec.tsindex 775f0becff..28beee4aca 100644--- a/packages/trilium-core/src/services/llm/web_search.spec.ts+++ b/packages/trilium-core/src/services/llm/web_search.spec.ts@@ -80,11 +80,15 @@ describe("createWebSearchTool", () => {const run = (tool: ReturnType<typeof createWebSearchTool>, query: string) =>tool.execute?.({ query }, { toolCallId: "1", messages: [], context: {} });- it("returns the query and its sources, or the failure for the model to report", async () => {- const tool = createWebSearchTool({ provider: "tavily", apiKey: "tk" });+ it("returns the query, the provider that ran it and its sources, or the failure for the model to report", async () => {+ const tool = createWebSearchTool({ provider: "tavily", name: "My Tavily", apiKey: "tk" });respond({ results: [ { ...PAGE, content: "Hit" } ] });- expect(await run(tool, "trilium")).toEqual({ query: "trilium", sources: [ { ...PAGE, snippet: "Hit" } ] });+ expect(await run(tool, "trilium")).toEqual({+ query: "trilium",+ searchProvider: { type: "tavily", name: "My Tavily" },+ sources: [ { ...PAGE, snippet: "Hit" } ]+ });respond({}, 403);expect(await run(tool, "trilium")).toEqual({ error: expect.stringContaining("HTTP 403") });diff --git a/packages/trilium-core/src/services/llm/web_search.ts b/packages/trilium-core/src/services/llm/web_search.tsindex a0804b252a..d74be07c4b 100644--- a/packages/trilium-core/src/services/llm/web_search.ts+++ b/packages/trilium-core/src/services/llm/web_search.ts@@ -12,6 +12,8 @@ import { llmFetch } from "./providers/fetch.js";/** A configured search provider, as stored in the `llmProviders` option. */export interface WebSearchSetup {provider: string;+ /** The name the user sees the provider by; the provider type stands in when absent. */+ name?: string;apiKey: string;baseURL?: string;}@@ -27,7 +29,8 @@ const SEARCH_TIMEOUT_MS = 30_000;/*** The `web_search` tool for `setup`. Its result has the `sources` the chat lists under the tool's- * line, and a failure is returned as `{ error }` so the model can tell the user about it.+ * line and the `searchProvider` whose mark the line shows, and a failure is returned as+ * `{ error }` so the model can tell the user about it.*/export function createWebSearchTool(setup: WebSearchSetup) {return tool({@@ -37,7 +40,11 @@ export function createWebSearchTool(setup: WebSearchSetup) {}),execute: async ({ query }) => {try {- return { query, sources: await searchWeb(setup, query) };+ return {+ query,+ searchProvider: { type: setup.provider, name: setup.name ?? setup.provider },+ sources: await searchWeb(setup, query)+ };} catch (e) {return { error: e instanceof Error ? e.message : String(e) };}