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