claude-code-workflow 6.1.0 → 6.1.3

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,1948 +1,2063 @@
1
- import http from 'http';
2
- import { URL } from 'url';
3
- import { readFileSync, writeFileSync, existsSync, readdirSync, mkdirSync, statSync, promises as fsPromises } from 'fs';
4
- import { join, dirname } from 'path';
5
- import { homedir } from 'os';
6
- import { createHash } from 'crypto';
7
- import { scanSessions } from './session-scanner.js';
8
- import { aggregateData } from './data-aggregator.js';
9
- import { resolvePath, getRecentPaths, trackRecentPath, removeRecentPath, normalizePathForDisplay, getWorkflowDir } from '../utils/path-resolver.js';
10
-
11
- // Claude config file paths
12
- const CLAUDE_CONFIG_PATH = join(homedir(), '.claude.json');
13
- const CLAUDE_SETTINGS_DIR = join(homedir(), '.claude');
14
- const CLAUDE_GLOBAL_SETTINGS = join(CLAUDE_SETTINGS_DIR, 'settings.json');
15
- const CLAUDE_GLOBAL_SETTINGS_LOCAL = join(CLAUDE_SETTINGS_DIR, 'settings.local.json');
16
-
17
- // Enterprise managed MCP paths (platform-specific)
18
- function getEnterpriseMcpPath() {
19
- const platform = process.platform;
20
- if (platform === 'darwin') {
21
- return '/Library/Application Support/ClaudeCode/managed-mcp.json';
22
- } else if (platform === 'win32') {
23
- return 'C:\\Program Files\\ClaudeCode\\managed-mcp.json';
24
- } else {
25
- // Linux and WSL
26
- return '/etc/claude-code/managed-mcp.json';
27
- }
28
- }
29
-
30
- // WebSocket clients for real-time notifications
31
- const wsClients = new Set();
32
-
33
- const TEMPLATE_PATH = join(import.meta.dirname, '../templates/dashboard.html');
34
- const MODULE_CSS_DIR = join(import.meta.dirname, '../templates/dashboard-css');
35
- const JS_FILE = join(import.meta.dirname, '../templates/dashboard.js');
36
- const MODULE_JS_DIR = join(import.meta.dirname, '../templates/dashboard-js');
37
-
38
- // Modular CSS files in load order
39
- const MODULE_CSS_FILES = [
40
- '01-base.css',
41
- '02-session.css',
42
- '03-tasks.css',
43
- '04-lite-tasks.css',
44
- '05-context.css',
45
- '06-cards.css',
46
- '07-managers.css',
47
- '08-review.css',
48
- '09-explorer.css'
49
- ];
50
-
51
- /**
52
- * Handle POST request with JSON body
53
- */
54
- function handlePostRequest(req, res, handler) {
55
- let body = '';
56
- req.on('data', chunk => { body += chunk; });
57
- req.on('end', async () => {
58
- try {
59
- const parsed = JSON.parse(body);
60
- const result = await handler(parsed);
61
-
62
- if (result.error) {
63
- const status = result.status || 500;
64
- res.writeHead(status, { 'Content-Type': 'application/json' });
65
- res.end(JSON.stringify({ error: result.error }));
66
- } else {
67
- res.writeHead(200, { 'Content-Type': 'application/json' });
68
- res.end(JSON.stringify(result));
69
- }
70
- } catch (error) {
71
- res.writeHead(500, { 'Content-Type': 'application/json' });
72
- res.end(JSON.stringify({ error: error.message }));
73
- }
74
- });
75
- }
76
-
77
- // Modular JS files in dependency order
78
- const MODULE_FILES = [
79
- 'utils.js',
80
- 'state.js',
81
- 'api.js',
82
- 'components/theme.js',
83
- 'components/modals.js',
84
- 'components/navigation.js',
85
- 'components/sidebar.js',
86
- 'components/carousel.js',
87
- 'components/notifications.js',
88
- 'components/global-notifications.js',
89
- 'components/mcp-manager.js',
90
- 'components/hook-manager.js',
91
- 'components/_exp_helpers.js',
92
- 'components/tabs-other.js',
93
- 'components/tabs-context.js',
94
- 'components/_conflict_tab.js',
95
- 'components/_review_tab.js',
96
- 'components/task-drawer-core.js',
97
- 'components/task-drawer-renderers.js',
98
- 'components/flowchart.js',
99
- 'views/home.js',
100
- 'views/project-overview.js',
101
- 'views/session-detail.js',
102
- 'views/review-session.js',
103
- 'views/lite-tasks.js',
104
- 'views/fix-session.js',
105
- 'views/mcp-manager.js',
106
- 'views/hook-manager.js',
107
- 'views/explorer.js',
108
- 'main.js'
109
- ];
110
- /**
111
- * Create and start the dashboard server
112
- * @param {Object} options - Server options
113
- * @param {number} options.port - Port to listen on (default: 3456)
114
- * @param {string} options.initialPath - Initial project path
115
- * @returns {Promise<http.Server>}
116
- */
117
- export async function startServer(options = {}) {
118
- const port = options.port || 3456;
119
- const initialPath = options.initialPath || process.cwd();
120
-
121
- const server = http.createServer(async (req, res) => {
122
- const url = new URL(req.url, `http://localhost:${port}`);
123
- const pathname = url.pathname;
124
-
125
- // CORS headers for API requests
126
- res.setHeader('Access-Control-Allow-Origin', '*');
127
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
128
- res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
129
-
130
- if (req.method === 'OPTIONS') {
131
- res.writeHead(200);
132
- res.end();
133
- return;
134
- }
135
-
136
- try {
137
- // Debug log for API requests
138
- if (pathname.startsWith('/api/')) {
139
- console.log(`[API] ${req.method} ${pathname}`);
140
- }
141
-
142
- // API: Get workflow data for a path
143
- if (pathname === '/api/data') {
144
- const projectPath = url.searchParams.get('path') || initialPath;
145
- const data = await getWorkflowData(projectPath);
146
-
147
- res.writeHead(200, { 'Content-Type': 'application/json' });
148
- res.end(JSON.stringify(data));
149
- return;
150
- }
151
-
152
- // API: Get recent paths
153
- if (pathname === '/api/recent-paths') {
154
- const paths = getRecentPaths();
155
- res.writeHead(200, { 'Content-Type': 'application/json' });
156
- res.end(JSON.stringify({ paths }));
157
- return;
158
- }
159
-
160
- // API: Switch workspace path (for ccw view command)
161
- if (pathname === '/api/switch-path') {
162
- const newPath = url.searchParams.get('path');
163
- if (!newPath) {
164
- res.writeHead(400, { 'Content-Type': 'application/json' });
165
- res.end(JSON.stringify({ error: 'Path is required' }));
166
- return;
167
- }
168
-
169
- const resolved = resolvePath(newPath);
170
- if (!existsSync(resolved)) {
171
- res.writeHead(404, { 'Content-Type': 'application/json' });
172
- res.end(JSON.stringify({ error: 'Path does not exist' }));
173
- return;
174
- }
175
-
176
- // Track the path and return success
177
- trackRecentPath(resolved);
178
- res.writeHead(200, { 'Content-Type': 'application/json' });
179
- res.end(JSON.stringify({
180
- success: true,
181
- path: resolved,
182
- recentPaths: getRecentPaths()
183
- }));
184
- return;
185
- }
186
-
187
- // API: Health check (for ccw view to detect running server)
188
- if (pathname === '/api/health') {
189
- res.writeHead(200, { 'Content-Type': 'application/json' });
190
- res.end(JSON.stringify({ status: 'ok', timestamp: Date.now() }));
191
- return;
192
- }
193
-
194
- // API: Shutdown server (for ccw stop command)
195
- if (pathname === '/api/shutdown' && req.method === 'POST') {
196
- res.writeHead(200, { 'Content-Type': 'application/json' });
197
- res.end(JSON.stringify({ status: 'shutting_down' }));
198
-
199
- // Graceful shutdown
200
- console.log('\n Received shutdown signal...');
201
- setTimeout(() => {
202
- server.close(() => {
203
- console.log(' Server stopped.\n');
204
- process.exit(0);
205
- });
206
- // Force exit after 3 seconds if graceful shutdown fails
207
- setTimeout(() => process.exit(0), 3000);
208
- }, 100);
209
- return;
210
- }
211
-
212
- // API: Remove a recent path
213
- if (pathname === '/api/remove-recent-path' && req.method === 'POST') {
214
- handlePostRequest(req, res, async (body) => {
215
- const { path } = body;
216
- if (!path) {
217
- return { error: 'path is required', status: 400 };
218
- }
219
- const removed = removeRecentPath(path);
220
- return { success: removed, paths: getRecentPaths() };
221
- });
222
- return;
223
- }
224
-
225
- // API: Read a JSON file (for fix progress tracking)
226
- if (pathname === '/api/file') {
227
- const filePath = url.searchParams.get('path');
228
- if (!filePath) {
229
- res.writeHead(400, { 'Content-Type': 'application/json' });
230
- res.end(JSON.stringify({ error: 'File path is required' }));
231
- return;
232
- }
233
-
234
- try {
235
- const content = await fsPromises.readFile(filePath, 'utf-8');
236
- const json = JSON.parse(content);
237
- res.writeHead(200, { 'Content-Type': 'application/json' });
238
- res.end(JSON.stringify(json));
239
- } catch (err) {
240
- res.writeHead(404, { 'Content-Type': 'application/json' });
241
- res.end(JSON.stringify({ error: 'File not found or invalid JSON' }));
242
- }
243
- return;
244
- }
245
-
246
- // API: Get session detail data (context, summaries, impl-plan, review)
247
- if (pathname === '/api/session-detail') {
248
- const sessionPath = url.searchParams.get('path');
249
- const dataType = url.searchParams.get('type') || 'all';
250
-
251
- if (!sessionPath) {
252
- res.writeHead(400, { 'Content-Type': 'application/json' });
253
- res.end(JSON.stringify({ error: 'Session path is required' }));
254
- return;
255
- }
256
-
257
- const detail = await getSessionDetailData(sessionPath, dataType);
258
- res.writeHead(200, { 'Content-Type': 'application/json' });
259
- res.end(JSON.stringify(detail));
260
- return;
261
- }
262
-
263
- // API: Update task status
264
- if (pathname === '/api/update-task-status' && req.method === 'POST') {
265
- handlePostRequest(req, res, async (body) => {
266
- const { sessionPath, taskId, newStatus } = body;
267
-
268
- if (!sessionPath || !taskId || !newStatus) {
269
- return { error: 'sessionPath, taskId, and newStatus are required', status: 400 };
270
- }
271
-
272
- return await updateTaskStatus(sessionPath, taskId, newStatus);
273
- });
274
- return;
275
- }
276
-
277
- // API: Bulk update task status
278
- if (pathname === '/api/bulk-update-task-status' && req.method === 'POST') {
279
- handlePostRequest(req, res, async (body) => {
280
- const { sessionPath, taskIds, newStatus } = body;
281
-
282
- if (!sessionPath || !taskIds || !newStatus) {
283
- return { error: 'sessionPath, taskIds, and newStatus are required', status: 400 };
284
- }
285
-
286
- const results = [];
287
- for (const taskId of taskIds) {
288
- try {
289
- const result = await updateTaskStatus(sessionPath, taskId, newStatus);
290
- results.push(result);
291
- } catch (err) {
292
- results.push({ taskId, error: err.message });
293
- }
294
- }
295
- return { success: true, results };
296
- });
297
- return;
298
- }
299
-
300
- // API: Get MCP configuration
301
- if (pathname === '/api/mcp-config') {
302
- const mcpData = getMcpConfig();
303
- res.writeHead(200, { 'Content-Type': 'application/json' });
304
- res.end(JSON.stringify(mcpData));
305
- return;
306
- }
307
-
308
- // API: Toggle MCP server enabled/disabled
309
- if (pathname === '/api/mcp-toggle' && req.method === 'POST') {
310
- handlePostRequest(req, res, async (body) => {
311
- const { projectPath, serverName, enable } = body;
312
- if (!projectPath || !serverName) {
313
- return { error: 'projectPath and serverName are required', status: 400 };
314
- }
315
- return toggleMcpServerEnabled(projectPath, serverName, enable);
316
- });
317
- return;
318
- }
319
-
320
- // API: Copy MCP server to project
321
- if (pathname === '/api/mcp-copy-server' && req.method === 'POST') {
322
- handlePostRequest(req, res, async (body) => {
323
- const { projectPath, serverName, serverConfig } = body;
324
- if (!projectPath || !serverName || !serverConfig) {
325
- return { error: 'projectPath, serverName, and serverConfig are required', status: 400 };
326
- }
327
- return addMcpServerToProject(projectPath, serverName, serverConfig);
328
- });
329
- return;
330
- }
331
-
332
- // API: Remove MCP server from project
333
- if (pathname === '/api/mcp-remove-server' && req.method === 'POST') {
334
- handlePostRequest(req, res, async (body) => {
335
- const { projectPath, serverName } = body;
336
- if (!projectPath || !serverName) {
337
- return { error: 'projectPath and serverName are required', status: 400 };
338
- }
339
- return removeMcpServerFromProject(projectPath, serverName);
340
- });
341
- return;
342
- }
343
-
344
- // API: Hook endpoint for Claude Code notifications
345
- if (pathname === '/api/hook' && req.method === 'POST') {
346
- handlePostRequest(req, res, async (body) => {
347
- const { type, filePath, sessionId } = body;
348
-
349
- // Determine session ID from file path if not provided
350
- let resolvedSessionId = sessionId;
351
- if (!resolvedSessionId && filePath) {
352
- resolvedSessionId = extractSessionIdFromPath(filePath);
353
- }
354
-
355
- // Broadcast to all connected WebSocket clients
356
- const notification = {
357
- type: type || 'session_updated',
358
- payload: {
359
- sessionId: resolvedSessionId,
360
- filePath: filePath,
361
- timestamp: new Date().toISOString()
362
- }
363
- };
364
-
365
- broadcastToClients(notification);
366
-
367
- return { success: true, notification };
368
- });
369
- return;
370
- }
371
-
372
- // API: Get hooks configuration
373
- if (pathname === '/api/hooks' && req.method === 'GET') {
374
- const projectPathParam = url.searchParams.get('path');
375
- const hooksData = getHooksConfig(projectPathParam);
376
- res.writeHead(200, { 'Content-Type': 'application/json' });
377
- res.end(JSON.stringify(hooksData));
378
- return;
379
- }
380
-
381
- // API: Save hook
382
- if (pathname === '/api/hooks' && req.method === 'POST') {
383
- handlePostRequest(req, res, async (body) => {
384
- const { projectPath, scope, event, hookData } = body;
385
- if (!scope || !event || !hookData) {
386
- return { error: 'scope, event, and hookData are required', status: 400 };
387
- }
388
- return saveHookToSettings(projectPath, scope, event, hookData);
389
- });
390
- return;
391
- }
392
-
393
- // API: Delete hook
394
- if (pathname === '/api/hooks' && req.method === 'DELETE') {
395
- handlePostRequest(req, res, async (body) => {
396
- const { projectPath, scope, event, hookIndex } = body;
397
- if (!scope || !event || hookIndex === undefined) {
398
- return { error: 'scope, event, and hookIndex are required', status: 400 };
399
- }
400
- return deleteHookFromSettings(projectPath, scope, event, hookIndex);
401
- });
402
- return;
403
- }
404
-
405
- // API: List directory files with .gitignore filtering (Explorer view)
406
- if (pathname === '/api/files') {
407
- const dirPath = url.searchParams.get('path') || initialPath;
408
- const filesData = await listDirectoryFiles(dirPath);
409
- res.writeHead(200, { 'Content-Type': 'application/json' });
410
- res.end(JSON.stringify(filesData));
411
- return;
412
- }
413
-
414
- // API: Get file content for preview (Explorer view)
415
- if (pathname === '/api/file-content') {
416
- const filePath = url.searchParams.get('path');
417
- if (!filePath) {
418
- res.writeHead(400, { 'Content-Type': 'application/json' });
419
- res.end(JSON.stringify({ error: 'File path is required' }));
420
- return;
421
- }
422
- const fileData = await getFileContent(filePath);
423
- res.writeHead(fileData.error ? 404 : 200, { 'Content-Type': 'application/json' });
424
- res.end(JSON.stringify(fileData));
425
- return;
426
- }
427
-
428
- // API: Update CLAUDE.md using CLI tools (Explorer view)
429
- if (pathname === '/api/update-claude-md' && req.method === 'POST') {
430
- handlePostRequest(req, res, async (body) => {
431
- const { path: targetPath, tool = 'gemini', strategy = 'single-layer' } = body;
432
- if (!targetPath) {
433
- return { error: 'path is required', status: 400 };
434
- }
435
- return await triggerUpdateClaudeMd(targetPath, tool, strategy);
436
- });
437
- return;
438
- }
439
-
440
- // Serve dashboard HTML
441
- if (pathname === '/' || pathname === '/index.html') {
442
- const html = generateServerDashboard(initialPath);
443
- res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
444
- res.end(html);
445
- return;
446
- }
447
-
448
- // 404
449
- res.writeHead(404, { 'Content-Type': 'text/plain' });
450
- res.end('Not Found');
451
-
452
- } catch (error) {
453
- console.error('Server error:', error);
454
- res.writeHead(500, { 'Content-Type': 'application/json' });
455
- res.end(JSON.stringify({ error: error.message }));
456
- }
457
- });
458
-
459
- // Handle WebSocket upgrade requests
460
- server.on('upgrade', (req, socket, head) => {
461
- if (req.url === '/ws') {
462
- handleWebSocketUpgrade(req, socket, head);
463
- } else {
464
- socket.destroy();
465
- }
466
- });
467
-
468
- return new Promise((resolve, reject) => {
469
- server.listen(port, () => {
470
- console.log(`Dashboard server running at http://localhost:${port}`);
471
- console.log(`WebSocket endpoint available at ws://localhost:${port}/ws`);
472
- console.log(`Hook endpoint available at POST http://localhost:${port}/api/hook`);
473
- resolve(server);
474
- });
475
- server.on('error', reject);
476
- });
477
- }
478
-
479
- // ========================================
480
- // WebSocket Functions
481
- // ========================================
482
-
483
- /**
484
- * Handle WebSocket upgrade
485
- */
486
- function handleWebSocketUpgrade(req, socket, head) {
487
- const key = req.headers['sec-websocket-key'];
488
- const acceptKey = createHash('sha1')
489
- .update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
490
- .digest('base64');
491
-
492
- const responseHeaders = [
493
- 'HTTP/1.1 101 Switching Protocols',
494
- 'Upgrade: websocket',
495
- 'Connection: Upgrade',
496
- `Sec-WebSocket-Accept: ${acceptKey}`,
497
- '',
498
- ''
499
- ].join('\r\n');
500
-
501
- socket.write(responseHeaders);
502
-
503
- // Add to clients set
504
- wsClients.add(socket);
505
- console.log(`[WS] Client connected (${wsClients.size} total)`);
506
-
507
- // Handle incoming messages
508
- socket.on('data', (buffer) => {
509
- try {
510
- const frame = parseWebSocketFrame(buffer);
511
- if (!frame) return;
512
-
513
- const { opcode, payload } = frame;
514
-
515
- switch (opcode) {
516
- case 0x1: // Text frame
517
- if (payload) {
518
- console.log('[WS] Received:', payload);
519
- }
520
- break;
521
- case 0x8: // Close frame
522
- socket.end();
523
- break;
524
- case 0x9: // Ping frame - respond with Pong
525
- const pongFrame = Buffer.alloc(2);
526
- pongFrame[0] = 0x8A; // Pong opcode with FIN bit
527
- pongFrame[1] = 0x00; // No payload
528
- socket.write(pongFrame);
529
- break;
530
- case 0xA: // Pong frame - ignore
531
- break;
532
- default:
533
- // Ignore other frame types (binary, continuation)
534
- break;
535
- }
536
- } catch (e) {
537
- // Ignore parse errors
538
- }
539
- });
540
-
541
- // Handle disconnect
542
- socket.on('close', () => {
543
- wsClients.delete(socket);
544
- console.log(`[WS] Client disconnected (${wsClients.size} remaining)`);
545
- });
546
-
547
- socket.on('error', () => {
548
- wsClients.delete(socket);
549
- });
550
- }
551
-
552
- /**
553
- * Parse WebSocket frame (simplified)
554
- * Returns { opcode, payload } or null
555
- */
556
- function parseWebSocketFrame(buffer) {
557
- if (buffer.length < 2) return null;
558
-
559
- const firstByte = buffer[0];
560
- const opcode = firstByte & 0x0f; // Extract opcode (bits 0-3)
561
-
562
- // Opcode types:
563
- // 0x0 = continuation, 0x1 = text, 0x2 = binary
564
- // 0x8 = close, 0x9 = ping, 0xA = pong
565
-
566
- const secondByte = buffer[1];
567
- const isMasked = (secondByte & 0x80) !== 0;
568
- let payloadLength = secondByte & 0x7f;
569
-
570
- let offset = 2;
571
- if (payloadLength === 126) {
572
- payloadLength = buffer.readUInt16BE(2);
573
- offset = 4;
574
- } else if (payloadLength === 127) {
575
- payloadLength = Number(buffer.readBigUInt64BE(2));
576
- offset = 10;
577
- }
578
-
579
- let mask = null;
580
- if (isMasked) {
581
- mask = buffer.slice(offset, offset + 4);
582
- offset += 4;
583
- }
584
-
585
- const payload = buffer.slice(offset, offset + payloadLength);
586
-
587
- if (isMasked && mask) {
588
- for (let i = 0; i < payload.length; i++) {
589
- payload[i] ^= mask[i % 4];
590
- }
591
- }
592
-
593
- return { opcode, payload: payload.toString('utf8') };
594
- }
595
-
596
- /**
597
- * Create WebSocket frame
598
- */
599
- function createWebSocketFrame(data) {
600
- const payload = Buffer.from(JSON.stringify(data), 'utf8');
601
- const length = payload.length;
602
-
603
- let frame;
604
- if (length <= 125) {
605
- frame = Buffer.alloc(2 + length);
606
- frame[0] = 0x81; // Text frame, FIN
607
- frame[1] = length;
608
- payload.copy(frame, 2);
609
- } else if (length <= 65535) {
610
- frame = Buffer.alloc(4 + length);
611
- frame[0] = 0x81;
612
- frame[1] = 126;
613
- frame.writeUInt16BE(length, 2);
614
- payload.copy(frame, 4);
615
- } else {
616
- frame = Buffer.alloc(10 + length);
617
- frame[0] = 0x81;
618
- frame[1] = 127;
619
- frame.writeBigUInt64BE(BigInt(length), 2);
620
- payload.copy(frame, 10);
621
- }
622
-
623
- return frame;
624
- }
625
-
626
- /**
627
- * Broadcast message to all connected WebSocket clients
628
- */
629
- function broadcastToClients(data) {
630
- const frame = createWebSocketFrame(data);
631
-
632
- for (const client of wsClients) {
633
- try {
634
- client.write(frame);
635
- } catch (e) {
636
- wsClients.delete(client);
637
- }
638
- }
639
-
640
- console.log(`[WS] Broadcast to ${wsClients.size} clients:`, data.type);
641
- }
642
-
643
- /**
644
- * Extract session ID from file path
645
- */
646
- function extractSessionIdFromPath(filePath) {
647
- // Normalize path
648
- const normalized = filePath.replace(/\\/g, '/');
649
-
650
- // Look for session pattern: WFS-xxx, WRS-xxx, etc.
651
- const sessionMatch = normalized.match(/\/(W[A-Z]S-[^/]+)\//);
652
- if (sessionMatch) {
653
- return sessionMatch[1];
654
- }
655
-
656
- // Look for .workflow/.sessions/xxx pattern
657
- const sessionsMatch = normalized.match(/\.workflow\/\.sessions\/([^/]+)/);
658
- if (sessionsMatch) {
659
- return sessionsMatch[1];
660
- }
661
-
662
- // Look for lite-plan/lite-fix pattern
663
- const liteMatch = normalized.match(/\.(lite-plan|lite-fix)\/([^/]+)/);
664
- if (liteMatch) {
665
- return liteMatch[2];
666
- }
667
-
668
- return null;
669
- }
670
-
671
- /**
672
- * Get workflow data for a project path
673
- * @param {string} projectPath
674
- * @returns {Promise<Object>}
675
- */
676
- async function getWorkflowData(projectPath) {
677
- const resolvedPath = resolvePath(projectPath);
678
- const workflowDir = join(resolvedPath, '.workflow');
679
-
680
- // Track this path
681
- trackRecentPath(resolvedPath);
682
-
683
- // Check if .workflow exists
684
- if (!existsSync(workflowDir)) {
685
- return {
686
- generatedAt: new Date().toISOString(),
687
- activeSessions: [],
688
- archivedSessions: [],
689
- liteTasks: { litePlan: [], liteFix: [] },
690
- reviewData: { dimensions: {} },
691
- projectOverview: null,
692
- statistics: {
693
- totalSessions: 0,
694
- activeSessions: 0,
695
- totalTasks: 0,
696
- completedTasks: 0,
697
- reviewFindings: 0,
698
- litePlanCount: 0,
699
- liteFixCount: 0
700
- },
701
- projectPath: normalizePathForDisplay(resolvedPath),
702
- recentPaths: getRecentPaths()
703
- };
704
- }
705
-
706
- // Scan and aggregate data
707
- const sessions = await scanSessions(workflowDir);
708
- const data = await aggregateData(sessions, workflowDir);
709
-
710
- data.projectPath = normalizePathForDisplay(resolvedPath);
711
- data.recentPaths = getRecentPaths();
712
-
713
- return data;
714
- }
715
-
716
- /**
717
- * Get session detail data (context, summaries, impl-plan, review)
718
- * @param {string} sessionPath - Path to session directory
719
- * @param {string} dataType - Type of data to load: context, summary, impl-plan, review, or all
720
- * @returns {Promise<Object>}
721
- */
722
- async function getSessionDetailData(sessionPath, dataType) {
723
- const result = {};
724
-
725
- // Normalize path
726
- const normalizedPath = sessionPath.replace(/\\/g, '/');
727
-
728
- try {
729
- // Load context-package.json (in .process/ subfolder)
730
- if (dataType === 'context' || dataType === 'all') {
731
- // Try .process/context-package.json first (common location)
732
- let contextFile = join(normalizedPath, '.process', 'context-package.json');
733
- if (!existsSync(contextFile)) {
734
- // Fallback to session root
735
- contextFile = join(normalizedPath, 'context-package.json');
736
- }
737
- if (existsSync(contextFile)) {
738
- try {
739
- result.context = JSON.parse(readFileSync(contextFile, 'utf8'));
740
- } catch (e) {
741
- result.context = null;
742
- }
743
- }
744
- }
745
-
746
- // Load task JSONs from .task/ folder
747
- if (dataType === 'tasks' || dataType === 'all') {
748
- const taskDir = join(normalizedPath, '.task');
749
- result.tasks = [];
750
- if (existsSync(taskDir)) {
751
- const files = readdirSync(taskDir).filter(f => f.endsWith('.json') && f.startsWith('IMPL-'));
752
- for (const file of files) {
753
- try {
754
- const content = JSON.parse(readFileSync(join(taskDir, file), 'utf8'));
755
- result.tasks.push({
756
- filename: file,
757
- task_id: file.replace('.json', ''),
758
- ...content
759
- });
760
- } catch (e) {
761
- // Skip unreadable files
762
- }
763
- }
764
- // Sort by task ID
765
- result.tasks.sort((a, b) => a.task_id.localeCompare(b.task_id));
766
- }
767
- }
768
-
769
- // Load summaries from .summaries/
770
- if (dataType === 'summary' || dataType === 'all') {
771
- const summariesDir = join(normalizedPath, '.summaries');
772
- result.summaries = [];
773
- if (existsSync(summariesDir)) {
774
- const files = readdirSync(summariesDir).filter(f => f.endsWith('.md'));
775
- for (const file of files) {
776
- try {
777
- const content = readFileSync(join(summariesDir, file), 'utf8');
778
- result.summaries.push({ name: file.replace('.md', ''), content });
779
- } catch (e) {
780
- // Skip unreadable files
781
- }
782
- }
783
- }
784
- }
785
-
786
- // Load plan.json (for lite tasks)
787
- if (dataType === 'plan' || dataType === 'all') {
788
- const planFile = join(normalizedPath, 'plan.json');
789
- if (existsSync(planFile)) {
790
- try {
791
- result.plan = JSON.parse(readFileSync(planFile, 'utf8'));
792
- } catch (e) {
793
- result.plan = null;
794
- }
795
- }
796
- }
797
-
798
- // Load explorations (exploration-*.json files) - check .process/ first, then session root
799
- if (dataType === 'context' || dataType === 'explorations' || dataType === 'all') {
800
- result.explorations = { manifest: null, data: {} };
801
-
802
- // Try .process/ first (standard workflow sessions), then session root (lite tasks)
803
- const searchDirs = [
804
- join(normalizedPath, '.process'),
805
- normalizedPath
806
- ];
807
-
808
- for (const searchDir of searchDirs) {
809
- if (!existsSync(searchDir)) continue;
810
-
811
- // Look for explorations-manifest.json
812
- const manifestFile = join(searchDir, 'explorations-manifest.json');
813
- if (existsSync(manifestFile)) {
814
- try {
815
- result.explorations.manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
816
-
817
- // Load each exploration file based on manifest
818
- const explorations = result.explorations.manifest.explorations || [];
819
- for (const exp of explorations) {
820
- const expFile = join(searchDir, exp.file);
821
- if (existsSync(expFile)) {
822
- try {
823
- result.explorations.data[exp.angle] = JSON.parse(readFileSync(expFile, 'utf8'));
824
- } catch (e) {
825
- // Skip unreadable exploration files
826
- }
827
- }
828
- }
829
- break; // Found manifest, stop searching
830
- } catch (e) {
831
- result.explorations.manifest = null;
832
- }
833
- } else {
834
- // Fallback: scan for exploration-*.json files directly
835
- try {
836
- const files = readdirSync(searchDir).filter(f => f.startsWith('exploration-') && f.endsWith('.json'));
837
- if (files.length > 0) {
838
- // Create synthetic manifest
839
- result.explorations.manifest = {
840
- exploration_count: files.length,
841
- explorations: files.map((f, i) => ({
842
- angle: f.replace('exploration-', '').replace('.json', ''),
843
- file: f,
844
- index: i + 1
845
- }))
846
- };
847
-
848
- // Load each file
849
- for (const file of files) {
850
- const angle = file.replace('exploration-', '').replace('.json', '');
851
- try {
852
- result.explorations.data[angle] = JSON.parse(readFileSync(join(searchDir, file), 'utf8'));
853
- } catch (e) {
854
- // Skip unreadable files
855
- }
856
- }
857
- break; // Found explorations, stop searching
858
- }
859
- } catch (e) {
860
- // Directory read failed
861
- }
862
- }
863
- }
864
- }
865
-
866
- // Load conflict resolution decisions (conflict-resolution-decisions.json)
867
- if (dataType === 'context' || dataType === 'conflict' || dataType === 'all') {
868
- result.conflictResolution = null;
869
-
870
- // Try .process/ first (standard workflow sessions)
871
- const conflictFiles = [
872
- join(normalizedPath, '.process', 'conflict-resolution-decisions.json'),
873
- join(normalizedPath, 'conflict-resolution-decisions.json')
874
- ];
875
-
876
- for (const conflictFile of conflictFiles) {
877
- if (existsSync(conflictFile)) {
878
- try {
879
- result.conflictResolution = JSON.parse(readFileSync(conflictFile, 'utf8'));
880
- break; // Found file, stop searching
881
- } catch (e) {
882
- // Skip unreadable file
883
- }
884
- }
885
- }
886
- }
887
-
888
- // Load IMPL_PLAN.md
889
- if (dataType === 'impl-plan' || dataType === 'all') {
890
- const implPlanFile = join(normalizedPath, 'IMPL_PLAN.md');
891
- if (existsSync(implPlanFile)) {
892
- try {
893
- result.implPlan = readFileSync(implPlanFile, 'utf8');
894
- } catch (e) {
895
- result.implPlan = null;
896
- }
897
- }
898
- }
899
-
900
- // Load review data from .review/
901
- if (dataType === 'review' || dataType === 'all') {
902
- const reviewDir = join(normalizedPath, '.review');
903
- result.review = {
904
- state: null,
905
- dimensions: [],
906
- severityDistribution: null,
907
- totalFindings: 0
908
- };
909
-
910
- if (existsSync(reviewDir)) {
911
- // Load review-state.json
912
- const stateFile = join(reviewDir, 'review-state.json');
913
- if (existsSync(stateFile)) {
914
- try {
915
- const state = JSON.parse(readFileSync(stateFile, 'utf8'));
916
- result.review.state = state;
917
- result.review.severityDistribution = state.severity_distribution || {};
918
- result.review.totalFindings = state.total_findings || 0;
919
- result.review.phase = state.phase || 'unknown';
920
- result.review.dimensionSummaries = state.dimension_summaries || {};
921
- result.review.crossCuttingConcerns = state.cross_cutting_concerns || [];
922
- result.review.criticalFiles = state.critical_files || [];
923
- } catch (e) {
924
- // Skip unreadable state
925
- }
926
- }
927
-
928
- // Load dimension findings
929
- const dimensionsDir = join(reviewDir, 'dimensions');
930
- if (existsSync(dimensionsDir)) {
931
- const files = readdirSync(dimensionsDir).filter(f => f.endsWith('.json'));
932
- for (const file of files) {
933
- try {
934
- const dimName = file.replace('.json', '');
935
- const data = JSON.parse(readFileSync(join(dimensionsDir, file), 'utf8'));
936
-
937
- // Handle array structure: [ { findings: [...] } ]
938
- let findings = [];
939
- let summary = null;
940
-
941
- if (Array.isArray(data) && data.length > 0) {
942
- const dimData = data[0];
943
- findings = dimData.findings || [];
944
- summary = dimData.summary || null;
945
- } else if (data.findings) {
946
- findings = data.findings;
947
- summary = data.summary || null;
948
- }
949
-
950
- result.review.dimensions.push({
951
- name: dimName,
952
- findings: findings,
953
- summary: summary,
954
- count: findings.length
955
- });
956
- } catch (e) {
957
- // Skip unreadable files
958
- }
959
- }
960
- }
961
- }
962
- }
963
-
964
- } catch (error) {
965
- console.error('Error loading session detail:', error);
966
- result.error = error.message;
967
- }
968
-
969
- return result;
970
- }
971
-
972
- /**
973
- * Update task status in a task JSON file
974
- * @param {string} sessionPath - Path to session directory
975
- * @param {string} taskId - Task ID (e.g., IMPL-001)
976
- * @param {string} newStatus - New status (pending, in_progress, completed)
977
- * @returns {Promise<Object>}
978
- */
979
- async function updateTaskStatus(sessionPath, taskId, newStatus) {
980
- // Normalize path (handle both forward and back slashes)
981
- let normalizedPath = sessionPath.replace(/\\/g, '/');
982
-
983
- // Handle Windows drive letter format
984
- if (normalizedPath.match(/^[a-zA-Z]:\//)) {
985
- // Already in correct format
986
- } else if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
987
- // Convert /D/path to D:/path
988
- normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
989
- }
990
-
991
- const taskDir = join(normalizedPath, '.task');
992
-
993
- // Check if task directory exists
994
- if (!existsSync(taskDir)) {
995
- throw new Error(`Task directory not found: ${taskDir}`);
996
- }
997
-
998
- // Try to find the task file
999
- let taskFile = join(taskDir, `${taskId}.json`);
1000
-
1001
- if (!existsSync(taskFile)) {
1002
- // Try without .json if taskId already has it
1003
- if (taskId.endsWith('.json')) {
1004
- taskFile = join(taskDir, taskId);
1005
- }
1006
- if (!existsSync(taskFile)) {
1007
- throw new Error(`Task file not found: ${taskId}.json in ${taskDir}`);
1008
- }
1009
- }
1010
-
1011
- try {
1012
- const content = JSON.parse(readFileSync(taskFile, 'utf8'));
1013
- const oldStatus = content.status || 'pending';
1014
- content.status = newStatus;
1015
-
1016
- // Add status change timestamp
1017
- if (!content.status_history) {
1018
- content.status_history = [];
1019
- }
1020
- content.status_history.push({
1021
- from: oldStatus,
1022
- to: newStatus,
1023
- changed_at: new Date().toISOString()
1024
- });
1025
-
1026
- writeFileSync(taskFile, JSON.stringify(content, null, 2), 'utf8');
1027
-
1028
- return {
1029
- success: true,
1030
- taskId,
1031
- oldStatus,
1032
- newStatus,
1033
- file: taskFile
1034
- };
1035
- } catch (error) {
1036
- throw new Error(`Failed to update task ${taskId}: ${error.message}`);
1037
- }
1038
- }
1039
-
1040
- /**
1041
- * Generate dashboard HTML for server mode
1042
- * @param {string} initialPath
1043
- * @returns {string}
1044
- */
1045
- function generateServerDashboard(initialPath) {
1046
- let html = readFileSync(TEMPLATE_PATH, 'utf8');
1047
-
1048
- // Read and concatenate modular CSS files in load order
1049
- const cssContent = MODULE_CSS_FILES.map(file => {
1050
- const filePath = join(MODULE_CSS_DIR, file);
1051
- return existsSync(filePath) ? readFileSync(filePath, 'utf8') : '';
1052
- }).join('\n\n');
1053
-
1054
- // Read and concatenate modular JS files in dependency order
1055
- let jsContent = MODULE_FILES.map(file => {
1056
- const filePath = join(MODULE_JS_DIR, file);
1057
- return existsSync(filePath) ? readFileSync(filePath, 'utf8') : '';
1058
- }).join('\n\n');
1059
-
1060
- // Inject CSS content
1061
- html = html.replace('{{CSS_CONTENT}}', cssContent);
1062
-
1063
- // Prepare JS content with empty initial data (will be loaded dynamically)
1064
- const emptyData = {
1065
- generatedAt: new Date().toISOString(),
1066
- activeSessions: [],
1067
- archivedSessions: [],
1068
- liteTasks: { litePlan: [], liteFix: [] },
1069
- reviewData: { dimensions: {} },
1070
- projectOverview: null,
1071
- statistics: { totalSessions: 0, activeSessions: 0, totalTasks: 0, completedTasks: 0, reviewFindings: 0, litePlanCount: 0, liteFixCount: 0 }
1072
- };
1073
-
1074
- // Replace JS placeholders
1075
- jsContent = jsContent.replace('{{WORKFLOW_DATA}}', JSON.stringify(emptyData, null, 2));
1076
- jsContent = jsContent.replace(/\{\{PROJECT_PATH\}\}/g, normalizePathForDisplay(initialPath).replace(/\\/g, '/'));
1077
- jsContent = jsContent.replace('{{RECENT_PATHS}}', JSON.stringify(getRecentPaths()));
1078
-
1079
- // Add server mode flag and dynamic loading functions at the start of JS
1080
- const serverModeScript = `
1081
- // Server mode - load data dynamically
1082
- window.SERVER_MODE = true;
1083
- window.INITIAL_PATH = '${normalizePathForDisplay(initialPath).replace(/\\/g, '/')}';
1084
-
1085
- async function loadDashboardData(path) {
1086
- try {
1087
- const res = await fetch('/api/data?path=' + encodeURIComponent(path));
1088
- if (!res.ok) throw new Error('Failed to load data');
1089
- return await res.json();
1090
- } catch (err) {
1091
- console.error('Error loading data:', err);
1092
- return null;
1093
- }
1094
- }
1095
-
1096
- async function loadRecentPaths() {
1097
- try {
1098
- const res = await fetch('/api/recent-paths');
1099
- if (!res.ok) return [];
1100
- const data = await res.json();
1101
- return data.paths || [];
1102
- } catch (err) {
1103
- return [];
1104
- }
1105
- }
1106
-
1107
- `;
1108
-
1109
- // Prepend server mode script to JS content
1110
- jsContent = serverModeScript + jsContent;
1111
-
1112
- // Inject JS content
1113
- html = html.replace('{{JS_CONTENT}}', jsContent);
1114
-
1115
- // Replace any remaining placeholders in HTML
1116
- html = html.replace(/\{\{PROJECT_PATH\}\}/g, normalizePathForDisplay(initialPath).replace(/\\/g, '/'));
1117
-
1118
- return html;
1119
- }
1120
-
1121
- // ========================================
1122
- // MCP Configuration Functions
1123
- // ========================================
1124
-
1125
- /**
1126
- * Safely read and parse JSON file
1127
- * @param {string} filePath
1128
- * @returns {Object|null}
1129
- */
1130
- function safeReadJson(filePath) {
1131
- try {
1132
- if (!existsSync(filePath)) return null;
1133
- const content = readFileSync(filePath, 'utf8');
1134
- return JSON.parse(content);
1135
- } catch {
1136
- return null;
1137
- }
1138
- }
1139
-
1140
- /**
1141
- * Get MCP servers from a JSON file (expects mcpServers key at top level)
1142
- * @param {string} filePath
1143
- * @returns {Object} mcpServers object or empty object
1144
- */
1145
- function getMcpServersFromFile(filePath) {
1146
- const config = safeReadJson(filePath);
1147
- if (!config) return {};
1148
- return config.mcpServers || {};
1149
- }
1150
-
1151
- /**
1152
- * Get MCP configuration from multiple sources (per official Claude Code docs):
1153
- *
1154
- * Priority (highest to lowest):
1155
- * 1. Enterprise managed-mcp.json (cannot be overridden)
1156
- * 2. Local scope (project-specific private in ~/.claude.json)
1157
- * 3. Project scope (.mcp.json in project root)
1158
- * 4. User scope (mcpServers in ~/.claude.json)
1159
- *
1160
- * Note: ~/.claude/settings.json is for MCP PERMISSIONS, NOT definitions!
1161
- *
1162
- * @returns {Object}
1163
- */
1164
- function getMcpConfig() {
1165
- try {
1166
- const result = {
1167
- projects: {},
1168
- userServers: {}, // User-level servers from ~/.claude.json mcpServers
1169
- enterpriseServers: {}, // Enterprise managed servers (highest priority)
1170
- configSources: [] // Track where configs came from for debugging
1171
- };
1172
-
1173
- // 1. Read Enterprise managed MCP servers (highest priority)
1174
- const enterprisePath = getEnterpriseMcpPath();
1175
- if (existsSync(enterprisePath)) {
1176
- const enterpriseConfig = safeReadJson(enterprisePath);
1177
- if (enterpriseConfig?.mcpServers) {
1178
- result.enterpriseServers = enterpriseConfig.mcpServers;
1179
- result.configSources.push({ type: 'enterprise', path: enterprisePath, count: Object.keys(enterpriseConfig.mcpServers).length });
1180
- }
1181
- }
1182
-
1183
- // 2. Read from ~/.claude.json
1184
- if (existsSync(CLAUDE_CONFIG_PATH)) {
1185
- const claudeConfig = safeReadJson(CLAUDE_CONFIG_PATH);
1186
- if (claudeConfig) {
1187
- // 2a. User-level mcpServers (top-level mcpServers key)
1188
- if (claudeConfig.mcpServers) {
1189
- result.userServers = claudeConfig.mcpServers;
1190
- result.configSources.push({ type: 'user', path: CLAUDE_CONFIG_PATH, count: Object.keys(claudeConfig.mcpServers).length });
1191
- }
1192
-
1193
- // 2b. Project-specific configurations (projects[path].mcpServers)
1194
- if (claudeConfig.projects) {
1195
- result.projects = claudeConfig.projects;
1196
- }
1197
- }
1198
- }
1199
-
1200
- // 3. For each known project, check for .mcp.json (project-level config)
1201
- const projectPaths = Object.keys(result.projects);
1202
- for (const projectPath of projectPaths) {
1203
- const mcpJsonPath = join(projectPath, '.mcp.json');
1204
- if (existsSync(mcpJsonPath)) {
1205
- const mcpJsonConfig = safeReadJson(mcpJsonPath);
1206
- if (mcpJsonConfig?.mcpServers) {
1207
- // Merge .mcp.json servers into project config
1208
- // Project's .mcp.json has lower priority than ~/.claude.json projects[path].mcpServers
1209
- const existingServers = result.projects[projectPath]?.mcpServers || {};
1210
- result.projects[projectPath] = {
1211
- ...result.projects[projectPath],
1212
- mcpServers: {
1213
- ...mcpJsonConfig.mcpServers, // .mcp.json (lower priority)
1214
- ...existingServers // ~/.claude.json projects[path] (higher priority)
1215
- },
1216
- mcpJsonPath: mcpJsonPath // Track source for debugging
1217
- };
1218
- result.configSources.push({ type: 'project-mcp-json', path: mcpJsonPath, count: Object.keys(mcpJsonConfig.mcpServers).length });
1219
- }
1220
- }
1221
- }
1222
-
1223
- // Build globalServers by merging user and enterprise servers
1224
- // Enterprise servers override user servers
1225
- result.globalServers = {
1226
- ...result.userServers,
1227
- ...result.enterpriseServers
1228
- };
1229
-
1230
- return result;
1231
- } catch (error) {
1232
- console.error('Error reading MCP config:', error);
1233
- return { projects: {}, globalServers: {}, userServers: {}, enterpriseServers: {}, configSources: [], error: error.message };
1234
- }
1235
- }
1236
-
1237
- /**
1238
- * Normalize project path for .claude.json (Windows backslash format)
1239
- * @param {string} path
1240
- * @returns {string}
1241
- */
1242
- function normalizeProjectPathForConfig(path) {
1243
- // Convert forward slashes to backslashes for Windows .claude.json format
1244
- let normalized = path.replace(/\//g, '\\');
1245
-
1246
- // Handle /d/path format -> D:\path
1247
- if (normalized.match(/^\\[a-zA-Z]\\/)) {
1248
- normalized = normalized.charAt(1).toUpperCase() + ':' + normalized.slice(2);
1249
- }
1250
-
1251
- return normalized;
1252
- }
1253
-
1254
- /**
1255
- * Toggle MCP server enabled/disabled
1256
- * @param {string} projectPath
1257
- * @param {string} serverName
1258
- * @param {boolean} enable
1259
- * @returns {Object}
1260
- */
1261
- function toggleMcpServerEnabled(projectPath, serverName, enable) {
1262
- try {
1263
- if (!existsSync(CLAUDE_CONFIG_PATH)) {
1264
- return { error: '.claude.json not found' };
1265
- }
1266
-
1267
- const content = readFileSync(CLAUDE_CONFIG_PATH, 'utf8');
1268
- const config = JSON.parse(content);
1269
-
1270
- const normalizedPath = normalizeProjectPathForConfig(projectPath);
1271
-
1272
- if (!config.projects || !config.projects[normalizedPath]) {
1273
- return { error: `Project not found: ${normalizedPath}` };
1274
- }
1275
-
1276
- const projectConfig = config.projects[normalizedPath];
1277
-
1278
- // Ensure disabledMcpServers array exists
1279
- if (!projectConfig.disabledMcpServers) {
1280
- projectConfig.disabledMcpServers = [];
1281
- }
1282
-
1283
- if (enable) {
1284
- // Remove from disabled list
1285
- projectConfig.disabledMcpServers = projectConfig.disabledMcpServers.filter(s => s !== serverName);
1286
- } else {
1287
- // Add to disabled list if not already there
1288
- if (!projectConfig.disabledMcpServers.includes(serverName)) {
1289
- projectConfig.disabledMcpServers.push(serverName);
1290
- }
1291
- }
1292
-
1293
- // Write back to file
1294
- writeFileSync(CLAUDE_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
1295
-
1296
- return {
1297
- success: true,
1298
- serverName,
1299
- enabled: enable,
1300
- disabledMcpServers: projectConfig.disabledMcpServers
1301
- };
1302
- } catch (error) {
1303
- console.error('Error toggling MCP server:', error);
1304
- return { error: error.message };
1305
- }
1306
- }
1307
-
1308
- /**
1309
- * Add MCP server to project
1310
- * @param {string} projectPath
1311
- * @param {string} serverName
1312
- * @param {Object} serverConfig
1313
- * @returns {Object}
1314
- */
1315
- function addMcpServerToProject(projectPath, serverName, serverConfig) {
1316
- try {
1317
- if (!existsSync(CLAUDE_CONFIG_PATH)) {
1318
- return { error: '.claude.json not found' };
1319
- }
1320
-
1321
- const content = readFileSync(CLAUDE_CONFIG_PATH, 'utf8');
1322
- const config = JSON.parse(content);
1323
-
1324
- const normalizedPath = normalizeProjectPathForConfig(projectPath);
1325
-
1326
- // Create project entry if it doesn't exist
1327
- if (!config.projects) {
1328
- config.projects = {};
1329
- }
1330
-
1331
- if (!config.projects[normalizedPath]) {
1332
- config.projects[normalizedPath] = {
1333
- allowedTools: [],
1334
- mcpContextUris: [],
1335
- mcpServers: {},
1336
- enabledMcpjsonServers: [],
1337
- disabledMcpjsonServers: [],
1338
- hasTrustDialogAccepted: false,
1339
- projectOnboardingSeenCount: 0,
1340
- hasClaudeMdExternalIncludesApproved: false,
1341
- hasClaudeMdExternalIncludesWarningShown: false
1342
- };
1343
- }
1344
-
1345
- const projectConfig = config.projects[normalizedPath];
1346
-
1347
- // Ensure mcpServers exists
1348
- if (!projectConfig.mcpServers) {
1349
- projectConfig.mcpServers = {};
1350
- }
1351
-
1352
- // Add the server
1353
- projectConfig.mcpServers[serverName] = serverConfig;
1354
-
1355
- // Write back to file
1356
- writeFileSync(CLAUDE_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
1357
-
1358
- return {
1359
- success: true,
1360
- serverName,
1361
- serverConfig
1362
- };
1363
- } catch (error) {
1364
- console.error('Error adding MCP server:', error);
1365
- return { error: error.message };
1366
- }
1367
- }
1368
-
1369
- /**
1370
- * Remove MCP server from project
1371
- * @param {string} projectPath
1372
- * @param {string} serverName
1373
- * @returns {Object}
1374
- */
1375
- function removeMcpServerFromProject(projectPath, serverName) {
1376
- try {
1377
- if (!existsSync(CLAUDE_CONFIG_PATH)) {
1378
- return { error: '.claude.json not found' };
1379
- }
1380
-
1381
- const content = readFileSync(CLAUDE_CONFIG_PATH, 'utf8');
1382
- const config = JSON.parse(content);
1383
-
1384
- const normalizedPath = normalizeProjectPathForConfig(projectPath);
1385
-
1386
- if (!config.projects || !config.projects[normalizedPath]) {
1387
- return { error: `Project not found: ${normalizedPath}` };
1388
- }
1389
-
1390
- const projectConfig = config.projects[normalizedPath];
1391
-
1392
- if (!projectConfig.mcpServers || !projectConfig.mcpServers[serverName]) {
1393
- return { error: `Server not found: ${serverName}` };
1394
- }
1395
-
1396
- // Remove the server
1397
- delete projectConfig.mcpServers[serverName];
1398
-
1399
- // Also remove from disabled list if present
1400
- if (projectConfig.disabledMcpServers) {
1401
- projectConfig.disabledMcpServers = projectConfig.disabledMcpServers.filter(s => s !== serverName);
1402
- }
1403
-
1404
- // Write back to file
1405
- writeFileSync(CLAUDE_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
1406
-
1407
- return {
1408
- success: true,
1409
- serverName,
1410
- removed: true
1411
- };
1412
- } catch (error) {
1413
- console.error('Error removing MCP server:', error);
1414
- return { error: error.message };
1415
- }
1416
- }
1417
-
1418
- // ========================================
1419
- // Hook Configuration Functions
1420
- // ========================================
1421
-
1422
- const GLOBAL_SETTINGS_PATH = join(homedir(), '.claude', 'settings.json');
1423
-
1424
- /**
1425
- * Get project settings path
1426
- * @param {string} projectPath
1427
- * @returns {string}
1428
- */
1429
- function getProjectSettingsPath(projectPath) {
1430
- const normalizedPath = projectPath.replace(/\//g, '\\').replace(/^\\([a-zA-Z])\\/, '$1:\\');
1431
- return join(normalizedPath, '.claude', 'settings.json');
1432
- }
1433
-
1434
- /**
1435
- * Read settings file safely
1436
- * @param {string} filePath
1437
- * @returns {Object}
1438
- */
1439
- function readSettingsFile(filePath) {
1440
- try {
1441
- if (!existsSync(filePath)) {
1442
- return { hooks: {} };
1443
- }
1444
- const content = readFileSync(filePath, 'utf8');
1445
- return JSON.parse(content);
1446
- } catch (error) {
1447
- console.error(`Error reading settings file ${filePath}:`, error);
1448
- return { hooks: {} };
1449
- }
1450
- }
1451
-
1452
- /**
1453
- * Write settings file safely
1454
- * @param {string} filePath
1455
- * @param {Object} settings
1456
- */
1457
- function writeSettingsFile(filePath, settings) {
1458
- const dirPath = dirname(filePath);
1459
- // Ensure directory exists
1460
- if (!existsSync(dirPath)) {
1461
- mkdirSync(dirPath, { recursive: true });
1462
- }
1463
- writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf8');
1464
- }
1465
-
1466
- /**
1467
- * Get hooks configuration from both global and project settings
1468
- * @param {string} projectPath
1469
- * @returns {Object}
1470
- */
1471
- function getHooksConfig(projectPath) {
1472
- const globalSettings = readSettingsFile(GLOBAL_SETTINGS_PATH);
1473
- const projectSettingsPath = projectPath ? getProjectSettingsPath(projectPath) : null;
1474
- const projectSettings = projectSettingsPath ? readSettingsFile(projectSettingsPath) : { hooks: {} };
1475
-
1476
- return {
1477
- global: {
1478
- path: GLOBAL_SETTINGS_PATH,
1479
- hooks: globalSettings.hooks || {}
1480
- },
1481
- project: {
1482
- path: projectSettingsPath,
1483
- hooks: projectSettings.hooks || {}
1484
- }
1485
- };
1486
- }
1487
-
1488
- /**
1489
- * Save a hook to settings file
1490
- * @param {string} projectPath
1491
- * @param {string} scope - 'global' or 'project'
1492
- * @param {string} event - Hook event type
1493
- * @param {Object} hookData - Hook configuration
1494
- * @returns {Object}
1495
- */
1496
- function saveHookToSettings(projectPath, scope, event, hookData) {
1497
- try {
1498
- const filePath = scope === 'global' ? GLOBAL_SETTINGS_PATH : getProjectSettingsPath(projectPath);
1499
- const settings = readSettingsFile(filePath);
1500
-
1501
- // Ensure hooks object exists
1502
- if (!settings.hooks) {
1503
- settings.hooks = {};
1504
- }
1505
-
1506
- // Ensure the event array exists
1507
- if (!settings.hooks[event]) {
1508
- settings.hooks[event] = [];
1509
- }
1510
-
1511
- // Ensure it's an array
1512
- if (!Array.isArray(settings.hooks[event])) {
1513
- settings.hooks[event] = [settings.hooks[event]];
1514
- }
1515
-
1516
- // Check if we're replacing an existing hook
1517
- if (hookData.replaceIndex !== undefined) {
1518
- const index = hookData.replaceIndex;
1519
- delete hookData.replaceIndex;
1520
- if (index >= 0 && index < settings.hooks[event].length) {
1521
- settings.hooks[event][index] = hookData;
1522
- }
1523
- } else {
1524
- // Add new hook
1525
- settings.hooks[event].push(hookData);
1526
- }
1527
-
1528
- // Ensure directory exists and write file
1529
- const dirPath = dirname(filePath);
1530
- if (!existsSync(dirPath)) {
1531
- mkdirSync(dirPath, { recursive: true });
1532
- }
1533
- writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf8');
1534
-
1535
- return {
1536
- success: true,
1537
- event,
1538
- hookData
1539
- };
1540
- } catch (error) {
1541
- console.error('Error saving hook:', error);
1542
- return { error: error.message };
1543
- }
1544
- }
1545
-
1546
- /**
1547
- * Delete a hook from settings file
1548
- * @param {string} projectPath
1549
- * @param {string} scope - 'global' or 'project'
1550
- * @param {string} event - Hook event type
1551
- * @param {number} hookIndex - Index of hook to delete
1552
- * @returns {Object}
1553
- */
1554
- function deleteHookFromSettings(projectPath, scope, event, hookIndex) {
1555
- try {
1556
- const filePath = scope === 'global' ? GLOBAL_SETTINGS_PATH : getProjectSettingsPath(projectPath);
1557
- const settings = readSettingsFile(filePath);
1558
-
1559
- if (!settings.hooks || !settings.hooks[event]) {
1560
- return { error: 'Hook not found' };
1561
- }
1562
-
1563
- // Ensure it's an array
1564
- if (!Array.isArray(settings.hooks[event])) {
1565
- settings.hooks[event] = [settings.hooks[event]];
1566
- }
1567
-
1568
- if (hookIndex < 0 || hookIndex >= settings.hooks[event].length) {
1569
- return { error: 'Invalid hook index' };
1570
- }
1571
-
1572
- // Remove the hook
1573
- settings.hooks[event].splice(hookIndex, 1);
1574
-
1575
- // Remove empty event arrays
1576
- if (settings.hooks[event].length === 0) {
1577
- delete settings.hooks[event];
1578
- }
1579
-
1580
- writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf8');
1581
-
1582
- return {
1583
- success: true,
1584
- event,
1585
- hookIndex
1586
- };
1587
- } catch (error) {
1588
- console.error('Error deleting hook:', error);
1589
- return { error: error.message };
1590
- }
1591
- }
1592
-
1593
- // ========================================
1594
- // Explorer View Functions
1595
- // ========================================
1596
-
1597
- // Directories to always exclude from file tree
1598
- const EXPLORER_EXCLUDE_DIRS = [
1599
- '.git', '__pycache__', 'node_modules', '.venv', 'venv', 'env',
1600
- 'dist', 'build', '.cache', '.pytest_cache', '.mypy_cache',
1601
- 'coverage', '.nyc_output', 'logs', 'tmp', 'temp', '.next',
1602
- '.nuxt', '.output', '.turbo', '.parcel-cache'
1603
- ];
1604
-
1605
- // File extensions to language mapping for syntax highlighting
1606
- const EXT_TO_LANGUAGE = {
1607
- '.js': 'javascript',
1608
- '.jsx': 'javascript',
1609
- '.ts': 'typescript',
1610
- '.tsx': 'typescript',
1611
- '.py': 'python',
1612
- '.rb': 'ruby',
1613
- '.java': 'java',
1614
- '.go': 'go',
1615
- '.rs': 'rust',
1616
- '.c': 'c',
1617
- '.cpp': 'cpp',
1618
- '.h': 'c',
1619
- '.hpp': 'cpp',
1620
- '.cs': 'csharp',
1621
- '.php': 'php',
1622
- '.swift': 'swift',
1623
- '.kt': 'kotlin',
1624
- '.scala': 'scala',
1625
- '.sh': 'bash',
1626
- '.bash': 'bash',
1627
- '.zsh': 'bash',
1628
- '.ps1': 'powershell',
1629
- '.sql': 'sql',
1630
- '.html': 'html',
1631
- '.htm': 'html',
1632
- '.css': 'css',
1633
- '.scss': 'scss',
1634
- '.sass': 'sass',
1635
- '.less': 'less',
1636
- '.json': 'json',
1637
- '.xml': 'xml',
1638
- '.yaml': 'yaml',
1639
- '.yml': 'yaml',
1640
- '.toml': 'toml',
1641
- '.ini': 'ini',
1642
- '.cfg': 'ini',
1643
- '.conf': 'nginx',
1644
- '.md': 'markdown',
1645
- '.markdown': 'markdown',
1646
- '.txt': 'plaintext',
1647
- '.log': 'plaintext',
1648
- '.env': 'bash',
1649
- '.dockerfile': 'dockerfile',
1650
- '.vue': 'html',
1651
- '.svelte': 'html'
1652
- };
1653
-
1654
- /**
1655
- * Parse .gitignore file and return patterns
1656
- * @param {string} gitignorePath - Path to .gitignore file
1657
- * @returns {string[]} Array of gitignore patterns
1658
- */
1659
- function parseGitignore(gitignorePath) {
1660
- try {
1661
- if (!existsSync(gitignorePath)) return [];
1662
- const content = readFileSync(gitignorePath, 'utf8');
1663
- return content
1664
- .split('\n')
1665
- .map(line => line.trim())
1666
- .filter(line => line && !line.startsWith('#'));
1667
- } catch {
1668
- return [];
1669
- }
1670
- }
1671
-
1672
- /**
1673
- * Check if a file/directory should be ignored based on gitignore patterns
1674
- * Simple pattern matching (supports basic glob patterns)
1675
- * @param {string} name - File or directory name
1676
- * @param {string[]} patterns - Gitignore patterns
1677
- * @param {boolean} isDirectory - Whether the entry is a directory
1678
- * @returns {boolean}
1679
- */
1680
- function shouldIgnore(name, patterns, isDirectory) {
1681
- // Always exclude certain directories
1682
- if (isDirectory && EXPLORER_EXCLUDE_DIRS.includes(name)) {
1683
- return true;
1684
- }
1685
-
1686
- // Skip hidden files/directories (starting with .)
1687
- if (name.startsWith('.') && name !== '.claude' && name !== '.workflow') {
1688
- return true;
1689
- }
1690
-
1691
- for (const pattern of patterns) {
1692
- let p = pattern;
1693
-
1694
- // Handle negation patterns (we skip them for simplicity)
1695
- if (p.startsWith('!')) continue;
1696
-
1697
- // Handle directory-only patterns
1698
- if (p.endsWith('/')) {
1699
- if (!isDirectory) continue;
1700
- p = p.slice(0, -1);
1701
- }
1702
-
1703
- // Simple pattern matching
1704
- if (p === name) return true;
1705
-
1706
- // Handle wildcard patterns
1707
- if (p.includes('*')) {
1708
- const regex = new RegExp('^' + p.replace(/\./g, '\\.').replace(/\*/g, '.*') + '$');
1709
- if (regex.test(name)) return true;
1710
- }
1711
-
1712
- // Handle extension patterns like *.log
1713
- if (p.startsWith('*.')) {
1714
- const ext = p.slice(1);
1715
- if (name.endsWith(ext)) return true;
1716
- }
1717
- }
1718
-
1719
- return false;
1720
- }
1721
-
1722
- /**
1723
- * List directory files with .gitignore filtering
1724
- * @param {string} dirPath - Directory path to list
1725
- * @returns {Promise<Object>}
1726
- */
1727
- async function listDirectoryFiles(dirPath) {
1728
- try {
1729
- // Normalize path
1730
- let normalizedPath = dirPath.replace(/\\/g, '/');
1731
- if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
1732
- normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
1733
- }
1734
-
1735
- if (!existsSync(normalizedPath)) {
1736
- return { error: 'Directory not found', files: [] };
1737
- }
1738
-
1739
- if (!statSync(normalizedPath).isDirectory()) {
1740
- return { error: 'Not a directory', files: [] };
1741
- }
1742
-
1743
- // Parse .gitignore patterns
1744
- const gitignorePath = join(normalizedPath, '.gitignore');
1745
- const gitignorePatterns = parseGitignore(gitignorePath);
1746
-
1747
- // Read directory entries
1748
- const entries = readdirSync(normalizedPath, { withFileTypes: true });
1749
-
1750
- const files = [];
1751
- for (const entry of entries) {
1752
- const isDirectory = entry.isDirectory();
1753
-
1754
- // Check if should be ignored
1755
- if (shouldIgnore(entry.name, gitignorePatterns, isDirectory)) {
1756
- continue;
1757
- }
1758
-
1759
- const entryPath = join(normalizedPath, entry.name);
1760
- const fileInfo = {
1761
- name: entry.name,
1762
- type: isDirectory ? 'directory' : 'file',
1763
- path: entryPath.replace(/\\/g, '/')
1764
- };
1765
-
1766
- // Check if directory has CLAUDE.md
1767
- if (isDirectory) {
1768
- const claudeMdPath = join(entryPath, 'CLAUDE.md');
1769
- fileInfo.hasClaudeMd = existsSync(claudeMdPath);
1770
- }
1771
-
1772
- files.push(fileInfo);
1773
- }
1774
-
1775
- // Sort: directories first, then alphabetically
1776
- files.sort((a, b) => {
1777
- if (a.type === 'directory' && b.type !== 'directory') return -1;
1778
- if (a.type !== 'directory' && b.type === 'directory') return 1;
1779
- return a.name.localeCompare(b.name);
1780
- });
1781
-
1782
- return {
1783
- path: normalizedPath.replace(/\\/g, '/'),
1784
- files,
1785
- gitignorePatterns
1786
- };
1787
- } catch (error) {
1788
- console.error('Error listing directory:', error);
1789
- return { error: error.message, files: [] };
1790
- }
1791
- }
1792
-
1793
- /**
1794
- * Get file content for preview
1795
- * @param {string} filePath - Path to file
1796
- * @returns {Promise<Object>}
1797
- */
1798
- async function getFileContent(filePath) {
1799
- try {
1800
- // Normalize path
1801
- let normalizedPath = filePath.replace(/\\/g, '/');
1802
- if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
1803
- normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
1804
- }
1805
-
1806
- if (!existsSync(normalizedPath)) {
1807
- return { error: 'File not found' };
1808
- }
1809
-
1810
- const stats = statSync(normalizedPath);
1811
- if (stats.isDirectory()) {
1812
- return { error: 'Cannot read directory' };
1813
- }
1814
-
1815
- // Check file size (limit to 1MB for preview)
1816
- if (stats.size > 1024 * 1024) {
1817
- return { error: 'File too large for preview (max 1MB)', size: stats.size };
1818
- }
1819
-
1820
- // Read file content
1821
- const content = readFileSync(normalizedPath, 'utf8');
1822
- const ext = normalizedPath.substring(normalizedPath.lastIndexOf('.')).toLowerCase();
1823
- const language = EXT_TO_LANGUAGE[ext] || 'plaintext';
1824
- const isMarkdown = ext === '.md' || ext === '.markdown';
1825
- const fileName = normalizedPath.split('/').pop();
1826
-
1827
- return {
1828
- content,
1829
- language,
1830
- isMarkdown,
1831
- fileName,
1832
- path: normalizedPath,
1833
- size: stats.size,
1834
- lines: content.split('\n').length
1835
- };
1836
- } catch (error) {
1837
- console.error('Error reading file:', error);
1838
- return { error: error.message };
1839
- }
1840
- }
1841
-
1842
- /**
1843
- * Trigger update-module-claude tool (async execution)
1844
- * @param {string} targetPath - Directory path to update
1845
- * @param {string} tool - CLI tool to use (gemini, qwen, codex)
1846
- * @param {string} strategy - Update strategy (single-layer, multi-layer)
1847
- * @returns {Promise<Object>}
1848
- */
1849
- async function triggerUpdateClaudeMd(targetPath, tool, strategy) {
1850
- const { spawn } = await import('child_process');
1851
-
1852
- // Normalize path
1853
- let normalizedPath = targetPath.replace(/\\/g, '/');
1854
- if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
1855
- normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
1856
- }
1857
-
1858
- if (!existsSync(normalizedPath)) {
1859
- return { error: 'Directory not found' };
1860
- }
1861
-
1862
- if (!statSync(normalizedPath).isDirectory()) {
1863
- return { error: 'Not a directory' };
1864
- }
1865
-
1866
- // Build ccw tool command with JSON parameters
1867
- const params = JSON.stringify({
1868
- strategy,
1869
- path: normalizedPath,
1870
- tool
1871
- });
1872
-
1873
- console.log(`[Explorer] Running async: ccw tool exec update_module_claude with ${tool} (${strategy})`);
1874
-
1875
- return new Promise((resolve) => {
1876
- const isWindows = process.platform === 'win32';
1877
-
1878
- // Spawn the process
1879
- const child = spawn('ccw', ['tool', 'exec', 'update_module_claude', params], {
1880
- cwd: normalizedPath,
1881
- shell: isWindows,
1882
- stdio: ['ignore', 'pipe', 'pipe']
1883
- });
1884
-
1885
- let stdout = '';
1886
- let stderr = '';
1887
-
1888
- child.stdout.on('data', (data) => {
1889
- stdout += data.toString();
1890
- });
1891
-
1892
- child.stderr.on('data', (data) => {
1893
- stderr += data.toString();
1894
- });
1895
-
1896
- child.on('close', (code) => {
1897
- if (code === 0) {
1898
- // Parse the JSON output from the tool
1899
- let result;
1900
- try {
1901
- result = JSON.parse(stdout);
1902
- } catch {
1903
- result = { output: stdout };
1904
- }
1905
-
1906
- if (result.success === false || result.error) {
1907
- resolve({
1908
- success: false,
1909
- error: result.error || result.message || 'Update failed',
1910
- output: stdout
1911
- });
1912
- } else {
1913
- resolve({
1914
- success: true,
1915
- message: result.message || `CLAUDE.md updated successfully using ${tool} (${strategy})`,
1916
- output: stdout,
1917
- path: normalizedPath
1918
- });
1919
- }
1920
- } else {
1921
- resolve({
1922
- success: false,
1923
- error: stderr || `Process exited with code ${code}`,
1924
- output: stdout + stderr
1925
- });
1926
- }
1927
- });
1928
-
1929
- child.on('error', (error) => {
1930
- console.error('Error spawning process:', error);
1931
- resolve({
1932
- success: false,
1933
- error: error.message,
1934
- output: ''
1935
- });
1936
- });
1937
-
1938
- // Timeout after 5 minutes
1939
- setTimeout(() => {
1940
- child.kill();
1941
- resolve({
1942
- success: false,
1943
- error: 'Timeout: Process took longer than 5 minutes',
1944
- output: stdout
1945
- });
1946
- }, 300000);
1947
- });
1948
- }
1
+ import http from 'http';
2
+ import { URL } from 'url';
3
+ import { readFileSync, writeFileSync, existsSync, readdirSync, mkdirSync, statSync, promises as fsPromises } from 'fs';
4
+ import { join, dirname } from 'path';
5
+ import { homedir } from 'os';
6
+ import { createHash } from 'crypto';
7
+ import { scanSessions } from './session-scanner.js';
8
+ import { aggregateData } from './data-aggregator.js';
9
+ import { resolvePath, getRecentPaths, trackRecentPath, removeRecentPath, normalizePathForDisplay, getWorkflowDir } from '../utils/path-resolver.js';
10
+
11
+ // Claude config file paths
12
+ const CLAUDE_CONFIG_PATH = join(homedir(), '.claude.json');
13
+ const CLAUDE_SETTINGS_DIR = join(homedir(), '.claude');
14
+ const CLAUDE_GLOBAL_SETTINGS = join(CLAUDE_SETTINGS_DIR, 'settings.json');
15
+ const CLAUDE_GLOBAL_SETTINGS_LOCAL = join(CLAUDE_SETTINGS_DIR, 'settings.local.json');
16
+
17
+ // Enterprise managed MCP paths (platform-specific)
18
+ function getEnterpriseMcpPath() {
19
+ const platform = process.platform;
20
+ if (platform === 'darwin') {
21
+ return '/Library/Application Support/ClaudeCode/managed-mcp.json';
22
+ } else if (platform === 'win32') {
23
+ return 'C:\\Program Files\\ClaudeCode\\managed-mcp.json';
24
+ } else {
25
+ // Linux and WSL
26
+ return '/etc/claude-code/managed-mcp.json';
27
+ }
28
+ }
29
+
30
+ // WebSocket clients for real-time notifications
31
+ const wsClients = new Set();
32
+
33
+ const TEMPLATE_PATH = join(import.meta.dirname, '../templates/dashboard.html');
34
+ const MODULE_CSS_DIR = join(import.meta.dirname, '../templates/dashboard-css');
35
+ const JS_FILE = join(import.meta.dirname, '../templates/dashboard.js');
36
+ const MODULE_JS_DIR = join(import.meta.dirname, '../templates/dashboard-js');
37
+
38
+ // Modular CSS files in load order
39
+ const MODULE_CSS_FILES = [
40
+ '01-base.css',
41
+ '02-session.css',
42
+ '03-tasks.css',
43
+ '04-lite-tasks.css',
44
+ '05-context.css',
45
+ '06-cards.css',
46
+ '07-managers.css',
47
+ '08-review.css',
48
+ '09-explorer.css'
49
+ ];
50
+
51
+ /**
52
+ * Handle POST request with JSON body
53
+ */
54
+ function handlePostRequest(req, res, handler) {
55
+ let body = '';
56
+ req.on('data', chunk => { body += chunk; });
57
+ req.on('end', async () => {
58
+ try {
59
+ const parsed = JSON.parse(body);
60
+ const result = await handler(parsed);
61
+
62
+ if (result.error) {
63
+ const status = result.status || 500;
64
+ res.writeHead(status, { 'Content-Type': 'application/json' });
65
+ res.end(JSON.stringify({ error: result.error }));
66
+ } else {
67
+ res.writeHead(200, { 'Content-Type': 'application/json' });
68
+ res.end(JSON.stringify(result));
69
+ }
70
+ } catch (error) {
71
+ res.writeHead(500, { 'Content-Type': 'application/json' });
72
+ res.end(JSON.stringify({ error: error.message }));
73
+ }
74
+ });
75
+ }
76
+
77
+ // Modular JS files in dependency order
78
+ const MODULE_FILES = [
79
+ 'utils.js',
80
+ 'state.js',
81
+ 'api.js',
82
+ 'components/theme.js',
83
+ 'components/modals.js',
84
+ 'components/navigation.js',
85
+ 'components/sidebar.js',
86
+ 'components/carousel.js',
87
+ 'components/notifications.js',
88
+ 'components/global-notifications.js',
89
+ 'components/version-check.js',
90
+ 'components/mcp-manager.js',
91
+ 'components/hook-manager.js',
92
+ 'components/_exp_helpers.js',
93
+ 'components/tabs-other.js',
94
+ 'components/tabs-context.js',
95
+ 'components/_conflict_tab.js',
96
+ 'components/_review_tab.js',
97
+ 'components/task-drawer-core.js',
98
+ 'components/task-drawer-renderers.js',
99
+ 'components/flowchart.js',
100
+ 'views/home.js',
101
+ 'views/project-overview.js',
102
+ 'views/session-detail.js',
103
+ 'views/review-session.js',
104
+ 'views/lite-tasks.js',
105
+ 'views/fix-session.js',
106
+ 'views/mcp-manager.js',
107
+ 'views/hook-manager.js',
108
+ 'views/explorer.js',
109
+ 'main.js'
110
+ ];
111
+ /**
112
+ * Create and start the dashboard server
113
+ * @param {Object} options - Server options
114
+ * @param {number} options.port - Port to listen on (default: 3456)
115
+ * @param {string} options.initialPath - Initial project path
116
+ * @returns {Promise<http.Server>}
117
+ */
118
+ export async function startServer(options = {}) {
119
+ const port = options.port || 3456;
120
+ const initialPath = options.initialPath || process.cwd();
121
+
122
+ const server = http.createServer(async (req, res) => {
123
+ const url = new URL(req.url, `http://localhost:${port}`);
124
+ const pathname = url.pathname;
125
+
126
+ // CORS headers for API requests
127
+ res.setHeader('Access-Control-Allow-Origin', '*');
128
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
129
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
130
+
131
+ if (req.method === 'OPTIONS') {
132
+ res.writeHead(200);
133
+ res.end();
134
+ return;
135
+ }
136
+
137
+ try {
138
+ // Debug log for API requests
139
+ if (pathname.startsWith('/api/')) {
140
+ console.log(`[API] ${req.method} ${pathname}`);
141
+ }
142
+
143
+ // API: Get workflow data for a path
144
+ if (pathname === '/api/data') {
145
+ const projectPath = url.searchParams.get('path') || initialPath;
146
+ const data = await getWorkflowData(projectPath);
147
+
148
+ res.writeHead(200, { 'Content-Type': 'application/json' });
149
+ res.end(JSON.stringify(data));
150
+ return;
151
+ }
152
+
153
+ // API: Get recent paths
154
+ if (pathname === '/api/recent-paths') {
155
+ const paths = getRecentPaths();
156
+ res.writeHead(200, { 'Content-Type': 'application/json' });
157
+ res.end(JSON.stringify({ paths }));
158
+ return;
159
+ }
160
+
161
+ // API: Switch workspace path (for ccw view command)
162
+ if (pathname === '/api/switch-path') {
163
+ const newPath = url.searchParams.get('path');
164
+ if (!newPath) {
165
+ res.writeHead(400, { 'Content-Type': 'application/json' });
166
+ res.end(JSON.stringify({ error: 'Path is required' }));
167
+ return;
168
+ }
169
+
170
+ const resolved = resolvePath(newPath);
171
+ if (!existsSync(resolved)) {
172
+ res.writeHead(404, { 'Content-Type': 'application/json' });
173
+ res.end(JSON.stringify({ error: 'Path does not exist' }));
174
+ return;
175
+ }
176
+
177
+ // Track the path and return success
178
+ trackRecentPath(resolved);
179
+ res.writeHead(200, { 'Content-Type': 'application/json' });
180
+ res.end(JSON.stringify({
181
+ success: true,
182
+ path: resolved,
183
+ recentPaths: getRecentPaths()
184
+ }));
185
+ return;
186
+ }
187
+
188
+ // API: Health check (for ccw view to detect running server)
189
+ if (pathname === '/api/health') {
190
+ res.writeHead(200, { 'Content-Type': 'application/json' });
191
+ res.end(JSON.stringify({ status: 'ok', timestamp: Date.now() }));
192
+ return;
193
+ }
194
+
195
+ // API: Version check (check for npm updates)
196
+ if (pathname === '/api/version-check') {
197
+ const versionData = await checkNpmVersion();
198
+ res.writeHead(200, { 'Content-Type': 'application/json' });
199
+ res.end(JSON.stringify(versionData));
200
+ return;
201
+ }
202
+
203
+
204
+ // API: Shutdown server (for ccw stop command)
205
+ if (pathname === '/api/shutdown' && req.method === 'POST') {
206
+ res.writeHead(200, { 'Content-Type': 'application/json' });
207
+ res.end(JSON.stringify({ status: 'shutting_down' }));
208
+
209
+ // Graceful shutdown
210
+ console.log('\n Received shutdown signal...');
211
+ setTimeout(() => {
212
+ server.close(() => {
213
+ console.log(' Server stopped.\n');
214
+ process.exit(0);
215
+ });
216
+ // Force exit after 3 seconds if graceful shutdown fails
217
+ setTimeout(() => process.exit(0), 3000);
218
+ }, 100);
219
+ return;
220
+ }
221
+
222
+ // API: Remove a recent path
223
+ if (pathname === '/api/remove-recent-path' && req.method === 'POST') {
224
+ handlePostRequest(req, res, async (body) => {
225
+ const { path } = body;
226
+ if (!path) {
227
+ return { error: 'path is required', status: 400 };
228
+ }
229
+ const removed = removeRecentPath(path);
230
+ return { success: removed, paths: getRecentPaths() };
231
+ });
232
+ return;
233
+ }
234
+
235
+ // API: Read a JSON file (for fix progress tracking)
236
+ if (pathname === '/api/file') {
237
+ const filePath = url.searchParams.get('path');
238
+ if (!filePath) {
239
+ res.writeHead(400, { 'Content-Type': 'application/json' });
240
+ res.end(JSON.stringify({ error: 'File path is required' }));
241
+ return;
242
+ }
243
+
244
+ try {
245
+ const content = await fsPromises.readFile(filePath, 'utf-8');
246
+ const json = JSON.parse(content);
247
+ res.writeHead(200, { 'Content-Type': 'application/json' });
248
+ res.end(JSON.stringify(json));
249
+ } catch (err) {
250
+ res.writeHead(404, { 'Content-Type': 'application/json' });
251
+ res.end(JSON.stringify({ error: 'File not found or invalid JSON' }));
252
+ }
253
+ return;
254
+ }
255
+
256
+ // API: Get session detail data (context, summaries, impl-plan, review)
257
+ if (pathname === '/api/session-detail') {
258
+ const sessionPath = url.searchParams.get('path');
259
+ const dataType = url.searchParams.get('type') || 'all';
260
+
261
+ if (!sessionPath) {
262
+ res.writeHead(400, { 'Content-Type': 'application/json' });
263
+ res.end(JSON.stringify({ error: 'Session path is required' }));
264
+ return;
265
+ }
266
+
267
+ const detail = await getSessionDetailData(sessionPath, dataType);
268
+ res.writeHead(200, { 'Content-Type': 'application/json' });
269
+ res.end(JSON.stringify(detail));
270
+ return;
271
+ }
272
+
273
+ // API: Update task status
274
+ if (pathname === '/api/update-task-status' && req.method === 'POST') {
275
+ handlePostRequest(req, res, async (body) => {
276
+ const { sessionPath, taskId, newStatus } = body;
277
+
278
+ if (!sessionPath || !taskId || !newStatus) {
279
+ return { error: 'sessionPath, taskId, and newStatus are required', status: 400 };
280
+ }
281
+
282
+ return await updateTaskStatus(sessionPath, taskId, newStatus);
283
+ });
284
+ return;
285
+ }
286
+
287
+ // API: Bulk update task status
288
+ if (pathname === '/api/bulk-update-task-status' && req.method === 'POST') {
289
+ handlePostRequest(req, res, async (body) => {
290
+ const { sessionPath, taskIds, newStatus } = body;
291
+
292
+ if (!sessionPath || !taskIds || !newStatus) {
293
+ return { error: 'sessionPath, taskIds, and newStatus are required', status: 400 };
294
+ }
295
+
296
+ const results = [];
297
+ for (const taskId of taskIds) {
298
+ try {
299
+ const result = await updateTaskStatus(sessionPath, taskId, newStatus);
300
+ results.push(result);
301
+ } catch (err) {
302
+ results.push({ taskId, error: err.message });
303
+ }
304
+ }
305
+ return { success: true, results };
306
+ });
307
+ return;
308
+ }
309
+
310
+ // API: Get MCP configuration
311
+ if (pathname === '/api/mcp-config') {
312
+ const mcpData = getMcpConfig();
313
+ res.writeHead(200, { 'Content-Type': 'application/json' });
314
+ res.end(JSON.stringify(mcpData));
315
+ return;
316
+ }
317
+
318
+ // API: Toggle MCP server enabled/disabled
319
+ if (pathname === '/api/mcp-toggle' && req.method === 'POST') {
320
+ handlePostRequest(req, res, async (body) => {
321
+ const { projectPath, serverName, enable } = body;
322
+ if (!projectPath || !serverName) {
323
+ return { error: 'projectPath and serverName are required', status: 400 };
324
+ }
325
+ return toggleMcpServerEnabled(projectPath, serverName, enable);
326
+ });
327
+ return;
328
+ }
329
+
330
+ // API: Copy MCP server to project
331
+ if (pathname === '/api/mcp-copy-server' && req.method === 'POST') {
332
+ handlePostRequest(req, res, async (body) => {
333
+ const { projectPath, serverName, serverConfig } = body;
334
+ if (!projectPath || !serverName || !serverConfig) {
335
+ return { error: 'projectPath, serverName, and serverConfig are required', status: 400 };
336
+ }
337
+ return addMcpServerToProject(projectPath, serverName, serverConfig);
338
+ });
339
+ return;
340
+ }
341
+
342
+ // API: Remove MCP server from project
343
+ if (pathname === '/api/mcp-remove-server' && req.method === 'POST') {
344
+ handlePostRequest(req, res, async (body) => {
345
+ const { projectPath, serverName } = body;
346
+ if (!projectPath || !serverName) {
347
+ return { error: 'projectPath and serverName are required', status: 400 };
348
+ }
349
+ return removeMcpServerFromProject(projectPath, serverName);
350
+ });
351
+ return;
352
+ }
353
+
354
+ // API: Hook endpoint for Claude Code notifications
355
+ if (pathname === '/api/hook' && req.method === 'POST') {
356
+ handlePostRequest(req, res, async (body) => {
357
+ const { type, filePath, sessionId } = body;
358
+
359
+ // Determine session ID from file path if not provided
360
+ let resolvedSessionId = sessionId;
361
+ if (!resolvedSessionId && filePath) {
362
+ resolvedSessionId = extractSessionIdFromPath(filePath);
363
+ }
364
+
365
+ // Broadcast to all connected WebSocket clients
366
+ const notification = {
367
+ type: type || 'session_updated',
368
+ payload: {
369
+ sessionId: resolvedSessionId,
370
+ filePath: filePath,
371
+ timestamp: new Date().toISOString()
372
+ }
373
+ };
374
+
375
+ broadcastToClients(notification);
376
+
377
+ return { success: true, notification };
378
+ });
379
+ return;
380
+ }
381
+
382
+ // API: Get hooks configuration
383
+ if (pathname === '/api/hooks' && req.method === 'GET') {
384
+ const projectPathParam = url.searchParams.get('path');
385
+ const hooksData = getHooksConfig(projectPathParam);
386
+ res.writeHead(200, { 'Content-Type': 'application/json' });
387
+ res.end(JSON.stringify(hooksData));
388
+ return;
389
+ }
390
+
391
+ // API: Save hook
392
+ if (pathname === '/api/hooks' && req.method === 'POST') {
393
+ handlePostRequest(req, res, async (body) => {
394
+ const { projectPath, scope, event, hookData } = body;
395
+ if (!scope || !event || !hookData) {
396
+ return { error: 'scope, event, and hookData are required', status: 400 };
397
+ }
398
+ return saveHookToSettings(projectPath, scope, event, hookData);
399
+ });
400
+ return;
401
+ }
402
+
403
+ // API: Delete hook
404
+ if (pathname === '/api/hooks' && req.method === 'DELETE') {
405
+ handlePostRequest(req, res, async (body) => {
406
+ const { projectPath, scope, event, hookIndex } = body;
407
+ if (!scope || !event || hookIndex === undefined) {
408
+ return { error: 'scope, event, and hookIndex are required', status: 400 };
409
+ }
410
+ return deleteHookFromSettings(projectPath, scope, event, hookIndex);
411
+ });
412
+ return;
413
+ }
414
+
415
+ // API: List directory files with .gitignore filtering (Explorer view)
416
+ if (pathname === '/api/files') {
417
+ const dirPath = url.searchParams.get('path') || initialPath;
418
+ const filesData = await listDirectoryFiles(dirPath);
419
+ res.writeHead(200, { 'Content-Type': 'application/json' });
420
+ res.end(JSON.stringify(filesData));
421
+ return;
422
+ }
423
+
424
+ // API: Get file content for preview (Explorer view)
425
+ if (pathname === '/api/file-content') {
426
+ const filePath = url.searchParams.get('path');
427
+ if (!filePath) {
428
+ res.writeHead(400, { 'Content-Type': 'application/json' });
429
+ res.end(JSON.stringify({ error: 'File path is required' }));
430
+ return;
431
+ }
432
+ const fileData = await getFileContent(filePath);
433
+ res.writeHead(fileData.error ? 404 : 200, { 'Content-Type': 'application/json' });
434
+ res.end(JSON.stringify(fileData));
435
+ return;
436
+ }
437
+
438
+ // API: Update CLAUDE.md using CLI tools (Explorer view)
439
+ if (pathname === '/api/update-claude-md' && req.method === 'POST') {
440
+ handlePostRequest(req, res, async (body) => {
441
+ const { path: targetPath, tool = 'gemini', strategy = 'single-layer' } = body;
442
+ if (!targetPath) {
443
+ return { error: 'path is required', status: 400 };
444
+ }
445
+ return await triggerUpdateClaudeMd(targetPath, tool, strategy);
446
+ });
447
+ return;
448
+ }
449
+
450
+ // Serve dashboard HTML
451
+ if (pathname === '/' || pathname === '/index.html') {
452
+ const html = generateServerDashboard(initialPath);
453
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
454
+ res.end(html);
455
+ return;
456
+ }
457
+
458
+ // 404
459
+ res.writeHead(404, { 'Content-Type': 'text/plain' });
460
+ res.end('Not Found');
461
+
462
+ } catch (error) {
463
+ console.error('Server error:', error);
464
+ res.writeHead(500, { 'Content-Type': 'application/json' });
465
+ res.end(JSON.stringify({ error: error.message }));
466
+ }
467
+ });
468
+
469
+ // Handle WebSocket upgrade requests
470
+ server.on('upgrade', (req, socket, head) => {
471
+ if (req.url === '/ws') {
472
+ handleWebSocketUpgrade(req, socket, head);
473
+ } else {
474
+ socket.destroy();
475
+ }
476
+ });
477
+
478
+ return new Promise((resolve, reject) => {
479
+ server.listen(port, () => {
480
+ console.log(`Dashboard server running at http://localhost:${port}`);
481
+ console.log(`WebSocket endpoint available at ws://localhost:${port}/ws`);
482
+ console.log(`Hook endpoint available at POST http://localhost:${port}/api/hook`);
483
+ resolve(server);
484
+ });
485
+ server.on('error', reject);
486
+ });
487
+ }
488
+
489
+ // ========================================
490
+ // WebSocket Functions
491
+ // ========================================
492
+
493
+ /**
494
+ * Handle WebSocket upgrade
495
+ */
496
+ function handleWebSocketUpgrade(req, socket, head) {
497
+ const key = req.headers['sec-websocket-key'];
498
+ const acceptKey = createHash('sha1')
499
+ .update(key + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')
500
+ .digest('base64');
501
+
502
+ const responseHeaders = [
503
+ 'HTTP/1.1 101 Switching Protocols',
504
+ 'Upgrade: websocket',
505
+ 'Connection: Upgrade',
506
+ `Sec-WebSocket-Accept: ${acceptKey}`,
507
+ '',
508
+ ''
509
+ ].join('\r\n');
510
+
511
+ socket.write(responseHeaders);
512
+
513
+ // Add to clients set
514
+ wsClients.add(socket);
515
+ console.log(`[WS] Client connected (${wsClients.size} total)`);
516
+
517
+ // Handle incoming messages
518
+ socket.on('data', (buffer) => {
519
+ try {
520
+ const frame = parseWebSocketFrame(buffer);
521
+ if (!frame) return;
522
+
523
+ const { opcode, payload } = frame;
524
+
525
+ switch (opcode) {
526
+ case 0x1: // Text frame
527
+ if (payload) {
528
+ console.log('[WS] Received:', payload);
529
+ }
530
+ break;
531
+ case 0x8: // Close frame
532
+ socket.end();
533
+ break;
534
+ case 0x9: // Ping frame - respond with Pong
535
+ const pongFrame = Buffer.alloc(2);
536
+ pongFrame[0] = 0x8A; // Pong opcode with FIN bit
537
+ pongFrame[1] = 0x00; // No payload
538
+ socket.write(pongFrame);
539
+ break;
540
+ case 0xA: // Pong frame - ignore
541
+ break;
542
+ default:
543
+ // Ignore other frame types (binary, continuation)
544
+ break;
545
+ }
546
+ } catch (e) {
547
+ // Ignore parse errors
548
+ }
549
+ });
550
+
551
+ // Handle disconnect
552
+ socket.on('close', () => {
553
+ wsClients.delete(socket);
554
+ console.log(`[WS] Client disconnected (${wsClients.size} remaining)`);
555
+ });
556
+
557
+ socket.on('error', () => {
558
+ wsClients.delete(socket);
559
+ });
560
+ }
561
+
562
+ /**
563
+ * Parse WebSocket frame (simplified)
564
+ * Returns { opcode, payload } or null
565
+ */
566
+ function parseWebSocketFrame(buffer) {
567
+ if (buffer.length < 2) return null;
568
+
569
+ const firstByte = buffer[0];
570
+ const opcode = firstByte & 0x0f; // Extract opcode (bits 0-3)
571
+
572
+ // Opcode types:
573
+ // 0x0 = continuation, 0x1 = text, 0x2 = binary
574
+ // 0x8 = close, 0x9 = ping, 0xA = pong
575
+
576
+ const secondByte = buffer[1];
577
+ const isMasked = (secondByte & 0x80) !== 0;
578
+ let payloadLength = secondByte & 0x7f;
579
+
580
+ let offset = 2;
581
+ if (payloadLength === 126) {
582
+ payloadLength = buffer.readUInt16BE(2);
583
+ offset = 4;
584
+ } else if (payloadLength === 127) {
585
+ payloadLength = Number(buffer.readBigUInt64BE(2));
586
+ offset = 10;
587
+ }
588
+
589
+ let mask = null;
590
+ if (isMasked) {
591
+ mask = buffer.slice(offset, offset + 4);
592
+ offset += 4;
593
+ }
594
+
595
+ const payload = buffer.slice(offset, offset + payloadLength);
596
+
597
+ if (isMasked && mask) {
598
+ for (let i = 0; i < payload.length; i++) {
599
+ payload[i] ^= mask[i % 4];
600
+ }
601
+ }
602
+
603
+ return { opcode, payload: payload.toString('utf8') };
604
+ }
605
+
606
+ /**
607
+ * Create WebSocket frame
608
+ */
609
+ function createWebSocketFrame(data) {
610
+ const payload = Buffer.from(JSON.stringify(data), 'utf8');
611
+ const length = payload.length;
612
+
613
+ let frame;
614
+ if (length <= 125) {
615
+ frame = Buffer.alloc(2 + length);
616
+ frame[0] = 0x81; // Text frame, FIN
617
+ frame[1] = length;
618
+ payload.copy(frame, 2);
619
+ } else if (length <= 65535) {
620
+ frame = Buffer.alloc(4 + length);
621
+ frame[0] = 0x81;
622
+ frame[1] = 126;
623
+ frame.writeUInt16BE(length, 2);
624
+ payload.copy(frame, 4);
625
+ } else {
626
+ frame = Buffer.alloc(10 + length);
627
+ frame[0] = 0x81;
628
+ frame[1] = 127;
629
+ frame.writeBigUInt64BE(BigInt(length), 2);
630
+ payload.copy(frame, 10);
631
+ }
632
+
633
+ return frame;
634
+ }
635
+
636
+ /**
637
+ * Broadcast message to all connected WebSocket clients
638
+ */
639
+ function broadcastToClients(data) {
640
+ const frame = createWebSocketFrame(data);
641
+
642
+ for (const client of wsClients) {
643
+ try {
644
+ client.write(frame);
645
+ } catch (e) {
646
+ wsClients.delete(client);
647
+ }
648
+ }
649
+
650
+ console.log(`[WS] Broadcast to ${wsClients.size} clients:`, data.type);
651
+ }
652
+
653
+ /**
654
+ * Extract session ID from file path
655
+ */
656
+ function extractSessionIdFromPath(filePath) {
657
+ // Normalize path
658
+ const normalized = filePath.replace(/\\/g, '/');
659
+
660
+ // Look for session pattern: WFS-xxx, WRS-xxx, etc.
661
+ const sessionMatch = normalized.match(/\/(W[A-Z]S-[^/]+)\//);
662
+ if (sessionMatch) {
663
+ return sessionMatch[1];
664
+ }
665
+
666
+ // Look for .workflow/.sessions/xxx pattern
667
+ const sessionsMatch = normalized.match(/\.workflow\/\.sessions\/([^/]+)/);
668
+ if (sessionsMatch) {
669
+ return sessionsMatch[1];
670
+ }
671
+
672
+ // Look for lite-plan/lite-fix pattern
673
+ const liteMatch = normalized.match(/\.(lite-plan|lite-fix)\/([^/]+)/);
674
+ if (liteMatch) {
675
+ return liteMatch[2];
676
+ }
677
+
678
+ return null;
679
+ }
680
+
681
+ /**
682
+ * Get workflow data for a project path
683
+ * @param {string} projectPath
684
+ * @returns {Promise<Object>}
685
+ */
686
+ async function getWorkflowData(projectPath) {
687
+ const resolvedPath = resolvePath(projectPath);
688
+ const workflowDir = join(resolvedPath, '.workflow');
689
+
690
+ // Track this path
691
+ trackRecentPath(resolvedPath);
692
+
693
+ // Check if .workflow exists
694
+ if (!existsSync(workflowDir)) {
695
+ return {
696
+ generatedAt: new Date().toISOString(),
697
+ activeSessions: [],
698
+ archivedSessions: [],
699
+ liteTasks: { litePlan: [], liteFix: [] },
700
+ reviewData: { dimensions: {} },
701
+ projectOverview: null,
702
+ statistics: {
703
+ totalSessions: 0,
704
+ activeSessions: 0,
705
+ totalTasks: 0,
706
+ completedTasks: 0,
707
+ reviewFindings: 0,
708
+ litePlanCount: 0,
709
+ liteFixCount: 0
710
+ },
711
+ projectPath: normalizePathForDisplay(resolvedPath),
712
+ recentPaths: getRecentPaths()
713
+ };
714
+ }
715
+
716
+ // Scan and aggregate data
717
+ const sessions = await scanSessions(workflowDir);
718
+ const data = await aggregateData(sessions, workflowDir);
719
+
720
+ data.projectPath = normalizePathForDisplay(resolvedPath);
721
+ data.recentPaths = getRecentPaths();
722
+
723
+ return data;
724
+ }
725
+
726
+ /**
727
+ * Get session detail data (context, summaries, impl-plan, review)
728
+ * @param {string} sessionPath - Path to session directory
729
+ * @param {string} dataType - Type of data to load: context, summary, impl-plan, review, or all
730
+ * @returns {Promise<Object>}
731
+ */
732
+ async function getSessionDetailData(sessionPath, dataType) {
733
+ const result = {};
734
+
735
+ // Normalize path
736
+ const normalizedPath = sessionPath.replace(/\\/g, '/');
737
+
738
+ try {
739
+ // Load context-package.json (in .process/ subfolder)
740
+ if (dataType === 'context' || dataType === 'all') {
741
+ // Try .process/context-package.json first (common location)
742
+ let contextFile = join(normalizedPath, '.process', 'context-package.json');
743
+ if (!existsSync(contextFile)) {
744
+ // Fallback to session root
745
+ contextFile = join(normalizedPath, 'context-package.json');
746
+ }
747
+ if (existsSync(contextFile)) {
748
+ try {
749
+ result.context = JSON.parse(readFileSync(contextFile, 'utf8'));
750
+ } catch (e) {
751
+ result.context = null;
752
+ }
753
+ }
754
+ }
755
+
756
+ // Load task JSONs from .task/ folder
757
+ if (dataType === 'tasks' || dataType === 'all') {
758
+ const taskDir = join(normalizedPath, '.task');
759
+ result.tasks = [];
760
+ if (existsSync(taskDir)) {
761
+ const files = readdirSync(taskDir).filter(f => f.endsWith('.json') && f.startsWith('IMPL-'));
762
+ for (const file of files) {
763
+ try {
764
+ const content = JSON.parse(readFileSync(join(taskDir, file), 'utf8'));
765
+ result.tasks.push({
766
+ filename: file,
767
+ task_id: file.replace('.json', ''),
768
+ ...content
769
+ });
770
+ } catch (e) {
771
+ // Skip unreadable files
772
+ }
773
+ }
774
+ // Sort by task ID
775
+ result.tasks.sort((a, b) => a.task_id.localeCompare(b.task_id));
776
+ }
777
+ }
778
+
779
+ // Load summaries from .summaries/
780
+ if (dataType === 'summary' || dataType === 'all') {
781
+ const summariesDir = join(normalizedPath, '.summaries');
782
+ result.summaries = [];
783
+ if (existsSync(summariesDir)) {
784
+ const files = readdirSync(summariesDir).filter(f => f.endsWith('.md'));
785
+ for (const file of files) {
786
+ try {
787
+ const content = readFileSync(join(summariesDir, file), 'utf8');
788
+ result.summaries.push({ name: file.replace('.md', ''), content });
789
+ } catch (e) {
790
+ // Skip unreadable files
791
+ }
792
+ }
793
+ }
794
+ }
795
+
796
+ // Load plan.json (for lite tasks)
797
+ if (dataType === 'plan' || dataType === 'all') {
798
+ const planFile = join(normalizedPath, 'plan.json');
799
+ if (existsSync(planFile)) {
800
+ try {
801
+ result.plan = JSON.parse(readFileSync(planFile, 'utf8'));
802
+ } catch (e) {
803
+ result.plan = null;
804
+ }
805
+ }
806
+ }
807
+
808
+ // Load explorations (exploration-*.json files) - check .process/ first, then session root
809
+ if (dataType === 'context' || dataType === 'explorations' || dataType === 'all') {
810
+ result.explorations = { manifest: null, data: {} };
811
+
812
+ // Try .process/ first (standard workflow sessions), then session root (lite tasks)
813
+ const searchDirs = [
814
+ join(normalizedPath, '.process'),
815
+ normalizedPath
816
+ ];
817
+
818
+ for (const searchDir of searchDirs) {
819
+ if (!existsSync(searchDir)) continue;
820
+
821
+ // Look for explorations-manifest.json
822
+ const manifestFile = join(searchDir, 'explorations-manifest.json');
823
+ if (existsSync(manifestFile)) {
824
+ try {
825
+ result.explorations.manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
826
+
827
+ // Load each exploration file based on manifest
828
+ const explorations = result.explorations.manifest.explorations || [];
829
+ for (const exp of explorations) {
830
+ const expFile = join(searchDir, exp.file);
831
+ if (existsSync(expFile)) {
832
+ try {
833
+ result.explorations.data[exp.angle] = JSON.parse(readFileSync(expFile, 'utf8'));
834
+ } catch (e) {
835
+ // Skip unreadable exploration files
836
+ }
837
+ }
838
+ }
839
+ break; // Found manifest, stop searching
840
+ } catch (e) {
841
+ result.explorations.manifest = null;
842
+ }
843
+ } else {
844
+ // Fallback: scan for exploration-*.json files directly
845
+ try {
846
+ const files = readdirSync(searchDir).filter(f => f.startsWith('exploration-') && f.endsWith('.json'));
847
+ if (files.length > 0) {
848
+ // Create synthetic manifest
849
+ result.explorations.manifest = {
850
+ exploration_count: files.length,
851
+ explorations: files.map((f, i) => ({
852
+ angle: f.replace('exploration-', '').replace('.json', ''),
853
+ file: f,
854
+ index: i + 1
855
+ }))
856
+ };
857
+
858
+ // Load each file
859
+ for (const file of files) {
860
+ const angle = file.replace('exploration-', '').replace('.json', '');
861
+ try {
862
+ result.explorations.data[angle] = JSON.parse(readFileSync(join(searchDir, file), 'utf8'));
863
+ } catch (e) {
864
+ // Skip unreadable files
865
+ }
866
+ }
867
+ break; // Found explorations, stop searching
868
+ }
869
+ } catch (e) {
870
+ // Directory read failed
871
+ }
872
+ }
873
+ }
874
+ }
875
+
876
+ // Load conflict resolution decisions (conflict-resolution-decisions.json)
877
+ if (dataType === 'context' || dataType === 'conflict' || dataType === 'all') {
878
+ result.conflictResolution = null;
879
+
880
+ // Try .process/ first (standard workflow sessions)
881
+ const conflictFiles = [
882
+ join(normalizedPath, '.process', 'conflict-resolution-decisions.json'),
883
+ join(normalizedPath, 'conflict-resolution-decisions.json')
884
+ ];
885
+
886
+ for (const conflictFile of conflictFiles) {
887
+ if (existsSync(conflictFile)) {
888
+ try {
889
+ result.conflictResolution = JSON.parse(readFileSync(conflictFile, 'utf8'));
890
+ break; // Found file, stop searching
891
+ } catch (e) {
892
+ // Skip unreadable file
893
+ }
894
+ }
895
+ }
896
+ }
897
+
898
+ // Load IMPL_PLAN.md
899
+ if (dataType === 'impl-plan' || dataType === 'all') {
900
+ const implPlanFile = join(normalizedPath, 'IMPL_PLAN.md');
901
+ if (existsSync(implPlanFile)) {
902
+ try {
903
+ result.implPlan = readFileSync(implPlanFile, 'utf8');
904
+ } catch (e) {
905
+ result.implPlan = null;
906
+ }
907
+ }
908
+ }
909
+
910
+ // Load review data from .review/
911
+ if (dataType === 'review' || dataType === 'all') {
912
+ const reviewDir = join(normalizedPath, '.review');
913
+ result.review = {
914
+ state: null,
915
+ dimensions: [],
916
+ severityDistribution: null,
917
+ totalFindings: 0
918
+ };
919
+
920
+ if (existsSync(reviewDir)) {
921
+ // Load review-state.json
922
+ const stateFile = join(reviewDir, 'review-state.json');
923
+ if (existsSync(stateFile)) {
924
+ try {
925
+ const state = JSON.parse(readFileSync(stateFile, 'utf8'));
926
+ result.review.state = state;
927
+ result.review.severityDistribution = state.severity_distribution || {};
928
+ result.review.totalFindings = state.total_findings || 0;
929
+ result.review.phase = state.phase || 'unknown';
930
+ result.review.dimensionSummaries = state.dimension_summaries || {};
931
+ result.review.crossCuttingConcerns = state.cross_cutting_concerns || [];
932
+ result.review.criticalFiles = state.critical_files || [];
933
+ } catch (e) {
934
+ // Skip unreadable state
935
+ }
936
+ }
937
+
938
+ // Load dimension findings
939
+ const dimensionsDir = join(reviewDir, 'dimensions');
940
+ if (existsSync(dimensionsDir)) {
941
+ const files = readdirSync(dimensionsDir).filter(f => f.endsWith('.json'));
942
+ for (const file of files) {
943
+ try {
944
+ const dimName = file.replace('.json', '');
945
+ const data = JSON.parse(readFileSync(join(dimensionsDir, file), 'utf8'));
946
+
947
+ // Handle array structure: [ { findings: [...] } ]
948
+ let findings = [];
949
+ let summary = null;
950
+
951
+ if (Array.isArray(data) && data.length > 0) {
952
+ const dimData = data[0];
953
+ findings = dimData.findings || [];
954
+ summary = dimData.summary || null;
955
+ } else if (data.findings) {
956
+ findings = data.findings;
957
+ summary = data.summary || null;
958
+ }
959
+
960
+ result.review.dimensions.push({
961
+ name: dimName,
962
+ findings: findings,
963
+ summary: summary,
964
+ count: findings.length
965
+ });
966
+ } catch (e) {
967
+ // Skip unreadable files
968
+ }
969
+ }
970
+ }
971
+ }
972
+ }
973
+
974
+ } catch (error) {
975
+ console.error('Error loading session detail:', error);
976
+ result.error = error.message;
977
+ }
978
+
979
+ return result;
980
+ }
981
+
982
+ /**
983
+ * Update task status in a task JSON file
984
+ * @param {string} sessionPath - Path to session directory
985
+ * @param {string} taskId - Task ID (e.g., IMPL-001)
986
+ * @param {string} newStatus - New status (pending, in_progress, completed)
987
+ * @returns {Promise<Object>}
988
+ */
989
+ async function updateTaskStatus(sessionPath, taskId, newStatus) {
990
+ // Normalize path (handle both forward and back slashes)
991
+ let normalizedPath = sessionPath.replace(/\\/g, '/');
992
+
993
+ // Handle Windows drive letter format
994
+ if (normalizedPath.match(/^[a-zA-Z]:\//)) {
995
+ // Already in correct format
996
+ } else if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
997
+ // Convert /D/path to D:/path
998
+ normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
999
+ }
1000
+
1001
+ const taskDir = join(normalizedPath, '.task');
1002
+
1003
+ // Check if task directory exists
1004
+ if (!existsSync(taskDir)) {
1005
+ throw new Error(`Task directory not found: ${taskDir}`);
1006
+ }
1007
+
1008
+ // Try to find the task file
1009
+ let taskFile = join(taskDir, `${taskId}.json`);
1010
+
1011
+ if (!existsSync(taskFile)) {
1012
+ // Try without .json if taskId already has it
1013
+ if (taskId.endsWith('.json')) {
1014
+ taskFile = join(taskDir, taskId);
1015
+ }
1016
+ if (!existsSync(taskFile)) {
1017
+ throw new Error(`Task file not found: ${taskId}.json in ${taskDir}`);
1018
+ }
1019
+ }
1020
+
1021
+ try {
1022
+ const content = JSON.parse(readFileSync(taskFile, 'utf8'));
1023
+ const oldStatus = content.status || 'pending';
1024
+ content.status = newStatus;
1025
+
1026
+ // Add status change timestamp
1027
+ if (!content.status_history) {
1028
+ content.status_history = [];
1029
+ }
1030
+ content.status_history.push({
1031
+ from: oldStatus,
1032
+ to: newStatus,
1033
+ changed_at: new Date().toISOString()
1034
+ });
1035
+
1036
+ writeFileSync(taskFile, JSON.stringify(content, null, 2), 'utf8');
1037
+
1038
+ return {
1039
+ success: true,
1040
+ taskId,
1041
+ oldStatus,
1042
+ newStatus,
1043
+ file: taskFile
1044
+ };
1045
+ } catch (error) {
1046
+ throw new Error(`Failed to update task ${taskId}: ${error.message}`);
1047
+ }
1048
+ }
1049
+
1050
+ /**
1051
+ * Generate dashboard HTML for server mode
1052
+ * @param {string} initialPath
1053
+ * @returns {string}
1054
+ */
1055
+ function generateServerDashboard(initialPath) {
1056
+ let html = readFileSync(TEMPLATE_PATH, 'utf8');
1057
+
1058
+ // Read and concatenate modular CSS files in load order
1059
+ const cssContent = MODULE_CSS_FILES.map(file => {
1060
+ const filePath = join(MODULE_CSS_DIR, file);
1061
+ return existsSync(filePath) ? readFileSync(filePath, 'utf8') : '';
1062
+ }).join('\n\n');
1063
+
1064
+ // Read and concatenate modular JS files in dependency order
1065
+ let jsContent = MODULE_FILES.map(file => {
1066
+ const filePath = join(MODULE_JS_DIR, file);
1067
+ return existsSync(filePath) ? readFileSync(filePath, 'utf8') : '';
1068
+ }).join('\n\n');
1069
+
1070
+ // Inject CSS content
1071
+ html = html.replace('{{CSS_CONTENT}}', cssContent);
1072
+
1073
+ // Prepare JS content with empty initial data (will be loaded dynamically)
1074
+ const emptyData = {
1075
+ generatedAt: new Date().toISOString(),
1076
+ activeSessions: [],
1077
+ archivedSessions: [],
1078
+ liteTasks: { litePlan: [], liteFix: [] },
1079
+ reviewData: { dimensions: {} },
1080
+ projectOverview: null,
1081
+ statistics: { totalSessions: 0, activeSessions: 0, totalTasks: 0, completedTasks: 0, reviewFindings: 0, litePlanCount: 0, liteFixCount: 0 }
1082
+ };
1083
+
1084
+ // Replace JS placeholders
1085
+ jsContent = jsContent.replace('{{WORKFLOW_DATA}}', JSON.stringify(emptyData, null, 2));
1086
+ jsContent = jsContent.replace(/\{\{PROJECT_PATH\}\}/g, normalizePathForDisplay(initialPath).replace(/\\/g, '/'));
1087
+ jsContent = jsContent.replace('{{RECENT_PATHS}}', JSON.stringify(getRecentPaths()));
1088
+
1089
+ // Add server mode flag and dynamic loading functions at the start of JS
1090
+ const serverModeScript = `
1091
+ // Server mode - load data dynamically
1092
+ window.SERVER_MODE = true;
1093
+ window.INITIAL_PATH = '${normalizePathForDisplay(initialPath).replace(/\\/g, '/')}';
1094
+
1095
+ async function loadDashboardData(path) {
1096
+ try {
1097
+ const res = await fetch('/api/data?path=' + encodeURIComponent(path));
1098
+ if (!res.ok) throw new Error('Failed to load data');
1099
+ return await res.json();
1100
+ } catch (err) {
1101
+ console.error('Error loading data:', err);
1102
+ return null;
1103
+ }
1104
+ }
1105
+
1106
+ async function loadRecentPaths() {
1107
+ try {
1108
+ const res = await fetch('/api/recent-paths');
1109
+ if (!res.ok) return [];
1110
+ const data = await res.json();
1111
+ return data.paths || [];
1112
+ } catch (err) {
1113
+ return [];
1114
+ }
1115
+ }
1116
+
1117
+ `;
1118
+
1119
+ // Prepend server mode script to JS content
1120
+ jsContent = serverModeScript + jsContent;
1121
+
1122
+ // Inject JS content
1123
+ html = html.replace('{{JS_CONTENT}}', jsContent);
1124
+
1125
+ // Replace any remaining placeholders in HTML
1126
+ html = html.replace(/\{\{PROJECT_PATH\}\}/g, normalizePathForDisplay(initialPath).replace(/\\/g, '/'));
1127
+
1128
+ return html;
1129
+ }
1130
+
1131
+ // ========================================
1132
+ // MCP Configuration Functions
1133
+ // ========================================
1134
+
1135
+ /**
1136
+ * Safely read and parse JSON file
1137
+ * @param {string} filePath
1138
+ * @returns {Object|null}
1139
+ */
1140
+ function safeReadJson(filePath) {
1141
+ try {
1142
+ if (!existsSync(filePath)) return null;
1143
+ const content = readFileSync(filePath, 'utf8');
1144
+ return JSON.parse(content);
1145
+ } catch {
1146
+ return null;
1147
+ }
1148
+ }
1149
+
1150
+ /**
1151
+ * Get MCP servers from a JSON file (expects mcpServers key at top level)
1152
+ * @param {string} filePath
1153
+ * @returns {Object} mcpServers object or empty object
1154
+ */
1155
+ function getMcpServersFromFile(filePath) {
1156
+ const config = safeReadJson(filePath);
1157
+ if (!config) return {};
1158
+ return config.mcpServers || {};
1159
+ }
1160
+
1161
+ /**
1162
+ * Get MCP configuration from multiple sources (per official Claude Code docs):
1163
+ *
1164
+ * Priority (highest to lowest):
1165
+ * 1. Enterprise managed-mcp.json (cannot be overridden)
1166
+ * 2. Local scope (project-specific private in ~/.claude.json)
1167
+ * 3. Project scope (.mcp.json in project root)
1168
+ * 4. User scope (mcpServers in ~/.claude.json)
1169
+ *
1170
+ * Note: ~/.claude/settings.json is for MCP PERMISSIONS, NOT definitions!
1171
+ *
1172
+ * @returns {Object}
1173
+ */
1174
+ function getMcpConfig() {
1175
+ try {
1176
+ const result = {
1177
+ projects: {},
1178
+ userServers: {}, // User-level servers from ~/.claude.json mcpServers
1179
+ enterpriseServers: {}, // Enterprise managed servers (highest priority)
1180
+ configSources: [] // Track where configs came from for debugging
1181
+ };
1182
+
1183
+ // 1. Read Enterprise managed MCP servers (highest priority)
1184
+ const enterprisePath = getEnterpriseMcpPath();
1185
+ if (existsSync(enterprisePath)) {
1186
+ const enterpriseConfig = safeReadJson(enterprisePath);
1187
+ if (enterpriseConfig?.mcpServers) {
1188
+ result.enterpriseServers = enterpriseConfig.mcpServers;
1189
+ result.configSources.push({ type: 'enterprise', path: enterprisePath, count: Object.keys(enterpriseConfig.mcpServers).length });
1190
+ }
1191
+ }
1192
+
1193
+ // 2. Read from ~/.claude.json
1194
+ if (existsSync(CLAUDE_CONFIG_PATH)) {
1195
+ const claudeConfig = safeReadJson(CLAUDE_CONFIG_PATH);
1196
+ if (claudeConfig) {
1197
+ // 2a. User-level mcpServers (top-level mcpServers key)
1198
+ if (claudeConfig.mcpServers) {
1199
+ result.userServers = claudeConfig.mcpServers;
1200
+ result.configSources.push({ type: 'user', path: CLAUDE_CONFIG_PATH, count: Object.keys(claudeConfig.mcpServers).length });
1201
+ }
1202
+
1203
+ // 2b. Project-specific configurations (projects[path].mcpServers)
1204
+ if (claudeConfig.projects) {
1205
+ result.projects = claudeConfig.projects;
1206
+ }
1207
+ }
1208
+ }
1209
+
1210
+ // 3. For each known project, check for .mcp.json (project-level config)
1211
+ const projectPaths = Object.keys(result.projects);
1212
+ for (const projectPath of projectPaths) {
1213
+ const mcpJsonPath = join(projectPath, '.mcp.json');
1214
+ if (existsSync(mcpJsonPath)) {
1215
+ const mcpJsonConfig = safeReadJson(mcpJsonPath);
1216
+ if (mcpJsonConfig?.mcpServers) {
1217
+ // Merge .mcp.json servers into project config
1218
+ // Project's .mcp.json has lower priority than ~/.claude.json projects[path].mcpServers
1219
+ const existingServers = result.projects[projectPath]?.mcpServers || {};
1220
+ result.projects[projectPath] = {
1221
+ ...result.projects[projectPath],
1222
+ mcpServers: {
1223
+ ...mcpJsonConfig.mcpServers, // .mcp.json (lower priority)
1224
+ ...existingServers // ~/.claude.json projects[path] (higher priority)
1225
+ },
1226
+ mcpJsonPath: mcpJsonPath // Track source for debugging
1227
+ };
1228
+ result.configSources.push({ type: 'project-mcp-json', path: mcpJsonPath, count: Object.keys(mcpJsonConfig.mcpServers).length });
1229
+ }
1230
+ }
1231
+ }
1232
+
1233
+ // Build globalServers by merging user and enterprise servers
1234
+ // Enterprise servers override user servers
1235
+ result.globalServers = {
1236
+ ...result.userServers,
1237
+ ...result.enterpriseServers
1238
+ };
1239
+
1240
+ return result;
1241
+ } catch (error) {
1242
+ console.error('Error reading MCP config:', error);
1243
+ return { projects: {}, globalServers: {}, userServers: {}, enterpriseServers: {}, configSources: [], error: error.message };
1244
+ }
1245
+ }
1246
+
1247
+ /**
1248
+ * Normalize project path for .claude.json (Windows backslash format)
1249
+ * @param {string} path
1250
+ * @returns {string}
1251
+ */
1252
+ function normalizeProjectPathForConfig(path) {
1253
+ // Convert forward slashes to backslashes for Windows .claude.json format
1254
+ let normalized = path.replace(/\//g, '\\');
1255
+
1256
+ // Handle /d/path format -> D:\path
1257
+ if (normalized.match(/^\\[a-zA-Z]\\/)) {
1258
+ normalized = normalized.charAt(1).toUpperCase() + ':' + normalized.slice(2);
1259
+ }
1260
+
1261
+ return normalized;
1262
+ }
1263
+
1264
+ /**
1265
+ * Toggle MCP server enabled/disabled
1266
+ * @param {string} projectPath
1267
+ * @param {string} serverName
1268
+ * @param {boolean} enable
1269
+ * @returns {Object}
1270
+ */
1271
+ function toggleMcpServerEnabled(projectPath, serverName, enable) {
1272
+ try {
1273
+ if (!existsSync(CLAUDE_CONFIG_PATH)) {
1274
+ return { error: '.claude.json not found' };
1275
+ }
1276
+
1277
+ const content = readFileSync(CLAUDE_CONFIG_PATH, 'utf8');
1278
+ const config = JSON.parse(content);
1279
+
1280
+ const normalizedPath = normalizeProjectPathForConfig(projectPath);
1281
+
1282
+ if (!config.projects || !config.projects[normalizedPath]) {
1283
+ return { error: `Project not found: ${normalizedPath}` };
1284
+ }
1285
+
1286
+ const projectConfig = config.projects[normalizedPath];
1287
+
1288
+ // Ensure disabledMcpServers array exists
1289
+ if (!projectConfig.disabledMcpServers) {
1290
+ projectConfig.disabledMcpServers = [];
1291
+ }
1292
+
1293
+ if (enable) {
1294
+ // Remove from disabled list
1295
+ projectConfig.disabledMcpServers = projectConfig.disabledMcpServers.filter(s => s !== serverName);
1296
+ } else {
1297
+ // Add to disabled list if not already there
1298
+ if (!projectConfig.disabledMcpServers.includes(serverName)) {
1299
+ projectConfig.disabledMcpServers.push(serverName);
1300
+ }
1301
+ }
1302
+
1303
+ // Write back to file
1304
+ writeFileSync(CLAUDE_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
1305
+
1306
+ return {
1307
+ success: true,
1308
+ serverName,
1309
+ enabled: enable,
1310
+ disabledMcpServers: projectConfig.disabledMcpServers
1311
+ };
1312
+ } catch (error) {
1313
+ console.error('Error toggling MCP server:', error);
1314
+ return { error: error.message };
1315
+ }
1316
+ }
1317
+
1318
+ /**
1319
+ * Add MCP server to project
1320
+ * @param {string} projectPath
1321
+ * @param {string} serverName
1322
+ * @param {Object} serverConfig
1323
+ * @returns {Object}
1324
+ */
1325
+ function addMcpServerToProject(projectPath, serverName, serverConfig) {
1326
+ try {
1327
+ if (!existsSync(CLAUDE_CONFIG_PATH)) {
1328
+ return { error: '.claude.json not found' };
1329
+ }
1330
+
1331
+ const content = readFileSync(CLAUDE_CONFIG_PATH, 'utf8');
1332
+ const config = JSON.parse(content);
1333
+
1334
+ const normalizedPath = normalizeProjectPathForConfig(projectPath);
1335
+
1336
+ // Create project entry if it doesn't exist
1337
+ if (!config.projects) {
1338
+ config.projects = {};
1339
+ }
1340
+
1341
+ if (!config.projects[normalizedPath]) {
1342
+ config.projects[normalizedPath] = {
1343
+ allowedTools: [],
1344
+ mcpContextUris: [],
1345
+ mcpServers: {},
1346
+ enabledMcpjsonServers: [],
1347
+ disabledMcpjsonServers: [],
1348
+ hasTrustDialogAccepted: false,
1349
+ projectOnboardingSeenCount: 0,
1350
+ hasClaudeMdExternalIncludesApproved: false,
1351
+ hasClaudeMdExternalIncludesWarningShown: false
1352
+ };
1353
+ }
1354
+
1355
+ const projectConfig = config.projects[normalizedPath];
1356
+
1357
+ // Ensure mcpServers exists
1358
+ if (!projectConfig.mcpServers) {
1359
+ projectConfig.mcpServers = {};
1360
+ }
1361
+
1362
+ // Add the server
1363
+ projectConfig.mcpServers[serverName] = serverConfig;
1364
+
1365
+ // Write back to file
1366
+ writeFileSync(CLAUDE_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
1367
+
1368
+ return {
1369
+ success: true,
1370
+ serverName,
1371
+ serverConfig
1372
+ };
1373
+ } catch (error) {
1374
+ console.error('Error adding MCP server:', error);
1375
+ return { error: error.message };
1376
+ }
1377
+ }
1378
+
1379
+ /**
1380
+ * Remove MCP server from project
1381
+ * @param {string} projectPath
1382
+ * @param {string} serverName
1383
+ * @returns {Object}
1384
+ */
1385
+ function removeMcpServerFromProject(projectPath, serverName) {
1386
+ try {
1387
+ if (!existsSync(CLAUDE_CONFIG_PATH)) {
1388
+ return { error: '.claude.json not found' };
1389
+ }
1390
+
1391
+ const content = readFileSync(CLAUDE_CONFIG_PATH, 'utf8');
1392
+ const config = JSON.parse(content);
1393
+
1394
+ const normalizedPath = normalizeProjectPathForConfig(projectPath);
1395
+
1396
+ if (!config.projects || !config.projects[normalizedPath]) {
1397
+ return { error: `Project not found: ${normalizedPath}` };
1398
+ }
1399
+
1400
+ const projectConfig = config.projects[normalizedPath];
1401
+
1402
+ if (!projectConfig.mcpServers || !projectConfig.mcpServers[serverName]) {
1403
+ return { error: `Server not found: ${serverName}` };
1404
+ }
1405
+
1406
+ // Remove the server
1407
+ delete projectConfig.mcpServers[serverName];
1408
+
1409
+ // Also remove from disabled list if present
1410
+ if (projectConfig.disabledMcpServers) {
1411
+ projectConfig.disabledMcpServers = projectConfig.disabledMcpServers.filter(s => s !== serverName);
1412
+ }
1413
+
1414
+ // Write back to file
1415
+ writeFileSync(CLAUDE_CONFIG_PATH, JSON.stringify(config, null, 2), 'utf8');
1416
+
1417
+ return {
1418
+ success: true,
1419
+ serverName,
1420
+ removed: true
1421
+ };
1422
+ } catch (error) {
1423
+ console.error('Error removing MCP server:', error);
1424
+ return { error: error.message };
1425
+ }
1426
+ }
1427
+
1428
+ // ========================================
1429
+ // Hook Configuration Functions
1430
+ // ========================================
1431
+
1432
+ const GLOBAL_SETTINGS_PATH = join(homedir(), '.claude', 'settings.json');
1433
+
1434
+ /**
1435
+ * Get project settings path
1436
+ * @param {string} projectPath
1437
+ * @returns {string}
1438
+ */
1439
+ function getProjectSettingsPath(projectPath) {
1440
+ const normalizedPath = projectPath.replace(/\//g, '\\').replace(/^\\([a-zA-Z])\\/, '$1:\\');
1441
+ return join(normalizedPath, '.claude', 'settings.json');
1442
+ }
1443
+
1444
+ /**
1445
+ * Read settings file safely
1446
+ * @param {string} filePath
1447
+ * @returns {Object}
1448
+ */
1449
+ function readSettingsFile(filePath) {
1450
+ try {
1451
+ if (!existsSync(filePath)) {
1452
+ return { hooks: {} };
1453
+ }
1454
+ const content = readFileSync(filePath, 'utf8');
1455
+ return JSON.parse(content);
1456
+ } catch (error) {
1457
+ console.error(`Error reading settings file ${filePath}:`, error);
1458
+ return { hooks: {} };
1459
+ }
1460
+ }
1461
+
1462
+ /**
1463
+ * Write settings file safely
1464
+ * @param {string} filePath
1465
+ * @param {Object} settings
1466
+ */
1467
+ function writeSettingsFile(filePath, settings) {
1468
+ const dirPath = dirname(filePath);
1469
+ // Ensure directory exists
1470
+ if (!existsSync(dirPath)) {
1471
+ mkdirSync(dirPath, { recursive: true });
1472
+ }
1473
+ writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf8');
1474
+ }
1475
+
1476
+ /**
1477
+ * Get hooks configuration from both global and project settings
1478
+ * @param {string} projectPath
1479
+ * @returns {Object}
1480
+ */
1481
+ function getHooksConfig(projectPath) {
1482
+ const globalSettings = readSettingsFile(GLOBAL_SETTINGS_PATH);
1483
+ const projectSettingsPath = projectPath ? getProjectSettingsPath(projectPath) : null;
1484
+ const projectSettings = projectSettingsPath ? readSettingsFile(projectSettingsPath) : { hooks: {} };
1485
+
1486
+ return {
1487
+ global: {
1488
+ path: GLOBAL_SETTINGS_PATH,
1489
+ hooks: globalSettings.hooks || {}
1490
+ },
1491
+ project: {
1492
+ path: projectSettingsPath,
1493
+ hooks: projectSettings.hooks || {}
1494
+ }
1495
+ };
1496
+ }
1497
+
1498
+ /**
1499
+ * Save a hook to settings file
1500
+ * @param {string} projectPath
1501
+ * @param {string} scope - 'global' or 'project'
1502
+ * @param {string} event - Hook event type
1503
+ * @param {Object} hookData - Hook configuration
1504
+ * @returns {Object}
1505
+ */
1506
+ function saveHookToSettings(projectPath, scope, event, hookData) {
1507
+ try {
1508
+ const filePath = scope === 'global' ? GLOBAL_SETTINGS_PATH : getProjectSettingsPath(projectPath);
1509
+ const settings = readSettingsFile(filePath);
1510
+
1511
+ // Ensure hooks object exists
1512
+ if (!settings.hooks) {
1513
+ settings.hooks = {};
1514
+ }
1515
+
1516
+ // Ensure the event array exists
1517
+ if (!settings.hooks[event]) {
1518
+ settings.hooks[event] = [];
1519
+ }
1520
+
1521
+ // Ensure it's an array
1522
+ if (!Array.isArray(settings.hooks[event])) {
1523
+ settings.hooks[event] = [settings.hooks[event]];
1524
+ }
1525
+
1526
+ // Check if we're replacing an existing hook
1527
+ if (hookData.replaceIndex !== undefined) {
1528
+ const index = hookData.replaceIndex;
1529
+ delete hookData.replaceIndex;
1530
+ if (index >= 0 && index < settings.hooks[event].length) {
1531
+ settings.hooks[event][index] = hookData;
1532
+ }
1533
+ } else {
1534
+ // Add new hook
1535
+ settings.hooks[event].push(hookData);
1536
+ }
1537
+
1538
+ // Ensure directory exists and write file
1539
+ const dirPath = dirname(filePath);
1540
+ if (!existsSync(dirPath)) {
1541
+ mkdirSync(dirPath, { recursive: true });
1542
+ }
1543
+ writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf8');
1544
+
1545
+ return {
1546
+ success: true,
1547
+ event,
1548
+ hookData
1549
+ };
1550
+ } catch (error) {
1551
+ console.error('Error saving hook:', error);
1552
+ return { error: error.message };
1553
+ }
1554
+ }
1555
+
1556
+ /**
1557
+ * Delete a hook from settings file
1558
+ * @param {string} projectPath
1559
+ * @param {string} scope - 'global' or 'project'
1560
+ * @param {string} event - Hook event type
1561
+ * @param {number} hookIndex - Index of hook to delete
1562
+ * @returns {Object}
1563
+ */
1564
+ function deleteHookFromSettings(projectPath, scope, event, hookIndex) {
1565
+ try {
1566
+ const filePath = scope === 'global' ? GLOBAL_SETTINGS_PATH : getProjectSettingsPath(projectPath);
1567
+ const settings = readSettingsFile(filePath);
1568
+
1569
+ if (!settings.hooks || !settings.hooks[event]) {
1570
+ return { error: 'Hook not found' };
1571
+ }
1572
+
1573
+ // Ensure it's an array
1574
+ if (!Array.isArray(settings.hooks[event])) {
1575
+ settings.hooks[event] = [settings.hooks[event]];
1576
+ }
1577
+
1578
+ if (hookIndex < 0 || hookIndex >= settings.hooks[event].length) {
1579
+ return { error: 'Invalid hook index' };
1580
+ }
1581
+
1582
+ // Remove the hook
1583
+ settings.hooks[event].splice(hookIndex, 1);
1584
+
1585
+ // Remove empty event arrays
1586
+ if (settings.hooks[event].length === 0) {
1587
+ delete settings.hooks[event];
1588
+ }
1589
+
1590
+ writeFileSync(filePath, JSON.stringify(settings, null, 2), 'utf8');
1591
+
1592
+ return {
1593
+ success: true,
1594
+ event,
1595
+ hookIndex
1596
+ };
1597
+ } catch (error) {
1598
+ console.error('Error deleting hook:', error);
1599
+ return { error: error.message };
1600
+ }
1601
+ }
1602
+
1603
+ // ========================================
1604
+ // Explorer View Functions
1605
+ // ========================================
1606
+
1607
+ // Directories to always exclude from file tree
1608
+ const EXPLORER_EXCLUDE_DIRS = [
1609
+ '.git', '__pycache__', 'node_modules', '.venv', 'venv', 'env',
1610
+ 'dist', 'build', '.cache', '.pytest_cache', '.mypy_cache',
1611
+ 'coverage', '.nyc_output', 'logs', 'tmp', 'temp', '.next',
1612
+ '.nuxt', '.output', '.turbo', '.parcel-cache'
1613
+ ];
1614
+
1615
+ // File extensions to language mapping for syntax highlighting
1616
+ const EXT_TO_LANGUAGE = {
1617
+ '.js': 'javascript',
1618
+ '.jsx': 'javascript',
1619
+ '.ts': 'typescript',
1620
+ '.tsx': 'typescript',
1621
+ '.py': 'python',
1622
+ '.rb': 'ruby',
1623
+ '.java': 'java',
1624
+ '.go': 'go',
1625
+ '.rs': 'rust',
1626
+ '.c': 'c',
1627
+ '.cpp': 'cpp',
1628
+ '.h': 'c',
1629
+ '.hpp': 'cpp',
1630
+ '.cs': 'csharp',
1631
+ '.php': 'php',
1632
+ '.swift': 'swift',
1633
+ '.kt': 'kotlin',
1634
+ '.scala': 'scala',
1635
+ '.sh': 'bash',
1636
+ '.bash': 'bash',
1637
+ '.zsh': 'bash',
1638
+ '.ps1': 'powershell',
1639
+ '.sql': 'sql',
1640
+ '.html': 'html',
1641
+ '.htm': 'html',
1642
+ '.css': 'css',
1643
+ '.scss': 'scss',
1644
+ '.sass': 'sass',
1645
+ '.less': 'less',
1646
+ '.json': 'json',
1647
+ '.xml': 'xml',
1648
+ '.yaml': 'yaml',
1649
+ '.yml': 'yaml',
1650
+ '.toml': 'toml',
1651
+ '.ini': 'ini',
1652
+ '.cfg': 'ini',
1653
+ '.conf': 'nginx',
1654
+ '.md': 'markdown',
1655
+ '.markdown': 'markdown',
1656
+ '.txt': 'plaintext',
1657
+ '.log': 'plaintext',
1658
+ '.env': 'bash',
1659
+ '.dockerfile': 'dockerfile',
1660
+ '.vue': 'html',
1661
+ '.svelte': 'html'
1662
+ };
1663
+
1664
+ /**
1665
+ * Parse .gitignore file and return patterns
1666
+ * @param {string} gitignorePath - Path to .gitignore file
1667
+ * @returns {string[]} Array of gitignore patterns
1668
+ */
1669
+ function parseGitignore(gitignorePath) {
1670
+ try {
1671
+ if (!existsSync(gitignorePath)) return [];
1672
+ const content = readFileSync(gitignorePath, 'utf8');
1673
+ return content
1674
+ .split('\n')
1675
+ .map(line => line.trim())
1676
+ .filter(line => line && !line.startsWith('#'));
1677
+ } catch {
1678
+ return [];
1679
+ }
1680
+ }
1681
+
1682
+ /**
1683
+ * Check if a file/directory should be ignored based on gitignore patterns
1684
+ * Simple pattern matching (supports basic glob patterns)
1685
+ * @param {string} name - File or directory name
1686
+ * @param {string[]} patterns - Gitignore patterns
1687
+ * @param {boolean} isDirectory - Whether the entry is a directory
1688
+ * @returns {boolean}
1689
+ */
1690
+ function shouldIgnore(name, patterns, isDirectory) {
1691
+ // Always exclude certain directories
1692
+ if (isDirectory && EXPLORER_EXCLUDE_DIRS.includes(name)) {
1693
+ return true;
1694
+ }
1695
+
1696
+ // Skip hidden files/directories (starting with .)
1697
+ if (name.startsWith('.') && name !== '.claude' && name !== '.workflow') {
1698
+ return true;
1699
+ }
1700
+
1701
+ for (const pattern of patterns) {
1702
+ let p = pattern;
1703
+
1704
+ // Handle negation patterns (we skip them for simplicity)
1705
+ if (p.startsWith('!')) continue;
1706
+
1707
+ // Handle directory-only patterns
1708
+ if (p.endsWith('/')) {
1709
+ if (!isDirectory) continue;
1710
+ p = p.slice(0, -1);
1711
+ }
1712
+
1713
+ // Simple pattern matching
1714
+ if (p === name) return true;
1715
+
1716
+ // Handle wildcard patterns
1717
+ if (p.includes('*')) {
1718
+ const regex = new RegExp('^' + p.replace(/\./g, '\\.').replace(/\*/g, '.*') + '$');
1719
+ if (regex.test(name)) return true;
1720
+ }
1721
+
1722
+ // Handle extension patterns like *.log
1723
+ if (p.startsWith('*.')) {
1724
+ const ext = p.slice(1);
1725
+ if (name.endsWith(ext)) return true;
1726
+ }
1727
+ }
1728
+
1729
+ return false;
1730
+ }
1731
+
1732
+ /**
1733
+ * List directory files with .gitignore filtering
1734
+ * @param {string} dirPath - Directory path to list
1735
+ * @returns {Promise<Object>}
1736
+ */
1737
+ async function listDirectoryFiles(dirPath) {
1738
+ try {
1739
+ // Normalize path
1740
+ let normalizedPath = dirPath.replace(/\\/g, '/');
1741
+ if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
1742
+ normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
1743
+ }
1744
+
1745
+ if (!existsSync(normalizedPath)) {
1746
+ return { error: 'Directory not found', files: [] };
1747
+ }
1748
+
1749
+ if (!statSync(normalizedPath).isDirectory()) {
1750
+ return { error: 'Not a directory', files: [] };
1751
+ }
1752
+
1753
+ // Parse .gitignore patterns
1754
+ const gitignorePath = join(normalizedPath, '.gitignore');
1755
+ const gitignorePatterns = parseGitignore(gitignorePath);
1756
+
1757
+ // Read directory entries
1758
+ const entries = readdirSync(normalizedPath, { withFileTypes: true });
1759
+
1760
+ const files = [];
1761
+ for (const entry of entries) {
1762
+ const isDirectory = entry.isDirectory();
1763
+
1764
+ // Check if should be ignored
1765
+ if (shouldIgnore(entry.name, gitignorePatterns, isDirectory)) {
1766
+ continue;
1767
+ }
1768
+
1769
+ const entryPath = join(normalizedPath, entry.name);
1770
+ const fileInfo = {
1771
+ name: entry.name,
1772
+ type: isDirectory ? 'directory' : 'file',
1773
+ path: entryPath.replace(/\\/g, '/')
1774
+ };
1775
+
1776
+ // Check if directory has CLAUDE.md
1777
+ if (isDirectory) {
1778
+ const claudeMdPath = join(entryPath, 'CLAUDE.md');
1779
+ fileInfo.hasClaudeMd = existsSync(claudeMdPath);
1780
+ }
1781
+
1782
+ files.push(fileInfo);
1783
+ }
1784
+
1785
+ // Sort: directories first, then alphabetically
1786
+ files.sort((a, b) => {
1787
+ if (a.type === 'directory' && b.type !== 'directory') return -1;
1788
+ if (a.type !== 'directory' && b.type === 'directory') return 1;
1789
+ return a.name.localeCompare(b.name);
1790
+ });
1791
+
1792
+ return {
1793
+ path: normalizedPath.replace(/\\/g, '/'),
1794
+ files,
1795
+ gitignorePatterns
1796
+ };
1797
+ } catch (error) {
1798
+ console.error('Error listing directory:', error);
1799
+ return { error: error.message, files: [] };
1800
+ }
1801
+ }
1802
+
1803
+ /**
1804
+ * Get file content for preview
1805
+ * @param {string} filePath - Path to file
1806
+ * @returns {Promise<Object>}
1807
+ */
1808
+ async function getFileContent(filePath) {
1809
+ try {
1810
+ // Normalize path
1811
+ let normalizedPath = filePath.replace(/\\/g, '/');
1812
+ if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
1813
+ normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
1814
+ }
1815
+
1816
+ if (!existsSync(normalizedPath)) {
1817
+ return { error: 'File not found' };
1818
+ }
1819
+
1820
+ const stats = statSync(normalizedPath);
1821
+ if (stats.isDirectory()) {
1822
+ return { error: 'Cannot read directory' };
1823
+ }
1824
+
1825
+ // Check file size (limit to 1MB for preview)
1826
+ if (stats.size > 1024 * 1024) {
1827
+ return { error: 'File too large for preview (max 1MB)', size: stats.size };
1828
+ }
1829
+
1830
+ // Read file content
1831
+ const content = readFileSync(normalizedPath, 'utf8');
1832
+ const ext = normalizedPath.substring(normalizedPath.lastIndexOf('.')).toLowerCase();
1833
+ const language = EXT_TO_LANGUAGE[ext] || 'plaintext';
1834
+ const isMarkdown = ext === '.md' || ext === '.markdown';
1835
+ const fileName = normalizedPath.split('/').pop();
1836
+
1837
+ return {
1838
+ content,
1839
+ language,
1840
+ isMarkdown,
1841
+ fileName,
1842
+ path: normalizedPath,
1843
+ size: stats.size,
1844
+ lines: content.split('\n').length
1845
+ };
1846
+ } catch (error) {
1847
+ console.error('Error reading file:', error);
1848
+ return { error: error.message };
1849
+ }
1850
+ }
1851
+
1852
+ /**
1853
+ * Trigger update-module-claude tool (async execution)
1854
+ * @param {string} targetPath - Directory path to update
1855
+ * @param {string} tool - CLI tool to use (gemini, qwen, codex)
1856
+ * @param {string} strategy - Update strategy (single-layer, multi-layer)
1857
+ * @returns {Promise<Object>}
1858
+ */
1859
+ async function triggerUpdateClaudeMd(targetPath, tool, strategy) {
1860
+ const { spawn } = await import('child_process');
1861
+
1862
+ // Normalize path
1863
+ let normalizedPath = targetPath.replace(/\\/g, '/');
1864
+ if (normalizedPath.match(/^\/[a-zA-Z]\//)) {
1865
+ normalizedPath = normalizedPath.charAt(1).toUpperCase() + ':' + normalizedPath.slice(2);
1866
+ }
1867
+
1868
+ if (!existsSync(normalizedPath)) {
1869
+ return { error: 'Directory not found' };
1870
+ }
1871
+
1872
+ if (!statSync(normalizedPath).isDirectory()) {
1873
+ return { error: 'Not a directory' };
1874
+ }
1875
+
1876
+ // Build ccw tool command with JSON parameters
1877
+ const params = JSON.stringify({
1878
+ strategy,
1879
+ path: normalizedPath,
1880
+ tool
1881
+ });
1882
+
1883
+ console.log(`[Explorer] Running async: ccw tool exec update_module_claude with ${tool} (${strategy})`);
1884
+
1885
+ return new Promise((resolve) => {
1886
+ const isWindows = process.platform === 'win32';
1887
+
1888
+ // Spawn the process
1889
+ const child = spawn('ccw', ['tool', 'exec', 'update_module_claude', params], {
1890
+ cwd: normalizedPath,
1891
+ shell: isWindows,
1892
+ stdio: ['ignore', 'pipe', 'pipe']
1893
+ });
1894
+
1895
+ let stdout = '';
1896
+ let stderr = '';
1897
+
1898
+ child.stdout.on('data', (data) => {
1899
+ stdout += data.toString();
1900
+ });
1901
+
1902
+ child.stderr.on('data', (data) => {
1903
+ stderr += data.toString();
1904
+ });
1905
+
1906
+ child.on('close', (code) => {
1907
+ if (code === 0) {
1908
+ // Parse the JSON output from the tool
1909
+ let result;
1910
+ try {
1911
+ result = JSON.parse(stdout);
1912
+ } catch {
1913
+ result = { output: stdout };
1914
+ }
1915
+
1916
+ if (result.success === false || result.error) {
1917
+ resolve({
1918
+ success: false,
1919
+ error: result.error || result.message || 'Update failed',
1920
+ output: stdout
1921
+ });
1922
+ } else {
1923
+ resolve({
1924
+ success: true,
1925
+ message: result.message || `CLAUDE.md updated successfully using ${tool} (${strategy})`,
1926
+ output: stdout,
1927
+ path: normalizedPath
1928
+ });
1929
+ }
1930
+ } else {
1931
+ resolve({
1932
+ success: false,
1933
+ error: stderr || `Process exited with code ${code}`,
1934
+ output: stdout + stderr
1935
+ });
1936
+ }
1937
+ });
1938
+
1939
+ child.on('error', (error) => {
1940
+ console.error('Error spawning process:', error);
1941
+ resolve({
1942
+ success: false,
1943
+ error: error.message,
1944
+ output: ''
1945
+ });
1946
+ });
1947
+
1948
+ // Timeout after 5 minutes
1949
+ setTimeout(() => {
1950
+ child.kill();
1951
+ resolve({
1952
+ success: false,
1953
+ error: 'Timeout: Process took longer than 5 minutes',
1954
+ output: stdout
1955
+ });
1956
+ }, 300000);
1957
+ });
1958
+ }
1959
+
1960
+
1961
+ // ========================================
1962
+ // Version Check Functions
1963
+ // ========================================
1964
+
1965
+ // Package name on npm registry
1966
+ const NPM_PACKAGE_NAME = 'claude-code-workflow';
1967
+
1968
+ // Cache for version check (avoid too frequent requests)
1969
+ let versionCheckCache = null;
1970
+ let versionCheckTime = 0;
1971
+ const VERSION_CHECK_CACHE_TTL = 3600000; // 1 hour
1972
+
1973
+ /**
1974
+ * Get current package version from package.json
1975
+ * @returns {string}
1976
+ */
1977
+ function getCurrentVersion() {
1978
+ try {
1979
+ const packageJsonPath = join(import.meta.dirname, '../../../package.json');
1980
+ if (existsSync(packageJsonPath)) {
1981
+ const pkg = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
1982
+ return pkg.version || '0.0.0';
1983
+ }
1984
+ } catch (e) {
1985
+ console.error('Error reading package.json:', e);
1986
+ }
1987
+ return '0.0.0';
1988
+ }
1989
+
1990
+ /**
1991
+ * Check npm registry for latest version
1992
+ * @returns {Promise<Object>}
1993
+ */
1994
+ async function checkNpmVersion() {
1995
+ // Return cached result if still valid
1996
+ const now = Date.now();
1997
+ if (versionCheckCache && (now - versionCheckTime) < VERSION_CHECK_CACHE_TTL) {
1998
+ return versionCheckCache;
1999
+ }
2000
+
2001
+ const currentVersion = getCurrentVersion();
2002
+
2003
+ try {
2004
+ // Fetch latest version from npm registry
2005
+ const npmUrl = 'https://registry.npmjs.org/' + encodeURIComponent(NPM_PACKAGE_NAME) + '/latest';
2006
+ const response = await fetch(npmUrl, {
2007
+ headers: { 'Accept': 'application/json' }
2008
+ });
2009
+
2010
+ if (!response.ok) {
2011
+ throw new Error('HTTP ' + response.status);
2012
+ }
2013
+
2014
+ const data = await response.json();
2015
+ const latestVersion = data.version;
2016
+
2017
+ // Compare versions
2018
+ const hasUpdate = compareVersions(latestVersion, currentVersion) > 0;
2019
+
2020
+ const result = {
2021
+ currentVersion,
2022
+ latestVersion,
2023
+ hasUpdate,
2024
+ packageName: NPM_PACKAGE_NAME,
2025
+ updateCommand: 'npm update -g ' + NPM_PACKAGE_NAME,
2026
+ checkedAt: new Date().toISOString()
2027
+ };
2028
+
2029
+ // Cache the result
2030
+ versionCheckCache = result;
2031
+ versionCheckTime = now;
2032
+
2033
+ return result;
2034
+ } catch (error) {
2035
+ console.error('Version check failed:', error.message);
2036
+ return {
2037
+ currentVersion,
2038
+ latestVersion: null,
2039
+ hasUpdate: false,
2040
+ error: error.message,
2041
+ checkedAt: new Date().toISOString()
2042
+ };
2043
+ }
2044
+ }
2045
+
2046
+ /**
2047
+ * Compare two semver versions
2048
+ * @param {string} v1
2049
+ * @param {string} v2
2050
+ * @returns {number} 1 if v1 > v2, -1 if v1 < v2, 0 if equal
2051
+ */
2052
+ function compareVersions(v1, v2) {
2053
+ const parts1 = v1.split('.').map(Number);
2054
+ const parts2 = v2.split('.').map(Number);
2055
+
2056
+ for (let i = 0; i < 3; i++) {
2057
+ const p1 = parts1[i] || 0;
2058
+ const p2 = parts2[i] || 0;
2059
+ if (p1 > p2) return 1;
2060
+ if (p1 < p2) return -1;
2061
+ }
2062
+ return 0;
2063
+ }