| """ |
| Auto Approval Node for ReAct Pattern |
| |
| This module implements automatic tool approval for the ReAct workflow when |
| human approval is disabled via the HUMAN_APPROVAL environment variable. |
| |
| When HUMAN_APPROVAL is set to False, this node automatically approves all |
| pending tool calls without requiring user interaction, allowing for |
| fully automated tool execution in trusted environments. |
| |
| Key Features: |
| - Automatic approval of all pending tool calls |
| - Consistent state management with human approval flow |
| - Comprehensive logging for audit trails |
| - Safety checks for empty pending lists |
| |
| The auto approval process mirrors the human approval workflow but bypasses |
| user interaction, making it suitable for automated scenarios, testing, |
| or trusted environments where human oversight is not required. |
| |
| State Updates: |
| After auto-approval, the state is updated with: |
| - approved_tool_calls: All pending tools moved to approved |
| - rejected_tool_calls: Empty list (no rejections in auto mode) |
| - pending_tool_calls: Cleared after approval process |
| |
| Example: |
| >>> state = { |
| ... "pending_tool_calls": [ |
| ... {"name": "research", "args": {"query": "AI trends"}} |
| ... ] |
| ... } |
| >>> result = await auto_approval_node(state) |
| >>> # All pending tools automatically approved |
| >>> print(state["approved_tool_calls"]) # Contains the research tool call |
| """ |
|
|
| from typing import Dict, Any |
| import logging |
|
|
| logger = logging.getLogger("ReAct Auto Approval") |
|
|
|
|
| async def auto_approval_node(state: Dict[str, Any]) -> Dict[str, Any]: |
| """ |
| Node that automatically approves all pending tool calls without user interaction. |
| |
| This node is used when HUMAN_APPROVAL is disabled, providing a seamless |
| automated workflow while maintaining the same state structure as human approval. |
| |
| Args: |
| state: Current ReAct state with pending tool calls |
| |
| Returns: |
| Updated state with all pending tools moved to approved_tool_calls |
| """ |
| pending_tools = state.get("pending_tool_calls", []) |
| |
| if not pending_tools: |
| logger.info("โน๏ธ No pending tool calls for auto-approval") |
| return state |
| |
| logger.info(f"๐ค Auto-approving {len(pending_tools)} tool call(s)") |
| |
| |
| for tool_call in pending_tools: |
| logger.info(f"โ
Auto-approved tool: '{tool_call['name']}' with args: {tool_call['args']}") |
| |
| |
| updated_state = state.copy() |
| updated_state["approved_tool_calls"] = pending_tools.copy() |
| updated_state["rejected_tool_calls"] = [] |
| updated_state["pending_tool_calls"] = [] |
| updated_state["current_step"] = "auto_approval_complete" |
|
|
| |
| |
|
|
| logger.info(f"๐ Auto-approval complete: {len(pending_tools)} tools approved, 0 rejected") |
|
|
| return updated_state |