LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。
TypeScript
Add SearXNG web search provider
nash_su95f7393d0e5f
diff --git a/src/components/settings/sections/web-search-section.tsx b/src/components/settings/sections/web-search-section.tsxindex 94088139..5dba3638 100644--- a/src/components/settings/sections/web-search-section.tsx+++ b/src/components/settings/sections/web-search-section.tsx@@ -9,7 +9,7 @@ import {type SearchProvider,type SearchProviderOverride,} from "@/stores/wiki-store"-import { SERPAPI_ENGINE_OPTIONS, resolveSearchConfig } from "@/lib/web-search"+import { SEARXNG_CATEGORY_OPTIONS, SERPAPI_ENGINE_OPTIONS, resolveSearchConfig } from "@/lib/web-search"const SEARCH_PROVIDERS = [{@@ -17,12 +17,21 @@ const SEARCH_PROVIDERS = [label: "Tavily",hint: "General web search for Deep Research",keyPlaceholder: "Enter your Tavily API key (tavily.com)",+ needsApiKey: true,},{id: "serpapi",label: "SerpApi",hint: "Google, Bing, DuckDuckGo, Scholar, News, Images, Videos, YouTube",keyPlaceholder: "Enter your SerpApi API key (serpapi.com)",+ needsApiKey: true,+ },+ {+ id: "searxng",+ label: "SearXNG",+ hint: "Self-hosted metasearch via the SearXNG JSON API",+ urlPlaceholder: "https://search.example.com",+ needsApiKey: false,},] as const@@ -71,7 +80,9 @@ export function WebSearchSection() {{SEARCH_PROVIDERS.map((provider) => {const override = resolvedConfig.providerConfigs?.[provider.id]const isActive = resolvedConfig.provider === provider.id- const hasConfig = !!override?.apiKey+ const hasConfig = provider.id === "searxng"+ ? !!override?.searXngUrl+ : !!override?.apiKeyconst isExpanded = !!expanded[provider.id]return (<div@@ -136,15 +147,29 @@ export function WebSearchSection() {{isExpanded && (<div className="space-y-4 border-t bg-background/50 px-4 py-3">- <div className="space-y-2">- <Label>API Key</Label>- <Input- type="password"- value={override?.apiKey ?? ""}- onChange={(e) => updateProvider(provider.id, { apiKey: e.target.value })}- placeholder={provider.keyPlaceholder}- />- </div>+ {provider.needsApiKey ? (+ <div className="space-y-2">+ <Label>API Key</Label>+ <Input+ type="password"+ value={override?.apiKey ?? ""}+ onChange={(e) => updateProvider(provider.id, { apiKey: e.target.value })}+ placeholder={provider.keyPlaceholder}+ />+ </div>+ ) : (+ <div className="space-y-2">+ <Label>Instance URL</Label>+ <Input+ value={override?.searXngUrl ?? resolvedConfig.searXngUrl ?? ""}+ onChange={(e) => updateProvider("searxng", { searXngUrl: e.target.value })}+ placeholder={provider.urlPlaceholder}+ />+ <p className="text-xs text-muted-foreground">+ The instance must allow JSON search responses (`format=json`).+ </p>+ </div>+ )}{provider.id === "serpapi" && (<SerpApiEnginePicker@@ -152,6 +177,13 @@ export function WebSearchSection() {onChange={(serpApiEngine) => updateProvider("serpapi", { serpApiEngine })}/>)}++ {provider.id === "searxng" && (+ <SearXngCategoryPicker+ value={override?.searXngCategories ?? resolvedConfig.searXngCategories ?? ["general"]}+ onChange={(searXngCategories) => updateProvider("searxng", { searXngCategories })}+ />+ )}</div>)}</div>@@ -162,6 +194,49 @@ export function WebSearchSection() {)}+function SearXngCategoryPicker({+ value,+ onChange,+}: {+ value: string[]+ onChange: (value: string[]) => void+}) {+ const selected = value.length > 0 ? value : ["general"]++ function toggle(category: string) {+ const next = selected.includes(category)+ ? selected.filter((item) => item !== category)+ : [...selected, category]+ onChange(next.length > 0 ? next : ["general"])+ }++ return (+ <div className="space-y-2">+ <Label>Search categories</Label>+ <div className="flex flex-wrap gap-1.5">+ {SEARXNG_CATEGORY_OPTIONS.map((category) => (+ <button+ key={category.value}+ type="button"+ onClick={() => toggle(category.value)}+ className={`rounded-md border px-2.5 py-1 text-xs transition-colors ${+ selected.includes(category.value)+ ? "border-primary bg-primary text-primary-foreground"+ : "border-border hover:bg-accent"+ }`}+ title={category.hint}+ >+ {category.label}+ </button>+ ))}+ </div>+ <p className="text-xs text-muted-foreground">+ Categories are sent as the SearXNG `categories` parameter.+ </p>+ </div>+ )+}+function SerpApiEnginePicker({value,onChange,diff --git a/src/lib/web-search.test.ts b/src/lib/web-search.test.tsindex 6c825250..5368144f 100644--- a/src/lib/web-search.test.ts+++ b/src/lib/web-search.test.ts@@ -85,6 +85,65 @@ describe("webSearch", () => {])})+ it("calls SearXNG JSON search with the configured instance and categories", async () => {+ fetchMock.mockResolvedValueOnce(jsonResponse({+ results: [+ {+ title: "SearXNG result",+ url: "https://docs.example/page",+ content: "Result content",+ engine: "duckduckgo",+ },+ ],+ }))++ const out = await webSearch(+ "local search",+ {+ provider: "searxng",+ apiKey: "",+ providerConfigs: {+ searxng: {+ searXngUrl: "https://search.example.com",+ searXngCategories: ["general", "news"],+ },+ },+ },+ 3,+ )+ const [url, init] = fetchMock.mock.calls[0]+ const parsed = new URL(String(url))++ expect(parsed.origin + parsed.pathname).toBe("https://search.example.com/search")+ expect(parsed.searchParams.get("q")).toBe("local search")+ expect(parsed.searchParams.get("format")).toBe("json")+ expect(parsed.searchParams.get("categories")).toBe("general,news")+ expect(init).toEqual(expect.objectContaining({ method: "GET" }))+ expect(out).toEqual([+ { title: "SearXNG result", url: "https://docs.example/page", snippet: "Result content", source: "docs.example" },+ ])+ })++ it("preserves SearXNG subpath instances when building the search endpoint", async () => {+ fetchMock.mockResolvedValueOnce(jsonResponse({ results: [] }))++ await webSearch(+ "subpath",+ {+ provider: "searxng",+ apiKey: "",+ providerConfigs: {+ searxng: { searXngUrl: "http://localhost:8080/searx/" },+ },+ },+ 5,+ )+ const parsed = new URL(String(fetchMock.mock.calls[0][0]))++ expect(parsed.origin + parsed.pathname).toBe("http://localhost:8080/searx/search")+ expect(parsed.searchParams.get("categories")).toBe("general")+ })+it("surfaces SerpApi JSON errors", async () => {fetchMock.mockResolvedValueOnce(jsonResponse({ error: "Invalid API key" }))@@ -94,6 +153,10 @@ describe("webSearch", () => {it("requires a configured search provider and key", async () => {await expect(webSearch("x", { provider: "none", apiKey: "" }, 5))+ .rejects.toThrow("Select a search provider")+ await expect(webSearch("x", { provider: "serpapi", apiKey: "" }, 5)).rejects.toThrow("Tavily or SerpApi API key")+ await expect(webSearch("x", { provider: "searxng", apiKey: "" }, 5))+ .rejects.toThrow("SearXNG instance URL")})})diff --git a/src/lib/web-search.ts b/src/lib/web-search.tsindex 30c93f38..20024965 100644--- a/src/lib/web-search.ts+++ b/src/lib/web-search.ts@@ -1,4 +1,10 @@-import type { SearchApiConfig, SearchProvider, SearchProviderConfigs, SerpApiEngine } from "@/stores/wiki-store"+import type {+ SearchApiConfig,+ SearchProvider,+ SearchProviderConfigs,+ SearXngCategory,+ SerpApiEngine,+} from "@/stores/wiki-store"import { getHttpFetch, isFetchNetworkError } from "@/lib/tauri-fetch"export interface WebSearchResult {@@ -20,10 +26,38 @@ export const SERPAPI_ENGINE_OPTIONS: { value: SerpApiEngine; label: string; hint{ value: "youtube", label: "YouTube", hint: "YouTube video results" },]+export const SEARXNG_CATEGORY_OPTIONS: { value: SearXngCategory; label: string; hint: string }[] = [+ { value: "general", label: "General", hint: "Default web results" },+ { value: "news", label: "News", hint: "News engines" },+ { value: "science", label: "Science", hint: "Academic and science-focused engines" },+ { value: "it", label: "IT", hint: "Developer and technology engines" },+ { value: "images", label: "Images", hint: "Image search results" },+ { value: "videos", label: "Videos", hint: "Video search results" },+ { value: "files", label: "Files", hint: "File and document search" },+ { value: "map", label: "Map", hint: "Map and location results" },+ { value: "music", label: "Music", hint: "Music engines" },+ { value: "social media", label: "Social", hint: "Social media engines" },+]+export function resolveSearchConfig(config: SearchApiConfig): SearchApiConfig {const providerConfigs: SearchProviderConfigs = config.providerConfigs ?? {...(config.provider !== "none" && config.apiKey- ? { [config.provider]: { apiKey: config.apiKey, serpApiEngine: config.serpApiEngine } }+ ? {+ [config.provider]: {+ apiKey: config.apiKey,+ serpApiEngine: config.serpApiEngine,+ searXngUrl: config.searXngUrl,+ searXngCategories: config.searXngCategories,+ },+ }+ : {}),+ ...(config.provider === "searxng" && config.searXngUrl+ ? {+ searxng: {+ searXngUrl: config.searXngUrl,+ searXngCategories: config.searXngCategories,+ },+ }: {}),}@@ -34,6 +68,8 @@ export function resolveSearchConfig(config: SearchApiConfig): SearchApiConfig {provider: "none",apiKey: "",serpApiEngine: config.serpApiEngine ?? providerConfigs.serpapi?.serpApiEngine ?? "google",+ searXngUrl: config.searXngUrl ?? providerConfigs.searxng?.searXngUrl ?? "",+ searXngCategories: config.searXngCategories ?? providerConfigs.searxng?.searXngCategories ?? ["general"],providerConfigs,}}@@ -44,6 +80,8 @@ export function resolveSearchConfig(config: SearchApiConfig): SearchApiConfig {provider: activeProvider,apiKey: activeOverride?.apiKey ?? config.apiKey ?? "",serpApiEngine: activeOverride?.serpApiEngine ?? config.serpApiEngine ?? "google",+ searXngUrl: activeOverride?.searXngUrl ?? config.searXngUrl ?? "",+ searXngCategories: activeOverride?.searXngCategories ?? config.searXngCategories ?? ["general"],providerConfigs,}}@@ -54,20 +92,107 @@ export async function webSearch(maxResults: number = 10,): Promise<WebSearchResult[]> {const resolved = resolveSearchConfig(config)- if (resolved.provider === "none" || !resolved.apiKey) {+ if (resolved.provider === "none") {+ throw new Error("Web search not configured. Select a search provider in Settings.")+ }+ if ((resolved.provider === "tavily" || resolved.provider === "serpapi") && !resolved.apiKey) {throw new Error("Web search not configured. Add a Tavily or SerpApi API key in Settings.")}+ if (resolved.provider === "searxng" && !resolved.searXngUrl?.trim()) {+ throw new Error("Web search not configured. Add a SearXNG instance URL in Settings.")+ }switch (resolved.provider) {case "tavily":return tavilySearch(query, resolved.apiKey, maxResults)case "serpapi":return serpApiSearch(query, resolved.apiKey, maxResults, resolved.serpApiEngine ?? "google")+ case "searxng":+ return searXngSearch(query, resolved.searXngUrl ?? "", maxResults, resolved.searXngCategories ?? ["general"])default:throw new Error(`Unknown search provider: ${resolved.provider}`)}}+function searXngSearchUrl(instanceUrl: string): URL {+ const trimmed = instanceUrl.trim()+ const withProtocol = /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`+ const url = new URL(withProtocol)+ const path = url.pathname.replace(/\/+$/, "")+ url.pathname = path.endsWith("/search") || path === "/search"+ ? path+ : `${path}/search`+ url.search = ""+ url.hash = ""+ return url+}++async function searXngSearch(+ query: string,+ instanceUrl: string,+ maxResults: number,+ categories: SearXngCategory[],+): Promise<WebSearchResult[]> {+ let endpoint: URL+ try {+ endpoint = searXngSearchUrl(instanceUrl)+ } catch {+ throw new Error("Invalid SearXNG instance URL. Use a valid http(s) URL, for example https://search.example.com.")+ }++ endpoint.searchParams.set("q", query)+ endpoint.searchParams.set("format", "json")+ endpoint.searchParams.set("categories", (categories.length > 0 ? categories : ["general"]).join(","))++ const httpFetch = await getHttpFetch()+ let response: Response+ try {+ response = await httpFetch(endpoint.toString(), {+ method: "GET",+ headers: { Accept: "application/json" },+ })+ } catch (err) {+ if (isFetchNetworkError(err)) {+ throw new Error(+ "Network error reaching the SearXNG instance. Check the instance URL and whether JSON search is enabled.",+ )+ }+ throw err+ }++ if (!response.ok) {+ const errorText = await response.text().catch(() => "Unknown error")+ throw new Error(`SearXNG search failed (${response.status}): ${errorText}`)+ }++ const data = await response.json()+ return normalizeSearXngResults(data, maxResults)+}++function normalizeSearXngResults(data: { results?: unknown[] }, maxResults: number): WebSearchResult[] {+ return (data.results ?? [])+ .slice(0, maxResults)+ .map((item) => normalizeSearXngResult(item))+ .filter((item) => item.url.length > 0)+}++function normalizeSearXngResult(item: unknown): WebSearchResult {+ const r = item as {+ title?: string+ url?: string+ content?: string+ engine?: string+ category?: string+ }+ const url = r.url ?? ""+ return {+ title: r.title ?? "Untitled",+ url,+ snippet: r.content ?? "",+ source: hostnameFromUrl(url) || r.engine || r.category || "",+ }+}+function hostnameFromUrl(url: string): string {try {return new URL(url).hostname.replace("www.", "")diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.tsindex ed82cd6a..ec445ac5 100644--- a/src/stores/wiki-store.ts+++ b/src/stores/wiki-store.ts@@ -26,7 +26,7 @@ interface LlmConfig {reasoning?: ReasoningConfig}-export type SearchProvider = "tavily" | "serpapi" | "none"+export type SearchProvider = "tavily" | "serpapi" | "searxng" | "none"export type SerpApiEngine =| "google"| "google_news"@@ -38,10 +38,24 @@ export type SerpApiEngine =| "google_videos"| "youtube"| string+export type SearXngCategory =+ | "general"+ | "news"+ | "science"+ | "it"+ | "images"+ | "videos"+ | "files"+ | "map"+ | "music"+ | "social media"+ | stringexport interface SearchProviderOverride {apiKey?: stringserpApiEngine?: SerpApiEngine+ searXngUrl?: string+ searXngCategories?: SearXngCategory[]}export type SearchProviderConfigs = Partial<Record<Exclude<SearchProvider, "none">, SearchProviderOverride>>@@ -50,6 +64,8 @@ interface SearchApiConfig {provider: SearchProviderapiKey: stringserpApiEngine?: SerpApiEngine+ searXngUrl?: string+ searXngCategories?: SearXngCategory[]providerConfigs?: SearchProviderConfigs}@@ -262,6 +278,8 @@ export const useWikiStore = create<WikiState>((set) => ({provider: "none",apiKey: "",serpApiEngine: "google",+ searXngUrl: "",+ searXngCategories: ["general"],providerConfigs: {},},