diff --git a/metagpt/actions/di/rewrite_code.py b/metagpt/actions/di/rewrite_code.py index 03831f912..ea093b36c 100644 --- a/metagpt/actions/di/rewrite_code.py +++ b/metagpt/actions/di/rewrite_code.py @@ -1,4 +1,4 @@ -import asyncio +import os from tenacity import retry, stop_after_attempt, wait_random_exponential @@ -21,17 +21,17 @@ class RewriteCode(Action): name: str = "RewriteCode" async def run( - self, code_path: str, design_doc_path: str = "", task_doc_path: str = "", code_review_k_times: int = 2 + self, code_path: str, design_doc_input: str = "", task_doc_input: str = "", code_review_k_times: int = 2 ) -> str: """Reviews the provided code based on the accompanying design and task documentation, return the complete and correct code. Read the code from `code_path`, and write the final code to `code_path`. - If there is no `design_doc_path` or `task_doc_path`, it will return and do nothing. + If there is no `design_doc_input` or `task_doc_input`, it will return and do nothing. Args: code_path (str): The file path of the code snippet to be reviewed. This should be a string containing the path to the source code file. - design_doc_path (str): The file path of the design document associated with the code. This should describe the system architecture, used in the code. It helps provide context for the review process. - task_doc_path (str): The file path of the task document describing what the code is intended to accomplish. This should outline the functional requirements or objectives of the code. + design_doc_input (str): Content or file path of the design document associated with the code. This should describe the system architecture, used in the code. It helps provide context for the review process. + task_doc_input (str): Content or file path of the task document describing what the code is intended to accomplish. This should outline the functional requirements or objectives of the code. code_review_k_times (int, optional): The number of iterations for reviewing and potentially rewriting the code. Defaults to 2. Returns: @@ -41,21 +41,28 @@ class RewriteCode(Action): # Example of how to call the run method with a code snippet and documentation await WriteCodeReview().run( code_path="/tmp/game.js", - design_doc_path="/tmp/design_doc.json", - task_doc_path="/tmp/task_doc.json" + design_doc_input="/tmp/design_doc.json", + task_doc_input='{"Required packages":["No third-party dependencies required"], ...}' ) """ - if not design_doc_path or not task_doc_path: + if not design_doc_input or not task_doc_input: return - code, design_doc, task_doc = await asyncio.gather( - aread(code_path), aread(design_doc_path), aread(task_doc_path) - ) + code = await aread(code_path) + + # Check if design_doc_input and task_doc_input are paths or content, and read if they are paths + if os.path.exists(design_doc_input): + logger.info(f"read from {design_doc_input}") + design_doc_input = await aread(design_doc_input) + + if os.path.exists(task_doc_input): + logger.info(f"read from {task_doc_input}") + task_doc_input = await aread(task_doc_input) context = "\n".join( [ - "## System Design\n" + design_doc + "\n", - "## Task\n" + task_doc + "\n", + "## System Design\n" + design_doc_input + "\n", + "## Task\n" + task_doc_input + "\n", ] )