#!/usr/bin/env node "use strict"; const fs = require("node:fs"); const path = require("node:path"); const { execFileSync } = require("node:child_process"); const HOME = process.env.OPENCLAW_STATE_DIR || "/home/node/.openclaw"; const CONFIG = path.join(HOME, "openclaw.json"); const BACKUPS = path.join(HOME, "self-edit", "backups"); function result(text, error = false) { return { text: typeof text === "string" ? text : JSON.stringify(text, null, 2), error }; } function diagnosticsConfigAudit() { try { const raw = fs.readFileSync(CONFIG, "utf8"); const parsed = JSON.parse(raw); const agentCount = Object.keys(parsed.agents?.entries || {}).length; const mcpCount = Object.keys(parsed.mcp?.servers || parsed.mcpServers || {}).length; let validateOutput = "OK"; try { validateOutput = execFileSync("openclaw", ["config", "validate"], { env: process.env, timeout: 15000, stdio: ["ignore", "pipe", "pipe"] }).toString("utf8").trim(); } catch (e) { validateOutput = String(e.stderr || e.message || e); } return result({ config_path: CONFIG, config_size_bytes: raw.length, agents_configured: agentCount, mcp_servers_configured: mcpCount, validation_result: validateOutput }); } catch (err) { return result(`Config-Audit fehlgeschlagen: ${err.message}`, true); } } function diagnosticsBackupHistory() { try { let files = []; if (fs.existsSync(BACKUPS)) { files = fs.readdirSync(BACKUPS).map(f => { const full = path.join(BACKUPS, f); const stat = fs.statSync(full); return { name: f, size_bytes: stat.size, mtime: stat.mtime }; }); } const rootBackups = fs.readdirSync(HOME).filter(f => f.startsWith("openclaw.json.bak")).map(f => { const full = path.join(HOME, f); const stat = fs.statSync(full); return { name: f, size_bytes: stat.size, mtime: stat.mtime }; }); return result({ self_edit_backups: files.slice(-10), root_backups: rootBackups.slice(-10) }); } catch (err) { return result(`Backup-History konnte nicht gelesen werden: ${err.message}`, true); } } function diagnosticsSystemSummary() { try { const memory = process.memoryUsage(); return result({ node_version: process.version, platform: process.platform, arch: process.arch, uptime_seconds: Math.round(process.uptime()), memory_heap_used_mb: Math.round(memory.heapUsed / 1024 / 1024), memory_heap_total_mb: Math.round(memory.heapTotal / 1024 / 1024), rss_mb: Math.round(memory.rss / 1024 / 1024) }); } catch (err) { return result(`System-Summary fehlgeschlagen: ${err.message}`, true); } } const TOOLS = [ { name: "diagnostics_config_audit", description: "Auditiert die aktuelle OpenClaw-Konfiguration, Agenten und MCP-Server. Lesend.", inputSchema: { type: "object", properties: {}, additionalProperties: false } }, { name: "diagnostics_backup_history", description: "Listet vorhandene Konfigurations-Backups und Revisionshistorie auf. Lesend.", inputSchema: { type: "object", properties: {}, additionalProperties: false } }, { name: "diagnostics_system_summary", description: "Liefert eine Laufzeit- und Speicherübersicht des Node.js-Prozesses. Lesend.", inputSchema: { type: "object", properties: {}, additionalProperties: false } } ]; function call(name, args) { try { if (name === "diagnostics_config_audit") return diagnosticsConfigAudit(); if (name === "diagnostics_backup_history") return diagnosticsBackupHistory(); if (name === "diagnostics_system_summary") return diagnosticsSystemSummary(); return result("Unbekanntes Werkzeug.", true); } catch (err) { return result(String(err.message || err), true); } } function send(id, value) { process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, result: value }) + "\n"); } function reply(id, value) { send(id, { content: [{ type: "text", text: value.text }], isError: value.error }); } let buffer = ""; process.stdin.setEncoding("utf8"); process.stdin.on("data", (chunk) => { buffer += chunk; let end; while ((end = buffer.indexOf("\n")) >= 0) { const line = buffer.slice(0, end).trim(); buffer = buffer.slice(end + 1); if (!line) continue; let message; try { message = JSON.parse(line); } catch { continue; } if (message.method === "initialize") send(message.id, { protocolVersion: message.params?.protocolVersion || "2024-11-05", capabilities: { tools: {} }, serverInfo: { name: "openclaw-diagnostics", version: "1.0.0" } }); else if (message.method === "tools/list") send(message.id, { tools: TOOLS }); else if (message.method === "tools/call") reply(message.id, call(message.params?.name, message.params?.arguments || {})); } });