Only use recent chat sessions for agent MRU. Handle null agent chats

This commit is contained in:
Debanjum Singh Solanky 2024-10-22 14:21:48 -07:00
parent 9b554feb91
commit aca8bef024

View file

@ -1,7 +1,7 @@
import json import json
import logging import logging
import random import random
from datetime import datetime, timezone from datetime import datetime, timedelta, timezone
from typing import Dict, List, Optional from typing import Dict, List, Optional
from asgiref.sync import sync_to_async from asgiref.sync import sync_to_async
@ -72,14 +72,17 @@ async def all_agents(
else: else:
agents_packet.append(agent_packet) agents_packet.append(agent_packet)
# Load Conversation Sessions # Load recent conversation sessions
min_date = datetime.min.replace(tzinfo=timezone.utc) min_date = datetime.min.replace(tzinfo=timezone.utc)
two_weeks_ago = datetime.today() - timedelta(weeks=2)
conversations = [] conversations = []
if user: if user:
conversations = await sync_to_async(list[Conversation])( conversations = await sync_to_async(list[Conversation])(
ConversationAdapters.get_conversation_sessions(user, request.user.client_app).reverse() ConversationAdapters.get_conversation_sessions(user, request.user.client_app)
.filter(updated_at__gte=two_weeks_ago)
.order_by("-updated_at")[:50]
) )
conversation_times = {conv.agent.slug: conv.updated_at for conv in conversations} conversation_times = {conv.agent.slug: conv.updated_at for conv in conversations if conv.agent}
# Put default agent first, then sort by mru and finally shuffle unused randomly # Put default agent first, then sort by mru and finally shuffle unused randomly
random.shuffle(agents_packet) random.shuffle(agents_packet)