1
1
from __future__ import annotations
2
2
3
+ import json
4
+
3
5
import pytest
4
6
5
- from aws_lambda_powertools .event_handler import BedrockAgentFunctionResolver
7
+ from aws_lambda_powertools .event_handler import BedrockAgentFunctionResolver , Response , content_types
6
8
from aws_lambda_powertools .utilities .data_classes import BedrockAgentFunctionEvent
7
9
from tests .functional .utils import load_event
8
10
9
11
10
- def test_bedrock_agent_function ():
12
+ def test_bedrock_agent_function_with_string_response ():
11
13
# GIVEN a Bedrock Agent Function resolver
12
14
app = BedrockAgentFunctionResolver ()
13
15
14
- @app .tool (description = "Gets the current time " )
15
- def get_current_time ():
16
+ @app .tool (description = "Returns a string " )
17
+ def test_function ():
16
18
assert isinstance (app .current_event , BedrockAgentFunctionEvent )
17
- return "2024-02-01T12:00:00Z "
19
+ return "Hello from string "
18
20
19
21
# WHEN calling the event handler
20
22
raw_event = load_event ("bedrockAgentFunctionEvent.json" )
21
- raw_event ["function" ] = "get_current_time" # ensure function name matches
23
+ raw_event ["function" ] = "test_function"
22
24
result = app .resolve (raw_event , {})
23
25
24
- # THEN process event correctly
26
+ # THEN process event correctly with string response
25
27
assert result ["messageVersion" ] == "1.0"
26
28
assert result ["response" ]["actionGroup" ] == raw_event ["actionGroup" ]
27
- assert result ["response" ]["function" ] == "get_current_time"
28
- assert result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ] == "2024-02-01T12:00:00Z"
29
+ assert result ["response" ]["function" ] == "test_function"
30
+ assert result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ] == "Hello from string"
31
+ assert "responseState" not in result ["response" ]["functionResponse" ] # Success has no state
29
32
30
33
31
34
def test_bedrock_agent_function_with_error ():
@@ -46,29 +49,53 @@ def error_function():
46
49
assert result ["response" ]["actionGroup" ] == raw_event ["actionGroup" ]
47
50
assert result ["response" ]["function" ] == "error_function"
48
51
assert "Error: Something went wrong" in result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ]
52
+ assert result ["response" ]["functionResponse" ]["responseState" ] == "FAILURE"
49
53
50
54
51
55
def test_bedrock_agent_function_not_found ():
52
56
# GIVEN a Bedrock Agent Function resolver
53
57
app = BedrockAgentFunctionResolver ()
54
58
55
- @app .tool (description = "Test function" )
56
- def test_function ():
57
- return "test"
58
-
59
59
# WHEN calling the event handler with a non-existent function
60
60
raw_event = load_event ("bedrockAgentFunctionEvent.json" )
61
61
raw_event ["function" ] = "nonexistent_function"
62
62
result = app .resolve (raw_event , {})
63
63
64
- # THEN return function not found response
64
+ # THEN return function not found response with REPROMPT state
65
65
assert result ["messageVersion" ] == "1.0"
66
66
assert result ["response" ]["actionGroup" ] == raw_event ["actionGroup" ]
67
67
assert result ["response" ]["function" ] == "nonexistent_function"
68
68
assert "Function not found" in result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ]
69
+ assert result ["response" ]["functionResponse" ]["responseState" ] == "REPROMPT"
69
70
70
71
71
- def test_bedrock_agent_function_missing_description ():
72
+ def test_bedrock_agent_function_with_response_object ():
73
+ # GIVEN a Bedrock Agent Function resolver
74
+ app = BedrockAgentFunctionResolver ()
75
+
76
+ @app .tool (description = "Returns a Response object" )
77
+ def test_function ():
78
+ return Response (
79
+ status_code = 200 ,
80
+ content_type = content_types .APPLICATION_JSON ,
81
+ body = {"message" : "Hello from Response" },
82
+ )
83
+
84
+ # WHEN calling the event handler
85
+ raw_event = load_event ("bedrockAgentFunctionEvent.json" )
86
+ raw_event ["function" ] = "test_function"
87
+ result = app .resolve (raw_event , {})
88
+
89
+ # THEN process event correctly with Response object
90
+ assert result ["messageVersion" ] == "1.0"
91
+ assert result ["response" ]["actionGroup" ] == raw_event ["actionGroup" ]
92
+ assert result ["response" ]["function" ] == "test_function"
93
+ response_body = result ["response" ]["functionResponse" ]["responseBody" ]["TEXT" ]["body" ]
94
+ assert json .loads (response_body ) == {"message" : "Hello from Response" }
95
+ assert "responseState" not in result ["response" ]["functionResponse" ] # Success has no state
96
+
97
+
98
+ def test_bedrock_agent_function_registration ():
72
99
# GIVEN a Bedrock Agent Function resolver
73
100
app = BedrockAgentFunctionResolver ()
74
101
@@ -80,32 +107,23 @@ def test_bedrock_agent_function_missing_description():
80
107
def test_function ():
81
108
return "test"
82
109
83
-
84
- def test_bedrock_agent_function_duplicate_registration ():
85
- # GIVEN a Bedrock Agent Function resolver
86
- app = BedrockAgentFunctionResolver ()
87
-
88
110
# WHEN registering the same function twice
111
+ # THEN raise ValueError
89
112
@app .tool (description = "First registration" )
90
- def test_function ():
113
+ def duplicate_function ():
91
114
return "test"
92
115
93
- # THEN raise ValueError on second registration
94
- with pytest .raises (ValueError , match = "Tool 'test_function' already registered" ):
116
+ with pytest .raises (ValueError , match = "Tool 'duplicate_function' already registered" ):
95
117
96
118
@app .tool (description = "Second registration" )
97
- def test_function (): # noqa: F811
119
+ def duplicate_function (): # noqa: F811
98
120
return "test"
99
121
100
122
101
123
def test_bedrock_agent_function_invalid_event ():
102
124
# GIVEN a Bedrock Agent Function resolver
103
125
app = BedrockAgentFunctionResolver ()
104
126
105
- @app .tool (description = "Test function" )
106
- def test_function ():
107
- return "test"
108
-
109
127
# WHEN calling with invalid event
110
128
# THEN raise ValueError
111
129
with pytest .raises (ValueError , match = "Missing required field" ):
0 commit comments