fix:line numbers and line contents do not match.

This commit is contained in:
黄伟韬 2024-09-19 15:12:19 +08:00
parent a8aaece8fb
commit e495ea90d0
3 changed files with 32 additions and 33 deletions

View file

@ -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

View file

@ -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:

View file

@ -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()