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: make reasoning controls provider aware
nash_su401bf26068a1
diff --git a/src-tauri/src/agent/provider.rs b/src-tauri/src/agent/provider.rsindex b40f9371..5c4e12ed 100644--- a/src-tauri/src/agent/provider.rs+++ b/src-tauri/src/agent/provider.rs@@ -39,6 +39,8 @@ pub struct LlmConfig {#[serde(default)]pub azure_api_version: Option<String>,#[serde(default)]+ pub azure_model_family: Option<String>,+ #[serde(default)]pub api_mode: Option<String>,#[serde(default)]pub reasoning: Option<LlmReasoningConfig>,@@ -311,7 +313,7 @@ impl LlmClient {if include_model {body["model"] = Value::String(self.config.model.clone());}- apply_openai_reasoning(&mut body, self.config.reasoning.as_ref());+ apply_openai_reasoning(&mut body, &self.config);body}@@ -399,7 +401,7 @@ impl LlmClient {if stream {body["stream"] = Value::Bool(true);}- apply_anthropic_reasoning(&mut body, self.config.reasoning.as_ref());+ apply_anthropic_reasoning(&mut body, &self.config);body}@@ -500,7 +502,7 @@ impl LlmClient {"maxOutputTokens": self.max_output_tokens()}});- apply_google_reasoning(&mut body, self.config.reasoning.as_ref());+ apply_google_reasoning(&mut body, &self.config);body}@@ -585,8 +587,11 @@ impl LlmClient {pub fn structured_task_config(&self, max_tokens: u32) -> Self {let mut config = self.config.clone();+ // Structured tasks need concise final content, but forcing "off" is+ // invalid for thinking-only models and generic custom gateways. Auto+ // omits provider-specific controls and is the portable safe default.config.reasoning = Some(LlmReasoningConfig {- mode: Some("off".to_string()),+ mode: Some("auto".to_string()),budget_tokens: None,});config.max_tokens = Some(max_tokens);@@ -909,10 +914,30 @@ fn requires_bearer_auth(url: &str) -> bool {lower.contains("minimax.io") || lower.contains("minimaxi.com")}-fn apply_openai_reasoning(body: &mut Value, reasoning: Option<&LlmReasoningConfig>) {- let Some(reasoning) = reasoning else {+fn apply_openai_reasoning(body: &mut Value, config: &LlmConfig) {+ let Some(reasoning) = config.reasoning.as_ref() else {return;};+ if config.provider == "ollama" {+ match reasoning.mode.as_deref() {+ Some("off") => body["reasoning_effort"] = Value::String("none".to_string()),+ Some("low" | "medium" | "high") => {+ body["reasoning_effort"] =+ Value::String(reasoning.mode.clone().unwrap_or_default());+ }+ Some("max") => body["reasoning_effort"] = Value::String("high".to_string()),+ _ => {}+ }+ return;+ }+ // Generic OpenAI-compatible gateways do not promise support for OpenAI's+ // private reasoning fields. The frontend follows the same omission rule.+ if config.provider != "openai" && config.provider != "azure" {+ return;+ }+ if !is_openai_reasoning_model(config) {+ return;+ }match reasoning.mode.as_deref() {Some("off") => {}Some("low" | "medium" | "high") => {@@ -922,34 +947,92 @@ fn apply_openai_reasoning(body: &mut Value, reasoning: Option<&LlmReasoningConfi}}-fn apply_anthropic_reasoning(body: &mut Value, reasoning: Option<&LlmReasoningConfig>) {- let Some(reasoning) = reasoning else {+fn apply_anthropic_reasoning(body: &mut Value, config: &LlmConfig) {+ let Some(reasoning) = config.reasoning.as_ref() else {return;};+ if config.provider != "anthropic" {+ return;+ }if reasoning.mode.as_deref() == Some("off") {return;}let Some(budget) = reasoning_budget(reasoning) else {return;};+ if is_claude_47_or_later(&config.model) {+ let effort = match reasoning.mode.as_deref() {+ Some("low") => "low",+ Some("medium") => "medium",+ Some("max") => "max",+ _ => "high",+ };+ body["thinking"] = json!({ "type": "adaptive" });+ body["output_config"] = json!({ "effort": effort });+ return;+ }body["thinking"] = json!({ "type": "enabled", "budget_tokens": budget });let min_tokens = budget.saturating_add(1024).max(DEFAULT_MAX_TOKENS);body["max_tokens"] = Value::from(min_tokens);}-fn apply_google_reasoning(body: &mut Value, reasoning: Option<&LlmReasoningConfig>) {- let Some(reasoning) = reasoning else {+fn apply_google_reasoning(body: &mut Value, config: &LlmConfig) {+ let Some(reasoning) = config.reasoning.as_ref() else {return;};if reasoning.mode.as_deref() == Some("off") {+ if is_gemini_thinking_required(&config.model) {+ return;+ }body["generationConfig"]["thinkingConfig"] = json!({ "thinkingBudget": 0 });return;}+ if is_gemini_3(&config.model) && reasoning_budget(reasoning).is_some() {+ let level = match reasoning.mode.as_deref() {+ Some("low") => "low",+ Some("medium") => "medium",+ _ => "high",+ };+ body["generationConfig"]["thinkingConfig"] = json!({ "thinkingLevel": level });+ return;+ }if let Some(budget) = reasoning_budget(reasoning) {body["generationConfig"]["thinkingConfig"] = json!({ "thinkingBudget": budget });}}+fn is_claude_47_or_later(model: &str) -> bool {+ let lower = model.to_ascii_lowercase();+ ["claude-opus-4-", "claude-sonnet-4-", "claude-haiku-4-"]+ .iter()+ .find_map(|prefix| lower.strip_prefix(prefix))+ .and_then(|tail| tail.split(['-', '_', '.']).next())+ .and_then(|version| version.parse::<u32>().ok())+ .is_some_and(|version| version >= 7)+}++fn is_openai_reasoning_model(config: &LlmConfig) -> bool {+ if config.provider == "azure" && config.azure_model_family.as_deref() == Some("gpt5") {+ return true;+ }+ let lower = config.model.to_ascii_lowercase();+ lower.starts_with("gpt-5")+ || lower+ .strip_prefix('o')+ .and_then(|tail| tail.chars().next())+ .is_some_and(|first| first.is_ascii_digit())+}++fn is_gemini_3(model: &str) -> bool {+ let lower = model.to_ascii_lowercase();+ lower.starts_with("gemini-3-") || lower.starts_with("gemini_3_")+}++fn is_gemini_thinking_required(model: &str) -> bool {+ let lower = model.to_ascii_lowercase();+ lower.starts_with("gemini-2.5-pro") || is_gemini_3(model)+}+fn reasoning_budget(reasoning: &LlmReasoningConfig) -> Option<u32> {match reasoning.mode.as_deref() {Some("custom") => reasoning.budget_tokens,@@ -994,6 +1077,7 @@ mod tests {ollama_url: "http://localhost:11434/v1".to_string(),custom_endpoint: "https://example.com/v1".to_string(),azure_api_version: None,+ azure_model_family: None,api_mode: None,reasoning: None,max_tokens: None,@@ -1122,18 +1206,17 @@ mod tests {#[test]fn openai_reasoning_off_does_not_emit_null_field() {let mut body = json!({});- apply_openai_reasoning(- &mut body,- Some(&LlmReasoningConfig {- mode: Some("off".to_string()),- budget_tokens: None,- }),- );+ let mut cfg = config("openai");+ cfg.reasoning = Some(LlmReasoningConfig {+ mode: Some("off".to_string()),+ budget_tokens: None,+ });+ apply_openai_reasoning(&mut body, &cfg);assert!(body.get("reasoning_effort").is_none());}#[test]- fn structured_task_config_disables_reasoning_and_raises_output_budget() {+ fn structured_task_config_uses_portable_auto_reasoning_and_raises_output_budget() {let mut cfg = config("openai");cfg.reasoning = Some(LlmReasoningConfig {mode: Some("high".to_string()),@@ -1144,6 +1227,57 @@ mod tests {assert_eq!(body.get("max_tokens").and_then(Value::as_u64), Some(16_384));assert!(body.get("reasoning_effort").is_none());+ assert_eq!(+ client+ .config+ .reasoning+ .as_ref()+ .and_then(|value| value.mode.as_deref()),+ Some("auto")+ );+ }++ #[test]+ fn generic_custom_gateway_does_not_receive_openai_reasoning_fields() {+ let mut cfg = config("custom");+ cfg.model = "qwen3-thinking-only".to_string();+ cfg.reasoning = Some(LlmReasoningConfig {+ mode: Some("high".to_string()),+ budget_tokens: None,+ });+ let client = LlmClient::new(cfg).unwrap();+ let body = client.openai_like_body("system", "user", &[], true, false);+ assert!(body.get("reasoning_effort").is_none());+ }++ #[test]+ fn claude_47_uses_adaptive_thinking() {+ let mut cfg = config("anthropic");+ cfg.model = "claude-opus-4-7".to_string();+ cfg.reasoning = Some(LlmReasoningConfig {+ mode: Some("high".to_string()),+ budget_tokens: None,+ });+ let client = LlmClient::new(cfg).unwrap();+ let body = client.anthropic_like_body("system", "user", &[], false);+ assert_eq!(body.get("thinking"), Some(&json!({ "type": "adaptive" })));+ assert_eq!(+ body.get("output_config"),+ Some(&json!({ "effort": "high" }))+ );+ }++ #[test]+ fn gemini_25_pro_does_not_receive_unsupported_zero_thinking_budget() {+ let mut cfg = config("google");+ cfg.model = "gemini-2.5-pro".to_string();+ cfg.reasoning = Some(LlmReasoningConfig {+ mode: Some("off".to_string()),+ budget_tokens: None,+ });+ let client = LlmClient::new(cfg).unwrap();+ let body = client.google_body("system", "user", &[]);+ assert!(body["generationConfig"].get("thinkingConfig").is_none());}#[test]diff --git a/src-tauri/src/agent/runtime.rs b/src-tauri/src/agent/runtime.rsindex 0bcbc51f..bacba02a 100644--- a/src-tauri/src/agent/runtime.rs+++ b/src-tauri/src/agent/runtime.rs@@ -4171,6 +4171,7 @@ mod tests {ollama_url: String::new(),custom_endpoint: "http://127.0.0.1:11434/v1".to_string(),azure_api_version: None,+ azure_model_family: None,api_mode: None,reasoning: None,max_tokens: None,@@ -4197,6 +4198,7 @@ mod tests {ollama_url: String::new(),custom_endpoint: "http://127.0.0.1:11434/v1".to_string(),azure_api_version: None,+ azure_model_family: None,api_mode: None,reasoning: None,max_tokens: None,diff --git a/src/components/settings/sections/llm-provider-section.tsx b/src/components/settings/sections/llm-provider-section.tsxindex 092ff5bb..425fc964 100644--- a/src/components/settings/sections/llm-provider-section.tsx+++ b/src/components/settings/sections/llm-provider-section.tsx@@ -5,7 +5,7 @@ import { invoke } from "@tauri-apps/api/core"import { Input } from "@/components/ui/input"import { Button } from "@/components/ui/button"import { Label } from "@/components/ui/label"-import { useWikiStore, type ProviderOverride, type ReasoningConfig, type ReasoningMode } from "@/stores/wiki-store"+import { useWikiStore, type LlmConfig, type ProviderOverride, type ReasoningConfig, type ReasoningMode } from "@/stores/wiki-store"import { availableLlmPresets, findLlmPreset, type LlmPreset } from "../llm-presets"import { ContextSizeSelector } from "../context-size-selector"import { disabledLlmConfig, resolveConfig } from "../preset-resolver"@@ -14,6 +14,7 @@ import { AZURE_OPENAI_API_VERSION } from "@/lib/azure-openai"import { testLlmConnection, testLlmFunction, type ProviderTestResult } from "@/lib/connection-tests"import { projectLlmProfile, resolveProjectLlmConfig } from "@/lib/llm-task-routing"import { saveProjectLlmOverride } from "@/lib/project-store"+import { normalizeReasoningForProvider, resolveReasoningCapabilities } from "@/lib/reasoning-capabilities"const HTTP_HEADER_NAME_RE = /^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/@@ -765,6 +766,7 @@ function PresetRow({<ReasoningControlsvalue={reasoning}+ config={resolvedConfig}onChange={(reasoning) => onChange({ reasoning })}/>@@ -826,13 +828,18 @@ function PresetRow({function ReasoningControls({value,+ config,onChange,}: {value: ReasoningConfig+ config: LlmConfigonChange: (value: ReasoningConfig) => void}) {const { t } = useTranslation()- const modes: { value: ReasoningMode; label: string }[] = [+ const capabilities = resolveReasoningCapabilities(config)+ const supportedModes = capabilities.modes+ const normalizedValue = normalizeReasoningForProvider(config, value)+ const allModes: { value: ReasoningMode; label: string }[] = [{ value: "auto", label: t("settings.sections.llm.reasoning.auto") },{ value: "off", label: t("settings.sections.llm.reasoning.off") },{ value: "low", label: t("settings.sections.llm.reasoning.low") },@@ -841,13 +848,14 @@ function ReasoningControls({{ value: "max", label: t("settings.sections.llm.reasoning.max") },{ value: "custom", label: t("settings.sections.llm.reasoning.custom") },]+ const modes = allModes.filter((mode) => supportedModes.includes(mode.value))return (<div className="space-y-2"><Label>{t("settings.sections.llm.reasoning.title")}</Label><div className="flex flex-wrap gap-1.5">{modes.map((m) => {- const active = value.mode === m.value+ const active = normalizedValue.mode === m.valuereturn (<buttonkey={m.value}@@ -864,11 +872,13 @@ function ReasoningControls({)})}</div>- {value.mode === "custom" && (+ {normalizedValue.mode === "custom" && (<div className="flex items-center gap-2"><Inputtype="number"- min={0}+ min={capabilities.customBudgetRange.min}+ max={capabilities.customBudgetRange.max}+ step={1}className="w-28"value={value.budgetTokens ?? ""}onChange={(e) => {@@ -876,7 +886,12 @@ function ReasoningControls({const n = Number(raw)onChange({...value,- budgetTokens: raw === "" || !Number.isFinite(n) ? undefined : Math.max(0, n),+ budgetTokens: raw === "" || !Number.isFinite(n)+ ? undefined+ : Math.max(+ capabilities.customBudgetRange.min,+ Math.min(capabilities.customBudgetRange.max, Math.floor(n)),+ ),})}}placeholder="1024"diff --git a/src/lib/connection-tests.test.ts b/src/lib/connection-tests.test.tsindex 9cf50568..b3075599 100644--- a/src/lib/connection-tests.test.ts+++ b/src/lib/connection-tests.test.ts@@ -93,7 +93,7 @@ describe("provider connection tests", () => {expect.any(Array),expect.any(Object),undefined,- { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "off" } },+ { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "auto" } },)})@@ -146,7 +146,7 @@ describe("provider connection tests", () => {expect.any(Array),expect.any(Object),undefined,- { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "off" } },+ { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "auto" } },)expect(cfg.localCliIsolation).toBe(initialIsolation)},@@ -166,7 +166,7 @@ describe("provider connection tests", () => {expect.any(Array),expect.any(Object),undefined,- { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "off" } },+ { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "auto" } },)})})diff --git a/src/lib/connection-tests.ts b/src/lib/connection-tests.tsindex d7e6a086..f2593afd 100644--- a/src/lib/connection-tests.ts+++ b/src/lib/connection-tests.ts@@ -87,7 +87,7 @@ export async function testLlmConnection(cfg: LlmConfig): Promise<ProviderTestResonError: (err) => { errorMessage = err.message },},undefined,- { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "off" } },+ { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "auto" } },)if (errorMessage) return { ok: false, message: errorMessage }@@ -118,7 +118,7 @@ export async function testLlmFunction(cfg: LlmConfig): Promise<ProviderTestResulonError: (err) => { errorMessage = err.message },},undefined,- { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "off" } },+ { max_tokens: LLM_PROVIDER_TEST_MAX_TOKENS, reasoning: { mode: "auto" } },)if (errorMessage) return { ok: false, message: errorMessage }diff --git a/src/lib/llm-providers.test.ts b/src/lib/llm-providers.test.tsindex 299767f2..f1bb2085 100644--- a/src/lib/llm-providers.test.ts+++ b/src/lib/llm-providers.test.ts@@ -621,7 +621,7 @@ describe("reasoning controls", () => {expect(body.reasoning_effort).toBe("high")})- it("does not send an undocumented DeepSeek max_reasoning_tokens field", () => {+ it("omits unsupported custom budgets on DeepSeek V4", () => {const cfg = mkConfig({provider: "custom",model: "deepseek-v4-pro",@@ -633,7 +633,7 @@ describe("reasoning controls", () => {{ reasoning: { mode: "custom", budgetTokens: 2048 } },) as Record<string, unknown>- expect(body.thinking).toEqual({ type: "enabled" })+ expect(body.thinking).toBeUndefined()expect(body.max_reasoning_tokens).toBeUndefined()})@@ -744,7 +744,7 @@ describe("reasoning controls", () => {expect(body.messages).toEqual([{ role: "user", content: "Hi" }])})- it("disables Qwen3 thinking on OpenAI-compatible local endpoints", () => {+ it("does not infer Qwen private parameters on generic custom endpoints", () => {const cfg = mkConfig({provider: "custom",model: "Qwen3.5-122B",@@ -756,7 +756,7 @@ describe("reasoning controls", () => {{ reasoning: { mode: "off" } },) as Record<string, unknown>- expect(body.chat_template_kwargs).toEqual({ enable_thinking: false })+ expect(body.chat_template_kwargs).toBeUndefined()})it("strips temperature for Kimi/Moonshot OpenAI-compatible endpoints", () => {@@ -807,14 +807,36 @@ describe("reasoning controls", () => {expect(body.temperature).toBeUndefined()})- it("maps Gemini reasoning off to thinkingBudget 0", () => {+ it("omits unsupported reasoning off for Gemini 2.5 Pro", () => {const cfg = mkConfig({ provider: "google", model: "gemini-2.5-pro" })const body = getProviderConfig(cfg).buildBody([{ role: "user", content: "hi" }],{ reasoning: { mode: "off" } },) as { generationConfig?: Record<string, unknown> }- expect(body.generationConfig?.thinkingConfig).toEqual({ thinkingBudget: 0 })+ expect(body.generationConfig?.thinkingConfig).toBeUndefined()+ })++ it("uses adaptive thinking and effort for Claude 4.7+", () => {+ const cfg = mkConfig({ provider: "anthropic", model: "claude-opus-4-7" })+ const body = getProviderConfig(cfg).buildBody(+ [{ role: "user", content: "hi" }],+ { reasoning: { mode: "high" }, temperature: 0.1 },+ ) as Record<string, unknown>++ expect(body.thinking).toEqual({ type: "adaptive" })+ expect(body.output_config).toEqual({ effort: "high" })+ expect(body.temperature).toBeUndefined()+ })++ it("uses thinkingLevel rather than token budgets for Gemini 3", () => {+ const cfg = mkConfig({ provider: "google", model: "gemini-3-pro-preview" })+ const body = getProviderConfig(cfg).buildBody(+ [{ role: "user", content: "hi" }],+ { reasoning: { mode: "medium" } },+ ) as { generationConfig?: Record<string, unknown> }++ expect(body.generationConfig?.thinkingConfig).toEqual({ thinkingLevel: "medium" })})it("maps Ollama reasoning off to reasoning_effort none (stops thinking-runaway empty content)", () => {diff --git a/src/lib/llm-providers.ts b/src/lib/llm-providers.tsindex 14f47e7d..ad780525 100644--- a/src/lib/llm-providers.ts+++ b/src/lib/llm-providers.ts@@ -5,6 +5,11 @@ import {buildAzureOpenAiUrl,isAzureOpenAiEndpoint,} from "@/lib/azure-openai"+import {+ isAdaptiveAnthropicModel,+ isGeminiThinkingLevelModel,+ normalizeReasoningForProvider,+} from "@/lib/reasoning-capabilities"/*** One piece of a multimodal message body. Text + image is the only@@ -336,30 +341,27 @@ function stripWireAgnosticOverrides(overrides?: RequestOverrides): Omit<RequestO}function effectiveReasoning(config: LlmConfig, overrides?: RequestOverrides): ReasoningConfig {- return overrides?.reasoning ?? config.reasoning ?? { mode: "auto" }+ return normalizeReasoningForProvider(+ config,+ overrides?.reasoning ?? config.reasoning ?? { mode: "auto" },+ )}function isDeepSeekEndpoint(config: LlmConfig): boolean {- return /deepseek/i.test(config.model) || /deepseek/i.test(config.customEndpoint)+ return /api\.deepseek\.(?:com|cn)(?:[:/]|$)/i.test(config.customEndpoint)}function supportsDeepSeekThinkingParam(config: LlmConfig): boolean {return /deepseek[-_]?v4/i.test(config.model)}-function isQwenThinkingModel(model: string): boolean {- return /qwen[-_]?3/i.test(model)-}-function isKimiEndpoint(config: LlmConfig): boolean {- return /(^|[/:.-])kimi([/:.-]|$)/i.test(config.model)- || /moonshot/i.test(config.model)- || /api\.moonshot\.(ai|cn)/i.test(config.customEndpoint)+ return /api\.moonshot\.(ai|cn)(?:[:/]|$)/i.test(config.customEndpoint)+ || /api\.kimi\.com\/coding(?:\/|$)/i.test(config.customEndpoint)}function isXiaomiMimoEndpoint(config: LlmConfig): boolean {- return /(^|[/:.-])mimo([/:.-]|$)/i.test(config.model)- || /\.?xiaomimimo\.com(?::|\/|$)/i.test(config.customEndpoint)+ return /\.?xiaomimimo\.com(?::|\/|$)/i.test(config.customEndpoint)}function isBigModelEndpoint(config: LlmConfig): boolean {@@ -507,10 +509,6 @@ function buildOpenAiCompatibleBody(return body}- if (reasoning.mode === "off" && isQwenThinkingModel(config.model)) {- body.chat_template_kwargs = { enable_thinking: false }- }-if (config.provider === "openai" && reasoning.mode !== "auto" && reasoning.mode !== "off") {if (reasoning.mode === "low" || reasoning.mode === "medium" || reasoning.mode === "high") {body.reasoning_effort = reasoning.mode@@ -618,6 +616,20 @@ function buildAnthropicBodyWithReasoning(const reasoning = effectiveReasoning(config, overrides)if (reasoning.mode === "auto" || reasoning.mode === "off") return body+ if (isAdaptiveAnthropicModel(config)) {+ body.thinking = { type: "adaptive" }+ const effort = reasoning.mode === "custom"+ ? "high"+ : reasoning.mode === "max"+ ? "max"+ : reasoning.mode+ body.output_config = { effort }+ delete body.temperature+ delete body.top_p+ delete body.top_k+ return body+ }+const budget =reasoning.mode === "custom" && reasoning.budgetTokens !== undefined? reasoning.budgetTokens@@ -807,6 +819,7 @@ function flattenGoogleSystemParts(content: string | ContentBlock[]): string {}function buildGoogleBody(+ config: LlmConfig,messages: ChatMessage[],overrides?: RequestOverrides,): Record<string, unknown> {@@ -846,18 +859,26 @@ function buildGoogleBody(if (overrides?.stop !== undefined) {generationConfig.stopSequences = Array.isArray(overrides.stop) ? overrides.stop : [overrides.stop]}- if (overrides?.reasoning?.mode === "off") {+ const reasoning = effectiveReasoning(config, overrides)+ if (reasoning.mode === "off") {generationConfig.thinkingConfig = { thinkingBudget: 0 }- } else if (overrides?.reasoning && overrides.reasoning.mode !== "auto") {- const budget =- overrides.reasoning.mode === "custom" && overrides.reasoning.budgetTokens !== undefined- ? overrides.reasoning.budgetTokens- : overrides.reasoning.mode === "low"- ? 1024- : overrides.reasoning.mode === "medium"- ? 4096- : 8192- generationConfig.thinkingConfig = { thinkingBudget: budget }+ } else if (reasoning.mode !== "auto") {+ if (isGeminiThinkingLevelModel(config)) {+ const thinkingLevel = reasoning.mode === "low" || reasoning.mode === "medium"+ ? reasoning.mode+ : "high"+ generationConfig.thinkingConfig = { thinkingLevel }+ } else {+ const budget =+ reasoning.mode === "custom" && reasoning.budgetTokens !== undefined+ ? reasoning.budgetTokens+ : reasoning.mode === "low"+ ? 1024+ : reasoning.mode === "medium"+ ? 4096+ : 8192+ generationConfig.thinkingConfig = { thinkingBudget: budget }+ }}return {@@ -939,7 +960,7 @@ export function getProviderConfig(config: LlmConfig): ProviderConfig {"Content-Type": JSON_CONTENT_TYPE,"x-goog-api-key": apiKey,}),- buildBody: (messages, overrides) => buildGoogleBody(messages, {+ buildBody: (messages, overrides) => buildGoogleBody(config, messages, {...(overrides ?? {}),reasoning: effectiveReasoning(config, overrides),}),diff --git a/src/lib/reasoning-capabilities.test.ts b/src/lib/reasoning-capabilities.test.tsnew file mode 100644index 00000000..793a1371--- /dev/null+++ b/src/lib/reasoning-capabilities.test.ts@@ -0,0 +1,48 @@+import { describe, expect, it } from "vitest"+import type { LlmConfig } from "@/stores/wiki-store"+import { normalizeReasoningForProvider, resolveReasoningCapabilities } from "./reasoning-capabilities"++function config(provider: LlmConfig["provider"], model: string): LlmConfig {+ return {+ provider,+ model,+ apiKey: "key",+ ollamaUrl: "http://localhost:11434",+ customEndpoint: "https://gateway.example/v1",+ maxContextSize: 128_000,+ }+}++describe("reasoning capabilities", () => {+ it("does not infer vendor-private controls from a custom gateway model name", () => {+ const cfg = config("custom", "Qwen3-thinking-only")+ expect(resolveReasoningCapabilities(cfg).modes).toEqual(["auto"])+ expect(normalizeReasoningForProvider(cfg, { mode: "off" })).toEqual({ mode: "auto" })+ })++ it("does not offer off for thinking-required Gemini and Claude models", () => {+ expect(resolveReasoningCapabilities(config("google", "gemini-2.5-pro")).modes)+ .not.toContain("off")+ expect(resolveReasoningCapabilities(config("anthropic", "claude-opus-4-7")).modes)+ .not.toContain("off")+ })++ it("limits OpenAI reasoning models to representable effort levels", () => {+ expect(resolveReasoningCapabilities(config("openai", "gpt-5.4")).modes)+ .toEqual(["auto", "low", "medium", "high"])+ expect(normalizeReasoningForProvider(config("openai", "gpt-5.4"), { mode: "max" }))+ .toEqual({ mode: "auto" })+ })++ it("normalizes custom budgets to positive integer values", () => {+ const cfg = config("anthropic", "claude-sonnet-4-5")+ expect(normalizeReasoningForProvider(cfg, { mode: "custom", budgetTokens: 2048.9 }))+ .toEqual({ mode: "custom", budgetTokens: 2048 })+ expect(normalizeReasoningForProvider(cfg, { mode: "custom", budgetTokens: 10 }))+ .toEqual({ mode: "custom", budgetTokens: 1024 })+ expect(normalizeReasoningForProvider(cfg, { mode: "custom", budgetTokens: 99_999 }))+ .toEqual({ mode: "custom", budgetTokens: 32_768 })+ expect(normalizeReasoningForProvider(cfg, { mode: "custom", budgetTokens: 0 }))+ .toEqual({ mode: "auto" })+ })+})diff --git a/src/lib/reasoning-capabilities.ts b/src/lib/reasoning-capabilities.tsnew file mode 100644index 00000000..a048fead--- /dev/null+++ b/src/lib/reasoning-capabilities.ts@@ -0,0 +1,115 @@+import type { LlmConfig, ReasoningConfig, ReasoningMode } from "@/stores/wiki-store"++export interface ReasoningCapabilities {+ modes: readonly ReasoningMode[]+ customBudgetRange: { min: number; max: number }+ /** A saved mode can outlive a model/provider change. Normalize it before+ * building the wire payload so stale settings cannot create invalid calls. */+ normalize(requested: ReasoningConfig): ReasoningConfig+}++const AUTO_ONLY = ["auto"] as const+const OPENAI_LEVELS = ["auto", "low", "medium", "high"] as const+const BUDGET_LEVELS = ["auto", "off", "low", "medium", "high", "max", "custom"] as const+const THINKING_REQUIRED_BUDGETS = ["auto", "low", "medium", "high", "max", "custom"] as const+const THINKING_REQUIRED_LEVELS = ["auto", "low", "medium", "high", "max"] as const+const OLLAMA_LEVELS = ["auto", "off", "low", "medium", "high", "max"] as const+const TOGGLE_LEVELS = ["auto", "off"] as const+const DEEPSEEK_LEVELS = ["auto", "off", "high", "max"] as const++function capabilities(+ modes: readonly ReasoningMode[],+ customBudgetRange: { min: number; max: number } = { min: 1, max: 32_768 },+): ReasoningCapabilities {+ return {+ modes,+ customBudgetRange,+ normalize(requested) {+ if (!modes.includes(requested.mode)) return { mode: "auto" }+ if (requested.mode !== "custom") return { mode: requested.mode }+ const budget = Math.min(customBudgetRange.max, Math.floor(requested.budgetTokens ?? 0))+ if (budget > 0 && budget < customBudgetRange.min) {+ return { mode: "custom", budgetTokens: customBudgetRange.min }+ }+ return budget > 0 ? { mode: "custom", budgetTokens: budget } : { mode: "auto" }+ },+ }+}++function isClaude47OrLater(model: string): boolean {+ return /claude-(?:opus|sonnet|haiku)-4[-_.]?(?:[7-9]|\d{2,})(?:[-_.]|$)/i.test(model)+}++function isGemini25Pro(model: string): boolean {+ return /gemini[-_.]?2\.5[-_.]?pro(?:[-_.]|$)/i.test(model)+}++function isGemini3(model: string): boolean {+ return /gemini[-_.]?3(?:[-_.]|$)/i.test(model)+}++function isOpenAiReasoningModel(config: LlmConfig): boolean {+ if (config.provider === "azure" && config.azureModelFamily === "gpt5") return true+ const model = config.model.trim().toLowerCase()+ return /^(?:gpt-5|o\d+)(?:[.\-_]|$)/.test(model)+}++/**+ * Resolve only capabilities that are part of the selected wire contract.+ * Generic custom gateways deliberately stay Auto-only: a vendor-looking+ * model name does not prove that an aggregator accepts that vendor's private+ * request fields.+ */+export function resolveReasoningCapabilities(config: LlmConfig): ReasoningCapabilities {+ if (config.provider === "claude-code" || config.provider === "codex-cli") {+ return capabilities(AUTO_ONLY)+ }+ if (config.provider === "ollama") return capabilities(OLLAMA_LEVELS)+ if (config.provider === "google") {+ if (isGemini3(config.model)) return capabilities(THINKING_REQUIRED_LEVELS)+ if (isGemini25Pro(config.model)) {+ return capabilities(THINKING_REQUIRED_BUDGETS, { min: 128, max: 32_768 })+ }+ return capabilities(BUDGET_LEVELS)+ }+ if (config.provider === "anthropic") {+ return capabilities(+ isClaude47OrLater(config.model) ? THINKING_REQUIRED_LEVELS : BUDGET_LEVELS,+ { min: 1024, max: 32_768 },+ )+ }+ if (config.provider === "minimax") return capabilities(AUTO_ONLY)+ if (config.provider === "openai" || config.provider === "azure") {+ return capabilities(isOpenAiReasoningModel(config) ? OPENAI_LEVELS : AUTO_ONLY)+ }+ if (config.provider === "custom") {+ const endpoint = config.customEndpoint.toLowerCase()+ if (/api\.deepseek\.(?:com|cn)(?:[:/]|$)/.test(endpoint)) {+ return capabilities(DEEPSEEK_LEVELS)+ }+ if (/xiaomimimo\.com(?:[:/]|$)/.test(endpoint)) {+ return capabilities(TOGGLE_LEVELS)+ }+ // Anthropic-compatible custom endpoints are not necessarily Anthropic+ // itself (MiniMax, Kimi and enterprise proxies differ), so omission is the+ // only portable default. Users can select a first-party preset when they+ // need vendor-specific controls.+ return capabilities(AUTO_ONLY)+ }+ return capabilities(AUTO_ONLY)+}++export function normalizeReasoningForProvider(+ config: LlmConfig,+ requested: ReasoningConfig,+): ReasoningConfig {+ return resolveReasoningCapabilities(config).normalize(requested)+}++export function isAdaptiveAnthropicModel(config: LlmConfig): boolean {+ return config.provider === "anthropic" && isClaude47OrLater(config.model)+}++export function isGeminiThinkingLevelModel(config: LlmConfig): boolean {+ return config.provider === "google" && isGemini3(config.model)+}