Hivenet_ComputeAgent / ComputeAgent /nodes /ReAct /auto_approval_node.py
carraraig's picture
Hello
8816dfd
"""
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)")
# Log each tool being auto-approved for audit trail
for tool_call in pending_tools:
logger.info(f"โœ… Auto-approved tool: '{tool_call['name']}' with args: {tool_call['args']}")
# Update state with auto-approval results
updated_state = state.copy()
updated_state["approved_tool_calls"] = pending_tools.copy() # Approve all pending tools
updated_state["rejected_tool_calls"] = [] # No rejections in auto mode
updated_state["pending_tool_calls"] = [] # Clear pending calls
updated_state["current_step"] = "auto_approval_complete"
# NOTE: Don't remove tools here - tool_execution needs them next
# Tools are only removed in terminal nodes (generate, tool_rejection_exit)
logger.info(f"๐Ÿ“Š Auto-approval complete: {len(pending_tools)} tools approved, 0 rejected")
return updated_state