From 85e7a508c2d0e7ab4f88f5d1ef7807034cbbd744 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Wed, 20 Dec 2023 16:30:05 -0800 Subject: [PATCH] simplify profiles and events --- agent.js | 93 +++++++++++++++++-------------------- bots/assist.json | 8 +--- bots/survive.json | 19 -------- controller/agent-process.js | 2 +- controller/init-agent.js | 3 +- utils/coder.js | 2 + utils/events.js | 11 ++++- 7 files changed, 56 insertions(+), 82 deletions(-) delete mode 100644 bots/survive.json diff --git a/agent.js b/agent.js index 2fab852..a6beffd 100644 --- a/agent.js +++ b/agent.js @@ -29,70 +29,61 @@ export class Agent { if (username === this.name) return; console.log('received message from', username, ':', message); - this.history.add(username, message); - this.handleMessage(); + this.handleMessage(username, message); }); if (init_message) { - this.history.add('system', init_message); - this.handleMessage(); + this.handleMessage('system', init_message); } else { this.bot.emit('finished_executing'); } }); } - async executeCode(code, triesRemaining=5) { - if (code == 'default') - code = this.history.default; + async handleMessage(source, message) { + await this.history.add(source, message); - if (code) { - this.coder.queueCode(code); - let code_return = await this.coder.execute(); - let message = code_return.message; - if (code_return.interrupted) - return; - if (!code_return.success) - message += "\n Write code to fix the problem and try again."; - console.log('code return:', message); - this.history.add('system', message); - if (!code_return.success) - await this.handleMessage(triesRemaining-1); - } - } + for (let i=0; i<5; i++) { + let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage()); + this.history.add(this.name, res); + let query_cmd = containsQuery(res); + if (query_cmd) { // contains query + let message = res.substring(0, res.indexOf(query_cmd)).trim(); + if (message) + this.bot.chat(message); + let query = getQuery(query_cmd); + let query_res = query.perform(this); + console.log('Agent used query:', query_cmd, 'and got:', query_res) + this.history.add('system', query_res); + } + else if (containsCodeBlock(res)) { // contains code block + console.log('Agent is executing code:', res) - async handleMessage(triesRemaining=5) { - if (triesRemaining == 0) { - console.log('Quitting response loop.'); - return; + let message = res.substring(0, res.indexOf('```')).trim(); + if (message) + this.bot.chat(message); + let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```')); + + if (code) { + this.coder.queueCode(code); + let code_return = await this.coder.execute(); + let message = code_return.message; + if (code_return.interrupted && !code_return.timedout) + break; + if (!code_return.success) { + message += "\nWrite code to fix the problem and try again."; + } + console.log('code return:', message); + this.history.add('system', message); + } + } + else { // conversation response + this.bot.chat(res); + console.log('Purely conversational response:', res); + break; + } } - let res = await sendRequest(this.history.getHistory(), this.history.getSystemMessage()); - this.history.add(this.name, res); - let query_cmd = containsQuery(res); - if (query_cmd) { // contains query - let message = res.substring(0, res.indexOf(query_cmd)).trim(); - if (message) - this.bot.chat(message); - let query = getQuery(query_cmd); - let query_res = query.perform(this); - console.log('Agent used query:', query_cmd, 'and got:', query_res) - this.history.add('system', query_res); - await this.handleMessage(triesRemaining-1); - } - else if (containsCodeBlock(res)) { // contains code block - console.log('Agent is executing code:', res) - - let message = res.substring(0, res.indexOf('```')).trim(); - if (message) - this.bot.chat(message); - let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```')); - await this.executeCode(code, triesRemaining); - } - else { // conversation response - this.bot.chat(res); - console.log('Purely conversational response:', res) - } this.history.save(); this.bot.emit('finished_executing'); } diff --git a/bots/assist.json b/bots/assist.json index 079fe4b..2307682 100644 --- a/bots/assist.json +++ b/bots/assist.json @@ -2,12 +2,6 @@ "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));" - ] - ], + "events": [], "turns": [] } \ No newline at end of file diff --git a/bots/survive.json b/bots/survive.json deleted file mode 100644 index ec69983..0000000 --- a/bots/survive.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "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 3bc4fb7..f3439f6 100644 --- a/controller/agent-process.js +++ b/controller/agent-process.js @@ -4,7 +4,7 @@ export class AgentProcess { constructor(name) { this.name = name; } - start(clear_memory=false, autostart=false, profile='survive') { + start(clear_memory=false, autostart=false, profile='assist') { let args = ['controller/init-agent.js', this.name]; args.push('-p', profile); if (clear_memory) diff --git a/controller/init-agent.js b/controller/init-agent.js index b9329d9..96d9381 100644 --- a/controller/init-agent.js +++ b/controller/init-agent.js @@ -26,8 +26,7 @@ const argv = yargs(args) const name = argv._[0]; const save_path = './bots/'+name+'.json'; -const profile = argv.profile; -const load_path = !!argv.clear_memory ? './bots/'+profile+'.json' : save_path; +const load_path = !!argv.clear_memory ? './bots/'+argv.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, load_path, init_message); diff --git a/utils/coder.js b/utils/coder.js index 96ad275..0b6e405 100644 --- a/utils/coder.js +++ b/utils/coder.js @@ -100,6 +100,7 @@ export class Coder { let interrupted = this.agent.bot.interrupt_code; let timedout = this.timedout; this.clear(); + this.agent.bot.emit("code_terminated"); return {success:true, message: output, interrupted, timedout}; } catch (err) { this.executing = false; @@ -110,6 +111,7 @@ export class Coder { message += '!!Code threw exception!! Error: ' + err; let interrupted = this.agent.bot.interrupt_code; await this.stop(); + this.agent.bot.emit("code_terminated"); return {success: false, message, interrupted, timedout: false}; } } diff --git a/utils/events.js b/utils/events.js index 1da99ab..4a64c0e 100644 --- a/utils/events.js +++ b/utils/events.js @@ -29,8 +29,12 @@ export class Events { }); } - executeCode(agent, code) { - agent.executeCode(code); + async executeCode(agent, code) { + console.log('responding to event with code.'); + agent.coder.queueCode(code); + let code_return = await agent.coder.execute(); + console.log('code return:', code_return.message); + agent.history.add('system', code_return.message); } sendThought(agent, message) { @@ -38,4 +42,7 @@ export class Events { agent.handleMessage(); } + sendChat(agent, message) { + agent.bot.chat(message); + } } \ No newline at end of file