Created
March 21, 2025 05:20
-
-
Save ManiaciaChao/028fca415254df132fff67c3073180a3 to your computer and use it in GitHub Desktop.
LLM command auto-completion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bind \e\\ __llm_cmd | |
# run `cargo install llm` to get `llm` command available | |
# system prompt is borrowed and modified from https://github.com/CGamesPlay/llm-cmd-comp/blob/main/llm_cmd_comp.py | |
function __llm_cmd -d "Fill in the command using an LLM" | |
set -l __system_prompt "Return only the command to be executed as a raw string, no string delimiters wrapping it, no yapping, no markdown, no fenced code blocks, what you return will be passed to the shell directly. | |
For example, if the user asks: undo last git commit | |
You return only: git reset --soft HEAD~1 | |
The shell is $(which fish) on $(uname)" | |
set -l __llm_oldcmd (commandline -b) | |
set -l __llm_cursor_pos (commandline -C) | |
echo # Start the program on a blank line | |
set -l result (echo $__llm_oldcmd | llm --system $__system_prompt | tail -n 1) | |
echo | |
set_color brmagenta | |
echo "π€ Suggested command:" | |
set_color normal | |
echo "ββββββββββββββββββββββ" | |
set_color brgreen | |
echo "β $result" | |
set_color normal | |
echo "ββββββββββββββββββββββ" | |
set_color brblue | |
echo -n "βοΈ Anything to revise? " | |
set_color yellow | |
echo "[Enter to confirm, type to revise, R to execute]" | |
set_color normal | |
read -l revision | |
switch $revision | |
case '' # Empty (just Enter) | |
if test $status -eq 0 | |
commandline -r $result | |
echo # Move down a line | |
end | |
case R r # Direct execution | |
if test $status -eq 0 | |
commandline -r $result | |
commandline -f execute | |
end | |
case '*' # Any other input is treated as revision | |
set -l new_result (echo -e "$__llm_oldcmd\n$result\n$revision" | llm --system $__system_prompt | tail -n 1) | |
if test $status -eq 0 | |
commandline -r $new_result | |
echo # Move down a line | |
end | |
end | |
commandline -f repaint | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment