From cee25212aaf5ef5a68a722614619eefaeba6ee6f Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Fri, 26 Jan 2024 15:05:36 -0600 Subject: [PATCH 1/2] fixed end executing, logging, added param --- src/agent/coder.js | 12 ++++++------ src/agent/commands/actions.js | 12 ++++++++---- src/agent/library/skills.js | 2 +- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/agent/coder.js b/src/agent/coder.js index a5d19d4..6bdab1a 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -62,7 +62,7 @@ export class Coder { } santitizeCode(code) { - const remove_strs = ['javascript', 'js'] + const remove_strs = ['Javascript', 'javascript', 'js'] for (let r of remove_strs) { if (code.startsWith(r)) { code = code.slice(r.length); @@ -97,6 +97,8 @@ export class Coder { let code_return = null; let failures = 0; for (let i=0; i<5; i++) { + if (this.agent.bot.interrupt_code) + return; console.log(messages) let res = await sendRequest(messages, system_message); console.log('Code generation response:', res) @@ -142,11 +144,7 @@ export class Coder { role: 'system', content: code_return.message }); - - if (this.agent.bot.interrupt_code) - return; } - return; } @@ -173,8 +171,10 @@ export class Coder { this.clear(); return {success:true, message: output, interrupted, timedout}; } catch (err) { - console.error("Code execution triggered catch: " + err); + this.executing = false; clearTimeout(TIMEOUT); + + console.error("Code execution triggered catch: " + err); await this.stop(); let message = this.formatOutput(this.agent.bot) + '!!Code threw exception!! Error: ' + err; diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 2bb9cb8..119de1f 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -64,10 +64,14 @@ export const actionsList = [ }, { name: '!givePlayer', - description: 'Give the specified item to the given player. Ex: !givePlayer("steve", "stone_pickaxe")', - params: { 'player_name': '(string) The name of the player to give the item to.', 'item_name': '(string) The name of the item to give.' }, - perform: wrapExecution(async (agent, player_name, item_name) => { - await skills.giveToPlayer(agent.bot, item_name, player_name); + description: 'Give the specified item to the given player. Ex: !givePlayer("steve", "stone_pickaxe", 1)', + params: { + 'player_name': '(string) The name of the player to give the item to.', + 'item_name': '(string) The name of the item to give.' , + 'num': '(number) The number of items to give.' + }, + perform: wrapExecution(async (agent, player_name, item_name, num) => { + await skills.giveToPlayer(agent.bot, item_name, player_name, num); }) }, { diff --git a/src/agent/library/skills.js b/src/agent/library/skills.js index 8cea083..9cc8583 100644 --- a/src/agent/library/skills.js +++ b/src/agent/library/skills.js @@ -4,7 +4,7 @@ import pf from 'mineflayer-pathfinder'; import Vec3 from 'vec3'; -function log(bot, message, chat=false) { +export function log(bot, message, chat=false) { bot.output += message + '\n'; if (chat) bot.chat(message); From e63b528bba7c180e6da0e0ce47425c0a4af63f53 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Fri, 26 Jan 2024 15:41:55 -0600 Subject: [PATCH 2/2] better idle check, wait for done generating --- src/agent/agent.js | 8 ++++---- src/agent/coder.js | 10 ++++++++++ src/agent/modes.js | 10 +++++----- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index 2b486a3..cb1d9c7 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -165,13 +165,13 @@ export class Agent { } }); - this.self_defense = true; - this.defending = false; - this._pause_defending = false; - // set interval every 300ms to update the bot's state this.update_interval = setInterval(async () => { this.bot.modes.update(); }, 300); } + + isIdle() { + return !this.coder.executing && !this.coder.generating; + } } diff --git a/src/agent/coder.js b/src/agent/coder.js index 6bdab1a..d4d842a 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -10,6 +10,7 @@ export class Coder { this.file_counter = 0; this.fp = '/bots/'+agent.name+'/action-code/'; this.executing = false; + this.generating = false; this.code_template = ''; this.timedout = false; } @@ -87,6 +88,15 @@ export class Coder { async generateCode(agent_history) { + // wrapper to prevent overlapping code generation loops + await this.stop(); + this.generating = true; + await this.generateCodeLoop(agent_history); + this.generating = false; + } + + + async generateCodeLoop(agent_history) { let system_message = "You are a minecraft mineflayer bot that plays minecraft by writing javascript codeblocks. Given the conversation between you and the user, use the provided skills and world functions to write your code in a codeblock. Example response: ``` // your code here ``` You will then be given a response to your code. If you are satisfied with the response, respond without a codeblock in a conversational way. If something went wrong, write another codeblock and try to fix the problem."; system_message += getSkillDocs(); diff --git a/src/agent/modes.js b/src/agent/modes.js index f6add51..adf87ee 100644 --- a/src/agent/modes.js +++ b/src/agent/modes.js @@ -34,7 +34,7 @@ const modes = [ on: true, active: false, update: function (agent) { - if (!agent.coder.executing) { + if (agent.isIdle()) { const huntable = world.getNearestEntityWhere(agent.bot, entity => mc.isHuntable(entity), 8); if (huntable) { execute(this, agent, async () => { @@ -51,7 +51,7 @@ const modes = [ on: true, active: false, update: function (agent) { - if (!agent.coder.executing) { + if (agent.isIdle()) { let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8); if (item) { execute(this, agent, async () => { @@ -70,7 +70,7 @@ const modes = [ active: false, update: function (agent) { if (this.active) return; - if (!agent.coder.executing) { + if (agent.isIdle()) { // TODO: check light level instead of nearby torches, block.light is broken const near_torch = world.getNearestBlock(agent.bot, 'torch', 8); if (!near_torch) { @@ -96,7 +96,7 @@ const modes = [ last_entity: null, next_change: 0, update: function (agent) { - if (!agent.coder.executing) { + if (agent.isIdle()) { this.active = true; const entity = agent.bot.nearestEntity(); let entity_in_view = entity && entity.position.distanceTo(agent.bot.entity.position) < 10 && entity.name !== 'enderman'; @@ -174,7 +174,7 @@ class ModeController { } update() { - if (!this.agent.coder.executing) { + if (this.agent.isIdle()) { // other actions might pause a mode to override it // when idle, unpause all modes for (let mode of this.modes_list) {