From 4b0192ffd9301e5eb09de9411e9e70483324783b Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 3 Jul 2024 20:08:51 -0300 Subject: [PATCH] cache the completions in AutoCompleter --- aider/commands.py | 30 ++++++++++++++++-------------- aider/io.py | 20 ++++++++++++-------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 3012e2b56..ca6875986 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -41,7 +41,7 @@ class Commands: raise SwitchModel(model) def completions_model(self): - models = [] # litellm.model_cost.keys() + models = litellm.model_cost.keys() return models def cmd_models(self, args): @@ -79,20 +79,22 @@ class Commands: def is_command(self, inp): return inp[0] in "/!" - def get_commands(self, with_completions=False): - commands = dict() + def get_completions(self, cmd): + assert cmd.startswith("/") + cmd = cmd[1:] + + fun = getattr(self, f"completions_{cmd}", None) + if not fun: + return [] + return sorted(fun()) + + def get_commands(self): + commands = [] for attr in dir(self): if not attr.startswith("cmd_"): continue - cmd = attr[4:] - completions = getattr(self, f"completions_{cmd}", None) - if completions and with_completions: - completions = sorted(completions()) - else: - completions = [] - - commands["/" + cmd] = completions + commands.append("/" + cmd) return commands @@ -112,7 +114,7 @@ class Commands: first_word = words[0] rest_inp = inp[len(words[0]) :] - all_commands = self.get_commands().keys() + all_commands = self.get_commands() matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)] return matching_commands, first_word, rest_inp @@ -618,7 +620,7 @@ class Commands: def cmd_help(self, args): "Show help about all commands" - commands = sorted(self.get_commands().keys()) + commands = sorted(self.get_commands()) for cmd in commands: cmd_method_name = f"cmd_{cmd[1:]}" cmd_method = getattr(self, cmd_method_name, None) @@ -632,7 +634,7 @@ class Commands: "Show help about all commands in markdown" res = "" - commands = sorted(self.get_commands().keys()) + commands = sorted(self.get_commands()) for cmd in commands: cmd_method_name = f"cmd_{cmd[1:]}" cmd_method = getattr(self, cmd_method_name, None) diff --git a/aider/io.py b/aider/io.py index b08033df1..160c855a2 100644 --- a/aider/io.py +++ b/aider/io.py @@ -36,12 +36,9 @@ class AutoCompleter(Completer): self.words = set() - import time - - start = time.time() - self.commands = commands.get_commands(with_completions=True) - dur = time.time() - start - dump(dur) + self.commands = commands + self.command_names = self.commands.get_commands() + self.command_completions = dict() for rel_fname in addable_rel_fnames: self.words.add(rel_fname) @@ -71,7 +68,7 @@ class AutoCompleter(Completer): if text[0] == "/": if len(words) == 1 and not text[-1].isspace(): partial = words[0] - candidates = self.commands.keys() + candidates = self.command_names for cmd in candidates: if cmd.startswith(partial): yield Completion(cmd, start_position=-len(partial)) @@ -79,7 +76,14 @@ class AutoCompleter(Completer): cmd = words[0] partial = words[-1] - candidates = self.commands.get(cmd, []) + if cmd not in self.command_names: + return + if cmd not in self.command_completions: + candidates = self.commands.get_completions(cmd) + self.command_completions[cmd] = candidates + else: + candidates = self.command_completions[cmd] + for word in candidates: if partial in word: yield Completion(word, start_position=-len(partial))