File size: 1,241 Bytes
4e23b01 | 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 | import type { XstateInspectionCollector } from '@moonshot-ai/agent-core-v2/human/xstateInspection';
import { WebSocketServer } from 'ws';
import { selectWsBearerProtocol } from '../bearerProtocol';
import { WsConnectionDebug } from './wsConnectionDebug';
export const WS_DEBUG_PATH = '/api/v1/debug/ws';
export interface RegisterWsDebugOptions {
readonly collector?: XstateInspectionCollector;
readonly heartbeatIntervalMs?: number;
readonly flushIntervalMs?: number;
readonly highWaterMarkBytes?: number;
}
export function registerWsDebug(opts: RegisterWsDebugOptions = {}): WebSocketServer {
const wss = new WebSocketServer({ noServer: true, handleProtocols: selectWsBearerProtocol });
const connections = new Set<WsConnectionDebug>();
wss.on('connection', (socket) => {
const conn = new WsConnectionDebug({
socket,
collector: opts.collector,
heartbeatIntervalMs: opts.heartbeatIntervalMs,
flushIntervalMs: opts.flushIntervalMs,
highWaterMarkBytes: opts.highWaterMarkBytes,
});
connections.add(conn);
socket.on('close', () => {
connections.delete(conn);
});
});
wss.on('close', () => {
for (const conn of connections) conn.close();
});
return wss;
}
|