Spaces:
Paused
Paused
fix: 修复额度检查误判和 exhausted 账号未及时移出的问题
Browse files1. codex_auth: 修正请求参数(模型名 codex-mini→gpt-5,添加 Chatgpt-Account-Id 等头),
参考 CPA 实现确保正确检测 Team 5 小时额度限制
2. manager: rotate Step 2 改为移出所有 exhausted 账号,而非仅本次新发现的
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Files changed (2)
525 unmodified lines5265275285295305315325335345355365375385395405415425435445455465475485492 unmodified lines552553554555556557558559560561562563564565566567568569570571572573574575576577578579580525 unmodified linesreturn str(filepath)def check_codex_quota(access_token):"""检查 Codex 额度是否用完。返回 ("ok", None) | ("exhausted", resets_at) | ("auth_error", None)"""import requeststry:resp = requests.post("https://chatgpt.com/backend-api/codex/responses",headers={"Authorization": f"Bearer {access_token}","Content-Type": "application/json",},json={"model": "codex-mini","instructions": "test","stream": False,},timeout=30,)2 unmodified linesreturn "auth_error", Noneif resp.status_code == 429:try:err = resp.json().get("error", {})if err.get("type") == "usage_limit_reached":resets_at = err.get("resets_at", 0)resets_in = err.get("resets_in_seconds", 0)if resets_at and resets_at > time.time():recover_time = resets_atelif resets_in:recover_time = time.time() + resets_inelse:recover_time = time.time() + 18000return "exhausted", recover_timeexcept Exception:passreturn "exhausted", time.time() + 18000if resp.status_code in (401, 403):return "auth_error", None# 200 或其他非错误状态码(如 400 参数错误)都说明 token 有效、额度可用return "ok", Nonedef refresh_access_token(refresh_token):"""刷新 access token"""import requests525 unmodified lines5265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645652 unmodified lines568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607525 unmodified linesreturn str(filepath)def check_codex_quota(access_token, account_id=None):"""检查 Codex 额度是否用完。参考 CPA (CLIProxyAPI) 的实现,需要正确设置请求头才能检查 Team 额度。返回 ("ok", None) | ("exhausted", resets_at) | ("auth_error", None)"""import requestsif not account_id:from autoteam.config import CHATGPT_ACCOUNT_IDaccount_id = CHATGPT_ACCOUNT_IDheaders = {"Authorization": f"Bearer {access_token}","Content-Type": "application/json","Accept": "text/event-stream","User-Agent": "codex-tui/0.118.0 (Mac OS 26.3.1; arm64) iTerm.app/3.6.9 (codex-tui; 0.118.0)","Originator": "codex-tui","Connection": "Keep-Alive",}if account_id:headers["Chatgpt-Account-Id"] = account_idtry:# 最小化请求:额度用完时 429 在模型执行前返回,不消耗额度resp = requests.post("https://chatgpt.com/backend-api/codex/responses",headers=headers,json={"model": "gpt-5","instructions": "","stream": True,"store": False,"input": [{"role": "user", "content": [{"type": "input_text", "text": "."}]}],},timeout=30,)2 unmodified linesreturn "auth_error", Noneif resp.status_code == 429:return _parse_quota_error(resp)if resp.status_code in (401, 403):return "auth_error", None# "model is at capacity" 也视为临时不可用(参考 CPA isCodexModelCapacityError)if resp.status_code >= 400:body_text = resp.text[:500].lower()if "at capacity" in body_text:return "exhausted", time.time() + 300 # 5 分钟后重试# 200 或其他非错误状态码(如 400 参数错误)都说明 token 有效、额度可用return "ok", Nonedef _parse_quota_error(resp):"""解析 429 响应中的额度限制信息(参考 CPA parseCodexRetryAfter)"""try:err = resp.json().get("error", {})if err.get("type") == "usage_limit_reached":resets_at = err.get("resets_at", 0)resets_in = err.get("resets_in_seconds", 0)# 优先使用 resets_at(绝对时间),CPA 也是这个优先级if resets_at and resets_at > time.time():return "exhausted", resets_at# fallback 到 resets_in_seconds(相对时间)if resets_in:return "exhausted", time.time() + resets_inreturn "exhausted", time.time() + 18000except Exception:passreturn "exhausted", time.time() + 18000def refresh_access_token(refresh_token):"""刷新 access token"""import requests
809 unmodified lines81081181281381481581681781881982044 unmodified lines865866867868869870871872809 unmodified linesreturn mail_clienttry:# === Step 2: 移出额度用完的账号 ===if exhausted:print(f"\n=== Step 2: 移出 {len(exhausted)} 个额度用完的账号 ===")api = ensure_chatgpt()for acc in exhausted:email = acc["email"]if not chatgpt.browser:chatgpt.start()44 unmodified lines# === Step 5: 判断是否需要创建新账号 ===# 检查当前 active 账号中是否还有额度可用的active_accounts = get_active_accounts()active_with_quota = [a for a in active_accounts if a["email"] not in{e["email"] for e in exhausted}]not_recovered = [a for a in standby_list if not a.get("_quota_recovered")]809 unmodified lines81081181281381481581681781881982082182282344 unmodified lines868869870871872873874875809 unmodified linesreturn mail_clienttry:# === Step 2: 移出所有额度用完的账号(包括之前已标记为 exhausted 的)===all_accounts = load_accounts()all_exhausted = [a for a in all_accounts if a["status"] == STATUS_EXHAUSTED]if all_exhausted:print(f"\n=== Step 2: 移出 {len(all_exhausted)} 个额度用完的账号 ===")ensure_chatgpt()for acc in all_exhausted:email = acc["email"]if not chatgpt.browser:chatgpt.start()44 unmodified lines# === Step 5: 判断是否需要创建新账号 ===# 检查当前 active 账号中是否还有额度可用的active_accounts = get_active_accounts()exhausted_emails = {a["email"] for a in load_accounts() if a["status"] == STATUS_EXHAUSTED}active_with_quota = [a for a in active_accounts if a["email"] not in exhausted_emails]not_recovered = [a for a in standby_list if not a.get("_quota_recovered")]
src/autoteam/codex_auth.py+50-23
src/autoteam/manager.py+10-7