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