|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import os from 'os'; |
| 4 | +import { getGeminiOAuthStatus } from './oauth-gemini.js'; |
| 5 | +import { getCodexOAuthStatus } from './oauth-codex.js'; |
| 6 | + |
| 7 | +export function buildSystemPrompt(agentId, model, subAgent) { |
| 8 | + const parts = []; |
| 9 | + if (agentId && agentId !== 'claude-code') { |
| 10 | + const displayAgentId = agentId.split('-·-')[0]; |
| 11 | + parts.push(`Use ${displayAgentId} subagent for all tasks.`); |
| 12 | + } |
| 13 | + if (model) parts.push(`Model: ${model}.`); |
| 14 | + if (subAgent) parts.push(`Subagent: ${subAgent}.`); |
| 15 | + return parts.join(' '); |
| 16 | +} |
| 17 | + |
| 18 | +const PROVIDER_CONFIGS = { |
| 19 | + 'anthropic': { |
| 20 | + name: 'Anthropic', configPaths: [ |
| 21 | + path.join(os.homedir(), '.claude.json'), |
| 22 | + path.join(os.homedir(), '.config', 'claude', 'settings.json'), |
| 23 | + path.join(os.homedir(), '.anthropic.json') |
| 24 | + ], |
| 25 | + configFormat: (apiKey, model) => ({ api_key: apiKey, default_model: model }) |
| 26 | + }, |
| 27 | + 'openai': { |
| 28 | + name: 'OpenAI', configPaths: [ |
| 29 | + path.join(os.homedir(), '.openai.json'), |
| 30 | + path.join(os.homedir(), '.config', 'openai', 'api-key') |
| 31 | + ], |
| 32 | + configFormat: (apiKey, model) => ({ apiKey, defaultModel: model }) |
| 33 | + }, |
| 34 | + 'google': { |
| 35 | + name: 'Google Gemini', configPaths: [ |
| 36 | + path.join(os.homedir(), '.gemini.json'), |
| 37 | + path.join(os.homedir(), '.config', 'gemini', 'credentials.json') |
| 38 | + ], |
| 39 | + configFormat: (apiKey, model) => ({ api_key: apiKey, default_model: model }) |
| 40 | + }, |
| 41 | + 'openrouter': { |
| 42 | + name: 'OpenRouter', configPaths: [ |
| 43 | + path.join(os.homedir(), '.openrouter.json'), |
| 44 | + path.join(os.homedir(), '.config', 'openrouter', 'config.json') |
| 45 | + ], |
| 46 | + configFormat: (apiKey, model) => ({ api_key: apiKey, default_model: model }) |
| 47 | + }, |
| 48 | + 'github': { |
| 49 | + name: 'GitHub Models', configPaths: [ |
| 50 | + path.join(os.homedir(), '.github.json'), |
| 51 | + path.join(os.homedir(), '.config', 'github-copilot.json') |
| 52 | + ], |
| 53 | + configFormat: (apiKey, model) => ({ github_token: apiKey, default_model: model }) |
| 54 | + }, |
| 55 | + 'azure': { |
| 56 | + name: 'Azure OpenAI', configPaths: [ |
| 57 | + path.join(os.homedir(), '.azure.json'), |
| 58 | + path.join(os.homedir(), '.config', 'azure-openai', 'config.json') |
| 59 | + ], |
| 60 | + configFormat: (apiKey, model) => ({ api_key: apiKey, endpoint: '', default_model: model }) |
| 61 | + }, |
| 62 | + 'anthropic-claude-code': { |
| 63 | + name: 'Claude Code Max', configPaths: [ |
| 64 | + path.join(os.homedir(), '.claude', 'max.json'), |
| 65 | + path.join(os.homedir(), '.config', 'claude-code', 'max.json') |
| 66 | + ], |
| 67 | + configFormat: (apiKey, model) => ({ api_key: apiKey, plan: 'max', default_model: model }) |
| 68 | + }, |
| 69 | + 'opencode': { |
| 70 | + name: 'OpenCode', configPaths: [ |
| 71 | + path.join(os.homedir(), '.opencode', 'config.json'), |
| 72 | + path.join(os.homedir(), '.config', 'opencode', 'config.json') |
| 73 | + ], |
| 74 | + configFormat: (apiKey, model) => ({ api_key: apiKey, default_model: model, providers: ['anthropic', 'openai', 'google'] }) |
| 75 | + }, |
| 76 | + 'proxypilot': { |
| 77 | + name: 'ProxyPilot', configPaths: [ |
| 78 | + path.join(os.homedir(), '.proxypilot', 'config.json'), |
| 79 | + path.join(os.homedir(), '.config', 'proxypilot', 'config.json') |
| 80 | + ], |
| 81 | + configFormat: (apiKey, model) => ({ api_key: apiKey, default_model: model }) |
| 82 | + }, |
| 83 | + 'codex': { |
| 84 | + name: 'Codex CLI', configPaths: [ |
| 85 | + path.join(os.homedir(), '.codex', 'auth.json') |
| 86 | + ], |
| 87 | + configFormat: (apiKey) => ({ auth_mode: 'apikey', OPENAI_API_KEY: apiKey }) |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +export function maskKey(key) { |
| 92 | + if (!key || key.length < 8) return '****'; |
| 93 | + return '****' + key.slice(-4); |
| 94 | +} |
| 95 | + |
| 96 | +export function getProviderConfigs() { |
| 97 | + const configs = {}; |
| 98 | + for (const [providerId, config] of Object.entries(PROVIDER_CONFIGS)) { |
| 99 | + if (providerId === 'google') { |
| 100 | + const oauthStatus = getGeminiOAuthStatus(); |
| 101 | + if (oauthStatus) { |
| 102 | + configs[providerId] = { name: config.name, ...oauthStatus }; |
| 103 | + continue; |
| 104 | + } |
| 105 | + } |
| 106 | + if (providerId === 'codex') { |
| 107 | + const oauthStatus = getCodexOAuthStatus(); |
| 108 | + if (oauthStatus) { |
| 109 | + configs[providerId] = { name: config.name, ...oauthStatus }; |
| 110 | + continue; |
| 111 | + } |
| 112 | + } |
| 113 | + for (const configPath of config.configPaths) { |
| 114 | + try { |
| 115 | + if (fs.existsSync(configPath)) { |
| 116 | + const content = fs.readFileSync(configPath, 'utf8'); |
| 117 | + const parsed = JSON.parse(content); |
| 118 | + const rawKey = parsed.api_key || parsed.apiKey || parsed.github_token || parsed.OPENAI_API_KEY || ''; |
| 119 | + configs[providerId] = { |
| 120 | + name: config.name, |
| 121 | + apiKey: maskKey(rawKey), |
| 122 | + hasKey: !!rawKey, |
| 123 | + defaultModel: parsed.default_model || parsed.defaultModel || '', |
| 124 | + path: configPath |
| 125 | + }; |
| 126 | + break; |
| 127 | + } |
| 128 | + } catch (_) {} |
| 129 | + } |
| 130 | + if (!configs[providerId]) { |
| 131 | + configs[providerId] = { name: config.name, apiKey: '', hasKey: false, defaultModel: '', path: '' }; |
| 132 | + } |
| 133 | + } |
| 134 | + return configs; |
| 135 | +} |
| 136 | + |
| 137 | +export function saveProviderConfig(providerId, apiKey, defaultModel) { |
| 138 | + const config = PROVIDER_CONFIGS[providerId]; |
| 139 | + if (!config) throw new Error('Unknown provider: ' + providerId); |
| 140 | + const configPath = config.configPaths[0]; |
| 141 | + const configDir = path.dirname(configPath); |
| 142 | + if (!fs.existsSync(configDir)) fs.mkdirSync(configDir, { recursive: true }); |
| 143 | + let existing = {}; |
| 144 | + try { |
| 145 | + if (fs.existsSync(configPath)) existing = JSON.parse(fs.readFileSync(configPath, 'utf8')); |
| 146 | + } catch (_) {} |
| 147 | + const merged = { ...existing, ...config.configFormat(apiKey, defaultModel) }; |
| 148 | + fs.writeFileSync(configPath, JSON.stringify(merged, null, 2), { mode: 0o600 }); |
| 149 | + return configPath; |
| 150 | +} |
0 commit comments