Load memory from file, return empty dict if file doesn't exist
(memory_file)
| 12 | |
| 13 | |
| 14 | async def _load_memory(memory_file) -> Dict[str, Any]: |
| 15 | """Load memory from file, return empty dict if file doesn't exist""" |
| 16 | |
| 17 | try: |
| 18 | # Ensure directory exists |
| 19 | Path(memory_file).parent.mkdir(parents=True, exist_ok=True) |
| 20 | |
| 21 | if not os.path.exists(memory_file): |
| 22 | return {} |
| 23 | |
| 24 | # Read file synchronously (aiofiles would be better but adds dependency) |
| 25 | with open(memory_file, "r", encoding="utf-8") as f: |
| 26 | return json.load(f) |
| 27 | except Exception as e: |
| 28 | # If file is corrupted, log error and start fresh |
| 29 | print(f"Warning: Could not load memory file {memory_file}: {e}") |
| 30 | return {} |
| 31 | |
| 32 | |
| 33 | async def _save_memory(memory_data: Dict[str, Any], memory_file: str) -> bool: |
no outgoing calls