fix: interrupt message output before tool result

This commit is contained in:
Xin Wang
2026-08-04 09:30:52 +08:00
parent d068927b53
commit a16ecd8e01
10 changed files with 152 additions and 37 deletions

View File

@@ -8,6 +8,7 @@
import asyncio
import base64
from collections.abc import Awaitable, Callable
from io import BytesIO
from typing import Any
@@ -106,13 +107,19 @@ ON_DEMAND_KNOWLEDGE_SYSTEM_HINT = (
)
async def _interrupt_pipeline_output(
source: FrameProcessor,
async def _wait_for_interrupted_output(
worker: PipelineWorker,
*,
wait_until_stopped: Callable[[], Awaitable[None]] | None = None,
) -> None:
"""Broadcast an interruption and wait until it crosses the media pipeline."""
await source.broadcast_interruption()
await worker.flush_pipeline(timeout=1.0)
"""Wait until an in-band interruption crosses pipeline and playback."""
if not await worker.flush_pipeline(timeout=2.0):
raise RuntimeError("输出中断帧未能及时穿过媒体管线")
if wait_until_stopped is not None:
try:
await asyncio.wait_for(wait_until_stopped(), timeout=2.0)
except TimeoutError as exc:
raise RuntimeError("等待客户端语音停止超时") from exc
def _compact_knowledge_metadata(value: str, max_length: int) -> str:
@@ -744,9 +751,15 @@ async def run_pipeline(
current_enable_interrupt = enable_interrupt
current_turn_config = normalized
async def interrupt_output() -> None:
"""Stop active output through the same boundary as text/voice input."""
await _interrupt_pipeline_output(user_input, worker)
async def wait_for_output_stopped() -> None:
"""Keep workflow continuation behind the interrupted output."""
wait_until_stopped = (
call_end.wait_until_silent if call_end.speaking else None
)
await _wait_for_interrupted_output(
worker,
wait_until_stopped=wait_until_stopped,
)
def set_system_prompt(text: str) -> None:
@@ -781,7 +794,7 @@ async def run_pipeline(
set_vision_scope=lambda scope: workflow_vision_scope.update(scope),
vision_function=workflow_vision_function,
set_input_enabled=lambda enabled: input_state.__setitem__("enabled", enabled),
interrupt_output=interrupt_output,
wait_for_output_stopped=wait_for_output_stopped,
apply_turn_config=apply_workflow_turn_config,
flow_global_functions=flow_global_functions,
),