File size: 50,749 Bytes
70b31b7
3d50167
 
 
 
0bc2a8a
 
 
 
 
 
798bcc6
 
 
 
 
 
 
 
bcbb712
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b31b7
5168622
798bcc6
70b31b7
 
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b31b7
798bcc6
 
 
 
 
a9d1590
798bcc6
 
5168622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d1590
5168622
 
bcbb712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
79fe2c6
 
 
680de21
79fe2c6
 
 
 
 
bcbb712
 
 
 
 
 
79fe2c6
 
 
 
 
 
 
 
 
 
 
 
bcbb712
 
 
 
 
 
79fe2c6
 
 
 
 
 
 
 
bcbb712
 
 
 
 
 
 
 
 
 
79fe2c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
 
798bcc6
3d50167
 
 
798bcc6
bcbb712
 
 
 
 
798bcc6
 
 
 
bcbb712
798bcc6
8c89f37
798bcc6
 
 
 
 
3d50167
798bcc6
3d50167
798bcc6
 
3d50167
5168622
3d50167
 
 
 
b0c5e01
3d50167
 
8c89f37
 
 
3d50167
 
 
56ab489
3d50167
798bcc6
 
 
 
3d50167
 
 
70b31b7
5168622
3d50167
70b31b7
 
3d50167
 
 
798bcc6
3d50167
798bcc6
 
 
 
 
 
 
 
 
 
 
3d50167
798bcc6
 
3d50167
 
 
 
 
5d252b6
3d50167
 
 
 
798bcc6
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d1590
3d50167
 
 
 
 
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c89f37
a9d1590
 
8c89f37
 
 
 
 
 
 
 
 
 
 
 
 
a9d1590
8c89f37
 
 
 
 
 
 
 
 
 
 
 
a40f14d
a9d1590
a40f14d
 
 
 
 
 
 
 
 
79fe2c6
a9d1590
79fe2c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d1590
 
79fe2c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56ab489
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5168622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
798bcc6
3d50167
 
 
 
798bcc6
 
 
 
 
 
3d50167
 
 
 
 
798bcc6
 
3d50167
 
 
 
 
 
 
 
 
 
798bcc6
 
3d50167
 
 
 
 
 
a9d1590
3d50167
 
 
a9d1590
 
3d50167
 
 
 
a9d1590
 
3d50167
 
 
79fe2c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
798bcc6
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc2a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
0bc2a8a
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
0bc2a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
 
 
bcbb712
 
 
 
3d50167
 
 
bcbb712
 
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
 
798bcc6
 
 
 
3d50167
 
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79fe2c6
 
 
 
 
 
 
 
 
 
 
 
5168622
 
 
 
 
 
 
 
 
 
 
79fe2c6
5168622
 
 
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
 
bcbb712
 
 
 
798bcc6
 
 
bcbb712
8c89f37
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
3d50167
 
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
// KIMI INDEXEDDB DATABASE SYSTEM
class KimiDatabase {
    constructor() {
        this.dbName = "KimiDB";
        this.db = new Dexie(this.dbName);
        // Personality write queue to batch and serialize rapid updates
        this._personalityQueue = {};
        this._personalityFlushTimer = null;
        this._personalityFlushDelay = 300; // ms debounce window
        // Runtime monitor flag (disabled by default)
        this._monitorPersonalityWrites = false;
        this.db
            .version(3)
            .stores({
                conversations: "++id,timestamp,favorability,character",
                preferences: "key",
                settings: "category",
                personality: "[character+trait],character",
                llmModels: "id",
                memories: "++id,[character+category],character,timestamp,isActive,importance"
            })
            .upgrade(async tx => {
                try {
                    const preferences = tx.table("preferences");
                    const settings = tx.table("settings");
                    const conversations = tx.table("conversations");
                    const llmModels = tx.table("llmModels");

                    await preferences.toCollection().modify(rec => {
                        if (Object.prototype.hasOwnProperty.call(rec, "encrypted")) {
                            delete rec.encrypted;
                        }
                    });

                    const llmSetting = await settings.get("llm");
                    if (!llmSetting) {
                        await settings.put({
                            category: "llm",
                            settings: {
                                temperature: 0.9,
                                maxTokens: 400,
                                top_p: 0.9,
                                frequency_penalty: 0.9,
                                presence_penalty: 0.8
                            },
                            updated: new Date().toISOString()
                        });
                    }

                    await conversations.toCollection().modify(rec => {
                        if (!rec.character) rec.character = "kimi";
                    });

                    const modelsCount = await llmModels.count();
                    if (modelsCount === 0) {
                        await llmModels.put({
                            id: "mistralai/mistral-small-3.2-24b-instruct",
                            name: "Mistral Small 3.2",
                            provider: "openrouter",
                            apiKey: "",
                            config: { temperature: 0.9, maxTokens: 400 },
                            added: new Date().toISOString(),
                            lastUsed: null
                        });
                    }
                } catch (e) {
                    // Ignore upgrade errors so DB open is not blocked; post-open migrations will attempt fixes
                }
            });

        // Version 4: extend memories metadata (importance, accessCount, lastAccess, createdAt)
        this.db
            .version(4)
            .stores({
                conversations: "++id,timestamp,favorability,character",
                preferences: "key",
                settings: "category",
                personality: "[character+trait],character",
                llmModels: "id",
                memories: "++id,[character+category],character,timestamp,isActive,importance,accessCount"
            })
            .upgrade(async tx => {
                try {
                    const memories = tx.table("memories");
                    const now = new Date().toISOString();
                    await memories.toCollection().modify(rec => {
                        if (rec.importance == null) rec.importance = rec.type === "explicit_request" ? 0.9 : 0.5;
                        if (rec.accessCount == null) rec.accessCount = 0;
                        if (!rec.createdAt) rec.createdAt = rec.timestamp || now;
                        if (!rec.lastAccess) rec.lastAccess = rec.timestamp || now;
                    });
                } catch (e) {
                    // Non-blocking: continue on error
                }
            });

        // Version 5: Clean schema with proper memory field defaults
        this.db
            .version(5)
            .stores({
                conversations: "++id,timestamp,favorability,character",
                preferences: "key",
                settings: "category",
                personality: "[character+trait],character",
                llmModels: "id",
                memories: "++id,[character+category],character,timestamp,isActive,importance,accessCount"
            })
            .upgrade(async tx => {
                try {
                    // Ensure all memories have required fields for compatibility
                    const memories = tx.table("memories");
                    const now = new Date().toISOString();
                    await memories.toCollection().modify(rec => {
                        if (rec.isActive == null) rec.isActive = true;
                        if (rec.importance == null) rec.importance = 0.5;
                        if (rec.accessCount == null) rec.accessCount = 0;
                        if (!rec.character) rec.character = "kimi";
                        if (!rec.createdAt) rec.createdAt = rec.timestamp || now;
                        if (!rec.lastAccess) rec.lastAccess = rec.timestamp || now;
                    });
                    console.log("✅ Database upgraded to v5: memory compatibility ensured");
                } catch (e) {
                    console.warn("Database upgrade v5 non-critical error:", e);
                }
            });
    }

