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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
/**
 * 模型选择器类 - 处理模型选择下拉列表的所有交互
 */
class ModelSelector {
    /**
     * 构造函数
     * @param {Object} options 配置选项
     * @param {Function} options.onChange 选择变更回调
     * @param {Object} options.models 模型定义对象
     * @param {Object} options.providers 提供商定义对象
     */
    constructor(options) {
        this.options = options || {};
        this.models = this.options.models || {};
        this.providers = this.options.providers || {};
        this.onChange = this.options.onChange || (() => {});
        
        // 元素引用
        this.container = document.getElementById('modelSelector');
        this.display = this.container?.querySelector('.model-display');
        this.currentNameEl = document.getElementById('currentModelName');
        this.currentProviderEl = document.getElementById('currentModelProvider');
        this.badgesContainer = document.getElementById('modelBadges');
        this.originalSelect = document.getElementById('modelSelect');
        
        // 创建下拉面板和遮罩
        this.createDropdownElements();
        
        // 当前选中的模型ID
        this.selectedModelId = null;
        
        // 初始化
        this.initEvents();
    }
    
    /**
     * 创建下拉面板和遮罩元素
     */
    createDropdownElements() {
        // 创建遮罩层
        this.overlay = document.createElement('div');
        this.overlay.className = 'model-dropdown-overlay';
        document.body.appendChild(this.overlay);
        
        // 创建下拉面板
        this.dropdown = document.createElement('div');
        this.dropdown.className = 'model-dropdown-panel';
        document.body.appendChild(this.dropdown);
    }
    
    /**
     * 初始化事件监听
     */
    initEvents() {
        if (!this.display) return;
        
        // 点击选择器显示下拉框
        this.display.addEventListener('click', (e) => {
            e.stopPropagation();
            this.toggleDropdown();
        });
        
        // 点击遮罩层关闭下拉框
        this.overlay.addEventListener('click', () => {
            this.closeDropdown();
        });
        
        // 监听原始select变化,保持同步
        if (this.originalSelect) {
            this.originalSelect.addEventListener('change', () => {
                const modelId = this.originalSelect.value;
                if (modelId && modelId !== this.selectedModelId) {
                    this.selectModel(modelId, false); // 不触发onChange避免循环
                }
            });
        }
        
        // 防止面板内部点击冒泡到遮罩
        this.dropdown.addEventListener('click', (e) => {
            e.stopPropagation();
        });
        
        console.log('模型选择器事件初始化完成');
    }
    
    /**
     * 加载模型选项到下拉面板
     */
    loadModelOptions() {
        if (!this.dropdown) return;
        
        // 清空下拉面板
        this.dropdown.innerHTML = '';
        
        // 按提供商分组模型
        const groupedModels = {};
        Object.entries(this.models).forEach(([modelId, model]) => {
            const providerId = model.provider;
            if (!groupedModels[providerId]) {
                groupedModels[providerId] = [];
            }
            groupedModels[providerId].push({ id: modelId, ...model });
        });
        
        // 创建分组选项
        Object.entries(groupedModels).forEach(([providerId, models]) => {
            const provider = this.providers[providerId] || { name: providerId };
            
            // 创建分组容器
            const group = document.createElement('div');
            group.className = 'model-group';
            
            // 创建分组标题
            const title = document.createElement('div');
            title.className = 'model-group-title';
            title.textContent = provider.name;
            group.appendChild(title);
            
            // 添加该分组的模型选项
            models.sort((a, b) => a.name.localeCompare(b.name))
                .forEach(model => {
                    const option = document.createElement('div');
                    option.className = 'model-option';
                    option.dataset.modelId = model.id;
                    if (model.id === this.selectedModelId) {
                        option.classList.add('selected');
                    }
                    
                    // 模型名称 - 移除版本显示
                    const nameEl = document.createElement('div');
                    nameEl.className = 'model-option-name';
                    nameEl.textContent = model.name;
                    option.appendChild(nameEl);
                    
                    // 能力徽章
                    if (model.supportsMultimodal || model.isReasoning) {
                        const badges = document.createElement('div');
                        badges.className = 'model-option-badges';
                        
                        if (model.supportsMultimodal) {
                            const badge = document.createElement('div');
                            badge.className = 'model-option-badge';
                            badge.title = '支持图像';
                            badge.innerHTML = '<i class="fas fa-image"></i>';
                            badges.appendChild(badge);
                        }
                        
                        if (model.isReasoning) {
                            const badge = document.createElement('div');
                            badge.className = 'model-option-badge';
                            badge.title = '支持深度推理';
                            badge.innerHTML = '<i class="fas fa-brain"></i>';
                            badges.appendChild(badge);
                        }
                        
                        option.appendChild(badges);
                    }
                    
                    // 点击选项选择模型
                    option.addEventListener('click', () => {
                        this.selectModel(model.id);
                        this.closeDropdown();
                    });
                    
                    group.appendChild(option);
                });
            
            // 只添加有模型的分组
            if (group.childElementCount > 1) { // > 1 因为包含标题
                this.dropdown.appendChild(group);
            }
        });
        
        console.log('模型选项加载完成');
    }
    
    /**
     * 打开下拉面板
     */
    openDropdown() {
        if (!this.container || !this.dropdown || !this.overlay) return;
        
        // 加载模型选项
        this.loadModelOptions();
        
        // 显示遮罩和下拉面板
        this.overlay.style.display = 'block';
        this.dropdown.style.display = 'block';
        
        // 设置面板位置 - 相对于视口
        this.adjustDropdownPosition();
        
        // 添加窗口调整大小和滚动事件监听器
        window.addEventListener('resize', this.adjustDropdownPosition.bind(this));
        window.addEventListener('scroll', this.adjustDropdownPosition.bind(this));
        
        // 延迟添加可见类以启用过渡效果
        setTimeout(() => {
            this.dropdown.classList.add('visible');
        }, 10);
        
        // 添加打开状态类
        this.container.classList.add('open');
        
        // 确保当前选中的选项可见
        setTimeout(() => {
            const selectedOption = this.dropdown.querySelector('.model-option.selected');
            if (selectedOption) {
                selectedOption.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
            }
        }, 100);
    }
    
    /**
     * 调整下拉面板位置
     */
    adjustDropdownPosition() {
        if (!this.display || !this.dropdown) return;
        
        // 获取模型选择器的位置
        const rect = this.display.getBoundingClientRect();
        
        // 设置下拉面板宽度
        this.dropdown.style.width = `${rect.width}px`;
        
        // 计算最佳位置
        const viewportHeight = window.innerHeight;
        const spaceBelow = viewportHeight - rect.bottom;
        const spaceAbove = rect.top;
        const dropdownHeight = Math.min(300, Math.max(this.dropdown.scrollHeight, 100));
        
        // 检查下方空间是否足够
        if (spaceBelow >= dropdownHeight || spaceBelow >= spaceAbove) {
            // 显示在下方
            this.dropdown.style.top = `${rect.bottom + 5}px`;
            this.dropdown.style.bottom = 'auto';
        } else {
            // 显示在上方
            this.dropdown.style.bottom = `${viewportHeight - rect.top + 5}px`;
            this.dropdown.style.top = 'auto';
        }
        
        // 水平定位
        this.dropdown.style.left = `${rect.left}px`;
        
        // 检查是否超出右侧边界
        const rightEdge = rect.left + rect.width;
        const viewportWidth = window.innerWidth;
        if (rightEdge > viewportWidth) {
            this.dropdown.style.left = 'auto';
            this.dropdown.style.right = '10px';
        }
    }
    
    /**
     * 关闭下拉面板
     */
    closeDropdown() {
        if (!this.container || !this.dropdown || !this.overlay) return;
        
        // 移除可见类
        this.dropdown.classList.remove('visible');
        
        // 移除打开状态类
        this.container.classList.remove('open');
        
        // 移除事件监听器
        window.removeEventListener('resize', this.adjustDropdownPosition.bind(this));
        window.removeEventListener('scroll', this.adjustDropdownPosition.bind(this));
        
        // 延迟隐藏元素,以便完成过渡动画
        setTimeout(() => {
            this.overlay.style.display = 'none';
            this.dropdown.style.display = 'none';
        }, 200);
    }
    
    /**
     * 切换下拉面板显示状态
     */
    toggleDropdown() {
        if (this.container.classList.contains('open')) {
            this.closeDropdown();
        } else {
            this.openDropdown();
        }
    }
    
    /**
     * 选择模型
     * @param {string} modelId 模型ID
     * @param {boolean} triggerChange 是否触发onChange回调,默认true
     */
    selectModel(modelId, triggerChange = true) {
        const model = this.models[modelId];
        if (!model) return;
        
        // 更新选中的模型ID
        this.selectedModelId = modelId;
        
        // 更新显示信息
        this.updateDisplayInfo(model);
        
        // 更新原始select
        if (this.originalSelect && this.originalSelect.value !== modelId) {
            this.originalSelect.value = modelId;
        }
        
        // 触发变更回调
        if (triggerChange) {
            this.onChange(modelId, model);
        }
        
        console.log('已选择模型:', modelId);
    }
    
    /**
     * 更新显示信息
     * @param {Object} model 模型信息
     */
    updateDisplayInfo(model) {
        if (!this.currentNameEl || !this.currentProviderEl || !this.badgesContainer) return;
        
        // 更新模型名称 - 不在这里显示版本号
        this.currentNameEl.textContent = model.name;
        
        // 更新提供商
        const provider = this.providers[model.provider] || { name: model.provider };
        this.currentProviderEl.textContent = provider.name;
        
        // 更新能力徽章
        this.badgesContainer.innerHTML = '';
        if (model.supportsMultimodal) {
            const badge = document.createElement('div');
            badge.className = 'model-badge';
            badge.title = '支持图像';
            badge.innerHTML = '<i class="fas fa-image"></i>';
            this.badgesContainer.appendChild(badge);
        }
        
        if (model.isReasoning) {
            const badge = document.createElement('div');
            badge.className = 'model-badge';
            badge.title = '支持深度推理';
            badge.innerHTML = '<i class="fas fa-brain"></i>';
            this.badgesContainer.appendChild(badge);
        }
    }
    
    /**
     * 设置模型数据
     * @param {Object} models 模型定义对象
     * @param {Object} providers 提供商定义对象
     */
    setModelData(models, providers) {
        this.models = models || {};
        this.providers = providers || {};
    }
}

