fix: improve modify_file failure hints

This commit is contained in:
JOJO 2025-11-14 22:59:51 +08:00
parent 8709dda329
commit a58d695f13

View File

@ -2128,6 +2128,22 @@ async def handle_task_with_sender(terminal: WebTerminal, message, sender, client
block_reports = {} block_reports = {}
detected_indices = set() detected_indices = set()
block_pattern = re.compile(r"\[replace:(\d+)\](.*?)\[/replace\]", re.DOTALL) block_pattern = re.compile(r"\[replace:(\d+)\](.*?)\[/replace\]", re.DOTALL)
structure_warnings: List[str] = []
structure_detail_entries: List[Dict] = []
def record_structure_warning(message: str, hint: Optional[str] = None):
"""记录结构性缺陷,便于给出更具体的反馈。"""
if message in structure_warnings:
return
structure_warnings.append(message)
structure_detail_entries.append({
"index": 0,
"status": "failed",
"reason": message,
"removed_lines": 0,
"added_lines": 0,
"hint": hint or "请严格按照模板输出:[replace:n] + <<OLD>>/<<NEW>> + [/replace],并使用 <<<END_MODIFY>>> 收尾。"
})
def extract_segment(body: str, tag: str): def extract_segment(body: str, tag: str):
marker = f"<<{tag}>>" marker = f"<<{tag}>>"
@ -2176,6 +2192,30 @@ async def handle_task_with_sender(terminal: WebTerminal, message, sender, client
"error": old_error or new_error "error": old_error or new_error
}) })
if not blocks_info:
has_replace_start = bool(re.search(r"\[replace:\s*\d+\]", apply_text))
has_replace_end = "[/replace]" in apply_text
has_old_tag = "<<OLD>>" in apply_text
has_new_tag = "<<NEW>>" in apply_text
if has_replace_start and not has_replace_end:
record_structure_warning("检测到 [replace:n] 标记但缺少对应的 [/replace] 结束标记。")
if has_replace_end and not has_replace_start:
record_structure_warning("检测到 [/replace] 结束标记但缺少对应的 [replace:n] 起始标记。")
old_tags = len(re.findall(r"<<OLD>>", apply_text))
completed_old_tags = len(re.findall(r"<<OLD>>[\s\S]*?<<END>>", apply_text))
if old_tags and completed_old_tags < old_tags:
record_structure_warning("检测到 <<OLD>> 段落但未看到对应的 <<END>> 结束标记。")
new_tags = len(re.findall(r"<<NEW>>", apply_text))
completed_new_tags = len(re.findall(r"<<NEW>>[\s\S]*?<<END>>", apply_text))
if new_tags and completed_new_tags < new_tags:
record_structure_warning("检测到 <<NEW>> 段落但未看到对应的 <<END>> 结束标记。")
if (has_replace_start or has_replace_end or has_old_tag or has_new_tag) and not structure_warnings:
record_structure_warning("检测到部分补丁标记,但整体结构不完整,请严格按照模板填写所有标记。")
total_blocks = len(blocks_info) total_blocks = len(blocks_info)
result["total_blocks"] = total_blocks result["total_blocks"] = total_blocks
if forced: if forced:
@ -2240,11 +2280,15 @@ async def handle_task_with_sender(terminal: WebTerminal, message, sender, client
result["completed_blocks"] = completed_blocks result["completed_blocks"] = completed_blocks
result["failed_blocks"] = failed_blocks result["failed_blocks"] = failed_blocks
result["details"] = sorted(block_reports.values(), key=lambda x: x["index"]) details = sorted(block_reports.values(), key=lambda x: x["index"])
if structure_detail_entries:
details = structure_detail_entries + details
result["details"] = details
summary_parts = [] summary_parts = []
if total_blocks == 0: if total_blocks == 0:
summary_parts.append("未检测到有效的修改块,未执行任何修改。") summary_parts.append("未检测到有效的修改块,未执行任何修改。")
summary_parts.extend(structure_warnings)
else: else:
if not completed_blocks and failed_blocks: if not completed_blocks and failed_blocks:
summary_parts.append(f"共检测到 {total_blocks} 个修改块,全部未执行。") summary_parts.append(f"共检测到 {total_blocks} 个修改块,全部未执行。")