From 03f473e7c2ff2dbd3540563d6307241ab705c1b1 Mon Sep 17 00:00:00 2001 From: Kacper Date: Wed, 10 Dec 2025 10:36:40 +0100 Subject: [PATCH] feat(auth): improve authentication error handling for Claude SDK - Enhanced error messages in FeatureExecutor and ClaudeProvider to provide clearer guidance on missing authentication. - Added checks for Claude CLI installation status to inform users if they need to authenticate via CLI or set environment variables. - Improved fallback error messages to ensure users receive relevant instructions regardless of the authentication method. These changes enhance user experience by providing more informative feedback regarding authentication issues. --- app/electron/services/feature-executor.js | 15 ++++++++++-- app/electron/services/model-provider.js | 28 +++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/electron/services/feature-executor.js b/app/electron/services/feature-executor.js index 7a3cd30b..bac9a4f1 100644 --- a/app/electron/services/feature-executor.js +++ b/app/electron/services/feature-executor.js @@ -369,8 +369,19 @@ class FeatureExecutor { // Ensure provider auth is available (especially for Claude SDK) const provider = this.getProvider(feature); if (provider?.ensureAuthEnv && !provider.ensureAuthEnv()) { - const authMsg = - "Missing Anthropic auth. Set ANTHROPIC_API_KEY or run `claude login` so ~/.claude/config.json contains oauth_token."; + // Check if CLI is installed to provide better error message + let authMsg = "Missing Anthropic auth. Set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN environment variable."; + try { + const claudeCliDetector = require('./claude-cli-detector'); + const detection = claudeCliDetector.detectClaudeInstallation(); + if (detection.installed && detection.method === 'cli') { + authMsg = "Claude CLI is installed but not authenticated. Run `claude login` to authenticate, or set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN environment variable."; + } else { + authMsg = "Missing Anthropic auth. Set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN, or install Claude CLI and run `claude login`."; + } + } catch (err) { + // Fallback to default message + } console.error(`[FeatureExecutor] ${authMsg}`); throw new Error(authMsg); } diff --git a/app/electron/services/model-provider.js b/app/electron/services/model-provider.js index bbc6f9cd..d009089e 100644 --- a/app/electron/services/model-provider.js +++ b/app/electron/services/model-provider.js @@ -129,7 +129,19 @@ class ClaudeProvider extends ModelProvider { console.log('[ClaudeProvider] Loaded CLAUDE_CODE_OAUTH_TOKEN from ~/.claude/config.json'); return true; } - console.error('[ClaudeProvider] No Anthropic auth found (env empty, ~/.claude/config.json missing token)'); + + // Check if CLI is installed but not logged in + try { + const claudeCliDetector = require('./claude-cli-detector'); + const detection = claudeCliDetector.detectClaudeInstallation(); + if (detection.installed && detection.method === 'cli') { + console.error('[ClaudeProvider] Claude CLI is installed but not logged in. Run `claude login` to authenticate.'); + } else { + console.error('[ClaudeProvider] No Anthropic auth found (env empty, ~/.claude/config.json missing token)'); + } + } catch (err) { + console.error('[ClaudeProvider] No Anthropic auth found (env empty, ~/.claude/config.json missing token)'); + } return false; } @@ -146,7 +158,19 @@ class ClaudeProvider extends ModelProvider { async *executeQuery(options) { // Ensure we have auth; fall back to CLI login token if available. if (!this.ensureAuthEnv()) { - const msg = 'Missing Anthropic auth. Set ANTHROPIC_API_KEY or run `claude login` (CLI) so ~/.claude/config.json contains oauth_token.'; + // Check if CLI is installed to provide better error message + let msg = 'Missing Anthropic auth. Set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN environment variable.'; + try { + const claudeCliDetector = require('./claude-cli-detector'); + const detection = claudeCliDetector.detectClaudeInstallation(); + if (detection.installed && detection.method === 'cli') { + msg = 'Claude CLI is installed but not authenticated. Run `claude login` to authenticate, or set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN environment variable.'; + } else { + msg = 'Missing Anthropic auth. Set ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN, or install Claude CLI and run `claude login`.'; + } + } catch (err) { + // Fallback to default message + } console.error(`[ClaudeProvider] ${msg}`); yield { type: 'error', error: msg }; return;