File size: 7,457 Bytes
1fff71f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { writable, derived } from 'svelte/store';
import type { Session, SessionMetadata } from '$lib/types/api';
import type { SessionListItem } from '$lib/types/client';
import { apiClient } from '$lib/services/api';
import { STORAGE_KEY_ACTIVE_SESSION } from '$lib/utils/constants';
import { getItem, setItem, removeItem } from '$lib/services/storage';

/**
 * Session store managing user's chat sessions
 * Handles session list, active session, and CRUD operations
 */

interface SessionState {
	sessions: SessionListItem[]; // List of user sessions (metadata only)
	activeSession: Session | null; // Currently loaded session with full messages
	loading: boolean;
	error: string | null;
}

function createInitialState(): SessionState {
	return {
		sessions: [],
		activeSession: null,
		loading: false,
		error: null
	};
}

// Create the session store
function createSessionStore() {
	const { subscribe, set, update } = writable<SessionState>(createInitialState());

	return {
		subscribe,

		/**
		 * Load all sessions for current user
		 */
		async loadSessions() {
			update((state) => ({ ...state, loading: true, error: null }));

			try {
				const sessions = await apiClient.listSessions();

				// Convert to SessionListItem format (convert Unix timestamps to ISO strings)
				const sessionList: SessionListItem[] = sessions.map((s) => ({
					id: s.id,
					title: s.title,
					last_interaction: new Date(s.last_interaction * 1000).toISOString(),
					message_count: s.message_count,
					is_reference: s.is_reference,
					is_active: false
				}));

				update((state) => ({
					...state,
					sessions: sessionList,
					loading: false
				}));
			} catch (err) {
				const errorMessage = err instanceof Error ? err.message : 'Failed to load sessions';
				update((state) => ({
					...state,
					loading: false,
					error: errorMessage
				}));
			}
		},

		/**
		 * Create a new session
		 */
		async createSession(title: string): Promise<string | null> {
			update((state) => ({ ...state, loading: true, error: null }));

			try {
				const response = await apiClient.createSession(title);
				const newSessionId = response.session_id;

				// Add to session list
				const newSession: SessionListItem = {
					id: newSessionId,
					title,
					last_interaction: new Date().toISOString(),
					message_count: 0,
					is_reference: false,
					is_active: false
				};

				update((state) => ({
					...state,
					sessions: [newSession, ...state.sessions],
					loading: false
				}));

				return newSessionId;
			} catch (err) {
				const errorMessage = err instanceof Error ? err.message : 'Failed to create session';
				update((state) => ({
					...state,
					loading: false,
					error: errorMessage
				}));
				return null;
			}
		},

		/**
		 * Load a specific session with messages
		 */
		async loadSession(sessionId: string) {
			update((state) => ({ ...state, loading: true, error: null }));

			try {
				const session = await apiClient.getSession(sessionId);

				// Mark as active in session list
				update((state) => ({
					...state,
					sessions: state.sessions.map((s) => ({
						...s,
						is_active: s.id === sessionId
					})),
					activeSession: session,
					loading: false
				}));

				// Persist active session ID
				setItem(STORAGE_KEY_ACTIVE_SESSION, sessionId);
			} catch (err) {
				const errorMessage = err instanceof Error ? err.message : 'Failed to load session';
				update((state) => ({
					...state,
					loading: false,
					error: errorMessage
				}));
			}
		},

		/**
		 * Delete a session
		 */
		async deleteSession(sessionId: string): Promise<boolean> {
			update((state) => ({ ...state, loading: true, error: null }));

			try {
				await apiClient.deleteSession(sessionId);

				update((state) => {
					const newState = {
						...state,
						sessions: state.sessions.filter((s) => s.id !== sessionId),
						loading: false
					};

					// Clear active session if it was deleted
					if (state.activeSession?.id === sessionId) {
						newState.activeSession = null;
						removeItem(STORAGE_KEY_ACTIVE_SESSION);
					}

					return newState;
				});

				return true;
			} catch (err) {
				const errorMessage = err instanceof Error ? err.message : 'Failed to delete session';
				update((state) => ({
					...state,
					loading: false,
					error: errorMessage
				}));
				return false;
			}
		},

		/**
		 * Update session reference flag
		 */
		async updateSessionReference(sessionId: string, isReference: boolean): Promise<boolean> {
			update((state) => ({ ...state, loading: true, error: null }));

			try {
				await apiClient.updateSession(sessionId, isReference);

				update((state) => ({
					...state,
					sessions: state.sessions.map((s) =>
						s.id === sessionId ? { ...s, is_reference: isReference } : s
					),
					activeSession:
						state.activeSession?.id === sessionId
							? { ...state.activeSession, is_reference: isReference }
							: state.activeSession,
					loading: false
				}));

				return true;
			} catch (err) {
				const errorMessage = err instanceof Error ? err.message : 'Failed to update session';
				update((state) => ({
					...state,
					loading: false,
					error: errorMessage
				}));
				return false;
			}
		},

		/**
		 * Clear active session
		 */
		clearActive() {
			update((state) => ({
				...state,
				activeSession: null
			}));
			removeItem(STORAGE_KEY_ACTIVE_SESSION);
		},

		/**
		 * Send a message to the active session
		 */
		async sendMessage(
			sessionId: string,
			content: string,
			mode: string
		): Promise<boolean> {
			update((state) => ({ ...state, loading: true, error: null }));

			try {
				const response = await apiClient.sendMessage(sessionId, {
					mode: mode as any,
					message: content,
					save_to_memory: mode === 'memorize'
				});

				// Update active session with new message
				update((state) => {
					if (state.activeSession?.id === sessionId) {
						return {
							...state,
							activeSession: {
								...state.activeSession,
								messages: [
									...state.activeSession.messages,
									{
										mode: mode as any,
										content,
										created_at: new Date().toISOString()
									}
								],
								last_interaction: new Date().toISOString()
							},
							sessions: state.sessions.map((s) =>
								s.id === sessionId
									? {
											...s,
											message_count: s.message_count + 1,
											last_interaction: new Date().toISOString()
										}
									: s
							),
							loading: false
						};
					}
					return { ...state, loading: false };
				});

				return true;
			} catch (err) {
				const errorMessage = err instanceof Error ? err.message : 'Failed to send message';
				update((state) => ({
					...state,
					loading: false,
					error: errorMessage
				}));
				return false;
			}
		},

		/**
		 * Clear error
		 */
		clearError() {
			update((state) => ({ ...state, error: null }));
		}
	};
}

export const sessionStore = createSessionStore();

// Derived stores
export const activeSessions = derived(sessionStore, ($session) => $session.sessions);

export const activeSession = derived(sessionStore, ($session) => $session.activeSession);

export const sessionCount = derived(sessionStore, ($session) => $session.sessions.length);

export const isLoading = derived(sessionStore, ($session) => $session.loading);

export const sessionError = derived(sessionStore, ($session) => $session.error);