class SettingsManager {
    constructor() {
        // 初始化属性
        this.modelDefinitions = {};
        this.providerDefinitions = {};
        
        // 初始化界面元素
        this.initializeElements();
        
        // 提示词配置
        this.prompts = {};
        this.currentPromptId = 'default';
        
        // 模型选择器对象
        this.modelSelector = null;
        
        // OCR源配置
        this.ocrSource = 'auto'; // 默认自动选择
        
        // 存储API密钥的对象
        this.apiKeyValues = {
            'AnthropicApiKey': '',
            'OpenaiApiKey': '',
            'DeepseekApiKey': '',
            'AlibabaApiKey': '',
            'GoogleApiKey': '',
            'DoubaoApiKey': '',
            'BaiduApiKey': '',
            'BaiduSecretKey': '',
            'MathpixAppId': '',
            'MathpixAppKey': ''
        };
        
        // 存储API基础URL的对象
        this.apiBaseUrlValues = {
            'AnthropicApiBaseUrl': '',
            'OpenaiApiBaseUrl': '',
            'DeepseekApiBaseUrl': '',
            'AlibabaApiBaseUrl': '',
            'GoogleApiBaseUrl': '',
            'DoubaoApiBaseUrl': ''
        };
        
        // 加载模型配置
        this.isInitialized = false;
        this.initialize();
    }
    
    async initialize() {
        try {
            // 加载模型配置
            await this.loadModelConfig();
            
                // 成功加载配置后,执行后续初始化
                this.updateModelOptions();
            await this.loadSettings();
            await this.loadPrompts(); // 加载提示词配置
                this.setupEventListeners();
                this.updateUIBasedOnModelType();
            
            // 刷新API密钥状态
            await this.refreshApiKeyStatus();
            
            // 刷新API基础URL状态
            await this.refreshApiBaseUrlStatus();
            
            // 初始化可折叠内容逻辑
            this.initCollapsibleContent();
            
            // 初始化Token显示
            if (this.maxTokens && this.maxTokensValue) {
                this.updateTokenValueDisplay();
                this.highlightActivePreset();
            }
            
            // 初始化模型选择器
            this.initModelSelector();
            
            // 添加到window对象,方便在控制台调试
            window.debugModelSelector = {
                open: () => this.modelSelector?.openDropdown(),
                close: () => this.modelSelector?.closeDropdown(),
                toggle: () => this.modelSelector?.toggleDropdown(),
                instance: this.modelSelector
            };
            
            // 绑定提示词预览区域点击事件
            this.initPromptPreviewEvents();
            
            this.isInitialized = true;
            console.log('设置管理器初始化完成');
        } catch (error) {
            console.error('初始化设置管理器失败:', error);
                window.uiManager?.showToast('加载模型配置失败,使用默认配置', 'error');
                
                // 使用默认配置作为备份
                this.setupDefaultModels();
                this.updateModelOptions();
            await this.loadSettings();
            await this.loadPrompts(); // 加载提示词配置
                this.setupEventListeners();
                this.updateUIBasedOnModelType();
        
            // 刷新API密钥状态(即使在出错情况下也尝试)
            try {
                await this.refreshApiKeyStatus();
                await this.refreshApiBaseUrlStatus();
            } catch (e) {
                console.error('刷新API状态失败:', e);
            }
                
            // 初始化可折叠内容逻辑
            this.initCollapsibleContent();
            
            // 初始化模型选择器
            this.initModelSelector();
            
            this.isInitialized = true;
        }
    }

    // 初始化新的模型选择器
    initModelSelector() {
        if (this.modelSelector) {
            // 如果已存在,更新数据
            this.modelSelector.setModelData(this.modelDefinitions, this.providerDefinitions);
        } else {
            // 创建新实例
            this.modelSelector = new ModelSelector({
                models: this.modelDefinitions,
                providers: this.providerDefinitions,
                onChange: (modelId) => {
                    // 处理模型变更
                    console.log('模型已变更:', modelId);
                    this.updateVisibleApiKey(modelId);
                    this.updateUIBasedOnModelType();
                    this.updateModelVersionDisplay(modelId);
                    this.saveSettings();
                }
            });
        }
        
        // 设置当前选择的模型
        if (this.modelSelect && this.modelSelect.value) {
            this.modelSelector.selectModel(this.modelSelect.value, false);
        }
    }

    // 更新模型选择下拉框
    updateModelOptions() {
        // 清空现有选项
        this.modelSelect.innerHTML = '';
        
        // 提取提供商信息
        const providers = {};
        Object.entries(this.providerDefinitions).forEach(([providerId, provider]) => {
            providers[providerId] = provider.name;
        });
        
        // 为每个提供商创建一个选项组
        for (const [providerId, providerName] of Object.entries(providers)) {
            const optgroup = document.createElement('optgroup');
            optgroup.label = providerName;
            
            // 过滤该提供商的模型
            const providerModels = Object.entries(this.modelDefinitions)
                .filter(([_, modelInfo]) => modelInfo.provider === providerId)
                .sort((a, b) => a[1].name.localeCompare(b[1].name));
            
            // 添加该提供商的模型选项
            for (const [modelId, modelInfo] of providerModels) {
                // 添加到原始select元素
                const option = document.createElement('option');
                option.value = modelId;
                
                // 只显示模型名称,不再显示版本号
                option.textContent = modelInfo.name;
                optgroup.appendChild(option);
            }
            
            // 只添加有模型的提供商
            if (optgroup.children.length > 0) {
                this.modelSelect.appendChild(optgroup);
            }
        }
    }

    async loadSettings() {
        try {
            // 先从localStorage加载大部分设置
        const settings = JSON.parse(localStorage.getItem('aiSettings') || '{}');
        
            // 刷新API密钥状态(自动从服务器获取最新状态)
            await this.refreshApiKeyStatus();
            console.log('已自动刷新API密钥状态');
            
            // 加载 API 基础 URL 设置
            if (settings.apiBaseUrlValues) {
                this.apiBaseUrlValues = settings.apiBaseUrlValues;
                await this.refreshApiBaseUrlStatus();
                console.log('已加载 API 基础 URL 设置');
            }
            
            // 加载其他设置
        // Load model selection
        if (settings.model && this.modelExists(settings.model)) {
            this.modelSelect.value = settings.model;
            this.updateVisibleApiKey(settings.model);
                
                // 使用新的模型选择器更新UI
                if (this.modelSelector) {
                    this.modelSelector.selectModel(settings.model, false);
                }
        }
        
        // Load max tokens setting - 现在直接设置输入框值
            if (settings.maxTokens) {
                this.maxTokens.value = settings.maxTokens;
                this.updateTokenValueDisplay();
                this.highlightActivePreset();
            }
        
        // Load reasoning depth & think budget settings
        if (settings.reasoningDepth) {
            this.reasoningDepthSelect.value = settings.reasoningDepth;
                // 更新推理深度选项UI
                this.updateReasoningOptionUI(settings.reasoningDepth);
        }
        
        // 加载豆包思考模式设置
        if (settings.doubaoThinkingMode && this.doubaoThinkingModeSelect) {
            this.doubaoThinkingModeSelect.value = settings.doubaoThinkingMode;
            // 更新豆包思考选项UI
            this.updateDoubaoThinkingOptionUI(settings.doubaoThinkingMode);
        }
        
        // 加载思考预算百分比
        const thinkBudgetPercent = parseInt(settings.thinkBudgetPercent || '50');
        if (this.thinkBudgetPercentInput) {
            this.thinkBudgetPercentInput.value = thinkBudgetPercent;
        }
        
            // 更新思考预算显示和滑块背景
        this.updateThinkBudgetDisplay();
            this.updateThinkBudgetSliderBackground();
            this.highlightActiveThinkPreset();
        
            // Load temperature setting
        if (settings.temperature) {
            this.temperatureInput.value = settings.temperature;
            }
            
            // 先记录用户设置的提示词ID(如果有)
            if (settings.currentPromptId) {
                this.currentPromptId = settings.currentPromptId;
            }
            
            // 如果系统提示词内容已保存在设置中,先恢复它
        if (settings.systemPrompt) {
            this.systemPromptInput.value = settings.systemPrompt;
        }
        
        if (settings.language) {
            this.languageInput.value = settings.language;
        }
        
        // Load proxy settings
        if (settings.proxyEnabled !== undefined) {
            this.proxyEnabledInput.checked = settings.proxyEnabled;
            this.proxySettings.style.display = settings.proxyEnabled ? 'block' : 'none';
        }
        
        if (settings.proxyHost) {
            this.proxyHostInput.value = settings.proxyHost;
        }
        
        if (settings.proxyPort) {
            this.proxyPortInput.value = settings.proxyPort;
        }
        
        // Load OCR source setting
        if (settings.ocrSource) {
            this.ocrSource = settings.ocrSource;
            if (this.ocrSourceSelect) {
                this.ocrSourceSelect.value = settings.ocrSource;
            }
        }
        
        // Update UI based on model type
        this.updateUIBasedOnModelType();
            
        } catch (error) {
            console.error('加载设置出错:', error);
            window.uiManager?.showToast('加载设置出错', 'error');
        }
    }

    modelExists(modelId) {
        return this.modelDefinitions.hasOwnProperty(modelId);
    }

    // 更新模型版本显示
    updateModelVersionDisplay(modelId) {
        const modelVersionInfo = document.getElementById('modelVersionInfo');
        const modelVersionText = document.getElementById('modelVersionText');
        if (!modelVersionText || !modelVersionInfo) return;
        
        const model = this.modelDefinitions[modelId];
        if (!model) {
            modelVersionText.textContent = '-';
            modelVersionInfo.classList.remove('has-version');
            return;
        }
        
        // 显示版本信息(如果有)
        if (model.version && model.version !== 'latest') {
            // 设置版本文本
            modelVersionText.textContent = model.version;
            // 添加具有版本的类
            modelVersionInfo.classList.add('has-version');
            
            // 统一使用分支图标和紫色
            const icon = modelVersionInfo.querySelector('i');
            if (icon) {
                icon.className = 'fas fa-code-branch';
                icon.title = `版本 ${model.version}`;
            }
            
            // 移除所有特定版本类型的类
            modelVersionInfo.classList.remove('date-version', 'semantic-version');
        } else if (model.version === 'latest') {
            // 使用英文"latest"而不是中文
            modelVersionText.textContent = 'latest';
            modelVersionInfo.classList.add('has-version');
            
            // 对latest版本也使用相同的分支图标
            const icon = modelVersionInfo.querySelector('i');
            if (icon) {
                icon.className = 'fas fa-code-branch';
                icon.title = '最新版本';
            }
            
            // 移除所有特定版本类型的类
            modelVersionInfo.classList.remove('date-version', 'semantic-version');
        } else {
            modelVersionText.textContent = '-';
            modelVersionInfo.classList.remove('has-version', 'date-version', 'semantic-version');
        }
    }

