UNPKG

incize

Version:

AI Commit Copilot for Power Developers

227 lines (226 loc) 7.05 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const os = __importStar(require("os")); class ConfigManager { configPath; config; constructor() { const configDir = path.join(os.homedir(), '.incize'); this.configPath = path.join(configDir, 'config.json'); // Ensure config directory exists if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } this.config = this.loadConfig(); } /** * Load configuration from file */ loadConfig() { try { if (fs.existsSync(this.configPath)) { const data = fs.readFileSync(this.configPath, 'utf8'); const loaded = JSON.parse(data); return { defaultModel: 'claude-3-5-sonnet', telemetry: true, maxTokens: 2000, temperature: 0.1, ...loaded }; } } catch (error) { console.warn('Failed to load config, using defaults'); } return { defaultModel: 'claude-3-5-sonnet', telemetry: true, maxTokens: 2000, temperature: 0.1, }; } /** * Save configuration to file */ saveConfig() { try { fs.writeFileSync(this.configPath, JSON.stringify(this.config, null, 2)); } catch (error) { throw new Error(`Failed to save config: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Get configuration value */ get(key) { return this.config[key]; } /** * Set configuration value */ set(key, value) { this.config[key] = value; this.saveConfig(); } /** * Set API keys */ setApiKeys(anthropicKey, openaiKey) { if (anthropicKey && !this.isValidAnthropicKey(anthropicKey)) { throw new Error('Invalid Anthropic API key format. Expected format: sk-ant-api03-...'); } if (openaiKey && !this.isValidOpenAIKey(openaiKey)) { throw new Error('Invalid OpenAI API key format. Expected format: sk-...'); } if (anthropicKey) { this.config.anthropicApiKey = anthropicKey; } if (openaiKey) { this.config.openaiApiKey = openaiKey; } this.saveConfig(); } isValidAnthropicKey(key) { // Anthropic keys start with sk-ant-api03- and can be various lengths // Format: sk-ant-api03- followed by alphanumeric characters and hyphens return /^sk-ant-api03-[a-zA-Z0-9_-]{20,}$/.test(key); } isValidOpenAIKey(key) { // OpenAI keys start with sk- and can be various lengths // Format: sk- followed by alphanumeric characters and hyphens return /^sk-[a-zA-Z0-9_-]{20,}$/.test(key); } /** * Get API keys */ getApiKeys() { const anthropic = this.get('anthropicApiKey'); const openai = this.get('openaiApiKey'); return { ...(anthropic && { anthropic }), ...(openai && { openai }), }; } /** * Check if any API keys are configured */ hasApiKeys() { return !!(this.get('anthropicApiKey') || this.get('openaiApiKey')); } /** * Get available models based on configured API keys */ getAvailableModels() { const models = ['mock']; if (this.get('anthropicApiKey')) models.push('claude-3-5-sonnet'); if (this.get('openaiApiKey')) models.push('gpt-4o'); return models; } /** * Get current configuration as object */ getAll() { return { ...this.config }; } /** * Reset configuration to defaults */ reset() { this.config = { defaultModel: 'claude-3-5-sonnet', telemetry: true, maxTokens: 2000, temperature: 0.1, }; this.saveConfig(); } /** * Get config file path */ getConfigPath() { return this.configPath; } /** * Get authentication configuration */ async getAuthConfig() { try { const authPath = path.join(path.dirname(this.configPath), 'auth.json'); if (fs.existsSync(authPath)) { const data = fs.readFileSync(authPath, 'utf8'); return JSON.parse(data); } } catch (error) { console.warn('Failed to load auth config'); } return null; } /** * Set authentication configuration */ async setAuthConfig(authConfig) { try { const authPath = path.join(path.dirname(this.configPath), 'auth.json'); fs.writeFileSync(authPath, JSON.stringify(authConfig, null, 2)); } catch (error) { throw new Error(`Failed to save auth config: ${error instanceof Error ? error.message : 'Unknown error'}`); } } /** * Clear authentication configuration */ async clearAuthConfig() { try { const authPath = path.join(path.dirname(this.configPath), 'auth.json'); if (fs.existsSync(authPath)) { fs.unlinkSync(authPath); } } catch (error) { console.warn('Failed to clear auth config'); } } } exports.ConfigManager = ConfigManager;