feat: optimize editing tools

This commit is contained in:
zhayujie
2026-02-01 17:46:43 +08:00
parent 99c273a293
commit d337140577
2 changed files with 44 additions and 33 deletions

View File

@@ -305,8 +305,8 @@ def _build_memory_section(memory_manager: Any, tools: Optional[List[Any]], langu
"", "",
"在回答关于以前的工作、决定、日期、人物、偏好或待办事项的任何问题之前:", "在回答关于以前的工作、决定、日期、人物、偏好或待办事项的任何问题之前:",
"", "",
"1. 不确定信息位置 → 先用 `memory_search` 通过关键词和语义检索相关内容", "1. 不确定记忆文件位置 → 先用 `memory_search` 通过关键词和语义检索相关内容",
"2. 已知文件和大致位置 → 直接用 `memory_get` 读取相应的行", "2. 已知文件位置 → 直接用 `memory_get` 读取相应的行",
"3. search 无结果 → 尝试用 `memory_get` 读取最近两天的记忆文件", "3. search 无结果 → 尝试用 `memory_get` 读取最近两天的记忆文件",
"", "",
"**记忆文件结构**:", "**记忆文件结构**:",
@@ -317,10 +317,10 @@ def _build_memory_section(memory_manager: Any, tools: Optional[List[Any]], langu
"- 自然使用记忆,就像你本来就知道; 不用刻意提起或列举记忆,除非用户提起相关内容", "- 自然使用记忆,就像你本来就知道; 不用刻意提起或列举记忆,除非用户提起相关内容",
"", "",
"**写入记忆的正确方式**:", "**写入记忆的正确方式**:",
"- 追加到现有文件末尾 → 用 `read` 读取文件最后几行offset=-10然后用 `edit` 追加", "- 追加到现有文件末尾 → 用 `edit` 工具oldText 留空",
" 例: read(path=memory/2026-02-01.md, offset=-10) → 看到最后内容 → edit(oldText=最后几行完整文本, newText=最后几行+新内容)", " 例: edit(path=memory/2026-02-01.md, oldText=\"\", newText=\"\\n## 新内容\\n...\")",
"- 修改文件中的某段文字 → 用 `edit` 工具oldText 填写要替换的文本",
"- 创建新文件 → 用 `write`", "- 创建新文件 → 用 `write`",
"- ⚠️ 不要用 `memory_get` 读取后再 `edit`,因为会截断长文本",
"", "",
] ]

View File

@@ -22,7 +22,7 @@ class Edit(BaseTool):
"""Tool for precise file editing""" """Tool for precise file editing"""
name: str = "edit" name: str = "edit"
description: str = "Edit a file by replacing exact text. The oldText must match exactly (including whitespace). Use this for precise, surgical edits." description: str = "Edit a file by replacing exact text, or append to end if oldText is empty. For append: use empty oldText. For replace: oldText must match exactly (including whitespace)."
params: dict = { params: dict = {
"type": "object", "type": "object",
@@ -33,7 +33,7 @@ class Edit(BaseTool):
}, },
"oldText": { "oldText": {
"type": "string", "type": "string",
"description": "Exact text to find and replace (must match exactly, cannot be empty). To append to end of file, include the last few lines as oldText." "description": "Text to find and replace. Use empty string to append to end of file. For replacement: must match exactly including whitespace."
}, },
"newText": { "newText": {
"type": "string", "type": "string",
@@ -89,6 +89,17 @@ class Edit(BaseTool):
normalized_old_text = normalize_to_lf(old_text) normalized_old_text = normalize_to_lf(old_text)
normalized_new_text = normalize_to_lf(new_text) normalized_new_text = normalize_to_lf(new_text)
# Special case: empty oldText means append to end of file
if not old_text or not old_text.strip():
# Append mode: add newText to the end
# Add newline before newText if file doesn't end with one
if normalized_content and not normalized_content.endswith('\n'):
new_content = normalized_content + '\n' + normalized_new_text
else:
new_content = normalized_content + normalized_new_text
base_content = normalized_content # For verification
else:
# Normal edit mode: find and replace
# Use fuzzy matching to find old text (try exact match first, then fuzzy match) # Use fuzzy matching to find old text (try exact match first, then fuzzy match)
match_result = fuzzy_find_text(normalized_content, normalized_old_text) match_result = fuzzy_find_text(normalized_content, normalized_old_text)