#!/usr/bin/env node // Bookworm Smart Assistant v6.6 — 可视化中控 API 服务器 // 零 npm 依赖,纯 Node.js 内置模块 // 启动: node dashboard-server.js // 访问: http://localhost:3210 const http = require('http'); const fs = require('fs'); const path = require('path'); const { exec } = require('child_process'); const url = require('url'); // ─── 配置 ─────────────────────────────────────────────── const PORT = parseInt(process.env.DASHBOARD_PORT, 10) || 3210; const SCRIPT_CACHE_TTL = 15000; const SCRIPT_TIMEOUT = 120000; // ─── 根目录检测 ───────────────────────────────────────── function detectClaudeRoot() { if (process.env.CLAUDE_HOME) return process.env.CLAUDE_HOME; if (process.env.CLAUDE_ROOT) return process.env.CLAUDE_ROOT; const selfDir = path.dirname(__filename); if (selfDir.includes('.claude')) { return selfDir.replace(/[/\\]scripts$/, ''); } const IS_WSL = process.platform === 'linux' && fs.existsSync('/mnt/c'); if (IS_WSL) { try { const usersDir = '/mnt/c/Users'; for (const u of fs.readdirSync(usersDir)) { const candidate = path.join(usersDir, u, '.claude'); if (fs.existsSync(candidate)) return candidate; } } catch {} } try { return require('./paths.config.js').PATHS.root; } catch { return (process.env.USERPROFILE || process.env.HOME || '').replace(/\\/g, '/') + '/.claude'; } } const ROOT = detectClaudeRoot(); const SCRIPTS_DIR = path.join(ROOT, 'scripts'); const DEBUG_DIR = path.join(ROOT, 'debug'); const PROJECTS_DIR = path.join(ROOT, 'projects'); // ─── 工具函数 ─────────────────────────────────────────── function readJsonl(filePath) { if (!fs.existsSync(filePath)) return []; try { const content = fs.readFileSync(filePath, 'utf8').trim(); if (!content) return []; const lines = content.split('\n'); const entries = []; for (const line of lines) { if (!line.trim()) continue; try { entries.push(JSON.parse(line)); } catch {} } return entries; } catch { return []; } } function readJsonlByDateRange(prefix, days) { const result = []; const now = new Date(); for (let i = 0; i < days; i++) { const d = new Date(now); d.setDate(d.getDate() - i); const dateStr = d.toISOString().slice(0, 10); const filePath = path.join(DEBUG_DIR, `${prefix}${dateStr}.jsonl`); result.push(...readJsonl(filePath)); } return result; } const _cache = {}; function cachedRunScript(key, script, args = '', ttlMs = SCRIPT_CACHE_TTL) { return new Promise((resolve) => { const now = Date.now(); if (_cache[key] && (now - _cache[key].ts) < ttlMs) { return resolve(_cache[key].data); } if (_cache[key] && _cache[key].pending) { return _cache[key].pending.then(resolve); } const cmd = `node "${path.join(SCRIPTS_DIR, script)}" ${args}`; const pending = new Promise((res) => { exec(cmd, { timeout: SCRIPT_TIMEOUT, encoding: 'utf8', cwd: SCRIPTS_DIR, env: { ...process.env, CLAUDE_HOME: ROOT }, maxBuffer: 10 * 1024 * 1024, }, (err, stdout) => { delete (_cache[key] || {}).pending; if (err) { if (err.killed) { return res({ error: 'timeout', message: `脚本 ${script} 超时 (${SCRIPT_TIMEOUT}ms)` }); } if (stdout) { try { return res(JSON.parse(stdout)); } catch {} } return res({ error: 'script_error', message: err.message || String(err) }); } try { const data = JSON.parse(stdout); _cache[key] = { ts: Date.now(), data }; res(data); } catch { res({ error: 'parse_error', message: '脚本输出不是有效 JSON', raw: (stdout || '').slice(0, 200) }); } }); }); _cache[key] = { ..._cache[key], pending }; pending.then(resolve); }); } function respondJson(res, code, data) { const body = JSON.stringify(data); res.writeHead(code, { 'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', 'Cache-Control': 'no-cache', }); res.end(body); } function respondHtml(res, html) { res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-cache', }); res.end(html); } function findEvolutionLogs() { const entries = []; if (!fs.existsSync(PROJECTS_DIR)) return entries; try { for (const proj of fs.readdirSync(PROJECTS_DIR)) { const evoPath = path.join(PROJECTS_DIR, proj, 'memory', 'evolution-log.jsonl'); entries.push(...readJsonl(evoPath)); } } catch {} entries.sort((a, b) => (a.seq || 0) - (b.seq || 0)); return entries; } function readJsonFile(filePath) { if (!fs.existsSync(filePath)) return null; try { return JSON.parse(fs.readFileSync(filePath, 'utf8')); } catch { return null; } } function today() { return new Date().toISOString().slice(0, 10); } function getLastModified(prefix) { const dateStr = today(); const filePath = path.join(DEBUG_DIR, `${prefix}${dateStr}.jsonl`); try { const stat = fs.statSync(filePath); return stat.mtime.toISOString(); } catch { return null; } } /** 读取 stats-compiled.json 摘要 — v6.6 新增 */ function getSystemStats() { const data = readJsonFile(path.join(ROOT, 'stats-compiled.json')); if (!data || !data.summary) return null; return { version: data.version || 'unknown', skills: data.summary.skills || 0, agents: data.summary.agents || 0, hooks: data.summary.hooksRegistered || 0, hooksTotal: data.summary.hooks || 0, mcp: data.summary.mcpTotal || 0, generated: data.generated || null, }; } // ─── API 路由 ─────────────────────────────────────────── const routes = {}; routes['/'] = (req, res) => { const htmlPath = path.join(SCRIPTS_DIR, 'dashboard.html'); if (!fs.existsSync(htmlPath)) { res.writeHead(404, { 'Content-Type': 'text/plain' }); res.end('dashboard.html not found'); return; } const html = fs.readFileSync(htmlPath, 'utf8'); respondHtml(res, html); }; routes['/api/health'] = async (req, res) => { const data = await cachedRunScript('health', 'health-check.js', '--json'); respondJson(res, 200, data); }; routes['/api/disk'] = async (req, res) => { const data = await cachedRunScript('disk', 'auto-cleanup.js', '--report'); respondJson(res, 200, data); }; routes['/api/weekly'] = async (req, res) => { const data = await cachedRunScript('weekly', 'weekly-report.js', '--json'); respondJson(res, 200, data); }; routes['/api/dashboard'] = async (req, res, query) => { const range = parseInt(query.range, 10) || 7; const data = await cachedRunScript(`dashboard-${range}`, 'dashboard.js', `--json --range ${range}`); respondJson(res, 200, data); }; routes['/api/activity'] = (req, res, query) => { const days = Math.min(parseInt(query.days, 10) || 7, 90); const entries = readJsonlByDateRange('activity-', days); respondJson(res, 200, entries); }; routes['/api/security'] = (req, res, query) => { const days = Math.min(parseInt(query.days, 10) || 7, 90); const entries = readJsonlByDateRange('security-', days); respondJson(res, 200, entries); }; routes['/api/compliance'] = (req, res, query) => { const days = Math.min(parseInt(query.days, 10) || 7, 90); const entries = readJsonlByDateRange('compliance-', days); respondJson(res, 200, entries); }; routes['/api/evolution'] = (req, res) => { const entries = findEvolutionLogs(); respondJson(res, 200, entries); }; routes['/api/skills'] = (req, res) => { const data = readJsonFile(path.join(ROOT, 'skills-index.json')); respondJson(res, 200, data || { error: 'skills-index.json not found' }); }; routes['/api/route-feedback'] = (req, res) => { const entries = readJsonl(path.join(DEBUG_DIR, 'route-feedback.jsonl')); respondJson(res, 200, entries); }; routes['/api/weights'] = (req, res) => { const data = readJsonFile(path.join(DEBUG_DIR, 'route-weights.json')); respondJson(res, 200, data || {}); }; // v6.6: /api/status 新增 system 字段,从 stats-compiled.json 读取真实计数 routes['/api/status'] = (req, res) => { const now = new Date().toISOString(); const activityMtime = getLastModified('activity-'); const securityMtime = getLastModified('security-'); const complianceMtime = getLastModified('compliance-'); let sessionInfo = null; try { sessionInfo = JSON.parse(fs.readFileSync(path.join(DEBUG_DIR, 'session-active.lock'), 'utf8')); } catch {} function getFileMtime(filePath) { try { return fs.statSync(filePath).mtime.toISOString(); } catch { return null; } } const detectionMtime = getFileMtime(path.join(DEBUG_DIR, 'detection-stats.json')); const skillCorrMtime = getFileMtime(path.join(DEBUG_DIR, 'skill-outcome-correlation.json')); const outcomeAggMtime = getFileMtime(path.join(DEBUG_DIR, 'outcome-aggregation.json')); const traceMtime = getLastModified('trace-'); const remediationMtime = getFileMtime(path.join(DEBUG_DIR, 'remediation-log.jsonl')); respondJson(res, 200, { serverTime: now, uptime: process.uptime(), cacheTTL: SCRIPT_CACHE_TTL, pollInterval: 30, system: getSystemStats(), dataSources: { activity: { lastModified: activityMtime }, security: { lastModified: securityMtime }, compliance: { lastModified: complianceMtime }, detectionStats: { lastModified: detectionMtime }, skillCorrelation: { lastModified: skillCorrMtime }, outcomeAggregation: { lastModified: outcomeAggMtime }, traces: { lastModified: traceMtime }, remediations: { lastModified: remediationMtime }, }, session: sessionInfo, }); }; routes['/api/detection-stats'] = (req, res) => { const data = readJsonFile(path.join(DEBUG_DIR, 'detection-stats.json')); respondJson(res, 200, data || { error: 'detection-stats.json not found' }); }; routes['/api/skill-correlation'] = (req, res) => { const data = readJsonFile(path.join(DEBUG_DIR, 'skill-outcome-correlation.json')); respondJson(res, 200, data || { error: 'skill-outcome-correlation.json not found' }); }; routes['/api/outcome-aggregation'] = (req, res) => { const data = readJsonFile(path.join(DEBUG_DIR, 'outcome-aggregation.json')); respondJson(res, 200, data || { error: 'outcome-aggregation.json not found' }); }; routes['/api/traces'] = (req, res, query) => { const days = Math.min(parseInt(query.days, 10) || 7, 90); const entries = readJsonlByDateRange('trace-', days); respondJson(res, 200, entries); }; routes['/api/remediations'] = (req, res) => { const entries = readJsonl(path.join(DEBUG_DIR, 'remediation-log.jsonl')); respondJson(res, 200, entries); }; routes['/api/config-validate'] = async (req, res) => { const data = await cachedRunScript('configValidate', 'config-validator.js', '--json', 60000); respondJson(res, 200, data); }; routes['/api/health-history'] = (req, res) => { let data = readJsonFile(path.join(DEBUG_DIR, 'health-weight-history.json')); if (Array.isArray(data)) { data = data.slice(-30); } respondJson(res, 200, data || []); }; // ─── SSE 实时推送 ──────────────────────────────────────── const sseClients = new Set(); let lastMtimes = {}; function collectMtimes() { const mtimes = {}; const files = [ ['detection', path.join(DEBUG_DIR, 'detection-stats.json')], ['skillCorr', path.join(DEBUG_DIR, 'skill-outcome-correlation.json')], ['outcome', path.join(DEBUG_DIR, 'outcome-aggregation.json')], ['remediation', path.join(DEBUG_DIR, 'remediation-log.jsonl')], ['weightHistory', path.join(DEBUG_DIR, 'health-weight-history.json')], ]; const dateStr = today(); const dateFiles = [ ['activity', path.join(DEBUG_DIR, `activity-${dateStr}.jsonl`)], ['security', path.join(DEBUG_DIR, `security-${dateStr}.jsonl`)], ['compliance', path.join(DEBUG_DIR, `compliance-${dateStr}.jsonl`)], ['trace', path.join(DEBUG_DIR, `trace-${dateStr}.jsonl`)], ]; for (const [key, fp] of [...files, ...dateFiles]) { try { mtimes[key] = fs.statSync(fp).mtimeMs; } catch { mtimes[key] = 0; } } return mtimes; } function checkAndPush() { if (sseClients.size === 0) return; const current = collectMtimes(); const changed = []; for (const key of Object.keys(current)) { if (current[key] !== (lastMtimes[key] || 0)) changed.push(key); } if (changed.length > 0) { lastMtimes = current; const msg = `data: ${JSON.stringify({ type: 'data-changed', sources: changed, ts: new Date().toISOString() })}\n\n`; for (const client of sseClients) { try { client.write(msg); } catch { sseClients.delete(client); } } } } setInterval(checkAndPush, 5000); lastMtimes = collectMtimes(); routes['/api/events'] = (req, res) => { res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', }); res.write(`data: ${JSON.stringify({ type: 'connected', ts: new Date().toISOString() })}\n\n`); sseClients.add(res); req.on('close', () => sseClients.delete(res)); }; // ─── HTTP 服务器 ──────────────────────────────────────── const server = http.createServer((req, res) => { if (req.method === 'OPTIONS') { res.writeHead(204, { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', }); res.end(); return; } if (req.method !== 'GET') { respondJson(res, 405, { error: 'Method not allowed' }); return; } const parsed = url.parse(req.url, true); const pathname = parsed.pathname; const query = parsed.query || {}; const handler = routes[pathname]; if (handler) { Promise.resolve(handler(req, res, query)).catch(e => { respondJson(res, 500, { error: 'Internal error', message: e.message }); }); } else { respondJson(res, 404, { error: 'Not found', path: pathname }); } }); server.on('error', (e) => { if (e.code === 'EADDRINUSE') { console.error(`\n端口 ${PORT} 已被占用!`); console.error(` 请设置环境变量: DASHBOARD_PORT=3211 node dashboard-server.js\n`); process.exit(1); } throw e; }); // v6.6: 绑定 127.0.0.1 (仅本机访问) if (require.main === module) { server.listen(PORT, '127.0.0.1', () => { console.log(`\nBookworm 控制中心 v6.6 已启动`); console.log(` 地址: http://localhost:${PORT}`); console.log(` 根目录: ${ROOT}`); console.log(` 缓存 TTL: ${SCRIPT_CACHE_TTL / 1000}s`); console.log(` 按 Ctrl+C 停止\n`); }); } if (typeof module !== 'undefined') { module.exports = { detectClaudeRoot, readJsonl, readJsonlByDateRange, cachedRunScript, respondJson, findEvolutionLogs, readJsonFile, getSystemStats, routes, ROOT, }; }