from __future__ import annotations from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from googleapiclient.discovery import Resource from yuxi.utils.logging_config import logger async def list_spaces( chat_service: Resource, page_size: int = 50, page_token: str | None = None, filter_str: str | None = None, ) -> tuple[list[dict], str | None]: kwargs: dict[str, Any] = {"pageSize": page_size} if page_token: kwargs["pageToken"] = page_token if filter_str: kwargs["filter"] = filter_str try: result = chat_service.spaces().list(**kwargs).execute() return result.get("spaces", []), result.get("nextPageToken") except Exception as e: logger.warning(f"list_spaces failed: {e}") return [], None def list_peers(allow_from: list[str]) -> list[dict[str, Any]]: peers: list[dict[str, Any]] = [] for entry in allow_from: if entry == "*": continue normalized = normalize_id(entry) peers.append({"id": normalized, "raw": entry, "type": "user"}) return peers def list_groups(groups_config: dict[str, Any]) -> list[dict[str, Any]]: groups: list[dict[str, Any]] = [] for space_name, cfg in groups_config.items(): if not isinstance(cfg, dict): continue groups.append( { "id": space_name, "type": "space", "name": cfg.get("name", space_name), "enabled": cfg.get("enabled", True), "requires_mention": cfg.get("require_mention", cfg.get("requireMention", False)), } ) return groups def normalize_id(raw: str) -> str: if not raw: return "" raw = raw.strip() if raw.startswith("spaces/"): return raw if raw.startswith("users/"): return raw if raw.startswith("user:"): return raw.replace("user:", "users/", 1) if "@" in raw: return f"users/{raw}" return f"users/{raw}"