File size: 24,547 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/**
 * conversationsStore - Conversation lifecycle, persistence and navigation
 *
 * Owns conversation CRUD, message tree navigation, import/export and title
 * management, persisted through DatabaseService. Per-chat options (MCP
 * overrides, reasoning effort, cwd) live in ConversationPreferences,
 * composed as {@link ConversationsStore.preferences}.
 */

import { browser } from '$app/environment';
import { goto } from '$app/navigation';
import { ROUTES } from '$lib/constants';
import { MessageRole } from '$lib/enums';
import { ConversationTransferService } from '$lib/services/conversation-transfer.service';
import { DatabaseService } from '$lib/services/database.service';
import { MigrationService } from '$lib/services/migration.service';
import { RouterService } from '$lib/services/router.service';
// direct imports between stores, not via the barrel, to avoid circular deps
import {
	ConversationPreferences,
	type ConversationsPreferencesHost
} from '$lib/stores/conversations/preferences.svelte';
import { settingsStore } from '$lib/stores/settings/index.svelte';
import { tabsStore } from '$lib/stores/tabs.svelte';
import { filterByLeafNodeId, findLeafNode, generateConversationTitle } from '$lib/utils';
import { SvelteSet } from 'svelte/reactivity';
import { toast } from 'svelte-sonner';

class ConversationsStore implements ConversationsPreferencesHost {
	/** Currently active conversation */
	activeConversation = $state<DatabaseConversation | null>(null);

	/** Messages in the active conversation (filtered by currNode path) */
	activeMessages = $state<DatabaseMessage[]>([]);

	/** List of all conversations */
	conversations = $state<DatabaseConversation[]>([]);

	/** Whether the store has been initialized */
	isInitialized = $state(false);

	/** Per-chat options (MCP overrides, reasoning effort, cwd), composed here. */
	private _preferences = new ConversationPreferences(this);

	/**
	 * Listeners notified with the ids of conversations that were deleted.
	 * Lets dependent stores (e.g. agenticStore) drop per-conversation state
	 * without introducing a circular import back into this store.
	 */
	private conversationDeletionListeners = new Set<(convIds: string[]) => void>();

	/** In-flight init run; shared by concurrent callers, reset on failure to allow retry */
	private initPromise: Promise<void> | null = null;

	/**
	 * Messages loadConversation just read, handed off once so the chat
	 * screen can reuse them for sibling info instead of re-fetching the
	 * whole conversation a second time.
	 */
	private lastLoadedMessages: { convId: string; messages: DatabaseMessage[] } | null = null;

	/**
	 * Memo of the last findMessageIndex() lookup. Streaming calls it once per
	 * chunk for the same message, so a validated cache hit keeps that O(1)
	 * instead of a linear scan of activeMessages on every token.
	 */
	private lastMessageIndex: { id: string; index: number } | null = null;

	get preferences() {
		return this._preferences;
	}

	/**
	 * Adds a message to the active messages array
	 */
	addMessageToActive(message: DatabaseMessage): void {
		this.activeMessages.push(message);
	}

	/**
	 * Applies a field update to a conversation row, mirroring it into both the
	 * conversations list and the active conversation when it is the target.
	 * Shared by the rename/pin/preferences flows so no caller can forget to
	 * mirror one side.
	 */
	applyConversationUpdate(id: string, updates: Partial<DatabaseConversation>): void {
		const convIndex = this.conversations.findIndex((c) => c.id === id);

		if (convIndex !== -1) {
			const target = this.conversations[convIndex] as unknown as Record<string, unknown>;

			for (const [key, value] of Object.entries(updates)) {
				if (target[key] !== value) target[key] = value;
			}
		}

		if (this.activeConversation?.id === id) {
			// field-wise, not object replacement: effects that track the active
			// conversation identity would otherwise refire on every rename or pin
			const target = this.activeConversation as unknown as Record<string, unknown>;

			for (const [key, value] of Object.entries(updates)) {
				if (target[key] !== value) target[key] = value;
			}
		}
	}

