@tina_summer/codepocket 2.2.0 → 2.2.1

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 +67 -28
  2. package/package.json +1 -1
package/bin/codepocket.js CHANGED
@@ -163,29 +163,33 @@ if (relayKey) {
163
163
  }
164
164
  if (!relayUrl) relayUrl = saved.relayUrl || ''
165
165
 
166
- // ── Auto-daemonize: if not already a daemon, fork to background ──
167
- if (!process.env.CODEPOCKET_DAEMON && relayKey) {
168
- const pid = checkRunning()
169
- if (pid) {
170
- console.log(`[codepocket] Already running (PID ${pid})`)
166
+ // ── 'start' subcommand — pure background daemon (no Claude Code) ──
167
+ if (args[0] === 'start') {
168
+ if (process.env.CODEPOCKET_DAEMON) {
169
+ // We ARE the daemon — fall through to run()
170
+ } else {
171
+ const pid = checkRunning()
172
+ if (pid) {
173
+ console.log(`[codepocket] Already running (PID ${pid})`)
174
+ process.exit(0)
175
+ }
176
+ fs.mkdirSync(LOG_DIR, { recursive: true })
177
+ const logFile = getLogFile()
178
+ const out = fs.openSync(logFile, 'a')
179
+ const daemonArgs = process.argv.slice(2).slice(1) // remove 'start'
180
+ const child = spawn(process.argv[0], [process.argv[1], 'start', ...daemonArgs], {
181
+ stdio: ['ignore', out, out],
182
+ detached: true,
183
+ env: { ...process.env, CODEPOCKET_DAEMON: '1' },
184
+ })
185
+ child.unref()
186
+ fs.writeFileSync(PID_FILE, String(child.pid))
187
+ console.log(`[codepocket] ✓ 已后台启动 (PID ${child.pid})`)
188
+ console.log(` 日志: ${logFile}`)
189
+ console.log(` codepocket stop 停止`)
190
+ console.log(` codepocket logs 查看日志`)
171
191
  process.exit(0)
172
192
  }
173
- fs.mkdirSync(LOG_DIR, { recursive: true })
174
- const logFile = getLogFile()
175
- const out = fs.openSync(logFile, 'a')
176
- const daemonArgs = process.argv.slice(2).filter(a => a !== '--key' && !a.startsWith('--key='))
177
- const child = spawn(process.argv[0], [process.argv[1], ...daemonArgs], {
178
- stdio: ['ignore', out, out],
179
- detached: true,
180
- env: { ...process.env, CODEPOCKET_DAEMON: '1' },
181
- })
182
- child.unref()
183
- fs.writeFileSync(PID_FILE, String(child.pid))
184
- console.log(`[codepocket] ✓ 已后台启动 (PID ${child.pid})`)
185
- console.log(` 日志: ${logFile}`)
186
- console.log(` codepocket stop 停止`)
187
- console.log(` codepocket logs 查看日志`)
188
- process.exit(0)
189
193
  }
190
194
 
191
195
  // ── State ──
@@ -195,6 +199,25 @@ let relayWs = null
195
199
  let relayPingInterval = null
196
200
  let hubBin = findHubBin()
197
201
 
202
+ // ── Logging: write to log file when running with Claude Code foreground ──
203
+
204
+ let _logStream = null
205
+ function log(msg) {
206
+ const ts = new Date().toISOString().slice(11, 19)
207
+ const line = `${ts} ${msg}\n`
208
+ if (process.env.CODEPOCKET_DAEMON || CONNECT_ONLY) {
209
+ // daemon/connect-only: write to stdout (redirected to log file by start)
210
+ process.stdout.write(line)
211
+ } else {
212
+ // foreground (Claude Code running): write to log file only
213
+ if (!_logStream) {
214
+ fs.mkdirSync(LOG_DIR, { recursive: true })
215
+ _logStream = fs.openSync(getLogFile(), 'a')
216
+ }
217
+ fs.writeSync(_logStream, line)
218
+ }
219
+ }
220
+
198
221
  // ── Hub ──
199
222
 
200
223
  function startHub(hubBin) {
@@ -312,14 +335,14 @@ function connectRelay() {
312
335
  fs.writeFileSync(SESSION_FILE, JSON.stringify({ sessionToken: msg.sessionToken, hubUrl, relayUrl }))
313
336
  } catch {}
314
337
  if (msg.reconnected) {
315
- console.log('[codepocket] ✓ 已重新连接')
338
+ log('[codepocket] ✓ 已重新连接')
316
339
  } else {
317
- console.log('[codepocket] ✓ 已连接中继')
340
+ log('[codepocket] ✓ 已连接中继')
318
341
  }
319
342
  break
320
343
  }
321
344
  case 'client-connected': {
322
- console.log('[codepocket] ✓ 小程序已连接')
345
+ log('[codepocket] ✓ 小程序已连接')
323
346
  break
324
347
  }
325
348
  case 'rest-request': {
@@ -331,7 +354,7 @@ function connectRelay() {
331
354
  break
332
355
  }
333
356
  case 'register-error': {
334
- console.error(`[codepocket] ✗ ${msg.error}`)
357
+ log(`[codepocket] ✗ ${msg.error}`)
335
358
  break
336
359
  }
337
360
  case 'phone-action-response': {
@@ -413,7 +436,7 @@ async function handleSpawnSession(requestId, directory) {
413
436
  // Expand ~ to home directory
414
437
  directory = directory.replace(/^~/, os.homedir())
415
438
 
416
- console.log(`[codepocket] Spawning session in: ${directory}`)
439
+ log(`[codepocket] Spawning session in: ${directory}`)
417
440
 
418
441
  try {
419
442
  const child = spawn(hubBin, ['--path', directory], {
@@ -444,7 +467,7 @@ async function handleSpawnSession(requestId, directory) {
444
467
  }
445
468
 
446
469
  if (sessionId) {
447
- console.log(`[codepocket] Session created: ${sessionId}`)
470
+ log(`[codepocket] Session created: ${sessionId}`)
448
471
  sendToRelay({ type: 'spawn-session-result', requestId, success: true, sessionId })
449
472
  } else {
450
473
  // Timeout but process started — might still be initializing
@@ -530,7 +553,23 @@ async function run() {
530
553
  // 3. Connect relay (auth + MCP + SSE + relay)
531
554
  connectInBackground()
532
555
 
533
- console.log('[codepocket] ✓ 已连接,等待小程序...')
556
+ // 4. Daemon mode — just wait
557
+ if (process.env.CODEPOCKET_DAEMON || CONNECT_ONLY) {
558
+ console.log('[codepocket] ✓ 已连接,等待小程序...')
559
+ return
560
+ }
561
+
562
+ // 5. Launch Claude Code (foreground)
563
+ if (!hubBin) {
564
+ console.error('[codepocket] ✗ 找不到 Hub 二进制')
565
+ process.exit(1)
566
+ }
567
+
568
+ const child = spawn(hubBin, passThroughArgs, {
569
+ stdio: 'inherit',
570
+ env: { ...process.env, ...HOME_ENV },
571
+ })
572
+ child.on('exit', (code) => process.exit(code || 0))
534
573
  }
535
574
 
536
575
  run().catch((err) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tina_summer/codepocket",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "CodePocket — 你的全能 AI 随身控制台",
5
5
  "bin": {
6
6
  "codepocket": "bin/codepocket.js",