mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-06 12:45:00 +00:00
roughed in faster completions
This commit is contained in:
parent
d403f37469
commit
60e838df9f
2 changed files with 50 additions and 42 deletions
|
@ -5,7 +5,6 @@ import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import git
|
import git
|
||||||
from prompt_toolkit.completion import Completion
|
|
||||||
|
|
||||||
from aider import models, prompts, voice
|
from aider import models, prompts, voice
|
||||||
from aider.litellm import litellm
|
from aider.litellm import litellm
|
||||||
|
@ -41,11 +40,9 @@ class Commands:
|
||||||
models.sanity_check_models(self.io, model)
|
models.sanity_check_models(self.io, model)
|
||||||
raise SwitchModel(model)
|
raise SwitchModel(model)
|
||||||
|
|
||||||
def completions_model(self, partial):
|
def completions_model(self):
|
||||||
models = litellm.model_cost.keys()
|
models = [] # litellm.model_cost.keys()
|
||||||
for model in models:
|
return models
|
||||||
if partial.lower() in model.lower():
|
|
||||||
yield Completion(model, start_position=-len(partial))
|
|
||||||
|
|
||||||
def cmd_models(self, args):
|
def cmd_models(self, args):
|
||||||
"Search the list of available models"
|
"Search the list of available models"
|
||||||
|
@ -82,21 +79,23 @@ class Commands:
|
||||||
def is_command(self, inp):
|
def is_command(self, inp):
|
||||||
return inp[0] in "/!"
|
return inp[0] in "/!"
|
||||||
|
|
||||||
def get_commands(self):
|
def get_commands(self, with_completions=False):
|
||||||
commands = []
|
commands = dict()
|
||||||
for attr in dir(self):
|
for attr in dir(self):
|
||||||
if attr.startswith("cmd_"):
|
if not attr.startswith("cmd_"):
|
||||||
commands.append("/" + attr[4:])
|
continue
|
||||||
|
|
||||||
|
cmd = attr[4:]
|
||||||
|
completions = getattr(self, f"completions_{cmd}", None)
|
||||||
|
if completions and with_completions:
|
||||||
|
completions = sorted(completions())
|
||||||
|
else:
|
||||||
|
completions = []
|
||||||
|
|
||||||
|
commands["/" + cmd] = completions
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
|
|
||||||
def get_command_completions(self, cmd_name, partial):
|
|
||||||
cmd_completions_method_name = f"completions_{cmd_name}"
|
|
||||||
cmd_completions_method = getattr(self, cmd_completions_method_name, None)
|
|
||||||
if cmd_completions_method:
|
|
||||||
for completion in cmd_completions_method(partial):
|
|
||||||
yield completion
|
|
||||||
|
|
||||||
def do_run(self, cmd_name, args):
|
def do_run(self, cmd_name, args):
|
||||||
cmd_method_name = f"cmd_{cmd_name}"
|
cmd_method_name = f"cmd_{cmd_name}"
|
||||||
cmd_method = getattr(self, cmd_method_name, None)
|
cmd_method = getattr(self, cmd_method_name, None)
|
||||||
|
@ -113,7 +112,7 @@ class Commands:
|
||||||
first_word = words[0]
|
first_word = words[0]
|
||||||
rest_inp = inp[len(words[0]) :]
|
rest_inp = inp[len(words[0]) :]
|
||||||
|
|
||||||
all_commands = self.get_commands()
|
all_commands = self.get_commands().keys()
|
||||||
matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)]
|
matching_commands = [cmd for cmd in all_commands if cmd.startswith(first_word)]
|
||||||
return matching_commands, first_word, rest_inp
|
return matching_commands, first_word, rest_inp
|
||||||
|
|
||||||
|
@ -377,12 +376,10 @@ class Commands:
|
||||||
fname = f'"{fname}"'
|
fname = f'"{fname}"'
|
||||||
return fname
|
return fname
|
||||||
|
|
||||||
def completions_add(self, partial):
|
def completions_add(self):
|
||||||
files = set(self.coder.get_all_relative_files())
|
files = set(self.coder.get_all_relative_files())
|
||||||
files = files - set(self.coder.get_inchat_relative_files())
|
files = files - set(self.coder.get_inchat_relative_files())
|
||||||
for fname in files:
|
return files
|
||||||
if partial.lower() in fname.lower():
|
|
||||||
yield Completion(self.quote_fname(fname), start_position=-len(partial))
|
|
||||||
|
|
||||||
def glob_filtered_to_repo(self, pattern):
|
def glob_filtered_to_repo(self, pattern):
|
||||||
try:
|
try:
|
||||||
|
@ -483,12 +480,9 @@ class Commands:
|
||||||
reply = prompts.added_files.format(fnames=", ".join(added_fnames))
|
reply = prompts.added_files.format(fnames=", ".join(added_fnames))
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
def completions_drop(self, partial):
|
def completions_drop(self):
|
||||||
files = self.coder.get_inchat_relative_files()
|
files = self.coder.get_inchat_relative_files()
|
||||||
|
return files
|
||||||
for fname in files:
|
|
||||||
if partial.lower() in fname.lower():
|
|
||||||
yield Completion(self.quote_fname(fname), start_position=-len(partial))
|
|
||||||
|
|
||||||
def cmd_drop(self, args=""):
|
def cmd_drop(self, args=""):
|
||||||
"Remove files from the chat session to free up context space"
|
"Remove files from the chat session to free up context space"
|
||||||
|
@ -624,7 +618,7 @@ class Commands:
|
||||||
|
|
||||||
def cmd_help(self, args):
|
def cmd_help(self, args):
|
||||||
"Show help about all commands"
|
"Show help about all commands"
|
||||||
commands = sorted(self.get_commands())
|
commands = sorted(self.get_commands().keys())
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
cmd_method_name = f"cmd_{cmd[1:]}"
|
cmd_method_name = f"cmd_{cmd[1:]}"
|
||||||
cmd_method = getattr(self, cmd_method_name, None)
|
cmd_method = getattr(self, cmd_method_name, None)
|
||||||
|
@ -638,7 +632,7 @@ class Commands:
|
||||||
"Show help about all commands in markdown"
|
"Show help about all commands in markdown"
|
||||||
|
|
||||||
res = ""
|
res = ""
|
||||||
commands = sorted(self.get_commands())
|
commands = sorted(self.get_commands().keys())
|
||||||
for cmd in commands:
|
for cmd in commands:
|
||||||
cmd_method_name = f"cmd_{cmd[1:]}"
|
cmd_method_name = f"cmd_{cmd[1:]}"
|
||||||
cmd_method = getattr(self, cmd_method_name, None)
|
cmd_method = getattr(self, cmd_method_name, None)
|
||||||
|
|
32
aider/io.py
32
aider/io.py
|
@ -23,7 +23,6 @@ from .utils import is_image_file
|
||||||
|
|
||||||
class AutoCompleter(Completer):
|
class AutoCompleter(Completer):
|
||||||
def __init__(self, root, rel_fnames, addable_rel_fnames, commands, encoding):
|
def __init__(self, root, rel_fnames, addable_rel_fnames, commands, encoding):
|
||||||
self.commands = commands
|
|
||||||
self.addable_rel_fnames = addable_rel_fnames
|
self.addable_rel_fnames = addable_rel_fnames
|
||||||
self.rel_fnames = rel_fnames
|
self.rel_fnames = rel_fnames
|
||||||
self.encoding = encoding
|
self.encoding = encoding
|
||||||
|
@ -37,6 +36,13 @@ class AutoCompleter(Completer):
|
||||||
|
|
||||||
self.words = set()
|
self.words = set()
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
self.commands = commands.get_commands(with_completions=True)
|
||||||
|
dur = time.time() - start
|
||||||
|
dump(dur)
|
||||||
|
|
||||||
for rel_fname in addable_rel_fnames:
|
for rel_fname in addable_rel_fnames:
|
||||||
self.words.add(rel_fname)
|
self.words.add(rel_fname)
|
||||||
|
|
||||||
|
@ -64,13 +70,21 @@ class AutoCompleter(Completer):
|
||||||
|
|
||||||
if text[0] == "/":
|
if text[0] == "/":
|
||||||
if len(words) == 1 and not text[-1].isspace():
|
if len(words) == 1 and not text[-1].isspace():
|
||||||
candidates = self.commands.get_commands()
|
partial = words[0]
|
||||||
candidates = [(cmd, cmd) for cmd in candidates]
|
candidates = self.commands.keys()
|
||||||
else:
|
for cmd in candidates:
|
||||||
for completion in self.commands.get_command_completions(words[0][1:], words[-1]):
|
if cmd.startswith(partial):
|
||||||
yield completion
|
yield Completion(cmd, start_position=-len(partial))
|
||||||
|
elif len(words) > 1:
|
||||||
|
cmd = words[0]
|
||||||
|
partial = words[-1]
|
||||||
|
|
||||||
|
candidates = self.commands.get(cmd, [])
|
||||||
|
for word in candidates:
|
||||||
|
if partial in word:
|
||||||
|
yield Completion(word, start_position=-len(partial))
|
||||||
return
|
return
|
||||||
else:
|
|
||||||
candidates = self.words
|
candidates = self.words
|
||||||
candidates.update(set(self.fname_to_rel_fnames))
|
candidates.update(set(self.fname_to_rel_fnames))
|
||||||
candidates = [(word, f"`{word}`") for word in candidates]
|
candidates = [(word, f"`{word}`") for word in candidates]
|
||||||
|
@ -277,8 +291,8 @@ class InputOutput:
|
||||||
def log_llm_history(self, role, content):
|
def log_llm_history(self, role, content):
|
||||||
if not self.llm_history_file:
|
if not self.llm_history_file:
|
||||||
return
|
return
|
||||||
timestamp = datetime.now().isoformat(timespec='seconds')
|
timestamp = datetime.now().isoformat(timespec="seconds")
|
||||||
with open(self.llm_history_file, 'a', encoding=self.encoding) as log_file:
|
with open(self.llm_history_file, "a", encoding=self.encoding) as log_file:
|
||||||
log_file.write(f"{role.upper()} {timestamp}\n")
|
log_file.write(f"{role.upper()} {timestamp}\n")
|
||||||
log_file.write(content + "\n")
|
log_file.write(content + "\n")
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue