Apply the patches OpenAI models write with the apply_patch tool, to strings
in memory. It handles both the V4A update diffs of the Responses API
apply_patch
tool and the *** Begin Patch format of the
Codex CLI, and it exports the Codex tool
definition.
go get github.com/fgn/applypatch// One text and one update diff.
output, err := applypatch.Apply(input, diff)
// A Codex patch over several files: add, delete, update, move.
files, err := applypatch.Codex.ApplyFiles(files, patch)
// The tool to offer a model; send its input to ApplyFiles.
tool := applypatch.CodexTool()Strict, the default, never guesses. Anchors, context, and removed lines must match byte for byte and exactly once, or the patch is an error the model can fix.Codexdoes what the Codex CLI does with line-ending preservation on: first match, then fallbacks that ignore whitespace and Unicode punctuation, insertions without context at the end of the file, and a newline after every line. It is checked against the real Codex binary.
Development tasks run through Task, which you can
install from taskfile.dev/docs/installation.
Run task to format, lint, and test, and task --list to see the rest,
including the Codex parity, fuzz, and mutation tasks.
MIT, except the parts ported from OpenAI Codex, which are Apache-2.0; see
NOTICE. Strict mode is a Go port of applyDiff from the
OpenAI Agents SDK for JavaScript
(MIT), and Codex mode, the patch parser, and the tool definition are a Go port
of apply_patch
from OpenAI Codex (Apache-2.0).