@@ -29,12 +29,14 @@ def lambda_handler(event, context):
29
29
return app.resolve(event, context)
30
30
```
31
31
"""
32
+
32
33
def __init__ (self ) -> None :
33
34
self ._tools : dict [str , dict [str , Any ]] = {}
34
35
self .current_event : BedrockAgentFunctionEvent | None = None
35
36
36
37
def tool (self , description : str | None = None ) -> Callable :
37
38
"""Decorator to register a tool function"""
39
+
38
40
def decorator (func : Callable ) -> Callable :
39
41
if not description :
40
42
raise ValueError ("Tool description is required" )
@@ -48,6 +50,7 @@ def decorator(func: Callable) -> Callable:
48
50
"description" : description ,
49
51
}
50
52
return func
53
+
51
54
return decorator
52
55
53
56
def resolve (self , event : dict [str , Any ], context : Any ) -> dict [str , Any ]:
@@ -60,28 +63,27 @@ def resolve(self, event: dict[str, Any], context: Any) -> dict[str, Any]:
60
63
61
64
def _resolve (self ) -> dict [str , Any ]:
62
65
"""Internal resolution logic"""
66
+ if self .current_event is None :
67
+ raise ValueError ("No event to process" )
68
+
63
69
function_name = self .current_event .function
64
70
action_group = self .current_event .action_group
65
71
66
72
if function_name not in self ._tools :
67
73
return self ._create_response (
68
74
action_group = action_group ,
69
75
function_name = function_name ,
70
- result = f"Function not found: { function_name } "
76
+ result = f"Function not found: { function_name } " ,
71
77
)
72
78
73
79
try :
74
80
result = self ._tools [function_name ]["function" ]()
75
- return self ._create_response (
76
- action_group = action_group ,
77
- function_name = function_name ,
78
- result = result
79
- )
81
+ return self ._create_response (action_group = action_group , function_name = function_name , result = result )
80
82
except Exception as e :
81
83
return self ._create_response (
82
84
action_group = action_group ,
83
85
function_name = function_name ,
84
- result = f"Error: { str (e )} "
86
+ result = f"Error: { str (e )} " ,
85
87
)
86
88
87
89
def _create_response (self , action_group : str , function_name : str , result : Any ) -> dict [str , Any ]:
@@ -91,12 +93,6 @@ def _create_response(self, action_group: str, function_name: str, result: Any) -
91
93
"response" : {
92
94
"actionGroup" : action_group ,
93
95
"function" : function_name ,
94
- "functionResponse" : {
95
- "responseBody" : {
96
- "TEXT" : {
97
- "body" : str (result )
98
- }
99
- }
100
- }
101
- }
102
- }
96
+ "functionResponse" : {"responseBody" : {"TEXT" : {"body" : str (result )}}},
97
+ },
98
+ }
0 commit comments