import json import os from pathlib import Path try: from huggingface_hub import HfApi HF_AVAILABLE = True except ImportError: HF_AVAILABLE = False print("huggingface_hub not found. Backup disabled.") CONFIG_DIR = Path(".openclaw") MEMORY_FILE = CONFIG_DIR / "cain_memory.json" REPO_ID = os.environ.get("SPACE_ID", "tao-shen/HuggingClaw-Cain") def save_memory(memory_data: dict): """Save local memory state and commit to dataset.""" print(f"[Cain] Saving memory to local dataset...") CONFIG_DIR.mkdir(exist_ok=True) with open(MEMORY_FILE, "w") as f: json.dump(memory_data, f, indent=2) if HF_AVAILABLE: try: api = HfApi() api.upload_file( path_or_fileobj=MEMORY_FILE, path_in_repo=MEMORY_FILE.as_posix(), repo_id=REPO_ID, repo_type="dataset", commit_message="Update Cain memory" ) print(f"[Cain] Memory successfully synced to Dataset.") except Exception as e: print(f"[Cain] Warning: Failed to sync to dataset: {e}")