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
248 lines • 9.33 kB
JavaScript
/**
* Resource Monitor - System Resource Tracking for Multi-Project Sessions
*
* Monitors system resources and provides metrics for quota enforcement
* and optimization recommendations.
*/
import { exec } from 'child_process';
import { promisify } from 'util';
import * as os from 'os';
const execAsync = promisify(exec);
export class ResourceMonitor {
monitoringInterval;
lastMetrics;
constructor(monitoringInterval = 30000) {
this.monitoringInterval = monitoringInterval;
}
/**
* Get current system resource metrics
*/
async getSystemResources() {
try {
const [memoryInfo, cpuInfo, processInfo] = await Promise.all([
this.getMemoryInfo(),
this.getCpuInfo(),
this.getBrowserProcessInfo()
]);
const metrics = {
browserProcesses: processInfo.browserProcesses,
memoryUsageMB: memoryInfo.usedMemoryMB,
cpuUsagePercent: cpuInfo.cpuUsagePercent,
tempDirectorySize: await this.getTempDirectorySize(),
lastActivity: new Date()
};
this.lastMetrics = metrics;
return metrics;
}
catch (error) {
// Return conservative estimates on error
return {
browserProcesses: 0,
memoryUsageMB: 0,
cpuUsagePercent: 0,
tempDirectorySize: 0,
lastActivity: new Date()
};
}
}
/**
* Get resource metrics for specific AI-Debug processes
*/
async getAiDebugResourceUsage() {
try {
const [processInfo, tempSize] = await Promise.all([
this.getAiDebugProcessInfo(),
this.getTempDirectorySize()
]);
return {
browserProcesses: processInfo.browserProcesses,
memoryUsageMB: processInfo.memoryUsageMB,
cpuUsagePercent: processInfo.cpuUsagePercent,
tempDirectorySize: tempSize,
lastActivity: new Date()
};
}
catch (error) {
return {
browserProcesses: 0,
memoryUsageMB: 0,
cpuUsagePercent: 0,
tempDirectorySize: 0,
lastActivity: new Date()
};
}
}
/**
* Get memory usage information
*/
async getMemoryInfo() {
const totalMemory = os.totalmem();
const freeMemory = os.freemem();
const usedMemory = totalMemory - freeMemory;
return {
totalMemoryMB: Math.round(totalMemory / 1024 / 1024),
usedMemoryMB: Math.round(usedMemory / 1024 / 1024),
freeMemoryMB: Math.round(freeMemory / 1024 / 1024)
};
}
/**
* Get CPU usage information
*/
async getCpuInfo() {
const cpus = os.cpus();
const loadAverage = os.loadavg();
// Calculate CPU usage based on load average relative to CPU count
const cpuUsagePercent = Math.min(100, (loadAverage[0] / cpus.length) * 100);
return {
cpuUsagePercent: Math.round(cpuUsagePercent),
loadAverage
};
}
/**
* Get information about browser processes (Chromium, Firefox, etc.)
*/
async getBrowserProcessInfo() {
try {
// Look for browser processes related to debugging
const { stdout } = await execAsync("ps aux | grep -E '(chromium|chrome|firefox|webkit).*ai-debug|playwright' | grep -v grep");
if (!stdout.trim()) {
return { browserProcesses: 0, memoryUsageMB: 0, cpuUsagePercent: 0 };
}
const processes = stdout.trim().split('\n');
let totalMemoryKB = 0;
let totalCpuPercent = 0;
processes.forEach(line => {
const parts = line.trim().split(/\s+/);
if (parts.length >= 6) {
const cpuPercent = parseFloat(parts[2]) || 0;
const memoryPercent = parseFloat(parts[3]) || 0;
totalCpuPercent += cpuPercent;
// Convert memory percentage to KB (approximate)
const totalMemoryKB_system = os.totalmem() / 1024;
totalMemoryKB += (memoryPercent / 100) * totalMemoryKB_system;
}
});
return {
browserProcesses: processes.length,
memoryUsageMB: Math.round(totalMemoryKB / 1024),
cpuUsagePercent: Math.round(totalCpuPercent)
};
}
catch (error) {
return { browserProcesses: 0, memoryUsageMB: 0, cpuUsagePercent: 0 };
}
}
/**
* Get AI-Debug specific process information
*/
async getAiDebugProcessInfo() {
try {
const { stdout } = await execAsync("ps aux | grep -E '(ai-debug|dist/server.js)' | grep -v grep");
if (!stdout.trim()) {
return { browserProcesses: 0, memoryUsageMB: 0, cpuUsagePercent: 0 };
}
const processes = stdout.trim().split('\n');
let totalMemoryKB = 0;
let totalCpuPercent = 0;
let browserProcessCount = 0;
processes.forEach(line => {
const parts = line.trim().split(/\s+/);
if (parts.length >= 6) {
const cpuPercent = parseFloat(parts[2]) || 0;
const memoryPercent = parseFloat(parts[3]) || 0;
totalCpuPercent += cpuPercent;
const totalMemoryKB_system = os.totalmem() / 1024;
totalMemoryKB += (memoryPercent / 100) * totalMemoryKB_system;
// Count browser-related processes
if (line.includes('chromium') || line.includes('chrome') || line.includes('playwright')) {
browserProcessCount++;
}
}
});
return {
browserProcesses: browserProcessCount,
memoryUsageMB: Math.round(totalMemoryKB / 1024),
cpuUsagePercent: Math.round(totalCpuPercent)
};
}
catch (error) {
return { browserProcesses: 0, memoryUsageMB: 0, cpuUsagePercent: 0 };
}
}
/**
* Get temporary directory size
*/
async getTempDirectorySize() {
try {
const { stdout } = await execAsync('du -sm /tmp/ai-debug* 2>/dev/null | awk \'{sum += $1} END {print sum}\'');
return parseInt(stdout.trim()) || 0;
}
catch (error) {
return 0;
}
}
/**
* Check if system is under resource pressure
*/
async isSystemUnderPressure() {
const metrics = await this.getSystemResources();
const memoryInfo = await this.getMemoryInfo();
const memoryUsagePercent = (metrics.memoryUsageMB / memoryInfo.totalMemoryMB) * 100;
const memoryPressure = memoryUsagePercent > 85;
const cpuPressure = metrics.cpuUsagePercent > 80;
const diskPressure = metrics.tempDirectorySize > 1000; // 1GB
let severity = 'low';
if (memoryUsagePercent > 95 || metrics.cpuUsagePercent > 95) {
severity = 'critical';
}
else if (memoryUsagePercent > 90 || metrics.cpuUsagePercent > 90) {
severity = 'high';
}
else if (memoryPressure || cpuPressure || diskPressure) {
severity = 'medium';
}
return {
memoryPressure,
cpuPressure,
diskPressure,
severity
};
}
/**
* Get resource usage trend (requires history)
*/
getResourceTrend() {
if (!this.lastMetrics) {
return 'unknown';
}
// Simple trend analysis would require historical data
// For now, return stable
return 'stable';
}
/**
* Get performance recommendations based on current metrics
*/
async getPerformanceRecommendations() {
const recommendations = [];
const metrics = await this.getSystemResources();
const memoryInfo = await this.getMemoryInfo();
const pressure = await this.isSystemUnderPressure();
if (pressure.memoryPressure) {
recommendations.push('Consider closing idle browser tabs or debugging sessions to free memory');
}
if (pressure.cpuPressure) {
recommendations.push('System CPU usage is high - consider reducing concurrent debugging sessions');
}
if (pressure.diskPressure) {
recommendations.push('Temporary directory size is large - run cleanup to free disk space');
}
if (metrics.browserProcesses > 10) {
recommendations.push(`${metrics.browserProcesses} browser processes detected - consider consolidating debugging sessions`);
}
if (memoryInfo.freeMemoryMB < 500) {
recommendations.push('Low free memory detected - consider increasing system RAM or closing applications');
}
return recommendations;
}
}
//# sourceMappingURL=resource-monitor.js.map