mindcraft/src/agent/server_proxy.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

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';
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}`);
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);
});
}
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);
}