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

@@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
from dataclasses import dataclass
from typing import Any, Literal, Protocol
from loguru import logger
@@ -23,6 +24,12 @@ class ClientToolError(RuntimeError):
ClientToolResponseWaitMode = Literal["timeout", "session"]
@dataclass(frozen=True)
class _PendingClientToolCall:
future: asyncio.Future[dict[str, Any]]
interrupt_on_result: bool = False
class ClientToolPort(Protocol):
async def call(
self,
@@ -32,6 +39,7 @@ class ClientToolPort(Protocol):
timeout_seconds: float,
wait_for_response: bool = True,
response_wait_mode: ClientToolResponseWaitMode = "timeout",
interrupt_on_result: bool = False,
) -> dict[str, Any]: ...
@@ -40,7 +48,7 @@ class ClientToolBroker(FrameProcessor):
def __init__(self) -> None:
super().__init__()
self._pending: dict[str, asyncio.Future[dict[str, Any]]] = {}
self._pending: dict[str, _PendingClientToolCall] = {}
self._closed_message: str | None = None
async def call(
@@ -51,6 +59,7 @@ class ClientToolBroker(FrameProcessor):
timeout_seconds: float,
wait_for_response: bool = True,
response_wait_mode: ClientToolResponseWaitMode = "timeout",
interrupt_on_result: bool = False,
) -> dict[str, Any]:
from uuid import uuid4
@@ -81,7 +90,10 @@ class ClientToolBroker(FrameProcessor):
loop = asyncio.get_running_loop()
future: asyncio.Future[dict[str, Any]] = loop.create_future()
self._pending[tool_call_id] = future
self._pending[tool_call_id] = _PendingClientToolCall(
future=future,
interrupt_on_result=interrupt_on_result,
)
try:
await self.push_frame(
OutputTransportMessageUrgentFrame(message=message)
@@ -125,12 +137,19 @@ class ClientToolBroker(FrameProcessor):
return
tool_call_id = str(message.get("tool_call_id") or "")
future = self._pending.get(tool_call_id)
if future is None or future.done():
pending = self._pending.get(tool_call_id)
if pending is None or pending.future.done():
logger.debug(f"忽略未知或过期的客户端工具结果: {tool_call_id}")
return
status = str(message.get("status") or "error")
# Keep interruption in the input-frame path, matching text input.
# Resolve the tool future only after the interruption has been
# broadcast, so the workflow cannot enqueue its next node first.
if status == "ok" and pending.interrupt_on_result:
await self.broadcast_interruption()
future = pending.future
if status == "ok":
future.set_result(
{
@@ -148,7 +167,8 @@ class ClientToolBroker(FrameProcessor):
)
def _fail_pending(self, message: str) -> None:
for future in self._pending.values():
for pending in self._pending.values():
future = pending.future
if not future.done():
future.set_exception(ClientToolError(message))
self._pending.clear()