diff --git a/src/bot/bot.py b/src/bot/bot.py index b3bca69..28d12e1 100644 --- a/src/bot/bot.py +++ b/src/bot/bot.py @@ -305,12 +305,19 @@ async def cmd_generate(update: Update, ctx: ContextTypes.DEFAULT_TYPE): db = ResearchDB(db_conn) try: - # Find last session for this chat - cursor = await db_conn.execute( - """SELECT * FROM research_sessions WHERE telegram_chat_id = ? - ORDER BY created_at DESC LIMIT 1""", - (chat_id,) - ) + # Usa la sesión activa si existe, si no la más reciente + session_id = _active_sessions.get(chat_id) + if session_id: + cursor = await db_conn.execute( + "SELECT * FROM research_sessions WHERE id = ?", + (session_id,) + ) + else: + cursor = await db_conn.execute( + """SELECT * FROM research_sessions WHERE telegram_chat_id = ? + ORDER BY created_at DESC LIMIT 1""", + (chat_id,) + ) row = await cursor.fetchone() if not row: await update.message.reply_text("No research sessions found. Start with /research ")