|
| 1 | +"""Custom MemoryService implementation that persists sessions to a JSON file.""" |
| 2 | + |
| 3 | +import ast # Import ast for literal_eval |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import os |
| 7 | +from dataclasses import dataclass # Import dataclass |
| 8 | +from typing import Any, Dict, List, Tuple |
| 9 | + |
| 10 | +from google.adk.memory import BaseMemoryService |
| 11 | +from google.adk.sessions import Session |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +# Define type for the internal session storage key |
| 16 | +SessionKey = Tuple[str, str, str] # (app_name, user_id, session_id) |
| 17 | + |
| 18 | + |
| 19 | +# Define a simple response structure for load_memory/search_memory |
| 20 | +@dataclass |
| 21 | +class MemoryServiceResponse: |
| 22 | + memories: List[Dict[str, Any]] |
| 23 | + |
| 24 | + |
| 25 | +class JsonFileMemoryService(BaseMemoryService): |
| 26 | + """ |
| 27 | + An implementation of BaseMemoryService that stores sessions in memory |
| 28 | + and persists them to/loads them from a JSON file. |
| 29 | +
|
| 30 | + The load_memory implementation is a basic substring search. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, filepath: str): |
| 34 | + """ |
| 35 | + Initializes the service, loading existing data from the JSON file if it exists. |
| 36 | +
|
| 37 | + Args: |
| 38 | + filepath: The path to the JSON file for persistence. |
| 39 | + """ |
| 40 | + super().__init__() |
| 41 | + self.filepath = filepath |
| 42 | + # Store Session objects directly, keyed by the tuple |
| 43 | + self._sessions: Dict[SessionKey, Session] = {} |
| 44 | + self._load_from_json() |
| 45 | + |
| 46 | + def _get_session_key(self, session: Session) -> SessionKey: |
| 47 | + """Helper to generate the dictionary key for a session.""" |
| 48 | + # Use the correct attribute name 'id' based on Session model |
| 49 | + return (session.app_name, session.user_id, session.id) |
| 50 | + |
| 51 | + def _load_from_json(self): |
| 52 | + """Loads session data from the JSON file into the internal dictionary.""" |
| 53 | + if not os.path.exists(self.filepath): |
| 54 | + logger.info(f"Memory file not found at {self.filepath}. Starting with empty memory.") |
| 55 | + self._sessions = {} |
| 56 | + return |
| 57 | + |
| 58 | + logger.info(f"Loading memory from {self.filepath}...") |
| 59 | + try: |
| 60 | + with open(self.filepath, "r", encoding="utf-8") as f: |
| 61 | + serialized_sessions: Dict[str, Dict[str, Any]] = json.load(f) |
| 62 | + |
| 63 | + # Store validated Session objects |
| 64 | + loaded_sessions: Dict[SessionKey, Session] = {} |
| 65 | + for key_str, session_data in serialized_sessions.items(): |
| 66 | + try: |
| 67 | + # Convert string key back to tuple using safe evaluation |
| 68 | + key_tuple = ast.literal_eval(key_str) |
| 69 | + if not isinstance(key_tuple, tuple) or len(key_tuple) != 3: |
| 70 | + logger.warning(f"Invalid key format loaded from JSON: {key_str}. Skipping.") |
| 71 | + continue |
| 72 | + key: SessionKey = key_tuple # Cast to type hint |
| 73 | + |
| 74 | + # Recreate Session object from stored data |
| 75 | + session = Session.model_validate(session_data) |
| 76 | + # Store the Session object with the correct tuple key |
| 77 | + loaded_sessions[key] = session |
| 78 | + except (ValueError, SyntaxError, TypeError) as e: |
| 79 | + logger.warning(f"Failed to parse key {key_str}: {e}. Skipping.") |
| 80 | + except Exception as e: |
| 81 | + logger.warning(f"Failed to validate session data for key {key_str}: {e}. Skipping.") |
| 82 | + self._sessions = loaded_sessions |
| 83 | + logger.info(f"Successfully loaded {len(self._sessions)} sessions from {self.filepath}.") |
| 84 | + |
| 85 | + except FileNotFoundError: |
| 86 | + logger.info(f"Memory file not found at {self.filepath}. Starting with empty memory.") |
| 87 | + self._sessions = {} |
| 88 | + except json.JSONDecodeError as e: |
| 89 | + logger.error(f"Error decoding JSON from {self.filepath}: {e}. Starting with empty memory.") |
| 90 | + self._sessions = {} |
| 91 | + except Exception as e: |
| 92 | + logger.error(f"Unexpected error loading memory from {self.filepath}: {e}. Starting with empty memory.") |
| 93 | + self._sessions = {} |
| 94 | + |
| 95 | + def _save_to_json(self): |
| 96 | + """Saves the current internal session dictionary to the JSON file.""" |
| 97 | + logger.info(f"Saving memory ({len(self._sessions)} sessions) to {self.filepath}...") |
| 98 | + # Serialize Session objects using Pydantic's model_dump |
| 99 | + # Use a string representation of the tuple key for JSON compatibility |
| 100 | + serialized_sessions: Dict[str, Dict[str, Any]] = {str(key): session.model_dump(mode="json") for key, session in self._sessions.items()} |
| 101 | + |
| 102 | + try: |
| 103 | + # Ensure directory exists |
| 104 | + os.makedirs(os.path.dirname(self.filepath), exist_ok=True) |
| 105 | + with open(self.filepath, "w", encoding="utf-8") as f: |
| 106 | + json.dump(serialized_sessions, f, indent=4) |
| 107 | + except IOError as e: |
| 108 | + logger.error(f"Error saving memory to {self.filepath}: {e}") |
| 109 | + except TypeError as e: |
| 110 | + logger.error(f"Unexpected error saving memory to {self.filepath}: {e}") |
| 111 | + |
| 112 | + def add_session_to_memory(self, session: Session): |
| 113 | + """ |
| 114 | + Adds a completed session to the memory store and persists to JSON. |
| 115 | +
|
| 116 | + Args: |
| 117 | + session: The Session object to add. |
| 118 | + """ |
| 119 | + # Add logging to see if runner calls this |
| 120 | + logger.info(f"JsonFileMemoryService.add_session_to_memory called by Runner? Session ID: {getattr(session, 'session_id', 'N/A')}") |
| 121 | + |
| 122 | + if not isinstance(session, Session): |
| 123 | + logger.warning(f"Attempted to add non-Session object to memory: {type(session)}") |
| 124 | + return |
| 125 | + |
| 126 | + key = self._get_session_key(session) |
| 127 | + logger.debug(f"Adding session with key {key} to memory.") |
| 128 | + self._sessions[key] = session # Store the session object |
| 129 | + self._save_to_json() # Persist after adding |
| 130 | + |
| 131 | + def load_memory(self, query: str, **kwargs) -> MemoryServiceResponse: |
| 132 | + """ |
| 133 | + Retrieves relevant information based on a query. |
| 134 | +
|
| 135 | + Args: |
| 136 | + query: The natural language query string. |
| 137 | + **kwargs: Additional keyword arguments (currently ignored). |
| 138 | +
|
| 139 | + Returns: |
| 140 | + A MemoryServiceResponse containing a list of dictionaries, |
| 141 | + each representing a relevant message's session data. |
| 142 | + """ |
| 143 | + logger.info(f"Loading memory with query: '{query}'") |
| 144 | + results: List[Dict[str, Any]] = [] |
| 145 | + query_lower = query.lower() |
| 146 | + |
| 147 | + for session in self._sessions.values(): # Iterate over Session objects |
| 148 | + session_matched = False |
| 149 | + # Access history directly from the Session object |
| 150 | + if session.history: |
| 151 | + for message in session.history: |
| 152 | + # Access parts directly from the Content object in history |
| 153 | + message_text = "" |
| 154 | + if message.parts: |
| 155 | + message_text = "".join([part.text for part in message.parts if hasattr(part, "text") and part.text is not None]).lower() |
| 156 | + |
| 157 | + if query_lower in message_text: |
| 158 | + session_matched = True |
| 159 | + break # Found a match in this session's history |
| 160 | + |
| 161 | + if session_matched: |
| 162 | + # Add relevant session data (e.g., the whole session dump) |
| 163 | + results.append(session.model_dump(mode="json")) |
| 164 | + |
| 165 | + logger.info(f"Found {len(results)} relevant session(s) for query: '{query}'") |
| 166 | + # Return as an instance of the dataclass |
| 167 | + return MemoryServiceResponse(memories=results) |
| 168 | + |
| 169 | + # Implementing the abstract method required by BaseMemoryService |
| 170 | + # Type hint reflects Base class, but implementation delegates to load_memory which returns MemoryServiceResponse |
| 171 | + def search_memory(self, query: str, **kwargs) -> List[Dict[str, Any]]: |
| 172 | + """ |
| 173 | + Searches the stored sessions for relevant information based on a query. |
| 174 | + This method fulfills the abstract requirement from BaseMemoryService. |
| 175 | + """ |
| 176 | + # For this implementation, search_memory simply delegates to load_memory. |
| 177 | + # A more sophisticated implementation might differ. |
| 178 | + logger.debug(f"search_memory called, delegating to load_memory for query: '{query}'") |
| 179 | + response = self.load_memory(query, **kwargs) |
| 180 | + # Base class expects List[Dict], extract from dataclass |
| 181 | + return response.memories |
| 182 | + |
| 183 | + def get_memory_service_info(self) -> Dict[str, Any]: |
| 184 | + """ |
| 185 | + Returns information about this memory service. |
| 186 | + """ |
| 187 | + return { |
| 188 | + "service_type": "JsonFileMemoryService", |
| 189 | + "description": "Stores session memory in a local JSON file.", |
| 190 | + "filepath": self.filepath, |
| 191 | + "current_session_count": len(self._sessions), |
| 192 | + "capabilities": {"persistence": True, "search_type": "basic_substring"}, |
| 193 | + } |
0 commit comments