4
|
1 'use strict';
|
|
2
|
29
|
3 let chat;
|
|
4
|
|
5 function setChat(newChat) {
|
32
|
6 let audioChanged = chat && (chat.language_region != newChat.language_region || chat.voice != newChat.voice);
|
29
|
7 chat = newChat;
|
|
8 document.querySelector('[content] [name]').textContent = chat.name;
|
31
|
9 if(audioChanged) {
|
32
|
10 let lang = `lang=${chat.language_region}&`;
|
|
11 let voice = `voice=${chat.voice}&`;
|
31
|
12 let audios = document.querySelectorAll('audio[src]');
|
|
13 for( let audio of audios ) {
|
32
|
14 let src = audio.src;
|
|
15 src = src.replace(/lang=[^&]+&/,lang);
|
|
16 src = src.replace(/voice=[^&]+&/,voice);
|
|
17 audio.src = src;
|
31
|
18 }
|
|
19 }
|
29
|
20 }
|
|
21
|
|
22 function editChat(name) {
|
|
23 let dialog = document.querySelector('dialog[edit]');
|
|
24 dialog.querySelector('input[name=name]').value = chat.name;
|
32
|
25 dialog.querySelector('select[name=language_region]').value = chat.language_region;
|
31
|
26 dialog.querySelector('select[name=voice]').value = chat.voice;
|
34
|
27 dialog.querySelector('input[name=show_text]').checked = chat.show_text;
|
36
|
28 dialog.querySelector('input[name=autoplay]').checked = chat.autoplay;
|
4
|
29 dialog.showModal();
|
|
30 }
|
|
31
|
29
|
32 function saveChat(form) {
|
|
33 closeModal(form);
|
|
34 ajaxForm('save_chat.js',form);
|
4
|
35 }
|
|
36
|
|
37 function deleteChat() {
|
|
38 let dialog = document.querySelector('dialog[delete]');
|
|
39 dialog.showModal();
|
|
40 }
|
|
41
|
|
42 function doDeleteChat(el) {
|
|
43 closeModal(el);
|
29
|
44 ajax(`delete_chat.js?chat=${chat.id}`);
|
4
|
45 }
|
|
46
|
9
|
47 function systemPrompt() {
|
|
48 let dialog = document.querySelector('dialog[system_prompt]');
|
|
49 dialog.showModal();
|
|
50 }
|
|
51
|
4
|
52 function showWaitingAiIcon() {
|
|
53 document.querySelector('[waiting-ai-icon]').style.display = 'block';
|
|
54 }
|
|
55
|
5
|
56 function hideWaitingAiIcon() {
|
|
57 document.querySelector('[waiting-ai-icon]').style.display = 'none';
|
|
58 }
|
|
59
|
25
|
60 function playLastMessage() {
|
32
|
61 let audios = document.querySelectorAll('[messages] audio');
|
25
|
62 if( audios.length >= 1 ) {
|
|
63 let audio = audios[audios.length-1];
|
|
64 audio.play();
|
|
65 }
|
|
66 }
|
|
67
|
29
|
68 function handleChatMarkdown() {
|
31
|
69 handleMarkdown(chat.language_region,chat.voice);
|
29
|
70 }
|
|
71
|
27
|
72 function scrollToEnd() {
|
|
73 window.scrollTo(0, document.body.scrollHeight);
|
|
74 }
|
|
75
|
5
|
76 function updateAi(html) {
|
|
77 hideWaitingAiIcon();
|
13
|
78 document.querySelector('div[messages]').insertAdjacentHTML('beforeend',html);
|
29
|
79 handleChatMarkdown();
|
5
|
80 document.querySelector('textarea').focus();
|
27
|
81 scrollToEnd();
|
36
|
82 if( chat.autoplay )
|
|
83 playLastMessage();
|
5
|
84 }
|
|
85
|
4
|
86 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
|
|
87
|
|
88 function textareaKey(event) {
|
|
89 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
|
|
90 event.preventDefault();
|
|
91 askAi();
|
|
92 }
|
|
93 }
|
|
94
|
28
|
95 let audio;
|
|
96
|
11
|
97 function fixTextarea(textarea) {
|
4
|
98 textarea.style.height = 'initial';
|
|
99 textarea.style.height = (textarea.scrollHeight+2) + 'px';
|
14
|
100 textarea.parentNode.scrollIntoViewIfNeeded(false);
|
28
|
101 if( !audio )
|
|
102 audio = document.querySelector('div[buttons] audio');
|
31
|
103 audio.src = `/tts.mp3?lang=${chat.language_region}&voice=${chat.voice}&text=${encodeURIComponent(textarea.value)}`;
|
4
|
104 }
|
|
105
|
|
106 function askAi() {
|
|
107 let input = document.querySelector('textarea');
|
29
|
108 let url = `ai_ask.js?chat=${chat.id}&input=${encodeURIComponent(input.value)}`;
|
4
|
109 ajax(url);
|
|
110 input.value = '';
|
11
|
111 fixTextarea(input);
|
4
|
112 showWaitingAiIcon();
|
|
113 }
|
14
|
114
|
|
115
|
|
116 function setText(text) {
|
|
117 let textarea = document.querySelector('textarea');
|
|
118 textarea.value = text;
|
|
119 fixTextarea(textarea);
|
|
120 }
|
|
121
|
|
122 let recorder = null;
|
|
123 let chunks;
|
|
124
|
|
125 function startRecording() {
|
|
126 chunks = [];
|
|
127 function record(stream) {
|
|
128 recorder = new MediaRecorder( stream, { mimeType: 'audio/webm;codecs=opus' } );
|
|
129 recorder.ondataavailable = function(event) {
|
|
130 chunks.push(event.data);
|
|
131 };
|
|
132 recorder.onstop = function(event) {
|
|
133 recorder = null;
|
|
134 let blob = new Blob(chunks, { type: 'audio/webm' });
|
|
135 let formData = new FormData();
|
|
136 formData.append('audio', blob, 'recording.webm');
|
|
137 ajax('stt.js',formData);
|
|
138 document.querySelector('button[record]').textContent = 'Record';
|
|
139 };
|
|
140 recorder.start();
|
|
141 }
|
|
142 navigator.mediaDevices.getUserMedia({ audio: { channelCount: 1 } }).then(record);
|
|
143 document.querySelector('button[record]').textContent = 'Stop Recording';
|
|
144 }
|
|
145
|
|
146 function toggleRecording() {
|
|
147 if( recorder === null ) {
|
|
148 startRecording();
|
|
149 } else {
|
|
150 recorder.stop();
|
|
151 }
|
|
152 }
|