Add Restore Default Claude CLI button to settings page

Co-authored-by: simon-ami <102378134+simon-ami@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-11 11:54:13 +00:00
parent 66c80452e3
commit 8eba68e47a
5 changed files with 104 additions and 2 deletions

View File

@@ -83,6 +83,35 @@ export async function updateClaudeConfig(updates) {
}
}
/**
* Replace the global Claude CLI configuration entirely
* Unlike updateClaudeConfig, this replaces the config instead of merging.
*
* @param {Object} config - The new configuration to write
* @returns {Promise<Object>} The written configuration
*/
export async function replaceClaudeConfig(config) {
const configPath = getClaudeConfigPath();
// 1. Ensure .claude directory exists
const configDir = path.dirname(configPath);
try {
await fs.mkdir(configDir, { recursive: true });
} catch (error) {
// Ignore if exists
}
// 2. Write config directly (no merge)
try {
await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf8');
logger.info(`[ClaudeConfig] Replaced config at ${configPath}`);
return config;
} catch (error) {
logger.error(`[ClaudeConfig] Failed to write config:`, error.message);
throw error;
}
}
/**
* Simple deep merge for objects
*/

View File

@@ -18,7 +18,7 @@ import { fileURLToPath } from 'url';
import express from 'express';
import { getPublicConfig, saveConfig, config } from '../config.js';
import { DEFAULT_PORT, ACCOUNT_CONFIG_PATH } from '../constants.js';
import { readClaudeConfig, updateClaudeConfig, getClaudeConfigPath } from '../utils/claude-config.js';
import { readClaudeConfig, updateClaudeConfig, replaceClaudeConfig, getClaudeConfigPath } from '../utils/claude-config.js';
import { logger } from '../utils/logger.js';
import { getAuthorizationUrl, completeOAuthFlow, startCallbackServer } from '../auth/oauth.js';
import { loadAccounts, saveAccounts } from '../account-manager/storage.js';
@@ -438,6 +438,41 @@ export function mountWebUI(app, dirname, accountManager) {
}
});
/**
* POST /api/claude/config/restore - Restore Claude CLI to default (remove proxy settings)
*/
app.post('/api/claude/config/restore', async (req, res) => {
try {
const claudeConfig = await readClaudeConfig();
// Remove proxy-related environment variables to restore defaults
if (claudeConfig.env) {
delete claudeConfig.env.ANTHROPIC_BASE_URL;
delete claudeConfig.env.ANTHROPIC_AUTH_TOKEN;
delete claudeConfig.env.ANTHROPIC_MODEL;
delete claudeConfig.env.CLAUDE_CODE_SUBAGENT_MODEL;
delete claudeConfig.env.ANTHROPIC_DEFAULT_OPUS_MODEL;
delete claudeConfig.env.ANTHROPIC_DEFAULT_SONNET_MODEL;
delete claudeConfig.env.ANTHROPIC_DEFAULT_HAIKU_MODEL;
delete claudeConfig.env.ENABLE_EXPERIMENTAL_MCP_CLI;
}
// Use replaceClaudeConfig to completely overwrite the config (not merge)
const newConfig = await replaceClaudeConfig(claudeConfig);
logger.info(`[WebUI] Restored Claude CLI config to defaults at ${getClaudeConfigPath()}`);
res.json({
status: 'ok',
config: newConfig,
message: 'Claude CLI configuration restored to defaults'
});
} catch (error) {
logger.error('[WebUI] Error restoring Claude config:', error);
res.status(500).json({ status: 'error', error: error.message });
}
});
/**
* POST /api/models/config - Update model configuration (hidden/pinned/alias)
*/