22
22
23
23
24
24
class BedrockResponse :
25
- """
26
- Response class for Bedrock Agents Lambda functions.
27
-
28
- Parameters
29
- ----------
30
- status_code: int
31
- HTTP status code for the response
32
- body: Any
33
- Response body content
34
- content_type: str, optional
35
- Content type of the response (default: application/json)
36
- session_attributes: dict[str, Any], optional
37
- Session attributes to maintain state
38
- prompt_session_attributes: dict[str, Any], optional
39
- Prompt-specific session attributes
40
- knowledge_bases_configuration: dict[str, Any], optional
41
- Knowledge base configuration settings
42
- """
43
-
44
25
def __init__ (
45
26
self ,
46
- status_code : int ,
47
27
body : Any ,
28
+ status_code : int = 200 ,
48
29
content_type : str = "application/json" ,
49
30
session_attributes : dict [str , Any ] | None = None ,
50
31
prompt_session_attributes : dict [str , Any ] | None = None ,
51
- knowledge_bases_configuration : dict [str , Any ] | None = None ,
32
+ knowledge_bases_configuration : list [ dict [str , Any ] ] | None = None ,
52
33
) -> None :
53
- self .status_code = status_code
54
34
self .body = body
35
+ self .status_code = status_code
55
36
self .content_type = content_type
56
37
self .session_attributes = session_attributes
57
38
self .prompt_session_attributes = prompt_session_attributes
39
+
40
+ if knowledge_bases_configuration is not None :
41
+ if not isinstance (knowledge_bases_configuration , list ) or not all (
42
+ isinstance (item , dict ) for item in knowledge_bases_configuration
43
+ ):
44
+ raise ValueError ("knowledge_bases_configuration must be a list of dictionaries" )
45
+
58
46
self .knowledge_bases_configuration = knowledge_bases_configuration
59
47
48
+ def to_dict (self , event ) -> dict [str , Any ]:
49
+ result = {
50
+ "messageVersion" : "1.0" ,
51
+ "response" : {
52
+ "apiPath" : event .api_path ,
53
+ "actionGroup" : event .action_group ,
54
+ "httpMethod" : event .http_method ,
55
+ "httpStatusCode" : self .status_code ,
56
+ "responseBody" : {
57
+ self .content_type : {"body" : json .dumps (self .body ) if isinstance (self .body , dict ) else self .body },
58
+ },
59
+ },
60
+ }
61
+
62
+ # Add optional attributes if they exist
63
+ if self .session_attributes is not None :
64
+ result ["sessionAttributes" ] = self .session_attributes
65
+
66
+ if self .prompt_session_attributes is not None :
67
+ result ["promptSessionAttributes" ] = self .prompt_session_attributes
68
+
69
+ if self .knowledge_bases_configuration is not None :
70
+ result ["knowledgeBasesConfiguration" ] = self .knowledge_bases_configuration
71
+
72
+ return result
73
+
60
74
61
75
class BedrockResponseBuilder (ResponseBuilder ):
62
76
"""
@@ -72,6 +86,10 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
72
86
"""
73
87
self ._route (event , None )
74
88
89
+ body = self .response .body
90
+ if self .response .is_json () and not isinstance (self .response .body , str ):
91
+ body = self .serializer (self .response .body )
92
+
75
93
base_response = {
76
94
"messageVersion" : "1.0" ,
77
95
"response" : {
@@ -81,7 +99,7 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
81
99
"httpStatusCode" : self .response .status_code ,
82
100
"responseBody" : {
83
101
self .response .content_type : {
84
- "body" : self . _get_formatted_body () ,
102
+ "body" : body ,
85
103
},
86
104
},
87
105
},
@@ -92,22 +110,7 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
92
110
93
111
return base_response
94
112
95
- def _get_formatted_body (self ) -> Any :
96
- """Format the response body based on content type"""
97
- if not isinstance (self .response , BedrockResponse ):
98
- if self .response .is_json () and not isinstance (self .response .body , str ):
99
- return self .serializer (self .response .body )
100
- return self .response .body
101
-
102
113
def _add_bedrock_specific_configs (self , response : dict [str , Any ]) -> None :
103
- """
104
- Add Bedrock-specific configurations to the response if present.
105
-
106
- Parameters
107
- ----------
108
- response: dict[str, Any]
109
- The base response dictionary to be updated
110
- """
111
114
if not isinstance (self .response , BedrockResponse ):
112
115
return
113
116
0 commit comments