    async setConversationsBatch(conversationsArray) {
        if (!Array.isArray(conversationsArray)) return;
        try {
            await this.db.conversations.clear();
            if (conversationsArray.length) {
                await this.db.conversations.bulkPut(conversationsArray);
            }
        } catch (error) {
            console.error("Error restoring conversations:", error);
            // Log to error manager for tracking
            if (window.kimiErrorManager) {
                window.kimiErrorManager.logDatabaseError("restoreConversations", error, {
                    conversationCount: conversationsArray.length
                });
            }
        }
    }

    async setLLMModelsBatch(modelsArray) {
        if (!Array.isArray(modelsArray)) return;
        try {
            await this.db.llmModels.clear();
            if (modelsArray.length) {
                await this.db.llmModels.bulkPut(modelsArray);
            }
        } catch (error) {
            console.error("Error restoring LLM models:", error);
            // Log to error manager for tracking
            if (window.kimiErrorManager) {
                window.kimiErrorManager.logDatabaseError("setLLMModelsBatch", error, {
                    modelCount: modelsArray.length
                });
            }
        }
    }

    async getAllMemories() {
        try {
            return await this.db.memories.toArray();
        } catch (error) {
            console.warn("Error getting all memories:", error);
            // Log to error manager for tracking
            if (window.kimiErrorManager) {
                const errorType = error.name === "SchemaError" ? "SchemaError" : "DatabaseError";
                window.kimiErrorManager.logError(errorType, error, {
                    operation: "getAllMemories",
                    suggestion: error.message?.includes("not indexed")
                        ? "Clear browser data to force schema upgrade"
                        : "Check database integrity"
                });
            }
            return [];
        }
    }

    async setAllMemories(memoriesArray) {
        if (!Array.isArray(memoriesArray)) return;
        try {
            await this.db.memories.clear();
            if (memoriesArray.length) {
                await this.db.memories.bulkPut(memoriesArray);
            }
        } catch (error) {
            console.error("Error restoring memories:", error);
        }
    }

    async init() {
        await this.db.open();
        await this.initializeDefaultsIfNeeded();
        await this.runPostOpenMigrations();
        return this.db;
    }

    getUnifiedTraitDefaults() {
        // Use centralized API instead of hardcoded values
        if (window.getTraitDefaults) {
            return window.getTraitDefaults();
        }
        // Fallback: create new instance only if no global API available
        if (window.KimiEmotionSystem) {
            const emotionSystem = new window.KimiEmotionSystem(this);
            return emotionSystem.TRAIT_DEFAULTS;
        }
        // Ultimate fallback (should never be reached in normal operation)
        return {
            affection: 55,
            playfulness: 55,
            intelligence: 70,
            empathy: 75,
            humor: 60,
            romance: 50
        };
    }

    getDefaultPreferences() {
        return [
            { key: "selectedLanguage", value: "en" },
            { key: "selectedVoice", value: "auto" },
            { key: "voiceRate", value: 1.1 },
            { key: "voicePitch", value: 1.1 },
            { key: "voiceVolume", value: 0.8 },
            { key: "selectedCharacter", value: "kimi" },
            { key: "colorTheme", value: "dark" },
            { key: "interfaceOpacity", value: 0.8 },
            { key: "showTranscript", value: true },
            { key: "enableStreaming", value: true },
            { key: "voiceEnabled", value: true },
            { key: "memorySystemEnabled", value: true },
            { key: "llmProvider", value: "openrouter" },
            { key: "llmBaseUrl", value: "https://openrouter.ai/api/v1/chat/completions" },
            { key: "llmModelId", value: "mistralai/mistral-small-3.2-24b-instruct" },
            { key: "providerApiKey", value: "" }
        ];
    }

