From 7a156e2af19794f8727a778808a6c1502e90e4b1 Mon Sep 17 00:00:00 2001 From: ChemaVX Date: Wed, 6 May 2026 07:49:29 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20mover=20alerta=20de=20coste=20a=20/gener?= =?UTF-8?q?ate=20donde=20est=C3=A1=20el=20gasto=20real?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot/bot.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/bot/bot.py b/src/bot/bot.py index e29fa37..7842f13 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -112,23 +112,8 @@ 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( @@ -382,6 +367,18 @@ async def cmd_generate(update: Update, ctx: ContextTypes.DEFAULT_TYPE): else: await send_chunked(update.message, output) + try: + stats = await db.get_usage_stats(session_id) + total_cost = sum(s.get("total_cost", 0) for s in stats) + if total_cost > settings.cost_alert_threshold: + await update.message.reply_text( + f"āš ļø Coste acumulado de esta sesión: `${total_cost:.4f}`" + f" (umbral: `${settings.cost_alert_threshold:.2f}`)", + parse_mode=ParseMode.MARKDOWN + ) + except Exception: + pass + except Exception as e: logger.error("Generate failed", error=str(e)) await update.message.reply_text(f"āŒ Generation failed: {str(e)[:200]}")