The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway/platforms): qqbot contextlib.suppress for swallow-only try/except, __init__ layout
Teknium9fd798cc688b
diff --git a/gateway/platforms/qqbot/adapter.py b/gateway/platforms/qqbot/adapter.pyindex 6184078904a..6a4ea923c81 100644--- a/gateway/platforms/qqbot/adapter.py+++ b/gateway/platforms/qqbot/adapter.py@@ -11,6 +11,7 @@ tries QQ's free ``asr_refer_text`` first, then the configured STT provider.from __future__ import annotationsimport asyncio+import contextlibimport jsonimport loggingimport os@@ -132,10 +133,8 @@ class QQAdapter(BasePlatformAdapter):extra = config.extra or {}self._app_id = str(extra.get("app_id") or _resolve_qq_secret("QQ_APP_ID", "")).strip()- self._client_secret = str(- extra.get("client_secret") or _resolve_qq_secret("QQ_CLIENT_SECRET", "")).strip()+ self._client_secret = str(extra.get("client_secret") or _resolve_qq_secret("QQ_CLIENT_SECRET", "")).strip()self._markdown_support = bool(extra.get("markdown_support", True))-self._dm_policy = str(extra.get("dm_policy", "pairing")).strip().lower()self._allow_from = _coerce_list(extra.get("allow_from") or extra.get("allowFrom"))self._group_policy = str(extra.get("group_policy", "pairing")).strip().lower()@@ -150,12 +149,10 @@ class QQAdapter(BasePlatformAdapter):self._session_id: Optional[str] = Noneself._last_seq: Optional[int] = Noneself._chat_type_map: Dict[str, str] = {} # chat_id → "c2c"|"group"|"guild"|"dm"-self._pending_responses: Dict[str, asyncio.Future] = {} # request/response correlationself._seen_messages: Dict[str, float] = {}self._last_msg_id: Dict[str, str] = {} # last inbound message ID per chat (send_typing)self._typing_sent_at: Dict[str, float] = {} # typing debounce: chat_id → last send_typing ts-self._access_token: Optional[str] = Noneself._token_expires_at: float = 0.0self._token_lock = asyncio.Lock()@@ -163,9 +160,8 @@ class QQAdapter(BasePlatformAdapter):# Inline-keyboard interaction routing: invoked for every INTERACTION_CREATE# after the adapter ACKed it. Defaults to the approval/update-prompt# dispatcher; override via set_interaction_callback() (None drops clicks).- self._interaction_callback: Optional[- Callable[[InteractionEvent], Awaitable[None]]- ] = self._default_interaction_dispatch+ self._interaction_callback: Optional[Callable[[InteractionEvent], Awaitable[None]]] = (+ self._default_interaction_dispatch)# ── Properties ──@@ -237,10 +233,8 @@ class QQAdapter(BasePlatformAdapter):"""Cancel and await *task* (if any); always returns None for reassignment."""if task:task.cancel()- try:+ with contextlib.suppress(asyncio.CancelledError):await task- except asyncio.CancelledError:- passreturn Noneasync def _close_ws(self) -> None:@@ -449,7 +443,7 @@ class QQAdapter(BasePlatformAdapter):async def _heartbeat_loop(self) -> None:"""Send op 1 heartbeats with the latest seq at 80% of the Hello interval."""- try:+ with contextlib.suppress(asyncio.CancelledError):while self._running:await asyncio.sleep(self._heartbeat_interval)if not self._ws or self._ws.closed:@@ -458,8 +452,6 @@ class QQAdapter(BasePlatformAdapter):await self._ws.send_json({"op": 1, "d": self._last_seq})except Exception as exc:logger.debug("[%s] Heartbeat failed: %s", self._log_tag, exc)- except asyncio.CancelledError:- passasync def _send_ws_auth(self, name: str, payload: Dict[str, Any], sent_msg: str, *log_args) -> bool:"""Send an Identify/Resume payload; returns False if the send raised."""@@ -1077,10 +1069,8 @@ class QQAdapter(BasePlatformAdapter):@staticmethoddef _unlink_quiet(path: str) -> None:- try:+ with contextlib.suppress(OSError):os.unlink(path)- except OSError:- pass@staticmethoddef _wav_ok(wav_path: str) -> bool:@@ -1685,16 +1675,11 @@ class QQAdapter(BasePlatformAdapter):def _parse_qq_timestamp(self, raw: str) -> datetime:"""Parse a QQ timestamp — ISO 8601 string (current) or integer ms (legacy)."""- if not raw:- return datetime.now(tz=timezone.utc)- try:- return datetime.fromisoformat(raw)- except (ValueError, TypeError):- pass- try:- return datetime.fromtimestamp(int(raw) / 1000, tz=timezone.utc)- except (ValueError, TypeError):- pass+ if raw:+ with contextlib.suppress(ValueError, TypeError):+ return datetime.fromisoformat(raw)+ with contextlib.suppress(ValueError, TypeError):+ return datetime.fromtimestamp(int(raw) / 1000, tz=timezone.utc)return datetime.now(tz=timezone.utc)def _is_duplicate(self, msg_id: str) -> bool: