rename task -> action

This commit is contained in:
JurassikLizard 2024-11-03 12:03:12 -05:00
parent 0896907224
commit 25a0f5c069
7 changed files with 67 additions and 68 deletions

View file

@ -1,23 +1,23 @@
export class TaskManager {
export class ActionManager {
constructor(agent) {
this.agent = agent;
this.executing = false;
this.currentTaskLabel = '';
this.currentTaskFn = null;
this.currentActionLabel = '';
this.currentActionFn = null;
this.timedout = false;
this.resume_func = null;
this.resume_name = '';
}
async resumeTask(taskFn, timeout) {
return this._executeResume(taskFn, timeout);
async resumeAction(actionFn, timeout) {
return this._executeResume(actionFn, timeout);
}
async runTask(taskLabel, taskFn, { timeout, resume = false } = {}) {
async runAction(actionLabel, actionFn, { timeout, resume = false } = {}) {
if (resume) {
return this._executeResume(taskFn, timeout);
return this._executeResume(actionFn, timeout);
} else {
return this._executeTask(taskLabel, taskFn, timeout);
return this._executeAction(actionLabel, actionFn, timeout);
}
}
@ -40,31 +40,31 @@ export class TaskManager {
this.resume_name = null;
}
async _executeResume(taskFn = null, timeout = 10) {
const new_resume = taskFn != null;
async _executeResume(actionFn = null, timeout = 10) {
const new_resume = actionFn != null;
if (new_resume) { // start new resume
this.resume_func = taskFn;
this.resume_name = this.currentTaskLabel;
this.resume_func = actionFn;
this.resume_name = this.currentActionLabel;
}
if (this.resume_func != null && this.agent.isIdle() && (!this.agent.self_prompter.on || new_resume)) {
this.currentTaskLabel = this.resume_name;
let res = await this._executeTask(this.resume_name, this.resume_func, timeout);
this.currentTaskLabel = '';
this.currentActionLabel = this.resume_name;
let res = await this._executeAction(this.resume_name, this.resume_func, timeout);
this.currentActionLabel = '';
return res;
} else {
return { success: false, message: null, interrupted: false, timedout: false };
}
}
async _executeTask(taskLabel, taskFn, timeout = 10) {
async _executeAction(actionLabel, actionFn, timeout = 10) {
let TIMEOUT;
try {
console.log('executing code...\n');
// await current task to finish (executing=false), with 10 seconds timeout
// await current action to finish (executing=false), with 10 seconds timeout
// also tell agent.bot to stop various actions
if (this.executing) {
console.log(`new task "${taskLabel}" trying to interrupt current task "${this.currentTaskLabel}"`);
console.log(`new action "${actionLabel}" trying to interrupt current action "${this.currentActionLabel}"`);
}
await this.stop();
@ -72,21 +72,21 @@ export class TaskManager {
this.agent.clearBotLogs();
this.executing = true;
this.currentTaskLabel = taskLabel;
this.currentTaskFn = taskFn;
this.currentActionLabel = actionLabel;
this.currentActionFn = actionFn;
// timeout in minutes
if (timeout > 0) {
TIMEOUT = this._startTimeout(timeout);
}
// start the task
await taskFn();
// start the action
await actionFn();
// mark task as finished + cleanup
// mark action as finished + cleanup
this.executing = false;
this.currentTaskLabel = '';
this.currentTaskFn = null;
this.currentActionLabel = '';
this.currentActionFn = null;
clearTimeout(TIMEOUT);
// get bot activity summary
@ -100,12 +100,12 @@ export class TaskManager {
this.agent.bot.emit('idle');
}
// return task status report
// return action status report
return { success: true, message: output, interrupted, timedout };
} catch (err) {
this.executing = false;
this.currentTaskLabel = '';
this.currentTaskFn = null;
this.currentActionLabel = '';
this.currentActionFn = null;
clearTimeout(TIMEOUT);
this.cancelResume();
console.error("Code execution triggered catch: " + err);

View file

@ -94,7 +94,7 @@ export class Coder {
async generateCode(agent_history) {
// wrapper to prevent overlapping code generation loops
await this.agent.tasks.stop();
await this.agent.actions.stop();
this.generating = true;
let res = await this.generateCodeLoop(agent_history);
this.generating = false;
@ -154,7 +154,7 @@ export class Coder {
return {success: false, message: null, interrupted: false, timedout: false};
}
code_return = await this.agent.tasks.runTask('newAction', async () => {
code_return = await this.agent.actions.runAction('newAction', async () => {
return await executionModuleExports.main(this.agent.bot);
}, { timeout: settings.code_timeout_mins });
if (code_return.interrupted && !code_return.timedout)

View file

@ -1,12 +1,12 @@
import * as skills from '../library/skills.js';
import settings from '../../../settings.js';
function runAsTask (taskLabel, taskFn, resume = false, timeout = -1) {
function runAsAction (taskLabel, actionFn, resume = false, timeout = -1) {
return async function (agent, ...args) {
const taskFnWithAgent = async () => {
await taskFn(agent, ...args);
const actionFnWithAgent = async () => {
await actionFn(agent, ...args);
};
const code_return = await agent.tasks.runTask(`action:${taskLabel}`, taskFnWithAgent, { timeout, resume });
const code_return = await agent.actions.runAction(`action:${taskLabel}`, actionFnWithAgent, { timeout, resume });
if (code_return.interrupted && !code_return.timedout)
return;
return code_return.message;
@ -31,9 +31,9 @@ export const actionsList = [
name: '!stop',
description: 'Force stop all actions and commands that are currently executing.',
perform: async function (agent) {
await agent.tasks.stop();
await agent.actions.stop();
agent.clearBotLogs();
agent.tasks.cancelResume();
agent.actions.cancelResume();
agent.bot.emit('idle');
let msg = 'Agent stopped.';
if (agent.self_prompter.on)
@ -73,7 +73,7 @@ export const actionsList = [
'player_name': {type: 'string', description: 'The name of the player to go to.'},
'closeness': {type: 'float', description: 'How close to get to the player.', domain: [0, Infinity]}
},
perform: runAsTask('goToPlayer', async (agent, player_name, closeness) => {
perform: runAsAction('goToPlayer', async (agent, player_name, closeness) => {
return await skills.goToPlayer(agent.bot, player_name, closeness);
})
},
@ -84,7 +84,7 @@ export const actionsList = [
'player_name': {type: 'string', description: 'name of the player to follow.'},
'follow_dist': {type: 'float', description: 'The distance to follow from.', domain: [0, Infinity]}
},
perform: runAsTask('followPlayer', async (agent, player_name, follow_dist) => {
perform: runAsAction('followPlayer', async (agent, player_name, follow_dist) => {
await skills.followPlayer(agent.bot, player_name, follow_dist);
}, true)
},
@ -96,7 +96,7 @@ export const actionsList = [
'closeness': { type: 'float', description: 'How close to get to the block.', domain: [0, Infinity] },
'search_range': { type: 'float', description: 'The distance to search for the block.', domain: [0, Infinity] }
},
perform: runAsTask('goToBlock', async (agent, type, closeness, range) => {
perform: runAsAction('goToBlock', async (agent, type, closeness, range) => {
await skills.goToNearestBlock(agent.bot, type, closeness, range);
})
},
@ -104,7 +104,7 @@ export const actionsList = [
name: '!moveAway',
description: 'Move away from the current location in any direction by a given distance.',
params: {'distance': { type: 'float', description: 'The distance to move away.', domain: [0, Infinity] }},
perform: runAsTask('moveAway', async (agent, distance) => {
perform: runAsAction('moveAway', async (agent, distance) => {
await skills.moveAway(agent.bot, distance);
})
},
@ -122,7 +122,7 @@ export const actionsList = [
name: '!goToPlace',
description: 'Go to a saved location.',
params: {'name': { type: 'string', description: 'The name of the location to go to.' }},
perform: runAsTask('goToPlace', async (agent, name) => {
perform: runAsAction('goToPlace', async (agent, name) => {
const pos = agent.memory_bank.recallPlace(name);
if (!pos) {
skills.log(agent.bot, `No location named "${name}" saved.`);
@ -139,7 +139,7 @@ export const actionsList = [
'item_name': { type: 'ItemName', description: 'The name of the item to give.' },
'num': { type: 'int', description: 'The number of items to give.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('givePlayer', async (agent, player_name, item_name, num) => {
perform: runAsAction('givePlayer', async (agent, player_name, item_name, num) => {
await skills.giveToPlayer(agent.bot, item_name, player_name, num);
})
},
@ -147,7 +147,7 @@ export const actionsList = [
name: '!consume',
description: 'Eat/drink the given item.',
params: {'item_name': { type: 'ItemName', description: 'The name of the item to consume.' }},
perform: runAsTask('consume', async (agent, item_name) => {
perform: runAsAction('consume', async (agent, item_name) => {
await agent.bot.consume(item_name);
skills.log(agent.bot, `Consumed ${item_name}.`);
})
@ -156,7 +156,7 @@ export const actionsList = [
name: '!equip',
description: 'Equip the given item.',
params: {'item_name': { type: 'ItemName', description: 'The name of the item to equip.' }},
perform: runAsTask('equip', async (agent, item_name) => {
perform: runAsAction('equip', async (agent, item_name) => {
await skills.equip(agent.bot, item_name);
})
},
@ -167,7 +167,7 @@ export const actionsList = [
'item_name': { type: 'ItemName', description: 'The name of the item to put in the chest.' },
'num': { type: 'int', description: 'The number of items to put in the chest.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('putInChest', async (agent, item_name, num) => {
perform: runAsAction('putInChest', async (agent, item_name, num) => {
await skills.putInChest(agent.bot, item_name, num);
})
},
@ -178,7 +178,7 @@ export const actionsList = [
'item_name': { type: 'ItemName', description: 'The name of the item to take.' },
'num': { type: 'int', description: 'The number of items to take.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('takeFromChest', async (agent, item_name, num) => {
perform: runAsAction('takeFromChest', async (agent, item_name, num) => {
await skills.takeFromChest(agent.bot, item_name, num);
})
},
@ -186,7 +186,7 @@ export const actionsList = [
name: '!viewChest',
description: 'View the items/counts of the nearest chest.',
params: { },
perform: runAsTask('viewChest', async (agent) => {
perform: runAsAction('viewChest', async (agent) => {
await skills.viewChest(agent.bot);
})
},
@ -197,7 +197,7 @@ export const actionsList = [
'item_name': { type: 'ItemName', description: 'The name of the item to discard.' },
'num': { type: 'int', description: 'The number of items to discard.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('discard', async (agent, item_name, num) => {
perform: runAsAction('discard', async (agent, item_name, num) => {
const start_loc = agent.bot.entity.position;
await skills.moveAway(agent.bot, 5);
await skills.discard(agent.bot, item_name, num);
@ -211,7 +211,7 @@ export const actionsList = [
'type': { type: 'BlockName', description: 'The block type to collect.' },
'num': { type: 'int', description: 'The number of blocks to collect.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('collectBlocks', async (agent, type, num) => {
perform: runAsAction('collectBlocks', async (agent, type, num) => {
await skills.collectBlock(agent.bot, type, num);
}, false, 10) // 10 minute timeout
},
@ -221,7 +221,7 @@ export const actionsList = [
params: {
'type': { type: 'BlockName', description: 'The block type to collect.' }
},
perform: runAsTask('collectAllBlocks', async (agent, type) => {
perform: runAsAction('collectAllBlocks', async (agent, type) => {
let success = await skills.collectBlock(agent.bot, type, 1);
if (!success)
agent.tasks.cancelResume();
@ -234,7 +234,7 @@ export const actionsList = [
'recipe_name': { type: 'ItemName', description: 'The name of the output item to craft.' },
'num': { type: 'int', description: 'The number of times to craft the recipe. This is NOT the number of output items, as it may craft many more items depending on the recipe.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('craftRecipe', async (agent, recipe_name, num) => {
perform: runAsAction('craftRecipe', async (agent, recipe_name, num) => {
await skills.craftRecipe(agent.bot, recipe_name, num);
})
},
@ -245,7 +245,7 @@ export const actionsList = [
'item_name': { type: 'ItemName', description: 'The name of the input item to smelt.' },
'num': { type: 'int', description: 'The number of times to smelt the item.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsTask('smeltItem', async (agent, item_name, num) => {
perform: runAsAction('smeltItem', async (agent, item_name, num) => {
let response = await skills.smeltItem(agent.bot, item_name, num);
if (response.indexOf('Successfully') !== -1) {
// there is a bug where the bot's inventory is not updated after smelting
@ -258,9 +258,8 @@ export const actionsList = [
{
name: '!clearFurnace',
description: 'Take all items out of the nearest furnace.',
description: 'Take all items out of the nearest furnace.',
params: { },
perform: runAsTask('clearFurnace', async (agent) => {
perform: runAsAction('clearFurnace', async (agent) => {
await skills.clearNearestFurnace(agent.bot);
})
},
@ -268,7 +267,7 @@ export const actionsList = [
name: '!placeHere',
description: 'Place a given block in the current location. Do NOT use to build structures, only use for single blocks/torches.',
params: {'type': { type: 'BlockName', description: 'The block type to place.' }},
perform: runAsTask('placeHere', async (agent, type) => {
perform: runAsAction('placeHere', async (agent, type) => {
let pos = agent.bot.entity.position;
await skills.placeBlock(agent.bot, type, pos.x, pos.y, pos.z);
})
@ -277,14 +276,14 @@ export const actionsList = [
name: '!attack',
description: 'Attack and kill the nearest entity of a given type.',
params: {'type': { type: 'string', description: 'The type of entity to attack.'}},
perform: runAsTask('attack', async (agent, type) => {
perform: runAsAction('attack', async (agent, type) => {
await skills.attackNearest(agent.bot, type, true);
})
},
{
name: '!goToBed',
description: 'Go to the nearest bed and sleep.',
perform: runAsTask('goToBed', async (agent) => {
perform: runAsAction('goToBed', async (agent) => {
await skills.goToBed(agent.bot);
})
},
@ -292,7 +291,7 @@ export const actionsList = [
name: '!activate',
description: 'Activate the nearest object of a given type.',
params: {'type': { type: 'BlockName', description: 'The type of object to activate.' }},
perform: runAsTask('activate', async (agent, type) => {
perform: runAsAction('activate', async (agent, type) => {
await skills.activateNearestBlock(agent.bot, type);
})
},
@ -300,7 +299,7 @@ export const actionsList = [
name: '!stay',
description: 'Stay in the current location no matter what. Pauses all modes.',
params: {'type': { type: 'int', description: 'The number of seconds to stay. -1 for forever.', domain: [-1, Number.MAX_SAFE_INTEGER] }},
perform: runAsTask('stay', async (agent, seconds) => {
perform: runAsAction('stay', async (agent, seconds) => {
await skills.stay(agent.bot, seconds);
})
},

View file

@ -260,7 +260,7 @@ async function execute(mode, agent, func, timeout=-1) {
if (agent.self_prompter.on)
agent.self_prompter.stopLoop();
mode.active = true;
let code_return = await agent.tasks.runTask(`mode:${mode.name}`, async () => {
let code_return = await agent.actions.runAction(`mode:${mode.name}`, async () => {
await func();
}, { timeout });
mode.active = false;
@ -328,7 +328,7 @@ class ModeController {
this.unPauseAll();
}
for (let mode of this.modes_list) {
let interruptible = mode.interrupts.some(i => i === 'all') || mode.interrupts.some(i => `action:${i}` === this.agent.tasks.currentTaskLabel);
let interruptible = mode.interrupts.some(i => i === 'all') || mode.interrupts.some(i => `action:${i}` === this.agent.actions.currentActionLabel);
if (mode.on && !mode.paused && !mode.active && (this.agent.isIdle() || interruptible)) {
await mode.update(this.agent);
}

View file

@ -13,7 +13,7 @@ export class BuildGoal {
async wrapSkill(func) {
if (!this.agent.isIdle())
return false;
let res = await this.agent.tasks.runTask('BuildGoal', func);
let res = await this.agent.actions.runAction('BuildGoal', func);
return !res.interrupted;
}

View file

@ -72,7 +72,7 @@ export class NPCContoller {
if (!this.agent.isIdle()) return;
// Persue goal
if (!this.agent.tasks.resume_func) {
if (!this.agent.actions.resume_func) {
this.executeNext();
this.agent.history.save();
}
@ -104,7 +104,7 @@ export class NPCContoller {
async executeNext() {
if (!this.agent.isIdle()) return;
await this.agent.tasks.runTask('npc:moveAway', async () => {
await this.agent.actions.runAction('npc:moveAway', async () => {
await skills.moveAway(this.agent.bot, 2);
});
@ -114,7 +114,7 @@ export class NPCContoller {
if (building == this.data.home) {
let door_pos = this.getBuildingDoor(building);
if (door_pos) {
await this.agent.tasks.runTask('npc:exitBuilding', async () => {
await this.agent.actions.runAction('npc:exitBuilding', async () => {
await skills.useDoor(this.agent.bot, door_pos);
await skills.moveAway(this.agent.bot, 2); // If the bot is too close to the building it will try to enter again
});
@ -132,13 +132,13 @@ export class NPCContoller {
let building = this.currentBuilding();
if (this.data.home !== null && (building === null || building != this.data.home)) {
let door_pos = this.getBuildingDoor(this.data.home);
await this.agent.tasks.runTask('npc:returnHome', async () => {
await this.agent.actions.runAction('npc:returnHome', async () => {
await skills.useDoor(this.agent.bot, door_pos);
});
}
// Go to bed
await this.agent.tasks.runTask('npc:bed', async () => {
await this.agent.actions.runAction('npc:bed', async () => {
await skills.goToBed(this.agent.bot);
});
}

View file

@ -87,7 +87,7 @@ export class SelfPrompter {
async stop(stop_action=true) {
this.interrupt = true;
if (stop_action)
await this.agent.tasks.stop();
await this.agent.actions.stop();
await this.stopLoop();
this.on = false;
}