Skip to content

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

feat: add unified streaming LLM client for all providers

nash_su40f686b01470
diff --git a/src/lib/llm-client.ts b/src/lib/llm-client.ts
new file mode 100644
index 00000000..8c4786ff
--- /dev/null
+++ b/src/lib/llm-client.ts
@@ -0,0 +1,100 @@
+import type { LlmConfig } from "@/stores/wiki-store"
+import { getProviderConfig } from "./llm-providers"
+
+export type { ChatMessage } from "./llm-providers"
+
+export interface StreamCallbacks {
+ onToken: (token: string) => void
+ onDone: () => void
+ onError: (error: Error) => void
+}
+
+const DECODER = new TextDecoder()
+
+function parseLines(chunk: Uint8Array, buffer: string): [string[], string] {
+ const text = buffer + DECODER.decode(chunk, { stream: true })
+ const lines = text.split("\n")
+ const remaining = lines.pop() ?? ""
+ return [lines, remaining]
+}
+
+export async function streamChat(
+ config: LlmConfig,
+ messages: import("./llm-providers").ChatMessage[],
+ callbacks: StreamCallbacks,
+ signal?: AbortSignal,
+): Promise<void> {
+ const { onToken, onDone, onError } = callbacks
+ const providerConfig = getProviderConfig(config)
+
+ let response: Response
+ try {
+ response = await fetch(providerConfig.url, {
+ method: "POST",
+ headers: providerConfig.headers,
+ body: JSON.stringify(providerConfig.buildBody(messages)),
+ signal,
+ })
+ } catch (err) {
+ if (err instanceof Error && err.name === "AbortError") {
+ onDone()
+ return
+ }
+ onError(err instanceof Error ? err : new Error(String(err)))
+ return
+ }
+
+ if (!response.ok) {
+ let errorDetail = `HTTP ${response.status}: ${response.statusText}`
+ try {
+ const body = await response.text()
+ if (body) errorDetail += ` — ${body}`
+ } catch {
+ // ignore body read failure
+ }
+ onError(new Error(errorDetail))
+ return
+ }
+
+ if (!response.body) {
+ onError(new Error("Response body is null"))
+ return
+ }
+
+ const reader = response.body.getReader()
+ let lineBuffer = ""
+
+ try {
+ while (true) {
+ const { done, value } = await reader.read()
+
+ if (done) {
+ if (lineBuffer.trim()) {
+ const token = providerConfig.parseStream(lineBuffer.trim())
+ if (token !== null) onToken(token)
+ }
+ break
+ }
+
+ const [lines, remaining] = parseLines(value, lineBuffer)
+ lineBuffer = remaining
+
+ for (const line of lines) {
+ const trimmed = line.trim()
+ if (!trimmed) continue
+ const token = providerConfig.parseStream(trimmed)
+ if (token !== null) onToken(token)
+ }
+ }
+
+ onDone()
+ } catch (err) {
+ if (err instanceof Error && err.name === "AbortError") {
+ onDone()
+ return
+ }
+ onError(err instanceof Error ? err : new Error(String(err)))
+ } finally {
+ reader.releaseLock()
+ }
+}
diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.ts
new file mode 100644
index 00000000..c35e7e81
--- /dev/null
+++ b/src/lib/llm-providers.ts
@@ -0,0 +1,181 @@
+import type { LlmConfig } from "@/stores/wiki-store"
+
+export interface ChatMessage {
+ role: "system" | "user" | "assistant"
+ content: string
+}
+
+interface ProviderConfig {
+ url: string
+ headers: Record<string, string>
+ buildBody: (messages: ChatMessage[]) => unknown
+ parseStream: (line: string) => string | null
+}
+
+const JSON_CONTENT_TYPE = "application/json"
+
+function parseOpenAiLine(line: string): string | null {
+ if (!line.startsWith("data: ")) return null
+ const data = line.slice(6).trim()
+ if (data === "[DONE]") return null
+ try {
+ const parsed = JSON.parse(data) as {
+ choices: Array<{ delta: { content?: string } }>
+ }
+ return parsed.choices?.[0]?.delta?.content ?? null
+ } catch {
+ return null
+ }
+}
+
+function parseAnthropicLine(line: string): string | null {
+ if (!line.startsWith("data: ")) return null
+ const data = line.slice(6).trim()
+ try {
+ const parsed = JSON.parse(data) as {
+ type: string
+ delta?: { type: string; text?: string }
+ }
+ if (
+ parsed.type === "content_block_delta" &&
+ parsed.delta?.type === "text_delta"
+ ) {
+ return parsed.delta.text ?? null
+ }
+ return null
+ } catch {
+ return null
+ }
+}
+
+function parseGoogleLine(line: string): string | null {
+ if (!line.startsWith("data: ")) return null
+ const data = line.slice(6).trim()
+ try {
+ const parsed = JSON.parse(data) as {
+ candidates: Array<{
+ content: { parts: Array<{ text?: string }> }
+ }>
+ }
+ return parsed.candidates?.[0]?.content?.parts?.[0]?.text ?? null
+ } catch {
+ return null
+ }
+}
+
+function buildOpenAiBody(messages: ChatMessage[]): unknown {
+ return { messages, stream: true }
+}
+
+function buildAnthropicBody(messages: ChatMessage[]): unknown {
+ const systemMessages = messages.filter((m) => m.role === "system")
+ const conversationMessages = messages.filter((m) => m.role !== "system")
+ const system = systemMessages.map((m) => m.content).join("\n") || undefined
+
+ return {
+ messages: conversationMessages,
+ ...(system !== undefined ? { system } : {}),
+ stream: true,
+ max_tokens: 4096,
+ }
+}
+
+function buildGoogleBody(messages: ChatMessage[]): unknown {
+ const systemMessages = messages.filter((m) => m.role === "system")
+ const conversationMessages = messages.filter((m) => m.role !== "system")
+
+ const contents = conversationMessages.map((m) => ({
+ role: m.role === "assistant" ? "model" : "user",
+ parts: [{ text: m.content }],
+ }))
+
+ const systemInstruction =
+ systemMessages.length > 0
+ ? {
+ parts: systemMessages.map((m) => ({ text: m.content })),
+ }
+ : undefined
+
+ return {
+ contents,
+ ...(systemInstruction !== undefined ? { systemInstruction } : {}),
+ }
+}
+
+export function getProviderConfig(config: LlmConfig): ProviderConfig {
+ const { provider, apiKey, model, ollamaUrl, customEndpoint } = config
+
+ switch (provider) {
+ case "openai":
+ return {
+ url: "https://api.openai.com/v1/chat/completions",
+ headers: {
+ "Content-Type": JSON_CONTENT_TYPE,
+ Authorization: `Bearer ${apiKey}`,
+ },
+ buildBody: (messages) => ({
+ ...buildOpenAiBody(messages),
+ model,
+ }),
+ parseStream: parseOpenAiLine,
+ }
+
+ case "anthropic":
+ return {
+ url: "https://api.anthropic.com/v1/messages",
+ headers: {
+ "Content-Type": JSON_CONTENT_TYPE,
+ "x-api-key": apiKey,
+ "anthropic-version": "2023-06-01",
+ "anthropic-dangerous-direct-browser-access": "true",
+ },
+ buildBody: (messages) => ({
+ ...buildAnthropicBody(messages),
+ model,
+ }),
+ parseStream: parseAnthropicLine,
+ }
+
+ case "google":
+ return {
+ url: `https://generativelanguage.googleapis.com/v1beta/models/${model}:streamGenerateContent?alt=sse&key=${apiKey}`,
+ headers: {
+ "Content-Type": JSON_CONTENT_TYPE,
+ },
+ buildBody: buildGoogleBody,
+ parseStream: parseGoogleLine,
+ }
+
+ case "ollama":
+ return {
+ url: `${ollamaUrl}/v1/chat/completions`,
+ headers: {
+ "Content-Type": JSON_CONTENT_TYPE,
+ },
+ buildBody: (messages) => ({
+ ...buildOpenAiBody(messages),
+ model,
+ }),
+ parseStream: parseOpenAiLine,
+ }
+
+ case "custom":
+ return {
+ url: `${customEndpoint}/chat/completions`,
+ headers: {
+ "Content-Type": JSON_CONTENT_TYPE,
+ ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
+ },
+ buildBody: (messages) => ({
+ ...buildOpenAiBody(messages),
+ model,
+ }),
+ parseStream: parseOpenAiLine,
+ }
+
+ default: {
+ const exhaustive: never = provider
+ throw new Error(`Unknown provider: ${String(exhaustive)}`)
+ }
+ }
+}
diff --git a/src/stores/wiki-store.ts b/src/stores/wiki-store.ts
index d17e4cff..0453c129 100644
--- a/src/stores/wiki-store.ts
+++ b/src/stores/wiki-store.ts
@@ -51,4 +51,4 @@ export const useWikiStore = create<WikiState>((set) => ({
setLlmConfig: (llmConfig) => set({ llmConfig }),
}))
-export type { WikiState }
+export type { WikiState, LlmConfig }