From 52343b53407c3c5eddd7afd6c7e195f44759cd25 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sun, 25 Jun 2023 19:40:12 -0700 Subject: [PATCH] More generic handling of GPT mangling of filenames --- aider/coders/wholefile_coder.py | 9 +++++---- tests/test_wholefile.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/aider/coders/wholefile_coder.py b/aider/coders/wholefile_coder.py index 2ba813dae..16e5e7995 100644 --- a/aider/coders/wholefile_coder.py +++ b/aider/coders/wholefile_coder.py @@ -66,10 +66,11 @@ class WholeFileCoder(Coder): # fname==None ... starting a new block if i > 0: fname = lines[i - 1].strip() - path_to = "path/to/" - # gpt-3.5 will sometimes crib /path/to from the one-shot example - if fname.startswith(path_to) and fname not in chat_files: - fname = fname[len(path_to) :] + # Did gpt prepend a bogus dir? It especially likes to + # include the path/to prefix from the one-shot example in + # the prompt. + if fname and fname not in chat_files and Path(fname).name in chat_files: + fname = Path(fname).name if not fname: # blank line? or ``` was on first line i==0 if saw_fname: fname = saw_fname diff --git a/tests/test_wholefile.py b/tests/test_wholefile.py index bc9db67c1..fa62473cb 100644 --- a/tests/test_wholefile.py +++ b/tests/test_wholefile.py @@ -42,6 +42,34 @@ class TestWholeFileCoder(unittest.TestCase): updated_content = f.read() self.assertEqual(updated_content, "Updated content\n") + def test_update_files_bogus_path_prefix(self): + with tempfile.TemporaryDirectory() as temp_dir: + os.chdir(temp_dir) + + # Create a sample file in the temporary directory + sample_file = "sample.txt" + with open(sample_file, "w") as f: + f.write("Original content\n") + + # Initialize WholeFileCoder with the temporary directory + io = InputOutput(yes=True) + coder = WholeFileCoder(main_model=models.GPT35, io=io, fnames=[sample_file]) + + # Set the partial response content with the updated content + # With path/to/ prepended onto the filename + coder.partial_response_content = f"path/to/{sample_file}\n```\nUpdated content\n```" + + # Call update_files method + edited_files = coder.update_files() + + # Check if the sample file was updated + self.assertIn("sample.txt", edited_files) + + # Check if the content of the sample file was updated + with open(sample_file, "r") as f: + updated_content = f.read() + self.assertEqual(updated_content, "Updated content\n") + def test_update_files_not_in_chat(self): with tempfile.TemporaryDirectory() as temp_dir: os.chdir(temp_dir)