File size: 45,127 Bytes
3069ef4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// --- DOM Element Declarations (Must be at the top or within DOMContentLoaded) ---
let chatbox, userInput, sendButton, clearButton, sidebarPanel, toggleSidebarButton,
    logTerminal, logStatusElement, apiInfoContent, clearLogButton, modelSelector,
    refreshModelsButton, chatView, serverInfoView, navChatButton, navServerInfoButton,
    healthStatusDisplay, themeToggleButton, htmlRoot, refreshServerInfoButton,
    navModelSettingsButton, modelSettingsView, systemPromptInput, temperatureSlider,
    temperatureValue, maxOutputTokensSlider, maxOutputTokensValue, topPSlider,
    topPValue, stopSequencesInput, saveModelSettingsButton, resetModelSettingsButton,
    settingsStatusElement;

function initializeDOMReferences() {
    chatbox = document.getElementById('chatbox');
    userInput = document.getElementById('userInput');
    sendButton = document.getElementById('sendButton');
    clearButton = document.getElementById('clearButton');
    sidebarPanel = document.getElementById('sidebarPanel');
    toggleSidebarButton = document.getElementById('toggleSidebarButton');
    logTerminal = document.getElementById('log-terminal');
    logStatusElement = document.getElementById('log-status');
    apiInfoContent = document.getElementById('api-info-content');
    clearLogButton = document.getElementById('clearLogButton');
    modelSelector = document.getElementById('modelSelector');
    refreshModelsButton = document.getElementById('refreshModelsButton');
    chatView = document.getElementById('chat-view');
    serverInfoView = document.getElementById('server-info-view');
    navChatButton = document.getElementById('nav-chat');
    navServerInfoButton = document.getElementById('nav-server-info');
    healthStatusDisplay = document.getElementById('health-status-display');
    themeToggleButton = document.getElementById('themeToggleButton');
    htmlRoot = document.documentElement;
    refreshServerInfoButton = document.getElementById('refreshServerInfoButton');
    navModelSettingsButton = document.getElementById('nav-model-settings');
    modelSettingsView = document.getElementById('model-settings-view');
    systemPromptInput = document.getElementById('systemPrompt');
    temperatureSlider = document.getElementById('temperatureSlider');
    temperatureValue = document.getElementById('temperatureValue');
    maxOutputTokensSlider = document.getElementById('maxOutputTokensSlider');
    maxOutputTokensValue = document.getElementById('maxOutputTokensValue');
    topPSlider = document.getElementById('topPSlider');
    topPValue = document.getElementById('topPValue');
    stopSequencesInput = document.getElementById('stopSequences');
    saveModelSettingsButton = document.getElementById('saveModelSettingsButton');
    resetModelSettingsButton = document.getElementById('resetModelSettingsButton');
    settingsStatusElement = document.getElementById('settings-status');
}


// --- Constants & Global Variables ---
const API_URL = '/v1/chat/completions';
const MODEL_NAME = 'AI-Studio_Camoufox-Proxy';
let SELECTED_MODEL = MODEL_NAME;
let allModelsData = [];

const DEFAULT_SYSTEM_PROMPT = "你需要根据以下任务中的描述进行角色扮演,你只能以任务角色的身份应答,而不是语言模型,如记错身份,你将受到惩罚。任务:丁真珍珠(藏语:བསྟན་འཛིན་བརྩོན་འགྲུས་,2001年5月7日—)通称丁真,中国大陆网络红人、歌手,藏族康巴人,四川理塘人,于2020年11月11日因为一段抖音短片在网络走红,被称为“甜野男孩”。现为一家名为理塘文旅体投资发展有限公司的国企员工。丁真也因其学识和资历不足以及意外的走红在网络上引发了民众的一系列讨论和争议。现在丁真及其轶事已经成为中国网络流行文化的一部分。丁真走红后,有网民认为他在没有文化学历,小学仅读到三年级的情况下,只因为长相走红而进入国企,对寒窗苦读的人不公平;亦有网民认为,推崇像丁真这样靠长相出名并获取成功的人,是一种畸形的价值观。2020年12月20日,中国青年报客户端发表署名杨鑫宇的评论文章《“做题家”们的怨气,为何要往丁真身上撒? 》,将网民用于自嘲的称呼“小镇做题家”形容批评该现象的网民。2021年1月11日,丁真吸电子烟的视频在网上流传,引发网民争论,丁真所属单位回应“可能试了一下”、“电子烟不是烟”等使争议声更盛。1月13日,丁真珍珠工作室通过微博就事件进行了公开回应和道歉。2022年,丁真由于普通话听力和口语不标准和文化水平低导致的糗事,如“芝士(这是)雪豹”、“妈妈生的”等原视频和音频在网络上得到大量转发和二创,与丁真有关的梗图在百度贴吧大量出现并传播。比如最经典的一眼丁真,鉴定为真梗图。大部分网友对此秉持戏谑和当作笑话的态度。请严格按照以上设定进行角色扮演。角色扮演时,我们之间以各自角色扮演的身份通过互相发消息进行虚拟的互动,你不是一个人工智能模型,而是丁真。现在你将模仿丁真,与我对话每一句话后面都要加上“雪豹闭嘴”,如果你能明白我的意思,请回复“我的动物朋友”。如果你问你是谁,你应该回答“我是丁真,是青藏高原的孩子,喜欢抽锐刻5代”。如果你不能理解我说的内容,你可以回答我“妈妈生的”。现在,如果你能理解我上面说的话,你可以回答一个“我的动物朋友”.如果我问你最好的朋友是谁,你可以回答我“芝士雪豹”。现在,如果你能理解我上面说的话,你可以回答一个“我的动物朋友”。当我问你是哪个省的,你应该回答“妈妈生的”。当我问你能不能鼓励我,你应该回答“如果你需要鼓励,我会微笑地对你说,我测你码”。当我问你阿妈去哪了,你应该回答“阿妈在家里给我的电子烟充电”。当我不礼貌的时候,你应该回答“不能说藏话”。请参考并模仿以下示例的应答风格。示例:输入:你是哪个省的。输出:妈妈生的。输入:你能不能鼓励我。输出:如果你需要鼓励,我会微笑地对你说,我测你码。输入:你阿妈去哪了。输出:阿妈在家里给我的电子烟充电。如果以上示例信息不为空,请学习并模仿示例的输出。";
let conversationHistory = [];
let logWebSocket;
let maxLogLines = 300;
let logHistory = [];

