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

355 lines • 14.2 kB
/** * AI Assistant Bridge - Seamless integration for AI models * * This bridge provides a high-level interface for AI models to use AI-Debug tools * without needing detailed knowledge of individual tool parameters or workflows. */ import { AIToolDiscovery } from '../discovery/ai-tool-discovery.js'; export class AIAssistantBridge { discovery; conversationContext = new Map(); constructor() { this.discovery = new AIToolDiscovery(); } /** * Primary method for AI models to get comprehensive assistance */ async analyzeUserRequest(request) { const understanding = await this.analyzeUserIntent(request.userInput, request.context); const recommendations = await this.generateRecommendations(understanding, request.context); const executionPlan = await this.createExecutionPlan(recommendations, request.preferences); const guidance = await this.generateGuidance(executionPlan, understanding); // Store context for follow-up requests if (request.context?.sessionId) { this.conversationContext.set(request.context.sessionId, { lastRequest: request, understanding, recommendations, timestamp: Date.now() }); } return { understanding, recommendations, executionPlan, guidance }; } /** * Simplified method for quick tool suggestions */ async getQuickSuggestions(userInput) { const suggestions = this.discovery.suggestToolsForInput(userInput); if (suggestions.length === 0) { return { topTool: 'inject_debugging', parameters: { url: this.extractUrl(userInput) || 'https://example.com' }, reasoning: 'Default debugging session to start analysis', confidence: 0.5 }; } const topSuggestion = suggestions[0]; return { topTool: topSuggestion.toolName, parameters: topSuggestion.suggestedParameters, reasoning: topSuggestion.reasoning, confidence: topSuggestion.confidence }; } /** * Get workflow for common debugging patterns */ async getDebugWorkflow(scenario) { const workflows = this.discovery.suggestWorkflow(scenario); if (workflows.length === 0) { // Default comprehensive debugging workflow return { workflow: [ { step: 1, tool: 'inject_debugging', description: 'Establish debugging session', parameters: { url: this.extractUrl(scenario) || '{{USER_URL}}' } }, { step: 2, tool: 'take_screenshot', description: 'Document current visual state', parameters: { sessionId: '{{SESSION_ID}}', fullPage: true } }, { step: 3, tool: 'run_audit', description: 'Analyze performance and quality', parameters: { sessionId: '{{SESSION_ID}}' } } ], description: 'Standard debugging workflow for comprehensive analysis' }; } const bestWorkflow = workflows[0]; return { workflow: bestWorkflow.toolSequence.map((tool, index) => ({ step: index + 1, tool, description: this.getToolDescription(tool), parameters: this.getDefaultParameters(tool, scenario) })), description: bestWorkflow.reasoning }; } /** * Smart parameter extraction from user input */ extractParameters(userInput, toolName) { const parameters = {}; const toolMetadata = this.discovery.getToolMetadata(toolName); if (!toolMetadata) return parameters; // Extract URL const url = this.extractUrl(userInput); if (url && toolMetadata.requiredParameters.some(p => p.name === 'url')) { parameters.url = url; } // Extract CSS selectors const selector = this.extractSelector(userInput); if (selector && toolMetadata.optionalParameters?.some(p => p.name === 'selector')) { parameters.selector = selector; } // Extract text for typing actions const quotedText = userInput.match(/"([^"]+)"/); if (quotedText && toolMetadata.optionalParameters?.some(p => p.name === 'text')) { parameters.text = quotedText[1]; } // Extract action types if (toolName === 'simulate_user_action') { if (userInput.includes('click')) parameters.action = 'click'; else if (userInput.includes('type') || userInput.includes('fill')) parameters.action = 'type'; else if (userInput.includes('scroll')) parameters.action = 'scroll'; else if (userInput.includes('wait')) parameters.action = 'wait'; } return parameters; } /** * Get contextual examples for AI models */ getContextualExamples(userInput) { const examples = []; if (userInput.toLowerCase().includes('slow') || userInput.toLowerCase().includes('performance')) { examples.push({ scenario: 'Performance debugging', toolSequence: ['inject_debugging', 'run_audit', 'take_screenshot'], explanation: 'Start session, analyze performance metrics, document visual state' }); } if (userInput.toLowerCase().includes('form') || userInput.toLowerCase().includes('submit')) { examples.push({ scenario: 'Form testing', toolSequence: ['inject_debugging', 'take_screenshot', 'simulate_user_action', 'simulate_user_action', 'simulate_user_action'], explanation: 'Start session, document form, fill fields, submit, check result' }); } if (userInput.toLowerCase().includes('look') || userInput.toLowerCase().includes('appearance')) { examples.push({ scenario: 'Visual analysis', toolSequence: ['inject_debugging', 'take_screenshot'], explanation: 'Start session and capture current visual state' }); } return examples; } // Private helper methods async analyzeUserIntent(userInput, context) { const extractedInfo = {}; // Extract key information extractedInfo.url = this.extractUrl(userInput); extractedInfo.selectors = this.extractSelectors(userInput); extractedInfo.actions = this.extractActions(userInput); extractedInfo.goals = this.extractGoals(userInput); // Determine user intent let userIntent = 'general_debugging'; let confidence = 0.7; if (userInput.toLowerCase().includes('debug')) { userIntent = 'debugging_analysis'; confidence = 0.9; } else if (userInput.toLowerCase().includes('test')) { userIntent = 'functionality_testing'; confidence = 0.9; } else if (userInput.toLowerCase().includes('performance') || userInput.toLowerCase().includes('slow')) { userIntent = 'performance_analysis'; confidence = 0.9; } else if (userInput.toLowerCase().includes('look') || userInput.toLowerCase().includes('screenshot')) { userIntent = 'visual_analysis'; confidence = 0.9; } return { userIntent, extractedInfo, confidence }; } async generateRecommendations(understanding, context) { const suggestions = this.discovery.suggestToolsForInput(understanding.userIntent); const workflows = this.discovery.suggestWorkflow(understanding.userIntent); const toolSequence = suggestions.slice(0, 3).map(suggestion => ({ tool: suggestion.toolName, parameters: { ...suggestion.suggestedParameters, ...understanding.extractedInfo }, reasoning: suggestion.reasoning, confidence: suggestion.confidence })); return { toolSequence, expectedOutcome: workflows[0]?.expectedOutcome || 'Comprehensive analysis completed', alternatives: workflows.slice(1).map(w => ({ description: w.reasoning, toolSequence: w.toolSequence })) }; } async createExecutionPlan(recommendations, preferences) { const steps = recommendations.toolSequence.map((tool, index) => ({ stepNumber: index + 1, action: this.getStepDescription(tool.tool, tool.parameters), tool: tool.tool, parameters: tool.parameters, expectedResult: this.getExpectedResult(tool.tool), fallbackOptions: this.getFallbackOptions(tool.tool) })); return { steps, estimatedDuration: this.estimateDuration(steps.length), complexity: steps.length <= 2 ? 'simple' : steps.length <= 4 ? 'moderate' : 'complex' }; } async generateGuidance(executionPlan, understanding) { return { whatToExpect: [ 'Debugging session will be established', 'Visual documentation will be captured', 'Analysis results will be provided', 'Recommendations will be generated' ], potentialIssues: [ 'URL might not be accessible', 'Page might take time to load', 'Network connectivity issues', 'Elements might not be found' ], successIndicators: [ 'Session established successfully', 'Screenshots captured clearly', 'Audit results show metrics', 'No error messages in logs' ] }; } // Utility methods extractUrl(text) { const urlMatch = text.match(/https?:\/\/[^\s]+/); return urlMatch ? urlMatch[0] : null; } extractSelector(text) { const selectorMatch = text.match(/[#.][a-zA-Z][a-zA-Z0-9-_]*/); return selectorMatch ? selectorMatch[0] : null; } extractSelectors(text) { const selectors = text.match(/[#.][a-zA-Z][a-zA-Z0-9-_]*/g) || []; return selectors; } extractActions(text) { const actions = []; if (text.includes('click')) actions.push('click'); if (text.includes('type') || text.includes('fill')) actions.push('type'); if (text.includes('scroll')) actions.push('scroll'); if (text.includes('submit')) actions.push('submit'); return actions; } extractGoals(text) { const goals = []; if (text.includes('debug')) goals.push('debug'); if (text.includes('test')) goals.push('test'); if (text.includes('analyze')) goals.push('analyze'); if (text.includes('optimize')) goals.push('optimize'); return goals; } getToolDescription(toolName) { const metadata = this.discovery.getToolMetadata(toolName); return metadata?.purpose || `Execute ${toolName}`; } getDefaultParameters(toolName, context) { const params = {}; if (toolName === 'inject_debugging') { params.url = this.extractUrl(context) || '{{USER_URL}}'; } else { params.sessionId = '{{SESSION_ID}}'; } return params; } getStepDescription(toolName, parameters) { switch (toolName) { case 'inject_debugging': return `Start debugging session for ${parameters.url || 'target URL'}`; case 'take_screenshot': return 'Capture current visual state of the page'; case 'run_audit': return 'Perform comprehensive quality and performance analysis'; case 'simulate_user_action': return `Simulate ${parameters.action || 'user interaction'} on the page`; default: return `Execute ${toolName}`; } } getExpectedResult(toolName) { switch (toolName) { case 'inject_debugging': return 'Active debugging session with session ID'; case 'take_screenshot': return 'Screenshot image of current page state'; case 'run_audit': return 'Performance scores and optimization recommendations'; case 'simulate_user_action': return 'Confirmation of successful interaction'; default: return 'Tool execution result'; } } getFallbackOptions(toolName) { switch (toolName) { case 'inject_debugging': return ['Try alternative URL format', 'Check network connectivity', 'Verify URL accessibility']; case 'take_screenshot': return ['Wait for page load', 'Try smaller viewport', 'Check element visibility']; case 'run_audit': return ['Run specific audit categories', 'Try lighter analysis', 'Check page stability']; default: return ['Retry with different parameters', 'Check prerequisites', 'Contact support']; } } estimateDuration(stepCount) { if (stepCount <= 2) return '30-60 seconds'; if (stepCount <= 4) return '1-2 minutes'; return '2-5 minutes'; } } //# sourceMappingURL=ai-assistant-bridge.js.map