Skip to content

Commit

Permalink
fix: rerun the command using python subprocess
Browse files Browse the repository at this point in the history
When fixing a command, rerun the previously executed
command and capture the output in Python code. The
fish command for doing the same thing is broken.
  • Loading branch information
Realiserad committed Feb 10, 2024
1 parent 87a37ae commit 646bd71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
6 changes: 2 additions & 4 deletions functions/_fish_ai_autocomplete_or_fix.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
function _fish_ai_autocomplete_or_fix --description "Autocomplete the current command or fix the previous command using AI."
set previous_status $status
if test -z "$input" && test $previous_status -ne 0
# Fix the previous command. To give the AI a bit more context
# rerun the previous command and capture stdout and stderr
# Fix the previous command.
set previous_command (history | head -1)
set error_message (eval "$previous_command" &| tail -10)
set fixed_command (_fish_ai_fix "$previous_command" "$error_message")
set fixed_command (_fish_ai_fix "$previous_command")
commandline --replace "$fixed_command"
else
# Autocomplete the current command
Expand Down
4 changes: 2 additions & 2 deletions functions/_fish_ai_fix.fish
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env fish

function _fish_ai_fix --description "Fix a command using AI." --argument-names "previous_command" "error_message"
function _fish_ai_fix --description "Fix a command using AI." --argument-names "previous_command"
set dir (dirname (status -f))
set output ("$dir/_fish_ai_fix.py" "$previous_command" "$error_message")
set output ("$dir/_fish_ai_fix.py" "$previous_command")
echo -n "$output"
end
23 changes: 18 additions & 5 deletions functions/_fish_ai_fix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

from sys import argv
import subprocess
import _fish_ai_engine as engine

def get_instructions(command, error_message):
Expand Down Expand Up @@ -63,14 +64,26 @@ def get_instructions(command, error_message):
def get_messages(command, error_message):
return [ engine.get_system_prompt() ] + get_instructions(command, error_message)

command = argv[1]
# Cut lines in the error output exceeding 200 characters
error_message = '\n'.join([line[:200] for line in argv[2].split('\n')])
def get_error_message(previous_command):
"""
There is no way to get the output of the previous command in fish, so
let's rerun the previous command and capture the output.
"""
try:
subprocess.check_output(previous_command,
stderr = subprocess.STDOUT,
shell = True)
except subprocess.CalledProcessError as e:
# Get the last 10 lines of the output and truncate lines exceeding 200 characters
return '\n'.join([line[:200] for line in e.output.decode('utf-8').split('\n')[-10:]])

previous_command = argv[1]
error_message = get_error_message(previous_command)

try:
engine.get_logger().debug('Fixing command: ' + command)
engine.get_logger().debug('Fixing command: ' + previous_command)
engine.get_logger().debug('Command output: ' + error_message)
response = engine.get_response(messages = get_messages(command, error_message))
response = engine.get_response(messages = get_messages(previous_command, error_message))
print(response)
except Exception as e:
engine.get_logger().exception(e)

0 comments on commit 646bd71

Please sign in to comment.