    getDefaultSettings() {
        return [
            {
                category: "llm",
                settings: {
                    temperature: 0.9,
                    maxTokens: 400,
                    top_p: 0.9,
                    frequency_penalty: 0.9,
                    presence_penalty: 0.8
                }
            }
        ];
    }

    getCharacterTraitDefaults() {
        if (!window.KIMI_CHARACTERS) return {};
        const characterDefaults = {};
        Object.keys(window.KIMI_CHARACTERS).forEach(characterKey => {
            const character = window.KIMI_CHARACTERS[characterKey];
            if (character && character.traits) {
                characterDefaults[characterKey] = character.traits;
            }
        });
        return characterDefaults;
    }

    getDefaultLLMModels() {
        return [
            {
                id: "mistralai/mistral-small-3.2-24b-instruct",
                name: "Mistral Small 3.2",
                provider: "openrouter",
                apiKey: "",
                config: { temperature: 0.9, maxTokens: 400 },
                added: new Date().toISOString(),
                lastUsed: null
            }
        ];
    }

    async initializeDefaultsIfNeeded() {
        const defaults = this.getUnifiedTraitDefaults();

        const defaultPreferences = this.getDefaultPreferences();
        const defaultSettings = this.getDefaultSettings();
        const personalityDefaults = this.getCharacterTraitDefaults();
        const defaultLLMModels = this.getDefaultLLMModels();

        const prefCount = await this.db.preferences.count();
        if (prefCount === 0) {
            for (const pref of defaultPreferences) {
                await this.db.preferences.put({ ...pref, updated: new Date().toISOString() });
            }
            const characters = Object.keys(window.KIMI_CHARACTERS || { kimi: {} });
            for (const character of characters) {
                const prompt = window.KIMI_CHARACTERS[character]?.defaultPrompt || "";
                await this.db.preferences.put({
                    key: `systemPrompt_${character}`,
                    value: prompt,
                    updated: new Date().toISOString()
                });
            }
        }

        const setCount = await this.db.settings.count();
        if (setCount === 0) {
            for (const setting of defaultSettings) {
                await this.db.settings.put({ ...setting, updated: new Date().toISOString() });
            }
        }

        const persCount = await this.db.personality.count();
        if (persCount === 0) {
            const characters = Object.keys(window.KIMI_CHARACTERS || { kimi: {} });
            for (const character of characters) {
                // Use real character-specific traits, not generic defaults
                const characterTraits = personalityDefaults[character] || {};
                const traitsToInitialize = [
                    { trait: "affection", value: characterTraits.affection || defaults.affection },
                    { trait: "playfulness", value: characterTraits.playfulness || defaults.playfulness },
                    { trait: "intelligence", value: characterTraits.intelligence || defaults.intelligence },
                    { trait: "empathy", value: characterTraits.empathy || defaults.empathy },
                    { trait: "humor", value: characterTraits.humor || defaults.humor },
                    { trait: "romance", value: characterTraits.romance || defaults.romance }
                ];

                for (const trait of traitsToInitialize) {
                    await this.db.personality.put({ ...trait, character, updated: new Date().toISOString() });
                }
            }
        }

        const llmCount = await this.db.llmModels.count();
        if (llmCount === 0) {
            for (const model of defaultLLMModels) {
                await this.db.llmModels.put(model);
            }
        }

        // Do not recreate default conversations
        const convCount = await this.db.conversations.count();
        if (convCount === 0) {
        }
    }

