Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
feat(tts): support dashscope cosyvoice realtime TTS API (#1855)
qbc85e524975514
diff --git a/pyproject.toml b/pyproject.tomlindex a9e4aa3c0..146eefb77 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -125,7 +125,7 @@ include-package-data = true# subdirs that ship with each model provider (they have no# ``__init__.py`` and are not packages, but the files still# live alongside the parent package's source on disk).-"*" = ["py.typed", "_models/*.yaml"]+"*" = ["py.typed", "_models/*.yaml", "_cosyvoice_models/*.yaml"]"agentscope.workspace._docker" = ["Dockerfile*.template"][build-system]diff --git a/src/agentscope/credential/_dashscope.py b/src/agentscope/credential/_dashscope.pyindex 11e95fd1c..0eb41bcae 100644--- a/src/agentscope/credential/_dashscope.py+++ b/src/agentscope/credential/_dashscope.py@@ -47,9 +47,17 @@ class DashScopeCredential(CredentialBase):@classmethoddef get_tts_model_classes(cls) -> list[Type["TTSModelBase"]]:"""Return the DashScope TTS model classes."""- from ..tts import DashScopeRealtimeTTSModel, DashScopeTTSModel-- return [DashScopeTTSModel, DashScopeRealtimeTTSModel]+ from ..tts import (+ DashScopeCosyVoiceRealtimeTTSModel,+ DashScopeRealtimeTTSModel,+ DashScopeTTSModel,+ )++ return [+ DashScopeTTSModel,+ DashScopeRealtimeTTSModel,+ DashScopeCosyVoiceRealtimeTTSModel,+ ]@classmethoddef get_embedding_model_class(cls) -> Type["EmbeddingModelBase"]:diff --git a/src/agentscope/tts/__init__.py b/src/agentscope/tts/__init__.pyindex 42179d92d..4448f5d0e 100644--- a/src/agentscope/tts/__init__.py+++ b/src/agentscope/tts/__init__.py@@ -4,7 +4,11 @@from ._tts_base import TTSModelBasefrom ._tts_model_card import TTSModelCardfrom ._tts_response import TTSResponse, TTSUsage-from ._dashscope import DashScopeTTSModel, DashScopeRealtimeTTSModel+from ._dashscope import (+ DashScopeTTSModel,+ DashScopeRealtimeTTSModel,+ DashScopeCosyVoiceRealtimeTTSModel,+)__all__ = ["TTSModelBase",@@ -13,4 +17,5 @@ __all__ = ["TTSUsage","DashScopeTTSModel","DashScopeRealtimeTTSModel",+ "DashScopeCosyVoiceRealtimeTTSModel",]diff --git a/src/agentscope/tts/_dashscope/__init__.py b/src/agentscope/tts/_dashscope/__init__.pyindex 33cb3c420..b34b47880 100644--- a/src/agentscope/tts/_dashscope/__init__.py+++ b/src/agentscope/tts/_dashscope/__init__.py@@ -3,8 +3,10 @@from ._model import DashScopeTTSModelfrom ._realtime_model import DashScopeRealtimeTTSModel+from ._cosyvoice_realtime_model import DashScopeCosyVoiceRealtimeTTSModel__all__ = ["DashScopeTTSModel","DashScopeRealtimeTTSModel",+ "DashScopeCosyVoiceRealtimeTTSModel",]diff --git a/src/agentscope/tts/_dashscope/_cosyvoice_models/cosyvoice-v3-flash.yaml b/src/agentscope/tts/_dashscope/_cosyvoice_models/cosyvoice-v3-flash.yamlnew file mode 100644index 000000000..608cd198b--- /dev/null+++ b/src/agentscope/tts/_dashscope/_cosyvoice_models/cosyvoice-v3-flash.yaml@@ -0,0 +1,25 @@+name: cosyvoice-v3-flash+label: CosyVoice-v3-Flash+status: active+realtime: true++input_types:+ - text/plain++output_types:+ - audio/wav++voices:+ # Source: https://help.aliyun.com/en/model-studio/cosyvoice-voice-list+ - longanyang+ - longanhuan+ - longhuhu_v3+ - longyingmu_v3+ - longxiaochun+ - longxiaoxia+ - longlaotie+ - longshuo+ - longjing+ - longshu++parameter_overrides: {}diff --git a/src/agentscope/tts/_dashscope/_cosyvoice_models/cosyvoice-v3-plus.yaml b/src/agentscope/tts/_dashscope/_cosyvoice_models/cosyvoice-v3-plus.yamlnew file mode 100644index 000000000..d5f69d073--- /dev/null+++ b/src/agentscope/tts/_dashscope/_cosyvoice_models/cosyvoice-v3-plus.yaml@@ -0,0 +1,25 @@+name: cosyvoice-v3-plus+label: CosyVoice-v3-Plus+status: active+realtime: true++input_types:+ - text/plain++output_types:+ - audio/wav++voices:+ # Source: https://help.aliyun.com/en/model-studio/cosyvoice-voice-list+ - longanyang+ - longanhuan+ - longhuhu_v3+ - longyingmu_v3+ - longxiaochun+ - longxiaoxia+ - longlaotie+ - longshuo+ - longjing+ - longshu++parameter_overrides: {}diff --git a/src/agentscope/tts/_dashscope/_cosyvoice_realtime_model.py b/src/agentscope/tts/_dashscope/_cosyvoice_realtime_model.pynew file mode 100644index 000000000..81ed34504--- /dev/null+++ b/src/agentscope/tts/_dashscope/_cosyvoice_realtime_model.py@@ -0,0 +1,507 @@+# -*- coding: utf-8 -*-+"""DashScope CosyVoice Realtime TTS model implementation.++Uses the older ``dashscope.audio.tts_v2.SpeechSynthesizer`` SDK for models+such as ``cosyvoice-v3-plus``, ``cosyvoice-v3-flash``, ``sambert``, etc.+"""+import asyncio+import base64+import os+import threading+from typing import Any, AsyncGenerator, Literal, TYPE_CHECKING++from pydantic import BaseModel, Field++from .._tts_base import TTSModelBase+from .._tts_response import TTSResponse+from ..._logging import logger+from ..._utils._audio import _build_streaming_wav_header+from ...credential import DashScopeCredential+from ...message import DataBlock, Base64Source++if TYPE_CHECKING:+ from dashscope.audio.tts_v2 import ResultCallback+++_MEDIA_TYPE = "audio/wav"+_SAMPLE_RATE = 24000+_CHANNELS = 1+_BITS_PER_SAMPLE = 16+++def _make_cosyvoice_callback_class() -> type["ResultCallback"]:+ """Create the DashScope CosyVoice TTS callback class lazily."""+ from dashscope.audio.tts_v2 import ResultCallback++ class _CosyVoiceCallback(ResultCallback):+ """Internal callback that accumulates PCM audio from the WebSocket+ and exposes incremental deltas."""++ def __init__(self) -> None:+ """Initialize callback with audio buffer and synchronization+ events."""+ super().__init__()+ self.chunk_event = threading.Event()+ self.finish_event = threading.Event()+ self._pcm_bytes: bytearray = bytearray()+ self._consumed: int = 0++ def on_open(self) -> None:+ """Handle WebSocket open — reset audio state."""+ self._pcm_bytes = bytearray()+ self._consumed = 0+ self.finish_event.clear()+ self.chunk_event.clear()++ def on_data(self, data: bytes) -> None:+ """Handle incoming PCM audio data."""+ if data:+ self._pcm_bytes += data+ if not self.chunk_event.is_set():+ self.chunk_event.set()++ def on_complete(self) -> None:+ """Handle synthesis completion."""+ self.finish_event.set()+ self.chunk_event.set()++ def on_close(self) -> None:+ """Handle WebSocket close."""+ self.finish_event.set()+ self.chunk_event.set()++ def on_error(self, message: Any) -> None:+ """Handle synthesis error."""+ logger.error("CosyVoice TTS error: %s", message)+ self.finish_event.set()+ self.chunk_event.set()++ def _take_delta(self, header: bool = False) -> bytes | None:+ """Return new PCM bytes since last call, or None if empty."""+ new_data = self._pcm_bytes[self._consumed :]+ if not new_data:+ return None+ self._consumed = len(self._pcm_bytes)+ if header:+ return _build_streaming_wav_header(+ sample_rate=_SAMPLE_RATE,+ channels=_CHANNELS,+ bits_per_sample=_BITS_PER_SAMPLE,+ ) + bytes(new_data)+ return bytes(new_data)++ def get_audio_response(self, block: bool) -> TTSResponse:+ """Return incremental audio delta."""+ if block:+ self.finish_event.wait()+ delta = self._take_delta(header=self._consumed == 0)+ if delta:+ return TTSResponse(+ content=DataBlock(+ source=Base64Source(+ data=base64.b64encode(delta).decode("ascii"),+ media_type=_MEDIA_TYPE,+ ),+ ),+ )+ return TTSResponse(content=None)++ async def get_audio_chunks(self) -> AsyncGenerator[TTSResponse, None]:+ """Yield incremental WAV audio chunks as they arrive."""+ header_sent = self._consumed > 0+ while True:+ if self.finish_event.is_set():+ delta = self._take_delta(header=not header_sent)+ if delta:+ yield TTSResponse(+ content=DataBlock(+ source=Base64Source(+ data=base64.b64encode(delta).decode(+ "ascii",+ ),+ media_type=_MEDIA_TYPE,+ ),+ ),+ is_last=True,+ )+ else:+ yield TTSResponse(content=None, is_last=True)+ self.reset()+ break++ if self.chunk_event.is_set():+ self.chunk_event.clear()+ else:+ await asyncio.to_thread(self.chunk_event.wait, 30)++ if self.finish_event.is_set():+ continue++ delta = self._take_delta(header=not header_sent)+ if delta:+ header_sent = True+ yield TTSResponse(+ content=DataBlock(+ source=Base64Source(+ data=base64.b64encode(delta).decode("ascii"),+ media_type=_MEDIA_TYPE,+ ),+ ),+ is_last=False,+ )++ def reset(self) -> None:+ """Reset internal state for the next utterance."""+ self.finish_event.clear()+ self.chunk_event.clear()+ self._pcm_bytes = bytearray()+ self._consumed = 0++ def has_audio_data(self) -> bool:+ """Return whether any audio data has been received."""+ return bool(self._pcm_bytes)++ return _CosyVoiceCallback+++class DashScopeCosyVoiceRealtimeTTSModel(TTSModelBase):+ """DashScope CosyVoice Realtime TTS model using the+ ``SpeechSynthesizer`` streaming API.++ Supports streaming input: text can be pushed incrementally via+ :meth:`push`, and :meth:`synthesize` finalizes the current utterance.++ Supported models include ``cosyvoice-v3-plus``, ``cosyvoice-v3-flash``,+ ``sambert``, etc. For more details see the `official document+ <https://help.aliyun.com/document_detail/2712523.html>`_.++ .. note:: Only one streaming input request can be active at a time.++ .. note:: Unlike ``DashScopeRealtimeTTSModel`` (Qwen3) which produces+ audio at token-level granularity, CosyVoice server automatically+ segments incoming text into sentences and synthesizes per-sentence.+ Audio is returned via callback only after a complete sentence boundary+ is detected. This means :meth:`push` may return empty responses until+ enough text accumulates to form a sentence. Calling+ :meth:`synthesize` forces synthesis of all remaining text (including+ incomplete sentences). See `official docs+ <https://help.aliyun.com/en/model-studio/cosyvoice-python-sdk>`_.+ """++ class Parameters(BaseModel):+ """Frontend-exposed parameters for CosyVoice Realtime TTS models."""++ voice: str = Field(+ default="longanyang",+ title="Voice",+ description="The voice to use for synthesis.",+ )++ type: Literal[+ "dashscope_cosyvoice_realtime_tts"+ ] = "dashscope_cosyvoice_realtime_tts"++ realtime: bool = True++ _MODELS_DIR = os.path.join(os.path.dirname(__file__), "_cosyvoice_models")++ @classmethod+ def list_models(+ cls,+ custom_yaml_dir: str | None = None,+ ) -> list:+ """List CosyVoice model cards from the dedicated YAML directory."""+ return super().list_models(+ custom_yaml_dir=custom_yaml_dir or cls._MODELS_DIR,+ )++ def __init__(+ self,+ credential: DashScopeCredential,+ model: str = "cosyvoice-v3-plus",+ parameters: "DashScopeCosyVoiceRealtimeTTSModel.Parameters | None" = (+ None+ ),+ stream: bool = True,+ cold_start_length: int | None = None,+ cold_start_words: int | None = None,+ max_retries: int = 3,+ retry_delay: float = 5.0,+ ) -> None:+ """Initialize the DashScope CosyVoice Realtime TTS model.++ Args:+ credential (`DashScopeCredential`):+ The DashScope credential.+ model (`str`, defaults to ``"cosyvoice-v3-plus"``):+ The CosyVoice model name, e.g. ``"cosyvoice-v3-plus"``,+ ``"cosyvoice-v3-flash"``, ``"sambert"``.+ parameters (`Parameters | None`, defaults to `None`):+ The TTS parameters (voice, etc.).+ stream (`bool`, defaults to `True`):+ Whether :meth:`synthesize` returns a streaming async generator.+ cold_start_length (`int | None`, defaults to `None`):+ Minimum character count before the first text chunk is sent+ to the synthesizer.+ cold_start_words (`int | None`, defaults to `None`):+ Minimum word count (split by spaces) before the first text+ chunk is sent.+ max_retries (`int`, defaults to `3`):+ Max retry attempts on synthesis failure.+ retry_delay (`float`, defaults to `5.0`):+ Initial retry delay in seconds (exponential backoff).+ """+ super().__init__(+ credential=credential,+ model=model,+ parameters=parameters,+ stream=stream,+ )+ self.cold_start_length = cold_start_length+ self.cold_start_words = cold_start_words+ self.max_retries = max_retries+ self.retry_delay = retry_delay++ self._synthesizer: Any = None+ self._callback: Any = None+ self._connected = False+ self._cold_start_buffer: str = ""+ self._cold_start_done: bool = False+ self._accumulated_text: str = ""++ def _create_synthesizer(self) -> None:+ """Create a fresh SpeechSynthesizer and callback."""+ import dashscope+ from dashscope.audio.tts_v2 import SpeechSynthesizer, AudioFormat++ dashscope.api_key = self.credential.api_key.get_secret_value()++ callback_cls = _make_cosyvoice_callback_class()+ self._callback = callback_cls()+ self._synthesizer = SpeechSynthesizer(+ model=self.model,+ voice=self.parameters.voice,+ format=AudioFormat.PCM_24000HZ_MONO_16BIT,+ callback=self._callback,+ )++ async def connect(self) -> None:+ """Initialize the SpeechSynthesizer."""+ if self._connected:+ return+ self._create_synthesizer()+ self._connected = True++ async def close(self) -> None:+ """Close the SpeechSynthesizer."""+ if not self._connected:+ return+ self._connected = False+ try:+ if self._synthesizer is not None:+ self._synthesizer.close()+ except Exception:+ pass++ async def _reconnect(self) -> None:+ """Reconnect by recreating the synthesizer."""+ try:+ if self._synthesizer is not None:+ self._synthesizer.close()+ except Exception:+ pass+ self._connected = False+ self._cold_start_buffer = ""+ self._cold_start_done = False+ await self.connect()++ async def push(+ self,+ text: str,+ **kwargs: Any,+ ) -> TTSResponse:+ """Push an incremental text delta for realtime synthesis.++ .. note:: The CosyVoice server automatically segments text into+ sentences before synthesizing. Audio is only produced after a+ complete sentence is detected, so this method often returns an+ empty response (``content=None``) for partial sentences. Remaining+ audio is force-synthesized when :meth:`synthesize` is called.+ See `CosyVoice Python SDK docs+ <https://help.aliyun.com/en/model-studio/cosyvoice-python-sdk>`_.++ Args:+ text (`str`):+ An incremental text chunk (delta) to append.+ **kwargs (`Any`):+ Additional keyword arguments (unused).++ Returns:+ `TTSResponse`:+ Audio accumulated so far, or empty if not yet available.+ """+ if not self._connected:+ raise RuntimeError(+ "TTS model is not connected. Call `connect()` first.",+ )++ if not text:+ return TTSResponse(content=None)++ self._accumulated_text += text++ if self._cold_start_done:+ text_to_send = text+ else:+ self._cold_start_buffer += text+ if (+ self.cold_start_length+ and len(self._cold_start_buffer) < self.cold_start_length+ ) or (+ self.cold_start_words+ and len(self._cold_start_buffer.split())+ < self.cold_start_words+ ):+ 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:+ return TTSResponse(content=None)++ return self._callback.get_audio_response(block=False)++ async def synthesize(+ self,+ text: str | None = None,+ **kwargs: Any,+ ) -> TTSResponse | AsyncGenerator[TTSResponse, None]:+ """Finalize synthesis for the current utterance.++ If text was previously pushed via :meth:`push`, this flushes any+ remaining buffered text, calls ``streaming_complete()``, and waits+ for audio. If ``text`` is provided, it is appended before finalizing.++ Args:+ text (`str | None`, defaults to `None`):+ Optional additional text to append before finalizing.+ **kwargs (`Any`):+ Additional keyword arguments (unused).++ Returns:+ `TTSResponse | AsyncGenerator[TTSResponse, None]`:+ A single response when ``stream=False``, or an async generator+ of incremental chunks when ``stream=True``.+ """+ if not self._connected:+ raise RuntimeError(+ "TTS model is not connected. Call `connect()` first.",+ )++ if text is not None:+ self._accumulated_text += text++ unsent = self._cold_start_buffer+ if text is not None:+ unsent += text+ self._cold_start_buffer = ""++ full_text = self._accumulated_text+ delay = self.retry_delay++ try:+ if not full_text and not unsent:+ if self.stream:++ async def _empty_gen() -> AsyncGenerator[+ TTSResponse,+ None,+ ]:+ yield TTSResponse(content=None)++ return _empty_gen()+ return TTSResponse(content=None)++ for attempt in range(self.max_retries):+ try:+ if unsent:+ self._synthesizer.streaming_call(unsent)++ self._synthesizer.streaming_complete()++ finished = await asyncio.to_thread(+ self._callback.finish_event.wait,+ 30,+ )++ if not finished:+ logger.warning(+ "CosyVoice TTS: timed out waiting for synthesis "+ "completion (30s)",+ )+ if attempt < self.max_retries - 1:+ await asyncio.sleep(delay)+ await self._reconnect()+ unsent = full_text+ delay *= 2+ continue+ raise RuntimeError(+ "CosyVoice TTS synthesis timed out after 30s",+ )++ if full_text and not self._callback.has_audio_data():+ if attempt < self.max_retries - 1:+ logger.warning(+ "CosyVoice TTS: no audio received, retrying "+ "(%d/%d) in %.1fs...",+ attempt + 1,+ self.max_retries,+ delay,+ )+ await asyncio.sleep(delay)+ await self._reconnect()+ unsent = full_text+ delay *= 2+ continue+ raise RuntimeError(+ f"CosyVoice TTS synthesis failed: no audio after "+ f"{self.max_retries} attempts",+ )+ break++ except RuntimeError:+ raise+ except Exception as e:+ if attempt < self.max_retries - 1:+ logger.warning(+ "CosyVoice TTS error, retrying (%d/%d) in "+ "%.1fs: %s",+ attempt + 1,+ self.max_retries,+ delay,+ e,+ )+ await asyncio.sleep(delay)+ await self._reconnect()+ unsent = full_text+ delay *= 2+ else:+ raise++ if self.stream:+ return self._callback.get_audio_chunks()++ response = self._callback.get_audio_response(block=True)+ self._callback.reset()+ return response+ finally:+ self._reset_state()++ def _reset_state(self) -> None:+ """Reset per-utterance tracking state."""+ self._cold_start_buffer = ""+ self._cold_start_done = False+ self._accumulated_text = ""diff --git a/src/agentscope/tts/_dashscope/_realtime_model.py b/src/agentscope/tts/_dashscope/_realtime_model.pyindex 40767c20b..49df43c8b 100644--- a/src/agentscope/tts/_dashscope/_realtime_model.py+++ b/src/agentscope/tts/_dashscope/_realtime_model.py@@ -40,6 +40,8 @@ def _make_callback_class() -> type["QwenTtsRealtimeCallback"]:"""def __init__(self) -> None:+ """Initialize callback with audio buffer and synchronization+ events."""super().__init__()self.chunk_event = threading.Event()self.finish_event = threading.Event()@@ -55,6 +57,7 @@ def _make_callback_class() -> type["QwenTtsRealtimeCallback"]:self._pcm_bytes = bytearray()self._consumed = 0self.finish_event.clear()+ self.chunk_event.clear()elif event_type == "response.audio.delta":audio_data = response.get("delta")@@ -122,7 +125,7 @@ def _make_callback_class() -> type["QwenTtsRealtimeCallback"]:async def get_audio_chunks(self) -> AsyncGenerator[TTSResponse, None]:"""Yield incremental WAV audio chunks as they arrive."""- header_sent = False+ header_sent = self._consumed > 0while True:if self.finish_event.is_set():delta = self._take_delta(header=not header_sent)@@ -454,7 +457,9 @@ class DashScopeRealtimeTTSModel(TTSModelBase):if self.stream:return self._callback.get_audio_chunks()- return self._callback.get_audio_response(block=True)+ response = self._callback.get_audio_response(block=True)+ self._callback.reset()+ return responsedef _reset_state(self) -> None:"""Reset per-utterance tracking state."""diff --git a/tests/tts_dashscope_test.py b/tests/tts_dashscope_test.pyindex adf27521f..b0b0a6ec9 100644--- a/tests/tts_dashscope_test.py+++ b/tests/tts_dashscope_test.py@@ -10,6 +10,8 @@ Covers:placement at the final chunk only.* ``DashScopeRealtimeTTSModel`` connect / close / push / synthesizelifecycle over a mocked WebSocket.+ * ``DashScopeCosyVoiceRealtimeTTSModel`` connect / close / push /+ synthesize lifecycle over a mocked SpeechSynthesizer."""import base64import io@@ -20,6 +22,7 @@ from unittest.mock import MagicMock, Mock, patchfrom agentscope.credential import DashScopeCredentialfrom agentscope.tts import (+ DashScopeCosyVoiceRealtimeTTSModel,DashScopeTTSModel,DashScopeRealtimeTTSModel,TTSModelBase,@@ -385,33 +388,6 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methods# -- connect / close --- async def test_connect_creates_client(self) -> None:- """connect() creates a WebSocket client and calls update_session."""- with patch.dict("sys.modules", self.mock_modules):- model = self._make_model()- await model.connect()-- self.assertTrue(model._connected)- self.mock_client.connect.assert_called_once()- self.mock_client.update_session.assert_called_once()-- async def test_close_disconnects(self) -> None:- """close() sets _connected to False and closes the client."""- with patch.dict("sys.modules", self.mock_modules):- model = self._make_model()- await model.connect()- await model.close()-- self.assertFalse(model._connected)- self.mock_client.close.assert_called_once()-- async def test_close_idempotent(self) -> None:- """Calling close() when already disconnected is a no-op."""- model = self._make_model()- model._connected = False- await model.close()- self.assertFalse(model._connected)-async def test_async_context_manager(self) -> None:"""``async with`` triggers connect on enter and close on exit."""with patch.dict("sys.modules", self.mock_modules):@@ -422,16 +398,6 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methods# -- push --- async def test_push_appends_text(self) -> None:- """A single push forwards the delta to append_text."""- with patch.dict("sys.modules", self.mock_modules):- async with self._make_model() as model:- await model.push("Hello")-- self.mock_client.append_text.assert_called_once_with("Hello")- self.assertEqual(model._accumulated_text, "Hello")- self.assertTrue(model._cold_start_done)-async def test_push_incremental_deltas(self) -> None:"""Consecutive deltas are each forwarded verbatim."""with patch.dict("sys.modules", self.mock_modules):@@ -445,23 +411,7 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methods)self.mock_client.append_text.assert_any_call("Hello")self.mock_client.append_text.assert_any_call(" world")-- async def test_push_empty_text_no_call(self) -> None:- """Empty string does not call append_text."""- with patch.dict("sys.modules", self.mock_modules):- async with self._make_model() as model:- res = await model.push("")-- self.mock_client.append_text.assert_not_called()- self.assertIsNone(res.content)-- async def test_push_not_connected_raises(self) -> None:- """push() raises RuntimeError when not connected."""- model = self._make_model()- model._connected = False-- with self.assertRaises(RuntimeError):- await model.push("Hello")+ self.assertEqual(model._accumulated_text, "Hello world")async def test_push_returns_audio_when_available(self) -> None:"""push() returns audio data from the callback when available."""@@ -484,16 +434,6 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methodsself.assertIsNotNone(res.content)self.assertEqual(res.content.source.data, mock_audio_data)- async def test_push_returns_empty_when_no_audio(self) -> None:- """push() returns empty response when no audio is ready."""- with patch.dict("sys.modules", self.mock_modules):- async with self._make_model() as model:- model._callback.get_audio_response = Mock(- return_value=TTSResponse(content=None),- )- res = await model.push("Hello")- self.assertIsNone(res.content)-async def test_push_cold_start_buffers_across_deltas(self) -> None:"""Multiple small deltas are buffered until cold_start_length ismet, then flushed as a single ``append_text`` call."""@@ -510,16 +450,6 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methods)self.assertTrue(model._cold_start_done)- async def test_push_cold_start_single_delta_meets_threshold(self) -> None:- """A single delta exceeding cold_start_length sends immediately."""- with patch.dict("sys.modules", self.mock_modules):- async with self._make_model(cold_start_length=5) as model:- await model.push("Hello world")-- self.mock_client.append_text.assert_called_once_with(- "Hello world",- )-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):@@ -592,16 +522,420 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methodsself.mock_client.append_text.assert_not_called()self.mock_client.commit.assert_called_once()- async def test_synthesize_resets_state(self) -> None:- """State is reset after synthesize completes."""+ async def test_synthesize_stream_returns_generator(self) -> None:+ """stream=True returns an async generator with is_last."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model(stream=True) as model:+ self._mock_synthesize_callback(model)++ async def mock_chunks() -> AsyncGenerator[TTSResponse, None]:+ yield TTSResponse(content=None, is_last=True)++ model._callback.get_audio_chunks = mock_chunks++ gen = await model.synthesize(text="Hello")+ chunks = [c async for c in gen]++ self.assertTrue(len(chunks) >= 1)+ self.assertTrue(chunks[-1].is_last)++ async def test_synthesize_flushes_cold_start_buffer(self) -> None:+ """If cold start was never met during push, synthesize flushes the+ buffered text before committing."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model(cold_start_length=100) as model:+ await model.push("Hi")+ await model.push(" there")+ self.mock_client.append_text.assert_not_called()++ self._mock_synthesize_callback(model)+ await model.synthesize()++ self.mock_client.append_text.assert_called_once_with(+ "Hi there",+ )+ self.mock_client.commit.assert_called_once()++ async def test_synthesize_no_audio_raises_after_retries(self) -> None:+ """RuntimeError after max_retries with no audio received."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model(+ max_retries=1,+ retry_delay=0.0,+ ) as model:+ model._callback.finish_event = Mock()+ model._callback.finish_event.wait = Mock()+ model._callback.has_audio_data = Mock(return_value=False)++ with self.assertRaises(RuntimeError):+ await model.synthesize(text="Hello")++ # -- callback --++ async def test_get_audio_chunks_prepends_header_without_prior_push(+ self,+ ) -> None:+ """get_audio_chunks() prepends a WAV header when push() has not+ yet consumed any bytes (_consumed == 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._realtime_model import (+ _make_callback_class,+ )++ callback_cls = _make_callback_class()+ cb = callback_cls()++ audio_b64 = base64.b64encode(b"ALLPCM").decode()+ cb.on_event({"type": "response.audio.delta", "delta": audio_b64})+ cb.on_event({"type": "session.finished"})++ chunks = [c async for c in cb.get_audio_chunks()]+ raw_payloads = [+ base64.b64decode(c.content.source.data)+ for c in chunks+ if c.content is not None+ ]++ self.assertTrue(len(raw_payloads) > 0)+ self.assertTrue(raw_payloads[0].startswith(b"RIFF"))+ self.assertTrue(raw_payloads[0].endswith(b"ALLPCM"))++ async def test_get_audio_chunks_skips_header_after_partial_push(+ self,+ ) -> None:+ """get_audio_chunks() does NOT prepend a second WAV header when+ push() already consumed and sent the first chunk with a header+ (_consumed > 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._realtime_model import (+ _make_callback_class,+ )++ callback_cls = _make_callback_class()+ cb = callback_cls()++ # Simulate push() consuming the first chunk (with WAV header)+ first_b64 = base64.b64encode(b"FIRSTPCM").decode()+ cb.on_event(+ {"type": "response.audio.delta", "delta": first_b64},+ )+ first_resp = cb.get_audio_response(block=False)+ self.assertIsNotNone(first_resp.content)+ self.assertGreater(cb._consumed, 0)++ # More data arrives and session finishes+ more_b64 = base64.b64encode(b"MOREPCM").decode()+ cb.on_event(+ {"type": "response.audio.delta", "delta": more_b64},+ )+ cb.on_event({"type": "session.finished"})++ chunks = [c async for c in cb.get_audio_chunks()]+ raw_payloads = [+ base64.b64decode(c.content.source.data)+ for c in chunks+ if c.content is not None+ ]++ self.assertTrue(len(raw_payloads) > 0)+ # No second RIFF header should appear mid-stream+ self.assertFalse(raw_payloads[0].startswith(b"RIFF"))+ self.assertEqual(raw_payloads[0], b"MOREPCM")++ async def test_get_audio_response_includes_header_without_prior_push(+ self,+ ) -> None:+ """get_audio_response() prepends a WAV header when no data has been+ consumed yet (_consumed == 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._realtime_model import (+ _make_callback_class,+ )++ callback_cls = _make_callback_class()+ cb = callback_cls()++ audio_b64 = base64.b64encode(b"ALLPCM").decode()+ cb.on_event({"type": "response.audio.delta", "delta": audio_b64})+ cb.on_event({"type": "session.finished"})++ resp = cb.get_audio_response(block=True)+ raw = base64.b64decode(resp.content.source.data)++ self.assertTrue(raw.startswith(b"RIFF"))+ self.assertTrue(raw.endswith(b"ALLPCM"))++ async def test_get_audio_response_skips_header_after_partial_push(+ self,+ ) -> None:+ """get_audio_response() does NOT prepend a second WAV header when+ push() already consumed and sent the first chunk with a header+ (_consumed > 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._realtime_model import (+ _make_callback_class,+ )++ callback_cls = _make_callback_class()+ cb = callback_cls()++ first_b64 = base64.b64encode(b"FIRSTPCM").decode()+ cb.on_event(+ {"type": "response.audio.delta", "delta": first_b64},+ )+ first_resp = cb.get_audio_response(block=False)+ self.assertIsNotNone(first_resp.content)+ self.assertGreater(cb._consumed, 0)++ more_b64 = base64.b64encode(b"MOREPCM").decode()+ cb.on_event(+ {"type": "response.audio.delta", "delta": more_b64},+ )+ cb.on_event({"type": "session.finished"})++ resp = cb.get_audio_response(block=True)+ raw = base64.b64decode(resp.content.source.data)++ self.assertFalse(raw.startswith(b"RIFF"))+ self.assertEqual(raw, b"MOREPCM")+++# ---------------------------------------------------------------------------+# DashScopeCosyVoiceRealtimeTTSModel — realtime push / synthesize lifecycle+# ---------------------------------------------------------------------------+++# pylint: disable=too-many-public-methods+class TestDashScopeCosyVoiceRealtimeTTSModel(+ IsolatedAsyncioTestCase,+):+ """Unit tests for the CosyVoice Realtime TTS model."""++ def setUp(self) -> None:+ self.mock_modules = self._create_mock_cosyvoice_modules()+ self.mock_synthesizer = self._create_mock_synthesizer()+ self.mock_modules["dashscope.audio.tts_v2"].SpeechSynthesizer = Mock(+ return_value=self.mock_synthesizer,+ )++ @staticmethod+ def _create_mock_cosyvoice_modules() -> dict:+ mock_result_callback = Mock+ mock_audio_format = MagicMock()+ mock_audio_format.PCM_24000HZ_MONO_16BIT = "pcm_24000hz_mono_16bit"++ mock_tts_v2 = MagicMock()+ mock_tts_v2.ResultCallback = mock_result_callback+ mock_tts_v2.AudioFormat = mock_audio_format+ mock_tts_v2.SpeechSynthesizer = Mock++ mock_audio = MagicMock()+ mock_audio.tts_v2 = mock_tts_v2++ mock_dashscope = MagicMock()+ mock_dashscope.api_key = None+ mock_dashscope.audio = mock_audio++ return {+ "dashscope": mock_dashscope,+ "dashscope.audio": mock_audio,+ "dashscope.audio.tts_v2": mock_tts_v2,+ }++ @staticmethod+ def _create_mock_synthesizer() -> Mock:+ synth = Mock()+ synth.streaming_call = Mock()+ synth.streaming_complete = Mock()+ synth.close = Mock()+ return synth++ def _make_model(+ self,+ **kwargs: Any,+ ) -> DashScopeCosyVoiceRealtimeTTSModel:+ defaults: dict[str, Any] = {+ "credential": DashScopeCredential(api_key="test"),+ "model": "cosyvoice-v3-plus",+ "stream": True,+ "max_retries": 1,+ "retry_delay": 0.0,+ }+ defaults.update(kwargs)+ return DashScopeCosyVoiceRealtimeTTSModel(**defaults)++ def _mock_synthesize_callback(self, model: Any) -> None:+ """Set up callback mocks so synthesize() doesn't block."""+ model._callback.finish_event = Mock()+ model._callback.finish_event.wait = Mock(return_value=True)+ model._callback.has_audio_data = Mock(return_value=True)+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )++ # -- connect / close --++ async def test_async_context_manager(self) -> None:+ """async with triggers connect on enter and close on exit."""+ with patch.dict("sys.modules", self.mock_modules):+ model = self._make_model()+ async with model:+ self.assertTrue(model._connected)+ self.assertFalse(model._connected)++ # -- push --++ async def test_push_incremental_deltas(self) -> None:+ """Consecutive deltas are each forwarded via streaming_call."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model() as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )+ await model.push("Hello")+ await model.push(" world")++ self.assertEqual(+ self.mock_synthesizer.streaming_call.call_count,+ 2,+ )+ self.mock_synthesizer.streaming_call.assert_any_call("Hello")+ self.mock_synthesizer.streaming_call.assert_any_call(" world")++ async def test_push_returns_audio_when_available(self) -> None:+ """push() returns audio data from the callback when available."""+ mock_audio_data = base64.b64encode(b"PCMDATA").decode("ascii")++ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model() as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(+ content=MagicMock(+ source=MagicMock(+ data=mock_audio_data,+ media_type=_MEDIA_TYPE,+ ),+ ),+ ),+ )+ res = await model.push("Hello")++ self.assertIsNotNone(res.content)+ self.assertEqual(res.content.source.data, mock_audio_data)++ async def test_push_cold_start_buffers_across_deltas(self) -> None:+ """Multiple small deltas are buffered until cold_start_length is+ met, then flushed as a single streaming_call."""+ 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),+ )+ await model.push("Hi")+ await model.push(" there")+ self.mock_synthesizer.streaming_call.assert_not_called()+ self.assertFalse(model._cold_start_done)++ await model.push(" friend!")+ 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):+ async with self._make_model(cold_start_length=3) as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )+ await model.push("Hello")+ self.mock_synthesizer.streaming_call.assert_called_with(+ "Hello",+ )++ await model.push(" world")+ self.mock_synthesizer.streaming_call.assert_called_with(+ " world",+ )+ self.assertEqual(+ self.mock_synthesizer.streaming_call.call_count,+ 2,+ )++ async def test_push_cold_start_words_buffers(self) -> None:+ """Deltas below cold_start_words are buffered."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model(cold_start_words=3) as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )+ await model.push("Hello")+ await model.push(" world")++ self.mock_synthesizer.streaming_call.assert_not_called()+ self.assertFalse(model._cold_start_done)++ async def test_push_exception_returns_empty(self) -> None:+ """push() returns empty response if streaming_call raises."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model() as model:+ self.mock_synthesizer.streaming_call.side_effect = Exception(+ "connection error",+ )+ res = await model.push("Hello")++ self.assertIsNone(res.content)++ # -- synthesize --++ async def test_synthesize_calls_streaming_complete(self) -> None:+ """synthesize() calls streaming_call and streaming_complete."""with patch.dict("sys.modules", self.mock_modules):async with self._make_model() as model:self._mock_synthesize_callback(model)+await model.synthesize(text="Hello")- self.assertFalse(model._cold_start_done)- self.assertEqual(model._accumulated_text, "")- self.assertEqual(model._cold_start_buffer, "")+ self.mock_synthesizer.streaming_call.assert_called_once_with(+ "Hello",+ )+ self.mock_synthesizer.streaming_complete.assert_called_once()++ async def test_synthesize_appends_extra_text(self) -> None:+ """synthesize(text=...) after push appends the extra text."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model() as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )+ await model.push("Hello")+ self.mock_synthesizer.streaming_call.reset_mock()++ self._mock_synthesize_callback(model)+ await model.synthesize(text=" world")++ self.mock_synthesizer.streaming_call.assert_called_once_with(+ " world",+ )+ self.mock_synthesizer.streaming_complete.assert_called_once()++ async def test_synthesize_no_text_drain(self) -> None:+ """synthesize(None) after push calls streaming_complete without+ extra streaming_call."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model() as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )+ await model.push("Hello")+ self.mock_synthesizer.streaming_call.reset_mock()++ self._mock_synthesize_callback(model)+ await model.synthesize()++ self.mock_synthesizer.streaming_call.assert_not_called()+ self.mock_synthesizer.streaming_complete.assert_called_once()async def test_synthesize_stream_returns_generator(self) -> None:"""stream=True returns an async generator with is_last."""@@ -645,28 +979,35 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methodsasync def test_synthesize_flushes_cold_start_buffer(self) -> None:"""If cold start was never met during push, synthesize flushes the- buffered text before committing."""+ buffered text before calling streaming_complete."""with patch.dict("sys.modules", self.mock_modules):async with self._make_model(cold_start_length=100) as model:+ model._callback.get_audio_response = Mock(+ return_value=TTSResponse(content=None),+ )await model.push("Hi")await model.push(" there")- self.mock_client.append_text.assert_not_called()+ self.mock_synthesizer.streaming_call.assert_not_called()self._mock_synthesize_callback(model)await model.synthesize()- self.mock_client.append_text.assert_called_once_with(+ self.mock_synthesizer.streaming_call.assert_called_once_with("Hi there",)- self.mock_client.commit.assert_called_once()+ self.mock_synthesizer.streaming_complete.assert_called_once()- async def test_synthesize_not_connected_raises(self) -> None:- """synthesize() raises RuntimeError when not connected."""- model = self._make_model()- model._connected = False+ async def test_synthesize_empty_text_short_circuits(self) -> None:+ """synthesize() with no accumulated text returns empty."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model(stream=True) as model:+ gen = await model.synthesize()+ chunks = [c async for c in gen]- with self.assertRaises(RuntimeError):- await model.synthesize(text="Hello")+ self.assertEqual(len(chunks), 1)+ self.assertIsNone(chunks[0].content)+ self.assertTrue(chunks[0].is_last)+ self.mock_synthesizer.streaming_complete.assert_not_called()async def test_synthesize_no_audio_raises_after_retries(self) -> None:"""RuntimeError after max_retries with no audio received."""@@ -676,8 +1017,244 @@ class TestDashScopeRealtimeTTSModel( # pylint: disable=too-many-public-methodsretry_delay=0.0,) as model:model._callback.finish_event = Mock()- model._callback.finish_event.wait = Mock()+ model._callback.finish_event.wait = Mock(return_value=True)model._callback.has_audio_data = Mock(return_value=False)with self.assertRaises(RuntimeError):await model.synthesize(text="Hello")++ async def test_synthesize_timeout_raises(self) -> None:+ """RuntimeError when finish_event.wait times out."""+ with patch.dict("sys.modules", self.mock_modules):+ async with self._make_model(+ max_retries=1,+ retry_delay=0.0,+ ) as model:+ model._callback.finish_event = Mock()+ model._callback.finish_event.wait = Mock(return_value=False)++ with self.assertRaises(RuntimeError) as ctx:+ await model.synthesize(text="Hello")++ self.assertIn("timed out", str(ctx.exception))++ # -- callback --++ async def test_callback_on_complete_sets_finish_event(self) -> None:+ """on_complete() correctly sets finish_event and chunk_event."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ self.assertFalse(cb.finish_event.is_set())+ self.assertFalse(cb.chunk_event.is_set())++ cb.on_complete()++ self.assertTrue(cb.finish_event.is_set())+ self.assertTrue(cb.chunk_event.is_set())++ async def test_callback_on_data_accumulates(self) -> None:+ """on_data() accumulates PCM bytes and signals chunk_event."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"AAAA")+ cb.on_data(b"BBBB")++ self.assertEqual(bytes(cb._pcm_bytes), b"AAAABBBB")+ self.assertTrue(cb.chunk_event.is_set())++ async def test_callback_take_delta_incremental(self) -> None:+ """_take_delta() returns only new bytes since last call."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"AAAA")+ delta1 = cb._take_delta(header=False)+ self.assertEqual(delta1, b"AAAA")++ cb.on_data(b"BBBB")+ delta2 = cb._take_delta(header=False)+ self.assertEqual(delta2, b"BBBB")++ delta3 = cb._take_delta(header=False)+ self.assertIsNone(delta3)++ async def test_callback_take_delta_with_header(self) -> None:+ """_take_delta(header=True) prepends WAV header to first chunk."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"PCMPCM")+ delta = cb._take_delta(header=True)++ self.assertTrue(delta.startswith(b"RIFF"))+ self.assertIn(b"WAVE", delta[:12])+ self.assertTrue(delta.endswith(b"PCMPCM"))++ async def test_get_audio_chunks_prepends_header_without_prior_push(+ self,+ ) -> None:+ """get_audio_chunks() prepends a WAV header when push() has not+ yet consumed any bytes (_consumed == 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"ALLPCM")+ cb.on_complete()++ chunks = [c async for c in cb.get_audio_chunks()]+ raw_payloads = [+ base64.b64decode(c.content.source.data)+ for c in chunks+ if c.content is not None+ ]++ self.assertTrue(len(raw_payloads) > 0)+ self.assertTrue(raw_payloads[0].startswith(b"RIFF"))+ self.assertTrue(raw_payloads[0].endswith(b"ALLPCM"))++ async def test_get_audio_chunks_skips_header_after_partial_push(+ self,+ ) -> None:+ """get_audio_chunks() does NOT prepend a second WAV header when+ push() already consumed and sent the first chunk with a header+ (_consumed > 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ # Simulate push() consuming the first chunk (with WAV header)+ cb.on_data(b"FIRSTPCM")+ first_resp = cb.get_audio_response(block=False)+ self.assertIsNotNone(first_resp.content)+ self.assertGreater(cb._consumed, 0)++ # More data arrives and synthesis completes+ cb.on_data(b"MOREPCM")+ cb.on_complete()++ chunks = [c async for c in cb.get_audio_chunks()]+ raw_payloads = [+ base64.b64decode(c.content.source.data)+ for c in chunks+ if c.content is not None+ ]++ self.assertTrue(len(raw_payloads) > 0)+ # No second RIFF header should appear mid-stream+ self.assertFalse(raw_payloads[0].startswith(b"RIFF"))+ self.assertEqual(raw_payloads[0], b"MOREPCM")++ async def test_get_audio_response_includes_header_without_prior_push(+ self,+ ) -> None:+ """get_audio_response() prepends a WAV header when no data has been+ consumed yet (_consumed == 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"ALLPCM")+ cb.on_complete()++ resp = cb.get_audio_response(block=True)+ raw = base64.b64decode(resp.content.source.data)++ self.assertTrue(raw.startswith(b"RIFF"))+ self.assertTrue(raw.endswith(b"ALLPCM"))++ async def test_get_audio_response_skips_header_after_partial_push(+ self,+ ) -> None:+ """get_audio_response() does NOT prepend a second WAV header when+ push() already consumed and sent the first chunk with a header+ (_consumed > 0)."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"FIRSTPCM")+ first_resp = cb.get_audio_response(block=False)+ self.assertIsNotNone(first_resp.content)+ self.assertGreater(cb._consumed, 0)++ cb.on_data(b"MOREPCM")+ cb.on_complete()++ resp = cb.get_audio_response(block=True)+ raw = base64.b64decode(resp.content.source.data)++ self.assertFalse(raw.startswith(b"RIFF"))+ self.assertEqual(raw, b"MOREPCM")++ async def test_callback_reset(self) -> None:+ """reset() clears all state."""+ with patch.dict("sys.modules", self.mock_modules):+ from agentscope.tts._dashscope._cosyvoice_realtime_model import (+ _make_cosyvoice_callback_class,+ )++ callback_cls = _make_cosyvoice_callback_class()+ cb = callback_cls()++ cb.on_data(b"AAAA")+ cb.on_complete()+ cb.reset()++ self.assertFalse(cb.finish_event.is_set())+ self.assertFalse(cb.chunk_event.is_set())+ self.assertEqual(bytes(cb._pcm_bytes), b"")+ self.assertEqual(cb._consumed, 0)++ # -- reconnect --++ async def test_reconnect_recreates_synthesizer(self) -> None:+ """_reconnect() closes old synthesizer and creates a new one."""+ with patch.dict("sys.modules", self.mock_modules):+ model = self._make_model()+ await model.connect()+ old_callback = model._callback++ await model._reconnect()++ self.mock_synthesizer.close.assert_called_once()+ self.assertTrue(model._connected)+ self.assertIsNot(model._callback, old_callback)