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
refactor: share tauri-fetch across llm/embedding/web-search paths
nash_su766bbbc40554
diff --git a/src/lib/embedding.ts b/src/lib/embedding.tsindex 98329d39..96e88edd 100644--- a/src/lib/embedding.ts+++ b/src/lib/embedding.ts@@ -3,9 +3,22 @@ import { invoke } from "@tauri-apps/api/core"import type { EmbeddingConfig } from "@/stores/wiki-store"import type { FileNode } from "@/types/wiki"import { normalizePath } from "@/lib/path-utils"+import { getHttpFetch, isFetchNetworkError } from "@/lib/tauri-fetch"// ── Embedding API ─────────────────────────────────────────────────────────+/**+ * Track the most recent embedding failure so the UI / callers can+ * surface *why* embedding isn't working instead of silently falling+ * back to BM25-only retrieval. Set on every failure, cleared on every+ * success. Read-only from outside via `getLastEmbeddingError`.+ */+let lastEmbeddingError: string | null = null++export function getLastEmbeddingError(): string | null {+ return lastEmbeddingError+}+async function fetchEmbedding(text: string,embeddingConfig: EmbeddingConfig,@@ -18,7 +31,14 @@ async function fetchEmbedding(}try {- const resp = await fetch(embeddingConfig.endpoint, {+ // Route through the Tauri HTTP plugin so user-configured embedding+ // endpoints — local LM Studio / llama.cpp with `--embedding`, remote+ // proxies, enterprise gateways — aren't blocked by the same CORS+ // preflight issues the LLM path hit before we migrated it. See+ // `src/lib/tauri-fetch.ts` for why the plugin beats browser fetch+ // for third-party URLs.+ const httpFetch = await getHttpFetch()+ const resp = await httpFetch(embeddingConfig.endpoint, {method: "POST",headers,body: JSON.stringify({@@ -27,12 +47,35 @@ async function fetchEmbedding(}),})if (!resp.ok) {- console.warn(`[Embedding] API error: ${resp.status} ${resp.statusText}`)+ let detail = `${resp.status} ${resp.statusText}`+ try {+ const body = await resp.text()+ if (body) detail += ` — ${body.slice(0, 200)}`+ } catch {+ // ignore body read errors+ }+ lastEmbeddingError = `API ${detail} at ${embeddingConfig.endpoint}`+ console.warn(`[Embedding] ${lastEmbeddingError}`)return null}const data = await resp.json()- return data?.data?.[0]?.embedding ?? null- } catch {+ const embedding = data?.data?.[0]?.embedding ?? null+ if (embedding) {+ lastEmbeddingError = null+ } else {+ lastEmbeddingError = `Response did not contain data[0].embedding (got ${JSON.stringify(data).slice(0, 200)})`+ console.warn(`[Embedding] ${lastEmbeddingError}`)+ }+ return embedding+ } catch (err) {+ // Translate cross-webview fetch failures into a single actionable+ // message that names the URL, mirroring the LLM-client treatment.+ if (isFetchNetworkError(err)) {+ lastEmbeddingError = `Network error reaching ${embeddingConfig.endpoint}. Check endpoint URL, API key, and connectivity.`+ } else {+ lastEmbeddingError = err instanceof Error ? err.message : String(err)+ }+ console.warn(`[Embedding] ${lastEmbeddingError}`)return null}}diff --git a/src/lib/llm-client.ts b/src/lib/llm-client.tsindex c77f1797..9e160c62 100644--- a/src/lib/llm-client.ts+++ b/src/lib/llm-client.ts@@ -1,7 +1,9 @@import type { LlmConfig } from "@/stores/wiki-store"import { getProviderConfig, type RequestOverrides } from "./llm-providers"+import { getHttpFetch, isFetchNetworkError } from "./tauri-fetch"export type { ChatMessage, RequestOverrides } from "./llm-providers"+export { isFetchNetworkError } from "./tauri-fetch"export interface StreamCallbacks {onToken: (token: string) => void@@ -11,63 +13,6 @@ export interface StreamCallbacks {const DECODER = new TextDecoder()-/**- * All LLM HTTP traffic goes through Tauri's Rust-backed HTTP plugin- * instead of the webview's native fetch. This bypasses CORS entirely —- * the request leaves the app from Rust, never the browser engine — which- * is the only way some third-party LLM endpoints work at all:- *- * - MiniMax (CORS allow-headers omits `x-api-key`; we worked around- * this by using Bearer, but the plugin makes it robust)- * - Volcengine Ark `/api/coding/v3` (allow-headers omits `authorization`- * entirely — there is NO header swap that fixes it in the webview)- * - Any other enterprise / on-prem gateway that doesn't expect browser- * origins (a common class of bug across domestic Chinese clouds)- *- * The plugin's `fetch` API mirrors the web Fetch API shape so the rest- * of this file looks like normal fetch code. Import is lazy + cached so- * unit tests (vitest in node) can import this module for the helpers- * below without the plugin's browser-only globals blowing up at load.- */-let pluginFetchPromise: Promise<typeof globalThis.fetch> | null = null-function getHttpFetch(): Promise<typeof globalThis.fetch> {- if (!pluginFetchPromise) {- pluginFetchPromise = import("@tauri-apps/plugin-http")- .then((m) => m.fetch as unknown as typeof globalThis.fetch)- // In a non-Tauri context (vitest / node / storybook) the plugin's- // global init fails; fall back to the browser's own fetch so- // importing this module for helper functions still works.- .catch(() => globalThis.fetch)- }- return pluginFetchPromise-}--/**- * Detect fetch-level network failures across Tauri's different webview- * backends. Each platform phrases the same failure class differently:- *- * macOS / iOS (WebKit): Error, message === "Load failed"- * Windows (Edge WebView2): TypeError, message === "Failed to fetch"- * Linux (WebKitGTK): Error, message === "Load failed"- *- * They all collapse DNS / TLS / connection-refused / CORS-preflight into- * a single opaque error with no structured detail. The only reliable- * cross-platform signal is "it's not an AbortError and it's one of these- * generic network error shapes", which this helper centralizes.- */-export function isFetchNetworkError(err: unknown): boolean {- if (!(err instanceof Error)) return false- if (err.name === "AbortError") return false- // Chromium / Edge WebView2- if (err.name === "TypeError") return true- // WebKit (macOS / Linux GTK)- if (err.message === "Load failed") return true- // Chromium mid-stream drop- if (err.message === "Failed to fetch") return true- if (err.message.includes("network error")) return true- return false-}-function parseLines(chunk: Uint8Array, buffer: string): [string[], string] {const text = buffer + DECODER.decode(chunk, { stream: true })const lines = text.split("\n")diff --git a/src/lib/tauri-fetch.ts b/src/lib/tauri-fetch.tsnew file mode 100644index 00000000..a9313572--- /dev/null+++ b/src/lib/tauri-fetch.ts@@ -0,0 +1,65 @@+/**+ * Shared HTTP helpers routed through Tauri's Rust-backed plugin so+ * third-party endpoints that don't set browser-friendly CORS headers+ * still work. Every part of the app that hits a user-configured URL+ * (LLM chat, embedding, web search, anything new) should import from+ * here rather than call `fetch` directly.+ *+ * Why it matters:+ * - MiniMax's /anthropic endpoint: CORS allow-headers omits x-api-key+ * - Volcengine Ark /api/coding/v3: CORS omits Authorization entirely+ * - Any enterprise / on-prem gateway that doesn't anticipate browser+ * origins — a common shape across domestic Chinese clouds+ *+ * In unit tests (vitest / node), the plugin's browser-only globals+ * aren't available; `getHttpFetch` lazily imports and falls back to+ * `globalThis.fetch` so helper functions in this file can be imported+ * from any environment without crashing at module load.+ */++let pluginFetchPromise: Promise<typeof globalThis.fetch> | null = null++/**+ * Returns a fetch function that routes through Tauri's HTTP plugin in+ * production, falling back to the platform's native fetch in non-Tauri+ * environments (tests / SSR / storybook). Call this once per request:+ *+ * const httpFetch = await getHttpFetch()+ * const response = await httpFetch(url, opts)+ *+ * The promise is cached, so repeated calls don't re-import the plugin.+ */+export function getHttpFetch(): Promise<typeof globalThis.fetch> {+ if (!pluginFetchPromise) {+ pluginFetchPromise = import("@tauri-apps/plugin-http")+ .then((m) => m.fetch as unknown as typeof globalThis.fetch)+ .catch(() => globalThis.fetch)+ }+ return pluginFetchPromise+}++/**+ * Detect fetch-level network failures across Tauri's different webview+ * backends. Each platform phrases the same failure class differently:+ *+ * macOS / iOS (WebKit): Error, message === "Load failed"+ * Windows (Edge WebView2): TypeError, message === "Failed to fetch"+ * Linux (WebKitGTK): Error, message === "Load failed"+ *+ * They all collapse DNS / TLS / connection-refused / CORS-preflight+ * into a single opaque error with no structured detail. The only+ * reliable cross-platform signal is "not an AbortError AND one of+ * these generic network error shapes", which this helper centralizes.+ */+export function isFetchNetworkError(err: unknown): boolean {+ if (!(err instanceof Error)) return false+ if (err.name === "AbortError") return false+ // Chromium / Edge WebView2+ if (err.name === "TypeError") return true+ // WebKit (macOS / Linux GTK)+ if (err.message === "Load failed") return true+ // Chromium mid-stream drop+ if (err.message === "Failed to fetch") return true+ if (err.message.includes("network error")) return true+ return false+}diff --git a/src/lib/web-search.ts b/src/lib/web-search.tsindex 46d99a43..4491aa06 100644--- a/src/lib/web-search.ts+++ b/src/lib/web-search.ts@@ -1,4 +1,5 @@import type { SearchApiConfig } from "@/stores/wiki-store"+import { getHttpFetch, isFetchNetworkError } from "@/lib/tauri-fetch"export interface WebSearchResult {title: string@@ -29,17 +30,31 @@ async function tavilySearch(apiKey: string,maxResults: number,): Promise<WebSearchResult[]> {- const response = await fetch("https://api.tavily.com/search", {- method: "POST",- headers: { "Content-Type": "application/json" },- body: JSON.stringify({- api_key: apiKey,- query,- max_results: maxResults,- search_depth: "advanced",- include_answer: false,- }),- })+ // Route through the Tauri HTTP plugin so future non-Tavily search+ // providers (Serper, Exa, Brave, Google CSE, ...) with less friendly+ // CORS don't each need their own workaround. See tauri-fetch.ts.+ const httpFetch = await getHttpFetch()+ let response: Response+ try {+ response = await httpFetch("https://api.tavily.com/search", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({+ api_key: apiKey,+ query,+ max_results: maxResults,+ search_depth: "advanced",+ include_answer: false,+ }),+ })+ } catch (err) {+ if (isFetchNetworkError(err)) {+ throw new Error(+ "Network error reaching api.tavily.com. Check your connectivity and whether the Tavily API key is still valid.",+ )+ }+ throw err+ }if (!response.ok) {const errorText = await response.text().catch(() => "Unknown error")