11
11
BaseEvent , EventType ,
12
12
TextMessageStartEvent , TextMessageContentEvent , TextMessageEndEvent ,
13
13
ToolCallStartEvent , ToolCallArgsEvent , ToolCallEndEvent ,
14
- ToolCallChunkEvent ,
14
+ ToolCallChunkEvent ,ToolCallResultEvent ,
15
15
StateSnapshotEvent , StateDeltaEvent ,
16
16
MessagesSnapshotEvent ,
17
17
CustomEvent ,
18
18
Message , AssistantMessage , UserMessage , ToolMessage
19
19
)
20
-
20
+ import json
21
21
from google .adk .events import Event as ADKEvent
22
22
23
23
import logging
@@ -87,13 +87,6 @@ async def translate(
87
87
88
88
89
89
90
- # Handle function responses
91
- if hasattr (adk_event , 'get_function_responses' ):
92
- function_responses = adk_event .get_function_responses ()
93
- if function_responses :
94
- # Function responses are typically handled by the agent internally
95
- # We don't need to emit them as AG-UI events
96
- pass
97
90
98
91
# call _translate_function_calls function to yield Tool Events
99
92
if hasattr (adk_event , 'get_function_calls' ):
@@ -104,6 +97,15 @@ async def translate(
104
97
async for event in self ._translate_function_calls (function_calls ):
105
98
yield event
106
99
100
+ # Handle function responses and yield the tool response event
101
+ # this is essential for scenerios when user has to render function response at frontend
102
+ if hasattr (adk_event , 'get_function_responses' ):
103
+ function_responses = adk_event .get_function_responses ()
104
+ if function_responses :
105
+ # Function responses should be emmitted to frontend so it can render the response as well
106
+ async for event in self ._translate_function_response (function_responses ):
107
+ yield event
108
+
107
109
108
110
# Handle state changes
109
111
if hasattr (adk_event , 'actions' ) and adk_event .actions and hasattr (adk_event .actions , 'state_delta' ) and adk_event .actions .state_delta :
@@ -281,6 +283,32 @@ async def _translate_function_calls(
281
283
# Clean up tracking
282
284
self ._active_tool_calls .pop (tool_call_id , None )
283
285
286
+
287
+ async def _translate_function_response (
288
+ self ,
289
+ function_response : list [types .FunctionResponse ],
290
+ ) -> AsyncGenerator [BaseEvent , None ]:
291
+ """Translate function calls from ADK event to AG-UI tool call events.
292
+
293
+ Args:
294
+ adk_event: The ADK event containing function calls
295
+ function_response: List of function response from the event
296
+
297
+ Yields:
298
+ Tool result events
299
+ """
300
+
301
+ for func_response in function_response :
302
+
303
+ tool_call_id = getattr (func_response , 'id' , str (uuid .uuid4 ()))
304
+
305
+ yield ToolCallResultEvent (
306
+ message_id = str (uuid .uuid4 ()),
307
+ type = EventType .TOOL_CALL_RESULT ,
308
+ tool_call_id = tool_call_id ,
309
+ content = json .dumps (func_response .response )
310
+ )
311
+
284
312
def _create_state_delta_event (
285
313
self ,
286
314
state_delta : Dict [str , Any ],
0 commit comments