    /**
     * 根据选择的模型类型更新UI显示
     */
    updateUIBasedOnModelType() {
        // 更新UI元素显示,根据所选模型类型
        const selectedModel = this.modelSelect.value;
        const modelInfo = this.modelDefinitions[selectedModel];
        
        // 更新当前可见的API密钥
        this.updateVisibleApiKey(selectedModel);
        
        if (!modelInfo) return;
        
        // 处理温度设置显示
        if (this.temperatureGroup) {
            this.temperatureGroup.style.display = modelInfo.isReasoning ? 'none' : 'block';
        }
        
        // 处理深度推理设置显示
        const isAnthropicReasoning = modelInfo.isReasoning && modelInfo.provider === 'anthropic';
        
        // 只有对Claude 3.7 Sonnet这样的Anthropic推理模型才显示深度推理设置
        if (this.reasoningSettingGroup) {
            this.reasoningSettingGroup.style.display = isAnthropicReasoning ? 'block' : 'none';
        }
        
        // 只有当启用深度推理且是Anthropic推理模型时才显示思考预算设置
        if (this.thinkBudgetGroup) {
            const showThinkBudget = isAnthropicReasoning && 
                                    this.reasoningDepthSelect && 
                                    this.reasoningDepthSelect.value === 'extended';
            this.thinkBudgetGroup.style.display = showThinkBudget ? 'block' : 'none';
        }
        
        // 处理豆包深度思考设置显示
        const isDoubaoReasoning = modelInfo.isReasoning && modelInfo.provider === 'doubao';
        
        // 只有对豆包推理模型才显示深度思考设置
        if (this.doubaoThinkingGroup) {
            this.doubaoThinkingGroup.style.display = isDoubaoReasoning ? 'block' : 'none';
        }
        
        // 控制最大Token设置的显示
        // 阿里巴巴模型不支持自定义Token设置
        const maxTokensGroup = this.maxTokens ? this.maxTokens.closest('.setting-group') : null;
        if (maxTokensGroup) {
            // 如果是阿里巴巴模型,隐藏Token设置
            const isAlibabaModel = modelInfo.provider === 'alibaba';
            maxTokensGroup.style.display = isAlibabaModel ? 'none' : 'block';
        }
        
        // 更新模型版本显示
        this.updateModelVersionDisplay(selectedModel);
    }

    /**
     * 根据选择的模型更新显示的API密钥
     * @param {string} modelType 模型类型
     */
    updateVisibleApiKey(modelType) {
        // 高亮显示对应的API密钥
        const allApiKeys = document.querySelectorAll('.api-key-status');
        
        // 先清除所有高亮
        allApiKeys.forEach(key => {
            key.classList.remove('highlight');
        });
        
        // 根据模型类型高亮相应的API密钥
        let apiKeyToHighlight = null;
        
        if (modelType && modelType.toLowerCase().includes('claude')) {
            apiKeyToHighlight = document.querySelector('.api-key-status:nth-child(1)'); // Anthropic
        } else if (modelType && (modelType.toLowerCase().includes('gpt') || modelType.toLowerCase().includes('openai'))) {
            apiKeyToHighlight = document.querySelector('.api-key-status:nth-child(2)'); // OpenAI
        } else if (modelType && modelType.toLowerCase().includes('deepseek')) {
            apiKeyToHighlight = document.querySelector('.api-key-status:nth-child(3)'); // DeepSeek
        } else if (modelType && (modelType.toLowerCase().includes('qwen') || modelType.toLowerCase().includes('qvq') || modelType.toLowerCase().includes('alibaba'))) {
            apiKeyToHighlight = document.querySelector('.api-key-status:nth-child(4)'); // Alibaba
        } else if (modelType && (modelType.toLowerCase().includes('gemini') || modelType.toLowerCase().includes('google'))) {
            apiKeyToHighlight = document.querySelector('.api-key-status:nth-child(5)'); // Google
        } else if (modelType && modelType.toLowerCase().includes('doubao')) {
            apiKeyToHighlight = document.querySelector('.api-key-status:nth-child(6)'); // 豆包
        }
        
        if (apiKeyToHighlight) {
            apiKeyToHighlight.classList.add('highlight');
        }
    }

    async saveSettings() {
        try {
            // 保存UI设置到localStorage(不包含API密钥)
        const settings = {
                apiKeys: this.apiKeyValues, // 保存到localStorage(向后兼容)
                apiBaseUrlValues: this.apiBaseUrlValues, // 添加API基础URL保存到localStorage
            model: this.modelSelect.value,
                maxTokens: this.maxTokens.value,
            reasoningDepth: this.reasoningDepthSelect?.value || 'standard',
            doubaoThinkingMode: this.doubaoThinkingModeSelect?.value || 'auto',
            thinkBudgetPercent: this.thinkBudgetPercentInput?.value || '50',
            temperature: this.temperatureInput.value,
            language: this.languageInput.value,
            systemPrompt: this.systemPromptInput.value,
            currentPromptId: this.currentPromptId,
            proxyEnabled: this.proxyEnabledInput.checked,
            proxyHost: this.proxyHostInput.value,
            proxyPort: this.proxyPortInput.value,
            ocrSource: this.ocrSource // 添加OCR源配置保存
        };

            // 保存设置到localStorage
        localStorage.setItem('aiSettings', JSON.stringify(settings));
            
            window.uiManager?.showToast('设置已保存', 'success');
        } catch (error) {
            console.error('保存设置出错:', error);
            window.uiManager?.showToast('保存设置出错: ' + error.message, 'error');
        }
    }

    getApiKey() {
        const selectedModel = this.modelSelect.value;
        const modelInfo = this.modelDefinitions[selectedModel];
        
        if (!modelInfo) return '';
        
        const apiKeyId = modelInfo.apiKeyId;
        const apiKey = this.apiKeyInputs[apiKeyId]?.value;
        
        if (!apiKey) {
            window.showToast('Please enter API key for the selected model', 'error');
            return '';
        }
        
        return apiKey;
    }

    getSettings() {
        const language = this.languageInput.value || '中文';
        const basePrompt = this.systemPromptInput.value || '';
        
        // 直接使用带语言提示的系统提示词
        // 注意:systemPromptInput.value 可能已经在 loadPrompt 中设置了语言提示
        // 为避免重复添加,我们先提取提示词的主体部分(不含语言提示)
        const promptMainPart = basePrompt.split("\n\n请务必使用")[0];
        const systemPrompt = `${promptMainPart}\n\n请务必使用${language}回答。`;
        
        const selectedModel = this.modelSelect.value;
        const modelInfo = this.modelDefinitions[selectedModel] || {};
        
        // 获取最大Token数
        const maxTokens = parseInt(this.maxTokens?.value || '8192');
        
        // 获取推理深度设置
        const reasoningDepth = this.reasoningDepthSelect?.value || 'standard';
        const thinkBudgetPercent = parseInt(this.thinkBudgetPercentInput?.value || '50');
        
        // 获取豆包思考模式设置
        const doubaoThinkingMode = this.doubaoThinkingModeSelect?.value || 'auto';
        
        // 计算思考预算的实际Token数
        const thinkBudget = Math.floor(maxTokens * (thinkBudgetPercent / 100));
        
        // 构建推理配置参数
        const reasoningConfig = {};
        
        // 处理不同模型的推理配置
        if (modelInfo.isReasoning) {
            // 对于Anthropic模型
            if (modelInfo.provider === 'anthropic') {
                if (reasoningDepth === 'extended') {
                    reasoningConfig.reasoning_depth = 'extended';
                    reasoningConfig.think_budget = thinkBudget;
                } else {
                    reasoningConfig.speed_mode = 'instant';
                }
            }
            
            // 对于豆包模型
            if (modelInfo.provider === 'doubao') {
                reasoningConfig.thinking_mode = doubaoThinkingMode;
            }
        }
        
        // 从apiKeyValues获取Mathpix信息,而不是直接从DOM读取
        const mathpixAppId = this.apiKeyValues['MathpixAppId'] || '';
        const mathpixAppKey = this.apiKeyValues['MathpixAppKey'] || '';
        const mathpixApiKey = mathpixAppId && mathpixAppKey ? `${mathpixAppId}:${mathpixAppKey}` : '';
        
        // 从apiBaseUrlValues映射到服务器API所需格式
        const apiBaseUrls = {};
        if (this.apiBaseUrlValues) {
            if (this.apiBaseUrlValues['AnthropicApiBaseUrl']) {
                apiBaseUrls.anthropic = this.apiBaseUrlValues['AnthropicApiBaseUrl'];
            }
            if (this.apiBaseUrlValues['OpenaiApiBaseUrl']) {
                apiBaseUrls.openai = this.apiBaseUrlValues['OpenaiApiBaseUrl'];
            }
            if (this.apiBaseUrlValues['DeepseekApiBaseUrl']) {
                apiBaseUrls.deepseek = this.apiBaseUrlValues['DeepseekApiBaseUrl'];
            }
            if (this.apiBaseUrlValues['AlibabaApiBaseUrl']) {
                apiBaseUrls.alibaba = this.apiBaseUrlValues['AlibabaApiBaseUrl'];
            }
            if (this.apiBaseUrlValues['GoogleApiBaseUrl']) {
                apiBaseUrls.google = this.apiBaseUrlValues['GoogleApiBaseUrl'];
            }
            if (this.apiBaseUrlValues['DoubaoApiBaseUrl']) {
                apiBaseUrls.doubao = this.apiBaseUrlValues['DoubaoApiBaseUrl'];
            }
        }
        
        return {
            model: selectedModel,
            maxTokens: maxTokens,
            temperature: this.temperatureInput.value,
            language: language,
            systemPrompt: systemPrompt,
            proxyEnabled: this.proxyEnabledInput.checked,
            proxyHost: this.proxyHostInput.value,
            proxyPort: this.proxyPortInput.value,
            mathpixApiKey: mathpixApiKey,
            ocrSource: this.ocrSource, // 添加OCR源配置
            doubaoThinkingMode: doubaoThinkingMode, // 添加豆包思考模式配置
            modelInfo: {
                supportsMultimodal: modelInfo.supportsMultimodal || false,
                isReasoning: modelInfo.isReasoning || false,
                provider: modelInfo.provider || 'unknown'
            },
            reasoningConfig: reasoningConfig,
            apiBaseUrls: apiBaseUrls,
            apiKeys: this.apiKeyValues // 确保传递API密钥
        };
    }

    getModelCapabilities(modelId) {
        const model = this.modelDefinitions[modelId];
        if (!model) return { supportsMultimodal: false, isReasoning: false };
        
        return {
            supportsMultimodal: model.supportsMultimodal,
            isReasoning: model.isReasoning
        };
    }

    setupEventListeners() {
        this.modelSelect.addEventListener('change', (e) => {
            this.updateVisibleApiKey(e.target.value);
            this.updateUIBasedOnModelType();
            this.updateModelVersionDisplay(e.target.value);
            this.saveSettings();
            
            // 通知应用更新图像操作按钮
            if (window.app && typeof window.app.updateImageActionButtons === 'function') {
                window.app.updateImageActionButtons();
            }
        });

        // 提示词相关事件监听
        if (this.promptSelect) {
            this.promptSelect.addEventListener('change', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 加载选中的提示词
                this.loadPrompt(e.target.value);
            });
        }
        
