|
| 1 | +""" |
| 2 | +Gives the LLM agent the ability to patch files, by using a adapted version git conflict markers. |
| 3 | +
|
| 4 | +Inspired by aider. |
| 5 | +""" |
| 6 | + |
| 7 | +import re |
| 8 | +from pathlib import Path |
| 9 | +from typing import Generator |
| 10 | + |
| 11 | +from ..message import Message |
| 12 | +from ..util import ask_execute |
| 13 | + |
| 14 | +example_patch = """ |
| 15 | +```patch filename.py |
| 16 | +<<<<<<< ORIGINAL |
| 17 | +original lines |
| 18 | +======= |
| 19 | +modified lines |
| 20 | +>>>>>>> UPDATED |
| 21 | +``` |
| 22 | +""" |
| 23 | + |
| 24 | +instructions = """ |
| 25 | +# Patching files |
| 26 | +
|
| 27 | +The LLM agent can patch files, by using a adapted version git conflict markers. |
| 28 | +This can be used to make changes to files we have written in the past, without having to rewrite the whole file. |
| 29 | +
|
| 30 | +## Example |
| 31 | +
|
| 32 | +> User: Patch the file `hello.py` to ask for the name of the user. |
| 33 | +```hello.py |
| 34 | +def hello(): |
| 35 | + print("hello world") |
| 36 | +``` |
| 37 | +
|
| 38 | +> Assistant: |
| 39 | +```patch hello.py |
| 40 | +<<<<<<< ORIGINAL |
| 41 | + print("hello world") |
| 42 | +======= |
| 43 | + name = input("What is your name? ") |
| 44 | + print(f"hello {name}") |
| 45 | +>>>>>>> UPDATED |
| 46 | +``` |
| 47 | +""" |
| 48 | + |
| 49 | + |
| 50 | +def apply(codeblock: str, content: str) -> str: |
| 51 | + """ |
| 52 | + Applies the patch to the file. |
| 53 | + """ |
| 54 | + codeblock = codeblock.strip() |
| 55 | + |
| 56 | + # get the original chunk |
| 57 | + original = re.split("\n<<<<<<< ORIGINAL\n", codeblock)[1] |
| 58 | + original = re.split("\n=======\n", original)[0] |
| 59 | + |
| 60 | + # get the modified chunk |
| 61 | + modified = re.split("\n=======\n", codeblock)[1] |
| 62 | + modified = re.split("\n>>>>>>> UPDATED\n", modified)[0] |
| 63 | + |
| 64 | + # replace the original chunk with the modified chunk |
| 65 | + content = content.replace(original, modified) |
| 66 | + |
| 67 | + return content |
| 68 | + |
| 69 | + |
| 70 | +def apply_file(codeblock, filename): |
| 71 | + codeblock = codeblock.strip() |
| 72 | + _patch, filename = codeblock.splitlines()[0].split() |
| 73 | + assert _patch == "```patch" |
| 74 | + assert Path(filename).exists() |
| 75 | + |
| 76 | + with open(filename, "r") as f: |
| 77 | + content = f.read() |
| 78 | + |
| 79 | + result = apply(codeblock, content) |
| 80 | + |
| 81 | + with open(filename, "w") as f: |
| 82 | + f.write(result) |
| 83 | + |
| 84 | + print(f"Applied patch to {filename}") |
| 85 | + |
| 86 | + |
| 87 | +def execute_patch(codeblock: str, fn: str, ask: bool) -> Generator[Message, None, None]: |
| 88 | + """ |
| 89 | + Executes the patch. |
| 90 | + """ |
| 91 | + if ask: |
| 92 | + confirm = ask_execute() |
| 93 | + if not confirm: |
| 94 | + print("Patch not applied") |
| 95 | + return |
| 96 | + |
| 97 | + apply_file(codeblock, fn) |
| 98 | + yield Message("system", "Patch applied") |
| 99 | + |
| 100 | + |
| 101 | +def test_apply_simple(): |
| 102 | + codeblock = example_patch |
| 103 | + content = """original lines""" |
| 104 | + result = apply(codeblock, content) |
| 105 | + assert result == """modified lines""" |
| 106 | + |
| 107 | + |
| 108 | +def test_apply_function(): |
| 109 | + content = """ |
| 110 | +def hello(): |
| 111 | + print("hello") |
| 112 | +
|
| 113 | +if __name__ == "__main__": |
| 114 | + hello() |
| 115 | +""" |
| 116 | + |
| 117 | + codeblock = """ |
| 118 | +```patch test.py |
| 119 | +<<<<<<< ORIGINAL |
| 120 | +def hello(): |
| 121 | + print("hello") |
| 122 | +======= |
| 123 | +def hello(name="world"): |
| 124 | + print(f"hello {name}") |
| 125 | +>>>>>>> UPDATED |
| 126 | +``` |
| 127 | +""" |
| 128 | + |
| 129 | + result = apply(codeblock, content) |
| 130 | + assert result.startswith( |
| 131 | + """ |
| 132 | +def hello(name="world"): |
| 133 | + print(f"hello {name}") |
| 134 | +""" |
| 135 | + ) |
0 commit comments