77import pytest
88import requests
99from dotenv import load_dotenv
10- from letta_client import Letta , MessageCreate
10+ from letta_client import ApprovalCreate , Letta , MessageCreate
11+ from letta_client .core .api_error import ApiError
1112
1213from letta .log import get_logger
1314from letta .schemas .agent import AgentState
@@ -98,7 +99,7 @@ def approval_tool_fixture(client: Letta):
9899 client .tools .upsert_base_tools ()
99100 approval_tool = client .tools .upsert_from_function (
100101 func = requires_approval_tool ,
101- # default_requires_approval=True,
102+ # default_requires_approval=True, switch to this once it is supported in sdk
102103 )
103104 yield approval_tool
104105
@@ -118,6 +119,11 @@ def agent(client: Letta, approval_tool_fixture) -> AgentState:
118119 embedding = "openai/text-embedding-3-small" ,
119120 tags = ["approval_test" ],
120121 )
122+ client .agents .tools .modify_approval (
123+ agent_id = agent_state .id ,
124+ tool_name = approval_tool_fixture .name ,
125+ requires_approval = True ,
126+ )
121127 yield agent_state
122128
123129
@@ -136,6 +142,13 @@ def test_send_message_with_approval_tool(
136142 This test just verifies that the agent can send a message successfully.
137143 The actual approval logic testing will be filled out by the user.
138144 """
145+ # Attempt to send approval without pending request
146+ with pytest .raises (ApiError , match = "No tool call is currently awaiting approval" ):
147+ client .agents .messages .create (
148+ agent_id = agent .id ,
149+ messages = [ApprovalCreate (approve = True , approval_request_id = "fake_id" )],
150+ )
151+
139152 # Send a simple greeting message to test basic functionality
140153 response = client .agents .messages .create (
141154 agent_id = agent .id ,
@@ -147,3 +160,17 @@ def test_send_message_with_approval_tool(
147160 assert len (response .messages ) == 2
148161 assert response .messages [0 ].message_type == "reasoning_message"
149162 assert response .messages [1 ].message_type == "approval_request_message"
163+
164+ # Attempt to send user message - should fail
165+ with pytest .raises (ApiError , match = "Please approve or deny the pending request before continuing" ):
166+ client .agents .messages .create (
167+ agent_id = agent .id ,
168+ messages = [MessageCreate (role = "user" , content = "hi" )],
169+ )
170+
171+ # Attempt to send approval with incorrect id
172+ with pytest .raises (ApiError , match = "Invalid approval request ID" ):
173+ client .agents .messages .create (
174+ agent_id = agent .id ,
175+ messages = [ApprovalCreate (approve = True , approval_request_id = "fake_id" )],
176+ )
0 commit comments