comparison src/chat.js @ 46:cc20eebaa74a

use openai tts
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Aug 2025 11:27:34 +0900
parents 1d9d9786d76f
children 97b0d206cacd
comparison
equal deleted inserted replaced
45:fef7a5c65cfb 46:cc20eebaa74a
1 'use strict'; 1 'use strict';
2 2
3 let chat; 3 let chat;
4 4
5 function setChat(newChat) { 5 function setChat(newChat) {
6 let audioChanged = chat && (chat.language_region != newChat.language_region || chat.voice != newChat.voice); 6 let audioChanged = chat && chat.voice != newChat.voice;
7 chat = newChat; 7 chat = newChat;
8 document.querySelector('[content] [name]').textContent = chat.name; 8 document.querySelector('[content] [name]').textContent = chat.name;
9 if(audioChanged) { 9 if(audioChanged) {
10 let lang = `lang=${chat.language_region}&`;
11 let voice = `voice=${chat.voice}&`; 10 let voice = `voice=${chat.voice}&`;
12 let audios = document.querySelectorAll('audio[src]'); 11 let audios = document.querySelectorAll('audio[src]');
13 for( let audio of audios ) { 12 for( let audio of audios ) {
14 let src = audio.src; 13 let src = audio.src;
15 src = src.replace(/lang=[^&]+&/,lang); 14 src = src.replace(/lang=[^&]+&/,lang);
20 } 19 }
21 20
22 function editChat(name) { 21 function editChat(name) {
23 let dialog = document.querySelector('dialog[edit]'); 22 let dialog = document.querySelector('dialog[edit]');
24 dialog.querySelector('input[name=name]').value = chat.name; 23 dialog.querySelector('input[name=name]').value = chat.name;
25 dialog.querySelector('select[name=language_region]').value = chat.language_region;
26 dialog.querySelector('select[name=voice]').value = chat.voice; 24 dialog.querySelector('select[name=voice]').value = chat.voice;
27 dialog.querySelector('input[name=show_text]').checked = chat.show_text; 25 dialog.querySelector('input[name=show_text]').checked = chat.show_text;
28 dialog.querySelector('input[name=autoplay]').checked = chat.autoplay; 26 dialog.querySelector('input[name=autoplay]').checked = chat.autoplay;
29 dialog.querySelector('input[name=is_private]').checked = chat.is_private; 27 dialog.querySelector('input[name=is_private]').checked = chat.is_private;
30 dialog.showModal(); 28 dialog.showModal();
65 audio.play(); 63 audio.play();
66 } 64 }
67 } 65 }
68 66
69 function handleChatMarkdown() { 67 function handleChatMarkdown() {
70 handleMarkdown(chat.language_region,chat.voice); 68 handleMarkdown(chat.voice,chat.tts_instructions);
71 } 69 }
72 70
73 function scrollToEnd() { 71 function scrollToEnd() {
74 window.scrollTo(0, document.body.scrollHeight); 72 window.scrollTo(0, document.body.scrollHeight);
75 } 73 }
98 function fixChatTextarea(textarea) { 96 function fixChatTextarea(textarea) {
99 fixTextarea(textarea); 97 fixTextarea(textarea);
100 textarea.parentNode.scrollIntoViewIfNeeded(false); 98 textarea.parentNode.scrollIntoViewIfNeeded(false);
101 if( !audio ) 99 if( !audio )
102 audio = document.querySelector('div[buttons] audio'); 100 audio = document.querySelector('div[buttons] audio');
103 audio.src = `/tts.mp3?lang=${chat.language_region}&voice=${chat.voice}&text=${encodeURIComponent(textarea.value)}`; 101 audio.src = `/tts.mp3?voice=${chat.voice}&instructions=${encodeURIComponent(chat.tts_instructions)}&text=${encodeURIComponent(textarea.value)}`;
104 } 102 }
105 103
106 function askAi() { 104 function askAi() {
107 let input = document.querySelector('textarea'); 105 let input = document.querySelector('textarea');
108 let url = `ai_ask.js?chat=${chat.id}&input=${encodeURIComponent(input.value)}`; 106 let url = `ai_ask.js?chat=${chat.id}&input=${encodeURIComponent(input.value)}`;