Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: File summary for large files #2800

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions aider/coders/base_coder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
from aider.run_cmd import run_cmd
from aider.sendchat import RETRY_TIMEOUT, send_completion
from aider.utils import format_content, format_messages, format_tokens, is_image_file
from aider.summary_cache import SummaryCache

from ..dump import dump # noqa: F401
from .chat_chunks import ChatChunks


class UnknownEditFormat(ValueError):
def __init__(self, edit_format, valid_formats):
self.edit_format = edit_format
Expand Down Expand Up @@ -105,6 +105,7 @@ class Coder:
ignore_mentions = None
chat_language = None
file_watcher = None
summarize_file_size = 200 * 1024 # 200 KB is a rather large code file

@classmethod
def create(
Expand Down Expand Up @@ -420,6 +421,9 @@ def __init__(
if not self.repo:
self.root = utils.find_common_root(self.abs_fnames)

# Add summary cache to the coder
self.summary_cache = SummaryCache(self.io, self.root, self.main_model)

if read_only_fnames:
self.abs_read_only_fnames = set()
for fname in read_only_fnames:
Expand Down Expand Up @@ -531,9 +535,32 @@ def show_pretty(self):

return True

def get_file_content(self, fname):
if not os.path.exists(fname):
return None

try:
if os.path.getsize(fname) > self.summarize_file_size:
rel_fname = self.get_rel_fname(fname)
if self.io.confirm_ask(f"File {rel_fname} is large. Create/use a summary to reduce token usage?"):
content = self.summary_cache.get_file_summary(fname)
else:
content = self.io.read_text(fname)
else:
content = self.io.read_text(fname)
return content
except OSError:
return None

def has_file_summary(self, fname):
return self.summary_cache.has_file_summary(fname)

def get_existing_file_summary(self, fname):
return self.summary_cache.get_file_summary(fname)

def get_abs_fnames_content(self):
for fname in list(self.abs_fnames):
content = self.io.read_text(fname)
content = self.get_file_content(fname)

if content is None:
relative_fname = self.get_rel_fname(fname)
Expand Down Expand Up @@ -591,11 +618,11 @@ def get_files_content(self, fnames=None):
prompt += f"{self.fence[1]}\n"

return prompt

def get_read_only_files_content(self):
prompt = ""
for fname in self.abs_read_only_fnames:
content = self.io.read_text(fname)
content = self.get_file_content(fname)
if content is not None and not is_image_file(fname):
relative_fname = self.get_rel_fname(fname)
prompt += "\n"
Expand Down Expand Up @@ -1251,6 +1278,10 @@ def send_message(self, inp):
self.mdstream = None

retry_delay = 0.125
hourglass_chars = "⌛⏳"
hourglass_idx = 0
last_hourglass_time = 0
hourglass_delay = 0.5 # seconds between hourglass flips

litellm_ex = LiteLLMExceptions()

Expand All @@ -1260,7 +1291,16 @@ def send_message(self, inp):
try:
while True:
try:
# Show rotating hourglass while waiting
current_time = time.time()
if current_time - last_hourglass_time >= hourglass_delay:
last_hourglass_time = current_time
hourglass = hourglass_chars[hourglass_idx]
hourglass_idx = (hourglass_idx + 1) % len(hourglass_chars)
print(f"\r{hourglass}", end="", flush=True)

yield from self.send(messages, functions=self.functions)
print("\r ", end="", flush=True) # Clear the hourglass
break
except litellm_ex.exceptions_tuple() as err:
ex_info = litellm_ex.get_ex_info(err)
Expand Down Expand Up @@ -1963,8 +2003,9 @@ def check_added_files(self):
for fname in self.abs_fnames:
if is_image_file(fname):
continue
content = self.io.read_text(fname)
tokens += self.main_model.token_count(content)
content = self.get_file_content(fname)
if content:
tokens += self.main_model.token_count(content)

if tokens < warn_number_of_tokens:
return
Expand Down
74 changes: 74 additions & 0 deletions aider/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess
import sys
import tempfile
from datetime import datetime
from collections import OrderedDict
from os.path import expanduser
from pathlib import Path
Expand Down Expand Up @@ -353,6 +354,79 @@ def cmd_clear(self, args):

self._clear_chat_history()

def cmd_clear_summary_cache(self, args):
"Clear the summary cache"
try:
self.coder.summary_cache.clear()
self.io.tool_output("Summary cache cleared.")
except Exception as e:
self.io.tool_error(f"Error clearing cache: {str(e)}")

def cmd_show_summary_cache(self, args):
"Show summary cache statistics"
try:
# Show files currently loaded in memory
if self.coder.summary_cache.file_summaries:
self.io.tool_output("Currently loaded file summaries:")
for fname, summary in self.coder.summary_cache.file_summaries.items():
if summary.summary:
rel_fname = self.coder.get_rel_fname(fname)
mtime = datetime.fromtimestamp(summary.last_mtime)
date_str = mtime.strftime("%Y-%m-%d %H:%M:%S")
self.io.tool_output(
f" {rel_fname}: {date_str}, {len(summary.summary)} chars"
)
self.io.tool_output()

# Show files in disk cache but not loaded
cache_only = set(k for k in self.coder.summary_cache.cache) - set(
self.coder.summary_cache.file_summaries.keys()
)
if cache_only:
self.io.tool_output("Cached but not loaded file summaries:")
for fname in sorted(cache_only):
cached = self.coder.summary_cache.cache[fname]
rel_fname = self.coder.get_rel_fname(fname)
mtime = datetime.fromtimestamp(cached['mtime'])
date_str = mtime.strftime("%Y-%m-%d %H:%M:%S")
self.io.tool_output(
f" {rel_fname}: {date_str}, {len(cached['summary'])} chars"
)

except Exception as e:
self.io.tool_error(f"Error getting cache stats: {str(e)}")

def completions_show_summary(self):
files = self.coder.get_inchat_relative_files()
read_only_files = [self.coder.get_rel_fname(fn) for fn in self.coder.abs_read_only_fnames]
all_files = files + read_only_files
all_files = [self.quote_fname(fn) for fn in all_files]
return all_files

def cmd_show_summary(self, args):
"Show the cached summary of a file"
if not args.strip():
self.io.tool_error("Please provide a filename to show the summary for.")
return

try:
filenames = parse_quoted_filenames(args)
if not filenames:
self.io.tool_error("No valid filename provided.")
return

file_path = self.coder.abs_root_path(filenames[0])

if self.coder.has_file_summary(file_path):
summary = self.coder.get_existing_file_summary(file_path)
self.io.tool_output(summary)
return

self.io.tool_output(f"No summary found in cache for {file_path}.")

except Exception as e:
self.io.tool_error(f"Error getting cached summary: {str(e)}")

def _drop_all_files(self):
self.coder.abs_fnames = set()
self.coder.abs_read_only_fnames = set()
Expand Down
Loading
Loading