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