import json as _json
from pathlib import Path

# ROOT now resolved via frozen_path (PyInstaller-safe)
from frozen_path import APP_ROOT as _ROOT
import config_manager
_STATE_FILE = config_manager.get_data_dir() / 'bridge' / 'state' / 'agent_state.json'

def get_state() -> dict:
    if not _STATE_FILE.exists():
        return {}
    try:
        return _json.loads(_STATE_FILE.read_text(encoding="utf-8"))
    except Exception:
        return {}

def set_state(state: dict) -> None:
    _STATE_FILE.parent.mkdir(parents=True, exist_ok=True)
    _STATE_FILE.write_text(_json.dumps(state, indent=2, ensure_ascii=False))
