llm-provider-billing: bailian 脚本自动续期控制台会话(过期自动重登+重试)

This commit is contained in:
2026-09-14 14:36:19 +08:00
parent 1b822a3b27
commit 4a87701f8b
2 changed files with 65 additions and 11 deletions

View File

@@ -69,6 +69,7 @@ bash ~/.hermes/skills/custom/llm-provider-billing/scripts/bailian-token-plan.sh
- **Console 鉴权**:首次运行前需浏览器一次性授权 `bl auth login --console --console-site domestic`(国内站)
- **API Key 查不了配额**:`bl auth login --api-key <key>` / 环境变量里的 key(用户的是 `OPENAI_API_KEY`)只能调模型,不能查余量
- 授权凭据落盘 `~/.bailian/config.json`,登录一次长期有效;回调走本机 `127.0.0.1` 端口,浏览器必须与 CLI 同机
- **会话有效期由服务端决定**(实测约 1 天级,bl CLI 无 TTL 参数可调,config.json 只存 36 位 opaque access_token,无 refresh token)——过期后 `bailian-token-plan.sh` **已内置自动续期**:检测到 expired 错误自动拉起 `bl auth login --console`,等回调写盘后重试查询。若本机浏览器保持阿里云「自动登录」登录态,整条链路无需人工点击
### 返回结构(实测 2026-09-10)

View File

@@ -9,18 +9,71 @@ if ! command -v bl >/dev/null 2>&1; then
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
CFG_FILE="$HOME/.bailian/config.json"
# macOS / Linux 兼容的 mtime 秒数
cfg_mtime() {
if [ -f "$CFG_FILE" ]; then
if command -v stat >/dev/null 2>&1; then
stat -f %m "$CFG_FILE" 2>/dev/null || stat -c %Y "$CFG_FILE" 2>/dev/null || echo 0
else
echo 0
fi
else
echo 0
fi
}
echo "🔍 查询百炼 Token Plan 配额 ..." >&2
if ! bl usage token-plan --output json > /tmp/bailian-token-plan.json 2>/tmp/bailian-token-plan.err; then
# 尝试查询;失败原因写入 /tmp/bailian-token-plan.err
query_token_plan() {
echo "🔍 查询百炼 Token Plan 配额 ..." >&2
bl usage token-plan --output json > /tmp/bailian-token-plan.json 2>/tmp/bailian-token-plan.err
}
# 控制台会话过期/未登录时自动续期:拉起浏览器授权,等 config.json 刷新,然后重试。
# 前提:本机浏览器保持阿里云登录态(勾选「自动登录」),授权页会自动完成回调,无需点击。
relogin_console() {
local before after mtime_before
mtime_before=$(cfg_mtime)
echo "⚠️ 控制台会话已过期/未登录,自动发起浏览器续期(15 分钟内有效)..." >&2
bl auth login --console --console-site domestic >/tmp/bailian-relogin.log 2>&1 &
local LPID=$!
# 最多等 150s:config.json 被刷新,或登录进程退出
local i
for i in $(seq 1 30); do
sleep 5
after=$(cfg_mtime)
if [ "$after" != "$mtime_before" ] && [ "$after" != "0" ]; then
wait "$LPID" 2>/dev/null || true
echo "✅ 控制台授权完成,继续查询" >&2
return 0
fi
if ! kill -0 "$LPID" 2>/dev/null; then
wait "$LPID" 2>/dev/null || true
break
fi
done
# 仍然没刷新:杀掉可能残留的登录进程,提示手动完成
kill "$LPID" 2>/dev/null || true
wait "$LPID" 2>/dev/null || true
echo "⚠️ 未检测到授权回调完成。请检查是否弹出浏览器并完成授权(保持阿里云「自动登录」可免点击),然后重跑本脚本。" >&2
echo " 浏览器授权页若已关闭,可手动执行: bl auth login --console --console-site domestic" >&2
return 1
}
if ! query_token_plan; then
if grep -qiE "not logged in|has expired|NotLogined" /tmp/bailian-token-plan.err; then
if ! relogin_console || ! query_token_plan; then
echo "❌ bl usage token-plan 仍失败:" >&2
cat /tmp/bailian-token-plan.err >&2
exit 1
fi
else
echo "❌ bl usage token-plan 失败:" >&2
cat /tmp/bailian-token-plan.err >&2
exit 1
fi
fi
cat /tmp/bailian-token-plan.json
echo