File size: 1,128 Bytes
1760db0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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}")