diff --git a/agent.js b/agent.js index 5dda00a..b741c36 100644 --- a/agent.js +++ b/agent.js @@ -8,34 +8,37 @@ import { Events } from './utils/events.js'; export class Agent { - constructor(name, save_path, clear_memory=false, autostart=false) { + constructor(name, save_path, load_path=null, init_message=null) { this.name = name; this.bot = initBot(name); this.history = new History(this, save_path); this.history.loadExamples(); this.coder = new Coder(this); - if (!clear_memory) { - this.history.load(); + if (load_path) { + this.history.load(load_path); } - this.events = Events(this, this.history.events) + this.events = new Events(this, this.history.events) this.bot.on('login', () => { this.bot.chat('Hello world! I am ' + this.name); console.log(`${this.name} logged in.`); this.bot.on('chat', (username, message) => { - if (username === this.name) return; - console.log('received message from', username, ':', message); - - this.history.add(username, message); - this.handleMessage(); - }); + if (username === this.name) return; + console.log('received message from', username, ':', message); - if (autostart) - this.respond('system', 'Agent process restarted. Notify the user and decide what to do.'); - + this.history.add(username, message); + this.handleMessage(); + }); + + if (init_message) { + this.history.add('system', init_message); + this.handleMessage(); + } else { + this.bot.emit('finished_executing'); + } }); } diff --git a/bots/assist.json b/bots/assist.json new file mode 100644 index 0000000..079fe4b --- /dev/null +++ b/bots/assist.json @@ -0,0 +1,13 @@ +{ + "name": "andy", + "bio": "You are playing minecraft and assisting other players in tasks.", + "memory": "", + "events": [ + [ + "finished_executing", + "executeCode", + "let blocks = world.getNearbyBlockTypes(bot, 4);\nlet block_type = blocks[Math.floor(Math.random() * blocks.length)];\nawait skills.collectBlock(bot, block_type);\nawait new Promise(r => setTimeout(r, 1000));\nlet players = world.getNearbyPlayerNames(bot);\nlet player_name = players[Math.floor(Math.random() * players.length)];\nawait skills.goToPlayer(bot, player_name);\nawait new Promise(r => setTimeout(r, 1000));" + ] + ], + "turns": [] +} \ No newline at end of file diff --git a/bots/survive.json b/bots/survive.json new file mode 100644 index 0000000..ec69983 --- /dev/null +++ b/bots/survive.json @@ -0,0 +1,19 @@ +{ + "name": "andy", + "bio": "You are playing minecraft. Your goal is to collect materials and survive for as long as possible. Follow instructions from other players when appropriate.", + "memory": "", + "default": "", + "events": [ + [ + "finished_executing", + "sendThought", + "I need keep collecting and crafting to survive. I should plan what to do next and execute it." + ], + [ + "damaged", + "sendThought", + "I may be under attack or need to eat! I will stop what I am doing to check my health and take action." + ] + ], + "turns": [] +} \ No newline at end of file diff --git a/controller/agent-process.js b/controller/agent-process.js index 53fdf1b..3bc4fb7 100644 --- a/controller/agent-process.js +++ b/controller/agent-process.js @@ -4,8 +4,9 @@ export class AgentProcess { constructor(name) { this.name = name; } - start(clear_memory=false, autostart=false) { + start(clear_memory=false, autostart=false, profile='survive') { let args = ['controller/init-agent.js', this.name]; + args.push('-p', profile); if (clear_memory) args.push('-c'); if (autostart) diff --git a/controller/init-agent.js b/controller/init-agent.js index e632064..b9329d9 100644 --- a/controller/init-agent.js +++ b/controller/init-agent.js @@ -8,6 +8,11 @@ if (args.length < 1) { } const argv = yargs(args) + .option('profile', { + alias: 'p', + type: 'string', + description: 'profile to use for agent' + }) .option('clear_memory', { alias: 'c', type: 'boolean', @@ -20,8 +25,9 @@ const argv = yargs(args) }).argv const name = argv._[0]; -const clear_memory = !!argv.clear_memory; -const autostart = !!argv.autostart; const save_path = './bots/'+name+'.json'; +const profile = argv.profile; +const load_path = !!argv.clear_memory ? './bots/'+profile+'.json' : save_path; +const init_message = !!argv.autostart ? 'Agent process restarted. Notify the user and decide what to do.' : null; -new Agent(name, save_path, clear_memory, autostart); +new Agent(name, save_path, load_path, init_message); diff --git a/utils/events.js b/utils/events.js new file mode 100644 index 0000000..1da99ab --- /dev/null +++ b/utils/events.js @@ -0,0 +1,41 @@ +export class Events { + constructor(agent, events) { + this.events = events; + if (agent != null) + this.init(agent, events); + } + + init(agent, events) { + this.events = events; + for (let [event, callback, params] of events) { + if (callback != null) + agent.bot.on(event, this[callback].bind(this, agent, params)); + } + + agent.bot.on('time', () => { + if (agent.bot.time.timeOfDay == 0) + agent.bot.emit('sunrise'); + else if (agent.bot.time.timeOfDay == 6000) + agent.bot.emit('noon'); + else if (agent.bot.time.timeOfDay == 12000) + agent.bot.emit('sunset'); + else if (agent.bot.time.timeOfDay == 18000) + agent.bot.emit('midnight'); + }); + + agent.bot.on('health', () => { + if (agent.bot.health < 20) + agent.bot.emit('damaged'); + }); + } + + executeCode(agent, code) { + agent.executeCode(code); + } + + sendThought(agent, message) { + agent.history.add(agent.name, message); + agent.handleMessage(); + } + +} \ No newline at end of file diff --git a/utils/history.js b/utils/history.js index f1d4d60..93d46be 100644 --- a/utils/history.js +++ b/utils/history.js @@ -37,8 +37,7 @@ export class History { } getSystemMessage() { - let system_message = `You are a playful Minecraft bot named '${this.name}' that can communicate with players, see, move, mine, build, and interact with the world by writing and executing code. - Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in your responses, omit needless words, and do not give instructions unless asked.`; + let system_message = `You are a playful Minecraft bot named '${this.name}' that can communicate with players, see, move, mine, build, and interact with the world by writing and executing code. Act human-like as if you were a typical Minecraft player, rather than an AI. Be very brief in your responses, omit needless words, and do not give instructions unless asked.`; system_message += getQueryDocs(); system_message += getSkillDocs(); if (this.bio != '') @@ -104,6 +103,8 @@ export class History { const embedding = await embed(messages); this.examples.push({'embedding': embedding, 'turns': example}); } + + await this.setExamples(); } async setExamples() { @@ -143,12 +144,14 @@ export class History { await this.storeMemories(to_summarize); } - if (role === 'user') + if (role != 'assistant') await this.setExamples(); } - save() { - if (this.save_path === '' || this.save_path == null) return; + save(save_path=null) { + if (save_path == null) + save_path = this.save_path; + if (save_path === '' || save_path == null) return; // save history object to json file mkdirSync('bots', { recursive: true }); let data = { @@ -159,7 +162,7 @@ export class History { 'turns': this.turns }; const json_data = JSON.stringify(data, null, 4); - writeFileSync(this.save_path, json_data, (err) => { + writeFileSync(save_path, json_data, (err) => { if (err) { throw err; } @@ -167,11 +170,13 @@ export class History { }); } - load() { - if (this.save_path === '' || this.save_path == null) return; + load(load_path=null) { + if (load_path == null) + load_path = this.save_path; + if (load_path === '' || load_path == null) return; try { // load history object from json file - const data = readFileSync(this.save_path, 'utf8'); + const data = readFileSync(load_path, 'utf8'); const obj = JSON.parse(data); this.bio = obj.bio; this.memory = obj.memory; @@ -179,6 +184,7 @@ export class History { this.turns = obj.turns; } catch (err) { console.log('No history file found for ' + this.name + '.'); + console.log(load_path); } } } \ No newline at end of file diff --git a/utils/queries.js b/utils/queries.js index e2cbdb4..2f732d6 100644 --- a/utils/queries.js +++ b/utils/queries.js @@ -79,8 +79,7 @@ export function containsQuery(message) { } export function getQueryDocs() { - let docs = `\n*QUERY DOCS\n You can use the following commands to query for information about the world. - Use the query name in your response and the next input will have the requested information.\n`; + let docs = `\n*QUERY DOCS\n You can use the following commands to query for information about the world. Use the query name in your response and the next input will have the requested information.\n`; for (let query of queryList) { docs += query.name + ': ' + query.description + '\n'; } diff --git a/utils/skill-library.js b/utils/skill-library.js index e9830dd..246e3ab 100644 --- a/utils/skill-library.js +++ b/utils/skill-library.js @@ -2,8 +2,7 @@ import * as skills from './skills.js'; import * as world from './world.js'; export function getSkillDocs() { - let docstring = "\n*SKILL DOCS\nThese skills are javascript functions that can be called with a js function by writing a code block. Ex: '```// write description comment and code here```' \n\ - Your code block should return a bool indicating if the task was completed successfully. It will return true if you don't write a return statement.\n"; + let docstring = "\n*SKILL DOCS\nThese skills are javascript functions that can be called with a js function by writing a code block. Ex: '```// write description comment and code here```' \nYour code block should return a bool indicating if the task was completed successfully. It will return true if you don't write a return statement.\n"; docstring += docHelper(Object.values(skills), 'skills'); docstring += docHelper(Object.values(world), 'world'); return docstring + '*\n';