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