diff --git a/telegram/bot.py b/telegram/bot.py index d65a7b6..67f2e75 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -268,9 +268,20 @@ def _compress_history(history: list[dict]) -> str: def _format_conversation_history(chat_id: int, user_id: int) -> str: - """Format conversation history with compressed context summary (Ganymede: Option C+A).""" - key = (chat_id, user_id) - history = conversation_history.get(key, []) + """Format conversation history with compressed context summary (Ganymede: Option C+A). + + In group chats, merges user-specific history with chat-level history + so the bot sees exchanges from other users in the same chat. + """ + user_key = (chat_id, user_id) + chat_key = (chat_id, 0) # chat-level history (all users) + + # Merge: chat-level history gives full group context + chat_history = conversation_history.get(chat_key, []) + user_history = conversation_history.get(user_key, []) + + # Use chat-level if available (group chats), otherwise user-level (DMs) + history = chat_history if chat_history else user_history if not history: return "(No prior conversation)" @@ -280,8 +291,9 @@ def _format_conversation_history(chat_id: int, user_id: int) -> str: # Full exchange log for reference for exchange in history: + who = exchange.get("username", "User") if exchange.get("user"): - lines.append(f"User: {exchange['user']}") + lines.append(f"@{who}: {exchange['user']}") if exchange.get("bot"): lines.append(f"Rio: {exchange['bot']}") lines.append("") @@ -675,12 +687,21 @@ IMPORTANT: Two special tags you can append at the end of your response (after yo # Update conversation state: reset window, store history (Ganymede+Rhea) if user: + username = user.username or "anonymous" key = (msg.chat_id, user.id) unanswered_count[key] = 0 # reset — conversation alive + entry = {"user": text[:500], "bot": response[:500], "username": username} + # Per-user history history = conversation_history.setdefault(key, []) - history.append({"user": text[:500], "bot": response[:500]}) + history.append(entry) if len(history) > MAX_HISTORY: history.pop(0) + # Chat-level history (group context — all users visible) + chat_key = (msg.chat_id, 0) + chat_history = conversation_history.setdefault(chat_key, []) + chat_history.append(entry) + if len(chat_history) > MAX_HISTORY: + chat_history.pop(0) # Record rate limit if user: