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
fix: Gemini HTTP 400 — translate sampling overrides into generationConfig
nash_su19af20c6c64f
diff --git a/src/lib/__tests__/llm-providers.test.ts b/src/lib/__tests__/llm-providers.test.tsindex c2aa0a9c..9e81bd29 100644--- a/src/lib/__tests__/llm-providers.test.ts+++ b/src/lib/__tests__/llm-providers.test.ts@@ -219,3 +219,87 @@ describe("Google provider URL — model path encoding", () => {expect(cfg.url).toContain("google%2Fgemini-3-pro-preview:streamGenerateContent")})})++describe("Sampling override translation across wires", () => {+ const baseMessages = [{ role: "user" as const, content: "hi" }]++ it("Gemini nests overrides under generationConfig with Gemini naming", () => {+ // Regression for a user-reported HTTP 400 —+ // "Unknown name 'temperature': Cannot find field."+ // Gemini rejects sampling knobs at the top level and requires the+ // renamed keys (top_p → topP, max_tokens → maxOutputTokens).+ const cfg = getProviderConfig({+ provider: "google",+ apiKey: "k",+ model: "gemini-2.5-flash",+ ollamaUrl: "",+ customEndpoint: "",+ maxContextSize: 128000,+ })+ const body = cfg.buildBody(baseMessages, {+ temperature: 0.1,+ top_p: 0.9,+ top_k: 40,+ max_tokens: 500,+ stop: ["###"],+ }) as Record<string, unknown>++ expect(body.temperature).toBeUndefined()+ expect(body.top_p).toBeUndefined()+ expect(body.max_tokens).toBeUndefined()+ const gc = body.generationConfig as Record<string, unknown>+ expect(gc.temperature).toBe(0.1)+ expect(gc.topP).toBe(0.9)+ expect(gc.topK).toBe(40)+ expect(gc.maxOutputTokens).toBe(500)+ expect(gc.stopSequences).toEqual(["###"])+ })++ it("Gemini omits generationConfig entirely when no overrides passed", () => {+ const cfg = getProviderConfig({+ provider: "google",+ apiKey: "k",+ model: "gemini-2.5-flash",+ ollamaUrl: "",+ customEndpoint: "",+ maxContextSize: 128000,+ })+ const body = cfg.buildBody(baseMessages) as Record<string, unknown>+ expect(body.generationConfig).toBeUndefined()+ })++ it("OpenAI wires put overrides at the top level", () => {+ const cfg = getProviderConfig({+ provider: "openai",+ apiKey: "k",+ model: "gpt-4o",+ ollamaUrl: "",+ customEndpoint: "",+ maxContextSize: 128000,+ })+ const body = cfg.buildBody(baseMessages, { temperature: 0.1, max_tokens: 500 }) as Record<string, unknown>+ expect(body.temperature).toBe(0.1)+ expect(body.max_tokens).toBe(500)+ })++ it("Anthropic maps stop → stop_sequences and respects max_tokens override", () => {+ const cfg = getProviderConfig({+ provider: "anthropic",+ apiKey: "k",+ model: "claude-sonnet-4-5-20250929",+ ollamaUrl: "",+ customEndpoint: "",+ maxContextSize: 200000,+ })+ const body = cfg.buildBody(baseMessages, {+ temperature: 0.1,+ stop: "END",+ max_tokens: 8192,+ }) as Record<string, unknown>+ expect(body.temperature).toBe(0.1)+ // Anthropic's wire field is `stop_sequences` (array), not `stop`.+ expect(body.stop_sequences).toEqual(["END"])+ expect(body.stop).toBeUndefined()+ expect(body.max_tokens).toBe(8192)+ })+})diff --git a/src/lib/llm-client.ts b/src/lib/llm-client.tsindex 192ae9f8..c77f1797 100644--- a/src/lib/llm-client.ts+++ b/src/lib/llm-client.ts@@ -1,7 +1,7 @@import type { LlmConfig } from "@/stores/wiki-store"-import { getProviderConfig } from "./llm-providers"+import { getProviderConfig, type RequestOverrides } from "./llm-providers"-export type { ChatMessage } from "./llm-providers"+export type { ChatMessage, RequestOverrides } from "./llm-providers"export interface StreamCallbacks {onToken: (token: string) => void@@ -81,12 +81,14 @@ export async function streamChat(callbacks: StreamCallbacks,signal?: AbortSignal,/**- * Extra fields to merge into the request body. Use for provider-specific- * knobs like `temperature`, `top_p`, `max_tokens`. Callers that need- * strict format adherence (e.g. ingest stage 2 emitting FILE blocks)- * should pass `{ temperature: 0.1 }` to reduce sampling variance.+ * Wire-agnostic sampling knobs. The provider's buildBody() translates+ * these into its native schema — OpenAI-style wires accept them at+ * the top level ({temperature: 0.1}), Gemini nests them under+ * generationConfig with renamed keys ({generationConfig: {temperature: 0.1}}).+ * Previously we spread them onto the body here, which broke Gemini+ * with "Unknown name 'temperature': Cannot find field." HTTP 400.*/- requestOverrides?: Record<string, unknown>,+ requestOverrides?: RequestOverrides,): Promise<void> {const { onToken, onDone, onError } = callbacksconst providerConfig = getProviderConfig(config)@@ -120,8 +122,7 @@ export async function streamChat(let response: Responsetry {- const baseBody = providerConfig.buildBody(messages) as Record<string, unknown>- const body = requestOverrides ? { ...baseBody, ...requestOverrides } : baseBody+ const body = providerConfig.buildBody(messages, requestOverrides)const httpFetch = await getHttpFetch()response = await httpFetch(providerConfig.url, {method: "POST",diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.tsindex 56c8ccf8..4f2c492f 100644--- a/src/lib/llm-providers.ts+++ b/src/lib/llm-providers.ts@@ -5,10 +5,27 @@ export interface ChatMessage {content: string}+/**+ * Sampling knobs a caller can pass to `streamChat` without caring about+ * the underlying wire's naming. Each provider's `buildBody` is+ * responsible for translating these into its native schema — OpenAI-+ * style wires accept them at the top level, Gemini demands they live+ * under `generationConfig` with renamed keys (`top_p` → `topP`,+ * `max_tokens` → `maxOutputTokens`, etc.). Missing fields are left+ * unset; providers keep their existing defaults.+ */+export interface RequestOverrides {+ temperature?: number+ top_p?: number+ top_k?: number+ max_tokens?: number+ stop?: string | string[]+}+interface ProviderConfig {url: stringheaders: Record<string, string>- buildBody: (messages: ChatMessage[]) => unknown+ buildBody: (messages: ChatMessage[], overrides?: RequestOverrides) => unknownparseStream: (line: string) => string | null}@@ -77,20 +94,38 @@ export function parseGoogleLine(line: string): string | null {}}-function buildOpenAiBody(messages: ChatMessage[]): Record<string, unknown> {- return { messages, stream: true }+function buildOpenAiBody(+ messages: ChatMessage[],+ overrides?: RequestOverrides,+): Record<string, unknown> {+ // OpenAI (and every /v1/chat/completions clone — DeepSeek, Groq,+ // Ollama, Zhipu, Kimi, xAI, MiniMax OpenAI-compat, ...) accepts these+ // knobs at the top level using the names clients already send.+ return { messages, stream: true, ...(overrides ?? {}) }}-function buildAnthropicBody(messages: ChatMessage[]): Record<string, unknown> {+function buildAnthropicBody(+ messages: ChatMessage[],+ overrides?: RequestOverrides,+): Record<string, 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+ // Anthropic Messages uses top_p / top_k (Python-style snake_case), a+ // mandatory `max_tokens`, and `stop_sequences` instead of `stop`.+ // Overrides may still set max_tokens to stretch long outputs.return {messages: conversationMessages,...(system !== undefined ? { system } : {}),stream: true,- max_tokens: 4096,+ max_tokens: overrides?.max_tokens ?? 4096,+ ...(overrides?.temperature !== undefined ? { temperature: overrides.temperature } : {}),+ ...(overrides?.top_p !== undefined ? { top_p: overrides.top_p } : {}),+ ...(overrides?.top_k !== undefined ? { top_k: overrides.top_k } : {}),+ ...(overrides?.stop !== undefined+ ? { stop_sequences: Array.isArray(overrides.stop) ? overrides.stop : [overrides.stop] }+ : {}),}}@@ -152,7 +187,10 @@ function buildAnthropicHeaders(apiKey: string, url: string): Record<string, strireturn base}-function buildGoogleBody(messages: ChatMessage[]): Record<string, unknown> {+function buildGoogleBody(+ messages: ChatMessage[],+ overrides?: RequestOverrides,+): Record<string, unknown> {const systemMessages = messages.filter((m) => m.role === "system")const conversationMessages = messages.filter((m) => m.role !== "system")@@ -168,9 +206,28 @@ function buildGoogleBody(messages: ChatMessage[]): Record<string, unknown> {}: undefined+ // Gemini rejects sampling knobs at the top level (HTTP 400+ // "Unknown name 'temperature': Cannot find field.") — everything+ // must live under `generationConfig` with Gemini-specific naming:+ // top_p → topP+ // top_k → topK+ // max_tokens → maxOutputTokens+ // stop → stopSequences (array)+ // Build it only when the caller actually passed something, so an+ // unmodified request stays minimal and lets server defaults apply.+ const generationConfig: Record<string, unknown> = {}+ if (overrides?.temperature !== undefined) generationConfig.temperature = overrides.temperature+ if (overrides?.top_p !== undefined) generationConfig.topP = overrides.top_p+ if (overrides?.top_k !== undefined) generationConfig.topK = overrides.top_k+ if (overrides?.max_tokens !== undefined) generationConfig.maxOutputTokens = overrides.max_tokens+ if (overrides?.stop !== undefined) {+ generationConfig.stopSequences = Array.isArray(overrides.stop) ? overrides.stop : [overrides.stop]+ }+return {contents,...(systemInstruction !== undefined ? { systemInstruction } : {}),+ ...(Object.keys(generationConfig).length > 0 ? { generationConfig } : {}),}}@@ -185,8 +242,8 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {"Content-Type": JSON_CONTENT_TYPE,Authorization: `Bearer ${apiKey}`,},- buildBody: (messages) => ({- ...buildOpenAiBody(messages),+ buildBody: (messages, overrides) => ({+ ...buildOpenAiBody(messages, overrides),model,}),parseStream: parseOpenAiLine,@@ -197,8 +254,8 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {return {url,headers: buildAnthropicHeaders(apiKey, url),- buildBody: (messages) => ({- ...buildAnthropicBody(messages),+ buildBody: (messages, overrides) => ({+ ...buildAnthropicBody(messages, overrides),model,}),parseStream: parseAnthropicLine,@@ -238,9 +295,9 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {headers: {"Content-Type": JSON_CONTENT_TYPE,},- buildBody: (messages) => {+ buildBody: (messages, overrides) => {const body: Record<string, unknown> = {- ...buildOpenAiBody(messages),+ ...buildOpenAiBody(messages, overrides),model,}// Qwen3 thinking mode disable. Recognized by llama.cpp server@@ -267,8 +324,8 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {return {url,headers: buildAnthropicHeaders(apiKey, url),- buildBody: (messages) => ({- ...buildAnthropicBody(messages),+ buildBody: (messages, overrides) => ({+ ...buildAnthropicBody(messages, overrides),model,}),parseStream: parseAnthropicLine,@@ -286,8 +343,8 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {return {url,headers: buildAnthropicHeaders(apiKey, url),- buildBody: (messages) => ({- ...buildAnthropicBody(messages),+ buildBody: (messages, overrides) => ({+ ...buildAnthropicBody(messages, overrides),model,}),parseStream: parseAnthropicLine,@@ -307,8 +364,8 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {"Content-Type": JSON_CONTENT_TYPE,...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),},- buildBody: (messages) => ({- ...buildOpenAiBody(messages),+ buildBody: (messages, overrides) => ({+ ...buildOpenAiBody(messages, overrides),model,}),parseStream: parseOpenAiLine,