mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-04-21 21:52:07 +02:00
40 lines
No EOL
1.3 KiB
JavaScript
40 lines
No EOL
1.3 KiB
JavaScript
import { spawn } from 'child_process';
|
|
|
|
export class AgentProcess {
|
|
constructor(name) {
|
|
this.name = name;
|
|
}
|
|
start(clear_memory=false, autostart=false, profile='assist') {
|
|
let args = ['controller/init-agent.js', this.name];
|
|
args.push('-p', profile);
|
|
if (clear_memory)
|
|
args.push('-c');
|
|
if (autostart)
|
|
args.push('-a');
|
|
|
|
const agentProcess = spawn('node', args, {
|
|
stdio: 'inherit',
|
|
stderr: 'inherit',
|
|
});
|
|
|
|
let last_restart = Date.now();
|
|
agentProcess.on('exit', (code, signal) => {
|
|
console.log(`Agent process exited with code ${code} and signal ${signal}`);
|
|
|
|
if (code !== 0) {
|
|
// agent must run for at least 10 seconds before restarting
|
|
if (Date.now() - last_restart < 10000) {
|
|
console.error('Agent process exited too quickly. Killing entire process. Goodbye.');
|
|
process.exit(1);
|
|
}
|
|
console.log('Restarting agent...');
|
|
this.start(false, true);
|
|
last_restart = Date.now();
|
|
}
|
|
});
|
|
|
|
agentProcess.on('error', (err) => {
|
|
console.error('Failed to start agent process:', err);
|
|
});
|
|
}
|
|
} |