shim: model-level cooldown on Theos-403, balance-first ordering, MAX_TRIES=14; e2e 5/5 via 48005
This commit is contained in:
@@ -25,29 +25,40 @@ import dpcode as D
|
||||
|
||||
BANK_PATH = D.BANK
|
||||
COOLDOWN = 120 # сек, сколько аккаунт не трогаем после ошибки
|
||||
MAX_TRIES = 6
|
||||
MAX_TRIES = 14
|
||||
_lock = threading.Lock()
|
||||
_state = {"cool": {}, "used": {}, "ok": 0, "fail": 0}
|
||||
_state = {"cool": {}, "modelcool": {}, "used": {}, "ok": 0, "fail": 0}
|
||||
|
||||
|
||||
def accounts():
|
||||
return [r for r in D.read_bank(BANK_PATH) if r.get("cookies")]
|
||||
|
||||
|
||||
def _bal(rec):
|
||||
try:
|
||||
return float((rec.get("theos") or {}).get("balance") or 0)
|
||||
except Exception:
|
||||
return 0.0
|
||||
|
||||
|
||||
def ordered():
|
||||
recs = accounts()
|
||||
now = time.time()
|
||||
fresh = [r for r in recs if _state["cool"].get(r["email"], 0) <= now]
|
||||
stale = [r for r in recs if _state["cool"].get(r["email"], 0) > now]
|
||||
# ротация по кругу: кто меньше использован — первым
|
||||
fresh.sort(key=lambda r: _state["used"].get(r["email"], 0))
|
||||
# приоритет: у кого есть Theos (для frontier), затем менее использованные
|
||||
fresh.sort(key=lambda r: (-_bal(r), _state["used"].get(r["email"], 0)))
|
||||
return fresh + stale
|
||||
|
||||
|
||||
def call_upstream(model, payload, stream):
|
||||
"""Возвращает (requests.Response, rec) либо (None, last_error)."""
|
||||
last = None
|
||||
now = time.time()
|
||||
for rec in ordered()[:MAX_TRIES]:
|
||||
email = rec["email"]
|
||||
if _state["modelcool"].get((email, model), 0) > now:
|
||||
continue # у этого аккаунта кончились Theos на эту модель — free-модели не трогаем
|
||||
machine = rec.get("machine") or D.machine_header(salt=rec.get("email"))
|
||||
s = D.session_from(rec)
|
||||
body = dict(payload)
|
||||
@@ -74,6 +85,12 @@ def call_upstream(model, payload, stream):
|
||||
except Exception:
|
||||
pass
|
||||
last = f"{r.status_code} {text}"
|
||||
# кончились Theos на frontier — это НЕ мёртвый аккаунт: free-модели продолжают работать
|
||||
if "Free daily Theos" in text or "Theos are not available" in text:
|
||||
with _lock:
|
||||
_state["modelcool"][(email, model)] = time.time() + 6 * 3600
|
||||
_state["fail"] += 1
|
||||
continue
|
||||
# machine_required лечится пересчётом machine-id, а не сменой аккаунта
|
||||
if "machine_required" in text:
|
||||
rec["machine"] = D.machine_header(salt=rec["email"] + secrets.token_hex(4))
|
||||
|
||||
Reference in New Issue
Block a user