File size: 12,121 Bytes
99d2ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a818a2d
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
332
333
334
335
# chat_manager.py - Chat Session Management System
import json
import os
import uuid
from dataclasses import dataclass, asdict
from typing import List, Optional, Dict, Any
from datetime import datetime
from pathlib import Path

@dataclass
class ChatMessage:
    """Individual chat message structure"""
    message_id: str
    role: str  # 'user' or 'assistant'
    content: str
    timestamp: str
    rating: Optional[int] = None  # 1 for thumbs up, -1 for thumbs down, None for no rating
    is_bookmarked: bool = False
    source_documents: List[str] = None
    
    def __post_init__(self):
        if self.source_documents is None:
            self.source_documents = []

@dataclass
class ChatSession:
    """Chat session data structure"""
    session_id: str
    user_id: str
    title: str
    created_at: str
    updated_at: str
    messages: List[ChatMessage] = None
    is_archived: bool = False
    tags: List[str] = None
    
    def __post_init__(self):
        if self.messages is None:
            self.messages = []
        if self.tags is None:
            self.tags = []

class ChatManager:
    """Manages chat sessions and messages"""
    
    def __init__(self, data_dir: str):
        self.data_dir = Path(data_dir)
        self.data_dir.mkdir(exist_ok=True)
        self.sessions_file = self.data_dir / "sessions.json"
        self.ensure_sessions_file()
    
    def ensure_sessions_file(self):
        """Ensure sessions file exists"""
        if not self.sessions_file.exists():
            with open(self.sessions_file, 'w') as f:
                json.dump({}, f)
    
    def create_session(self, user_id: str, title: str = None) -> str:
        """Create a new chat session"""
        session_id = str(uuid.uuid4())
        timestamp = datetime.now().isoformat()
        
        if not title:
            title = f"Chat {datetime.now().strftime('%Y-%m-%d %H:%M')}"
        
        session = ChatSession(
            session_id=session_id,
            user_id=user_id,
            title=title,
            created_at=timestamp,
            updated_at=timestamp
        )
        
        try:
            sessions = self.load_all_sessions()
            sessions[session_id] = asdict(session)
            
            with open(self.sessions_file, 'w') as f:
                json.dump(sessions, f, indent=2)
            
            return session_id
            
        except Exception as e:
            raise Exception(f"Failed to create session: {str(e)}")
    
    def load_all_sessions(self) -> Dict[str, Dict]:
        """Load all sessions from storage"""
        try:
            with open(self.sessions_file, 'r') as f:
                return json.load(f)
        except (FileNotFoundError, json.JSONDecodeError):
            return {}
    
    def get_session(self, session_id: str) -> Optional[ChatSession]:
        """Get chat session by ID"""
        sessions = self.load_all_sessions()
        session_data = sessions.get(session_id)
        
        if session_data:
            # Convert message dictionaries back to ChatMessage objects
            messages = []
            for msg_data in session_data.get('messages', []):
                messages.append(ChatMessage(**msg_data))
            session_data['messages'] = messages
            return ChatSession(**session_data)
        return None
    
    def get_user_sessions(self, user_id: str, include_archived: bool = False) -> List[ChatSession]:
        """Get all sessions for a user"""
        sessions = self.load_all_sessions()
        user_sessions = []
        
        for session_data in sessions.values():
            if session_data.get('user_id') == user_id:
                if include_archived or not session_data.get('is_archived', False):
                    # Convert message dictionaries back to ChatMessage objects
                    messages = []
                    for msg_data in session_data.get('messages', []):
                        messages.append(ChatMessage(**msg_data))
                    session_data['messages'] = messages
                    user_sessions.append(ChatSession(**session_data))
        
        # Sort by updated_at descending
        user_sessions.sort(key=lambda x: x.updated_at, reverse=True)
        return user_sessions
    
    def add_message(self, session_id: str, role: str, content: str, source_documents: List[str] = None) -> str:
        """Add a message to a chat session"""
        message_id = str(uuid.uuid4())
        timestamp = datetime.now().isoformat()
        
        message = ChatMessage(
            message_id=message_id,
            role=role,
            content=content,
            timestamp=timestamp,
            source_documents=source_documents or []
        )
        
        try:
            sessions = self.load_all_sessions()
            
            if session_id not in sessions:
                raise ValueError(f"Session {session_id} not found")
            
            # Convert message to dict for storage
            message_dict = asdict(message)
            sessions[session_id]['messages'].append(message_dict)
            sessions[session_id]['updated_at'] = timestamp
            
            with open(self.sessions_file, 'w') as f:
                json.dump(sessions, f, indent=2)
            
            return message_id
            
        except Exception as e:
            raise Exception(f"Failed to add message: {str(e)}")
    
    def rate_message(self, session_id: str, message_id: str, rating: int) -> bool:
        """Rate a message (1 for thumbs up, -1 for thumbs down)"""
        try:
            sessions = self.load_all_sessions()
            
            if session_id not in sessions:
                return False
            
            for message in sessions[session_id]['messages']:
                if message['message_id'] == message_id:
                    message['rating'] = rating
                    sessions[session_id]['updated_at'] = datetime.now().isoformat()
                    
                    with open(self.sessions_file, 'w') as f:
                        json.dump(sessions, f, indent=2)
                    
                    return True
            
            return False
            
        except Exception:
            return False
    
    def bookmark_message(self, session_id: str, message_id: str, is_bookmarked: bool = True) -> bool:
        """Bookmark or unbookmark a message"""
        try:
            sessions = self.load_all_sessions()
            
            if session_id not in sessions:
                return False
            
            for message in sessions[session_id]['messages']:
                if message['message_id'] == message_id:
                    message['is_bookmarked'] = is_bookmarked
                    sessions[session_id]['updated_at'] = datetime.now().isoformat()
                    
                    with open(self.sessions_file, 'w') as f:
                        json.dump(sessions, f, indent=2)
                    
                    return True
            
            return False
            
        except Exception:
            return False
    
    def get_bookmarked_messages(self, user_id: str) -> List[Dict[str, Any]]:
        """Get all bookmarked messages for a user"""
        sessions = self.load_all_sessions()
        bookmarked = []
        
        for session_data in sessions.values():
            if session_data.get('user_id') == user_id:
                for message in session_data.get('messages', []):
                    if message.get('is_bookmarked', False):
                        bookmarked.append({
                            'session_id': session_data['session_id'],
                            'session_title': session_data['title'],
                            'message': message,
                            'timestamp': message['timestamp']
                        })
        
        # Sort by timestamp descending
        bookmarked.sort(key=lambda x: x['timestamp'], reverse=True)
        return bookmarked
    
    def update_session_title(self, session_id: str, title: str) -> bool:
        """Update session title"""
        try:
            sessions = self.load_all_sessions()
            
            if session_id not in sessions:
                return False
            
            sessions[session_id]['title'] = title
            sessions[session_id]['updated_at'] = datetime.now().isoformat()
            
            with open(self.sessions_file, 'w') as f:
                json.dump(sessions, f, indent=2)
            
            return True
            
        except Exception:
            return False
    
    def archive_session(self, session_id: str, is_archived: bool = True) -> bool:
        """Archive or unarchive a session"""
        try:
            sessions = self.load_all_sessions()
            
            if session_id not in sessions:
                return False
            
            sessions[session_id]['is_archived'] = is_archived
            sessions[session_id]['updated_at'] = datetime.now().isoformat()
            
            with open(self.sessions_file, 'w') as f:
                json.dump(sessions, f, indent=2)
            
            return True
            
        except Exception:
            return False
    
    def delete_session(self, session_id: str) -> bool:
        """Delete a chat session"""
        try:
            sessions = self.load_all_sessions()
            
            if session_id in sessions:
                del sessions[session_id]
                
                with open(self.sessions_file, 'w') as f:
                    json.dump(sessions, f, indent=2)
                
                return True
            return False
            
        except Exception:
            return False
    
    def export_chat_history(self, user_id: str, session_id: str = None) -> Dict[str, Any]:
        """Export chat history for a user or specific session"""
        if session_id:
            session = self.get_session(session_id)
            if session and session.user_id == user_id:
                return {
                    'export_type': 'single_session',
                    'session': asdict(session),
                    'exported_at': datetime.now().isoformat()
                }
        else:
            sessions = self.get_user_sessions(user_id, include_archived=True)
            return {
                'export_type': 'all_sessions',
                'sessions': [asdict(session) for session in sessions],
                'exported_at': datetime.now().isoformat(),
                'total_sessions': len(sessions)
            }
        
        return {}
    
    def get_chat_statistics(self, user_id: str) -> Dict[str, Any]:
        """Get chat statistics for a user"""
        sessions = self.get_user_sessions(user_id, include_archived=True)
        
        total_messages = 0
        total_user_messages = 0
        total_assistant_messages = 0
        bookmarked_count = 0
        rated_messages = {'positive': 0, 'negative': 0}
        
        for session in sessions:
            total_messages += len(session.messages)
            for message in session.messages:
                if message.role == 'user':
                    total_user_messages += 1
                else:
                    total_assistant_messages += 1
                
                if message.is_bookmarked:
                    bookmarked_count += 1
                
                if message.rating == 1:
                    rated_messages['positive'] += 1
                elif message.rating == -1:
                    rated_messages['negative'] += 1
        
        return {
            'total_sessions': len(sessions),
            'total_messages': total_messages,
            'user_messages': total_user_messages,
            'assistant_messages': total_assistant_messages,
            'bookmarked_messages': bookmarked_count,
            'message_ratings': rated_messages,
            'average_messages_per_session': total_messages / len(sessions) if sessions else 0
        }