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

214 lines • 8.44 kB
/** * Sub-Agent Executor * Real implementation for delegating tasks to specialized sub-agents */ export class SubAgentExecutor { handler; // UniversalRealHandler instance activeAgents = new Map(); constructor(handler) { this.handler = handler; } async executeTask(task) { const startTime = Date.now(); try { // Map agent types to actual tools/orchestrators const agentMapping = this.getAgentMapping(); const toolName = agentMapping[task.agent]; if (!toolName) { throw new Error(`Unknown sub-agent: ${task.agent}`); } // Execute the appropriate tool/orchestrator const result = await this.handler.execute(toolName, { task: task.action, context: task.context, tools: task.tools }, {}); // Transform the result into SubAgentResult format return this.transformResult(task, result, Date.now() - startTime); } catch (error) { return { agent: task.agent, action: task.action, success: false, summary: `Failed to execute ${task.action}: ${error instanceof Error ? error.message : String(error)}`, data: { errors: [error instanceof Error ? error.message : String(error)] }, duration: Date.now() - startTime }; } } async executeTasks(tasks) { // Execute tasks in parallel when possible const results = []; // Group tasks by priority const criticalTasks = tasks.filter(t => t.priority === 'critical'); const highTasks = tasks.filter(t => t.priority === 'high'); const otherTasks = tasks.filter(t => !['critical', 'high'].includes(t.priority || 'medium')); // Execute critical tasks first, in sequence for (const task of criticalTasks) { results.push(await this.executeTask(task)); } // Execute high priority tasks in parallel if (highTasks.length > 0) { const highResults = await Promise.all(highTasks.map(task => this.executeTask(task))); results.push(...highResults); } // Execute other tasks in parallel if (otherTasks.length > 0) { const otherResults = await Promise.all(otherTasks.map(task => this.executeTask(task))); results.push(...otherResults); } return results; } getAgentMapping() { return { // Debug agents 'debug_discovery_agent': 'debug_orchestrator', 'debug_agent': 'debug_orchestrator', // Performance agents 'performance_analysis_agent': 'performance_orchestrator', 'performance_agent': 'performance_orchestrator', // Test agents 'validation_testing_agent': 'test_orchestrator', 'test_agent': 'test_orchestrator', // Error investigation agents 'error_investigation_agent': 'fix_orchestrator', 'error_agent': 'fix_orchestrator', // Architecture agents 'architecture_agent': 'architecture_orchestrator', // QA agents 'accessibility_audit_agent': 'qa_orchestrator', 'qa_agent': 'qa_orchestrator' }; } transformResult(task, toolResult, duration) { // Handle different result formats from tools/orchestrators if (toolResult.success !== undefined) { // OrchestratorResult format return { agent: task.agent, action: task.action, success: toolResult.success, summary: toolResult.summary || `Completed ${task.action}`, data: { findings: this.extractFindings(toolResult), metrics: toolResult.metadata || {}, suggestions: toolResult.suggestions || [], evidence: toolResult.evidence || [] }, duration, toolsUsed: toolResult.toolsUsed || [] }; } // Tool result format const content = toolResult.content?.[0]?.text || ''; const success = !toolResult.isError && !content.includes('error'); return { agent: task.agent, action: task.action, success, summary: this.extractSummary(content, task.action), data: { findings: this.parseFindings(content), metrics: this.parseMetrics(content), suggestions: this.parseSuggestions(content) }, duration }; } extractFindings(result) { const findings = []; if (result.findings) { findings.push(...(result.findings.critical || [])); findings.push(...(result.findings.warnings || [])); findings.push(...(result.findings.info || [])); } return findings; } extractSummary(content, action) { // Extract first meaningful line or paragraph const lines = content.split('\n').filter(line => line.trim()); for (const line of lines) { if (line.length > 20 && !line.startsWith('#') && !line.startsWith('*')) { return line.trim(); } } return `Completed ${action}`; } parseFindings(content) { const findings = []; const lines = content.split('\n'); lines.forEach((line, index) => { const lowerLine = line.toLowerCase(); if (lowerLine.includes('error') || lowerLine.includes('exception')) { findings.push({ type: 'error', severity: 'critical', message: line.trim(), line: index + 1 }); } else if (lowerLine.includes('warning') || lowerLine.includes('deprecated')) { findings.push({ type: 'warning', severity: 'warning', message: line.trim(), line: index + 1 }); } else if (lowerLine.includes('info') || lowerLine.includes('note')) { findings.push({ type: 'info', severity: 'info', message: line.trim(), line: index + 1 }); } }); return findings; } parseMetrics(content) { const metrics = {}; // Parse common metric patterns const patterns = [ { regex: /performance[:\s]+(\d+(?:\.\d+)?)\s*%/i, key: 'performance' }, { regex: /memory[:\s]+(\d+(?:\.\d+)?)\s*MB/i, key: 'memory' }, { regex: /cpu[:\s]+(\d+(?:\.\d+)?)\s*%/i, key: 'cpu' }, { regex: /fps[:\s]+(\d+)/i, key: 'fps' }, { regex: /load\s+time[:\s]+(\d+(?:\.\d+)?)\s*ms/i, key: 'loadTime' }, { regex: /errors?[:\s]+(\d+)/i, key: 'errorCount' }, { regex: /warnings?[:\s]+(\d+)/i, key: 'warningCount' } ]; patterns.forEach(({ regex, key }) => { const match = content.match(regex); if (match) { metrics[key] = parseFloat(match[1]); } }); return metrics; } parseSuggestions(content) { const suggestions = []; const lines = content.split('\n'); let inSuggestionsSection = false; lines.forEach(line => { const lowerLine = line.toLowerCase(); if (lowerLine.includes('suggestion') || lowerLine.includes('recommend')) { inSuggestionsSection = true; } else if (line.startsWith('#') || line.trim() === '') { inSuggestionsSection = false; } if (inSuggestionsSection && line.trim().startsWith('-')) { suggestions.push(line.trim().substring(1).trim()); } else if (lowerLine.includes('should') || lowerLine.includes('consider')) { suggestions.push(line.trim()); } }); return suggestions; } } //# sourceMappingURL=sub-agent-executor.js.map