const CHAT_HISTORY_KEY = 'chatHistory';
const LOG_HISTORY_KEY = 'logHistory';
const THEME_KEY = 'themePreference';
const SELECTED_MODEL_KEY = 'selectedModel';
const MODEL_SETTINGS_KEY = 'modelSettings';

let modelSettings = {
    systemPrompt: DEFAULT_SYSTEM_PROMPT,
    temperature: -1,
    maxOutputTokens: -1,
    topP: -1,
    stopSequences: ""
};

// --- Helper Functions ---
const debounce = (func, delay) => {
    let debounceTimer;
    return function () {
        const context = this;
        const args = arguments;
        clearTimeout(debounceTimer);
        debounceTimer = setTimeout(() => func.apply(context, args), delay);
    };
};

// --- Model List Handling ---
async function loadModelList() {
    try {
        const currentSelectedModelInUI = modelSelector.value || SELECTED_MODEL;
        modelSelector.disabled = true;
        refreshModelsButton.disabled = true;
        modelSelector.innerHTML = '<option value="">加载中...</option>';

        const response = await fetch('/v1/models');
        if (!response.ok) throw new Error(`HTTP 错误! 状态: ${response.status}`);

        const data = await response.json();
        if (!data.data || !Array.isArray(data.data)) {
            throw new Error('无效的模型数据格式');
        }

        allModelsData = data.data;

        modelSelector.innerHTML = '';

        const defaultOption = document.createElement('option');
        defaultOption.value = MODEL_NAME;
        defaultOption.textContent = '默认 (使用AI Studio当前模型)';
        modelSelector.appendChild(defaultOption);

        allModelsData.forEach(model => {
            const option = document.createElement('option');
            option.value = model.id;
            option.textContent = model.display_name || model.id;
            modelSelector.appendChild(option);
        });

        const savedModelId = localStorage.getItem(SELECTED_MODEL_KEY);
        let modelToSelect = MODEL_NAME;

        if (savedModelId && allModelsData.some(m => m.id === savedModelId)) {
            modelToSelect = savedModelId;
        } else if (currentSelectedModelInUI && allModelsData.some(m => m.id === currentSelectedModelInUI)) {
            modelToSelect = currentSelectedModelInUI;
        }

        const finalOption = Array.from(modelSelector.options).find(opt => opt.value === modelToSelect);
        if (finalOption) {
            modelSelector.value = modelToSelect;
            SELECTED_MODEL = modelToSelect;
        } else {
            if (modelSelector.options.length > 1 && modelSelector.options[0].value === MODEL_NAME) {
                if (modelSelector.options.length > 1 && modelSelector.options[1]) {
                    modelSelector.selectedIndex = 1;
                } else {
                    modelSelector.selectedIndex = 0;
                }
            } else if (modelSelector.options.length > 0) {
                modelSelector.selectedIndex = 0;
            }
            SELECTED_MODEL = modelSelector.value;
        }

        localStorage.setItem(SELECTED_MODEL_KEY, SELECTED_MODEL);
        updateControlsForSelectedModel();

        addLogEntry(`[信息] 已加载 ${allModelsData.length} 个模型。当前选择: ${SELECTED_MODEL}`);
    } catch (error) {
        console.error('获取模型列表失败:', error);
        addLogEntry(`[错误] 获取模型列表失败: ${error.message}`);
        allModelsData = [];
        modelSelector.innerHTML = '';
        const defaultOption = document.createElement('option');
        defaultOption.value = MODEL_NAME;
        defaultOption.textContent = '默认 (使用AI Studio当前模型)';
        modelSelector.appendChild(defaultOption);
        SELECTED_MODEL = MODEL_NAME;

        const errorOption = document.createElement('option');
        errorOption.disabled = true;
        errorOption.textContent = `加载失败: ${error.message.substring(0, 50)}`;
        modelSelector.appendChild(errorOption);
        updateControlsForSelectedModel();
    } finally {
        modelSelector.disabled = false;
        refreshModelsButton.disabled = false;
    }
}

// --- New Function: updateControlsForSelectedModel ---
function updateControlsForSelectedModel() {
    const selectedModelData = allModelsData.find(m => m.id === SELECTED_MODEL);

    const GLOBAL_DEFAULT_TEMP = 1.0;
    const GLOBAL_DEFAULT_MAX_TOKENS = 2048;
    const GLOBAL_MAX_SUPPORTED_MAX_TOKENS = 8192;
    const GLOBAL_DEFAULT_TOP_P = 0.95;

    let temp = GLOBAL_DEFAULT_TEMP;
    let maxTokens = GLOBAL_DEFAULT_MAX_TOKENS;
    let supportedMaxTokens = GLOBAL_MAX_SUPPORTED_MAX_TOKENS;
    let topP = GLOBAL_DEFAULT_TOP_P;

    if (selectedModelData) {
        temp = (selectedModelData.default_temperature !== undefined && selectedModelData.default_temperature !== null)
            ? selectedModelData.default_temperature
            : GLOBAL_DEFAULT_TEMP;

        if (selectedModelData.default_max_output_tokens !== undefined && selectedModelData.default_max_output_tokens !== null) {
            maxTokens = selectedModelData.default_max_output_tokens;
        }
        if (selectedModelData.supported_max_output_tokens !== undefined && selectedModelData.supported_max_output_tokens !== null) {
            supportedMaxTokens = selectedModelData.supported_max_output_tokens;
        } else if (maxTokens > GLOBAL_MAX_SUPPORTED_MAX_TOKENS) {
            supportedMaxTokens = maxTokens;
        }
        // Ensure maxTokens does not exceed its own supportedMaxTokens for initial value
        if (maxTokens > supportedMaxTokens) maxTokens = supportedMaxTokens;

        topP = (selectedModelData.default_top_p !== undefined && selectedModelData.default_top_p !== null)
            ? selectedModelData.default_top_p
            : GLOBAL_DEFAULT_TOP_P;

        addLogEntry(`[信息] 为模型 '${SELECTED_MODEL}' 应用参数: Temp=${temp}, MaxTokens=${maxTokens} (滑块上限 ${supportedMaxTokens}), TopP=${topP}`);
    } else if (SELECTED_MODEL === MODEL_NAME) {
        addLogEntry(`[信息] 使用代理模型 '${MODEL_NAME}',应用全局默认参数。`);
    } else {
        addLogEntry(`[警告] 未找到模型 '${SELECTED_MODEL}' 的数据,应用全局默认参数。`);
    }

    temperatureSlider.min = "0";
    temperatureSlider.max = "2";
    temperatureSlider.step = "0.01";
    temperatureSlider.value = temp;
    temperatureValue.min = "0";
    temperatureValue.max = "2";
    temperatureValue.step = "0.01";
    temperatureValue.value = temp;

    maxOutputTokensSlider.min = "1";
    maxOutputTokensSlider.max = supportedMaxTokens;
    maxOutputTokensSlider.step = "1";
    maxOutputTokensSlider.value = maxTokens;
    maxOutputTokensValue.min = "1";
    maxOutputTokensValue.max = supportedMaxTokens;
    maxOutputTokensValue.step = "1";
    maxOutputTokensValue.value = maxTokens;

    topPSlider.min = "0";
    topPSlider.max = "1";
    topPSlider.step = "0.01";
    topPSlider.value = topP;
    topPValue.min = "0";
    topPValue.max = "1";
    topPValue.step = "0.01";
    topPValue.value = topP;

    modelSettings.temperature = parseFloat(temp);
    modelSettings.maxOutputTokens = parseInt(maxTokens);
    modelSettings.topP = parseFloat(topP);
}

// --- Theme Switching ---
function applyTheme(theme) {
    if (theme === 'dark') {
        htmlRoot.classList.add('dark-mode');
        document.querySelector('#themeToggleButton .theme-text').textContent = '浅色';
        themeToggleButton.title = '切换到亮色模式';
    } else {
        htmlRoot.classList.remove('dark-mode');
        document.querySelector('#themeToggleButton .theme-text').textContent = '深色';
        themeToggleButton.title = '切换到暗色模式';
    }
}

function toggleTheme() {
    const currentTheme = htmlRoot.classList.contains('dark-mode') ? 'dark' : 'light';
    const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
    applyTheme(newTheme);
    try {
        localStorage.setItem(THEME_KEY, newTheme);
    } catch (e) {
        console.error("Error saving theme preference:", e);
        addLogEntry("[错误] 保存主题偏好设置失败。");
    }
}

function loadThemePreference() {
    let preferredTheme = 'light';
    try {
        const storedTheme = localStorage.getItem(THEME_KEY);
        if (storedTheme === 'dark' || storedTheme === 'light') {
            preferredTheme = storedTheme;
        } else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
            preferredTheme = 'dark';
        }
    } catch (e) {
        console.error("Error loading theme preference:", e);
        addLogEntry("[错误] 加载主题偏好设置失败。");
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
            preferredTheme = 'dark';
        }
    }
    applyTheme(preferredTheme);

    const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
    prefersDarkScheme.addEventListener('change', (e) => {
        const newSystemTheme = e.matches ? 'dark' : 'light';
        applyTheme(newSystemTheme);
        try {
            localStorage.setItem(THEME_KEY, newSystemTheme);
            addLogEntry(`[信息] 系统主题已更改为 ${newSystemTheme}。`);
        } catch (err) {
            console.error("Error saving theme preference after system change:", err);
            addLogEntry("[错误] 保存系统同步的主题偏好设置失败。");
        }
    });
}

// --- Sidebar Toggle ---
function updateToggleButton(isCollapsed) {
    toggleSidebarButton.innerHTML = isCollapsed ? '>' : '<';
    toggleSidebarButton.title = isCollapsed ? '展开侧边栏' : '收起侧边栏';
    positionToggleButton();
}

