FireRedChat语音Agent教程 · 05-打断机制与会话控制API

05 - 打断(barge-in)机制与会话控制 API

所属系列:FireRedChat语音Agent教程 | 前置:04-AgentSession会话主循环与轮次检测 | 下一篇:06-音频输入链路与pVAD
证据基线:主仓 commit 9d8c4b99,agents@3f476b9。路径约定:agents/… = agents/livekit-agents/livekit/agents/。全部断言 Observed in source。

本篇知识图谱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%%{init: {'theme':'base','themeVariables':{'primaryColor':'#eef2ff','primaryTextColor':'#1b1b1f','primaryBorderColor':'#6366f1','lineColor':'#6366f1','textColor':'#1b1b1f','fontSize':'15px'}}}%%
mindmap
root((打断与会话控制))
三类触发
VAD INFERENCE_DONE 加时长门槛
interim 转写非空
final 转写
min_interruption_words 防误触
两种处置
真取消 interrupt
false_interruption 仅 pause
定时器到点 resume
用户再开口 取消定时器
取消闭环
SpeechHandle._cancel 置 future
wait_if_not_interrupted
FIRST_COMPLETED 竞争
播放侧清队列
会话控制面
say
generate_reply
interrupt
clear_user_turn
房间RPC new_conversation
抢先生成
转写未定型 先生成不入队
on_end_of_turn 命中复用
不等价 即取消

本篇目标

  1. 拆打断的触发面:三类触发条件与两道误触防线(时长门槛、词数门槛)。
  2. 区分两种处置:真取消(interrupt)与 false_interruption 的"pause + 定时恢复"。
  3. 追取消闭环:SpeechHandle._cancel 如何让 LLM/TTS/播放任务经 wait_if_not_interrupted 以 FIRST_COMPLETED 竞争退出。
  4. 盘点框架级会话控制面:公共方法、房间 RPC、抢先生成的创建与命中复用。

1. 打断全景

1
2
3
4
5
6
7
8
9
10
11
12
%%{init: {'theme':'base','themeVariables':{'primaryColor':'#eef2ff','primaryTextColor':'#1b1b1f','primaryBorderColor':'#6366f1','lineColor':'#6366f1','textColor':'#1b1b1f','fontSize':'15px'}}}%%
flowchart TD
A["VAD INFERENCE_DONE<br/>:1136-1142 时长达门槛"] --> C["_interrupt_by_audio_activity :1071"]
B["interim/final transcript 非空<br/>:1158-1159 / :1175-1177"] --> C
C -->|"min_interruption_words<br/>不足 :1087-1088"| X["直接返回"]
C --> D{"false_interruption<br/>use_pause? :1073"}
D -->|"是 且 can_pause"| E["output.audio.pause() :1105-1106<br/>播放暂停 未取消"]
E --> F["用户停说 → 定时器 :1129-1134<br/>到点 resume :2244"]
E -->|"用户再开口"| G["取消定时器 :1118-1121"]
D -->|"否"| H["current_speech.interrupt() :1111<br/>真取消"]
H --> I["SpeechHandle._cancel :158-165<br/>置 _interrupt_fut"]
I --> J["LLM/TTS/播放任务<br/>wait_if_not_interrupted :151-156<br/>FIRST_COMPLETED 竞争退出"]

2. 三类触发与两道防线(agent_activity.py:1071-1177)

打断入口是 _interrupt_by_audio_activity(agents/voice/agent_activity.py:1071-1111),三个 recognition hook 是它的触发源:

触发 行号 条件
VAD 判说 :1136-1142 on_vad_inference_done:ev.speech_duration >= min_interruption_duration(:1141)——说得够久才可能是插话
interim 转写 :1144-1159 on_interim_transcript:ev.alternatives[0].text 非空即触发(:1158-1159)——STT 一出中间字就打断
final 转写 :1161-1177 on_final_transcript:起 _interrupt_paused_speech task(:1175-1177),主要处理"暂停中的语音是否恢复/收尾"

