import { io } from 'socket.io-client'; import convoManager from './conversation.js'; import { setSettings } from './settings.js'; // agents connection to mindserver // always connect to localhost class MindServerProxy { constructor() { if (MindServerProxy.instance) { return MindServerProxy.instance; } this.socket = null; this.connected = false; this.agents = []; MindServerProxy.instance = this; } async connect(name, port) { if (this.connected) return; this.name = name; this.socket = io(`http://localhost:${port}`); await new Promise((resolve, reject) => { this.socket.on('connect', resolve); this.socket.on('connect_error', (err) => { console.error('Connection failed:', err); reject(err); }); }); this.connected = true; console.log(name, 'connected to MindServer'); this.socket.on('disconnect', () => { console.log('Disconnected from MindServer'); this.connected = false; }); this.socket.on('chat-message', (agentName, json) => { convoManager.receiveFromBot(agentName, json); }); this.socket.on('agents-update', (agents) => { this.agents = agents; convoManager.updateAgents(agents); if (this.agent?.task) { console.log(this.agent.name, 'updating available agents'); this.agent.task.updateAvailableAgents(agents); } }); this.socket.on('restart-agent', (agentName) => { console.log(`Restarting agent: ${agentName}`); this.agent.cleanKill(); }); this.socket.on('send-message', (agentName, message) => { try { this.agent.respondFunc("NO USERNAME", message); } catch (error) { console.error('Error: ', JSON.stringify(error, Object.getOwnPropertyNames(error))); } }); // Request settings and wait for response await new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error('Settings request timed out after 5 seconds')); }, 5000); this.socket.emit('get-settings', name, (response) => { clearTimeout(timeout); if (response.error) { return reject(new Error(response.error)); } setSettings(response.settings); resolve(); }); }); } setAgent(agent) { this.agent = agent; } getAgents() { return this.agents; } getNumOtherAgents() { return this.agents.length - 1; } login() { this.socket.emit('login-agent', this.agent.name); } shutdown() { this.socket.emit('shutdown'); } getSocket() { return this.socket; } } // Create and export a singleton instance export const serverProxy = new MindServerProxy(); export function sendBotChatToServer(agentName, json) { serverProxy.getSocket().emit('chat-message', agentName, json); }