apply_patch 工具

用结构化的补丁格式来应用文件变更。当单个 edit 调用容易出错时,这个工具特别适合多文件或多代码块的编辑。

该工具接受一个 input 字符串,里面包含一个或多个文件操作:

*** Begin Patch
*** Add File: path/to/file.txt
+line 1
+line 2
*** Update File: src/app.ts
@@
-old line
+new line
*** Delete File: obsolete.txt
*** End Patch

参数

  • input(必填):完整的补丁内容,包括 *** Begin Patch*** End Patch

注意事项

  • 补丁路径支持相对路径(基于工作区目录)和绝对路径。
  • tools.exec.applyPatch.workspaceOnly 默认为 true(限制在工作区内)。只有在你确实需要 apply_patch 写入/删除工作区目录之外的文件时,才设为 false
  • *** Update File: 块中使用 *** Move to: 可以重命名文件。
  • *** End of File 用于标记需要在文件末尾插入的内容。
  • 实验性功能,默认禁用。通过 tools.exec.applyPatch.enabled 启用。
  • 仅限 OpenAI(含 OpenAI Codex)。可通过 tools.exec.applyPatch.allowModels 按模型进行门控。
  • 配置项仅在 tools.exec 下面。

示例

{
  "tool": "apply_patch",
  "input": "*** Begin Patch\n*** Update File: src/index.ts\n@@\n-const foo = 1\n+const foo = 2\n*** End Patch"
}