	/**
	 * Derives a conversation title from its first message content and applies
	 * it, honoring the title-generation setting. Shared by every flow that
	 * edits or creates the first user message.
	 */
	async applyTitleFromContent(convId: string, content: string): Promise<void> {
		await this.updateConversationName(
			convId,
			generateConversationTitle(content, Boolean(settingsStore.config.titleGenerationUseFirstLine))
		);
	}

	/**
	 * Deletes multiple conversations in sequence.
	 * Mirrors deleteConversation() per-id; navigates to the new-chat screen only
	 * if the currently-open chat was among the deleted ones.
	 * @param convIds - Conversation IDs to delete
	 */
	async bulkDeleteConversations(convIds: string[]): Promise<void> {
		if (convIds.length === 0) return;

		try {
			const idsToRemove = new SvelteSet(convIds);
			// Collect all descendants recursively so the local cache stays consistent
			// even when deleteWithForks is omitted.
			const queue = [...convIds];

			while (queue.length > 0) {
				const parentId = queue.pop()!;

				for (const c of this.conversations) {
					if (c.forkedFromConversationId === parentId && !idsToRemove.has(c.id)) {
						idsToRemove.add(c.id);
						queue.push(c.id);
					}
				}
			}

			const activeWasDeleted =
				this.activeConversation !== null && idsToRemove.has(this.activeConversation.id);

			await DatabaseService.bulkDeleteConversations([...idsToRemove]);

			this.conversations = this.conversations.filter((c) => !idsToRemove.has(c.id));
			this.notifyConversationsDeleted([...idsToRemove]);

			if (activeWasDeleted) {
				const activeId = this.activeConversation!.id;

				tabsStore.removeTabs([...idsToRemove].filter((id) => id !== activeId));
				this.clearActiveConversation();
				await tabsStore.close(activeId, activeId);
			} else {
				tabsStore.removeTabs([...idsToRemove]);
			}

			toast.success(
				idsToRemove.size === 1
					? 'Conversation deleted'
					: `${idsToRemove.size} conversations deleted`
			);
		} catch (error) {
			console.error('Failed to bulk delete conversations:', error);
			toast.error('Failed to delete conversations');
		}
	}

	/**
	 * Bundles the given conversations into a single zip archive and triggers a
	 * browser download (one JSONL file per conversation).
	 * @param convIds - Conversation IDs to export
	 */
	async bulkExportConversations(convIds: string[]): Promise<void> {
		if (convIds.length === 0) return;

		try {
			const exported = await this.getConversationsForExport(convIds);

			if (exported.length === 0) {
				toast.error('No conversations to export');

				return;
			}

			ConversationTransferService.downloadConversationsArchive(exported);

			toast.success(
				exported.length === 1
					? 'Conversation exported'
					: `${exported.length} conversations exported`
			);
		} catch (error) {
			console.error('Failed to bulk export conversations:', error);
			toast.error('Failed to export conversations');
		}
	}

	/**
	 * Toggles the pinned state of each conversation individually.
	 * Mixed-pin selections are intentionally not normalised here; the bulk
	 * action UI surfaces them as a disabled mixed-state instead.
	 * @param convIds - Conversation IDs to toggle
	 */
	async bulkToggleConversationPin(convIds: string[]): Promise<void> {
		if (convIds.length === 0) return;

		try {
			const updates = await DatabaseService.bulkToggleConversationPins(convIds);
			const activeId = this.activeConversation?.id;

			if (this.activeConversation && activeId && updates.has(activeId)) {
				this.activeConversation.pinned = updates.get(activeId)!;
			}

			for (let i = 0; i < this.conversations.length; i++) {
				const newPinned = updates.get(this.conversations[i].id);

				if (newPinned !== undefined) this.conversations[i].pinned = newPinned;
			}

			toast.success(
				convIds.length === 1
					? 'Conversation pin toggled'
					: `Updated pin state for ${convIds.length} conversations`
			);
		} catch (error) {
			console.error('Failed to bulk toggle pin:', error);
			toast.error('Failed to update pin state');
		}
	}

