|
| 1 | +import functools |
| 2 | +import inspect |
| 3 | +from inspect import Parameter |
| 4 | +from typing import Annotated, Any, Callable |
| 5 | + |
| 6 | +from langchain_core.tools import BaseTool, InjectedToolCallId |
| 7 | +from langchain_core.tools import tool as langchain_tool |
| 8 | +from langgraph.types import interrupt |
| 9 | +from uipath.core.chat import ( |
| 10 | + UiPathConversationToolCallConfirmationValue, |
| 11 | +) |
| 12 | + |
| 13 | +_CANCELLED_MESSAGE = "Cancelled by user" |
| 14 | + |
| 15 | + |
| 16 | +def _request_approval( |
| 17 | + tool_args: dict[str, Any], |
| 18 | + tool: BaseTool, |
| 19 | +) -> dict[str, Any] | None: |
| 20 | + """Interrupt the graph to request user approval for a tool call. |
| 21 | +
|
| 22 | + Returns the (possibly edited) tool arguments if approved, or None if rejected. |
| 23 | + """ |
| 24 | + tool_call_id: str = tool_args.pop("tool_call_id") |
| 25 | + |
| 26 | + input_schema: dict[str, Any] = {} |
| 27 | + tool_call_schema = getattr( |
| 28 | + tool, "tool_call_schema", None |
| 29 | + ) # doesn't include InjectedToolCallId (tool id from claude/oai/etc.) |
| 30 | + if tool_call_schema is not None: |
| 31 | + input_schema = tool_call_schema.model_json_schema() |
| 32 | + |
| 33 | + response = interrupt( |
| 34 | + UiPathConversationToolCallConfirmationValue( |
| 35 | + tool_call_id=tool_call_id, |
| 36 | + tool_name=tool.name, |
| 37 | + input_schema=input_schema, |
| 38 | + input_value=tool_args, |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + # The resume payload from CAS has shape: |
| 43 | + # {"type": "uipath_cas_tool_call_confirmation", |
| 44 | + # "value": {"approved": bool, "input": <edited args | None>}} |
| 45 | + if not isinstance(response, dict): |
| 46 | + return tool_args |
| 47 | + |
| 48 | + confirmation = response.get("value", response) |
| 49 | + if not confirmation.get("approved", True): |
| 50 | + return None |
| 51 | + |
| 52 | + return confirmation.get("input") or tool_args |
| 53 | + |
| 54 | + |
| 55 | +def hitl_tool( |
| 56 | + func: Callable[..., Any] | None = None, |
| 57 | + *, |
| 58 | + name: str | None = None, |
| 59 | + description: str | None = None, |
| 60 | + args_schema: type | None = None, |
| 61 | + return_direct: bool = False, |
| 62 | +) -> BaseTool | Callable[..., BaseTool]: |
| 63 | + |
| 64 | + def decorator(fn: Callable[..., Any]) -> BaseTool: |
| 65 | + _created_tool: list[BaseTool] = [] |
| 66 | + |
| 67 | + # wrap the tool/function |
| 68 | + @functools.wraps(fn) |
| 69 | + def wrapper(**tool_args: Any) -> Any: |
| 70 | + approved_args = _request_approval(tool_args, _created_tool[0]) |
| 71 | + if approved_args is None: |
| 72 | + return _CANCELLED_MESSAGE |
| 73 | + return fn(**approved_args) |
| 74 | + |
| 75 | + # rewrite the signature: e.g. (query: str) -> (query: str, *, tool_call_id: str) |
| 76 | + original_sig = inspect.signature(fn) |
| 77 | + params = list[Parameter](original_sig.parameters.values()) + [ |
| 78 | + inspect.Parameter( |
| 79 | + "tool_call_id", |
| 80 | + inspect.Parameter.KEYWORD_ONLY, |
| 81 | + annotation=Annotated[str, InjectedToolCallId], |
| 82 | + ), |
| 83 | + ] |
| 84 | + wrapper.__signature__ = original_sig.replace(parameters=params) # type: ignore[attr-defined] |
| 85 | + wrapper.__annotations__ = { |
| 86 | + **fn.__annotations__, |
| 87 | + "tool_call_id": Annotated[str, InjectedToolCallId], |
| 88 | + } |
| 89 | + |
| 90 | + # Create the LangChain tool |
| 91 | + if name is not None: |
| 92 | + result: BaseTool = langchain_tool( |
| 93 | + name, |
| 94 | + description=description, |
| 95 | + args_schema=args_schema, |
| 96 | + return_direct=return_direct, |
| 97 | + )(wrapper) |
| 98 | + else: |
| 99 | + result = langchain_tool( |
| 100 | + wrapper, |
| 101 | + description=description, |
| 102 | + args_schema=args_schema, |
| 103 | + return_direct=return_direct, |
| 104 | + ) |
| 105 | + |
| 106 | + _created_tool.append(result) |
| 107 | + return result |
| 108 | + |
| 109 | + if func is not None: |
| 110 | + return decorator(func) |
| 111 | + return decorator |
0 commit comments