fix: interrupt message output before tool result
This commit is contained in:
@@ -19,6 +19,8 @@ class CallEndCoordinator:
|
||||
self._ending = False
|
||||
self._armed = False
|
||||
self._speaking = False
|
||||
self._speech_stopped = asyncio.Event()
|
||||
self._speech_stopped.set()
|
||||
self._response_speech_started = False
|
||||
self._tracked_speeches = 0
|
||||
self._tracked_speech_completions: deque[asyncio.Future[None]] = deque()
|
||||
@@ -30,6 +32,15 @@ class CallEndCoordinator:
|
||||
def ending(self) -> bool:
|
||||
return self._ending
|
||||
|
||||
@property
|
||||
def speaking(self) -> bool:
|
||||
"""Whether transport output is currently producing bot speech."""
|
||||
return self._speaking
|
||||
|
||||
async def wait_until_silent(self) -> None:
|
||||
"""Wait for the transport-owned bot speech boundary."""
|
||||
await self._speech_stopped.wait()
|
||||
|
||||
def begin(self, reason: str) -> None:
|
||||
self._ending = True
|
||||
self._reason = reason or "completed"
|
||||
@@ -74,9 +85,11 @@ class CallEndCoordinator:
|
||||
async def observe(self, frame) -> None:
|
||||
if isinstance(frame, BotStartedSpeakingFrame):
|
||||
self._speaking = True
|
||||
self._speech_stopped.clear()
|
||||
self._response_speech_started = True
|
||||
elif isinstance(frame, BotStoppedSpeakingFrame) and self._speaking:
|
||||
self._speaking = False
|
||||
self._speech_stopped.set()
|
||||
if self._tracked_speeches > 0:
|
||||
self._tracked_speeches -= 1
|
||||
completion = self._tracked_speech_completions.popleft()
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user