4
|
1 'use strict';
|
|
2
|
29
|
3 let chat;
|
|
4
|
|
5 function setChat(newChat) {
|
46
|
6 let audioChanged = chat && chat.voice != newChat.voice;
|
29
|
7 chat = newChat;
|
|
8 document.querySelector('[content] [name]').textContent = chat.name;
|
31
|
9 if(audioChanged) {
|
32
|
10 let voice = `voice=${chat.voice}&`;
|
31
|
11 let audios = document.querySelectorAll('audio[src]');
|
|
12 for( let audio of audios ) {
|
32
|
13 let src = audio.src;
|
|
14 src = src.replace(/lang=[^&]+&/,lang);
|
|
15 src = src.replace(/voice=[^&]+&/,voice);
|
|
16 audio.src = src;
|
31
|
17 }
|
|
18 }
|
29
|
19 }
|
|
20
|
|
21 function editChat(name) {
|
|
22 let dialog = document.querySelector('dialog[edit]');
|
|
23 dialog.querySelector('input[name=name]').value = chat.name;
|
31
|
24 dialog.querySelector('select[name=voice]').value = chat.voice;
|
52
|
25 if( chat.show_text )
|
|
26 dialog.querySelector('select[name=show_text]').value = chat.show_text;
|
36
|
27 dialog.querySelector('input[name=autoplay]').checked = chat.autoplay;
|
41
|
28 dialog.querySelector('input[name=is_private]').checked = chat.is_private;
|
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() {
|
46
|
69 handleMarkdown(chat.voice,chat.tts_instructions);
|
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
|
42
|
97 function fixChatTextarea(textarea) {
|
|
98 fixTextarea(textarea);
|
14
|
99 textarea.parentNode.scrollIntoViewIfNeeded(false);
|
28
|
100 if( !audio )
|
|
101 audio = document.querySelector('div[buttons] audio');
|
46
|
102 audio.src = `/tts.mp3?voice=${chat.voice}&instructions=${encodeURIComponent(chat.tts_instructions)}&text=${encodeURIComponent(textarea.value)}`;
|
4
|
103 }
|
|
104
|
|
105 function askAi() {
|
|
106 let input = document.querySelector('textarea');
|
29
|
107 let url = `ai_ask.js?chat=${chat.id}&input=${encodeURIComponent(input.value)}`;
|
4
|
108 ajax(url);
|
|
109 input.value = '';
|
42
|
110 fixChatTextarea(input);
|
4
|
111 showWaitingAiIcon();
|
|
112 }
|
14
|
113
|
|
114
|
|
115 function setText(text) {
|
|
116 let textarea = document.querySelector('textarea');
|
|
117 textarea.value = text;
|
42
|
118 fixChatTextarea(textarea);
|
14
|
119 }
|
|
120
|
|
121 let recorder = null;
|
|
122 let chunks;
|
|
123
|
|
124 function startRecording() {
|
|
125 chunks = [];
|
|
126 function record(stream) {
|
|
127 recorder = new MediaRecorder( stream, { mimeType: 'audio/webm;codecs=opus' } );
|
|
128 recorder.ondataavailable = function(event) {
|
|
129 chunks.push(event.data);
|
|
130 };
|
|
131 recorder.onstop = function(event) {
|
|
132 recorder = null;
|
|
133 let blob = new Blob(chunks, { type: 'audio/webm' });
|
|
134 let formData = new FormData();
|
|
135 formData.append('audio', blob, 'recording.webm');
|
69
|
136 formData.append('prompt', chat.stt_prompt);
|
14
|
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 }
|