import logging from yuxi.channel.extensions.tlon.sse_client import UrbitSSEClient logger = logging.getLogger(__name__) async def discover_channels(client: UrbitSSEClient) -> list[str]: try: init_data = await client.scry("/groups-ui/v6/init.json") except Exception as e: logger.warning("[tlon] Failed to scry groups-ui init: %s", e) return [] channels = [] for group in init_data.get("groups", {}).values(): for channel in group.get("channels", []): if channel.startswith("chat/"): channels.append(channel) return channels async def discover_and_track(client: UrbitSSEClient, watched_channels: set[str]) -> set[str]: discovered = await discover_channels(client) new_channels = set() for nest in discovered: if nest not in watched_channels: watched_channels.add(nest) new_channels.add(nest) logger.info("[tlon] New channel discovered: %s", nest) return new_channels