-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1060 lines (898 loc) · 32.1 KB
/
server.js
File metadata and controls
1060 lines (898 loc) · 32.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const express = require('express');
const http = require('http');
const { WebSocketServer, WebSocket } = require('ws');
const pty = require('node-pty');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const url = require('url');
const { execSync } = require('child_process');
// ── Ensure claude and other tools are in PATH ───────────────────────────────
const HOME = process.env.HOME || '/Users/maxwraae';
const extraPaths = [
path.join(HOME, '.local', 'bin'),
'/opt/homebrew/bin',
'/opt/homebrew/sbin',
'/usr/local/bin',
];
const currentPath = process.env.PATH || '';
for (const p of extraPaths) {
if (!currentPath.includes(p)) {
process.env.PATH = p + ':' + process.env.PATH;
}
}
// ── Config ──────────────────────────────────────────────────────────────────
const PORT = process.env.PORT || 8080;
const HOST = '0.0.0.0';
const CLAUDE_PROJECTS_DIR = path.join(process.env.HOME, '.claude', 'projects');
const SCROLLBACK_SIZE = 100 * 1024; // 100KB circular buffer per session
const IDLE_TIMEOUT_MS = 30 * 1000; // 30 seconds no output = idle
const STATUS_CHECK_INTERVAL_MS = 5000; // Check status every 5 seconds
const SESSION_CACHE_TTL_MS = 5000; // Re-read from disk every 5 seconds
const ANSI_STRIP_RE = /\x1B\[[0-9;]*[a-zA-Z]/g;
// ── Metadata layer ───────────────────────────────────────────────────────────
const META_DIR = path.join(process.env.HOME, '.config', 'aimessage');
const META_FILE = path.join(META_DIR, 'meta.json');
const PROJECT_COLORS = [
'#FF6B6B', '#FF9F43', '#FECA57', '#48DBFB', '#0ABDE3',
'#1DD1A1', '#00D2D3', '#54A0FF', '#5F27CD', '#C44569',
'#F78FB3', '#3DC1D3', '#E77F67', '#778BEB', '#786FA6'
];
function ensureMetaDir() {
if (!fs.existsSync(META_DIR)) {
fs.mkdirSync(META_DIR, { recursive: true });
}
}
function loadMeta() {
try {
if (!fs.existsSync(META_FILE)) {
return { version: 1, projects: {}, sessions: {} };
}
const raw = fs.readFileSync(META_FILE, 'utf-8');
return JSON.parse(raw);
} catch (err) {
console.error('Failed to load meta.json:', err.message);
return { version: 1, projects: {}, sessions: {} };
}
}
function saveMeta(meta) {
ensureMetaDir();
try {
fs.writeFileSync(META_FILE, JSON.stringify(meta, null, 2), 'utf-8');
} catch (err) {
console.error('Failed to save meta.json:', err.message);
}
}
// ── State ────────────────────────────────────────────────────────────────────
// activeSessions: Map<sessionId, sessionObject> — runtime state for sessions we've spawned
const activeSessions = new Map();
// ptyProcesses: Map<sessionId, ptyProcess>
const ptyProcesses = new Map();
// scrollbackBuffers: Map<sessionId, Buffer> — circular buffer of raw pty output
const scrollbackBuffers = new Map();
// wsClients: Map<sessionId, Set<WebSocket>>
const wsClients = new Map();
// Activity tracking for status detection
const lastOutputTime = new Map();
const lastInputTime = new Map();
// ── Session index cache ──────────────────────────────────────────────────────
let sessionCache = null;
let sessionCacheTime = 0;
function stripSummaryMarkdown(summary) {
if (!summary) return summary;
// Strip ```json\n{"title": "..."}``` wrapping
const jsonMatch = summary.match(/^```(?:json)?\s*\n?\{[\s\S]*?"title"\s*:\s*"([^"]+)"[\s\S]*?\}\s*\n?```$/);
if (jsonMatch) return jsonMatch[1];
// Strip plain ``` wrapping
const plainMatch = summary.match(/^```[\s\S]*?\n([\s\S]*?)\n?```$/);
if (plainMatch) return plainMatch[1].trim();
return summary;
}
function deriveGroupName(projectPath) {
if (!projectPath) return null;
const home = process.env.HOME || '/Users/maxwraae';
if (projectPath === home) return null; // Home dir sessions are ungrouped
const relative = projectPath.replace(home, '').replace(/^\//, '');
const segments = relative.split('/').filter(Boolean);
if (segments.length === 0) return null;
if (segments.length === 1) return segments[0];
// For deep paths like Library/CloudStorage/.../CV-Resume, use last segment
return segments[segments.length - 1];
}
function deriveStatus(entry) {
// If we have an active pty for this session, use real-time status
if (activeSessions.has(entry.sessionId)) {
return activeSessions.get(entry.sessionId).status;
}
const now = Date.now();
const modified = new Date(entry.modified).getTime();
const age = now - modified;
if (age < 60000) return 'running'; // Modified < 1 min ago
if (age < 300000) return 'done'; // Modified < 5 min ago
return 'idle'; // Older than 5 min
}
function extractSessionMetadata(jsonlPath, sessionId) {
try {
// Read only the first ~10KB of the file to avoid loading large files
const READ_LIMIT = 10 * 1024;
const fd = fs.openSync(jsonlPath, 'r');
const buf = Buffer.alloc(READ_LIMIT);
const bytesRead = fs.readSync(fd, buf, 0, READ_LIMIT, 0);
fs.closeSync(fd);
const chunk = buf.slice(0, bytesRead).toString('utf-8');
const lines = chunk.split('\n');
let firstPrompt = null;
let created = null;
let isSidechain = false;
let projectPath = null;
let messageCount = 0;
let seenIsSidechain = false;
for (const line of lines) {
if (!line.trim()) continue;
let obj;
try {
obj = JSON.parse(line);
} catch {
continue; // Skip malformed lines (last line may be partial)
}
// Capture isSidechain from any line that has it
if (!seenIsSidechain && obj.isSidechain !== undefined) {
isSidechain = !!obj.isSidechain;
seenIsSidechain = true;
}
// Capture cwd (projectPath) from any line that has it
if (!projectPath && obj.cwd) {
projectPath = obj.cwd;
}
// Capture earliest timestamp as created
if (obj.timestamp) {
const t = new Date(obj.timestamp).getTime();
if (!isNaN(t) && (created === null || t < created)) {
created = t;
}
}
// Count user/assistant messages
if (obj.type === 'user' || obj.type === 'assistant') {
messageCount++;
}
// Extract first user prompt
if (!firstPrompt && obj.type === 'user' && obj.message) {
const content = obj.message.content;
if (typeof content === 'string') {
firstPrompt = content;
} else if (Array.isArray(content)) {
for (const block of content) {
if (block && block.type === 'text' && block.text) {
firstPrompt = block.text;
break;
}
}
}
}
}
const stat = fs.statSync(jsonlPath);
const modified = stat.mtime.toISOString();
return {
sessionId,
firstPrompt: firstPrompt ? firstPrompt.substring(0, 500) : null,
created: created ? new Date(created).toISOString() : modified,
modified,
isSidechain,
projectPath,
summary: null,
messageCount,
};
} catch (err) {
console.error(`Failed to extract metadata from ${jsonlPath}:`, err.message);
return null;
}
}
function loadSessionIndex(includeArchived = false) {
const now = Date.now();
if (!includeArchived && sessionCache && now - sessionCacheTime < SESSION_CACHE_TTL_MS) {
return sessionCache;
}
const allEntries = new Map(); // keyed by sessionId for deduplication
let projectDirs;
try {
projectDirs = fs.readdirSync(CLAUDE_PROJECTS_DIR);
} catch (err) {
console.error('Failed to read Claude projects dir:', err.message);
sessionCache = [];
sessionCacheTime = now;
return sessionCache;
}
for (const dir of projectDirs) {
const projectDir = path.join(CLAUDE_PROJECTS_DIR, dir);
// Skip if not a directory
try {
if (!fs.statSync(projectDir).isDirectory()) continue;
} catch {
continue;
}
const indexPath = path.join(projectDir, 'sessions-index.json');
// First pass: read the sessions-index.json if it exists
let indexMtime = 0;
if (fs.existsSync(indexPath)) {
try {
indexMtime = fs.statSync(indexPath).mtimeMs;
const raw = fs.readFileSync(indexPath, 'utf-8');
const index = JSON.parse(raw);
if (Array.isArray(index.entries)) {
for (const entry of index.entries) {
if (!entry.sessionId) continue;
if (entry.isSidechain) continue;
const fp = (entry.firstPrompt || '').toLowerCase();
if (fp.startsWith('you are a memory extraction system')) continue;
if (fp.startsWith('create these memory entities')) continue;
if (fp.startsWith('no prompt') && !entry.summary) continue;
if (entry.messageCount <= 1) continue;
if (!allEntries.has(entry.sessionId)) {
allEntries.set(entry.sessionId, entry);
}
}
}
} catch (err) {
console.error(`Failed to parse ${indexPath}:`, err.message);
}
}
// Second pass: scan for .jsonl files newer than the index
let dirEntries;
try {
dirEntries = fs.readdirSync(projectDir);
} catch {
continue;
}
for (const filename of dirEntries) {
if (!filename.endsWith('.jsonl')) continue;
const jsonlPath = path.join(projectDir, filename);
let fileStat;
try {
fileStat = fs.statSync(jsonlPath);
} catch {
continue;
}
// Only process files newer than the index
if (fileStat.mtimeMs <= indexMtime) continue;
// Extract sessionId from filename (strip .jsonl extension)
const sessionId = filename.slice(0, -6);
// Skip if already indexed
if (allEntries.has(sessionId)) continue;
const meta = extractSessionMetadata(jsonlPath, sessionId);
if (!meta) continue;
if (meta.isSidechain) continue;
const fp = (meta.firstPrompt || '').toLowerCase();
if (fp.startsWith('you are a memory extraction system')) continue;
if (fp.startsWith('create these memory entities')) continue;
if (!meta.firstPrompt && !meta.summary) continue;
if (meta.messageCount <= 1) continue;
allEntries.set(sessionId, meta);
}
}
const home = process.env.HOME || '/Users/maxwraae';
const list = Array.from(allEntries.values()).map(entry => {
const rawSummary = entry.summary || null;
const cleanSummary = stripSummaryMarkdown(rawSummary);
const name = cleanSummary
|| (entry.firstPrompt && entry.firstPrompt !== 'No prompt'
? entry.firstPrompt.substring(0, 50)
: null)
|| 'Untitled';
const preview = (entry.firstPrompt && entry.firstPrompt !== 'No prompt')
? entry.firstPrompt.trim().substring(0, 150)
: '';
// If we have an active session overriding some fields, merge them
const active = activeSessions.get(entry.sessionId);
return {
id: entry.sessionId,
name,
group: null, // Only set from metadata; directory path available in workingDir
status: deriveStatus(entry),
createdAt: entry.created,
lastActivity: active ? active.lastActivity : entry.modified,
preview: active ? (active.preview || preview) : preview,
workingDir: entry.projectPath,
messageCount: entry.messageCount,
};
});
// Merge metadata overlay
const meta = loadMeta();
const enriched = list.map(entry => {
const sessionMeta = meta.sessions[entry.id] || {};
// Apply custom name if set
if (sessionMeta.customName) {
entry.name = sessionMeta.customName;
}
// Apply project from metadata (overrides directory-derived group)
if (sessionMeta.project) {
entry.group = sessionMeta.project;
}
// Add pin/archive flags
entry.pinned = sessionMeta.pinned || false;
entry.archived = sessionMeta.archived || false;
// Add project color if project exists in meta
const projectMeta = meta.projects[entry.group];
if (projectMeta) {
entry.projectColor = projectMeta.color;
entry.projectIcon = projectMeta.icon;
}
return entry;
});
// Filter out archived sessions by default
const filtered = includeArchived ? enriched : enriched.filter(e => !e.archived);
// Sort: pinned first, then by lastActivity
filtered.sort((a, b) => {
if (a.pinned && !b.pinned) return -1;
if (!a.pinned && b.pinned) return 1;
return new Date(b.lastActivity) - new Date(a.lastActivity);
});
if (!includeArchived) {
sessionCache = filtered;
sessionCacheTime = now;
}
return filtered;
}
function invalidateSessionCache() {
sessionCacheTime = 0;
}
// ── Scrollback buffer ────────────────────────────────────────────────────────
function appendScrollback(id, data) {
const buf = scrollbackBuffers.get(id) || Buffer.alloc(0);
const chunk = Buffer.isBuffer(data) ? data : Buffer.from(data);
let combined = Buffer.concat([buf, chunk]);
if (combined.length > SCROLLBACK_SIZE) {
combined = combined.slice(combined.length - SCROLLBACK_SIZE);
}
scrollbackBuffers.set(id, combined);
}
// ── Preview text ─────────────────────────────────────────────────────────────
// Patterns that indicate terminal metadata rather than conversation content
const TERMINAL_JUNK_RE = /(\[ws-[0-9a-f]+|Claude Code"|\\x1b\[|\u001b\[|\[0,0\]|ESC\[|\[\d+,\d+\])/i;
function isTerminalMetadata(line) {
if (!line || !line.trim()) return true;
// tmux pane title format: [ws-<id>:...]
if (/^\[ws-[0-9a-f]/.test(line)) return true;
// Contains Claude Code terminal title
if (/Claude Code"/.test(line)) return true;
// Contains ANSI escape sequences (raw or unicode)
if (/\x1b\[|\u001b\[/.test(line)) return true;
// Contains terminal coordinate patterns like [0,0]
if (/\[\d+,\d+\]/.test(line)) return true;
// tmux status bar patterns
if (/^\s*\d+:\d+\s*\[/.test(line)) return true;
return false;
}
function updatePreview(id, rawData) {
const session = activeSessions.get(id);
if (!session) return;
const text = typeof rawData === 'string' ? rawData : rawData.toString('utf-8');
const stripped = text.replace(ANSI_STRIP_RE, '');
const lines = stripped.split('\n')
.map(l => l.trim())
.filter(l => l.length > 0 && !isTerminalMetadata(l));
if (lines.length > 0) {
session.preview = lines[lines.length - 1].substring(0, 150);
}
}
// ── tmux / pty ───────────────────────────────────────────────────────────────
function tmuxName(id) {
return `ws-${id}`;
}
function tmuxSessionExists(id) {
try {
execSync(`tmux has-session -t ${tmuxName(id)} 2>/dev/null`);
return true;
} catch {
return false;
}
}
function reattachSession(id, workingDir) {
if (!tmuxSessionExists(id)) return false;
const cwd = workingDir || process.env.HOME;
const cols = 200;
const rows = 50;
const proc = pty.spawn('tmux', ['attach-session', '-t', tmuxName(id)], {
name: 'xterm-256color',
cols,
rows,
cwd,
env: process.env,
});
ptyProcesses.set(id, proc);
// Ensure active session tracking exists
if (!activeSessions.has(id)) {
activeSessions.set(id, {
id,
status: 'idle',
lastActivity: new Date().toISOString(),
preview: null,
workingDir: cwd,
});
}
setupPtyListeners(id, proc);
lastOutputTime.set(id, Date.now());
return true;
}
function spawnSession(id, workingDir) {
const cols = 200;
const rows = 50;
const cwd = workingDir || process.env.HOME;
const name = tmuxName(id);
// Spawn tmux with claude inside via node-pty
const proc = pty.spawn(
'tmux',
[
'new-session', '-d', '-s', name, '-x', String(cols), '-y', String(rows),
'claude', '--dangerously-skip-permissions',
],
{
name: 'xterm-256color',
cols,
rows,
cwd,
env: process.env,
}
);
// tmux new-session -d exits immediately. Attach after a short delay.
proc.onExit(() => {
try { execSync(`tmux set-option -t "${name}" status off 2>/dev/null`); } catch (_) {}
setTimeout(() => {
const attachProc = pty.spawn('tmux', ['attach-session', '-t', name], {
name: 'xterm-256color',
cols,
rows,
cwd,
env: process.env,
});
ptyProcesses.set(id, attachProc);
setupPtyListeners(id, attachProc);
}, 300);
});
}
function setupPtyListeners(id, proc) {
proc.onData((data) => {
const session = activeSessions.get(id);
if (!session) return;
const now = Date.now();
session.lastActivity = new Date().toISOString();
session.status = 'running';
lastOutputTime.set(id, now);
appendScrollback(id, data);
updatePreview(id, data);
broadcastRaw(id, data);
});
proc.onExit(({ exitCode }) => {
const session = activeSessions.get(id);
if (session) {
session.status = exitCode === 0 ? 'done' : 'error';
session.lastActivity = new Date().toISOString();
}
ptyProcesses.delete(id);
lastOutputTime.delete(id);
lastInputTime.delete(id);
invalidateSessionCache();
const exitMsg = `\r\n\x1B[31m[Process exited with code ${exitCode}]\x1B[0m\r\n`;
broadcastRaw(id, exitMsg);
});
}
// ── Status detection (for active pty sessions) ───────────────────────────────
setInterval(() => {
const now = Date.now();
for (const [id, session] of activeSessions) {
if (session.status === 'done' || session.status === 'error') continue;
if (!ptyProcesses.has(id)) {
if (session.status !== 'error') {
session.status = 'error';
}
continue;
}
const lastOut = lastOutputTime.get(id) || 0;
const elapsed = now - lastOut;
if (elapsed > IDLE_TIMEOUT_MS && session.status === 'running') {
session.status = 'idle';
}
}
}, STATUS_CHECK_INTERVAL_MS);
// ── WebSocket broadcast ──────────────────────────────────────────────────────
function broadcastRaw(sessionId, data) {
const clients = wsClients.get(sessionId);
if (!clients) return;
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
for (const ws of clients) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(buf);
}
}
}
// ── Express app ──────────────────────────────────────────────────────────────
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// GET /sessions — List all sessions from Claude Code's native storage
app.get('/sessions', (req, res) => {
const includeArchived = req.query.archived === 'true';
const list = loadSessionIndex(includeArchived);
res.json(list);
});
// GET /sessions/:id — Get single session
app.get('/sessions/:id', (req, res) => {
const id = req.params.id;
const list = loadSessionIndex();
const session = list.find(s => s.id === id);
if (!session) {
return res.status(404).json({ error: 'Session not found' });
}
res.json(session);
});
// POST /sessions — Create a new session
app.post('/sessions', (req, res) => {
const { name, group, workingDir, message } = req.body;
const id = crypto.randomUUID();
const cwd = workingDir || process.env.HOME;
const now = new Date().toISOString();
// Track in activeSessions for runtime state
const session = {
id,
name: name || 'New Session',
group: group || null,
status: 'running',
createdAt: now,
lastActivity: now,
preview: null,
workingDir: cwd,
messageCount: 0,
};
activeSessions.set(id, session);
scrollbackBuffers.set(id, Buffer.alloc(0));
wsClients.set(id, new Set());
spawnSession(id, cwd);
// Persist metadata if a project or custom name was specified
if (group || (name && name !== 'New Session')) {
const meta = loadMeta();
meta.sessions[id] = meta.sessions[id] || {};
if (group) {
meta.sessions[id].project = group;
}
if (name && name !== 'New Session' && name !== 'Untitled') {
meta.sessions[id].customName = name;
}
saveMeta(meta);
}
invalidateSessionCache();
// Send initial message to pty after process starts
if (message) {
setTimeout(() => {
const proc = ptyProcesses.get(id);
if (proc) {
proc.write(message + '\n');
}
}, 1500);
}
res.status(201).json(session);
});
// DELETE /sessions/:id — Kill session
app.delete('/sessions/:id', (req, res) => {
const id = req.params.id;
// Kill the pty process
const proc = ptyProcesses.get(id);
if (proc) {
proc.kill();
ptyProcesses.delete(id);
}
// Kill the tmux session
try {
execSync(`tmux kill-session -t ${tmuxName(id)} 2>/dev/null`);
} catch {
// Already dead, that's fine
}
// Close WebSocket connections
const clients = wsClients.get(id);
if (clients) {
for (const ws of clients) {
ws.close(1000, 'Session deleted');
}
}
// Cleanup runtime state
activeSessions.delete(id);
scrollbackBuffers.delete(id);
wsClients.delete(id);
lastOutputTime.delete(id);
lastInputTime.delete(id);
invalidateSessionCache();
res.json({ ok: true });
});
// PATCH /sessions/:id/meta — Update session metadata (custom name, project, pin, archive)
app.patch('/sessions/:id/meta', (req, res) => {
const meta = loadMeta();
const id = req.params.id;
const updates = req.body; // { customName?, project?, pinned?, archived? }
if (!meta.sessions[id]) {
meta.sessions[id] = {};
}
// Only store non-null values (sparse storage)
for (const [key, value] of Object.entries(updates)) {
if (['customName', 'project', 'pinned', 'archived'].includes(key)) {
if (value === null || value === undefined || value === '' || value === false) {
delete meta.sessions[id][key];
} else {
meta.sessions[id][key] = value;
}
}
}
// Clean up empty session entries
if (Object.keys(meta.sessions[id]).length === 0) {
delete meta.sessions[id];
}
saveMeta(meta);
invalidateSessionCache();
res.json({ ok: true });
});
// GET /projects — List all projects
app.get('/projects', (req, res) => {
const meta = loadMeta();
res.json(meta.projects);
});
// POST /projects — Create a new project
app.post('/projects', (req, res) => {
const { name, color, defaultDir, icon } = req.body;
if (!name) return res.status(400).json({ error: 'Name required' });
const meta = loadMeta();
meta.projects[name] = {
color: color || PROJECT_COLORS[Object.keys(meta.projects).length % PROJECT_COLORS.length],
defaultDir: defaultDir || process.env.HOME,
icon: icon || name[0].toUpperCase(),
};
saveMeta(meta);
invalidateSessionCache();
res.json(meta.projects[name]);
});
// PATCH /projects/:name — Update project (rename, recolor, etc.)
app.patch('/projects/:name', (req, res) => {
const meta = loadMeta();
const oldName = req.params.name;
if (!meta.projects[oldName]) {
return res.status(404).json({ error: 'Project not found' });
}
const { name: newName, color, defaultDir, icon } = req.body;
if (newName && newName !== oldName) {
// Rename: move project and update all session references
meta.projects[newName] = { ...meta.projects[oldName] };
delete meta.projects[oldName];
for (const session of Object.values(meta.sessions)) {
if (session.project === oldName) {
session.project = newName;
}
}
}
const target = meta.projects[newName || oldName];
if (color) target.color = color;
if (defaultDir) target.defaultDir = defaultDir;
if (icon) target.icon = icon;
saveMeta(meta);
invalidateSessionCache();
res.json(target);
});
// DELETE /projects/:name — Delete project (ungroups its sessions)
app.delete('/projects/:name', (req, res) => {
const meta = loadMeta();
const name = req.params.name;
if (!meta.projects[name]) {
return res.status(404).json({ error: 'Project not found' });
}
delete meta.projects[name];
// Remove project references from sessions
for (const session of Object.values(meta.sessions)) {
if (session.project === name) {
delete session.project;
}
}
saveMeta(meta);
invalidateSessionCache();
res.json({ ok: true });
});
// SPA catch-all: serve index.html for client-side routes
app.get('/session/:id', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/project/:name', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// ── HTTP + WebSocket server ──────────────────────────────────────────────────
const server = http.createServer(app);
const wss = new WebSocketServer({ noServer: true });
// Handle upgrade for WebSocket
server.on('upgrade', (request, socket, head) => {
const pathname = url.parse(request.url).pathname;
const match = pathname.match(/^\/sessions\/([^/]+)\/stream$/);
if (!match) {
socket.destroy();
return;
}
const sessionId = match[1];
// Session must exist in index or active sessions
const list = loadSessionIndex();
const sessionExists = list.some(s => s.id === sessionId) || activeSessions.has(sessionId);
if (!sessionExists) {
socket.write('HTTP/1.1 404 Not Found\r\n\r\n');
socket.destroy();
return;
}
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request, sessionId);
});
});
wss.on('connection', (ws, request, sessionId) => {
// Register this client
let clients = wsClients.get(sessionId);
if (!clients) {
clients = new Set();
wsClients.set(sessionId, clients);
}
clients.add(ws);
console.log(`WebSocket connected to session ${sessionId} (${clients.size} clients)`);
// If no active pty for this session, try to attach
if (!ptyProcesses.has(sessionId)) {
if (tmuxSessionExists(sessionId)) {
// Reattach to existing tmux session
const list = loadSessionIndex();
const entry = list.find(s => s.id === sessionId);
const workingDir = entry ? entry.workingDir : process.env.HOME;
reattachSession(sessionId, workingDir);
} else {
// Spawn a new claude --resume session
const list = loadSessionIndex();
const entry = list.find(s => s.id === sessionId);
const cwd = entry ? (entry.workingDir || process.env.HOME) : process.env.HOME;
const name = tmuxName(sessionId);
const cols = 200;
const rows = 50;
if (!activeSessions.has(sessionId)) {
activeSessions.set(sessionId, {
id: sessionId,
status: 'running',
lastActivity: new Date().toISOString(),
preview: null,
workingDir: cwd,
});
}
if (!scrollbackBuffers.has(sessionId)) {
scrollbackBuffers.set(sessionId, Buffer.alloc(0));
}
const spawnProc = pty.spawn(
'tmux',
[
'new-session', '-d', '-s', name, '-x', String(cols), '-y', String(rows),
'claude', '--resume', sessionId, '--dangerously-skip-permissions',
],
{ name: 'xterm-256color', cols, rows, cwd, env: process.env }
);
spawnProc.onExit(() => {
try { execSync(`tmux set-option -t "${name}" status off 2>/dev/null`); } catch (_) {}
setTimeout(() => {
const attachProc = pty.spawn('tmux', ['attach-session', '-t', name], {
name: 'xterm-256color',
cols,
rows,
cwd,
env: process.env,
});
ptyProcesses.set(sessionId, attachProc);
setupPtyListeners(sessionId, attachProc);
}, 300);
});
}
}
// Send scrollback buffer for reconnection
const scrollback = scrollbackBuffers.get(sessionId);
if (scrollback && scrollback.length > 0 && ws.readyState === WebSocket.OPEN) {
ws.send(scrollback);
}
// Handle incoming messages — raw keystrokes or resize commands
ws.on('message', (data) => {
const message = data.toString();
// Check if this is a resize command
if (message.charAt(0) === '{' && message.includes('"type"') && message.includes('"resize"')) {
try {
const cmd = JSON.parse(message);
if (cmd.type === 'resize' && cmd.cols && cmd.rows) {
const proc = ptyProcesses.get(sessionId);
if (proc) {
proc.resize(cmd.cols, cmd.rows);
}
try {
execSync(`tmux resize-window -t ${tmuxName(sessionId)} -x ${cmd.cols} -y ${cmd.rows} 2>/dev/null`);
} catch {
// tmux resize may fail if session doesn't exist, that's ok
}
return;
}
} catch {
// Not valid JSON — fall through to write as raw input
}
}
// Raw keystroke input — write directly to pty stdin
const proc = ptyProcesses.get(sessionId);
if (proc) {
proc.write(message);
const session = activeSessions.get(sessionId);
if (session) {
session.lastActivity = new Date().toISOString();
session.status = 'running';
lastInputTime.set(sessionId, Date.now());
}
}
});
ws.on('close', () => {
clients.delete(ws);
console.log(`WebSocket disconnected from session ${sessionId} (${clients.size} clients remaining)`);
});
ws.on('error', (err) => {
console.error(`WebSocket error on session ${sessionId}:`, err.message);
clients.delete(ws);
});
});
// ── Startup ──────────────────────────────────────────────────────────────────
function autoDiscoverProjects() {
const meta = loadMeta();
const sessions = loadSessionIndex(true); // include archived
const groups = new Map();
for (const session of sessions) {
if (session.group && !meta.projects[session.group]) {
if (!groups.has(session.group)) {
groups.set(session.group, session.workingDir);
}
}
}
if (groups.size > 0) {