-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1101 lines (968 loc) · 39.3 KB
/
index.html
File metadata and controls
1101 lines (968 loc) · 39.3 KB
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>X-WisDom Pro - Max</title>
<link rel="stylesheet" href="vendor/fontawesome/css/all.min.css">
<script src="vendor/highlight.min.js"></script>
<script src="vendor/marked.min.js"></script>
<style>
:root {
--primary: #6b46c1;
--primary-light: rgba(107, 70, 193, 0.1);
--bg-rail: #f6f6f9;
--bg-sidebar: #ffffff;
--bg-chat: #fbfbfd;
--text-main: #1d1d1f;
--text-muted: #86868b;
--border: #e5e5e7;
--shadow: 0 8px 30px rgba(0, 0, 0, 0.04);
}
.dark-mode {
--bg-rail: #0f0f10;
--bg-sidebar: #18181a;
--bg-chat: #0f0f10;
--text-main: #f5f5f7;
--text-muted: #86868b;
--border: #2d2d2f;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: var(--bg-chat);
color: var(--text-main);
font-family: -apple-system, system-ui, sans-serif;
height: 100vh;
display: flex;
overflow: hidden;
}
/* --- Rail: 导航条 --- */
.rail {
width: 68px;
background: var(--bg-rail);
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
align-items: center;
padding: 20px 0;
gap: 15px;
flex-shrink: 0;
z-index: 100;
}
.rail-btn {
width: 44px;
height: 44px;
border-radius: 12px;
display: flex;
align-items: center;
justify-content: center;
color: var(--text-muted);
cursor: pointer;
transition: 0.2s;
font-size: 18px;
}
.rail-btn:hover {
background: var(--primary-light);
color: var(--primary);
}
.rail-btn.active {
background: var(--primary);
color: white;
}
.rail-bottom {
margin-top: auto;
display: flex;
flex-direction: column;
gap: 10px;
}
/* --- Sidebar: 侧边栏 --- */
.sidebar {
width: 260px;
background: var(--bg-sidebar);
border-right: 1px solid var(--border);
display: flex;
flex-direction: column;
transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
overflow: hidden;
flex-shrink: 0;
}
.sidebar.collapsed {
width: 0;
border-right: none;
}
.sidebar-inner {
width: 260px;
height: 100%;
display: flex;
flex-direction: column;
}
.sidebar-header {
padding: 25px 18px;
}
.new-chat-btn {
width: 100%;
padding: 12px;
border: 1px solid var(--border);
background: var(--bg-sidebar);
border-radius: 12px;
font-weight: 500;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
color: var(--text-main);
white-space: nowrap;
}
#chatList {
flex: 1;
overflow-y: auto;
padding: 0 10px;
}
.chat-item {
padding: 12px 15px;
margin: 4px 0;
border-radius: 10px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 14px;
transition: 0.2s;
color: var(--text-muted);
}
.chat-item.active {
background: var(--primary-light);
color: var(--primary);
font-weight: 600;
}
/* --- Main: 聊天区 --- */
.main {
flex: 1;
display: flex;
flex-direction: column;
position: relative;
min-width: 0;
}
header {
height: 64px;
border-bottom: 1px solid var(--border);
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 30px;
background: rgba(255, 255, 255, 0.11);
backdrop-filter: blur(20px);
z-index: 10;
}
#chatBox {
flex: 1;
overflow-y: auto;
padding: 40px 18% 180px 18%;
display: flex;
flex-direction: column;
gap: 30px;
scroll-behavior: smooth;
}
/* 消息泡 */
.message {
display: flex;
gap: 18px;
line-height: 1.6;
animation: fadeIn 0.3s ease;
}
.message.user {
flex-direction: row-reverse;
}
.avatar {
width: 36px;
height: 36px;
border-radius: 10px;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 18px;
}
.user .avatar {
background: var(--primary);
color: white;
}
.ai .avatar {
background: white;
border: 1px solid var(--border);
color: var(--primary);
box-shadow: var(--shadow);
}
.content {
max-width: 85%;
font-size: 15px;
}
/* AI思考过程样式 */
.thinking-indicator {
color: var(--text-muted);
font-size: 14px;
padding: 8px 0;
display: flex;
align-items: center;
gap: 8px;
}
.thinking-indicator .dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--text-muted);
display: inline-block;
animation: pulse 1.5s infinite;
}
.thinking-indicator .dot:nth-child(2) {
animation-delay: 0.3s;
}
.thinking-indicator .dot:nth-child(3) {
animation-delay: 0.6s;
}
@keyframes pulse {
0%,
100% {
opacity: 0.4;
transform: scale(0.1);
}
50% {
opacity: 1;
transform: scale(1.1);
}
}
.thinking-section {
background: rgba(107, 70, 193, 0.05);
border-left: 3px solid var(--primary);
padding: 12px 15px;
margin: 10px 0;
border-radius: 0 8px 8px 0;
font-size: 14px;
color: var(--text-main);
}
.answer-section {
padding-top: 10px;
}
/* 文档搁板与快捷短语 */
.doc-shelf {
position: absolute;
bottom: 145px;
left: 50%;
transform: translateX(-50%);
width: 75%;
max-width: 800px;
display: flex;
gap: 8px;
overflow-x: auto;
z-index: 5;
}
.doc-tag {
background: var(--primary-light);
color: var(--primary);
border: 1px solid var(--primary);
padding: 4px 10px;
border-radius: 8px;
font-size: 11px;
display: flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.quick-phrases {
position: absolute;
bottom: 105px;
left: 50%;
transform: translateX(-50%);
width: 75%;
max-width: 800px;
display: flex;
gap: 8px;
overflow-x: auto;
z-index: 5;
}
.phrase-tag {
white-space: nowrap;
padding: 6px 14px;
background: var(--bg-sidebar);
border: 1px solid var(--border);
border-radius: 20px;
font-size: 12px;
cursor: pointer;
}
/* 输入框 */
.input-area {
position: absolute;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
width: 75%;
max-width: 800px;
background: var(--bg-sidebar);
border: 1px solid var(--border);
border-radius: 20px;
padding: 10px 18px;
display: flex;
align-items: flex-end;
gap: 12px;
box-shadow: var(--shadow);
z-index: 5;
}
#userInput {
flex: 1;
border: none;
background: transparent;
padding: 10px 0;
outline: none;
resize: none;
max-height: 180px;
font-size: 15px;
color: var(--text-main);
}
.tool-btn {
width: 38px;
height: 38px;
border-radius: 12px;
border: none;
background: transparent;
color: var(--text-muted);
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.tool-btn.active {
color: #22c55e;
}
.send-btn.active {
background: var(--primary);
color: white;
}
/* 设置面板 */
.panel-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.1);
backdrop-filter: blur(5px);
display: none;
z-index: 1000;
}
.settings-panel {
position: fixed;
right: -450px;
top: 0;
width: 420px;
height: 100vh;
background: var(--bg-sidebar);
border-left: 1px solid var(--border);
z-index: 1001;
transition: 0.4s cubic-bezier(0.16, 1, 0.3, 1);
padding: 35px;
overflow-y: auto;
}
.settings-panel.active {
right: 0;
}
/* 关于面板样式 */
.about-panel {
position: fixed;
right: -450px;
top: 0;
width: 420px;
height: 100vh;
background: var(--bg-sidebar);
border-left: 1px solid var(--border);
z-index: 1001;
transition: 0.4s cubic-bezier(0.16, 1, 0.3, 1);
padding: 35px;
overflow-y: auto;
}
.about-panel.active {
right: 0;
}
.card {
background: var(--bg-rail);
border-radius: 16px;
padding: 18px;
margin-bottom: 15px;
}
.card h4 {
font-size: 11px;
color: var(--text-muted);
text-transform: uppercase;
margin-bottom: 12px;
}
.card input,
.card select,
.card textarea {
width: 100%;
background: transparent;
border: none;
border-bottom: 1px solid var(--border);
padding: 8px 0;
color: var(--text-main);
font-size: 14px;
outline: none;
margin-bottom: 12px;
}
.assistant-item {
padding: 10px;
background: var(--bg-sidebar);
border: 1px solid var(--border);
border-radius: 8px;
cursor: pointer;
font-size: 12px;
margin-bottom: 6px;
display: flex;
justify-content: space-between;
align-items: center;
}
/* 关于页面样式 */
.about-content {
padding: 20px;
}
.about-logo {
text-align: center;
margin-bottom: 30px;
}
.about-logo h1 {
font-size: 28px;
margin-bottom: 10px;
color: var(--primary);
}
.about-logo .version {
background: var(--primary-light);
color: var(--primary);
padding: 3px 10px;
border-radius: 20px;
font-size: 12px;
display: inline-block;
}
.about-features {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
margin: 20px 0;
}
.feature-card {
background: var(--bg-rail);
padding: 15px;
border-radius: 12px;
text-align: center;
}
.feature-card i {
font-size: 24px;
color: var(--primary);
margin-bottom: 10px;
}
.about-footer {
margin-top: 30px;
text-align: center;
font-size: 13px;
color: var(--text-muted);
}
.about-footer a {
color: var(--primary);
text-decoration: none;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@media (max-width: 900px) {
.sidebar {
position: absolute;
left: 68px;
height: 100%;
z-index: 50;
}
.input-area,
.quick-phrases {
width: 92%;
}
}
</style>
</head>
<body>
<nav class="rail">
<div class="rail-btn" onclick="toggleSidebar()"><i class="fas fa-bars"></i></div>
<div class="rail-btn active"><i class="fas fa-comment-dots"></i></div>
<div class="rail-btn" id="configBtn"><i class="fas fa-sliders-h"></i></div>
<div class="rail-btn" id="themeBtn"><i class="fas fa-moon"></i></div>
<div class="rail-bottom">
<div class="rail-btn" id="aboutBtn"><i class="fas fa-info-circle"></i></div>
</div>
</nav>
<aside class="sidebar" id="sidebar">
<div class="sidebar-inner">
<div class="sidebar-header">
<button class="new-chat-btn" onclick="newChat()"><i class="fas fa-plus"></i> 新建对话</button>
</div>
<div id="chatList"></div>
</div>
</aside>
<main class="main">
<header>
<div style="display:flex; flex-direction:column">
<b id="chatTitle">新对话</b>
<span id="modelLabel" style="font-size:10px; color:var(--text-muted)">XWisDom-Chat</span>
</div>
<div id="vendorBadge"
style="font-size: 10px; background: var(--primary); color: white; padding: 4px 12px; border-radius: 20px; font-weight: 700;">
X-WisDom</div>
</header>
<div id="chatBox"></div>
<div class="doc-shelf" id="docShelf"></div>
<div class="quick-phrases" id="quickPhrases">
<div class="phrase-tag" onclick="usePhrase('请基于提供的文档总结核心观点:')">📖 知识总结</div>
<div class="phrase-tag" onclick="usePhrase('帮我润色这段话:')">✨ 润色文字</div>
<div class="phrase-tag" onclick="usePhrase('把这段代码优化一下:')">💻 代码优化</div>
</div>
<div class="input-area">
<input type="file" id="fileUpload" hidden multiple onchange="handleFileUpload(event)">
<button class="tool-btn" onclick="document.getElementById('fileUpload').click()"><i
class="fas fa-paperclip"></i></button>
<button class="tool-btn" id="searchBtn" onclick="toggleSearch()"><i class="fas fa-globe"></i></button>
<textarea id="userInput" placeholder="输入消息..." rows="1"></textarea>
<button class="tool-btn send-btn" id="sendBtn" onclick="handleSend()">
<i class="fas fa-arrow-up" id="sendIcon"></i>
</button>
</div>
</main>
<div class="panel-overlay" id="overlay" onclick="closeAllModals()"></div>
<div class="settings-panel" id="settingsPanel">
<h2 style="margin-bottom: 25px;">Settings</h2>
<div class="card">
<h4>服务商与模型</h4>
<select id="vendorSelector" onchange="applyPreset()">
<option value="deepseek">DeepSeek (深度求索)</option>
<option value="openai">chatGPT (OpenAI)</option>
<option value="anthropic">Anthropic (Claude)</option>
<option value="google">Gemini (Google)</option>
<option value="groq">Groq (XAI)</option>
<option value="aihubmix">AIHubMix</option>
<option value="wofu">无问芯穹 (Infini-AI)</option>
<option value="moonshot">月之暗面 (Kimi)</option>
<option value="alibaba">通义千问 (DashScope)</option>
<option value="custom">Custom (自定义)</option>
</select>
<input type="text" id="baseUrl" placeholder="API 地址">
<input type="password" id="apiKey" placeholder="API Key">
<input type="text" id="modelName" placeholder="模型名称">
</div>
<div class="card">
<h4>助手模板库 (System)</h4>
<textarea id="systemPrompt" rows="3" placeholder="系统提示词..."></textarea>
<div id="assistantLibrary" style="margin-top: 10px;"></div>
<button onclick="saveAsTemplate()"
style="width:100%; border:1px dashed var(--border); background:none; padding:8px; border-radius:8px; font-size:11px; cursor:pointer;">+
另存为助手模板</button>
</div>
<div class="card">
<h4>个性化头像</h4>
<div style="display:flex; gap:10px;">
<input type="text" id="userAvatarSet" placeholder="用户 (如: 👤)">
<input type="text" id="aiAvatarSet" placeholder="AI (如: 🤖)">
</div>
</div>
<div class="card">
<h4>高级参数</h4>
<div style="font-size: 11px; color:var(--text-muted)">Temperature: <span id="v-temp">0.7</span></div>
<input type="range" id="p-temp" min="0" max="2" step="0.1"
oninput="document.getElementById('v-temp').innerText=this.value">
<div style="font-size: 11px; color:var(--text-muted); margin-top:10px;">Max History: <span
id="v-history">12</span></div>
<input type="range" id="p-history" min="1" max="50" step="1"
oninput="document.getElementById('v-history').innerText=this.value">
</div>
<button class="new-chat-btn" style="background:var(--primary); color:white; border:none; padding:15px;"
onclick="saveConfig()">保存设置</button>
</div>
<!-- 新增关于面板 -->
<div class="about-panel" id="aboutPanel">
<div class="about-content">
<div class="about-logo">
<h1>X-WisDom Pro Max</h1>
<span class="version">v2.1.0</span>
</div>
<p style="text-align: center; margin-bottom: 25px; line-height: 1.6">
智能对话助手,支持多种大模型接口<br>
提供高效、专业的知识服务
</p>
<div class="about-features">
<div class="feature-card">
<i class="fas fa-brain"></i>
<h4>多模型支持</h4>
<p>集成DeepSeek、GPT、Claude等主流AI模型</p>
</div>
<div class="feature-card">
<i class="fas fa-code"></i>
<h4>代码智能</h4>
<p>代码优化、调试、解释功能</p>
</div>
<div class="feature-card">
<i class="fas fa-file-alt"></i>
<h4>文档处理</h4>
<p>支持上传文档进行知识提取</p>
</div>
<div class="feature-card">
<i class="fas fa-cogs"></i>
<h4>高级定制</h4>
<p>自定义提示词和个性化设置</p>
</div>
</div>
<div class="card">
<h4>核心能力</h4>
<ul style="padding-left: 20px; font-size: 14px; line-height: 1.8">
<li>多轮上下文对话支持</li>
<li>文档内容提取与总结</li>
<li>实时思考过程可视化</li>
<li>代码语法高亮与优化</li>
<li>个性化助手模板库</li>
</ul>
</div>
<div class="about-footer">
<p>© 2024 X-WisDom Pro Max 智能对话系统</p>
<p>开发者: <a href="github.com/XProit">XProit</a></p>
<p style="margin-top: 10px"><i class="fas fa-heart" style="color:#e74c3c"></i> 用心创造智能体验</p>
</div>
</div>
</div>
<script>
const PRESETS = {
deepseek: { url: 'https://api.deepseek.com', model: 'deepseek-chat' },
openai: { url: 'https://api.openai.com/v1', model: 'gpt-4o-mini' },
anthropic: { url: 'https://api.anthropic.com/v1', model: 'claude-3-5-sonnet-20240620' },
google: { url: 'https://generativelanguage.googleapis.com/v1beta/openai', model: 'gemini-1.5-pro' },
groq: { url: 'https://api.groq.com/openai/v1', model: 'llama3-70b-8192' },
aihubmix: { url: 'https://api.aihubmix.com/v1', model: 'gpt-4o' },
wofu: { url: 'https://api.cloud.infini-ai.com/maas/v1', model: 'yi-lightning' },
moonshot: { url: 'https://api.moonshot.cn/v1', model: 'moonshot-v1-8k' },
alibaba: { url: 'https://dashscope.aliyuncs.com/compatible-mode/v1', model: 'qwen-plus' },
custom: { url: '', model: '' }
};
let chats = JSON.parse(localStorage.getItem('cm_chats')) || [{ id: Date.now(), title: '新对话', history: [] }];
let currentChatId = chats[0].id;
let assistants = JSON.parse(localStorage.getItem('cm_assts')) || [
{ name: '✨ 默认助手', prompt: '你是默认助手,我可以和你聊天了!' },
{ name: '💻 编程专家', prompt: '你是一个精通全栈开发的架构师。在回答代码问题前先分析需求。' }
];
let config = {
vendor: localStorage.getItem('cm_vendor') || 'deepseek',
url: localStorage.getItem('cm_url') || PRESETS.deepseek.url,
key: localStorage.getItem('cm_key') || '',
model: localStorage.getItem('cm_model') || PRESETS.deepseek.model,
system: localStorage.getItem('cm_system') || '你是一个专业的AI助手。在回答之前请先进行思考,并将思考过程放在<thinking>标签内,然后在<answer>标签内给出最终答案。',
temp: localStorage.getItem('cm_temp') || 0.7,
history: localStorage.getItem('cm_history') || 12,
userAvatar: localStorage.getItem('cm_user_av') || '👤',
aiAvatar: localStorage.getItem('cm_ai_av') || '🤖',
sidebarCollapsed: localStorage.getItem('cm_sidebar_collapsed') === 'true'
};
let isGenerating = false, controller = null, isSearching = false, uploadedFiles = [];
function init() {
document.getElementById('vendorSelector').value = config.vendor;
document.getElementById('baseUrl').value = config.url;
document.getElementById('apiKey').value = config.key;
document.getElementById('modelName').value = config.model;
// [!] 确保系统提示词包含必要指令 [!]
if (!config.system.includes('<thinking>') || !config.system.includes('<answer>')) {
config.system = '你是一个专业的AI助手。在回答之前请先进行思考,并将思考过程放在<thinking>标签内,然后在<answer>标签内给出最终答案。\n\n' + config.system;
localStorage.setItem('cm_system', config.system);
}
document.getElementById('systemPrompt').value = config.system;
document.getElementById('userAvatarSet').value = config.userAvatar;
document.getElementById('aiAvatarSet').value = config.aiAvatar;
document.getElementById('p-temp').value = config.temp;
document.getElementById('v-temp').innerText = config.temp;
document.getElementById('p-history').value = config.history;
document.getElementById('v-history').innerText = config.history;
if (config.sidebarCollapsed) document.getElementById('sidebar').classList.add('collapsed');
updateHeader();
renderChatList();
renderAssistantLibrary();
switchChat(currentChatId);
}
function saveConfig() {
config.vendor = document.getElementById('vendorSelector').value;
config.url = document.getElementById('baseUrl').value;
config.key = document.getElementById('apiKey').value;
config.model = document.getElementById('modelName').value;
config.system = document.getElementById('systemPrompt').value;
// [!] 确保系统提示词包含必要指令 [!]
if (!config.system.includes('<thinking>') || !config.system.includes('<answer>')) {
config.system = '你是一个专业的AI助手。在回答之前请先进行思考,并将思考过程放在<thinking>标签内,然后在<answer>标签内给出最终答案。\n\n' + config.system;
}
config.userAvatar = document.getElementById('userAvatarSet').value || '👤';
config.aiAvatar = document.getElementById('aiAvatarSet').value || '🤖';
config.temp = document.getElementById('p-temp').value;
config.history = document.getElementById('p-history').value;
localStorage.setItem('cm_vendor', config.vendor);
localStorage.setItem('cm_url', config.url);
localStorage.setItem('cm_key', config.key);
localStorage.setItem('cm_model', config.model);
localStorage.setItem('cm_system', config.system);
localStorage.setItem('cm_user_av', config.userAvatar);
localStorage.setItem('cm_ai_av', config.aiAvatar);
localStorage.setItem('cm_temp', config.temp);
localStorage.setItem('cm_history', config.history);
updateHeader(); closeAllModals();
}
function toggleSidebar() {
const isCollapsed = document.getElementById('sidebar').classList.toggle('collapsed');
localStorage.setItem('cm_sidebar_collapsed', isCollapsed);
}
function applyPreset() {
const v = document.getElementById('vendorSelector').value;
if (PRESETS[v]) {
document.getElementById('baseUrl').value = PRESETS[v].url;
document.getElementById('modelName').value = PRESETS[v].model;
}
}
function switchChat(id) {
currentChatId = id;
const chat = chats.find(c => c.id === id);
const box = document.getElementById('chatBox');
box.innerHTML = chat.history.length ? '' : `<div style="flex:1; display:flex; flex-direction:column; align-items:center; justify-content:center; opacity:0.3"><h2>有什么可以帮您?</h2></div>`;
chat.history.forEach(m => appendMsg(m.role === 'user' ? 'user' : 'ai', m.content));
document.getElementById('chatTitle').innerText = chat.title;
renderChatList();
}
async function handleSend() {
if (isGenerating) { controller.abort(); return; }
const input = document.getElementById('userInput');
const text = input.value.trim();
if (!text || !config.key) return;
const chat = chats.find(c => c.id === currentChatId);
if (chat.history.length === 0) {
chat.title = text.substring(0, 15);
document.getElementById('chatBox').innerHTML = '';
}
appendMsg('user', text);
input.value = ''; input.style.height = 'auto';
isGenerating = true; toggleUI(true);
// 创建AI消息骨架(包含思考指示器)
const aiDiv = createAiMessageSkeleton();
const box = document.getElementById('chatBox');
box.appendChild(aiDiv);
box.scrollTop = box.scrollHeight;
let fullText = "";
controller = new AbortController();
try {
const response = await fetch(`${config.url}/chat/completions`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${config.key}`, 'Content-Type': 'application/json' },
signal: controller.signal,
body: JSON.stringify({
model: config.model,
messages: [{ role: "system", content: config.system }, ...chat.history.slice(-config.history * 2), { role: "user", content: text }],
temperature: parseFloat(config.temp),
stream: true
})
});
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = new TextDecoder().decode(value);
const lines = chunk.split('\n');
for (let line of lines) {
if (line.startsWith('data: ') && line !== 'data: [DONE]') {
try {
const data = JSON.parse(line.substring(6));
fullText += data.choices[0].delta.content || "";
updateAiMessageContent(aiDiv, fullText);
} catch (e) { }
}
}
}
// 最终替换为带格式的思考过程和答案
renderFinalAiMessage(aiDiv, fullText);
chat.history.push({ role: "user", content: text }, { role: "assistant", content: fullText });
localStorage.setItem('cm_chats', JSON.stringify(chats));
} catch (e) {
aiDiv.querySelector('.content').innerHTML = `<span style="color:red">Error: ${e.message}</span>`;
}
finally { isGenerating = false; toggleUI(false); renderChatList(); }
}
function createAiMessageSkeleton() {
const div = document.createElement('div');
div.className = 'message ai';
div.innerHTML = `
<div class="avatar">${config.aiAvatar}</div>
<div class="content">
<div class="thinking-indicator">
<span>思考中</span>
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
</div>`;
return div;
}
function updateAiMessageContent(aiDiv, text) {
const contentDiv = aiDiv.querySelector('.content');
// 检查是否有思考指示器,如果有则移除
const thinkingIndicator = contentDiv.querySelector('.thinking-indicator');
if (thinkingIndicator && text.length > 0) {
thinkingIndicator.remove();
}
// 如果还没有内容,则初始化内容区域
if (!contentDiv.querySelector('.thinking-section') && !contentDiv.querySelector('.answer-section')) {
contentDiv.innerHTML = '';
}
// 尝试解析思考过程和最终答案
const thinkingMatch = text.match(/<thinking>(.*?)<\/thinking>/s);
const answerMatch = text.match(/<answer>(.*?)<\/answer>/s);
let thinkingContent = '';
let answerContent = '';
if (thinkingMatch) {
thinkingContent = thinkingMatch[1];
// 如果已经有思考部分则更新,否则创建
let thinkingSection = contentDiv.querySelector('.thinking-section');
if (!thinkingSection) {
thinkingSection = document.createElement('div');
thinkingSection.className = 'thinking-section';
contentDiv.appendChild(thinkingSection);
}
thinkingSection.innerHTML = marked.parse(thinkingContent);
}
if (answerMatch) {
answerContent = answerMatch[1];
// 如果已经有答案部分则更新,否则创建
let answerSection = contentDiv.querySelector('.answer-section');
if (!answerSection) {
answerSection = document.createElement('div');
answerSection.className = 'answer-section';
contentDiv.appendChild(answerSection);
}
answerSection.innerHTML = marked.parse(answerContent);
} else if (!thinkingMatch && text) {
// 如果没有找到标签,显示所有文本
contentDiv.innerHTML = marked.parse(text);
}
}
function renderFinalAiMessage(aiDiv, fullText) {
const contentDiv = aiDiv.querySelector('.content');
// 解析思考过程和最终答案
const thinkingMatch = fullText.match(/<thinking>(.*?)<\/thinking>/s);
const answerMatch = fullText.match(/<answer>(.*?)<\/answer>/s);
if (thinkingMatch && answerMatch) {
const thinkingContent = thinkingMatch[1];
const answerContent = answerMatch[1];
contentDiv.innerHTML = `
<div class="thinking-section">${marked.parse(thinkingContent)}</div>
<div class="answer-section">${marked.parse(answerContent)}</div>
`;
} else {
// 如果没有标签,只显示最终文本
contentDiv.innerHTML = marked.parse(fullText);
}
// 代码高亮
hljs.highlightAll();
}
function appendMsg(role, text) {
const div = document.createElement('div');
div.className = `message ${role}`;
div.innerHTML = `<div class="avatar">${role === 'user' ? config.userAvatar : config.aiAvatar}</div><div class="content markdown-body">${marked.parse(text)}</div>`;
const box = document.getElementById('chatBox');
box.appendChild(div);
box.scrollTop = box.scrollHeight;
return div;
}