Added spinner animation to indicate AI processing and improved error handling for API request

This commit is contained in:
ilguappo 2024-08-07 03:48:26 -07:00
parent a7509a913c
commit 08d5fbdabc

View File

@ -4,10 +4,21 @@ set -eu
set -o pipefail set -o pipefail
ARGS=("${@-}") ARGS=("${@-}")
TMP_FILE="/tmp/aicommit"
pushd "$(pwd)" >/dev/null || exit 1 pushd "$(pwd)" >/dev/null || exit 1
TMP_FILE="/tmp/aicommit" spin() {
echo "${@}"
while true; do
spinner=('-' '\' '|' '/')
for i in "${spinner[@]}"; do
echo -ne "\r${i}"
sleep .05
done
done
echo ""
}
if [[ -z ${GROQ_API_KEY} ]]; then if [[ -z ${GROQ_API_KEY} ]]; then
echo "API key not set." echo "API key not set."
@ -31,7 +42,8 @@ message=$(
.stream = false | .stream = false |
.stop = null" .stop = null"
) )
spin "AI is thinking..." &
SPINNER="$!"
curl -s "https://api.groq.com/openai/v1/chat/completions" \ curl -s "https://api.groq.com/openai/v1/chat/completions" \
-X POST \ -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
@ -40,8 +52,10 @@ curl -s "https://api.groq.com/openai/v1/chat/completions" \
if [[ 0 -ne $? ]]; then if [[ 0 -ne $? ]]; then
echo "Failed to get response." echo "Failed to get response."
kill "${SPINNER}"
exit 1 exit 1
fi fi
kill "${SPINNER}"
git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE} git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE}