Create backend/models.py
Browse files- backend/models.py +66 -0
backend/models.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import List, Dict
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
class Message:
|
| 6 |
+
def __init__(self, role: str, content: str, language: str = 'en'):
|
| 7 |
+
self.role = role
|
| 8 |
+
self.content = content
|
| 9 |
+
self.language = language
|
| 10 |
+
self.timestamp = datetime.now().isoformat()
|
| 11 |
+
|
| 12 |
+
def to_dict(self):
|
| 13 |
+
return {
|
| 14 |
+
'role': self.role,
|
| 15 |
+
'content': self.content,
|
| 16 |
+
'language': self.language,
|
| 17 |
+
'timestamp': self.timestamp
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
class ConversationManager:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
self.conversations: Dict[str, List[Message]] = {}
|
| 23 |
+
|
| 24 |
+
def add_message(self, conversation_id: str, role: str, content: str, language: str = 'en'):
|
| 25 |
+
"""إضافة رسالة للمحادثة"""
|
| 26 |
+
if conversation_id not in self.conversations:
|
| 27 |
+
self.conversations[conversation_id] = []
|
| 28 |
+
|
| 29 |
+
message = Message(role, content, language)
|
| 30 |
+
self.conversations[conversation_id].append(message)
|
| 31 |
+
|
| 32 |
+
def get_messages(self, conversation_id: str) -> List[Dict]:
|
| 33 |
+
"""الحصول على رسائل المحادثة"""
|
| 34 |
+
if conversation_id not in self.conversations:
|
| 35 |
+
return []
|
| 36 |
+
|
| 37 |
+
return [msg.to_dict() for msg in self.conversations[conversation_id]]
|
| 38 |
+
|
| 39 |
+
def get_context(self, conversation_id: str, limit: int = 10) -> List[Dict]:
|
| 40 |
+
"""الحصول على السياق (آخر n رسالة)"""
|
| 41 |
+
messages = self.get_messages(conversation_id)
|
| 42 |
+
return messages[-limit:] if len(messages) > limit else messages
|
| 43 |
+
|
| 44 |
+
def get_all_conversations(self) -> List[Dict]:
|
| 45 |
+
"""الحصول على قائمة المحادثات"""
|
| 46 |
+
conversations = []
|
| 47 |
+
for conv_id, messages in self.conversations.items():
|
| 48 |
+
if messages:
|
| 49 |
+
conversations.append({
|
| 50 |
+
'id': conv_id,
|
| 51 |
+
'title': messages[0].content[:50] + '...' if len(messages[0].content) > 50 else messages[0].content,
|
| 52 |
+
'last_message': messages[-1].timestamp,
|
| 53 |
+
'message_count': len(messages)
|
| 54 |
+
})
|
| 55 |
+
return sorted(conversations, key=lambda x: x['last_message'], reverse=True)
|
| 56 |
+
|
| 57 |
+
def delete_conversation(self, conversation_id: str):
|
| 58 |
+
"""حذف محادثة"""
|
| 59 |
+
if conversation_id in self.conversations:
|
| 60 |
+
del self.conversations[conversation_id]
|
| 61 |
+
|
| 62 |
+
def get_last_timestamp(self, conversation_id: str) -> str:
|
| 63 |
+
"""الحصول على آخر وقت رسالة"""
|
| 64 |
+
if conversation_id in self.conversations and self.conversations[conversation_id]:
|
| 65 |
+
return self.conversations[conversation_id][-1].timestamp
|
| 66 |
+
return datetime.now().isoformat()
|