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): move web search to a submenu of the Tools menu
Elian Doranc9b0fe26a8d7
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex 9198539be2..1bcfd4170c 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -405,8 +405,12 @@ export interface FormDropdownSubmenuProps {icon?: string;/** The SVG of an icon drawn in place of {@link icon}, such as a CKEditor toolbar item's. */iconSvg?: string;+ /** An element drawn in place of {@link icon}, such as a `MaskedIcon`, with the `tn-icon` class. */+ iconElement?: ComponentChildren;title: ComponentChildren;children: ComponentChildren;+ /** A line under the title, as {@link FormListItem} shows one. */+ description?: string;/** Called when the row is clicked, or run from the keyboard, on the desktop. */onDropdownToggleClicked?: (e: MouseEvent) => void;/**@@ -432,7 +436,7 @@ export function FormDropdownSubmenu(props: FormDropdownSubmenuProps) {return <MenuSubmenu {...props} menu={menu} />;}-function MenuSubmenu({ menu, icon, iconSvg, title, children, onDropdownToggleClicked, split = !!onDropdownToggleClicked, disabled, className, filterable }: FormDropdownSubmenuProps & { menu: MenuContextValue }) {+function MenuSubmenu({ menu, icon, iconSvg, iconElement, title, description, children, onDropdownToggleClicked, split = !!onDropdownToggleClicked, disabled, className, filterable }: FormDropdownSubmenuProps & { menu: MenuContextValue }) {const level = useContext(MenuLevelContext);const id = useUniqueName("menu-row");const openSubmenu = menu.open[level];@@ -489,9 +493,14 @@ function MenuSubmenu({ menu, icon, iconSvg, title, children, onDropdownToggleCli}}><span className="dropdown-toggle">- {iconSvg ? <SvgIcon svg={iconSvg} /> : <Icon icon={icon} />}+ {iconSvg ? <SvgIcon svg={iconSvg} /> : iconElement ?? <Icon icon={icon} />}<span className="tn-menu-gap" />- <span id={titleId(id)}>{title}</span>+ {description ? (+ <div>+ <span id={titleId(id)}>{title}</span>+ <div className="description">{description}</div>+ </div>+ ) : <span id={titleId(id)}>{title}</span>}</span>{split && !isMobile() && (<spandiff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsxindex c820fbdff8..37a2130001 100644--- a/apps/client/src/widgets/react/Menu.spec.tsx+++ b/apps/client/src/widgets/react/Menu.spec.tsx@@ -118,6 +118,20 @@ describe("Menu with declared rows", () => {expect(calls).toEqual([]);});+ it("draws a submenu row's own icon element and a description under its title", () => {+ render((+ <Menu anchor={{ x: 10, y: 10 }} onClose={vi.fn()}>+ <FormDropdownSubmenu title="Search" description="Tavily" iconElement={<span className="tn-icon mark" />}>+ <FormListItem>Tavily</FormListItem>+ </FormDropdownSubmenu>+ </Menu>+ ), host);+ const toggle = document.querySelector(".dropdown-submenu > .dropdown-toggle");+ expect(toggle?.firstElementChild?.className).toBe("tn-icon mark");+ expect(toggle?.querySelector(".description")?.textContent).toBe("Tavily");+ expect(toggle?.querySelector(".description")?.previousElementSibling?.textContent).toBe("Search");+ });+it("closes the menu after running a row, but not for a row that opens a submenu or is disabled", async () => {const { menu, calls } = renderMenu();diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.cssindex e4741b541e..b4ec21adb4 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.css@@ -20,3 +20,7 @@opacity: 0.8;}}++.tn-dropdown-portal.llm-chat-tools .dropdown-submenu .masked-icon.tn-icon {+ --masked-icon-size: 1em;+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsxindex 47f0af7c1b..7b94a76bbe 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.spec.tsx@@ -35,6 +35,19 @@ vi.mock("../../react/Dropdown.js", () => ({)}));+// Renders a submenu as its row followed by its rows, as the menu draws them once it is open.+vi.mock("../../react/FormList.js", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../../react/FormList.js")>()),+ FormDropdownSubmenu: ({ title, description, iconElement, children }: {+ title: ComponentChildren; description?: string; iconElement?: ComponentChildren; children: ComponentChildren;+ }) => (+ <>+ <div className="submenu-row">{iconElement}{title}<span className="description">{description}</span></div>+ {children}+ </>+ )+}));+import type { WebSearchState } from "../../../services/llm_providers.js";import ChatToolsDropdown, { useChatWebSearch } from "./ChatToolsDropdown.js";@@ -123,12 +136,31 @@ describe("ChatToolsDropdown", () => {await act(async () => menu.row("llm_chat.note_tools")?.click());expect(menu.onNoteToolsChange).toHaveBeenCalledExactlyOnceWith(false);- const manage = host?.querySelector<HTMLButtonElement>(".dropdown-header button.bx-cog");- expect(manage).not.toBeNull();- await act(async () => manage?.click());+ await act(async () => menu.row("llm_chat.manage_search_providers")?.click());expect(mocks.triggerCommand).toHaveBeenCalledExactlyOnceWith("showOptions", { section: "_optionsLlm" });});+ it("shows the chosen web search source on the submenu's row, by its icon and name", () => {+ const submenuRow = (webSearch: WebSearchState) => {+ renderMenu({ modelProvider: "anthropic", webSearch });+ const row = host?.querySelector(".submenu-row");+ expect(row?.textContent?.startsWith("llm_chat.web_search")).toBe(true);+ return { icon: row?.firstElementChild, description: row?.querySelector(".description")?.textContent };+ };++ const off = submenuRow({ choice: "disabled", enableWebSearch: false });+ expect(off.icon?.classList.contains("bx-block")).toBe(true);+ expect(off.description).toBe("llm_chat.web_search_disabled");++ const tavily = submenuRow({ choice: "s1", enableWebSearch: true, webSearchProviderId: "s1" });+ expect(tavily.icon?.classList.contains("masked-icon")).toBe(true);+ const tavilyRowIcon = [ ...(host?.querySelectorAll(".menu .dropdown-item") ?? []) ]+ .find((row) => row.textContent === "Tavily")?.querySelector(".llm-chat-tools-choice-icon");+ expect(tavily.icon?.getAttribute("style")).toBe(tavilyRowIcon?.getAttribute("style"));+ expect(tavilyRowIcon).toBeDefined();+ expect(tavily.description).toBe("Tavily");+ });+it("gives every web search choice an icon, the model's own mark standing for its built-in search", () => {const icons = (menu: ReturnType<typeof renderMenu>) => [ "llm_chat.web_search_disabled", "llm_chat.web_search_builtin", "Tavily" ].map((text) => menu.row(text)?.querySelector(".llm-chat-tools-choice-icon"));diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsxindex 30d7972be1..e310ab065e 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatToolsDropdown.tsx@@ -1,15 +1,13 @@import "./ChatToolsDropdown.css";import clsx from "clsx";-import type { ComponentChildren } from "preact";import { useMemo } from "preact/hooks";import appContext from "../../../components/app_context.js";import { t } from "../../../services/i18n.js";import { readSearchProviders, resolveWebSearch, type SearchProviderOption, type WebSearchState } from "../../../services/llm_providers.js";-import ActionButton from "../../react/ActionButton.js";import Dropdown from "../../react/Dropdown.js";-import { FormDropdownDivider, FormListHeader, FormListItem, FormListToggleableItem } from "../../react/FormList.js";+import { FormDropdownDivider, FormDropdownSubmenu, FormListItem, FormListToggleableItem } from "../../react/FormList.js";import { useTriliumOption } from "../../react/hooks.js";import Icon from "../../react/Icon.js";import MaskedIcon from "../../react/MaskedIcon.js";@@ -17,9 +15,10 @@ import { providerIconUrl } from "../options/llm/provider_icons.js";import type { UseLlmChatReturn } from "./useLlmChat.js";/**- * The Tools menu of the chat input bar: what the model can reach this turn, one section per group- * of tools. Its rows leave the menu open, so several can be changed in one visit. Its toggle- * shares the compact combo box styling of the model picker beside it.+ * The Tools menu of the chat input bar: what the model can reach this turn. Note access is a row of+ * its own; web search is a submenu of its sources, whose row shows the chosen one. Its rows leave+ * the menu open, so several can be changed in one visit. Its toggle shares the compact combo box+ * styling of the model picker beside it.*/export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange, modelProvider, webSearch, searchProviders, onWebSearchChoose, disabled }: {enableNoteTools: boolean;@@ -33,7 +32,8 @@ export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange,disabled?: boolean;}) {const anyActive = enableNoteTools || webSearch.enableWebSearch;- const searchProviderUnavailable = webSearch.searchProviderUnavailableKey && t(webSearch.searchProviderUnavailableKey);+ const sources = getWebSearchSources(modelProvider, webSearch, searchProviders);+ const chosen = sources.find((source) => source.choice === webSearch.choice) ?? sources[0];return (<Dropdown@@ -53,44 +53,33 @@ export default function ChatToolsDropdown({ enableNoteTools, onNoteToolsChange,onChange={onNoteToolsChange}/>- <FormDropdownDivider />- <FormListHeader- text={t("llm_chat.web_search")}- action={- <ActionButton- icon="bx bx-cog"- text={t("llm_chat.manage_search_providers")}- onClick={() => appContext.triggerCommand("showOptions", { section: "_optionsLlm" })}- />- }- />- <WebSearchChoice choice="disabled" webSearch={webSearch} onChoose={onWebSearchChoose}>- <Icon icon="bx bx-block" className="llm-chat-tools-choice-icon" />- {t("llm_chat.web_search_disabled")}- </WebSearchChoice>- <WebSearchChoice- choice="builtin"- webSearch={webSearch}- onChoose={onWebSearchChoose}- unavailableReason={webSearch.builtInUnavailableKey && t(webSearch.builtInUnavailableKey)}+ <FormDropdownSubmenu+ iconElement={<WebSearchSourceIcon source={chosen} className="tn-icon" />}+ title={t("llm_chat.web_search")}+ description={chosen.label}>- {modelProvider- ? <MaskedIcon url={providerIconUrl(modelProvider)} className="llm-chat-tools-choice-icon" />- : <Icon icon="bx bx-globe" className="llm-chat-tools-choice-icon" />}- {t("llm_chat.web_search_builtin")}- </WebSearchChoice>- {searchProviders.map(provider => (- <WebSearchChoice- key={provider.id}- choice={provider.id}- webSearch={webSearch}- onChoose={onWebSearchChoose}- unavailableReason={searchProviderUnavailable}+ {sources.map((source) => (+ <FormListItem+ key={source.choice}+ checkable+ checked={source === chosen}+ disabled={!!source.unavailableReason}+ disabledTooltip={source.unavailableReason}+ closeOnSelect={false}+ onClick={() => onWebSearchChoose(source.choice)}+ >+ <WebSearchSourceIcon source={source} className="llm-chat-tools-choice-icon" />+ {source.label}+ </FormListItem>+ ))}+ <FormDropdownDivider />+ <FormListItem+ icon="bx bx-cog"+ onClick={() => appContext.triggerCommand("showOptions", { section: "_optionsLlm" })}>- <MaskedIcon url={providerIconUrl(provider.provider)} className="llm-chat-tools-choice-icon" />- {provider.name}- </WebSearchChoice>- ))}+ {t("llm_chat.manage_search_providers")}+ </FormListItem>+ </FormDropdownSubmenu></Dropdown>);}@@ -129,27 +118,44 @@ export function useChatWebSearch(return { webSearch, searchProviders, chooseWebSearch };}-/**- * One choice of the web search section. A choice the current model can't use stays listed,- * disabled, with the reason on an info icon beside it; the marked one is shown as such either way.- */-function WebSearchChoice({ choice, webSearch, onChoose, unavailableReason, children }: {+/** A source web search can be turned to: off, the model's built-in search, or a search provider. */+interface WebSearchSource {+ /** `"disabled"`, `"builtin"` or the config id of a search provider. */choice: string;- webSearch: WebSearchState;- onChoose: (choice: string) => void;+ label: string;+ /** A boxicons class, or the URL of a mark drawn as a `MaskedIcon`. */+ icon: { className: string } | { url: string };+ /** Why the current model can't use the source. A source it can't use stays listed, disabled. */unavailableReason?: string;- children: ComponentChildren;-}) {- return (- <FormListItem- checkable- checked={webSearch.choice === choice}- disabled={!!unavailableReason}- disabledTooltip={unavailableReason}- closeOnSelect={false}- onClick={() => onChoose(choice)}- >- {children}- </FormListItem>- );+}++function getWebSearchSources(+ modelProvider: string | undefined,+ webSearch: WebSearchState,+ searchProviders: SearchProviderOption[]+): WebSearchSource[] {+ const searchProviderUnavailable = webSearch.searchProviderUnavailableKey+ && t(webSearch.searchProviderUnavailableKey);+ return [+ { choice: "disabled", label: t("llm_chat.web_search_disabled"), icon: { className: "bx bx-block" } },+ {+ choice: "builtin",+ label: t("llm_chat.web_search_builtin"),+ // The model's own mark stands for its built-in search.+ icon: modelProvider ? { url: providerIconUrl(modelProvider) } : { className: "bx bx-globe" },+ unavailableReason: webSearch.builtInUnavailableKey && t(webSearch.builtInUnavailableKey)+ },+ ...searchProviders.map((provider) => ({+ choice: provider.id,+ label: provider.name,+ icon: { url: providerIconUrl(provider.provider) },+ unavailableReason: searchProviderUnavailable+ }))+ ];+}++function WebSearchSourceIcon({ source, className }: { source: WebSearchSource; className: string }) {+ return "url" in source.icon+ ? <MaskedIcon url={source.icon.url} className={className} />+ : <Icon icon={source.icon.className} className={className} />;}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 26ec30502a..9af3e4e2a9 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@@ -112,9 +112,10 @@<h3>Web search</h3><p>The AI can optionally search the web to find more information about aspecific topic. </p>-<p>Where the search comes from is chosen under <em>Web search</em> in the <span- class="tn-icon bx bx-shield-quarter"></span> <em>Tools</em> menu next to- the model selector at the bottom of the chat:</p>+<p>Where the search comes from is chosen in the <em>Web search</em> submenu+ of the <span class="tn-icon bx bx-shield-quarter"></span> <em>Tools</em> menu+ next to the model selector at the bottom of the chat, whose row shows the+ source in use:</p><ul><li><em>Disabled</em> turns web search off for this chat.</li><li><em>Use the model's built-in search</em>, the default, lets the model@@ -125,10 +126,9 @@<li>A search provider (Brave Search, Tavily, Exa, Serper, Perplexity or SearXNG)searches through that service instead, whichever model is answering. Searchproviders are added in the <em>Search Providers</em> section of the AI settings,- which the <span class="tn-icon bx bx-cog"></span> button beside the <em>Web search</em> heading- in the menu opens. The browser version can't use Brave Search, Exa or Perplexity,- whose services refuse requests made from a web page, so they're shown disabled- there.</li>+ which <em>Manage search providers</em> at the bottom of the submenu opens.+ The browser version can't use Brave Search, Exa or Perplexity, whose services+ refuse requests made from a web page, so they're shown disabled there.</li></ul><p>To use SearXNG, enable its JSON output by adding <code>json</code> to thesearch formats in the instance's <code>settings.yml</code>. SearXNG turnsdiff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 60a4108f3b..f80f7c1493 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -85,11 +85,11 @@ Pricing information is displayed for known models. The pricing information (pricThe AI can optionally search the web to find more information about a specific topic.-Where the search comes from is chosen under _Web search_ in the <span class="tn-icon bx bx-shield-quarter"></span> _Tools_ menu next to the model selector at the bottom of the chat:+Where the search comes from is chosen in the _Web search_ submenu of the <span class="tn-icon bx bx-shield-quarter"></span> _Tools_ menu next to the model selector at the bottom of the chat, whose row shows the source in use:* _Disabled_ turns web search off for this chat.* _Use the model's built-in search_, the default, lets the model search with its provider's own search. Anthropic, OpenAI and Google Gemini models have one, as do Claude Code, Google Antigravity and OpenAI Codex. DeepSeek, Ollama, LM Studio and custom endpoints don't, so this choice is disabled for their models.-* A search provider (Brave Search, Tavily, Exa, Serper, Perplexity or SearXNG) searches through that service instead, whichever model is answering. Search providers are added in the _Search Providers_ section of the AI settings, which the <span class="tn-icon bx bx-cog"></span> button beside the _Web search_ heading in the menu opens. The browser version can't use Brave Search, Exa or Perplexity, whose services refuse requests made from a web page, so they're shown disabled there.+* A search provider (Brave Search, Tavily, Exa, Serper, Perplexity or SearXNG) searches through that service instead, whichever model is answering. Search providers are added in the _Search Providers_ section of the AI settings, which _Manage search providers_ at the bottom of the submenu opens. The browser version can't use Brave Search, Exa or Perplexity, whose services refuse requests made from a web page, so they're shown disabled there.To use SearXNG, enable its JSON output by adding `json` to the search formats in the instance's `settings.yml`. SearXNG turns JSON off by default, and without it every search fails.