From a6971a4c4a09671b3caf39e0fbcd33b39bee3e6e Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Thu, 25 Jan 2024 15:52:07 -0800 Subject: [PATCH] removed idle --- src/agent/agent.js | 1 - src/agent/coder.js | 17 +++++------------ src/agent/commands/actions.js | 9 +-------- src/agent/commands/queries.js | 9 +-------- src/agent/modes.js | 13 +++++-------- 5 files changed, 12 insertions(+), 37 deletions(-) diff --git a/src/agent/agent.js b/src/agent/agent.js index b6bc8d5..2b486a3 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -21,7 +21,6 @@ export class Agent { this.bot = initBot(name); initModes(this); - this.idle = true; this.bot.on('login', async () => { diff --git a/src/agent/coder.js b/src/agent/coder.js index 3c47e98..a5d19d4 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -7,7 +7,6 @@ import { Examples } from '../utils/examples.js'; export class Coder { constructor(agent) { this.agent = agent; - this.current_code = ''; this.file_counter = 0; this.fp = '/bots/'+agent.name+'/action-code/'; this.executing = false; @@ -59,7 +58,6 @@ export class Coder { console.error('Error writing code execution file: ' + result); return null; } - this.current_code = code; return await import('../..' + this.fp + filename); } @@ -148,8 +146,8 @@ export class Coder { if (this.agent.bot.interrupt_code) return; } - - return + + return; } // returns {success: bool, message: string, interrupted: bool, timedout: false} @@ -173,19 +171,15 @@ 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; - clearTimeout(TIMEOUT); - console.error("Code execution triggered catch: " + err); + clearTimeout(TIMEOUT); await this.stop(); - let message = this.formatOutput(this.agent.bot); - message += '!!Code threw exception!! Error: ' + err; + + let message = this.formatOutput(this.agent.bot) + '!!Code threw exception!! Error: ' + err; let interrupted = this.agent.bot.interrupt_code; this.clear(); - this.agent.bot.emit("code_terminated"); return {success: false, message, interrupted, timedout: false}; } } @@ -217,7 +211,6 @@ export class Coder { } clear() { - this.current_code = ''; this.agent.bot.output = ''; this.agent.bot.interrupt_code = false; this.timedout = false; diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index f3d5ff8..2bb9cb8 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -3,13 +3,11 @@ import * as skills from '../library/skills.js'; function wrapExecution(func, timeout=-1) { return async function (agent, ...args) { - agent.idle = false; let code_return = await agent.coder.execute(async () => { await func(agent, ...args); }, timeout); if (code_return.interrupted && !code_return.timedout) return; - agent.idle = true; return code_return.message; } } @@ -19,11 +17,7 @@ export const actionsList = [ name: '!newAction', description: 'Perform new and unknown custom behaviors that are not available as a command by writing code.', perform: async function (agent) { - agent.idle = false; - let res = await agent.coder.generateCode(agent.history); - agent.idle = true; - if (res) - return '\n' + res + '\n'; + await agent.coder.generateCode(agent.history); } }, { @@ -32,7 +26,6 @@ export const actionsList = [ perform: async function (agent) { await agent.coder.stop(); agent.coder.clear(); - agent.idle = true; return 'Agent stopped.'; } }, diff --git a/src/agent/commands/queries.js b/src/agent/commands/queries.js index 74da90b..ec79474 100644 --- a/src/agent/commands/queries.js +++ b/src/agent/commands/queries.js @@ -117,12 +117,5 @@ export const queryList = [ perform: function (agent) { return agent.bot.modes.getStr(); } - }, - { - name: "!currentAction", - description: "Get the currently executing code.", - perform: function (agent) { - return pad("Current code:\n`" + agent.coder.current_code +"`"); - } - }, + } ]; diff --git a/src/agent/modes.js b/src/agent/modes.js index 60e21f0..f6add51 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.idle) { + if (!agent.coder.executing) { 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.idle) { + if (!agent.coder.executing) { 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.idle) { + if (!agent.coder.executing) { // 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.idle) { + if (!agent.coder.executing) { this.active = true; const entity = agent.bot.nearestEntity(); let entity_in_view = entity && entity.position.distanceTo(agent.bot.entity.position) < 10 && entity.name !== 'enderman'; @@ -131,13 +131,10 @@ const modes = [ async function execute(mode, agent, func, timeout=-1) { mode.active = true; - await agent.coder.stop(); - agent.idle = false; let code_return = await agent.coder.execute(async () => { await func(); }, timeout); mode.active = false; - agent.idle = true; console.log(`Mode ${mode.name} finished executing, code_return: ${code_return.message}`); } @@ -177,7 +174,7 @@ class ModeController { } update() { - if (this.agent.idle) { + if (!this.agent.coder.executing) { // other actions might pause a mode to override it // when idle, unpause all modes for (let mode of this.modes_list) {