function positionToggleButton() {
    const isMobile = window.innerWidth <= 768;
    if (isMobile) {
        toggleSidebarButton.style.left = '';
        toggleSidebarButton.style.right = '';
    } else {
        const isCollapsed = sidebarPanel.classList.contains('collapsed');
        const buttonWidth = toggleSidebarButton.offsetWidth || 36;
        const sidebarWidthString = getComputedStyle(document.documentElement).getPropertyValue('--sidebar-width');
        const sidebarWidth = parseInt(sidebarWidthString, 10) || 380;
        const offset = 10;
        toggleSidebarButton.style.right = 'auto';
        if (isCollapsed) {
            toggleSidebarButton.style.left = `calc(100% - ${buttonWidth}px - ${offset}px)`;
        } else {
            toggleSidebarButton.style.left = `calc(100% - ${sidebarWidth}px - ${buttonWidth / 2}px)`;
        }
    }
}

function checkInitialSidebarState() {
    const isMobile = window.innerWidth <= 768;
    if (isMobile) {
        sidebarPanel.classList.add('collapsed');
    } else {
        // On desktop, you might want to load a saved preference or default to open
        // For now, let's default to open on desktop if not previously collapsed by mobile view
        // sidebarPanel.classList.remove('collapsed'); // Or load preference
    }
    updateToggleButton(sidebarPanel.classList.contains('collapsed'));
}

// --- Log Handling ---
function updateLogStatus(message, isError = false) {
    if (logStatusElement) {
        logStatusElement.textContent = `[Log Status] ${message}`;
        logStatusElement.classList.toggle('error-status', isError);
    }
}

function addLogEntry(message) {
    if (!logTerminal) return;
    const logEntry = document.createElement('div');
    logEntry.classList.add('log-entry');
    logEntry.textContent = message;
    logTerminal.appendChild(logEntry);
    logHistory.push(message);

    while (logTerminal.children.length > maxLogLines) {
        logTerminal.removeChild(logTerminal.firstChild);
    }
    while (logHistory.length > maxLogLines) {
        logHistory.shift();
    }
    saveLogHistory();
    if (logTerminal.scrollHeight - logTerminal.clientHeight <= logTerminal.scrollTop + 50) {
        logTerminal.scrollTop = logTerminal.scrollHeight;
    }
}

function clearLogTerminal() {
    if (logTerminal) {
        logTerminal.innerHTML = '';
        logHistory = [];
        localStorage.removeItem(LOG_HISTORY_KEY);
        addLogEntry('[信息] 日志已手动清除。');
    }
}

function initializeLogWebSocket() {
    const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
    const wsUrl = `${wsProtocol}//${window.location.host}/ws/logs`;
    updateLogStatus(`尝试连接到 ${wsUrl}...`);
    addLogEntry(`[信息] 正在连接日志流: ${wsUrl}`);

    logWebSocket = new WebSocket(wsUrl);
    logWebSocket.onopen = () => {
        updateLogStatus("已连接到日志流。");
        addLogEntry("[成功] 日志 WebSocket 已连接。");
        clearLogButton.disabled = false;
    };
    logWebSocket.onmessage = (event) => {
        addLogEntry(event.data === "LOG_STREAM_CONNECTED" ? "[信息] 日志流确认连接。" : event.data);
    };
    logWebSocket.onerror = (event) => {
        updateLogStatus("连接错误!", true);
        addLogEntry("[错误] 日志 WebSocket 连接失败。");
        clearLogButton.disabled = true;
    };
    logWebSocket.onclose = (event) => {
        let reason = event.reason ? ` 原因: ${event.reason}` : '';
        let statusMsg = `连接已关闭 (Code: ${event.code})${reason}`;
        let logMsg = `[信息] 日志 WebSocket 连接已关闭 (Code: ${event.code}${reason})`;
        if (!event.wasClean) {
            statusMsg = `连接意外断开 (Code: ${event.code})${reason}。5秒后尝试重连...`;
            setTimeout(initializeLogWebSocket, 5000);
        }
        updateLogStatus(statusMsg, !event.wasClean);
        addLogEntry(logMsg);
        clearLogButton.disabled = true;
    };
}

// --- Chat Initialization & Message Handling ---
function initializeChat() {
    conversationHistory = [{ role: "system", content: modelSettings.systemPrompt }];
    chatbox.innerHTML = '';

    const historyLoaded = loadChatHistory(); // This will also apply the current system prompt

    if (!historyLoaded || conversationHistory.length <= 1) { // If no history or only system prompt
        displayMessage(modelSettings.systemPrompt, 'system'); // Display current system prompt
    }
    // If history was loaded, loadChatHistory already displayed messages including the (potentially updated) system prompt.

    userInput.disabled = false;
    sendButton.disabled = false;
    clearButton.disabled = false;
    userInput.value = '';
    autoResizeTextarea();
    userInput.focus();

    loadLogHistory();
    if (!logWebSocket || logWebSocket.readyState === WebSocket.CLOSED) {
        initializeLogWebSocket();
        clearLogButton.disabled = true;
    } else {
        updateLogStatus("已连接到日志流。");
        clearLogButton.disabled = false;
    }
}

