From 49c904eea57d4eef8e538474acfb7361ce31e3f5 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sun, 25 Feb 2024 15:34:51 -0800 Subject: [PATCH] Added a warning if the user adds many files totaling many tokens --- aider/coders/base_coder.py | 35 +++++++++++++++++++++++++++++++++++ aider/commands.py | 1 + 2 files changed, 36 insertions(+) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 10e87c79e..882e28281 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -183,6 +183,8 @@ class Coder: raise ValueError(f"{fname} is not a file") self.abs_fnames.add(str(fname.resolve())) + self.check_added_files() + if self.repo: rel_repo_dir = self.repo.get_rel_repo_dir() num_files = len(self.repo.get_tracked_files()) @@ -250,6 +252,7 @@ class Coder: def add_rel_fname(self, rel_fname): self.abs_fnames.add(self.abs_root_path(rel_fname)) + self.check_added_files() def abs_root_path(self, path): res = Path(self.root) / path @@ -857,6 +860,7 @@ class Coder: self.repo.repo.git.add(full_path) self.abs_fnames.add(full_path) + self.check_added_files() return True if not self.io.confirm_ask( @@ -869,10 +873,41 @@ class Coder: self.repo.repo.git.add(full_path) self.abs_fnames.add(full_path) + self.check_added_files() self.check_for_dirty_commit(path) return True + warning_given = False + + def check_added_files(self): + if self.warning_given: + return + + warn_number_of_files = 4 + warn_number_of_tokens = 20 * 1024 + + num_files = len(self.abs_fnames) + if num_files < warn_number_of_files: + return + + tokens = 0 + for fname in self.abs_fnames: + relative_fname = self.get_rel_fname(fname) + if is_image_file(relative_fname): + continue + content = self.io.read_text(fname) + tokens += self.main_model.token_count(content) + + if tokens < warn_number_of_tokens: + return + + self.io.tool_error("Warning: it's best to only add files that need changes to the chat.") + self.io.tool_error( + "https://aider.chat/docs/faq.html#how-can-i-add-all-the-files-to-the-chat" + ) + self.warning_given = True + apply_update_errors = 0 def prepare_to_edit(self, edits): diff --git a/aider/commands.py b/aider/commands.py index 4c723a193..0efcc3d06 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -386,6 +386,7 @@ class Commands: else: self.coder.abs_fnames.add(abs_file_path) self.io.tool_output(f"Added {matched_file} to the chat") + self.coder.check_added_files() added_fnames.append(matched_file) if not added_fnames: