Fix error handling for failed POST requests to infer API endpoints

This commit is contained in:
ilguappo 2024-08-07 03:11:37 -07:00
parent 20a30bd9ed
commit 31ae31643b

View File

@ -1,39 +1,58 @@
#!/bin/bash #!/bin/bash
set -eu
set -o pipefail
pushd $(pwd) "$@" >/dev/null ARGS=("${@-}")
pushd "$(pwd)" "$@" >/dev/null
TMP_FILE="/tmp/aicommit" TMP_FILE="/tmp/aicommit"
hasFlag() {
local flags=("$@")
for var in "${ARGS[@]}"; do
for flag in "${flags[@]}"; do
if [[ ${flag} == "${var}" ]]; then
echo 'true'
return
fi
done
done
echo 'false'
}
system="You are an expert programmer that values clear, unambiguous communication and are specialized in generating concise and informative git commit messages. Only reply with the commit message and nothing else." system="You are an expert programmer that values clear, unambiguous communication and are specialized in generating concise and informative git commit messages. Only reply with the commit message and nothing else."
diff=$(git diff --cached) if ! diff=$(git diff --cached); then
if [[ 0 -ne $? ]]; then
echo "Failed to get diff." echo "Failed to get diff."
exit 1 exit 1
fi fi
message_cat="$system diff: $diff" message_cat="${system} diff: ${diff}"
message=$(jq -n \ message=$(
".message = $(jq -R -s '@json' <<< "$message_cat")" jq -n \
".message = $(jq -R -s '@json' <<<"${message_cat}")"
) )
if hasFlag -r --reset; then
curl -s -X POST https://infer.x64.world/reset-conversation "$@" >/dev/null curl -s -X POST https://infer.x64.world/reset-conversation "$@" >/dev/null
if [[ 0 -ne $? ]]; then if ! curl -s -X POST https://infer.x64.world/reset-conversation "$@" >/dev/null; then
echo "Failed to reset conversation." echo "Failed to reset conversation."
exit 1 exit 1
fi fi
fi
curl -s "https://infer.x64.world/chat" \ curl -s "https://infer.x64.world/chat" \
-X POST \ -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d "$message" | jq -r '.content | gsub("\""; "")' > ${TMP_FILE} -d "${message}" | jq -r '.content | gsub("\""; "")' >"${TMP_FILE}"
if [[ 0 -ne $? ]]; then if [[ 0 -ne $? ]]; then
echo "Failed to get response." echo "Failed to get response."
exit 1 exit 1
fi fi
git commit -e -m "$(cat ${TMP_FILE})" && rm ${TMP_FILE} git commit -e -m "$(cat "${TMP_FILE}")" && rm "${TMP_FILE}"
popd "$@" >/dev/null popd "$@" >/dev/null