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
|
5
|
51 function updateAi(html) {
|
|
52 hideWaitingAiIcon();
|
13
|
53 document.querySelector('div[messages]').insertAdjacentHTML('beforeend',html);
|
6
|
54 handleMarkdown();
|
5
|
55 document.querySelector('textarea').focus();
|
6
|
56 window.scrollTo(0, document.body.scrollHeight);
|
5
|
57 /*
|
|
58 let scroll = aiDialog.querySelector('[scroll]');
|
|
59 setTimeout(function(){
|
|
60 scroll.scrollTo(0,scroll.scrollHeight);
|
|
61 });
|
|
62 */
|
25
|
63 playLastMessage();
|
5
|
64 }
|
|
65
|
4
|
66 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
|
|
67
|
|
68 function textareaKey(event) {
|
|
69 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
|
|
70 event.preventDefault();
|
|
71 askAi();
|
|
72 }
|
|
73 }
|
|
74
|
11
|
75 function fixTextarea(textarea) {
|
4
|
76 textarea.style.height = 'initial';
|
|
77 textarea.style.height = (textarea.scrollHeight+2) + 'px';
|
14
|
78 textarea.parentNode.scrollIntoViewIfNeeded(false);
|
4
|
79 }
|
|
80
|
|
81 function askAi() {
|
|
82 let input = document.querySelector('textarea');
|
5
|
83 let url = `ai_ask.js?chat=${chatId}&input=${encodeURIComponent(input.value)}`;
|
4
|
84 ajax(url);
|
|
85 input.value = '';
|
11
|
86 fixTextarea(input);
|
4
|
87 showWaitingAiIcon();
|
|
88 }
|
14
|
89
|
|
90
|
|
91 function setText(text) {
|
|
92 let textarea = document.querySelector('textarea');
|
|
93 textarea.value = text;
|
|
94 fixTextarea(textarea);
|
|
95 }
|
|
96
|
|
97 let recorder = null;
|
|
98 let chunks;
|
|
99
|
|
100 function startRecording() {
|
|
101 chunks = [];
|
|
102 function record(stream) {
|
|
103 recorder = new MediaRecorder( stream, { mimeType: 'audio/webm;codecs=opus' } );
|
|
104 recorder.ondataavailable = function(event) {
|
|
105 chunks.push(event.data);
|
|
106 };
|
|
107 recorder.onstop = function(event) {
|
|
108 recorder = null;
|
|
109 let blob = new Blob(chunks, { type: 'audio/webm' });
|
|
110 let formData = new FormData();
|
|
111 formData.append('audio', blob, 'recording.webm');
|
|
112 ajax('stt.js',formData);
|
|
113 document.querySelector('button[record]').textContent = 'Record';
|
|
114 };
|
|
115 recorder.start();
|
|
116 }
|
|
117 navigator.mediaDevices.getUserMedia({ audio: { channelCount: 1 } }).then(record);
|
|
118 document.querySelector('button[record]').textContent = 'Stop Recording';
|
|
119 }
|
|
120
|
|
121 function toggleRecording() {
|
|
122 if( recorder === null ) {
|
|
123 startRecording();
|
|
124 } else {
|
|
125 recorder.stop();
|
|
126 }
|
|
127 }
|