|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from collections.abc import Awaitable, Callable, Sequence |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +from ..helpers import text_block, tool_content |
| 7 | +from ..schema import PermissionOption, RequestPermissionRequest, RequestPermissionResponse, ToolCall |
| 8 | +from .tool_calls import ToolCallTracker, _copy_model_list |
| 9 | + |
| 10 | + |
| 11 | +class PermissionBrokerError(ValueError): |
| 12 | + """Base error for permission broker misconfiguration.""" |
| 13 | + |
| 14 | + |
| 15 | +class MissingToolCallError(PermissionBrokerError): |
| 16 | + """Raised when a permission request is missing the referenced tool call.""" |
| 17 | + |
| 18 | + def __init__(self) -> None: |
| 19 | + super().__init__("tool_call must be provided when no ToolCallTracker is configured") |
| 20 | + |
| 21 | + |
| 22 | +class MissingPermissionOptionsError(PermissionBrokerError): |
| 23 | + """Raised when no permission options are available for a request.""" |
| 24 | + |
| 25 | + def __init__(self) -> None: |
| 26 | + super().__init__("PermissionBroker requires at least one permission option") |
| 27 | + |
| 28 | + |
| 29 | +def default_permission_options() -> tuple[PermissionOption, PermissionOption, PermissionOption]: |
| 30 | + """Return a standard approval/reject option set.""" |
| 31 | + return ( |
| 32 | + PermissionOption(optionId="approve", name="Approve", kind="allow_once"), |
| 33 | + PermissionOption(optionId="approve_for_session", name="Approve for session", kind="allow_always"), |
| 34 | + PermissionOption(optionId="reject", name="Reject", kind="reject_once"), |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +class PermissionBroker: |
| 39 | + """Helper for issuing permission requests tied to tracked tool calls.""" |
| 40 | + |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + session_id: str, |
| 44 | + requester: Callable[[RequestPermissionRequest], Awaitable[RequestPermissionResponse]], |
| 45 | + *, |
| 46 | + tracker: ToolCallTracker | None = None, |
| 47 | + default_options: Sequence[PermissionOption] | None = None, |
| 48 | + ) -> None: |
| 49 | + self._session_id = session_id |
| 50 | + self._requester = requester |
| 51 | + self._tracker = tracker |
| 52 | + self._default_options = tuple( |
| 53 | + option.model_copy(deep=True) for option in (default_options or default_permission_options()) |
| 54 | + ) |
| 55 | + |
| 56 | + async def request_for( |
| 57 | + self, |
| 58 | + external_id: str, |
| 59 | + *, |
| 60 | + description: str | None = None, |
| 61 | + options: Sequence[PermissionOption] | None = None, |
| 62 | + content: Sequence[Any] | None = None, |
| 63 | + tool_call: ToolCall | None = None, |
| 64 | + ) -> RequestPermissionResponse: |
| 65 | + """Request user approval for a tool call.""" |
| 66 | + if tool_call is None: |
| 67 | + if self._tracker is None: |
| 68 | + raise MissingToolCallError() |
| 69 | + tool_call = self._tracker.tool_call_model(external_id) |
| 70 | + else: |
| 71 | + tool_call = tool_call.model_copy(deep=True) |
| 72 | + |
| 73 | + if content is not None: |
| 74 | + tool_call.content = _copy_model_list(content) |
| 75 | + |
| 76 | + if description: |
| 77 | + existing = tool_call.content or [] |
| 78 | + existing.append(tool_content(text_block(description))) |
| 79 | + tool_call.content = existing |
| 80 | + |
| 81 | + option_set = tuple(option.model_copy(deep=True) for option in (options or self._default_options)) |
| 82 | + if not option_set: |
| 83 | + raise MissingPermissionOptionsError() |
| 84 | + |
| 85 | + request = RequestPermissionRequest( |
| 86 | + sessionId=self._session_id, |
| 87 | + toolCall=tool_call, |
| 88 | + options=list(option_set), |
| 89 | + ) |
| 90 | + return await self._requester(request) |
| 91 | + |
| 92 | + |
| 93 | +__all__ = [ |
| 94 | + "PermissionBroker", |
| 95 | + "default_permission_options", |
| 96 | +] |
0 commit comments