Spaces:
Running
Running
sync: 172 file da Baida98/AI@f715ae37 (bypass deploy-all)
Browse files- api/policy.py +10 -2
api/policy.py
CHANGED
|
@@ -35,6 +35,10 @@ from .state import sb
|
|
| 35 |
|
| 36 |
_logger = logging.getLogger("api.policy")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# ββ Router βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
router = APIRouter(
|
| 40 |
prefix="/api/policy",
|
|
@@ -225,7 +229,11 @@ async def _sync_budget_from_supabase() -> None:
|
|
| 225 |
p = row.get("provider", "")
|
| 226 |
if p in _BUDGET:
|
| 227 |
_BUDGET[p]["used"] = float(row.get("used", 0))
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
_BUDGET[p]["currency"] = str(row.get("currency", "USD"))
|
| 230 |
except Exception as exc:
|
| 231 |
_logger.debug("[policy] Sync budget Supabase fallito (non bloccante): %s", exc)
|
|
@@ -275,7 +283,7 @@ async def record_budget_usage(req: BudgetRecordRequest) -> Dict[str, Any]:
|
|
| 275 |
"""
|
| 276 |
provider = req.provider.lower()
|
| 277 |
if provider not in _BUDGET:
|
| 278 |
-
_BUDGET[provider] = {"limit":
|
| 279 |
|
| 280 |
_BUDGET[provider]["used"] = round(_BUDGET[provider]["used"] + req.cost_usd, 6)
|
| 281 |
new_used = _BUDGET[provider]["used"]
|
|
|
|
| 35 |
|
| 36 |
_logger = logging.getLogger("api.policy")
|
| 37 |
|
| 38 |
+
# Safety cap per provider sconosciuto: evita che una riga budget assente
|
| 39 |
+
# venga interpretata come accesso illimitato o blocco permanente.
|
| 40 |
+
_DEFAULT_PROVIDER_LIMIT_USD = 1.0
|
| 41 |
+
|
| 42 |
# ββ Router βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 43 |
router = APIRouter(
|
| 44 |
prefix="/api/policy",
|
|
|
|
| 229 |
p = row.get("provider", "")
|
| 230 |
if p in _BUDGET:
|
| 231 |
_BUDGET[p]["used"] = float(row.get("used", 0))
|
| 232 |
+
# Supabase puΓ² contenere vecchie righe con limit=0: non
|
| 233 |
+
# deve disattivare accidentalmente i limiti paid hardcoded.
|
| 234 |
+
stored_limit = float(row.get("limit", 0))
|
| 235 |
+
if stored_limit > 0 or _BUDGET[p]["limit"] <= 0:
|
| 236 |
+
_BUDGET[p]["limit"] = stored_limit
|
| 237 |
_BUDGET[p]["currency"] = str(row.get("currency", "USD"))
|
| 238 |
except Exception as exc:
|
| 239 |
_logger.debug("[policy] Sync budget Supabase fallito (non bloccante): %s", exc)
|
|
|
|
| 283 |
"""
|
| 284 |
provider = req.provider.lower()
|
| 285 |
if provider not in _BUDGET:
|
| 286 |
+
_BUDGET[provider] = {"limit": _DEFAULT_PROVIDER_LIMIT_USD, "used": 0.0, "currency": "USD"}
|
| 287 |
|
| 288 |
_BUDGET[provider]["used"] = round(_BUDGET[provider]["used"] + req.cost_usd, 6)
|
| 289 |
new_used = _BUDGET[provider]["used"]
|