refactor: Simplify cmd_run to use confirm_ask and add output to cur_messages

This commit is contained in:
Paul Gauthier (aider) 2024-11-21 06:28:41 -08:00
parent f96cc03587
commit 28be59582f

View file

@ -865,11 +865,10 @@ class Commands:
return errors return errors
def cmd_run(self, args, add_on_nonzero_exit=False): def cmd_run(self, args, add_on_nonzero_exit=False):
"Run a shell command and optionally add the output to the chat (alias: !)" "Run a shell command and add the output to the chat (alias: !)"
exit_status, combined_output = run_cmd( exit_status, combined_output = run_cmd(
args, verbose=self.verbose, error_print=self.io.tool_error args, verbose=self.verbose, error_print=self.io.tool_error
) )
instructions = None
if combined_output is None: if combined_output is None:
return return
@ -877,36 +876,21 @@ class Commands:
if add_on_nonzero_exit: if add_on_nonzero_exit:
add = exit_status != 0 add = exit_status != 0
else: else:
self.io.tool_output() add = self.io.confirm_ask("Add command output to the chat?")
response = self.io.prompt_ask(
"Add the output to the chat?\n(Y)es/(n)o/message with instructions:",
).strip()
self.io.tool_output()
if response.lower() in ["yes", "y"]:
add = True
elif response.lower() in ["no", "n"]:
add = False
else:
add = True
instructions = response
if response.strip():
self.io.user_input(response, log_only=True)
self.io.add_to_input_history(response)
if add: if add:
for line in combined_output.splitlines(): num_lines = len(combined_output.strip().splitlines())
self.io.tool_output(line, log_only=True) self.io.tool_output(f"Added {num_lines} lines of output to the chat")
msg = prompts.run_output.format( msg = prompts.run_output.format(
command=args, command=args,
output=combined_output, output=combined_output,
) )
if instructions: self.coder.cur_messages += [
msg = instructions + "\n\n" + msg dict(role="user", content=msg),
dict(role="assistant", content="Ok."),
return msg ]
def cmd_exit(self, args): def cmd_exit(self, args):
"Exit the application" "Exit the application"