-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
2031 lines (1812 loc) · 65.6 KB
/
Copy pathindex.html
File metadata and controls
2031 lines (1812 loc) · 65.6 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="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>llama.cpp simple WebUI</title>
<style>
/* -------------------------------------------------------------
0 ルート設定
------------------------------------------------------------- */
:root{
/* ---- バブル全体 ---- */
--bubble-bg-user: #e0f7fa;
--bubble-bg-bot : #fff;
--bubble-fg : #212121;
--bubble-radius : 16px;
--bubble-padding: 12px 16px;
--bubble-font : "Helvetica Neue",Helvetica,Arial,sans-serif;
--bubble-fsize : 0.95rem;
--bubble-lineh : 1.6;
/* ---- コードブロック ---- */
--code-bg : #1F1F1F;
--code-fg : #D7D7D7;
--code-font : "Source Code Pro", Menlo, Consolas, monospace;
--code-fsize : 0.88rem;
--code-padding : 0.6rem 0.8rem;
--code-radius : 6px;
/* ---- サイドバーの標準幅(デスクトップ) ---- */
--sidebar-width: 260px;
/* ---- サイドバーの折りたたみ幅(モバイル/ミニ) ---- */
--sidebar-collapsed-width: 60px;
}
/* -------------------------------------------------------------
1. 基本レイアウト(レスポンシブ対応)
------------------------------------------------------------- */
body{
margin:0;
font-family: Helvetica,Arial,sans-serif;
background:#f5f5f5;
display:flex;
height: 100svh ;
overflow: auto;
}
/* ---------- サイドバー ---------- */
#sidebar{
width: var(--sidebar-width);
background:#fff;
border-right:1px solid #ddd;
display:flex;
flex-direction:column;
font-size: 0.9rem;
flex-shrink: 0; /* サイズが縮まないようにする */
position: relative;
}
#sidebar.collapsed {
width: var(--sidebar-collapsed-width);
}
/* サイドバーのトグルボタン */
#sidebar-toggle {
display: flex; /* text-alignの代わりにflexで中央寄せにする */
align-items: center;
justify-content: center;
background: none;
border: none;
font-size: 1.3rem;
cursor: pointer;
padding: 12px; /* paddingを統一 */
color: #333;
flex-shrink: 0;
width: 100%; /* 幅をいっぱいにする */
box-sizing: border-box; /* paddingを含めた幅計算にする */
}
#sidebar-toggle:hover {
color: #1976d2;
}
#sidebar h3 {
margin: 0;
padding: 12px; /* paddingを統一 */
background: #f0f0f0;
font-size: 1.0rem;
border-bottom: 1px solid #ddd;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
box-sizing: border-box;
}
/* サイドバーが畳まれたとき、h3のテキストを隠し、アイコンのみ表示させる */
#sidebar.collapsed h3 {
text-align: center;
}
/* ボタンを横並びにするラッパー */
#export-import-wrapper{
display:flex;
gap:8px;
padding:0 4px;
margin-top:4px;
margin-bottom:4px;
}
/* ---------- 会話リスト ---------- */
#conv-list{
flex:1;
margin:0;
padding:0;
list-style:none;
overflow-y:auto;
}
#conv-list li{
display:flex;
align-items:center;
justify-content:space-between;
padding:8px 12px;
cursor:pointer;
border-bottom:1px solid #eee;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
#conv-list li:hover{
background:#f9f9f9;
}
#conv-list li.active{
background:#e0f7fa;
}
.conv-name{
flex:1;
white-space:nowrap;
overflow:hidden;
text-overflow:ellipsis;
margin-right: 10px; /* アイコンと名の間に少しスペースを確保 */
}
.del-btn{
background:none;
border:none;
color:#c00;
font-weight:bold;
cursor:pointer;
padding:0 4px;
flex-shrink: 0; /* 縮まないように */
}
.del-btn:hover{
color:#900;
}
/* 名前編集ボタン */
.edit-btn{
background:none;
border:none;
color:#0066cc;
font-size:1.1rem;
cursor:pointer;
padding:0 4px;
font-family: var(--emoji-font), sans-serif;
flex-shrink: 0;
}
.edit-btn:hover{
color:#003399;
}
/* ---------- ボタン ---------- */
#new-conv{
margin:4px;
padding:8px 12px;
background:#1976d2;
color:#fff;
border:none;
border-radius:4px;
cursor:pointer;
flex-shrink: 0; /* 縮まないように */
}
#new-conv:hover{ background:#1871ca; }
/* エクスポート/インポートは色を変えて目立たせる */
#export-conv{
margin:0;
padding:8px 12px;
background:#4caf50;
color:#fff;
border:none;
border-radius:4px;
flex:1;
}
#export-conv:hover{ background:#43a047; }
#import-conv{
margin:0;
padding:8px 12px;
background:#ff9800;
color:#fff;
border:none;
border-radius:4px;
flex:1;
}
#import-conv:hover{ background:#fb8c00; }
/* ---------- メインエリア ---------- */
#main{
flex:1;
display:flex;
flex-direction:column;
overflow-y:auto; /* メインエリアでスクロール */
padding:20px;
}
#title{
margin:0 0 20px;
color:#333;
}
/* タイトル横の会話名 */
#conv-title{
font-weight:normal;
font-size:0.85em;
color:#555;
}
/* サイドバーが閉じている時、ハンバーガーボタンとタイトル横の会話名が重ならないように余白を確保 */
body.sidebar-closed #conv-title {
padding-left: 30px;
}
/* -------------------------------------------------------------
2. チャット領域
------------------------------------------------------------- */
#chat{
flex:1;
overflow-y:auto;
display:flex;
flex-direction:column;
gap:10px;
padding-bottom: 20px;
}
/* -------------------------------------------------------------
3. メッセージ行と吹き出し(バブル)
------------------------------------------------------------- */
.msg-row {
display: flex;
align-items: flex-end;
width: 100%;
}
.msg-row.bot {
justify-content: flex-start;
}
.msg-row.user {
justify-content: flex-end;
}
/* アクションボタン(再実行・編集)のコンテナ */
.msg-actions {
display: flex;
gap: 4px;
margin: 0 8px;
opacity: 0; /* デフォルトは非表示 */
}
.msg-row:hover .msg-actions {
opacity: 1;
}
.action-btn {
background: none;
border: none;
cursor: pointer;
font-size: 1.1rem;
padding: 2px;
color: #666;
font-family: var(--emoji-font), sans-serif;
transition: transform 0.1s ease, color 0.1s ease; /* スムーズな変化を追加 */
}
.action-btn:hover {
color: #000;
transform: scale(1.1);
}
.action-btn:active {
transform: scale(0.7); /* クリックした瞬間に少し小さくして「押し込まれた」演出にする */
color: #333;
opacity: 0.7;
}
.bubble{
box-sizing: border-box;
max-width:80%;
padding: var(--bubble-padding);
border-radius: var(--bubble-radius);
line-height: var(--bubble-lineh);
font-family: var(--bubble-font);
font-size: var(--bubble-fsize);
color: var(--bubble-fg);
background: var(--bubble-bg-bot);
word-wrap: break-word;
position: relative; /* 追加 */
}
.bot .bubble{
border-bottom-left-radius: 2px;
}
.user .bubble{
background: var(--bubble-bg-user);
border-bottom-right-radius: 2px;
}
/* チャット画像用のスタイルを新規追加 */
.chat-images {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 8px;
}
.chat-images img {
max-width: 100%;
height: auto;
display: block;
border-radius: 8px;
}
/* コードブロック(<pre><code>)の見た目 */
.bubble pre{
background: var(--code-bg);
padding: var(--code-padding);
border-radius: var(--code-radius);
overflow-x:auto; /* 横スクロール */
margin: 0.8rem 0; /* 上下の余白 */
position:relative; /* コピー・展開ボタンの基点 */
}
.bubble pre code{
background: var(--code-bg);
color: var(--code-fg);
font-family: var(--code-font);
font-size: var(--code-fsize);
padding: 0; /* 余計な padding を除去 */
border-radius: var(--code-radius);
}
/* コピー/展開ボタンの共通デザイン */
.copy-btn,
.toggle-btn{
position:absolute;
top:4px;
right:4px;
background:#eee;
border:none;
padding:2px 6px;
font-size:0.75rem;
cursor:pointer;
border-radius:3px;
opacity:0.8;
z-index: 10; /* 他の要素の上に表示 */
}
.copy-btn:hover,
.toggle-btn:hover{
background:#ddd;
}
/* インラインコードは少しだけ装飾 */
.bubble code{
background:#f0f0f0;
color:#c7254e;
padding:0.1rem 0.3rem;
border-radius:3px;
font-family:var(--code-font);
font-size:0.94em;
}
/* 行間・段落マージンの微調整(必要なら)*/
.bubble p{
margin:0 0 0rem 0; /* 段落間の余白 */
}
/* テーブルの見た目を整える */
.bubble table {
width: 100%;
border-collapse: collapse;
margin: 0.8rem 0;
}
.bubble th,
.bubble td {
border: 1px solid #ddd;
padding: 0.4rem 0.6rem;
text-align: left;
}
.bubble th {
background: #f0f0f0;
}
/* 折りたたみ時のスタイル */
.collapsed{
max-height:200px; /* 目安の高さ */
overflow:hidden;
position:relative;
}
.collapsed::after{
content:"";
position:absolute;
bottom:0; left:0; right:0;
height:40px;
background:linear-gradient(to bottom,transparent,var(--bubble-bg-bot));
}
/* -------------------------------------------------------------
4. 入力エリア
------------------------------------------------------------- */
#input-container {
display: flex;
flex-direction: column;
position: relative; /* z-indexを有効にするために必要 */
z-index: 2000; /* ハンバーガー(1001)やブロッカー(500)よりも高い値に設定 */
}
/* ★ 画像プレビューエリア ★ */
#image-preview-area {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 8px;
}
.preview-item {
position: relative;
width: 60px;
height: 60px;
border: 1px solid #ccc;
border-radius: 4px;
background-size: cover;
background-position: center;
}
.remove-img-btn {
position: absolute;
top: -6px;
right: -6px;
background: #f44334;
color: white;
border: none;
border-radius: 50%;
width: 20px;
height: 20px;
font-size: 12px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
padding: 0;
line-height: 1;
}
#input-area{
display:flex;
gap:8px;
margin-top:12px;
}
#attach-img-btn {
padding: 10px;
font-size: 1.2rem;
cursor: pointer;
background: #fff;
border: 1px solid #bbb;
border-radius: 4px;
}
#attach-img-btn:hover { background: #eee; }
#user-input {
flex: 1;
min-height: 120px;
height: 120px; /* 初期値をmin-heightに合わせる */
max-height: 500px;
padding: 10px;
font-size: 1rem;
border: 1px solid #bbb;
border-radius: 4px;
resize: none; /* 自動調整させるため、ユーザーによる手動リサイズは無効にするのが一般的です */
box-sizing: border-box;
line-height: 1.5; /* 行間を一定にする(計算を安定させるため) */
}
#send-btn{
padding:10px 7px;
font-size:1rem;
cursor:pointer;
}
#stop-btn {
padding: 10px 7px;
font-size: 1rem;
cursor: pointer;
background: #f44336;
color: #fff;
border: none;
border-radius: 4px;
display: none; /* 通常時は非表示 */
}
/* キャンセルボタン(編集中だけ表示) */
#cancel-edit-btn{
padding:10px 7px;
font-size:1rem;
cursor:pointer;
background:#f44334;
color:#fff;
border:none;
border-radius:4px;
display:none; /* デフォルト非表示 */
}
/* -------------------------------------------------------------
5. ローディングインジケータ
------------------------------------------------------------- */
.loading{
display:inline-block;
width:6px;
height:6px;
margin-left:4px;
background:#777;
border-radius:50%;
animation:blink 1s infinite;
}
@keyframes blink{
0% {opacity:0.2;}
50% {opacity:1;}
100% {opacity:0.2;}
}
/* -------------------------------------------------------------
6. UIブロッカー(ストリーミング中に全操作を無効化)
------------------------------------------------------------- */
#ui-blocker{
position:fixed;
top:0; left:0;
width:100%; height:100%;
background:transparent;
z-index: 500; /* 100以下の値に変更し、入力エリア(z-index:100)より下に配置 */
display:none;
}
/* -------------------------------------------------------------
6‑1. サイドバーがロック中の見た目
------------------------------------------------------------- */
#sidebar.locked{
background-color: #e0e0e0 !important; /* 統一したグレーの背景色 */
color: #888 !important; /* 統一した薄い文字色 */
border-color: #ccc !important; /* 統一した薄い枠線 */
pointer-events: none;
cursor: not-allowed; /* 「禁止マーク」のカーソルにする */
opacity: 1; /* ボタンの不透明度変化をリセット(任意) */
}
#sidebar-toggle {
display: block;
}
/* サイドバー本体を完全に隠す(幅0) */
body.sidebar-closed #sidebar {
width: 0 !important;
min-width: 0;
overflow: hidden;
border-right: none;
}
/* ハンバーガーボタンだけは画面左上に固定表示(開閉切り替え用) */
body.sidebar-closed #sidebar-toggle {
position: fixed;
left: 0;
top: 0;
width: auto;
padding: 12px 12px;
background: #fff;
border-right: 1px solid #ddd;
z-index: 1002;
box-sizing: border-box;
}
/* モバイル用ハンバーガーボタン(サイドバー外) */
#sidebar-toggle-mobile {
display: none;
position: fixed;
top: 10px;
left: 10px;
z-index: 999;
background: #fff;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1.3rem;
padding: 6px 10px;
cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
#sidebar-toggle-mobile:hover {
background: #f0f0f0;
}
/* ドラッグ中のスタイル */
#conv-list li.dragging {
opacity: 0.5;
background: #eee;
}
#conv-list li {
user-select: none; /* ドラッグ操作をスムーズにする */
}
/* 履歴クリアボタンのコンテナ(サイドバーの最下部に配置) */
#clear-history-wrapper {
padding: 8px;
border-top: 1px solid #ddd;
}
#clear-history-btn {
width: 100%;
padding: 10px;
background: #f44336; /* 赤色 */
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
}
#clear-history-btn:hover {
background: #d32f2f;
}
/* サイドバーが畳まれた時のボタン表示制御 */
#sidebar.collapsed #clear-history-wrapper {
display: none; /* 畳まれている時は隠す(またはアイコンのみにする) */
}
/* --- 768px未満でのオーバーレイ設定 --- */
@media (max-width: 767px) {
#sidebar:not(.collapsed) {
position: fixed;
left: 0;
top: 0;
height: 100svh;
z-index: 2001; /* チャット入力エリア(2000)より上に表示 */
width: var(--sidebar-width) !important;
box-shadow: 2px 0 10px rgba(0,0,0,0.2);
}
}
/* 無効化(disabled)時のスタイルを統一する */
#attach-img-btn:disabled,
#user-input:disabled {
background-color: #e0e0e0 !important; /* 統一したグレーの背景色 */
color: #888 !important; /* 統一した薄い文字色 */
border-color: #ccc !important; /* 統一した薄い枠線 */
cursor: not-allowed; /* 「禁止マーク」のカーソルにする */
opacity: 1; /* ボタンの不透明度変化をリセット(任意) */
}
</style>
<!-- ★ Markdown パーサ (marked.js) を CDN から読み込み -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
/* ここで marked の設定を行う */
marked.setOptions({
gfm: true, // GitHub Flavored Markdown (テーブル等)
tables: true, // テーブルを有効化(念のため)
breaks: false, // <br> は自動で入れない
smartLists: true
});
</script>
<!-- ★ highlight.js 本体とテーマ -->
<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/vs2015.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/highlight.min.js"></script>
<!-- ★ MathJax を CDN から読み込む -->
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>
// MathJaxの設定: $...$ (インライン) と $$...$$ (ディスプレイ) を有効にする
window.MathJax = {
tex: {
inlineMath: [['$', '$'], ['\\(', '\\)']],
displayMath: [['$$', '$$'], ['\\[', '\\]']],
processEscapes: true
},
options: {
skipHtmlTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
};
</script>
</head>
<body class="sidebar-closed">
<!-- モバイル用ハンバーガーボタン(サイドバー外) -->
<button id="sidebar-toggle-mobile">☰</button>
<div id="sidebar">
<button id="sidebar-toggle" title="サイドバーを開閉">☰</button>
<h3 id="sidebar-header">会話履歴</h3>
<ul id="conv-list"></ul>
<button id="new-conv">+ 新しい会話</button>
<!-- ★ インポート・エクスポートを横並びにするラッパー ★ -->
<div id="export-import-wrapper">
<button id="export-conv">エクスポート</button>
<button id="import-conv">インポート</button>
</div>
<!-- ★ 履歴クリアボタンのエリア ★ -->
<div id="clear-history-wrapper">
<button id="clear-history-btn">履歴をすべて削除</button>
</div>
<!-- 隠しファイル入力 (インポート用) -->
<input type="file" id="import-file" accept=".json" multiple style="display:none;">
</div>
<div id="main">
<!-- タイトルに会話名を表示する -->
<h2 id="title"><span id="conv-title"></span></h2>
<div id="chat"></div>
<div id="input-container">
<div id="image-preview-area"></div>
<div id="input-area">
<button id="attach-img-btn" title="画像を添付">🖼️</button>
<input type="file" id="img-input" accept="image/*" multiple style="display:none;">
<textarea id="user-input" placeholder="質問を入力…"></textarea>
<button id="send-btn"title="送信">➤</button>
<button id="stop-btn">■</button>
<button id="cancel-edit-btn" title="キャンセル">×</button>
</div>
</div>
</div>
<div id="ui-blocker"></div>
<script>
/* -------------------------------------------------------------
0. 定数・ストレージキー
------------------------------------------------------------- */
const API_URL = "http://localhost:8080/v1/chat/completions";
const STORAGE_KEY = "llm_conversations";
/* ---- LLM へ渡すパラメータ ---- */
const TEMPERATURE = 0.8; // 生成温度
const TOP_P = 0.95; // nucleus sampling の上限確率
const PRESENCE_PENALTY= 0.0; // 過去に同じトークンが現れたか否かによって、一定のペナルティ
const FREQUENCY_PENALTY= 0.0;//過去に同じトークンが現れた回数が増えれば増えるほど、大きなペナルティ
/* トリミング対象のトークン(必要に応じて追加してください。空だと何もトリミングしません。) */
const TRIM_TOKEN_01 = "";
const TRIM_TOKEN_02 = "";
const TRIM_TOKEN_03 = "";
/* 画像を読み込んだ後リサイズする幅と高さ(環境に合わせて調整してください。大きすぎると読み込めず、小さすぎると上手く認識できません) */
const MAX_WIDTH = 400;
const MAX_HEIGHT = 400;
// スクロール制御用フラグ
let shouldAutoScroll = true;
let isUserScrolling = false;
/* モバイルOSかどうかを判定する関数 */
function isMobileOS() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}
/* -------------------------------------------------------------
1. SYSTEM_ROLE
------------------------------------------------------------- */
const SYSTEM_ROLE_TEMPLATE =`
あなたは優秀なアシスタントです。
・現在の時刻は{time}です。時刻情報が会話に必要ならこれを参照してください。
`;
function formatNow() {
const now = new Date();
const y = now.getFullYear();
const m = String(now.getMonth() + 1).padStart(2, '0');
const d = String(now.getDate()).padStart(2, '0');
const hh = String(now.getHours()).padStart(2, '0');
const mm = String(now.getMinutes()).padStart(2, '0');
return `${y}/${m}/${d} ${hh}:${mm}`;
}
function getSystemRole() {
return SYSTEM_ROLE_TEMPLATE.replace("{time}", formatNow());
}
/* -------------------------------------------------------------
2. UI 要素取得
------------------------------------------------------------- */
const chatDiv = document.getElementById('chat');
const userInput = document.getElementById('user-input');
const sendBtn = document.getElementById('send-btn');
const stopBtn = document.getElementById('stop-btn');
const cancelEditBtn = document.getElementById('cancel-edit-btn');
const convListEl = document.getElementById('conv-list');
const newConvBtn = document.getElementById('new-conv');
const titleEl = document.getElementById('title');
const convTitleEl = document.getElementById('conv-title');
const uiBlocker = document.getElementById('ui-blocker');
const sidebarEl = document.getElementById('sidebar');
const sidebarToggleBtn = document.getElementById('sidebar-toggle');
const sidebarToggleMobileBtn = document.getElementById('sidebar-toggle-mobile');
const bodyEl = document.body;
/* エクスポート / インポート用要素 */
const exportBtn = document.getElementById('export-conv');
const importBtn = document.getElementById('import-conv');
const importFileInput = document.getElementById('import-file');
// 履歴クリアボタンの要素取得
const clearHistoryBtn = document.getElementById('clear-history-btn');
// 画像関連の要素
const attachImgBtn = document.getElementById('attach-img-btn');
const imgInput = document.getElementById('img-input');
const imagePreviewArea = document.getElementById('image-preview-area');
/* -------------------------------------------------------------
3. UI ロック/アンロック
------------------------------------------------------------- */
function blockUI(){
uiBlocker.style.display = 'block';
uiBlocker.style.pointerEvents = 'none';
userInput.disabled = true;
sendBtn.style.display = 'none';
stopBtn.style.display = 'inline-block';
cancelEditBtn.disabled = true;
newConvBtn.disabled = true;
attachImgBtn.disabled = true;
sidebarToggleBtn.disabled = true;
sidebarToggleMobileBtn.disabled = true;
clearHistoryBtn.disabled = true;
// サイドバー本体をロック
sidebarEl.classList.add('locked');
}
function unblockUI(){
uiBlocker.style.display = 'none';
userInput.disabled = false;
sendBtn.style.display = 'inline-block';
stopBtn.style.display = 'none';
cancelEditBtn.disabled = false;
newConvBtn.disabled = false;
attachImgBtn.disabled = false;
sidebarToggleBtn.disabled = false;
sidebarToggleMobileBtn.disabled = false;
clearHistoryBtn.disabled = false;
// サイドバーのロック解除
sidebarEl.classList.remove('locked');
}
/* -------------------------------------------------------------
4. USER-INPUT自動リサイズ
------------------------------------------------------------- */
const tx = document.getElementById('user-input');
function resizeUserInput() {
tx.style.height = '120px';
tx.style.height = tx.scrollHeight + 'px';
if (isMobileOS())
{
tx.style.minHeight = '50px';
tx.style.height = '50px';
tx.style.maxHeight = '300px';
tx.style.height = tx.scrollHeight + 'px';
}
}
if (isMobileOS())
{
resizeUserInput();
}
tx.addEventListener('input', resizeUserInput);
/* -------------------------------------------------------------
5. データ構造と状態管理
------------------------------------------------------------- */
let conversations = [];
let curConvId = null;
let editState = {
active: false,
index: null
};
let isSidebarOpen = false;
let desktopSidebarWasOpen = true; // デスクトップでの展開状態を記憶する変数
// 添付予定の画像(Base64形式の文字列の配列)
let currentPendingImages = [];
/* -------------------------------------------------------------
6. 画像選択・プレビュー処理
------------------------------------------------------------- */
attachImgBtn.addEventListener('click', () => {
imgInput.click();
});
imgInput.addEventListener('change', (e) => {
const files = Array.from(e.target.files);
files.forEach(file => {
processImageFile(file); // 共通関数を呼び出す
});
imgInput.value = ''; // リセット
});
/**
* 画像を読み込み、長辺が指定したサイズになるようにリサイズして新しいData URLを返す非同期関数
* param {string} dataUrl - 読み込まれた画像のData URL
* returns {Promise<string>} リサイズ後の画像のData URL
*/
function resizeImage(dataUrl) {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
let width = img.width;
let height = img.height;
// アスペクト比を維持しながら、長辺が指定サイズを超える場合に縮小する
if (width > MAX_WIDTH || height > MAX_HEIGHT) {
let ratio = Math.min(MAX_WIDTH / width, MAX_HEIGHT / height);
width = width * ratio;
height = height * ratio;
}
// Canvasの設定
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
// 画像を描画(リサイズ適用)
ctx.drawImage(img, 0, 0, width, height);
// リサイズされた画像をData URLとして取得
const resizedDataUrl = canvas.toDataURL('image/jpeg', 0.9); // 品質指定(任意)
resolve(resizedDataUrl);
};
img.onerror = (err) => {
reject(new Error("画像のロードに失敗しました。"));
};
// Data URLをセットして画像ロードを開始
img.src = dataUrl;
});
}
function renderPendingImages() {
imagePreviewArea.innerHTML = '';
currentPendingImages.forEach((dataUrl, index) => {
const wrapper = document.createElement('div');
wrapper.className = 'preview-item';
wrapper.style.backgroundImage = `url(${dataUrl})`;
const removeBtn = document.createElement('button');
removeBtn.className = 'remove-img-btn';
removeBtn.innerHTML = '×';
removeBtn.onclick = () => {
currentPendingImages.splice(index, 1);
renderPendingImages();
};
wrapper.appendChild(removeBtn);
imagePreviewArea.appendChild(wrapper);
});
}
/* -------------------------------------------------------------
7. ペースト・ドラッグアンドドロップ処理
------------------------------------------------------------- */
// --- ペーストイベント (Ctrl+V) ---
userInput.addEventListener('paste', (e) => {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (let item of items) {
if (item.type.indexOf("image") !== -1) {
const file = item.getAsFile();
processImageFile(file);
}
}
});
// --- ドラッグ&ドロップイベント ---
// 画面全体でドロップを受け付ける(誤操作防止のため dragover を抑制)
window.addEventListener('dragover', (e) => {
e.preventDefault();
e.stopPropagation();
});
window.addEventListener('drop', (e) => {
e.preventDefault();
e.stopPropagation();
const files = Array.from(e.dataTransfer.files);
files.forEach(file => {
if (file.type.startsWith('image/')) {
processImageFile(file);
}
});
});
/**
* ファイルを読み込んでリサイズし、プレビューに追加する共通関数
*/
function processImageFile(file) {
const reader = new FileReader();
reader.onload = (ev) => {
const dataUrl = ev.target.result;
resizeImage(dataUrl)
.then(resizedDataUrl => {
currentPendingImages.push(resizedDataUrl);
renderPendingImages();
})