save memory before restart

This commit is contained in:
MaxRobinsonTheGreat 2024-05-20 00:52:08 -05:00
parent 89a35d3495
commit 5454252dde
4 changed files with 14 additions and 18 deletions

View file

@ -170,7 +170,7 @@ export class Agent {
});
this.bot.on('end', (reason) => {
console.warn('Bot disconnected! Killing agent process.', reason)
process.exit(1);
this.cleanKill('Bot disconnected! Killing agent process.');
});
this.bot.on('death', () => {
this.coder.cancelResume();
@ -178,7 +178,7 @@ export class Agent {
});
this.bot.on('kicked', (reason) => {
console.warn('Bot kicked!', reason);
process.exit(1);
this.cleanKill('Bot kicked! Killing agent process.');
});
this.bot.on('messagestr', async (message, _, jsonMsg) => {
if (jsonMsg.translate && jsonMsg.translate.startsWith('death') && message.startsWith(this.name)) {
@ -215,4 +215,11 @@ export class Agent {
isIdle() {
return !this.coder.executing && !this.coder.generating;
}
cleanKill(msg='Killing agent process...') {
this.history.add('system', msg);
this.bot.chat('Goodbye world.')
this.history.save();
process.exit(1);
}
}

View file

@ -240,7 +240,7 @@ export class Coder {
console.log('waiting for code to finish executing...');
await new Promise(resolve => setTimeout(resolve, 1000));
if (Date.now() - start > 10 * 1000) {
process.exit(1); // force exit program after 10 seconds of failing to stop
this.agent.cleanKill('Code execution refused stop after 10 seconds. Killing process.');
}
}
}
@ -255,19 +255,8 @@ export class Coder {
return setTimeout(async () => {
console.warn(`Code execution timed out after ${TIMEOUT_MINS} minutes. Attempting force stop.`);
this.timedout = true;
this.agent.bot.output += `\nAction performed for ${TIMEOUT_MINS} minutes and then timed out and stopped. You may want to continue or do something else.`;
this.stop(); // last attempt to stop
await new Promise(resolve => setTimeout(resolve, 5 * 1000)); // wait 5 seconds
if (this.executing) {
console.error(`Failed to stop. Killing process. Goodbye.`);
this.agent.bot.output += `\nForce stop failed! Process was killed and will be restarted. Goodbye world.`;
this.agent.bot.chat('Goodbye world.');
let output = this.formatOutput(this.agent.bot);
this.agent.history.add('system', output);
this.agent.history.save();
process.exit(1); // force exit program
}
console.log('Code execution stopped successfully.');
this.agent.history.add('system', `Code execution timed out after ${TIMEOUT_MINS} minutes. Attempting force stop.`);
await this.stop(); // last attempt to stop
}, TIMEOUT_MINS*60*1000);
}
}

View file

@ -45,7 +45,7 @@ export const actionsList = [
description: 'Restart the agent process.',
perform: async function (agent) {
await agent.history.save();
process.exit(1);
agent.cleanKill();
}
},
{

View file

@ -25,7 +25,7 @@ export class AgentProcess {
process.exit(1);
}
console.log('Restarting agent...');
this.start(profile, true, 'Agent process restarted. Notify the user and decide what to do.');
this.start(profile, true, 'Agent process restarted.');
last_restart = Date.now();
}
});