""" agent_last_action_memory.py 에이전트를 재시작하더라도 직전에 실행했던 action 및 결과를 기억하기 위한 간단 파일 메모리. """ from __future__ import annotations import json import os from typing import Any LAST_ACTION_CACHE_FILE = "agent_last_action_memory.json" BASE_DIR = os.path.dirname(os.path.abspath(__file__)) def _cache_path() -> str: return os.path.join(BASE_DIR, LAST_ACTION_CACHE_FILE) def load_last_action_memory() -> dict[str, Any]: path = _cache_path() try: if not os.path.exists(path): return {} with open(path, "r", encoding="utf-8") as f: raw = f.read().strip() if not raw: return {} data = json.loads(raw) return data if isinstance(data, dict) else {} except Exception: return {} def save_last_action_memory(memory: dict[str, Any]) -> None: try: if not isinstance(memory, dict): return with open(_cache_path(), "w", encoding="utf-8") as f: json.dump(memory, f, ensure_ascii=False) except Exception: # 메모리는 부가 기능이므로 저장 실패는 전체 동작에 영향 주지 않음 pass