@tina_summer/codepocket 2.2.3 → 2.2.4

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 +70 -12
  2. package/package.json +1 -1
package/bin/codepocket.js CHANGED
@@ -262,6 +262,23 @@ function log(msg) {
262
262
 
263
263
  // ── Hub ──
264
264
 
265
+ function waitForHub() {
266
+ return new Promise((resolve) => {
267
+ if (noHub || isHubRunning(hubUrl)) { resolve(); return }
268
+ let attempts = 0
269
+ const check = setInterval(() => {
270
+ attempts++
271
+ if (isHubRunning(hubUrl)) {
272
+ clearInterval(check)
273
+ resolve()
274
+ } else if (attempts > 30) {
275
+ clearInterval(check)
276
+ resolve()
277
+ }
278
+ }, 1000)
279
+ })
280
+ }
281
+
265
282
  function startHub(hubBin) {
266
283
  return new Promise((resolve) => {
267
284
  if (noHub) { resolve(); return }
@@ -577,7 +594,7 @@ async function handleResumeSession(requestId, sessionId) {
577
594
  const data = await res.json()
578
595
  const session = data.session || data
579
596
  const directory = (session.metadata?.path || '').replace(/^~/, os.homedir())
580
- let claudeSessionId = session.metadata?.claudeSessionId
597
+ let claudeSessionId = session.metadata?.agentSessionId || session.metadata?.claudeSessionId
581
598
 
582
599
  if (!directory) {
583
600
  sendToRelay({ type: 'resume-session-result', requestId, success: false, error: 'Session has no working directory' })
@@ -609,6 +626,7 @@ async function handleResumeSession(requestId, sessionId) {
609
626
 
610
627
  // 3. Poll for session to become active
611
628
  let resumed = false
629
+ let newSession = null
612
630
  for (let i = 0; i < 30; i++) {
613
631
  await new Promise(r => setTimeout(r, 1000))
614
632
  try {
@@ -618,9 +636,10 @@ async function handleResumeSession(requestId, sessionId) {
618
636
  const pollData = await pollRes.json()
619
637
  const sessions = pollData.sessions || (Array.isArray(pollData) ? pollData : [])
620
638
  const match = sessions.find(s =>
621
- s.metadata?.path === directory && s.active
639
+ s.metadata?.path === directory && s.active && s.id !== sessionId
622
640
  )
623
641
  if (match) {
642
+ newSession = match
624
643
  resumed = true
625
644
  break
626
645
  }
@@ -628,8 +647,18 @@ async function handleResumeSession(requestId, sessionId) {
628
647
  }
629
648
 
630
649
  if (resumed) {
631
- log(`[codepocket] Session resumed: ${sessionId}`)
632
- sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId })
650
+ // Delete the old inactive session to avoid duplicates
651
+ try {
652
+ await fetch(`${hubUrl}/api/sessions/${sessionId}`, {
653
+ method: 'DELETE',
654
+ headers: jwt ? { Authorization: `Bearer ${jwt}` } : {},
655
+ })
656
+ log(`[codepocket] Deleted old session: ${sessionId}`)
657
+ } catch (e) {
658
+ log(`[codepocket] Failed to delete old session: ${e.message}`)
659
+ }
660
+ log(`[codepocket] Session resumed: ${newSession.id} (replaced ${sessionId})`)
661
+ sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId: newSession.id })
633
662
  } else {
634
663
  sendToRelay({ type: 'resume-session-result', requestId, success: true, sessionId: null, warning: 'Session resuming' })
635
664
  }
@@ -680,6 +709,10 @@ async function run() {
680
709
 
681
710
  // 1. Key setup (interactive if first time)
682
711
  if (!relayKey) {
712
+ if (process.env.CODEPOCKET_DAEMON) {
713
+ console.error('[codepocket] ✗ 无 Key,请在前台运行 codepocket 配置')
714
+ process.exit(1)
715
+ }
683
716
  console.log('[codepocket] 首次使用,请输入 Key')
684
717
  console.log(' 在 CodePocket 小程序中获取 Key →')
685
718
  relayKey = await new Promise((resolve) => {
@@ -705,20 +738,45 @@ async function run() {
705
738
  console.log('[codepocket] ✓ 配置已保存')
706
739
  }
707
740
 
708
- // 2. Start hub (quiet, wait for ready)
709
- if (!noHub) {
710
- await startHub(hubBin)
741
+ // 2. Daemon mode: full hub + relay setup
742
+ if (process.env.CODEPOCKET_DAEMON) {
743
+ if (!noHub) await startHub(hubBin)
744
+ connectInBackground()
745
+ console.log('[codepocket] ✓ 后台服务已就绪,等待小程序...')
746
+ return
711
747
  }
712
748
 
713
- // 3. Connect relay (auth + MCP + SSE + relay)
714
- connectInBackground()
715
-
716
- // 4. Daemon mode — just wait
717
- if (process.env.CODEPOCKET_DAEMON || CONNECT_ONLY) {
749
+ // 3. Connect-only mode (same as old daemon for manual use)
750
+ if (CONNECT_ONLY) {
751
+ if (!noHub) await startHub(hubBin)
752
+ connectInBackground()
718
753
  console.log('[codepocket] ✓ 已连接,等待小程序...')
719
754
  return
720
755
  }
721
756
 
757
+ // 4. Foreground: ensure daemon running, then just spawn Claude
758
+ if (!noHub) {
759
+ const daemonPid = checkRunning()
760
+ if (!daemonPid) {
761
+ // Auto-start daemon
762
+ fs.mkdirSync(LOG_DIR, { recursive: true })
763
+ const logFile = getLogFile()
764
+ const out = fs.openSync(logFile, 'a')
765
+ const daemonChild = spawn(process.argv[0], [process.argv[1], 'start'], {
766
+ stdio: ['ignore', out, out],
767
+ detached: true,
768
+ env: { ...process.env, CODEPOCKET_DAEMON: '1' },
769
+ })
770
+ daemonChild.unref()
771
+ fs.writeFileSync(PID_FILE, String(daemonChild.pid))
772
+ console.log(`[codepocket] ✓ 后台服务已启动 (PID ${daemonChild.pid})`)
773
+ } else {
774
+ console.log(`[codepocket] ✓ 后台服务运行中 (PID ${daemonPid})`)
775
+ }
776
+ // Wait for hub to be ready (started by daemon)
777
+ await waitForHub()
778
+ }
779
+
722
780
  // 5. Launch Claude Code (foreground)
723
781
  if (!hubBin) {
724
782
  console.error('[codepocket] ✗ 找不到 Hub 二进制')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tina_summer/codepocket",
3
- "version": "2.2.3",
3
+ "version": "2.2.4",
4
4
  "description": "CodePocket — 基于ClaudeCode的微信小程序工作台",
5
5
  "bin": {
6
6
  "codepocket": "bin/codepocket.js",