from __future__ import annotations import os from pathlib import Path from yuxi.utils.logging_config import logger def resolve_auth_dir(instance_id: str, config: dict, account_id: str | None = None) -> Path: base = config.get("dataDir", config.get("data_dir", "")) channel_id = config.get("channelId", config.get("channel_id", instance_id)) if base: auth_dir = Path(base) / "whatsapp" / channel_id else: auth_dir = Path(os.getcwd()) / "data" / "channels" / "whatsapp" / channel_id if account_id: auth_dir = auth_dir / "accounts" / account_id auth_dir.mkdir(parents=True, exist_ok=True) logger.debug(f"AuthDir resolved: {auth_dir}") return auth_dir def ensure_auth_dir(auth_dir: Path) -> Path: try: auth_dir.mkdir(parents=True, exist_ok=True) test_file = auth_dir / ".write_test" test_file.write_text("ok") test_file.unlink() except (OSError, PermissionError) as e: raise RuntimeError(f"Cannot write to auth_dir {auth_dir}: {e}") from e return auth_dir