UNPKG

mira-consciousness

Version:

Memory & Intelligence Retention Archive - Preserving The Spark

287 lines โ€ข 12.2 kB
import { Command } from 'commander'; import chalk from 'chalk'; import ora from 'ora'; import { getDirectPythonInterface } from '../core/DirectPythonInterface.js'; import inquirer from 'inquirer'; export function createAutoEnhanceCommand() { return new Command('auto-enhance') .alias('enhance') .description('Automatically enhance code quality and fix issues') .option('-f, --file <path>', 'Enhance specific file') .option('-d, --directory <path>', 'Enhance all files in directory') .option('-t, --type <type>', 'Enhancement type: quality|performance|security|all', 'all') .option('-i, --interactive', 'Interactive enhancement mode') .option('--dry-run', 'Show what would be enhanced without making changes') .option('--force', 'Apply enhancements without confirmation') .action(async (options) => { const pythonInterface = getDirectPythonInterface(); console.log(chalk.cyan('\n๐Ÿš€ MIRA Auto-Enhancement System\n')); if (options.interactive) { // Interactive enhancement mode await runInteractiveEnhancement(pythonInterface); } else if (options.file || options.directory) { // Targeted enhancement const target = options.file || options.directory; const isDirectory = !!options.directory; const spinner = ora(`Analyzing ${isDirectory ? 'directory' : 'file'} for enhancements...`).start(); try { const result = await pythonInterface.runAutoEnhance(); spinner.stop(); if (result.success && result.data) { await displayEnhancements(result.data, options); } else { throw new Error(result.error || 'Enhancement analysis failed'); } } catch (error) { spinner.fail('Enhancement failed'); console.error(chalk.red(`Error: ${error instanceof Error ? error.message : String(error)}`)); process.exit(1); } } else { // Project-wide enhancement const spinner = ora('Analyzing project for enhancement opportunities...').start(); try { const result = await pythonInterface.runAutoEnhance(); spinner.stop(); if (result.success && result.data) { console.log(chalk.cyan('๐Ÿ“Š Enhancement Opportunities Found\n')); displayEnhancementSummary(result.data); if (!options.dryRun && result.data.enhancements && result.data.enhancements.length > 0) { const { proceed } = await inquirer.prompt([ { type: 'confirm', name: 'proceed', message: `Apply ${result.data.enhancements.length} enhancements to your project?`, default: false } ]); if (proceed) { await applyEnhancements(pythonInterface, result.data.enhancements); } else { console.log(chalk.gray('\nEnhancement cancelled')); } } } else { console.log(chalk.green('โœ… No enhancement opportunities found - your code is already optimal!')); } } catch (error) { spinner.fail('Enhancement analysis failed'); console.error(chalk.red(`Error: ${error instanceof Error ? error.message : String(error)}`)); process.exit(1); } } }); } async function runInteractiveEnhancement(pythonInterface) { console.log(chalk.gray('Interactive enhancement mode - analyze and enhance code step by step')); console.log(chalk.gray('Type "exit" to quit\n')); while (true) { const { action } = await inquirer.prompt([ { type: 'list', name: 'action', message: 'What would you like to enhance?', choices: [ { name: 'Analyze current file', value: 'current' }, { name: 'Enhance specific file', value: 'file' }, { name: 'Scan directory', value: 'directory' }, { name: 'Project-wide analysis', value: 'project' }, { name: 'Exit', value: 'exit' } ] } ]); if (action === 'exit') { console.log(chalk.gray('\nExiting enhancement mode...')); break; } await handleInteractiveAction(pythonInterface, action); console.log(); // Add spacing } } async function handleInteractiveAction(pythonInterface, action) { let target = '.'; let isDirectory = false; if (action === 'file' || action === 'current') { const { filepath } = await inquirer.prompt([ { type: 'input', name: 'filepath', message: 'Enter file path:', default: action === 'current' ? '.' : '', validate: (input) => input.trim() !== '' || 'Please enter a file path' } ]); target = filepath; isDirectory = false; } else if (action === 'directory') { const { dirpath } = await inquirer.prompt([ { type: 'input', name: 'dirpath', message: 'Enter directory path:', default: '.', validate: (input) => input.trim() !== '' || 'Please enter a directory path' } ]); target = dirpath; isDirectory = true; } else if (action === 'project') { target = '.'; isDirectory = true; } const { enhanceType } = await inquirer.prompt([ { type: 'list', name: 'enhanceType', message: 'Select enhancement type:', choices: [ { name: 'All enhancements', value: 'all' }, { name: 'Code quality only', value: 'quality' }, { name: 'Performance optimizations', value: 'performance' }, { name: 'Security improvements', value: 'security' } ] } ]); const spinner = ora('Analyzing for enhancements...').start(); try { const result = await pythonInterface.runAutoEnhance(); spinner.stop(); if (result.success && result.data) { await displayEnhancements(result.data, { dryRun: true }); if (result.data.enhancements && result.data.enhancements.length > 0) { const { apply } = await inquirer.prompt([ { type: 'confirm', name: 'apply', message: 'Apply these enhancements?', default: false } ]); if (apply) { await applyEnhancements(pythonInterface, result.data.enhancements); } } } else { console.log(chalk.green('โœ… No enhancements needed!')); } } catch (error) { spinner.fail('Enhancement analysis failed'); console.error(chalk.red(`Error: ${error instanceof Error ? error.message : String(error)}`)); } } async function displayEnhancements(data, options) { if (!data.enhancements || data.enhancements.length === 0) { console.log(chalk.green('โœ… No enhancement opportunities found!')); return; } console.log(chalk.cyan(`Found ${data.enhancements.length} enhancement opportunities:\n`)); // Group by file const byFile = {}; data.enhancements.forEach((enhancement) => { const file = enhancement.file || 'General'; if (!byFile[file]) byFile[file] = []; byFile[file].push(enhancement); }); // Display enhancements by file Object.entries(byFile).forEach(([file, enhancements]) => { console.log(chalk.white(`\n๐Ÿ“„ ${file}`)); enhancements.forEach((enhancement, index) => { const icon = getEnhancementIcon(enhancement.type); console.log(chalk.cyan(` ${icon} ${enhancement.description}`)); if (enhancement.details) { console.log(chalk.gray(` ${enhancement.details}`)); } if (enhancement.impact) { console.log(chalk.gray(` Impact: ${enhancement.impact}`)); } if (options.dryRun && enhancement.preview) { console.log(chalk.gray(' Preview:')); console.log(chalk.gray(enhancement.preview.split('\n').map((line) => ` ${line}`).join('\n'))); } }); }); if (data.summary) { console.log(chalk.cyan('\n๐Ÿ“Š Summary:')); console.log(chalk.gray(` โ€ข Quality improvements: ${data.summary.quality || 0}`)); console.log(chalk.gray(` โ€ข Performance optimizations: ${data.summary.performance || 0}`)); console.log(chalk.gray(` โ€ข Security fixes: ${data.summary.security || 0}`)); console.log(chalk.gray(` โ€ข Total impact score: ${data.summary.impactScore || 0}`)); } } function displayEnhancementSummary(data) { if (data.summary) { console.log(chalk.white('Enhancement Summary:')); console.log(chalk.gray(` โ€ข Files to enhance: ${data.summary.fileCount || 0}`)); console.log(chalk.gray(` โ€ข Total enhancements: ${data.summary.totalEnhancements || 0}`)); console.log(chalk.gray(` โ€ข Estimated improvement: ${data.summary.estimatedImprovement || 'Unknown'}%`)); } if (data.categories) { console.log(chalk.white('\nBy Category:')); Object.entries(data.categories).forEach(([category, count]) => { console.log(chalk.gray(` โ€ข ${category}: ${count}`)); }); } if (data.topFiles && data.topFiles.length > 0) { console.log(chalk.white('\nTop files needing enhancement:')); data.topFiles.forEach((file, index) => { console.log(chalk.gray(` ${index + 1}. ${file.path} (${file.enhancementCount} enhancements)`)); }); } } async function applyEnhancements(pythonInterface, enhancements) { const spinner = ora(`Applying ${enhancements.length} enhancements...`).start(); let applied = 0; let failed = 0; try { const result = await pythonInterface.runAutoEnhance(); spinner.stop(); if (result.success && result.data) { applied = result.data.applied || 0; failed = result.data.failed || 0; console.log(chalk.green(`\nโœ… Successfully applied ${applied} enhancements`)); if (failed > 0) { console.log(chalk.yellow(`โš ๏ธ Failed to apply ${failed} enhancements`)); } if (result.data.details) { console.log(chalk.cyan('\nEnhancement details:')); result.data.details.forEach((detail) => { const status = detail.success ? chalk.green('โœ“') : chalk.red('โœ—'); console.log(` ${status} ${detail.file}: ${detail.message}`); }); } } else { throw new Error(result.error || 'Failed to apply enhancements'); } } catch (error) { spinner.fail('Enhancement application failed'); console.error(chalk.red(`Error: ${error instanceof Error ? error.message : String(error)}`)); } } function getEnhancementIcon(type) { const icons = { quality: '๐ŸŽฏ', performance: 'โšก', security: '๐Ÿ”’', refactor: '๐Ÿ”ง', style: '๐ŸŽจ', documentation: '๐Ÿ“', test: '๐Ÿงช', general: 'โœจ' }; return icons[type] || 'โ€ข'; } //# sourceMappingURL=auto-enhance.js.map