@tina_summer/codepocket 2.1.2 → 2.2.0
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 +91 -31
- package/package.json +1 -1
package/bin/codepocket.js
CHANGED
|
@@ -72,12 +72,72 @@ 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
|
|
78
137
|
|
|
79
138
|
let hubUrl = process.env.CPKT_HUB_URL || process.env.HAPI_HUB_URL || 'http://127.0.0.1:3006'
|
|
80
|
-
|
|
139
|
+
const DEFAULT_RELAY_URL = 'wss://bkstory.cn/ws'
|
|
140
|
+
let relayUrl = process.env.RELAY_WS_URL || process.env.RELAY_URL || DEFAULT_RELAY_URL
|
|
81
141
|
let relayKey = process.env.RELAY_KEY || ''
|
|
82
142
|
let noHub = false
|
|
83
143
|
|
|
@@ -103,11 +163,37 @@ if (relayKey) {
|
|
|
103
163
|
}
|
|
104
164
|
if (!relayUrl) relayUrl = saved.relayUrl || ''
|
|
105
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})`)
|
|
171
|
+
process.exit(0)
|
|
172
|
+
}
|
|
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
|
+
}
|
|
190
|
+
|
|
106
191
|
// ── State ──
|
|
107
192
|
|
|
108
193
|
let jwt = ''
|
|
109
194
|
let relayWs = null
|
|
110
195
|
let relayPingInterval = null
|
|
196
|
+
let hubBin = findHubBin()
|
|
111
197
|
|
|
112
198
|
// ── Hub ──
|
|
113
199
|
|
|
@@ -260,10 +346,6 @@ function connectRelay() {
|
|
|
260
346
|
handleListDir(msg.requestId, msg.path)
|
|
261
347
|
break
|
|
262
348
|
}
|
|
263
|
-
case 'list-dir': {
|
|
264
|
-
handleListDir(msg.requestId, msg.path)
|
|
265
|
-
break
|
|
266
|
-
}
|
|
267
349
|
}
|
|
268
350
|
})
|
|
269
351
|
|
|
@@ -412,7 +494,6 @@ async function connectInBackground() {
|
|
|
412
494
|
// ── Main ──
|
|
413
495
|
|
|
414
496
|
async function run() {
|
|
415
|
-
const hubBin = findHubBin()
|
|
416
497
|
|
|
417
498
|
// 1. Key setup (interactive if first time)
|
|
418
499
|
if (!relayKey) {
|
|
@@ -431,15 +512,9 @@ async function run() {
|
|
|
431
512
|
}
|
|
432
513
|
}
|
|
433
514
|
|
|
434
|
-
// 1.5 Relay URL (
|
|
515
|
+
// 1.5 Relay URL (default: wss://bkstory.cn/ws)
|
|
435
516
|
if (!relayUrl) {
|
|
436
|
-
relayUrl =
|
|
437
|
-
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
438
|
-
rl.question(' Relay URL (如 wss://example.com/ws): ', (answer) => {
|
|
439
|
-
rl.close()
|
|
440
|
-
resolve(answer.trim())
|
|
441
|
-
})
|
|
442
|
-
})
|
|
517
|
+
relayUrl = DEFAULT_RELAY_URL
|
|
443
518
|
}
|
|
444
519
|
|
|
445
520
|
if (relayKey) {
|
|
@@ -452,25 +527,10 @@ async function run() {
|
|
|
452
527
|
await startHub(hubBin)
|
|
453
528
|
}
|
|
454
529
|
|
|
455
|
-
// 3. Connect relay
|
|
530
|
+
// 3. Connect relay (auth + MCP + SSE + relay)
|
|
456
531
|
connectInBackground()
|
|
457
532
|
|
|
458
|
-
|
|
459
|
-
if (CONNECT_ONLY) {
|
|
460
|
-
console.log('[codepocket] 已连接,等待小程序...')
|
|
461
|
-
return
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
if (!hubBin) {
|
|
465
|
-
console.error('[codepocket] ✗ 找不到 Hub 二进制')
|
|
466
|
-
process.exit(1)
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
const child = spawn(hubBin, passThroughArgs, {
|
|
470
|
-
stdio: 'inherit',
|
|
471
|
-
env: { ...process.env, ...HOME_ENV },
|
|
472
|
-
})
|
|
473
|
-
child.on('exit', (code) => process.exit(code || 0))
|
|
533
|
+
console.log('[codepocket] ✓ 已连接,等待小程序...')
|
|
474
534
|
}
|
|
475
535
|
|
|
476
536
|
run().catch((err) => {
|