UNPKG

adpa-enterprise-framework-automation

Version:

Modular, standards-compliant Node.js/TypeScript automation framework for enterprise requirements, project, and data management. Provides CLI and API for BABOK v3, PMBOK 7th Edition, and DMBOK 2.0 (in progress). Production-ready Express.js API with TypeSpe

277 lines โ€ข 12.4 kB
/** * Environment Configuration CLI Commands * * Provides CLI commands for environment setup, validation, and optimization * to help users configure AI providers with automatic fallback mechanisms. * * @version 1.0.0 * @author Requirements Gathering Agent Team * @created 2024 * * @filepath src/commands/environment.ts */ import { Command } from 'commander'; import { writeFileSync } from 'fs'; import { EnvironmentSetup } from '../utils/environmentSetup.js'; import { ProviderFallbackManager } from '../modules/ai/ProviderFallbackManager.js'; import { PROVIDER_DEFINITIONS } from '../modules/ai/provider-definitions.js'; /** * Create environment configuration commands */ export function createEnvironmentCommands() { const envCommand = new Command('env') .description('Environment configuration and validation commands'); // Validate environment command envCommand .command('validate') .description('Validate current environment configuration') .option('--report', 'Generate detailed configuration report') .option('--output <file>', 'Save report to file') .action(async (options) => { try { console.log('๐Ÿ” Validating environment configuration...\n'); const setup = EnvironmentSetup.getInstance(); const validation = await setup.validateEnvironment(); setup.displayValidationResults(validation); if (options.report) { const report = await setup.generateConfigurationReport(); if (options.output) { writeFileSync(options.output, report); console.log(`\n๐Ÿ“„ Report saved to: ${options.output}`); } else { console.log('\n' + '='.repeat(60)); console.log(report); } } process.exit(validation.isValid ? 0 : 1); } catch (error) { console.error('โŒ Validation failed:', error.message); process.exit(1); } }); // Setup environment command envCommand .command('setup') .description('Interactive environment setup wizard') .option('--template <type>', 'Environment template (development|production)', 'development') .option('--output <file>', 'Output file path', '.env') .option('--force', 'Overwrite existing .env file') .action(async (options) => { try { const setup = EnvironmentSetup.getInstance(); if (options.template && options.output) { console.log(`๐Ÿ“‹ Creating environment file from ${options.template} template...\n`); setup.createEnvironmentFile(options.template, options.output); console.log('\nโœ… Environment file created successfully!'); console.log('๐Ÿ“ Please edit the file and configure your AI providers.\n'); } await setup.runSetupWizard(); } catch (error) { console.error('โŒ Setup failed:', error.message); process.exit(1); } }); // Provider status command envCommand .command('providers') .description('Show AI provider status and configuration') .option('--detailed', 'Show detailed provider information') .action(async (options) => { try { console.log('๐Ÿ” Checking AI provider status...\n'); const setup = EnvironmentSetup.getInstance(); console.log('๐Ÿ“Š Provider Status'); console.log('='.repeat(50)); for (const providerDef of PROVIDER_DEFINITIONS) { const status = await setup.checkProviderStatus(providerDef.id); const statusIcon = status.connected ? 'โœ…' : status.configured ? 'โš ๏ธ' : 'โŒ'; const statusText = status.connected ? 'Connected' : status.configured ? 'Configured' : 'Not Configured'; console.log(`\n${statusIcon} ${providerDef.displayName}`); console.log(` Status: ${statusText}`); console.log(` Category: ${providerDef.category}`); console.log(` Priority: ${providerDef.priority}`); if (options.detailed) { console.log(` Description: ${providerDef.description}`); console.log(` Setup Time: ${status.estimatedSetupTime}`); console.log(` Complexity: ${status.setupComplexity}`); console.log(` Required Vars: ${providerDef.requiredEnvVars.join(', ')}`); if (status.missingVars.length > 0) { console.log(` Missing Vars: ${status.missingVars.join(', ')}`); } console.log(` Features: ${providerDef.features.join(', ')}`); } } } catch (error) { console.error('โŒ Provider check failed:', error.message); process.exit(1); } }); // Fallback status command envCommand .command('fallback') .description('Show provider fallback status and configuration') .option('--history', 'Show fallback history') .option('--health', 'Show provider health status') .action(async (options) => { try { console.log('๐Ÿ”„ Checking provider fallback configuration...\n'); const fallbackManager = ProviderFallbackManager.getInstance(); const config = fallbackManager.getConfig(); console.log('๐Ÿ“Š Fallback Configuration'); console.log('='.repeat(50)); console.log(`Enabled: ${config.enabled ? 'Yes' : 'No'}`); console.log(`Fallback Order: ${config.fallbackOrder.join(' โ†’ ')}`); console.log(`Health Check Interval: ${config.healthCheckInterval}ms`); console.log(`Max Consecutive Failures: ${config.maxConsecutiveFailures}`); console.log(`Current Provider: ${fallbackManager.getCurrentProvider() || 'None'}`); if (options.health) { console.log('\n๐Ÿฅ Provider Health Status'); console.log('='.repeat(50)); const healthMap = fallbackManager.getProviderHealth(); for (const [provider, health] of healthMap) { const healthIcon = health.isHealthy ? 'โœ…' : 'โŒ'; console.log(`\n${healthIcon} ${provider}`); console.log(` Healthy: ${health.isHealthy ? 'Yes' : 'No'}`); console.log(` Consecutive Failures: ${health.consecutiveFailures}`); console.log(` Success Rate: ${(health.successRate * 100).toFixed(1)}%`); console.log(` Avg Response Time: ${health.averageResponseTime.toFixed(0)}ms`); console.log(` Last Check: ${health.lastHealthCheck.toLocaleString()}`); if (health.lastError) { console.log(` Last Error: ${health.lastError}`); } } } if (options.history) { console.log('\n๐Ÿ“ˆ Fallback History'); console.log('='.repeat(50)); const history = fallbackManager.getFallbackHistory(); if (history.length === 0) { console.log('No fallback events recorded'); } else { const recentHistory = history.slice(-10); // Show last 10 events recentHistory.forEach(event => { const icon = event.success ? 'โœ…' : 'โŒ'; console.log(`${icon} ${event.timestamp.toLocaleString()}: ${event.fromProvider} โ†’ ${event.toProvider} (${event.reason})`); }); if (history.length > 10) { console.log(`\n... and ${history.length - 10} more events`); } } } } catch (error) { console.error('โŒ Fallback check failed:', error.message); process.exit(1); } }); // Test providers command envCommand .command('test') .description('Test AI provider connections and performance') .option('--provider <name>', 'Test specific provider') .option('--all', 'Test all configured providers') .action(async (options) => { try { console.log('๐Ÿงช Testing AI provider connections...\n'); const fallbackManager = ProviderFallbackManager.getInstance(); if (options.provider) { await testSingleProvider(options.provider); } else if (options.all) { await testAllProviders(); } else { // Test current provider const currentProvider = fallbackManager.getCurrentProvider(); if (currentProvider) { await testSingleProvider(currentProvider); } else { console.log('โŒ No current provider set'); process.exit(1); } } } catch (error) { console.error('โŒ Provider test failed:', error.message); process.exit(1); } }); // Optimize configuration command envCommand .command('optimize') .description('Analyze and optimize environment configuration') .action(async () => { try { console.log('โšก Analyzing environment configuration for optimization...\n'); const setup = EnvironmentSetup.getInstance(); const validation = await setup.validateEnvironment(); console.log('๐ŸŽฏ Optimization Analysis'); console.log('='.repeat(50)); if (validation.optimizations.length > 0) { console.log('\nโšก Performance Optimizations:'); validation.optimizations.forEach(opt => console.log(` ${opt}`)); } if (validation.recommendations.length > 0) { console.log('\n๐Ÿ’ก Configuration Recommendations:'); validation.recommendations.forEach(rec => console.log(` ${rec}`)); } if (validation.warnings.length > 0) { console.log('\nโš ๏ธ Configuration Warnings:'); validation.warnings.forEach(warning => console.log(` ${warning}`)); } if (validation.optimizations.length === 0 && validation.recommendations.length === 0 && validation.warnings.length === 0) { console.log('โœ… Your configuration is already optimized!'); } } catch (error) { console.error('โŒ Optimization analysis failed:', error.message); process.exit(1); } }); return envCommand; } /** * Test a single provider */ async function testSingleProvider(providerId) { console.log(`๐Ÿงช Testing provider: ${providerId}`); const fallbackManager = ProviderFallbackManager.getInstance(); try { const startTime = Date.now(); // Test with a simple operation await fallbackManager.executeWithFallback(async (provider) => { if (provider !== providerId) { throw new Error(`Expected ${providerId}, got ${provider}`); } // Simulate a simple AI operation console.log(` ๐Ÿ”„ Testing connection to ${provider}...`); return 'test successful'; }, `Test_${providerId}`); const responseTime = Date.now() - startTime; console.log(` โœ… ${providerId} test successful (${responseTime}ms)`); } catch (error) { console.log(` โŒ ${providerId} test failed: ${error.message}`); } } /** * Test all configured providers */ async function testAllProviders() { const setup = EnvironmentSetup.getInstance(); const validation = await setup.validateEnvironment(); console.log(`Testing ${validation.configuredProviders.length} configured providers...\n`); for (const provider of validation.configuredProviders) { await testSingleProvider(provider); } } //# sourceMappingURL=environment.js.map