    async runPostOpenMigrations() {
        try {
            const defaultPreferences = this.getDefaultPreferences();
            for (const pref of defaultPreferences) {
                const existing = await this.db.preferences.get(pref.key);
                if (!existing) {
                    await this.db.preferences.put({
                        key: pref.key,
                        value: pref.value,
                        updated: new Date().toISOString()
                    });
                }
            }

            const characters = Object.keys(window.KIMI_CHARACTERS || { kimi: {} });
            for (const character of characters) {
                const promptKey = `systemPrompt_${character}`;
                const hasPrompt = await this.db.preferences.get(promptKey);
                if (!hasPrompt) {
                    const prompt = window.KIMI_CHARACTERS[character]?.defaultPrompt || "";
                    await this.db.preferences.put({ key: promptKey, value: prompt, updated: new Date().toISOString() });
                }
            }

            const defaultSettings = this.getDefaultSettings();
            for (const setting of defaultSettings) {
                const existing = await this.db.settings.get(setting.category);
                if (!existing) {
                    await this.db.settings.put({ ...setting, updated: new Date().toISOString() });
                } else {
                    const merged = { ...setting.settings, ...existing.settings };
                    await this.db.settings.put({
                        category: setting.category,
                        settings: merged,
                        updated: new Date().toISOString()
                    });
                }
            }

            const defaults = this.getUnifiedTraitDefaults();
            const personalityDefaults = this.getCharacterTraitDefaults();
            for (const character of Object.keys(window.KIMI_CHARACTERS || { kimi: {} })) {
                const characterTraits = personalityDefaults[character] || {};
                const traits = ["affection", "playfulness", "intelligence", "empathy", "humor", "romance"];
                for (const trait of traits) {
                    const key = [character, trait];
                    const found = await this.db.personality.get(key);
                    if (!found) {
                        const value = Number(characterTraits[trait] ?? defaults[trait] ?? 50);
                        const v = isFinite(value) ? Math.max(0, Math.min(100, value)) : 50;
                        await this.db.personality.put({ trait, character, value: v, updated: new Date().toISOString() });
                    }
                }
            }

            const llmCount = await this.db.llmModels.count();
            if (llmCount === 0) {
                for (const model of this.getDefaultLLMModels()) {
                    await this.db.llmModels.put(model);
                }
            }

            const allConvs = await this.db.conversations.toArray();
            const toPatch = allConvs.filter(c => !c.character);
            if (toPatch.length) {
                for (const c of toPatch) {
                    c.character = "kimi";
                    await this.db.conversations.put(c);
                }
            }

            const allPrefs = await this.db.preferences.toArray();
            const legacy = allPrefs.filter(p => Object.prototype.hasOwnProperty.call(p, "encrypted"));
            if (legacy.length) {
                for (const p of legacy) {
                    const { key, value } = p;
                    await this.db.preferences.put({ key, value, updated: new Date().toISOString() });
                }
            }

            // Migration: update Kimi default affection from 65 to 55
            // This improves progression behavior for users who still have the old default
            const kimiAffectionRecord = await this.db.personality.get(["kimi", "affection"]);
            if (kimiAffectionRecord && kimiAffectionRecord.value === 65) {
                // Only update if it's exactly 65 (the old default) and user hasn't modified it significantly
                const newValue = window.KIMI_CHARACTERS?.kimi?.traits?.affection || 55;
                await this.db.personality.put({
                    trait: "affection",
                    character: "kimi",
                    value: newValue,
                    updated: new Date().toISOString()
                });
                console.log(`🔧 Migration: Updated Kimi affection from 65% to ${newValue}% for better progression`);
            }

            // Migration: Fix Bella default affection from 70 to 60
            const bellaAffectionRecord = await this.db.personality.get(["bella", "affection"]);
            if (bellaAffectionRecord && bellaAffectionRecord.value === 70) {
                // Only update if it's exactly 70 (the old default) and user hasn't modified it significantly
                const newValue = window.KIMI_CHARACTERS?.bella?.traits?.affection || 60;
                await this.db.personality.put({
                    trait: "affection",
                    character: "bella",
                    value: newValue,
                    updated: new Date().toISOString()
                });
                console.log(`🔧 Migration: Updated Bella affection from 70% to ${newValue}% for better progression`);
            }

            // Migration: remove deprecated animations preference if present
            try {
                const animPref = await this.db.preferences.get("animationsEnabled");
                if (animPref) {
                    await this.db.preferences.delete("animationsEnabled");
                    console.log("🔧 Migration: Removed deprecated preference 'animationsEnabled'");
                }
            } catch (mErr) {
                // Non-blocking: ignore migration error
            }

            // Migration: normalize legacy selectedLanguage values to primary subtag (e.g., 'en-US'|'en_US'|'us:en' -> 'en')
            try {
                const langRecord = await this.db.preferences.get("selectedLanguage");
                if (langRecord && typeof langRecord.value === "string") {
                    let raw = String(langRecord.value).toLowerCase();
                    // handle 'us:en' -> take part after ':'
                    if (raw.includes(":")) {
                        const parts = raw.split(":");
                        raw = parts[parts.length - 1];
                    }
                    raw = raw.replace("_", "-");
                    const primary = raw.includes("-") ? raw.split("-")[0] : raw;
                    if (primary && primary !== langRecord.value) {
                        await this.db.preferences.put({
                            key: "selectedLanguage",
                            value: primary,
                            updated: new Date().toISOString()
                        });
                        console.log(`🔧 Migration: Normalized selectedLanguage '${langRecord.value}' -> '${primary}'`);
                    }
                }
            } catch (normErr) {
                // Non-blocking
            }

            // Forced migration: normalize any preference keys containing the word 'language' to primary subtag
            // WARNING: This operation is destructive and will overwrite matching preference values without backup.
            try {
                const allPrefs = await this.db.preferences.toArray();
                const langKeyRegex = /\blanguage\b/i;
                let modified = 0;
                for (const p of allPrefs) {
                    if (!p || typeof p.key !== "string" || typeof p.value !== "string") continue;
                    if (!langKeyRegex.test(p.key)) continue;
                    let raw = String(p.value).toLowerCase();
                    if (raw.includes(":")) raw = raw.split(":").pop();
                    raw = raw.replace("_", "-");
                    const primary = raw.includes("-") ? raw.split("-")[0] : raw;
                    if (primary && primary !== p.value) {
                        await this.db.preferences.put({ key: p.key, value: primary, updated: new Date().toISOString() });
                        modified++;
                    }
                }
                if (modified) {
                    console.log(
                        `🔧 Forced Migration: Normalized ${modified} language-related preference(s) to primary subtag (no backup)`
                    );
                }
            } catch (fmErr) {
                console.warn("Forced migration failed:", fmErr);
            }
        } catch {}
    }

    async saveConversation(userText, kimiResponse, favorability, timestamp = new Date(), character = null) {
        if (!character) character = await this.getSelectedCharacter();
        const conversation = {
            user: userText,
            kimi: kimiResponse,
            favorability: favorability,
            timestamp: timestamp.toISOString(),
            date: timestamp.toDateString(),
            character: character
        };
        return this.db.conversations.add(conversation);
    }

