Added a warning if the user adds many files totaling many tokens

This commit is contained in:
Paul Gauthier 2024-02-25 15:34:51 -08:00
parent 98db78887d
commit 49c904eea5
2 changed files with 36 additions and 0 deletions

View file

@ -183,6 +183,8 @@ class Coder:
raise ValueError(f"{fname} is not a file") raise ValueError(f"{fname} is not a file")
self.abs_fnames.add(str(fname.resolve())) self.abs_fnames.add(str(fname.resolve()))
self.check_added_files()
if self.repo: if self.repo:
rel_repo_dir = self.repo.get_rel_repo_dir() rel_repo_dir = self.repo.get_rel_repo_dir()
num_files = len(self.repo.get_tracked_files()) num_files = len(self.repo.get_tracked_files())
@ -250,6 +252,7 @@ class Coder:
def add_rel_fname(self, rel_fname): def add_rel_fname(self, rel_fname):
self.abs_fnames.add(self.abs_root_path(rel_fname)) self.abs_fnames.add(self.abs_root_path(rel_fname))
self.check_added_files()
def abs_root_path(self, path): def abs_root_path(self, path):
res = Path(self.root) / path res = Path(self.root) / path
@ -857,6 +860,7 @@ class Coder:
self.repo.repo.git.add(full_path) self.repo.repo.git.add(full_path)
self.abs_fnames.add(full_path) self.abs_fnames.add(full_path)
self.check_added_files()
return True return True
if not self.io.confirm_ask( if not self.io.confirm_ask(
@ -869,10 +873,41 @@ class Coder:
self.repo.repo.git.add(full_path) self.repo.repo.git.add(full_path)
self.abs_fnames.add(full_path) self.abs_fnames.add(full_path)
self.check_added_files()
self.check_for_dirty_commit(path) self.check_for_dirty_commit(path)
return True 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 apply_update_errors = 0
def prepare_to_edit(self, edits): def prepare_to_edit(self, edits):

View file

@ -386,6 +386,7 @@ class Commands:
else: else:
self.coder.abs_fnames.add(abs_file_path) self.coder.abs_fnames.add(abs_file_path)
self.io.tool_output(f"Added {matched_file} to the chat") self.io.tool_output(f"Added {matched_file} to the chat")
self.coder.check_added_files()
added_fnames.append(matched_file) added_fnames.append(matched_file)
if not added_fnames: if not added_fnames: