diff --git a/src/agent/action_manager.js b/src/agent/action_manager.js index 833f3c0..5133a6b 100644 --- a/src/agent/action_manager.js +++ b/src/agent/action_manager.js @@ -111,7 +111,9 @@ export class ActionManager { console.error("Code execution triggered catch: " + err); await this.stop(); - let message = this._getBotOutputSummary() + '!!Code threw exception!! Error: ' + err; + err = err.toString(); + let relevant_skill_docs = await this.agent.prompter.getRelevantSkillDocs(err,5); + let message = this._getBotOutputSummary() + '!!Code threw exception!! Error: ' + err+'\n'+relevant_skill_docs; let interrupted = this.agent.bot.interrupt_code; this.agent.clearBotLogs(); if (!interrupted && !this.agent.coder.generating) { diff --git a/src/agent/coder.js b/src/agent/coder.js index 36742d7..a4c18f6 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -27,10 +27,28 @@ export class Coder { } async checkCode(code) { + let result = '#### CODE ERROR INFO ###\n'; + // Extract everything in the code between the beginning of 'skills./world.' and the '(' + const skillRegex = /(?:skills|world)\.(.*?)\(/g; + const skills = []; + let match; + while ((match = skillRegex.exec(code)) !== null) { + skills.push(match[1]); + } + const allDocs = await this.agent.prompter.getRelevantSkillDocs(); + //Check if the function exists + const missingSkills = skills.filter(skill => !allDocs.includes(skill)); + if (missingSkills.length > 0) { + result += 'These functions do not exist. Please modify the correct function name and try again.\n'; + result += '### FUNCTIONS NOT FOUND ###\n'; + result += missingSkills.join('\n'); + console.log(result) + return result; + } + const eslint = new ESLint(); const results = await eslint.lintText(code); const codeLines = code.split('\n'); - let result = '#### CODE ERROR INFO ###\n'; const exceptions = results.map(r => r.messages).flat(); if (exceptions.length > 0) { @@ -40,10 +58,10 @@ export class Coder { result += `#ERROR ${index + 1}\n`; result += `Message: ${exc.message}\n`; result += `Location: Line ${exc.line}, Column ${exc.column}\n`; - result += `Related Code Line: ${errorLine}\n\n`; + result += `Related Code Line: ${errorLine}\n`; } }); - result += 'The code contains exceptions and cannot continue execution.\n'; + result += 'The code contains exceptions and cannot continue execution.'; } else { return null;//no error } @@ -172,14 +190,14 @@ export class Coder { code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```')); const result = await this.stageCode(code); const executionModuleExports = result.func; + let src_check_copy = result.src_check_copy; + const analysisResult = await this.checkCode(src_check_copy); + if (analysisResult) { + const message = 'Error: Code syntax error. Please try again:'+'\n'+analysisResult+'\n'+await this.agent.prompter.getRelevantSkillDocs(analysisResult,3); + messages.push({ role: 'system', content: message }); + continue; + } if (!executionModuleExports) { - let src_check_copy = result.src_check_copy; - const analysisResult = await this.checkCode(src_check_copy); - if (analysisResult) { - const message = 'Error: Code syntax error. Please try again:'+'\n'+analysisResult+'\n'+await this.agent.prompter.getRelevantSkillDocs(analysisResult,3); - messages.push({ role: 'system', content: message }); - continue; - } agent_history.add('system', 'Failed to stage code, something is wrong.'); return {success: false, message: null, interrupted: false, timedout: false}; } @@ -189,10 +207,10 @@ export class Coder { }, { timeout: settings.code_timeout_mins }); if (code_return.interrupted && !code_return.timedout) return { success: false, message: null, interrupted: true, timedout: false }; - console.log("Code generation result:", code_return.success, code_return.message); + console.log("Code generation result:", code_return.success, code_return.message.toString()); if (code_return.success) { - const summary = "Summary of newAction\nAgent wrote this code: \n```" + this.sanitizeCode(code) + "```\nCode Output:\n" + code_return.message; + const summary = "Summary of newAction\nAgent wrote this code: \n```" + this.sanitizeCode(code) + "```\nCode Output:\n" + code_return.message.toString(); return { success: true, message: summary, interrupted: false, timedout: false }; } @@ -207,8 +225,4 @@ export class Coder { } return { success: false, message: null, interrupted: false, timedout: true }; } -//err = err.toString(); -// let relevant_skill_docs = await this.agent.prompter.getRelevantSkillDocs(err,5); -// let message = this.formatOutput(this.agent.bot) + '!!Code threw exception!! Error: ' + err+'\n'+relevant_skill_docs; -// } \ No newline at end of file diff --git a/src/agent/prompter.js b/src/agent/prompter.js index 48649f9..0ef02f8 100644 --- a/src/agent/prompter.js +++ b/src/agent/prompter.js @@ -133,7 +133,9 @@ export class Prompter { } async getRelevantSkillDocs(message, select_num) { - let latest_message_embedding = await this.embedding_model.embed(message); + let latest_message_embedding = ''; + if(message) //message is not empty, get the relevant skill docs, else return all skill docs + latest_message_embedding = await this.embedding_model.embed(message); let skill_doc_similarities = Object.keys(this.skill_docs_embeddings) .map(doc_key => ({ @@ -149,8 +151,9 @@ export class Prompter { select_num = Math.min(Math.floor(select_num), length); } let selected_docs = skill_doc_similarities.slice(0, select_num); - let relevant_skill_docs = '####RELEVENT DOCS INFO###\nThe following functions are listed in descending order of relevance.\nSkillDocs:\n'; - relevant_skill_docs += selected_docs.map(doc => `${doc.doc_key}`).join('\n'); + let relevant_skill_docs = '#### RELEVENT DOCS INFO ###\nThe following functions are listed in descending order of relevance.\n'; + relevant_skill_docs += 'SkillDocs:\n' + relevant_skill_docs += '###'+ selected_docs.map(doc => `${doc.doc_key}`).join('\n'); return relevant_skill_docs; }