diff --git a/src/agent/pythonAgentRunner.ts b/src/agent/pythonAgentRunner.ts index 0fb6605c..1ebce7f6 100644 --- a/src/agent/pythonAgentRunner.ts +++ b/src/agent/pythonAgentRunner.ts @@ -122,18 +122,24 @@ export async function runPythonAgent(agent: AgentContext): Promise\n${pythonScript}\n${e.message}\nPlease adjust/reformat the Python script to fix the issue. Output only the updated code. Do no chat, do not output markdown ticks. Only the updated code.`; - pythonScript = await llms().hard.generateText(prompt, null, { id: 'Fix python script error' }); - - // Re-try execution of fixed syntax/indentation error - const result = await pyodide.runPythonAsync(pythonScript, { globals }); - pythonScriptResult = result?.toJs ? result.toJs() : result; - pythonScriptResult = JSON.stringify(pythonScriptResult); - if (result?.destroy) result.destroy(); + if (e.type === 'IndentationError' || e.type !== 'SyntaxError') { + // Fix the compile issues in the script + const prompt = `${functionsXml}\n\n${pythonScript}\n${e.message}\nPlease adjust/reformat the Python script to fix the issue. Output only the updated code. Do no chat, do not output markdown ticks. Only the updated code.`; + pythonScript = await llms().hard.generateText(prompt, null, { id: 'Fix python script error' }); + + // Re-try execution of fixed syntax/indentation error + const result = await pyodide.runPythonAsync(pythonScript, { globals }); + pythonScriptResult = result?.toJs ? result.toJs() : result; + pythonScriptResult = JSON.stringify(pythonScriptResult); + if (result?.destroy) result.destroy(); + } else { + throw e; + } } logger.info(pythonScriptResult, 'Script result');