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
348 lines • 14.9 kB
JavaScript
/**
* AI Feedback Analytics Engine - Phase 2 Implementation
*
* Revolutionary analytics engine that transforms raw feedback data into actionable insights
* for continuous improvement of AI-Debug tools and user experience.
*/
/**
* Advanced Analytics Engine for AI Feedback Intelligence
*/
export class AIFeedbackAnalyticsEngine {
TREND_ANALYSIS_WINDOW = 7 * 24 * 60 * 60 * 1000; // 7 days
ANOMALY_THRESHOLD = 2; // Standard deviations
MIN_DATA_POINTS = 5;
/**
* Generate comprehensive intelligence report from feedback data
*/
async generateIntelligenceReport(feedbackData, timeRange) {
const now = Date.now();
const range = timeRange || {
start: now - (30 * 24 * 60 * 60 * 1000), // Last 30 days
end: now
};
// Filter data to time range
const filteredData = feedbackData.filter(entry => entry.timestamp >= range.start && entry.timestamp <= range.end);
if (filteredData.length < this.MIN_DATA_POINTS) {
return this.generateMinimalReport(range, filteredData.length);
}
// Parallel analysis for performance
const [trends, anomalies, patterns, predictions] = await Promise.all([
this.analyzeTrends(filteredData),
this.detectAnomalies(filteredData),
this.identifyPatterns(filteredData),
this.generatePredictions(filteredData)
]);
const actionableInsights = this.generateActionableInsights(trends, anomalies, patterns, predictions);
return {
generatedAt: now,
timeRange: range,
summary: {
totalFeedback: filteredData.length,
avgSatisfaction: this.calculateAverageSatisfaction(filteredData),
trendDirection: this.determineTrendDirection(trends),
criticalInsights: actionableInsights.filter(i => i.priority === 'critical').length
},
trends,
anomalies,
patterns,
predictions,
actionableInsights
};
}
/**
* Analyze satisfaction and performance trends over time
*/
async analyzeTrends(data) {
const trends = [];
// Satisfaction trend analysis
const satisfactionTrend = this.calculateTrend(data.map(d => ({
timestamp: d.timestamp,
value: d.userExperience?.satisfaction || 0
})), 'satisfaction');
if (satisfactionTrend)
trends.push(satisfactionTrend);
// Efficiency trend analysis
const efficiencyTrend = this.calculateTrend(data.map(d => ({
timestamp: d.timestamp,
value: d.userExperience?.efficiency || 0
})), 'efficiency');
if (efficiencyTrend)
trends.push(efficiencyTrend);
// Tool usage trend analysis
const toolUsageTrend = this.analyzeToolUsage(data);
if (toolUsageTrend)
trends.push(toolUsageTrend);
return trends;
}
/**
* Detect anomalies in feedback patterns
*/
async detectAnomalies(data) {
const anomalies = [];
// Satisfaction anomaly detection
const satisfactionData = data.map(d => d.userExperience?.satisfaction || 0);
const satisfactionAnomalies = this.detectStatisticalAnomalies(satisfactionData, data.map(d => d.timestamp), 'satisfaction_spike');
anomalies.push(...satisfactionAnomalies);
// Error clustering detection
const errorClusters = this.detectErrorClusters(data);
anomalies.push(...errorClusters);
// Usage pattern anomalies
const usageAnomalies = this.detectUsagePatternAnomalies(data);
anomalies.push(...usageAnomalies);
return anomalies;
}
/**
* Identify recurring patterns and insights
*/
async identifyPatterns(data) {
const patterns = [];
// User behavior patterns
const behaviorPatterns = this.analyzeBehaviorPatterns(data);
patterns.push(...behaviorPatterns);
// Tool performance patterns
const toolPatterns = this.analyzeToolPerformancePatterns(data);
patterns.push(...toolPatterns);
// Workflow efficiency patterns
const workflowPatterns = this.analyzeWorkflowPatterns(data);
patterns.push(...workflowPatterns);
return patterns;
}
/**
* Generate predictive analysis for future trends
*/
async generatePredictions(data) {
const predictions = [];
// Satisfaction prediction
const satisfactionPrediction = this.predictMetric(data.map(d => ({ timestamp: d.timestamp, value: d.userExperience?.satisfaction || 0 })), 'satisfaction');
if (satisfactionPrediction)
predictions.push(satisfactionPrediction);
// Usage growth prediction
const usagePrediction = this.predictUsageGrowth(data);
if (usagePrediction)
predictions.push(usagePrediction);
return predictions;
}
/**
* Calculate trend for a specific metric
*/
calculateTrend(dataPoints, metricName) {
if (dataPoints.length < this.MIN_DATA_POINTS)
return null;
// Sort by timestamp
const sorted = dataPoints.sort((a, b) => a.timestamp - b.timestamp);
// Calculate linear regression
const n = sorted.length;
const sumX = sorted.reduce((sum, p, i) => sum + i, 0);
const sumY = sorted.reduce((sum, p) => sum + p.value, 0);
const sumXY = sorted.reduce((sum, p, i) => sum + (i * p.value), 0);
const sumXX = sorted.reduce((sum, p, i) => sum + (i * i), 0);
const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
const direction = slope > 0.1 ? 'improving' : slope < -0.1 ? 'declining' : 'stable';
// Calculate R-squared for confidence
const meanY = sumY / n;
const predictedY = sorted.map((p, i) => (slope * i) + (sumY - slope * sumX) / n);
const ssRes = sorted.reduce((sum, p, i) => sum + Math.pow(p.value - predictedY[i], 2), 0);
const ssTot = sorted.reduce((sum, p) => sum + Math.pow(p.value - meanY, 2), 0);
const rSquared = 1 - (ssRes / ssTot);
const insights = this.generateTrendInsights(metricName, direction, slope, rSquared);
return {
timeframe: `${Math.round((sorted[sorted.length - 1].timestamp - sorted[0].timestamp) / (24 * 60 * 60 * 1000))} days`,
direction,
confidence: Math.max(0, Math.min(1, rSquared)),
dataPoints: sorted,
insights
};
}
/**
* Detect statistical anomalies using standard deviation
*/
detectStatisticalAnomalies(values, timestamps, type) {
if (values.length < this.MIN_DATA_POINTS)
return [];
const mean = values.reduce((sum, v) => sum + v, 0) / values.length;
const stdDev = Math.sqrt(values.reduce((sum, v) => sum + Math.pow(v - mean, 2), 0) / values.length);
const anomalies = [];
values.forEach((value, index) => {
const zScore = Math.abs(value - mean) / stdDev;
if (zScore > this.ANOMALY_THRESHOLD) {
const severity = zScore > 3 ? 'critical' : zScore > 2.5 ? 'high' : 'medium';
anomalies.push({
anomalyType: type,
severity: severity,
detectedAt: timestamps[index],
description: `${type} detected: value ${value.toFixed(2)} deviates ${zScore.toFixed(2)} standard deviations from mean ${mean.toFixed(2)}`,
dataPoints: [{ timestamp: timestamps[index], value, expected: mean }],
suggestedActions: this.generateAnomalyActions(type, severity, value, mean)
});
}
});
return anomalies;
}
/**
* Analyze tool usage patterns and trends
*/
analyzeToolUsage(data) {
const toolUsage = new Map();
data.forEach(entry => {
if (entry.toolsUsed && Array.isArray(entry.toolsUsed)) {
entry.toolsUsed.forEach((tool) => {
toolUsage.set(tool, (toolUsage.get(tool) || 0) + 1);
});
}
});
if (toolUsage.size === 0)
return null;
const sortedTools = Array.from(toolUsage.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 5);
const insights = [
`Most used tool: ${sortedTools[0][0]} (${sortedTools[0][1]} times)`,
`Tool diversity: ${toolUsage.size} different tools used`,
`Usage concentration: Top 3 tools account for ${Math.round((sortedTools.slice(0, 3).reduce((sum, [, count]) => sum + count, 0) / data.length) * 100)}% of usage`
];
return {
timeframe: 'current_period',
direction: 'stable',
confidence: 0.8,
dataPoints: sortedTools.map(([tool, count], index) => ({
timestamp: Date.now() - (index * 1000),
value: count,
context: tool
})),
insights
};
}
/**
* Generate actionable insights from all analyses
*/
generateActionableInsights(trends, anomalies, patterns, predictions) {
const insights = [];
// Critical anomalies become critical insights
anomalies.filter(a => a.severity === 'critical').forEach(anomaly => {
insights.push({
priority: 'critical',
category: 'anomaly_detection',
insight: anomaly.description,
impact: 'High - immediate attention required',
effort: 'Medium',
recommendations: anomaly.suggestedActions
});
});
// Declining trends become high priority insights
trends.filter(t => t.direction === 'declining' && t.confidence > 0.6).forEach(trend => {
insights.push({
priority: 'high',
category: 'performance_trend',
insight: `Declining trend detected with ${Math.round(trend.confidence * 100)}% confidence`,
impact: 'Medium - user satisfaction at risk',
effort: 'Medium',
recommendations: trend.insights
});
});
// High-impact patterns become medium priority insights
patterns.filter(p => p.impact === 'negative' && p.frequency > 3).forEach(pattern => {
insights.push({
priority: 'medium',
category: 'pattern_analysis',
insight: pattern.description,
impact: 'Medium - recurring issue affecting multiple users',
effort: 'Low',
recommendations: pattern.recommendations
});
});
return insights.sort((a, b) => {
const priorityOrder = { critical: 4, high: 3, medium: 2, low: 1 };
return priorityOrder[b.priority] - priorityOrder[a.priority];
});
}
// Helper methods
generateMinimalReport(range, dataCount) {
return {
generatedAt: Date.now(),
timeRange: range,
summary: {
totalFeedback: dataCount,
avgSatisfaction: 0,
trendDirection: 'stable',
criticalInsights: 0
},
trends: [],
anomalies: [],
patterns: [],
predictions: [],
actionableInsights: [{
priority: 'low',
category: 'data_availability',
insight: 'Insufficient data for comprehensive analysis',
impact: 'Low - need more feedback data',
effort: 'Low',
recommendations: ['Encourage more user feedback', 'Extend data collection period']
}]
};
}
calculateAverageSatisfaction(data) {
const satisfactionValues = data
.map(d => d.userExperience?.satisfaction)
.filter(s => typeof s === 'number');
return satisfactionValues.length > 0
? satisfactionValues.reduce((sum, s) => sum + s, 0) / satisfactionValues.length
: 0;
}
determineTrendDirection(trends) {
if (trends.length === 0)
return 'stable';
const weightedDirection = trends.reduce((sum, trend) => {
const weight = trend.confidence;
const direction = trend.direction === 'improving' ? 1 : trend.direction === 'declining' ? -1 : 0;
return sum + (direction * weight);
}, 0) / trends.length;
return weightedDirection > 0.1 ? 'improving' : weightedDirection < -0.1 ? 'declining' : 'stable';
}
generateTrendInsights(metric, direction, slope, confidence) {
const insights = [`${metric} trend: ${direction} (slope: ${slope.toFixed(3)})`];
if (confidence > 0.8) {
insights.push('High confidence trend - reliable for predictions');
}
else if (confidence > 0.6) {
insights.push('Moderate confidence trend - monitor closely');
}
else {
insights.push('Low confidence trend - more data needed');
}
if (direction === 'improving') {
insights.push('Positive momentum - continue current strategies');
}
else if (direction === 'declining') {
insights.push('Declining performance - investigate root causes');
}
return insights;
}
generateAnomalyActions(type, severity, value, expected) {
const actions = [];
if (severity === 'critical') {
actions.push('Immediate investigation required');
actions.push('Alert development team');
}
if (type.includes('satisfaction')) {
if (value > expected) {
actions.push('Identify what caused satisfaction spike');
actions.push('Document and replicate successful patterns');
}
else {
actions.push('Investigate satisfaction drop');
actions.push('Review recent changes and issues');
}
}
actions.push('Monitor closely for pattern continuation');
return actions;
}
// Stub methods for pattern analysis (to be expanded)
analyzeBehaviorPatterns(data) { return []; }
analyzeToolPerformancePatterns(data) { return []; }
analyzeWorkflowPatterns(data) { return []; }
detectErrorClusters(data) { return []; }
detectUsagePatternAnomalies(data) { return []; }
predictMetric(data, metric) { return null; }
predictUsageGrowth(data) { return null; }
}
//# sourceMappingURL=ai-feedback-analytics-engine.js.map