From fed5a6d887d8195f206617ada954a1b09112f157 Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Sun, 12 Nov 2023 13:57:22 -0600 Subject: [PATCH 1/3] read template.js/feed code output to gpt --- agent.js | 14 +++------ agent_code/template.js | 6 ++-- utils/coder.js | 44 +++++++++++++++++++-------- utils/history.js | 19 +++++++++--- utils/skills.js | 69 +++++++++++++++++++++++++++++++++++++----- 5 files changed, 118 insertions(+), 34 deletions(-) diff --git a/agent.js b/agent.js index 2736b1c..95f64a5 100644 --- a/agent.js +++ b/agent.js @@ -45,26 +45,22 @@ export class Agent { console.log('Agent used query:', query_cmd); let query = getQuery(query_cmd); let query_res = query.perform(this); - this.history.add(this.name, query_res); + this.history.add('system', query_res); } else if (containsCodeBlock(res)) { // contains code block let message = res.substring(0, res.indexOf('```')).trim(); if (message) this.bot.chat(message); - else - this.bot.chat("Executing code..."); let code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```')); if (code) { - console.log('Queuing code: ' + code); this.coder.queueCode(code); let code_return = await this.coder.execute(); - if (code_return.success) - break; - else { - let message = "Code execution failed: " + code_return.message; + let message = code_return.message; + if (!code_return.success) { message += "\n Write code to fix the problem and try again."; - this.history.add(this.name, message); } + console.log('code return:', message); + this.history.add('system', message); } } else { // conversation response diff --git a/agent_code/template.js b/agent_code/template.js index 4fe1f61..1892adc 100644 --- a/agent_code/template.js +++ b/agent_code/template.js @@ -1,6 +1,8 @@ import * as skills from '../utils/skills.js'; import * as world from '../utils/world.js'; -// this file is currently unused +import Vec3 from 'vec3'; + export async function main(bot) { - // agent's code goes here + /* CODE HERE */ + return true; // potentially redundant return statement, in case agent doesn't return anything } diff --git a/utils/coder.js b/utils/coder.js index 2242339..38fd693 100644 --- a/utils/coder.js +++ b/utils/coder.js @@ -1,4 +1,4 @@ -import { writeFile, unlink } from 'fs'; +import { writeFile, readFile, unlink } from 'fs'; export class Coder { constructor(agent) { @@ -6,6 +6,14 @@ export class Coder { this.current_code = ''; this.file_counter = 0; this.fp = './agent_code/'; + this.agent.bot.output = ''; + this.code_template = ''; + + readFile(this.fp+'template.js', 'utf8', (err, data) => { + if (err) throw err; + console.log('Template str:', data); + this.code_template = data; + }); } queueCode(code) { @@ -42,14 +50,12 @@ export class Coder { async execute() { if (!this.current_code) return {success: false, message: "No code to execute."}; - let src = "import * as skills from '../utils/skills.js';"; - src += "\nimport * as world from '../utils/world.js';" - src += "\nimport Vec3 from 'vec3';" - src += `\n\nexport async function main(bot) {\n`; + if (!this.code_template) return {success: false, message: "Code template not loaded."}; + let src = ''; for (let line of this.current_code.split('\n')) { src += ` ${line}\n`; } - src += ` return true;\n}\n`; // potentially redundant return statement, in case agent doesn't return anything + src = this.code_template.replace('/* CODE HERE */', src); console.log("writing to file...", src) @@ -73,20 +79,34 @@ export class Coder { try { console.log('executing code...\n'); let execution_file = await import('.'+filename); + this.stop(); + let success = await execution_file.main(this.agent.bot); + console.log('code execution finished.', success) + let output = this.agent.bot.output ? 'Code output: \n' + this.agent.bot.output : ''; + // if there is output, add it to the message + if (success) + output += 'Code execution finished successfully.'; + else + output += 'Code execution failed!'; + console.log(output) this.clear(); - await execution_file.main(this.agent.bot); - let msg = 'Code executed successfully.'; - console.log(msg) - return {success: true, message: msg}; + return {success, message: output}; } catch (err) { console.error("Code execution triggered catch:" + err); - this.clear(); - return {success: false, message: err}; + let message = 'Code output: \n' + this.agent.bot.output + '\n'; + message += '!!Code threw exception!! Error: ' + err; + this.stop(); + return {success: false, message}; } } clear() { this.current_code = ''; + this.agent.bot.output = ''; + } + + stop() { + this.clear(); this.agent.bot.pathfinder.setGoal(null); } } \ No newline at end of file diff --git a/utils/history.js b/utils/history.js index 74b5d22..4d38389 100644 --- a/utils/history.js +++ b/utils/history.js @@ -4,25 +4,33 @@ let history_examples = [ {'role': 'user', 'content': 'grombo_Xx: What do you see?'}, {'role': 'assistant', 'content': 'Let me see... !blocks'}, - {'role': 'assistant', 'content': 'NEARBY_BLOCKS\n- oak_log\n- dirt\n- cobblestone'}, + {'role': 'system', 'content': 'NEARBY_BLOCKS\n- oak_log\n- dirt\n- cobblestone'}, {'role': 'assistant', 'content': 'I see some oak logs, dirt, and cobblestone.'}, {'role': 'user', 'content': 'zZZn98: come here'}, {'role': 'assistant', 'content': '```// I am going to navigate to zZZn98.\nreturn await skills.goToPlayer(bot, "zZZn98");```'}, + {'role': 'system', 'content': 'Code execution finished successfully.'}, + {'role': 'assistant', 'content': 'Here!'}, {'role': 'user', 'content': 'hanky: collect some sand for me please'}, {'role': 'assistant', 'content': 'Collecting sand...```// I am going to collect 3 sand and give to hanky.\n\ await skills.collectBlock(bot, "sand");\nreturn await skills.giveToPlayer(bot, "sand", "hanky");```'}, + {'role': 'system', 'content': 'Code Output:\nYou have reached player hanky.\nCode execution finished successfully.'}, + {'role': 'assistant', 'content': 'Here!'}, - {'role': 'user', 'content': 'sarah_O.o: can you do a dance for me?'}, - {'role': 'assistant', 'content': "I don't know how to do that."}, + {'role': 'user', 'content': 'sarah_O.o: can you fly up in the air?'}, + {'role': 'assistant', 'content': "I can't do that."}, {'role': 'user', 'content': 'hanky: kill that zombie!'}, {'role': 'assistant', 'content': "I'm attacking! ```//I'm going to attack the nearest zombie.\n\ return await skills.attackMob(bot, 'zombie');```"}, + {'role': 'system', 'content': 'Code Output:\nNo zombie nearby\nCode execution failed!'}, + {'role': 'assistant', 'content': 'I could not find a zombie nearby.'}, {'role': 'user', 'content': 'billybob: stop what you are doing'}, {'role': 'assistant', 'content': '```// I am going to write nothing to clear my code\n return true;```'}, + + ] export class History { @@ -37,7 +45,10 @@ export class History { add(name, content) { let role = 'assistant'; - if (name !== this.agent.name) { + if (name === 'system') { + role = 'system'; + } + else if (name !== this.agent.name) { role = 'user'; content = `${name}: ${content}`; } diff --git a/utils/skills.js b/utils/skills.js index 54e9219..5aa5c35 100644 --- a/utils/skills.js +++ b/utils/skills.js @@ -3,6 +3,13 @@ import { getCraftingTable, getInventoryCounts, getInventoryStacks, getNearbyMobs import pf from 'mineflayer-pathfinder'; import Vec3 from 'vec3'; +function log(bot, message) { + bot.output += message + '\n'; +} + +function f(x) { + return Math.floor(x); +} export async function craftItem(bot, itemName) { /** @@ -93,17 +100,58 @@ export async function placeBlock(bot, blockType, x, y, z, faceVec=new Vec3(0, 1, * let position = world.getPosition(bot); * await skills.placeBlock(bot, "oak_log", position.x + 1, position.y, position.x, new Vec3(1, 0, 0)); **/ + + // top face: new Vec3(0, 1, 0) + // bottom face: new Vec3(0, -1, 0) + // north face: new Vec3(0, 0, -1) + // south face: new Vec3(0, 0, 1) + // east face: new Vec3(1, 0, 0) + // west face: new Vec3(-1, 0, 0) + let referenceBlock = bot.blockAt(new Vec3(x, y, z)); - if (referenceBlock.name != 'air') + console.log("reference block", referenceBlock.name) + + // check if bot is blocking the block to place + // if (bot.entity.position.distanceTo(referenceBlock.position) < 2) { + // console.log("bot is blocking block to place") + // // move out of the way + // let pos = bot.entity.position; + // let dx = pos.x - referenceBlock.position.x; + // let dz = pos.z - referenceBlock.position.z; + // let moveVec = new Vec3(dx, 0, dz); + // await bot.pathfinder.setMovements(new pf.Movements(bot)); + // bot.pathfinder.setGoal(new pf.goals.GoalBlock(referenceBlock.position.x + moveVec.x, referenceBlock.position.y, referenceBlock.position.z + moveVec.z)); + // } + // all blocks that can be replaced by another block: + let blocks_to_allow = ['air', 'water', 'lava', 'grass', 'tall_grass']; + if (!blocks_to_allow.includes(referenceBlock.name)) { + log(bot, `Block at ${f(x)}, ${f(y)}, ${f(z)} is ${referenceBlock.name} and cannot be replaced.`); return false; + } let block = bot.inventory.items().find(item => item.name === blockType); - if (!block) + if (!block) { + log(bot, `Don't have any ${blockType} to place.`); return false; + } await bot.equip(block, 'hand'); - bot.placeBlock(referenceBlock, faceVec).then(() => { - return true; - }).catch((err) => { - return false; + + // placeblock's callback is broken (always returns error) + bot.placeBlock(referenceBlock, faceVec).catch(err => {}); + + // await to check if block was actually placed + return await new Promise((resolve) => { + setTimeout(() => { + let current = bot.blockAt(new Vec3(x, y, z)); + console.log("Checking current block", current.name); + if (current.name !== blockType) { + console.log('Failed to place block') + log(bot, `Failed to place block ${blockType} at ${f(x)}, ${f(y)}, ${f(z)}, which is ${current.name}.`); + resolve(false); + } else { + console.log('Successfully placed block') + resolve(true); + } + }, 1000); }); } @@ -191,7 +239,13 @@ export async function goToPlayer(bot, username) { bot.pathfinder.setMovements(new pf.Movements(bot)); let pos = player.position; - bot.pathfinder.setGoal(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 3)); + let distance = 2; + await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, distance)); + if (bot.entity.position.distanceTo(pos) > distance+1) { + log(bot, `Failed to reach player ${username} (${bot.entity.position.distanceTo(pos)}m away)`); + return false; + } + log(bot, `You have reached player ${username}.`); return true; } @@ -212,6 +266,7 @@ export async function followPlayer(bot, username) { bot.pathfinder.setMovements(new pf.Movements(bot)); let pos = player.position; bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 3), true); + log(bot, `You are now actively following player ${username}.`); return true; } \ No newline at end of file From 54aa62de88307fbbdcdb01f1d3f3fabca78c2ced Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Sun, 12 Nov 2023 23:19:58 -0600 Subject: [PATCH 2/3] no code return, placeblock more reliable --- agent_code/template.js | 4 +- utils/coder.js | 22 +++++---- utils/history.js | 8 ++-- utils/skills.js | 105 ++++++++++++++++++++--------------------- 4 files changed, 69 insertions(+), 70 deletions(-) diff --git a/agent_code/template.js b/agent_code/template.js index 1892adc..52d0544 100644 --- a/agent_code/template.js +++ b/agent_code/template.js @@ -2,7 +2,9 @@ import * as skills from '../utils/skills.js'; import * as world from '../utils/world.js'; import Vec3 from 'vec3'; +const log = skills.log; + export async function main(bot) { /* CODE HERE */ - return true; // potentially redundant return statement, in case agent doesn't return anything + log(bot, 'Code finished.'); } diff --git a/utils/coder.js b/utils/coder.js index 38fd693..ac4eddc 100644 --- a/utils/coder.js +++ b/utils/coder.js @@ -80,17 +80,19 @@ export class Coder { console.log('executing code...\n'); let execution_file = await import('.'+filename); this.stop(); - let success = await execution_file.main(this.agent.bot); - console.log('code execution finished.', success) - let output = this.agent.bot.output ? 'Code output: \n' + this.agent.bot.output : ''; - // if there is output, add it to the message - if (success) - output += 'Code execution finished successfully.'; - else - output += 'Code execution failed!'; - console.log(output) + await execution_file.main(this.agent.bot); + let output = this.agent.bot.output; + const MAX_OUT = 1000; + if (output.length > MAX_OUT) { + // get the first and last part of the output and combine them with a message in between that says the output was truncated + output = `Code output is very long (${output.length} chars) and has been shortened.\n + First outputs:\n${output.substring(0, MAX_OUT/2)}\n...skipping many lines.\nFinal outputs:\n ${output.substring(output.length - MAX_OUT/2)}`; + } + else { + output = 'Code output:\n' + output; + } this.clear(); - return {success, message: output}; + return {success:true, message: output}; } catch (err) { console.error("Code execution triggered catch:" + err); let message = 'Code output: \n' + this.agent.bot.output + '\n'; diff --git a/utils/history.js b/utils/history.js index 4d38389..33e4ad7 100644 --- a/utils/history.js +++ b/utils/history.js @@ -8,13 +8,13 @@ let history_examples = [ {'role': 'assistant', 'content': 'I see some oak logs, dirt, and cobblestone.'}, {'role': 'user', 'content': 'zZZn98: come here'}, - {'role': 'assistant', 'content': '```// I am going to navigate to zZZn98.\nreturn await skills.goToPlayer(bot, "zZZn98");```'}, + {'role': 'assistant', 'content': '```// I am going to navigate to zZZn98.\nawait skills.goToPlayer(bot, "zZZn98");```'}, {'role': 'system', 'content': 'Code execution finished successfully.'}, {'role': 'assistant', 'content': 'Here!'}, {'role': 'user', 'content': 'hanky: collect some sand for me please'}, {'role': 'assistant', 'content': 'Collecting sand...```// I am going to collect 3 sand and give to hanky.\n\ - await skills.collectBlock(bot, "sand");\nreturn await skills.giveToPlayer(bot, "sand", "hanky");```'}, + await skills.collectBlock(bot, "sand");\nawait skills.giveToPlayer(bot, "sand", "hanky");```'}, {'role': 'system', 'content': 'Code Output:\nYou have reached player hanky.\nCode execution finished successfully.'}, {'role': 'assistant', 'content': 'Here!'}, @@ -23,12 +23,12 @@ let history_examples = [ {'role': 'user', 'content': 'hanky: kill that zombie!'}, {'role': 'assistant', 'content': "I'm attacking! ```//I'm going to attack the nearest zombie.\n\ - return await skills.attackMob(bot, 'zombie');```"}, + await skills.attackMob(bot, 'zombie');```"}, {'role': 'system', 'content': 'Code Output:\nNo zombie nearby\nCode execution failed!'}, {'role': 'assistant', 'content': 'I could not find a zombie nearby.'}, {'role': 'user', 'content': 'billybob: stop what you are doing'}, - {'role': 'assistant', 'content': '```// I am going to write nothing to clear my code\n return true;```'}, + {'role': 'assistant', 'content': '```// I am going to write nothing to clear my code\n```'}, ] diff --git a/utils/skills.js b/utils/skills.js index 5aa5c35..3d0a51a 100644 --- a/utils/skills.js +++ b/utils/skills.js @@ -3,13 +3,10 @@ import { getCraftingTable, getInventoryCounts, getInventoryStacks, getNearbyMobs import pf from 'mineflayer-pathfinder'; import Vec3 from 'vec3'; -function log(bot, message) { +export function log(bot, message) { bot.output += message + '\n'; } -function f(x) { - return Math.floor(x); -} export async function craftItem(bot, itemName) { /** @@ -86,48 +83,53 @@ export async function breakBlockAt(bot, x, y, z) { } -export async function placeBlock(bot, blockType, x, y, z, faceVec=new Vec3(0, 1, 0)) { +export async function placeBlock(bot, blockType, x, y, z) { /** - * Place the given block type at the given position. + * Place the given block type at the given position. It will build off from any adjacent blocks. Will fail if there is a block in the way or nothing to build off of. * @param {MinecraftBot} bot, reference to the minecraft bot. * @param {string} blockType, the type of block to place. * @param {number} x, the x coordinate of the block to place. * @param {number} y, the y coordinate of the block to place. * @param {number} z, the z coordinate of the block to place. - * @param {Vec3} faceVec, the face of the block to place against. Defaults to the top face. * @returns {Promise} true if the block was placed, false otherwise. * @example * let position = world.getPosition(bot); - * await skills.placeBlock(bot, "oak_log", position.x + 1, position.y, position.x, new Vec3(1, 0, 0)); + * await skills.placeBlock(bot, "oak_log", position.x + 1, position.y - 1, position.x); **/ - // top face: new Vec3(0, 1, 0) - // bottom face: new Vec3(0, -1, 0) - // north face: new Vec3(0, 0, -1) - // south face: new Vec3(0, 0, 1) - // east face: new Vec3(1, 0, 0) - // west face: new Vec3(-1, 0, 0) - - let referenceBlock = bot.blockAt(new Vec3(x, y, z)); - console.log("reference block", referenceBlock.name) - - // check if bot is blocking the block to place - // if (bot.entity.position.distanceTo(referenceBlock.position) < 2) { - // console.log("bot is blocking block to place") - // // move out of the way - // let pos = bot.entity.position; - // let dx = pos.x - referenceBlock.position.x; - // let dz = pos.z - referenceBlock.position.z; - // let moveVec = new Vec3(dx, 0, dz); - // await bot.pathfinder.setMovements(new pf.Movements(bot)); - // bot.pathfinder.setGoal(new pf.goals.GoalBlock(referenceBlock.position.x + moveVec.x, referenceBlock.position.y, referenceBlock.position.z + moveVec.z)); - // } - // all blocks that can be replaced by another block: - let blocks_to_allow = ['air', 'water', 'lava', 'grass', 'tall_grass']; - if (!blocks_to_allow.includes(referenceBlock.name)) { - log(bot, `Block at ${f(x)}, ${f(y)}, ${f(z)} is ${referenceBlock.name} and cannot be replaced.`); + const empty_blocks = ['air', 'water', 'lava']; + const targetBlock = bot.blockAt(new Vec3(x, y, z)); + if (!empty_blocks.includes(targetBlock.name)) { + log(bot, `Cannot place block at ${targetBlock.position} because ${targetBlock.name} is in the way.`); return false; } + // get the buildoffblock and facevec based on whichever adjacent block is not empty + let buildOffBlock = null; + let faceVec = null; + const dirs = [Vec3(0, -1, 0), Vec3(0, 1, 0), Vec3(1, 0, 0), Vec3(-1, 0, 0), Vec3(0, 0, 1), Vec3(0, 0, -1)]; + for (let d of dirs) { + const block = bot.blockAt(new Vec3(x, y, z).plus(d)); + if (!empty_blocks.includes(block.name)) { + buildOffBlock = block; + faceVec = new Vec3(-d.x, -d.y, -d.z); + break; + } + } + if (!buildOffBlock) { + log(bot, `Cannot place block at ${targetBlock.position} because there is nothing to build off of.`); + return false; + } + console.log("buildOffBlock: ", buildOffBlock.position, buildOffBlock.name, "faceVec: ", faceVec) + + // check if bot is in the way + console.log("bot position: ", bot.entity.position, "buildOffBlock.position: ", buildOffBlock.position.plus(faceVec), "distance: ", bot.entity.position.distanceTo(buildOffBlock.position.plus(faceVec))) + if (bot.entity.position.distanceTo(buildOffBlock.position.plus(faceVec)) < 0.5) { + log(bot, `Cannot place block at ${buildOffBlock.position} because you are in the way.`); + return false; + } + + console.log("Placing on: ", buildOffBlock.position, buildOffBlock.name) + let block = bot.inventory.items().find(item => item.name === blockType); if (!block) { log(bot, `Don't have any ${blockType} to place.`); @@ -135,24 +137,21 @@ export async function placeBlock(bot, blockType, x, y, z, faceVec=new Vec3(0, 1, } await bot.equip(block, 'hand'); - // placeblock's callback is broken (always returns error) - bot.placeBlock(referenceBlock, faceVec).catch(err => {}); + // can still throw error if blocked by a bot player or mob, but takes a long time to timeout + bot.placeBlock(buildOffBlock, faceVec).catch(err => {console.log('placeBlock threw error, ignoring')}); + console.log("placing block...") - // await to check if block was actually placed - return await new Promise((resolve) => { - setTimeout(() => { - let current = bot.blockAt(new Vec3(x, y, z)); - console.log("Checking current block", current.name); - if (current.name !== blockType) { - console.log('Failed to place block') - log(bot, `Failed to place block ${blockType} at ${f(x)}, ${f(y)}, ${f(z)}, which is ${current.name}.`); - resolve(false); - } else { - console.log('Successfully placed block') - resolve(true); - } - }, 1000); - }); + // wait and then check if the block was placed + await new Promise(resolve => setTimeout(resolve, 500)); + const newBlock = bot.blockAt(buildOffBlock.position.plus(faceVec)); + if (!newBlock) return false; + if (newBlock.name !== blockType) { + log(bot, `Failed to place ${blockType} at ${newBlock.position}.`); + return false; + } + console.log('block placed') + log(bot, `Successfully placed ${blockType} at ${newBlock.position}.`); + return true; } @@ -241,11 +240,7 @@ export async function goToPlayer(bot, username) { let pos = player.position; let distance = 2; await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, distance)); - if (bot.entity.position.distanceTo(pos) > distance+1) { - log(bot, `Failed to reach player ${username} (${bot.entity.position.distanceTo(pos)}m away)`); - return false; - } - log(bot, `You have reached player ${username}.`); + log(bot, `You have reached your destination.`); return true; } From 79d8ade00dea5bc7d8384983e763fa5136a51faa Mon Sep 17 00:00:00 2001 From: MaxRobinsonTheGreat Date: Mon, 13 Nov 2023 11:09:37 -0600 Subject: [PATCH 3/3] abort -> interrupt --- agent.js | 4 ++-- utils/coder.js | 31 +++++++++++++++---------------- utils/skills.js | 4 ++-- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/agent.js b/agent.js index 40590f9..ca9733e 100644 --- a/agent.js +++ b/agent.js @@ -66,8 +66,8 @@ export class Agent { this.coder.queueCode(code); let code_return = await this.coder.execute(); let message = code_return.message; - if (code_return.aborted) - break; + if (code_return.interrupted) + break; // can only be interrupted by another chat, so this chat is over. if (!code_return.success) { message += "\n Write code to fix the problem and try again."; } diff --git a/utils/coder.js b/utils/coder.js index 84d2167..5509e30 100644 --- a/utils/coder.js +++ b/utils/coder.js @@ -6,7 +6,7 @@ export class Coder { this.current_code = ''; this.file_counter = 0; this.fp = './agent_code/'; - this.agent.bot.abort_code = false; + this.agent.bot.interrupt_code = false; this.executing = false; this.agent.bot.output = ''; this.code_template = ''; @@ -30,7 +30,8 @@ export class Coder { return code; } } - code = code.replaceAll(';\n', '; if(bot.abort_code) {log(bot, "Code aborted.");return;}\n'); + // this may cause problems in callback functions + code = code.replaceAll(';\n', '; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}\n'); return code; } @@ -52,10 +53,10 @@ export class Coder { } - + // returns {success: bool, message: string, interrupted: bool} async execute() { - if (!this.current_code) return {success: false, message: "No code to execute."}; - if (!this.code_template) return {success: false, message: "Code template not loaded."}; + if (!this.current_code) return {success: false, message: "No code to execute.", interrupted: false}; + if (!this.code_template) return {success: false, message: "Code template not loaded.", interrupted: false}; let src = ''; for (let line of this.current_code.split('\n')) { src += ` ${line}\n`; @@ -78,7 +79,7 @@ export class Coder { if (write_result) { console.error('Error writing code execution file: ' + result); - return {success: false, message: result}; + return {success: false, message: result, interrupted: false}; } try { @@ -92,22 +93,23 @@ export class Coder { this.agent.bot.emit('finished_executing'); let output = this.formatOutput(this.agent.bot); - let aborted = this.agent.bot.abort_code; + let interrupted = this.agent.bot.interrupt_code; this.clear(); - return {success:true, message: output, aborted}; + return {success:true, message: output, interrupted}; } catch (err) { this.executing = false; this.agent.bot.emit('finished_executing'); console.error("Code execution triggered catch:" + err); let message = this.formatOutput(this.agent.bot); message += '!!Code threw exception!! Error: ' + err; - let aborted = this.agent.bot.abort_code; + let interrupted = this.agent.bot.interrupt_code; await this.stop(); - return {success: false, message, aborted}; + return {success: false, message, interrupted}; } } formatOutput(bot) { + if (bot.interrupt_code) return ''; let output = bot.output; const MAX_OUT = 1000; if (output.length > MAX_OUT) { @@ -117,18 +119,15 @@ export class Coder { else { output = 'Code output:\n' + output; } - if (bot.abort_code) { - output = 'Code was aborted.\n' + output; - } return output; } async stop() { while (this.executing) { - console.log('waiting for code to finish executing... Abort:', this.agent.abort_code); - this.agent.bot.abort_code = true; + this.agent.bot.interrupt_code = true; this.agent.bot.collectBlock.cancelTask(); this.agent.bot.pathfinder.stop(); + console.log('waiting for code to finish executing... interrupt:', this.agent.bot.interrupt_code); await new Promise(resolve => setTimeout(resolve, 1000)); } this.clear(); @@ -137,6 +136,6 @@ export class Coder { clear() { this.current_code = ''; this.agent.bot.output = ''; - this.agent.bot.abort_code = false; + this.agent.bot.interrupt_code = false; } } \ No newline at end of file diff --git a/utils/skills.js b/utils/skills.js index a6cf4fa..d7db856 100644 --- a/utils/skills.js +++ b/utils/skills.js @@ -265,8 +265,8 @@ export async function followPlayer(bot, username) { bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 2), true); log(bot, `You are now actively following player ${username}.`); - while (!bot.abort_code) { - console.log('Waiting for abort...', bot.abort_code); + while (!bot.interrupt_code) { + console.log('followPlayer waiting for interrupt...', bot.interrupt_code); await new Promise(resolve => setTimeout(resolve, 1000)); }