Files
ai-video-fullstack/backend/services/tool_policy.py
T

42 lines
1.3 KiB
Python
Raw Normal View History

"""Runtime behavior shared by HTTP, MCP, and client tools."""
from __future__ import annotations
from dataclasses import dataclass
from models import RuntimeTool
@dataclass(frozen=True)
class ToolRuntimePolicy:
"""Normalized interaction policy for one reusable tool."""
allow_interruptions: bool
execution_mode: str
wait_for_response: bool
2026-07-31 23:53:47 +08:00
response_wait_mode: str
@property
def cancel_on_interruption(self) -> bool:
"""Pipecat uses this flag to distinguish waiting from async tools."""
return self.execution_mode == "immediate"
@property
def runs_llm_after_result(self) -> bool:
"""Late async results update context without starting another reply."""
return self.execution_mode == "immediate"
def policy_for_tool(tool: RuntimeTool) -> ToolRuntimePolicy:
"""Read the validated policy stored on a current tool resource."""
config = tool.definition["config"]
is_client = tool.type == "client"
return ToolRuntimePolicy(
allow_interruptions=bool(config["allow_interruptions"]),
execution_mode=str(config["execution_mode"]),
wait_for_response=(bool(config["wait_for_response"]) if is_client else True),
response_wait_mode=(
str(config["response_wait_mode"]) if is_client else "timeout"
),
)