2024-11-18 23:02:37 -06:00
|
|
|
import { io } from 'socket.io-client';
|
2024-11-19 22:21:17 -06:00
|
|
|
import { recieveFromBot, updateAgents } from './conversation.js';
|
|
|
|
import settings from '../../settings.js';
|
2024-11-18 23:02:37 -06:00
|
|
|
|
|
|
|
class ServerProxy {
|
|
|
|
constructor() {
|
|
|
|
if (ServerProxy.instance) {
|
|
|
|
return ServerProxy.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.socket = null;
|
|
|
|
this.connected = false;
|
|
|
|
ServerProxy.instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
connect() {
|
|
|
|
if (this.connected) return;
|
|
|
|
|
2024-11-19 22:21:17 -06:00
|
|
|
this.socket = io(`http://${settings.mindserver_host}:${settings.mindserver_port}`);
|
2024-11-18 23:02:37 -06:00
|
|
|
this.connected = true;
|
|
|
|
|
|
|
|
this.socket.on('connect', () => {
|
|
|
|
console.log('Connected to MindServer');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on('disconnect', () => {
|
|
|
|
console.log('Disconnected from MindServer');
|
|
|
|
this.connected = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on('chat-message', (agentName, json) => {
|
|
|
|
recieveFromBot(agentName, json);
|
|
|
|
});
|
2024-11-19 22:21:17 -06:00
|
|
|
|
|
|
|
this.socket.on('agents-update', (agents) => {
|
|
|
|
updateAgents(agents);
|
|
|
|
});
|
2024-11-18 23:02:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
registerAgent(agentName) {
|
|
|
|
if (!this.connected) {
|
|
|
|
console.warn('Cannot register agent: not connected to MindServer');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.socket.emit('register-agent', agentName);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSocket() {
|
|
|
|
return this.socket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and export a singleton instance
|
|
|
|
export const serverProxy = new ServerProxy();
|
|
|
|
|
|
|
|
export function sendBotChatToServer(agentName, json) {
|
|
|
|
serverProxy.getSocket().emit('chat-message', agentName, json);
|
|
|
|
}
|