Merge #4016 (re-arm browser voice mode when speechSynthesis drops, #3983) onto master

This commit is contained in:
nesquena-hermes
2026-06-13 07:37:31 +00:00
2 changed files with 140 additions and 2 deletions

View File

@@ -853,8 +853,48 @@ window._micPendingSend=window._micPendingSend||false;
// a different session's last assistant reply if the user navigated away
// between send and stream completion. (Opus pre-release advisor.)
let _voiceModeThinkingSid=null;
let _browserTtsKeepAlive=null;
let _browserTtsWatchdog=null;
let _browserTtsSuppressNextErrorRearm=false;
const SILENCE_MS=1800; // auto-send after 1.8s silence
function _clearBrowserTtsRecovery(){
if(_browserTtsKeepAlive){
clearInterval(_browserTtsKeepAlive);
_browserTtsKeepAlive=null;
}
if(_browserTtsWatchdog){
clearTimeout(_browserTtsWatchdog);
_browserTtsWatchdog=null;
}
}
function _armBrowserTtsRecovery(clean, rate){
_clearBrowserTtsRecovery();
_browserTtsSuppressNextErrorRearm=false;
const safeRate=(Number.isFinite(rate)&&rate>0)?rate:1;
// Chromium can drop utter.onend on later turns, so force a recovery path.
const watchdogMs=Math.max(4000,Math.round((String(clean||'').length/(12*safeRate))*1000)+10000);
_browserTtsWatchdog=setTimeout(()=>{
if(!_voiceModeActive||_voiceModeState!=='speaking') return;
_browserTtsSuppressNextErrorRearm=true;
try{ speechSynthesis.cancel(); }catch(_){}
_clearBrowserTtsRecovery();
_startListening();
},watchdogMs);
_browserTtsKeepAlive=setInterval(()=>{
if(!_voiceModeActive||_voiceModeState!=='speaking'){
_clearBrowserTtsRecovery();
return;
}
if(!speechSynthesis.speaking) return;
try{
speechSynthesis.pause();
speechSynthesis.resume();
}catch(_){}
},10000);
}
function _setState(state){
_voiceModeState=state;
indicator.className='voice-mode-indicator '+state;
@@ -867,6 +907,7 @@ window._micPendingSend=window._micPendingSend||false;
function _startListening(){
if(!_voiceModeActive) return;
_clearBrowserTtsRecovery();
_setState('listening');
_recognition=new SpeechRecognition();
@@ -1057,14 +1098,27 @@ window._micPendingSend=window._micPendingSend||false;
if(!isNaN(savedPitch)) utter.pitch=Math.min(2,Math.max(0,savedPitch));
utter.onend=()=>{
_browserTtsSuppressNextErrorRearm=false;
_clearBrowserTtsRecovery();
// After speaking, go back to listening
if(_voiceModeActive) setTimeout(()=>_startListening(),500);
if(_voiceModeActive&&_voiceModeState==='speaking') setTimeout(()=>_startListening(),500);
};
utter.onerror=()=>{
_clearBrowserTtsRecovery();
if(_browserTtsSuppressNextErrorRearm){
_browserTtsSuppressNextErrorRearm=false;
return;
}
if(_voiceModeActive) setTimeout(()=>_startListening(),1000);
};
speechSynthesis.speak(utter);
_armBrowserTtsRecovery(clean, utter.rate);
try{
speechSynthesis.speak(utter);
}catch(_){
_clearBrowserTtsRecovery();
if(_voiceModeActive) setTimeout(()=>_startListening(),1000);
}
}
// Hook into response completion — observe when the agent finishes
@@ -1121,10 +1175,12 @@ window._micPendingSend=window._micPendingSend||false;
_voiceModeActive=false;
_voiceModeState='idle';
_voiceModeThinkingSid=null;
_browserTtsSuppressNextErrorRearm=false;
modeBtn.classList.remove('active');
_setButtonTooltip(modeBtn, t('voice_mode_toggle'));
bar.style.display='none';
clearTimeout(_silenceTimer);
_clearBrowserTtsRecovery();
try{ if(_recognition) _recognition.abort(); }catch(_){}
_recognition=null;
if(typeof stopTTS==='function') stopTTS();