AS AgentScopeAPI 手册 / API Reference
类class agentscope.middleware

TTSMiddleware

class TTSMiddleware(MiddlewareBase)
源码Source agentscope/middleware/_tts_middleware.py:21–179

TTS 中间件,在智能体执行流程中提供横切扩展。位于 agentscope.middleware 包,继承自 MiddlewareBase。核心成员包括 on_reply。

  • Non-realtime TTS (realtime=False): on each TextBlockEndEvent the accumulated text is sent to :meth:TTSModelBase.synthesize; the resulting audio chunks are emitted as one DATA_BLOCK_START + N DATA_BLOCK_DELTA + DATA_BLOCK_END.
  • Realtime TTS (realtime=True): each TextBlockDeltaEvent is pushed into the model via :meth:TTSModelBase.push; any audio produced is emitted immediately. On TextBlockEndEvent :meth:TTSModelBase.synthesize is 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_modelTTSModelBaseTTS 模型(类型 `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_kwargsdict输入关键字参数(类型 `dict`)。The input kwargs.
next_handlerCallable[..., AsyncGenerator]下一个处理器(类型 `Callable[..., AsyncGenerator]`)。The next handler.
返回Returns AsyncGenerator
返回AsyncGenerator。Returns async generator.
源码Source agentscope/middleware/_tts_middleware.py:51–117

继承成员Inherited Members

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.