-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathchat.html
More file actions
2008 lines (1889 loc) · 78.2 KB
/
Copy pathchat.html
File metadata and controls
2008 lines (1889 loc) · 78.2 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>Qwen2API Chat</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;600&family=Space+Grotesk:wght@500;600&display=swap" rel="stylesheet" />
<style>
:root {
color-scheme: light;
--page-bg: #f6f7fb;
--ink: #0f172a;
--muted: #475569;
--panel: rgba(255, 255, 255, 0.88);
--panel-2: rgba(255, 255, 255, 0.72);
--border: rgba(15, 23, 42, 0.10);
--shadow: 0 18px 42px rgba(15, 23, 42, 0.10);
--shadow-soft: 0 10px 24px rgba(15, 23, 42, 0.08);
--ring: 0 0 0 4px rgba(14, 165, 164, 0.18);
--accent: #0ea5a4;
--accent-2: #16a34a;
--danger: #ef4444;
--radius: 14px;
--radius-sm: 10px;
--empty-hint: "开始对话吧。支持粘贴文本、上传文件,Enter 发送。";
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
margin: 0;
color: var(--ink);
background:
radial-gradient(1200px 700px at 15% 10%, rgba(14, 165, 164, 0.14), transparent 55%),
radial-gradient(900px 520px at 85% 20%, rgba(34, 197, 94, 0.10), transparent 60%),
radial-gradient(1000px 600px at 50% 100%, rgba(56, 189, 248, 0.10), transparent 55%),
var(--page-bg);
font-family: "Noto Sans SC", system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
letter-spacing: 0.1px;
}
main {
max-width: 1120px;
margin: 0 auto;
padding: 18px 14px 20px;
display: flex;
flex-direction: column;
gap: 12px;
min-height: 100vh;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
border-radius: var(--radius);
box-shadow: var(--shadow-soft);
backdrop-filter: blur(10px);
}
.topbar {
padding: 12px;
display: flex;
gap: 12px;
align-items: center;
justify-content: space-between;
}
.brand {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 150px;
}
.brand b {
font-family: "Space Grotesk", "Noto Sans SC", sans-serif;
font-size: 16px;
letter-spacing: 0.3px;
}
.brand span {
color: var(--muted);
font-size: 12px;
}
.controls {
display: flex;
gap: 8px;
align-items: center;
flex-wrap: wrap;
justify-content: flex-end;
}
.controls .grow { flex: 1; min-width: 160px; }
input, select, textarea, button {
font: inherit;
border-radius: 12px;
border: 1px solid rgba(15, 23, 42, 0.14);
background: rgba(255, 255, 255, 0.75);
color: var(--ink);
outline: none;
}
input, select, button { padding: 10px 11px; }
textarea { padding: 12px 12px; width: 100%; min-height: 96px; resize: vertical; }
input:focus, select:focus, textarea:focus { box-shadow: var(--ring); border-color: rgba(14, 165, 164, 0.55); }
button { cursor: pointer; transition: transform 140ms ease, box-shadow 140ms ease, background-color 140ms ease; }
button:disabled { cursor: not-allowed; opacity: 0.6; }
button:active:not(:disabled) { transform: translateY(1px); }
.btn {
background: rgba(255, 255, 255, 0.75);
}
.primary {
background: linear-gradient(135deg, #0ea5a4, #22c55e);
color: #ffffff;
border-color: rgba(14, 165, 164, 0.55);
box-shadow: 0 10px 22px rgba(14, 165, 164, 0.18);
}
.primary:hover:not(:disabled) { box-shadow: 0 14px 28px rgba(14, 165, 164, 0.22); }
.warn {
background: rgba(239, 68, 68, 0.10);
color: #991b1b;
border-color: rgba(239, 68, 68, 0.35);
}
.warn:hover:not(:disabled) { background: rgba(239, 68, 68, 0.14); }
.log-toggle {
background: rgba(15, 23, 42, 0.06);
border-color: rgba(15, 23, 42, 0.14);
}
.log-toggle.active {
background: rgba(14, 165, 164, 0.12);
border-color: rgba(14, 165, 164, 0.35);
}
.shell {
display: grid;
grid-template-columns: 1fr;
gap: 12px;
align-items: stretch;
min-height: 58vh;
}
.shell.has-logs { grid-template-columns: 1fr; }
@media (min-width: 981px) {
.shell.has-logs { grid-template-columns: 1fr 360px; }
}
#messages {
padding: 14px;
overflow: auto;
min-height: 56vh;
max-height: 62vh;
display: flex;
flex-direction: column;
gap: 10px;
}
#messages:empty::before {
content: var(--empty-hint);
color: rgba(71, 85, 105, 0.9);
background: rgba(255, 255, 255, 0.55);
border: 1px dashed rgba(15, 23, 42, 0.18);
border-radius: 14px;
padding: 14px 14px;
line-height: 1.5;
}
.msg {
max-width: min(760px, 92%);
padding: 10px 12px;
border-radius: 16px;
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
border: 1px solid rgba(15, 23, 42, 0.10);
animation: popIn 220ms ease both;
}
.msg.u {
align-self: flex-end;
background: linear-gradient(135deg, rgba(14, 165, 164, 0.12), rgba(34, 197, 94, 0.10));
}
.msg.a {
align-self: flex-start;
background: rgba(255, 255, 255, 0.72);
}
.msg-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 6px;
font-size: 12px;
color: rgba(71, 85, 105, 0.92);
}
.msg-head span {
display: inline-flex;
align-items: center;
gap: 8px;
}
.think {
border: 1px solid rgba(15, 23, 42, 0.10);
background: rgba(15, 23, 42, 0.035);
border-radius: 12px;
padding: 8px 10px;
margin-bottom: 8px;
}
.think summary {
cursor: pointer;
font-size: 12px;
color: rgba(71, 85, 105, 0.92);
user-select: none;
list-style: none;
}
.think summary::-webkit-details-marker { display: none; }
.think summary::before {
content: "▸";
display: inline-block;
margin-right: 6px;
transform: translateY(-1px);
}
.think[open] summary::before { content: "▾"; }
.think pre {
margin: 8px 0 0;
white-space: pre-wrap;
word-break: break-word;
overflow-wrap: anywhere;
font-size: 12px;
line-height: 1.55;
color: rgba(71, 85, 105, 0.96);
font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}
.mini {
padding: 6px 10px;
border-radius: 999px;
font-size: 12px;
background: rgba(15, 23, 42, 0.06);
border-color: rgba(15, 23, 42, 0.10);
}
.mini:hover:not(:disabled) { background: rgba(15, 23, 42, 0.08); }
.composer {
padding: 12px;
display: flex;
flex-direction: column;
gap: 10px;
}
.video-panel { padding: 12px; }
.action-row {
display: flex;
gap: 8px;
flex-wrap: wrap;
align-items: center;
}
.file {
flex: 1;
min-width: 220px;
padding: 8px;
background: rgba(255, 255, 255, 0.55);
}
.file::file-selector-button {
font: inherit;
padding: 8px 10px;
border-radius: 999px;
border: 1px solid rgba(15, 23, 42, 0.14);
background: rgba(15, 23, 42, 0.05);
margin-right: 10px;
cursor: pointer;
}
.file::file-selector-button:hover { background: rgba(15, 23, 42, 0.08); }
.status {
font-size: 12px;
min-height: 18px;
color: rgba(71, 85, 105, 0.95);
}
.status.err { color: #b91c1c; }
.status.ok { color: #047857; }
#atts { display: grid; gap: 8px; }
.fi {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
padding: 10px 10px;
border: 1px solid rgba(15, 23, 42, 0.12);
border-radius: var(--radius-sm);
background: rgba(255, 255, 255, 0.62);
}
.fi button {
padding: 8px 10px;
border-radius: 999px;
background: rgba(239, 68, 68, 0.10);
color: #991b1b;
border-color: rgba(239, 68, 68, 0.28);
}
.log-panel { display: none; padding: 12px; }
.log-panel.visible {
display: flex;
flex-direction: column;
gap: 10px;
}
.log-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
}
.log-header span {
font-family: "Space Grotesk", "Noto Sans SC", sans-serif;
font-weight: 600;
letter-spacing: 0.2px;
}
.video-row {
display: flex;
gap: 8px;
align-items: center;
flex-wrap: wrap;
}
.video-tag {
font-size: 12px;
color: rgba(71, 85, 105, 0.95);
padding: 6px 10px;
border-radius: 999px;
border: 1px solid rgba(15, 23, 42, 0.12);
background: rgba(255, 255, 255, 0.55);
white-space: nowrap;
}
.video-row input {
flex: 1;
min-width: 220px;
font-size: 12px;
padding: 10px 10px;
}
.video-clear {
padding: 10px 12px;
font-size: 12px;
border-radius: 999px;
background: rgba(15, 23, 42, 0.06);
border-color: rgba(15, 23, 42, 0.12);
}
#logs {
flex: 1;
min-height: 46vh;
max-height: 56vh;
overflow: auto;
font-size: 12px;
font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
background:
radial-gradient(900px 450px at 20% 10%, rgba(14, 165, 164, 0.14), transparent 60%),
radial-gradient(800px 420px at 80% 30%, rgba(34, 197, 94, 0.10), transparent 60%),
#0b1220;
color: rgba(226, 232, 240, 0.92);
border-radius: var(--radius-sm);
border: 1px solid rgba(148, 163, 184, 0.20);
padding: 10px 10px;
}
.log-entry { padding: 3px 0; border-bottom: 1px solid rgba(148, 163, 184, 0.16); }
.log-entry:last-child { border-bottom: none; }
.log-time { color: rgba(34, 197, 94, 0.85); margin-right: 8px; }
.log-event { color: rgba(56, 189, 248, 0.95); font-weight: 600; }
.log-detail { color: rgba(251, 191, 36, 0.88); margin-left: 8px; white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; }
.modal-backdrop {
position: fixed;
inset: 0;
background: rgba(15, 23, 42, 0.45);
z-index: 999;
display: flex;
align-items: center;
justify-content: center;
padding: 16px;
animation: popIn 200ms ease;
}
.modal {
background: var(--panel);
border: 1px solid var(--border);
border-radius: var(--radius);
box-shadow: var(--shadow);
max-width: 480px;
width: 100%;
padding: 18px;
display: flex;
flex-direction: column;
gap: 12px;
backdrop-filter: blur(10px);
}
.modal h3 {
margin: 0;
font-family: "Space Grotesk", "Noto Sans SC", sans-serif;
font-size: 16px;
letter-spacing: 0.2px;
}
.modal .field-label {
font-size: 12px;
color: var(--muted);
}
.modal .hint {
font-size: 12px;
color: var(--muted);
line-height: 1.5;
}
.modal .actions {
display: flex;
gap: 8px;
justify-content: flex-end;
flex-wrap: wrap;
}
@keyframes popIn {
from { opacity: 0; transform: translateY(6px); }
to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
.msg { animation: none; }
button { transition: none; }
}
@media (max-width: 980px) {
.shell.has-logs { grid-template-columns: 1fr; }
#messages { max-height: 56vh; }
#logs { max-height: 44vh; }
.brand { display: none; }
}
</style>
</head>
<body>
<main>
<header class="panel topbar">
<div class="brand">
<b>Qwen2API Chat</b>
<span id="brandSub">轻量 · 流式 · 支持附件</span>
</div>
<div class="controls">
<button id="langToggle" class="btn" type="button" aria-label="Language">EN</button>
<input id="apiKey" class="grow" type="text" placeholder="API Key(可选)" autocomplete="off" spellcheck="false" />
<select id="model" class="grow"><option value="qwen3.5-plus">qwen3.5-plus</option></select>
<button id="refreshModels" class="btn" type="button">刷新模型</button>
<button id="logToggle" class="log-toggle" type="button">显示日志</button>
<button id="settingsBtn" class="btn" type="button">设置</button>
</div>
</header>
<section class="panel video-panel" aria-label="视频分析">
<div class="video-row">
<span class="video-tag" id="videoTag">视频分析(可选)</span>
<input id="videoUrl" type="text" placeholder="粘贴视频链接;发送时自动进入视频分析;留空则普通对话" autocomplete="off" spellcheck="false" />
<button id="clearVideo" class="video-clear" type="button">清空</button>
</div>
</section>
<section class="panel video-panel" aria-label="图片生成">
<div class="video-row">
<select id="imageGenMode" style="flex: 0 0 auto; min-width: 120px;">
<option value="chat">聊天模式</option>
<option value="image">图片生成</option>
</select>
<select id="imageSize" style="flex: 0 0 auto; min-width: 120px;">
<option value="1:1">1:1 (正方形)</option>
<option value="16:9">16:9 (宽屏)</option>
<option value="9:16">9:16 (竖屏)</option>
<option value="4:3">4:3 (传统)</option>
<option value="3:4">3:4 (传统竖)</option>
</select>
<select id="imageCount" style="flex: 0 0 auto; min-width: 80px;">
<option value="1">1张</option>
<option value="2">2张</option>
<option value="3">3张</option>
<option value="4">4张</option>
<option value="5">5张</option>
</select>
</div>
</section>
<div class="shell" id="shell">
<section id="messages" class="panel" aria-label="消息列表"></section>
<aside class="panel log-panel" id="logPanel" aria-label="运行日志">
<div class="log-header">
<span id="logsTitle">运行日志</span>
<button id="clearLogs" type="button" class="mini">清空</button>
</div>
<div id="logs"></div>
</aside>
</div>
<section class="panel composer" aria-label="输入区">
<textarea id="prompt" placeholder="输入消息;Enter 发送,Shift+Enter 换行"></textarea>
<div class="action-row">
<input id="files" class="file" type="file" multiple accept=".pdf,.doc,.docx,.dot,.csv,.xlsx,.xls,.txt,.text,.md,.js,.mjs,.ts,.jsx,.tsx,.vue,.html,.htm,.css,.svg,.svgz,.xml,.json,.jsonc,.wasm,.tex,.latex,.c,.h,.cc,.cxx,.cpp,.hpp,.hh,.hxx,.ino,.java,.kt,.kts,.scala,.groovy,.go,.rs,.swift,.php,.rb,.cs,.vb,.fs,.csproj,.sln,.sql,.lua,.r,.pl,.tcl,.awk,.fish,.yaml,.yml,.toml,.ini,.sh,.bat,.cmd,.dockerfile,.containerfile,.proto,.thrift,.graphql,.gql,.qmd,.smali,.gif,.webp,.jpg,.jpeg,.png,.bmp,.icns,.jp2,.sgi,.tif,.tiff,.mkv,.mov,.wav,.mp3,.m4a,.amr,.aac,image/*,audio/*,video/*" />
<button id="send" class="primary" type="button">发送</button>
<button id="stop" class="btn" type="button" disabled>中断</button>
<button id="clear" class="warn" type="button">清空会话</button>
</div>
<div id="atts"></div>
<div id="status" class="status"></div>
</section>
<footer style="text-align: center; padding: 16px; margin-top: 8px;">
<a href="https://github.com/smanx/qwen2api" target="_blank" rel="noopener noreferrer" style="display: inline-flex; align-items: center; gap: 8px; text-decoration: none; color: var(--muted); font-size: 14px; padding: 10px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--panel); transition: all 140ms ease;">
<svg viewBox="0 0 24 24" style="width: 20px; height: 20px; fill: currentColor;">
<path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/>
</svg>
<span>GitHub</span>
</a>
</footer>
</main>
<script>
(function () {
var MAX = 5;
var MAX_SIZE = 100 * 1024 * 1024;
var MAX_HISTORY = 80;
var MAX_HISTORY_TEXT_CHARS = 12000;
var MAX_LOG_ENTRIES = 300;
var MAX_LOG_RAW_PREVIEW = 300;
var KEY = 'qwen2api.chat.history.v1';
var VIDEO_URL_KEY = 'qwen2api.video.url.v1';
var SHOW_LOGS_KEY = 'qwen2api.chat.showLogs.v1';
var MODEL_KEY = 'qwen2api.chat.model.v1';
var LANG_KEY = 'qwen2api.chat.lang.v1';
var IMAGE_PROXY_KEY = 'qwen2api.chat.imageProxy.v1';
var state = { hist: [], files: [], ac: null, sending: false, showLogs: false, videoUrl: '', modelLoadId: 0, modelLoading: false, preferredModel: '', lastModelAuthKey: null, imageGenMode: 'chat', imageProxy: '' };
var API_BASE = (function () {
var p = (location && location.pathname) ? location.pathname : '';
if (p === '/.netlify/functions/api' || p.indexOf('/.netlify/functions/api/') === 0) return '/.netlify/functions/api';
if (p === '/api' || p.indexOf('/api/') === 0) return '/api';
return '';
})();
function apiUrl(path) {
return API_BASE ? (API_BASE + path) : path;
}
var e = {
shell: document.getElementById('shell'),
langToggle: document.getElementById('langToggle'),
apiKey: document.getElementById('apiKey'),
model: document.getElementById('model'),
refreshModels: document.getElementById('refreshModels'),
clear: document.getElementById('clear'),
logToggle: document.getElementById('logToggle'),
settingsBtn: document.getElementById('settingsBtn'),
logPanel: document.getElementById('logPanel'),
logs: document.getElementById('logs'),
clearLogs: document.getElementById('clearLogs'),
videoUrl: document.getElementById('videoUrl'),
clearVideo: document.getElementById('clearVideo'),
messages: document.getElementById('messages'),
prompt: document.getElementById('prompt'),
files: document.getElementById('files'),
atts: document.getElementById('atts'),
send: document.getElementById('send'),
stop: document.getElementById('stop'),
status: document.getElementById('status'),
imageGenMode: document.getElementById('imageGenMode'),
imageSize: document.getElementById('imageSize'),
imageCount: document.getElementById('imageCount')
};
function getPreferredLang() {
var v = '';
try { v = localStorage.getItem(LANG_KEY) || ''; } catch (_) {}
v = String(v || '').toLowerCase();
if (v === 'en' || v === 'zh') return v;
try {
var nav = (navigator && (navigator.language || (navigator.languages && navigator.languages[0]))) || '';
nav = String(nav || '').toLowerCase();
if (nav.indexOf('zh') === 0) return 'zh';
} catch (_) {}
return 'en';
}
function t(key) {
var L = state.lang || 'zh';
var dict = {
zh: {
brandSub: '轻量 · 流式 · 支持附件',
apiKeyPh: 'API Key(可选)',
refreshModels: '刷新模型',
refreshModelsLoading: '加载中...',
showLogs: '显示日志',
hideLogs: '隐藏日志',
clearChat: '清空会话',
logsTitle: '运行日志',
clear: '清空',
videoTag: '视频分析(可选)',
videoPh: '粘贴视频链接;发送时自动进入视频分析;留空则普通对话',
inputPh: '输入消息;Enter 发送,Shift+Enter 换行',
send: '发送',
sending: '发送中...',
stop: '中断',
stopping: '中断中...',
retry: '重发',
assistant: '助手',
user: '用户',
think: '思考过程',
emptyAssistant: '(空响应)',
attachMsg: '[附件消息]',
attachPrefix: '[附件] ',
emptyHint: '开始对话吧。支持粘贴文本、上传文件,Enter 发送。'
,ready: '就绪。'
,loadingModels: '正在加载模型列表...'
,modelsUpdated: '模型列表已更新({n})'
,modelsLoadingWait: '模型列表加载中,请稍候。'
,modelsLoadingWaitSend: '模型列表加载中,请稍候发送。'
,resendWait: '模型列表加载中,请稍候重发。'
,onlyResendUser: '仅支持重发用户消息。'
,historySimplified: '历史附件已简化,已回退为文本重发。'
,inputOrFileRequired: '请输入内容或选择附件。'
,processingAttachments: '正在处理附件...'
,attachmentsFailed: '附件处理失败'
,requestingChat: '请求中(流式)...'
,requestingLogs: '请求中(日志)...'
,requestingVideo: '请求中(视频分析)...'
,requestingVideoLogs: '请求中(视频分析 + 日志)...'
,done: '完成。'
,aborted: '已中断请求。'
,abortInProgress: '正在中断请求...'
,noActiveRequest: '当前没有进行中的请求。'
,busyNoDoubleSend: '请求进行中,请勿重复发送。'
,maxAttachments: '最多允许 {n} 个附件。'
,fileTooLarge: '文件过大:{name}(上限 {max})'
,addedAttachments: '已选择 {n} 个附件。'
,addedAndSkipped: '已添加 {added} 个附件,跳过 {skipped} 个重复/无效附件。'
,addedNoneSkipped: '未新增附件(均为重复或无效文件)。'
,videoUrlInvalid: '视频链接需以 http:// 或 https:// 开头。'
,videoSet: '已设置视频链接:发送时将自动进入视频分析。'
,videoCleared: '已清空视频链接:将恢复普通对话。'
,videoRestored: '已恢复上次视频链接。'
,logsClearBusy: '请求进行中,暂不能清空日志。'
,logsClearBusyModels: '模型列表加载中,请稍候清空日志。'
,logsCleared: '日志已清空。'
,logsAlreadyEmpty: '日志已为空。'
,clearChatBusy: '请求进行中,暂不能清空会话。'
,clearChatBusyModels: '模型列表加载中,请稍候清空会话。'
,chatCleared: '会话已清空。'
,chatAlreadyEmpty: '会话已为空。'
,refreshModelsBusy: '请求进行中,暂不能刷新模型。'
,loadingModelsStatus: '正在加载模型列表...'
,requestFailed: '请求失败'
,abortedTag: '[已中断]'
,errorTag: '[错误]'
,fileReadFailed: '读取文件失败: {name}'
,attachmentRemoveBusy: '请求进行中,暂不能修改附件。'
,attachmentRemoveBusyModels: '模型列表加载中,请稍候再修改附件。'
,remove: '移除'
,modelsLoadFailedFallback: '加载模型失败,已回退默认模型。'
,authFailed: '鉴权失败:Incorrect API key provided.'
,payloadTooLarge: '请求体过大,请减小附件。'
,serverError: '服务端异常,请稍后重试。'
,settings: '设置'
,imageProxy: '图片代理前缀'
,imageProxyPh: '例如:https://proxy.example.com/?url='
,imageProxyHint: '设置后,图片链接会直接拼接到该代理地址之后。留空则不使用代理。'
,save: '保存'
,cancel: '取消'
,clearProxy: '清除代理'
,imageProxySet: '图片代理已设置。'
,imageProxyCleared: '图片代理已清除。'
},
en: {
brandSub: 'Light · Streaming · Attachments',
apiKeyPh: 'API key (optional)',
refreshModels: 'Refresh models',
refreshModelsLoading: 'Loading...',
showLogs: 'Show logs',
hideLogs: 'Hide logs',
clearChat: 'Clear chat',
logsTitle: 'Runtime logs',
clear: 'Clear',
videoTag: 'Video analysis (optional)',
videoPh: 'Paste a video URL; sending will trigger video analysis; empty = normal chat',
inputPh: 'Type a message; Enter to send, Shift+Enter for newline',
send: 'Send',
sending: 'Sending...',
stop: 'Stop',
stopping: 'Stopping...',
retry: 'Resend',
assistant: 'Assistant',
user: 'User',
think: 'Thinking',
emptyAssistant: '(empty response)',
attachMsg: '[Attachment message]',
attachPrefix: '[Attachments] ',
emptyHint: 'Start chatting. Paste text, upload files, press Enter to send.'
,ready: 'Ready.'
,loadingModels: 'Loading model list...'
,modelsUpdated: 'Model list updated ({n})'
,modelsLoadingWait: 'Model list is loading, please wait.'
,modelsLoadingWaitSend: 'Model list is loading, please wait before sending.'
,resendWait: 'Model list is loading, please wait before resending.'
,onlyResendUser: 'Only user messages can be resent.'
,historySimplified: 'Attachments in history were simplified; falling back to text resend.'
,inputOrFileRequired: 'Type a message or choose attachments.'
,processingAttachments: 'Processing attachments...'
,attachmentsFailed: 'Failed to process attachments'
,requestingChat: 'Requesting (streaming)...'
,requestingLogs: 'Requesting (logs)...'
,requestingVideo: 'Requesting (video analysis)...'
,requestingVideoLogs: 'Requesting (video + logs)...'
,done: 'Done.'
,aborted: 'Request aborted.'
,abortInProgress: 'Aborting request...'
,noActiveRequest: 'No active request.'
,busyNoDoubleSend: 'Request in progress. Please do not send again.'
,maxAttachments: 'Up to {n} attachments.'
,fileTooLarge: 'File too large: {name} (limit {max})'
,addedAttachments: '{n} attachments selected.'
,addedAndSkipped: 'Added {added} attachments, skipped {skipped} duplicates/invalid.'
,addedNoneSkipped: 'No new attachments (all duplicates/invalid).'
,videoUrlInvalid: 'Video URL must start with http:// or https://'
,videoSet: 'Video URL set: sending will trigger video analysis.'
,videoCleared: 'Video URL cleared: normal chat mode.'
,videoRestored: 'Restored previous video URL.'
,logsClearBusy: 'Request in progress; cannot clear logs.'
,logsClearBusyModels: 'Model list is loading; cannot clear logs yet.'
,logsCleared: 'Logs cleared.'
,logsAlreadyEmpty: 'Logs are already empty.'
,clearChatBusy: 'Request in progress; cannot clear chat.'
,clearChatBusyModels: 'Model list is loading; cannot clear chat yet.'
,chatCleared: 'Chat cleared.'
,chatAlreadyEmpty: 'Chat is already empty.'
,refreshModelsBusy: 'Request in progress; cannot refresh models.'
,loadingModelsStatus: 'Loading model list...'
,requestFailed: 'Request failed'
,abortedTag: '[ABORTED]'
,errorTag: '[ERROR]'
,fileReadFailed: 'Failed to read file: {name}'
,attachmentRemoveBusy: 'Request in progress; attachments cannot be modified.'
,attachmentRemoveBusyModels: 'Model list is loading; attachments cannot be modified yet.'
,remove: 'Remove'
,modelsLoadFailedFallback: 'Failed to load models; fell back to default model.'
,authFailed: 'Auth failed: Incorrect API key provided.'
,payloadTooLarge: 'Request body too large; please reduce attachments.'
,serverError: 'Server error; please try again later.'
,settings: 'Settings'
,imageProxy: 'Image proxy prefix'
,imageProxyPh: 'e.g. https://proxy.example.com/?url='
,imageProxyHint: 'When set, image URLs are prepended with this proxy. Leave empty to disable.'
,save: 'Save'
,cancel: 'Cancel'
,clearProxy: 'Clear proxy'
,imageProxySet: 'Image proxy set.'
,imageProxyCleared: 'Image proxy cleared.'
}
};
return (dict[L] && dict[L][key]) || (dict.zh && dict.zh[key]) || key;
}
function tf(key, params) {
var s = String(t(key) || '');
var p = params && typeof params === 'object' ? params : {};
return s.replace(/\{(\w+)\}/g, function (_, k) { return (p[k] !== undefined && p[k] !== null) ? String(p[k]) : ''; });
}
function applyLangToUI() {
document.documentElement.setAttribute('lang', state.lang === 'en' ? 'en' : 'zh-CN');
try { document.documentElement.style.setProperty('--empty-hint', '"' + String(t('emptyHint')).replace(/"/g, '\\"') + '"'); } catch (_) {}
if (e.langToggle) e.langToggle.textContent = state.lang === 'en' ? '中文' : 'EN';
var brandSub = document.getElementById('brandSub');
if (brandSub) brandSub.textContent = t('brandSub');
if (e.apiKey) e.apiKey.placeholder = t('apiKeyPh');
if (e.refreshModels) e.refreshModels.textContent = state.modelLoading ? t('refreshModelsLoading') : t('refreshModels');
if (e.clear) e.clear.textContent = t('clearChat');
if (e.clearLogs) e.clearLogs.textContent = t('clear');
if (e.videoUrl) e.videoUrl.placeholder = t('videoPh');
var videoTagEl = document.getElementById('videoTag');
if (videoTagEl) videoTagEl.textContent = t('videoTag');
if (e.clearVideo) e.clearVideo.textContent = t('clear');
var logsTitle = document.getElementById('logsTitle');
if (logsTitle) logsTitle.textContent = t('logsTitle');
if (e.prompt) e.prompt.placeholder = t('inputPh');
if (e.send) e.send.textContent = state.sending ? t('sending') : t('send');
if (e.stop) e.stop.textContent = (state.ac && state.stop.disabled) ? t('stopping') : t('stop');
if (e.logToggle) {
e.logToggle.textContent = state.showLogs ? t('hideLogs') : t('showLogs');
}
if (e.settingsBtn) e.settingsBtn.textContent = t('settings');
rHist();
}
function setLang(next) {
var v = String(next || '').toLowerCase();
if (v !== 'en' && v !== 'zh') v = 'zh';
state.lang = v;
try { localStorage.setItem(LANG_KEY, v); } catch (_) {}
applyLangToUI();
}
// state/history
function st(t, k) { e.status.textContent = t || ''; e.status.className = 'status ' + (k || ''); }
function compactMessageContentForStorage(content) {
if (typeof content === 'string') {
if (content.length > MAX_HISTORY_TEXT_CHARS) return content.slice(0, MAX_HISTORY_TEXT_CHARS) + '\n\n[TRUNCATED]';
return content;
}
return summarizeMessageContent(content);
}
function compactHistoryForStorage(list) {
var source = Array.isArray(list) ? list : [];
var out = [];
for (var i = 0; i < source.length; i++) {
var item = source[i] || {};
out.push({
role: item.role === 'assistant' ? 'assistant' : 'user',
content: compactMessageContentForStorage(item.content)
});
}
return out;
}
function persistHistoryWithFallback(kept) {
var list = compactHistoryForStorage(kept);
for (var start = 0; start <= list.length; start++) {
try {
localStorage.setItem(KEY, JSON.stringify(list.slice(start)));
return;
} catch (_) {}
}
}
function save() {
var kept = [];
try {
kept = state.hist.filter(function (x) {
if (!x) return false;
if (typeof x.content === 'string') return !!x.content.trim();
return Array.isArray(x.content) && x.content.length > 0;
}).slice(-MAX_HISTORY);
} catch (_) {}
try { persistHistoryWithFallback(kept); } catch (_) {}
try {
if (state.videoUrl) localStorage.setItem(VIDEO_URL_KEY, state.videoUrl);
else localStorage.removeItem(VIDEO_URL_KEY);
localStorage.setItem(SHOW_LOGS_KEY, state.showLogs ? '1' : '0');
if (state.preferredModel) localStorage.setItem(MODEL_KEY, state.preferredModel);
else localStorage.removeItem(MODEL_KEY);
if (state.imageProxy) localStorage.setItem(IMAGE_PROXY_KEY, state.imageProxy);
else localStorage.removeItem(IMAGE_PROXY_KEY);
} catch (_) {}
}
function load() {
try {
var v = localStorage.getItem(KEY);
if (v) {
var a = JSON.parse(v);
if (Array.isArray(a)) {
var normalized = [];
for (var i = 0; i < a.length; i++) {
var item = a[i] || {};
var content = item.content;
if (typeof content !== 'string' && !Array.isArray(content)) continue;
normalized.push({
role: item.role === 'assistant' ? 'assistant' : 'user',
content: content
});
}
state.hist = normalized.slice(-MAX_HISTORY);
}
}
} catch (_) {}
try {
var savedVideoUrl = localStorage.getItem(VIDEO_URL_KEY);
if (savedVideoUrl) {
state.videoUrl = savedVideoUrl;
e.videoUrl.value = savedVideoUrl;
}
} catch (_) {}
try {
state.showLogs = localStorage.getItem(SHOW_LOGS_KEY) === '1';
} catch (_) {}
try {
var savedModel = localStorage.getItem(MODEL_KEY);
if (savedModel) state.preferredModel = savedModel;
} catch (_) {}
try {
var savedLang = localStorage.getItem(LANG_KEY);
if (savedLang) state.lang = String(savedLang || '').toLowerCase();
} catch (_) {}
try {
var savedImageProxy = localStorage.getItem(IMAGE_PROXY_KEY);
if (savedImageProxy) state.imageProxy = String(savedImageProxy);
} catch (_) {}
}
function summarizeMessageContent(content) {
if (typeof content === 'string') {
if (content.length > MAX_HISTORY_TEXT_CHARS) return content.slice(0, MAX_HISTORY_TEXT_CHARS) + '\n\n[TRUNCATED]';
return content;
}
if (!Array.isArray(content)) return '';
var texts = [];
var names = [];
for (var i = 0; i < content.length; i++) {
var part = content[i] || {};
var type = part.type || '';
if (type === 'input_text' && typeof part.input_text === 'string' && part.input_text.trim()) texts.push(part.input_text.trim());
if ((type === 'input_file' || type === 'input_image' || type === 'input_video' || type === 'input_audio') && typeof part.filename === 'string' && part.filename.trim()) names.push(part.filename.trim());
}
var base = texts.join('\n');
if (base.length > MAX_HISTORY_TEXT_CHARS) base = base.slice(0, MAX_HISTORY_TEXT_CHARS) + '\n\n[TRUNCATED]';
if (names.length > 0) return (base ? base + '\n' : '') + t('attachPrefix') + names.join(', ');
return base;
}
function splitThinking(rawText) {
var raw = String(rawText || '');
if (!raw) return { think: '', final: '' };
var thinkParts = [];
var final = raw.replace(/<think>([\s\S]*?)<\/think>/gi, function (_, inner) {
if (inner && String(inner).trim()) thinkParts.push(String(inner).trim());
return '';
});
final = final.replace(/\n{3,}/g, '\n\n').trim();
return { think: thinkParts.join('\n\n').trim(), final: final };
}
function msg(role, text, idx) {
var d = document.createElement('div');
d.className = 'msg ' + (role === 'assistant' ? 'a' : 'u');
var head = document.createElement('div');
head.className = 'msg-head';
var roleLabel = document.createElement('span');
roleLabel.textContent = role === 'assistant' ? t('assistant') : t('user');
head.appendChild(roleLabel);
if (role === 'user' && typeof idx === 'number') {
var retry = document.createElement('button');
retry.type = 'button';
retry.className = 'mini';
retry.textContent = t('retry');
retry.disabled = state.sending || state.modelLoading;
retry.onclick = function () { resendFromIndex(idx); };
head.appendChild(retry);
}
var body = document.createElement('div');
d.appendChild(head);
d.appendChild(body);
if (role === 'assistant') {
var thinkWrap = document.createElement('details');
thinkWrap.className = 'think';
thinkWrap.open = false;
var thinkSummary = document.createElement('summary');
thinkSummary.textContent = t('think');
var thinkPre = document.createElement('pre');
thinkWrap.appendChild(thinkSummary);
thinkWrap.appendChild(thinkPre);
var finalDiv = document.createElement('div');
var imageContainer = document.createElement('div');
imageContainer.style.display = 'flex';
imageContainer.style.flexWrap = 'wrap';
imageContainer.style.gap = '10px';
imageContainer.style.marginTop = '10px';
body.appendChild(thinkWrap);
body.appendChild(finalDiv);
body.appendChild(imageContainer);
function setAssistantRaw(rawText, imageUrls) {
var sp = splitThinking(rawText);
if (sp.think) {
thinkWrap.style.display = '';
thinkPre.textContent = sp.think;
} else {
thinkWrap.style.display = 'none';
thinkPre.textContent = '';
}
finalDiv.textContent = sp.final || '';
if (imageUrls && imageUrls.length > 0) {
imageContainer.innerHTML = '';
for (var i = 0; i < imageUrls.length; i++) {
var img = document.createElement('img');
var rawUrl = imageUrls[i];
img.src = state.imageProxy ? (state.imageProxy + rawUrl) : rawUrl;
img.style.maxWidth = '100%';
img.style.maxHeight = '400px';
img.style.borderRadius = '8px';
img.style.cursor = 'pointer';
img.onclick = function() {
window.open(this.src, '_blank');
};
imageContainer.appendChild(img);
}
}
}
setAssistantRaw(text || '');
e.messages.appendChild(d);
e.messages.scrollTop = e.messages.scrollHeight;
return { setRaw: setAssistantRaw };
}