Skip to content

Commit 40c8b0f

Browse files
author
Pontus
committed
fix: persist events after plugin on_event_callback modifications
1 parent 82fa10b commit 40c8b0f

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

src/google/adk/runners.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -806,49 +806,44 @@ async def _exec_with_plugin(
806806
'Appending transcription finished event: %s', event
807807
)
808808
if self._should_append_event(event, is_live_call):
809-
await self.session_service.append_event(
810-
session=session, event=event
811-
)
809+
event = await self.__append_event(invocation_context, session, event)
812810

813811
for buffered_event in buffered_events:
814812
logger.debug('Appending buffered event: %s', buffered_event)
815-
await self.session_service.append_event(
816-
session=session, event=buffered_event
817-
)
813+
buffered_event = await self.__append_event(invocation_context, session, buffered_event)
818814
yield buffered_event # yield buffered events to caller
819815
buffered_events = []
820816
else:
821817
# non-transcription event or empty transcription event, for
822818
# example, event that stores blob reference, should be appended.
823819
if self._should_append_event(event, is_live_call):
824820
logger.debug('Appending non-buffered event: %s', event)
825-
await self.session_service.append_event(
826-
session=session, event=event
827-
)
821+
event = await self.__append_event(invocation_context, session, event)
822+
# Run the on_event callbacks to optionally modify the event.
828823
else:
829-
if event.partial is not True:
830-
await self.session_service.append_event(
831-
session=session, event=event
832-
)
824+
event = await self.__append_event(invocation_context, session, event)
825+
yield event
833826

834-
# Step 3: Run the on_event callbacks to optionally modify the event.
835-
modified_event = await plugin_manager.run_on_event_callback(
836-
invocation_context=invocation_context, event=event
837-
)
838-
if modified_event:
839-
_apply_run_config_custom_metadata(
840-
modified_event, invocation_context.run_config
841-
)
842-
yield modified_event
843-
else:
844-
yield event
845827

846-
# Step 4: Run the after_run callbacks to perform global cleanup tasks or
847-
# finalizing logs and metrics data.
848-
# This does NOT emit any event.
849-
await plugin_manager.run_after_run_callback(
850-
invocation_context=invocation_context
828+
async def __append_event(self, invocation_context: InvocationContext, session: Session, event: Event) -> None:
829+
"""Appends an event to the session with plugin callbacks.
830+
831+
Args:
832+
invocation_context: The invocation context.
833+
session: The session to append the event to.
834+
event: The event to process and append to the session.
835+
"""
836+
plugin_manager = invocation_context.plugin_manager
837+
modified_event = await plugin_manager.run_on_event_callback(
838+
invocation_context=invocation_context, event=event
851839
)
840+
if modified_event:
841+
event = modified_event
842+
_apply_run_config_custom_metadata(
843+
event, invocation_context.run_config
844+
)
845+
await self.session_service.append_event(session=session, event=event)
846+
return event
852847

853848
async def _append_new_message_to_session(
854849
self,

0 commit comments

Comments
 (0)