added full shutdown from ui/task

This commit is contained in:
MaxRobinsonTheGreat 2024-12-13 10:42:41 -06:00
parent 3717d9b9f4
commit 7ad48c29ad
6 changed files with 58 additions and 12 deletions

View file

@ -438,22 +438,18 @@ export class Agent {
}, INTERVAL);
this.bot.emit('idle');
// Check for task completion
if (this.task.data) {
setInterval(() => {
let res = this.task.isDone();
if (res) {
// TODO kill other bots
this.cleanKill(res.message, res.code);
}
}, 1000);
}
}
async update(delta) {
await this.bot.modes.update();
this.self_prompter.update(delta);
if (this.task.data) {
let res = this.task.isDone();
if (res) {
console.log('Task finished:', res.message);
this.killAll();
}
}
}
isIdle() {
@ -466,4 +462,8 @@ export class Agent {
this.history.save();
process.exit(code);
}
killAll() {
serverProxy.shutdown();
}
}

View file

@ -48,6 +48,10 @@ class AgentServerProxy {
this.socket.emit('login-agent', this.agent.name);
}
shutdown() {
this.socket.emit('shutdown');
}
getSocket() {
return this.socket;
}

View file

@ -179,7 +179,7 @@ export class Task {
await new Promise((resolve) => setTimeout(resolve, 10000));
if (available_agents.length < this.data.agent_count) {
console.log(`Missing ${this.data.agent_count - available_agents.length} bot(s).`);
this.agent.cleanKill('Not all required players/bots are present in the world. Exiting.', 4);
this.agent.killAll();
}
}

View file

@ -35,6 +35,16 @@ class MainProxy {
this.socket.on('register-agents-success', () => {
console.log('Agents registered');
});
this.socket.on('shutdown', () => {
console.log('Shutting down');
for (let agentName in this.agent_processes) {
this.agent_processes[agentName].stop();
}
setTimeout(() => {
process.exit(0);
}, 2000);
});
}
addAgent(agent) {

View file

@ -101,6 +101,19 @@ export function createMindServer(port = 8080) {
}
});
socket.on('stop-all-agents', () => {
console.log('Killing all agents');
stopAllAgents();
});
socket.on('shutdown', () => {
console.log('Shutting down');
for (let manager of Object.values(agentManagers)) {
manager.emit('shutdown');
}
process.exit(0);
});
});
server.listen(port, 'localhost', () => {
@ -121,6 +134,15 @@ function agentsUpdate(socket) {
socket.emit('agents-update', agents);
}
function stopAllAgents() {
for (const agentName in inGameAgents) {
let manager = agentManagers[agentName];
if (manager) {
manager.emit('stop-agent', agentName);
}
}
}
// Optional: export these if you need access to them from other files
export const getIO = () => io;
export const getServer = () => server;

View file

@ -85,6 +85,8 @@
`}
</div>
</div>
<button class="stop-btn" onclick="killAllAgents()">Stop All</button>
<button class="stop-btn" onclick="shutdown()">Shutdown</button>
`).join('') :
'<div class="agent">No agents connected</div>';
});
@ -100,6 +102,14 @@
function stopAgent(agentName) {
socket.emit('stop-agent', agentName);
}
function killAllAgents() {
socket.emit('stop-all-agents');
}
function shutdown() {
socket.emit('shutdown');
}
</script>
</body>
</html>