import os from pathlib import Path HF_TOKEN = os.environ.get("HF_TOKEN", "") DATASET_REPO = os.environ.get("OPENCLAW_DATASET_REPO", "") PERSISTENT_FILES = {"topic_history.json", "fact_history.json", "token.pickle"} def pull_state(base_dir): if not DATASET_REPO or not HF_TOKEN: print("Skipping hub pull: missing env vars"); return from huggingface_hub import hf_hub_download for fname in PERSISTENT_FILES: try: hf_hub_download(repo_id=DATASET_REPO, filename=fname, repo_type="dataset", token=HF_TOKEN, local_dir=str(base_dir), local_dir_use_symlinks=False) print(f" pulled {fname}") except Exception as e: print(f" skipped {fname}: {e}") def push_file(local_path): if not DATASET_REPO or not HF_TOKEN: return p = Path(local_path) if p.name not in PERSISTENT_FILES or not p.exists(): return try: from huggingface_hub import HfApi HfApi(token=HF_TOKEN).upload_file( path_or_fileobj=str(p), path_in_repo=p.name, repo_id=DATASET_REPO, repo_type="dataset", token=HF_TOKEN, commit_message=f"auto: {p.name}") except Exception as e: print(f" push failed {p.name}: {e}") def push_all_state(base_dir): for fname in PERSISTENT_FILES: push_file(Path(base_dir) / fname)