From 35075134c254cbd1caabfc3a69014af839d32308 Mon Sep 17 00:00:00 2001 From: zhouhangeng Date: Thu, 5 Sep 2024 16:16:03 +0800 Subject: [PATCH 1/3] =?UTF-8?q?update:=20cr=E5=A2=9E=E5=8A=A0=E5=85=9C?= =?UTF-8?q?=E5=BA=95=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/ext/cr/actions/code_review.py | 9 ++++++++- metagpt/roles/di/engineer2.py | 6 +++++- metagpt/tools/libs/cr.py | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/metagpt/ext/cr/actions/code_review.py b/metagpt/ext/cr/actions/code_review.py index 5f861c3e3..7ee452194 100644 --- a/metagpt/ext/cr/actions/code_review.py +++ b/metagpt/ext/cr/actions/code_review.py @@ -18,7 +18,7 @@ from metagpt.ext.cr.utils.cleaner import ( from metagpt.ext.cr.utils.schema import Point from metagpt.logs import logger from metagpt.utils.common import parse_json_code_block -from metagpt.utils.report import EditorReporter +from metagpt.utils.report import EditorReporter, ThoughtReporter CODE_REVIEW_PROMPT_TEMPLATE = """ NOTICE @@ -175,13 +175,16 @@ class CodeReview(Action): async def cr_by_points(self, patch: PatchSet, points: list[Point]): comments = [] + valid_patch_count = 0 for patched_file in patch: if not patched_file: continue if patched_file.path.endswith(".py"): points = [p for p in points if p.language == "Python"] + valid_patch_count += 1 elif patched_file.path.endswith(".java"): points = [p for p in points if p.language == "Java"] + valid_patch_count += 1 else: continue group_points = [points[i : i + 3] for i in range(0, len(points), 3)] @@ -198,6 +201,10 @@ class CodeReview(Action): c["commented_file"] = patched_file_path comments.extend(comments_batch) + if valid_patch_count == 0: + await ThoughtReporter().async_report(value={"type": "limit", "classify": "language"}) + raise ValueError("Only code reviews for Python and Java languages are supported.") + return comments async def run(self, patch: PatchSet, points: list[Point], output_file: str): diff --git a/metagpt/roles/di/engineer2.py b/metagpt/roles/di/engineer2.py index 4600e3254..bcd5f5738 100644 --- a/metagpt/roles/di/engineer2.py +++ b/metagpt/roles/di/engineer2.py @@ -13,6 +13,7 @@ from metagpt.prompts.di.engineer2 import ( from metagpt.roles.di.role_zero import RoleZero from metagpt.schema import UserMessage from metagpt.strategy.experience_retriever import ENGINEER_EXAMPLE +from metagpt.tools.libs.cr import CodeReview from metagpt.tools.libs.terminal import Terminal from metagpt.tools.tool_registry import register_tool from metagpt.utils.common import CodeParser, awrite @@ -28,14 +29,17 @@ class Engineer2(RoleZero): terminal: Terminal = Field(default_factory=Terminal, exclude=True) - tools: list[str] = ["Plan", "Editor:read", "RoleZero", "Terminal:run_command", "Engineer2"] + tools: list[str] = ["Plan", "Editor:read", "RoleZero", "Terminal:run_command", "Engineer2", "SearchEnhancedQA", "CodeReview"] def _update_tool_execution(self): # validate = ValidateAndRewriteCode() + cr = CodeReview() self.tool_execution_map.update( { "Terminal.run_command": self.terminal.run_command, "Engineer2.write_new_code": self.write_new_code, + "CodeReview.review": cr.review, + "CodeReview.fix": cr.fix, # "ValidateAndRewriteCode.run": validate.run, # "ValidateAndRewriteCode": validate.run, } diff --git a/metagpt/tools/libs/cr.py b/metagpt/tools/libs/cr.py index e3509591b..150f24bd5 100644 --- a/metagpt/tools/libs/cr.py +++ b/metagpt/tools/libs/cr.py @@ -45,6 +45,7 @@ class CodeReview: """ patch = await self._get_patch_content(patch_path) point_file = point_file if point_file else Path(metagpt.ext.cr.__file__).parent / "points.json" + await EditorReporter().async_report(str(point_file), "path") async with aiofiles.open(point_file, "rb") as f: cr_point_content = await f.read() cr_points = [Point(**i) for i in json.loads(cr_point_content)] From 93b5716e8c6226fa5a585478fced47673efb6fbc Mon Sep 17 00:00:00 2001 From: zhouhangeng Date: Thu, 5 Sep 2024 17:29:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?update:=20cr=E5=A2=9E=E5=8A=A0=E5=85=9C?= =?UTF-8?q?=E5=BA=95=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/ext/cr/actions/code_review.py | 1 - metagpt/tools/libs/cr.py | 8 +++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/metagpt/ext/cr/actions/code_review.py b/metagpt/ext/cr/actions/code_review.py index 7ee452194..a9733531e 100644 --- a/metagpt/ext/cr/actions/code_review.py +++ b/metagpt/ext/cr/actions/code_review.py @@ -202,7 +202,6 @@ class CodeReview(Action): comments.extend(comments_batch) if valid_patch_count == 0: - await ThoughtReporter().async_report(value={"type": "limit", "classify": "language"}) raise ValueError("Only code reviews for Python and Java languages are supported.") return comments diff --git a/metagpt/tools/libs/cr.py b/metagpt/tools/libs/cr.py index 150f24bd5..0a53dd194 100644 --- a/metagpt/tools/libs/cr.py +++ b/metagpt/tools/libs/cr.py @@ -49,9 +49,11 @@ class CodeReview: async with aiofiles.open(point_file, "rb") as f: cr_point_content = await f.read() cr_points = [Point(**i) for i in json.loads(cr_point_content)] - - comments = await CodeReview_().run(patch, cr_points, output_file) - return f"The number of defects: {len(comments)} and the comments are stored in {output_file}" + try: + comments = await CodeReview_().run(patch, cr_points, output_file) + except ValueError as e: + return str(e) + return f"The number of defects: {len(comments)}, the comments are stored in {output_file}, and the checkpoints are stored in {str(point_file)}" async def fix( self, From ac44f41fb9a3bd707e3c8d16715d0fe1a1c922ea Mon Sep 17 00:00:00 2001 From: zhouhangeng Date: Thu, 5 Sep 2024 18:44:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update:=20cr=E5=A2=9E=E5=8A=A0=E5=85=9C?= =?UTF-8?q?=E5=BA=95=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- metagpt/ext/cr/actions/code_review.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metagpt/ext/cr/actions/code_review.py b/metagpt/ext/cr/actions/code_review.py index a9733531e..0235dc2c6 100644 --- a/metagpt/ext/cr/actions/code_review.py +++ b/metagpt/ext/cr/actions/code_review.py @@ -18,7 +18,7 @@ from metagpt.ext.cr.utils.cleaner import ( from metagpt.ext.cr.utils.schema import Point from metagpt.logs import logger from metagpt.utils.common import parse_json_code_block -from metagpt.utils.report import EditorReporter, ThoughtReporter +from metagpt.utils.report import EditorReporter CODE_REVIEW_PROMPT_TEMPLATE = """ NOTICE