两道防误触防线(都在 _interrupt_by_audio_activity 内):

  • 时长门槛:VAD 触发路径自带 min_interruption_duration 比较(:1141)——短促的"嗯""啊"达不到时长直接无效。
  • 词数门槛(:1079-1088):min_interruption_words > 0 时对 current_transcript 分词计数(split_words,:1087,注释自承多语言切分还比较粗糙),不足阈值直接 return——防止背景噪声转写成零星字符就掐掉回复。

此外还有一道资格检查:当前 speech 必须 allow_interruptions 且未被打断过(:1093-1097);Realtime 模型自带 turn detection 时整个打断逻辑跳过(:1075-1077)——那是模型自己管轮次。

3. false_interruption:pause 而非真取消

打断不一定要作废整段回复。_interrupt_by_audio_activity 里有个分岔(:1073、:1105-1111):

  • use_pause = resume_false_interruption and false_interruption_timeout is not None(:1073;两个选项默认 resume_false_interruption=True、false_interruption_timeout=2.0,agents/voice/agent_session.py:160-161)且输出通道 can_pause(:1105)。
  • pause 模式(:1105-1106):只调 self._session.output.audio.pause()——播放侧 RoomIOAudioOutput.pause() 清掉 _playback_enabled 事件(agents/voice/room_io/_output.py:127-130),缓冲与生成都不销毁,回复"冻"在半截。
  • 真打断模式(:1107-1111):self._current_speech.interrupt() 走第 4 节的取消闭环。

恢复逻辑(Observed,:1123-1134 + :2225-2246):用户停止说话时(on_end_of_speech)起一个恢复定时器(:1129-1134);定时器到点后若 _paused_speech 还是当前语音且未结束,就 audio_output.resume()(:2244)——之前被暂停的回复接着播,不用重新生成。用户在超时前又开口(on_start_of_speech)则取消定时器(:1118-1121),说明真是新插话,交由后续 final/EOU 流程处置。这是"误打断"的低成本兜底:宁可暂停两秒,也不要白扔一段已生成的 TTS。

4. 真取消闭环:_cancel 与 FIRST_COMPLETED 竞争

AgentActivity.interrupt(:852-887)是打断的总闸(供 session.interrupt() 与轮次完成逻辑调用):

  1. 先取消抢先生成(:859);2. 收掉背景语音(:863);3. 打断当前 speech(:865-867);4. 遍历堆里所有排队 speech 全部打断(:869-871);5. realtime 会话同步打断(:873-874);6. 返回的 future 在所有被打断 speech 收尾后完成(:876-885)。

单个 SpeechHandle 的取消机制(agents/voice/speech_handle.py:100-165):

  • interrupt()(:100-113):非 force 且 allow_interruptions=False 时抛 RuntimeError(打断资格检查的另一半);否则调 self._cancel()。
  • _cancel()(:158-165):核心就一行——给 self._interrupt_fut set_result(None),把"已打断"广播成一个 future 的完成事件。
  • wait_if_not_interrupted(aw)(:151-156):生成管线各环节把自己的任务列表传进来,它 asyncio.wait([asyncio.gather(*aw), self._interrupt_fut], return_when=FIRST_COMPLETED)——正常完成与被打断谁先到算谁的。LLM 流、TTS 合成、播放推进等任务都用它包裹自己的 await,_interrupt_fut 一完成就提前退出,正在路上的一次性工作(未发送的 LLM chunk、未合成的文本)随之丢弃。

播放侧的对应物在 room_io/_output.py:120-168:clear_buffer() 清字节流并置 _interrupted_event(:120-125);_wait_for_playout 用 asyncio.wait([播放任务, 打断事件], FIRST_COMPLETED)(:147-150)——被打断时把还在队列里的音频帧清掉、清 _audio_source 队列(:155-162),统计实际已播时长后上报(:166-168)。取消不是异常,是一套 FIRST_COMPLETED 竞争约定:所有环节永远在"做完"与"被取消"两个出口之一干净退出。

5. 会话控制面:公共方法与房间 RPC

5.1 AgentSession 公共方法(agent_session.py:739-883)

方法 行号 行为
say(text) :739-775 直接播一段文本/流;调度暂停期会转发给下一个 activity(:751-764)
generate_reply() :777-818 走 LLM 生成回复,可带 user_input/instructions/tool_choice
interrupt() :834 转 AgentActivity.interrupt(第 4 节总闸)
clear_user_turn() :846 清当前用户轮(转写缓存与 VAD 状态,audio_recognition.py:178 有对应实现)

5.2 房间 RPC:new_conversation 重置会话

worker 示例把控制面暴露到房间里(agents/examples/fireredchat_worker.py:143-147,证据线①):

1
2
3
4
5
@ctx.room.local_participant.register_rpc_method("new_conversation")
async def new_conversation(data: rtc.RpcInvocationData):
session.interrupt() # :145 打断当前语音
session.clear_user_turn() # :146 清用户轮
await session._agent.update_chat_ctx(ChatContext.empty()) # :147 清空对话上下文

WebUI 端发起 RPC 即得一个"重新开始"按钮语义。注意 :147 访问的是私有成员 _agent——示例代码亲自示范了框架的"内部通道",框架未提供公共封装(Observed)。

5.3 抢先生成:先跑起来等转写定型

EOU 模型推理要 1 秒(04 章),抢先生成把这段等待变成生成时间:

  • 创建(agent_activity.py:1179-1205):on_preemptive_generation hook 在转写尚未 final 时被调,条件检查后(:1180-1185:功能开关、调度未暂停、当前语音已打断、LLM 是标准 LLM),先 _cancel_preemptive_generation()(:1188),再 _generate_reply(..., schedule_speech=False)(:1197-1200)——生成但不入队,连同当时的 chat_ctx/tools/tool_choice 快照存进 _PreemptiveGeneration(:1201-1205)。
  • 命中复用(:1329-1350,04 章已引):轮次真正完成时,若转写文本、chat ctx、tools、tool_choice 与抢跑快照全等价(:1332-1337)就直接复用该 SpeechHandle 入队(:1338-1339);任何一个变了说明用户实际说的和抢跑时不同,抢跑件 _cancel 作废(:1344-1348)。
  • 一致性:AgentActivity.interrupt 第一步就是取消抢跑件(:859)——抢跑件永远不会跟正式回复共存播报。

本篇小结

  • 触发三通道:VAD 时长门槛(:1141)、interim 非空(:1158-1159)、final(:1175-1177);词数门槛 min_interruption_words 在入口统一拦截(:1087-1088)。
  • 两种处置:false_interruption 走 output.audio.pause() + 定时 resume()(:1105-1106、:2244,默认超时 2.0s agent_session.py:160);真打断走 SpeechHandle._cancel 置 future(speech_handle.py:158-165)。
  • 取消闭环是一套 FIRST_COMPLETED 竞争约定:wait_if_not_interrupted(:151-156)让 LLM/TTS/播放任务在"完成"与"取消"两个出口干净退出;播放侧配 clear_buffer + 清音频队列(_output.py:120-168)。
  • 控制面四方法(say/generate_reply/interrupt/clear_user_turn,agent_session.py:739-883)+ 房间 RPC new_conversation 三连(interrupt/clear_user_turn/清上下文,fireredchat_worker.py:143-147)。
  • 抢先生成:schedule_speech=False 先生成不入队(:1197-1205),轮次定型后全等价才复用(:1332-1339),否则 _cancel(:1348)。
  • 下一章回到音频本身:RoomIO 分帧、单 VAD 改造与 pVAD 插件。

前置阅读:04-AgentSession会话主循环与轮次检测 | 下一篇:06-音频输入链路与pVAD | 回目录:README