🎤 feat: Sistema de conversa por voz - auto-TTS quando mensagem vem do microfone, envio automático, melhor visibilidade do ícone
This commit is contained in:
+24
-3
@@ -288,6 +288,7 @@ const initApp = () => {
|
||||
// Variáveis de Reconhecimento de Voz
|
||||
let recognition = null;
|
||||
let isListening = false;
|
||||
let lastInputWasVoice = false; // Rastrear se a última mensagem veio do microfone
|
||||
|
||||
// ==========================================================================
|
||||
// THEME TOGGLE — Claro/Escuro
|
||||
@@ -488,13 +489,14 @@ const initApp = () => {
|
||||
});
|
||||
|
||||
// ==========================================================================
|
||||
// VOZ — WEB SPEECH API (ditado)
|
||||
// VOZ — WEB SPEECH API (ditado + conversa por voz)
|
||||
// ==========================================================================
|
||||
|
||||
const initVoiceRecognition = () => {
|
||||
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
||||
if (!SpeechRecognition) {
|
||||
btnVoice.style.display = 'none';
|
||||
console.warn('SpeechRecognition não suportado neste navegador.');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -509,6 +511,16 @@ const initApp = () => {
|
||||
userInput.value = transcript;
|
||||
userInput.dispatchEvent(new Event('input'));
|
||||
setVoiceState(false);
|
||||
|
||||
// Marca que o input veio do microfone para auto-responder por voz
|
||||
lastInputWasVoice = true;
|
||||
|
||||
// Envia automaticamente a mensagem ditada
|
||||
if (transcript.trim()) {
|
||||
setTimeout(() => {
|
||||
chatForm.dispatchEvent(new Event('submit', { cancelable: true }));
|
||||
}, 300);
|
||||
}
|
||||
};
|
||||
|
||||
recognition.onerror = (event) => {
|
||||
@@ -525,10 +537,10 @@ const initApp = () => {
|
||||
isListening = listening;
|
||||
if (listening) {
|
||||
btnVoice.classList.add('listening');
|
||||
btnVoice.setAttribute('title', 'Clique para parar');
|
||||
btnVoice.setAttribute('title', 'Clique para parar de ouvir');
|
||||
} else {
|
||||
btnVoice.classList.remove('listening');
|
||||
btnVoice.setAttribute('title', 'Ditar mensagem');
|
||||
btnVoice.setAttribute('title', 'Conversar por voz');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1685,6 +1697,15 @@ const initApp = () => {
|
||||
|
||||
scrollToBottom();
|
||||
|
||||
// AUTO-TTS: Se a mensagem veio do microfone, ler a resposta em voz alta automaticamente
|
||||
if (lastInputWasVoice && assistantContent && !isMediaResponse) {
|
||||
console.log('[VOZ] Mensagem veio do microfone — respondendo por voz automaticamente.');
|
||||
setTimeout(() => {
|
||||
speakText(assistantContent, speakBtn);
|
||||
}, 400);
|
||||
}
|
||||
lastInputWasVoice = false;
|
||||
|
||||
// Salvar resposta no estado e localStorage
|
||||
const assistantMsg = { role: 'assistant', content: assistantContent };
|
||||
chat.messages.push(assistantMsg);
|
||||
|
||||
Reference in New Issue
Block a user