类class
agentscope.middlewareTTSMiddleware
class TTSMiddleware(MiddlewareBase)
源码Source
agentscope/middleware/_tts_middleware.py:21–179TTS 中间件,在智能体执行流程中提供横切扩展。位于 agentscope.middleware 包,继承自 MiddlewareBase。核心成员包括 on_reply。
- Non-realtime TTS (
realtime=False): on eachTextBlockEndEventthe accumulated text is sent to :meth:TTSModelBase.synthesize; the resulting audio chunks are emitted as oneDATA_BLOCK_START+ NDATA_BLOCK_DELTA+DATA_BLOCK_END. - Realtime TTS (
realtime=True): eachTextBlockDeltaEventis pushed into the model via :meth:TTSModelBase.push; any audio produced is emitted immediately. OnTextBlockEndEvent:meth:TTSModelBase.synthesizeis called to drain remaining audio, and the data block is closed.
Each DataBlockDeltaEvent.data carries an incremental base64 PCM
chunk; the full audio is the concatenation of every delta's decoded
bytes (the data block is keyed by block_id).
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(tts_model: TTSModelBase) -> None | 初始化 `TTSMiddleware` 实例。 Initialize the TTS middleware. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGeneratorasync | 拦截并扩展回复流程。 Intercept the reply stream, synthesize speech for text blocks, and inject ``DATA_BLOCK_*`` audio events into the output. |
构造器详情Constructor Detail
TTSMiddleware
def __init__(tts_model: TTSModelBase) -> None
初始化 TTSMiddleware 实例。
Initialize the TTS middleware.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
tts_model | TTSModelBase | TTS 模型(类型 `TTSModelBase`)。The TTS model used to synthesize speech for assistant text blocks produced during reasoning. |
源码Source
agentscope/middleware/_tts_middleware.py:41–49方法详情Method Detail
on_reply
async def on_reply(agent: 'Agent', input_kwargs: dict, next_handler: Callable[..., AsyncGenerator]) -> AsyncGenerator
async
拦截并扩展回复流程。
Intercept the reply stream, synthesize speech for text blocks, and inject DATA_BLOCK_* audio events into the output.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
agent | 'Agent' | 智能体(类型 `'Agent'`)。The agent. |
input_kwargs | dict | 输入关键字参数(类型 `dict`)。The input kwargs. |
next_handler | Callable[..., AsyncGenerator] | 下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler. |
AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source
agentscope/middleware/_tts_middleware.py:51–117