From 1881ce6168300134f579a28641c864d96f5e56b4 Mon Sep 17 00:00:00 2001 From: operator Date: Sun, 19 Apr 2026 05:32:11 +0000 Subject: [PATCH] fix: use globalThis.WebSocket instead of ws module Node 22+ has WebSocket built-in on globalThis. Co-Authored-By: Claude Opus 4.6 (1M context) --- plugin/index.ts | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/plugin/index.ts b/plugin/index.ts index 42ae7f3..7300abb 100644 --- a/plugin/index.ts +++ b/plugin/index.ts @@ -198,46 +198,41 @@ export default { const agentId = process.env.AGENT_ID || 'unknown'; try { - // Connect to gateway via WebSocket and trigger an agent turn + // Connect to gateway via WebSocket and trigger an agent turn. + // Uses the same gateway RPC that sessions_spawn and cron use internally. const gatewayUrl = 'ws://127.0.0.1:18789'; - const ws = await import('ws'); - const WebSocket = ws.default || ws.WebSocket || ws; + const WS = (globalThis as any).WebSocket ?? (await import('node:http')).default; const result = await new Promise<{ sessionId?: string; error?: string }>((resolve, reject) => { const timeout = setTimeout(() => { - client.close(); + try { client.close(); } catch {} reject(new Error('Gateway connection timeout')); }, 15000); - const client = new WebSocket(gatewayUrl); + const client = new (globalThis as any).WebSocket(gatewayUrl); - client.on('error', (err: Error) => { + client.onerror = (err: any) => { clearTimeout(timeout); - reject(err); - }); + reject(err?.error || err); + }; - client.on('open', () => { - // Send hello + client.onopen = () => { client.send(JSON.stringify({ jsonrpc: '2.0', method: 'hello', - params: { - clientName: 'harbor-forge-calendar', - mode: 'backend', - }, + params: { clientName: 'harbor-forge-calendar', mode: 'backend' }, id: 1, })); - }); + }; let helloAcked = false; - client.on('message', (data: Buffer | string) => { + client.onmessage = (ev: any) => { try { - const msg = JSON.parse(data.toString()); + const msg = JSON.parse(typeof ev.data === 'string' ? ev.data : ev.data.toString()); if (!helloAcked && msg.id === 1) { helloAcked = true; - // Send agent turn request client.send(JSON.stringify({ jsonrpc: '2.0', method: 'agent', @@ -253,7 +248,7 @@ export default { if (msg.id === 2) { clearTimeout(timeout); - client.close(); + try { client.close(); } catch {} if (msg.error) { resolve({ error: msg.error.message || JSON.stringify(msg.error) }); } else { @@ -263,7 +258,7 @@ export default { } catch { // ignore parse errors } - }); + }; }); if (result.error) {