From 279475a175018e097a23233f892e30ec4057623a Mon Sep 17 00:00:00 2001 From: ChemaVX Date: Wed, 6 May 2026 07:23:11 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20alerta=20de=20coste=20=E2=80=94=20aviso?= =?UTF-8?q?=20si=20sesi=C3=B3n=20supera=20COST=5FALERT=5FTHRESHOLD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot/bot.py | 15 +++++++++++++++ src/config.py | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/bot/bot.py b/src/bot/bot.py index a4bf5c4..e29fa37 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -112,8 +112,23 @@ async def run_scheduled_research(bot, chat_id: int, topic: str, f"🔍 Investigación completada — analizando novedades…" ) else: + async def get_session_cost(session_id: int) -> float: + try: + stats = await db.get_usage_stats(session_id) + return sum(s.get("total_cost", 0) for s in stats) + except Exception: + return 0.0 + + session_cost = await get_session_cost(session_id) + cost_warning = "" + if session_cost > settings.cost_alert_threshold: + cost_warning = ( + f"\n⚠️ Coste: ${session_cost:.4f}" + f" (umbral: ${settings.cost_alert_threshold:.2f})" + ) await reporter.done( f"✅ Listo — `{scraped}` fuentes · `{chunk_count}` chunks · usa /generate " + f"{cost_warning}" ) else: await reporter.done( diff --git a/src/config.py b/src/config.py index 8363b87..03420de 100644 --- a/src/config.py +++ b/src/config.py @@ -33,6 +33,9 @@ class Settings(BaseSettings): chunk_overlap: int = Field(100, env="CHUNK_OVERLAP") quality_threshold: float = Field(0.3, env="QUALITY_THRESHOLD") # 0-1, chunks below discarded + # Alerts + cost_alert_threshold: float = Field(0.15, env="COST_ALERT_THRESHOLD") + # App log_level: str = Field("INFO", env="LOG_LEVEL")