From 222cc6a48af0e82c4e78c3ce6dd11401b1d99f21 Mon Sep 17 00:00:00 2001 From: eriksun421 Date: Wed, 11 Mar 2026 20:45:37 +0800 Subject: [PATCH] fix: remove empty auth env placeholders from anthropic-thirdparty and openrouter presets Storing ANTHROPIC_API_KEY:"" or ANTHROPIC_AUTH_TOKEN:"" in extra_env caused inferAuthStyleFromLegacy() to detect the wrong auth style, and left confusing empty entries visible in the UI after save/reopen. Auth injection is already handled via the api_key field in toClaudeCodeEnv(), so these placeholders serve no purpose and should not be persisted. Fixes #241 --- src/components/settings/ProviderForm.tsx | 2 +- src/components/settings/ProviderManager.tsx | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/components/settings/ProviderForm.tsx b/src/components/settings/ProviderForm.tsx index 8c412a9b..125008e5 100644 --- a/src/components/settings/ProviderForm.tsx +++ b/src/components/settings/ProviderForm.tsx @@ -27,7 +27,7 @@ import { useTranslation } from "@/hooks/useTranslation"; const PROVIDER_PRESETS: Record = { anthropic: { base_url: "https://api.anthropic.com", extra_env: "{}", protocol: "anthropic" }, - openrouter: { base_url: "https://openrouter.ai/api", extra_env: '{"ANTHROPIC_API_KEY":""}', protocol: "openrouter" }, + openrouter: { base_url: "https://openrouter.ai/api", extra_env: '{}', protocol: "openrouter" }, bedrock: { base_url: "", extra_env: '{"CLAUDE_CODE_USE_BEDROCK":"1","AWS_REGION":"us-east-1","CLAUDE_CODE_SKIP_BEDROCK_AUTH":"1"}', protocol: "bedrock" }, vertex: { base_url: "", extra_env: '{"CLAUDE_CODE_USE_VERTEX":"1","CLOUD_ML_REGION":"us-east5","CLAUDE_CODE_SKIP_VERTEX_AUTH":"1"}', protocol: "vertex" }, custom: { base_url: "", extra_env: "{}", protocol: "openai-compatible" }, diff --git a/src/components/settings/ProviderManager.tsx b/src/components/settings/ProviderManager.tsx index 6ace6d47..bc25ceb8 100644 --- a/src/components/settings/ProviderManager.tsx +++ b/src/components/settings/ProviderManager.tsx @@ -130,7 +130,7 @@ const QUICK_PRESETS: QuickPreset[] = [ provider_type: "anthropic", protocol: "anthropic", base_url: "", - extra_env: '{"ANTHROPIC_API_KEY":""}', + extra_env: "{}", fields: ["name", "api_key", "base_url", "model_mapping"], }, { @@ -458,26 +458,19 @@ function PresetConnectDialog({ return; } - // For anthropic-thirdparty, inject the correct auth key into extra_env - // while preserving any other user-specified env vars (e.g. API_TIMEOUT_MS) + // For anthropic-thirdparty, strip auth key placeholders from extra_env. + // Auth is handled via the api_key field — storing empty ANTHROPIC_API_KEY / + // ANTHROPIC_AUTH_TOKEN placeholders in extra_env causes toClaudeCodeEnv() + // to infer the wrong authStyle and can override freshly-injected credentials. let finalExtraEnv = extraEnv; if (preset.key === "anthropic-thirdparty") { try { const parsed = JSON.parse(extraEnv || "{}"); - // Remove both auth keys, then set the correct one delete parsed["ANTHROPIC_API_KEY"]; delete parsed["ANTHROPIC_AUTH_TOKEN"]; - if (authStyle === "auth_token") { - parsed["ANTHROPIC_AUTH_TOKEN"] = ""; - } else { - parsed["ANTHROPIC_API_KEY"] = ""; - } finalExtraEnv = JSON.stringify(parsed); } catch { - // If parse fails, fall back to simple replacement - finalExtraEnv = authStyle === "auth_token" - ? '{"ANTHROPIC_AUTH_TOKEN":""}' - : '{"ANTHROPIC_API_KEY":""}'; + finalExtraEnv = "{}"; } } // In edit mode, preserve existing role_models_json unless the user modifies mapping fields