From e6e360a4ef568bb2d5c4eb60ad2b0c779fcaeff8 Mon Sep 17 00:00:00 2001 From: Daniel Vainsencher Date: Fri, 7 Jun 2024 09:48:23 -0400 Subject: [PATCH] Added support for logging LLM history to a specified file. --- aider/args.py | 8 +++++++- aider/coders/base_coder.py | 13 +++++-------- aider/io.py | 17 ++++++++++++++--- aider/main.py | 1 + 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/aider/args.py b/aider/args.py index f747016dc..24682fa55 100644 --- a/aider/args.py +++ b/aider/args.py @@ -20,11 +20,17 @@ def get_parser(default_config_files, git_root): auto_env_var_prefix="AIDER_", ) group = parser.add_argument_group("Main") + group.add_argument( + "--llm-history-file", + metavar="LLM_HISTORY_FILE", + default=".aider.llm.history", + help="Specify the LLM history file (default: .aider.llm.history)", + ) group.add_argument( "files", metavar="FILE", nargs="*", - help="files to edit with an LLM (optional)", + help="files to edit with an LLM (optional)" ) group.add_argument( "--openai-api-key", diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index b1616fb95..a8ddf8830 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -784,10 +784,7 @@ class Coder: messages = self.format_messages() - timestamp = datetime.datetime.now().isoformat(timespec='seconds') - with open('.aider.llm.history', 'a') as log_file: - log_file.write(f"TO LLM {timestamp}\n") - log_file.write(format_messages(messages) + "\n") + self.io.log_llm_history("TO LLM", format_messages(messages)) if self.verbose: utils.show_messages(messages, functions=self.functions) @@ -808,6 +805,9 @@ class Coder: exhausted = True else: raise err + except Exception as err: + self.io.tool_error(f"Unexpected error: {err}") + return if exhausted: self.num_exhausted_context_windows += 1 @@ -831,10 +831,7 @@ class Coder: self.io.tool_output() - timestamp = datetime.datetime.now().isoformat(timespec='seconds') - with open('.aider.llm.history', 'a') as log_file: - log_file.write(f"LLM RESPONSE {timestamp}\n") - log_file.write(format_content("ASSISTANT", content) + "\n") + self.io.log_llm_history("LLM RESPONSE", format_content("ASSISTANT", content)) if interrupted: content += "\n^C KeyboardInterrupt" diff --git a/aider/io.py b/aider/io.py index a365aa1f0..b6f7d15e5 100644 --- a/aider/io.py +++ b/aider/io.py @@ -106,6 +106,7 @@ class InputOutput: tool_error_color="red", encoding="utf-8", dry_run=False, + llm_history_file=None, ): no_color = os.environ.get("NO_COLOR") if no_color is not None and no_color != "": @@ -125,6 +126,7 @@ class InputOutput: self.yes = yes self.input_history_file = input_history_file + self.llm_history_file = llm_history_file if chat_history_file is not None: self.chat_history_file = Path(chat_history_file) else: @@ -206,10 +208,11 @@ class InputOutput: else: style = None + completer_instance = AutoCompleter( + root, rel_fnames, addable_rel_fnames, commands, self.encoding + ) + while True: - completer_instance = AutoCompleter( - root, rel_fnames, addable_rel_fnames, commands, self.encoding - ) if multiline_input: show = ". " @@ -266,6 +269,14 @@ class InputOutput: fh = FileHistory(self.input_history_file) return fh.load_history_strings() + def log_llm_history(self, role, content): + if not self.llm_history_file: + return + timestamp = datetime.now().isoformat(timespec='seconds') + with open(self.llm_history_file, 'a', encoding=self.encoding) as log_file: + log_file.write(f"{role.upper()} {timestamp}\n") + log_file.write(content + "\n") + def user_input(self, inp, log_only=True): if not log_only: style = dict(style=self.user_input_color) if self.user_input_color else dict() diff --git a/aider/main.py b/aider/main.py index 9cf0dda10..2e878934c 100644 --- a/aider/main.py +++ b/aider/main.py @@ -258,6 +258,7 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F tool_error_color=args.tool_error_color, dry_run=args.dry_run, encoding=args.encoding, + llm_history_file=args.llm_history_file, ) fnames = [str(Path(fn).resolve()) for fn in args.files]