apply_patch 工具允许使用结构化补丁格式应用文件更改,非常适合多文件或多代码块的编辑。
概述
apply_patch 是一个实验性工具,用于通过结构化补丁格式对文件进行批量修改。它支持添加、更新和删除文件操作。
注意
此工具为实验性功能,默认禁用。仅支持 OpenAI 模型。需要在配置中启用。
补丁格式
补丁必须包含在 *** Begin Patch *** 和 *** End Patch *** 标记之间:
添加文件
*** Begin Patch ***
*** Add File: path/to/file.txt
+line 1
+line 2
+line 3
*** End Patch ***
更新文件
*** Begin Patch ***
*** Update File: src/app.ts
@@
-const foo = 1
+const foo = 2
@@
-const bar = 'old'
+const bar = 'new'
*** End Patch ***
删除文件
*** Begin Patch ***
*** Delete File: obsolete.txt
*** End Patch ***
重命名文件
*** Begin Patch ***
*** Update File: old-name.ts
*** Move to: new-name.ts
*** End Patch ***
工具参数
input(必需): 完整的补丁内容,包含*** Begin Patch ***和*** End Patch ***
使用示例
{
"tool": "apply_patch",
"input": "*** Begin Patch ***\n*** Update File: src/index.ts\n@@\n-const foo = 1\n+const foo = 2\n*** End Patch ***"
}
配置启用
在配置文件中启用 apply_patch 工具:
{
"tools": {
"exec": {
"applyPatch": {
"enabled": true
}
}
}
}
注意事项
- 所有路径相对于工作空间根目录
*** Move to:用于在更新操作中重命名文件*** End of File标记仅在文件末尾插入内容时使用- 实验性功能,可能在未来版本中变更
- 仅支持 OpenAI 模型
- 需要在
tools.exec配置下启用
使用场景
- 批量重构: 一次性修改多个文件
- 项目初始化: 快速创建项目结构
- 配置更新: 批量更新配置文件
- 代码迁移: 执行大规模代码变更