diff --git a/package-lock.json b/package-lock.json index 6773216..775ab4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "maestri-remote-agent", - "version": "1.0.26", + "version": "1.0.27", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "maestri-remote-agent", - "version": "1.0.26", + "version": "1.0.27", "dependencies": { "@capacitor/push-notifications": "^6.0.2", "pg": "^8.23.1", diff --git a/public/app.js b/public/app.js index 952d6e4..32f8f93 100644 --- a/public/app.js +++ b/public/app.js @@ -4619,20 +4619,31 @@ function setupChatEvents() { let voiceAccumulatedTranscript = ''; let voiceSessionFinal = ''; let voiceRestartCount = 0; + let voiceRestartTimeout = null; const stopRecordingUI = () => { isRecording = false; isVoiceExplicitlyStopped = true; + if (voiceRestartTimeout) { + clearTimeout(voiceRestartTimeout); + voiceRestartTimeout = null; + } voiceRestartCount = 0; btnVoice.classList.remove('recording'); + btnVoice.removeAttribute('aria-pressed'); if (chatInput) { chatInput.placeholder = 'Digite ou fale para o agente...'; chatInput.value = chatInput.value.trim(); + updateInputActionButtonState(); } }; const stopAnyActiveVoiceSession = () => { isVoiceExplicitlyStopped = true; + if (voiceRestartTimeout) { + clearTimeout(voiceRestartTimeout); + voiceRestartTimeout = null; + } if (speechRecognizer) { try { speechRecognizer.stop(); } catch (e) {} } @@ -4646,6 +4657,57 @@ function setupChatEvents() { stopRecordingUI(); }; + const restartRecognitionSafely = (delay = 0) => { + if (isVoiceExplicitlyStopped || !isRecording || !speechRecognizer) { + return; + } + if (voiceRestartTimeout) { + clearTimeout(voiceRestartTimeout); + voiceRestartTimeout = null; + } + + const attemptStart = () => { + if (isVoiceExplicitlyStopped || !isRecording || !speechRecognizer) { + return; + } + try { + speechRecognizer.start(); + } catch (e) { + // Erro comum ao tentar reiniciar enquanto o motor nativo ainda está liberando o hardware + // ou durante foco temporário de notificação (DOMException, InvalidStateError, etc.) + console.warn('[Voice] Tentativa de reiniciar reconhecimento contínuo falhou, agendando retry:', e); + if (!isVoiceExplicitlyStopped && isRecording) { + voiceRestartTimeout = setTimeout(() => { + restartRecognitionSafely(300); + }, 300); + } + } + }; + + if (delay > 0) { + voiceRestartTimeout = setTimeout(attemptStart, delay); + } else { + attemptStart(); + } + }; + + // Mantém a gravação viva caso o app receba notificações ou perca foco temporariamente + const handleVoiceFocusResume = () => { + if (!isVoiceExplicitlyStopped && isRecording && speechRecognizer) { + restartRecognitionSafely(100); + } + }; + if (typeof window !== 'undefined') { + window.addEventListener('focus', handleVoiceFocusResume); + } + if (typeof document !== 'undefined') { + document.addEventListener('visibilitychange', () => { + if (document.visibilityState === 'visible') { + handleVoiceFocusResume(); + } + }); + } + const startAudioRecording = () => { if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { showToast('Gravação de microfone não suportada neste aparelho.', 'error'); @@ -4659,7 +4721,9 @@ function setupChatEvents() { navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => { voiceActiveStream = stream; isRecording = true; + isVoiceExplicitlyStopped = false; btnVoice.classList.add('recording'); + btnVoice.setAttribute('aria-pressed', 'true'); if (chatInput) chatInput.placeholder = 'Gravando áudio... Toque no mic para finalizar'; showToast('Gravando áudio... Fale e toque no microfone para enviar.', 'info'); @@ -4752,6 +4816,7 @@ function setupChatEvents() { isRecording = true; isVoiceExplicitlyStopped = false; btnVoice.classList.add('recording'); + btnVoice.setAttribute('aria-pressed', 'true'); if (chatInput) chatInput.placeholder = 'Ouvindo... Fale à vontade (toque no mic para encerrar)'; showToast('Ditado contínuo ativado. Toque no microfone para concluir.', 'info'); }; @@ -4775,41 +4840,35 @@ function setupChatEvents() { speechRecognizer.onerror = (err) => { console.warn('[Voice] SpeechRecognition erro:', err.error); - if (err.error === 'no-speech') { - // Silêncio temporário (pausa para respirar ou pensar): ignora e deixa o ciclo continuar + if (isVoiceExplicitlyStopped) return; + + // Erros não fatais causados por pausas de respiração, interrupções transitórias de foco ou sons de notificações + if (err.error === 'no-speech' || err.error === 'aborted' || err.error === 'audio-capture') { + if (err.error === 'audio-capture') { + restartRecognitionSafely(400); + } return; } - if (err.error === 'aborted') { - return; - } - stopRecordingUI(); - - // Feedback claro e contextual sem anular a instância do recognizer e sem cair silenciosamente para gravação física + + // Se for erro crítico (falha de rede do Google ou permissão negada), notifica e interrompe if (err.error === 'network') { showToast('Falha de conexão com o serviço de voz. Verifique sua internet e tente novamente.', 'warning'); } else if (err.error === 'service-not-allowed' || err.error === 'not-allowed') { showToast('Permissão de microfone/fala não concedida no dispositivo ou navegador.', 'error'); - } else if (err.error === 'audio-capture') { - showToast('Nenhum microfone detectado ou microfone ocupado por outro app.', 'warning'); } else { showToast(`Reconhecimento de fala: ${err.error}`, 'warning'); } + stopRecordingUI(); }; speechRecognizer.onend = () => { if (!isVoiceExplicitlyStopped && isRecording && speechRecognizer) { - // Consolida a transcrição final da sessão e reinicia para tolerar pausas de respiração + // Consolida a transcrição final da sessão e reinicia sem desligar a gravação voiceAccumulatedTranscript += voiceSessionFinal; voiceSessionFinal = ''; voiceRestartCount++; - if (voiceRestartCount < 50) { - try { - speechRecognizer.start(); - return; - } catch (e) { - console.warn('[Voice] Falha ao auto-reiniciar reconhecimento contínuo:', e); - } - } + restartRecognitionSafely(0); + return; } stopRecordingUI(); }; @@ -4835,7 +4894,7 @@ function setupChatEvents() { const recognizer = getOrCreateSpeechRecognizer(); if (!recognizer) { - showToast('Reconhecimento de fala não suportado neste navegador.', 'error'); + startAudioRecording(); return; } diff --git a/public/bundles/bundle-1.0.27.zip b/public/bundles/bundle-1.0.27.zip index a4633da..1118821 100644 Binary files a/public/bundles/bundle-1.0.27.zip and b/public/bundles/bundle-1.0.27.zip differ diff --git a/public/bundles/bundle-latest.zip b/public/bundles/bundle-latest.zip index a4633da..1118821 100644 Binary files a/public/bundles/bundle-latest.zip and b/public/bundles/bundle-latest.zip differ diff --git a/public/bundles/manifest.json b/public/bundles/manifest.json index e7fb42d..98585fe 100644 --- a/public/bundles/manifest.json +++ b/public/bundles/manifest.json @@ -1,9 +1,9 @@ { "version": "1.0.27", - "buildId": "49785205", - "releasedAt": "2026-11-21T19:43:00.809Z", + "buildId": "5181769b", + "releasedAt": "2026-11-21T20:13:48.591Z", "bundleUrl": "/bundles/bundle-1.0.27.zip", "latestUrl": "/bundles/bundle-latest.zip", - "checksum": "1be6eda404c2600d5156e9a66f9eaae737e9f9a3909fe07f1ccc3384c2e04c7d", - "sizeBytes": 69527 + "checksum": "00b871aa4e720f78156263527c325a6fb941e9e145ecfbbd3edb852595441690", + "sizeBytes": 70574 } \ No newline at end of file diff --git a/public/index.html b/public/index.html index 7a3b780..0e84f08 100644 --- a/public/index.html +++ b/public/index.html @@ -291,7 +291,7 @@