diff --git a/app/main.py b/app/main.py index e026e55..d4ac141 100644 --- a/app/main.py +++ b/app/main.py @@ -4229,6 +4229,29 @@ async def litellm_update_team_budget(team_id: str, budget: float) -> None: r.raise_for_status() +async def litellm_clear_team_budget(team_id: str) -> None: + """Remove a team's spend cap (max_budget = NULL = unlimited). + + Operator/infrastructure accounts are collective expenses, not members: + applying the $15/credits cap to them would cut off the co-op's own tooling + mid-task. Written straight to the team row because /team/update also + silently drops budget_reset_at (an uncapped team needs no reset). + """ + def _write() -> None: + conn = _pg_connect() + try: + cur = conn.cursor() + cur.execute( + 'UPDATE "LiteLLM_TeamTable" SET max_budget = NULL WHERE team_id = %s', + (team_id,), + ) + conn.commit() + finally: + conn.close() + + await asyncio.get_event_loop().run_in_executor(None, _write) + + async def litellm_pin_reset_date(team_id: str, reset_at: datetime) -> None: """Pin a team's allowance reset to an exact moment (next OC payment date). @@ -4257,20 +4280,25 @@ def next_reset_from_now() -> datetime: return datetime.now(timezone.utc).replace(minute=0, second=0, microsecond=0) + timedelta(days=30) -async def sync_team_budget_from_balance(email: str) -> float: +async def sync_team_budget_from_balance(email: str) -> float | None: """Apply the member's effective budget to their LiteLLM team. Effective budget = monthly allowance + credits still available (credits drawn only after the allowance is exhausted; credits consumed in CLOSED cycles are deducted by `close_credit_cycles`). Reads the member's live spend from LiteLLM to split this cycle's spend for display only. - Returns the applied budget. + Returns the applied budget; None for uncapped operator accounts, whose team + cap is cleared rather than computed. """ balance = get_member_balance(email) credits = get_credit_balance(email) drawn = get_credits_drawn(email) team_id = await litellm_get_or_create_team(email, balance) + if (email or "").strip().lower() in UNCAPPED_EMAILS: + await litellm_clear_team_budget(team_id) + return None + # Live spend this cycle (DB path preferred; LiteLLM computes the reset). team = get_team_budget_from_db(team_id) or {} team_spend = team.get("spend") @@ -6159,6 +6187,15 @@ OPERATOR_EMAILS = { if e.strip() } +# Accounts that hold NO LiteLLM spend cap: the co-op's own infrastructure, whose +# usage is a collective expense and must not be cut off mid-task. Default: the +# operator accounts; UNCAPPED_EMAILS adds (or replaces) via env. +UNCAPPED_EMAILS = ( + {e.strip().lower() for e in os.environ["UNCAPPED_EMAILS"].split(",") if e.strip()} + if os.environ.get("UNCAPPED_EMAILS") + else set(OPERATOR_EMAILS) +) + def _membership_events_for_overview(conn: sqlite3.Connection) -> tuple[list[dict], dict[str, dict]]: """Return (newest-first event log, latest deactivation per member email)."""