@yeaft/webchat-agent 0.0.3 → 0.0.5
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/connection.js +43 -0
- package/index.js +1 -0
- package/package.json +1 -1
package/connection.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
+
import { execSync } from 'child_process';
|
|
2
3
|
import ctx from './context.js';
|
|
3
4
|
import { encrypt, decrypt, isEncrypted, decodeKey } from './encryption.js';
|
|
4
5
|
import { handleTerminalCreate, handleTerminalInput, handleTerminalResize, handleTerminalClose } from './terminal.js';
|
|
@@ -264,6 +265,48 @@ async function handleMessage(msg) {
|
|
|
264
265
|
process.exit(1);
|
|
265
266
|
}, 500);
|
|
266
267
|
break;
|
|
268
|
+
|
|
269
|
+
case 'upgrade_agent':
|
|
270
|
+
console.log('[Agent] Upgrade requested, checking for updates...');
|
|
271
|
+
try {
|
|
272
|
+
const pkgName = ctx.pkgName || '@yeaft/webchat-agent';
|
|
273
|
+
// Check latest version before installing
|
|
274
|
+
const latestVersion = execSync(`npm view ${pkgName} version`, { stdio: 'pipe' }).toString().trim();
|
|
275
|
+
if (latestVersion === ctx.agentVersion) {
|
|
276
|
+
console.log(`[Agent] Already at latest version (${ctx.agentVersion}), skipping upgrade.`);
|
|
277
|
+
sendToServer({ type: 'upgrade_agent_ack', success: true, alreadyLatest: true, version: ctx.agentVersion });
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
console.log(`[Agent] Upgrading from ${ctx.agentVersion} to ${latestVersion}...`);
|
|
281
|
+
execSync(`npm install -g ${pkgName}@latest`, { stdio: 'pipe' });
|
|
282
|
+
console.log('[Agent] Upgrade successful, restarting...');
|
|
283
|
+
sendToServer({ type: 'upgrade_agent_ack', success: true, version: latestVersion });
|
|
284
|
+
// Restart after upgrade (same as restart_agent)
|
|
285
|
+
setTimeout(() => {
|
|
286
|
+
for (const [, term] of ctx.terminals) {
|
|
287
|
+
if (term.pty) { try { term.pty.kill(); } catch {} }
|
|
288
|
+
if (term.timer) clearTimeout(term.timer);
|
|
289
|
+
}
|
|
290
|
+
ctx.terminals.clear();
|
|
291
|
+
for (const [, state] of ctx.conversations) {
|
|
292
|
+
if (state.abortController) state.abortController.abort();
|
|
293
|
+
if (state.inputStream) state.inputStream.done();
|
|
294
|
+
}
|
|
295
|
+
ctx.conversations.clear();
|
|
296
|
+
stopAgentHeartbeat();
|
|
297
|
+
if (ctx.ws) {
|
|
298
|
+
ctx.ws.removeAllListeners('close');
|
|
299
|
+
ctx.ws.close();
|
|
300
|
+
}
|
|
301
|
+
clearTimeout(ctx.reconnectTimer);
|
|
302
|
+
console.log('[Agent] Cleanup done, exiting for auto-restart...');
|
|
303
|
+
process.exit(1);
|
|
304
|
+
}, 500);
|
|
305
|
+
} catch (e) {
|
|
306
|
+
console.error('[Agent] Upgrade failed:', e.message);
|
|
307
|
+
sendToServer({ type: 'upgrade_agent_ack', success: false, error: e.message });
|
|
308
|
+
}
|
|
309
|
+
break;
|
|
267
310
|
}
|
|
268
311
|
}
|
|
269
312
|
|
package/index.js
CHANGED
|
@@ -16,6 +16,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
16
16
|
// Load package version
|
|
17
17
|
const pkg = JSON.parse(readFileSync(join(__dirname, 'package.json'), 'utf-8'));
|
|
18
18
|
ctx.agentVersion = pkg.version;
|
|
19
|
+
ctx.pkgName = pkg.name;
|
|
19
20
|
|
|
20
21
|
// 配置文件路径(向后兼容:先查当前目录 .claude-agent.json)
|
|
21
22
|
const LOCAL_CONFIG_FILE = join(process.cwd(), '.claude-agent.json');
|