@tina_summer/codepocket 2.1.3 → 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 +119 -15
  2. package/package.json +1 -1
package/bin/codepocket.js CHANGED
@@ -72,6 +72,65 @@ if (args[0] === 'test') {
72
72
  return
73
73
  }
74
74
 
75
+ // ── Background mode helpers ──
76
+
77
+ const PID_FILE = path.join(CONFIG_DIR, 'codepocket.pid')
78
+ const LOG_DIR = path.join(CONFIG_DIR, 'logs')
79
+
80
+ function getLogFile() {
81
+ const d = new Date()
82
+ const ts = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
83
+ return path.join(LOG_DIR, `${ts}.log`)
84
+ }
85
+
86
+ function checkRunning() {
87
+ try {
88
+ const pid = parseInt(fs.readFileSync(PID_FILE, 'utf-8').trim())
89
+ if (pid && process.kill(pid, 0)) return pid
90
+ } catch {}
91
+ try { fs.unlinkSync(PID_FILE) } catch {}
92
+ return 0
93
+ }
94
+
95
+ // 'stop' subcommand
96
+ if (args[0] === 'stop') {
97
+ const pid = checkRunning()
98
+ if (!pid) {
99
+ console.log('[codepocket] Not running')
100
+ process.exit(0)
101
+ }
102
+ try { process.kill(pid, 'SIGTERM') } catch {}
103
+ try { fs.unlinkSync(PID_FILE) } catch {}
104
+ console.log(`[codepocket] Stopped (PID ${pid})`)
105
+ process.exit(0)
106
+ }
107
+
108
+ // 'logs' subcommand
109
+ if (args[0] === 'logs') {
110
+ const logFile = getLogFile()
111
+ if (!fs.existsSync(logFile)) {
112
+ console.log('[codepocket] No log file for today')
113
+ process.exit(1)
114
+ }
115
+ const lines = parseInt(args[1]) || 50
116
+ try {
117
+ const { execSync } = require('child_process')
118
+ execSync(`tail -n ${lines} -f "${logFile}"`, { stdio: 'inherit' })
119
+ } catch {}
120
+ process.exit(0)
121
+ }
122
+
123
+ // 'status' subcommand
124
+ if (args[0] === 'status') {
125
+ const pid = checkRunning()
126
+ if (pid) {
127
+ console.log(`[codepocket] Running (PID ${pid})`)
128
+ } else {
129
+ console.log('[codepocket] Not running')
130
+ }
131
+ process.exit(0)
132
+ }
133
+
75
134
  // Determine mode
76
135
  const CONNECT_ONLY = ['connect', 'c'].includes(args[0])
77
136
  const filteredArgs = CONNECT_ONLY ? args.slice(1) : args
@@ -104,11 +163,60 @@ if (relayKey) {
104
163
  }
105
164
  if (!relayUrl) relayUrl = saved.relayUrl || ''
106
165
 
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 查看日志`)
191
+ process.exit(0)
192
+ }
193
+ }
194
+
107
195
  // ── State ──
108
196
 
109
197
  let jwt = ''
110
198
  let relayWs = null
111
199
  let relayPingInterval = null
200
+ let hubBin = findHubBin()
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
+ }
112
220
 
113
221
  // ── Hub ──
114
222
 
@@ -227,14 +335,14 @@ function connectRelay() {
227
335
  fs.writeFileSync(SESSION_FILE, JSON.stringify({ sessionToken: msg.sessionToken, hubUrl, relayUrl }))
228
336
  } catch {}
229
337
  if (msg.reconnected) {
230
- console.log('[codepocket] ✓ 已重新连接')
338
+ log('[codepocket] ✓ 已重新连接')
231
339
  } else {
232
- console.log('[codepocket] ✓ 已连接中继')
340
+ log('[codepocket] ✓ 已连接中继')
233
341
  }
234
342
  break
235
343
  }
236
344
  case 'client-connected': {
237
- console.log('[codepocket] ✓ 小程序已连接')
345
+ log('[codepocket] ✓ 小程序已连接')
238
346
  break
239
347
  }
240
348
  case 'rest-request': {
@@ -246,7 +354,7 @@ function connectRelay() {
246
354
  break
247
355
  }
248
356
  case 'register-error': {
249
- console.error(`[codepocket] ✗ ${msg.error}`)
357
+ log(`[codepocket] ✗ ${msg.error}`)
250
358
  break
251
359
  }
252
360
  case 'phone-action-response': {
@@ -261,10 +369,6 @@ function connectRelay() {
261
369
  handleListDir(msg.requestId, msg.path)
262
370
  break
263
371
  }
264
- case 'list-dir': {
265
- handleListDir(msg.requestId, msg.path)
266
- break
267
- }
268
372
  }
269
373
  })
270
374
 
@@ -332,7 +436,7 @@ async function handleSpawnSession(requestId, directory) {
332
436
  // Expand ~ to home directory
333
437
  directory = directory.replace(/^~/, os.homedir())
334
438
 
335
- console.log(`[codepocket] Spawning session in: ${directory}`)
439
+ log(`[codepocket] Spawning session in: ${directory}`)
336
440
 
337
441
  try {
338
442
  const child = spawn(hubBin, ['--path', directory], {
@@ -363,7 +467,7 @@ async function handleSpawnSession(requestId, directory) {
363
467
  }
364
468
 
365
469
  if (sessionId) {
366
- console.log(`[codepocket] Session created: ${sessionId}`)
470
+ log(`[codepocket] Session created: ${sessionId}`)
367
471
  sendToRelay({ type: 'spawn-session-result', requestId, success: true, sessionId })
368
472
  } else {
369
473
  // Timeout but process started — might still be initializing
@@ -413,7 +517,6 @@ async function connectInBackground() {
413
517
  // ── Main ──
414
518
 
415
519
  async function run() {
416
- const hubBin = findHubBin()
417
520
 
418
521
  // 1. Key setup (interactive if first time)
419
522
  if (!relayKey) {
@@ -447,15 +550,16 @@ async function run() {
447
550
  await startHub(hubBin)
448
551
  }
449
552
 
450
- // 3. Connect relay in background (auth + MCP + SSE + relay)
553
+ // 3. Connect relay (auth + MCP + SSE + relay)
451
554
  connectInBackground()
452
555
 
453
- // 4. Launch Claude Code (foreground) — unless connect-only mode
454
- if (CONNECT_ONLY) {
455
- console.log('[codepocket] 已连接,等待小程序...')
556
+ // 4. Daemon mode — just wait
557
+ if (process.env.CODEPOCKET_DAEMON || CONNECT_ONLY) {
558
+ console.log('[codepocket] ✓ 已连接,等待小程序...')
456
559
  return
457
560
  }
458
561
 
562
+ // 5. Launch Claude Code (foreground)
459
563
  if (!hubBin) {
460
564
  console.error('[codepocket] ✗ 找不到 Hub 二进制')
461
565
  process.exit(1)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tina_summer/codepocket",
3
- "version": "2.1.3",
3
+ "version": "2.2.1",
4
4
  "description": "CodePocket — 你的全能 AI 随身控制台",
5
5
  "bin": {
6
6
  "codepocket": "bin/codepocket.js",