UNPKG

ai-debug-local-mcp

Version:

🎯 ENHANCED AI GUIDANCE v4.1.2: Dramatically improved tool descriptions help AI users choose the right tools instead of 'close enough' options. Ultra-fast keyboard automation (10x speed), universal recording, multi-ecosystem debugging support, and compreh

305 lines 11.9 kB
/** * Fix Orchestrator - Automated fixing and code generation * * Coordinates automated fixes for bugs, performance issues, and code quality problems */ export class FixOrchestrator { subAgents = { analyzer: 'issue_analysis_agent', generator: 'fix_generation_agent', validator: 'fix_validation_agent', applicator: 'fix_application_agent', tester: 'fix_testing_agent' }; async orchestrate(task) { console.error('🔧 Fix Orchestrator: Analyzing fix requirements...'); const analysis = this.analyzeTask(task); const plan = this.createExecutionPlan(analysis); const results = await this.executePlan(plan); return this.synthesizeResults(results); } analyzeTask(task) { const keywords = (task.task || task.description || '').toLowerCase(); const analysis = { fixType: 'general', scope: task.scope || 'minimal', autoApply: task.autoApply || false, priority: task.priority || 'medium', hasSpecificIssues: !!(task.issues && task.issues.length > 0), suggestedAgents: [] }; // Determine fix type from keywords if (keywords.includes('bug') || keywords.includes('error') || keywords.includes('crash')) { analysis.fixType = 'bug'; } else if (keywords.includes('performance') || keywords.includes('slow') || keywords.includes('optimize')) { analysis.fixType = 'performance'; } else if (keywords.includes('lint') || keywords.includes('style') || keywords.includes('format')) { analysis.fixType = 'style'; } else if (keywords.includes('security') || keywords.includes('vulnerability')) { analysis.fixType = 'security'; analysis.priority = 'critical'; // Security always critical } else if (keywords.includes('refactor') || keywords.includes('clean')) { analysis.fixType = 'refactor'; } // Build agent pipeline analysis.suggestedAgents.push(this.subAgents.analyzer); analysis.suggestedAgents.push(this.subAgents.generator); analysis.suggestedAgents.push(this.subAgents.validator); if (analysis.autoApply) { analysis.suggestedAgents.push(this.subAgents.applicator); analysis.suggestedAgents.push(this.subAgents.tester); } return analysis; } createExecutionPlan(analysis) { const plan = { steps: [], parallel: false, // Fixes should be sequential estimatedDuration: 0 }; // Step 1: Analyze issues plan.steps.push({ agent: this.subAgents.analyzer, action: 'analyze_issues', params: { type: analysis.fixType, deepAnalysis: analysis.priority === 'critical', includeRoot: true } }); // Step 2: Generate fixes plan.steps.push({ agent: this.subAgents.generator, action: 'generate_fixes', params: { scope: analysis.scope, strategy: analysis.fixType === 'security' ? 'defensive' : 'balanced', multipleOptions: !analysis.autoApply } }); // Step 3: Validate fixes plan.steps.push({ agent: this.subAgents.validator, action: 'validate_fixes', params: { runTests: true, checkSideEffects: true, performanceImpact: analysis.fixType === 'performance' } }); // Optional Step 4: Apply fixes if (analysis.autoApply) { plan.steps.push({ agent: this.subAgents.applicator, action: 'apply_fixes', params: { backup: true, atomic: true, rollbackOnError: true } }); // Optional Step 5: Test after application plan.steps.push({ agent: this.subAgents.tester, action: 'test_applied_fixes', params: { regression: true, integration: true } }); } plan.estimatedDuration = plan.steps.length * 7; // 7 seconds per step return plan; } async executePlan(plan) { const results = []; for (const step of plan.steps) { console.log(` → Delegating to ${step.agent} for ${step.action}...`); // Mock implementation const result = { agent: step.agent, action: step.action, success: true, summary: `Completed ${step.action}`, data: this.getMockDataForAction(step.action), duration: Math.random() * 5000 + 2000 }; results.push(result); // Stop if validation fails if (step.action === 'validate_fixes' && !result.data.allValid) { console.error(' ⚠️ Fix validation failed, stopping pipeline'); break; } } return results; } getMockDataForAction(action) { switch (action) { case 'analyze_issues': return { issues: [ { id: 'BUG-001', type: 'null_reference', severity: 'high', location: 'src/utils/helper.js:45', rootCause: 'Missing null check' }, { id: 'PERF-001', type: 'n+1_query', severity: 'medium', location: 'src/api/users.js:23' } ], totalIssues: 2, criticalCount: 0 }; case 'generate_fixes': return { fixes: [ { issueId: 'BUG-001', description: 'Add null check before accessing property', code: 'if (obj && obj.property) { ... }', confidence: 0.95, alternatives: 1 }, { issueId: 'PERF-001', description: 'Use eager loading to prevent N+1 queries', code: 'User.findAll({ include: [Profile] })', confidence: 0.88, alternatives: 2 } ], generationTime: 3.2 }; case 'validate_fixes': return { allValid: true, results: [ { fixId: 'BUG-001', valid: true, testsPass: true, noSideEffects: true }, { fixId: 'PERF-001', valid: true, testsPass: true, performanceGain: '65%' } ] }; case 'apply_fixes': return { applied: 2, successful: 2, failed: 0, backupLocation: '/tmp/backup-12345', changedFiles: ['src/utils/helper.js', 'src/api/users.js'] }; case 'test_applied_fixes': return { allTestsPass: true, regressions: 0, performance: { before: { responseTime: 250 }, after: { responseTime: 150 } } }; default: return {}; } } synthesizeResults(results) { const analysisResult = results.find(r => r.action === 'analyze_issues'); const generationResult = results.find(r => r.action === 'generate_fixes'); const validationResult = results.find(r => r.action === 'validate_fixes'); const applicationResult = results.find(r => r.action === 'apply_fixes'); const findings = { critical: [], warnings: [], info: [] }; // Report on issues found if (analysisResult?.data?.issues) { analysisResult.data.issues.forEach((issue) => { if (issue.severity === 'high' || issue.severity === 'critical') { findings.critical.push({ type: 'issue_found', severity: 'critical', title: `${issue.type} at ${issue.location}`, description: issue.rootCause }); } }); } // Report on fixes if (generationResult?.data?.fixes) { findings.info.push({ type: 'fixes_generated', severity: 'info', title: `Generated ${generationResult.data.fixes.length} fixes`, description: 'Fixes are ready to be reviewed or applied' }); } // Build suggestions from generated fixes const suggestions = generationResult?.data?.fixes?.map((fix) => ({ title: fix.description, description: `Fix for ${fix.issueId} with ${fix.confidence * 100}% confidence`, priority: 'high', effort: 'small', code: fix.code })) || []; const summary = this.createSummary(analysisResult?.data, generationResult?.data, applicationResult?.data); return { success: validationResult?.data?.allValid || false, summary, findings, suggestions: suggestions.slice(0, 3), nextSteps: this.determineNextSteps(applicationResult?.data), metadata: { duration: results.reduce((sum, r) => sum + r.duration, 0), agentsUsed: [...new Set(results.map(r => r.agent))], confidence: 0.9, fixesGenerated: generationResult?.data?.fixes?.length || 0, fixesApplied: applicationResult?.data?.applied || 0 } }; } createSummary(analysisData, generationData, applicationData) { const parts = []; if (analysisData?.totalIssues) { parts.push(`Found ${analysisData.totalIssues} issues`); } if (generationData?.fixes) { parts.push(`Generated ${generationData.fixes.length} fixes`); } if (applicationData?.applied) { parts.push(`Applied ${applicationData.successful}/${applicationData.applied} fixes`); } return parts.join(' → ') || 'Fix analysis complete'; } determineNextSteps(applicationData) { const steps = []; if (applicationData?.applied) { steps.push('Review changed files'); steps.push('Run full test suite'); steps.push('Monitor application for regressions'); } else { steps.push('Review generated fixes'); steps.push('Apply fixes using fix_orchestrator with autoApply: true'); steps.push('Run tests after applying fixes'); } return steps; } } //# sourceMappingURL=fix-orchestrator.js.map