	/**
	 * Clears the active conversation and messages.
	 */
	clearActiveConversation(): void {
		this.activeConversation = null;
		this.activeMessages = [];
		// reload defaults so new chats inherit persisted state
		this.preferences.resetPending();
	}

	/** One-shot handoff of the messages the last loadConversation read. */
	consumeLastLoadedMessages(convId: string): DatabaseMessage[] | null {
		if (this.lastLoadedMessages?.convId !== convId) return null;

		const messages = this.lastLoadedMessages.messages;

		this.lastLoadedMessages = null;

		return messages;
	}

	/**
	 * Creates a new conversation and navigates to it
	 * @param name - Optional name for the conversation
	 * @returns The ID of the created conversation
	 */
	async createConversation(name?: string): Promise<string> {
		const conversationName = name || `Chat ${new Date().toLocaleString()}`;
		// The tool policy is seeded from the current defaults: edits made inside
		// the conversation afterwards live on its row and do not flow back into
		// the defaults. Working directory picked on the new-chat screen gets
		// threaded in here too, then cleared so it doesn't bleed onto subsequent
		// new chats.
		const conversation = await DatabaseService.createConversation(conversationName, {
			cwd: this.preferences.pendingCwd ?? undefined,
			reasoningEffort: this.preferences.pendingReasoningEffort,
			...this.preferences.getToolPolicySnapshot()
		});

		this.preferences.pendingCwd = null;

		this.conversations = [conversation, ...this.conversations];
		this.activeConversation = conversation;
		this.activeMessages = [];

		await goto(RouterService.chat(conversation.id));

		return conversation.id;
	}

	/**
	 * Deletes all conversations and their messages
	 */
	async deleteAll(): Promise<void> {
		try {
			const allConversations = await DatabaseService.getAllConversations();
			const allIds = allConversations.map((c) => c.id);

			await DatabaseService.bulkDeleteConversations(allIds);

			this.clearActiveConversation();
			this.conversations = [];
			tabsStore.clear();
			this.notifyConversationsDeleted(allIds);

			toast.success('All conversations deleted');

			await goto(ROUTES.START);
		} catch (error) {
			console.error('Failed to delete all conversations:', error);
			toast.error('Failed to delete conversations');
		}
	}

	/**
	 * Deletes a conversation and all its messages
	 * @param convId - The conversation ID to delete
	 */
	async deleteConversation(convId: string, options?: { deleteWithForks?: boolean }): Promise<void> {
		try {
			await DatabaseService.deleteConversation(convId, options);

			if (options?.deleteWithForks) {
				// Collect all descendants recursively
				const idsToRemove = new SvelteSet([convId]);
				const queue = [convId];

				while (queue.length > 0) {
					const parentId = queue.pop()!;

					for (const c of this.conversations) {
						if (c.forkedFromConversationId === parentId && !idsToRemove.has(c.id)) {
							idsToRemove.add(c.id);
							queue.push(c.id);
						}
					}
				}
				this.conversations = this.conversations.filter((c) => !idsToRemove.has(c.id));

				if (this.activeConversation && idsToRemove.has(this.activeConversation.id)) {
					const activeId = this.activeConversation.id;

					tabsStore.removeTabs([...idsToRemove].filter((id) => id !== activeId));
					this.clearActiveConversation();
					await tabsStore.close(activeId, activeId);
				} else {
					tabsStore.removeTabs([...idsToRemove]);
				}

				this.notifyConversationsDeleted([...idsToRemove]);
			} else {
				// Reparent direct children to deleted conv's parent (or promote to top-level)
				const deletedConv = this.conversations.find((c) => c.id === convId);
				const newParent = deletedConv?.forkedFromConversationId;

				this.conversations = this.conversations
					.filter((c) => c.id !== convId)
					.map((c) =>
						c.forkedFromConversationId === convId
							? { ...c, forkedFromConversationId: newParent }
							: c
					);

				if (this.activeConversation?.id === convId) {
					this.clearActiveConversation();
					await tabsStore.close(convId, convId);
				} else {
					tabsStore.removeTabs([convId]);
				}

				this.notifyConversationsDeleted([convId]);
			}
		} catch (error) {
			console.error('Failed to delete conversation:', error);
		}
	}

