CartesiaTTSService: reconnect on Cartesia's timeout

This commit is contained in:
Aleix Conchillo Flaqué
2025-08-25 14:09:03 -07:00
parent ab61d09ec1
commit 39ebc2c9c1
2 changed files with 16 additions and 1 deletions

View File

@@ -386,7 +386,7 @@ class CartesiaTTSService(AudioContextWordTTSService):
await self._websocket.send(msg)
self._context_id = None
async def _receive_messages(self):
async def _process_messages(self):
async for message in self._get_websocket():
msg = json.loads(message)
if not msg or not self.audio_context_available(msg["context_id"]):
@@ -419,6 +419,14 @@ class CartesiaTTSService(AudioContextWordTTSService):
else:
logger.error(f"{self} error, unknown message type: {msg}")
async def _receive_messages(self):
while True:
await self._process_messages()
# Cartesia times out after 5 minutes of innactivity (no keepalive
# mechanism is available). So, we try to reconnect.
logger.debug(f"{self} Cartesia connection was disconnected (timeout?), reconnecting")
await self._connect_websocket()
@traced_tts
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
"""Generate speech from text using Cartesia's streaming API.