@nhtio/adk/lib/patch
The structured apply_patch envelope: parser and applier for the *** Begin Patch multi-file dialect.
Remarks
Shared structured-patch primitives for workspace-shaped batteries. This dialect derives from the GitHub Copilot apply_patch format — one of the most robust and battle-tested patch formats in production use, which is also why models already know how to write it. The grammar (*** Begin Patch … *** End Patch, *** Add File: / *** Delete File: / *** Update File: (+ *** Move to:), @@ context hunks with +/-/space lines) is preserved exactly — inventing a "better" variation would forfeit the pretraining. Ported from the source server's doc.apply_patch adapter (its green e2e suite informs the spec coverage); the context matcher rejects ambiguity (a hunk whose context matches more than one location fails rather than guessing).
Interfaces
| Interface | Description |
|---|---|
| AddOperation | *** Add File: — create a new file from + lines. |
| DeleteOperation | *** Delete File: — remove a file. |
| ParsedApplyPatch | The parsed envelope: ordered operations plus totals. |
| ParsedHunk | One parsed @@ hunk: the context+removal lines and their replacement. |
| UpdateOperation | *** Update File: (+ optional *** Move to:) — apply hunks, optionally rename. |
| WorkspaceFile | One file in the virtual workspace a structured patch operates over. |
Type Aliases
| Type Alias | Description |
|---|---|
| PatchOperation | Any one operation of a structured patch. |
Functions
| Function | Description |
|---|---|
| applyOperations | Apply a parsed structured patch to a virtual workspace of files keyed by normalized path. |
| applyUpdateHunks | Apply an update operation's hunks to inputText. Context matching is exact and rejects ambiguity: a hunk whose old-lines match more than one location past the cursor fails rather than guessing. |
| inferTextMimeFromPath | Infer a text MIME from a workspace path's extension (Add File outputs). |
| isStructuredPatch | true when patch is the structured envelope rather than a unified diff. |
| normalizeWorkspacePath | Normalize a workspace path: relative, no ./../empty segments, forward slashes. |
| parseStructuredPatch | Parse a structured *** Begin Patch envelope. |