mocode-ai 1.0.7 → 1.0.9

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.
@@ -84,13 +84,47 @@ export async function checkVersion() {
84
84
  const hasUpdate = latest ? compareSemver(latest, current) > 0 : false;
85
85
  return { current, latest, hasUpdate };
86
86
  }
87
- /** 后台 spawn `npm i -g mocode-ai@latest`。 */
87
+ /** 生成一个升级脚本,等当前 mocode 进程退出后再执行 `npm i -g`,避免 npm 无法覆盖正在运行的包文件。 */
88
+ function writeUpgradeScript(logPath, parentPid) {
89
+ const scriptDir = path.join(os.homedir(), '.mocode');
90
+ fs.mkdirSync(scriptDir, { recursive: true });
91
+ if (process.platform === 'win32') {
92
+ const scriptPath = path.join(scriptDir, 'upgrade.cmd');
93
+ const script = `@echo off\r\n` +
94
+ `echo [upgrade] waiting for mocode pid ${parentPid} to exit... >> "${logPath}"\r\n` +
95
+ `:wait\r\n` +
96
+ `tasklist /FI "PID eq ${parentPid}" 2>NUL | findstr "${parentPid}" >NUL\r\n` +
97
+ `if %ERRORLEVEL% == 0 (\r\n` +
98
+ ` timeout /T 1 /NOBREAK >NUL\r\n` +
99
+ ` goto wait\r\n` +
100
+ `)\r\n` +
101
+ `echo [upgrade] mocode exited, running npm install... >> "${logPath}"\r\n` +
102
+ `npm install -g ${PKG_NAME}@latest >> "${logPath}" 2>&1\r\n` +
103
+ `echo [upgrade] done at %DATE% %TIME% >> "${logPath}"\r\n`;
104
+ fs.writeFileSync(scriptPath, script);
105
+ return { scriptPath, shell: 'cmd', args: ['/c', scriptPath] };
106
+ }
107
+ const scriptPath = path.join(scriptDir, 'upgrade.sh');
108
+ const script = `#!/usr/bin/env sh\n` +
109
+ `set -e\n` +
110
+ `echo "[upgrade] waiting for mocode pid ${parentPid} to exit..." >> "${logPath}"\n` +
111
+ `while kill -0 ${parentPid} 2>/dev/null; do sleep 1; done\n` +
112
+ `echo "[upgrade] mocode exited, running npm install..." >> "${logPath}"\n` +
113
+ `npm install -g ${PKG_NAME}@latest >> "${logPath}" 2>&1\n` +
114
+ `echo "[upgrade] done at $(date -Iseconds)" >> "${logPath}"\n`;
115
+ fs.writeFileSync(scriptPath, script, { mode: 0o755 });
116
+ return { scriptPath, shell: 'sh', args: [scriptPath] };
117
+ }
118
+ /** 后台 spawn 升级脚本(当前进程退出后才真正跑 npm install)。 */
88
119
  export function spawnUpgrade() {
89
120
  if (isDevRun() || process.env.MOCODE_NO_SPAWN)
90
121
  return;
91
122
  try {
92
- const child = spawn('npm', ['install', '-g', `${PKG_NAME}@latest`], {
93
- shell: true,
123
+ const logDir = path.join(os.homedir(), '.mocode');
124
+ fs.mkdirSync(logDir, { recursive: true });
125
+ const logPath = path.join(logDir, 'upgrade.log');
126
+ const { shell, args } = writeUpgradeScript(logPath, process.pid);
127
+ const child = spawn(shell, args, {
94
128
  detached: true,
95
129
  stdio: 'ignore',
96
130
  windowsHide: true,
@@ -241,7 +241,7 @@ const zhCN = {
241
241
  'upgrade.noUpdate': '已是最新版本 v{version}',
242
242
  'upgrade.hasUpdate': '发现新版本:当前 v{current} → 最新 v{latest}',
243
243
  'upgrade.upgradeStarted': '已启动后台升级 → {version}',
244
- 'upgrade.upgradeHint': 'npm install -g mocode-ai@latest 正在后台运行,完成后下次启动 mocode 即生效。',
244
+ 'upgrade.upgradeHint': '后台升级进程已启动。当前 mocode 进程退出后才会执行 npm install -g mocode-ai@latest,完成后下次启动即生效。日志:~/.mocode/upgrade.log',
245
245
  'upgrade.devSkip': '当前为开发运行(tsx),已跳过真实 npm install,避免误改全局包。',
246
246
  'upgrade.fetchFailed': '无法获取最新版本(请检查网络或 npm registry)。',
247
247
  'upgrade.pkgName': '包名:mocode-ai',
@@ -492,7 +492,7 @@ const en = {
492
492
  'upgrade.noUpdate': 'Already up to date: v{version}',
493
493
  'upgrade.hasUpdate': 'New version available: v{current} → v{latest}',
494
494
  'upgrade.upgradeStarted': 'Background upgrade started → {version}',
495
- 'upgrade.upgradeHint': 'npm install -g mocode-ai@latest is running in the background; restart mocode to use it.',
495
+ 'upgrade.upgradeHint': 'Background upgrade process started. npm install -g mocode-ai@latest will run after you exit mocode. Log: ~/.mocode/upgrade.log',
496
496
  'upgrade.devSkip': 'Running in dev mode (tsx); skipped real npm install to avoid touching the global package.',
497
497
  'upgrade.fetchFailed': 'Unable to fetch the latest version (check network or npm registry).',
498
498
  'upgrade.pkgName': 'Package: mocode-ai',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocode-ai",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "终端编码 agent:LLM + tool-call 循环 + 流式输出(含思考)+ 16 个工具,接任意 OpenAI 兼容后端。",
5
5
  "type": "module",
6
6
  "bin": {