feat: показывать баланс DeepSeek при запуске и в статусной строке Claude Code

- Функция _deepseek_balance в ai-api-helpers: запрос к GET /user/balance
- Вывод баланса при проверке сохранённого и нового ключа в ai-deepseek
- Кеширование баланса в ~/.cache/ai-setup/deepseek_balance для статусной строки
- statusline-command.sh: если модель содержит deepseek — показывать  вместо usage

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-06 11:14:57 +03:00
parent 2f48d038bd
commit c1e68571f8

View File

@@ -787,6 +787,51 @@ _claude_test_openai_api() {
_CLAUDE_TEST_BODY=$(echo "$response" | sed '$d')
}
# _deepseek_balance: Query DeepSeek balance API and print info
_deepseek_balance() {
local api_key="$1"
local response
response=$(curl -s --max-time 10 "https://api.deepseek.com/user/balance" \
-H "Authorization: Bearer $api_key" \
-H "Accept: application/json" \
2>/dev/null || echo "")
if [ -z "$response" ]; then
echo -e " \033[0;33m[БАЛАНС]\033[0m Не удалось получить баланс (сеть?)"
return 1
fi
echo "$response" | python3 -c "
import sys, json, os
try:
d = json.load(sys.stdin)
available = d.get('is_available', False)
infos = d.get('balance_infos', [])
if not infos:
print(' \033[0;33m[БАЛАНС]\033[0m Нет данных о балансе')
sys.exit(0)
first = True
for info in infos:
curr = info.get('currency', '???')
total = info.get('total_balance', '0')
granted = info.get('granted_balance', '0')
topped_up = info.get('topped_up_balance', '0')
status = '✅ доступен' if available else '❌ не активен'
print(f' \033[1;36m💰 Баланс DeepSeek:\033[0m {total} {curr} {status}')
if float(granted) > 0:
print(f' └─ Начислено: {granted} {curr}')
if float(topped_up) > 0:
print(f' └─ Пополнено: {topped_up} {curr}')
# Cache first currency entry for statusline
if first:
cache_dir = os.path.expanduser('~/.cache/ai-setup')
os.makedirs(cache_dir, exist_ok=True)
with open(os.path.join(cache_dir, 'deepseek_balance'), 'w') as f:
f.write(f'{total} {curr}\n')
first = False
except Exception as e:
print(f' ⚠️ Не удалось разобрать баланс: {e}')
" 2>/dev/null || echo -e " \033[0;33m[БАЛАНС]\033[0m Ошибка парсинга ответа"
}
_handle_openai_api_response() {
local provider="$1"
local code="$2"
@@ -1019,6 +1064,7 @@ if [ -n "$api_key" ]; then
elif [ $ret -ne 0 ]; then
exit 1
fi
_deepseek_balance "$api_key"
fi
if [ -z "$api_key" ] && [ "$reauth" -eq 1 ]; then
@@ -1040,6 +1086,7 @@ if [ -z "$api_key" ]; then
echo "$api_key" > "$key_file"
chmod 600 "$key_file"
echo "Ключ сохранён."
_deepseek_balance "$api_key"
if [ $ret -eq 429 ]; then
echo -n "Продолжить всё равно? (запросы могут не проходить) [y/N] "
read -r _ans; case "${_ans:-N}" in [Yy]*) ;; *) exit 1 ;; esac