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';
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) {