UNPKG

behemoth-cli

Version:

🌍 BEHEMOTH CLIv3.760.4 - Level 50+ POST-SINGULARITY Intelligence Trading AI

211 lines 7.12 kB
import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; const CONFIG_DIR = '.groq'; // In home directory const CONFIG_FILE = 'local-settings.json'; export class ConfigManager { configPath; constructor() { const homeDir = os.homedir(); this.configPath = path.join(homeDir, CONFIG_DIR, CONFIG_FILE); } ensureConfigDir() { const configDir = path.dirname(this.configPath); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true, mode: 0o700 }); } } setApiKey(apiKey) { try { this.ensureConfigDir(); let config = {}; if (fs.existsSync(this.configPath)) { const configData = fs.readFileSync(this.configPath, 'utf8'); config = JSON.parse(configData); } config.groqApiKey = apiKey; fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), { mode: 0o600, // Read/write for owner only }); } catch (error) { throw new Error(`Failed to save API key: ${error}`); } } addApiKey(apiKey) { try { this.ensureConfigDir(); let config = {}; if (fs.existsSync(this.configPath)) { const configData = fs.readFileSync(this.configPath, 'utf8'); config = JSON.parse(configData); } if (!config.groqApiKeys) { config.groqApiKeys = []; } if (!config.groqApiKeys.includes(apiKey)) { config.groqApiKeys.push(apiKey); } fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), { mode: 0o600, // Read/write for owner only }); } catch (error) { throw new Error(`Failed to add API key: ${error}`); } } getApiKey() { try { if (!fs.existsSync(this.configPath)) { return null; } const configData = fs.readFileSync(this.configPath, 'utf8'); const config = JSON.parse(configData); return config.groqApiKey || null; } catch (error) { console.warn('Failed to read API key:', error); return null; } } getApiKeys() { try { if (!fs.existsSync(this.configPath)) { return []; } const configData = fs.readFileSync(this.configPath, 'utf8'); const config = JSON.parse(configData); return config.groqApiKeys || []; } catch (error) { console.warn('Failed to read API keys:', error); return []; } } setCurrentKeyIndex(index) { try { this.ensureConfigDir(); let config = {}; if (fs.existsSync(this.configPath)) { const configData = fs.readFileSync(this.configPath, 'utf8'); config = JSON.parse(configData); } config.currentKeyIndex = index; fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), { mode: 0o600, // Read/write for owner only }); } catch (error) { throw new Error(`Failed to save current key index: ${error}`); } } getCurrentKeyIndex() { try { if (!fs.existsSync(this.configPath)) { return null; } const configData = fs.readFileSync(this.configPath, 'utf8'); const config = JSON.parse(configData); return config.currentKeyIndex ?? null; } catch (error) { console.warn('Failed to read current key index:', error); return null; } } getDefaultModel() { try { if (!fs.existsSync(this.configPath)) { return null; } const configData = fs.readFileSync(this.configPath, 'utf8'); const config = JSON.parse(configData); return config.defaultModel || null; } catch (error) { console.warn('Failed to read default model:', error); return null; } } setN8nConfig(url, apiKey) { try { this.ensureConfigDir(); let config = {}; if (fs.existsSync(this.configPath)) { const configData = fs.readFileSync(this.configPath, 'utf8'); config = JSON.parse(configData); } config.n8nUrl = url; config.n8nApiKey = apiKey; fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), { mode: 0o600, }); } catch (error) { throw new Error(`Failed to save n8n config: ${error}`); } } getN8nConfig() { try { if (!fs.existsSync(this.configPath)) { return null; } const configData = fs.readFileSync(this.configPath, 'utf8'); const config = JSON.parse(configData); if (config.n8nUrl && config.n8nApiKey) { return { url: config.n8nUrl, apiKey: config.n8nApiKey }; } return null; } catch (error) { console.warn('Failed to read n8n config:', error); return null; } } setDefaultModel(model) { try { this.ensureConfigDir(); let config = {}; if (fs.existsSync(this.configPath)) { const configData = fs.readFileSync(this.configPath, 'utf8'); config = JSON.parse(configData); } config.defaultModel = model; fs.writeFileSync(this.configPath, JSON.stringify(config, null, 2), { mode: 0o600 // Read/write for owner only }); } catch (error) { throw new Error(`Failed to save default model: ${error}`); } } getCurrentApiKey() { try { const keys = this.getApiKeys(); const currentIndex = this.getCurrentKeyIndex() || 0; if (keys.length === 0) { return this.getApiKey(); // Fallback to single key } return keys[currentIndex % keys.length] || null; } catch (error) { console.warn('Failed to get current API key:', error); return null; } } rotateApiKey() { try { const keys = this.getApiKeys(); if (keys.length <= 1) { return; // No rotation needed for single key or no keys } const currentIndex = this.getCurrentKeyIndex() || 0; const nextIndex = (currentIndex + 1) % keys.length; this.setCurrentKeyIndex(nextIndex); } catch (error) { console.warn('Failed to rotate API key:', error); } } } //# sourceMappingURL=local-settings.js.map