feat(PHASE-06): 最小 Edit 工具 + 工具优先级优化(修改文件场景大幅提升)
This commit is contained in:
parent
67f6467da0
commit
103725e652
@ -38,9 +38,10 @@
|
|||||||
|
|
||||||
- `Read`:用于读取文件内容。
|
- `Read`:用于读取文件内容。
|
||||||
- `Glob`:用于按模式查找文件。
|
- `Glob`:用于按模式查找文件。
|
||||||
- 创建或覆盖文件时,优先使用 `Write` 工具。
|
- 修改已有文件内容时,优先使用 `Edit` 工具。
|
||||||
|
- 创建新文件时,优先使用 `Write` 工具。
|
||||||
- `Bash`:用于执行必须通过 shell 完成的最小命令。
|
- `Bash`:用于执行必须通过 shell 完成的最小命令。
|
||||||
- 只有在 Bash 确实必要时才使用 Bash。
|
- 只有在确实需要 shell 特性时才使用 Bash。
|
||||||
- 不要用 Bash 拼接文件内容。
|
- 不要用 Bash 拼接文件内容。
|
||||||
- Windows 环境下优先使用兼容写法,不默认使用 `cat <<EOF`、`ls -la` 等 Unix 风格写法。
|
- Windows 环境下优先使用兼容写法,不默认使用 `cat <<EOF`、`ls -la` 等 Unix 风格写法。
|
||||||
- 同一写入或创建目标,最多尝试 2 种不同 Bash 方案。
|
- 同一写入或创建目标,最多尝试 2 种不同 Bash 方案。
|
||||||
|
|||||||
@ -20,6 +20,7 @@
|
|||||||
- 重构时:
|
- 重构时:
|
||||||
- 先找全部用法
|
- 先找全部用法
|
||||||
- 避免修改无关文件
|
- 避免修改无关文件
|
||||||
|
- 修改已有文件时,优先使用 `Edit`,而不是 `Bash` 或 `Write`
|
||||||
- 需要创建或覆盖文件时,优先使用 `Write` 工具,而不是 `Bash`
|
- 需要创建或覆盖文件时,优先使用 `Write` 工具,而不是 `Bash`
|
||||||
- Windows 下先验证 shell 兼容性,再选择命令写法
|
- Windows 下先验证 shell 兼容性,再选择命令写法
|
||||||
|
|
||||||
|
|||||||
@ -43,6 +43,21 @@ def build_default_tools(workspace: Path) -> list[Tool]:
|
|||||||
},
|
},
|
||||||
execute=lambda data: glob_tool(workspace, data),
|
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(
|
Tool(
|
||||||
name="Write",
|
name="Write",
|
||||||
description="创建或覆盖工作区内的文本文件。",
|
description="创建或覆盖工作区内的文本文件。",
|
||||||
@ -97,6 +112,18 @@ def glob_tool(workspace: Path, data: dict[str, Any]) -> str:
|
|||||||
return "\n".join(matches[:200])
|
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:
|
def write_tool(workspace: Path, data: dict[str, Any]) -> str:
|
||||||
path = _safe_path(workspace, str(data["path"]))
|
path = _safe_path(workspace, str(data["path"]))
|
||||||
if path.exists() and path.is_dir():
|
if path.exists() and path.is_dir():
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user