diff --git a/app/main.py b/app/main.py index 0f4b8d4..9cb8430 100644 --- a/app/main.py +++ b/app/main.py @@ -13,6 +13,7 @@ Single-file FastAPI app: from __future__ import annotations +import asyncio import json import logging import os @@ -811,7 +812,17 @@ async def chat_ws(ws: WebSocket) -> None: if response_text: history.append({"role": "assistant", "content": response_text}) - await _send_audio_with_voice(ws, response_text, voice) + # TTS runs after text is fully streamed — don't let it block + # the WebSocket. If it takes too long, user still has the text. + try: + await asyncio.wait_for( + _send_audio_with_voice(ws, response_text, voice), + timeout=20.0, + ) + except asyncio.TimeoutError: + log.warning("TTS timed out for voice=%s, skipping audio", voice) + except Exception: + log.exception("TTS failed, continuing without audio") except WebSocketDisconnect: log.info("%s disconnected", user["email"])