Codex.Tools.ApplyPatchTool (Codex SDK v0.7.2)

Copy Markdown View Source

Hosted tool for applying Codex apply_patch edits to files.

Options

  • :base_path - Base directory for file paths (defaults to CWD)
  • :approval - Approval callback for reviewing changes before applying
  • :dry_run - If true, only validate without applying changes

Approval Callback

The approval callback can be:

  • A function with arity 1-3: fn changes -> :ok | {:deny, reason} end

  • A module implementing review_patch/2

Examples

# Basic usage (*** Begin Patch format)
args = %{"input" => patch_content}
{:ok, result} = ApplyPatchTool.invoke(args, %{base_path: "/project"})

# With approval
context = %{
  metadata: %{
    approval: fn changes, _ctx -> :ok end
  }
}
{:ok, result} = ApplyPatchTool.invoke(args, context)

# Dry run to validate
{:ok, result} = ApplyPatchTool.invoke(args, %{dry_run: true})

Summary

Functions

Applies hunks to file content.

Parses an apply_patch or unified diff patch string into a list of file changes.

Functions

apply_hunks(content, hunks)

@spec apply_hunks(String.t(), list()) :: {:ok, String.t()}

Applies hunks to file content.

Returns {:ok, new_content} or {:error, reason}.

parse_patch(patch)

@spec parse_patch(String.t()) :: {:ok, list()} | {:error, {:parse_error, String.t()}}

Parses an apply_patch or unified diff patch string into a list of file changes.