	/**
	 * Downloads a single conversation as a JSONL file, serializing the full message tree.
	 * @param convId - The conversation ID to download
	 */
	async downloadConversation(convId: string): Promise<void> {
		const [exportedConversation] = await this.getConversationsForExport([convId]);

		if (!exportedConversation) return;

		ConversationTransferService.downloadConversationFile(exportedConversation);
	}

	/**
	 * Finds the index of a message in active messages.
	 *
	 * The last lookup is memoized and reused when it still validates against
	 * the current array (same id at the same position), which covers the
	 * streaming hot path where the same message is looked up on every chunk
	 * while the array itself only mutates by field. Any structural change
	 * (splice, reassignment, reordering) fails validation and falls back to a
	 * full scan.
	 */
	findMessageIndex(messageId: string): number {
		const last = this.lastMessageIndex;
		const messages = this.activeMessages;

		if (
			last &&
			last.id === messageId &&
			last.index >= 0 &&
			last.index < messages.length &&
			messages[last.index]?.id === messageId
		) {
			return last.index;
		}

		const index = messages.findIndex((m) => m.id === messageId);

		this.lastMessageIndex = { id: messageId, index };

		return index;
	}

	/**
	 * Forks a conversation at a specific message, creating a new conversation
	 * containing messages from root up to the target message, then navigates to it.
	 *
	 * @param messageId - The message ID to fork at
	 * @param options - Fork options (name and whether to include attachments)
	 * @returns The new conversation ID, or null if fork failed
	 */
	async forkConversation(
		messageId: string,
		options: { name: string; includeAttachments: boolean }
	): Promise<string | null> {
		if (!this.activeConversation) return null;

		try {
			const newConv = await DatabaseService.forkConversation(
				this.activeConversation.id,
				messageId,
				options
			);

			this.conversations = [newConv, ...this.conversations];

			await goto(RouterService.chat(newConv.id));

			toast.success('Conversation forked');

			return newConv.id;
		} catch (error) {
			console.error('Failed to fork conversation:', error);
			toast.error('Failed to fork conversation');

			return null;
		}
	}

	/**
	 * Gets all messages for a specific conversation
	 * @param convId - The conversation ID
	 * @returns Array of messages
	 */
	async getConversationMessages(convId: string): Promise<DatabaseMessage[]> {
		return await DatabaseService.getConversationMessages(convId);
	}

	/**
	 * Gets conversations and their messages from the database for export.
	 * @param convIds - Conversation IDs
	 * @returns List of conversations with messages, ordered by the input IDs
	 */
	async getConversationsForExport(convIds: string[]): Promise<ExportedConversation[]> {
		const fetched = await DatabaseService.getConversationsWithMessages(convIds);

		return convIds
			.map((id) => fetched.get(id))
			.filter((entry): entry is ExportedConversation => entry !== undefined);
	}

	/**
	 * Imports conversations from provided data (without file picker)
	 * @param data - Array of conversation data with messages
	 * @returns The conversations written to the database and the ones skipped
	 */
	async importConversationsData(
		data: ExportedConversations
	): Promise<{ imported: DatabaseConversation[]; skipped: DatabaseConversation[] }> {
		const result = await DatabaseService.importConversations(data);

		await this.loadConversations();

		return result;
	}

	/**
	 * Initialize the store by loading conversations from database.
	 * Safe to call multiple times: concurrent callers share a single run,
	 * and a failed run can be retried by calling again.
	 */
	initialize(): Promise<void> {
		if (!browser) return Promise.resolve();

		if (this.initPromise) return this.initPromise;

		this.initPromise = (async () => {
			try {
				await MigrationService.runAllMigrations();
				await this.loadConversations();
				this.isInitialized = true;
			} catch (error) {
				console.error('Failed to initialize conversations:', error);
				this.initPromise = null;
			}
		})();

		return this.initPromise;
	}