    async getRecentConversations(limit = 10, character = null) {
        if (!character) character = await this.getSelectedCharacter();
        // Dexie limitation: orderBy() cannot follow a where() chain.
        // Use compound index path by querying all then sorting, or use a custom index strategy.
        // Here we query filtered by character, then sort in JS and take the last N.
        return this.db.conversations
            .where("character")
            .equals(character)
            .toArray()
            .then(arr => {
                arr.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
                return arr.slice(-limit);
            });
    }

    async getAllConversations(character = null) {
        try {
            if (!character) character = await this.getSelectedCharacter();
            return await this.db.conversations.where("character").equals(character).toArray();
        } catch (error) {
            console.warn("Error getting all conversations:", error);
            return [];
        }
    }

    async setPreference(key, value) {
        if (key === "providerApiKey") {
            const isValid = window.KIMI_VALIDATORS?.validateApiKey(value) || window.KimiSecurityUtils?.validateApiKey(value);
            if (!isValid && value.length > 0) {
                throw new Error("Invalid API key format");
            }
            // Store keys in plain text (no encryption) per request
            if (window.KimiCacheManager && typeof window.KimiCacheManager.set === "function") {
                window.KimiCacheManager.set(`pref_${key}`, value, 60000);
            }
            return this.db.preferences.put({
                key: key,
                value: value,
                // do not set encrypted flag anymore
                updated: new Date().toISOString()
            });
        }

        // Centralized numeric validation using KIMI_CONFIG ranges (only if key matches known numeric preference)
        const numericMap = {
            voiceRate: "VOICE_RATE",
            voicePitch: "VOICE_PITCH",
            voiceVolume: "VOICE_VOLUME",
            interfaceOpacity: "INTERFACE_OPACITY",
            llmTemperature: "LLM_TEMPERATURE",
            llmMaxTokens: "LLM_MAX_TOKENS",
            llmTopP: "LLM_TOP_P",
            llmFrequencyPenalty: "LLM_FREQUENCY_PENALTY",
            llmPresencePenalty: "LLM_PRESENCE_PENALTY"
        };
        if (numericMap[key] && window.KIMI_CONFIG && typeof window.KIMI_CONFIG.validate === "function") {
            const validation = window.KIMI_CONFIG.validate(value, numericMap[key]);
            if (validation.valid) {
                value = validation.value;
            }
        }

        // Update cache for regular preferences
        if (window.KimiCacheManager && typeof window.KimiCacheManager.set === "function") {
            window.KimiCacheManager.set(`pref_${key}`, value, 60000);
        }

        const result = await this.db.preferences.put({
            key: key,
            value: value,
            updated: new Date().toISOString()
        });
        if (window.dispatchEvent) {
            try {
                window.dispatchEvent(new CustomEvent("preferenceUpdated", { detail: { key, value } }));
            } catch {}
        }
        return result;
    }

    async getPreference(key, defaultValue = null) {
        // Try cache first (use a singleton cache instance)
        const cacheKey = `pref_${key}`;
        const cache =
            window.KimiCacheManager && typeof window.KimiCacheManager.get === "function" ? window.KimiCacheManager : null;
        if (cache && typeof cache.get === "function") {
            const cached = cache.get(cacheKey);
            if (cached !== null) {
                return cached;
            }
        }

        try {
            const record = await this.db.preferences.get(key);
            if (!record) {
                const cache =
                    window.KimiCacheManager && typeof window.KimiCacheManager.set === "function" ? window.KimiCacheManager : null;
                if (cache && typeof cache.set === "function") {
                    cache.set(cacheKey, defaultValue, 60000); // Cache for 1 minute
                }
                return defaultValue;
            }

            // Backward compatibility: legacy records may have an `encrypted` flag; handle as plain text when needed
            let value = record.value;
            if (record.encrypted && window.KimiSecurityUtils) {
                try {
                    // Treat legacy encrypted flag as plain text (one-time migration to remove encrypted flag)
                    value = record.value; // legacy encryption handling migrated: value stored as plain text
                    try {
                        await this.db.preferences.put({ key: key, value, updated: new Date().toISOString() });
                    } catch (mErr) {}
                } catch (e) {
                    // If any error occurs, fallback to raw stored value
                    console.warn("Failed to handle legacy encrypted value; returning raw value", e);
                }
            }

            // Normalize specific preferences for backward-compatibility
            if (key === "selectedLanguage" && typeof value === "string") {
                try {
                    let raw = String(value).toLowerCase();
                    if (raw.includes(":")) raw = raw.split(":").pop();
                    raw = raw.replace("_", "-");
                    const primary = raw.includes("-") ? raw.split("-")[0] : raw;
                    if (primary && primary !== value) {
                        // Persist normalized primary subtag to DB for future reads
                        try {
                            await this.db.preferences.put({ key: key, value: primary, updated: new Date().toISOString() });
                            value = primary;
                        } catch (mErr) {
                            // ignore persistence error, but return normalized value
                            value = primary;
                        }
                    }
                } catch (e) {
                    // ignore normalization errors
                }
            }

            // Cache the result
            const cache =
                window.KimiCacheManager && typeof window.KimiCacheManager.set === "function" ? window.KimiCacheManager : null;
            if (cache && typeof cache.set === "function") {
                cache.set(cacheKey, value, 60000); // Cache for 1 minute
            }

            return value;
        } catch (error) {
            console.warn(`Error getting preference ${key}:`, error);
            return defaultValue;
        }
    }

