Make end call tool can be cancelled

This commit is contained in:
Xin Wang
2026-08-12 09:18:40 +08:00
parent fc026e765e
commit d0bb5b3868
15 changed files with 350 additions and 48 deletions

View File

@@ -235,6 +235,7 @@ class WorkflowRealtimeController:
async def on_user_speech_started(self) -> None:
"""Let an interruptible Message finish its deterministic continuation."""
self._runtime.call_end.cancel_pending_end()
current = self._state.current_node_id
if (
self._engine.node_type(current) != "message"
@@ -263,11 +264,12 @@ class WorkflowRealtimeController:
self._state.consume_user_turn()
if self._engine.node_type(self._state.current_node_id) == "agent":
self._state.status = WorkflowStatus.WAITING_USER
if self._waiting_generated_end and self._runtime.call_end.ending:
if self._waiting_generated_end:
self._waiting_generated_end = False
await self._runtime.call_end.finish_after_current_speech(
has_text=bool(content.strip()) and not interrupted
)
if self._runtime.call_end.ending:
await self._runtime.call_end.finish_after_current_speech(
has_text=bool(content.strip()) and not interrupted
)
async def dispatch_tool(
self,
@@ -759,10 +761,18 @@ class WorkflowRealtimeController:
request_handoff,
)
if kind == "end_conversation":
config = (tool.definition or {}).get("config") or {}
allow_end_interruption = bool(
config.get("allow_end_interruption", False)
)
async def end_conversation(arguments: dict[str, Any]) -> RealtimeToolResult:
reason = str(arguments.get("reason") or "end_conversation")
self._waiting_generated_end = True
self._runtime.call_end.begin(reason)
self._runtime.call_end.begin(
reason,
interruptible=allow_end_interruption,
)
return RealtimeToolResult(
{"status": "success", "action": "ending_call"}
)