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

203 lines • 7.49 kB
import fetch from 'node-fetch'; export class CloudAIService { apiEndpoint; apiKey; constructor(apiEndpoint) { this.apiEndpoint = apiEndpoint || process.env.AI_DEBUG_API_ENDPOINT || 'https://api.ai-debug.com/v1'; } setApiKey(apiKey) { this.apiKey = apiKey; } async analyzeRootCause(error, state, context) { if (!this.apiKey) { throw new Error('API key required for AI analysis'); } const response = await fetch(`${this.apiEndpoint}/analyze/root-cause`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ error: { message: error.message, stack: error.stack, name: error.name }, state: this.sanitizeState(state), context, timestamp: new Date().toISOString() }) }); if (!response.ok) { throw new Error(`AI analysis failed: ${response.statusText}`); } return await response.json(); } async detectPatterns(events, state) { if (!this.apiKey) { throw new Error('API key required for pattern detection'); } const response = await fetch(`${this.apiEndpoint}/analyze/patterns`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ events: events.slice(-1000), // Last 1000 events state: this.sanitizeState(state), windowSize: '5m' }) }); if (!response.ok) { throw new Error(`Pattern detection failed: ${response.statusText}`); } return await response.json(); } async analyzePerformance(metrics, componentTree, renderTimes) { if (!this.apiKey) { throw new Error('API key required for performance analysis'); } const response = await fetch(`${this.apiEndpoint}/analyze/performance`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ metrics, componentTree, renderTimes, timestamp: new Date().toISOString() }) }); if (!response.ok) { throw new Error(`Performance analysis failed: ${response.statusText}`); } return await response.json(); } async suggestFixes(issue, codeContext, stackTrace) { if (!this.apiKey) { throw new Error('API key required for fix suggestions'); } const response = await fetch(`${this.apiEndpoint}/suggest/fixes`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ issue, codeContext, stackTrace, language: 'typescript', // Could be detected framework: 'phoenix-liveview' // Could be detected }) }); if (!response.ok) { throw new Error(`Fix suggestion failed: ${response.statusText}`); } return await response.json(); } sanitizeState(state) { // Remove sensitive data before sending to cloud const sanitized = { ...state }; // Remove potential secrets const sensitiveKeys = ['password', 'token', 'secret', 'key', 'auth']; const removeSensitive = (obj) => { if (typeof obj !== 'object' || obj === null) return obj; const cleaned = Array.isArray(obj) ? [] : {}; for (const [key, value] of Object.entries(obj)) { if (sensitiveKeys.some(sensitive => key.toLowerCase().includes(sensitive))) { cleaned[key] = '[REDACTED]'; } else if (typeof value === 'object') { cleaned[key] = removeSensitive(value); } else { cleaned[key] = value; } } return cleaned; }; return removeSensitive(sanitized); } async analyzeEvents(events) { if (!this.apiKey) { throw new Error('API key required for event analysis'); } const response = await fetch(`${this.apiEndpoint}/analyze/events`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ events: events.slice(-100), // Last 100 events timestamp: new Date().toISOString() }) }); if (!response.ok) { throw new Error(`Event analysis failed: ${response.statusText}`); } return await response.json(); } async comprehensiveAnalysis(events, pageState, analysisType) { if (!this.apiKey) { throw new Error('API key required for comprehensive analysis'); } const response = await fetch(`${this.apiEndpoint}/analyze/comprehensive`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ events: events.slice(-500), // Last 500 events pageState: this.sanitizePageState(pageState), analysisType, timestamp: new Date().toISOString() }) }); if (!response.ok) { throw new Error(`Comprehensive analysis failed: ${response.statusText}`); } return await response.json(); } async enhanceReport(basicReport, events) { if (!this.apiKey) { throw new Error('API key required for report enhancement'); } const response = await fetch(`${this.apiEndpoint}/enhance/report`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` }, body: JSON.stringify({ basicReport, events: events.slice(-200), // Last 200 events timestamp: new Date().toISOString() }) }); if (!response.ok) { throw new Error(`Report enhancement failed: ${response.statusText}`); } return await response.json(); } sanitizePageState(pageState) { const { localStorage, sessionStorage, cookies, ...rest } = pageState; return { ...rest, localStorage: this.sanitizeState({ localStorage }).localStorage, sessionStorage: this.sanitizeState({ sessionStorage }).sessionStorage, cookies: cookies.map((cookie) => ({ ...cookie, value: '[REDACTED]' })) }; } } //# sourceMappingURL=cloud-ai-service.js.map