added memory bank and commands for saving places

This commit is contained in:
MaxRobinsonTheGreat 2024-04-27 23:28:34 -05:00
parent 358500839a
commit 81cdc718bd
5 changed files with 57 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import { initModes } from './modes.js';
import { initBot } from '../utils/mcdata.js';
import { containsCommand, commandExists, executeCommand, truncCommandMessage } from './commands/index.js';
import { NPCContoller } from './npc/controller.js';
import { MemoryBank } from './memory_bank.js';
export class Agent {
@ -14,6 +15,7 @@ export class Agent {
this.history = new History(this);
this.coder = new Coder(this);
this.npc = new NPCContoller(this);
this.memory_bank = new MemoryBank();
await this.prompter.initExamples();

View file

@ -103,6 +103,28 @@ export const actionsList = [
await skills.moveAway(agent.bot, distance);
})
},
{
name: '!rememberHere',
description: 'Save the current location with a given name.',
params: {'name': '(string) The name to remember the location as.'},
perform: async function (agent, name) {
const pos = agent.bot.entity.position;
agent.memory_bank.rememberPlace(name, pos.x, pos.y, pos.z);
}
},
{
name: '!goToPlace',
description: 'Go to a saved location.',
params: {'name': '(string) The name of the location to go to.'},
perform: wrapExecution(async (agent, name) => {
const pos = agent.memory_bank.recallPlace(name);
if (!pos) {
skills.log(agent.bot, `No location named "${name}" saved.`);
return;
}
await skills.goToPosition(agent.bot, pos[0], pos[1], pos[2], 1);
})
},
{
name: '!givePlayer',
description: 'Give the specified item to the given player.',

View file

@ -122,5 +122,12 @@ export const queryList = [
perform: function (agent) {
return agent.bot.modes.getStr();
}
},
{
name: '!savedPlaces',
description: 'List all saved locations.',
perform: async function (agent) {
return "Saved place names: " + Object.keys(agent.memory_bank.places).join(', ');
}
}
];

View file

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

21
src/agent/memory_bank.js Normal file
View file

@ -0,0 +1,21 @@
export class MemoryBank {
constructor() {
this.memory = {};
}
rememberPlace(name, x, y, z) {
this.memory[name] = [x, y, z];
}
recallPlace(name) {
return this.memory[name];
}
getJson() {
return this.memory
}
loadJson(json) {
this.memory = json;
}
}