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) <noreply@anthropic.com>
This commit is contained in:
operator
2026-04-19 05:32:11 +00:00
parent ce61834738
commit 1881ce6168

View File

@@ -198,46 +198,41 @@ export default {
const agentId = process.env.AGENT_ID || 'unknown'; const agentId = process.env.AGENT_ID || 'unknown';
try { 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 gatewayUrl = 'ws://127.0.0.1:18789';
const ws = await import('ws'); const WS = (globalThis as any).WebSocket ?? (await import('node:http')).default;
const WebSocket = ws.default || ws.WebSocket || ws;
const result = await new Promise<{ sessionId?: string; error?: string }>((resolve, reject) => { const result = await new Promise<{ sessionId?: string; error?: string }>((resolve, reject) => {
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
client.close(); try { client.close(); } catch {}
reject(new Error('Gateway connection timeout')); reject(new Error('Gateway connection timeout'));
}, 15000); }, 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); clearTimeout(timeout);
reject(err); reject(err?.error || err);
}); };
client.on('open', () => { client.onopen = () => {
// Send hello
client.send(JSON.stringify({ client.send(JSON.stringify({
jsonrpc: '2.0', jsonrpc: '2.0',
method: 'hello', method: 'hello',
params: { params: { clientName: 'harbor-forge-calendar', mode: 'backend' },
clientName: 'harbor-forge-calendar',
mode: 'backend',
},
id: 1, id: 1,
})); }));
}); };
let helloAcked = false; let helloAcked = false;
client.on('message', (data: Buffer | string) => { client.onmessage = (ev: any) => {
try { 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) { if (!helloAcked && msg.id === 1) {
helloAcked = true; helloAcked = true;
// Send agent turn request
client.send(JSON.stringify({ client.send(JSON.stringify({
jsonrpc: '2.0', jsonrpc: '2.0',
method: 'agent', method: 'agent',
@@ -253,7 +248,7 @@ export default {
if (msg.id === 2) { if (msg.id === 2) {
clearTimeout(timeout); clearTimeout(timeout);
client.close(); try { client.close(); } catch {}
if (msg.error) { if (msg.error) {
resolve({ error: msg.error.message || JSON.stringify(msg.error) }); resolve({ error: msg.error.message || JSON.stringify(msg.error) });
} else { } else {
@@ -263,7 +258,7 @@ export default {
} catch { } catch {
// ignore parse errors // ignore parse errors
} }
}); };
}); });
if (result.error) { if (result.error) {