44
55from aws_lambda_powertools .event_handler import BedrockAgentFunctionResolver , BedrockFunctionResponse
66from aws_lambda_powertools .utilities .data_classes import BedrockAgentFunctionEvent
7+ from aws_lambda_powertools .warnings import PowertoolsUserWarning
78from tests .functional .utils import load_event
89
910
@@ -59,22 +60,25 @@ def test_bedrock_agent_function_registration():
5960 # GIVEN a Bedrock Agent Function resolver
6061 app = BedrockAgentFunctionResolver ()
6162
62- # WHEN registering without description or with duplicate name
63- with pytest .raises (ValueError , match = "Tool description is required" ):
64-
65- @app .tool ()
66- def test_function ():
67- return "test"
68-
63+ # WHEN registering with duplicate name
6964 @app .tool (name = "custom" , description = "First registration" )
7065 def first_function ():
71- return "test"
66+ return "first test"
7267
73- with pytest .raises (ValueError , match = "Tool 'custom' already registered" ):
68+ # THEN a warning should be issued when registering a duplicate
69+ with pytest .warns (PowertoolsUserWarning , match = "Tool 'custom' already registered" ):
7470
7571 @app .tool (name = "custom" , description = "Second registration" )
7672 def second_function ():
77- return "test"
73+ return "second test"
74+
75+ # AND the most recent function should be registered
76+ raw_event = load_event ("bedrockAgentFunctionEvent.json" )
77+ raw_event ["function" ] = "custom"
78+ result = app .resolve (raw_event , {})
79+
80+ # The second function should be used
81+ assert result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ] == "second test"
7882
7983
8084def test_bedrock_agent_function_with_optional_fields ():
@@ -156,7 +160,7 @@ def test_resolve_with_no_registered_function():
156160
157161def test_bedrock_function_response_state_validation ():
158162 # GIVEN invalid and valid response states
159- valid_states = [None , "FAILURE" , "REPROMPT" ]
163+ valid_states = ["FAILURE" , "REPROMPT" ]
160164 invalid_state = "INVALID"
161165
162166 # WHEN creating responses with valid states
@@ -172,4 +176,32 @@ def test_bedrock_function_response_state_validation():
172176 with pytest .raises (ValueError ) as exc_info :
173177 BedrockFunctionResponse (body = "test" , response_state = invalid_state )
174178
175- assert str (exc_info .value ) == "responseState must be None, 'FAILURE' or 'REPROMPT'"
179+ assert str (exc_info .value ) == "responseState must be 'FAILURE' or 'REPROMPT'"
180+
181+
182+ def test_bedrock_agent_function_with_parameters ():
183+ # GIVEN a Bedrock Agent Function resolver
184+ app = BedrockAgentFunctionResolver ()
185+
186+ # Track received parameters
187+ received_params = {}
188+
189+ @app .tool (description = "Function that accepts parameters" )
190+ def vacation_request (startDate , endDate ):
191+ # Store received parameters for assertion
192+ received_params ["startDate" ] = startDate
193+ received_params ["endDate" ] = endDate
194+ return f"Vacation request from { startDate } to { endDate } submitted"
195+
196+ # WHEN calling the event handler with parameters
197+ raw_event = load_event ("bedrockAgentFunctionEvent.json" )
198+ raw_event ["function" ] = "vacation_request"
199+ result = app .resolve (raw_event , {})
200+
201+ # THEN parameters should be correctly passed to the function
202+ assert received_params ["startDate" ] == "2024-03-15"
203+ assert received_params ["endDate" ] == "2024-03-20"
204+ assert (
205+ "Vacation request from 2024-03-15 to 2024-03-20 submitted"
206+ in result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ]
207+ )
0 commit comments