from __future__ import annotations from typing import Any from yuxi.channels.models import ChannelMessage, ChatType class WeChatResolverAdapter: def __init__(self): self._alias_map: dict[str, str] = {} def load_aliases(self, config: dict[str, Any]) -> None: self._alias_map.clear() aliases = config.get("aliases", {}) if isinstance(aliases, dict): self._alias_map.update(aliases) @staticmethod def looks_like_id(raw: str) -> bool: if not raw or not raw.strip(): return False return raw.strip().startswith("wx:") or bool(raw.strip()) @staticmethod def resolve_target(raw: str) -> dict[str, Any]: raw = raw.strip() if raw.startswith("wx:group:"): return {"to": raw, "chatType": "group"} if raw.startswith("wx:"): return {"to": raw, "chatType": "direct"} return {"to": f"wx:{raw}", "chatType": "direct"} @property def hint(self) -> str: return "格式: wx: 或 wx:group:" def resolve_alias(self, alias: str) -> str | None: return self._alias_map.get(alias) def resolve_target_with_aliases(self, raw: str) -> dict[str, Any]: target = self.resolve_target(raw) alias_resolved = self._alias_map.get(target["to"]) if alias_resolved: target["to"] = alias_resolved target["alias_resolved"] = True return target @staticmethod def format_target_display(target: dict[str, Any]) -> str: to = target.get("to", "unknown") chat_type = target.get("chatType", "direct") return f"[{chat_type}] {to}" @staticmethod def build_cross_context_presentation( source_ctx: dict[str, Any], target_ctx: dict[str, Any] | None = None, ) -> str: src_peer = source_ctx.get("peer_name", source_ctx.get("to", "unknown")) return f"[转发自 {src_peer}]" @staticmethod def resolve_session_target(message: ChannelMessage) -> dict[str, Any]: return { "to": message.identity.channel_user_id, "chatId": message.identity.channel_chat_id, "chatType": message.metadata.get("chat_type", "direct"), } @staticmethod def infer_chat_type(target_id: str) -> ChatType: if "group" in target_id: return ChatType.GROUP return ChatType.DIRECT def list_aliases(self) -> list[dict[str, str]]: return [{"alias": alias, "target": target} for alias, target in self._alias_map.items()]