AIDA / app /ai /agent /reply_intelligence.py
destinyebuka's picture
fyp
fd08741
"""
Intelligent reply detection for AIDA.
Determines which message AIDA should quote when responding.
"""
from typing import Optional, Dict, Any, List
def determine_reply_to_message(
conversation_history: List[Dict[str, Any]],
reply_context: Optional[Dict[str, Any]] = None
) -> Optional[str]:
"""
Analyze conversation and determine which message AIDA should quote.
Strategy:
1. If user just replied to a specific message (reply_context exists),
quote the LATEST USER MESSAGE, not the message they swiped
2. Otherwise, quote the most recent user message
Args:
conversation_history: List of messages with structure:
[{"id": str, "sender_id": str, "content": str, "is_ai": bool, ...}, ...]
reply_context: Optional context of message user replied to
Returns:
Message ID to quote, or None if no appropriate message found
"""
if not conversation_history:
return None
# Find the most recent user message (not AI)
latest_user_message = None
for msg in conversation_history:
if not msg.get("is_ai", False):
latest_user_message = msg
break # conversation_history is sorted newest first
if latest_user_message:
return latest_user_message.get("id")
return None
def build_reply_context_for_response(
conversation_history: List[Dict[str, Any]],
reply_to_id: str
) -> Optional[Dict[str, Any]]:
"""
Build reply context for AIDA's response.
Returns:
{
"id": message_id,
"sender_name": sender name,
"content": message content
}
"""
if not reply_to_id:
return None
# Find the message to quote
for msg in conversation_history:
if msg.get("id") == reply_to_id:
return {
"id": msg.get("id"),
"sender_name": msg.get("sender_name", "User"),
"content": msg.get("content", ""),
}
return None