116 lines
2.4 KiB
JavaScript
116 lines
2.4 KiB
JavaScript
import { AGENT_DEFS, colorToCss } from './agent-defs.js';
|
|
import { setAgentState } from './agents.js';
|
|
import { appendChatMessage } from './ui.js';
|
|
|
|
const WS_URL = import.meta.env.VITE_WS_URL || '';
|
|
|
|
const agentById = Object.fromEntries(AGENT_DEFS.map(d => [d.id, d]));
|
|
|
|
let ws = null;
|
|
let connectionState = 'disconnected';
|
|
let jobCount = 0;
|
|
let reconnectTimer = null;
|
|
const RECONNECT_DELAY_MS = 5000;
|
|
|
|
export function initWebSocket(_scene) {
|
|
if (!WS_URL) {
|
|
connectionState = 'disconnected';
|
|
return;
|
|
}
|
|
connect();
|
|
}
|
|
|
|
function connect() {
|
|
if (ws) {
|
|
ws.onclose = null;
|
|
ws.close();
|
|
}
|
|
|
|
connectionState = 'connecting';
|
|
|
|
try {
|
|
ws = new WebSocket(WS_URL);
|
|
} catch {
|
|
connectionState = 'disconnected';
|
|
scheduleReconnect();
|
|
return;
|
|
}
|
|
|
|
ws.onopen = () => {
|
|
connectionState = 'connected';
|
|
clearTimeout(reconnectTimer);
|
|
ws.send(JSON.stringify({
|
|
type: 'subscribe',
|
|
channel: 'agents',
|
|
clientId: crypto.randomUUID(),
|
|
}));
|
|
};
|
|
|
|
ws.onmessage = (event) => {
|
|
try {
|
|
handleMessage(JSON.parse(event.data));
|
|
} catch {
|
|
}
|
|
};
|
|
|
|
ws.onerror = () => {
|
|
connectionState = 'disconnected';
|
|
};
|
|
|
|
ws.onclose = () => {
|
|
connectionState = 'disconnected';
|
|
scheduleReconnect();
|
|
};
|
|
}
|
|
|
|
function scheduleReconnect() {
|
|
clearTimeout(reconnectTimer);
|
|
reconnectTimer = setTimeout(connect, RECONNECT_DELAY_MS);
|
|
}
|
|
|
|
function handleMessage(msg) {
|
|
switch (msg.type) {
|
|
case 'agent_state': {
|
|
if (msg.agentId && msg.state) {
|
|
setAgentState(msg.agentId, msg.state);
|
|
}
|
|
break;
|
|
}
|
|
case 'job_started': {
|
|
jobCount++;
|
|
if (msg.agentId) setAgentState(msg.agentId, 'active');
|
|
logEvent(`JOB ${(msg.jobId || '').slice(0, 8)} started`);
|
|
break;
|
|
}
|
|
case 'job_completed': {
|
|
if (jobCount > 0) jobCount--;
|
|
if (msg.agentId) setAgentState(msg.agentId, 'idle');
|
|
logEvent(`JOB ${(msg.jobId || '').slice(0, 8)} completed`);
|
|
break;
|
|
}
|
|
case 'chat': {
|
|
const def = agentById[msg.agentId];
|
|
if (def && msg.text) {
|
|
appendChatMessage(def.label, msg.text, colorToCss(def.color), def.id);
|
|
}
|
|
break;
|
|
}
|
|
case 'agent_count':
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
function logEvent(text) {
|
|
appendChatMessage('SYS', text, colorToCss(0x003300), 'sys');
|
|
}
|
|
|
|
export function getConnectionState() {
|
|
return connectionState;
|
|
}
|
|
|
|
export function getJobCount() {
|
|
return jobCount;
|
|
}
|