chatccc 0.2.162 → 0.2.163

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/package.json +1 -1
  2. package/src/orchestrator.ts +46 -33
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatccc",
3
- "version": "0.2.162",
3
+ "version": "0.2.163",
4
4
  "description": "Feishu bot bridge for Claude Code",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -7,8 +7,9 @@
7
7
 
8
8
  import { execSync, spawn } from "node:child_process";
9
9
  import { readdir, stat } from "node:fs/promises";
10
- import { existsSync, readFileSync, writeFileSync } from "node:fs";
11
- import { resolve, dirname } from "node:path";
10
+ import { appendFileSync, existsSync, readFileSync, writeFileSync } from "node:fs";
11
+ import { join, resolve, dirname } from "node:path";
12
+ import { homedir } from "node:os";
12
13
 
13
14
  import { makeTraceId, logTrace } from "./trace.ts";
14
15
  import { appendStartupTrace } from "./shared.ts";
@@ -169,33 +170,46 @@ function isRunningFromGlobalNpm(): boolean {
169
170
  }
170
171
  }
171
172
 
172
- /** 更新并重启:spawn detached Node.js 中间脚本,轮询父进程退出后执行 npm update + chatccc */
173
- function scheduleUpdateRestart(): void {
174
- const parentPid = process.pid;
175
- const watcherScript = `
176
- var spawn = require('child_process').spawn;
177
- var execSync = require('child_process').execSync;
178
- var parentPid = ${parentPid};
179
- (function wait() {
180
- try { process.kill(parentPid, 0); setTimeout(wait, 300); } catch (_) {
181
- try {
182
- var npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm';
183
- execSync(npmCmd + ' update -g chatccc', { stdio: 'inherit' });
184
- } catch (e) {
185
- console.error('npm update failed:', e.message);
186
- }
187
- var c = spawn('chatccc', [], { detached: true, stdio: 'ignore', shell: true });
188
- c.unref();
189
- process.exit(0);
190
- }
191
- })();
192
- `;
193
- const child = spawn("node", ["-e", watcherScript], {
194
- detached: true,
195
- stdio: "ignore",
196
- shell: true,
197
- });
198
- child.unref();
173
+ const UPDATEG_LOG = join(homedir(), ".chatccc", "logs", "updateg-watcher.log");
174
+
175
+ function updLog(msg: string): void {
176
+ const ts = new Date().toISOString();
177
+ try { appendFileSync(UPDATEG_LOG, `${ts} [UPG-SYNC] ${msg}\n`, "utf-8"); } catch {}
178
+ }
179
+
180
+ /** 同步更新 npm 全局包并重启(父进程存活等待,避免 systemd KillMode=control-group 杀 watcher) */
181
+ function syncUpdateAndRestart(): void {
182
+ updLog(`sync update start, pid=${process.pid}`);
183
+
184
+ // 1. npm update
185
+ const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
186
+ try {
187
+ const out = execSync(`${npmCmd} update -g chatccc`, { encoding: "utf8", timeout: 120000, windowsHide: true });
188
+ updLog(`npm update OK: ${out.slice(0, 200)}`);
189
+ } catch (e) {
190
+ const errMsg = (e as Error).message;
191
+ updLog(`npm update failed: ${errMsg}, falling back to npm install`);
192
+ try {
193
+ const out2 = execSync(`${npmCmd} install -g chatccc@latest`, { encoding: "utf8", timeout: 120000, windowsHide: true });
194
+ updLog(`npm install fallback OK: ${out2.slice(0, 200)}`);
195
+ } catch (e2) {
196
+ updLog(`npm install fallback also failed: ${(e2 as Error).message}`);
197
+ }
198
+ }
199
+
200
+ // 2. spawn new chatccc
201
+ const npmPrefix = process.env.NPM_PREFIX || "";
202
+ const binName = process.platform === "win32" ? "chatccc.cmd" : "chatccc";
203
+ const binPath = npmPrefix ? join(npmPrefix, binName) : "chatccc";
204
+ try {
205
+ const child = spawn(binPath, [], { detached: true, stdio: "ignore", shell: true });
206
+ child.unref();
207
+ updLog(`spawn new chatccc OK, childPid=${child.pid}, bin=${binPath}`);
208
+ } catch (e) {
209
+ updLog(`spawn new chatccc failed: ${(e as Error).message}`);
210
+ }
211
+
212
+ updLog("sync update done, parent exiting in 2s");
199
213
  }
200
214
 
201
215
  // ---------------------------------------------------------------------------
@@ -260,10 +274,9 @@ export async function handleCommand(
260
274
  }
261
275
  await platform.sendText(chatId, "正在更新并重启,请稍候...").catch(() => {});
262
276
  logTrace(tid, "DONE", { outcome: "updateg" });
263
- appendStartupTrace("updateg: spawn watcher begin", { fromPid: process.pid });
264
- scheduleUpdateRestart();
265
- // 短暂延迟确保消息发送后再退出
266
- setTimeout(() => process.exit(0), 500);
277
+ appendStartupTrace("updateg: sync update begin", { fromPid: process.pid });
278
+ syncUpdateAndRestart();
279
+ setTimeout(() => process.exit(0), 2000);
267
280
  return;
268
281
  }
269
282