Skip to content

Commit 1790c4b

Browse files
committed
function response
this is essential for scenerios when user has to render function response at frontend
1 parent bae3bd7 commit 1790c4b

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

typescript-sdk/integrations/adk-middleware/src/adk_middleware/event_translator.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
BaseEvent, EventType,
1212
TextMessageStartEvent, TextMessageContentEvent, TextMessageEndEvent,
1313
ToolCallStartEvent, ToolCallArgsEvent, ToolCallEndEvent,
14-
ToolCallChunkEvent,
14+
ToolCallChunkEvent,ToolCallResultEvent,
1515
StateSnapshotEvent, StateDeltaEvent,
1616
MessagesSnapshotEvent,
1717
CustomEvent,
1818
Message, AssistantMessage, UserMessage, ToolMessage
1919
)
20-
20+
import json
2121
from google.adk.events import Event as ADKEvent
2222

2323
import logging
@@ -87,13 +87,6 @@ async def translate(
8787

8888

8989

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
9790

9891
# call _translate_function_calls function to yield Tool Events
9992
if hasattr(adk_event, 'get_function_calls'):
@@ -104,6 +97,15 @@ async def translate(
10497
async for event in self._translate_function_calls(function_calls):
10598
yield event
10699

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+
107109

108110
# Handle state changes
109111
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(
281283
# Clean up tracking
282284
self._active_tool_calls.pop(tool_call_id, None)
283285

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+
284312
def _create_state_delta_event(
285313
self,
286314
state_delta: Dict[str, Any],

0 commit comments

Comments
 (0)