shell_gpt icon indicating copy to clipboard operation
shell_gpt copied to clipboard

Add --fix option to correct the last command (#698)

Open demouo opened this issue 8 months ago • 1 comments

Hi! Backgroud and motivation from #698

When I got an error command, I would like to correct ai that the command ... was wrong with the output ..., please fix it.

example follows: image

This PR introduces a --fix flag. When used, sgpt retrieves the last executed command and its output, then prompts the AI to provide a corrected version based on the error.

Key changes:

  • Added --fix option to sgpt/app.py.
  • Created sgpt/command.py to manage storing and retrieving the last command and output in json format.
  • Updated sgpt/utils.py to set and get the last command.
  • Replaced os.system with subprocess.Popen in run_command to capture output.
  • Added get_fixed_prompt to generate the prompt for the AI.
  • Store command and output using the new Command class.
  • Updated tests in tests/test_shell.py to reflect the use of subprocess.
  • Added necessary configuration in sgpt/config.py.

make sure the lint and test result as usual: image

Closes #698

demouo avatar May 06 '25 09:05 demouo

The 2nd~4th Commits' changes:

  1. LLM sometimes could not completely return the pure shell code, so I wrote the extracting function

  2. Due to I was not familiar with the operation, I accidentally wrote several empty prompts, which should be ended before chatting with LLM

  3. The idea of fix last command was added in REPL mode.

Hope everything goes well!

demouo avatar May 07 '25 15:05 demouo