pikakit 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* Dashboard Server -
|
|
3
|
+
* Dashboard Server v6.0 - AutoLearn Precision Learning Engine
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
5
|
+
* Serves real-time metrics from the new v6.0 modules:
|
|
6
|
+
* - metrics-collector.js (18 KPIs)
|
|
7
|
+
* - dashboard-data.js (aggregation)
|
|
8
|
+
* - causality-engine.js (patterns)
|
|
9
|
+
* - reinforcement.js (loop stats)
|
|
10
|
+
* - ab-testing.js (experiment stats)
|
|
11
|
+
* - precision-skill-generator.js (skills)
|
|
10
12
|
*
|
|
11
13
|
* Usage:
|
|
12
14
|
* node dashboard_server.js --start
|
|
@@ -18,6 +20,14 @@ import path from 'path';
|
|
|
18
20
|
import http from 'http';
|
|
19
21
|
import { fileURLToPath } from 'url';
|
|
20
22
|
|
|
23
|
+
// Import v6.0 modules
|
|
24
|
+
import { getDashboardData, getSummary, getMetricHistory } from '../lib/metrics-collector.js';
|
|
25
|
+
import { getFullDashboardData, getKeyTrends, generateAlerts, getGaugeWidgets, getCounterWidgets } from '../lib/dashboard-data.js';
|
|
26
|
+
import { getReinforcementStats } from '../lib/reinforcement.js';
|
|
27
|
+
import { getABTestStats, getActiveTests } from '../lib/ab-testing.js';
|
|
28
|
+
import { getSkillStats, loadAutoSkills } from '../lib/precision-skill-generator.js';
|
|
29
|
+
import { loadCausalPatterns } from '../lib/causality-engine.js';
|
|
30
|
+
|
|
21
31
|
const __filename = fileURLToPath(import.meta.url);
|
|
22
32
|
const __dirname = path.dirname(__filename);
|
|
23
33
|
|
|
@@ -45,62 +55,158 @@ function findProjectRoot() {
|
|
|
45
55
|
}
|
|
46
56
|
|
|
47
57
|
const projectRoot = findProjectRoot();
|
|
48
|
-
const knowledgePath = path.join(projectRoot, '.agent', 'knowledge');
|
|
49
58
|
const dashboardPath = path.join(__dirname, '..', 'dashboard');
|
|
50
59
|
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
if (fs.existsSync(filePath)) {
|
|
56
|
-
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
57
|
-
}
|
|
58
|
-
} catch { }
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// API v6.0 HANDLERS
|
|
62
|
+
// ============================================================================
|
|
61
63
|
|
|
62
|
-
// API handlers
|
|
63
64
|
const api = {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
// Full dashboard data (all metrics aggregated)
|
|
66
|
+
'/api/dashboard': () => {
|
|
67
|
+
try {
|
|
68
|
+
return getFullDashboardData();
|
|
69
|
+
} catch (e) {
|
|
70
|
+
return { error: e.message, version: '6.0.0' };
|
|
71
|
+
}
|
|
67
72
|
},
|
|
68
73
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
74
|
+
// KPIs only
|
|
75
|
+
'/api/kpis': () => {
|
|
76
|
+
try {
|
|
77
|
+
return getDashboardData();
|
|
78
|
+
} catch (e) {
|
|
79
|
+
return { error: e.message };
|
|
80
|
+
}
|
|
72
81
|
},
|
|
73
82
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
83
|
+
// Summary stats
|
|
84
|
+
'/api/summary': () => {
|
|
85
|
+
try {
|
|
86
|
+
return getSummary();
|
|
87
|
+
} catch (e) {
|
|
88
|
+
return { error: e.message };
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
// Trends over time
|
|
93
|
+
'/api/trends': () => {
|
|
94
|
+
try {
|
|
95
|
+
return getKeyTrends();
|
|
96
|
+
} catch (e) {
|
|
97
|
+
return { error: e.message };
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
// Active alerts
|
|
102
|
+
'/api/alerts': () => {
|
|
103
|
+
try {
|
|
104
|
+
return { alerts: generateAlerts() };
|
|
105
|
+
} catch (e) {
|
|
106
|
+
return { alerts: [], error: e.message };
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
// Gauge widget data
|
|
111
|
+
'/api/gauges': () => {
|
|
112
|
+
try {
|
|
113
|
+
return { gauges: getGaugeWidgets() };
|
|
114
|
+
} catch (e) {
|
|
115
|
+
return { gauges: [], error: e.message };
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
// Counter widget data
|
|
120
|
+
'/api/counters': () => {
|
|
121
|
+
try {
|
|
122
|
+
return { counters: getCounterWidgets() };
|
|
123
|
+
} catch (e) {
|
|
124
|
+
return { counters: [], error: e.message };
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
// Reinforcement loop stats
|
|
129
|
+
'/api/reinforcement': () => {
|
|
130
|
+
try {
|
|
131
|
+
return getReinforcementStats();
|
|
132
|
+
} catch (e) {
|
|
133
|
+
return { error: e.message };
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
// A/B testing stats
|
|
138
|
+
'/api/ab-testing': () => {
|
|
139
|
+
try {
|
|
140
|
+
return {
|
|
141
|
+
stats: getABTestStats(),
|
|
142
|
+
active: getActiveTests()
|
|
143
|
+
};
|
|
144
|
+
} catch (e) {
|
|
145
|
+
return { error: e.message };
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
// Auto-generated skills
|
|
150
|
+
'/api/skills': () => {
|
|
151
|
+
try {
|
|
152
|
+
return {
|
|
153
|
+
stats: getSkillStats(),
|
|
154
|
+
skills: loadAutoSkills()
|
|
155
|
+
};
|
|
156
|
+
} catch (e) {
|
|
157
|
+
return { error: e.message };
|
|
158
|
+
}
|
|
77
159
|
},
|
|
78
160
|
|
|
161
|
+
// Causal patterns
|
|
79
162
|
'/api/patterns': () => {
|
|
80
|
-
|
|
81
|
-
|
|
163
|
+
try {
|
|
164
|
+
const patterns = loadCausalPatterns();
|
|
165
|
+
return {
|
|
166
|
+
total: patterns.length,
|
|
167
|
+
patterns: patterns.slice(0, 20) // Limit to 20 most recent
|
|
168
|
+
};
|
|
169
|
+
} catch (e) {
|
|
170
|
+
return { total: 0, patterns: [], error: e.message };
|
|
171
|
+
}
|
|
82
172
|
},
|
|
83
173
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
174
|
+
// Metric history (query param: ?metric=task_success_rate&limit=168)
|
|
175
|
+
'/api/history': (query) => {
|
|
176
|
+
try {
|
|
177
|
+
const metric = query.get('metric') || 'task_success_rate';
|
|
178
|
+
const limit = parseInt(query.get('limit') || '168', 10);
|
|
179
|
+
return {
|
|
180
|
+
metric,
|
|
181
|
+
history: getMetricHistory(metric, limit)
|
|
182
|
+
};
|
|
183
|
+
} catch (e) {
|
|
184
|
+
return { error: e.message };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// ============================================================================
|
|
190
|
+
// LEGACY API COMPATIBILITY (v4.0 endpoints that still work)
|
|
191
|
+
// ============================================================================
|
|
192
|
+
|
|
193
|
+
const legacyApi = {
|
|
194
|
+
'/api/errors': () => ({
|
|
195
|
+
deprecation: 'Use /api/patterns instead',
|
|
196
|
+
redirect: '/api/patterns'
|
|
197
|
+
}),
|
|
198
|
+
'/api/corrections': () => ({
|
|
199
|
+
deprecation: 'Use /api/patterns instead',
|
|
200
|
+
redirect: '/api/patterns'
|
|
201
|
+
}),
|
|
202
|
+
'/api/lessons': () => {
|
|
203
|
+
const filePath = path.join(projectRoot, '.agent', 'knowledge', 'lessons-learned.json');
|
|
204
|
+
try {
|
|
205
|
+
if (fs.existsSync(filePath)) {
|
|
206
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
207
|
+
}
|
|
208
|
+
} catch { }
|
|
209
|
+
return { lessons: [] };
|
|
104
210
|
}
|
|
105
211
|
};
|
|
106
212
|
|
|
@@ -118,7 +224,9 @@ const mimeTypes = {
|
|
|
118
224
|
// Create server
|
|
119
225
|
function createServer(port) {
|
|
120
226
|
const server = http.createServer((req, res) => {
|
|
121
|
-
const
|
|
227
|
+
const urlParts = req.url.split('?');
|
|
228
|
+
const url = urlParts[0];
|
|
229
|
+
const query = new URLSearchParams(urlParts[1] || '');
|
|
122
230
|
|
|
123
231
|
// CORS headers for local development
|
|
124
232
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
@@ -127,14 +235,14 @@ function createServer(port) {
|
|
|
127
235
|
|
|
128
236
|
// Handle API requests
|
|
129
237
|
if (url.startsWith('/api/')) {
|
|
130
|
-
const handler = api[url];
|
|
238
|
+
const handler = api[url] || legacyApi[url];
|
|
131
239
|
if (handler) {
|
|
132
240
|
res.setHeader('Content-Type', 'application/json');
|
|
133
241
|
res.writeHead(200);
|
|
134
|
-
res.end(JSON.stringify(handler()));
|
|
242
|
+
res.end(JSON.stringify(handler(query)));
|
|
135
243
|
} else {
|
|
136
244
|
res.writeHead(404);
|
|
137
|
-
res.end(JSON.stringify({ error: 'Not found' }));
|
|
245
|
+
res.end(JSON.stringify({ error: 'Not found', availableEndpoints: Object.keys(api) }));
|
|
138
246
|
}
|
|
139
247
|
return;
|
|
140
248
|
}
|
|
@@ -163,17 +271,22 @@ function startServer(port = 3030) {
|
|
|
163
271
|
const server = createServer(port);
|
|
164
272
|
|
|
165
273
|
server.listen(port, () => {
|
|
166
|
-
console.log(`${c.cyan}
|
|
167
|
-
console.log(`${c.cyan}║${c.reset} 🧠
|
|
168
|
-
console.log(`${c.cyan}
|
|
274
|
+
console.log(`${c.cyan}╔════════════════════════════════════════════════════╗${c.reset}`);
|
|
275
|
+
console.log(`${c.cyan}║${c.reset} 🧠 AutoLearn v6.0 Dashboard Server ${c.cyan}║${c.reset}`);
|
|
276
|
+
console.log(`${c.cyan}║${c.reset} ${c.green}Precision Learning Engine${c.reset} ${c.cyan}║${c.reset}`);
|
|
277
|
+
console.log(`${c.cyan}╚════════════════════════════════════════════════════╝${c.reset}\n`);
|
|
169
278
|
console.log(`${c.green}✓ Server running at:${c.reset}`);
|
|
170
279
|
console.log(` ${c.bold}http://localhost:${port}${c.reset}\n`);
|
|
171
|
-
console.log(`${c.gray}API Endpoints:${c.reset}`);
|
|
172
|
-
console.log(` GET /api/
|
|
173
|
-
console.log(` GET /api/
|
|
174
|
-
console.log(` GET /api/
|
|
175
|
-
console.log(` GET /api/
|
|
176
|
-
console.log(` GET /api/
|
|
280
|
+
console.log(`${c.gray}API Endpoints (v6.0):${c.reset}`);
|
|
281
|
+
console.log(` GET /api/dashboard - Full dashboard data`);
|
|
282
|
+
console.log(` GET /api/kpis - 18 KPIs`);
|
|
283
|
+
console.log(` GET /api/summary - Summary stats`);
|
|
284
|
+
console.log(` GET /api/trends - Key trends`);
|
|
285
|
+
console.log(` GET /api/alerts - Active alerts`);
|
|
286
|
+
console.log(` GET /api/reinforcement - Reinforcement loop`);
|
|
287
|
+
console.log(` GET /api/ab-testing - A/B experiments`);
|
|
288
|
+
console.log(` GET /api/skills - Auto-generated skills`);
|
|
289
|
+
console.log(` GET /api/patterns - Causal patterns\n`);
|
|
177
290
|
console.log(`${c.yellow}Press Ctrl+C to stop${c.reset}`);
|
|
178
291
|
});
|
|
179
292
|
|
|
@@ -201,19 +314,26 @@ if (args.includes('--start') || args.includes('-s') || args.length === 0 || args
|
|
|
201
314
|
}
|
|
202
315
|
startServer(port);
|
|
203
316
|
} else if (args.includes('--help') || args.includes('-h')) {
|
|
204
|
-
console.log(`${c.cyan}
|
|
317
|
+
console.log(`${c.cyan}AutoLearn v6.0 Dashboard Server${c.reset}
|
|
205
318
|
|
|
206
319
|
${c.bold}Usage:${c.reset}
|
|
207
320
|
node dashboard_server.js Start server (default port 3030)
|
|
208
321
|
node dashboard_server.js --port 8080 Start on custom port
|
|
209
322
|
node dashboard_server.js --help Show this help
|
|
210
323
|
|
|
211
|
-
${c.bold}API Endpoints:${c.reset}
|
|
212
|
-
GET /api/
|
|
213
|
-
GET /api/
|
|
214
|
-
GET /api/
|
|
215
|
-
GET /api/
|
|
216
|
-
GET /api/
|
|
324
|
+
${c.bold}API Endpoints (v6.0):${c.reset}
|
|
325
|
+
GET /api/dashboard - Full dashboard aggregation
|
|
326
|
+
GET /api/kpis - 18 KPIs for Dashboard
|
|
327
|
+
GET /api/summary - Summary statistics
|
|
328
|
+
GET /api/trends - Week-over-week trends
|
|
329
|
+
GET /api/alerts - Active alerts
|
|
330
|
+
GET /api/gauges - Gauge widget data
|
|
331
|
+
GET /api/counters - Counter widget data
|
|
332
|
+
GET /api/reinforcement - Reinforcement loop stats
|
|
333
|
+
GET /api/ab-testing - A/B testing experiments
|
|
334
|
+
GET /api/skills - Auto-generated skills
|
|
335
|
+
GET /api/patterns - Causal patterns
|
|
336
|
+
GET /api/history?metric=X - Metric history
|
|
217
337
|
|
|
218
338
|
${c.bold}Example:${c.reset}
|
|
219
339
|
node dashboard_server.js --port 3030
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pikakit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "Enterprise-grade Agent Skill Manager with Antigravity Skills support, Progressive Disclosure detection, and semantic routing validation",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "pikakit <pikakit@gmail.com>",
|
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Dashboard Server - Local web server for Auto-Learn Dashboard
|
|
4
|
-
*
|
|
5
|
-
* Bundled with PikaKit CLI
|
|
6
|
-
*
|
|
7
|
-
* Serves:
|
|
8
|
-
* - Static dashboard HTML
|
|
9
|
-
* - API endpoints for data
|
|
10
|
-
*
|
|
11
|
-
* Usage:
|
|
12
|
-
* node dashboard_server.js --port 3030
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import fs from 'fs';
|
|
16
|
-
import path from 'path';
|
|
17
|
-
import http from 'http';
|
|
18
|
-
import { fileURLToPath } from 'url';
|
|
19
|
-
|
|
20
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
21
|
-
const __dirname = path.dirname(__filename);
|
|
22
|
-
|
|
23
|
-
// Colors
|
|
24
|
-
const c = {
|
|
25
|
-
reset: '\x1b[0m',
|
|
26
|
-
red: '\x1b[31m',
|
|
27
|
-
green: '\x1b[32m',
|
|
28
|
-
yellow: '\x1b[33m',
|
|
29
|
-
cyan: '\x1b[36m',
|
|
30
|
-
gray: '\x1b[90m',
|
|
31
|
-
bold: '\x1b[1m'
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// Find project root (where .agent folder exists)
|
|
35
|
-
function findProjectRoot() {
|
|
36
|
-
let current = process.cwd();
|
|
37
|
-
while (current !== path.dirname(current)) {
|
|
38
|
-
if (fs.existsSync(path.join(current, '.agent'))) {
|
|
39
|
-
return current;
|
|
40
|
-
}
|
|
41
|
-
current = path.dirname(current);
|
|
42
|
-
}
|
|
43
|
-
return process.cwd();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Find knowledge path - check multiple locations
|
|
47
|
-
function findKnowledgePath() {
|
|
48
|
-
const projectRoot = findProjectRoot();
|
|
49
|
-
const possiblePaths = [
|
|
50
|
-
path.join(projectRoot, '.agent', 'knowledge'),
|
|
51
|
-
path.join(projectRoot, '.agent', 'agentskillskit', '.agent', 'knowledge'),
|
|
52
|
-
];
|
|
53
|
-
|
|
54
|
-
for (const p of possiblePaths) {
|
|
55
|
-
if (fs.existsSync(p)) {
|
|
56
|
-
return p;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Create default if none exists
|
|
61
|
-
const defaultPath = path.join(projectRoot, '.agent', 'knowledge');
|
|
62
|
-
fs.mkdirSync(defaultPath, { recursive: true });
|
|
63
|
-
return defaultPath;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const knowledgePath = findKnowledgePath();
|
|
67
|
-
// Dashboard is in same directory as this server
|
|
68
|
-
const dashboardPath = __dirname;
|
|
69
|
-
|
|
70
|
-
// Load JSON files
|
|
71
|
-
function loadJson(filename) {
|
|
72
|
-
const filePath = path.join(knowledgePath, filename);
|
|
73
|
-
try {
|
|
74
|
-
if (fs.existsSync(filePath)) {
|
|
75
|
-
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
76
|
-
}
|
|
77
|
-
} catch { }
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// API handlers
|
|
82
|
-
const api = {
|
|
83
|
-
'/api/errors': () => {
|
|
84
|
-
const data = loadJson('detected-errors.json');
|
|
85
|
-
return data || { errors: [], totalErrors: 0 };
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
'/api/corrections': () => {
|
|
89
|
-
const data = loadJson('user-corrections.json');
|
|
90
|
-
return data || { corrections: [], totalCorrections: 0 };
|
|
91
|
-
},
|
|
92
|
-
|
|
93
|
-
'/api/lessons': () => {
|
|
94
|
-
const data = loadJson('lessons-learned.json');
|
|
95
|
-
return data || { lessons: [] };
|
|
96
|
-
},
|
|
97
|
-
|
|
98
|
-
'/api/patterns': () => {
|
|
99
|
-
const data = loadJson('patterns.json');
|
|
100
|
-
return data || { errors: {}, corrections: {}, highFrequency: [] };
|
|
101
|
-
},
|
|
102
|
-
|
|
103
|
-
'/api/successes': () => {
|
|
104
|
-
const data = loadJson('successes.json');
|
|
105
|
-
return data || { successes: [], totalSuccesses: 0 };
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
'/api/summary': () => {
|
|
109
|
-
const errors = loadJson('detected-errors.json');
|
|
110
|
-
const corrections = loadJson('user-corrections.json');
|
|
111
|
-
const lessons = loadJson('lessons-learned.json');
|
|
112
|
-
const patterns = loadJson('patterns.json');
|
|
113
|
-
const successes = loadJson('successes.json');
|
|
114
|
-
|
|
115
|
-
// Calculate success/failure ratio
|
|
116
|
-
const totalErrors = errors?.errors?.length || 0;
|
|
117
|
-
const totalCorrections = corrections?.corrections?.length || 0;
|
|
118
|
-
const totalSuccesses = successes?.successes?.length || 0;
|
|
119
|
-
const totalFailures = totalErrors + totalCorrections;
|
|
120
|
-
const total = totalFailures + totalSuccesses;
|
|
121
|
-
|
|
122
|
-
let ratio = 0;
|
|
123
|
-
let status = 'NO_DATA';
|
|
124
|
-
if (total > 0) {
|
|
125
|
-
ratio = Math.round((totalSuccesses / total) * 100) / 100;
|
|
126
|
-
if (ratio > 0.7) status = 'EXCELLENT';
|
|
127
|
-
else if (ratio > 0.5) status = 'GOOD';
|
|
128
|
-
else if (ratio > 0.3) status = 'LEARNING';
|
|
129
|
-
else status = 'NEEDS_ATTENTION';
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Group successes by pattern
|
|
133
|
-
const successesByPattern = {};
|
|
134
|
-
for (const s of (successes?.successes || [])) {
|
|
135
|
-
successesByPattern[s.pattern] = (successesByPattern[s.pattern] || 0) + 1;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return {
|
|
139
|
-
errors: {
|
|
140
|
-
total: totalErrors,
|
|
141
|
-
byType: patterns?.errors?.byType || {},
|
|
142
|
-
bySeverity: patterns?.errors?.bySeverity || {}
|
|
143
|
-
},
|
|
144
|
-
corrections: {
|
|
145
|
-
total: totalCorrections,
|
|
146
|
-
byCategory: patterns?.corrections?.byCategory || {}
|
|
147
|
-
},
|
|
148
|
-
successes: {
|
|
149
|
-
total: totalSuccesses,
|
|
150
|
-
byPattern: successesByPattern
|
|
151
|
-
},
|
|
152
|
-
balance: {
|
|
153
|
-
ratio,
|
|
154
|
-
status,
|
|
155
|
-
failures: totalFailures,
|
|
156
|
-
successes: totalSuccesses
|
|
157
|
-
},
|
|
158
|
-
lessons: lessons?.lessons?.length || 0,
|
|
159
|
-
highFrequency: patterns?.highFrequency || [],
|
|
160
|
-
lastUpdated: patterns?.analyzedAt || null,
|
|
161
|
-
knowledgePath: knowledgePath
|
|
162
|
-
};
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
'/api/trends': () => {
|
|
166
|
-
const errors = loadJson('detected-errors.json');
|
|
167
|
-
const successes = loadJson('successes.json');
|
|
168
|
-
|
|
169
|
-
const errorList = errors?.errors || [];
|
|
170
|
-
const successList = successes?.successes || [];
|
|
171
|
-
|
|
172
|
-
// Calculate trends
|
|
173
|
-
const now = new Date();
|
|
174
|
-
const oneWeekAgo = new Date(now - 7 * 24 * 60 * 60 * 1000);
|
|
175
|
-
const twoWeeksAgo = new Date(now - 14 * 24 * 60 * 60 * 1000);
|
|
176
|
-
|
|
177
|
-
const thisWeekErrors = errorList.filter(e => new Date(e.timestamp) > oneWeekAgo).length;
|
|
178
|
-
const lastWeekErrors = errorList.filter(e => {
|
|
179
|
-
const d = new Date(e.timestamp);
|
|
180
|
-
return d > twoWeeksAgo && d <= oneWeekAgo;
|
|
181
|
-
}).length;
|
|
182
|
-
|
|
183
|
-
const thisWeekSuccesses = successList.filter(s => {
|
|
184
|
-
const d = new Date(s.detectedAt || s.timestamp);
|
|
185
|
-
return d > oneWeekAgo;
|
|
186
|
-
}).length;
|
|
187
|
-
|
|
188
|
-
// Daily breakdown (last 7 days)
|
|
189
|
-
const daily = [];
|
|
190
|
-
for (let i = 6; i >= 0; i--) {
|
|
191
|
-
const date = new Date(now - i * 24 * 60 * 60 * 1000);
|
|
192
|
-
const dateStr = date.toISOString().split('T')[0];
|
|
193
|
-
|
|
194
|
-
const dayErrors = errorList.filter(e =>
|
|
195
|
-
e.timestamp && e.timestamp.startsWith(dateStr)
|
|
196
|
-
).length;
|
|
197
|
-
|
|
198
|
-
const daySuccesses = successList.filter(s =>
|
|
199
|
-
(s.detectedAt || s.timestamp || '').startsWith(dateStr)
|
|
200
|
-
).length;
|
|
201
|
-
|
|
202
|
-
daily.push({ date: dateStr, errors: dayErrors, successes: daySuccesses });
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const errorTrend = lastWeekErrors > 0
|
|
206
|
-
? Math.round(((thisWeekErrors - lastWeekErrors) / lastWeekErrors) * 100)
|
|
207
|
-
: 0;
|
|
208
|
-
|
|
209
|
-
let healthStatus = 'STABLE';
|
|
210
|
-
if (errorTrend < 0) healthStatus = 'IMPROVING';
|
|
211
|
-
else if (errorTrend > 20) healthStatus = 'DECLINING';
|
|
212
|
-
|
|
213
|
-
return {
|
|
214
|
-
thisWeek: { errors: thisWeekErrors, successes: thisWeekSuccesses },
|
|
215
|
-
lastWeek: { errors: lastWeekErrors },
|
|
216
|
-
trends: { errorChange: errorTrend },
|
|
217
|
-
healthStatus,
|
|
218
|
-
daily
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
// MIME types
|
|
224
|
-
const mimeTypes = {
|
|
225
|
-
'.html': 'text/html',
|
|
226
|
-
'.css': 'text/css',
|
|
227
|
-
'.js': 'application/javascript',
|
|
228
|
-
'.json': 'application/json',
|
|
229
|
-
'.png': 'image/png',
|
|
230
|
-
'.jpg': 'image/jpeg',
|
|
231
|
-
'.svg': 'image/svg+xml'
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
// Create server
|
|
235
|
-
function createServer(port) {
|
|
236
|
-
const server = http.createServer((req, res) => {
|
|
237
|
-
const url = req.url.split('?')[0];
|
|
238
|
-
|
|
239
|
-
// CORS headers for local development
|
|
240
|
-
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
241
|
-
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
242
|
-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
243
|
-
|
|
244
|
-
// Handle API requests
|
|
245
|
-
if (url.startsWith('/api/')) {
|
|
246
|
-
const handler = api[url];
|
|
247
|
-
if (handler) {
|
|
248
|
-
res.setHeader('Content-Type', 'application/json');
|
|
249
|
-
res.writeHead(200);
|
|
250
|
-
res.end(JSON.stringify(handler()));
|
|
251
|
-
} else {
|
|
252
|
-
res.writeHead(404);
|
|
253
|
-
res.end(JSON.stringify({ error: 'Not found' }));
|
|
254
|
-
}
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// Serve static files
|
|
259
|
-
let filePath = url === '/' ? '/index.html' : url;
|
|
260
|
-
filePath = path.join(dashboardPath, filePath);
|
|
261
|
-
|
|
262
|
-
if (fs.existsSync(filePath)) {
|
|
263
|
-
const ext = path.extname(filePath);
|
|
264
|
-
const mimeType = mimeTypes[ext] || 'text/plain';
|
|
265
|
-
|
|
266
|
-
res.setHeader('Content-Type', mimeType);
|
|
267
|
-
res.writeHead(200);
|
|
268
|
-
res.end(fs.readFileSync(filePath));
|
|
269
|
-
} else {
|
|
270
|
-
res.writeHead(404);
|
|
271
|
-
res.end('Not found');
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
return server;
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
function startServer(port = 3030) {
|
|
279
|
-
const server = createServer(port);
|
|
280
|
-
|
|
281
|
-
server.listen(port, () => {
|
|
282
|
-
console.log(`${c.cyan}╔════════════════════════════════════════╗${c.reset}`);
|
|
283
|
-
console.log(`${c.cyan}║${c.reset} 🧠 Auto-Learn Dashboard Server ${c.cyan}║${c.reset}`);
|
|
284
|
-
console.log(`${c.cyan}╚════════════════════════════════════════╝${c.reset}\n`);
|
|
285
|
-
console.log(`${c.green}✓ Server running at:${c.reset}`);
|
|
286
|
-
console.log(` ${c.bold}http://localhost:${port}${c.reset}\n`);
|
|
287
|
-
console.log(`${c.gray}API Endpoints:${c.reset}`);
|
|
288
|
-
console.log(` GET /api/errors - Detected errors`);
|
|
289
|
-
console.log(` GET /api/corrections - User corrections`);
|
|
290
|
-
console.log(` GET /api/lessons - Lessons learned`);
|
|
291
|
-
console.log(` GET /api/patterns - Pattern analysis`);
|
|
292
|
-
console.log(` GET /api/summary - Dashboard summary\n`);
|
|
293
|
-
console.log(`${c.yellow}Press Ctrl+C to stop${c.reset}`);
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
server.on('error', (err) => {
|
|
297
|
-
if (err.code === 'EADDRINUSE') {
|
|
298
|
-
console.log(`${c.red}Error: Port ${port} is already in use${c.reset}`);
|
|
299
|
-
console.log(`${c.gray}Try: node dashboard_server.js --port ${port + 1}${c.reset}`);
|
|
300
|
-
} else {
|
|
301
|
-
console.error(`${c.red}Server error:${c.reset}`, err);
|
|
302
|
-
}
|
|
303
|
-
process.exit(1);
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
return server;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Parse CLI args
|
|
310
|
-
const args = process.argv.slice(2);
|
|
311
|
-
|
|
312
|
-
if (args.includes('--start') || args.includes('-s') || args.length === 0 || args.includes('--port') || args.includes('-p')) {
|
|
313
|
-
let port = 3030;
|
|
314
|
-
const portIdx = args.findIndex(a => a === '--port' || a === '-p');
|
|
315
|
-
if (portIdx >= 0 && args[portIdx + 1]) {
|
|
316
|
-
port = parseInt(args[portIdx + 1], 10);
|
|
317
|
-
}
|
|
318
|
-
startServer(port);
|
|
319
|
-
} else if (args.includes('--help') || args.includes('-h')) {
|
|
320
|
-
console.log(`${c.cyan}dashboard_server - Auto-Learn Dashboard Web Server${c.reset}
|
|
321
|
-
|
|
322
|
-
${c.bold}Usage:${c.reset}
|
|
323
|
-
node dashboard_server.js Start server (default port 3030)
|
|
324
|
-
node dashboard_server.js --port 8080 Start on custom port
|
|
325
|
-
node dashboard_server.js --help Show this help
|
|
326
|
-
|
|
327
|
-
${c.bold}API Endpoints:${c.reset}
|
|
328
|
-
GET /api/errors - All detected errors
|
|
329
|
-
GET /api/corrections - All user corrections
|
|
330
|
-
GET /api/lessons - All lessons learned
|
|
331
|
-
GET /api/patterns - Pattern analysis results
|
|
332
|
-
GET /api/summary - Dashboard summary data
|
|
333
|
-
|
|
334
|
-
${c.bold}Example:${c.reset}
|
|
335
|
-
node dashboard_server.js --port 3030
|
|
336
|
-
# Open http://localhost:3030 in browser
|
|
337
|
-
`);
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
export { createServer, startServer };
|