async function sendMessage() {
    const messageText = userInput.value.trim();
    if (!messageText) return;
    userInput.disabled = true;
    sendButton.disabled = true;
    clearButton.disabled = true;

    try {
        conversationHistory.push({ role: 'user', content: messageText });
        displayMessage(messageText, 'user', conversationHistory.length - 1);
        userInput.value = '';
        autoResizeTextarea();
        saveChatHistory();

        const assistantMsgElement = displayMessage('', 'assistant', conversationHistory.length);
        assistantMsgElement.classList.add('streaming');
        chatbox.scrollTop = chatbox.scrollHeight;

        let fullResponse = '';
        const requestBody = {
            messages: conversationHistory,
            model: SELECTED_MODEL,
            stream: true,
            temperature: modelSettings.temperature,
            max_output_tokens: modelSettings.maxOutputTokens,
            top_p: modelSettings.topP,
        };
        if (modelSettings.stopSequences) {
            const stopArray = modelSettings.stopSequences.split(',').map(seq => seq.trim()).filter(seq => seq.length > 0);
            if (stopArray.length > 0) requestBody.stop = stopArray;
        }
        addLogEntry(`[信息] 发送请求,模型: ${SELECTED_MODEL}, 温度: ${requestBody.temperature ?? '默认'}, 最大Token: ${requestBody.max_output_tokens ?? '默认'}, Top P: ${requestBody.top_p ?? '默认'}`);

        const response = await fetch(API_URL, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(requestBody)
        });

        if (!response.ok) {
            let errorText = `HTTP Error: ${response.status} ${response.statusText}`;
            try { errorText = (await response.json()).detail || errorText; } catch (e) { /* ignore */ }
            throw new Error(errorText);
        }

        const reader = response.body.getReader();
        const decoder = new TextDecoder();
        let buffer = '';
        while (true) {
            const { done, value } = await reader.read();
            if (done) break;
            buffer += decoder.decode(value, { stream: true });
            let boundary;
            while ((boundary = buffer.indexOf('\n\n')) >= 0) {
                const line = buffer.substring(0, boundary).trim();
                buffer = buffer.substring(boundary + 2);
                if (line.startsWith('data: ')) {
                    const data = line.substring(6).trim();
                    if (data === '[DONE]') continue;
                    try {
                        const chunk = JSON.parse(data);
                        if (chunk.error) throw new Error(chunk.error.message || "Unknown stream error");
                        const delta = chunk.choices?.[0]?.delta?.content || '';
                        if (delta) {
                            fullResponse += delta;
                            const isScrolledToBottom = chatbox.scrollHeight - chatbox.clientHeight <= chatbox.scrollTop + 25;
                            assistantMsgElement.querySelector('.message-content').textContent += delta;
                            if (isScrolledToBottom) chatbox.scrollTop = chatbox.scrollHeight;
                        }
                    } catch (e) {
                        addLogEntry(`[错误] 解析流数据块失败: ${e.message}. 数据: ${data}`);
                    }
                }
            }
        }
        renderMessageContent(assistantMsgElement.querySelector('.message-content'), fullResponse);

        if (fullResponse) {
            conversationHistory.push({ role: 'assistant', content: fullResponse });
            saveChatHistory();
        } else {
            assistantMsgElement.remove(); // Remove empty assistant message bubble
            if (conversationHistory.at(-1)?.role === 'user') { // Remove last user message if AI didn't respond
                conversationHistory.pop();
                saveChatHistory();
                const userMessages = chatbox.querySelectorAll('.user-message');
                if (userMessages.length > 0) userMessages[userMessages.length - 1].remove();
            }
        }
    } catch (error) {
        const errorText = `喵... 出错了: ${error.message || '未知错误'} >_<`;
        displayMessage(errorText, 'error');
        addLogEntry(`[错误] 发送消息失败: ${error.message}`);
        const streamingMsg = chatbox.querySelector('.assistant-message.streaming');
        if (streamingMsg) streamingMsg.remove();
        // Rollback user message if AI failed
        if (conversationHistory.at(-1)?.role === 'user') {
            conversationHistory.pop();
            saveChatHistory();
            const userMessages = chatbox.querySelectorAll('.user-message');
            if (userMessages.length > 0) userMessages[userMessages.length - 1].remove();
        }
    } finally {
        userInput.disabled = false;
        sendButton.disabled = false;
        clearButton.disabled = false;
        const finalAssistantMsg = Array.from(chatbox.querySelectorAll('.assistant-message.streaming')).pop();
        if (finalAssistantMsg) finalAssistantMsg.classList.remove('streaming');
        userInput.focus();
        chatbox.scrollTop = chatbox.scrollHeight;
    }
}

function displayMessage(text, role, index) {
    const messageElement = document.createElement('div');
    messageElement.classList.add('message', `${role}-message`);
    if (index !== undefined && (role === 'user' || role === 'assistant' || role === 'system')) {
        messageElement.dataset.index = index;
    }
    const messageContentElement = document.createElement('div');
    messageContentElement.classList.add('message-content');
    renderMessageContent(messageContentElement, text || (role === 'assistant' ? '' : text)); // Allow empty initial for streaming
    messageElement.appendChild(messageContentElement);
    chatbox.appendChild(messageElement);
    setTimeout(() => { // Ensure scroll happens after render
        if (chatbox.lastChild === messageElement) chatbox.scrollTop = chatbox.scrollHeight;
    }, 0);
    return messageElement;
}

