restart saves memory, selfprompt will restart too

This commit is contained in:
MaxRobinsonTheGreat 2024-05-08 23:59:44 -05:00
parent 30a172aa9d
commit 85c2f56afe
6 changed files with 44 additions and 29 deletions

View file

@ -26,8 +26,10 @@ export class Agent {
initModes(this);
if (load_mem)
this.history.load();
let save_data = null;
if (load_mem) {
save_data = this.history.load();
}
this.bot.once('spawn', async () => {
// wait for a bit so stats are not undefined
@ -61,9 +63,16 @@ export class Agent {
bannedFood: ["rotten_flesh", "spider_eye", "poisonous_potato", "pufferfish", "chicken"]
};
if (init_message) {
if (save_data && save_data.self_prompt) {
let prompt = save_data.self_prompt;
if (init_message)
prompt = `${init_message}\n${prompt}`;
this.self_prompter.start(prompt);
}
else if (init_message) {
this.handleMessage('system', init_message, true);
} else {
}
else {
this.bot.chat('Hello world! I am ' + this.name);
this.bot.emit('finished_executing');
}
@ -194,7 +203,7 @@ export class Agent {
});
this.bot.on('end', (reason) => {
console.warn('Bot disconnected! Killing agent process.', reason)
process.exit(1);
this.cleanKill('Bot disconnected!');
});
this.bot.on('death', () => {
this.coder.cancelResume();
@ -202,7 +211,7 @@ export class Agent {
});
this.bot.on('kicked', (reason) => {
console.warn('Bot kicked!', reason);
process.exit(1);
this.cleanKill('Bot kicked!');
});
this.bot.on('messagestr', async (message, _, jsonMsg) => {
if (jsonMsg.translate && jsonMsg.translate.startsWith('death') && message.startsWith(this.name)) {
@ -246,4 +255,11 @@ export class Agent {
isIdle() {
return !this.coder.executing && !this.coder.generating;
}
cleanKill(msg='Killing agent process...') {
this.history.add('system', msg);
this.history.save();
process.exit(1);
}
}

View file

@ -239,7 +239,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.');
}
}
}
@ -254,19 +254,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

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

View file

@ -46,7 +46,7 @@ export class History {
}
}
save() {
async save() {
// save history object to json file
let data = {
'name': this.name,
@ -61,6 +61,9 @@ export class History {
const memory_bank = this.agent.memory_bank.getJson();
if (memory_bank !== null)
data.memory_bank = memory_bank;
if (this.agent.self_prompter.on) {
data.self_prompt = this.agent.self_prompter.prompt;
}
const json_data = JSON.stringify(data, null, 4);
writeFileSync(this.memory_fp, json_data, (err) => {
if (err) {
@ -82,9 +85,11 @@ export class History {
if (obj.memory_bank)
this.agent.memory_bank.loadJson(obj.memory_bank);
this.turns = obj.turns;
return obj;
} catch (err) {
console.error(`Error reading ${this.name}'s memory file: ${err.message}`);
}
return null;
}
clear() {

View file

@ -6,13 +6,16 @@ export class SelfPrompter {
this.interrupt = false;
this.prompt = '';
this.idle_time = 0;
this.restart_after = 1000;
this.cooldown = 2000;
}
start(prompt) {
console.log('Self-prompting started.');
if (!prompt) {
return 'No prompt specified. Ignoring request.';
}
if (this.on) {
return 'Agent is already self-prompting. Ignoring request.';
this.prompt = prompt;
}
this.on = true;
this.prompt = prompt;
@ -42,8 +45,10 @@ export class SelfPrompter {
break;
}
}
else
else {
no_command_count = 0;
await new Promise(r => setTimeout(r, this.cooldown));
}
}
console.log('self prompt loop stopped')
this.loop_active = false;
@ -58,8 +63,8 @@ export class SelfPrompter {
else
this.idle_time = 0;
if (this.idle_time >= this.restart_after) {
this.agent.bot.chat('Restarting self-prompting...');
if (this.idle_time >= this.cooldown) {
console.log('Restarting self-prompting...');
this.startLoop();
this.idle_time = 0;
}
@ -68,6 +73,7 @@ export class SelfPrompter {
async stopLoop() {
// you can call this without await if you don't need to wait for it to finish
console.log('stopping self-prompt loop')
this.interrupt = true;
while (this.loop_active) {
await new Promise(r => setTimeout(r, 500));

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();
}
});