go-v4a
Context-anchored V4A diff engine for LLM-powered file editing — Go library.
V4A hunks use @@ text anchors instead of line numbers, so patches stay resilient when the file changes between turns. This package parses V4A diffs, applies them to source text, and returns the patched result plus structured line metadata.
Pure text-to-text API with no filesystem I/O. Reading, writing, and deleting files is left to the consumer.
Install
go get github.com/b805rohit/go-v4a@latest
Usage
package main
import (
"fmt"
gov4a "github.com/b805rohit/go-v4a"
)
func main() {
result, err := gov4a.Apply(
"function add(a, b) {\n return a - b\n}",
"@@\n function add(a, b) {\n- return a - b\n+ return a + b\n }",
gov4a.ModeUpdate,
)
if err != nil {
panic(err)
}
fmt.Println(result.Text)
// function add(a, b) {
// return a + b
// }
refined := gov4a.Refine(result.Diff)
_ = refined
}
Modes
| Mode |
Purpose |
ModeUpdate (default) |
Apply hunks to existing content |
ModeCreate |
Build a new file from + lines |
ModeDelete |
Empty text; structured delete diff from source |
ModeMove |
Same as update (rename is handled by the caller) |
Envelope
result, err := gov4a.Apply(
"const x = 1",
"*** Begin Patch\n*** Update File: /src/config.ts\n@@\n-const x = 1\n+const x = 2\n*** End Patch",
gov4a.ModeUpdate,
)
API
func Apply(sourceText, diffText string, mode Mode) (Result, error)
func Refine(diff []DiffLine) []DiffLine
Result contains Text (patched), Diff (line ops with optional OldLine/NewLine), and Source (original for rollback).
Refine attaches grapheme-level Segments on paired delete/add lines.
*** Begin Patch
*** Update File: {path}
@@ {anchor_text}
context line (space prefix)
-removed line
+added line
*** End Patch
- Bare
@@ means start of file.
- Headers also include
*** Add File:, *** Delete File:, *** Move to:.
LLM tool schemas: schema/.
Testing
go test ./...
Acknowledgements
Clean-room Go port of the V4A context-anchored diff format.
Based on the V4A format and reference implementations by OpenAI:
License
MIT — see LICENSE.