        // 保存提示词按钮
        if (this.savePromptBtn) {
            this.savePromptBtn.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 打开编辑对话框
                this.openEditPromptDialog();
            });
        }
        
        // 新建提示词按钮
        if (this.newPromptBtn) {
            this.newPromptBtn.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 打开新建对话框
                this.openNewPromptDialog();
            });
        }
        
        // 删除提示词按钮
        if (this.deletePromptBtn) {
            this.deletePromptBtn.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 删除当前提示词
                this.deletePrompt();
            });
        }
        
        // 提示词对话框相关事件
        if (this.cancelPromptBtn) {
            this.cancelPromptBtn.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 关闭对话框
                this.closePromptDialog();
            });
        }
        
        if (this.confirmPromptBtn) {
            this.confirmPromptBtn.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 保存提示词
                this.savePrompt();
            });
        }
        
        if (this.promptDialogOverlay) {
            this.promptDialogOverlay.addEventListener('click', (e) => {
                // 点击遮罩关闭对话框
                this.closePromptDialog();
            });
        }
        
        // 最大Token输入框事件处理
        if (this.maxTokens) {
            this.maxTokens.addEventListener('input', () => {
                this.updateTokenValueDisplay();
                this.updateTokenSliderBackground();
                this.highlightActivePreset();
            this.saveSettings();
        });

            this.maxTokens.addEventListener('change', () => {
                this.saveSettings();
            });
        }

        // 推理深度选择事件处理 - 新增标签式UI
        if (this.reasoningOptions && this.reasoningOptions.length > 0) {
            this.reasoningOptions.forEach(option => {
                option.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                    // 获取选择的值
                    const value = option.getAttribute('data-value');
                    
                    // 更新隐藏的select元素值
                    if (this.reasoningDepthSelect) {
                        this.reasoningDepthSelect.value = value;
                    }
                    
                    // 更新视觉效果
                    this.reasoningOptions.forEach(opt => {
                        opt.classList.remove('active');
                    });
                    option.classList.add('active');
                    
                    // 更新思考预算组的可见性
                    if (this.thinkBudgetGroup) {
                        const showThinkBudget = value === 'extended';
                        this.thinkBudgetGroup.style.display = showThinkBudget ? 'block' : 'none';
                    }
                
                this.saveSettings();
                });
            });
        }

        // 思考预算预设按钮事件
        if (this.thinkPresets && this.thinkPresets.length > 0) {
            this.thinkPresets.forEach(preset => {
                preset.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                    // 获取预设值
                    const value = parseInt(preset.getAttribute('data-value'));
                    
                    // 更新滑块值
                    if (this.thinkBudgetPercentInput) {
                        this.thinkBudgetPercentInput.value = value;
                        
                        // 更新显示和滑块背景
                        this.updateThinkBudgetDisplay();
                        this.updateThinkBudgetSliderBackground();
                    }
                    
                    // 更新预设按钮样式
                    this.highlightActiveThinkPreset();
                    
                this.saveSettings();
                });
            });
        }

        // 思考预算占比滑块事件处理
        if (this.thinkBudgetPercentInput && this.thinkBudgetPercentValue) {
            this.thinkBudgetPercentInput.addEventListener('input', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 更新思考预算显示
                this.updateThinkBudgetDisplay();
                
                // 更新滑块背景
                this.updateThinkBudgetSliderBackground();
                
                // 更新预设按钮高亮状态
                this.highlightActiveThinkPreset();
            });
            
            this.thinkBudgetPercentInput.addEventListener('change', () => {
                this.saveSettings();
            });
        }

        this.temperatureInput.addEventListener('input', (e) => {
            // 阻止事件冒泡
            e.stopPropagation();
            
            this.saveSettings();
        });
        
        this.languageInput.addEventListener('change', (e) => {
            // 阻止事件冒泡
            e.stopPropagation();
            
            // 如果当前有加载提示词,重新加载它以更新语言
            if (this.currentPromptId && this.prompts[this.currentPromptId]) {
                this.loadPrompt(this.currentPromptId);
            } else {
                // 没有当前提示词,只保存设置
            this.saveSettings();
            }
        });
        
        this.proxyEnabledInput.addEventListener('change', (e) => {
            // 阻止事件冒泡
            e.stopPropagation();
            
            this.proxySettings.style.display = e.target.checked ? 'block' : 'none';
            this.saveSettings();
        });
        
        this.proxyHostInput.addEventListener('change', (e) => {
            // 阻止事件冒泡
            e.stopPropagation();
            this.saveSettings();
        });
        
        this.proxyPortInput.addEventListener('change', (e) => {
            // 阻止事件冒泡
            e.stopPropagation();
            this.saveSettings();
        });

        // OCR源选择器事件监听
        if (this.ocrSourceSelect) {
            this.ocrSourceSelect.addEventListener('change', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                // 更新OCR源配置
                this.ocrSource = e.target.value;
                this.saveSettings();
                
                console.log('OCR源已切换为:', this.ocrSource);
            });
        }

        // Panel visibility
        if (this.settingsToggle) {
        this.settingsToggle.addEventListener('click', () => {
            this.toggleSettingsPanel();
        });
        }

        if (this.closeSettings) {
        this.closeSettings.addEventListener('click', () => {
            this.closeSettingsPanel();
        });
        }
        
        // 确保设置面板自身的点击不会干扰内部操作
        if (this.settingsPanel) {
            const settingsSections = this.settingsPanel.querySelectorAll('.settings-section');
            settingsSections.forEach(section => {
                section.addEventListener('click', (e) => {
                    // 只阻止直接点击设置部分的事件
                    if (e.target === section) {
                        e.stopPropagation();
                    }
                });
            });
            
            // 设置内容区域防止冒泡
            const settingsContent = this.settingsPanel.querySelector('.settings-content');
            if (settingsContent) {
                settingsContent.addEventListener('click', (e) => {
                    // 只阻止直接点击设置内容区域的事件
                    if (e.target === settingsContent) {
                        e.stopPropagation();
                    }
                });
            }
        }

        if (this.tokenPresets) {
            this.tokenPresets.forEach(preset => {
                preset.addEventListener('click', e => {
                    const value = parseInt(e.currentTarget.dataset.value);
                    this.maxTokens.value = value;
                    this.updateTokenValueDisplay();
                    this.highlightActivePreset();
                    this.saveSettings();
                });
            });
        }

        // 主题切换监听
        const themeToggle = document.getElementById('themeToggle');
        if (themeToggle) {
            themeToggle.addEventListener('click', () => {
                const currentTheme = document.documentElement.getAttribute('data-theme') || 'light';
                const newTheme = currentTheme === 'light' ? 'dark' : 'light';
                document.documentElement.setAttribute('data-theme', newTheme);
                localStorage.setItem('theme', newTheme);
                
                // 更新滑块背景
                this.updateTokenSliderBackground();
                this.updateThinkBudgetSliderBackground();
            });
        }

        // 确保自定义模型选择器事件监听器被初始化
        if (this.modelSelectorDisplay && this.modelDropdown) {
            this.initCustomSelectorEvents();
        }
        
        // 初始化API基础URL编辑功能
        this.initApiBaseUrlEditFunctions();
        
        // 初始化API密钥编辑功能
        this.initApiKeyEditFunctions();
        
        // 初始化推理选项事件
        this.initReasoningOptionEvents();
        
        // 初始化豆包思考选项事件
        this.initDoubaoThinkingOptionEvents();
    }
    
    // 初始化推理选项事件
    initReasoningOptionEvents() {
        const reasoningOptions = document.querySelectorAll('.reasoning-option');
        reasoningOptions.forEach(option => {
            option.addEventListener('click', (e) => {
                e.preventDefault();
                e.stopPropagation();
                
                const value = option.getAttribute('data-value');
                if (value && this.reasoningDepthSelect) {
                    // 更新select值
                    this.reasoningDepthSelect.value = value;
                    
                    // 更新UI
                    this.updateReasoningOptionUI(value);
                    
                    // 保存设置
                    this.saveSettings();
                }
            });
        });
    }
    
    // 初始化豆包思考选项事件
    initDoubaoThinkingOptionEvents() {
        const doubaoThinkingOptions = document.querySelectorAll('.doubao-thinking-option');
        doubaoThinkingOptions.forEach(option => {
            option.addEventListener('click', (e) => {
                e.preventDefault();
                e.stopPropagation();
                
                const value = option.getAttribute('data-value');
                if (value && this.doubaoThinkingModeSelect) {
                    // 更新select值
                    this.doubaoThinkingModeSelect.value = value;
                    
                    // 更新UI
                    this.updateDoubaoThinkingOptionUI(value);
                    
                    // 保存设置
                    this.saveSettings();
                }
            });
        });
    }
    
    // 更新豆包思考选项UI
    updateDoubaoThinkingOptionUI(value) {
        const doubaoThinkingOptions = document.querySelectorAll('.doubao-thinking-option');
        doubaoThinkingOptions.forEach(option => {
            const optionValue = option.getAttribute('data-value');
            if (optionValue === value) {
                option.classList.add('active');
            } else {
                option.classList.remove('active');
            }
        });
    }

    // 更新思考预算显示
    updateThinkBudgetDisplay() {
        if (this.thinkBudgetPercentInput && this.thinkBudgetPercentValue) {
            const percent = parseInt(this.thinkBudgetPercentInput.value);
            this.thinkBudgetPercentValue.textContent = `${percent}%`;
        }
    }
    
    // 更新思考预算滑块背景
    updateThinkBudgetSliderBackground() {
        if (!this.thinkBudgetPercentInput) return;
        
        const min = parseInt(this.thinkBudgetPercentInput.min);
        const max = parseInt(this.thinkBudgetPercentInput.max);
        const value = parseInt(this.thinkBudgetPercentInput.value);
        const percentage = ((value - min) / (max - min)) * 100;
        
        // 获取当前主题
        const isDarkMode = document.documentElement.getAttribute('data-theme') === 'dark';
        const primaryColor = isDarkMode ? 'rgba(72, 149, 239, 0.8)' : 'rgba(58, 134, 255, 0.8)';
        const secondaryColor = isDarkMode ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';
        
        this.thinkBudgetPercentInput.style.background = `linear-gradient(to right, 
            ${primaryColor} 0%, 
            ${primaryColor} ${percentage}%, 
            ${secondaryColor} ${percentage}%, 
            ${secondaryColor} 100%)`;
    }
    
    // 更新推理深度选项UI
    updateReasoningOptionUI(value) {
        if (!this.reasoningOptions) return;
        
        this.reasoningOptions.forEach(option => {
            const optionValue = option.getAttribute('data-value');
            if (optionValue === value) {
                option.classList.add('active');
            } else {
                option.classList.remove('active');
            }
        });
        
        // 更新思考预算组的可见性
        if (this.thinkBudgetGroup) {
            const showThinkBudget = value === 'extended';
            this.thinkBudgetGroup.style.display = showThinkBudget ? 'block' : 'none';
        }
    }
    
    // 高亮当前激活的思考预算预设按钮
    highlightActiveThinkPreset() {
        if (!this.thinkPresets || !this.thinkBudgetPercentInput) return;
        
        const value = parseInt(this.thinkBudgetPercentInput.value);
        
        this.thinkPresets.forEach(preset => {
            const presetValue = parseInt(preset.getAttribute('data-value'));
            if (presetValue === value) {
                preset.classList.add('active');
            } else {
                preset.classList.remove('active');
            }
        });
    }

    /**
     * 初始化可折叠内容的交互逻辑
     */
    initCollapsibleContent() {
        const collapsibleHeaders = document.querySelectorAll('.collapsible-header');
        
        collapsibleHeaders.forEach(header => {
            header.addEventListener('click', () => {
                const content = header.nextElementSibling;
                if (content && content.classList.contains('collapsible-content')) {
                    // 切换展开/折叠状态
                    content.classList.toggle('expanded');
                    
                    // 切换箭头方向
                    const arrow = header.querySelector('i.fa-chevron-down, i.fa-chevron-up');
                    if (arrow) {
                        arrow.classList.toggle('fa-chevron-down');
                        arrow.classList.toggle('fa-chevron-up');
                    }
                }
            });
        });
        
        // 默认展开中转 API url 设置区域
        const apiBaseUrlHeader = document.querySelector('.api-url-settings .collapsible-header');
        if (apiBaseUrlHeader) {
            const content = apiBaseUrlHeader.nextElementSibling;
            if (content) {
                content.classList.add('expanded');
                const arrow = apiBaseUrlHeader.querySelector('i.fa-chevron-down');
                if (arrow) {
                    arrow.classList.remove('fa-chevron-down');
                    arrow.classList.add('fa-chevron-up');
                }
            }
        }
    }

    /**
     * 初始化API密钥编辑相关功能
     */
    initApiKeyEditFunctions() {
        // 1. 编辑按钮点击事件
        document.querySelectorAll('.edit-api-key').forEach(button => {
            button.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                const keyType = e.currentTarget.getAttribute('data-key-type');
                const keyStatus = e.currentTarget.closest('.key-status-wrapper');
                
                if (keyStatus) {
                    // 隐藏显示区域
                    const displayArea = keyStatus.querySelector('.key-display');
                    if (displayArea) displayArea.classList.add('hidden');
                    
                    // 显示编辑区域
                    const editArea = keyStatus.querySelector('.key-edit');
                    if (editArea) {
                        editArea.classList.remove('hidden');
                        
                        // 获取当前密钥值并填入输入框
                        const keyInput = editArea.querySelector('.key-input');
                        if (keyInput) {
                            // 从状态文本中获取当前值(如果不是"未设置")
                            const statusElement = keyStatus.querySelector('.key-status');
                            if (statusElement && statusElement.textContent !== '未设置') {
                                keyInput.value = this.apiKeyValues[keyType] || '';
                            } else {
                                keyInput.value = '';
                            }
                            
                            // 聚焦输入框
                            setTimeout(() => {
                                keyInput.focus();
                            }, 100);
                        }
                    }
                }
            });
        });
        
        // 2. 保存按钮点击事件
        document.querySelectorAll('.save-api-key').forEach(button => {
            button.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                const keyType = e.currentTarget.getAttribute('data-key-type');
                const keyStatus = e.currentTarget.closest('.key-status-wrapper');
                
                if (keyStatus) {
                    // 获取输入的新密钥值
                    const keyInput = keyStatus.querySelector('.key-input');
                    if (keyInput) {
                        const newValue = keyInput.value.trim();
                        
                        // 保存到内存中
                        this.apiKeyValues[keyType] = newValue;
                        
                        // 创建要保存的API密钥对象
                        const apiKeysToSave = {};
                        apiKeysToSave[keyType] = newValue;
                        
                        // 保存到服务器
                        this.saveApiKey(keyType, newValue, keyStatus);
                        
                        // 隐藏编辑区域
                        const editArea = keyStatus.querySelector('.key-edit');
                        if (editArea) editArea.classList.add('hidden');
                        
                        // 显示状态区域
                        const displayArea = keyStatus.querySelector('.key-display');
                        if (displayArea) displayArea.classList.remove('hidden');
                    }
                }
            });
        });
        
        // 3. 切换密码可见性按钮
        document.querySelectorAll('.toggle-visibility').forEach(button => {
            button.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                const keyInput = e.currentTarget.closest('.key-edit').querySelector('.key-input');
                if (keyInput) {
                    const type = keyInput.type === 'password' ? 'text' : 'password';
                    keyInput.type = type;
                    
                    // 更新图标
                    const icon = e.currentTarget.querySelector('i');
                if (icon) {
                        icon.className = `fas fa-${type === 'password' ? 'eye' : 'eye-slash'}`;
                    }
                }
            });
        });
        
        // 4. 输入框按下Enter保存
        document.querySelectorAll('.key-input').forEach(input => {
            input.addEventListener('keydown', (e) => {
                if (e.key === 'Enter') {
                    // 阻止事件冒泡
                    e.stopPropagation();
                    
                    const saveButton = e.currentTarget.closest('.key-edit').querySelector('.save-api-key');
                    if (saveButton) {
                        saveButton.click();
                    }
                } else if (e.key === 'Escape') {
                    // 阻止事件冒泡
                    e.stopPropagation();
                    
                    // 取消编辑
                    const keyStatus = e.currentTarget.closest('.key-status-wrapper');
                    if (keyStatus) {
                        const editArea = keyStatus.querySelector('.key-edit');
                        if (editArea) editArea.classList.add('hidden');
                        
                        const displayArea = keyStatus.querySelector('.key-display');
                        if (displayArea) displayArea.classList.remove('hidden');
                    }
                }
            });
        });
    }
    
    /**
     * 更新API密钥状态显示
     * @param {Object} apiKeys 密钥对象
     */
    updateApiKeyStatus(apiKeys) {
        if (!this.apiKeysList) return;
        
        // 保存API密钥值到内存中
        for (const [key, value] of Object.entries(apiKeys)) {
            this.apiKeyValues[key] = value;
        }
        
        // 找到所有密钥状态元素
        Object.keys(apiKeys).forEach(keyId => {
            const statusElement = document.getElementById(`${keyId}Status`);
            if (!statusElement) return;
            
            const value = apiKeys[keyId];
            
            if (value && value.trim() !== '') {
                // 显示密钥状态 - 已设置
                statusElement.className = 'key-status set';
                statusElement.innerHTML = `<i class="fas fa-check-circle"></i> 已设置`;
                    } else {
                // 显示密钥状态 - 未设置
                statusElement.className = 'key-status not-set';
                statusElement.innerHTML = `<i class="fas fa-times-circle"></i> 未设置`;
            }
        });
    }
    
    /**
     * 保存单个API密钥
     * @param {string} keyType 密钥类型
     * @param {string} value 密钥值
     * @param {HTMLElement} keyStatus 密钥状态容器
     */
    async saveApiKey(keyType, value, keyStatus) {
        try {
            // 显示保存中状态
            const saveToast = this.createToast('正在保存密钥...', 'info', true);
            
            // 创建要保存的数据对象
            const apiKeysData = {};
            apiKeysData[keyType] = value;
            
            // 发送到服务器
            const response = await fetch('/api/keys', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(apiKeysData)
            });
            
            // 移除保存中提示
            if (saveToast) {
                saveToast.remove();
            }
            
            if (response.ok) {
                const result = await response.json();
                if (result.success) {
                    // 更新密钥状态显示
                    const statusElem = document.getElementById(`${keyType}Status`);
                    if (statusElem) {
                        if (value && value.trim() !== '') {
                            statusElem.className = 'key-status set';
                            statusElem.innerHTML = `<i class="fas fa-check-circle"></i> 已设置`;
                        } else {
                            statusElem.className = 'key-status not-set';
                            statusElem.innerHTML = `<i class="fas fa-times-circle"></i> 未设置`;
                        }
                    }
                    
                    // 改用全局UIManager的showToast方法来显示成功消息
                    if (window.uiManager) {
                        window.uiManager.showToast('密钥已保存', 'success');
                    } else {
                        // 如果UIManager不可用,使用自己的方法作为备选
                    this.createToast('密钥已保存', 'success');
                    }
                } else {
                    if (window.uiManager) {
                        window.uiManager.showToast('保存密钥失败: ' + result.message, 'error');
                } else {
                    this.createToast('保存密钥失败: ' + result.message, 'error');
                    }
                }
            } else {
                if (window.uiManager) {
                    window.uiManager.showToast('无法连接到服务器', 'error');
            } else {
                this.createToast('无法连接到服务器', 'error');
                }
            }
        } catch (error) {
            console.error('保存密钥出错:', error);
            if (window.uiManager) {
                window.uiManager.showToast('保存密钥出错: ' + error.message, 'error');
            } else {
            this.createToast('保存密钥出错: ' + error.message, 'error');
            }
        }
    }

    /**
     * 创建一个Toast提示消息
     * @param {string} message 提示消息内容
     * @param {string} type 提示类型:'success', 'error', 'warning', 'info'
     * @param {boolean} persistent 是否为持久性提示(需要手动关闭)
     */
    createToast(message, type = 'success', persistent = false) {
        const toastContainer = document.querySelector('.toast-container') || this.createToastContainer();
        
        // 创建Toast元素
        const toast = document.createElement('div');
        toast.className = `toast ${type}`;
        if (persistent) {
            toast.classList.add('persistent');
        }
        
        // 设置消息内容
        toast.textContent = message;
        
        // 如果是持久性提示,添加关闭按钮
        if (persistent) {
            const closeBtn = document.createElement('button');
            closeBtn.className = 'toast-close';
            closeBtn.innerHTML = '&times;';
            closeBtn.addEventListener('click', () => {
                toast.remove();
            });
            toast.appendChild(closeBtn);
        }
        
        // 添加到容器
        toastContainer.appendChild(toast);
        
        // 非持久性提示自动消失
        if (!persistent) {
            setTimeout(() => {
                toast.remove();
            }, 3000);
        }
        
        return toast;
    }
    
    /**
     * 创建Toast容器
     * @returns {HTMLElement} Toast容器元素
     */
    createToastContainer() {
        const container = document.createElement('div');
        container.className = 'toast-container';
        document.body.appendChild(container);
        return container;
    }

    /**
     * 刷新API密钥状态
     * 每次加载设置时自动调用,无需用户手动点击按钮
     */
    async refreshApiKeyStatus() {
        try {
            // 先将所有状态显示为"检查中"
            Object.keys(this.apiKeyValues).forEach(keyId => {
                const statusElement = document.getElementById(`${keyId}Status`);
                if (statusElement) {
                    statusElement.className = 'key-status checking';
                    statusElement.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 检查中...';
                }
            });
            
            // 发送请求获取API密钥状态
            const response = await fetch('/api/keys', {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                }
            });
            
            if (response.ok) {
                const apiKeys = await response.json();
                this.updateApiKeyStatus(apiKeys);
                console.log('API密钥状态已刷新');
            } else {
                console.error('刷新API密钥状态失败');
            }
        } catch (error) {
            console.error('刷新API密钥状态出错:', error);
        }
    }

    toggleSettingsPanel() {
        if (this.settingsPanel) {
            this.settingsPanel.classList.toggle('active');
        }
    }

    closeSettingsPanel() {
        if (this.settingsPanel) {
            this.settingsPanel.classList.remove('active');
        }
    }

    /**
     * 从服务器加载提示词列表
     */
    async loadPrompts() {
        try {
            // 从服务器获取提示词配置
            const response = await fetch('/api/prompts');
            
            if (response.ok) {
                // 解析提示词数据
                const prompts = await response.json();
                
                // 存储提示词数据(保持原始顺序)
                this.prompts = prompts;
                // 添加提示词顺序属性,记录从服务器获取的原始顺序
                this.promptsOrder = Object.keys(prompts);
                
                // 更新提示词选择下拉框
                if (this.promptSelect) {
                    this.updatePromptSelect();
                }
                
                // 如果当前已有选中的提示词,尝试加载它
                if (this.currentPromptId && this.prompts[this.currentPromptId]) {
                    this.loadPrompt(this.currentPromptId);
                }
                // 否则尝试加载默认提示词
                else if (this.prompts.default) {
                    this.loadPrompt('default');
                }
                // 否则加载第一个提示词
                else if (Object.keys(this.prompts).length > 0) {
                    const promptIdToLoad = Object.keys(this.prompts)[0];
                    this.loadPrompt(promptIdToLoad);
                }
                
                console.log('提示词加载成功:', this.prompts);
            } else {
                console.error('加载提示词失败:', response.statusText);
            }
        } catch (error) {
            console.error('加载提示词错误:', error);
            
            // 显示错误描述
            if (this.promptDescriptionElement) {
                this.promptDescriptionElement.innerHTML = '<p>加载提示词错误,请检查网络连接</p>';
            }
        }
    }
    
    /**
     * 更新提示词选择下拉框
     */
    updatePromptSelect() {
        if (!this.promptSelect) return;
        
        // 暂存当前选中的提示词ID
        const currentPromptId = this.promptSelect.value;
        
        // 清空下拉框
        this.promptSelect.innerHTML = '';
        
        // 按prompts.json中的原始顺序添加提示词选项
        // 使用this.promptsOrder来保持原始顺序
        if (this.promptsOrder && this.promptsOrder.length > 0) {
            for (const promptId of this.promptsOrder) {
                if (this.prompts[promptId]) {
                    const prompt = this.prompts[promptId];
                    const option = document.createElement('option');
                    option.value = promptId;
                    option.textContent = prompt.name;
                    this.promptSelect.appendChild(option);
                }
            }
        } else {
            // 如果没有保存顺序,则使用对象键的顺序(不推荐,但作为后备方案)
            for (const promptId in this.prompts) {
                const prompt = this.prompts[promptId];
                const option = document.createElement('option');
                option.value = promptId;
                option.textContent = prompt.name;
                this.promptSelect.appendChild(option);
            }
        }
        
        // 恢复之前选中的提示词或选择第一个提示词
        if (currentPromptId && this.prompts[currentPromptId]) {
            this.promptSelect.value = currentPromptId;
        } else if (this.promptsOrder && this.promptsOrder.length > 0) {
            // 选择原始顺序的第一个提示词
            this.promptSelect.value = this.promptsOrder[0];
            // 更新当前提示词ID和描述显示
            this.loadPrompt(this.promptSelect.value);
        } else if (Object.keys(this.prompts).length > 0) {
            // 如果没有原始顺序,选择第一个提示词
            this.promptSelect.value = Object.keys(this.prompts)[0];
            // 更新当前提示词ID和描述显示
            this.loadPrompt(this.promptSelect.value);
        }
    }
    
    /**
     * 加载指定的提示词
     * @param {string} promptId 提示词ID
     */
    loadPrompt(promptId) {
        if (!this.prompts[promptId]) return;
        
        // 更新当前提示词ID
        this.currentPromptId = promptId;
        
        // 获取当前选择的语言
        const language = this.languageInput.value || '中文';
        
        // 获取原始提示词内容
        const basePrompt = this.prompts[promptId].content;
        
        // 添加语言指令(如果原始提示词中不包含语言指令)
        let systemPrompt = basePrompt;
        if (!basePrompt.includes('Please respond in') && !basePrompt.includes('请用') && !basePrompt.includes('使用')) {
            systemPrompt = `${basePrompt}\n\n请务必使用${language}回答。`;
        }
        
        // 更新提示词输入框 (隐藏,但仍需保存正确的内容)
        this.systemPromptInput.value = systemPrompt;
        
        // 更新提示词描述显示 - 使用完整的系统提示词,包括语言指令
        if (this.promptDescriptionElement) {
            const description = this.prompts[promptId].description || systemPrompt;
            this.promptDescriptionElement.innerHTML = `<p>${description}</p>`;
        }
        
        // 更新提示词选择下拉框
        if (this.promptSelect) {
            this.promptSelect.value = promptId;
        }
        
        // 保存设置
        this.saveSettings();
    }
    
    /**
     * 保存当前提示词到服务器
     * @param {boolean} isNew 是否是新建提示词
     */
    async savePrompt(isNew = false) {
        try {
            // 获取输入的提示词信息
            const promptId = this.promptIdInput.value.trim();
            const promptName = this.promptNameInput.value.trim();
            const promptContent = this.promptContentInput.value.trim();
            const promptDescription = this.promptDescriptionInput.value.trim();
            
            // 验证必填字段
            if (!promptId) {
                window.uiManager?.showToast('提示词ID不能为空', 'error');
                return;
            }
            
            if (!promptName) {
                window.uiManager?.showToast('提示词名称不能为空', 'error');
                return;
            }
            
            if (!promptContent) {
                window.uiManager?.showToast('提示词内容不能为空', 'error');
                return;
            }
            
            // 构建提示词数据
            const promptData = {
                id: promptId,
                name: promptName,
                content: promptContent,
                description: promptDescription
            };
            
            // 发送到服务器
            const response = await fetch('/api/prompts', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(promptData)
            });
            
            if (response.ok) {
                const result = await response.json();
                if (result.success) {
                    // 更新本地提示词列表
                    this.prompts[promptId] = {
                        name: promptName,
                        content: promptContent,
                        description: promptDescription
                    };
                    
                    // 如果是新增提示词,将其添加到顺序数组末尾
                    if (isNew && this.promptsOrder && !this.promptsOrder.includes(promptId)) {
                        this.promptsOrder.push(promptId);
                    }
                    
                    // 更新提示词选择下拉框
                    this.updatePromptSelect();
                    
                    // 加载新保存的提示词
                    this.loadPrompt(promptId);
                    
                    // 关闭对话框
                    this.closePromptDialog();
                    
                    window.uiManager?.showToast('提示词已保存', 'success');
                } else {
                    window.uiManager?.showToast('保存提示词失败: ' + result.error, 'error');
                }
            } else {
                window.uiManager?.showToast('无法连接到服务器', 'error');
            }
        } catch (error) {
            console.error('保存提示词出错:', error);
            window.uiManager?.showToast('保存提示词出错: ' + error.message, 'error');
        }
    }
    
    /**
     * 删除当前提示词
     */
    async deletePrompt() {
        try {
            // 获取当前提示词ID
            const promptId = this.currentPromptId;
            
            if (!promptId || !this.prompts[promptId]) {
                window.uiManager?.showToast('未选择提示词', 'error');
                return;
            }
            
            // 弹窗确认删除
            if (!confirm(`确定要删除提示词 "${this.prompts[promptId].name}" 吗?`)) {
                return;
            }
            
            // 发送到服务器
            const response = await fetch(`/api/prompts/${promptId}`, {
                method: 'DELETE'
            });
            
            if (response.ok) {
                const result = await response.json();
                if (result.success) {
                    // 从顺序数组中移除该提示词
                    if (this.promptsOrder && this.promptsOrder.includes(promptId)) {
                        this.promptsOrder = this.promptsOrder.filter(id => id !== promptId);
                    }
                    
                    // 删除本地提示词
                    delete this.prompts[promptId];
                    
                    // 更新提示词选择下拉框
                    this.updatePromptSelect();
                    
                    // 如果还有其他提示词,加载第一个
                    if (this.promptsOrder && this.promptsOrder.length > 0) {
                        this.loadPrompt(this.promptsOrder[0]);
                    } else if (Object.keys(this.prompts).length > 0) {
                        this.loadPrompt(Object.keys(this.prompts)[0]);
                    } else {
                        // 如果没有提示词了,清空输入框和描述显示
                        this.systemPromptInput.value = '';
                        if (this.promptDescriptionElement) {
                            this.promptDescriptionElement.innerHTML = '<p>暂无提示词,请点击"+"创建新提示词</p>';
                        }
                        this.currentPromptId = '';
                    }
                    
                    window.uiManager?.showToast('提示词已删除', 'success');
                } else {
                    window.uiManager?.showToast('删除提示词失败: ' + result.error, 'error');
                }
            } else {
                window.uiManager?.showToast('无法连接到服务器', 'error');
            }
        } catch (error) {
            console.error('删除提示词出错:', error);
            window.uiManager?.showToast('删除提示词出错: ' + error.message, 'error');
        }
    }
    
    /**
     * 打开新建提示词对话框
     */
    openNewPromptDialog() {
        // 清空输入框
        this.promptIdInput.value = '';
        this.promptNameInput.value = '';
        this.promptContentInput.value = '';
        this.promptDescriptionInput.value = '';
        
        // 启用ID输入框
        this.promptIdInput.disabled = false;
        
        // 显示对话框
        this.promptDialog.classList.add('active');
        this.promptDialogOverlay.classList.add('active');
    }
    
    /**
     * 打开编辑提示词对话框
     */
    openEditPromptDialog() {
        // 获取当前提示词ID
        const promptId = this.currentPromptId;
        
        if (!promptId || !this.prompts[promptId]) {
            // 如果没有选择提示词,但有系统提示词内容,将其作为新提示词
            if (this.systemPromptInput.value.trim()) {
                this.openNewPromptDialog();
                return;
            }
            
            window.uiManager?.showToast('未选择提示词', 'error');
            return;
        }
        
        // 填充输入框
        this.promptIdInput.value = promptId;
        this.promptNameInput.value = this.prompts[promptId].name;
        this.promptContentInput.value = this.prompts[promptId].content;
        this.promptDescriptionInput.value = this.prompts[promptId].description || '';
        
        // 禁用ID输入框(不允许修改ID)
        this.promptIdInput.disabled = true;
        
        // 显示对话框
        this.promptDialog.classList.add('active');
        this.promptDialogOverlay.classList.add('active');
    }
    
    /**
     * 关闭提示词对话框
     */
    closePromptDialog() {
        if (this.promptDialog) {
        this.promptDialog.classList.remove('active');
        }
        
        if (this.promptDialogOverlay) {
        this.promptDialogOverlay.classList.remove('active');
        }
    }

    // 更新token值显示
    updateTokenValueDisplay() {
        const value = parseInt(this.maxTokens.value);
        let displayValue = value.toString();
        
        // 格式化大数字显示
        if (value >= 1000) {
            if (value % 1000 === 0) {
                displayValue = (value / 1000) + 'K';
            } else {
                displayValue = (value / 1000).toFixed(1) + 'K';
            }
        }
        
        this.maxTokensValue.textContent = displayValue;
        this.updateTokenSliderBackground();
    }
    
    // 更新滑块背景
    updateTokenSliderBackground() {
        const min = parseInt(this.maxTokens.min);
        const max = parseInt(this.maxTokens.max);
        const value = parseInt(this.maxTokens.value);
        const percentage = ((value - min) / (max - min)) * 100;
        
        // 获取当前主题
        const isDarkMode = document.documentElement.getAttribute('data-theme') === 'dark';
        const primaryColor = isDarkMode ? 'rgba(72, 149, 239, 0.8)' : 'rgba(58, 134, 255, 0.8)';
        const secondaryColor = isDarkMode ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)';
        
        this.maxTokens.style.background = `linear-gradient(to right, 
            ${primaryColor} 0%, 
            ${primaryColor} ${percentage}%, 
            ${secondaryColor} ${percentage}%, 
            ${secondaryColor} 100%)`;
    }
    
    // 高亮当前激活的预设按钮
    highlightActivePreset() {
        const value = parseInt(this.maxTokens.value);
        
        this.tokenPresets.forEach(preset => {
            const presetValue = parseInt(preset.dataset.value);
            if (presetValue === value) {
                preset.classList.add('active');
            } else {
                preset.classList.remove('active');
            }
        });
    }

    // 从配置文件加载模型定义
    async loadModelConfig() {
        try {
            // 使用API端点获取模型列表
            const response = await fetch('/api/models');
            if (!response.ok) {
                throw new Error(`加载模型列表失败: ${response.status} ${response.statusText}`);
            }
            
            // 获取模型列表
            const modelsList = await response.json();
            
            // 获取提供商配置
            const configResponse = await fetch('/config/models.json');
            if (!configResponse.ok) {
                throw new Error(`加载提供商配置失败: ${configResponse.status} ${configResponse.statusText}`);
            }
            
            const config = await configResponse.json();
            
            // 保存提供商定义
            this.providerDefinitions = config.providers || {};
            
            // 处理模型定义
            this.modelDefinitions = {};
            
            // 从API获取的模型列表创建模型定义
            modelsList.forEach(model => {
                this.modelDefinitions[model.id] = {
                    name: model.display_name,
                    provider: this.getProviderIdByModel(model.id, config),
                    supportsMultimodal: model.is_multimodal,
                    isReasoning: model.is_reasoning,
                    apiKeyId: this.getApiKeyIdByModel(model.id, config),
                    description: model.description,
                    version: this.getVersionByModel(model.id, config)
                };
            });
            
            console.log('模型配置加载成功:', this.modelDefinitions);
        } catch (error) {
            console.error('加载模型配置时出错:', error);
            throw error;
        }
    }
    
    // 从配置中根据模型ID获取提供商ID
    getProviderIdByModel(modelId, config) {
        const modelConfig = config.models[modelId];
        return modelConfig ? modelConfig.provider : 'unknown';
    }
    
    // 从配置中根据模型ID获取API密钥ID
    getApiKeyIdByModel(modelId, config) {
        const modelConfig = config.models[modelId];
        if (!modelConfig) return null;
        
        const providerId = modelConfig.provider;
        const provider = config.providers[providerId];
        return provider ? provider.api_key_id : null;
    }
    
    // 从配置中根据模型ID获取版本信息
    getVersionByModel(modelId, config) {
        const modelConfig = config.models[modelId];
        return modelConfig ? modelConfig.version : 'latest';
    }

    // 设置默认模型定义(当配置加载失败时使用)
    setupDefaultModels() {
        this.providerDefinitions = {
            'anthropic': {
                name: 'Anthropic',
                api_key_id: 'AnthropicApiKey'
            },
            'openai': {
                name: 'OpenAI',
                api_key_id: 'OpenaiApiKey'
            },
            'deepseek': {
                name: 'DeepSeek',
                api_key_id: 'DeepseekApiKey'
            }
        };
        
        this.modelDefinitions = {
            'claude-3-7-sonnet-20250219': {
                name: 'Claude 3.7 Sonnet',
                provider: 'anthropic',
                supportsMultimodal: true,
                isReasoning: true,
                apiKeyId: 'AnthropicApiKey',
                version: '20250219'
            },
            'gpt-4o-2024-11-20': {
                name: 'GPT-4o',
                provider: 'openai',
                supportsMultimodal: true,
                isReasoning: false,
                apiKeyId: 'OpenaiApiKey',
                version: '2024-11-20'
            },
            'deepseek-reasoner': {
                name: 'DeepSeek Reasoner',
                provider: 'deepseek',
                supportsMultimodal: false,
                isReasoning: true,
                apiKeyId: 'DeepseekApiKey',
                version: 'latest'
            }
        };
        
        console.log('使用默认模型配置');
    }

    initializeElements() {
        // Settings panel elements
        this.settingsPanel = document.getElementById('settingsPanel');
        this.modelSelect = document.getElementById('modelSelect');
        this.temperatureInput = document.getElementById('temperature');
        this.temperatureGroup = document.querySelector('.setting-group:has(#temperature)') || 
                              document.querySelector('div.setting-group:has(input[id="temperature"])');
        this.systemPromptInput = document.getElementById('systemPrompt');
        this.promptDescriptionElement = document.getElementById('promptDescription');
        this.languageInput = document.getElementById('language');
        this.proxyEnabledInput = document.getElementById('proxyEnabled');
        this.proxyHostInput = document.getElementById('proxyHost');
        this.proxyPortInput = document.getElementById('proxyPort');
        this.proxySettings = document.getElementById('proxySettings');
        
        // API基础URL相关元素
        this.apiBaseUrlsList = document.getElementById('apiBaseUrlsList');
        
        // 获取所有API基础URL状态元素
        this.apiBaseUrlStatusElements = {
            'AnthropicApiBaseUrl': document.getElementById('AnthropicApiBaseUrlStatus'),
            'OpenaiApiBaseUrl': document.getElementById('OpenaiApiBaseUrlStatus'),
            'DeepseekApiBaseUrl': document.getElementById('DeepseekApiBaseUrlStatus'),
            'AlibabaApiBaseUrl': document.getElementById('AlibabaApiBaseUrlStatus'),
            'GoogleApiBaseUrl': document.getElementById('GoogleApiBaseUrlStatus')
        };
        
        // 提示词管理相关元素
        this.promptSelect = document.getElementById('promptSelect');
        this.savePromptBtn = document.getElementById('savePromptBtn');
        this.newPromptBtn = document.getElementById('newPromptBtn');
        this.deletePromptBtn = document.getElementById('deletePromptBtn');
        
        // 提示词对话框元素
        this.promptDialog = document.getElementById('promptDialog');
        this.promptDialogOverlay = document.getElementById('promptDialogOverlay');
        this.promptIdInput = document.getElementById('promptId');
        this.promptNameInput = document.getElementById('promptName');
        this.promptContentInput = document.getElementById('promptContent');
        this.promptDescriptionInput = document.getElementById('promptDescriptionEdit');
        this.cancelPromptBtn = document.getElementById('cancelPromptBtn');
        this.confirmPromptBtn = document.getElementById('confirmPromptBtn');
        
        // 最大Token设置元素 - 现在是输入框而不是滑块
        this.maxTokens = document.getElementById('maxTokens');
        this.maxTokensValue = document.getElementById('maxTokensValue');
        this.tokenPresets = document.querySelectorAll('.token-preset');
        
        // 理性推理相关元素
        this.reasoningDepthSelect = document.getElementById('reasoningDepth');
        this.reasoningSettingGroup = document.querySelector('.reasoning-setting-group');
        this.thinkBudgetPercentInput = document.getElementById('thinkBudgetPercent');
        this.thinkBudgetPercentValue = document.getElementById('thinkBudgetPercentValue');
        this.thinkBudgetGroup = document.querySelector('.think-budget-group');
        
        // 豆包深度思考相关元素
        this.doubaoThinkingModeSelect = document.getElementById('doubaoThinkingMode');
        this.doubaoThinkingGroup = document.querySelector('.doubao-thinking-group');
        
        // Initialize Mathpix inputs
        this.mathpixAppIdInput = document.getElementById('mathpixAppId');
        this.mathpixAppKeyInput = document.getElementById('mathpixAppKey');
        
        // OCR源选择器
        this.ocrSourceSelect = document.getElementById('ocrSourceSelect');
        
        // API Key elements - 所有的密钥输入框
        this.apiKeyInputs = {
            'AnthropicApiKey': document.getElementById('AnthropicApiKey'),
            'OpenaiApiKey': document.getElementById('OpenaiApiKey'),
            'DeepseekApiKey': document.getElementById('DeepseekApiKey'),
            'AlibabaApiKey': document.getElementById('AlibabaApiKey'),
            'GoogleApiKey': document.getElementById('GoogleApiKey'),
            'mathpixAppId': this.mathpixAppIdInput,
            'mathpixAppKey': this.mathpixAppKeyInput
        };
        
        // API密钥状态显示相关元素
        this.apiKeysList = document.getElementById('apiKeysList');
        
        // 防止API密钥区域的点击事件冒泡
        if (this.apiKeysList) {
            this.apiKeysList.addEventListener('click', (e) => {
                e.stopPropagation();
            });
        }
        
        // Settings toggle elements
        this.settingsToggle = document.getElementById('settingsToggle');
        this.closeSettings = document.getElementById('closeSettings');
        
        // 获取所有密钥输入组元素
        this.apiKeyGroups = document.querySelectorAll('.api-key-group');
        
        // Initialize API key toggle buttons
        document.querySelectorAll('.toggle-api-key').forEach(button => {
            button.addEventListener('click', (e) => {
                const input = e.currentTarget.closest('.input-group').querySelector('input');
                const type = input.type === 'password' ? 'text' : 'password';
                input.type = type;
                const icon = e.currentTarget.querySelector('i');
                if (icon) {
                    icon.className = `fas fa-${type === 'password' ? 'eye' : 'eye-slash'}`;
                }
            });
        });
        
        // 存储API密钥的对象
        this.apiKeyValues = {
            'AnthropicApiKey': '',
            'OpenaiApiKey': '',
            'DeepseekApiKey': '',
            'AlibabaApiKey': '',
            'GoogleApiKey': '',
            'DoubaoApiKey': '',
            'BaiduApiKey': '',
            'BaiduSecretKey': '',
            'MathpixAppId': '',
            'MathpixAppKey': ''
        };

        this.reasoningOptions = document.querySelectorAll('.reasoning-option');
        this.thinkPresets = document.querySelectorAll('.think-preset');
    }

    // 绑定提示词预览区域点击事件
    initPromptPreviewEvents() {
        const promptPreview = document.querySelector('.prompt-preview');
        if (promptPreview) {
            promptPreview.addEventListener('click', () => {
                // 触发保存按钮点击事件,打开编辑对话框
                document.getElementById('savePromptBtn').click();
            });
        }
    }

    /**
     * 打开提示词编辑对话框
     * @param {string|null} promptId 要编辑的提示词ID,为空则表示新建
     */
    openPromptDialog(promptId = null) {
        // 判断是否为新建提示词
        const isNew = !promptId || !this.prompts[promptId];
        
        if (isNew) {
            // 新建提示词 - 清空输入框
            this.promptIdInput.value = '';
            this.promptNameInput.value = '';
            this.promptContentInput.value = '';
            this.promptDescriptionInput.value = '';
            
            // 设置对话框标题
            if (this.promptDialogTitle) {
                this.promptDialogTitle.textContent = '新建提示词';
            }
            
            // 启用ID输入框
            this.promptIdInput.disabled = false;
            
            // 设置保存按钮动作
            this.promptSaveBtn.onclick = () => this.savePrompt(true); // 明确传递isNew=true
        } else {
            // 编辑现有提示词 - 填充现有内容
            this.promptIdInput.value = promptId;
            this.promptNameInput.value = this.prompts[promptId].name;
            this.promptContentInput.value = this.prompts[promptId].content;
            this.promptDescriptionInput.value = this.prompts[promptId].description || '';
            
            // 设置对话框标题
            if (this.promptDialogTitle) {
                this.promptDialogTitle.textContent = '编辑提示词';
            }
            
            // 禁用ID输入框(不允许修改ID)
            this.promptIdInput.disabled = true;
            
            // 设置保存按钮动作
            this.promptSaveBtn.onclick = () => this.savePrompt(false); // 明确传递isNew=false
        }
        
        // 显示对话框
        if (this.promptDialogMask) {
            this.promptDialogMask.classList.remove('hidden');
        }
    }

    /**
     * 刷新API基础URL状态
     */
    async refreshApiBaseUrlStatus() {
        try {
            // 先将所有状态显示为"检查中"
            Object.keys(this.apiBaseUrlValues).forEach(urlId => {
                const statusElement = document.getElementById(`${urlId}Status`);
                if (statusElement) {
                    statusElement.className = 'key-status checking';
                    statusElement.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 检查中...';
                }
            });
            
            // 发送请求获取API基础URL
            const response = await fetch('/api/proxy-api', {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                }
            });
            
            if (response.ok) {
                const proxyApiConfig = await response.json();
                // 提取APIs对象并更新状态
                const apiBaseUrls = {
                    'AnthropicApiBaseUrl': proxyApiConfig.apis?.anthropic || '',
                    'OpenaiApiBaseUrl': proxyApiConfig.apis?.openai || '',
                    'DeepseekApiBaseUrl': proxyApiConfig.apis?.deepseek || '',
                    'AlibabaApiBaseUrl': proxyApiConfig.apis?.alibaba || '',
                    'GoogleApiBaseUrl': proxyApiConfig.apis?.google || '',
                    'DoubaoApiBaseUrl': proxyApiConfig.apis?.doubao || ''
                };
                this.updateApiBaseUrlStatus(apiBaseUrls);
                console.log('API基础URL状态已刷新');
            } else {
                console.error('刷新API基础URL状态失败');
            }
        } catch (error) {
            console.error('刷新API基础URL状态出错:', error);
        }
    }
    
    /**
     * 更新API基础URL状态显示
     * @param {Object} apiBaseUrls 基础URL对象
     */
    updateApiBaseUrlStatus(apiBaseUrls) {
        if (!this.apiBaseUrlsList) return;
        
        // 保存API基础URL值到内存中
        for (const [key, value] of Object.entries(apiBaseUrls)) {
            this.apiBaseUrlValues[key] = value;
        }
        
        // 找到所有基础URL状态元素
        Object.keys(apiBaseUrls).forEach(urlId => {
            const statusElement = document.getElementById(`${urlId}Status`);
            if (!statusElement) return;
            
            const value = apiBaseUrls[urlId];
            
            if (value && value.trim() !== '') {
                // 显示基础URL状态 - 已设置
                statusElement.className = 'key-status set';
                statusElement.innerHTML = `<i class="fas fa-check-circle"></i> 已设置`;
            } else {
                // 显示基础URL状态 - 未设置
                statusElement.className = 'key-status not-set';
                statusElement.innerHTML = `<i class="fas fa-times-circle"></i> 未设置`;
            }
        });
    }
    
    /**
     * 保存单个API基础URL
     * @param {string} urlType URL类型
     * @param {string} value URL值
     * @param {HTMLElement} urlStatus URL状态容器
     */
    async saveApiBaseUrl(urlType, value, urlStatus) {
        try {
            // 显示保存中状态
            const saveToast = this.createToast('正在保存API基础URL...', 'info', true);
            
            // 获取当前中转API配置
            const response = await fetch('/api/proxy-api', {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                }
            });
            
            if (!response.ok) {
                throw new Error('获取现有配置失败');
            }
            
            const config = await response.json();
            
            // 确保apis对象存在
            if (!config.apis) {
                config.apis = {};
            }
            
            // 根据URL类型更新对应的API URL
            switch(urlType) {
                case 'AnthropicApiBaseUrl':
                    config.apis.anthropic = value;
                    break;
                case 'OpenaiApiBaseUrl':
                    config.apis.openai = value;
                    break;
                case 'DeepseekApiBaseUrl':
                    config.apis.deepseek = value;
                    break;
                case 'AlibabaApiBaseUrl':
                    config.apis.alibaba = value;
                    break;
                case 'GoogleApiBaseUrl':
                    config.apis.google = value;
                    break;
                case 'DoubaoApiBaseUrl':
                    config.apis.doubao = value;
                    break;
            }
            
            // 确保启用中转API
            if (value && value.trim() !== '') {
                config.enabled = true;
            }
            
            // 保存到服务器
            const saveResponse = await fetch('/api/proxy-api', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(config)
            });
            
            // 移除保存中提示
            if (saveToast) {
                saveToast.remove();
            }
            
            if (saveResponse.ok) {
                const result = await saveResponse.json();
                if (result.success) {
                    // 更新基础URL状态显示
                    const statusElem = document.getElementById(`${urlType}Status`);
                    if (statusElem) {
                        if (value && value.trim() !== '') {
                            statusElem.className = 'key-status set';
                            statusElem.innerHTML = `<i class="fas fa-check-circle"></i> 已设置`;
                        } else {
                            statusElem.className = 'key-status not-set';
                            statusElem.innerHTML = `<i class="fas fa-times-circle"></i> 未设置`;
                        }
                    }
                    
                    // 保存到内存
                    this.apiBaseUrlValues[urlType] = value;
                    
                    // 显示成功提示
                    this.createToast('API基础URL已保存', 'success');
                } else {
                    this.createToast(`保存失败: ${result.message || '未知错误'}`, 'error');
                }
            } else {
                this.createToast('保存API基础URL失败', 'error');
            }
        } catch (error) {
            console.error('保存API基础URL错误:', error);
            this.createToast(`保存失败: ${error.message || '未知错误'}`, 'error');
        }
    }
    
    /**
     * 初始化API基础URL编辑相关功能
     */
    initApiBaseUrlEditFunctions() {
        // 1. 编辑按钮点击事件
        document.querySelectorAll('.edit-api-base-url').forEach(button => {
            button.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                const urlType = e.currentTarget.getAttribute('data-key-type');
                const urlStatus = e.currentTarget.closest('.key-status-wrapper');
                
                if (urlStatus) {
                    // 隐藏显示区域
                    const displayArea = urlStatus.querySelector('.key-display');
                    if (displayArea) displayArea.classList.add('hidden');
                    
                    // 显示编辑区域
                    const editArea = urlStatus.querySelector('.key-edit');
                    if (editArea) {
                        editArea.classList.remove('hidden');
                        
                        // 获取当前URL值并填入输入框
                        const urlInput = editArea.querySelector('.key-input');
                        if (urlInput) {
                            // 从状态文本中获取当前值(如果不是"未设置")
                            const statusElement = urlStatus.querySelector('.key-status');
                            if (statusElement && statusElement.textContent !== '未设置') {
                                urlInput.value = this.apiBaseUrlValues[urlType] || '';
                            } else {
                                urlInput.value = '';
                            }
                            
                            // 聚焦输入框
                            setTimeout(() => {
                                urlInput.focus();
                            }, 100);
                        }
                    }
                }
            });
        });
        
        // 2. 保存按钮点击事件
        document.querySelectorAll('.save-api-base-url').forEach(button => {
            button.addEventListener('click', (e) => {
                // 阻止事件冒泡
                e.stopPropagation();
                
                const urlType = e.currentTarget.getAttribute('data-key-type');
                const urlStatus = e.currentTarget.closest('.key-status-wrapper');
                
                if (urlStatus) {
                    // 获取输入的新URL值
                    const urlInput = urlStatus.querySelector('.key-input');
                    if (urlInput) {
                        const newValue = urlInput.value.trim();
                        
                        // 保存到服务器
                        this.saveApiBaseUrl(urlType, newValue, urlStatus);
                        
                        // 隐藏编辑区域
                        const editArea = urlStatus.querySelector('.key-edit');
                        if (editArea) editArea.classList.add('hidden');
                        
                        // 显示状态区域
                        const displayArea = urlStatus.querySelector('.key-display');
                        if (displayArea) displayArea.classList.remove('hidden');
                    }
                }
            });
        });
        
        // 3. 输入框按下Enter保存
        document.querySelectorAll('#apiBaseUrlsList .key-input').forEach(input => {
            input.addEventListener('keydown', (e) => {
                if (e.key === 'Enter') {
                    // 阻止事件冒泡
                    e.stopPropagation();
                    
                    const saveButton = e.currentTarget.closest('.key-edit').querySelector('.save-api-base-url');
                    if (saveButton) {
                        saveButton.click();
                    }
                } else if (e.key === 'Escape') {
                    // 阻止事件冒泡
                    e.stopPropagation();
                    
                    // 取消编辑
                    const urlStatus = e.currentTarget.closest('.key-status-wrapper');
                    if (urlStatus) {
                        const editArea = urlStatus.querySelector('.key-edit');
                        if (editArea) editArea.classList.add('hidden');
                        
                        const displayArea = urlStatus.querySelector('.key-display');
                        if (displayArea) displayArea.classList.remove('hidden');
                    }
                }
            });
        });
    }
}

// Export for use in other modules
window.SettingsManager = SettingsManager;