-
Notifications
You must be signed in to change notification settings - Fork 21
/
p_ui.js
7702 lines (5999 loc) · 297 KB
/
p_ui.js
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
let simple_tasks_ordering = 'index';
let first_ui_generation_done = false;
let previous_selected_assistant = null;
// Option to scroll down in the chat window through JS: this.uiChat.scrollTo(0, this.uiChat.scrollHeight);
//
// ADD CHAT MESSAGE
//
//window.last_chat_times = {};
function add_chat_message(pane,participant,message,i18n_code=null,task_output=null,task_index=null){ //,save_to_conversations=false){
//save_to_conversations = false;
let message_to_be_filled_later = false;
if(window.settings.settings_complexity == 'developer'){
//console.log("in add_chat_message.");
//console.log("- pane: ", pane);
//console.log("- participant: ", participant);
//console.log("- message: ", message);
//console.log("- i18n_code: ", i18n_code);
//console.log("- task_output: ", task_output);
//console.log("- task_index: ", task_index);
//console.log("add_chat_message: message: ", message);
//console.log("add_chat_message: message: -->" + message + "<--");
}
try{
/*
let pane = null;
if(typeof participant == 'string'){
pane = participant;
}
else if(typeof participant == 'object' && participant != null){
if(typeof participant.pane == 'string'){
pane = participant.pane;
}
if(typeof participant.participant == 'string'){
participant = participant.participant;
}
else{
participant = window.settings.assistant;
}
}
*/
if(typeof pane == 'undefined' || pane == null){
pane = window.settings.assistant;
}
if(pane == 'current'){
//console.log("participant is current");
pane = window.settings.assistant;
//console.log("add_chat_message: changed pane from 'current' to: ", pane);
}
if(typeof participant == 'undefined' || participant == null){
participant = window.settings.assistant;
}
let special_task_index_prefix = '';
if(typeof task_index != 'number' && typeof task_index != 'string'){
task_index = window.task_counter;
}
/*
else{
//console.log("setting special_task_index_prefix to: ", pane);
special_task_index_prefix = '-' + pane;
}
*/
special_task_index_prefix = pane + '-';
// Ignore musicgen preload response message
if(typeof message == 'string' && message == 'papegai_preload'){
return;
}
//const original_participant = participant;
if(typeof message != 'string'){
console.error("add_chat_message: Error, message was not a string: ", typeof message, message);
console.error("add_chat_message: pane,participant: ", pane,participant);
return false
}
// They can't both be null. Eithe there's a text message, of there's an element/html to place in the output div (which will then be filled in later)
if(message.trim() == '' && (typeof task_output == 'undefined' || task_output == null || (typeof task_output == 'string' && task_output == ''))){
if(pane != 'developer'){
console.error("add_chat_message: message was empty string, not adding it.");
}
return false
}
if(message.trim() == '......'){
message_to_be_filled_later = true;
message = '';
}
if(participant == 'user' && message.trim() == ''){
console.error("add_chat_message: user's message was empty, not posting it");
return false
}
const original_message = message;
const my_message_nr = window.chat_message_counter;
if(participant == 'current'){
//console.log("participant is current");
participant = window.settings.assistant;
//console.log("add_chat_message: changed participant from 'current' to: ", participant);
}
const original_participant = participant;
//console.log("adding chat message to element ID: ", pane_id);
// chat message container
let chat_message_el = document.createElement('div');
chat_message_el.classList.add('message');
chat_message_el.classList.add('pane-' + participant);
if(message_to_be_filled_later){
chat_message_el.classList.add('to-be-filled');
}
chat_message_el.setAttribute('data-message-nr', my_message_nr);
// TODO check if assistant has 'chatter' enabled? If so, then there's no need to add an ID.
if(typeof task_index == 'number' && original_participant != 'developer' && original_participant != 'user' && pane != 'developer'){
const message_el_id = 'chat-message-' + pane + '-' + original_participant + task_index;
const already_existing_message_el = document.querySelector('#' + message_el_id);
if(already_existing_message_el != null){
console.error("add_chat_message: the chat message seems to already exist! ID: #" + message_el_id);
}
else if(typeof message == 'string' && !message.endsWith('#setting---')){
chat_message_el.setAttribute('id',message_el_id);
}
}
let bubble_wrap_el = document.createElement('div');
bubble_wrap_el.classList.add('chat-bubble-wrap');
let chat_profile_pic_el = document.createElement('div');
let task_output_el = document.createElement('div');
task_output_el.classList.add('chat-message-task-output');
//if((participant == 'speaker' || participant == 'translator' || participant == 'musicgen' || participant == 'imager') && !message.endsWith('#setting---')){
// task_output_el.setAttribute('id','chat-message-task-' + participant + window.task_counter);
//}
if(typeof task_output != 'undefined' && task_output != null){
if(!message.endsWith('#setting---')){
task_output_el.setAttribute('id','chat-message-task-' + special_task_index_prefix + participant + task_index);
}
//console.log("add_chat_message: task_output was provided: ", task_output);
if(typeof task_output == 'string'){
//console.log("add_chat_message: setting provided string as initial task output innerHTML: ", task_output);
task_output_el.innerHTML = task_output;
}
else if(task_output instanceof Element || task_output instanceof HTMLDocument){
//console.log("add_chat_message: add_chat_message: setting provided element as initial task output child: ", task_output);
task_output_el.appendChild(task_output);
}
else{
console.error("add_chat_message: invalid task_output value provided, ignoring: ", task_output);
}
}
// Chat bubble
let bubble_el = document.createElement('div');
bubble_el.classList.add('bubble');
if(original_message.startsWith('_$_')){
bubble_el.setAttribute('data-i18n',original_message);
}
/* SPECIAL MESSAGES */
if(participant != 'user' && message.endsWith('#setting---')){
const special_type = message.split('#')[0];
//console.log("add_chat_message: spotted special_type: -->" + special_type + "<--");
message = document.createElement('div');
message.classList.add('chat-setting');
//message.innerHTML = '<p>' + special_type.replaceAll('_',' ') + '</p>';
let setting_wrapper_el = document.createElement('div');
setting_wrapper_el.classList.add('chat-setting-wrapper');
if(special_type == 'language'){
let en_button_el = document.createElement('button');
en_button_el.textContent = 'English';
en_button_el.addEventListener("click", (event) => {
window.settings.language = 'en'; // sic - purposefully not using set_language here, so the UI is generated and then translated
window.settings['language'] = 'en';
save_settings();
generate_ui();
translate();
setting_language_dropdown_el.value = 'en';
});
let nl_button_el = document.createElement('button');
if(window.settings.language != 'nl'){
nl_button_el.textContent = 'Dutch';
}else{
nl_button_el.textContent = 'Nederlands';
}
nl_button_el.addEventListener("click", (event) => {
window.settings.language = 'nl'; // sic - purposefully not using set_language here, so the UI is generated and then translated
window.settings['language'] = 'nl';
save_settings();
generate_ui();
translate();
setting_language_dropdown_el.value = 'nl';
});
setting_wrapper_el.appendChild(en_button_el);
setting_wrapper_el.appendChild(nl_button_el);
setting_wrapper_el.classList.add('text-align-right');
}
else if(special_type == 'scribe_privacy'){
let medium_button_el = document.createElement('button');
medium_button_el.textContent = get_translation('Pseudonym');
medium_button_el.setAttribute('data-i18n','Pseudonym');
medium_button_el.addEventListener("click", (event) => {
window.settings.privacy_level = 'Medium';
window.settings.assistants['scribe'].privacy_level = 'Medium';
save_settings();
explanation1_el.classList.add('selected');
explanation2_el.classList.remove('selected');
if(window.microphone_enabled == false){
window.enable_microphone();
}
//generate_ui();
//setting_privacy_dropdown_el.value = 'en';
});
let high_button_el = document.createElement('button');
high_button_el.textContent = get_translation('Redacted');
high_button_el.setAttribute('data-i18n','Redacted');
high_button_el.addEventListener("click", (event) => {
window.settings.privacy_level = 'High';
window.settings.assistants['scribe'].privacy_level = 'High';
save_settings();
explanation1_el.classList.remove('selected');
explanation2_el.classList.add('selected');
//generate_ui();
//setting_privacy_dropdown_el.value = 'nl';
if(window.microphone_enabled == false){
window.enable_microphone();
}
});
let pre_explanation_title_el = document.createElement('h2');
pre_explanation_title_el.textContent = get_translation('Privacy');
pre_explanation_title_el.setAttribute('data-i18n','Privacy');
let pre_explanation_el = document.createElement('p');
pre_explanation_el.classList.add('small-vertical-margin');
pre_explanation_title_el.setAttribute('data-i18n','What_should_be_shown_until_a_speaker_has_given_their_consent_to_be_recorded');
pre_explanation_el.textContent = get_translation('What_should_be_shown_until_a_speaker_has_given_their_consent_to_be_recorded');
let explanations_container_el = document.createElement('div');
explanations_container_el.classList.add("big-chat-bubble-settings-container");
explanations_container_el.classList.add('flex-wrap');
let explanation1_el = document.createElement('div');
explanation1_el.classList.add("big-chat-bubble-setting-option");
//
explanation1_el.innerHTML = '<h3 data-i18n="Pseudonym">' + get_translation('Pseudonym') + '</h3><img src="./images/privacy_pseudonymous.svg" alt="' + get_translation('Pseudonym') + '"><p data-i18n="Scribe_medium_privacy_level_explanation">' + get_translation('Scribe_medium_privacy_level_explanation') + '</p>';
if(window.settings.assistants['scribe'].privacy_level == 'Medium'){
explanation1_el.classList.add('selected');
}
let explanation2_el = document.createElement('div');
explanation2_el.classList.add("big-chat-bubble-setting-option");
//
explanation2_el.innerHTML = '<h3 data-i18n="Redacted">' + get_translation('Redacted') + '</h3><img src="./images/privacy_redacted.svg" alt="' + get_translation('Redacted') + '"><p data-i18n="Scribe_high_privacy_level_explanation">' + get_translation('Scribe_high_privacy_level_explanation') + '</p>';
if(window.settings.assistants['scribe'].privacy_level == 'High'){
explanation2_el.classList.add('selected');
}
let explanation1_button_wrapper_el = document.createElement('div');
explanation1_button_wrapper_el.classList.add('align-right');
let explanation2_button_wrapper_el = document.createElement('div');
explanation2_button_wrapper_el.classList.add('align-right');
explanation1_button_wrapper_el.appendChild(medium_button_el);
explanation2_button_wrapper_el.appendChild(high_button_el);
explanation1_el.appendChild(explanation1_button_wrapper_el);
explanation2_el.appendChild(explanation2_button_wrapper_el);
setting_wrapper_el.appendChild(pre_explanation_title_el);
setting_wrapper_el.appendChild(pre_explanation_el);
explanations_container_el.appendChild(explanation1_el);
explanations_container_el.appendChild(explanation2_el);
setting_wrapper_el.appendChild(explanations_container_el);
setting_wrapper_el.classList.add('big-settings-bubble');
}
else if(special_type == 'scribe_clock'){
//console.log("adding scribe_clock");
let pre_explanation_title_el = document.createElement('h2');
pre_explanation_title_el.textContent = get_translation('Voice_recordings');
pre_explanation_title_el.setAttribute('data-i18n','Voice_recordings');
let pre_explanation_el = document.createElement('p');
pre_explanation_el.classList.add('small-vertical-margin');
pre_explanation_title_el.setAttribute('data-i18n','For_precise_time_keeping_please_press_start_when_the_voice_recording_should_begin');
pre_explanation_el.textContent = get_translation('For_precise_time_keeping_please_press_start_when_the_voice_recording_should_begin');
let explanation1_el = document.createElement('div');
explanation1_el.classList.add("big-chat-bubble-setting-option");
let time_wrapper_el = document.createElement('div');
time_wrapper_el.classList.add("flex-between");
time_wrapper_el.classList.add("flex-align-bottom");
window.scribe_clock_time_elapsed_el = document.createElement('div');
window.scribe_clock_time_elapsed_el.classList.add('clock-time-elapsed');
window.scribe_clock_time_elapsed_el.setAttribute('id','clock-time-elapsed');
window.scribe_clock_time_elapsed_el.textContent = '00:00';
time_wrapper_el.appendChild(window.scribe_clock_time_elapsed_el);
window.scribe_clock_time_remaining_el = document.createElement('div');
window.scribe_clock_time_remaining_el.classList.add('clock-time-remaining');
window.scribe_clock_time_remaining_el.setAttribute('id','clock-time-remaining');
//window.scribe_clock_time_remaining_el.innerHTML = create_time_remaining_html(window.maximum_scribe_duration);
time_wrapper_el.appendChild(window.scribe_clock_time_remaining_el);
explanation1_el.appendChild(time_wrapper_el);
let time_progress_wrapper_el = document.createElement('div');
time_progress_wrapper_el.classList.add("clock-progress-wrapper");
window.scribe_clock_progress_el = document.createElement('progress');
window.scribe_clock_progress_el.classList.add('clock-progress');
window.scribe_clock_progress_el.setAttribute('id','clock-progress');
window.scribe_clock_progress_el.setAttribute('value',0);
time_progress_wrapper_el.appendChild(window.scribe_clock_progress_el);
let start_transcription_button_el = document.createElement('button');
start_transcription_button_el.classList.add('always-blue-button');
start_transcription_button_el.textContent = get_translation('Start');
start_transcription_button_el.setAttribute('data-i18n','Start');
start_transcription_button_el.addEventListener("click", (event) => {
window.reset_scribe_clock();
// Stop an already started voice transcription first, if there is one
if(typeof window.current_scribe_voice_parent_task_id == 'number'){
window.stop_scribe_voice_task();
}
//window.create_scribe_parent_task('voice');
window.last_time_scribe_started = Date.now();
if(window.microphone_enabled == false){
window.enable_microphone();
}
window.unpause_vad();
window.continuous_vad();
start_transcription_button_el.classList.add('opacity05');
start_transcription_button_el.classList.remove('always-blue-button');
window.scribe_clock_progress_el.value = 0;
window.scribe_precise_sentences_count = 0; // TODO Maybe check if there is no file currently being transcribed, otherwise it would interrupt it's counting
});
let explanation1_button_wrapper_el = document.createElement('div');
explanation1_button_wrapper_el.classList.add('align-right');
explanation1_button_wrapper_el.appendChild(start_transcription_button_el);
explanation1_el.appendChild(time_wrapper_el);
explanation1_el.appendChild(time_progress_wrapper_el);
explanation1_el.appendChild(explanation1_button_wrapper_el);
//setting_wrapper_el.classList.add('big-settings-bubble');
// Microphone transcription settings
continuous_mic_footer_el = document.createElement('div');
continuous_mic_footer_el.classList.add('continuous-mic-bubble-settings');
continuous_mic_footer_el.setAttribute('id','continuous-mic-bubble-settings');
let continuous_mic_detail_container_el = document.createElement('div');
continuous_mic_detail_container_el.classList.add('flex-vertical');
continuous_mic_detail_container_el.classList.add('area');
let continuous_mic_setting_label_el = document.createElement('label');
continuous_mic_setting_label_el.classList.add('continuous-mic-bubble-settings-label')
continuous_mic_setting_label_el.textContent = window.get_translation('continuous_mic');
continuous_mic_setting_label_el.setAttribute('data-i18n','continuous_mic');
continuous_mic_setting_label_el.setAttribute('for','scribe-continuous-mic-setting');
let continuous_mic_setting_select_el = document.createElement('select');
continuous_mic_setting_select_el.classList.add('model-info-toggle');
continuous_mic_setting_select_el.setAttribute('id','scribe-continuous-mic-setting');
if(typeof window.settings.assistants['scribe'] == 'undefined'){
window.settings.assistants['scribe'] = {'selected':true,'continuous_mic':window.continuous_mic_options[2]};
}
for(let o = 0; o < window.continuous_mic_options.length; o++){
let option_el = document.createElement('option');
option_el.setAttribute('value',window.continuous_mic_options[o]);
option_el.setAttribute('data-i18n',window.continuous_mic_options[o]);
option_el.textContent = window.get_translation(window.continuous_mic_options[o]);
if(typeof window.settings.assistants['scribe'] != 'undefined' && typeof window.settings.assistants['scribe'].continuous_mic == 'string'){
//console.log("window.settings.assistants['scribe'].continuous_mic: ", window.settings.assistants['scribe'].continuous_mic);
if(window.continuous_mic_options[o] == window.settings.assistants['scribe'].continuous_mic){
option_el.setAttribute('selected','selected');
}
}
continuous_mic_setting_select_el.appendChild(option_el);
}
continuous_mic_setting_select_el.addEventListener('change',() => {
//console.log("continuous_mic_options select element changed to: ", continuous_mic_setting_select_el.value);
window.settings.assistants['scribe'].continuous_mic = continuous_mic_setting_select_el.value;
save_settings();
if(window.microphone_enabled == false){
window.enable_microphone();
}
window.continuous_vad();
});
continuous_mic_setting_label_el.setAttribute('data-i18n','continuous_mic');
continuous_mic_detail_container_el.appendChild(continuous_mic_setting_label_el);
continuous_mic_detail_container_el.appendChild(continuous_mic_setting_select_el);
continuous_mic_footer_el.appendChild(continuous_mic_detail_container_el);
setting_wrapper_el.appendChild(pre_explanation_title_el);
setting_wrapper_el.appendChild(pre_explanation_el);
setting_wrapper_el.appendChild(continuous_mic_footer_el);
setting_wrapper_el.appendChild(explanation1_el);
}
else if(special_type == 'scribe_transcription_info'){
//console.log("creating scribe transcription info chat message");
if(task_index == null){
console.error('add_chat_message: scribe_transcription_info element should not be created without a task_index');
}
let speaker_list_title_el = document.createElement('h2');
speaker_list_title_el.textContent = get_translation('Transcription');
speaker_list_title_el.setAttribute('data-i18n','Transcription');
setting_wrapper_el.appendChild(speaker_list_title_el);
let speaker_list_done_el = document.createElement('div');
speaker_list_done_el.textContent = '✅';
speaker_list_done_el.classList.add('scribe-transcription-done-indicator');
setting_wrapper_el.appendChild(speaker_list_done_el);
let speaker_list_container_el = document.createElement('div');
speaker_list_container_el.classList.add('scribe-transcription-info-container');
if(task_index == null){
speaker_list_container_el.setAttribute('id','scribe-transcription-info-container');
}
else{
speaker_list_container_el.setAttribute('id','scribe-transcription-info-container' + task_index);
chat_message_el.setAttribute('id','scribe-transcription-info-container-bubble' + task_index);
}
setting_wrapper_el.appendChild(speaker_list_container_el);
setting_wrapper_el.classList.add('scribe-transcription-info-settings-wrapper');
setTimeout(() => {
window.scroll_chat_to_bottom();
},1);
}
else if(special_type == 'switch_to_other_ai'){ // not used, but an AI could in theory recommend switching to another one
let switch_button_el = document.createElement('button');
switch_button_el.textContent = get_translation('Lets_chat');
switch_button_el.setAttribute('data-i18n','Lets_chat');
switch_button_el.addEventListener("click", (event) => {
switch_assistant(window.other_ai_to_switch_to);
//generate_ui();
//translate();
});
setting_wrapper_el.appendChild(switch_button_el);
setting_wrapper_el.classList.add('text-align-right');
}
else if(special_type == 'switch_to_functionality'){
chat_message_el.classList.add('show-if-mobile');
let functionality_counter = 0;
for (let [key, details] of Object.entries(window.functionality)) {
//console.log(key, details);
if(typeof details.requires_web_gpu == 'boolean' && details.requires_web_gpu == true && window.web_gpu == false){
continue
}
functionality_counter++;
if(typeof details.i18n_code == 'string' && typeof details.assistant_id == 'string'){
let switch_button_el = document.createElement('button');
switch_button_el.textContent = get_translation(details.i18n_code);
switch_button_el.setAttribute('data-i18n',details.i18n_code);
switch_button_el.addEventListener("click", (event) => {
if(window.web_gpu_supported && typeof details.fast_assistant_id == 'string'){
switch_assistant(details.fast_assistant_id);
}
else{
switch_assistant(details.assistant_id);
}
if(typeof details.functions != 'undefined'){
for(let d = 0; d < details.functions.length; d++){
//console.log("switch_to_functionality: doing function: ", details.functions[d]);
window[details.functions[d]]();
}
}
if(typeof details.classes_to_add != 'undefined'){
for(let d = 0; d < details.classes_to_add.length; d++){
//console.log("switch_to_functionality: adding class: ", details.classes_to_add[d]);
document.body.classList.add(details.classes_to_add[d]);
}
}
if(typeof details.classes_to_remove != 'undefined'){
for(let d = 0; d < details.classes_to_remove.length; d++){
//console.log("switch_to_functionality: removing class: ", details.classes_to_remove[d]);
document.body.classList.remove(details.classes_to_remove[d]);
}
}
//generate_ui();
//translate();
});
setTimeout(() => {
setting_wrapper_el.appendChild(switch_button_el);
},functionality_counter * 10);
}
}
}
else if(special_type == 'reveal_sidebar'){
//console.log("add_chat_message: revealing sidebar");
document.body.classList.remove('intro');
return
}
else if(special_type == 'hide_developer_prompt_input'){
//console.log("add_chat_message: setting developer prompt input to hidden");
developer_input_hidden = true;
document.body.classList.add('hide-developer-prompt');
return
}
else if(special_type == 'why_so_slow'){
let why_so_slow_image_el = document.createElement('img');
why_so_slow_image_el.src = './images/waiting_for_download.png';
why_so_slow_image_el.setAttribute('alt','Downloading illustration');
setting_wrapper_el.appendChild(why_so_slow_image_el);
let why_so_slow_title_el = document.createElement('h2');
why_so_slow_title_el.textContent = get_translation('What_is_taking_so_long');
why_so_slow_title_el.setAttribute('data-i18n','What_is_taking_so_long');
setting_wrapper_el.appendChild(why_so_slow_title_el);
let why_so_slow_first_paragraph_el = document.createElement('p');
why_so_slow_first_paragraph_el.innerHTML = '<span data-i18n="The_best_way_to_protect_your_privacy_is_to_never_send_your_data_to_an_AI_in_the_cloud_in_the_first_place">' + get_translation('The_best_way_to_protect_your_privacy_is_to_never_send_your_data_to_an_AI_in_the_cloud_in_the_first_place') + '</span>. <br><br><span data-i18n="Instead_Papeg_ai_brings_the_AI_to_you_by_downloading_a_complete_AI_to_your_device_first">' + get_translation('Instead_Papeg_ai_brings_the_AI_to_you_by_downloading_a_complete_AI_to_your_device_first') + '</span><br/><br/><span data-i18n="You_only_need_to_download_an_AI_once_and_it_will_remain_available">' + get_translation('You_only_need_to_download_an_AI_once_and_it_will_remain_available') + "</span>";
setting_wrapper_el.appendChild(why_so_slow_first_paragraph_el);
chat_message_el.classList.add('why-so-slow-hint');
}
else if(special_type == 'download_progress'){
//console.log("add_chat_message: adding model download progress chat message");
let existing_progress_el = document.getElementById('download-progress-' + participant);
if(existing_progress_el != null){
//console.error("add_chat_message: aborting, download progress chat message already exists for: ", participant);
return
}
let cancel_download_button_el = document.createElement('div');
cancel_download_button_el.classList.add('cancel-download-button');
cancel_download_button_el.textContent = '×';
cancel_download_button_el.addEventListener('click', () => {
//console.log("clicked on cancel download button");
abort_download(participant);
})
let ai_model_size_part = '<span id="download-ai-model-size' + task_index + '" class="ai-model-size"></span>';
if(typeof window.assistants[participant] != 'undefined' && typeof window.assistants[participant].size == 'number'){
ai_model_size_part = '<span id="download-ai-model-size' + task_index + '" class="ai-model-size"><span class="ai-model-size-number">' + window.assistants[participant].size + '</span><span class="ai-model-size-gb">GB</span></span>';
}
let cached = false;
if(typeof participant == 'string' && participant != 'translator' && typeof window.assistants[participant] != 'undefined'){ // translator is many models
cached = check_if_cached(participant);
}
let participant_download_name = '';
if(typeof window.settings.assistants[participant] != 'undefined' && typeof window.settings.assistants[participant]['custom_name'] == 'string'){
participant_download_name = window.settings.assistants[participant]['custom_name'];
}
else if(typeof window.translations[participant + '_name'] != 'undefined'){
participant_download_name = get_translation(participant + '_name');
}
if(cached){
message.classList.add('cached');
message.innerHTML = '<div class="download-progress-title flex-between" data-i18n="download_progress"><span class="capitalize-first-letter">' + participant_download_name + ' ' + get_translation('loading_progress') + '</span></div>';
}
else{
message.innerHTML = '<div class="download-progress-title flex-between" data-i18n="download_progress"><span class="capitalize-first-letter">' + participant_download_name + ' ' + get_translation('download_progress') + '</span></div>';
//message.appendChild(cancel_download_button_el); // TODO: allow model download to be interrupted
}
//let progress_container_el = document.createElement('div');
//progress_container_el.setAttribute('id','download-progress-' + window.settings.assistant + '-container');
chat_message_el.classList.add('download-progress-chat-message');
let progress_el = document.createElement('progress');
progress_el.classList.add('progress');
progress_el.setAttribute('id','download-progress-' + participant);
//setting_wrapper_el.appendChild(progress_el);
//console.log("progress_el: ", progress_el);
setting_wrapper_el.appendChild(progress_el);
let download_message_footer = document.createElement('div');
download_message_footer.classList.add('download-message-footer');
download_message_footer.classList.add('flex-between');
download_message_footer.innerHTML = ai_model_size_part;
// Estimated download time remaining indicator
let time_remaining_el = document.createElement('div');
time_remaining_el.classList.add('time-remaining');
download_message_footer.appendChild(time_remaining_el);
setting_wrapper_el.appendChild(download_message_footer);
setting_wrapper_el.addEventListener('click', () => {
//console.log("clicked on download message");
if(pane != 'current' && pane != window.settings.assistant){
switch_assistant(pane); // switch to the AI being downloaded
}
setTimeout(()=>{
add_chat_message_once(pane,'developer','why_so_slow#setting---');
},500);
})
message.appendChild(setting_wrapper_el);
chat_message_el.appendChild(message);
message_downloads_container_el.appendChild(chat_message_el);
return;
}
// Currently only really used for Scribe
else if(special_type == 'Would_you_like_to_create_a_new_document'){
//console.log("add_chat_message: special message: adding button to start a new empty document");
message.innerHTML = '<div class="model-example-title" data-i18n="Would_you_like_to_create_a_new_document">' + get_translation('Would_you_like_to_create_a_new_document') + '</div>';
let json_button_el = document.createElement('button');
json_button_el.classList.add('show-if-advanced');
json_button_el.textContent = 'JSON';
json_button_el.addEventListener("click", () => {
//console.log("clicked on button to create new document (for scribe)");
//const date_string = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
//const date_string = new Date().toLocaleString();
load_meeting_notes_example('json','');
window.scribe_precise_sentences_count = 0;
if(window.settings.assistant == 'scribe' && window.microphone_enabled == false){
window.enable_microphone();
}
});
setting_wrapper_el.appendChild(json_button_el);
let subtitle_button_el = document.createElement('button');
subtitle_button_el.textContent = get_translation('Subtitles');
subtitle_button_el.setAttribute('data-i18n','Subtitles');
subtitle_button_el.addEventListener("click", () => {
//console.log("clicked on button to create new document (for scribe)");
//const date_string = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
//const date_string = new Date().toLocaleString();
load_meeting_notes_example('vtt','');
create_scribe_parent_task();
window.scribe_precise_sentences_count = 0;
if(window.settings.assistant == 'scribe' && window.microphone_enabled == false){
window.enable_microphone();
}
});
setting_wrapper_el.appendChild(subtitle_button_el);
let load_button_el = document.createElement('button');
load_button_el.textContent = get_translation('Meeting');
load_button_el.setAttribute('data-i18n','Meeting');
load_button_el.addEventListener("click", () => {
//console.log("clicked on button to create new document (for scribe)");
//const date_string = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
//const date_string = new Date().toLocaleString();
load_meeting_notes_example('notes');
create_scribe_parent_task();
if(window.settings.assistant == 'scribe' && window.microphone_enabled == false){
window.enable_microphone();
}
});
setting_wrapper_el.appendChild(load_button_el);
setting_wrapper_el.classList.add('text-align-right');
}
else if(special_type == 'Switch_AI_button'){
//console.log("add_chat_message: adding model switch hint message");
if(window.busy_loading_assistant == true){
console.warn("add_chat_message: a model is already in the process of being loaded, aborting showing switch hint message");
return
}
//let progress_container_el = document.createElement('div');
//progress_container_el.setAttribute('id','download-progress-' + window.settings.assistant + '-container');
chat_message_el.classList.add('switch-hint-chat-message');
if(window.currently_loaded_assistant == null){
// in this case the model should automatically start loading
//add_chat_message(current,'developer',get_translation('Nice_choice'), 'Nice_choice');
console.warn("add_chat_message: currently_loaded_model was still null. aborting."); // the first model should in theory be auto-loaded
return
}
else{
// TODO: check if model is already cached, in which case show "load model" instead of "download model".
// TODO: show the file size of the model to be downloaded".
//message.innerHTML = '<p>' + get_translation('Would_you_like_to_download_this_AI') + '</p>';
}
let load_button_el = document.createElement('button');
load_button_el.textContent = get_translation('Switch_to_this_AI');
load_button_el.setAttribute('data-i18n','Switch_to_this_AI');
load_button_el.addEventListener("click", () => {
//console.log("clicked on switch to different AI button");
//window.stop_assistant();
chat_message_el.remove();
prompt_el.focus();
//clear_cache();
});
setting_wrapper_el.appendChild(load_button_el);
}
else if(special_type == 'Try_opening_the_camera'){
//console.log("add_chat_message: adding open the camera hint message");
if(window.continuous_ocr_enabled == true){
console.warn("add_chat_message: OCR is already acive");
return
}
message.innerHTML = '<div class="model-example-title" data-i18n="model_examples">' + get_translation('Try_opening_the_camera') + '</div><div><img src="./images/camera_translation.png" alt="camera-to-text-to-translation-to-voice example"/></div>';
//let progress_container_el = document.createElement('div');
//progress_container_el.setAttribute('id','download-progress-' + window.settings.assistant + '-container');
chat_message_el.classList.add('open-camera-hint-chat-message');
let load_ocr_button_el = document.createElement('button');
load_ocr_button_el.textContent = get_translation('Open_camera');
load_ocr_button_el.setAttribute('data-i18n','Open_camera');
load_ocr_button_el.addEventListener("click", () => {
//console.log("add_chat_message: clicked on Try_opening_the_camera button");
if(window.microphone_enabled){
window.disable_microphone();
}
if(window.is_mobile && window.ram < 4000){
window.start_ocr();
}
else{
load_live_camera_translation_example();
}
});
setting_wrapper_el.appendChild(load_ocr_button_el);
setting_wrapper_el.classList.add('text-align-right');
}
else if(special_type == 'model_examples'){
//console.log("add_chat_message: adding model examples chat message. pane: ", pane);
if(typeof window.intro_explanations_given[pane] == 'undefined'){
window.intro_explanations_given[pane] = [];
}
//console.log("add_chat_message: pane,participant,window.intro_explanations_given[pane],original_message: ", pane, participant, window.intro_explanations_given[pane], original_message);
if(window.intro_explanations_given[pane].indexOf(original_message) == -1){
window.intro_explanations_given[pane].push(original_message);
}
else{
console.warn("add_chat_message: aborting: the 'model_example' chat message was already show for this pane: ", pane, original_message);
return
}
//participant = 'developer'; // TODO: this hacky stuff might not be needed anymore now that pane can be set
message.innerHTML = '<div class="model-example-title" data-i18n="model_examples">' + get_translation('model_examples') + '</div>';
let assistant_to_load_examples_for = null;
if(original_participant != 'user'){
assistant_to_load_examples_for = pane;
}
//console.log('add_chat_message: original_participant, assistant_to_load_examples_for: ', original_participant, assistant_to_load_examples_for);
if(window.currently_loaded_assistant == null){
//console.warn("add_chat_message: adding model examples: notice: window.currently_loaded_assistant is null");
}
let examples = [];
if(typeof assistant_to_load_examples_for != 'string'){
console.error("add_chat_message: adding model examples: assistant_to_load_examples_for was not a string. Aborting.");
}
else if(
(typeof window.assistants[assistant_to_load_examples_for] != 'undefined' && typeof window.assistants[assistant_to_load_examples_for]['examples'] != 'undefined')
|| (typeof window.settings.assistants[assistant_to_load_examples_for] != 'undefined' && typeof window.settings.assistants[assistant_to_load_examples_for]['examples'] != 'undefined')
){
//console.log("add_chat_message: examples data: ", window.assistants[assistant_to_load_examples_for]['examples']);
message.innerHTML = '<div class="model-example-title" data-i18n="model_examples">' + get_translation('model_examples') + '</div>';
if(typeof window.settings.assistants[assistant_to_load_examples_for] != 'undefined' && typeof window.settings.assistants[assistant_to_load_examples_for]['examples'] != 'undefined'){
if(typeof window.settings.assistants[assistant_to_load_examples_for]['examples']['all'] != 'undefined'){
examples = window.settings.assistants[assistant_to_load_examples_for]['examples']['all'];
}
else if(typeof window.settings.assistants[assistant_to_load_examples_for]['examples'][window.settings.language] != 'undefined'){
examples = window.settings.assistants[assistant_to_load_examples_for]['examples'][window.settings.language];
}
else if(typeof window.settings.assistants[assistant_to_load_examples_for]['examples']['en'] != 'undefined'){
examples = window.settings.assistants[assistant_to_load_examples_for]['examples']['en'];
}
}
if(examples.length == 0 && typeof window.assistants[assistant_to_load_examples_for]['examples'] != 'undefined'){
if(typeof window.assistants[assistant_to_load_examples_for]['examples']['all'] != 'undefined'){
examples = window.assistants[assistant_to_load_examples_for]['examples']['all'];
}
else if(typeof window.assistants[assistant_to_load_examples_for]['examples'][window.settings.language] != 'undefined'){
examples = window.assistants[assistant_to_load_examples_for]['examples'][window.settings.language];
}
else if(typeof window.assistants[assistant_to_load_examples_for]['examples']['en'] != 'undefined'){
examples = window.assistants[assistant_to_load_examples_for]['examples']['en'];
}
}
if(examples.length == 0){
console.warn("add_chat_message: the examples list for this model in the current language existed, but was likely empty: ", assistant_to_load_examples_for, window.settings.language);
//return
}
for(let q=0; q<examples.length; q++){
const example = examples[q];
//console.log("add_chat_message: looping over example: ", examples[q]);
if(typeof example.title == 'string' && typeof example.prompt == 'string'){
let button_el = document.createElement('button');
let button_text_el = document.createElement('span');
button_text_el.innerText = example.title;
button_el.appendChild(button_text_el)
//console.log("example.title.length: ", example.title.length, example.title);
if(example.title.length < 3){
button_el.classList.add('example-emoji-title');
}
button_el.classList.add('chat-example-button');
button_el.addEventListener("click", () => {
//console.log("clicked on example button. Example: ", example);
handle_example(example);
});
setting_wrapper_el.appendChild(button_el);
}
else{
console.error("add_chat_message: example was incomplete: ", examples[q]);
}
}
let ai_type = 'generic';
if(typeof window.assistants[assistant_to_load_examples_for] != 'undefined' && typeof window.assistants[assistant_to_load_examples_for].type == 'string'){
ai_type = window.assistants[assistant_to_load_examples_for].type;
}
if( typeof window.assistants[assistant_to_load_examples_for] != 'undefined' && typeof window.assistants[assistant_to_load_examples_for]['languages'] == 'undefined' || (typeof window.assistants[assistant_to_load_examples_for]['languages'] != 'undefined' && window.assistants[assistant_to_load_examples_for]['languages'].indexOf('en') != -1)){
//console.log("adding extra default examples. ai_type: ", ai_type);
for(let qq=0; qq < window.examples.length; qq++){
if(window.examples[qq].types.indexOf(ai_type) != -1){
const example = window.examples[qq];
if(typeof example.title != 'undefined' && typeof example.prompt != 'undefined'){
let button_el = document.createElement('button');
let button_text_el = document.createElement('span');
button_text_el.innerText = get_translation(example.title);
button_el.appendChild(button_text_el)
//console.log("generic example.title.length: ", example.title.length, example.title);
if(example.title.length < 5){
button_el.classList.add('example-emoji-title');
}
button_el.classList.add('generic-chat-example-button');
button_el.addEventListener("click", () => {
//console.log("clicked on universal prompt example button. Example: ", example);
handle_example(example);
});
setting_wrapper_el.appendChild(button_el);
}
}
}
}
// What's the difference between green and red apples?