fix: use _API_RET global var instead of bash return for HTTP codes > 255

bash return values are truncated to 0-255: 401 % 256 = 145, 429 % 256 = 173.
This caused _handle_api_response / _handle_openai_api_response to return 145
instead of 401, so the reauth prompt was never triggered and the script exited
silently with code 1.

Fix: use global _API_RET variable in both functions, read via ret=$_API_RET
in ai-deepseek and ai-kimi launchers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-04 23:23:56 +07:00
parent 81a7b024ee
commit c68aff4725

View File

@@ -306,18 +306,24 @@ _handle_openai_api_response() {
local code="$2"
local body="$3"
local topup_url="$4"
# Используем глобальную переменную _API_RET вместо return,
# потому что bash return умеет только 0-255, а HTTP-коды
# вроде 401/429 обрезаются (401 % 256 = 145).
_API_RET=0
local _emsg
case "$code" in
200)
echo -e "\033[0;32mOK\033[0m"
return 0
_API_RET=0
;;
401|403)
_emsg=$(_claude_extract_error "$body")
echo ""
echo -e "\033[0;31m[ОШИБКА АВТОРИЗАЦИИ]\033[0m Авторизация $provider недействительна (HTTP $code)."
[ -n "$_emsg" ] && echo " $_emsg"
return 401
_API_RET=401
;;
429)
_emsg=$(_claude_extract_error "$body")
@@ -325,21 +331,22 @@ _handle_openai_api_response() {
echo -e "\033[0;33m[ЛИМИТ ИСЧЕРПАН]\033[0m Баланс/лимит $provider исчерпан."
[ -n "$_emsg" ] && echo " $_emsg"
[ -n "$topup_url" ] && echo " $topup_url"
return 429
_API_RET=429
;;
000)
echo ""
echo -e "\033[0;33m[СЕТЬ]\033[0m Не удалось проверить ключ (нет сети?). Продолжаю..."
return 0
_API_RET=0
;;
*)
_emsg=$(_claude_extract_error "$body")
echo ""
echo -e "\033[0;31m[ОШИБКА]\033[0m API $provider вернул HTTP $code."
[ -n "$_emsg" ] && echo " $_emsg"
return 1
_API_RET=$code
;;
esac
return 0
}
_claude_extract_error() {
@@ -377,19 +384,24 @@ _handle_api_response() {
local code="$2"
local body="$3"
local topup_url="$4"
# Используем глобальную переменную _API_RET вместо return,
# потому что bash return умеет только 0-255, а HTTP-коды
# вроде 401/429 обрезаются (401 % 256 = 145).
_API_RET=0
local _emsg
case "$code" in
200)
echo -e "\033[0;32mOK\033[0m"
return 0
_API_RET=0
;;
401|403)
_emsg=$(_claude_extract_error "$body")
echo ""
echo -e "\033[0;31m[ОШИБКА АВТОРИЗАЦИИ]\033[0m Авторизация $provider недействительна (HTTP $code)."
[ -n "$_emsg" ] && echo " $_emsg"
return 401
_API_RET=401
;;
429)
_emsg=$(_claude_extract_error "$body")
@@ -397,12 +409,12 @@ _handle_api_response() {
echo -e "\033[0;33m[ЛИМИТ ИСЧЕРПАН]\033[0m Баланс/лимит $provider исчерпан."
[ -n "$_emsg" ] && echo " $_emsg"
[ -n "$topup_url" ] && echo " $topup_url"
return 429
_API_RET=429
;;
000)
echo ""
echo -e "\033[0;33m[СЕТЬ]\033[0m Не удалось проверить ключ (нет сети?). Продолжаю..."
return 0
_API_RET=0
;;
400)
_emsg=$(_claude_extract_error "$body")
@@ -410,20 +422,22 @@ _handle_api_response() {
echo ""
echo -e "\033[0;33m[КВОТА ИСЧЕРПАНА]\033[0m Лимит запросов исчерпан."
[ -n "$topup_url" ] && echo " $topup_url"
return 429
_API_RET=429
else
# 400 = auth is valid, but max_tokens=1 is too small for thinking models
echo -e "\033[0;32mOK\033[0m"
_API_RET=0
fi
# 400 = auth is valid, but max_tokens=1 is too small for thinking models
echo -e "\033[0;32mOK\033[0m"
return 0
;;
*)
_emsg=$(_claude_extract_error "$body")
echo ""
echo -e "\033[0;31m[ОШИБКА]\033[0m API $provider вернул HTTP $code."
[ -n "$_emsg" ] && echo " $_emsg"
return 1
_API_RET=$code
;;
esac
return 0
}
_open_browser() {
@@ -508,7 +522,7 @@ if [ -n "$api_key" ]; then
echo -n "Проверка сохранённого DeepSeek ключа... "
_claude_test_api "https://api.deepseek.com/anthropic/v1/messages" "x-api-key: $api_key" "deepseek-v4-flash"
_handle_api_response "DeepSeek" "$_CLAUDE_TEST_CODE" "$_CLAUDE_TEST_BODY" "Пополните баланс: https://platform.deepseek.com/top_up"
ret=$?
ret=$_API_RET
if [ $ret -eq 401 ]; then
rm -f "$key_file"
api_key=""
@@ -534,7 +548,7 @@ if [ -z "$api_key" ]; then
echo -n "Проверяю ключ и баланс... "
_claude_test_api "https://api.deepseek.com/anthropic/v1/messages" "x-api-key: $api_key" "deepseek-v4-flash"
_handle_api_response "DeepSeek" "$_CLAUDE_TEST_CODE" "$_CLAUDE_TEST_BODY" "Пополните баланс: https://platform.deepseek.com/top_up"
ret=$?
ret=$_API_RET
if [ $ret -eq 0 ] || [ $ret -eq 429 ]; then
mkdir -p "$(dirname "$key_file")"
echo "$api_key" > "$key_file"
@@ -578,7 +592,7 @@ if [ -n "$api_key" ]; then
echo -n "Проверка сохранённого Kimi ключа... "
_claude_test_api "https://api.kimi.com/coding/v1/messages" "x-api-key: $api_key" "kimi-k2.6"
_handle_api_response "Kimi" "$_CLAUDE_TEST_CODE" "$_CLAUDE_TEST_BODY" "Пополните баланс: https://www.kimi.com/code"
ret=$?
ret=$_API_RET
if [ $ret -eq 401 ]; then
rm -f "$key_file"
api_key=""
@@ -598,7 +612,7 @@ if [ -z "$api_key" ]; then
echo -n "Проверяю ключ и баланс... "
_claude_test_api "https://api.kimi.com/coding/v1/messages" "x-api-key: $api_key" "kimi-k2.6"
_handle_api_response "Kimi" "$_CLAUDE_TEST_CODE" "$_CLAUDE_TEST_BODY" "Пополните баланс: https://www.kimi.com/code"
ret=$?
ret=$_API_RET
if [ $ret -eq 0 ] || [ $ret -eq 429 ]; then
mkdir -p "$(dirname "$key_file")"
echo "$api_key" > "$key_file"