	/**
	 * Loads a specific conversation and its messages
	 * @param convId - The conversation ID to load
	 * @returns True if conversation was loaded successfully
	 */
	async loadConversation(convId: string): Promise<boolean> {
		try {
			const conversation = await DatabaseService.getConversation(convId);

			if (!conversation) {
				return false;
			}

			// Drop any cwd the user drafted on the empty new-chat screen -
			// it doesn't belong to this conversation.
			this.preferences.pendingCwd = null;

			const allMessages = await DatabaseService.getConversationMessages(convId);

			// set conversation and messages in one sync block so effects never see
			// the new conversation with the previous conversation's messages
			this.lastLoadedMessages = { convId, messages: allMessages };
			this.activeConversation = conversation;
			this.activeMessages = conversation.currNode
				? (filterByLeafNodeId(allMessages, conversation.currNode, false) as DatabaseMessage[])
				: allMessages;

			return true;
		} catch (error) {
			console.error('Failed to load conversation:', error);

			return false;
		}
	}

	/**
	 * Loads all conversations from the database
	 */
	async loadConversations(): Promise<void> {
		const conversations = await DatabaseService.getAllConversations();

		this.conversations = conversations;
	}

	/**
	 * Navigates to a specific sibling branch by updating currNode and refreshing messages.
	 * @param siblingId - The sibling message ID to navigate to
	 */
	async navigateToSibling(siblingId: string): Promise<void> {
		if (!this.activeConversation) return;

		const allMessages = await DatabaseService.getConversationMessages(this.activeConversation.id);
		const rootMessage = allMessages.find((m) => m.type === 'root' && m.parent === null);
		const currentFirstUserMessage = this.activeMessages.find(
			(m) => m.role === MessageRole.USER && m.parent === rootMessage?.id
		);
		const currentLeafNodeId = findLeafNode(allMessages, siblingId);

		await DatabaseService.updateCurrentNode(this.activeConversation.id, currentLeafNodeId);
		this.activeConversation.currNode = currentLeafNodeId;
		await this.refreshActiveMessages();

		if (rootMessage && this.activeMessages.length > 0) {
			const newFirstUserMessage = this.activeMessages.find(
				(m) => m.role === MessageRole.USER && m.parent === rootMessage.id
			);

			if (
				newFirstUserMessage &&
				newFirstUserMessage.content.trim() &&
				(!currentFirstUserMessage ||
					newFirstUserMessage.id !== currentFirstUserMessage.id ||
					newFirstUserMessage.content.trim() !== currentFirstUserMessage.content.trim())
			) {
				await this.applyTitleFromContent(this.activeConversation.id, newFirstUserMessage.content);
			}
		}
	}

	/**
	 * Registers a listener invoked with the ids of deleted conversations.
	 * Returns an unsubscribe function.
	 */
	onConversationsDeleted(listener: (convIds: string[]) => void): () => void {
		this.conversationDeletionListeners.add(listener);

		return () => this.conversationDeletionListeners.delete(listener);
	}

	/**
	 * Start a fresh chat by navigating to the bare `#/` new-chat screen. The
	 * chat layout opens a new-chat tab for it when Conversation tabs are on.
	 */
	async openNewChat(): Promise<void> {
		this.clearActiveConversation();
		await goto(ROUTES.START);
	}

	/**
	 * Refreshes active messages based on currNode after branch navigation.
	 */
	async refreshActiveMessages(): Promise<void> {
		if (!this.activeConversation) return;

		const allMessages = await DatabaseService.getConversationMessages(this.activeConversation.id);

		if (allMessages.length === 0) {
			this.activeMessages = [];

			return;
		}

		const leafNodeId =
			this.activeConversation.currNode ||
			allMessages.reduce((latest, msg) => (msg.timestamp > latest.timestamp ? msg : latest)).id;
		const currentPath = filterByLeafNodeId(allMessages, leafNodeId, false) as DatabaseMessage[];

		this.activeMessages = currentPath;
	}