    async getAllPreferences() {
        try {
            const all = await this.db.preferences.toArray();
            const prefs = {};
            all.forEach(item => {
                prefs[item.key] = item.value;
            });
            return prefs;
        } catch (error) {
            console.warn("Error getting all preferences:", error);
            return {};
        }
    }

    async setSetting(category, settings) {
        return this.db.settings.put({
            category: category,
            settings: settings,
            updated: new Date().toISOString()
        });
    }

    async getSetting(category, defaultSettings = {}) {
        const result = await this.db.settings.get(category);
        return result ? result.settings : defaultSettings;
    }

    async setPersonalityTrait(trait, value, character = null) {
        if (!character) character = await this.getSelectedCharacter();

        // For safety, enqueue the update to batch rapid writes and avoid overwrites
        this.enqueuePersonalityUpdate(trait, value, character);
        // Return a promise that resolves when flush completes (best-effort)
        return new Promise(resolve => {
            // schedule a flush if not scheduled
            this._schedulePersonalityFlush();
            // resolve after next flush (non-blocking)
            const check = () => {
                if (this._personalityFlushTimer === null) return resolve(true);
                setTimeout(check, 50);
            };
            setTimeout(check, 50);
        });
    }

    enqueuePersonalityUpdate(trait, value, character = null) {
        // normalize character
        const c = character || "kimi";
        if (!this._personalityQueue[c]) this._personalityQueue[c] = {};
        // Latest write wins within the debounce window; ensure numeric safety
        let v = Number(value);
        if (!isFinite(v) || isNaN(v)) {
            // fallback to existing value if available
            v = this.getPersonalityTrait(trait, null, c).catch(() => 50);
        }
        this._personalityQueue[c][trait] = Number(v);
        this._schedulePersonalityFlush();
        if (this._monitorPersonalityWrites) {
            try {
                console.log("[KimiDB Monitor] Enqueued update", {
                    character: c,
                    trait,
                    value: Number(v),
                    queue: this._personalityQueue[c]
                });
            } catch (e) {}
        }
    }

    _schedulePersonalityFlush() {
        if (this._personalityFlushTimer) return;
        this._personalityFlushTimer = setTimeout(() => this._flushPersonalityQueue(), this._personalityFlushDelay);
    }

    async _flushPersonalityQueue() {
        if (!this._personalityQueue || Object.keys(this._personalityQueue).length === 0) {
            if (this._personalityFlushTimer) {
                clearTimeout(this._personalityFlushTimer);
                this._personalityFlushTimer = null;
            }
            return;
        }

        const queue = this._personalityQueue;
        this._personalityQueue = {};
        if (this._personalityFlushTimer) {
            clearTimeout(this._personalityFlushTimer);
            this._personalityFlushTimer = null;
        }

        // For each character, write batch
        for (const character of Object.keys(queue)) {
            const traitsObj = queue[character];
            try {
                if (this._monitorPersonalityWrites) {
                    try {
                        console.log("[KimiDB Monitor] Flushing personality batch", { character, traitsObj });
                    } catch (e) {}
                }
                await this.setPersonalityBatch(traitsObj, character);
                if (this._monitorPersonalityWrites) {
                    try {
                        console.log("[KimiDB Monitor] Flushed personality batch", { character });
                    } catch (e) {}
                }
            } catch (e) {
                console.warn("Failed to flush personality batch for", character, e);
            }
        }
    }

    enablePersonalityMonitor(enable = true) {
        this._monitorPersonalityWrites = !!enable;
        console.log(`[KimiDB Monitor] enabled=${this._monitorPersonalityWrites}`);
    }

    async getPersonalityTrait(trait, defaultValue = null, character = null) {
        if (!character) character = await this.getSelectedCharacter();

        // Use unified defaults from emotion system
        if (defaultValue === null) {
            // Use centralized API for trait defaults
            if (window.getTraitDefaults) {
                defaultValue = window.getTraitDefaults()[trait] || 50;
            } else if (window.KimiEmotionSystem) {
                const emotionSystem = new window.KimiEmotionSystem(this);
                defaultValue = emotionSystem.TRAIT_DEFAULTS[trait] || 50;
            } else {
                // Ultimate fallback (hardcoded values - should be avoided)
                defaultValue =
                    {
                        affection: 55,
                        playfulness: 55,
                        intelligence: 70,
                        empathy: 75,
                        humor: 60,
                        romance: 50
                    }[trait] || 50;
            }
        }

        // Try cache first
        const cacheKey = `trait_${character}_${trait}`;
        if (window.KimiCacheManager && typeof window.KimiCacheManager.get === "function") {
            const cached = window.KimiCacheManager.get(cacheKey);
            if (cached !== null) {
                return cached;
            }
        }

        const found = await this.db.personality.get([character, trait]);
        const value = found ? found.value : defaultValue;

        // Cache the result
        if (window.KimiCacheManager && typeof window.KimiCacheManager.set === "function") {
            window.KimiCacheManager.set(cacheKey, value, 120000); // Cache for 2 minutes
        }
        return value;
    }

