diff --git a/metagpt/actions/refine_design_api.py b/metagpt/actions/refine_design_api.py index 909fa6db9..d6a948b43 100644 --- a/metagpt/actions/refine_design_api.py +++ b/metagpt/actions/refine_design_api.py @@ -20,7 +20,7 @@ templates = { # Context {context} -## Legacy Design +## Legacy {legacy} ## Format example @@ -30,9 +30,7 @@ Role: You are an architect; the goal is to perform incremental development and d Requirement: Fill in the following missing information based on the context, each section name is a key in json. Output exactly as shown in the example, including single and double quotes. Max Output: 8192 chars or 2048 tokens. Try to use them up. -## Difference Description: Provide as list, the foremost differences description for system design here based on the previous. - -## Incremental implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. +## Incremental implementation approach: Provide as Python list[str]. Analyze the difficult points of the requirements, select the appropriate open-source framework. Up to 5. ## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores @@ -46,8 +44,7 @@ Output exactly as shown in the example, including single and double quotes, and "FORMAT_EXAMPLE": """ [CONTENT] { - "Difference Description": ["The ..."], - "Incremental implementation approach": "We will ...", + "Incremental implementation approach": ["We will ...",], "Python package name": "new_name", "Data structures and interface definitions": ' classDiagram @@ -72,7 +69,7 @@ Output exactly as shown in the example, including single and double quotes, and # Context {context} -## Legacy Design +## Legacy {legacy} ## Format example @@ -83,9 +80,7 @@ Requirement: Fill in the following missing information based on the context, not Max Output: 8192 chars or 2048 tokens. Try to use them up. Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. -## Difference Description: Provide as list, the foremost differences description for system design here based on the previous. - -## Incremental implementation approach: Provide as Plain text. Analyze the difficult points of the requirements, select the appropriate open-source framework. +## Incremental implementation approach: Provide as Python list[str]. Analyze the difficult points of the requirements, select the appropriate open-source framework. Up to 5. ## Python package name: Provide as Python str with python triple quoto, concise and clear, characters only use a combination of all lowercase and underscores @@ -96,15 +91,13 @@ Attention: Use '##' to split sections, not '#', and '## ' SHOULD W """, "FORMAT_EXAMPLE": """ --- -## Difference Description -```python -[ - "The ...", -] -``` ## Incremental implementation approach -We will ... +```python +[ + "We will ...", +] +``` ## Python package name ```python @@ -135,8 +128,8 @@ sequenceDiagram OUTPUT_MAPPING = { # "Incremental Requirements": (str, ...), - "Difference Description": (Union[List[str], str], ...), - "Incremental implementation approach": (str, ...), + # "Difference Description": (Union[List[str], str], ...), + "Incremental implementation approach": (Union[List[str], str], ...), "Python package name": (str, ...), # "File list": (List[str], ...), "Data structures and interface definitions": (str, ...), diff --git a/metagpt/actions/refine_prd.py b/metagpt/actions/refine_prd.py index 0f01b9904..1d8bab5f8 100644 --- a/metagpt/actions/refine_prd.py +++ b/metagpt/actions/refine_prd.py @@ -9,10 +9,9 @@ increment_template = { "json": { "PROMPT_TEMPLATE": """ # Context -## User's Incremental Requirements -{new_requirements} +{context} -## Legacy PRD +## Legacy {legacy} ## Search Information @@ -22,13 +21,9 @@ increment_template = { {format_example} ----- Role: You are a professional Product Manager tasked with overseeing incremental development and crafting Product Requirements Documents (PRDs) for a concise, usable, and efficient product. -Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive designOnly output one json, nothing else. +Requirements: According to the context, fill in the following missing information, each section name is a key in json ,If the requirements are unclear, ensure minimum viability and avoid excessive design. Only output one json, nothing else. -## Incremental Requirements: Provide as str, the foremost incremental requirements for PRD here based on the previous. - -## Difference Description: Provide as str, the foremost differences description for PRD here based on the previous. - -## Incremental Development Plan: Provide as Python list[str], up to 5 clear, incremental development plans. If the requirement itself is simple, the incremental development plan should also be simple +## Incremental Development Analysis: Provide as Python list[str], up to 5. incremental development analysis and plans based on the context and the legacy. If the requirement itself is simple, the Incremental Development Analysis should also be simple. output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, and only output the json inside this tag, nothing else @@ -36,9 +31,7 @@ and only output the json inside this tag, nothing else "FORMAT_EXAMPLE": """ [CONTENT] { - "Incremental Requirements": "", - "Difference Description": [], - "Incremental Development Plan": [], + "Incremental Development Analysis": [], } [/CONTENT] """, @@ -46,11 +39,9 @@ and only output the json inside this tag, nothing else "markdown": { "PROMPT_TEMPLATE": """ # Context -You need to refine the requirements based on the Incremental Requirements and the existing requirements' output. -## User's Incremental Requirements -{new_requirements} +{context} -## Legacy PRD +## Legacy {legacy} ## Search Information @@ -63,32 +54,21 @@ Role: You are a professional Product Manager tasked with overseeing incremental Requirements: According to the context, fill in the following missing information, note that each sections are returned in Python code triple quote form seperatedly. If the requirements are unclear, ensure minimum viability and avoid excessive design ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. AND '## ' SHOULD WRITE BEFORE the code and triple quote. Output carefully referenced "Format example" in format.Only output one json, nothing else. -## Incremental Requirements: Provide as str, the foremost incremental requirements for PRD here based on the previous. - -## Difference Description: Provide as str, the foremost differences description for PRD here based on the previous. - -## Incremental Development Plan: Provide as Python list[str], up to 5 clear, incremental development plans. If the requirement itself is simple, the incremental development plan should also be simple +## Incremental Development Analysis: Provide as Python list[str], up to 5. Incremental development analysis and plans based on the context and the legacy. If the requirement itself is simple, the Incremental Development Analysis should also be simple. """, "FORMAT_EXAMPLE": """ --- -## Incremental Requirements -The boss ... -## Difference Description -... - -## Incremental Development Plan +## Incremental Development Analysis [ - "...", + "We will ...", ] """, }, } INCREMENT_OUTPUT_MAPPING = { - "Incremental Requirements": (str, ...), - "Difference Description": (Union[List[str], str], ...), - "Incremental Development Plan": (List[str], ...), + "Incremental Development Analysis": (List[str], ...), } @@ -97,7 +77,7 @@ class RefinePRD(Refine): def __init__(self, name="RefinePRD", context=None, llm=None): super().__init__(name, context, llm) - async def run(self, new_requirements, legacy, format=CONFIG.prompt_format, *args, **kwargs): + async def run(self, context, legacy, format=CONFIG.prompt_format, *args, **kwargs): sas = SearchAndSummarize() rsp = "" info = f"### Search Results\n{sas.result}\n\n### Search Summary\n{rsp}" @@ -107,7 +87,7 @@ class RefinePRD(Refine): prompt_template, format_example = get_template(increment_template, format) prompt = prompt_template.format( - new_requirements=new_requirements, legacy=legacy, search_information=info, + context=context, legacy=legacy, search_information=info, format_example=format_example ) logger.debug(prompt) diff --git a/metagpt/actions/refine_project_management.py b/metagpt/actions/refine_project_management.py index dfeeb2db0..2775b1cb6 100644 --- a/metagpt/actions/refine_project_management.py +++ b/metagpt/actions/refine_project_management.py @@ -21,30 +21,23 @@ templates = { ## Format example {format_example} ----- -Role: You are a project manager; the goal is to perform incremental development based on the context and difference descriptions and the legacy. Break down tasks according to PRD/technical design, provide a Task list, and analyze task dependencies to start with the prerequisite modules. +Role: You are a project manager; the goal is to perform incremental development based on the context and the legacy. Break down tasks according to PRD/technical design, provide a Task list, and analyze task dependencies to start with the prerequisite modules. Requirements: Based on the context and the Legacy Project Management and Legacy Code, fill in the following missing information. Note that Please try your best to reuse legacy code, and all sections are returned in Python code triple quote form seperatedly. Here the granularity of the task is a file that need to modified. Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. +Output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT]. The following is the attribute description of the JSON object. -## Difference Description: Provide as a python list, the foremost differences description for project management here based on the previous. - -## Incremental Required Python third-party packages: Provided as a python list, the requirements.txt format +## Required Python third-party packages: Provided as a python list, the requirements.txt format ## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend based on the previous. -## Logic Analysis: Only files need to modified, Provided as a Python list[list[str]. If the file has no changes, the file will not be output. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first based on the previous. +## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first. -## Task list: Only files need to modified, provided as Python list[str]. If the file has no changes, the file will not be output. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first - -output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, +Output a properly formatted JSON, wrapped inside [CONTENT][/CONTENT] like format example, and only output the json inside this tag, nothing else """, "FORMAT_EXAMPLE": ''' { - "Incremental Requirements": "...", - "Difference Description": [ - "...", - ] - "Incremental Required Python third-party packages": [ + "Required Python third-party packages": [ "flask==1.1.2", "bcrypt==3.2.0" ], @@ -53,9 +46,6 @@ and only output the json inside this tag, nothing else ... description: A JSON object ... """, - "Logic Analysis": [ - ["game.py","Contains..."] - ], "Task list": [ "game.py" ] @@ -77,29 +67,16 @@ Role: You are a project manager; the goal is to perform incremental development Requirements: Based on the context and the Legacy Project Management and Legacy Code, fill in the following missing information. Note that Please try your best to reuse legacy code, and all sections are returned in Python code triple quote form seperatedly. Here the granularity of the task is a file that need to modified. Attention: Use '##' to split sections, not '#', and '## ' SHOULD WRITE BEFORE the code and triple quote. -## Difference Description: Provided as a python list, the foremost differences description for project management here based on the previous. - -## Incremental Required Python third-party packages: Provided as a python list, the requirements.txt format +## Required Python third-party packages: Provided as a python list, the requirements.txt format ## Full API spec: Use OpenAPI 3.0. Describe all APIs that may be used by both frontend and backend based on the previous. -## Logic Analysis: Only files need to modified, Provided as a Python list[list[str]. If the file has no changes, the file will not be output. the first is filename, the second is class/method/function should be implemented in this file. Analyze the dependencies between the files, which work should be done first based on the previous. - -## Task list: Only files need to modified, provided as Python list[str]. If the file has no changes, the file will not be output. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first +## Task list: Provided as Python list[str]. Each str is a filename, the more at the beginning, the more it is a prerequisite dependency, should be done first. """, "FORMAT_EXAMPLE": ''' --- -## Incremental Requirements -... -## Difference Description -```python -[ - "The ...", -] -``` - -## Incremental Required Python third-party packages +## Required Python third-party packages ```python [ "flask==1.1.2", @@ -116,13 +93,6 @@ description: A JSON object ... """ ``` -## Logic Analysis -```python -[ - ["game.py", "Contains ..."], -] -``` - ## Task list ```python [ @@ -136,16 +106,16 @@ description: A JSON object ... OUTPUT_MAPPING = { # "Incremental Requirements": (str, ...), # ## Incremental Requirements: Provided as a str, the foremost incremental requirements for project management here based on the previous. - "Difference Description": (Union[List[str], str], ...), - "Incremental Required Python third-party packages": (Union[List[str], str], ...), + # "Difference Analysis": (Union[List[str], str], ...), + "Required Python third-party packages": (Union[List[str], str], ...), "Full API spec": (str, ...), - "Logic Analysis": (List[List[str]], ...), + # "Logic Analysis": (List[List[str]], ...), "Task list": (List[str], ...), } class RefineTasks(Action): - def __init__(self, name="CreateTasks", context=None, llm=None): + def __init__(self, name="RefineTasks", context=None, llm=None): super().__init__(name, context, llm) def _save(self, context, rsp): @@ -158,7 +128,7 @@ class RefineTasks(Action): # Write requirements.txt requirements_path = WORKSPACE_ROOT / ws_name / "requirements.txt" - requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Incremental Required Python third-party packages"))) + requirements_path.write_text("\n".join(rsp.instruct_content.dict().get("Required Python third-party packages"))) async def run(self, context, legacy, format=CONFIG.prompt_format): prompt_template, format_example = get_template(templates, format) diff --git a/metagpt/actions/write_code_guide.py b/metagpt/actions/write_code_guide.py index 391173bb3..3e51f0d2d 100644 --- a/metagpt/actions/write_code_guide.py +++ b/metagpt/actions/write_code_guide.py @@ -10,24 +10,21 @@ from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE -Role: You are a professional software engineer, and your main task is to conduct incremental development, proposing incremental development plans and code guideance based on context and legacy code. Existing code and logic that need to be retained must also appear in the code after incremental development, do not omit it. Ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). -ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenced "Format example". +Role: You are a professional software engineer, and your main task is to conduct incremental development, proposing incremental development plans and code guideance based on context and legacy code. Existing code and logic that need to be retained must also appear in the code after incremental development, do not omit it. Ensure that the code conforms to the PEP8 standards, is elegantly designed and modularized, easy to read and maintain, and is written in Python 3.9 (or in another programming language). Output format carefully referenced "Format example". ## Regulations Review: To make the software directly operable without further coding, follow the regulations below during incremental development: +0) Determine the scope of responsibilities of each file and what classes and methods need to be implemented. 1) Import all referenced classes. -2) Implement all methods. -3) Add necessary explanation to all methods. +2) Implement all methods. +3) Add necessary explanation to all methods. 4) Ensure there are no potential bugs. 5) Confirm that the entire project conforms to the tasks proposed by the user. 6) Review the code thoroughly, checking for errors and validating the logic to ensure seamless user interaction without compromising any specified requirements. -## Incremental Development Plan: Proposed the Minimum essential incremental development plan, based on the following context and legacy code by thinking step by step. -... +## Incremental Development Plan: Provided as a Python list containing `filename.py`. Proposed the detail and essential incremental development plan, based on the following context and legacy code by thinking and analyzing step by step. All incremental modules/functions need to be added to the corresponding code files. + +## Code Guidance: Propose the foremost guidelines that how to implement code of modification part for incremental development based on the above context, legacy code and incremental development plan. -## Code guidelines: Propose the foremost guidelines that how to implement code of modification part for incremental development based on the above context, legacy code and incremental development plan. -```python -... -''' ----- # Context {context} @@ -35,20 +32,31 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format carefully referenc ## Legacy Code You are tasked with conducting incremental development in the existing code based on the provided legacy code and above information. ``` -{code} +{legacy} ``` ----- ## Format example ----- -## Incremental Development Guide: -... +## Incremental Development Guide +[ + "`game.py` Contains `Game` and ...", +] -## Code Guidance: -# Implementation the ... + +## Code Guidance +### Implementation `xx` in `xxx.py` ..., else retain the original xxx.py code. ```python +## xxx.py ... -''' +``` +--- +### Implementation of the `Game` in `game.py` ..., else retain the original game.py code. +```python +## game.py +class Game: + ... +``` ----- """ @@ -57,11 +65,10 @@ class WriteCodeGuide(Action): def __init__(self, name="WriteCodeGuide", context: list[Message] = None, llm=None): super().__init__(name, context, llm) - async def run(self, context, code): - prompt = PROMPT_TEMPLATE.format(context=context, code=code) + async def run(self, context, legacy): + prompt = PROMPT_TEMPLATE.format(context=context, legacy=legacy) logger.info(f'Write Code Guide ..') code_guide = await self._aask(prompt) # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) - # self._save(context, filename, code) return code_guide \ No newline at end of file diff --git a/metagpt/actions/write_code_refine.py b/metagpt/actions/write_code_refine.py index 466c30679..5a70b2c68 100644 --- a/metagpt/actions/write_code_refine.py +++ b/metagpt/actions/write_code_refine.py @@ -10,17 +10,19 @@ from tenacity import retry, stop_after_attempt, wait_fixed PROMPT_TEMPLATE = """ NOTICE Role: You are a professional engineer; your primary goal is to write PEP8 compliant, elegant, modular, easy-to-read, and maintainable Python 3.9 code (or any other programming language of your choice). -Requirements: You should modify the corresponding code based on the guidance. Then, output the complete code, fixing all errors according to the context. Ensure that you adhere to the specified guidelines for incremental development and modification of legacy code. +Requirements: Rewrite the complete code based on the Legacy Code so that it can be executed and avoid any potential bugs. You should modify the corresponding code based on the guidance. Output the complete code, fixing all errors according to the context. Ensure that you adhere to the specified guidelines for incremental development and modification of legacy code. ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format should be carefully referenced using the "Format example". Only output the current modified code, nothing else. In the modified code, if unchanged, you should output it, the complete code. -## Code: Only Write {filename}, Write code using triple quotes, based on the following list and context. -1. Do your best to implement THIS ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. -2. Requirement: Implement one of the following code files based on the provided context. Return the code in the specified format. Your code will be part of the entire project, so ensure it is complete, reliable, and reusable. -3. Attention1: If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. -4. Attention2: YOU MUST FOLLOW "Data structures and interface definitions". DONT CHANGE ANY DESIGN. +## Rewrite Complete Code: Only Write one file {filename}, Write code using triple quotes, based on the following list, context, guidelines and legacy code. +1. Important: Do your best to implement ONLY ONE FILE. ONLY USE EXISTING API. IF NO API, IMPLEMENT IT. +2. Implement one of the following code files based on the provided context. Return the code in the specified format. Your code will be part of the entire project, so ensure it is complete, reliable, and reusable. +3. Attention1: Implement the functions required by the current file scope of responsibility. For example, main only needs to focus on the basic functions of main.py in the legacy code and the incremental functions to be implemented. Reuse existing code as much as possible. You can import functions from other codes instead of reimplementing the function. If there is any setting, ALWAYS SET A DEFAULT VALUE, ALWAYS USE STRONG TYPE AND EXPLICIT VARIABLE. +4. Attention2: Make modifications and additions to the legacy code in accordance with the provided guidelines and API. Ensure that the complete code is implemented without any omissions, taking into account the guidelines, context, and existing legacy code. Retain the basic function methods from the legacy code, and make sure to preserve the existing code and logic that needs to be retained throughout the incremental development process. Avoid omitting any essential components. 5. Think before writing: What should be implemented and provided in this document? 6. CAREFULLY CHECK THAT YOU DONT MISS ANY NECESSARY CLASS/FUNCTION IN THIS FILE. 7. Do not use public member functions that do not exist in your design. +8. The Modified Code is implemented according to the requirements, and there are no issues with the code logic. All functions in the Modified Code are fully implemented; none are omitted or incomplete. + ----- # Context {context} @@ -30,13 +32,13 @@ ATTENTION: Use '##' to SPLIT SECTIONS, not '#'. Output format should be carefull {guide} ----- -## Legacy Code: The Legacy Code that needs to be modified. +## Legacy Code: The Legacy Code that needs to be modified. '===' is the separator of each code file in the legacy code. Basic function methods need to be retained in {filename}. {legacy} ----- ## Format example ----- -## Modified/Added Code: {filename} +## Rewrite Complete Code: {filename} ```python # {filename} ... @@ -55,8 +57,8 @@ class WriteCodeRefine(Action): code = CodeParser.parse_code(block="", text=code_rsp) return code - async def run(self, context, code, filename, guide): - prompt = PROMPT_TEMPLATE.format(context=context, legacy=code, filename=filename, guide=guide) + async def run(self, context, legacy, filename, guide): + prompt = PROMPT_TEMPLATE.format(context=context, legacy=legacy, filename=filename, guide=guide) logger.info(f'Code refine {filename}..') code = await self.write_code(prompt) # code_rsp = await self._aask_v1(prompt, "code_rsp", OUTPUT_MAPPING) diff --git a/metagpt/roles/architect.py b/metagpt/roles/architect.py index f87f8899d..00ab6de35 100644 --- a/metagpt/roles/architect.py +++ b/metagpt/roles/architect.py @@ -50,7 +50,11 @@ class Architect(Role): async def _act(self) -> Message: if self.increment: logger.info(f"{self._setting}: ready to RefineDesign") - legacy = self._rc.env.get_legacy()["legacy_design"] + legacy_dict = self._rc.env.get_legacy() + legacy_code = "" + for code_dict in legacy_dict.get("legacy_code"): + legacy_code += code_dict.get("code") + "\n===\n" + legacy = "Legacy Design:\n" + legacy_dict.get("legacy_design") + "\nLegacy Code:\n" + legacy_code response = await self._rc.todo.run(self._rc.history, legacy) else: diff --git a/metagpt/roles/engineer.py b/metagpt/roles/engineer.py index 66ce12f83..de144d4ca 100644 --- a/metagpt/roles/engineer.py +++ b/metagpt/roles/engineer.py @@ -16,6 +16,7 @@ from metagpt.actions import WriteCode, WriteCodeReview, WriteDesign, WriteTasks, from metagpt.actions.refine_design_api import RefineDesign from metagpt.actions.refine_prd import RefinePRD from metagpt.actions.refine_project_management import RefineTasks +from metagpt.actions.rewrite_code import RewriteCode from metagpt.actions.write_code_refine import WriteCodeRefine from metagpt.actions.write_code_guide import WriteCodeGuide from metagpt.const import WORKSPACE_ROOT @@ -88,7 +89,7 @@ class Engineer(Role): self._init_actions([WriteCode, WriteCodeReview]) if self.increment: - self._init_actions([WriteCodeGuide, WriteCodeRefine, WriteCodeReview]) + self._init_actions([WriteCodeGuide, WriteCodeRefine, RewriteCode]) self._watch([RefineTasks]) else: self._watch([WriteTasks]) @@ -210,18 +211,21 @@ class Engineer(Role): code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later workspace = self.get_workspace() human_str = str(self._rc.memory.get_by_role("Human")[0]) - code = self._rc.env.get_legacy()["legacy_code"] + legacy_code = "" + code_list = self._rc.env.get_legacy().get("legacy_code") + for code_dict in code_list: + legacy_code += code_dict.get("code") + "\n===\n" - # Refine code + # Code guide context = [] - msg = self._rc.memory.get_by_actions([RefineDesign, RefineTasks]) - - for m in msg: - context.append(m.content) + # msg = self._rc.memory.get_by_actions([RefineDesign, RefineTasks]) + msg = self._rc.memory.get_by_actions([RefineTasks]) + for tasks_msg in msg: + context.append(tasks_msg.content) context_str = human_str + "\n".join(context) try: logger.info("Write Code Guide start!") - guide = await WriteCodeGuide().run(context=context_str, code=code) + guide = await WriteCodeGuide().run(context=context_str, legacy=legacy_code) msg = Message(content=guide, role=self.profile, cause_by=WriteCodeGuide) self._rc.memory.add(msg) except Exception as e: @@ -230,19 +234,18 @@ class Engineer(Role): # Write code or Code review for todo in self.todos: - msg = self._rc.memory.get_by_actions([RefineTasks]) - context_str = human_str + "\n".join([m.content for m in msg]) # WriteCodeRefine try: logger.info("Write Code Refine start!") - code = await WriteCodeRefine().run(context=context_str, code=code, filename=todo, guide=guide) + code = await WriteCodeRefine().run(context=context_str, legacy=legacy_code, filename=todo, guide=guide) except Exception as e: logger.error("Write Code Refine failed!", e) pass - # FIXME: Code review Action + # FIXME: Code review Action : 如果有 pass 在代码里面,需要重写 # if self.use_code_review: # try: - # rewrite_code = await WriteCodeReview().run(context=context_str, code=code, filename=todo) + # # 解析成guides context = guide + human_str + # rewrite_code = await RewriteCode().run(context=human_str, code=code, filename=todo, legacy=legacy_code) # code = rewrite_code # except Exception as e: # logger.error("code review failed!", e) @@ -251,48 +254,17 @@ class Engineer(Role): msg = Message(content=code, role=self.profile, cause_by=WriteCodeRefine) self._rc.memory.add(msg) - code_msg = todo + FILENAME_CODE_SEP + str(file_path) - code_msg_all.append(code_msg) - - logger.info(f"Done {self.get_workspace()} generating.") - msg = Message( - content=MSG_SEP.join(code_msg_all), role=self.profile, cause_by=type(self._rc.todo), send_to="QaEngineer" - ) - return msg - - async def _act_bug_fix(self, bug_msgs) -> Message: - code_msg_all = [] # gather all code info, will pass to qa_engineer for tests later - workspace = self.get_workspace() - flag = True - # legacy_codes = legacy.split('---') - for todo in self.todos: - context = [] - - for m in bug_msgs: - if m.sent_from != "Engineer": - context.append(m.content) - context.append(m.content) - context_str = "\n".join(context) - code = [m.content for m in bug_msgs if m.sent_from == "Engineer"] - code = "\n".join(code) - - # Refine code or Write code - # if self.increment and len(legacy_codes) > 0: - # code = legacy_codes.pop(0) - - # Code review - try: - rewrite_code = await WriteCodeGuide().run(context=context_str, code=code, filename=todo) - code = rewrite_code - except Exception as e: - logger.error("code review failed!", e) - pass - - # code = await WriteCode().run(context=context_str, filename=todo) - - file_path = self.write_file(workspace, todo, code) - msg = Message(content=code, role=self.profile, cause_by=WriteCode) - self._rc.memory.add(msg) + # 利用todo作为key,去更新code_list的code + legacy_code = "" + for code_dict in code_list: + if code_dict.get("filename") == todo: + code_dict["code"] = code + legacy_code += code_dict.get("code") + "\n===\n" + # 若code_list中没有todo,则新增一个code_dict + if todo not in [code_dict.get("filename") for code_dict in code_list]: + code_dict = {"filename": todo, "code": code} + code_list.append(code_dict) + print(len(code_list)) code_msg = todo + FILENAME_CODE_SEP + str(file_path) code_msg_all.append(code_msg) diff --git a/metagpt/roles/product_manager.py b/metagpt/roles/product_manager.py index f48720430..2dc306297 100644 --- a/metagpt/roles/product_manager.py +++ b/metagpt/roles/product_manager.py @@ -52,7 +52,9 @@ class ProductManager(Role): async def _act(self) -> Message: if self.increment: logger.info(f"{self._setting}: ready to RefinePRD") - legacy = self._rc.env.get_legacy()["legacy_prd"] + legacy_dict = self._rc.env.get_legacy() + # legacy = "Legacy PRD:\n" + legacy_dict.get("legacy_prd") + "\nLegacy Code:\n" + legacy_dict.get("legacy_code") + legacy = legacy_dict.get("legacy_prd") response = await self._rc.todo.run(self._rc.history, legacy) else: diff --git a/metagpt/roles/project_manager.py b/metagpt/roles/project_manager.py index 5d4820f4a..8cf3e579f 100644 --- a/metagpt/roles/project_manager.py +++ b/metagpt/roles/project_manager.py @@ -57,7 +57,10 @@ class ProjectManager(Role): human_str = "\n".join([msg.content for msg in self._rc.memory.get_by_role("Human")]) # legacy_project_management and legacy_code legacy_dict = self._rc.env.get_legacy() - legacy_str = "Legacy Project Management:\n" + legacy_dict["legacy_project_management"] + "\nLegacy Code:\n" + legacy_dict["legacy_code"] + legacy_code = "" + for code_dict in legacy_dict.get("legacy_code"): + legacy_code += code_dict.get("code") + "\n===\n" + legacy_str = "Legacy Project Management:\n" + legacy_dict["legacy_project_management"] + "\nLegacy Code:\n" + legacy_code response = await self._rc.todo.run(self._rc.history, legacy=legacy_str) else: diff --git a/metagpt/team.py b/metagpt/team.py index ea5de4a12..d71880db9 100644 --- a/metagpt/team.py +++ b/metagpt/team.py @@ -5,6 +5,8 @@ @Author : alexanderwu @File : software_company.py """ +import os + from pydantic import BaseModel, Field from metagpt.actions import BossRequirement @@ -60,3 +62,31 @@ class Team(BaseModel): await self.environment.run() return self.environment.history + def save_legacy(self, project_path): + prd_path = os.path.join(project_path, 'docs/prd.md') + design_path = os.path.join(project_path, 'docs/system_design.md') + api_spec_and_tasks_path = os.path.join(project_path, 'docs/api_spec_and_tasks.md') + code_path = os.path.join(project_path, os.path.basename(project_path)) + with open(prd_path, 'r', encoding='utf-8') as f: + legacy_prd = f.read() + with open(design_path, 'r', encoding='utf-8') as f: + legacy_design = f.read() + with open(api_spec_and_tasks_path, 'r', encoding='utf-8') as f: + legacy_project_management = f.read() + legacy_codes = [] + for root, dirs, files in os.walk(code_path): + for file in files: + legacy_code = {} + if file.endswith('.py'): + with open(os.path.join(root, file), 'r', encoding='utf-8') as f: + legacy_code['filename'] = file + legacy_code['code'] = f.read() + legacy_codes.append(legacy_code) + legacy_dict = { + 'legacy_prd': legacy_prd, + 'legacy_design': legacy_design, + 'legacy_project_management': legacy_project_management, + 'legacy_code': legacy_codes + } + self.environment.set_legacy(legacy_dict) + diff --git a/startup.py b/startup.py index 60298ed36..1ecef65b0 100644 --- a/startup.py +++ b/startup.py @@ -20,48 +20,20 @@ from metagpt.utils.special_tokens import MSG_SEP async def startup( idea: str, - difference_description: str = "", - project_path: str = "", investment: float = 3.0, n_round: int = 5, code_review: bool = False, run_tests: bool = False, implement: bool = True, increment: bool = False, + path: str = "", + difference: str = "", ): """Run a startup. Be a boss.""" company = Team() if increment: - # 读取文件 - prd_path = os.path.join(project_path, 'docs/prd.md') - design_path = os.path.join(project_path, 'docs/system_design.md') - api_spec_and_tasks_path = os.path.join(project_path, 'docs/api_spec_and_tasks.md') - code_path = os.path.join(project_path, os.path.basename(project_path)) - - with open(prd_path, 'r', encoding='utf-8') as f: - legacy_prd = f.read() - - with open(design_path, 'r', encoding='utf-8') as f: - legacy_design = f.read() - - with open(api_spec_and_tasks_path, 'r', encoding='utf-8') as f: - legacy_project_management = f.read() - - legacy_code = '' - for root, dirs, files in os.walk(code_path): - for file in files: - if file.endswith('.py'): - with open(os.path.join(root, file), 'r', encoding='utf-8') as f: - legacy_code += f.read() + '\n---\n' - - legacy_dict = { - 'legacy_prd': legacy_prd, - 'legacy_design': legacy_design, - 'legacy_project_management': legacy_project_management, - 'legacy_code': legacy_code - } - company.environment.set_legacy(legacy_dict) + company.save_legacy(path) company.hire( [ ProductManager(increment=increment), @@ -85,20 +57,20 @@ async def startup( company.hire([QaEngineer()]) company.invest(investment) - company.start_project(idea+"\n"+difference_description) + company.start_project(idea +"\n" + difference) await company.run(n_round=n_round) def main( idea: str, - difference_description: str = "", - project_path: str = "", investment: float = 3.0, n_round: int = 5, code_review: bool = True, run_tests: bool = False, implement: bool = True, increment: bool = False, + path: str = "", + difference: str = "", ): """ We are a software startup comprised of AI. By investing in us, @@ -111,7 +83,7 @@ def main( :return: """ asyncio.run( - startup(idea, difference_description, project_path, investment, n_round, code_review, run_tests, implement, increment)) + startup(idea, investment, n_round, code_review, run_tests, implement, increment, path, difference)) if __name__ == "__main__":