feat(PHASE-06): 最小 Edit 工具 + 工具优先级优化(修改文件场景大幅提升)
This commit is contained in:
@@ -43,6 +43,21 @@ def build_default_tools(workspace: Path) -> list[Tool]:
|
||||
},
|
||||
execute=lambda data: glob_tool(workspace, data),
|
||||
),
|
||||
Tool(
|
||||
name="Edit",
|
||||
description="覆盖已有文本文件的完整内容。",
|
||||
input_schema={
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"path": {"type": "string", "description": "相对工作区的文件路径"},
|
||||
"content": {"type": "string", "description": "替换后的完整文件内容"},
|
||||
"encoding": {"type": "string", "description": "文件编码,默认 utf-8"},
|
||||
},
|
||||
"required": ["path", "content"],
|
||||
"additionalProperties": False,
|
||||
},
|
||||
execute=lambda data: edit_tool(workspace, data),
|
||||
),
|
||||
Tool(
|
||||
name="Write",
|
||||
description="创建或覆盖工作区内的文本文件。",
|
||||
@@ -97,6 +112,18 @@ def glob_tool(workspace: Path, data: dict[str, Any]) -> str:
|
||||
return "\n".join(matches[:200])
|
||||
|
||||
|
||||
def edit_tool(workspace: Path, data: dict[str, Any]) -> str:
|
||||
path = _safe_path(workspace, str(data["path"]))
|
||||
if not path.exists():
|
||||
return f"文件不存在: {path.relative_to(workspace)}"
|
||||
if path.is_dir():
|
||||
return f"目标是目录,不能编辑文件: {path.relative_to(workspace)}"
|
||||
|
||||
encoding = str(data.get("encoding") or "utf-8")
|
||||
path.write_text(str(data["content"]), encoding=encoding)
|
||||
return f"已修改文件: {path.relative_to(workspace)}"
|
||||
|
||||
|
||||
def write_tool(workspace: Path, data: dict[str, Any]) -> str:
|
||||
path = _safe_path(workspace, str(data["path"]))
|
||||
if path.exists() and path.is_dir():
|
||||
|
||||
Reference in New Issue
Block a user