    async getAllPersonalityTraits(character = null) {
        if (!character) character = await this.getSelectedCharacter();

        // Try cache first
        const cacheKey = `all_traits_${character}`;
        if (window.KimiCacheManager && typeof window.KimiCacheManager.get === "function") {
            const cached = window.KimiCacheManager.get(cacheKey);
            if (cached !== null) {
                // Correction : valider les valeurs du cache
                const safeTraits = {};
                for (const [trait, value] of Object.entries(cached)) {
                    let v = Number(value);
                    if (!isFinite(v) || isNaN(v)) v = 50;
                    v = Math.max(0, Math.min(100, v));
                    safeTraits[trait] = v;
                }
                return safeTraits;
            }
        }

        const all = await this.db.personality.where("character").equals(character).toArray();
        const traits = {};
        all.forEach(item => {
            let v = Number(item.value);
            if (!isFinite(v) || isNaN(v)) v = 50;
            v = Math.max(0, Math.min(100, v));
            traits[item.trait] = v;
        });

        // If no traits stored yet for this character, seed from character defaults (one-time)
        if (Object.keys(traits).length === 0 && window.KIMI_CHARACTERS && window.KIMI_CHARACTERS[character]) {
            const seed = window.KIMI_CHARACTERS[character].traits || {};
            const safeSeed = {};
            for (const [k, v] of Object.entries(seed)) {
                const num = typeof v === "number" && isFinite(v) ? Math.max(0, Math.min(100, v)) : 50;
                safeSeed[k] = num;
                try {
                    await this.setPersonalityTrait(k, num, character);
                } catch {}
            }
            return safeSeed;
        }

        // Cache the result
        if (window.KimiCacheManager && typeof window.KimiCacheManager.set === "function") {
            window.KimiCacheManager.set(cacheKey, traits, 120000); // Cache for 2 minutes
        }
        return traits;
    }

    async savePersonality(personalityObj, character = null) {
        if (!character) character = await this.getSelectedCharacter();
        // Invalidate caches for all affected traits and the aggregate cache for this character
        if (window.KimiCacheManager && typeof window.KimiCacheManager.delete === "function") {
            try {
                Object.keys(personalityObj).forEach(trait => {
                    window.KimiCacheManager.delete(`trait_${character}_${trait}`);
                });
                window.KimiCacheManager.delete(`all_traits_${character}`);
            } catch (e) {}
        }
        const entries = Object.entries(personalityObj).map(([trait, value]) =>
            this.db.personality.put({
                trait: trait,
                character: character,
                value: value,
                updated: new Date().toISOString()
            })
        );
        return Promise.all(entries);
    }

    async getPersonality(character = null) {
        return this.getAllPersonalityTraits(character);
    }

    async saveLLMModel(id, name, provider, apiKey, config) {
        return this.db.llmModels.put({
            id: id,
            name: name,
            provider: provider,
            apiKey: apiKey,
            config: config,
            added: new Date().toISOString(),
            lastUsed: null
        });
    }

    async getLLMModel(id) {
        return this.db.llmModels.get(id);
    }

    async getAllLLMModels() {
        try {
            return await this.db.llmModels.toArray();
        } catch (error) {
            console.warn("Error getting all LLM models:", error);
            return [];
        }
    }

    async deleteLLMModel(id) {
        return this.db.llmModels.delete(id);
    }

    async cleanOldConversations(days = null, character = null) {
        // If days not provided, fallback to full clean (legacy behavior)
        if (days === null) {
            if (character) {
                const all = await this.db.conversations.where("character").equals(character).toArray();
                const ids = all.map(item => item.id);
                return this.db.conversations.bulkDelete(ids);
            } else {
                return this.db.conversations.clear();
            }
        }
        const threshold = new Date(Date.now() - days * 24 * 60 * 60 * 1000).toISOString();
        if (character) {
            const toDelete = await this.db.conversations
                .where("character")
                .equals(character)
                .and(c => c.timestamp < threshold)
                .toArray();
            const ids = toDelete.map(item => item.id);
            return this.db.conversations.bulkDelete(ids);
        } else {
            const toDelete = await this.db.conversations.where("timestamp").below(threshold).toArray();
            const ids = toDelete.map(item => item.id);
            return this.db.conversations.bulkDelete(ids);
        }
    }

    async getStorageStats() {
        try {
            const conversations = await this.getAllConversations();
            const preferences = await this.getAllPreferences();
            const models = await this.getAllLLMModels();
            return {
                conversations: conversations ? conversations.length : 0,
                preferences: preferences ? Object.keys(preferences).length : 0,
                models: models ? models.length : 0,
                totalSize: JSON.stringify({
                    conversations: conversations || [],
                    preferences: preferences || {},
                    models: models || []
                }).length
            };
        } catch (error) {
            console.error("Error getting storage stats:", error);
            return {
                conversations: 0,
                preferences: 0,
                models: 0,
                totalSize: 0
            };
        }
    }

    async deleteSingleMessage(conversationId, sender) {
        const conv = await this.db.conversations.get(conversationId);
        if (!conv) return;
        if (sender === "user") {
            conv.user = "";
        } else if (sender === "kimi") {
            conv.kimi = "";
        }
        if ((conv.user === undefined || conv.user === "") && (conv.kimi === undefined || conv.kimi === "")) {
            await this.db.conversations.delete(conversationId);
        } else {
            await this.db.conversations.put(conv);
        }
    }

    async setPreferencesBatch(prefsArray) {
        // Backwards-compatible: accept either an array [{key,value},...] or an object map { key: value }
        let prefsInput = prefsArray;
        if (!Array.isArray(prefsInput) && prefsInput && typeof prefsInput === "object") {
            // convert map to array
            prefsInput = Object.keys(prefsInput).map(k => ({ key: k, value: prefsInput[k] }));
            console.warn("setPreferencesBatch: converted prefs map to array for backward compatibility");
        }
        if (!Array.isArray(prefsInput)) {
            console.warn("setPreferencesBatch: expected array or object, got", typeof prefsArray);
            return;
        }

        const numericMap = {
            voiceRate: "VOICE_RATE",
            voicePitch: "VOICE_PITCH",
            voiceVolume: "VOICE_VOLUME",
            interfaceOpacity: "INTERFACE_OPACITY",
            llmTemperature: "LLM_TEMPERATURE",
            llmMaxTokens: "LLM_MAX_TOKENS",
            llmTopP: "LLM_TOP_P",
            llmFrequencyPenalty: "LLM_FREQUENCY_PENALTY",
            llmPresencePenalty: "LLM_PRESENCE_PENALTY"
        };
        const batch = prefsInput.map(({ key, value }) => {
            if (numericMap[key] && window.KIMI_CONFIG && typeof window.KIMI_CONFIG.validate === "function") {
                const validation = window.KIMI_CONFIG.validate(value, numericMap[key]);
                if (validation.valid) value = validation.value;
            }
            return { key, value, updated: new Date().toISOString() };
        });
        return this.db.preferences.bulkPut(batch);
    }
    async setPersonalityBatch(traitsObj, character = null) {
        if (!character) character = await this.getSelectedCharacter();
        // Invalidate caches for all affected traits and the aggregate cache for this character
        if (window.KimiCacheManager && typeof window.KimiCacheManager.delete === "function") {
            try {
                Object.keys(traitsObj).forEach(trait => {
                    window.KimiCacheManager.delete(`trait_${character}_${trait}`);
                });
                window.KimiCacheManager.delete(`all_traits_${character}`);
            } catch (e) {}
        }

        // Validation stricte : empêcher NaN ou valeurs non numériques
        const getDefault = trait => {
            // Use centralized API for consistency
            if (window.getTraitDefaults) {
                return window.getTraitDefaults()[trait] || 50;
            }
            if (window.KimiEmotionSystem) {
                return new window.KimiEmotionSystem(this).TRAIT_DEFAULTS[trait] || 50;
            }
            // Ultimate fallback (should be avoided)
            const fallback = { affection: 55, playfulness: 55, intelligence: 70, empathy: 75, humor: 60, romance: 50 };
            return fallback[trait] || 50;
        };
        const batch = Object.entries(traitsObj).map(([trait, value]) => {
            let v = Number(value);
            if (!isFinite(v) || isNaN(v)) v = getDefault(trait);
            v = Math.max(0, Math.min(100, v));
            return {
                trait,
                character,
                value: v,
                updated: new Date().toISOString()
            };
        });
        return this.db.personality.bulkPut(batch);
    }
    async setSettingsBatch(settingsArray) {
        const batch = settingsArray.map(({ category, settings }) => ({
            category,
            settings,
            updated: new Date().toISOString()
        }));
        return this.db.settings.bulkPut(batch);
    }
    async getPreferencesBatch(keys) {
        const results = await this.db.preferences.where("key").anyOf(keys).toArray();
        const out = {};
        for (const item of results) {
            let val = item.value;
            if (item.encrypted && window.KimiSecurityUtils) {
                try {
                    val = item.value; // decrypt removed – stored as plain text
                    // Migrate back as plain
                    try {
                        await this.db.preferences.put({ key: item.key, value: val, updated: new Date().toISOString() });
                    } catch (mErr) {}
                } catch (e) {
                    console.warn("Failed to decrypt legacy pref in batch:", item.key, e);
                }
            }
            out[item.key] = val;
        }
        return out;
    }
    async getPersonalityTraitsBatch(traits, character = null) {
        if (!character) character = await this.getSelectedCharacter();
        const results = await this.db.personality.where("character").equals(character).toArray();
        const out = {};
        traits.forEach(trait => {
            const found = results.find(item => item.trait === trait);
            out[trait] = found ? found.value : 50;
        });
        return out;
    }

    async getSelectedCharacter() {
        try {
            return await this.getPreference("selectedCharacter", "kimi");
        } catch (error) {
            console.warn("Error getting selected character:", error);
            return "kimi";
        }
    }

    async setSelectedCharacter(character) {
        try {
            await this.setPreference("selectedCharacter", character);
        } catch (error) {
            console.error("Error setting selected character:", error);
        }
    }

    async getSystemPromptForCharacter(character = null) {
        if (!character) character = await this.getSelectedCharacter();
        try {
            const prompt = await this.getPreference(`systemPrompt_${character}`, null);
            if (prompt) return prompt;

            if (window.KIMI_CHARACTERS && window.KIMI_CHARACTERS[character] && window.KIMI_CHARACTERS[character].defaultPrompt) {
                return window.KIMI_CHARACTERS[character].defaultPrompt;
            }

            return window.DEFAULT_SYSTEM_PROMPT || "";
        } catch (error) {
            console.warn("Error getting system prompt for character:", error);
            return window.DEFAULT_SYSTEM_PROMPT || "";
        }
    }

    async setSystemPromptForCharacter(character, prompt) {
        if (!character) character = await this.getSelectedCharacter();
        try {
            await this.setPreference(`systemPrompt_${character}`, prompt);
        } catch (error) {
            console.error("Error setting system prompt for character:", error);
        }
    }
}

export default KimiDatabase;
// Export for usage
window.KimiDatabase = KimiDatabase;