From 015ffa13e1e338ca34165f60df10bafd572dc5cb Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 Nov 2023 01:35:04 +0100 Subject: [PATCH 1/5] Fixes #374 - logging add_to_input_history if args.message is used. Fixes https://github.com/paul-gauthier/aider/issues/374 --- aider/main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/aider/main.py b/aider/main.py index e1462f195..0f08ad835 100644 --- a/aider/main.py +++ b/aider/main.py @@ -560,6 +560,7 @@ def main(argv=None, input=None, output=None, force_git_root=None): io.tool_error(f"Git working dir: {git_root}") if args.message: + io.add_to_input_history(args.message) io.tool_output() coder.run(with_message=args.message) else: From 3e81cb1c6b3506f21525108daf41b6bc1971116d Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 Nov 2023 01:52:35 +0100 Subject: [PATCH 2/5] Fixes #374 - test_main.py += unit test for --message flag. Fixes https://github.com/paul-gauthier/aider/issues/374 --- tests/test_main.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index d16e1672d..c02204217 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -196,3 +196,13 @@ class TestMain(TestCase): MockSend.side_effect = side_effect main(["--yes", fname, "--encoding", "iso-8859-15"]) + @patch("aider.main.InputOutput") + @patch("openai.ChatCompletion.create") + def test_main_message_adds_to_input_history(self, mock_chat_completion, MockInputOutput): + test_message = "test message" + mock_io_instance = MockInputOutput.return_value + mock_chat_completion.return_value = MagicMock(choices=[{"text": "mocked response"}]) + + main(["--message", test_message], input=DummyInput(), output=DummyOutput()) + + mock_io_instance.add_to_input_history.assert_called_once_with(test_message) From 743179a4bdb249839937b3c722822781ae44b362 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 30 Nov 2023 11:38:37 +0100 Subject: [PATCH 3/5] aider: Add `--message-file` flag to specify a file containing the message to send GPT, process reply, then exit (disables chat mode). # Aider chat conversation: USER: Am I correct that currently if one wants to launch aider from commandline , the way to go is to use --message flag that allows providing prompt as command line parameter? ASSISTANT: Yes, you are correct. The `--message` or `-m` flag can be used to provide a single message to send to GPT, process the reply, and then exit the application. This flag disables the interactive chat mode and is suitable for scripting or when you want to issue a single command without entering the interactive chat session. USER: Is there a way to run prompt from command line like `--message` or `-m` however by providing prompt as file ? ASSISTANT: Currently, there is no direct command-line option in the `aider` tool to provide a prompt from a file. The `--message` or `-m` flag only accepts a string as an argument. If you want to provide a prompt from a file, you would need to read the content of the file and pass it as a string to the `--message` flag. However, this functionality can be added to the `aider` tool by implementing a new command-line option that reads the content of a specified file and uses it as the prompt message. Would you like me to implement this feature for you? If so, please provide the `aider/main.py` file, and I will suggest the necessary changes. USER: How would you name such flag to match current convention and how would you describe it? ASSISTANT: Following the current naming convention used in the `aider` tool, a suitable name for the flag that allows providing a prompt from a file could be `--message-file` or `-mf`. This flag would be described as: "Specify a file containing the message to send to GPT, process the reply, then exit (disables chat mode)." This description aligns with the existing `--message` flag while clearly indicating that the input is expected to be a file containing the message rather than a direct string input. USER: could you please add such flag? ASSISTANT: To add the `--message-file` flag to the `aider` tool, we need to modify the `aider/main.py` file. Please add the `aider/main.py` file to the chat so I can provide you with the necessary changes. USER: could you please add such flag? ASSISTANT: To add the `--message-file` flag to the `aider` tool, we need to modify the `aider/main.py` file. Please add the `aider/main.py` file to the chat so I can provide you with the necessary changes. USER: could you please add such flag? ASSISTANT: To add the `--message-file` flag to the `aider` tool, we need to modify the `aider/main.py` file. Please add the `aider/main.py` file to the chat so I can provide you with the necessary changes. USER: I added these *read-write* files: /home/gw-t490/github/VariousForks/aider-patching-for-pull-requests/aider/main.py ASSISTANT: Great, now that you've added the `aider/main.py` file, I will provide you with the necessary changes to implement the `--message-file` flag. We will need to: 1. Add a new command-line argument `--message-file` to the argument parser. 2. Read the content of the file specified by `--message-file` and pass it to the `coder.run()` method. Here are the *SEARCH/REPLACE* blocks for the changes: aider/main.py ```python <<<<<<< SEARCH other_group.add_argument( "--message", "--msg", "-m", metavar="COMMAND", help="Specify a single message to send GPT, process reply then exit (disables chat mode)", ) ======= other_group.add_argument( "--message", "--msg", "-m", metavar="COMMAND", help="Specify a single message to send GPT, process reply then exit (disables chat mode)", ) other_group.add_argument( "--message-file", "-mf", metavar="MESSAGE_FILE", help="Specify a file containing the message to send GPT, process reply, then exit (disables chat mode)", ) >>>>>>> REPLACE ``` aider/main.py ```python <<<<<<< SEARCH if args.message: io.tool_output() coder.run(with_message=args.message) else: coder.run() ======= if args.message: io.tool_output() coder.run(with_message=args.message) elif args.message_file: try: with open(args.message_file, 'r', encoding='utf-8') as file: message_from_file = file.read() io.tool_output() coder.run(with_message=message_from_file) except FileNotFoundError: io.tool_error(f"Message file not found: {args.message_file}") return 1 except IOError as e: io.tool_error(f"Error reading message file: {e}") return 1 else: coder.run() >>>>>>> REPLACE ``` These changes will add the `--message-file` flag to the argument parser and handle the case where a message file is provided. If the file is found and can be read, its content will be used as the message to send to GPT. If there is an error reading the file, an error message will be displayed, and the program will exit with a status code of 1. --- aider/main.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/aider/main.py b/aider/main.py index 0f08ad835..fe4e8517c 100644 --- a/aider/main.py +++ b/aider/main.py @@ -380,6 +380,12 @@ def main(argv=None, input=None, output=None, force_git_root=None): metavar="COMMAND", help="Specify a single message to send GPT, process reply then exit (disables chat mode)", ) + other_group.add_argument( + "--message-file", + "-mf", + metavar="MESSAGE_FILE", + help="Specify a file containing the message to send GPT, process reply, then exit (disables chat mode)", + ) other_group.add_argument( "--encoding", default="utf-8", @@ -563,6 +569,18 @@ def main(argv=None, input=None, output=None, force_git_root=None): io.add_to_input_history(args.message) io.tool_output() coder.run(with_message=args.message) + elif args.message_file: + try: + with open(args.message_file, 'r', encoding='utf-8') as file: + message_from_file = file.read() + io.tool_output() + coder.run(with_message=message_from_file) + except FileNotFoundError: + io.tool_error(f"Message file not found: {args.message_file}") + return 1 + except IOError as e: + io.tool_error(f"Error reading message file: {e}") + return 1 else: coder.run() From 13ac5f0b603aacb00a665947306ffe4acc8ff722 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 30 Nov 2023 11:38:37 +0100 Subject: [PATCH 4/5] Add `--message-file` flag and unit test This commit introduces the `--message-file` flag to the `aider` tool, allowing users to specify a file containing the message to send to GPT. This feature processes the reply and then exits, disabling the chat mode. The implementation includes reading the content of the specified file and using it as the prompt message. Additionally, a unit test has been added to `tests/test_main.py` to ensure the correct functionality of the `--message-file` flag. The test includes necessary mocks to handle non-interactive environments and verifies that the tool behaves as expected when a message file is provided. This enhancement improves the usability of the `aider` tool for users who prefer or require non-interactive execution, such as in scripting or automated workflows. --- tests/test_main.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_main.py b/tests/test_main.py index c02204217..fc363ce5f 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -182,6 +182,20 @@ class TestMain(TestCase): _, kwargs = MockCoder.call_args assert kwargs["dirty_commits"] is True + @patch('aider.main.InputOutput.confirm_ask', return_value=True) + def test_message_file_flag(self, mock_confirm): + message_file_content = "This is a test message from a file." + message_file_path = "test_message.txt" + with open(message_file_path, 'w', encoding='utf-8') as message_file: + message_file.write(message_file_content) + + with patch("aider.main.Coder.create") as MockCoder: + MockCoder.return_value.run = MagicMock() + main(["--message-file", message_file_path], input=DummyInput(), output=DummyOutput()) + MockCoder.return_value.run.assert_called_once_with(with_message=message_file_content) + + os.remove(message_file_path) + def test_encodings_arg(self): fname = "foo.py" From 4692a689fef9e5b4e1d68231e3ac7e496d0e6174 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 5 Dec 2023 23:49:02 +0100 Subject: [PATCH 5/5] Improvements: flag -f (not -mf); unitest improvements. --- aider/main.py | 5 ++--- tests/test_main.py | 7 +++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/aider/main.py b/aider/main.py index fe4e8517c..fe0821d08 100644 --- a/aider/main.py +++ b/aider/main.py @@ -382,7 +382,7 @@ def main(argv=None, input=None, output=None, force_git_root=None): ) other_group.add_argument( "--message-file", - "-mf", + "-f", metavar="MESSAGE_FILE", help="Specify a file containing the message to send GPT, process reply, then exit (disables chat mode)", ) @@ -571,8 +571,7 @@ def main(argv=None, input=None, output=None, force_git_root=None): coder.run(with_message=args.message) elif args.message_file: try: - with open(args.message_file, 'r', encoding='utf-8') as file: - message_from_file = file.read() + message_from_file = io.read_text(args.message_file) io.tool_output() coder.run(with_message=message_from_file) except FileNotFoundError: diff --git a/tests/test_main.py b/tests/test_main.py index fc363ce5f..b61de9d90 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -182,16 +182,15 @@ class TestMain(TestCase): _, kwargs = MockCoder.call_args assert kwargs["dirty_commits"] is True - @patch('aider.main.InputOutput.confirm_ask', return_value=True) - def test_message_file_flag(self, mock_confirm): + def test_message_file_flag(self): message_file_content = "This is a test message from a file." - message_file_path = "test_message.txt" + message_file_path = tempfile.mktemp() with open(message_file_path, 'w', encoding='utf-8') as message_file: message_file.write(message_file_content) with patch("aider.main.Coder.create") as MockCoder: MockCoder.return_value.run = MagicMock() - main(["--message-file", message_file_path], input=DummyInput(), output=DummyOutput()) + main(["--yes", "--message-file", message_file_path], input=DummyInput(), output=DummyOutput()) MockCoder.return_value.run.assert_called_once_with(with_message=message_file_content) os.remove(message_file_path)