	/**
	 * Removes a message from active messages by index
	 */
	removeMessageAtIndex(index: number): DatabaseMessage | undefined {
		if (index !== -1) {
			return this.activeMessages.splice(index, 1)[0];
		}

		return undefined;
	}

	/**
	 * Removes messages from active messages starting at an index
	 */
	sliceActiveMessages(startIndex: number): void {
		this.activeMessages = this.activeMessages.slice(0, startIndex);
	}

	/**
	 * Toggles the pinned status of a conversation.
	 * @param convId - The conversation ID to toggle
	 * @returns The new pinned status
	 */
	async toggleConversationPin(convId: string): Promise<boolean> {
		try {
			const newPinnedState = await DatabaseService.toggleConversationPin(convId);

			this.applyConversationUpdate(convId, { pinned: newPinnedState });

			return newPinnedState;
		} catch (error) {
			console.error('Failed to toggle conversation pin:', error);

			return false;
		}
	}

	/**
	 * Updates the name of a conversation.
	 * @param convId - The conversation ID to update
	 * @param name - The new name for the conversation
	 */
	async updateConversationName(convId: string, name: string): Promise<void> {
		try {
			await DatabaseService.updateConversation(convId, { name });

			this.applyConversationUpdate(convId, { name });
		} catch (error) {
			console.error('Failed to update conversation name:', error);
		}
	}

	/**
	 * Marks a conversation as recently active: stamps lastModified (persisted)
	 * and moves it to the top of the list. Only message-activity flows call
	 * this; metadata updates (rename, pin, settings) do not.
	 *
	 * @param convId - Conversation that produced the activity, defaults to the active one
	 */
	updateConversationTimestamp(convId?: string): void {
		const targetId = convId ?? this.activeConversation?.id;

		if (!targetId) return;

		const now = Date.now();
		const chatIndex = this.conversations.findIndex((c) => c.id === targetId);

		if (chatIndex !== -1) {
			this.conversations[chatIndex].lastModified = now;
			const updatedConv = this.conversations.splice(chatIndex, 1)[0];

			this.conversations = [updatedConv, ...this.conversations];
		}

		if (this.activeConversation?.id === targetId) {
			this.activeConversation.lastModified = now;
		}

		DatabaseService.updateConversation(targetId, { lastModified: now }).catch((error) =>
			console.error('Failed to update conversation timestamp:', error)
		);
	}

	/**
	 * Updates the current node of the active conversation
	 * @param nodeId - The new current node ID
	 */
	async updateCurrentNode(nodeId: string): Promise<void> {
		if (!this.activeConversation) return;

		await DatabaseService.updateCurrentNode(this.activeConversation.id, nodeId);
		this.activeConversation.currNode = nodeId;
	}

	/**
	 * Updates a message at a specific index in active messages
	 */
	updateMessageAtIndex(index: number, updates: Partial<DatabaseMessage>): void {
		const message = index === -1 ? undefined : this.activeMessages[index];

		if (!message) return;

		// Assign field by field rather than replacing the object. Replacing it
		// changes the array slot, which invalidates every consumer that merely
		// walks the list - notably ChatMessages.displayMessages, which rebuilds
		// entries for every message in the conversation. Deep $state proxies make
		// per-field writes fine-grained, so only readers of the changed field wake.
		const target = message as unknown as Record<string, unknown>;

		for (const [key, value] of Object.entries(updates)) {
			if (target[key] !== value) {
				target[key] = value;
			}
		}
	}

	/**
	 *
	 *
	 * Import & Export
	 *
	 *
	 */

	private notifyConversationsDeleted(convIds: string[]): void {
		if (convIds.length === 0) return;

		for (const listener of this.conversationDeletionListeners) {
			listener(convIds);
		}
	}
}

export const conversationsStore = new ConversationsStore();