-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplease.sh
executable file
·491 lines (422 loc) · 12.6 KB
/
please.sh
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#!/usr/bin/env bash
set -uo pipefail
model=${PLEASE_OPENAI_CHAT_MODEL:-'gpt-4-turbo'}
options=("[I] Invoke" "[C] Copy to clipboard" "[Q] Ask a question" "[A] Abort" )
number_of_options=${#options[@]}
keyName="OPENAI_API_KEY"
explain=0
debug_flag=0
initialized=0
selected_option_index=-1
yellow='\e[33m'
cyan='\e[36m'
black='\e[0m'
lightbulb="\xF0\x9F\x92\xA1"
exclamation="\xE2\x9D\x97"
questionMark="\x1B[31m?\x1B[0m"
checkMark="\x1B[31m\xE2\x9C\x93\x1B[0m"
openai_api_base=${PLEASE_OPENAI_API_BASE:-${OPENAI_API_BASE:-${OPENAI_URL:-"https://api.openai.com"}}}
openai_api_version=${PLEASE_OPENAI_API_VERSION:-${OPENAI_API_VERSION:-"v1"}}
openai_invocation_url=${openai_api_base}/${openai_api_version}
fail_msg="echo 'I do not know. Please rephrase your question.'"
declare -a qaMessages=()
check_args() {
while [[ $# -gt 0 ]]; do
case "${1}" in
-e|--explanation)
explain=1
shift
;;
-l|--legacy)
model="gpt-3.5-turbo"
shift
;;
--debug)
debug_flag=1
shift
;;
-a|--api-key)
store_api_key
exit 0
;;
-m|--model)
if [ -n "$2" ] && [ "${2:0:1}" != "-" ] && [ "${2:0:3}" == "gpt" ]; then
model="$2"
shift 2
else
echo "Error: --model requires a gpt model"
exit 1
fi
;;
-v|--version)
display_version
exit 0
;;
-h|--help)
display_help
exit 0
;;
*)
break
;;
esac
done
# Save remaining arguments to a string
commandDescription="$*"
}
function store_api_key() {
echo "Do you want the script to store an API key in the local keychain? (y/n)"
read -r answer
if [ "$answer" != "y" ]; then
echo "This script will need an API Key to run. Exiting..."
exit 1
fi
echo "The script needs to create or copy the API key. Press Enter to continue..."
read -rs
apiKeyUrl="https://platform.openai.com/account/api-keys"
echo "Opening ${apiKeyUrl} in your browser..."
open "${apiKeyUrl}" || xdg-open "${apiKeyUrl}"
while true; do
echo "Please enter your API key: [Press Ctrl+C to exit]"
read -rs apiKey
if [ -z "$apiKey" ]; then
echo "API key cannot be empty. Please try again."
else
if [[ "$OSTYPE" == "darwin"* ]]; then
security add-generic-password -a "${USER}" -s "${keyName}" -w "${apiKey}" -U
PLEASE_OPENAI_API_KEY=$(security find-generic-password -a "${USER}" -s "${keyName}" -w)
else
echo -e "${apiKey}" | secret-tool store --label="${keyName}" username "${USER}" key_name "${keyName}"
PLEASE_OPENAI_API_KEY=$(secret-tool lookup username "${USER}" key_name "${keyName}")
fi
echo "API key stored successfully and set as a global variable."
break
fi
done
}
display_version() {
echo "Please vVERSION_NUMBER"
}
display_help() {
echo "Please - a simple script to translate your thoughts into command line commands using GPT"
echo "Usage: $0 [options] [input]"
echo
echo "Options:"
echo " -e, --explanation Explain the command to the user"
echo " -l, --legacy Use GPT 3.5 (in case you do not have GPT4 API access)"
echo " --debug Show debugging output"
echo " -a, --api-key Store your API key in the local keychain"
echo " -m, --model Specify the exact LLM model for the script"
echo " -v, --version Display version information and exit"
echo " -h, --help Display this help message and exit"
echo
echo "Input:"
echo " The remaining arguments are used as input to be turned into a CLI command."
echo
echo "OpenAI API Key:"
echo " The API key needs to be set as PLEASE_OPENAI_API_KEY or OPENAI_API_KEY environment variable or keychain entry. "
}
debug() {
if [ "$debug_flag" = 1 ]; then
echo "DEBUG: $1" >&2
fi
}
check_key() {
if [ -z "${PLEASE_OPENAI_API_KEY:-${OPENAI_API_KEY:-}}" ]; then
debug "PLEASE_OPENAI_API_KEY or OPENAI_API_KEY environment variable not set, trying to find it in keychain"
get_key_from_keychain
fi
}
get_key_from_keychain() {
case "$(uname)" in
Darwin*) # macOS
key=$(security find-generic-password -a "${USER}" -s "${keyName}" -w)
exitStatus=$?
;;
Linux*)
if ! command -v secret-tool &> /dev/null; then
debug "PLEASE_OPENAI_API_KEY or OPENAI_API_KEY not set and secret-tool not installed. Install it with 'sudo apt install libsecret-tools'."
exitStatus=1
else
key=$(secret-tool lookup username "${USER}" key_name "${keyName}")
exitStatus=$?
fi
;;
*)
debug "PLEASE_OPENAI_API_KEY or OPENAI_API_KEY not set and no supported keychain available."
exitStatus=1
;;
esac
if [ "${exitStatus}" -ne 0 ]; then
echo "PLEASE_OPENAI_API_KEY or OPENAI_API_KEY not set and unable to find it in keychain. See the README on how to persist your key."
echo "You can get an API at https://beta.openai.com/"
echo "Please enter your OpenAI API key now to use it this time only, or rerun 'please -a' to store it in the keychain."
echo "Your API Key:"
read -r key
fi
if [ -z "${key}" ]; then
echo "No API key provided. Exiting."
exit 1
fi
PLEASE_OPENAI_API_KEY="${key}"
}
get_command() {
role="You translate the given input into a Linux command. You may not use natural language, but only a Linux shell command as an answer.
Do not use markdown. Do not quote the whole output. If you do not know the answer, answer with \\\"${fail_msg}\\\"."
payload=$(printf %s "$commandDescription" | jq --slurp --raw-input --compact-output '{
model: "'"$model"'",
messages: [{ role: "system", content: "'"$role"'" }, { role: "user", content: . }]
}')
debug "Sending request to OpenAI API: ${payload}"
perform_openai_request
command="${message}"
}
explain_command() {
if [ "${command}" = "$fail_msg" ]; then
explanation="There is no explanation because there was no answer."
else
prompt="Explain the step of the command that answers the following ${command}: ${commandDescription}\n. Be precise and succinct."
payload=$(printf %s "$prompt" | jq --slurp --raw-input --compact-output '{
max_tokens: 100,
model: "'"$model"'",
messages: [{ role: "user", content: . }]
}')
perform_openai_request
explanation="${message}"
fi
}
perform_openai_request() {
completions_url="${openai_invocation_url}/chat/completions"
IFS=$'\n' read -r -d '' -a result < <(curl "${completions_url}" \
-s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-H "Accept-Encoding: identity" \
-H "Authorization: Bearer ${PLEASE_OPENAI_API_KEY:-$OPENAI_API_KEY}" \
-d "${payload}" \
--silent)
debug "Response:\n${result[*]}"
length="${#result[@]}"
httpStatus="${result[$((length-1))]}"
length="${#result[@]}"
response_array=("${result[*]:0:$((length-1))}")
response="${response_array[*]}"
if [ "${httpStatus}" -ne 200 ]; then
echo "Error: Received HTTP status ${httpStatus} while trying to access ${completions_url}"
echo "${response}"
exit 1
else
message=$(echo "${response}" | jq '.choices[0].message.content' --raw-output)
fi
}
print_option() {
# shellcheck disable=SC2059
printf "${lightbulb} ${cyan}Command:${black}\n"
echo " ${command}"
if [ "${explain}" -eq 1 ]; then
echo ""
echo "${explanation}"
fi
}
choose_action() {
initialized=0
selected_option_index=-1
echo ""
# shellcheck disable=SC2059
printf "${exclamation} ${yellow}What should I do? ${cyan}[use arrow keys or initials to navigate]${black}\n"
while true; do
display_menu
read -rsn1 input
# Check for arrow keys and 'Enter'
case "$input" in
$'\x1b')
read -rsn1 tmp
if [[ "$tmp" == "[" ]]; then
read -rsn1 tmp
case "$tmp" in
"D") # Right arrow
selected_option_index=$(( (selected_option_index - 1 + number_of_options) % number_of_options ))
;;
"C") # Left arrow
selected_option_index=$(( (selected_option_index + 1) % number_of_options ))
;;
esac
fi
;;
i)
selected_option_index=0
display_menu
break
;;
c)
selected_option_index=1
display_menu
break
;;
q)
selected_option_index=2
display_menu
break
;;
a)
selected_option_index=3
display_menu
break
;;
"") # 'Enter' key
if [ "$selected_option_index" -ne -1 ]; then
break
fi
;;
esac
done
}
display_menu() {
if [ $initialized -eq 1 ]; then
# Go up 1 line
printf "\033[%dA" "1"
else
initialized=1
fi
index=0
for option in "${options[@]}"; do
(( index == selected_option_index )) && marker="${cyan}>${black}" || marker=" "
# shellcheck disable=SC2059
printf "$marker $option "
(( ++index ))
done
printf "\n"
}
act_on_action() {
if [ "$selected_option_index" -eq 0 ]; then
echo "Executing ..."
echo ""
execute_command
elif [ "$selected_option_index" -eq 1 ]; then
echo "Copying to clipboard ..."
copy_to_clipboard
elif [ "$selected_option_index" -eq 2 ]; then
ask_question
else
exit 0
fi
}
execute_command() {
save_command_in_history
eval "${command}"
}
save_command_in_history() {
# Get the name of the shell
shell=$(basename "$SHELL")
# Determine the history file based on the shell
case "$shell" in
bash)
histfile="${HISTFILE:-$HOME/.bash_history}"
;;
zsh)
histfile="${HISTFILE:-$HOME/.zsh_history}"
;;
fish)
# fish doesn't use HISTFILE, but uses a fixed location
histfile="$HOME/.local/share/fish/fish_history"
;;
ksh)
histfile="${HISTFILE:-$HOME/.sh_history}"
;;
tcsh)
histfile="${HISTFILE:-$HOME/.history}"
;;
*)
;;
esac
if [ -z "$histfile" ]; then
debug "Could not determine history file for shell ${shell}"
else
debug "Saving command ${command} to file ${histfile}"
echo "${command}" >> "${histfile}"
fi
}
copy_to_clipboard() {
case "$(uname)" in
Darwin*) # macOS
echo -n "${command}" | pbcopy
;;
Linux*)
if [ "$XDG_SESSION_TYPE" == "wayland" ]; then
echo -n "${command}" | wl-copy --primary
else
if command -v xclip &> /dev/null; then
echo -n "${command}" | xclip -selection clipboard
else
echo "xclip not installed. Exiting."
exit 1
fi
fi
;;
*)
echo "Unsupported operating system"
exit 1
;;
esac
}
init_questions() {
systemPrompt="You will give answers in the context of the command \"${command}\" which is a Linux bash command related to the prompt \"${commandDescription}\". Be precise and succinct, answer in full sentences, no lists, no markdown."
escapedPrompt=$(printf %s "${systemPrompt}" | jq -srR '@json')
qaMessages+=("{ \"role\": \"system\", \"content\": ${escapedPrompt} }")
}
ask_question() {
echo ""
# shellcheck disable=SC2059
printf "${questionMark} ${cyan}What do you want to know about this command?${black}\n"
read -r question
answer_question_about_command
echo "${answer}"
# shellcheck disable=SC2059
printf "${checkMark} ${answer}\n"
choose_action
act_on_action
}
answer_question_about_command() {
prompt="${question}"
escapedPrompt=$(printf %s "${prompt}" | jq -srR '@json')
qaMessages+=("{ \"role\": \"user\", \"content\": ${escapedPrompt} }")
messagesJson='['$(join_by , "${qaMessages[@]}")']'
payload=$(jq --null-input --compact-output --argjson messagesJson "${messagesJson}" '{
max_tokens: 200,
model: "'"$model"'",
messages: $messagesJson
}')
perform_openai_request
answer="${message}"
escapedAnswer=$(printf %s "$answer" | jq -srR '@json')
qaMessages+=("{ \"role\": \"assistant\", \"content\": ${escapedAnswer} }")
}
function join_by {
local d=${1-} f=${2-}
if shift 2; then
printf %s "$f" "${@/#/$d}"
fi
}
function main() {
if [ $# -eq 0 ]; then
input=("-h")
else
input=("$@")
fi
check_args "${input[@]}"
check_key
get_command
if [ "${explain}" -eq 1 ]; then
explain_command
fi
print_option
if test "${command}" = "${fail_msg}"; then
exit 1
fi
init_questions
choose_action
act_on_action
}
# Only call main if the script is not being sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi