File size: 54,942 Bytes
798bcc6
 
 
 
 
 
3d50167
 
 
 
 
 
 
798bcc6
 
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
3d50167
 
798bcc6
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
 
 
 
 
 
3d50167
 
 
 
 
 
 
56ab489
3d50167
 
 
 
 
 
 
5f17517
3d50167
 
 
5f17517
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
798bcc6
3d50167
 
5f17517
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4644bfd
 
 
 
 
 
 
 
5f17517
 
 
798bcc6
5f17517
 
 
 
3d50167
56ab489
 
 
3d50167
798bcc6
3d50167
5f17517
3d50167
5168622
 
 
 
 
 
 
 
3d50167
21485d0
3d50167
21485d0
3d50167
 
 
 
 
 
 
 
 
798bcc6
 
3d50167
64bd9f9
 
 
 
 
 
 
 
 
 
 
4644bfd
 
 
 
 
3d50167
 
798bcc6
 
3d50167
 
798bcc6
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4644bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
56ab489
 
 
 
 
 
 
 
 
 
 
 
5f17517
3d50167
64bd9f9
 
798bcc6
3d50167
 
 
4644bfd
3d50167
 
56ab489
 
 
b0c5e01
56ab489
21485d0
3d50167
5f17517
 
21485d0
 
 
 
 
3d50167
 
 
798bcc6
 
 
3d50167
 
56ab489
 
 
 
 
 
 
3d50167
4644bfd
 
 
 
 
 
 
 
 
3d50167
 
64bd9f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4644bfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
e8ca17d
 
 
 
3d50167
 
 
 
e8ca17d
 
 
 
 
 
 
 
 
 
3d50167
 
798bcc6
 
 
3d50167
 
 
798bcc6
 
3d50167
 
 
 
 
 
798bcc6
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5168622
 
3d50167
 
 
 
 
56ab489
 
 
 
3d50167
 
 
 
 
 
 
 
 
5168622
3d50167
 
 
 
 
 
56ab489
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56ab489
 
3d50167
56ab489
add7bfd
56ab489
 
 
 
3d50167
56ab489
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
56ab489
3d50167
 
56ab489
add7bfd
3d50167
 
 
56ab489
3d50167
 
5168622
 
 
 
 
 
 
 
 
3d50167
56ab489
3d50167
 
 
 
 
56ab489
5f17517
3d50167
 
 
5f17517
56ab489
 
 
3d50167
 
 
 
5f17517
3d50167
 
 
 
 
5f17517
56ab489
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
56ab489
 
 
3d50167
 
 
21485d0
3d50167
 
 
 
 
5168622
 
 
 
 
 
 
3d50167
 
5f17517
 
3d50167
 
 
 
 
 
 
 
 
5f17517
3d50167
 
 
 
 
 
 
 
 
 
 
 
798bcc6
 
 
3d50167
 
 
 
 
798bcc6
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfd4e57
 
 
3d50167
 
 
 
 
 
 
cfd4e57
 
 
3d50167
 
cfd4e57
 
 
 
3d50167
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfd4e57
 
3d50167
 
 
 
 
 
 
 
 
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c89f37
798bcc6
 
 
 
 
 
3d50167
798bcc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d50167
 
798bcc6
 
 
 
3d50167
798bcc6
3d50167
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
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
import KimiDatabase from "./kimi-database.js";
import KimiLLMManager from "./kimi-llm-manager.js";
import KimiEmotionSystem from "./kimi-emotion-system.js";
import KimiMemorySystem from "./kimi-memory-system.js";
import KimiMemory from "./kimi-memory.js";

document.addEventListener("DOMContentLoaded", async function () {
    const DEFAULT_SYSTEM_PROMPT = window.DEFAULT_SYSTEM_PROMPT;

    let kimiDB = null;
    let kimiLLM = null;
    let isSystemReady = false;

    // Global debug flag for sync/log verbosity (default: false)
    if (typeof window.KIMI_DEBUG_SYNC === "undefined") {
        window.KIMI_DEBUG_SYNC = false;
    }

    const kimiInit = new KimiInitManager();
    let kimiVideo = null;

    // Error manager is already initialized in kimi-error-manager.js

    try {
        kimiDB = new KimiDatabase();
        await kimiDB.init();

        // Expose globally as soon as available
        window.kimiDB = kimiDB;

        const selectedCharacter = await kimiDB.getPreference("selectedCharacter", "kimi");
        const favorabilityLabel = window.KimiDOMUtils.get("#favorability-label");
        if (favorabilityLabel && window.KIMI_CHARACTERS && window.KIMI_CHARACTERS[selectedCharacter]) {
            favorabilityLabel.setAttribute("data-i18n", "affection_level_of");
            favorabilityLabel.setAttribute(
                "data-i18n-params",
                JSON.stringify({ name: window.KIMI_CHARACTERS[selectedCharacter].name })
            );
            favorabilityLabel.textContent = `πŸ’– Affection level of ${window.KIMI_CHARACTERS[selectedCharacter].name}`;
        }
        const chatHeaderName = window.KimiDOMUtils.get(".chat-header span[data-i18n]");
        if (chatHeaderName && window.KIMI_CHARACTERS && window.KIMI_CHARACTERS[selectedCharacter]) {
            chatHeaderName.setAttribute("data-i18n", `chat_with_${selectedCharacter}`);
        }
        kimiLLM = new KimiLLMManager(kimiDB);
        window.kimiLLM = kimiLLM;
        await kimiLLM.init();

        // Initialize unified emotion system
        window.kimiEmotionSystem = new KimiEmotionSystem(kimiDB);

        // Initialize the new memory system
        window.kimiMemorySystem = new KimiMemorySystem(kimiDB);
        await window.kimiMemorySystem.init();

        // Initialize legacy memory for favorability
        const kimiMemory = new KimiMemory(kimiDB);
        await kimiMemory.init();
        window.kimiMemory = kimiMemory;

        // Expose globally (already set before init)

        // Load available models now that LLM is ready
        if (window.loadAvailableModels) {
            setTimeout(() => window.loadAvailableModels(), 500);
        }

        isSystemReady = true;
        window.isSystemReady = true;
        // API config UI will be initialized after ApiUi is defined
        if (window.refreshAllSliders) {
            try {
                await window.refreshAllSliders();
            } catch {}
        }
    } catch (error) {
        console.error("Initialization error:", error);
    }
    // Centralized helpers for API config UI
    const ApiUi = {
        presenceDot: () => document.getElementById("api-key-presence"),
        presenceDotTest: () => document.getElementById("api-key-presence-test"),
        apiKeyInput: () => document.getElementById("provider-api-key"),
        toggleBtn: () => document.getElementById("toggle-api-key"),
        providerSelect: () => document.getElementById("llm-provider"),
        baseUrlInput: () => document.getElementById("llm-base-url"),
        modelIdInput: () => document.getElementById("llm-model-id"),
        savedBadge: () => document.getElementById("api-key-saved"),
        statusSpan: () => document.getElementById("api-status"),
        testBtn: () => document.getElementById("test-api"),
        // Saved key indicator (left dot)
        setPresence(color) {
            const dot = this.presenceDot();
            if (dot) dot.style.backgroundColor = color;
        },
        // Test result indicator (right dot)
        setTestPresence(color) {
            const dot2 = this.presenceDotTest();
            if (dot2) dot2.style.backgroundColor = color;
        },
        clearStatus() {
            const s = this.statusSpan();
            if (s) {
                s.textContent = "";
                s.style.color = "";
            }
        },
        setTestEnabled(enabled) {
            const b = this.testBtn();
            if (b) b.disabled = !enabled;
        }
    };

    // Initial presence state based on current input value
    {
        const currentVal = (ApiUi.apiKeyInput() || {}).value || "";
        const colorInit = currentVal && currentVal.length > 0 ? "#4caf50" : "#9e9e9e";
        ApiUi.setPresence(colorInit);
        // On load, test status is unknown
        ApiUi.setTestPresence("#9e9e9e");
    }

    // Initialize API config UI from saved preferences
    async function initializeApiConfigUI() {
        try {
            if (!window.kimiDB) return;
            const provider = await window.kimiDB.getPreference("llmProvider", "openrouter");
            const baseUrl = await window.kimiDB.getPreference(
                "llmBaseUrl",
                provider === "openrouter"
                    ? "https://openrouter.ai/api/v1/chat/completions"
                    : "https://api.openai.com/v1/chat/completions"
            );
            const modelId = await window.kimiDB.getPreference(
                "llmModelId",
                window.kimiLLM ? window.kimiLLM.currentModel : "model-id"
            );
            const providerSelect = ApiUi.providerSelect();
            if (providerSelect) providerSelect.value = provider;
            const baseUrlInput = ApiUi.baseUrlInput();
            const modelIdInput = ApiUi.modelIdInput();
            const apiKeyInput = ApiUi.apiKeyInput();

            // Set base URL based on modifiability
            if (baseUrlInput) {
                const isModifiable = isUrlModifiable(provider);
                baseUrlInput.value = baseUrl || "";
                baseUrlInput.disabled = !isModifiable;
                baseUrlInput.style.opacity = isModifiable ? "1" : "0.6";
            }
            // Only prefill model for OpenRouter, others should show placeholder only
            if (modelIdInput) {
                if (provider === "openrouter") {
                    if (!modelIdInput.value) modelIdInput.value = modelId;
                } else {
                    modelIdInput.value = "";
                }
            }
            // Load the provider-specific key
            const keyPref = window.KimiProviderUtils
                ? window.KimiProviderUtils.getKeyPrefForProvider(provider)
                : "providerApiKey";
            const storedKey = await window.kimiDB.getPreference(keyPref, "");
            if (apiKeyInput) apiKeyInput.value = storedKey || "";
            ApiUi.setPresence(storedKey ? "#4caf50" : "#9e9e9e");
            ApiUi.setTestPresence("#9e9e9e");
            const savedBadge = ApiUi.savedBadge();
            if (savedBadge) {
                // Show only if provider requires a key and key exists
                if (provider !== "ollama" && storedKey) {
                    savedBadge.style.display = "inline";
                } else {
                    savedBadge.style.display = "none";
                }
            }
            ApiUi.clearStatus();
            // Enable/disable Test button according to validation (Ollama does not require API key)
            const valid = !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(storedKey || ""));
            ApiUi.setTestEnabled(provider === "ollama" ? true : valid);
            // Update dynamic label and placeholders using change handler logic
            if (providerSelect && typeof providerSelect.dispatchEvent === "function") {
                const ev = new Event("change");
                providerSelect.dispatchEvent(ev);
            }
        } catch (e) {
            console.warn("Failed to initialize API config UI:", e);
        }
    }
    // Hydrate API config UI from DB after ApiUi is defined and function declared
    initializeApiConfigUI();

    // Listen for model changes and update the UI only for OpenRouter
    window.addEventListener("llmModelChanged", function (event) {
        const modelIdInput = ApiUi.modelIdInput();
        const providerSelect = ApiUi.providerSelect();

        // Only update the field if current provider is OpenRouter
        if (modelIdInput && event.detail && event.detail.id && providerSelect && providerSelect.value === "openrouter") {
            modelIdInput.value = event.detail.id;
        }
    });

    // Helper function to check if URL is modifiable for current provider
    function isUrlModifiable(provider) {
        return provider === "openai-compatible" || provider === "ollama";
    }

    const providerSelectEl = document.getElementById("llm-provider");
    if (providerSelectEl) {
        providerSelectEl.addEventListener("change", async function (e) {
            const provider = e.target.value;
            const baseUrlInput = ApiUi.baseUrlInput();
            const modelIdInput = ApiUi.modelIdInput();
            const apiKeyInput = ApiUi.apiKeyInput();

            const placeholders = {
                openrouter: {
                    url: "https://openrouter.ai/api/v1/chat/completions",
                    keyPh: "sk-or-v1-...",
                    model: window.kimiLLM ? window.kimiLLM.currentModel : "model-id"
                },
                openai: {
                    url: "https://api.openai.com/v1/chat/completions",
                    keyPh: "sk-...",
                    model: "gpt-4o-mini"
                },
                groq: {
                    url: "https://api.groq.com/openai/v1/chat/completions",
                    keyPh: "gsk_...",
                    model: "llama-3.1-8b-instant"
                },
                together: {
                    url: "https://api.together.xyz/v1/chat/completions",
                    keyPh: "together_...",
                    model: "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"
                },
                deepseek: {
                    url: "https://api.deepseek.com/chat/completions",
                    keyPh: "sk-...",
                    model: "deepseek-chat"
                },
                "openai-compatible": {
                    url: "https://your-endpoint/v1/chat/completions",
                    keyPh: "your-key",
                    model: "model-id"
                },
                ollama: {
                    url: "http://localhost:11434/api/chat",
                    keyPh: "",
                    model: "llama3"
                }
            };
            const p = placeholders[provider] || placeholders.openai;
            if (baseUrlInput) {
                baseUrlInput.placeholder = p.url;
                // Only allow URL modification for custom and ollama providers
                const isModifiable = isUrlModifiable(provider);

                if (isModifiable) {
                    // For custom and ollama: load saved URL or use default
                    const savedUrl = await window.kimiDB.getPreference("llmBaseUrl", p.url);
                    baseUrlInput.value = savedUrl;
                    baseUrlInput.disabled = false;
                    baseUrlInput.style.opacity = "1";
                } else {
                    // For other providers: fixed URL, not modifiable
                    baseUrlInput.value = p.url;
                    baseUrlInput.disabled = true;
                    baseUrlInput.style.opacity = "0.6";
                }
            }
            if (apiKeyInput) {
                apiKeyInput.placeholder = p.keyPh;
                // Masquer/dΓ©sactiver le champ pour Ollama/local
                if (provider === "ollama") {
                    apiKeyInput.value = "";
                    apiKeyInput.disabled = true;
                    apiKeyInput.style.display = "none";
                } else {
                    apiKeyInput.disabled = false;
                    apiKeyInput.style.display = "";
                }
            }
            if (modelIdInput) {
                modelIdInput.placeholder = p.model;
                // Only populate the field for OpenRouter since those are the models we have in the list
                // For other providers, user must manually enter the provider-specific model ID
                modelIdInput.value = provider === "openrouter" && window.kimiLLM ? window.kimiLLM.currentModel : "";
            }
            if (window.kimiDB) {
                await window.kimiDB.setPreference("llmProvider", provider);

                const apiKeyLabel = document.getElementById("api-key-label");
                // Load provider-specific key into the input for clarity
                const keyPref = window.KimiProviderUtils
                    ? window.KimiProviderUtils.getKeyPrefForProvider(provider)
                    : "providerApiKey";
                const storedKey = await window.kimiDB.getPreference(keyPref, "");
                if (apiKeyInput && provider !== "ollama") apiKeyInput.value = storedKey || "";
                const color = provider === "ollama" ? "#9e9e9e" : storedKey && storedKey.length > 0 ? "#4caf50" : "#9e9e9e";
                ApiUi.setPresence(color);
                // Changing provider invalidates previous test state
                ApiUi.setTestPresence("#9e9e9e");
                ApiUi.setTestEnabled(
                    provider === "ollama"
                        ? true
                        : !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(storedKey || ""))
                );

                // Dynamic label per provider
                if (apiKeyLabel) {
                    apiKeyLabel.textContent = window.KimiProviderUtils
                        ? window.KimiProviderUtils.getLabelForProvider(provider)
                        : "API Key";
                }
                const savedBadge = ApiUi.savedBadge();
                if (savedBadge) {
                    if (provider !== "ollama" && storedKey) {
                        savedBadge.style.display = "inline";
                    } else {
                        savedBadge.style.display = "none";
                    }
                }
                ApiUi.clearStatus();

                // Save URL after all UI updates are complete
                const isModifiable = isUrlModifiable(provider);
                if (isModifiable && baseUrlInput) {
                    await window.kimiDB.setPreference("llmBaseUrl", baseUrlInput.value);
                } else {
                    // For fixed providers, save the standard URL
                    await window.kimiDB.setPreference("llmBaseUrl", p.url);
                }
            }
        });

        // Listen for model ID changes and update the current model
        const modelIdInput = ApiUi.modelIdInput();
        if (modelIdInput) {
            modelIdInput.addEventListener("blur", async function (e) {
                const newModelId = e.target.value.trim();
                if (newModelId && window.kimiLLM && newModelId !== window.kimiLLM.currentModel) {
                    try {
                        await window.kimiLLM.setCurrentModel(newModelId);
                    } catch (error) {
                        console.warn("Failed to set model:", error.message);
                        // Reset to current model if setting failed
                        e.target.value = window.kimiLLM.currentModel || "";
                    }
                }
            });
        }

        // Listen for Base URL changes and save for modifiable providers
        const baseUrlInput = ApiUi.baseUrlInput();
        if (baseUrlInput) {
            baseUrlInput.addEventListener("blur", async function (e) {
                const providerSelect = ApiUi.providerSelect();
                const provider = providerSelect ? providerSelect.value : "openrouter";
                const isModifiable = isUrlModifiable(provider);

                if (isModifiable && window.kimiDB) {
                    const newUrl = e.target.value.trim();
                    if (newUrl) {
                        try {
                            await window.kimiDB.setPreference("llmBaseUrl", newUrl);
                        } catch (error) {
                            console.warn("Failed to save base URL:", error.message);
                        }
                    }
                }
            });
        }
    }

    // Loading screen management
    const hideLoadingScreen = () => {
        const loadingScreen = document.getElementById("loading-screen");
        if (loadingScreen) {
            loadingScreen.style.opacity = "0";
            setTimeout(() => {
                loadingScreen.style.display = "none";
            }, 500);
        }
    };

    // Hide loading screen when resources are loaded
    if (document.readyState === "complete") {
        setTimeout(hideLoadingScreen, 1000);
    } else {
        window.addEventListener("load", () => {
            setTimeout(hideLoadingScreen, 1000);
        });
    }

    // Use centralized video utilities
    let video1 = window.KimiVideoManager.getVideoElement("#video1");
    let video2 = window.KimiVideoManager.getVideoElement("#video2");
    if (!video1 || !video2) {
        const videoContainer = document.querySelector(".video-container");
        if (videoContainer) {
            video1 = window.KimiVideoManager.createVideoElement("video1", "bg-video active");
            video2 = window.KimiVideoManager.createVideoElement("video2", "bg-video");
            videoContainer.appendChild(video1);
            videoContainer.appendChild(video2);
        }
    }
    let activeVideo = video1;
    let inactiveVideo = video2;
    kimiVideo = new window.KimiVideoManager(video1, video2);
    await kimiVideo.init(kimiDB);
    window.kimiVideo = kimiVideo;

    if (video1 && video2 && kimiDB && kimiDB.getSelectedCharacter) {
        try {
            const selectedCharacter = await kimiDB.getSelectedCharacter();
            if (selectedCharacter && window.KIMI_CHARACTERS) {
                kimiVideo.setCharacter(selectedCharacter);
                const folder = window.KIMI_CHARACTERS[selectedCharacter].videoFolder;
                const neutralVideo = `${folder}neutral/neutral-gentle-breathing.mp4`;
                const video1Source = video1.querySelector("source");
                if (video1Source) {
                    video1Source.setAttribute("src", neutralVideo);
                    video1.load();
                }
            }
            if (kimiVideo && kimiVideo.switchToContext) {
                kimiVideo.switchToContext("neutral");
            }
        } catch (e) {
            console.warn("Error loading initial video:", e);
        }
    }

    async function attachCharacterSection() {
        let saveCharacterBtn = window.KimiDOMUtils.get("#save-character-btn");
        if (saveCharacterBtn) {
            saveCharacterBtn.addEventListener("click", async e => {
                const settingsPanel = window.KimiDOMUtils.get(".settings-panel");
                let scrollTop = settingsPanel ? settingsPanel.scrollTop : null;
                const characterGrid = window.KimiDOMUtils.get("#character-grid");
                const selectedCard = characterGrid ? characterGrid.querySelector(".character-card.selected") : null;
                if (!selectedCard) return;
                const charKey = selectedCard.dataset.character;
                // Removed incorrect usage of the API key saved badge here.
                // Character save should not toggle the API key saved indicator.
                const promptInput = window.KimiDOMUtils.get(`#prompt-${charKey}`);
                const prompt = promptInput ? promptInput.value : "";

                await window.kimiDB.setSelectedCharacter(charKey);
                await window.kimiDB.setSystemPromptForCharacter(charKey, prompt);
                // Ensure memory system uses the correct character
                if (window.kimiMemorySystem) {
                    window.kimiMemorySystem.selectedCharacter = charKey;
                }
                if (window.kimiVideo && window.kimiVideo.setCharacter) {
                    window.kimiVideo.setCharacter(charKey);
                    if (window.kimiVideo.switchToContext) {
                        window.kimiVideo.switchToContext("neutral");
                    }
                }
                if (window.voiceManager && window.voiceManager.updateSelectedCharacter) {
                    await window.voiceManager.updateSelectedCharacter();
                }

                await window.loadCharacterSection();
                if (settingsPanel && scrollTop !== null) {
                    requestAnimationFrame(() => {
                        settingsPanel.scrollTop = scrollTop;
                    });
                }
                // Refresh memory tab after character selection
                if (window.kimiMemoryUI && typeof window.kimiMemoryUI.updateMemoryStats === "function") {
                    await window.kimiMemoryUI.updateMemoryStats();
                }
                saveCharacterBtn.setAttribute("data-i18n", "saved");
                saveCharacterBtn.classList.add("success");
                saveCharacterBtn.disabled = true;

                setTimeout(() => {
                    saveCharacterBtn.setAttribute("data-i18n", "save");
                    saveCharacterBtn.classList.remove("success");
                    saveCharacterBtn.disabled = false;
                }, 1500);
            });
        }
        let settingsButton2 = window.KimiDOMUtils.get("#settings-button");
        if (settingsButton2) {
            settingsButton2.addEventListener("click", window.loadCharacterSection);
        }
    }
    await attachCharacterSection();

    const chatContainer = document.getElementById("chat-container");
    const chatButton = document.getElementById("chat-button");
    const chatToggle = document.getElementById("chat-toggle");
    const chatMessages = document.getElementById("chat-messages");
    const chatInput = document.getElementById("chat-input");
    const sendButton = document.getElementById("send-button");
    const chatDelete = document.getElementById("chat-delete");
    const waitingIndicator = document.getElementById("waiting-indicator");

    if (!chatContainer || !chatButton || !chatMessages) {
        console.error("Critical chat elements missing from DOM");
        return;
    }

    window.kimiOverlayManager = new window.KimiOverlayManager();

    chatButton.addEventListener("click", () => {
        window.kimiOverlayManager.toggle("chat-container");
        if (window.kimiOverlayManager.isOpen("chat-container")) {
            window.loadChatHistory();
        }
    });

    if (chatToggle) {
        chatToggle.addEventListener("click", () => {
            window.kimiOverlayManager.close("chat-container");
        });
    }

    // Setup chat input and send button event listeners
    if (sendButton) {
        sendButton.addEventListener("click", () => {
            if (typeof window.sendMessage === "function") {
                window.sendMessage();
            } else {
                console.error("sendMessage function not available");
            }
        });
        console.log("Send button event listener attached");
    } else {
        console.error("Send button not found");
    }

    if (chatInput) {
        chatInput.addEventListener("keydown", e => {
            if (e.key === "Enter" && !e.shiftKey) {
                e.preventDefault();
                if (typeof window.sendMessage === "function") {
                    window.sendMessage();
                } else {
                    console.error("sendMessage function not available");
                }
            }
        });
        console.log("Chat input event listener attached");
    } else {
        console.error("Chat input not found");
    }

    const settingsOverlay = document.getElementById("settings-overlay");
    const settingsButton = document.getElementById("settings-button");
    const settingsClose = document.getElementById("settings-close");

    const helpOverlay = document.getElementById("help-overlay");
    const helpButton = document.getElementById("help-button");
    const helpClose = document.getElementById("help-close");

    if (!settingsButton || !helpButton) {
        console.error("Critical UI buttons missing from DOM");
        return;
    }

    helpButton.addEventListener("click", () => {
        window.kimiOverlayManager.open("help-overlay");
    });

    if (helpClose) {
        helpClose.addEventListener("click", () => {
            window.kimiOverlayManager.close("help-overlay");
        });
    }

    settingsButton.addEventListener("click", () => {
        window.kimiOverlayManager.open("settings-overlay");

        // Prevent multiple settings loading
        if (!window._settingsLoading) {
            window._settingsLoading = true;
            window.loadSettingsData();

            setTimeout(() => {
                window.updateTabsScrollIndicator();
                if (window.initializeAllSliders) window.initializeAllSliders();
                if (window.syncLLMMaxTokensSlider) window.syncLLMMaxTokensSlider();
                if (window.syncLLMTemperatureSlider) window.syncLLMTemperatureSlider();
                if (window.setupSettingsListeners) window.setupSettingsListeners(window.kimiDB, window.kimiMemory);
                if (window.syncPersonalityTraits) window.syncPersonalityTraits();
                if (window.ensureVideoContextConsistency) window.ensureVideoContextConsistency();

                // Only retry loading models if not already done
                if (window.loadAvailableModels && !loadAvailableModels._loading) {
                    setTimeout(() => window.loadAvailableModels(), 100);
                }

                window._settingsLoading = false;
            }, 200);
        }
    });

    if (settingsClose) {
        settingsClose.addEventListener("click", () => {
            window.kimiOverlayManager.close("settings-overlay");
        });
    }

    // Initialisation unifiΓ©e de la gestion des tabs
    window.kimiTabManager = new window.KimiTabManager({
        onTabChange: async tabName => {
            if (tabName === "personality") {
                await window.loadCharacterSection();
            }
        }
    });

    window.kimiUIEventManager = new window.KimiUIEventManager();
    window.kimiUIEventManager.addEvent(window, "resize", window.updateTabsScrollIndicator);

    window.kimiFormManager = new window.KimiFormManager({ db: window.kimiDB, memory: window.kimiMemory });

    const testVoiceButton = document.getElementById("test-voice");
    if (testVoiceButton) {
        testVoiceButton.addEventListener("click", () => {
            if (voiceManager) {
                const rate = parseFloat(document.getElementById("voice-rate").value);
                const pitch = parseFloat(document.getElementById("voice-pitch").value);
                const volume = parseFloat(document.getElementById("voice-volume").value);

                if (window.kimiMemory.preferences) {
                    window.kimiMemory.preferences.voiceRate = rate;
                    window.kimiMemory.preferences.voicePitch = pitch;
                    window.kimiMemory.preferences.voiceVolume = volume;
                }

                const testMessage =
                    window.kimiI18nManager?.t("voice_test_message") ||
                    "Hello my love! Here is my new voice configured with all the settings! Do you like it?";
                voiceManager.speak(testMessage, {
                    rate,
                    pitch,
                    volume
                });
            } else {
                console.warn("Voice manager not initialized");
            }
        });
    }

    const testApiButton = document.getElementById("test-api");
    if (testApiButton) {
        testApiButton.addEventListener("click", async () => {
            const statusSpan = ApiUi.statusSpan();
            const apiKeyInput = ApiUi.apiKeyInput();
            const apiKey = apiKeyInput ? apiKeyInput.value.trim() : "";
            const providerSelect = ApiUi.providerSelect();
            const baseUrlInput = ApiUi.baseUrlInput();
            const modelIdInput = ApiUi.modelIdInput();
            const provider = providerSelect ? providerSelect.value : "openrouter";
            const baseUrl = baseUrlInput ? baseUrlInput.value.trim() : "";
            const modelId = modelIdInput ? modelIdInput.value.trim() : "";

            if (!statusSpan) return;

            if (provider !== "ollama" && !apiKey) {
                statusSpan.textContent = window.kimiI18nManager?.t("api_key_missing") || "API key missing";
                statusSpan.style.color = "#ff6b6b";
                return;
            }

            // Validate API key format before saving/testing
            if (provider !== "ollama") {
                const isValid = (window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(apiKey)) || false;
                if (!isValid) {
                    statusSpan.textContent =
                        window.kimiI18nManager?.t("api_key_invalid_format") ||
                        "Invalid API key format (must start with sk-or-v1-)";
                    statusSpan.style.color = "#ff6b6b";
                    return;
                }
            }

            if (window.kimiDB) {
                // Save API key under provider-specific preference key (skip for Ollama)
                if (provider !== "ollama") {
                    const keyPref = window.KimiProviderUtils
                        ? window.KimiProviderUtils.getKeyPrefForProvider(provider)
                        : "providerApiKey";
                    await window.kimiDB.setPreference(keyPref, apiKey);
                }
                await window.kimiDB.setPreference("llmProvider", provider);
                if (baseUrl) await window.kimiDB.setPreference("llmBaseUrl", baseUrl);
                if (modelId) await window.kimiDB.setPreference("llmModelId", modelId);
            }
            statusSpan.textContent = "Testing in progress...";
            statusSpan.style.color = "#ffa726";

            try {
                if (window.kimiLLM) {
                    // Test API minimal et centralisΓ© pour tous les providers
                    const result = await window.kimiLLM.testApiKeyMinimal(modelId);
                    if (result.success) {
                        statusSpan.textContent = "Connection successful!";
                        statusSpan.style.color = "#4caf50";
                        // Only show saved badge if an actual non-empty API key is stored and provider requires one
                        const savedBadge = ApiUi.savedBadge();
                        if (savedBadge) {
                            const apiKeyInputEl = ApiUi.apiKeyInput();
                            const hasKey = apiKeyInputEl && apiKeyInputEl.value.trim().length > 0;
                            if (provider !== "ollama" && hasKey) {
                                savedBadge.textContent =
                                    (window.kimiI18nManager && window.kimiI18nManager.t("saved_short")) || "Saved";
                                savedBadge.style.display = "inline";
                            } else {
                                savedBadge.style.display = "none";
                            }
                        }

                        if (result.response) {
                            setTimeout(() => {
                                statusSpan.textContent = `Test response: \"${result.response.substring(0, 50)}...\"`;
                            }, 1000);
                        }
                        // Mark test success explicitly
                        ApiUi.setTestPresence("#4caf50");
                    } else {
                        statusSpan.textContent = `${result.error}`;
                        statusSpan.style.color = "#ff6b6b";
                        ApiUi.setTestPresence("#9e9e9e");
                        if (result.error.includes("similaires disponibles")) {
                            setTimeout(() => {}, 1000);
                        }
                    }
                } else {
                    statusSpan.textContent = "LLM manager not initialized";
                    statusSpan.style.color = "#ff6b6b";
                    ApiUi.setTestPresence("#9e9e9e");
                }
            } catch (error) {
                console.error("Error while testing API:", error);
                statusSpan.textContent = `Error: ${error.message}`;
                statusSpan.style.color = "#ff6b6b";
                ApiUi.setTestPresence("#9e9e9e");

                if (error.message.includes("non disponible")) {
                    setTimeout(() => {}, 1000);
                }
            }
        });
    }

    // Global, single handler for API key input to save and update presence in real-time
    (function setupApiKeyInputHandler() {
        const input = ApiUi.apiKeyInput();
        if (!input) return;
        let t;
        input.addEventListener("input", () => {
            clearTimeout(t);
            t = setTimeout(async () => {
                const providerEl = ApiUi.providerSelect();
                const provider = providerEl ? providerEl.value : "openrouter";
                const keyPref = window.KimiProviderUtils
                    ? window.KimiProviderUtils.getKeyPrefForProvider(provider)
                    : "providerApiKey";
                const value = input.value.trim();
                // Update Test button state immediately
                const validNow = !!(window.KIMI_VALIDATORS && window.KIMI_VALIDATORS.validateApiKey(value));
                ApiUi.setTestEnabled(provider === "ollama" ? true : validNow);
                if (window.kimiDB) {
                    try {
                        await window.kimiDB.setPreference(keyPref, value);
                        const savedBadge = ApiUi.savedBadge();
                        if (savedBadge) {
                            if (value) {
                                savedBadge.textContent =
                                    (window.kimiI18nManager && window.kimiI18nManager.t("saved_short")) || "Saved";
                                savedBadge.style.display = "inline";
                            } else {
                                savedBadge.style.display = "none";
                            }
                        }
                        ApiUi.setPresence(value ? "#4caf50" : "#9e9e9e");
                        // Any key change invalidates previous test state
                        ApiUi.setTestPresence("#9e9e9e");
                        ApiUi.clearStatus();
                    } catch (e) {
                        // Validation error from DB
                        const s = ApiUi.statusSpan();
                        if (s) {
                            s.textContent = e?.message || "Invalid API key";
                            s.style.color = "#ff6b6b";
                        }
                        ApiUi.setTestEnabled(false);
                        ApiUi.setTestPresence("#9e9e9e");
                    }
                }
            }, window.KIMI_SECURITY_CONFIG?.DEBOUNCE_DELAY || 300);
        });
    })();

    // Toggle show/hide for API key
    (function setupToggleEye() {
        const btn = ApiUi.toggleBtn();
        const input = ApiUi.apiKeyInput();
        if (!btn || !input) return;
        btn.addEventListener("click", () => {
            const showing = input.type === "text";
            input.type = showing ? "password" : "text";
            btn.setAttribute("aria-pressed", String(!showing));
            const icon = btn.querySelector("i");
            if (icon) {
                icon.classList.toggle("fa-eye");
                icon.classList.toggle("fa-eye-slash");
            }
            btn.setAttribute("aria-label", showing ? "Show API key" : "Hide API key");
        });
    })();

    kimiInit.register(
        "appearanceManager",
        async () => {
            const manager = new KimiAppearanceManager(window.kimiDB);
            await manager.init();
            window.kimiAppearanceManager = manager;
            return manager;
        },
        [],
        500
    );
    kimiInit.register(
        "dataManager",
        async () => {
            const manager = new KimiDataManager(window.kimiDB);
            await manager.init();
            window.kimiDataManager = manager;
            return manager;
        },
        [],
        600
    );

    kimiInit.register(
        "voiceManager",
        async () => {
            if (window.KimiVoiceManager) {
                const manager = new KimiVoiceManager(window.kimiDB, window.kimiMemory);
                const success = await manager.init();
                if (success) {
                    manager.setOnSpeechAnalysis(window.analyzeAndReact);
                    return manager;
                }
            }
            return null;
        },
        [],
        1000
    );

    try {
        await kimiInit.initializeAll();
        window.voiceManager = kimiInit.getInstance("voiceManager");
        window.kimiMemory.updateFavorabilityBar();
    } catch (error) {
        console.error("Initialization error:", error);
    }

    // Setup unified event handlers to prevent duplicates
    setupUnifiedEventHandlers();

    // Initialize language and UI
    await initializeLanguageAndUI();

    // Setup message handling
    setupMessageHandling();

    // Function definitions
    function setupUnifiedEventHandlers() {
        // SIMPLE FIX: Initialize _kimiEventCleanup to prevent undefined error
        if (!window._kimiEventCleanup) {
            window._kimiEventCleanup = [];
        }

        // Helper function to safely add event listeners
        function safeAddEventListener(element, event, handler, identifier) {
            if (element && !element[identifier]) {
                element.addEventListener(event, handler);
                element[identifier] = true;

                // Simple cleanup system
                const cleanupFn = () => {
                    element.removeEventListener(event, handler);
                    element[identifier] = false;
                };

                // Store cleanup function in the simple array
                window._kimiEventCleanup.push(cleanupFn);
            }
        }

        // Chat event handlers
        const chatDelete = document.getElementById("chat-delete");
        if (chatDelete) {
            const handler = async () => {
                if (confirm("Do you really want to delete all chat messages? This cannot be undone.")) {
                    const chatMessages = document.getElementById("chat-messages");
                    if (chatMessages) {
                        chatMessages.textContent = "";
                    }
                    if (window.kimiDB && window.kimiDB.db) {
                        try {
                            await window.kimiDB.db.conversations.clear();
                        } catch (error) {
                            console.error("Error deleting conversations:", error);
                        }
                    }
                }
            };
            safeAddEventListener(chatDelete, "click", handler, "_kimiChatDeleteHandlerAttached");
        }
    }

    async function initializeLanguageAndUI() {
        // Language initialization
        window.kimiI18nManager = new window.KimiI18nManager();
        const lang = await kimiDB.getPreference("selectedLanguage", "en");
        await window.kimiI18nManager.setLanguage(lang);
        // Note: Language selector event listener is now handled by VoiceManager.setupLanguageSelector()
        // This prevents duplicate event listeners and ensures proper coordination between voice and i18n systems

        window.kimiUIStateManager = new window.KimiUIStateManager();
    }

    function setupMessageHandling() {
        // Chat event handlers are already attached in the main script
        // No need to reattach them here to avoid duplicates
    }

    // ==== BATCHED EVENT AGGREGATOR (personality + preferences) ====
    const batchedUpdates = {
        personality: null,
        preferences: new Set()
    };
    let batchTimer = null;

    function scheduleFlush() {
        if (batchTimer) return;
        batchTimer = setTimeout(flushBatchedUpdates, 100); // 100ms coalescing window
    }

    async function flushBatchedUpdates() {
        const personalityPayload = batchedUpdates.personality;
        const prefKeys = Array.from(batchedUpdates.preferences);
        batchedUpdates.personality = null;
        batchedUpdates.preferences.clear();
        batchTimer = null;

        // Apply personality update once (last-wins)
        if (personalityPayload) {
            const { character, traits } = personalityPayload;
            const defaults = (window.getTraitDefaults && window.getTraitDefaults()) || {
                affection: 55, // Lowered to match emotion system defaults
                romance: 50,
                empathy: 75,
                playfulness: 55,
                humor: 60,
                intelligence: 70
            };

            // Prefer persisted DB traits over defaults to avoid temporary inconsistencies.
            let dbTraits = null;
            try {
                if (window.kimiDB && typeof window.kimiDB.getAllPersonalityTraits === "function") {
                    dbTraits = await window.kimiDB.getAllPersonalityTraits(character || null);
                }
            } catch (e) {
                dbTraits = null;
            }

            const baseline = { ...defaults, ...(dbTraits || {}) };
            const safeTraits = {};
            for (const key of Object.keys(defaults)) {
                // If incoming payload provides the key, use it; otherwise use baseline (DB -> defaults)
                let raw = Object.prototype.hasOwnProperty.call(traits || {}, key) ? traits[key] : baseline[key];
                let v = Number(raw);
                if (!isFinite(v) || isNaN(v)) v = Number(baseline[key]);
                v = Math.max(0, Math.min(100, v));
                safeTraits[key] = v;
            }
            if (window.KIMI_DEBUG_SYNC) {
                console.log(`🧠 (Batched) Personality updated for ${character}:`, safeTraits);
            }
            // Centralize side-effects elsewhere; aggregator remains a coalesced logger only.
        }

        // Preference keys batch (currently UI refresh for sliders already handled elsewhere)
        if (prefKeys.length > 0) {
            // Potential future hook: log or perform aggregated operations
            // console.log("βš™οΈ Batched preference keys:", prefKeys);
        }
    }

    // Also listen to the DB-wrapped event name to preserve batched logging
    window.addEventListener("personality:updated", event => {
        batchedUpdates.personality = event.detail; // last event wins
        scheduleFlush();
    });
    window.addEventListener("preferenceUpdated", event => {
        if (event.detail?.key) batchedUpdates.preferences.add(event.detail.key);
        scheduleFlush();
    });

    // Add global keyboard event listener for microphone toggle (F8)
    let f8KeyPressed = false;

    document.addEventListener("keydown", function (event) {
        // Check if F8 key is pressed and no input field is focused
        if (event.key === "F8" && !f8KeyPressed) {
            f8KeyPressed = true;
            const activeElement = document.activeElement;
            const isInputFocused =
                activeElement &&
                (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA" || activeElement.isContentEditable);

            // Only trigger if no input field is focused
            if (!isInputFocused && window.voiceManager && window.voiceManager.toggleMicrophone) {
                event.preventDefault();
                window.voiceManager.toggleMicrophone();
            }
        }
    });

    // Refresh sliders when character or language preference changes
    window.addEventListener("preferenceUpdated", evt => {
        const k = evt.detail?.key;
        if (!k) return;
        if (k === "selectedCharacter" || k === "selectedLanguage") {
            if (window.refreshAllSliders) {
                setTimeout(() => window.refreshAllSliders(), 50);
            }
        }
    });

    document.addEventListener("keyup", function (event) {
        if (event.key === "F8") {
            f8KeyPressed = false;
        }
    });

    // Monitor for consistency and errors
    setInterval(async () => {
        if (window.ensureVideoContextConsistency) {
            await window.ensureVideoContextConsistency();
        }
    }, 30000); // Check every 30 seconds

    // Personality sync: global event and wrappers
    (function setupPersonalitySync() {
        // Guard to avoid multiple initializations
        if (window._kimiPersonalitySyncReady) return;
        window._kimiPersonalitySyncReady = true;

        const dispatchUpdated = async (partialTraits, characterHint = null) => {
            try {
                const character = characterHint || (window.kimiDB && (await window.kimiDB.getSelectedCharacter())) || null;
                window.dispatchEvent(
                    new CustomEvent("personality:updated", {
                        detail: { character, traits: { ...partialTraits } }
                    })
                );
            } catch (e) {}
        };

        const tryWrapDB = () => {
            const db = window.kimiDB;
            if (!db) return false;

            const wrapOnce = (obj, methodName, buildTraitsFromArgs) => {
                if (!obj || typeof obj[methodName] !== "function") return;
                if (obj[methodName]._kimiWrapped) return;
                const original = obj[methodName].bind(obj);
                obj[methodName] = async function (...args) {
                    const res = await original(...args);
                    try {
                        const { traits, character } = await buildTraitsFromArgs(args, res);
                        if (traits && Object.keys(traits).length > 0) {
                            await dispatchUpdated(traits, character);
                        }
                    } catch (e) {}
                    return res;
                };
                obj[methodName]._kimiWrapped = true;
            };

            // setPersonalityTrait(trait, value, character?)
            wrapOnce(db, "setPersonalityTrait", async args => {
                const [trait, value, character] = args;
                return { traits: { [String(trait)]: Number(value) }, character: character || null };
            });

            // setPersonalityBatch(traitsObj, character?)
            wrapOnce(db, "setPersonalityBatch", async args => {
                const [traitsObj, character] = args;
                const traits = {};
                if (traitsObj && typeof traitsObj === "object") {
                    for (const [k, v] of Object.entries(traitsObj)) {
                        traits[String(k)] = Number(v);
                    }
                }
                return { traits, character: character || null };
            });

            // savePersonality(personalityObj, character?)
            wrapOnce(db, "savePersonality", async args => {
                const [personalityObj, character] = args;
                const traits = {};
                if (personalityObj && typeof personalityObj === "object") {
                    for (const [k, v] of Object.entries(personalityObj)) {
                        traits[String(k)] = Number(v);
                    }
                }
                return { traits, character: character || null };
            });

            return true;
        };

        // Try immediately and then retry a few times if DB not yet ready
        if (!tryWrapDB()) {
            let attempts = 0;
            const maxAttempts = 20;
            const interval = setInterval(() => {
                attempts++;
                if (tryWrapDB() || attempts >= maxAttempts) {
                    clearInterval(interval);
                }
            }, 250);
        }

        // Central listener: debounce UI/video sync to avoid thrashing
        let syncTimer = null;
        let lastTraits = {};
        window.addEventListener("personality:updated", async e => {
            try {
                if (e && e.detail && e.detail.traits) {
                    // Merge incremental updates
                    lastTraits = { ...lastTraits, ...e.detail.traits };
                }
            } catch {}

            if (syncTimer) clearTimeout(syncTimer);
            syncTimer = setTimeout(async () => {
                try {
                    const db = window.kimiDB;
                    const character = (e && e.detail && e.detail.character) || (db && (await db.getSelectedCharacter())) || null;
                    let traits = lastTraits;
                    if (!traits || Object.keys(traits).length === 0) {
                        // Fallback: fetch all traits if partial not provided
                        traits = db && (await db.getAllPersonalityTraits(character));
                    }

                    // 1) Update UI sliders if available
                    if (typeof window.updateSlider === "function" && traits) {
                        for (const [trait, value] of Object.entries(traits)) {
                            const id = `trait-${trait}`;
                            if (document.getElementById(id)) {
                                try {
                                    window.updateSlider(id, value);
                                } catch {}
                            }
                        }
                    }
                    if (typeof window.syncPersonalityTraits === "function") {
                        try {
                            await window.syncPersonalityTraits(character);
                        } catch {}
                    }

                    // 2) Update memory cache affection bar if available
                    if (window.kimiMemory && typeof window.kimiMemory.updateAffectionTrait === "function") {
                        try {
                            await window.kimiMemory.updateAffectionTrait();
                        } catch {}
                    }

                    // 3) Update video mood by personality
                    if (window.kimiVideo && typeof window.kimiVideo.setMoodByPersonality === "function") {
                        const allTraits =
                            traits && Object.keys(traits).length > 0
                                ? { ...traits }
                                : (db && (await db.getAllPersonalityTraits(character))) || {};
                        try {
                            window.kimiVideo.setMoodByPersonality(allTraits);
                        } catch {}
                        // 3b) Update voice modulation based on personality
                        try {
                            if (window.voiceManager && typeof window.voiceManager.updatePersonalityModulation === "function") {
                                window.voiceManager.updatePersonalityModulation(allTraits);
                            }
                        } catch {}
                    }

                    // 4) Ensure current video context is valid (lightweight guard)
                    let beforeInfo = null;
                    try {
                        if (window.kimiVideo && typeof window.kimiVideo.getCurrentVideoInfo === "function") {
                            beforeInfo = window.kimiVideo.getCurrentVideoInfo();
                        }
                    } catch {}

                    if (typeof window.ensureVideoContextConsistency === "function") {
                        try {
                            await window.ensureVideoContextConsistency();
                        } catch {}
                    }

                    try {
                        if (
                            window.KIMI_DEBUG_SYNC &&
                            window.kimiVideo &&
                            typeof window.kimiVideo.getCurrentVideoInfo === "function"
                        ) {
                            const afterInfo = window.kimiVideo.getCurrentVideoInfo();
                            if (
                                beforeInfo &&
                                afterInfo &&
                                (beforeInfo.context !== afterInfo.context ||
                                    beforeInfo.emotion !== afterInfo.emotion ||
                                    beforeInfo.category !== afterInfo.category)
                            ) {
                                console.log("πŸ”§ SyncGuard: corrected video context", { from: beforeInfo, to: afterInfo });
                            }
                        }
                    } catch {}
                } catch {
                } finally {
                    lastTraits = {};
                }
            }, 120); // small debounce
        });
    })();
});