-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzsh-llm-suggestions.zsh
100 lines (84 loc) · 2.68 KB
/
zsh-llm-suggestions.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
zsh_llm_suggestions_spinner() {
local pid=$1
local delay=0.1
local spinstr='|/-\'
cleanup() {
kill $pid
echo -ne "\e[?25h"
}
trap cleanup SIGINT
echo -ne "\e[?25l"
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c]" "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b"
done
printf " \b\b\b\b"
echo -ne "\e[?25h"
trap - SIGINT
}
zsh_llm_suggestions_run_query() {
local llm="$1"
local query="$2"
local result_file="$3"
local mode="$4"
echo -n "$query" | eval $llm $mode > $result_file
}
zsh_llm_completion() {
local llm="$1"
local mode="$2"
local query=${BUFFER}
# Empty prompt, nothing to do
if [[ "$query" == "" ]]; then
return
fi
# If the prompt is the last suggestions, just get another suggestion for the same query
if [[ "$mode" == "generate" ]]; then
if [[ "$query" == "$ZSH_LLM_SUGGESTIONS_LAST_RESULT" ]]; then
query=$ZSH_LLM_SUGGESTIONS_LAST_QUERY
else
ZSH_LLM_SUGGESTIONS_LAST_QUERY="$query"
fi
fi
# Temporary file to store the result of the background process
local result_file="/tmp/zsh-llm-suggestions-result"
# Run the actual query in the background (since it's long-running, and so that we can show a spinner)
read < <( zsh_llm_suggestions_run_query $llm $query $result_file $mode & echo $! )
# Get the PID of the background process
local pid=$REPLY
# Call the spinner function and pass the PID
zsh_llm_suggestions_spinner $pid
if [[ "$mode" == "generate" ]]; then
# Place the query in the history first
print -s $query
# Replace the current buffer with the result
ZSH_LLM_SUGGESTIONS_LAST_RESULT=$(cat $result_file)
BUFFER="${ZSH_LLM_SUGGESTIONS_LAST_RESULT}"
CURSOR=${#ZSH_LLM_SUGGESTIONS_LAST_RESULT}
fi
if [[ "$mode" == "explain" ]]; then
echo ""
eval "cat $result_file"
echo ""
zle reset-prompt
fi
}
SCRIPT_DIR=$( cd -- "$( dirname -- "$0" )" &> /dev/null && pwd )
zsh_llm_suggestions_openai() {
zsh_llm_completion "$SCRIPT_DIR/zsh-llm-suggestions-openai.py" "generate"
}
zsh_llm_suggestions_github_copilot() {
zsh_llm_completion "$SCRIPT_DIR/zsh-llm-suggestions-github-copilot.py" "generate"
}
zsh_llm_suggestions_openai_explain() {
zsh_llm_completion "$SCRIPT_DIR/zsh-llm-suggestions-openai.py" "explain"
}
zsh_llm_suggestions_github_copilot_explain() {
zsh_llm_completion "$SCRIPT_DIR/zsh-llm-suggestions-github-copilot.py" "explain"
}
zle -N zsh_llm_suggestions_openai
zle -N zsh_llm_suggestions_openai_explain
zle -N zsh_llm_suggestions_github_copilot
zle -N zsh_llm_suggestions_github_copilot_explain