feat: add session-scoped client tool waits
This commit is contained in:
@@ -533,7 +533,7 @@ class WorkflowBrain(BaseBrain):
|
||||
cancel_on_interruption=policy.cancel_on_interruption,
|
||||
timeout_secs=(
|
||||
float(((tool.definition or {}).get("config") or {}).get("timeout_seconds") or 3)
|
||||
if tool.type == "client"
|
||||
if tool.type == "client" and policy.response_wait_mode == "timeout"
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Any, Protocol
|
||||
from typing import Any, Literal, Protocol
|
||||
|
||||
from loguru import logger
|
||||
from pipecat.frames.frames import (
|
||||
CancelFrame,
|
||||
EndFrame,
|
||||
InputTransportMessageFrame,
|
||||
OutputTransportMessageUrgentFrame,
|
||||
StopFrame,
|
||||
)
|
||||
from pipecat.processors.frame_processor import FrameDirection, FrameProcessor
|
||||
|
||||
@@ -18,6 +20,9 @@ class ClientToolError(RuntimeError):
|
||||
"""Raised when a client tool cannot be delivered or completed."""
|
||||
|
||||
|
||||
ClientToolResponseWaitMode = Literal["timeout", "session"]
|
||||
|
||||
|
||||
class ClientToolPort(Protocol):
|
||||
async def call(
|
||||
self,
|
||||
@@ -26,6 +31,7 @@ class ClientToolPort(Protocol):
|
||||
*,
|
||||
timeout_seconds: float,
|
||||
wait_for_response: bool = True,
|
||||
response_wait_mode: ClientToolResponseWaitMode = "timeout",
|
||||
) -> dict[str, Any]: ...
|
||||
|
||||
|
||||
@@ -35,6 +41,7 @@ class ClientToolBroker(FrameProcessor):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._pending: dict[str, asyncio.Future[dict[str, Any]]] = {}
|
||||
self._closed_message: str | None = None
|
||||
|
||||
async def call(
|
||||
self,
|
||||
@@ -43,9 +50,13 @@ class ClientToolBroker(FrameProcessor):
|
||||
*,
|
||||
timeout_seconds: float,
|
||||
wait_for_response: bool = True,
|
||||
response_wait_mode: ClientToolResponseWaitMode = "timeout",
|
||||
) -> dict[str, Any]:
|
||||
from uuid import uuid4
|
||||
|
||||
if self._closed_message is not None:
|
||||
raise ClientToolError(self._closed_message)
|
||||
|
||||
tool_call_id = f"client_{uuid4().hex}"
|
||||
message = {
|
||||
"type": "client-tool-call",
|
||||
@@ -75,6 +86,8 @@ class ClientToolBroker(FrameProcessor):
|
||||
await self.push_frame(
|
||||
OutputTransportMessageUrgentFrame(message=message)
|
||||
)
|
||||
if response_wait_mode == "session":
|
||||
return await future
|
||||
return await asyncio.wait_for(future, timeout=timeout_seconds)
|
||||
except TimeoutError as exc:
|
||||
raise ClientToolError(f"客户端工具调用超时: {function_name}") from exc
|
||||
@@ -88,8 +101,17 @@ class ClientToolBroker(FrameProcessor):
|
||||
async def process_frame(self, frame, direction: FrameDirection):
|
||||
await super().process_frame(frame, direction)
|
||||
|
||||
if isinstance(frame, EndFrame):
|
||||
self._fail_pending("会话已结束")
|
||||
if isinstance(frame, (EndFrame, CancelFrame)):
|
||||
self._close(
|
||||
"会话已结束" if isinstance(frame, EndFrame) else "会话已取消"
|
||||
)
|
||||
await self.push_frame(frame, direction)
|
||||
return
|
||||
|
||||
if isinstance(frame, StopFrame):
|
||||
# StopFrame keeps processors reusable, so release only the calls
|
||||
# owned by the stopped run without permanently closing the broker.
|
||||
self._fail_pending("管线已停止")
|
||||
await self.push_frame(frame, direction)
|
||||
return
|
||||
|
||||
@@ -131,6 +153,12 @@ class ClientToolBroker(FrameProcessor):
|
||||
future.set_exception(ClientToolError(message))
|
||||
self._pending.clear()
|
||||
|
||||
def _close(self, message: str) -> None:
|
||||
"""Close once and reject both current and future client-tool calls."""
|
||||
if self._closed_message is None:
|
||||
self._closed_message = message
|
||||
self._fail_pending(self._closed_message)
|
||||
|
||||
async def cleanup(self) -> None:
|
||||
self._fail_pending("客户端工具通道已关闭")
|
||||
self._close("客户端工具通道已关闭")
|
||||
await super().cleanup()
|
||||
|
||||
@@ -213,6 +213,7 @@ class ToolExecutor:
|
||||
arguments,
|
||||
timeout_seconds=float(config.get("timeout_seconds") or 3),
|
||||
wait_for_response=policy.wait_for_response,
|
||||
response_wait_mode=policy.response_wait_mode,
|
||||
)
|
||||
except ClientToolError as exc:
|
||||
raise ToolExecutionError(str(exc)) from exc
|
||||
|
||||
@@ -19,6 +19,7 @@ class ToolRuntimePolicy:
|
||||
allow_interruptions: bool
|
||||
execution_mode: str
|
||||
wait_for_response: bool
|
||||
response_wait_mode: str
|
||||
|
||||
@property
|
||||
def cancel_on_interruption(self) -> bool:
|
||||
@@ -38,8 +39,12 @@ def policy_for_tool(tool: RuntimeTool) -> ToolRuntimePolicy:
|
||||
mode = str(config.get("execution_mode") or default_mode)
|
||||
if mode not in {"immediate", "async"}:
|
||||
mode = default_mode
|
||||
response_wait_mode = str(config.get("response_wait_mode") or "timeout")
|
||||
if response_wait_mode not in {"timeout", "session"}:
|
||||
response_wait_mode = "timeout"
|
||||
return ToolRuntimePolicy(
|
||||
allow_interruptions=bool(config.get("allow_interruptions", True)),
|
||||
execution_mode=mode,
|
||||
wait_for_response=bool(config.get("wait_for_response", True)),
|
||||
response_wait_mode=response_wait_mode,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user