-
Notifications
You must be signed in to change notification settings - Fork 40
feat: enable agent to automatically save session #402
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
BhAem
wants to merge
2
commits into
volcengine:main
Choose a base branch
from
BhAem:feat/auto_save_session
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.
+186
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
|
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. put this file in veadk/long_term_memory? |
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 |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| # Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import time | ||
| from google.adk.agents.callback_context import CallbackContext | ||
| from veadk.config import getenv | ||
| from veadk.utils.logger import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # Session-level cache for tracking save state | ||
| # Format: {(app_name, user_id, session_id): {'last_save_time': float, 'last_event_count': int}} | ||
| _session_save_cache: dict = {} | ||
|
|
||
| # Track active session per user to detect session switches | ||
| # Format: {(app_name, user_id): session_id} | ||
| _active_sessions: dict = {} | ||
|
|
||
| # Configurable thresholds | ||
| MIN_MESSAGES_THRESHOLD = getenv( | ||
| "MIN_MESSAGES_THRESHOLD", 10 | ||
| ) # Minimum number of new messages before saving | ||
| MIN_TIME_THRESHOLD = getenv( | ||
| "MIN_TIME_THRESHOLD", 60 | ||
| ) # Minimum seconds between saves (1 minute) | ||
|
|
||
|
|
||
| async def save_session_to_long_term_memory( | ||
| callback_context: CallbackContext, | ||
| ) -> None: | ||
| """Save the current session to long-term memory. | ||
|
|
||
| Args: | ||
| callback_context: The callback context containing invocation information. | ||
|
|
||
| Returns: | ||
| None | ||
| """ | ||
| try: | ||
| agent = callback_context._invocation_context.agent | ||
|
|
||
| long_term_memory = getattr(agent, "long_term_memory", None) | ||
| if not long_term_memory: | ||
| logger.error( | ||
| "Long-term memory is not initialized in agent, cannot save session to memory." | ||
| ) | ||
| return None | ||
|
|
||
| app_name = callback_context._invocation_context.app_name | ||
| user_id = callback_context._invocation_context.user_id | ||
| session_id = callback_context._invocation_context.session.id | ||
| session_service = callback_context._invocation_context.session_service | ||
|
|
||
| current_time = time.time() | ||
|
|
||
| # Detect session switch and force save previous session | ||
| user_key = (app_name, user_id) | ||
| previous_session_id = _active_sessions.get(user_key) | ||
|
|
||
| if previous_session_id and previous_session_id != session_id: | ||
| logger.info( | ||
| f"Session switch detected for user {user_id}: " | ||
| f"{previous_session_id} -> {session_id}. " | ||
| f"Force saving previous session." | ||
| ) | ||
| old_session = await session_service.get_session( | ||
| app_name=app_name, | ||
| user_id=user_id, | ||
| session_id=previous_session_id, | ||
| ) | ||
| if old_session: | ||
| old_events = getattr(old_session, "events", []) | ||
| old_event_count = len(old_events) | ||
| await long_term_memory.add_session_to_memory(old_session) | ||
| old_cache_key = (app_name, user_id, previous_session_id) | ||
|
|
||
| _session_save_cache[old_cache_key] = { | ||
| "last_save_time": current_time, | ||
| "last_event_count": old_event_count, | ||
| } | ||
| logger.info( | ||
| f"Previous session `{old_session.id}` saved to long term memory due to session switch." | ||
| ) | ||
|
|
||
| # Update active session | ||
| _active_sessions[user_key] = session_id | ||
|
|
||
| session = await session_service.get_session( | ||
| app_name=app_name, | ||
| user_id=user_id, | ||
| session_id=session_id, | ||
| ) | ||
|
|
||
| if not session: | ||
| logger.error( | ||
| f"Session {session_id} (app_name={app_name}, user_id={user_id}) not found in session service, cannot save to long-term memory." | ||
| ) | ||
| return None | ||
|
|
||
| current_events = getattr(session, "events", []) | ||
| current_event_count = len(current_events) | ||
| # logger.debug(f"Current event count: {current_event_count}") | ||
|
|
||
| # Create cache key | ||
| cache_key = (app_name, user_id, session_id) | ||
|
|
||
| cache_info = _session_save_cache.get(cache_key) | ||
|
|
||
| if cache_info: | ||
| last_save_time = cache_info.get("last_save_time", 0) | ||
| last_event_count = cache_info.get("last_event_count", 0) | ||
|
|
||
| time_elapsed = current_time - last_save_time | ||
| new_events_count = current_event_count - last_event_count | ||
|
|
||
| # Check if we should skip save | ||
| if ( | ||
| time_elapsed < MIN_TIME_THRESHOLD | ||
| and new_events_count < MIN_MESSAGES_THRESHOLD | ||
| ): | ||
| logger.info( | ||
| f"Skipping save for session {session_id}: " | ||
| f"only {new_events_count} new events (need {MIN_MESSAGES_THRESHOLD}) " | ||
| f"and {time_elapsed:.1f}s elapsed (need {MIN_TIME_THRESHOLD}s)" | ||
| ) | ||
| return None | ||
| else: | ||
| logger.info(f"First save for session {session_id}.") | ||
|
|
||
| # Save to long-term memory | ||
| await long_term_memory.add_session_to_memory(session) | ||
|
|
||
| # Update cache | ||
| _session_save_cache[cache_key] = { | ||
| "last_save_time": current_time, | ||
| "last_event_count": current_event_count, | ||
| } | ||
|
|
||
| logger.info(f"Add session `{session.id}` to long term memory.") | ||
|
|
||
| return None | ||
|
|
||
| except AttributeError as e: | ||
| logger.error(f"AttributeError while saving session to memory: {e}") | ||
| return None | ||
| except Exception as e: | ||
| logger.error(f"Unexpected error while saving session to memory: {e}") | ||
| return None |
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.
.append(...)?