@tina_summer/codepocket 2.2.1 → 2.2.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.
- package/bin/codepocket.js +171 -11
- package/package.json +2 -2
- package/vendor/cpkt-hub +0 -0
package/bin/codepocket.js
CHANGED
|
@@ -49,6 +49,48 @@ function injectMcpConfig(port) {
|
|
|
49
49
|
|
|
50
50
|
// ── Helpers ──
|
|
51
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Encode a directory path the same way Claude Code does:
|
|
54
|
+
* resolve() then replace non-alphanumeric with '-'
|
|
55
|
+
*/
|
|
56
|
+
function encodeProjectPath(dir) {
|
|
57
|
+
return path.resolve(dir).replace(/[^a-zA-Z0-9]/g, '-')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Find the Claude Code session ID (.jsonl file) for a given working directory.
|
|
62
|
+
* Tries to match by directory encoding; falls back to most recent valid file.
|
|
63
|
+
*/
|
|
64
|
+
function findClaudeSessionFile(directory) {
|
|
65
|
+
const claudeConfigDir = process.env.CLAUDE_CONFIG_DIR || path.join(os.homedir(), '.claude')
|
|
66
|
+
const projectId = encodeProjectPath(directory)
|
|
67
|
+
const projectDir = path.join(claudeConfigDir, 'projects', projectId)
|
|
68
|
+
|
|
69
|
+
if (!fs.existsSync(projectDir)) return null
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const files = fs.readdirSync(projectDir)
|
|
73
|
+
.filter(f => f.endsWith('.jsonl'))
|
|
74
|
+
.map(f => {
|
|
75
|
+
const filePath = path.join(projectDir, f)
|
|
76
|
+
try {
|
|
77
|
+
const stat = fs.statSync(filePath)
|
|
78
|
+
// Check file has real content (at least one line with uuid)
|
|
79
|
+
const data = fs.readFileSync(filePath, 'utf-8').split('\n')
|
|
80
|
+
const hasContent = data.some(line => {
|
|
81
|
+
try { return typeof JSON.parse(line).uuid === 'string' }
|
|
82
|
+
catch { return false }
|
|
83
|
+
})
|
|
84
|
+
return { name: f.replace('.jsonl', ''), mtime: stat.mtime.getTime(), hasContent }
|
|
85
|
+
} catch { return null }
|
|
86
|
+
})
|
|
87
|
+
.filter(f => f && f.hasContent)
|
|
88
|
+
.sort((a, b) => b.mtime - a.mtime)
|
|
89
|
+
|
|
90
|
+
return files.length > 0 ? files[0].name : null
|
|
91
|
+
} catch { return null }
|
|
92
|
+
}
|
|
93
|
+
|
|
52
94
|
function findHubBin() {
|
|
53
95
|
const bundledBin = path.join(__dirname, '..', 'vendor', process.platform === 'win32' ? 'cpkt-hub.exe' : 'cpkt-hub')
|
|
54
96
|
if (fs.existsSync(bundledBin)) return bundledBin
|
|
@@ -136,7 +178,7 @@ const CONNECT_ONLY = ['connect', 'c'].includes(args[0])
|
|
|
136
178
|
const filteredArgs = CONNECT_ONLY ? args.slice(1) : args
|
|
137
179
|
|
|
138
180
|
let hubUrl = process.env.CPKT_HUB_URL || process.env.HAPI_HUB_URL || 'http://127.0.0.1:3006'
|
|
139
|
-
const DEFAULT_RELAY_URL = '
|
|
181
|
+
const DEFAULT_RELAY_URL = ''
|
|
140
182
|
let relayUrl = process.env.RELAY_WS_URL || process.env.RELAY_URL || DEFAULT_RELAY_URL
|
|
141
183
|
let relayKey = process.env.RELAY_KEY || ''
|
|
142
184
|
let noHub = false
|
|
@@ -365,6 +407,10 @@ function connectRelay() {
|
|
|
365
407
|
handleSpawnSession(msg.requestId, msg.directory)
|
|
366
408
|
break
|
|
367
409
|
}
|
|
410
|
+
case 'resume-session': {
|
|
411
|
+
handleResumeSession(msg.requestId, msg.sessionId)
|
|
412
|
+
break
|
|
413
|
+
}
|
|
368
414
|
case 'list-dir': {
|
|
369
415
|
handleListDir(msg.requestId, msg.path)
|
|
370
416
|
break
|
|
@@ -384,6 +430,25 @@ function sendToRelay(msg) {
|
|
|
384
430
|
if (relayWs?.readyState === WebSocket.OPEN) relayWs.send(JSON.stringify(msg))
|
|
385
431
|
}
|
|
386
432
|
|
|
433
|
+
function sendSpawnResult(requestId, sessionId, error) {
|
|
434
|
+
const msg = error
|
|
435
|
+
? { type: 'spawn-session-result', requestId, success: false, error }
|
|
436
|
+
: { type: 'spawn-session-result', requestId, success: true, sessionId }
|
|
437
|
+
// Retry sending if relay connection is temporarily down
|
|
438
|
+
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
439
|
+
relayWs.send(JSON.stringify(msg))
|
|
440
|
+
} else {
|
|
441
|
+
log(`[codepocket] Relay not open, retrying spawn result in 2s...`)
|
|
442
|
+
setTimeout(() => {
|
|
443
|
+
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
444
|
+
relayWs.send(JSON.stringify(msg))
|
|
445
|
+
} else {
|
|
446
|
+
log(`[codepocket] Failed to send spawn result, relay still down`)
|
|
447
|
+
}
|
|
448
|
+
}, 2000)
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
387
452
|
async function doForwardToHub(requestId, method, p, body) {
|
|
388
453
|
const init = {
|
|
389
454
|
method,
|
|
@@ -440,23 +505,26 @@ async function handleSpawnSession(requestId, directory) {
|
|
|
440
505
|
|
|
441
506
|
try {
|
|
442
507
|
const child = spawn(hubBin, ['--path', directory], {
|
|
443
|
-
stdio:
|
|
508
|
+
stdio: 'ignore',
|
|
444
509
|
detached: true,
|
|
445
510
|
env: { ...process.env, ...HOME_ENV },
|
|
446
511
|
})
|
|
447
512
|
child.unref()
|
|
513
|
+
child.on('error', (err) => {
|
|
514
|
+
log(`[codepocket] Spawn process error: ${err.message}`)
|
|
515
|
+
})
|
|
448
516
|
|
|
449
517
|
// Wait for session to be created by polling /sessions
|
|
450
518
|
let sessionId = null
|
|
451
519
|
for (let i = 0; i < 30; i++) {
|
|
452
520
|
await new Promise(r => setTimeout(r, 1000))
|
|
453
521
|
try {
|
|
454
|
-
const res = await fetch(`${hubUrl}/sessions`, {
|
|
522
|
+
const res = await fetch(`${hubUrl}/api/sessions`, {
|
|
455
523
|
headers: jwt ? { Authorization: `Bearer ${jwt}` } : {},
|
|
456
524
|
})
|
|
457
|
-
const
|
|
458
|
-
|
|
459
|
-
const match =
|
|
525
|
+
const data = await res.json()
|
|
526
|
+
const sessions = data.sessions || (Array.isArray(data) ? data : [])
|
|
527
|
+
const match = sessions.find(s =>
|
|
460
528
|
s.metadata?.path === directory && s.id
|
|
461
529
|
)
|
|
462
530
|
if (match) {
|
|
@@ -468,14 +536,106 @@ async function handleSpawnSession(requestId, directory) {
|
|
|
468
536
|
|
|
469
537
|
if (sessionId) {
|
|
470
538
|
log(`[codepocket] Session created: ${sessionId}`)
|
|
471
|
-
sendToRelay({ type: 'spawn-session-result', requestId, success: true, sessionId })
|
|
472
539
|
} else {
|
|
473
|
-
|
|
474
|
-
sendToRelay({ type: 'spawn-session-result', requestId, success: true, sessionId: null, warning: 'Session initializing' })
|
|
540
|
+
log(`[codepocket] Session not found after polling, directory=${directory}`)
|
|
475
541
|
}
|
|
542
|
+
sendSpawnResult(requestId, sessionId)
|
|
476
543
|
} catch (err) {
|
|
477
544
|
console.error(`[codepocket] Spawn failed: ${err.message}`)
|
|
478
|
-
|
|
545
|
+
sendSpawnResult(requestId, null, err.message)
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async function handleResumeSession(requestId, sessionId) {
|
|
550
|
+
if (!hubBin) {
|
|
551
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Hub binary not found' })
|
|
552
|
+
return
|
|
553
|
+
}
|
|
554
|
+
if (!sessionId) {
|
|
555
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Session ID is required' })
|
|
556
|
+
return
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
log(`[codepocket] Resuming session: ${sessionId}`)
|
|
560
|
+
|
|
561
|
+
try {
|
|
562
|
+
// 1. Fetch session metadata from Hub
|
|
563
|
+
let res = await fetch(`${hubUrl}/api/sessions/${sessionId}`, {
|
|
564
|
+
headers: jwt ? { Authorization: `Bearer ${jwt}` } : {},
|
|
565
|
+
})
|
|
566
|
+
if (res.status === 401) {
|
|
567
|
+
const token = await findHubToken()
|
|
568
|
+
jwt = await authenticateHub(token)
|
|
569
|
+
res = await fetch(`${hubUrl}/api/sessions/${sessionId}`, {
|
|
570
|
+
headers: { Authorization: `Bearer ${jwt}` },
|
|
571
|
+
})
|
|
572
|
+
}
|
|
573
|
+
if (!res.ok) {
|
|
574
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: false, error: `Session not found (${res.status})` })
|
|
575
|
+
return
|
|
576
|
+
}
|
|
577
|
+
const data = await res.json()
|
|
578
|
+
const session = data.session || data
|
|
579
|
+
const directory = (session.metadata?.path || '').replace(/^~/, os.homedir())
|
|
580
|
+
let claudeSessionId = session.metadata?.claudeSessionId
|
|
581
|
+
|
|
582
|
+
if (!directory) {
|
|
583
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Session has no working directory' })
|
|
584
|
+
return
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Fallback: if claudeSessionId not in metadata, scan for the session file directly
|
|
588
|
+
if (!claudeSessionId) {
|
|
589
|
+
claudeSessionId = findClaudeSessionFile(directory)
|
|
590
|
+
if (claudeSessionId) {
|
|
591
|
+
log(`[codepocket] claudeSessionId not in metadata, found session file: ${claudeSessionId}`)
|
|
592
|
+
} else {
|
|
593
|
+
log(`[codepocket] claudeSessionId not in metadata and no session file found, using Hub session ID as fallback`)
|
|
594
|
+
claudeSessionId = sessionId
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// 2. Spawn with --resume
|
|
599
|
+
log(`[codepocket] Spawning resume: --path ${directory} --resume ${claudeSessionId}`)
|
|
600
|
+
const child = spawn(hubBin, ['--path', directory, '--resume', claudeSessionId], {
|
|
601
|
+
stdio: 'ignore',
|
|
602
|
+
detached: true,
|
|
603
|
+
env: { ...process.env, ...HOME_ENV },
|
|
604
|
+
})
|
|
605
|
+
child.unref()
|
|
606
|
+
child.on('error', (err) => {
|
|
607
|
+
log(`[codepocket] Resume spawn process error: ${err.message}`)
|
|
608
|
+
})
|
|
609
|
+
|
|
610
|
+
// 3. Poll for session to become active
|
|
611
|
+
let resumed = false
|
|
612
|
+
for (let i = 0; i < 30; i++) {
|
|
613
|
+
await new Promise(r => setTimeout(r, 1000))
|
|
614
|
+
try {
|
|
615
|
+
const pollRes = await fetch(`${hubUrl}/api/sessions`, {
|
|
616
|
+
headers: jwt ? { Authorization: `Bearer ${jwt}` } : {},
|
|
617
|
+
})
|
|
618
|
+
const pollData = await pollRes.json()
|
|
619
|
+
const sessions = pollData.sessions || (Array.isArray(pollData) ? pollData : [])
|
|
620
|
+
const match = sessions.find(s =>
|
|
621
|
+
s.metadata?.path === directory && s.active
|
|
622
|
+
)
|
|
623
|
+
if (match) {
|
|
624
|
+
resumed = true
|
|
625
|
+
break
|
|
626
|
+
}
|
|
627
|
+
} catch {}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
if (resumed) {
|
|
631
|
+
log(`[codepocket] Session resumed: ${sessionId}`)
|
|
632
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId })
|
|
633
|
+
} else {
|
|
634
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId: null, warning: 'Session resuming' })
|
|
635
|
+
}
|
|
636
|
+
} catch (err) {
|
|
637
|
+
console.error(`[codepocket] Resume failed: ${err.message}`)
|
|
638
|
+
sendToRelay({ type: 'resume-session-result', requestId, success: false, error: err.message })
|
|
479
639
|
}
|
|
480
640
|
}
|
|
481
641
|
|
|
@@ -535,7 +695,7 @@ async function run() {
|
|
|
535
695
|
}
|
|
536
696
|
}
|
|
537
697
|
|
|
538
|
-
// 1.5 Relay URL (
|
|
698
|
+
// 1.5 Relay URL (set RELAY_WS_URL env or --relay flag)
|
|
539
699
|
if (!relayUrl) {
|
|
540
700
|
relayUrl = DEFAULT_RELAY_URL
|
|
541
701
|
}
|
package/package.json
CHANGED
package/vendor/cpkt-hub
CHANGED
|
Binary file
|