mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
TTS 会话等 SessionStarted 确认
旧逻辑 start_session 发完 StartSession 即返回;服务端鉴权失败或 14s 内 26005 时 文本线程继续给死会话发文本,整轮无声。改成末尾 await 一个 future:monitor 读到 SessionStarted set True、读到 SessionFailed set False,超时报错让文本线程感知。
This commit is contained in:
parent
eb9dc8ca0f
commit
6fe5630346
1 changed files with 26 additions and 0 deletions
|
|
@ -144,6 +144,7 @@ class TTSProvider(TTSProviderBase):
|
|||
self.ws = None
|
||||
self.interface_type = InterfaceType.DUAL_STREAM
|
||||
self._monitor_task = None # 监听任务引用
|
||||
self._session_start_future: asyncio.Future | None = None
|
||||
self.appId = config.get("appid")
|
||||
self.access_token = config.get("access_token")
|
||||
self.cluster = config.get("cluster")
|
||||
|
|
@ -423,6 +424,21 @@ class TTSProvider(TTSProviderBase):
|
|||
)
|
||||
await self.send_event(self.ws, header, optional, payload)
|
||||
logger.bind(tag=TAG).debug("会话启动请求已发送")
|
||||
|
||||
# 等 monitor 读到 SessionStarted / SessionFailed 帧,失败 / 超时让文本线程感知
|
||||
loop = asyncio.get_event_loop()
|
||||
self._session_start_future = loop.create_future()
|
||||
try:
|
||||
ok = await asyncio.wait_for(
|
||||
self._session_start_future, timeout=self.tts_timeout
|
||||
)
|
||||
finally:
|
||||
self._session_start_future = None
|
||||
if not ok:
|
||||
await self.close()
|
||||
raise RuntimeError(
|
||||
f"TTS 会话 {session_id} 被服务端拒绝 (SessionFailed)"
|
||||
)
|
||||
except Exception as e:
|
||||
logger.bind(tag=TAG).error(f"启动会话失败: {str(e)}")
|
||||
# 确保清理资源
|
||||
|
|
@ -500,6 +516,16 @@ class TTSProvider(TTSProviderBase):
|
|||
logger.bind(tag=TAG).debug(f"链接关闭成功~~")
|
||||
break
|
||||
|
||||
# 把 SessionStarted / SessionFailed 解析结果回传给 start_session
|
||||
if (
|
||||
res.optional.event in (EVENT_SessionStarted, EVENT_SessionFailed)
|
||||
and res.optional.sessionId == self.conn.sentence_id
|
||||
):
|
||||
if self._session_start_future and not self._session_start_future.done():
|
||||
self._session_start_future.set_result(
|
||||
res.optional.event == EVENT_SessionStarted
|
||||
)
|
||||
|
||||
# 只处理当前活跃会话的响应
|
||||
if res.optional.sessionId and self.conn.sentence_id != res.optional.sessionId:
|
||||
# 如果是会话结束相关事件,即使会话ID不匹配也要重置状态
|
||||
|
|
|
|||
Loading…
Reference in a new issue