fixbug: DebugError

This commit is contained in:
莘权 马 2023-11-24 13:30:00 +08:00
parent 10d9f33150
commit 75dcc8d534
6 changed files with 107 additions and 43 deletions

View file

@ -5,6 +5,7 @@
@Author : alexanderwu
@File : debug_error.py
"""
import re
from metagpt.actions.action import Action
from metagpt.logs import logger
@ -36,7 +37,9 @@ class DebugError(Action):
# return fixed_code
async def run(self, *args, **kwargs) -> str:
if "PASS" in self.context.output:
pattern = r"Ran (\d+) tests in ([\d.]+)s\n\nOK"
matches = re.search(pattern, self.context.output)
if matches:
return "", "the original code works fine, no need to debug"
file_name = self.context.code_filename

View file

@ -51,8 +51,14 @@ CONTEXT = """
## Running Command
{command}
## Running Output
standard output: {outs};
standard errors: {errs};
standard output:
```text
{outs}
```
standard errors:
```text
{errs}
```
"""
@ -84,10 +90,19 @@ class RunCode(Action):
additional_python_paths = ":".join(additional_python_paths)
env["PYTHONPATH"] = additional_python_paths + ":" + env.get("PYTHONPATH", "")
install_command = ["python", "-m", "pip", "install", "-r", "requirements.txt"]
logger.info(" ".join(install_command))
subprocess.run(install_command, check=True, cwd=working_directory, env=env)
install_pytest_command = ["python", "-m", "pip", "install", "pytest"]
logger.info(" ".join(install_pytest_command))
subprocess.run(install_pytest_command, check=True, cwd=working_directory, env=env)
# Start the subprocess
process = subprocess.Popen(
command, cwd=working_directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
)
logger.info(" ".join(command))
try:
# Wait for the process to complete, with a timeout
@ -101,7 +116,11 @@ class RunCode(Action):
async def run(self, *args, **kwargs) -> str:
logger.info(f"Running {' '.join(self.context.command)}")
if self.context.mode == "script":
outs, errs = await self.run_script(command=self.context.command, **kwargs)
outs, errs = await self.run_script(
command=self.context.command,
working_directory=self.context.working_directory,
additional_python_paths=self.context.additional_python_paths,
)
elif self.context.mode == "text":
outs, errs = await self.run_text(code=self.context.code)