47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from yuxi.utils.logging_config import logger
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatConfiguredBindingAdapter:
|
||
|
|
def __init__(self):
|
||
|
|
self._bindings: dict[str, str] = {}
|
||
|
|
|
||
|
|
def load_bindings(self, config: dict[str, Any]) -> None:
|
||
|
|
self._bindings.clear()
|
||
|
|
bindings = config.get("bindings", {})
|
||
|
|
if isinstance(bindings, dict):
|
||
|
|
for raw_key, agent_id in bindings.items():
|
||
|
|
normalized = raw_key if raw_key.startswith("wx:") else f"wx:{raw_key}"
|
||
|
|
self._bindings[normalized] = str(agent_id)
|
||
|
|
logger.info(f"[WeChat/Bindings] Loaded {len(self._bindings)} configured binding(s)")
|
||
|
|
|
||
|
|
def resolve_agent_for_peer(self, channel_user_id: str) -> str | None:
|
||
|
|
normalized = channel_user_id if channel_user_id.startswith("wx:") else f"wx:{channel_user_id}"
|
||
|
|
return self._bindings.get(normalized)
|
||
|
|
|
||
|
|
def list_bindings(self) -> list[dict[str, str]]:
|
||
|
|
return [{"peer_id": peer, "agent_id": agent} for peer, agent in self._bindings.items()]
|
||
|
|
|
||
|
|
def add_binding(self, peer_id: str, agent_id: str) -> None:
|
||
|
|
normalized = peer_id if peer_id.startswith("wx:") else f"wx:{peer_id}"
|
||
|
|
self._bindings[normalized] = agent_id
|
||
|
|
logger.info(f"[WeChat/Bindings] Added binding: {normalized} → Agent {agent_id}")
|
||
|
|
|
||
|
|
def remove_binding(self, peer_id: str) -> bool:
|
||
|
|
normalized = peer_id if peer_id.startswith("wx:") else f"wx:{peer_id}"
|
||
|
|
existed = self._bindings.pop(normalized, None)
|
||
|
|
if existed:
|
||
|
|
logger.info(f"[WeChat/Bindings] Removed binding: {normalized}")
|
||
|
|
return True
|
||
|
|
return False
|
||
|
|
|
||
|
|
def has_binding(self, peer_id: str) -> bool:
|
||
|
|
normalized = peer_id if peer_id.startswith("wx:") else f"wx:{peer_id}"
|
||
|
|
return normalized in self._bindings
|
||
|
|
|
||
|
|
def serialize_bindings(self) -> dict[str, str]:
|
||
|
|
return dict(self._bindings)
|