abort -> interrupt

This commit is contained in:
MaxRobinsonTheGreat 2023-11-13 11:09:37 -06:00
parent f32d30e9b3
commit 79d8ade00d
3 changed files with 19 additions and 20 deletions

View file

@ -66,8 +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.interrupted)
break; // can only be interrupted by another chat, so this chat is over.
if (!code_return.success) {
message += "\n Write code to fix the problem and try again.";
}

View file

@ -6,7 +6,7 @@ export class Coder {
this.current_code = '';
this.file_counter = 0;
this.fp = './agent_code/';
this.agent.bot.abort_code = false;
this.agent.bot.interrupt_code = false;
this.executing = false;
this.agent.bot.output = '';
this.code_template = '';
@ -30,7 +30,8 @@ export class Coder {
return code;
}
}
code = code.replaceAll(';\n', '; if(bot.abort_code) {log(bot, "Code aborted.");return;}\n');
// this may cause problems in callback functions
code = code.replaceAll(';\n', '; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}\n');
return code;
}
@ -52,10 +53,10 @@ export class Coder {
}
// returns {success: bool, message: string, interrupted: bool}
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."};
if (!this.current_code) return {success: false, message: "No code to execute.", interrupted: false};
if (!this.code_template) return {success: false, message: "Code template not loaded.", interrupted: false};
let src = '';
for (let line of this.current_code.split('\n')) {
src += ` ${line}\n`;
@ -78,7 +79,7 @@ export class Coder {
if (write_result) {
console.error('Error writing code execution file: ' + result);
return {success: false, message: result};
return {success: false, message: result, interrupted: false};
}
try {
@ -92,22 +93,23 @@ export class Coder {
this.agent.bot.emit('finished_executing');
let output = this.formatOutput(this.agent.bot);
let aborted = this.agent.bot.abort_code;
let interrupted = this.agent.bot.interrupt_code;
this.clear();
return {success:true, message: output, aborted};
return {success:true, message: output, interrupted};
} 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;
let interrupted = this.agent.bot.interrupt_code;
await this.stop();
return {success: false, message, aborted};
return {success: false, message, interrupted};
}
}
formatOutput(bot) {
if (bot.interrupt_code) return '';
let output = bot.output;
const MAX_OUT = 1000;
if (output.length > MAX_OUT) {
@ -117,18 +119,15 @@ export class Coder {
else {
output = 'Code output:\n' + output;
}
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.interrupt_code = true;
this.agent.bot.collectBlock.cancelTask();
this.agent.bot.pathfinder.stop();
console.log('waiting for code to finish executing... interrupt:', this.agent.bot.interrupt_code);
await new Promise(resolve => setTimeout(resolve, 1000));
}
this.clear();
@ -137,6 +136,6 @@ export class Coder {
clear() {
this.current_code = '';
this.agent.bot.output = '';
this.agent.bot.abort_code = false;
this.agent.bot.interrupt_code = false;
}
}

View file

@ -265,8 +265,8 @@ export async function followPlayer(bot, username) {
bot.pathfinder.setGoal(new pf.goals.GoalFollow(player, 2), true);
log(bot, `You are now actively following player ${username}.`);
while (!bot.abort_code) {
console.log('Waiting for abort...', bot.abort_code);
while (!bot.interrupt_code) {
console.log('followPlayer waiting for interrupt...', bot.interrupt_code);
await new Promise(resolve => setTimeout(resolve, 1000));
}