Skip to content

Build and run agents you can see, understand and trust.

fix(tts): keep CosyVoice cold-start buffer when the send fails (#3535)

Kiko0301922af0450389
diff --git a/src/agentscope/tts/_dashscope/_cosyvoice_model.py b/src/agentscope/tts/_dashscope/_cosyvoice_model.py
index 2b1207107..d5a26a0c7 100644
--- a/src/agentscope/tts/_dashscope/_cosyvoice_model.py
+++ b/src/agentscope/tts/_dashscope/_cosyvoice_model.py
@@ -250,14 +250,20 @@ class DashScopeCosyVoiceTTSModel(TTSModelBase):
):
return self._callback.get_audio_response(block=False)
text_to_send = self._cold_start_buffer
- self._cold_start_buffer = ""
- self._cold_start_done = True
try:
self._synthesizer.streaming_call(text_to_send)
except Exception:
+ # Send failed: keep the cold-start buffer and flag intact so
+ # the next push retries it, mirroring the realtime model.
return TTSResponse(content=None)
+ if not self._cold_start_done:
+ # The cold-start flush succeeded; only now clear the buffer
+ # and latch the flag.
+ self._cold_start_buffer = ""
+ self._cold_start_done = True
+
return self._callback.get_audio_response(block=False)
async def _synthesize_realtime(
diff --git a/tests/tts_dashscope_test.py b/tests/tts_dashscope_test.py
index 4c59b20f1..783af6679 100644
--- a/tests/tts_dashscope_test.py
+++ b/tests/tts_dashscope_test.py
@@ -1165,6 +1165,34 @@ class TestDashScopeCosyVoiceRealtimeMode(
)
self.assertTrue(model._cold_start_done)
+ async def test_push_keeps_cold_start_buffer_on_send_failure(self) -> None:
+ """A failed streaming_call must not consume the buffered text."""
+ with patch.dict("sys.modules", self.mock_modules):
+ async with self._make_model(cold_start_length=10) as model:
+ model._callback.get_audio_response = Mock(
+ return_value=TTSResponse(content=None),
+ )
+ self.mock_synthesizer.streaming_call = Mock(
+ side_effect=RuntimeError("socket gone"),
+ )
+
+ res = await model.push("Hi there friend!")
+
+ self.assertIsNone(res.content)
+ self.assertFalse(model._cold_start_done)
+ self.assertEqual(
+ model._cold_start_buffer,
+ "Hi there friend!",
+ )
+
+ # The next push retries the whole buffered text.
+ self.mock_synthesizer.streaming_call = Mock()
+ await model.push("!")
+ self.mock_synthesizer.streaming_call.assert_called_once_with(
+ "Hi there friend!!",
+ )
+ self.assertTrue(model._cold_start_done)
+
async def test_push_after_cold_start_forwards_directly(self) -> None:
"""Once cold start is done, subsequent deltas bypass the buffer."""
with patch.dict("sys.modules", self.mock_modules):