-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebsocket.ts
More file actions
499 lines (448 loc) · 20.2 KB
/
Copy pathwebsocket.ts
File metadata and controls
499 lines (448 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/**
* @file websocket.ts
* @description WebSocket connection lifecycle management for the Agent Bridge.
*
* Responsibilities:
* - Establish and maintain the WebSocket connection to the CtrlNode.ai SaaS.
* - Perform the initial handshake (send agent list + version).
* - Detect and handle authentication failures (exit without retry).
* - Queue outgoing messages while disconnected and flush them on reconnect.
* - Send periodic heartbeats with per-agent status information.
* - Manage the config-poll timer that re-syncs discovered agents.
* - Provide `sendToSaas` as the single egress point for all other modules.
*/
import WebSocket from 'ws';
import fs from 'fs';
import {
SAAS_URL,
PAIRING_TOKEN,
BRIDGE_VERSION,
HEARTBEAT_MS,
RECONNECT_MS,
POLL_CONFIG_MS,
AGENT_IDLE_RESET_MS,
CONNECTION_TIMEOUT_MS,
BRIDGE_INCOMING_DUMP_PATH,
PROVIDERS,
} from './config.js';
import { discoveredAgents, agentStatuses, purgedAgentIds, buildAgentSummaries, syncAgentDiscovery } from './agentDiscovery.js';
import type { AgentInfo } from './types.js';
import { startWatcher, stopWatcher, processFileEvent } from './watcher.js';
import { handleMessage } from './messageHandlers.js';
import { logger } from './logger.js';
import { IProvider } from './providers/IProvider.js';
import { HermesProvider } from './providers/HermesProvider.js';
// ── Module-level state ────────────────────────────────────────────────────────
let ws: WebSocket | null = null;
let isConnected = false;
let activeProvider: IProvider | null = null;
/** Last-known provider health snapshot — used to send deltas on heartbeat. */
let lastProviderHealth: Record<string, boolean> = {};
let heartbeatTimer: ReturnType<typeof setInterval> | null = null;
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
let configPollTimer: ReturnType<typeof setInterval> | null = null;
let connectionAttemptTimer: ReturnType<typeof setTimeout> | null = null;
let incomingDumpWarned = false;
/** Set when an auth failure has been detected so the subsequent close event is ignored. */
let authFailed = false;
/** Outgoing messages queued while the WebSocket is not yet open. */
const pendingQueue: any[] = [];
const PENDING_QUEUE_MAX = 100;
/** Inactivity timers used to reset agent status back to "idle". */
const statusTimers: Record<string, ReturnType<typeof setTimeout>> = {};
// ── Egress ────────────────────────────────────────────────────────────────────
/**
* Sends a JSON payload to the SaaS over the active WebSocket connection.
* If the connection is not currently open the payload is queued and will be
* sent automatically once the connection is re-established.
*
* @param payload - Any JSON-serialisable object to send.
*/
export function sendToSaas(payload: any): void {
try {
const json = JSON.stringify(payload);
if (ws && isConnected && ws.readyState === WebSocket.OPEN) {
// Skip per-chunk text stream traces — too noisy
if (!(payload?.action === 'agent_stream' && payload?.event?.kind === 'text_chunk') &&
!(payload?.action === 'agent_activity')) {
logger.debug('outgoing', { payloadType: payload?.action || 'unknown', preview: json.slice(0, 512) });
}
ws.send(json);
} else {
pendingQueue.push(payload);
if (pendingQueue.length > PENDING_QUEUE_MAX) pendingQueue.shift();
logger.debug('queued_outgoing', { payloadType: payload?.action || 'unknown', queueLength: pendingQueue.length });
}
} catch (err: any) {
logger.error('sendToSaas_error', { error: err?.message });
}
}
/**
* Drains the pending message queue and sends all buffered messages
* now that the WebSocket connection is open.
*/
function flushPendingQueue(): void {
while (pendingQueue.length > 0 && ws?.readyState === WebSocket.OPEN) {
ws!.send(JSON.stringify(pendingQueue.shift()));
}
}
// ── Agent status ──────────────────────────────────────────────────────────────
/**
* Marks an agent as "running" and starts a 15-second inactivity timer.
* If the agent is already running, the existing timer is reset.
* When the timer fires the agent status reverts to "idle" and a heartbeat
* is sent so the SaaS UI reflects the change.
*
* @param agentId - ID of the agent that showed filesystem activity.
*/
export function setAgentRunning(agentId: string): void {
if (statusTimers[agentId]) clearTimeout(statusTimers[agentId]);
if (agentStatuses[agentId] !== 'running') {
agentStatuses[agentId] = 'running';
logger.info('agent_status', { agentId, status: 'running' });
sendHeartbeat();
}
statusTimers[agentId] = setTimeout(() => {
agentStatuses[agentId] = 'idle';
logger.info('agent_status', { agentId, status: 'idle' });
delete statusTimers[agentId];
sendHeartbeat();
}, AGENT_IDLE_RESET_MS);
}
// ── Provider health ───────────────────────────────────────────────────────────
/**
* Runs health checks on all sub-providers of a MultiProvider (or single provider)
* and sends a `provider_health` message with any changes since last check.
* On first call sends the full map (all entries, including healthy ones).
*/
async function checkAndSendProviderHealth(isFirstCheck: boolean): Promise<void> {
if (!activeProvider) return;
const mp = activeProvider as any;
if (typeof mp.checkAllProviders !== 'function') return;
try {
const health: Record<string, boolean> = await mp.checkAllProviders();
const delta: Record<string, boolean> = {};
let hasChanges = false;
for (const [name, available] of Object.entries(health)) {
if (isFirstCheck || lastProviderHealth[name] !== available) {
delta[name] = available;
hasChanges = true;
}
}
if (hasChanges) {
lastProviderHealth = { ...health };
const unhealthy = Object.entries(health).filter(([, v]) => !v).map(([k]) => k);
logger.info('provider_health_sent', {
delta: Object.keys(delta).join(',') || 'none',
unhealthy: unhealthy.join(',') || 'none',
});
sendToSaas({ action: 'provider_health', providerHealth: isFirstCheck ? health : delta });
}
} catch (err: any) {
logger.warn('provider_health_check_failed', { error: err?.message });
}
}
// ── Heartbeat ─────────────────────────────────────────────────────────────────
/**
* Sends a heartbeat message to the SaaS with the current list of agent IDs,
* an overall bridge status ("running" if any agent is active), and per-agent
* status details. Also re-checks provider health and sends a delta if anything changed.
*/
function sendHeartbeat(): void {
if (!isConnected) return;
const agents = Object.keys(discoveredAgents);
if (agents.length === 0) return;
sendToSaas({
action: 'heartbeat',
agents,
status: agents.some(id => agentStatuses[id] === 'running') ? 'running' : 'idle',
agentStatuses: agents.map(id => ({ id, status: agentStatuses[id] })),
timestamp: new Date().toISOString(),
});
void checkAndSendProviderHealth(false);
}
/**
* Starts the periodic heartbeat timer. Any previously running timer is
* stopped first to avoid duplicate intervals.
*/
function startHeartbeat(): void {
if (heartbeatTimer) clearInterval(heartbeatTimer);
heartbeatTimer = setInterval(sendHeartbeat, HEARTBEAT_MS);
}
/**
* Stops the periodic heartbeat timer.
*/
function stopHeartbeat(): void {
if (heartbeatTimer) { clearInterval(heartbeatTimer); heartbeatTimer = null; }
}
// ── Config poll ───────────────────────────────────────────────────────────────
/**
* Starts the periodic config-poll timer that re-reads openclaw.json and
* ctrlnode/agents-config.json to detect agent additions/removals.
* Safe to call multiple times — only one timer will run.
*/
function startConfigPoll(): void {
if (configPollTimer) return;
configPollTimer = setInterval(() => runSyncAgents(), POLL_CONFIG_MS);
}
// ── Agent sync ────────────────────────────────────────────────────────────────
/**
* Runs a full agent-discovery sync cycle and sends an `agent_update` message
* to the SaaS if the agent list has changed.
*
* This function is passed as a callback to handlers that need to trigger
* re-discovery (e.g. after a sync_config or update_agent_config message).
*/
export function runSyncAgents(): void {
if (!PROVIDERS.includes('openclaw') && activeProvider) {
activeProvider.discoverAgents().then((summaries) => {
for (const s of summaries) {
if (!discoveredAgents[s.id] && !purgedAgentIds.has(s.id)) {
const info: AgentInfo = { workspace: s.workspace, name: s.name, model: s.model, role: s.role, emoji: s.emoji, description: s.description, provider: s.provider };
discoveredAgents[s.id] = info;
agentStatuses[s.id] = 'idle';
}
}
// Include ALL discoveredAgents (SDK-discovered UUIDs + DB-registered slugs added via
// sync_cursor_agents) so the SaaS _agentIndex covers every registered BridgeAgentId.
const all = buildAgentSummaries();
if (all.length > 0) {
sendToSaas({ action: 'agent_update', version: BRIDGE_VERSION, agents: all });
sendHeartbeat();
}
}).catch((err) => {
logger.error('sync_agents_provider_error', { error: err?.message });
});
return;
}
// When OpenClaw is active: warm ownership map (discard results).
// Also merge Hermes profiles not yet in discoveredAgents so they appear
// as unregistered agents in the UI.
if (activeProvider) {
activeProvider.discoverAgents().catch((err) => {
logger.warn('sync_agents.ownership_warm_failed', { error: err?.message });
});
}
new HermesProvider().discoverAgents().then((summaries) => {
let changed = false;
for (const s of summaries) {
if (!discoveredAgents[s.id] && !purgedAgentIds.has(s.id)) {
discoveredAgents[s.id] = { workspace: s.workspace, name: s.name, model: s.model, role: s.role, emoji: s.emoji, description: s.description, provider: s.provider, fromFilesystem: true };
agentStatuses[s.id] = 'idle';
changed = true;
logger.debug('sync_agents.hermes_profile_discovered', { id: s.id });
}
}
if (changed) sendToSaas({ action: 'agent_update', version: BRIDGE_VERSION, agents: buildAgentSummaries() });
}).catch((err) => {
logger.warn('sync_agents.hermes_discover_failed', { error: err?.message });
});
syncAgentDiscovery({
onAgentAdded(id, info) {
startWatcher(id, info.workspace, (agentId, workspaceDir, event, filePath) => {
processFileEvent(agentId, workspaceDir, event, filePath, {
sendToSaas,
setAgentRunning,
});
});
},
onAgentRemoved(id) {
stopWatcher(id);
},
onChanged() {
sendToSaas({
action: 'agent_update',
version: BRIDGE_VERSION,
agents: buildAgentSummaries(),
});
sendHeartbeat();
},
});
}
// ── Connection ────────────────────────────────────────────────────────────────
/**
* Schedules a reconnection attempt after `RECONNECT_MS` milliseconds.
* Consecutive calls before the timer fires are ignored (only one timer runs).
*/
function scheduleReconnect(): void {
if (reconnectTimer) return;
reconnectTimer = setTimeout(() => { reconnectTimer = null; connect(); }, RECONNECT_MS);
}
/**
* Returns true when the WebSocket close code or error message indicates an
* authentication failure (invalid or missing PAIRING_TOKEN).
*
* @param code - WebSocket close code (pass 0 when checking an error).
* @param message - Error message string (pass "" when checking a close code).
*/
function isAuthError(code: number, message: string): boolean {
if (code === 1008 || code === 1002) return true;
return ['401', 'Unauthorized', 'Expected 101', '403'].some(s => message.includes(s));
}
const AUTH_RETRY_MS = 30_000;
/**
* Logs an authentication-failure banner and schedules a retry after AUTH_RETRY_MS.
* Does NOT exit — allows the token to be rotated without restarting the process.
*
* @param detail - Additional context to include in the error output.
*/
function retryOnAuthFailure(detail: string): void {
logger.error('auth_failed', { detail, retrySeconds: AUTH_RETRY_MS / 1000 });
console.error('AUTHENTICATION FAILED');
console.error(detail);
console.error(`Retrying in ${AUTH_RETRY_MS / 1000}s — update PAIRING_TOKEN and restart if this persists.`);
reconnectTimer = setTimeout(() => { reconnectTimer = null; connect(); }, AUTH_RETRY_MS);
}
/**
* Opens the WebSocket connection to the SaaS, registers all event handlers,
* and sends the initial handshake once the connection is established.
*
* On successful open:
* - Sends `handshake` with version and discovered agent list.
* - Flushes any queued outgoing messages.
* - Starts the heartbeat and config-poll timers.
*
* On close with an auth code (1008/1002): exits the process immediately.
* On other close codes: schedules a reconnect.
* On error with an auth marker: exits the process immediately.
* On other errors: schedules a reconnect.
*/
export function connect(provider?: IProvider): void {
if (provider) activeProvider = provider;
if (reconnectTimer) { clearTimeout(reconnectTimer); reconnectTimer = null; }
logger.info('connecting', { url: SAAS_URL, providers: PROVIDERS });
ws = new WebSocket(SAAS_URL, {
headers: {
'x-bridge-version': BRIDGE_VERSION,
'x-agents': Object.keys(discoveredAgents).join(','),
'authorization': `Bearer ${PAIRING_TOKEN}`,
},
});
// Abort if the server never responds within CONNECTION_TIMEOUT_MS
connectionAttemptTimer = setTimeout(() => {
if (ws && ws.readyState === WebSocket.CONNECTING) {
logger.warn('connection_timeout', { message: 'server not responding' });
try { ws.close(); } catch {}
scheduleReconnect();
}
}, CONNECTION_TIMEOUT_MS);
// ── open ──────────────────────────────────────────────────────────────────
ws.on('open', () => {
if (connectionAttemptTimer) { clearTimeout(connectionAttemptTimer); connectionAttemptTimer = null; }
isConnected = true;
logger.info('connected', {});
const isOpenClaw = PROVIDERS.includes('openclaw');
const agents = isOpenClaw ? buildAgentSummaries() : [];
const hs = { action: 'handshake', version: BRIDGE_VERSION, agents, providers: PROVIDERS };
if (isOpenClaw) {
logger.info('handshake_sent', { providers: PROVIDERS, agentCount: agents.length });
} else {
logger.info('handshake_sent', { providers: PROVIDERS });
}
sendToSaas(hs);
// Async: run provider health checks and send providerHealth as a follow-up message.
void checkAndSendProviderHealth(true);
// Async: query each active provider for its available models and forward to SaaS.
// This must not block the handshake or the message loop.
void (async () => {
try {
if (!activeProvider?.listModels) return;
const providerModels = await Promise.race([
activeProvider.listModels(),
new Promise<string[]>(resolve => setTimeout(() => resolve([]), 12_000)),
]);
if (providerModels.length > 0) {
// Send per-provider map: for a MultiProvider the names collapse into the flat list;
// here we use the top-level provider name so the backend can key by it.
const models: Record<string, string[]> = {};
// Expand MultiProvider sub-providers if supported, otherwise use top-level name.
if ((activeProvider as any).providers) {
const subProviders: IProvider[] = (activeProvider as any).providers;
const subResults = await Promise.allSettled(
subProviders.filter(p => p.listModels).map(p =>
Promise.race([
p.listModels!(),
new Promise<string[]>(resolve => setTimeout(() => resolve([]), 10_000)),
]).then(ids => ({ name: p.providerName, ids }))
)
);
for (const r of subResults) {
if (r.status === 'fulfilled' && r.value.ids.length > 0) {
models[r.value.name] = r.value.ids;
}
}
} else {
models[activeProvider.providerName] = providerModels;
}
if (Object.keys(models).length > 0) {
sendToSaas({ action: 'available_models', models });
logger.info('available_models_sent', { providers: Object.keys(models).join(',') });
}
}
} catch (err: any) {
logger.warn('available_models_fetch_failed', { error: err?.message });
}
})();
flushPendingQueue();
startHeartbeat();
startConfigPoll();
});
// ── message ───────────────────────────────────────────────────────────────
ws.on('message', async (data: any) => {
try {
const raw = data?.toString?.() ?? String(data);
logger.debug('incoming_message', { preview: raw.slice(0, 1024) });
// Optional debug dump: persist full incoming payloads when explicitly enabled.
if (BRIDGE_INCOMING_DUMP_PATH) {
try {
fs.appendFileSync(BRIDGE_INCOMING_DUMP_PATH, raw.replace(/\r?\n/g, '') + "\n");
} catch (e: any) {
if (!incomingDumpWarned) {
incomingDumpWarned = true;
logger.warn('dump_incoming_failed', {
path: BRIDGE_INCOMING_DUMP_PATH,
error: e?.message,
});
}
}
}
} catch (err) { /* ignore */ }
try {
await handleMessage(data, {
sendToSaas,
syncAgents: runSyncAgents,
provider: activeProvider!,
});
} catch (err: any) {
logger.error('handleMessage_critical_error', { error: err.message, stack: err.stack });
}
});
// ── close ─────────────────────────────────────────────────────────────────
ws.on('close', (code: number) => {
if (connectionAttemptTimer) { clearTimeout(connectionAttemptTimer); connectionAttemptTimer = null; }
isConnected = false;
stopHeartbeat();
// Auth failure was already handled by the error handler — skip.
if (authFailed) return;
if (isAuthError(code, '')) {
authFailed = true;
retryOnAuthFailure(`WebSocket closed with code ${code} (invalid/missing PAIRING_TOKEN).`);
} else {
logger.warn('connection_closed', { code, retrySeconds: RECONNECT_MS / 1000 });
scheduleReconnect();
}
});
// ── error ─────────────────────────────────────────────────────────────────
ws.on('error', (err: Error) => {
if (connectionAttemptTimer) { clearTimeout(connectionAttemptTimer); connectionAttemptTimer = null; }
if (isAuthError(0, err.message)) {
authFailed = true;
retryOnAuthFailure(err.message);
} else {
logger.error('websocket_error', { message: err.message });
scheduleReconnect();
}
});
// Reset auth flag when a fresh connection attempt begins.
authFailed = false;
}