-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1412 lines (1278 loc) · 41.8 KB
/
index.html
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="en">
<head>
<meta charset="UTF-8" />
<title>PenguinAI Playground</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
/*******************************************
* BASE & RESET
*******************************************/
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
height: 100%;
font-family: Arial, sans-serif;
background-color: #000000; /* Dark background */
color: #ffffff; /* Light text */
}
body {
display: flex;
flex-direction: column;
position: relative;
}
/* Scrollbar styles (optional) */
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
background: #222222;
}
::-webkit-scrollbar-thumb {
background: #555555;
border-radius: 5px;
}
::-webkit-scrollbar-thumb:hover {
background: #777777;
}
/*******************************************
* SETTINGS BUTTON
*******************************************/
.settings-btn {
position: absolute;
top: 10px;
right: 10px;
background-color: #404040;
color: #ffffff;
border: none;
padding: 8px 12px;
border-radius: 4px;
cursor: pointer;
z-index: 999;
transition: background-color 0.2s;
}
.settings-btn:hover {
background-color: #525252;
}
/*******************************************
* SETTINGS DRAWER
*******************************************/
.settings-drawer {
position: absolute;
top: 50px;
right: 10px;
width: 250px;
background-color: #111111;
border: 2px solid #222222;
border-radius: 6px;
padding: 10px;
display: none;
z-index: 1000;
animation: fadeIn 0.3s forwards;
}
.settings-drawer.active {
display: block;
}
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
.settings-drawer h3 {
font-size: 1.1rem;
margin-bottom: 10px;
}
.settings-drawer label {
display: block;
margin: 10px 0 5px 0;
}
.settings-drawer select,
.settings-drawer button,
.settings-drawer input[type="file"] {
width: 100%;
margin-bottom: 10px;
padding: 8px;
background-color: #444444;
color: #ffffff;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s;
}
.settings-drawer select:hover,
.settings-drawer button:hover,
.settings-drawer input[type="file"]:hover {
background-color: #555555;
}
.settings-drawer .checkbox-row {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
}
/* NEW: Add a close button inside the drawer */
.close-settings-btn {
display: none; /* Hidden on desktop */
background-color: transparent;
border: none;
color: #ffffff;
font-size: 1.2rem;
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
z-index: 1001; /* Above the drawer content */
}
.close-settings-btn:hover {
background-color: #222222;
border-radius: 50%;
}
/*******************************************
* TABS
*******************************************/
.tabs {
display: flex;
background-color: #111111;
border-bottom: 2px solid #222222;
}
.tab {
flex: 1;
padding: 15px;
text-align: center;
cursor: pointer;
color: #aaaaaa;
transition: background-color 0.2s, color 0.2s;
user-select: none;
}
.tab:hover {
background-color: #222222;
}
.tab.active {
background-color: #222222;
color: #ffffff;
font-weight: bold;
}
.tab-content {
display: none;
animation: fadeTab 0.3s;
}
.tab-content.active {
display: block;
}
@keyframes fadeTab {
from { opacity: 0; }
to { opacity: 1; }
}
/* Dropdown for mobile tabs */
#tabDropdown {
display: none; /* Hidden by default (desktop) */
width: 100%;
padding: 10px;
background-color: #111111;
color: #ffffff;
border: 2px solid #222222;
border-radius: 0;
appearance: none;
-webkit-appearance: none; /* Remove default arrow in some browsers */
-moz-appearance: none;
background-image: url('data:image/svg+xml;charset=US-ASCII,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12"><path fill="%23ffffff" d="M6 8.4L2.4 4.8 3.6 3.6 6 6l2.4-2.4 1.2 1.2z"/></svg>');
background-repeat: no-repeat;
background-position: right 10px center;
background-size: 12px;
cursor: pointer;
}
/*******************************************
* MAIN CONTENT
*******************************************/
.main-content {
flex: 1;
overflow-y: auto;
padding: 20px;
position: relative;
display: flex;
flex-direction: column;
}
/*******************************************
* FOOTERS (CHAT & IMAGES INPUT)
*******************************************/
.footer {
display: flex;
align-items: center;
gap: 10px;
background-color: #111111;
border-top: 2px solid #222222;
padding: 10px;
}
.footer textarea {
flex: 1;
background-color: #333333;
color: #ffffff;
border: none;
border-radius: 4px;
padding: 8px;
resize: none;
max-height: 100px;
overflow-y: auto;
line-height: 1.3;
}
.footer button {
background-color: #333333;
color: #ffffff;
border: none;
border-radius: 4px;
padding: 8px 12px;
cursor: pointer;
transition: background-color 0.2s;
white-space: nowrap;
}
.footer button:hover {
background-color: #444444;
}
.footer button:disabled {
background-color: #555555;
cursor: not-allowed;
}
/*******************************************
* CHAT LOG / MESSAGES
*******************************************/
.chat-log {
display: flex;
flex-direction: column;
gap: 15px;
}
.chat-message {
display: flex;
align-items: center;
margin-bottom: 10px;
}
.chat-message.user {
justify-content: flex-end; /* Align user messages to right */
}
.chat-message.ai {
justify-content: flex-start; /* Align AI messages to left */
}
.chat-bubble {
max-width: 60%;
background-color: #222222;
border-radius: 8px;
padding: 10px;
word-wrap: break-word;
line-height: 1.4;
color: #ffffff;
position: relative;
}
.chat-message.user .chat-bubble {
background-color: #007bff;
color: #ffffff;
}
.chat-message.ai .chat-bubble {
background-color: #333333;
color: #ffffff;
}
/* EDIT/DELETE */
.edit-delete-container {
display: flex;
flex-direction: column;
gap: 4px;
}
.edit-delete-btn {
background-color: #444444;
border: none;
color: #ffffff;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.75rem;
cursor: pointer;
transition: background-color 0.2s;
}
.edit-delete-btn:hover {
background-color: #666666;
}
.chat-message.user .edit-delete-container {
margin-left: 10px;
}
.chat-message.ai .edit-delete-container {
margin-right: 10px;
}
/*******************************************
* CODE SNIPPETS
*******************************************/
pre {
background-color: #111111;
padding: 10px;
border-radius: 6px;
max-height: 200px;
overflow: auto;
color: #00ff00;
font-family: Consolas, monospace;
margin: 0;
}
pre code {
white-space: pre-wrap;
}
.snippet-controls {
display: flex;
gap: 8px;
margin-top: 5px;
}
.snippet-btn, .open-tab-btn {
background-color: #444444;
color: #ffffff;
border: none;
border-radius: 4px;
padding: 4px 8px;
font-size: 0.8rem;
cursor: pointer;
transition: background-color 0.2s;
}
.snippet-btn:hover, .open-tab-btn:hover {
background-color: #666666;
}
/*******************************************
* IMAGES
*******************************************/
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
.image-grid img {
width: 100%;
height: auto;
border-radius: 8px;
cursor: pointer;
transition: transform 0.2s;
border: 2px solid #333333;
}
.image-grid img:hover {
transform: scale(1.03);
}
/*******************************************
* IMAGE MODAL
*******************************************/
.modal {
display: none; /* Hidden by default */
position: fixed;
z-index: 9999;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.8);
align-items: center;
justify-content: center;
}
.modal-content {
background-color: #111111;
padding: 20px;
border-radius: 8px;
position: relative;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.modal-content img {
max-width: 90vw;
max-height: 80vh;
}
.close-modal {
position: absolute;
top: 10px;
right: 10px;
background-color: transparent;
color: #ffffff;
font-size: 1.5rem;
border: none;
cursor: pointer;
}
.download-btn {
background-color: #555555;
color: #ffffff;
border: none;
border-radius: 4px;
padding: 8px 14px;
cursor: pointer;
transition: background-color 0.2s;
display: none; /* Hidden until image is loaded */
}
.download-btn.visible {
display: block;
}
.download-btn:hover {
background-color: #777777;
}
/*******************************************
* EDITING MESSAGES IN-LINE (DESKTOP-ONLY)
*******************************************/
.edit-box {
background-color: #222222;
padding: 8px;
border-radius: 6px;
margin-top: 5px;
display: flex;
flex-direction: column;
gap: 8px;
}
.edit-box textarea {
width: 100%;
background-color: #333333;
color: #ffffff;
border: none;
border-radius: 4px;
padding: 6px;
resize: none;
max-height: 100px;
overflow-y: auto;
}
.edit-controls {
display: flex;
gap: 8px;
justify-content: flex-end;
}
.edit-controls button {
background-color: #444444;
padding: 4px 12px;
border: none;
border-radius: 4px;
cursor: pointer;
color: #ffffff;
}
.edit-controls button:hover {
background-color: #666666;
}
/*******************************************
* MOBILE-FIRST & RESPONSIVE
*******************************************/
@media (max-width: 480px) {
/* Hide the horizontal .tabs on mobile */
.tabs {
display: none;
}
/* Display the mobile-friendly dropdown */
#tabDropdown {
display: block;
}
/* Make the chat bubbles span more width if desired */
.chat-bubble {
max-width: 80%;
}
/* Adjust footers to stack vertically */
.footer {
flex-direction: column;
gap: 8px;
}
.footer textarea {
width: 100%;
height: 60px;
}
.footer button {
width: 100%;
margin-top: 5px;
}
/* Reduce padding for the main content on mobile */
.main-content {
padding: 10px;
}
/* Image grid in one column on very small screens */
.image-grid {
grid-template-columns: 1fr;
gap: 10px;
}
/* Make settings drawer full-screen if open */
.settings-drawer {
width: 100%;
height: 100%;
top: 0;
right: 0;
border-radius: 0;
overflow-y: auto;
}
/* Adjust modal sizing on mobile */
.modal-content {
width: 90%;
height: 90%;
}
.modal-content img {
max-width: 100%;
max-height: 70vh;
}
/* SHOW the close button only on mobile */
.close-settings-btn {
display: block;
}
}
</style>
</head>
<body>
<!-- SETTINGS BUTTON -->
<button class="settings-btn" id="settingsBtn">Settings</button>
<!-- SETTINGS DRAWER -->
<div class="settings-drawer" id="settingsDrawer">
<!-- CLOSE BUTTON INSIDE THE DRAWER (ONLY SHOW ON MOBILE) -->
<button class="close-settings-btn" id="closeSettingsBtn">×</button>
<h3>Settings</h3>
<!-- Chat Model -->
<label for="chatModelSelect">Chat Model:</label>
<select id="chatModelSelect"></select>
<!-- Image Model -->
<label for="imageModelSelect">Image Model:</label>
<select id="imageModelSelect"></select>
<!-- Stream Toggle -->
<div class="checkbox-row">
<input type="checkbox" id="streamToggle" checked />
<label for="streamToggle" style="margin: 0;">Enable Streaming</label>
</div>
<!-- Import Chat -->
<label>Import Chat:</label>
<input type="file" id="importChatInput" accept=".json" />
<!-- Export Chat -->
<button id="exportChatBtnDrawer">Export Chat</button>
</div>
<!-- TABS (desktop) -->
<div class="tabs">
<div class="tab active" data-tab="chat">Chat</div>
<div class="tab" data-tab="images">Images</div>
</div>
<!-- Dropdown for Tabs (mobile) -->
<select id="tabDropdown">
<option value="chat" selected>Chat</option>
<option value="images">Images</option>
</select>
<!-- MAIN CONTENT -->
<div class="main-content">
<!-- Chat Tab -->
<div class="tab-content active" id="chatTab">
<div class="chat-log" id="chatLog"></div>
</div>
<!-- Images Tab -->
<div class="tab-content" id="imagesTab">
<h2>Generated Images</h2>
<div class="image-grid" id="imageResults"></div>
</div>
</div>
<!-- FOOTER (CHAT INPUT) -->
<div class="footer" id="chatFooter">
<textarea id="chatInput" placeholder="Type your message..."></textarea>
<button id="sendChatBtn">Send</button>
<button id="clearChatBtn">Clear Chat</button>
</div>
<!-- FOOTER (IMAGES INPUT) -->
<div class="footer" id="imagesFooter" style="display:none;">
<textarea id="imagePrompt" placeholder="Describe your image..."></textarea>
<button id="generateImageBtn">Generate</button>
<button id="clearImagesBtn">Clear Images</button>
</div>
<!-- MODAL (Enlarged Image) -->
<div class="modal" id="imageModal">
<div class="modal-content">
<button class="close-modal" id="closeModalBtn">×</button>
<img id="modalImage" src="" alt="enlarged" />
<button id="downloadImageBtn" class="download-btn">Download</button>
</div>
</div>
<script>
/******************************************
* UTILITY: Detect if on mobile
******************************************/
function isMobile() {
return window.matchMedia("(max-width: 480px)").matches;
}
/******************************************
* CONSTANTS & STORAGE KEYS
******************************************/
const CHAT_STORAGE_KEY = 'penguinAI_chat_history';
const IMAGE_STORAGE_KEY = 'penguinAI_images_history';
const CHAT_MODEL_KEY = 'penguinAI_chat_model';
const IMAGE_MODEL_KEY = 'penguinAI_image_model';
const STREAM_ENABLED_KEY = 'penguinAI_stream_enabled';
const DEFAULT_CHAT_MODEL = 'claude-3.5-sonnet'; // Example default
const FOUR_HOURS_MS = 4 * 60 * 60 * 1000;
let chatHistory = JSON.parse(localStorage.getItem(CHAT_STORAGE_KEY)) || [];
let imagesHistory = JSON.parse(localStorage.getItem(IMAGE_STORAGE_KEY)) || [];
/******************************************
* DOM ELEMENTS
******************************************/
const settingsBtn = document.getElementById('settingsBtn');
const settingsDrawer = document.getElementById('settingsDrawer');
const closeSettingsBtn = document.getElementById('closeSettingsBtn'); // New close button
const chatModelSelect = document.getElementById('chatModelSelect');
const imageModelSelect = document.getElementById('imageModelSelect');
const streamToggle = document.getElementById('streamToggle');
const importChatInput = document.getElementById('importChatInput');
const exportChatBtnDrawer = document.getElementById('exportChatBtnDrawer');
const tabs = document.querySelectorAll('.tab');
const tabDropdown = document.getElementById('tabDropdown');
const chatTabContent = document.getElementById('chatTab');
const imagesTabContent = document.getElementById('imagesTab');
const chatFooter = document.getElementById('chatFooter');
const imagesFooter = document.getElementById('imagesFooter');
const chatLog = document.getElementById('chatLog');
const chatInput = document.getElementById('chatInput');
const sendChatBtn = document.getElementById('sendChatBtn');
const clearChatBtn = document.getElementById('clearChatBtn');
const imageResults = document.getElementById('imageResults');
const imagePrompt = document.getElementById('imagePrompt');
const generateImageBtn = document.getElementById('generateImageBtn');
const clearImagesBtn = document.getElementById('clearImagesBtn');
const imageModal = document.getElementById('imageModal');
const modalImage = document.getElementById('modalImage');
const closeModalBtn = document.getElementById('closeModalBtn');
const downloadImageBtn = document.getElementById('downloadImageBtn');
/******************************************
* SETTINGS DRAWER & CLOSING LOGIC
******************************************/
let drawerOpen = false;
settingsBtn.addEventListener('click', () => {
drawerOpen = !drawerOpen;
settingsDrawer.classList.toggle('active', drawerOpen);
});
// Close via the new close button (only visible on mobile)
closeSettingsBtn.addEventListener('click', () => {
drawerOpen = false;
settingsDrawer.classList.remove('active');
});
// Close the settings drawer when clicking outside of it (desktop and mobile)
window.addEventListener('click', (event) => {
// If the settings drawer is open and the click is outside of it and not on the settings button
if (drawerOpen &&
!settingsDrawer.contains(event.target) &&
!settingsBtn.contains(event.target)) {
drawerOpen = false;
settingsDrawer.classList.remove('active');
}
});
// Export Chat
exportChatBtnDrawer.addEventListener('click', () => {
if (!chatHistory.length) {
alert("No conversation to export!");
return;
}
exportChatHistory();
});
// Import Chat
importChatInput.addEventListener('change', handleImportChat);
function handleImportChat(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = (event) => {
try {
const imported = JSON.parse(event.target.result);
if (Array.isArray(imported)) {
chatHistory = imported;
localStorage.setItem(CHAT_STORAGE_KEY, JSON.stringify(chatHistory));
renderChat();
alert("Chat imported successfully!");
} else {
alert("Invalid chat JSON. Must be an array of messages.");
}
} catch (err) {
alert("Failed to parse chat JSON.");
}
};
reader.readAsText(file);
}
/******************************************
* TAB SWITCHING LOGIC - DESKTOP
******************************************/
tabs.forEach(tab => {
tab.addEventListener('click', () => {
// Deactivate all tabs and contents
tabs.forEach(t => t.classList.remove('active'));
chatTabContent.classList.remove('active');
imagesTabContent.classList.remove('active');
chatFooter.style.display = 'none';
imagesFooter.style.display = 'none';
// Activate the selected tab and its content
tab.classList.add('active');
const which = tab.getAttribute('data-tab');
if (which === 'chat') {
chatTabContent.classList.add('active');
chatFooter.style.display = 'flex';
setTimeout(scrollToBottom, 200);
} else {
imagesTabContent.classList.add('active');
imagesFooter.style.display = 'flex';
}
});
});
/******************************************
* TAB SWITCHING LOGIC - MOBILE (Dropdown)
******************************************/
tabDropdown.addEventListener('change', (e) => {
const selectedTab = e.target.value;
// Deactivate all tabs and contents
tabs.forEach(t => t.classList.remove('active'));
chatTabContent.classList.remove('active');
imagesTabContent.classList.remove('active');
chatFooter.style.display = 'none';
imagesFooter.style.display = 'none';
if (selectedTab === 'chat') {
chatTabContent.classList.add('active');
chatFooter.style.display = 'flex';
setTimeout(scrollToBottom, 200);
} else if (selectedTab === 'images') {
imagesTabContent.classList.add('active');
imagesFooter.style.display = 'flex';
}
});
function scrollToBottom() {
chatLog.scrollTop = chatLog.scrollHeight;
}
/******************************************
* LOAD MODELS
******************************************/
async function loadChatModels() {
try {
const resp = await fetch("https://api.penguinai.tech/v1/models");
if (!resp.ok) throw new Error("Chat model load failed.");
const data = await resp.json();
const chatModels = data.data.filter(m => m.type === "chat.completions");
chatModelSelect.innerHTML = "";
chatModels.forEach(m => {
const opt = document.createElement('option');
opt.value = m.id;
opt.textContent = m.id;
chatModelSelect.appendChild(opt);
});
// Load saved or default chat model
let savedChatModel = localStorage.getItem(CHAT_MODEL_KEY);
if (savedChatModel && chatModels.some(c => c.id === savedChatModel)) {
chatModelSelect.value = savedChatModel;
} else {
const foundDefault = chatModels.find(c => c.id === DEFAULT_CHAT_MODEL);
if (foundDefault) {
chatModelSelect.value = DEFAULT_CHAT_MODEL;
localStorage.setItem(CHAT_MODEL_KEY, DEFAULT_CHAT_MODEL);
} else {
chatModelSelect.selectedIndex = 0;
localStorage.setItem(CHAT_MODEL_KEY, chatModelSelect.value);
}
}
} catch (err) {
console.error(err);
chatModelSelect.innerHTML = '<option value="Failed">Failed</option>';
}
}
async function loadImageModels() {
try {
const resp = await fetch("https://api.penguinai.tech/v1/models");
if (!resp.ok) throw new Error("Image model load failed.");
const data = await resp.json();
const imageModels = data.data.filter(m => m.type === "images.generations");
imageModelSelect.innerHTML = "";
imageModels.forEach(m => {
const opt = document.createElement('option');
opt.value = m.id;
opt.textContent = m.id;
imageModelSelect.appendChild(opt);
});
// Example default to "flux" if available
let defaultImageModel = imageModels.find(i => i.id === "flux")?.id || imageModels[0]?.id;
// Load saved or default image model
let savedImageModel = localStorage.getItem(IMAGE_MODEL_KEY);
if (savedImageModel && imageModels.some(i => i.id === savedImageModel)) {
imageModelSelect.value = savedImageModel;
} else if (defaultImageModel) {
imageModelSelect.value = defaultImageModel;
localStorage.setItem(IMAGE_MODEL_KEY, defaultImageModel);
}
} catch (err) {
console.error(err);
imageModelSelect.innerHTML = '<option value="Failed">Failed</option>';
}
}
chatModelSelect.addEventListener('change', () => {
localStorage.setItem(CHAT_MODEL_KEY, chatModelSelect.value);
});
imageModelSelect.addEventListener('change', () => {
localStorage.setItem(IMAGE_MODEL_KEY, imageModelSelect.value);
});
streamToggle.addEventListener('change', () => {
localStorage.setItem(STREAM_ENABLED_KEY, JSON.stringify(streamToggle.checked));
});
/******************************************
* INITIAL INIT
******************************************/
async function init() {
await loadChatModels();
await loadImageModels();
loadStreamToggle();
cleanupExpiredImages();
renderImages();
renderChat();
scrollToBottom();
}
init();
function loadStreamToggle() {
const savedStreamState = JSON.parse(localStorage.getItem(STREAM_ENABLED_KEY));
if (savedStreamState !== null) {
streamToggle.checked = savedStreamState;
} else {
streamToggle.checked = true;
localStorage.setItem(STREAM_ENABLED_KEY, JSON.stringify(true));
}
}
/******************************************
* CONVERSATION RENDER
******************************************/
function renderChat() {
chatLog.innerHTML = "";
for (let i = 0; i < chatHistory.length; i++) {
const msg = chatHistory[i];
const msgDiv = document.createElement('div');
msgDiv.classList.add('chat-message');
msgDiv.classList.add(msg.role === 'user' ? 'user' : 'ai');
const bubble = document.createElement('div');
bubble.classList.add('chat-bubble');
const parts = parseCodeSnippets(msg.content);
for (let part of parts) {
if (part.type === 'text') {
let p = document.createElement('p');
p.style.margin = '0';
p.style.padding = '0';
p.innerHTML = parseMarkdown(part.content);
bubble.appendChild(p);
} else if (part.type === 'code') {
let pre = document.createElement('pre');
let codeEl = document.createElement('code');
codeEl.textContent = part.content;
pre.appendChild(codeEl);
// Snippet controls
let snippetControls = document.createElement('div');
snippetControls.classList.add('snippet-controls');
// Copy button
let copyBtn = document.createElement('button');
copyBtn.classList.add('snippet-btn');
copyBtn.textContent = 'Copy';
copyBtn.addEventListener('click', () => {
navigator.clipboard.writeText(part.content)
.then(() => {
copyBtn.textContent = 'Copied!';
setTimeout(() => copyBtn.textContent = 'Copy', 2000);
})
.catch(err => console.error("Clipboard error:", err));
});
snippetControls.appendChild(copyBtn);
// Open in new tab (if detected HTML)
if (detectLanguage(part.content) === 'html') {
let openTabBtn = document.createElement('button');
openTabBtn.classList.add('open-tab-btn');
openTabBtn.textContent = 'Open in New Tab';
openTabBtn.addEventListener('click', () => {
let newWin = window.open();
newWin.document.write(part.content);
newWin.document.close();
});
snippetControls.appendChild(openTabBtn);
}
bubble.appendChild(pre);
bubble.appendChild(snippetControls);
}
}
// Edit/Delete container
const edContainer = document.createElement('div');
edContainer.classList.add('edit-delete-container');
const editBtn = document.createElement('button');
editBtn.classList.add('edit-delete-btn');
editBtn.textContent = 'Edit';
// CHANGED: Different edit flow for mobile vs. desktop
editBtn.addEventListener('click', () => {
if (isMobile()) {
// 1) On mobile, open a prompt
openMobileEditPrompt(i);
} else {
// 2) On desktop, do inline editing
openEditBox(i, bubble);
}
});
const delBtn = document.createElement('button');
delBtn.classList.add('edit-delete-btn');
delBtn.textContent = 'X';
delBtn.addEventListener('click', () => deleteMessage(i));
edContainer.appendChild(editBtn);
edContainer.appendChild(delBtn);
// Place elements based on role
if (msg.role === 'user') {
msgDiv.appendChild(bubble);
msgDiv.appendChild(edContainer);
} else {
msgDiv.appendChild(edContainer);
msgDiv.appendChild(bubble);
}
chatLog.appendChild(msgDiv);
}
}
/******************************************
* MOBILE EDIT PROMPT
******************************************/
function openMobileEditPrompt(index) {
const existingContent = chatHistory[index].content;
const newContent = prompt("Edit your message:", existingContent);
if (newContent !== null) {
let trimmed = newContent.trim();