#!/usr/bin/env bash # 阿里云百炼 Token Plan 余量/配额查询 — 依赖 bl CLI 的控制台登录(一次性授权) # 用法: bash bailian-token-plan.sh [--summary] # --summary 额外输出统一用量摘要(免费额度 + 近期用量) set -euo pipefail if ! command -v bl >/dev/null 2>&1; then echo "❌ 未找到 bl CLI,请先: npm install -g bailian-cli" >&2 exit 1 fi AUTH=$(bl auth status --output json 2>/dev/null || true) if ! echo "$AUTH" | grep -q '"authenticated": *true'; then echo "❌ 百炼控制台未登录(API Key 只能调模型,查不了配额)。请先执行一次性授权:" >&2 echo " bl auth login --console --console-site domestic" >&2 exit 1 fi echo "🔍 查询百炼 Token Plan 配额 ..." >&2 if ! bl usage token-plan --output json > /tmp/bailian-token-plan.json 2>/tmp/bailian-token-plan.err; then echo "❌ bl usage token-plan 失败:" >&2 cat /tmp/bailian-token-plan.err >&2 exit 1 fi cat /tmp/bailian-token-plan.json echo # 友好摘要:周配额已用/剩余比例 + 重置时间(本地时区) if command -v python3 >/dev/null 2>&1; then python3 - <<'PY' import json, sys, datetime, time try: d = json.load(open('/tmp/bailian-token-plan.json')) except Exception as e: print(f"⚠️ 无法解析 JSON: {e}", file=sys.stderr) sys.exit(0) used = d.get('per1WeekPercentage') reset = d.get('per1WeekResetTime') if used is not None: used_pct = used * 100 print(f"📊 本周 Token Plan 已用: {used_pct:.2f}% | 剩余约为: {100 - used_pct:.2f}%") else: print("⚠️ 响应中没有 per1WeekPercentage 字段(结构可能已变化),请参考上方原始 JSON") if reset: dt = datetime.datetime.fromtimestamp(reset / 1000) tzname = time.strftime('%Z') print(f"🔄 本周配额重置时间: {dt.strftime('%Y-%m-%d %H:%M:%S')}(本地时区 {tzname or 'local'})") PY fi if [ "${1:-}" = "--summary" ]; then echo "=== 统一用量摘要(免费额度等) ===" >&2 bl usage summary --output json fi