mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-26 17:03:45 +02:00
108 lines
2.9 KiB
JavaScript
108 lines
2.9 KiB
JavaScript
![]() |
import { io } from 'socket.io-client';
|
||
|
import convoManager from './conversation.js';
|
||
|
import { setSettings } from './settings.js';
|
||
|
|
||
|
class MindServerProxy {
|
||
|
constructor() {
|
||
|
if (MindServerProxy.instance) {
|
||
|
return MindServerProxy.instance;
|
||
|
}
|
||
|
|
||
|
this.socket = null;
|
||
|
this.connected = false;
|
||
|
this.agents = [];
|
||
|
MindServerProxy.instance = this;
|
||
|
}
|
||
|
|
||
|
async connect(name, host, port) {
|
||
|
if (this.connected) return;
|
||
|
|
||
|
this.name = name;
|
||
|
|
||
|
this.socket = io(`http://${host}:${port}`);
|
||
|
this.connected = true;
|
||
|
|
||
|
this.socket.on('connect', () => {
|
||
|
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 10 seconds'));
|
||
|
}, 10000);
|
||
|
|
||
|
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);
|
||
|
}
|