sniff when GPT mentions files that are not in the chat, prompt the user to add them

This commit is contained in:
Paul Gauthier 2023-05-10 22:05:00 -07:00
parent 65436ae255
commit f026f1eeeb
2 changed files with 55 additions and 17 deletions

View file

@ -175,7 +175,10 @@ class Coder:
while True: while True:
try: try:
self.run_loop() new_user_message = self.run_loop()
while new_user_message:
new_user_message = self.send_new_user_message(new_user_message)
except KeyboardInterrupt: except KeyboardInterrupt:
self.num_control_c += 1 self.num_control_c += 1
if self.num_control_c >= 2: if self.num_control_c >= 2:
@ -192,11 +195,6 @@ class Coder:
inp = get_input(self.history_file, self.abs_fnames, self.commands) inp = get_input(self.history_file, self.abs_fnames, self.commands)
if inp.startswith("/"):
inp = self.commands.run(inp)
if not inp:
return
self.num_control_c = 0 self.num_control_c = 0
if self.last_modified < self.get_last_modified(): if self.last_modified < self.get_last_modified():
@ -213,6 +211,12 @@ class Coder:
if not inp: if not inp:
return return
if inp.startswith("/"):
return self.commands.run(inp)
return self.send_new_user_message(inp)
def send_new_user_message(self, inp):
self.cur_messages += [ self.cur_messages += [
dict(role="user", content=inp), dict(role="user", content=inp),
] ]
@ -224,7 +228,7 @@ class Coder:
messages += self.get_files_messages() messages += self.get_files_messages()
messages += self.cur_messages messages += self.cur_messages
# utils.show_messages(messages, "all") utils.show_messages(messages, "all")
content, interrupted = self.send(messages) content, interrupted = self.send(messages)
if interrupted: if interrupted:
@ -238,7 +242,7 @@ class Coder:
self.console.print() self.console.print()
if interrupted: if interrupted:
return True return
try: try:
edited = self.update_files(content, inp) edited = self.update_files(content, inp)
@ -248,8 +252,12 @@ class Coder:
traceback.print_exc() traceback.print_exc()
edited = None edited = None
add_rel_files_message = self.check_for_file_mentions(content)
if add_rel_files_message:
return add_rel_files_message
if not edited: if not edited:
return True return
res = self.commit(history=self.cur_messages, prefix="aider: ") res = self.commit(history=self.cur_messages, prefix="aider: ")
if res: if res:
@ -270,7 +278,36 @@ class Coder:
dict(role="assistant", content="Ok."), dict(role="assistant", content="Ok."),
] ]
self.cur_messages = [] self.cur_messages = []
return True return
def check_for_file_mentions(self, content):
words = set(word for word in content.split())
for quote in ['"', "'", "`"]:
words = set(word.strip(quote) for word in words)
addable_rel_fnames = set(self.get_all_relative_files()) - set(
self.get_inchat_relative_files()
)
mentioned_rel_fnames = set()
for word in words:
if word in addable_rel_fnames:
mentioned_rel_fnames.add(word)
dump(mentioned_rel_fnames)
if not mentioned_rel_fnames:
return
for rel_fname in mentioned_rel_fnames:
self.console.print(f"[bright_black]{rel_fname}")
ok = Confirm.ask("[bright_black]Add these files?", console=self.console, default="y")
if not ok:
return
for rel_fname in mentioned_rel_fnames:
self.abs_fnames.add(os.path.abspath(os.path.join(self.root, rel_fname)))
return prompts.added_files.format(fnames=", ".join(mentioned_rel_fnames))
def send(self, messages, model=None, silent=False): def send(self, messages, model=None, silent=False):
if not model: if not model:
@ -343,7 +380,6 @@ class Coder:
def update_files(self, content, inp): def update_files(self, content, inp):
edited = set() edited = set()
for match in self.pattern.finditer(content): for match in self.pattern.finditer(content):
dump(match.groups())
_, path, _, original, updated = match.groups() _, path, _, original, updated = match.groups()
path = path.strip() path = path.strip()

View file

@ -45,7 +45,7 @@ some/dir/example.py
"""Multiplies 2 numbers""" """Multiplies 2 numbers"""
>>>>>>> UPDATED >>>>>>> UPDATED
If need to see the contents of a file from the git repo, ask the user! If need to see the contents of a file from the git repo, tell the user the file names you need!
Don't edit a listed file without looking at the contents first! Don't edit a listed file without looking at the contents first!
You can make a new file by replying with an ORIGINAL/UPDATE that has an empty ORIGINAL block. You can make a new file by replying with an ORIGINAL/UPDATE that has an empty ORIGINAL block.
@ -78,19 +78,21 @@ Only return the parts of the code which need changes!
# FILES # FILES
files_content_gpt_edits = "I committed your suggested changes with git hash {hash} and commit message: {message}" files_content_gpt_edits = (
"I committed your suggested changes with git hash {hash} and commit message: {message}"
files_content_gpt_no_edits = (
"I wasn't able to see any properly formatted edits in your reply?!"
) )
files_content_gpt_no_edits = "I wasn't able to see any properly formatted edits in your reply?!"
files_content_local_edits = "I made some changes to the files myself." files_content_local_edits = "I made some changes to the files myself."
repo_content_prefix = "These are the files in the git repo:\n\n" repo_content_prefix = "These are the files in the git repo:\n\n"
files_content_prefix = "Here is the current content of the files we have opened:\n\n" files_content_prefix = "Here is the current content of the files we have opened:\n\n"
files_content_suffix = """Base any edits on the current contents of the files as shown in the user's last message.""" files_content_suffix = (
"""Base any edits on the current contents of the files as shown in the user's last message."""
)
# COMMIT # COMMIT