mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-08-23 07:33:45 +02:00
Merge branch 'main' into code-output
This commit is contained in:
commit
f32d30e9b3
4 changed files with 77 additions and 29 deletions
15
agent.js
15
agent.js
|
@ -30,6 +30,14 @@ export class Agent {
|
||||||
|
|
||||||
this.respond(username, message);
|
this.respond(username, message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.bot.on('finished_executing', () => {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!this.coder.executing) {
|
||||||
|
// return to default behavior
|
||||||
|
}
|
||||||
|
}, 10000);
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async respond(username, message) {
|
async respond(username, message) {
|
||||||
|
@ -42,12 +50,14 @@ export class Agent {
|
||||||
let message = res.substring(0, res.indexOf(query_cmd)).trim();
|
let message = res.substring(0, res.indexOf(query_cmd)).trim();
|
||||||
if (message)
|
if (message)
|
||||||
this.bot.chat(message);
|
this.bot.chat(message);
|
||||||
console.log('Agent used query:', query_cmd);
|
|
||||||
let query = getQuery(query_cmd);
|
let query = getQuery(query_cmd);
|
||||||
let query_res = query.perform(this);
|
let query_res = query.perform(this);
|
||||||
|
console.log('Agent used query:', query_cmd, 'and got:', query_res)
|
||||||
this.history.add('system', query_res);
|
this.history.add('system', query_res);
|
||||||
}
|
}
|
||||||
else if (containsCodeBlock(res)) { // contains code block
|
else if (containsCodeBlock(res)) { // contains code block
|
||||||
|
console.log('Agent is executing code:', res)
|
||||||
|
|
||||||
let message = res.substring(0, res.indexOf('```')).trim();
|
let message = res.substring(0, res.indexOf('```')).trim();
|
||||||
if (message)
|
if (message)
|
||||||
this.bot.chat(message);
|
this.bot.chat(message);
|
||||||
|
@ -56,6 +66,8 @@ export class Agent {
|
||||||
this.coder.queueCode(code);
|
this.coder.queueCode(code);
|
||||||
let code_return = await this.coder.execute();
|
let code_return = await this.coder.execute();
|
||||||
let message = code_return.message;
|
let message = code_return.message;
|
||||||
|
if (code_return.aborted)
|
||||||
|
break;
|
||||||
if (!code_return.success) {
|
if (!code_return.success) {
|
||||||
message += "\n Write code to fix the problem and try again.";
|
message += "\n Write code to fix the problem and try again.";
|
||||||
}
|
}
|
||||||
|
@ -65,6 +77,7 @@ export class Agent {
|
||||||
}
|
}
|
||||||
else { // conversation response
|
else { // conversation response
|
||||||
this.bot.chat(res);
|
this.bot.chat(res);
|
||||||
|
console.log('Purely conversational response:', res)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ export class Coder {
|
||||||
this.current_code = '';
|
this.current_code = '';
|
||||||
this.file_counter = 0;
|
this.file_counter = 0;
|
||||||
this.fp = './agent_code/';
|
this.fp = './agent_code/';
|
||||||
|
this.agent.bot.abort_code = false;
|
||||||
|
this.executing = false;
|
||||||
this.agent.bot.output = '';
|
this.agent.bot.output = '';
|
||||||
this.code_template = '';
|
this.code_template = '';
|
||||||
|
|
||||||
|
@ -28,6 +30,7 @@ export class Coder {
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
code = code.replaceAll(';\n', '; if(bot.abort_code) {log(bot, "Code aborted.");return;}\n');
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +51,8 @@ export class Coder {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
async execute() {
|
async execute() {
|
||||||
if (!this.current_code) return {success: false, message: "No code to 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.code_template) return {success: false, message: "Code template not loaded."};
|
||||||
|
@ -79,36 +84,59 @@ export class Coder {
|
||||||
try {
|
try {
|
||||||
console.log('executing code...\n');
|
console.log('executing code...\n');
|
||||||
let execution_file = await import('.'+filename);
|
let execution_file = await import('.'+filename);
|
||||||
this.stop();
|
await this.stop();
|
||||||
|
|
||||||
|
this.executing = true;
|
||||||
await execution_file.main(this.agent.bot);
|
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;
|
const MAX_OUT = 1000;
|
||||||
if (output.length > MAX_OUT) {
|
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
|
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)}`;
|
First outputs:\n${output.substring(0, MAX_OUT/2)}\n...skipping many lines.\nFinal outputs:\n ${output.substring(output.length - MAX_OUT/2)}`;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
output = 'Code output:\n' + output;
|
output = 'Code output:\n' + output;
|
||||||
}
|
}
|
||||||
this.clear();
|
if (bot.abort_code) {
|
||||||
return {success:true, message: output};
|
output = 'Code was aborted.\n' + 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};
|
|
||||||
}
|
}
|
||||||
|
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() {
|
clear() {
|
||||||
this.current_code = '';
|
this.current_code = '';
|
||||||
this.agent.bot.output = '';
|
this.agent.bot.output = '';
|
||||||
}
|
this.agent.bot.abort_code = false;
|
||||||
|
|
||||||
stop() {
|
|
||||||
this.clear();
|
|
||||||
this.agent.bot.pathfinder.setGoal(null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -27,8 +27,8 @@ let history_examples = [
|
||||||
{'role': 'system', 'content': 'Code Output:\nNo zombie nearby\nCode execution failed!'},
|
{'role': 'system', 'content': 'Code Output:\nNo zombie nearby\nCode execution failed!'},
|
||||||
{'role': 'assistant', 'content': 'I could not find a zombie nearby.'},
|
{'role': 'assistant', 'content': 'I could not find a zombie nearby.'},
|
||||||
|
|
||||||
{'role': 'user', 'content': 'billybob: stop what you are doing'},
|
{'role': 'user', 'content': 'billybob: stop'},
|
||||||
{'role': 'assistant', 'content': '```// I am going to write nothing to clear my code\n```'},
|
{'role': 'assistant', 'content': '```// I am going to write empty code to stop whatever I am doing\n```'},
|
||||||
|
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -137,6 +137,9 @@ export async function placeBlock(bot, blockType, x, y, z) {
|
||||||
}
|
}
|
||||||
await bot.equip(block, 'hand');
|
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
|
// 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')});
|
bot.placeBlock(buildOffBlock, faceVec).catch(err => {console.log('placeBlock threw error, ignoring')});
|
||||||
console.log("placing block...")
|
console.log("placing block...")
|
||||||
|
@ -195,7 +198,7 @@ export async function goToPosition(bot, x, y, z) {
|
||||||
if (z == null) z = bot.entity.position.z;
|
if (z == null) z = bot.entity.position.z;
|
||||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||||
let pos = { x: x, y: y, z: z };
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,7 +218,7 @@ export async function giveToPlayer(bot, itemType, username) {
|
||||||
return false;
|
return false;
|
||||||
if (getInventoryCounts(bot)[itemType] == 0)
|
if (getInventoryCounts(bot)[itemType] == 0)
|
||||||
return false;
|
return false;
|
||||||
goToPlayer(bot, username);
|
await goToPlayer(bot, username);
|
||||||
let pos = player.position;
|
let pos = player.position;
|
||||||
await bot.lookAt(pos);
|
await bot.lookAt(pos);
|
||||||
await bot.toss(getItemId(itemType), null, 1);
|
await bot.toss(getItemId(itemType), null, 1);
|
||||||
|
@ -247,7 +250,7 @@ export async function goToPlayer(bot, username) {
|
||||||
|
|
||||||
export async function followPlayer(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 {MinecraftBot} bot, reference to the minecraft bot.
|
||||||
* @param {string} username, the username of the player to follow.
|
* @param {string} username, the username of the player to follow.
|
||||||
* @returns {Promise<boolean>} true if the player was found, false otherwise.
|
* @returns {Promise<boolean>} true if the player was found, false otherwise.
|
||||||
|
@ -259,9 +262,13 @@ export async function followPlayer(bot, username) {
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
bot.pathfinder.setMovements(new pf.Movements(bot));
|
bot.pathfinder.setMovements(new pf.Movements(bot));
|
||||||
let pos = player.position;
|
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 2), true);
|
||||||
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 3), true);
|
|
||||||
log(bot, `You are now actively following player ${username}.`);
|
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;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue