Add --fix option to correct the last command (#698)
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:
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
--fixoption tosgpt/app.py. - Created
sgpt/command.pyto manage storing and retrieving the last command and output in json format. - Updated
sgpt/utils.pyto set and get the last command. - Replaced
os.systemwithsubprocess.Popeninrun_commandto capture output. - Added
get_fixed_promptto generate the prompt for the AI. - Store command and output using the new Command class.
- Updated tests in
tests/test_shell.pyto reflect the use ofsubprocess. - Added necessary configuration in
sgpt/config.py.
make sure the lint and test result as usual:
Closes #698
The 2nd~4th Commits' changes:
-
LLM sometimes could not completely return the pure shell code, so I wrote the extracting function
-
Due to I was not familiar with the operation, I accidentally wrote several empty prompts, which should be ended before chatting with LLM
-
The idea of
fix last commandwas added in REPL mode.
Hope everything goes well!