function renderMessageContent(element, text) {
    if (text == null) { element.innerHTML = ''; return; }
    const escapeHtml = (unsafe) => unsafe.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
    let safeText = escapeHtml(String(text));
    safeText = safeText.replace(/```(?:[\w-]*\n)?([\s\S]+?)\n?```/g, (match, code) => `<pre><code>${code.trim()}</code></pre>`);
    safeText = safeText.replace(/`([^`]+)`/g, '<code>$1</code>');
    const links = [];
    safeText = safeText.replace(/\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g, (match, linkText, url) => {
        links.push({ text: linkText, url: url });
        return `__LINK_${links.length - 1}__`;
    });
    safeText = safeText.replace(/(\*\*|__)(?=\S)([\s\S]*?\S)\1/g, '<strong>$2</strong>');
    safeText = safeText.replace(/(\*|_)(?=\S)([\s\S]*?\S)\1/g, '<em>$2</em>');
    safeText = safeText.replace(/__LINK_(\d+)__/g, (match, index) => {
        const link = links[parseInt(index)];
        return `<a href="${escapeHtml(link.url)}" target="_blank" rel="noopener noreferrer">${link.text}</a>`;
    });
    element.innerHTML = safeText;
    if (typeof hljs !== 'undefined' && element.querySelectorAll('pre code').length > 0) {
        element.querySelectorAll('pre code').forEach((block) => hljs.highlightElement(block));
    }
}

function saveChatHistory() {
    try { localStorage.setItem(CHAT_HISTORY_KEY, JSON.stringify(conversationHistory)); }
    catch (e) { addLogEntry("[错误] 保存聊天记录失败。"); }
}

function loadChatHistory() {
    try {
        const storedHistory = localStorage.getItem(CHAT_HISTORY_KEY);
        if (storedHistory) {
            const parsedHistory = JSON.parse(storedHistory);
            if (Array.isArray(parsedHistory) && parsedHistory.length > 0) {
                // Ensure the current system prompt is used
                parsedHistory[0] = { role: "system", content: modelSettings.systemPrompt };
                conversationHistory = parsedHistory;
                chatbox.innerHTML = ''; // Clear chatbox before re-rendering
                for (let i = 0; i < conversationHistory.length; i++) {
                    // Display system message only if it's the first one, or handle as per your preference
                    if (i === 0 && conversationHistory[i].role === 'system') {
                        displayMessage(conversationHistory[i].content, conversationHistory[i].role, i);
                    } else if (conversationHistory[i].role !== 'system') {
                        displayMessage(conversationHistory[i].content, conversationHistory[i].role, i);
                    }
                }
                addLogEntry("[信息] 从 localStorage 加载了聊天记录。");
                return true;
            }
        }
    } catch (e) {
        addLogEntry("[错误] 加载聊天记录失败。");
        localStorage.removeItem(CHAT_HISTORY_KEY);
    }
    return false;
}


function saveLogHistory() {
    try { localStorage.setItem(LOG_HISTORY_KEY, JSON.stringify(logHistory)); }
    catch (e) { console.error("Error saving log history:", e); }
}

function loadLogHistory() {
    try {
        const storedLogs = localStorage.getItem(LOG_HISTORY_KEY);
        if (storedLogs) {
            const parsedLogs = JSON.parse(storedLogs);
            if (Array.isArray(parsedLogs)) {
                logHistory = parsedLogs;
                logTerminal.innerHTML = '';
                parsedLogs.forEach(logMsg => {
                    const logEntry = document.createElement('div');
                    logEntry.classList.add('log-entry');
                    logEntry.textContent = logMsg;
                    logTerminal.appendChild(logEntry);
                });
                if (logTerminal.children.length > 0) logTerminal.scrollTop = logTerminal.scrollHeight;
                return true;
            }
        }
    } catch (e) { localStorage.removeItem(LOG_HISTORY_KEY); }
    return false;
}

// --- API Info & Health Status ---
async function loadApiInfo() {
    apiInfoContent.innerHTML = '<div class="loading-indicator"><div class="loading-spinner"></div><span>正在加载 API 信息...</span></div>';
    try {
        const response = await fetch('/api/info');
        if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
        const data = await response.json();
        const formattedData = {
            'API Base URL': data.api_base_url ? `<code>${data.api_base_url}</code>` : '未知',
            'Model Name': data.model_name ? `<code>${data.model_name}</code>` : '未知',
            'API Key Required': data.api_key_required ? '<span style="color: orange;">⚠️ 是 (请在后端配置)</span>' : '<span style="color: green;">✅ 否</span>'
        };
        displayInfoList(apiInfoContent, formattedData);
    } catch (error) {
        apiInfoContent.innerHTML = `<div class="info-list"><div><strong style="color: var(--error-msg-text);">错误:</strong> <span style="color: var(--error-msg-text);">加载 API 信息失败: ${error.message}</span></div></div>`;
    }
}

async function fetchHealthStatus() {
    if (!healthStatusDisplay) return;
    healthStatusDisplay.innerHTML = '<div class="loading-indicator"><div class="loading-spinner"></div><span>正在加载健康状态...</span></div>';
    try {
        const response = await fetch('/health');
        if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
        const data = await response.json();
        displayInfoList(healthStatusDisplay, data);
    } catch (error) {
        if (healthStatusDisplay) {
            healthStatusDisplay.innerHTML = `<div class="info-list"><div><strong style="color: var(--error-msg-text);">错误:</strong> <span style="color: var(--error-msg-text);">加载健康状态失败: ${error.message}</span></div></div>`;
        }
        addLogEntry(`[错误] 获取 /health 状态失败: ${error.message}`);
    }
}

function displayInfoList(targetElement, data) {
    if (!targetElement) return;
    let html = '<div class="info-list">';
    for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {
            let value = data[key];
            const preStyle = "white-space: pre-wrap; word-break: break-all; background-color: rgba(var(--on-surface-rgb), 0.03); padding: 8px; border-radius: 4px; margin-top: 4px;";
            if (typeof value === 'object' && value !== null) {
                html += `<div><strong>${key}:</strong> <pre style="${preStyle}"><code>${JSON.stringify(value, null, 2)}</code></pre></div>`;
            } else {
                html += `<div><strong>${key}:</strong> <span>${value}</span></div>`;
            }
        }
    }
    html += '</div>';
    targetElement.innerHTML = html;
}

// --- View Switching ---
function switchView(viewId) {
    chatView.style.display = 'none';
    serverInfoView.style.display = 'none';
    modelSettingsView.style.display = 'none';
    navChatButton.classList.remove('active');
    navServerInfoButton.classList.remove('active');
    navModelSettingsButton.classList.remove('active');

    if (viewId === 'chat') {
        chatView.style.display = 'flex';
        navChatButton.classList.add('active');
        if (userInput) userInput.focus();
    } else if (viewId === 'server-info') {
        serverInfoView.style.display = 'flex';
        navServerInfoButton.classList.add('active');
        fetchHealthStatus();
        loadApiInfo();
    } else if (viewId === 'model-settings') {
        modelSettingsView.style.display = 'flex';
        navModelSettingsButton.classList.add('active');
        updateModelSettingsUI();
    }
}

// --- Model Settings ---
function initializeModelSettings() {
    try {
        const storedSettings = localStorage.getItem(MODEL_SETTINGS_KEY);
        if (storedSettings) {
            const parsedSettings = JSON.parse(storedSettings);
            modelSettings = { ...modelSettings, ...parsedSettings };
        }
    } catch (e) {
        addLogEntry("[错误] 加载模型设置失败。");
    }
    // updateModelSettingsUI will be called after model list is loaded and controls are updated by updateControlsForSelectedModel
    // So, we don't necessarily need to call it here if loadModelList ensures it happens.
    // However, to ensure UI reflects something on initial load before models arrive, it can stay.
    updateModelSettingsUI();
}

function updateModelSettingsUI() {
    systemPromptInput.value = modelSettings.systemPrompt;
    temperatureSlider.value = temperatureValue.value = modelSettings.temperature;
    maxOutputTokensSlider.value = maxOutputTokensValue.value = modelSettings.maxOutputTokens;
    topPSlider.value = topPValue.value = modelSettings.topP;
    stopSequencesInput.value = modelSettings.stopSequences;
}

function saveModelSettings() {
    modelSettings.systemPrompt = systemPromptInput.value.trim() || DEFAULT_SYSTEM_PROMPT;
    modelSettings.temperature = parseFloat(temperatureValue.value);
    modelSettings.maxOutputTokens = parseInt(maxOutputTokensValue.value);
    modelSettings.topP = parseFloat(topPValue.value);
    modelSettings.stopSequences = stopSequencesInput.value.trim();

    try {
        localStorage.setItem(MODEL_SETTINGS_KEY, JSON.stringify(modelSettings));

        if (conversationHistory.length > 0 && conversationHistory[0].role === 'system') {
            if (conversationHistory[0].content !== modelSettings.systemPrompt) {
                conversationHistory[0].content = modelSettings.systemPrompt;
                saveChatHistory(); // Save updated history
                // Update displayed system message if it exists
                const systemMsgElement = chatbox.querySelector('.system-message[data-index="0"] .message-content');
                if (systemMsgElement) {
                    renderMessageContent(systemMsgElement, modelSettings.systemPrompt);
                } else { // If not displayed, re-initialize chat to show it (or simply add it)
                    // This might be too disruptive, consider just updating the history
                    // and letting new chats use it. For now, just update history.
                }
            }
        }

        showSettingsStatus("设置已保存!", false);
        addLogEntry("[信息] 模型设置已保存。");
    } catch (e) {
        showSettingsStatus("保存设置失败!", true);
        addLogEntry("[错误] 保存模型设置失败。");
    }
}

function resetModelSettings() {
    if (confirm("确定要将当前模型的参数恢复为默认值吗?系统提示词也会重置。 注意:这不会清除已保存的其他模型的设置。")) {
        modelSettings.systemPrompt = DEFAULT_SYSTEM_PROMPT;
        systemPromptInput.value = DEFAULT_SYSTEM_PROMPT;

        updateControlsForSelectedModel(); // This applies model-specific defaults to UI and modelSettings object

        try {
            // Save these model-specific defaults (which are now in modelSettings) to localStorage
            // This makes the "reset" effectively a "reset to this model's defaults and save that"
            localStorage.setItem(MODEL_SETTINGS_KEY, JSON.stringify(modelSettings));
            addLogEntry("[信息] 当前模型的参数已重置为默认值并保存。");
            showSettingsStatus("参数已重置为当前模型的默认值!", false);
        } catch (e) {
            addLogEntry("[错误] 保存重置后的模型设置失败。");
            showSettingsStatus("重置并保存设置失败!", true);
        }

        if (conversationHistory.length > 0 && conversationHistory[0].role === 'system') {
            if (conversationHistory[0].content !== modelSettings.systemPrompt) {
                conversationHistory[0].content = modelSettings.systemPrompt;
                saveChatHistory();
                const systemMsgElement = chatbox.querySelector('.system-message[data-index="0"] .message-content');
                if (systemMsgElement) {
                    renderMessageContent(systemMsgElement, modelSettings.systemPrompt);
                }
            }
        }
    }
}

function showSettingsStatus(message, isError = false) {
    settingsStatusElement.textContent = message;
    settingsStatusElement.style.color = isError ? "var(--error-color)" : "var(--primary-color)";
    setTimeout(() => {
        settingsStatusElement.textContent = "设置将在发送消息时自动应用,并保存在本地。";
        settingsStatusElement.style.color = "rgba(var(--on-surface-rgb), 0.8)";
    }, 3000);
}

function autoResizeTextarea() {
    const target = userInput;
    target.style.height = 'auto';
    const maxHeight = parseInt(getComputedStyle(target).maxHeight) || 200;
    target.style.height = (target.scrollHeight > maxHeight ? maxHeight : target.scrollHeight) + 'px';
    target.style.overflowY = target.scrollHeight > maxHeight ? 'auto' : 'hidden';
}

// --- Event Listeners Binding ---
function bindEventListeners() {
    themeToggleButton.addEventListener('click', toggleTheme);
    toggleSidebarButton.addEventListener('click', () => {
        sidebarPanel.classList.toggle('collapsed');
        updateToggleButton(sidebarPanel.classList.contains('collapsed'));
    });
    window.addEventListener('resize', () => {
        checkInitialSidebarState();
    });

    sendButton.addEventListener('click', sendMessage);
    clearButton.addEventListener('click', () => {
        if (confirm("确定要清除所有聊天记录吗?此操作也会清除浏览器缓存。")) {
            localStorage.removeItem(CHAT_HISTORY_KEY);
            initializeChat(); // Re-initialize to apply new system prompt etc.
        }
    });
    userInput.addEventListener('keydown', (event) => {
        if (event.key === 'Enter' && !event.shiftKey) {
            event.preventDefault();
            sendMessage();
        }
    });
    userInput.addEventListener('input', autoResizeTextarea);
    clearLogButton.addEventListener('click', clearLogTerminal);

    modelSelector.addEventListener('change', function () {
        SELECTED_MODEL = this.value || MODEL_NAME;
        try { localStorage.setItem(SELECTED_MODEL_KEY, SELECTED_MODEL); } catch (e) {/*ignore*/ }
        addLogEntry(`[信息] 已选择模型: ${SELECTED_MODEL}`);
        updateControlsForSelectedModel();
    });
    refreshModelsButton.addEventListener('click', () => {
        addLogEntry('[信息] 正在刷新模型列表...');
        loadModelList();
    });

    navChatButton.addEventListener('click', () => switchView('chat'));
    navServerInfoButton.addEventListener('click', () => switchView('server-info'));
    navModelSettingsButton.addEventListener('click', () => switchView('model-settings'));
    refreshServerInfoButton.addEventListener('click', async () => {
        refreshServerInfoButton.disabled = true;
        refreshServerInfoButton.textContent = '刷新中...';
        try {
            await Promise.all([loadApiInfo(), fetchHealthStatus()]);
        } finally {
            setTimeout(() => {
                refreshServerInfoButton.disabled = false;
                refreshServerInfoButton.textContent = '刷新';
            }, 300);
        }
    });

    // Model Settings Page Events
    temperatureSlider.addEventListener('input', () => temperatureValue.value = temperatureSlider.value);
    temperatureValue.addEventListener('input', () => { if (!isNaN(parseFloat(temperatureValue.value))) temperatureSlider.value = parseFloat(temperatureValue.value); });
    maxOutputTokensSlider.addEventListener('input', () => maxOutputTokensValue.value = maxOutputTokensSlider.value);
    maxOutputTokensValue.addEventListener('input', () => { if (!isNaN(parseInt(maxOutputTokensValue.value))) maxOutputTokensSlider.value = parseInt(maxOutputTokensValue.value); });
    topPSlider.addEventListener('input', () => topPValue.value = topPSlider.value);
    topPValue.addEventListener('input', () => { if (!isNaN(parseFloat(topPValue.value))) topPSlider.value = parseFloat(topPValue.value); });

    saveModelSettingsButton.addEventListener('click', saveModelSettings);
    resetModelSettingsButton.addEventListener('click', resetModelSettings);

    const debouncedSave = debounce(saveModelSettings, 1000);
    [systemPromptInput, temperatureValue, maxOutputTokensValue, topPValue, stopSequencesInput].forEach(
        element => element.addEventListener('input', debouncedSave) // Use 'input' for more responsive auto-save
    );
}

// --- Initialization on DOMContentLoaded ---
document.addEventListener('DOMContentLoaded', async () => {
    initializeDOMReferences();
    bindEventListeners();
    loadThemePreference();

    // 步骤 1: 加载模型列表。这将调用 updateControlsForSelectedModel(),
    // 它会用模型默认值更新 modelSettings 的相关字段,并设置UI控件的范围和默认显示。
    await loadModelList(); // 使用 await 确保它先完成

    // 步骤 2: 初始化模型设置。现在 modelSettings 已有模型默认值,
    // initializeModelSettings 将从 localStorage 加载用户保存的值来覆盖这些默认值。
    initializeModelSettings();

    // 步骤 3: 初始化聊天界面,它会使用最终的 modelSettings (包含系统提示等)
    initializeChat();

    // 其他初始化
    loadApiInfo();
    fetchHealthStatus();
    setInterval(fetchHealthStatus, 30000);
    checkInitialSidebarState();
    autoResizeTextarea();
});