Merge branch 'main' into code-output

This commit is contained in:
MaxRobinsonTheGreat 2023-11-13 00:57:20 -06:00
commit f32d30e9b3
4 changed files with 77 additions and 29 deletions

View file

@ -30,6 +30,14 @@ export class Agent {
this.respond(username, message);
});
this.bot.on('finished_executing', () => {
setTimeout(() => {
if (!this.coder.executing) {
// return to default behavior
}
}, 10000);
})
}
async respond(username, message) {
@ -42,12 +50,14 @@ export class Agent {
let message = res.substring(0, res.indexOf(query_cmd)).trim();
if (message)
this.bot.chat(message);
console.log('Agent used query:', query_cmd);
let query = getQuery(query_cmd);
let query_res = query.perform(this);
console.log('Agent used query:', query_cmd, 'and got:', query_res)
this.history.add('system', query_res);
}
else if (containsCodeBlock(res)) { // contains code block
console.log('Agent is executing code:', res)
let message = res.substring(0, res.indexOf('```')).trim();
if (message)
this.bot.chat(message);
@ -56,6 +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.success) {
message += "\n Write code to fix the problem and try again.";
}
@ -65,6 +77,7 @@ export class Agent {
}
else { // conversation response
this.bot.chat(res);
console.log('Purely conversational response:', res)
break;
}
}

View file

@ -6,6 +6,8 @@ export class Coder {
this.current_code = '';
this.file_counter = 0;
this.fp = './agent_code/';
this.agent.bot.abort_code = false;
this.executing = false;
this.agent.bot.output = '';
this.code_template = '';
@ -28,6 +30,7 @@ export class Coder {
return code;
}
}
code = code.replaceAll(';\n', '; if(bot.abort_code) {log(bot, "Code aborted.");return;}\n');
return code;
}
@ -48,6 +51,8 @@ export class Coder {
});
}
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."};
@ -79,36 +84,59 @@ export class Coder {
try {
console.log('executing code...\n');
let execution_file = await import('.'+filename);
this.stop();
await this.stop();
this.executing = true;
await execution_file.main(this.agent.bot);
let output = this.agent.bot.output;
this.executing = false;
this.agent.bot.emit('finished_executing');
let output = this.formatOutput(this.agent.bot);
let aborted = this.agent.bot.abort_code;
this.clear();
return {success:true, message: output, aborted};
} 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;
await this.stop();
return {success: false, message, aborted};
}
}
formatOutput(bot) {
let output = 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:true, message: output};
} catch (err) {
console.error("Code execution triggered catch:" + err);
let message = 'Code output: \n' + this.agent.bot.output + '\n';
message += '!!Code threw exception!! Error: ' + err;
this.stop();
return {success: false, message};
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.collectBlock.cancelTask();
this.agent.bot.pathfinder.stop();
await new Promise(resolve => setTimeout(resolve, 1000));
}
this.clear();
}
clear() {
this.current_code = '';
this.agent.bot.output = '';
}
stop() {
this.clear();
this.agent.bot.pathfinder.setGoal(null);
this.agent.bot.abort_code = false;
}
}

View file

@ -27,8 +27,8 @@ let history_examples = [
{'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```'},
{'role': 'user', 'content': 'billybob: stop'},
{'role': 'assistant', 'content': '```// I am going to write empty code to stop whatever I am doing\n```'},
]

View file

@ -137,6 +137,9 @@ export async function placeBlock(bot, blockType, x, y, z) {
}
await bot.equip(block, 'hand');
// turn to face the block
await bot.lookAt(buildOffBlock.position.plus(faceVec));
// 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...")
@ -195,7 +198,7 @@ export async function goToPosition(bot, x, y, z) {
if (z == null) z = bot.entity.position.z;
bot.pathfinder.setMovements(new pf.Movements(bot));
let pos = { x: x, y: y, z: z };
bot.pathfinder.setGoal(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 1));
await bot.pathfinder.goto(new pf.goals.GoalNear(pos.x, pos.y, pos.z, 1));
return true;
}
@ -215,7 +218,7 @@ export async function giveToPlayer(bot, itemType, username) {
return false;
if (getInventoryCounts(bot)[itemType] == 0)
return false;
goToPlayer(bot, username);
await goToPlayer(bot, username);
let pos = player.position;
await bot.lookAt(pos);
await bot.toss(getItemId(itemType), null, 1);
@ -247,7 +250,7 @@ export async function goToPlayer(bot, username) {
export async function followPlayer(bot, username) {
/**
* Follow the given player endlessly.
* Follow the given player endlessly. Will not return until the code is manually stopped.
* @param {MinecraftBot} bot, reference to the minecraft bot.
* @param {string} username, the username of the player to follow.
* @returns {Promise<boolean>} true if the player was found, false otherwise.
@ -259,9 +262,13 @@ export async function followPlayer(bot, username) {
return false;
bot.pathfinder.setMovements(new pf.Movements(bot));
let pos = player.position;
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 3), true);
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 2), true);
log(bot, `You are now actively following player ${username}.`);
return true;
while (!bot.abort_code) {
console.log('Waiting for abort...', bot.abort_code);
await new Promise(resolve => setTimeout(resolve, 1000));
}
return true;
}