diff --git a/frontend/scripts/main.js b/frontend/scripts/main.js index ada4933..01a9241 100644 --- a/frontend/scripts/main.js +++ b/frontend/scripts/main.js @@ -43,7 +43,8 @@ const state = { isSpeaking: false, isMuted: false, jobInfo: null, - currentInteractionMode: InteractionMode.AUDIO + currentInteractionMode: InteractionMode.AUDIO, + keepAliveActive: false }; const elements = { @@ -256,14 +257,54 @@ const eventListeners = { }, [ICSEventKey.JOB_END]: (data) => { - logger.info('任务已结束', data); + logger.info('任务已结束,正在自动重新激活...', data); state.isChatting = false; state.jobInfo = null; - updateConnectionStatus(false); + if (state.keepAliveActive) { + setTimeout(async () => { + try { + logger.info('正在调用 activeInteractionMode...'); + await HwICSUiSdk.activeInteractionMode(); + logger.info('自动激活成功'); + state.isChatting = true; + updateUIState(); + } catch (error) { + logger.error('自动激活失败', error); + } + }, 1000); + } updateUIState(); } }; +let keepAliveTimer = null; +const KEEP_ALIVE_INTERVAL = 30000; + +function startKeepAlive() { + if (keepAliveTimer) return; + state.keepAliveActive = true; + logger.info('启动对话保活机制'); + keepAliveTimer = setInterval(async () => { + if (state.isInitialized && state.keepAliveActive) { + try { + await HwICSUiSdk.activeInteractionMode(); + logger.debug('保活 ping'); + } catch (error) { + logger.warn('保活失败', error); + } + } + }, KEEP_ALIVE_INTERVAL); +} + +function stopKeepAlive() { + if (keepAliveTimer) { + clearInterval(keepAliveTimer); + keepAliveTimer = null; + } + state.keepAliveActive = false; + logger.info('停止对话保活机制'); +} + function updateUIState() { const { isInitialized } = state; @@ -442,6 +483,7 @@ async function initializeSDK() { fullscreenState.sdkInitialized = true; updateConnectionStatus(true); updateUIState(); + startKeepAlive(); elements.sdkVersion.textContent = `v${HwICSUiSdk.version}`; @@ -468,6 +510,7 @@ async function destroySDK() { state.isSpeaking = false; state.jobInfo = null; fullscreenState.sdkInitialized = false; + stopKeepAlive(); updateConnectionStatus(false); updateUIState(); @@ -1284,6 +1327,7 @@ function bindEvents() { window.addEventListener('beforeunload', () => { if (state.isInitialized) { + stopKeepAlive(); HwICSUiSdk.destroy(); } });