separate queued/current code

This commit is contained in:
MaxRobinsonTheGreat 2023-11-15 17:00:02 -06:00
parent 305d233f62
commit b72bf3f697

View file

@ -3,6 +3,7 @@ import { writeFile, readFile, unlink } from 'fs';
export class Coder { export class Coder {
constructor(agent) { constructor(agent) {
this.agent = agent; this.agent = agent;
this.queued_code = '';
this.current_code = ''; this.current_code = '';
this.file_counter = 0; this.file_counter = 0;
this.fp = './agent_code/'; this.fp = './agent_code/';
@ -19,7 +20,7 @@ export class Coder {
} }
queueCode(code) { queueCode(code) {
this.current_code = this.santitizeCode(code); this.queued_code = this.santitizeCode(code);
} }
santitizeCode(code) { santitizeCode(code) {
@ -30,15 +31,9 @@ export class Coder {
return code; return code;
} }
} }
// this may cause problems in callback functions
code = code.replaceAll(';\n', '; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}\n');
return code; return code;
} }
hasCode() {
return this.current_code.length > 0;
}
writeFilePromise(filename, src) { writeFilePromise(filename, src) {
// makes it so we can await this function // makes it so we can await this function
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -55,10 +50,12 @@ export class Coder {
// returns {success: bool, message: string, interrupted: bool} // returns {success: bool, message: string, interrupted: bool}
async execute() { async execute() {
if (!this.current_code) return {success: false, message: "No code to execute.", interrupted: false}; if (!this.queued_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}; if (!this.code_template) return {success: false, message: "Code template not loaded.", interrupted: false};
let src = ''; let src = '';
for (let line of this.current_code.split('\n')) { // this may cause problems in callback functions
let code = this.queued_code.replaceAll(';\n', '; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}\n');
for (let line of code.split('\n')) {
src += ` ${line}\n`; src += ` ${line}\n`;
} }
src = this.code_template.replace('/* CODE HERE */', src); src = this.code_template.replace('/* CODE HERE */', src);
@ -86,6 +83,7 @@ export class Coder {
console.log('executing code...\n'); console.log('executing code...\n');
let execution_file = await import('.'+filename); let execution_file = await import('.'+filename);
await this.stop(); await this.stop();
this.current_code = this.queued_code;
this.executing = true; this.executing = true;
await execution_file.main(this.agent.bot); await execution_file.main(this.agent.bot);