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): add Serper as a search provider
Elian Doran184881a4d6ac
diff --git a/apps/client/src/widgets/type_widgets/options/llm/AddProviderModal.tsx b/apps/client/src/widgets/type_widgets/options/llm/AddProviderModal.tsxindex 5e997bd185..37af5c5434 100644--- a/apps/client/src/widgets/type_widgets/options/llm/AddProviderModal.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm/AddProviderModal.tsx@@ -167,6 +167,7 @@ export const SEARCH_PROVIDER_TYPES: ProviderType[] = [{ id: "brave", name: "Brave Search", group: "search-cloud", defaultBaseUrl: "https://api.search.brave.com/res/v1", iconUrl: PROVIDER_ICONS.brave },{ id: "tavily", name: "Tavily", group: "search-cloud", defaultBaseUrl: "https://api.tavily.com", iconUrl: PROVIDER_ICONS.tavily },{ id: "exa", name: "Exa", group: "search-cloud", defaultBaseUrl: "https://api.exa.ai", iconUrl: PROVIDER_ICONS.exa },+ { id: "serper", name: "Serper", group: "search-cloud", defaultBaseUrl: "https://google.serper.dev", iconUrl: PROVIDER_ICONS.serper },{id: "searxng", name: "SearXNG", group: "search-local", defaultBaseUrl: "http://localhost:8888", prefillBaseUrl: true,iconUrl: PROVIDER_ICONS.searxng,diff --git a/apps/client/src/widgets/type_widgets/options/llm/icons/search.svg b/apps/client/src/widgets/type_widgets/options/llm/icons/search.svgnew file mode 100644index 0000000000..de93f3ff21--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/llm/icons/search.svg@@ -0,0 +1 @@+<svg width="24" height="24" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M10 2a8 8 0 0 1 6.32 12.9l5.39 5.39-1.42 1.42-5.39-5.39A8 8 0 1 1 10 2m0 2a6 6 0 1 0 0 12 6 6 0 0 0 0-12"/></svg>diff --git a/apps/client/src/widgets/type_widgets/options/llm/provider_icons.ts b/apps/client/src/widgets/type_widgets/options/llm/provider_icons.tsindex 2cdcc06a42..92f5389f3b 100644--- a/apps/client/src/widgets/type_widgets/options/llm/provider_icons.ts+++ b/apps/client/src/widgets/type_widgets/options/llm/provider_icons.ts@@ -24,6 +24,7 @@ import lmStudioIcon from "./icons/lmstudio.svg?url";import ollamaIcon from "./icons/ollama.svg?url";import openaiIcon from "./icons/openai.svg?url";import openAiCompatibleIcon from "./icons/robot.svg?url";+import searchIcon from "./icons/search.svg?url";import searxngIcon from "./icons/searxng.svg?url";import tavilyIcon from "./icons/tavily.svg?url";@@ -42,6 +43,7 @@ export const PROVIDER_ICONS: Record<string, string> = {openai: openaiIcon,"openai-compatible": openAiCompatibleIcon,searxng: searxngIcon,+ serper: searchIcon,tavily: tavilyIcon};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 2e61bf6978..e7d272eec3 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@@ -122,9 +122,9 @@models have one, as do Claude Code, Google Antigravity and OpenAI Codex.DeepSeek, Ollama, LM Studio and custom endpoints don't, so this choiceis disabled for their models.</li>- <li>A search provider (Brave Search, Tavily, Exa or SearXNG) searches through- that service instead, whichever model is answering. Search providers are- added in the <em>Search Providers</em> section of the AI settings, which+ <li>A search provider (Brave Search, Tavily, Exa, Serper or SearXNG) searches+ through that service instead, whichever model is answering. Search providers+ are added in the <em>Search Providers</em> section of the AI settings, whichthe <span class="tn-icon bx bx-cog"></span> button beside the <em>Web search</em> headingin the menu opens.</li></ul>diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.htmlindex 63f425fc4f..d8a4548317 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html@@ -76,8 +76,8 @@makes that note fetchable.</li><li><strong>Your search query</strong>, when web search is enabled, the model'ssearch terms reach the provider's own search infrastructure, or the search- provider chosen in the <em>Tools</em> menu (Brave Search, Tavily, Exa or- your SearXNG instance).</li>+ provider chosen in the <em>Tools</em> menu (Brave Search, Tavily, Exa, Serper+ or your SearXNG instance).</li><li><strong>The pages the model reads</strong>, when web search goes througha search provider: Trilium fetches each page the model asks for from thesite itself, so the site sees a request from the machine running Trilium.diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 2b10fbedf3..f82bab4f8f 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -89,7 +89,7 @@ Where the search comes from is chosen under _Web search_ in the <span class="tn-* _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 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.+* A search provider (Brave Search, Tavily, Exa, Serper 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.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.diff --git a/docs/User Guide/User Guide/AI/Privacy.md b/docs/User Guide/User Guide/AI/Privacy.mdindex 38f007fa0d..3cdcd0f880 100644--- a/docs/User Guide/User Guide/AI/Privacy.md+++ b/docs/User Guide/User Guide/AI/Privacy.md@@ -30,7 +30,7 @@ This is the reason the provider list is grouped this way, and why Trilium pre-se* Tools let the model search your tree and read notes, attributes and attachments beyond the current one.* It decides what to fetch; there is no per-note permission system, so the reachable scope is your whole tree.* **Attachments and mentions**, when you add them: images, PDFs and text files are sent to the provider, and an `@` mention makes that note fetchable.-* **Your search query**, when web search is enabled, the model's search terms reach the provider's own search infrastructure, or the search provider chosen in the _Tools_ menu (Brave Search, Tavily, Exa or your SearXNG instance).+* **Your search query**, when web search is enabled, the model's search terms reach the provider's own search infrastructure, or the search provider chosen in the _Tools_ menu (Brave Search, Tavily, Exa, Serper or your SearXNG instance).* **The pages the model reads**, when web search goes through a search provider: Trilium fetches each page the model asks for from the site itself, so the site sees a request from the machine running Trilium. The page's text is then sent to the model's provider.## The in-editor AI assistantdiff --git a/packages/trilium-core/src/services/llm/web_search.spec.ts b/packages/trilium-core/src/services/llm/web_search.spec.tsindex 8e099fb2a2..e90f4e7675 100644--- a/packages/trilium-core/src/services/llm/web_search.spec.ts+++ b/packages/trilium-core/src/services/llm/web_search.spec.ts@@ -59,6 +59,21 @@ describe("searchWeb", () => {expect(exa.body).toMatchObject({ query: "q", numResults: 8 });});+ it("posts to Serper with its key and reads Google's organic results by their link", async () => {+ respond({+ answerBox: { title: "Ignored" },+ organic: [ { title: PAGE.title, link: PAGE.url, snippet: "From Google", position: 1 } ]+ });+ const sources = await searchWeb({ provider: "serper", apiKey: "sk" }, "q");++ const { url, init, headers, body } = request();+ expect(url).toBe("https://google.serper.dev/search");+ expect(init.method).toBe("POST");+ expect(headers["X-API-KEY"]).toBe("sk");+ expect(body).toEqual({ q: "q", num: 8 });+ expect(sources).toEqual([ { ...PAGE, snippet: "From Google" } ]);+ });+it("asks SearXNG for JSON, sends a key only when there is one, and keeps web URLs only", async () => {respond({ results: [ { ...PAGE, content: "Hit" }, { title: "Local", url: "file:///etc/passwd" }, { url: "https://untitled.example" } ] });const sources = await searchWeb({ provider: "searxng", apiKey: "", baseURL: "http://localhost:8888/" }, "q");diff --git a/packages/trilium-core/src/services/llm/web_search.ts b/packages/trilium-core/src/services/llm/web_search.tsindex f3f5da0bf0..16895f0e92 100644--- a/packages/trilium-core/src/services/llm/web_search.ts+++ b/packages/trilium-core/src/services/llm/web_search.ts@@ -88,6 +88,14 @@ export async function searchWeb(setup: WebSearchSetup, query: string): Promise<W}) as { results?: unknown[] };return toSources(payload.results, "text");}+ case "serper": {+ const payload = await requestJson(`${setup.baseURL ?? "https://google.serper.dev"}/search`, {+ method: "POST",+ headers: { "Content-Type": "application/json", "X-API-KEY": setup.apiKey },+ body: JSON.stringify({ q: query, num: MAX_RESULTS })+ }) as { organic?: unknown[] };+ return toSources(payload.organic, "snippet", "link");+ }case "searxng": {if (!setup.baseURL) {throw new Error("The SearXNG search provider has no address.");@@ -182,11 +190,11 @@ async function requestJson(url: string, init: RequestInit): Promise<unknown> {return await response.json();}-/** The results that carry a web URL, with the excerpt read from `snippetField`. */-function toSources(results: unknown[] | undefined, snippetField: string): WebSearchSource[] {+/** The results that carry a web URL, read from `urlField`, with the excerpt read from `snippetField`. */+function toSources(results: unknown[] | undefined, snippetField: string, urlField = "url"): WebSearchSource[] {const sources: WebSearchSource[] = [];for (const result of results ?? []) {- const { title, url, [snippetField]: snippet } = (result ?? {}) as Record<string, unknown>;+ const { title, [urlField]: url, [snippetField]: snippet } = (result ?? {}) as Record<string, unknown>;if (typeof url === "string" && /^https?:\/\//i.test(url)) {sources.push({title: typeof title === "string" ? title : url,