save/load modes to memory.json

This commit is contained in:
MaxRobinsonTheGreat 2024-04-20 22:18:26 -05:00
parent 89f25498c3
commit 2aeebac0cd
3 changed files with 24 additions and 8 deletions

View file

@ -17,15 +17,18 @@ export class Agent {
await this.prompter.initExamples();
if (load_mem)
this.history.load();
console.log('Logging in...');
this.bot = initBot(this.name);
initModes(this);
if (load_mem)
this.history.load();
this.bot.once('spawn', async () => {
// wait for a bit so stats are not undefined
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(`${this.name} spawned.`);
this.coder.clear();
@ -148,11 +151,17 @@ export class Agent {
else if (this.bot.time.timeOfDay == 18000)
this.bot.emit('midnight');
});
this.bot.on('health', () => {
if (this.bot.health < 20)
this.bot.emit('damaged');
});
let prev_health = this.bot.health;
this.bot.lastDamageTime = 0;
this.bot.lastDamageTaken = 0;
this.bot.on('health', () => {
if (this.bot.health < prev_health) {
this.bot.lastDamageTime = Date.now();
this.bot.lastDamageTaken = prev_health - this.bot.health;
}
prev_health = this.bot.health;
});
// Logging callbacks
this.bot.on('error' , (err) => {
console.error('Error event!', err);
@ -176,6 +185,7 @@ export class Agent {
}
});
this.bot.on('idle', () => {
this.bot.clearControlStates();
this.bot.modes.unPauseAll();
this.coder.executeResume();
});

View file

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

View file

@ -55,6 +55,9 @@ export class History {
};
if (this.agent.npc.data !== null)
data.npc = this.agent.npc.data.toObject();
const modes = this.agent.bot.modes.getJson();
if (modes !== null)
data.modes = modes;
const json_data = JSON.stringify(data, null, 4);
writeFileSync(this.memory_fp, json_data, (err) => {
if (err) {
@ -71,9 +74,11 @@ export class History {
const obj = JSON.parse(data);
this.memory = obj.memory;
this.agent.npc.data = NPCData.fromObject(obj.npc);
if (obj.modes)
this.agent.bot.modes.loadJson(obj.modes);
this.turns = obj.turns;
} catch (err) {
console.error(`No memory file '${this.memory_fp}' for agent ${this.name}.`);
console.error(`Error reading ${this.name}'s memory file: ${err.message}`);
}
}