diff --git a/metagpt/actions/write_code_plan_and_change_an.py b/metagpt/actions/write_code_plan_and_change_an.py index 708808050..f14e10ea5 100644 --- a/metagpt/actions/write_code_plan_and_change_an.py +++ b/metagpt/actions/write_code_plan_and_change_an.py @@ -6,30 +6,45 @@ @File : write_code_plan_and_change_an.py """ import os +from typing import List from pydantic import Field from metagpt.actions.action import Action from metagpt.actions.action_node import ActionNode +from metagpt.logs import logger from metagpt.schema import CodePlanAndChangeContext -CODE_PLAN_AND_CHANGE = ActionNode( - key="Code Plan And Change", +DEVELOPMENT_PLAN = ActionNode( + key="Development Plan", expected_type=str, - instruction="Developing comprehensive and step-by-step incremental development plan, and write Incremental " - "Change by making a code draft that how to implement incremental development including detailed steps based on the " - "context. Note: Track incremental changes using mark of '+' or '-' for add/modify/delete code, and conforms to the " - "output format of git diff", - example=""" -1. Plan for calculator.py: Enhance the functionality of `calculator.py` by extending it to incorporate methods for subtraction, multiplication, and division. Additionally, implement robust error handling for the division operation to mitigate potential issues related to division by zero. -```python + instruction="Develop a comprehensive and step-by-step incremental development plan, providing the essential " + "changes to be implemented at each step", + example="""To implement the new feature, we will create/change/update the methods in the calculator to support subtraction, multiplication, and division operations. Additionally, the ... +\nHere are the steps based on the order of 'Task List' we will follow:\n +1. Enhance the functionality of `calculator.py` by extending it to incorporate methods for subtraction, multiplication, and division. Also, the ... +2. Integrate new API endpoints for subtraction, multiplication, and division into the existing codebase of `main.py`. Then, ensure ... +3. ...""", +) + +IMPLEMENT_CHANGE = ActionNode( + key="Implement Change", + expected_type=List[str], + instruction="Write Incremental Change by making a code draft that how to implement incremental development " + "including detailed steps based on the context. Note: Track incremental changes using the marks `+` and `-` to " + "indicate added, deleted, or modified code, and ensure compliance with the output format of `git diff`", + example=[ + '''```diff +--- Old/calculator.py ++++ New/calculator.py + class Calculator: self.result = number1 + number2 return self.result - def sub(self, number1, number2) -> float: + def subtract(self, number1: float, number2: float) -> float: -+ ''' ++ """ + Subtracts the second number from the first and returns the result. + + Args: @@ -38,13 +53,13 @@ class Calculator: + + Returns: + float: The difference of number1 and number2. -+ ''' ++ """ + self.result = number1 - number2 + return self.result + def multiply(self, number1: float, number2: float) -> float: - pass -+ ''' ++ """ + Multiplies two numbers and returns the result. + + Args: @@ -53,15 +68,15 @@ class Calculator: + + Returns: + float: The product of number1 and number2. -+ ''' ++ """ + self.result = number1 * number2 + return self.result + def divide(self, number1: float, number2: float) -> float: - pass -+ ''' ++ """ + ValueError: If the second number is zero. -+ ''' ++ """ + if number2 == 0: + raise ValueError('Cannot divide by zero') + self.result = number1 / number2 @@ -75,10 +90,11 @@ class Calculator: + print("Result is already zero, no need to clear.") + self.result = 0.0 -``` +```''', + """```diff +--- Old/main.py ++++ New/main.py -2. Plan for main.py: Integrate new API endpoints for subtraction, multiplication, and division into the existing codebase of `main.py`. Then, ensure seamless integration with the overall application architecture and maintain consistency with coding standards. -```python def add_numbers(): result = calculator.add_numbers(num1, num2) return jsonify({'result': result}), 200 @@ -106,6 +122,7 @@ def add_numbers(): if __name__ == '__main__': app.run() ```""", + ], ) CODE_PLAN_AND_CHANGE_CONTEXT = """ @@ -179,7 +196,9 @@ Role: You are a professional engineer; The main goal is to complete incremental 9. Attention: Retain details that are not related to incremental development but are important for maintaining the consistency and clarity of the old code. """ -WRITE_CODE_PLAN_AND_CHANGE_NODE = ActionNode.from_children("WriteCodePlanAndChange", [CODE_PLAN_AND_CHANGE]) +WRITE_CODE_PLAN_AND_CHANGE_NODE = ActionNode.from_children( + "WriteCodePlanAndChange", [DEVELOPMENT_PLAN, IMPLEMENT_CHANGE] +) class WriteCodePlanAndChange(Action): @@ -200,6 +219,7 @@ class WriteCodePlanAndChange(Action): task=task_doc.content, code=code_text, ) + logger.info("Writing code plan and change..") return await WRITE_CODE_PLAN_AND_CHANGE_NODE.fill(context=context, llm=self.llm, schema="json") async def get_old_codes(self) -> str: diff --git a/metagpt/actions/write_prd_an.py b/metagpt/actions/write_prd_an.py index 9898be55b..5733b29da 100644 --- a/metagpt/actions/write_prd_an.py +++ b/metagpt/actions/write_prd_an.py @@ -56,7 +56,7 @@ REFINED_PRODUCT_GOALS = ActionNode( key="Refined Product Goals", expected_type=List[str], instruction="Update and expand the original product goals to reflect the evolving needs due to incremental " - "development.Ensure that the refined goals align with the current project direction and contribute to its success.", + "development. Ensure that the refined goals align with the current project direction and contribute to its success.", example=[ "Enhance user engagement through new features", "Optimize performance for scalability", diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 7037ca0b3..235ec0f69 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -204,7 +204,6 @@ class Engineer(Role): async def _act_code_plan_and_change(self): """Write code plan and change that guides subsequent WriteCode and WriteCodeReview""" - logger.info("Writing code plan and change..") node = await self.rc.todo.run() code_plan_and_change = node.instruct_content.model_dump_json() dependencies = {