Update regex patterns in AICodeAgentCog to match AI prompt format for search and replace operations

This commit is contained in:
Slipstream 2025-06-05 19:27:43 -06:00
parent 5cbf8033ec
commit 3d2a66e32a
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -976,13 +976,13 @@ class AICodeAgentCog(commands.Cog):
# Content between ------- and ======= is search_content.
# Content between ======= and >>>>>>> REPLACE is replace_content.
operation_pattern = re.compile(
r"^\s*<<<<<<< SEARCH\s*\n"
r"^\s*\[SEARCH_BLOCK_START\]\s*\n" # Changed to match AI prompt
r":start_line:(\d+)\s*\n"
r"-------\s*\n"
r"(.*?)" # search_content (group 2)
r"=======\s*\n"
r"\\=======\s*\n" # Changed to match AI prompt (\=======)
r"(.*?)" # replace_content (group 3)
r">>>>>>> REPLACE\s*$",
r"\[REPLACE_BLOCK_END\]\s*$", # Changed to match AI prompt
re.DOTALL | re.MULTILINE
)
@ -1000,14 +1000,9 @@ class AICodeAgentCog(commands.Cog):
# The (.*?) captures content up to the next \n======= or \n>>>>>>>
# We need to remove the structural newlines from the capture groups if they are included by DOTALL
# To get precise content: find the end of "-------\n" and start of "\n======="
search_content_start_offset = match.group(0).find("-------\n") + len("-------\n")
search_content_end_offset = match.group(0).find("\n=======")
search_c = match.group(0)[search_content_start_offset:search_content_end_offset]
replace_content_start_offset = match.group(0).find("=======\n") + len("=======\n")
replace_content_end_offset = match.group(0).find("\n>>>>>>> REPLACE")
replace_c = match.group(0)[replace_content_start_offset:replace_content_end_offset]
# Use regex capture groups directly for search and replace content
search_c = match.group(2)
replace_c = match.group(3)
operations.append({
"start_line": start_line,
@ -1628,13 +1623,13 @@ class AICodeAgentCog(commands.Cog):
try:
# Pattern for a single search/replace operation block (same as apply_diff)
operation_pattern = re.compile(
r"^\s*<<<<<<< SEARCH\s*\n"
r"^\s*\[SEARCH_BLOCK_START\]\s*\n" # Changed to match AI prompt
r":start_line:(\d+)\s*\n"
r"-------\s*\n"
r"(.*?)" # search_content
r"=======\s*\n"
r"(.*?)" # replace_content
r">>>>>>> REPLACE\s*$",
r"(.*?)" # search_content (group 2)
r"\\=======\s*\n" # Changed to match AI prompt (\=======)
r"(.*?)" # replace_content (group 3)
r"\[REPLACE_BLOCK_END\]\s*$", # Changed to match AI prompt
re.DOTALL | re.MULTILINE
)
@ -1647,13 +1642,9 @@ class AICodeAgentCog(commands.Cog):
return f"Dry run Error: Malformed diff_block. Unexpected content between operations near character {last_match_end}."
start_line = int(match.group(1))
search_content_start_offset = match.group(0).find("-------\n") + len("-------\n")
search_content_end_offset = match.group(0).find("\n=======")
search_c = match.group(0)[search_content_start_offset:search_content_end_offset]
replace_content_start_offset = match.group(0).find("=======\n") + len("=======\n")
replace_content_end_offset = match.group(0).find("\n>>>>>>> REPLACE")
replace_c = match.group(0)[replace_content_start_offset:replace_content_end_offset]
# Use regex capture groups directly for search and replace content
search_c = match.group(2)
replace_c = match.group(3)
operations.append({
"start_line": start_line,