Pipecat语音Agent教程 · 05-Turn状态机与打断机制
05 - Turn状态机与打断机制
所属系列:Pipecat语音Agent教程 | 前置:04-Pipeline与帧流核心机制 | 下一篇:06-音频底层与VAD降噪
证据基线:commit1995655。主战场:src/pipecat/turns/、src/pipecat/processors/aggregators/llm_response_universal.py、src/pipecat/processors/frame_processor.py、src/pipecat/transports/base_output.py。本章是会话管理的灵魂:"谁在说话"与"该谁说"的一切判定都在这里。
本篇知识图谱
1 | %%{init: {'theme':'base','themeVariables':{'primaryColor':'#eef2ff','primaryTextColor':'#1b1b1f','primaryBorderColor':'#6366f1','lineColor':'#6366f1','textColor':'#1b1b1f','fontSize':'15px'}}}%% |
本篇目标
- 认识
LLMUserAggregator内嵌的三控制器与可插拔 start/stop 策略族。 - 走通打断全链路:turn 开始 → 双向
InterruptionFrame→ 处理器取消重建 → 输出端只排空可打断音频。 - 理解 RTVI 客户端主动打断与 VAD 自动打断是同一条链。
- 掌握 turn 收尾:聚合落盘触发 LLM、投机推理的临时副本与撤销。
1. turn 状态机的家:LLMUserAggregator 与三控制器
第 1 章埋过伏笔:VAD 挂载点已迁入 LLMUserAggregatorParams(llm_response_universal.py:212)。聚合器拿到 vad_analyzer 后创建 VADController(llm_response_universal.py:879-881)。三个控制器各司其职(ls src/pipecat/turns + 类定义实查):
| 控制器 | 定义 | 职责 |
|---|---|---|
UserTurnController |
src/pipecat/turns/user_turn_controller.py:59 |
turn 状态机的策略引擎:消费策略事件、广播 turn 生命周期 |
VADController |
src/pipecat/audio/vad/vad_controller.py:30 |
包住 vad_analyzer,把语音活动变成 VAD 系帧/事件(第 6 章放大) |
UserIdleController |
src/pipecat/turns/user_idle_controller.py:31 |
用户空闲/等待语义(:1567 的 waiting_for_user 就来自它) |
聚合器是总装车间:它在 start/stop 策略之上挂自己的事件处理器——on_user_turn_started → self._on_user_turn_started、on_user_turn_inference_triggered → self._on_user_turn_inference_triggered、on_user_turn_stopped → self._on_user_turn_stopped(llm_response_universal.py:856-862)。策略只管"判定",聚合器管"后果"。
2. 可插拔策略族:start/stop 两头各一组
- 策略组合:
UserTurnStrategies(src/pipecat/turns/user_turn_strategies.py:104)聚合 start 列表与 stop 列表,预设四种组合——ExternalUserTurnStrategies(:188)、FilterIncompleteUserTurnStrategies(:219)、ClassifierUserTurnStrategies(:283)、EagerUserTurnStrategies(:350,投机推理的主角)。 - start 策略(
src/pipecat/turns/user_start/实查 6 个):external、krisp_viva_ip、min_words、transcription、vad、wake_phrase。 - stop 策略(
src/pipecat/turns/user_stop/实查 8 个):classifier、deferred、eager、external、external_user_turn_completion、llm_turn_completion、speech_timeout、turn_analyzer。
控制器的帧分发(user_turn_controller.py:278-323):process_frame 把帧喂给策略前先按四类分派——UserStarted/StoppedSpeakingFrame(含 Proposed 变体,:289-292)、VADUserStarted/StoppedSpeakingFrame(:293-296)、TranscriptionFrame/InterimTranscriptionFrame(:297-298),再依次跑 start 策略列表(:300-308)与 stop 策略列表(:310-318),UserTurnResponseCancelFrame 最后补一刀(:322-323)。
策略接线与保险(user_turn_controller.py:331-354):setup 时给每个 start 策略挂 on_user_turn_started 等四个事件钩子(:335-340),给每个 stop 策略挂 on_user_turn_inference_triggered/on_user_turn_speculation_cancelled/on_user_turn_stopped(:342-352)。若所有 stop 策略都"等推理"(waits_for_inference)而没有真正听用户停没停的,启动即告警"turn 永远只能靠 stop 超时结束"(:356-364)——策略配置错误在 setup 期就暴露。
3. 打断全链路(上):turn 开始即广播
用户 turn 一开始(_on_user_turn_started,llm_response_universal.py:1552-1578):
1 | self._user_turn_interrupted_bot = ( |
三个细节:打断不是独立消息,而是 turn 开始的副作用;waiting_for_user(bot 正等着用户说)时不打断——bot 刚问完"您叫什么"用户开口回答,这不算打断;开关在 enable_interruptions 参数。
broadcast_interruption(frame_processor.py:1048-1053)三连:重置本处理器的 process 任务、停掉所有指标计时、broadcast_frame(InterruptionFrame)。而 broadcast_frame(frame_processor.py:1069-1085)创建两个实例(上游一个、下游一个,互记 broadcast_sibling_id,:1080-1083)双向推——这就是"上/下行 InterruptionFrame"的出处。
InterruptionFrame 是 SystemFrame(frames.py:1506-1514)——结合第 4 章:SystemFrame 直达 input task(frame_processor.py:1369-1370),打断帧天然越过所有排队中的数据帧。
4. 打断全链路(下):处理器侧与输出侧
4.1 处理器侧:取消并重建 process 任务(frame_processor.py:1161-1180)
_start_interruption 收到 InterruptionFrame 后:
- 当前帧不可打断(
frame.interruptible == False):只排空队列(__reset_process_queue,:1165-1171),等它跑完; - 当前帧可打断:直接取消并重建 process 任务(
:1179-1180)——注释解释了为什么敢这么做:重建内部的__create_process_task会调__reset_process_queue,队列里的不可打断帧总是被保留(:1173-1178)。
所以"保留不可打断帧"有两层:正在跑的靠分支判断,排着队的靠重建时的队列过滤。
4.2 输出侧:MediaSender 只排空可打断音频(base_output.py:609-641)
输出端 MediaSender(base_output.py:430)的 handle_interruptions 是音频层的关键:
1 | # :615-620 注释原文:Keep the audio task running but drain all interruptible |
两个讲究:音频任务不重启只排空——重启会让 mixer 的背景音(保活音)出现可闻间隙(:615-620 注释点名电话线 serializer 还会清 playout buffer,雪上加霜);"bot 停止说话"事件推迟到不可打断音频也播完后才发(:637-641)。
5. RTVI 客户端主动打断:同一链路
客户端主动要求打断(RTVI 协议,第 8 章细讲)走 RTVIProcessor.interrupt_bot(src/pipecat/processors/frameworks/rtvi/processor.py:150-152):
1 | async def interrupt_bot(self): |
docstring 一句话挑明:与自动打断完全同链。调用点全是客户端消息路径——_handle_send_text 里 opts.run_immediately 先打断再追加新消息(processor.py:486-495),另一处 run_immediately 的上下文追加同理(:581)。VAD 打断与用户点击打断,最终都是同一次 broadcast_interruption。
6. turn 收尾与投机推理
6.1 正常收尾:push_aggregation 触发 LLM(llm_response_universal.py:1580-1623)
stop 策略判定 turn 该结束推理时触发 _on_user_turn_inference_triggered:
- realtime 模式只发事件、不推上下文(
:1586-1596,上下文写入改由助手响应开始驱动); - 投机分支(
:1598-1604)走 6.2; - 正常路径(
:1606-1623):push_aggregation()把聚合的用户话段写进 context 并发出LLMContextFrame(:1608-1614注释原话:“this writes the user message segment to the context and emits LLMContextFrame, which kicks LLM inference”);同 turn 多次推理的段累积进_full_user_turn_aggregation(:1616-1621),保证on_user_turn_stopped时上下文不丢段。
6.2 投机推理:临时 context 副本(llm_response_universal.py:1625-1673)
_run_speculative_inference(:1625-1673,第 1 批走查时已核实):turn 还没结束就提前跑推理,推的是临时副本,不碰真上下文(:1628-1632 docstring:“nothing here mutates the real context”)——
1 | provisional = LLMContext( |
响应被下游的 SpeculationGate 扣着,直到 turn 确认结束(UserStoppedSpeakingFrame 的 docstring 点名:frames.py:1533-1536)才放行。
6.3 撤销:UserTurnResponseCancelFrame(frames.py:1543-1558)
eager 端点判定错了(用户其实还在说,或提交的转写与 eager 版不一致)→ 发 UserTurnResponseCancelFrame。docstring 关键设计:LLM 服务停止生成,gate 丢弃扣住的一切——“nothing further down the pipeline has anything to undo”(:1550-1551),因为响应从未放行,下游无需回滚;已被 gate 放行的响应不归它管,那要靠 InterruptionFrame(:1552-1553)。它是 SystemFrame——“so it overtakes the speculative output it cancels”(:1555,第 4 章的优先队列机制在这里兑现)。控制器在帧分发最后消费它(user_turn_controller.py:322-323)。
本篇小结
- 三控制器:UserTurnController(策略引擎,
user_turn_controller.py:59)+ VADController(vad_controller.py:30)+ UserIdleController(user_idle_controller.py:31),聚合器在:856-862接线。 - 可插拔策略:start 6 种 / stop 8 种,预设四种组合(
user_turn_strategies.py:104-350);帧分发四类(user_turn_controller.py:278-323);配置错误 setup 期即告警(:356-364)。 - 打断链:turn 开始即
broadcast_interruption(llm_response_universal.py:1575-1576)→ 双实例双向 InterruptionFrame(frame_processor.py:1048-1053,1069-1085)→ 处理器取消重建 process 任务且保留不可打断帧(:1161-1180)→ MediaSender 只排空可打断音频、mixer 背景音不中断(base_output.py:609-641)。 - RTVI 主动打断:
interrupt_bot即broadcast_interruption(rtvi/processor.py:150-152)——自动与手动同一条链。 - 收尾:
push_aggregation写上下文 + LLMContextFrame 触发 LLM(llm_response_universal.py:1608-1614);投机推理跑临时副本(:1662-1671);撤销帧是 SystemFrame、无需下游回滚(frames.py:1543-1558)。 - 打断链里的"可打断音频"如何被切分与排队,正是下一章音频底层的第一站——06-音频底层与VAD降噪。
前置:04-Pipeline与帧流核心机制 | 下一篇:06-音频底层与VAD降噪 | 回目录:README