-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: persist events after plugin on_event_callback modifications #4239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ponts
wants to merge
1
commit into
google:main
Choose a base branch
from
Ponts:persist-modified-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+39
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -777,9 +777,6 @@ async def _exec_with_plugin( | |||||||||
|
|
||||||||||
| async with Aclosing(execute_fn(invocation_context)) as agen: | ||||||||||
| async for event in agen: | ||||||||||
| _apply_run_config_custom_metadata( | ||||||||||
| event, invocation_context.run_config | ||||||||||
| ) | ||||||||||
| if is_live_call: | ||||||||||
| if event.partial and _is_transcription(event): | ||||||||||
| is_transcribing = True | ||||||||||
|
|
@@ -806,14 +803,14 @@ async def _exec_with_plugin( | |||||||||
| 'Appending transcription finished event: %s', event | ||||||||||
| ) | ||||||||||
| if self._should_append_event(event, is_live_call): | ||||||||||
| await self.session_service.append_event( | ||||||||||
| session=session, event=event | ||||||||||
| event = await self._append_event( | ||||||||||
| invocation_context, session, event | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| for buffered_event in buffered_events: | ||||||||||
| logger.debug('Appending buffered event: %s', buffered_event) | ||||||||||
| await self.session_service.append_event( | ||||||||||
| session=session, event=buffered_event | ||||||||||
| buffered_event = await self._append_event( | ||||||||||
| invocation_context, session, buffered_event | ||||||||||
| ) | ||||||||||
| yield buffered_event # yield buffered events to caller | ||||||||||
| buffered_events = [] | ||||||||||
|
|
@@ -822,33 +819,46 @@ async def _exec_with_plugin( | |||||||||
| # example, event that stores blob reference, should be appended. | ||||||||||
| if self._should_append_event(event, is_live_call): | ||||||||||
| logger.debug('Appending non-buffered event: %s', event) | ||||||||||
| await self.session_service.append_event( | ||||||||||
| session=session, event=event | ||||||||||
| event = await self._append_event( | ||||||||||
| invocation_context, session, event | ||||||||||
| ) | ||||||||||
| # Run the on_event callbacks to optionally modify the event. | ||||||||||
| else: | ||||||||||
| if event.partial is not True: | ||||||||||
| await self.session_service.append_event( | ||||||||||
| session=session, event=event | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Step 3: Run the on_event callbacks to optionally modify the event. | ||||||||||
| modified_event = await plugin_manager.run_on_event_callback( | ||||||||||
| invocation_context=invocation_context, event=event | ||||||||||
| ) | ||||||||||
| if modified_event: | ||||||||||
| _apply_run_config_custom_metadata( | ||||||||||
| modified_event, invocation_context.run_config | ||||||||||
| event = await self._append_event( | ||||||||||
| invocation_context, session, event | ||||||||||
| ) | ||||||||||
| yield modified_event | ||||||||||
| else: | ||||||||||
| yield event | ||||||||||
| yield event | ||||||||||
| # Run after_run_callbacks to perform global cleanup tasks or finalizing logs and metrics data | ||||||||||
| # This does NOT emit any event. | ||||||||||
| await plugin_manager.run_after_run_callback( | ||||||||||
| invocation_context=invocation_context | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| # Step 4: Run the after_run callbacks to perform global cleanup tasks or | ||||||||||
| # finalizing logs and metrics data. | ||||||||||
| # This does NOT emit any event. | ||||||||||
| await plugin_manager.run_after_run_callback( | ||||||||||
| invocation_context=invocation_context | ||||||||||
| async def _append_event( | ||||||||||
| self, | ||||||||||
| invocation_context: InvocationContext, | ||||||||||
| session: Session, | ||||||||||
| event: Event, | ||||||||||
| ) -> Event: | ||||||||||
| """Appends an event to the session with plugin callbacks. | ||||||||||
|
|
||||||||||
| Args: | ||||||||||
| invocation_context: The invocation context. | ||||||||||
| session: The session to append the event to. | ||||||||||
| event: The event to process and append to the session. | ||||||||||
|
|
||||||||||
| Returns: | ||||||||||
| The event after processing by plugins. | ||||||||||
| """ | ||||||||||
| plugin_manager = invocation_context.plugin_manager | ||||||||||
| modified_event = await plugin_manager.run_on_event_callback( | ||||||||||
| invocation_context=invocation_context, event=event | ||||||||||
| ) | ||||||||||
| if modified_event: | ||||||||||
| event = modified_event | ||||||||||
|
Comment on lines
+857
to
+858
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For clarity and to avoid potential issues with an
Suggested change
|
||||||||||
| _apply_run_config_custom_metadata(event, invocation_context.run_config) | ||||||||||
| await self.session_service.append_event(session=session, event=event) | ||||||||||
| return event | ||||||||||
|
|
||||||||||
| async def _append_new_message_to_session( | ||||||||||
| self, | ||||||||||
|
|
||||||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment appears to be a leftover from the refactoring and is now misleading, as the
on_eventcallbacks are executed within the_append_eventmethod. I suggest removing this comment to avoid confusion.