From e495ea90d06188e4c3f886807bb21ba3b798330a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Thu, 19 Sep 2024 15:12:19 +0800 Subject: [PATCH] fix:line numbers and line contents do not match. --- metagpt/roles/di/role_zero.py | 32 +++++++++++-------------- metagpt/tools/libs/editor.py | 9 ++++--- tests/metagpt/tools/libs/test_editor.py | 24 +++++++++---------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/metagpt/roles/di/role_zero.py b/metagpt/roles/di/role_zero.py index f93ad9058..eaba43277 100644 --- a/metagpt/roles/di/role_zero.py +++ b/metagpt/roles/di/role_zero.py @@ -248,22 +248,22 @@ class RoleZero(Role): return memory async def parse_editor_result(self, memory: list[Message]) -> list[Message]: - """Retain the latest result for each editor command and remove outdated editor results.""" - # Set to keep track of unique editor commands - record = set() + """Retain the latest result and remove outdated editor results.""" + keep_count = 5 pattern = re.compile(r"Command Editor\.(\w+) executed") - # Iterate over the memory in reverse order + new_memory = [] for msg in reversed(memory): matches = pattern.findall(msg.content) - if not matches: - continue - # If all matches are already in the record, remove the editor content - if all(match in record for match in matches): - msg.content = msg.content[: msg.content.find("Command Editor")] - else: - # Add new matches to the record - record.update(matches) - return memory + if matches: + if keep_count < 0: + new_content = msg.content[: msg.content.find("Command Editor")] + new_content += "\n".join([f"Command Editor\.{match} executed." for match in matches]) + msg = UserMessage(content=new_content) + keep_count -= 1 + new_memory.append(msg) + # Reverse the new memory list so the latest message is at the end + new_memory.reverse() + return new_memory def parse_images(self, memory: list[Message]) -> list[Message]: if not self.llm.support_image_input(): @@ -446,11 +446,7 @@ class RoleZero(Role): if command_flag.count(False) > 1: # Keep only the first exclusive command index_of_first_exclusive = command_flag.index(False) - commands = [ - cmd - for index, cmd in enumerate(commands) - if index == index_of_first_exclusive or cmd["command_name"] not in self.exclusive_tool_commands - ] + commands = commands[: index_of_first_exclusive + 1] command_rsp = "```json\n" + json.dumps(commands, indent=4, ensure_ascii=False) + "\n```" logger.info( "exclusive command more than one in current command list. change the command list.\n" + command_rsp diff --git a/metagpt/tools/libs/editor.py b/metagpt/tools/libs/editor.py index 376c86db5..1fe75f732 100644 --- a/metagpt/tools/libs/editor.py +++ b/metagpt/tools/libs/editor.py @@ -780,14 +780,17 @@ class Editor(BaseModel): if lines[line_number - 1].rstrip() != line_content: start = max(1, line_number - 3) end = min(total_lines, line_number + 3) - context = "".join( - [f"{line_number:03d}|{lines[line_number-1]}" for line_number in range(start, end + 1)] + context = "\n".join( + [ + f'The {line_number:03d} line is "{lines[line_number-1].rstrip()}"' + for line_number in range(start, end + 1) + ] ) mismatch_error += LINE_NUMBER_AND_CONTENT_MISMATCH.format( position=position, line_number=line_number, true_content=lines[line_number - 1].rstrip(), - fake_content=line_content, + fake_content=line_content.replace("\n", "\\n"), context=context.strip(), ) if mismatch_error: diff --git a/tests/metagpt/tools/libs/test_editor.py b/tests/metagpt/tools/libs/test_editor.py index 40b905e13..b56f7bf0e 100644 --- a/tests/metagpt/tools/libs/test_editor.py +++ b/tests/metagpt/tools/libs/test_editor.py @@ -554,23 +554,23 @@ Error: The `first_replaced_replaced_line_number` does not match the `first_repla The `first_replaced_replaced_line_number` is 5 and the corresponding content is " b = 2". But the `first_replaced_replaced_line_content ` is "". The content around the specified line is: -002|def test_function_for_fm(): -003| "some docstring" -004| a = 1 -005| b = 2 -006| c = 3 -007| # this is the 7th line +The 002 line is "def test_function_for_fm():" +The 003 line is " "some docstring"" +The 004 line is " a = 1" +The 005 line is " b = 2" +The 006 line is " c = 3" +The 007 line is " # this is the 7th line" Pay attention to the new content. Ensure that it aligns with the new parameters. Error: The `last_replaced_replaced_line_number` does not match the `last_replaced_replaced_line_content`. Please correct the parameters. The `last_replaced_replaced_line_number` is 5 and the corresponding content is " b = 2". But the `last_replaced_replaced_line_content ` is "". The content around the specified line is: -002|def test_function_for_fm(): -003| "some docstring" -004| a = 1 -005| b = 2 -006| c = 3 -007| # this is the 7th line +The 002 line is "def test_function_for_fm():" +The 003 line is " "some docstring"" +The 004 line is " a = 1" +The 005 line is " b = 2" +The 006 line is " c = 3" +The 007 line is " # this is the 7th line" Pay attention to the new content. Ensure that it aligns with the new parameters. """.strip()