mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-05 06:45:35 +02:00
removed idle
This commit is contained in:
parent
546d5e7cef
commit
a6971a4c4a
5 changed files with 12 additions and 37 deletions
|
@ -21,7 +21,6 @@ export class Agent {
|
||||||
this.bot = initBot(name);
|
this.bot = initBot(name);
|
||||||
|
|
||||||
initModes(this);
|
initModes(this);
|
||||||
this.idle = true;
|
|
||||||
|
|
||||||
this.bot.on('login', async () => {
|
this.bot.on('login', async () => {
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import { Examples } from '../utils/examples.js';
|
||||||
export class Coder {
|
export class Coder {
|
||||||
constructor(agent) {
|
constructor(agent) {
|
||||||
this.agent = agent;
|
this.agent = agent;
|
||||||
this.current_code = '';
|
|
||||||
this.file_counter = 0;
|
this.file_counter = 0;
|
||||||
this.fp = '/bots/'+agent.name+'/action-code/';
|
this.fp = '/bots/'+agent.name+'/action-code/';
|
||||||
this.executing = false;
|
this.executing = false;
|
||||||
|
@ -59,7 +58,6 @@ export class Coder {
|
||||||
console.error('Error writing code execution file: ' + result);
|
console.error('Error writing code execution file: ' + result);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
this.current_code = code;
|
|
||||||
return await import('../..' + this.fp + filename);
|
return await import('../..' + this.fp + filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,8 +146,8 @@ export class Coder {
|
||||||
if (this.agent.bot.interrupt_code)
|
if (this.agent.bot.interrupt_code)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns {success: bool, message: string, interrupted: bool, timedout: false}
|
// returns {success: bool, message: string, interrupted: bool, timedout: false}
|
||||||
|
@ -173,19 +171,15 @@ export class Coder {
|
||||||
let interrupted = this.agent.bot.interrupt_code;
|
let interrupted = this.agent.bot.interrupt_code;
|
||||||
let timedout = this.timedout;
|
let timedout = this.timedout;
|
||||||
this.clear();
|
this.clear();
|
||||||
this.agent.bot.emit("code_terminated");
|
|
||||||
return {success:true, message: output, interrupted, timedout};
|
return {success:true, message: output, interrupted, timedout};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.executing = false;
|
|
||||||
clearTimeout(TIMEOUT);
|
|
||||||
|
|
||||||
console.error("Code execution triggered catch: " + err);
|
console.error("Code execution triggered catch: " + err);
|
||||||
|
clearTimeout(TIMEOUT);
|
||||||
await this.stop();
|
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;
|
let interrupted = this.agent.bot.interrupt_code;
|
||||||
this.clear();
|
this.clear();
|
||||||
this.agent.bot.emit("code_terminated");
|
|
||||||
return {success: false, message, interrupted, timedout: false};
|
return {success: false, message, interrupted, timedout: false};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,7 +211,6 @@ export class Coder {
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
this.current_code = '';
|
|
||||||
this.agent.bot.output = '';
|
this.agent.bot.output = '';
|
||||||
this.agent.bot.interrupt_code = false;
|
this.agent.bot.interrupt_code = false;
|
||||||
this.timedout = false;
|
this.timedout = false;
|
||||||
|
|
|
@ -3,13 +3,11 @@ import * as skills from '../library/skills.js';
|
||||||
|
|
||||||
function wrapExecution(func, timeout=-1) {
|
function wrapExecution(func, timeout=-1) {
|
||||||
return async function (agent, ...args) {
|
return async function (agent, ...args) {
|
||||||
agent.idle = false;
|
|
||||||
let code_return = await agent.coder.execute(async () => {
|
let code_return = await agent.coder.execute(async () => {
|
||||||
await func(agent, ...args);
|
await func(agent, ...args);
|
||||||
}, timeout);
|
}, timeout);
|
||||||
if (code_return.interrupted && !code_return.timedout)
|
if (code_return.interrupted && !code_return.timedout)
|
||||||
return;
|
return;
|
||||||
agent.idle = true;
|
|
||||||
return code_return.message;
|
return code_return.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,11 +17,7 @@ export const actionsList = [
|
||||||
name: '!newAction',
|
name: '!newAction',
|
||||||
description: 'Perform new and unknown custom behaviors that are not available as a command by writing code.',
|
description: 'Perform new and unknown custom behaviors that are not available as a command by writing code.',
|
||||||
perform: async function (agent) {
|
perform: async function (agent) {
|
||||||
agent.idle = false;
|
await agent.coder.generateCode(agent.history);
|
||||||
let res = await agent.coder.generateCode(agent.history);
|
|
||||||
agent.idle = true;
|
|
||||||
if (res)
|
|
||||||
return '\n' + res + '\n';
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -32,7 +26,6 @@ export const actionsList = [
|
||||||
perform: async function (agent) {
|
perform: async function (agent) {
|
||||||
await agent.coder.stop();
|
await agent.coder.stop();
|
||||||
agent.coder.clear();
|
agent.coder.clear();
|
||||||
agent.idle = true;
|
|
||||||
return 'Agent stopped.';
|
return 'Agent stopped.';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -117,12 +117,5 @@ export const queryList = [
|
||||||
perform: function (agent) {
|
perform: function (agent) {
|
||||||
return agent.bot.modes.getStr();
|
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 +"`");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -34,7 +34,7 @@ const modes = [
|
||||||
on: true,
|
on: true,
|
||||||
active: false,
|
active: false,
|
||||||
update: function (agent) {
|
update: function (agent) {
|
||||||
if (agent.idle) {
|
if (!agent.coder.executing) {
|
||||||
const huntable = world.getNearestEntityWhere(agent.bot, entity => mc.isHuntable(entity), 8);
|
const huntable = world.getNearestEntityWhere(agent.bot, entity => mc.isHuntable(entity), 8);
|
||||||
if (huntable) {
|
if (huntable) {
|
||||||
execute(this, agent, async () => {
|
execute(this, agent, async () => {
|
||||||
|
@ -51,7 +51,7 @@ const modes = [
|
||||||
on: true,
|
on: true,
|
||||||
active: false,
|
active: false,
|
||||||
update: function (agent) {
|
update: function (agent) {
|
||||||
if (agent.idle) {
|
if (!agent.coder.executing) {
|
||||||
let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8);
|
let item = world.getNearestEntityWhere(agent.bot, entity => entity.name === 'item', 8);
|
||||||
if (item) {
|
if (item) {
|
||||||
execute(this, agent, async () => {
|
execute(this, agent, async () => {
|
||||||
|
@ -70,7 +70,7 @@ const modes = [
|
||||||
active: false,
|
active: false,
|
||||||
update: function (agent) {
|
update: function (agent) {
|
||||||
if (this.active) return;
|
if (this.active) return;
|
||||||
if (agent.idle) {
|
if (!agent.coder.executing) {
|
||||||
// TODO: check light level instead of nearby torches, block.light is broken
|
// TODO: check light level instead of nearby torches, block.light is broken
|
||||||
const near_torch = world.getNearestBlock(agent.bot, 'torch', 8);
|
const near_torch = world.getNearestBlock(agent.bot, 'torch', 8);
|
||||||
if (!near_torch) {
|
if (!near_torch) {
|
||||||
|
@ -96,7 +96,7 @@ const modes = [
|
||||||
last_entity: null,
|
last_entity: null,
|
||||||
next_change: 0,
|
next_change: 0,
|
||||||
update: function (agent) {
|
update: function (agent) {
|
||||||
if (agent.idle) {
|
if (!agent.coder.executing) {
|
||||||
this.active = true;
|
this.active = true;
|
||||||
const entity = agent.bot.nearestEntity();
|
const entity = agent.bot.nearestEntity();
|
||||||
let entity_in_view = entity && entity.position.distanceTo(agent.bot.entity.position) < 10 && entity.name !== 'enderman';
|
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) {
|
async function execute(mode, agent, func, timeout=-1) {
|
||||||
mode.active = true;
|
mode.active = true;
|
||||||
await agent.coder.stop();
|
|
||||||
agent.idle = false;
|
|
||||||
let code_return = await agent.coder.execute(async () => {
|
let code_return = await agent.coder.execute(async () => {
|
||||||
await func();
|
await func();
|
||||||
}, timeout);
|
}, timeout);
|
||||||
mode.active = false;
|
mode.active = false;
|
||||||
agent.idle = true;
|
|
||||||
console.log(`Mode ${mode.name} finished executing, code_return: ${code_return.message}`);
|
console.log(`Mode ${mode.name} finished executing, code_return: ${code_return.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,7 +174,7 @@ class ModeController {
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
if (this.agent.idle) {
|
if (!this.agent.coder.executing) {
|
||||||
// other actions might pause a mode to override it
|
// other actions might pause a mode to override it
|
||||||
// when idle, unpause all modes
|
// when idle, unpause all modes
|
||||||
for (let mode of this.modes_list) {
|
for (let mode of this.modes_list) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue