feat: enhance call end coordination with state management
- Added support for tracking call end states (pending, speaking, cancelled) in CallEndCoordinator. - Introduced a callback mechanism for state changes to allow external handling of call end states. - Updated pipeline and tests to accommodate new state management features, ensuring proper handling of call end scenarios.
This commit is contained in:
@@ -63,11 +63,19 @@ def playback_marker_for(
|
||||
return FixedSpeechPlaybackMarkerFrame(completion=completion)
|
||||
|
||||
|
||||
CallEndStateHandler = Callable[[str, bool], Awaitable[None]]
|
||||
|
||||
|
||||
class CallEndCoordinator:
|
||||
"""Coordinate committed and user-cancellable conversation endings."""
|
||||
|
||||
def __init__(self, queue_end: Callable[[str], Awaitable[None]]):
|
||||
def __init__(
|
||||
self,
|
||||
queue_end: Callable[[str], Awaitable[None]],
|
||||
on_state_change: CallEndStateHandler | None = None,
|
||||
):
|
||||
self._queue_end = queue_end
|
||||
self._on_state_change = on_state_change
|
||||
self._ending = False
|
||||
self._interruptible = False
|
||||
self._armed = False
|
||||
@@ -110,6 +118,7 @@ class CallEndCoordinator:
|
||||
self._armed = False
|
||||
self._finish_after_tracked_speech = False
|
||||
self._reason = reason or "completed"
|
||||
self._schedule_state("speaking" if self._speaking else "pending")
|
||||
|
||||
def cancel_pending_end(self) -> bool:
|
||||
"""Cancel one pending ending when its tool explicitly allows it."""
|
||||
@@ -123,8 +132,13 @@ class CallEndCoordinator:
|
||||
# Transport speech state remains authoritative: if audio is currently
|
||||
# playing, the interruption path will still emit BotStoppedSpeakingFrame.
|
||||
logger.info("用户打断结束语,已撤销待挂断状态")
|
||||
self._schedule_state("cancelled")
|
||||
return True
|
||||
|
||||
def _schedule_state(self, state: str) -> None:
|
||||
if self._on_state_change is not None:
|
||||
asyncio.create_task(self._on_state_change(state, self.interruptible))
|
||||
|
||||
def begin_response(self) -> None:
|
||||
"""Start tracking speech produced by one LLM response."""
|
||||
self._response_speech_started = False
|
||||
@@ -200,6 +214,8 @@ class CallEndCoordinator:
|
||||
self._speaking = True
|
||||
self._speech_stopped.clear()
|
||||
self._response_speech_started = True
|
||||
if self._ending:
|
||||
await self._emit_state("speaking")
|
||||
elif isinstance(frame, BotStoppedSpeakingFrame) and self._speaking:
|
||||
self._speaking = False
|
||||
self._speech_stopped.set()
|
||||
@@ -207,6 +223,10 @@ class CallEndCoordinator:
|
||||
logger.info("结束语播报完毕,挂断通话")
|
||||
await self.finish()
|
||||
|
||||
async def _emit_state(self, state: str) -> None:
|
||||
if self._on_state_change is not None:
|
||||
await self._on_state_change(state, self.interruptible)
|
||||
|
||||
|
||||
class EndCallAfterSpeechProcessor(FrameProcessor):
|
||||
def __init__(self, coordinator: CallEndCoordinator):
|
||||
|
||||
Reference in New Issue
Block a user