Spaces:
Running
Running
File size: 8,051 Bytes
676582c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
"""Conversation service for CRUD operations."""
from typing import List, Optional
from datetime import datetime
from sqlmodel import Session, select
from src.models.conversation import Conversation
from src.models.message import Message
from src.core.config import settings
class ConversationService:
"""Service for managing conversations and messages.
Handles CRUD operations for conversations and messages,
including conversation history retrieval and trimming.
"""
def __init__(self, db: Session):
"""Initialize the conversation service.
Args:
db: SQLModel database session
"""
self.db = db
def create_conversation(self, user_id: int, title: str | None = None) -> Conversation:
"""Create a new conversation for a user.
Args:
user_id: ID of the user creating the conversation
title: Optional title for the conversation
Returns:
Created Conversation object
"""
conversation = Conversation(
user_id=user_id,
title=title or "New Conversation",
created_at=datetime.utcnow(),
updated_at=datetime.utcnow()
)
self.db.add(conversation)
self.db.commit()
self.db.refresh(conversation)
return conversation
def get_conversation(self, conversation_id: int, user_id: int) -> Optional[Conversation]:
"""Get a conversation by ID, ensuring it belongs to the user.
Args:
conversation_id: ID of the conversation
user_id: ID of the user (for authorization)
Returns:
Conversation object if found and authorized, None otherwise
"""
statement = select(Conversation).where(
Conversation.id == conversation_id,
Conversation.user_id == user_id
)
return self.db.exec(statement).first()
def get_user_conversations(self, user_id: int, limit: int = 50) -> List[Conversation]:
"""Get all conversations for a user.
Args:
user_id: ID of the user
limit: Maximum number of conversations to return
Returns:
List of Conversation objects
"""
statement = (
select(Conversation)
.where(Conversation.user_id == user_id)
.order_by(Conversation.updated_at.desc())
.limit(limit)
)
return list(self.db.exec(statement).all())
def add_message(
self,
conversation_id: int,
role: str,
content: str,
token_count: int | None = None
) -> Message:
"""Add a message to a conversation.
Args:
conversation_id: ID of the conversation
role: Role of the message sender ("user" or "assistant")
content: Content of the message
token_count: Optional token count for the message
Returns:
Created Message object
"""
message = Message(
conversation_id=conversation_id,
role=role,
content=content,
token_count=token_count,
timestamp=datetime.utcnow()
)
self.db.add(message)
# Update conversation's updated_at timestamp
conversation = self.db.get(Conversation, conversation_id)
if conversation:
conversation.updated_at = datetime.utcnow()
self.db.commit()
self.db.refresh(message)
return message
def get_conversation_messages(
self,
conversation_id: int,
limit: int | None = None
) -> List[Message]:
"""Get all messages for a conversation.
Args:
conversation_id: ID of the conversation
limit: Optional limit on number of messages to return
Returns:
List of Message objects ordered by timestamp
"""
statement = (
select(Message)
.where(Message.conversation_id == conversation_id)
.order_by(Message.timestamp.asc())
)
if limit:
statement = statement.limit(limit)
return list(self.db.exec(statement).all())
def trim_conversation_history(
self,
conversation_id: int,
max_messages: int | None = None,
max_tokens: int | None = None
) -> List[Message]:
"""Trim conversation history based on message count and token limits.
Implements hybrid trimming strategy:
1. Keep most recent N messages (max_messages)
2. Within those, ensure total tokens don't exceed max_tokens
Args:
conversation_id: ID of the conversation
max_messages: Maximum number of messages to keep (default from settings)
max_tokens: Maximum total tokens to keep (default from settings)
Returns:
List of trimmed Message objects
"""
max_messages = max_messages or settings.MAX_CONVERSATION_MESSAGES
max_tokens = max_tokens or settings.MAX_CONVERSATION_TOKENS
# Get all messages
all_messages = self.get_conversation_messages(conversation_id)
# Step 1: Keep only the most recent N messages
recent_messages = all_messages[-max_messages:] if len(all_messages) > max_messages else all_messages
# Step 2: Trim by token count if needed
if max_tokens:
total_tokens = sum(msg.token_count or 0 for msg in recent_messages)
# Remove oldest messages until under token limit
while total_tokens > max_tokens and len(recent_messages) > 1:
removed_message = recent_messages.pop(0)
total_tokens -= (removed_message.token_count or 0)
return recent_messages
def delete_conversation(self, conversation_id: int, user_id: int) -> bool:
"""Delete a conversation and all its messages.
Args:
conversation_id: ID of the conversation
user_id: ID of the user (for authorization)
Returns:
True if deleted, False if not found or unauthorized
"""
conversation = self.get_conversation(conversation_id, user_id)
if not conversation:
return False
self.db.delete(conversation)
self.db.commit()
return True
def format_messages_for_agent(
self,
messages: List[Message],
max_messages: int = 20,
max_tokens: int = 8000
) -> List[dict]:
"""Format messages for agent consumption with trimming.
Converts Message objects to the format expected by the agent:
[{"role": "user", "content": "..."}, {"role": "assistant", "content": "..."}]
Applies conversation history trimming to stay within free-tier constraints:
1. Keep only the most recent N messages (max_messages)
2. Within those, ensure total tokens don't exceed max_tokens
Args:
messages: List of Message objects from database
max_messages: Maximum number of messages to keep (default: 20)
max_tokens: Maximum total tokens to keep (default: 8000)
Returns:
List of formatted message dicts for agent
"""
# Step 1: Keep only the most recent N messages
recent_messages = messages[-max_messages:] if len(messages) > max_messages else messages
# Step 2: Trim by token count if needed
if max_tokens:
total_tokens = sum(msg.token_count or 0 for msg in recent_messages)
# Remove oldest messages until under token limit
while total_tokens > max_tokens and len(recent_messages) > 1:
removed_message = recent_messages.pop(0)
total_tokens -= (removed_message.token_count or 0)
# Step 3: Convert to agent format
formatted_messages = [
{"role": msg.role, "content": msg.content}
for msg in recent_messages
]
return formatted_messages
|