mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-26 01:06:27 +02:00
remove the outdated editor results.
This commit is contained in:
parent
b112074030
commit
a8aaece8fb
4 changed files with 68 additions and 46 deletions
|
|
@ -213,6 +213,7 @@ class RoleZero(Role):
|
|||
### Recent Observation ###
|
||||
memory = self.rc.memory.get(self.memory_k)
|
||||
memory = await self.parse_browser_actions(memory)
|
||||
memory = await self.parse_editor_result(memory)
|
||||
memory = self.parse_images(memory)
|
||||
|
||||
req = self.llm.format_msg(memory + [UserMessage(content=prompt)])
|
||||
|
|
@ -246,6 +247,24 @@ class RoleZero(Role):
|
|||
break
|
||||
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()
|
||||
pattern = re.compile(r"Command Editor\.(\w+) executed")
|
||||
# Iterate over the memory in reverse order
|
||||
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
|
||||
|
||||
def parse_images(self, memory: list[Message]) -> list[Message]:
|
||||
if not self.llm.support_image_input():
|
||||
return memory
|
||||
|
|
|
|||
|
|
@ -972,11 +972,11 @@ Editor tool is exclusive. If I use this tool, I cannot use any other commands in
|
|||
"command_name": "Editor.edit_file_by_replace",
|
||||
"args": {
|
||||
"file_name":"/workspace/MetaGPT/provider/openai_api.py",
|
||||
"start_line_number": 143,
|
||||
"start_line_content":" if event.key == pygame.K_w:",
|
||||
"first_replaced_line_number": 143,
|
||||
"first_replaced_line_content":" if event.key == pygame.K_w:",
|
||||
"new_content": " if event.key == pygame.K_UP:\\n self.move_up()\\n elif event.key == pygame.K_DOWN:\\n self.move_down()\\n elif event.key == pygame.K_SPACE:\\n self.stop()"
|
||||
"end_line_number": 146,
|
||||
"end_line_content": " self.move_down()",
|
||||
"last_replaced_line_number": 146,
|
||||
"last_replaced_line_content": " self.move_down()",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
|||
|
|
@ -54,13 +54,13 @@ Your changes have NOT been applied. Please fix your edit command and try again
|
|||
|
||||
"""
|
||||
|
||||
LINE_NUMBER_AND_CONTENT_MISMATCH = """
|
||||
Error: The `{position}_line_number` does not match the `{position}_line_content`. Please correct the parameters.
|
||||
The `{position}_line_number` is {line_number} and the corresponding content is "{true_content}".
|
||||
But the `{position}_line_content` is "{fake_content}".
|
||||
LINE_NUMBER_AND_CONTENT_MISMATCH = """Error: The `{position}_replaced_line_number` does not match the `{position}_replaced_line_content`. Please correct the parameters.
|
||||
The `{position}_replaced_line_number` is {line_number} and the corresponding content is "{true_content}".
|
||||
But the `{position}_replaced_line_content ` is "{fake_content}".
|
||||
The content around the specified line is:
|
||||
{context}
|
||||
""".strip()
|
||||
Pay attention to the new content. Ensure that it aligns with the new parameters.
|
||||
"""
|
||||
SUCCESS_EDIT_INFO = """
|
||||
[File: {file_name} ({n_total_lines} lines total after edit)]
|
||||
{window_after_applied}
|
||||
|
|
@ -689,10 +689,10 @@ class Editor(BaseModel):
|
|||
def edit_file_by_replace(
|
||||
self,
|
||||
file_name: str,
|
||||
start_line_number: int,
|
||||
start_line_content: str,
|
||||
end_line_number: int,
|
||||
end_line_content: str,
|
||||
first_replaced_line_number: int,
|
||||
first_replaced_line_content: str,
|
||||
last_replaced_line_number: int,
|
||||
last_replaced_line_content: str,
|
||||
new_content: str,
|
||||
) -> str:
|
||||
"""
|
||||
|
|
@ -713,10 +713,10 @@ class Editor(BaseModel):
|
|||
|
||||
edit_file_by_replace(
|
||||
'/workspace/example.txt',
|
||||
start_line_number=2,
|
||||
start_line_content="contain g",
|
||||
end_line_number=3,
|
||||
end_line_content="contain h",
|
||||
first_replaced_line_number =2,
|
||||
first_replaced_line_content="contain g",
|
||||
last_replaced_line_number =3,
|
||||
last_replaced_line_content="contain h",
|
||||
new_content='new content',
|
||||
)
|
||||
This will replace only the second line 2 and line 3 with "new content".
|
||||
|
|
@ -738,10 +738,10 @@ class Editor(BaseModel):
|
|||
EDITING: If you want to remove the line 2 and line 3
|
||||
edit_file_by_replace(
|
||||
'/workspace/example.txt',
|
||||
start_line_number=2,
|
||||
start_line_content="contain g",
|
||||
end_line_number=3,
|
||||
end_line_content="contain h",
|
||||
first_replaced_line_number =2,
|
||||
first_replaced_line_content="contain g",
|
||||
last_replaced_line_number =3,
|
||||
last_replaced_line_content="contain h",
|
||||
new_content='new line',
|
||||
)
|
||||
This will remove line 2 and line 3
|
||||
|
|
@ -753,17 +753,17 @@ class Editor(BaseModel):
|
|||
```
|
||||
Args:
|
||||
file_name str:The name of the file to edit.
|
||||
start_line_number int:The line number to start the edit at, starting from 1.
|
||||
start_line_content str:The content of the start replace line, according to the start_line_number.
|
||||
end_line_number int:The line number to end the edit at (inclusive), starting from 1.
|
||||
end_line_content str:The content of the end replace line, according to the end_line_number.
|
||||
first_replaced_line_number int:The line number to start the edit at, starting from 1.
|
||||
first_replaced_line_content str:The content of the start replace line, according to the first_replaced_line_number .
|
||||
last_replaced_line_number int:The line number to end the edit at (inclusive), starting from 1.
|
||||
last_replaced_line_content str:The content of the end replace line, according to the last_replaced_line_number .
|
||||
new_content str: The text to replace the current selection with, must conform to PEP8 standards.The content in the start line and end line will also be replaced.
|
||||
|
||||
"""
|
||||
|
||||
file_name = self._try_fix_path(file_name)
|
||||
|
||||
# Check if the start_line_number and end_line_number correspond to the appropriate content.
|
||||
# Check if the first_replaced_line_number and last_replaced_line_number correspond to the appropriate content.
|
||||
mismatch_error = ""
|
||||
with file_name.open() as file:
|
||||
content = file.read()
|
||||
|
|
@ -772,7 +772,10 @@ class Editor(BaseModel):
|
|||
content += "\n"
|
||||
lines = content.splitlines(True)
|
||||
total_lines = len(lines)
|
||||
check_list = [("start", start_line_number, start_line_content), ("end", end_line_number, end_line_content)]
|
||||
check_list = [
|
||||
("first_replaced", first_replaced_line_number, first_replaced_line_content),
|
||||
("last_replaced", last_replaced_line_number, last_replaced_line_content),
|
||||
]
|
||||
for position, line_number, line_content in check_list:
|
||||
if lines[line_number - 1].rstrip() != line_content:
|
||||
start = max(1, line_number - 3)
|
||||
|
|
@ -785,14 +788,14 @@ class Editor(BaseModel):
|
|||
line_number=line_number,
|
||||
true_content=lines[line_number - 1].rstrip(),
|
||||
fake_content=line_content,
|
||||
context=context,
|
||||
context=context.strip(),
|
||||
)
|
||||
if mismatch_error:
|
||||
return mismatch_error
|
||||
ret_str = self._edit_file_impl(
|
||||
file_name,
|
||||
start=start_line_number,
|
||||
end=end_line_number,
|
||||
start=first_replaced_line_number,
|
||||
end=last_replaced_line_number,
|
||||
content=new_content,
|
||||
)
|
||||
# TODO: automatically tries to fix linter error (maybe involve some static analysis tools on the location near the edit to figure out indentation)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue