|
@@ -11,10 +11,10 @@ Edit Tool - 文件编辑工具
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
from typing import Optional, Generator
|
|
from typing import Optional, Generator
|
|
|
-import difflib
|
|
|
|
|
import re
|
|
import re
|
|
|
|
|
|
|
|
from agent.tools import tool, ToolResult, ToolContext
|
|
from agent.tools import tool, ToolResult, ToolContext
|
|
|
|
|
+from agent.tools.builtin.file.diff_utils import create_unified_diff as _create_diff
|
|
|
|
|
|
|
|
|
|
|
|
|
@tool(description="编辑文件,使用精确字符串替换。支持多种智能匹配策略。", hidden_params=["context"], groups=["core"], capabilities=["write"])
|
|
@tool(description="编辑文件,使用精确字符串替换。支持多种智能匹配策略。", hidden_params=["context"], groups=["core"], capabilities=["write"])
|
|
@@ -225,8 +225,6 @@ def block_anchor_replacer(content: str, find: str) -> Generator[str, None, None]
|
|
|
|
|
|
|
|
first_line_find = find_lines[0].strip()
|
|
first_line_find = find_lines[0].strip()
|
|
|
last_line_find = find_lines[-1].strip()
|
|
last_line_find = find_lines[-1].strip()
|
|
|
- find_block_size = len(find_lines)
|
|
|
|
|
-
|
|
|
|
|
# 收集所有候选位置(首尾行都匹配)
|
|
# 收集所有候选位置(首尾行都匹配)
|
|
|
candidates = []
|
|
candidates = []
|
|
|
for i in range(len(content_lines)):
|
|
for i in range(len(content_lines)):
|
|
@@ -245,8 +243,6 @@ def block_anchor_replacer(content: str, find: str) -> Generator[str, None, None]
|
|
|
# 单个候选:使用宽松阈值
|
|
# 单个候选:使用宽松阈值
|
|
|
if len(candidates) == 1:
|
|
if len(candidates) == 1:
|
|
|
start_line, end_line = candidates[0]
|
|
start_line, end_line = candidates[0]
|
|
|
- actual_block_size = end_line - start_line + 1
|
|
|
|
|
-
|
|
|
|
|
similarity = _calculate_block_similarity(
|
|
similarity = _calculate_block_similarity(
|
|
|
content_lines[start_line:end_line + 1],
|
|
content_lines[start_line:end_line + 1],
|
|
|
find_lines
|
|
find_lines
|
|
@@ -506,26 +502,3 @@ def multi_occurrence_replacer(content: str, find: str) -> Generator[str, None, N
|
|
|
break
|
|
break
|
|
|
yield find
|
|
yield find
|
|
|
start_index = index + len(find)
|
|
start_index = index + len(find)
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-# ============================================================================
|
|
|
|
|
-# 辅助函数
|
|
|
|
|
-# ============================================================================
|
|
|
|
|
-
|
|
|
|
|
-def _create_diff(filepath: str, old_content: str, new_content: str) -> str:
|
|
|
|
|
- """生成 unified diff"""
|
|
|
|
|
- old_lines = old_content.splitlines(keepends=True)
|
|
|
|
|
- new_lines = new_content.splitlines(keepends=True)
|
|
|
|
|
-
|
|
|
|
|
- diff_lines = list(difflib.unified_diff(
|
|
|
|
|
- old_lines,
|
|
|
|
|
- new_lines,
|
|
|
|
|
- fromfile=f"a/{filepath}",
|
|
|
|
|
- tofile=f"b/{filepath}",
|
|
|
|
|
- lineterm=''
|
|
|
|
|
- ))
|
|
|
|
|
-
|
|
|
|
|
- if not diff_lines:
|
|
|
|
|
- return "(无变更)"
|
|
|
|
|
-
|
|
|
|
|
- return ''.join(diff_lines)
|
|
|