File size: 9,818 Bytes
310260a | 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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | """
ConversationService for managing chat conversations and messages.
This service handles CRUD operations for conversations, message creation
with sequence numbering, and conversation history retrieval.
"""
from sqlmodel import Session, select, func
from typing import List, Optional, Dict, Any
from datetime import datetime
from ..models.conversation import Conversation
from ..models.message import Message, MessageRole
class ConversationService:
"""
Service for managing conversations and messages.
This service provides:
- Conversation CRUD operations
- Message creation with automatic sequence numbering
- Conversation history retrieval
- User-scoped data access
"""
def __init__(self, session: Session):
"""
Initialize the ConversationService.
Args:
session: SQLModel database session
"""
self.session = session
def create_conversation(self, user_id: int) -> Conversation:
"""
Create a new conversation for a user.
Args:
user_id: ID of the user creating the conversation
Returns:
Conversation: The newly created conversation
Raises:
Exception: If database operation fails
"""
conversation = Conversation(
user_id=user_id,
created_at=datetime.utcnow(),
updated_at=datetime.utcnow()
)
self.session.add(conversation)
self.session.commit()
self.session.refresh(conversation)
return conversation
def get_conversation(
self,
conversation_id: int,
user_id: int
) -> Optional[Conversation]:
"""
Retrieve a specific conversation by ID.
This method enforces user_id scoping - users can only access
their own conversations.
Args:
conversation_id: ID of the conversation to retrieve
user_id: ID of the authenticated user
Returns:
Conversation if found and belongs to user, None otherwise
"""
statement = select(Conversation).where(
Conversation.id == conversation_id,
Conversation.user_id == user_id
)
return self.session.exec(statement).first()
def list_conversations(
self,
user_id: int,
limit: int = 50,
offset: int = 0
) -> List[Conversation]:
"""
List all conversations for a user.
Conversations are returned in reverse chronological order
(most recent first).
Args:
user_id: ID of the authenticated user
limit: Maximum number of conversations to return (default: 50)
offset: Number of conversations to skip (default: 0)
Returns:
List of conversations belonging to the user
"""
statement = (
select(Conversation)
.where(Conversation.user_id == user_id)
.order_by(Conversation.updated_at.desc())
.limit(limit)
.offset(offset)
)
return list(self.session.exec(statement).all())
def delete_conversation(
self,
conversation_id: int,
user_id: int
) -> bool:
"""
Delete a conversation and all its messages.
This method enforces user_id scoping - users can only delete
their own conversations.
Args:
conversation_id: ID of the conversation to delete
user_id: ID of the authenticated user
Returns:
True if conversation was deleted, False if not found
"""
conversation = self.get_conversation(conversation_id, user_id)
if not conversation:
return False
self.session.delete(conversation)
self.session.commit()
return True
def create_message(
self,
conversation_id: int,
user_id: int,
role: MessageRole,
content: str,
tool_calls: Optional[Dict[str, Any]] = None
) -> Optional[Message]:
"""
Create a new message in a conversation with automatic sequence numbering.
This method:
1. Verifies the conversation exists and belongs to the user
2. Calculates the next sequence number
3. Creates the message with proper sequencing
4. Updates the conversation's updated_at timestamp
Args:
conversation_id: ID of the conversation
user_id: ID of the authenticated user
role: Message role (USER or ASSISTANT)
content: Message content
tool_calls: Optional tool call metadata
Returns:
Message if created successfully, None if conversation not found
Raises:
Exception: If database operation fails
"""
# Verify conversation exists and belongs to user
conversation = self.get_conversation(conversation_id, user_id)
if not conversation:
return None
# Get next sequence number
sequence_number = self._get_next_sequence_number(conversation_id)
# Create message
message = Message(
conversation_id=conversation_id,
role=role,
content=content,
tool_calls=tool_calls,
sequence_number=sequence_number,
created_at=datetime.utcnow()
)
self.session.add(message)
# Update conversation timestamp
conversation.updated_at = datetime.utcnow()
self.session.add(conversation)
self.session.commit()
self.session.refresh(message)
return message
def get_conversation_history(
self,
conversation_id: int,
user_id: int,
include_deleted: bool = False
) -> List[Message]:
"""
Retrieve all messages in a conversation in chronological order.
This method enforces user_id scoping - users can only access
messages from their own conversations.
Args:
conversation_id: ID of the conversation
user_id: ID of the authenticated user
include_deleted: Whether to include soft-deleted messages (default: False)
Returns:
List of messages ordered by sequence_number (oldest first)
Empty list if conversation not found or doesn't belong to user
"""
# Verify conversation exists and belongs to user
conversation = self.get_conversation(conversation_id, user_id)
if not conversation:
return []
# Build query
statement = (
select(Message)
.where(Message.conversation_id == conversation_id)
.order_by(Message.sequence_number.asc())
)
# Filter out deleted messages unless requested
if not include_deleted:
statement = statement.where(Message.deleted_at.is_(None))
return list(self.session.exec(statement).all())
def get_message_count(
self,
conversation_id: int,
user_id: int
) -> int:
"""
Get the total number of messages in a conversation.
Args:
conversation_id: ID of the conversation
user_id: ID of the authenticated user
Returns:
Number of messages (excluding deleted), 0 if conversation not found
"""
# Verify conversation exists and belongs to user
conversation = self.get_conversation(conversation_id, user_id)
if not conversation:
return 0
statement = (
select(func.count(Message.id))
.where(
Message.conversation_id == conversation_id,
Message.deleted_at.is_(None)
)
)
return self.session.exec(statement).one()
def _get_next_sequence_number(self, conversation_id: int) -> int:
"""
Calculate the next sequence number for a message in a conversation.
This method finds the highest existing sequence number and adds 1.
If no messages exist, returns 1.
Args:
conversation_id: ID of the conversation
Returns:
Next sequence number to use
"""
statement = (
select(func.max(Message.sequence_number))
.where(Message.conversation_id == conversation_id)
)
max_sequence = self.session.exec(statement).one()
# If no messages exist, start at 1
if max_sequence is None:
return 1
return max_sequence + 1
def soft_delete_message(
self,
message_id: int,
conversation_id: int,
user_id: int
) -> bool:
"""
Soft delete a message (set deleted_at timestamp).
This method enforces user_id scoping - users can only delete
messages from their own conversations.
Args:
message_id: ID of the message to delete
conversation_id: ID of the conversation
user_id: ID of the authenticated user
Returns:
True if message was deleted, False if not found
"""
# Verify conversation belongs to user
conversation = self.get_conversation(conversation_id, user_id)
if not conversation:
return False
# Find message
statement = select(Message).where(
Message.id == message_id,
Message.conversation_id == conversation_id
)
message = self.session.exec(statement).first()
if not message:
return False
# Soft delete
message.deleted_at = datetime.utcnow()
self.session.add(message)
self.session.commit()
return True
|