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"
}