4
4
5
5
from aws_lambda_powertools .event_handler import BedrockAgentFunctionResolver , BedrockFunctionResponse
6
6
from aws_lambda_powertools .utilities .data_classes import BedrockAgentFunctionEvent
7
+ from aws_lambda_powertools .warnings import PowertoolsUserWarning
7
8
from tests .functional .utils import load_event
8
9
9
10
@@ -59,22 +60,25 @@ def test_bedrock_agent_function_registration():
59
60
# GIVEN a Bedrock Agent Function resolver
60
61
app = BedrockAgentFunctionResolver ()
61
62
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
69
64
@app .tool (name = "custom" , description = "First registration" )
70
65
def first_function ():
71
- return "test"
66
+ return "first test"
72
67
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" ):
74
70
75
71
@app .tool (name = "custom" , description = "Second registration" )
76
72
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"
78
82
79
83
80
84
def test_bedrock_agent_function_with_optional_fields ():
@@ -156,7 +160,7 @@ def test_resolve_with_no_registered_function():
156
160
157
161
def test_bedrock_function_response_state_validation ():
158
162
# GIVEN invalid and valid response states
159
- valid_states = [None , "FAILURE" , "REPROMPT" ]
163
+ valid_states = ["FAILURE" , "REPROMPT" ]
160
164
invalid_state = "INVALID"
161
165
162
166
# WHEN creating responses with valid states
@@ -172,4 +176,32 @@ def test_bedrock_function_response_state_validation():
172
176
with pytest .raises (ValueError ) as exc_info :
173
177
BedrockFunctionResponse (body = "test" , response_state = invalid_state )
174
178
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