UNPKG

contaigents

Version:

Modular AI Content Ecosystem with Audio Generation

177 lines (176 loc) • 7.43 kB
#!/usr/bin/env node import { Command } from 'commander'; import readline from 'readline'; import { LLMFactory, providers } from '../services/llm/LLMFactory.js'; import { FileService } from '../services/fileService.js'; export function createConfigureCommand() { const command = new Command('configure'); command .description('Configure LLM providers and set default provider') .action(async () => { console.log('šŸ”§ Contaigents LLM Configuration Setup\n'); try { const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Helper function to ask questions const askQuestion = (question) => { return new Promise((resolve) => { rl.question(question, (answer) => { resolve(answer.trim()); }); }); }; // Show available providers const availableProviders = Object.keys(providers); console.log('šŸ“‹ Available LLM Providers:'); availableProviders.forEach((provider, index) => { console.log(` ${index + 1}. ${provider}`); }); console.log(); // Ask user to select provider const providerChoice = await askQuestion('Select a provider (enter number or name): '); let selectedProvider; if (/^\d+$/.test(providerChoice)) { const index = parseInt(providerChoice) - 1; if (index >= 0 && index < availableProviders.length) { selectedProvider = availableProviders[index]; } else { console.error('āŒ Invalid provider number'); rl.close(); return; } } else { const found = availableProviders.find(p => p.toLowerCase() === providerChoice.toLowerCase()); if (found) { selectedProvider = found; } else { console.error('āŒ Invalid provider name'); rl.close(); return; } } console.log(`\nāœ… Selected provider: ${selectedProvider}`); // Get provider configuration const providerConfig = providers[selectedProvider]; const llmInstance = LLMFactory.getProvider(selectedProvider); const config = {}; console.log(`\nšŸ”‘ Configuring ${selectedProvider}:`); // Configure each field for (const field of providerConfig.configFields) { console.log(`\nšŸ“ ${field.name}:`); if (field.options) { console.log(' Available options:'); field.options.forEach((option, index) => { const defaultMarker = option === field.default ? ' (default)' : ''; console.log(` ${index + 1}. ${option}${defaultMarker}`); }); } if (field.default) { console.log(` Default: ${field.default}`); } let prompt = ` Enter ${field.name}`; if (field.name.toLowerCase().includes('key')) { prompt += ' (or "env:VARIABLE_NAME" to use environment variable)'; } if (field.default) { prompt += ` [${field.default}]`; } prompt += ': '; const value = await askQuestion(prompt); if (value === '' && field.default) { config[field.name] = field.default; } else if (value.startsWith('env:')) { // Store environment variable reference config[field.name] = value; } else if (field.options && /^\d+$/.test(value)) { // Handle numeric selection for options const index = parseInt(value) - 1; if (index >= 0 && index < field.options.length) { config[field.name] = field.options[index]; } else { config[field.name] = value; } } else { config[field.name] = value; } } // Save provider configuration llmInstance.configure(config); console.log(`\nāœ… ${selectedProvider} configuration saved!`); // Ask if this should be the default provider const setDefault = await askQuestion('\nšŸŽÆ Set this as the default provider? (y/N): '); if (setDefault.toLowerCase() === 'y' || setDefault.toLowerCase() === 'yes') { await setDefaultProvider(selectedProvider); console.log(`āœ… ${selectedProvider} set as default provider!`); } // Show configuration summary console.log('\nšŸ“Š Configuration Summary:'); console.log(` Provider: ${selectedProvider}`); Object.entries(config).forEach(([key, value]) => { if (key.toLowerCase().includes('key') && !value.toString().startsWith('env:')) { console.log(` ${key}: ${'*'.repeat(8)}`); } else { console.log(` ${key}: ${value}`); } }); const currentDefault = await getCurrentDefaultProvider(); if (currentDefault) { console.log(` Default Provider: ${currentDefault}`); } console.log('\nšŸŽ‰ Configuration completed successfully!'); console.log('šŸ’” You can now use this provider with the chat command or other CLI features.'); rl.close(); } catch (error) { console.error(`āŒ Configuration failed: ${error.message}`); process.exit(1); } }); return command; } async function setDefaultProvider(providerName) { const fileService = new FileService(); const configPath = './.config/global_config.json'; try { let globalConfig = { defaultProvider: null }; // Try to load existing config try { const existingConfig = await fileService.readFile(configPath); globalConfig = JSON.parse(existingConfig); } catch (error) { // File doesn't exist, use default config } globalConfig.defaultProvider = providerName; await fileService.saveFile({ path: configPath, content: JSON.stringify(globalConfig, null, 2) }); } catch (error) { console.error('Failed to save default provider configuration'); throw error; } } async function getCurrentDefaultProvider() { const fileService = new FileService(); const configPath = './.config/global_config.json'; try { const configData = await fileService.readFile(configPath); const globalConfig = JSON.parse(configData); return globalConfig.defaultProvider; } catch (error) { return null; } }