@tina_summer/codepocket 2.2.1 → 2.2.2

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.
Files changed (2) hide show
  1. package/bin/codepocket.js +82 -0
  2. package/package.json +1 -1
package/bin/codepocket.js CHANGED
@@ -365,6 +365,10 @@ function connectRelay() {
365
365
  handleSpawnSession(msg.requestId, msg.directory)
366
366
  break
367
367
  }
368
+ case 'resume-session': {
369
+ handleResumeSession(msg.requestId, msg.sessionId)
370
+ break
371
+ }
368
372
  case 'list-dir': {
369
373
  handleListDir(msg.requestId, msg.path)
370
374
  break
@@ -479,6 +483,84 @@ async function handleSpawnSession(requestId, directory) {
479
483
  }
480
484
  }
481
485
 
486
+ async function handleResumeSession(requestId, sessionId) {
487
+ if (!hubBin) {
488
+ sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Hub binary not found' })
489
+ return
490
+ }
491
+ if (!sessionId) {
492
+ sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Session ID is required' })
493
+ return
494
+ }
495
+
496
+ log(`[codepocket] Resuming session: ${sessionId}`)
497
+
498
+ try {
499
+ // 1. Fetch session metadata from Hub
500
+ let res = await fetch(`${hubUrl}/api/sessions/${sessionId}`, {
501
+ headers: jwt ? { Authorization: `Bearer ${jwt}` } : {},
502
+ })
503
+ if (res.status === 401) {
504
+ const token = await findHubToken()
505
+ jwt = await authenticateHub(token)
506
+ res = await fetch(`${hubUrl}/api/sessions/${sessionId}`, {
507
+ headers: { Authorization: `Bearer ${jwt}` },
508
+ })
509
+ }
510
+ if (!res.ok) {
511
+ sendToRelay({ type: 'resume-session-result', requestId, success: false, error: `Session not found (${res.status})` })
512
+ return
513
+ }
514
+ const data = await res.json()
515
+ const session = data.session || data
516
+ const directory = (session.metadata?.path || '').replace(/^~/, os.homedir())
517
+ const claudeSessionId = session.metadata?.claudeSessionId || session.id
518
+
519
+ if (!directory) {
520
+ sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Session has no working directory' })
521
+ return
522
+ }
523
+
524
+ // 2. Spawn with --resume
525
+ log(`[codepocket] Spawning resume: --path ${directory} --resume ${claudeSessionId}`)
526
+ const child = spawn(hubBin, ['--path', directory, '--resume', claudeSessionId], {
527
+ stdio: ['ignore', 'pipe', 'pipe'],
528
+ detached: true,
529
+ env: { ...process.env, ...HOME_ENV },
530
+ })
531
+ child.unref()
532
+
533
+ // 3. Poll for session to become active
534
+ let resumed = false
535
+ for (let i = 0; i < 30; i++) {
536
+ await new Promise(r => setTimeout(r, 1000))
537
+ try {
538
+ const pollRes = await fetch(`${hubUrl}/sessions`, {
539
+ headers: jwt ? { Authorization: `Bearer ${jwt}` } : {},
540
+ })
541
+ const sessions = await pollRes.json()
542
+ const match = (Array.isArray(sessions) ? sessions : []).find(s =>
543
+ s.metadata?.path === directory && s.active
544
+ )
545
+ if (match) {
546
+ resumed = true
547
+ break
548
+ }
549
+ } catch {}
550
+ }
551
+
552
+ if (resumed) {
553
+ log(`[codepocket] Session resumed: ${sessionId}`)
554
+ sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId })
555
+ } else {
556
+ sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId: null, warning: 'Session resuming' })
557
+ }
558
+ } catch (err) {
559
+ console.error(`[codepocket] Resume failed: ${err.message}`)
560
+ sendToRelay({ type: 'resume-session-result', requestId, success: false, error: err.message })
561
+ }
562
+ }
563
+
482
564
  function handleListDir(requestId, dirPath) {
483
565
  try {
484
566
  const resolved = (dirPath || '~').replace(/^~/, os.homedir())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tina_summer/codepocket",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "description": "CodePocket — 你的全能 AI 随身控制台",
5
5
  "bin": {
6
6
  "codepocket": "bin/codepocket.js",