Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: support deny tool call request (#4334)
cthomas65cd43031741
diff --git a/letta/agents/helpers.py b/letta/agents/helpers.pyindex 9bfa9ad7d..0d5da5fc4 100644--- a/letta/agents/helpers.py+++ b/letta/agents/helpers.py@@ -161,10 +161,7 @@ async def _prepare_in_context_messages_no_persist_async(f"Invalid approval request ID. Expected '{current_in_context_messages[-1].id}' "f"but received '{input_messages[0].approval_request_id}'.")- if input_messages[0].approve:- new_in_context_messages = []- else:- raise NotImplementedError("Deny flow not yet supported")+ new_in_context_messages = []else:# User is trying to send a regular messageif current_in_context_messages[-1].role == "approval":diff --git a/letta/agents/letta_agent.py b/letta/agents/letta_agent.pyindex 66302b5a1..4362214c4 100644--- a/letta/agents/letta_agent.py+++ b/letta/agents/letta_agent.py@@ -248,7 +248,9 @@ class LettaAgent(BaseAgent):is_final_step=(i == max_steps - 1),step_metrics=step_metrics,run_id=self.current_run_id,- is_approval=True,+ is_approval=input_messages[0].approve,+ is_denial=not input_messages[0].approve,+ denial_reason=input_messages[0].reason,)new_message_idx = 0self.response_messages.extend(persisted_messages)@@ -585,7 +587,9 @@ class LettaAgent(BaseAgent):is_final_step=(i == max_steps - 1),step_metrics=step_metrics,run_id=run_id or self.current_run_id,- is_approval=True,+ is_approval=input_messages[0].approve,+ is_denial=not input_messages[0].approve,+ denial_reason=input_messages[0].reason,)new_message_idx = 0self.response_messages.extend(persisted_messages)@@ -924,7 +928,9 @@ class LettaAgent(BaseAgent):is_final_step=(i == max_steps - 1),step_metrics=step_metrics,run_id=self.current_run_id,- is_approval=True,+ is_approval=input_messages[0].approve,+ is_denial=not input_messages[0].approve,+ denial_reason=input_messages[0].reason,)new_message_idx = 0self.response_messages.extend(persisted_messages)@@ -1615,14 +1621,43 @@ class LettaAgent(BaseAgent):run_id: str | None = None,step_metrics: StepMetrics = None,is_approval: bool | None = None,+ is_denial: bool | None = None,+ denial_reason: str | None = None,) -> tuple[list[Message], bool, LettaStopReason | None]:"""Handle the final AI response once streaming completes, execute / validate thetool call, decide whether we should keep stepping, and persist state."""+ tool_call_id: str = tool_call.id or f"call_{uuid.uuid4().hex[:8]}"++ if is_denial:+ continue_stepping = True+ stop_reason = None+ tool_call_messages = create_letta_messages_from_llm_response(+ agent_id=agent_state.id,+ model=agent_state.llm_config.model,+ function_name="",+ function_arguments={},+ tool_execution_result=ToolExecutionResult(status="error"),+ tool_call_id=tool_call_id,+ function_call_success=False,+ function_response=f"Error: request to call tool denied. User reason: {denial_reason}",+ timezone=agent_state.timezone,+ actor=self.actor,+ continue_stepping=continue_stepping,+ heartbeat_reason=f"{NON_USER_MSG_PREFIX}Continuing: user denied request to call tool.",+ reasoning_content=None,+ pre_computed_assistant_message_id=None,+ step_id=step_id,+ is_approval_response=True,+ )++ persisted_messages = await self.message_manager.create_many_messages_async(tool_call_messages, actor=self.actor)+ return persisted_messages, continue_stepping, stop_reason+# 1. Parse and validate the tool-call envelopetool_call_name: str = tool_call.function.name- tool_call_id: str = tool_call.id or f"call_{uuid.uuid4().hex[:8]}"+tool_args = _safe_load_tool_call_str(tool_call.function.arguments)request_heartbeat: bool = _pop_heartbeat(tool_args)tool_args.pop(INNER_THOUGHTS_KWARG, None)@@ -1723,7 +1758,7 @@ class LettaAgent(BaseAgent):reasoning_content=reasoning_content,pre_computed_assistant_message_id=pre_computed_assistant_message_id,step_id=step_id,- is_approval=is_approval,+ is_approval_response=is_approval or is_denial,)messages_to_persist = (initial_messages or []) + tool_call_messagesdiff --git a/letta/server/rest_api/utils.py b/letta/server/rest_api/utils.pyindex eb63d02cf..a193e57b6 100644--- a/letta/server/rest_api/utils.py+++ b/letta/server/rest_api/utils.py@@ -233,10 +233,10 @@ def create_letta_messages_from_llm_response(pre_computed_assistant_message_id: Optional[str] = None,llm_batch_item_id: Optional[str] = None,step_id: str | None = None,- is_approval: bool | None = None,+ is_approval_response: bool | None = None,) -> List[Message]:messages = []- if not is_approval:+ if not is_approval_response:# Construct the tool call with the assistant's message# Force set request_heartbeat in tool_args to calculated continue_steppingfunction_arguments[REQUEST_HEARTBEAT_PARAM] = continue_steppingdiff --git a/tests/integration_test_human_in_the_loop.py b/tests/integration_test_human_in_the_loop.pyindex 9c32c1966..8f3987fbc 100644--- a/tests/integration_test_human_in_the_loop.py+++ b/tests/integration_test_human_in_the_loop.py@@ -195,3 +195,44 @@ def test_send_message_with_approval_tool(assert response.messages[0].status == "success"assert response.messages[1].message_type == "reasoning_message"assert response.messages[2].message_type == "assistant_message"+++def test_deny(+ disable_e2b_api_key: Any,+ client: Letta,+ agent: AgentState,+) -> None:+ """+ Tests sending a message to an agent with a tool that requires approval.+ This test just verifies that the agent can send a message successfully.+ The actual approval logic testing will be filled out by the user.+ """+ # Send a simple greeting message to test basic functionality+ response = client.agents.messages.create(+ agent_id=agent.id,+ messages=USER_MESSAGE_TEST_APPROVAL,+ )++ # Basic assertion that we got a response with an approval request+ assert response.messages is not None+ assert len(response.messages) == 2+ assert response.messages[0].message_type == "reasoning_message"+ assert response.messages[1].message_type == "approval_request_message"+ approval_request_id = response.messages[0].id+ tool_call_id = response.messages[1].tool_call.tool_call_id++ response = client.agents.messages.create(+ agent_id=agent.id,+ messages=[+ ApprovalCreate(approve=False, approval_request_id=approval_request_id, reason="No don't do that, the answer is 2"),+ ],+ )++ # Basic assertion that we got a response with tool call return+ assert response.messages is not None+ assert len(response.messages) == 3+ assert response.messages[0].message_type == "tool_return_message"+ assert response.messages[0].tool_call_id == tool_call_id+ assert response.messages[0].status == "error"+ assert response.messages[1].message_type == "reasoning_message"+ assert response.messages[2].message_type == "assistant_message"