adapter.py 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. """Weixin (WeChat personal account) adapter over Tencent's iLink Bot API. Long-poll ``getupdates`` drives inbound
  2. delivery; every outbound reply must echo the peer's latest ``context_token``; media moves through an AES-128-ECB
  3. encrypted CDN; ``qr_login`` backs the gateway setup wizard."""
  4. from __future__ import annotations
  5. import asyncio, base64, contextlib, hashlib, json, logging, mimetypes, os, re, secrets, tempfile, textwrap, time, uuid
  6. from datetime import datetime
  7. from functools import partial
  8. from pathlib import Path
  9. from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple
  10. from urllib.parse import quote, urlparse
  11. logger = logging.getLogger(__name__)
  12. WEIXIN_COPY_LINE_WIDTH = 120
  13. try:
  14. import aiohttp
  15. except ImportError: # pragma: no cover - dependency gate
  16. aiohttp = None # type: ignore[assignment]
  17. AIOHTTP_AVAILABLE = aiohttp is not None
  18. try:
  19. from cryptography.hazmat.backends import default_backend
  20. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  21. except ImportError: # pragma: no cover - dependency gate
  22. default_backend = Cipher = algorithms = modes = None # type: ignore[assignment]
  23. CRYPTO_AVAILABLE = Cipher is not None
  24. from gateway.config import Platform, PlatformConfig
  25. from gateway.platforms.helpers import MessageDeduplicator, cancel_task, greedy_pack_blocks
  26. from gateway.platforms.access_policy_mixin import OwnAccessPolicyMixin
  27. from agent.i18n import t
  28. from gateway.platforms.base import (
  29. _IMAGE_EXTS, _VIDEO_EXTS, gateway_trust_env, BasePlatformAdapter, SendResult,
  30. cache_audio_from_bytes_async, cache_document_from_bytes_async, cache_image_from_bytes_async,
  31. )
  32. from gateway.platforms.event import MessageEvent, MessageType
  33. from hermes_constants import get_hermes_home
  34. from utils import atomic_json_write
  35. from gateway.platforms._shared import env_is_connected, extra_or_secret as _extra_or_env, get_scoped_secret as _wx_secret
  36. from gateway.platforms.send_targets import StandaloneSendPolicy, regex_groups_parser
  37. def _extra_or_secret(extra: dict[str, Any], key: str, default: str = "") -> str:
  38. """``config.extra[key]`` first, else the scoped ``WEIXIN_<KEY>``; stripped."""
  39. return str(_extra_or_env(extra, key, f"WEIXIN_{key.upper()}", default)).strip()
  40. ILINK_BASE_URL = "https://ilinkai.weixin.qq.com"
  41. WEIXIN_CDN_BASE_URL = "https://novac2c.cdn.weixin.qq.com/c2c"
  42. ILINK_APP_ID, CHANNEL_VERSION, ILINK_APP_CLIENT_VERSION = "bot", "2.2.0", (2 << 16) | (2 << 8) | 0
  43. EP_GET_UPDATES, EP_SEND_MESSAGE, EP_SEND_TYPING = "ilink/bot/getupdates", "ilink/bot/sendmessage", "ilink/bot/sendtyping"
  44. EP_GET_CONFIG, EP_GET_UPLOAD_URL = "ilink/bot/getconfig", "ilink/bot/getuploadurl"
  45. EP_GET_BOT_QR, EP_GET_QR_STATUS = "ilink/bot/get_bot_qrcode", "ilink/bot/get_qrcode_status"
  46. LONG_POLL_TIMEOUT_MS, API_TIMEOUT_MS, CONFIG_TIMEOUT_MS, QR_TIMEOUT_MS = 35_000, 15_000, 10_000, 35_000
  47. MAX_CONSECUTIVE_FAILURES, RETRY_DELAY_SECONDS, BACKOFF_DELAY_SECONDS = 3, 2, 30
  48. SESSION_EXPIRED_ERRCODE, RATE_LIMIT_ERRCODE = -14, -2 # -2: iLink frequency limit — backoff and retry
  49. MESSAGE_DEDUP_TTL_SECONDS = 300
  50. MEDIA_IMAGE, MEDIA_VIDEO, MEDIA_FILE, MEDIA_VOICE = 1, 2, 3, 4 # getuploadurl media_type
  51. ITEM_TEXT, ITEM_IMAGE, ITEM_VOICE, ITEM_FILE, ITEM_VIDEO = 1, 2, 3, 4, 5 # item_list entry types
  52. MSG_TYPE_BOT, MSG_STATE_FINISH = 2, 2
  53. TYPING_START, TYPING_STOP = 1, 2
  54. _LIVE_ADAPTERS: dict[str, Any] = {}
  55. _HEADER_RE = re.compile(r"^(#{1,6})\s+(.+?)\s*$")
  56. _TABLE_RULE_RE = re.compile(r"^\s*\|?(?:\s*:?-{3,}:?\s*\|)+\s*:?-{3,}:?\s*\|?\s*$")
  57. _FENCE_RE = re.compile(r"^```([^\n`]*)\s*$")
  58. def _is_stale_session_ret(ret: Optional[int], errcode: Optional[int], errmsg: Optional[str]) -> bool:
  59. """Recognize stale-session variants of iLink's ``-2`` response, not real rate limits."""
  60. return (ret == RATE_LIMIT_ERRCODE or errcode == RATE_LIMIT_ERRCODE) and (errmsg or "").lower() in {
  61. "unknown error",
  62. "prepare failed",
  63. }
  64. def _is_session_expired(resp: dict[str, Any], ret: Any, errcode: Any) -> bool:
  65. return SESSION_EXPIRED_ERRCODE in (ret, errcode) or _is_stale_session_ret(ret, errcode, resp.get("errmsg") or resp.get("msg"))
  66. def _session_not_ready_error(ret: Any, errcode: Any, errmsg: Any) -> RuntimeError:
  67. """The stale-session ``-2`` after the tokenless re-send is exhausted (or with no token to drop): iLink will not
  68. prepare a bot-initiated send until this peer messages the bot again. Deterministic, so it is neither retried nor
  69. fed to the rate-limit breaker (#80125). The text must not contain "rate limit" — ``classify_send_error`` would
  70. route it back into the rate-limited redelivery lane."""
  71. return RuntimeError(
  72. f"iLink sendmessage session not ready: ret={ret} errcode={errcode} errmsg={errmsg or 'unknown error'}"
  73. " — the user must send the bot a message first (or re-pair)")
  74. def _make_ssl_connector() -> Optional[aiohttp.TCPConnector]:
  75. """TCPConnector with certifi's CA bundle (``ilinkai.weixin.qq.com`` fails some system stores, e.g. Homebrew
  76. OpenSSL); None when no certifi bundle resolves (absent, or deleted by an install-environment rotation and
  77. not re-derivable), so aiohttp's default (honors ``SSL_CERT_FILE`` under trust_env) applies.
  78. ``keepalive_timeout=2`` + ``enable_cleanup_closed`` drain idle CLOSE_WAIT sockets behind proxies like Warp.
  79. Uses a tight ``keepalive_timeout=2`` (default aiohttp: 30s) so idle connections drain promptly behind
  80. proxies like Cloudflare Warp that leave peer-initiated FIN in ``CLOSE_WAIT`` (same class as #18451).
  81. ``enable_cleanup_closed=True`` helps the connector clean up sockets that the remote side has already
  82. closed.
  83. """
  84. try:
  85. import ssl
  86. from agent.ssl_verify import certifi_bundle_path
  87. except ImportError:
  88. return None
  89. if not AIOHTTP_AVAILABLE:
  90. return None
  91. # Re-derived when an install-environment rotation deleted the directory certifi was imported
  92. # from, so a long-lived gateway does not pin a cacert.pem that no longer exists (#137255).
  93. cafile = certifi_bundle_path()
  94. if cafile is None:
  95. return None
  96. return aiohttp.TCPConnector(ssl=ssl.create_default_context(cafile=cafile), keepalive_timeout=2, enable_cleanup_closed=True)
  97. def _new_session(**kwargs: Any) -> aiohttp.ClientSession:
  98. return aiohttp.ClientSession(trust_env=gateway_trust_env(), connector=_make_ssl_connector(), **kwargs)
  99. def check_weixin_requirements() -> bool:
  100. return AIOHTTP_AVAILABLE and CRYPTO_AVAILABLE
  101. def _safe_id(value: Optional[str], keep: int = 8) -> str:
  102. raw = str(value or "").strip()
  103. return raw[:keep] if raw else "?"
  104. def _pkcs7_pad(data: bytes, block_size: int = 16) -> bytes:
  105. pad_len = block_size - (len(data) % block_size)
  106. return data + bytes([pad_len] * pad_len)
  107. def _aes128_ecb_encrypt(plaintext: bytes, key: bytes) -> bytes:
  108. encryptor = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()).encryptor()
  109. return encryptor.update(_pkcs7_pad(plaintext)) + encryptor.finalize()
  110. def _aes128_ecb_decrypt(ciphertext: bytes, key: bytes) -> bytes:
  111. """Decrypt and strip PKCS#7 padding when it is well-formed (else return the raw block output)."""
  112. decryptor = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()).decryptor()
  113. padded = decryptor.update(ciphertext) + decryptor.finalize()
  114. pad_len = padded[-1] if padded else 0
  115. if 1 <= pad_len <= 16 and padded.endswith(bytes([pad_len]) * pad_len):
  116. return padded[:-pad_len]
  117. return padded
  118. def _headers(token: Optional[str], body: str) -> dict[str, str]:
  119. uin = base64.b64encode(str(int.from_bytes(secrets.token_bytes(4), "big")).encode("utf-8")).decode("ascii")
  120. return {
  121. "Content-Type": "application/json", "AuthorizationType": "ilink_bot_token",
  122. "Content-Length": str(len(body.encode("utf-8"))), "X-WECHAT-UIN": uin,
  123. "iLink-App-Id": ILINK_APP_ID, "iLink-App-ClientVersion": str(ILINK_APP_CLIENT_VERSION),
  124. **({"Authorization": f"Bearer {token}"} if token else {}),
  125. }
  126. def _account_dir(hermes_home: str) -> Path:
  127. path = Path(hermes_home) / "weixin" / "accounts"
  128. path.mkdir(parents=True, exist_ok=True)
  129. return path
  130. def _read_json(path: Path) -> Any:
  131. try:
  132. # utf-8-sig (ours): tolerate BOM-persisted JSON files.
  133. return json.loads(path.read_text(encoding="utf-8-sig")) if path.exists() else None
  134. except Exception:
  135. return None
  136. def save_weixin_account(hermes_home: str, *, account_id: str, token: str, base_url: str, user_id: str = "") -> None:
  137. path = _account_dir(hermes_home) / f"{account_id}.json"
  138. saved_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
  139. atomic_json_write(path, {"token": token, "base_url": base_url, "user_id": user_id, "saved_at": saved_at})
  140. with contextlib.suppress(OSError):
  141. path.chmod(0o600)
  142. def load_weixin_account(hermes_home: str, account_id: str) -> Optional[dict[str, Any]]:
  143. return _read_json(_account_dir(hermes_home) / f"{account_id}.json")
  144. class ContextTokenStore:
  145. """Disk-backed ``context_token`` cache keyed by account + peer."""
  146. def __init__(self, hermes_home: str):
  147. self._root = _account_dir(hermes_home)
  148. self._cache: dict[str, str] = {}
  149. # Serializes the offloaded flushes so two concurrent set() calls
  150. # cannot land their writes out of order (last-writer-wins would drop
  151. # the newer token from disk).
  152. self._persist_lock = asyncio.Lock()
  153. @staticmethod
  154. def _key(account_id: str, user_id: str) -> str:
  155. return f"{account_id}:{user_id}"
  156. def restore(self, account_id: str) -> None:
  157. path = self._root / f"{account_id}.context-tokens.json"
  158. if not path.exists():
  159. return
  160. try:
  161. data = json.loads(path.read_text(encoding="utf-8-sig"))
  162. except Exception as exc:
  163. logger.warning("weixin: failed to restore context tokens for %s: %s", _safe_id(account_id), exc)
  164. return
  165. restored = {self._key(account_id, u): t for u, t in data.items() if isinstance(t, str) and t}
  166. self._cache.update(restored)
  167. if restored:
  168. logger.info("weixin: restored %d context token(s) for %s", len(restored), _safe_id(account_id))
  169. def get(self, account_id: str, user_id: str) -> Optional[str]:
  170. return self._cache.get(self._key(account_id, user_id))
  171. async def set(self, account_id: str, user_id: str, token: str) -> None:
  172. self._cache[self._key(account_id, user_id)] = token
  173. # atomic_json_write() fsyncs, so the flush is offloaded off the loop; the payload is snapshotted
  174. # here (the worker never iterates ``_cache`` mid-mutation) and the lock keeps flushes in order.
  175. async with self._persist_lock:
  176. prefix = f"{account_id}:"
  177. payload = {key[len(prefix):]: value for key, value in self._cache.items() if key.startswith(prefix)}
  178. await asyncio.to_thread(self._persist, account_id, payload)
  179. def _persist(self, account_id: str, payload: dict[str, str]) -> None:
  180. try:
  181. atomic_json_write(self._root / f"{account_id}.context-tokens.json", payload)
  182. except Exception as exc:
  183. logger.warning("weixin: failed to persist context tokens for %s: %s", _safe_id(account_id), exc)
  184. class TypingTicketCache:
  185. """Short-lived typing ticket cache from ``getconfig``."""
  186. def __init__(self, ttl_seconds: float = 600.0):
  187. self._ttl_seconds = ttl_seconds
  188. self._cache: dict[str, tuple[str, float]] = {}
  189. def get(self, user_id: str) -> Optional[str]:
  190. entry = self._cache.get(user_id)
  191. if entry and time.time() - entry[1] < self._ttl_seconds:
  192. return entry[0]
  193. self._cache.pop(user_id, None)
  194. return None
  195. def set(self, user_id: str, ticket: str) -> None:
  196. self._cache[user_id] = (ticket, time.time())
  197. def _parse_aes_key(aes_key_b64: str) -> bytes:
  198. decoded = base64.b64decode(aes_key_b64)
  199. if len(decoded) == 16:
  200. return decoded
  201. text = decoded.decode("ascii", errors="ignore") if len(decoded) == 32 else ""
  202. if text and all(ch in "0123456789abcdefABCDEF" for ch in text):
  203. return bytes.fromhex(text)
  204. raise ValueError(f"unexpected aes_key format ({len(decoded)} decoded bytes)")
  205. def _guess_chat_type(message: dict[str, Any], account_id: str) -> tuple[str, str]:
  206. room_id = str(message.get("room_id") or message.get("chat_room_id") or "").strip()
  207. to_user_id = str(message.get("to_user_id") or "").strip()
  208. if room_id or (to_user_id and account_id and to_user_id != account_id and message.get("msg_type") == 1):
  209. return "group", room_id or to_user_id or str(message.get("from_user_id") or "")
  210. return "dm", str(message.get("from_user_id") or "")
  211. # HTTP helpers enforce timeouts via asyncio.wait_for(), not aiohttp ClientTimeout, which raises
  212. # "Timeout context manager should be used inside a task" under run_coroutine_threadsafe() from cron.
  213. async def _api_request(
  214. session: aiohttp.ClientSession, method: str, *, base_url: str, endpoint: str, headers: dict[str, str], timeout_ms: int, body: Optional[str] = None,
  215. ) -> dict[str, Any]:
  216. async def _do() -> dict[str, Any]:
  217. kwargs = {"data": body} if body is not None else {}
  218. async with getattr(session, method.lower())(f"{base_url.rstrip('/')}/{endpoint}", headers=headers, **kwargs) as response:
  219. raw = await response.text()
  220. if not response.ok:
  221. raise RuntimeError(f"iLink {method} {endpoint} HTTP {response.status}: {raw[:200]}")
  222. return json.loads(raw)
  223. return await asyncio.wait_for(_do(), timeout=timeout_ms / 1000)
  224. async def _api_post(
  225. session: aiohttp.ClientSession, *, base_url: str, endpoint: str, payload: dict[str, Any], token: Optional[str], timeout_ms: int,
  226. ) -> dict[str, Any]:
  227. body = json.dumps({**payload, "base_info": {"channel_version": CHANNEL_VERSION}}, ensure_ascii=False, separators=(",", ":"))
  228. return await _api_request(session, "POST", base_url=base_url, endpoint=endpoint, headers=_headers(token, body), timeout_ms=timeout_ms, body=body)
  229. async def _api_get(session: aiohttp.ClientSession, *, base_url: str, endpoint: str, timeout_ms: int) -> dict[str, Any]:
  230. headers = {"iLink-App-Id": ILINK_APP_ID, "iLink-App-ClientVersion": str(ILINK_APP_CLIENT_VERSION)}
  231. return await _api_request(session, "GET", base_url=base_url, endpoint=endpoint, headers=headers, timeout_ms=timeout_ms)
  232. async def _get_updates(session: aiohttp.ClientSession, *, base_url: str, token: str, sync_buf: str, timeout_ms: int) -> dict[str, Any]:
  233. try:
  234. return await _api_post(session, base_url=base_url, endpoint=EP_GET_UPDATES, payload={"get_updates_buf": sync_buf}, token=token, timeout_ms=timeout_ms)
  235. except TimeoutError:
  236. return {"ret": 0, "msgs": [], "get_updates_buf": sync_buf}
  237. async def _send_items(
  238. session: aiohttp.ClientSession, *, base_url: str, token: str, to: str, item_list: list[dict[str, Any]], context_token: Optional[str], client_id: str,
  239. ) -> dict[str, Any]:
  240. message: dict[str, Any] = {
  241. "from_user_id": "", "to_user_id": to, "client_id": client_id, "message_type": MSG_TYPE_BOT, "message_state": MSG_STATE_FINISH,
  242. "item_list": item_list}
  243. if context_token:
  244. message["context_token"] = context_token
  245. return await _api_post(session, base_url=base_url, endpoint=EP_SEND_MESSAGE, payload={"msg": message}, token=token, timeout_ms=API_TIMEOUT_MS)
  246. async def _send_message(
  247. session: aiohttp.ClientSession, *, base_url: str, token: str, to: str, text: str, context_token: Optional[str], client_id: str,
  248. ) -> dict[str, Any]:
  249. if not text or not text.strip():
  250. raise ValueError("_send_message: text must not be empty")
  251. item_list = [{"type": ITEM_TEXT, "text_item": {"text": text}}]
  252. return await _send_items(session, base_url=base_url, token=token, to=to, item_list=item_list, context_token=context_token, client_id=client_id)
  253. async def _get_config(session: aiohttp.ClientSession, *, base_url: str, token: str, user_id: str, context_token: Optional[str]) -> dict[str, Any]:
  254. payload: dict[str, Any] = {"ilink_user_id": user_id}
  255. if context_token:
  256. payload["context_token"] = context_token
  257. return await _api_post(session, base_url=base_url, endpoint=EP_GET_CONFIG, payload=payload, token=token, timeout_ms=CONFIG_TIMEOUT_MS)
  258. async def _get_upload_url(
  259. session: aiohttp.ClientSession, *, base_url: str, token: str, to_user_id: str, media_type: int, filekey: str, rawsize: int,
  260. rawfilemd5: str, filesize: int, aeskey_hex: str,
  261. ) -> dict[str, Any]:
  262. payload = {
  263. "filekey": filekey, "media_type": media_type, "to_user_id": to_user_id, "rawsize": rawsize, "rawfilemd5": rawfilemd5,
  264. "filesize": filesize, "no_need_thumb": True, "aeskey": aeskey_hex}
  265. return await _api_post(session, base_url=base_url, endpoint=EP_GET_UPLOAD_URL, payload=payload, token=token, timeout_ms=API_TIMEOUT_MS)
  266. async def _upload_ciphertext(session: aiohttp.ClientSession, *, ciphertext: bytes, upload_url: str) -> str:
  267. async def _do() -> str:
  268. async with session.post(upload_url, data=ciphertext, headers={"Content-Type": "application/octet-stream"}) as response:
  269. encrypted_param = response.headers.get("x-encrypted-param") if response.status == 200 else None
  270. if encrypted_param:
  271. await response.read()
  272. return encrypted_param
  273. raw = (await response.text())[:200]
  274. raise RuntimeError(f"CDN upload missing x-encrypted-param header: {raw}" if response.status == 200 else f"CDN upload HTTP {response.status}: {raw}")
  275. return await asyncio.wait_for(_do(), timeout=120)
  276. async def _download_bytes(session: aiohttp.ClientSession, *, url: str, timeout_seconds: float = 60.0) -> bytes:
  277. async def _do() -> bytes:
  278. async with session.get(url) as response:
  279. response.raise_for_status()
  280. return await response.read()
  281. return await asyncio.wait_for(_do(), timeout=timeout_seconds)
  282. _WEIXIN_CDN_ALLOWLIST: frozenset[str] = frozenset({
  283. "novac2c.cdn.weixin.qq.com", "ilinkai.weixin.qq.com", "wx.qlogo.cn", "thirdwx.qlogo.cn", "res.wx.qq.com", "mmbiz.qpic.cn", "mmbiz.qlogo.cn"})
  284. def _assert_weixin_cdn_url(url: str) -> None:
  285. try:
  286. parsed = urlparse(url)
  287. scheme, host = parsed.scheme.lower(), parsed.hostname or ""
  288. except Exception as exc:
  289. raise ValueError(f"Unparseable media URL: {url!r}") from exc
  290. if scheme not in {"http", "https"}:
  291. raise ValueError(f"Media URL has disallowed scheme {scheme!r}; only http/https are permitted.")
  292. if host not in _WEIXIN_CDN_ALLOWLIST:
  293. raise ValueError(f"Media URL host {host!r} is not in the WeChat CDN allowlist. Refusing to fetch to prevent SSRF.")
  294. async def _download_and_decrypt_media(
  295. session: aiohttp.ClientSession, *, cdn_base_url: str, encrypted_query_param: Optional[str], aes_key_b64: Optional[str],
  296. full_url: Optional[str], timeout_seconds: float,
  297. ) -> bytes:
  298. if encrypted_query_param:
  299. url = f"{cdn_base_url.rstrip('/')}/download?encrypted_query_param={quote(encrypted_query_param, safe='')}"
  300. elif full_url:
  301. _assert_weixin_cdn_url(full_url)
  302. url = full_url
  303. else:
  304. raise RuntimeError("media item had neither encrypt_query_param nor full_url")
  305. raw = await _download_bytes(session, url=url, timeout_seconds=timeout_seconds)
  306. return _aes128_ecb_decrypt(raw, _parse_aes_key(aes_key_b64)) if aes_key_b64 else raw
  307. def _walk_markdown_lines(content: str):
  308. """Yield ``(rstripped line, is_fence, in_code_block)`` per line; ``in_code_block`` is the state *before* a fence toggles it."""
  309. in_code_block = False
  310. for raw_line in content.splitlines():
  311. line = raw_line.rstrip()
  312. is_fence = bool(_FENCE_RE.match(line.strip()))
  313. yield line, is_fence, in_code_block
  314. if is_fence:
  315. in_code_block = not in_code_block
  316. def _normalize_markdown_blocks(content: str) -> str:
  317. """rstrip lines and collapse blank runs to one, leaving fenced code untouched."""
  318. result: list[str] = []
  319. blank_run = 0
  320. for line, is_fence, in_code_block in _walk_markdown_lines(content):
  321. if not is_fence and not in_code_block and not line.strip():
  322. blank_run += 1
  323. if blank_run <= 1:
  324. result.append("")
  325. continue
  326. if is_fence or not in_code_block:
  327. blank_run = 0
  328. result.append(line)
  329. return "\n".join(result).strip()
  330. def _wrap_copy_friendly_lines_for_weixin(content: str) -> str:
  331. """Wrap long display lines that are hard to copy in WeChat clients (not code/table lines)."""
  332. if not content:
  333. return content
  334. wrapped: list[str] = []
  335. for line, is_fence, in_code_block in _walk_markdown_lines(content):
  336. stripped = line.strip()
  337. keep = is_fence or in_code_block or len(line) <= WEIXIN_COPY_LINE_WIDTH or not stripped or stripped.startswith("|")
  338. if keep or _TABLE_RULE_RE.match(stripped):
  339. wrapped.append(line)
  340. else:
  341. wrapped.extend(textwrap.wrap(
  342. line, width=WEIXIN_COPY_LINE_WIDTH, break_long_words=False, break_on_hyphens=False, replace_whitespace=False,
  343. drop_whitespace=True) or [line])
  344. return "\n".join(wrapped).strip()
  345. def _split_markdown_blocks(content: str) -> list[str]:
  346. blocks: list[str] = []
  347. current: list[str] = []
  348. def flush() -> None:
  349. if current:
  350. blocks.append("\n".join(current).strip())
  351. current.clear()
  352. for line, is_fence, in_code_block in _walk_markdown_lines(content):
  353. if is_fence and not in_code_block: # opening fence starts a fresh block
  354. flush()
  355. if is_fence or in_code_block or line.strip():
  356. current.append(line)
  357. else:
  358. flush()
  359. if is_fence and in_code_block: # closing fence ends the block
  360. flush()
  361. flush()
  362. return [block for block in blocks if block]
  363. def _split_delivery_units_for_weixin(content: str) -> list[str]:
  364. """Top-level lines become units; fenced code stays intact; indented continuation lines attach to the previous line."""
  365. units: list[str] = []
  366. for block in _split_markdown_blocks(content):
  367. if _FENCE_RE.match(block.splitlines()[0].strip()):
  368. units.append(block)
  369. continue
  370. current: list[str] = []
  371. for raw_line in block.splitlines():
  372. line = raw_line.rstrip()
  373. if current and line.strip() and raw_line.startswith((" ", "\t")):
  374. current.append(line)
  375. continue
  376. units.append("\n".join(current).strip())
  377. current = [line] if line.strip() else []
  378. units.append("\n".join(current).strip())
  379. return [unit for unit in units if unit]
  380. def _looks_like_chatty_line_for_weixin(line: str) -> bool:
  381. stripped = line.strip()
  382. return bool(
  383. stripped and len(stripped) <= 48 and not line.startswith((" ", "\t")) and not stripped.startswith((">", "-", "*", "【", "#", "|"))
  384. and not _TABLE_RULE_RE.match(stripped) and not re.match(r"^\*\*[^*]+\*\*$", stripped) and not re.match(r"^\d+\.\s", stripped))
  385. def _should_split_short_chat_block_for_weixin(block: str) -> bool:
  386. """Split only chat-like multiline blocks (2-6 chatty lines, first line not a heading) into separate bubbles."""
  387. lines = [line for line in block.splitlines() if line.strip()]
  388. if not 2 <= len(lines) <= 6:
  389. return False
  390. first = lines[0].strip()
  391. if _HEADER_RE.match(first) or (len(first) <= 24 and first.endswith((":", ":"))):
  392. return False
  393. return all(_looks_like_chatty_line_for_weixin(line) for line in lines)
  394. def _pack_markdown_blocks_for_weixin(content: str, max_length: int) -> list[str]:
  395. if len(content) <= max_length:
  396. return [content]
  397. # Block extraction stays weixin-local (anchored _FENCE_RE + per-line rstrip); packing is shared.
  398. overflow = lambda block: BasePlatformAdapter.truncate_message(block, max_length)
  399. return greedy_pack_blocks(_split_markdown_blocks(content), max_length, overflow=overflow)
  400. def _split_text_for_weixin_delivery(content: str, max_length: int, split_per_line: bool = False) -> list[str]:
  401. """Compact (default): one message when it fits, unless it reads as a short chatty exchange (separate
  402. bubbles). Per-line (legacy ``extra.split_multiline_messages`` / ``WEIXIN_SPLIT_MULTILINE_MESSAGES``):
  403. top-level line breaks become separate messages. Oversized units use block-aware packing."""
  404. if not content:
  405. return []
  406. if split_per_line:
  407. if len(content) <= max_length and "\n" not in content:
  408. return [content]
  409. units = _split_delivery_units_for_weixin(content)
  410. chunks = [c for u in units for c in ([u] if len(u) <= max_length else _pack_markdown_blocks_for_weixin(u, max_length))]
  411. return [c for c in chunks if c] or [content]
  412. if len(content) > max_length:
  413. return _pack_markdown_blocks_for_weixin(content, max_length) or [content]
  414. return _split_delivery_units_for_weixin(content) if _should_split_short_chat_block_for_weixin(content) else [content]
  415. def _coerce_bool(value: Any, default: bool = True) -> bool:
  416. """Coerce a config value to bool, tolerating strings like ``"true"``; unknown -> ``default``."""
  417. if value is None:
  418. return default
  419. if isinstance(value, (int, float)):
  420. return bool(value)
  421. text = str(value).strip().lower()
  422. return True if text in {"1", "true", "yes", "on"} else False if text in {"0", "false", "no", "off"} else default
  423. def _extract_text(item_list: list[dict[str, Any]]) -> str:
  424. for item in item_list:
  425. if item.get("type") == ITEM_TEXT:
  426. text = str((item.get("text_item") or {}).get("text") or "")
  427. ref = item.get("ref_msg") or {}
  428. ref_item = ref.get("message_item") or {}
  429. if ref_item.get("type") in {ITEM_IMAGE, ITEM_VIDEO, ITEM_FILE, ITEM_VOICE}:
  430. title = ref.get("title") or ""
  431. return f"[引用媒体: {title}]\n{text}".strip() if title else f"[引用媒体]\n{text}".strip()
  432. if ref_item:
  433. parts = [p for p in (str(ref["title"]) if ref.get("title") else "", _extract_text([ref_item])) if p]
  434. if parts:
  435. return f"[引用: {' | '.join(parts)}]\n{text}".strip()
  436. return text
  437. for item in item_list:
  438. if item.get("type") == ITEM_VOICE:
  439. # Tencent's ``voice_item.text`` is their STT output and is wrong for non-Chinese audio.
  440. # When raw audio exists return "" so gateway/run.py's central STT transcribes the download;
  441. # otherwise use Weixin's transcript but mark its voice origin.
  442. # #27300: Tencent Cloud's `voice_item.text` is their STT output, which is wrong for any
  443. # non-Chinese audio (the original report was a Russian voice message that came back as English
  444. # gibberish). Return empty so the central STT pipeline in ``gateway/run.py`` produces the body
  445. # from the downloaded audio instead.
  446. voice_item = item.get("voice_item") or {}
  447. # Use it, but preserve the voice origin so the agent can distinguish this from text the user
  448. # typed (#65022).
  449. voice_text = str(voice_item.get("text") or "")
  450. if not (voice_item.get("media") or {}) and voice_text:
  451. return f"[Voice transcription provided by Weixin]\n{voice_text}"
  452. return ""
  453. _MIME_PREFIX_TYPES = (("image/", MessageType.PHOTO), ("video/", MessageType.VIDEO), ("audio/", MessageType.VOICE))
  454. def _message_type_from_media(media_types: list[str], text: str) -> MessageType:
  455. for prefix, message_type in _MIME_PREFIX_TYPES:
  456. if any(m.startswith(prefix) for m in media_types):
  457. return message_type
  458. return MessageType.DOCUMENT if media_types else MessageType.COMMAND if text.startswith("/") else MessageType.TEXT
  459. def _load_sync_buf(hermes_home: str, account_id: str) -> str:
  460. data = _read_json(_account_dir(hermes_home) / f"{account_id}.sync.json")
  461. return data.get("get_updates_buf", "") if isinstance(data, dict) else ""
  462. def _save_sync_buf(hermes_home: str, account_id: str, sync_buf: str) -> None:
  463. atomic_json_write(_account_dir(hermes_home) / f"{account_id}.sync.json", {"get_updates_buf": sync_buf})
  464. async def _fetch_qr(session: aiohttp.ClientSession, bot_type: str) -> tuple[str, str]:
  465. qr_resp = await _api_get(session, base_url=ILINK_BASE_URL, endpoint=f"{EP_GET_BOT_QR}?bot_type={bot_type}", timeout_ms=QR_TIMEOUT_MS)
  466. return str(qr_resp.get("qrcode") or ""), str(qr_resp.get("qrcode_img_content") or "")
  467. def _print_qr(qrcode_value: str, qrcode_url: str, *, report_render_error: bool) -> None:
  468. """Print the QR URL + ASCII render; WeChat must scan the liteapp URL (``qrcode_img_content``), not the bare hex token."""
  469. if qrcode_url:
  470. print(qrcode_url)
  471. try:
  472. import qrcode
  473. qr = qrcode.QRCode()
  474. qr.add_data(qrcode_url or qrcode_value)
  475. qr.make(fit=True)
  476. qr.print_ascii(invert=True)
  477. except Exception as _qr_exc:
  478. if report_render_error:
  479. print(f"(终端二维码渲染失败: {_qr_exc},请直接打开上面的二维码链接)")
  480. async def qr_login(hermes_home: str, *, bot_type: str = "3", timeout_seconds: int = 480) -> Optional[dict[str, str]]:
  481. if not AIOHTTP_AVAILABLE:
  482. raise RuntimeError("aiohttp is required for Weixin QR login")
  483. async with _new_session() as session:
  484. try:
  485. qrcode_value, qrcode_url = await _fetch_qr(session, bot_type)
  486. except Exception as exc:
  487. logger.error("weixin: failed to fetch QR code: %s", exc)
  488. return None
  489. if not qrcode_value:
  490. logger.error("weixin: QR response missing qrcode")
  491. return None
  492. print("\n请使用微信扫描以下二维码:")
  493. _print_qr(qrcode_value, qrcode_url, report_render_error=True)
  494. deadline = time.monotonic() + timeout_seconds
  495. current_base_url, refresh_count = ILINK_BASE_URL, 0
  496. while time.monotonic() < deadline:
  497. try:
  498. status_resp = await _api_get(session, base_url=current_base_url, endpoint=f"{EP_GET_QR_STATUS}?qrcode={qrcode_value}", timeout_ms=QR_TIMEOUT_MS)
  499. except Exception as exc:
  500. if not isinstance(exc, asyncio.TimeoutError):
  501. logger.warning("weixin: QR poll error: %s", exc)
  502. await asyncio.sleep(1)
  503. continue
  504. status = str(status_resp.get("status") or "wait")
  505. if status == "wait":
  506. print(".", end="", flush=True)
  507. elif status == "scaned":
  508. print("\n已扫码,请在微信里确认...")
  509. elif status == "scaned_but_redirect" and status_resp.get("redirect_host"):
  510. current_base_url = f"https://{status_resp['redirect_host']}"
  511. elif status == "expired":
  512. refresh_count += 1
  513. if refresh_count > 3:
  514. print("\n二维码多次过期,请重新执行登录。")
  515. return None
  516. print(f"\n二维码已过期,正在刷新... ({refresh_count}/3)")
  517. try:
  518. qrcode_value, qrcode_url = await _fetch_qr(session, bot_type)
  519. _print_qr(qrcode_value, qrcode_url, report_render_error=False)
  520. except Exception as exc:
  521. logger.error("weixin: QR refresh failed: %s", exc)
  522. return None
  523. elif status == "confirmed":
  524. creds = {
  525. "account_id": str(status_resp.get("ilink_bot_id") or ""), "token": str(status_resp.get("bot_token") or ""),
  526. "base_url": str(status_resp.get("baseurl") or ILINK_BASE_URL), "user_id": str(status_resp.get("ilink_user_id") or "")}
  527. if not creds["account_id"] or not creds["token"]:
  528. logger.error("weixin: QR confirmed but credential payload was incomplete")
  529. return None
  530. save_weixin_account(hermes_home, **creds)
  531. print(f"\n微信连接成功,account_id={creds['account_id']}")
  532. return creds
  533. await asyncio.sleep(1)
  534. print("\n微信登录超时。")
  535. return None
  536. # Outbound item shapes: item type -> (item key, extra fields after the shared ``media`` block).
  537. _ITEM_SHAPES: dict[int, tuple[str, Callable[[dict[str, Any]], dict[str, Any]]]] = {
  538. ITEM_FILE: ("file_item", lambda kw: {"file_name": kw["filename"], "len": str(kw["plaintext_size"])}),
  539. ITEM_IMAGE: ("image_item", lambda kw: {"mid_size": kw["ciphertext_size"]}),
  540. ITEM_VIDEO: ("video_item", lambda kw: {
  541. "video_size": kw["ciphertext_size"], "play_length": kw.get("play_length", 0), "video_md5": kw.get("rawfilemd5", "")}),
  542. ITEM_VOICE: ("voice_item", lambda kw: {
  543. "encode_type": kw.get("encode_type"), "bits_per_sample": kw.get("bits_per_sample"), "sample_rate": kw.get("sample_rate"),
  544. "playtime": kw.get("playtime", 0)}),
  545. }
  546. def _media_item(item_type: int, **kw: Any) -> dict[str, Any]:
  547. key, fields = _ITEM_SHAPES[item_type]
  548. media = {"encrypt_query_param": kw["encrypt_query_param"], "aes_key": kw["aes_key_for_api"], "encrypt_type": 1}
  549. return {"type": item_type, key: {"media": media, **fields(kw)}}
  550. _file_item, _image_item = partial(_media_item, ITEM_FILE), partial(_media_item, ITEM_IMAGE)
  551. _video_item, _voice_item = partial(_media_item, ITEM_VIDEO), partial(_media_item, ITEM_VOICE)
  552. # Inbound media dispatch: item type -> (item key, download timeout, cache fn, mime or None (= guess from
  553. # file_name), log label). Cache fns are lambdas so monkeypatching the module names takes effect at call time.
  554. _INBOUND_MEDIA: dict[int, tuple[str, float, Callable[[bytes, str], Awaitable[str]], Optional[str], str]] = {
  555. ITEM_IMAGE: ("image_item", 30.0, lambda data, _name: cache_image_from_bytes_async(data, ".jpg"), "image/jpeg", "image"),
  556. ITEM_VIDEO: ("video_item", 120.0, lambda data, _name: cache_document_from_bytes_async(data, "video.mp4"), "video/mp4", "video"),
  557. ITEM_FILE: ("file_item", 60.0, lambda data, name: cache_document_from_bytes_async(data, name), None, "file"),
  558. ITEM_VOICE: ("voice_item", 60.0, lambda data, _name: cache_audio_from_bytes_async(data, ".silk"), "audio/silk", "voice"),
  559. }
  560. # Outbound local-file dispatch by extension: (extensions, sender method, path kwarg); default = send_document.
  561. _OUTBOUND_BY_EXT: tuple[tuple[frozenset, str, str], ...] = (
  562. (frozenset({".ogg", ".opus", ".mp3", ".wav", ".m4a", ".flac"}), "send_voice", "audio_path"), # no .m2a, unlike base
  563. (_VIDEO_EXTS, "send_video", "video_path"),
  564. (_IMAGE_EXTS, "send_image_file", "image_path"),
  565. )
  566. _DIRECT_IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
  567. class WeixinAdapter(OwnAccessPolicyMixin, BasePlatformAdapter):
  568. ALLOW_ALL_ENV_PREFIX = "WEIXIN"
  569. supports_code_blocks = True # Weixin renders fenced code blocks
  570. splits_long_messages = True # send() chunks via _split_text()
  571. MAX_MESSAGE_LENGTH = 2000
  572. SUPPORTS_MESSAGE_EDITING = False # WeChat cannot edit; streaming must send-final-only so the cursor (▉) is never left visible
  573. _SPLIT_THRESHOLD = 1800 # iLink chunks at ~2048 chars
  574. def __init__(self, config: PlatformConfig):
  575. super().__init__(config, Platform.WEIXIN)
  576. extra = config.extra or {}
  577. self._hermes_home = hermes_home = str(get_hermes_home())
  578. self._token_store = ContextTokenStore(hermes_home)
  579. self._typing_cache = TypingTicketCache()
  580. self._poll_session = self._send_session = None # type: Optional[aiohttp.ClientSession]
  581. self._poll_task: Optional[asyncio.Task] = None
  582. self._dedup = MessageDeduplicator(ttl_seconds=MESSAGE_DEDUP_TTL_SECONDS)
  583. self._account_id = _extra_or_secret(extra, "account_id")
  584. self._token = str(config.token or extra.get("token") or _wx_secret("WEIXIN_TOKEN", "")).strip()
  585. self._base_url = _extra_or_secret(extra, "base_url", ILINK_BASE_URL).rstrip("/")
  586. self._cdn_base_url = _extra_or_secret(extra, "cdn_base_url", WEIXIN_CDN_BASE_URL).rstrip("/")
  587. # Tunables: ``extra.<key>`` else env ``WEIXIN_<KEY>`` (e.g. WEIXIN_SEND_CHUNK_RETRIES).
  588. self._send_chunk_delay_seconds = float(_extra_or_secret(extra, "send_chunk_delay_seconds", "1.5"))
  589. self._send_chunk_retries = int(_extra_or_secret(extra, "send_chunk_retries", "4"))
  590. self._send_chunk_retry_delay_seconds = float(_extra_or_secret(extra, "send_chunk_retry_delay_seconds", "1.0"))
  591. self._send_text_gate = asyncio.Lock()
  592. self._rate_limit_circuit_threshold = max(1, int(_extra_or_secret(extra, "rate_limit_circuit_threshold", "1")))
  593. self._rate_limit_circuit_window_seconds = float(_extra_or_secret(extra, "rate_limit_circuit_window_seconds", "30.0"))
  594. self._rate_limit_circuit_open_seconds = float(_extra_or_secret(extra, "rate_limit_circuit_open_seconds", "30.0"))
  595. self._rate_limit_circuit_until, self._rate_limit_events = 0.0, [] # type: float, List[float]
  596. self._dm_policy = _extra_or_secret(extra, "dm_policy", "pairing").lower()
  597. self._group_policy = _extra_or_secret(extra, "group_policy", "disabled").lower()
  598. # ``extra`` wins even when falsy (an explicit empty list disables the env allowlist).
  599. allow_from, group_allow_from = extra.get("allow_from"), extra.get("group_allow_from")
  600. self._allow_from = self._coerce_list(_wx_secret("WEIXIN_ALLOWED_USERS", "") if allow_from is None else allow_from)
  601. self._group_allow_from = self._coerce_list(_wx_secret("WEIXIN_GROUP_ALLOWED_USERS", "") if group_allow_from is None else group_allow_from)
  602. self._split_multiline_messages = _coerce_bool(_extra_or_secret(extra, "split_multiline_messages", ""), default=False)
  603. # Text debounce batching (Telegram pattern): iLink delivers messages individually, so rapid bursts would each
  604. # trigger a separate agent run. Telegram cadence and ceilings (#44883); ``0`` dispatches immediately.
  605. self._configure_text_batch_delays()
  606. persisted = load_weixin_account(hermes_home, self._account_id) if self._account_id and not self._token else None
  607. if persisted:
  608. self._token = str(persisted.get("token") or "").strip()
  609. self._base_url = str(persisted.get("base_url") or self._base_url).strip().rstrip("/")
  610. @staticmethod
  611. def _coerce_list(value: Any) -> list[str]:
  612. if value is None:
  613. return []
  614. items = value.split(",") if isinstance(value, str) else value if isinstance(value, (list, tuple, set)) else [value]
  615. return [str(item).strip() for item in items if str(item).strip()]
  616. async def connect(self, *, is_reconnect: bool = False) -> bool:
  617. preflight = (
  618. (check_weixin_requirements(), "weixin_missing_dependency", "aiohttp and cryptography are required"),
  619. (self._token, "weixin_missing_token", "WEIXIN_TOKEN is required"),
  620. (self._account_id, "weixin_missing_account", "WEIXIN_ACCOUNT_ID is required"))
  621. for ok, code, reason in preflight:
  622. if not ok:
  623. self._set_fatal_error(code, f"Weixin startup failed: {reason}", retryable=False)
  624. logger.warning("[%s] Weixin startup failed: %s", self.name, reason)
  625. return False
  626. try:
  627. if not self._acquire_platform_lock('weixin-bot-token', self._token, 'Weixin bot token'):
  628. return False
  629. except Exception as exc:
  630. logger.debug("[%s] Token lock unavailable (non-fatal): %s", self.name, exc)
  631. self._poll_session = _new_session()
  632. # total=None disables aiohttp's ClientTimeout so send() works via run_coroutine_threadsafe() from cron;
  633. # _api_post/_api_get enforce timeouts with asyncio.wait_for() instead.
  634. self._send_session = _new_session(timeout=aiohttp.ClientTimeout(total=None, connect=None, sock_connect=None, sock_read=None))
  635. self._token_store.restore(self._account_id)
  636. self._poll_task = asyncio.create_task(self._poll_loop(), name="weixin-poll")
  637. self._mark_connected()
  638. _LIVE_ADAPTERS[self._token] = self
  639. logger.info("[%s] Connected account=%s base=%s", self.name, _safe_id(self._account_id), self._base_url)
  640. if self._group_policy != "disabled":
  641. logger.warning(
  642. "[%s] WEIXIN_GROUP_POLICY=%s is set, but QR-login connects an iLink bot identity (e.g. ...@im.bot) "
  643. "which typically cannot be invited into ordinary WeChat groups. iLink usually does not deliver "
  644. "ordinary-group events for these accounts, so group messages may never reach Hermes regardless of "
  645. "this policy. If group delivery doesn't work, the limitation is on the iLink side, not in Hermes.",
  646. self.name, self._group_policy)
  647. self._wire_plugin_handlers(None) # plugin-registered native handlers
  648. return True
  649. async def disconnect(self) -> None:
  650. _LIVE_ADAPTERS.pop(self._token, None)
  651. self._running = False
  652. for task in self._pending_text_batch_tasks.values():
  653. if not task.done():
  654. task.cancel()
  655. self._pending_text_batches.clear()
  656. self._pending_text_batch_tasks.clear()
  657. await cancel_task(self._poll_task)
  658. self._poll_task = None
  659. for attr in ("_poll_session", "_send_session"):
  660. session = getattr(self, attr)
  661. if session and not session.closed:
  662. await session.close()
  663. setattr(self, attr, None)
  664. self._release_platform_lock()
  665. self._mark_disconnected()
  666. logger.info("[%s] Disconnected", self.name)
  667. async def _poll_loop(self) -> None:
  668. assert self._poll_session is not None
  669. sync_buf = _load_sync_buf(self._hermes_home, self._account_id)
  670. timeout_ms = LONG_POLL_TIMEOUT_MS
  671. consecutive_failures = 0
  672. async def backoff() -> int:
  673. """Sleep for the failure streak; returns the new streak count (0 after a full streak)."""
  674. streak_done = consecutive_failures >= MAX_CONSECUTIVE_FAILURES
  675. await asyncio.sleep(BACKOFF_DELAY_SECONDS if streak_done else RETRY_DELAY_SECONDS)
  676. return 0 if streak_done else consecutive_failures
  677. while self._running:
  678. try:
  679. response = await _get_updates(self._poll_session, base_url=self._base_url, token=self._token, sync_buf=sync_buf, timeout_ms=timeout_ms)
  680. suggested_timeout = response.get("longpolling_timeout_ms")
  681. if isinstance(suggested_timeout, int) and suggested_timeout > 0:
  682. timeout_ms = suggested_timeout
  683. ret, errcode = response.get("ret", 0), response.get("errcode", 0)
  684. if ret not in {0, None} or errcode not in {0, None}:
  685. if _is_session_expired(response, ret, errcode):
  686. logger.error("[%s] Session expired; pausing for 10 minutes", self.name)
  687. await asyncio.sleep(600)
  688. consecutive_failures = 0
  689. continue
  690. consecutive_failures += 1
  691. logger.warning("[%s] getUpdates failed ret=%s errcode=%s errmsg=%s (%d/%d)", self.name, ret, errcode,
  692. response.get("errmsg", ""), consecutive_failures, MAX_CONSECUTIVE_FAILURES)
  693. consecutive_failures = await backoff()
  694. continue
  695. consecutive_failures = 0
  696. # Dispatch before persisting: the off-loop write is an await, and a disconnect that
  697. # cancels it must not leave the advanced cursor on disk with this batch undelivered.
  698. for message in response.get("msgs") or []:
  699. asyncio.create_task(self._process_message_safe(message))
  700. # atomic_json_write fsyncs + renames: persist off the loop, and only when the cursor
  701. # moved (an empty long-poll echoes the same buffer back every cycle).
  702. if response.get("get_updates_buf") and str(response["get_updates_buf"]) != sync_buf:
  703. sync_buf = str(response["get_updates_buf"])
  704. await asyncio.to_thread(_save_sync_buf, self._hermes_home, self._account_id, sync_buf)
  705. except asyncio.CancelledError:
  706. break
  707. except Exception as exc:
  708. consecutive_failures += 1
  709. logger.error("[%s] poll error (%d/%d): %s", self.name, consecutive_failures, MAX_CONSECUTIVE_FAILURES, exc)
  710. consecutive_failures = await backoff()
  711. if consecutive_failures == 0:
  712. # Full failure streak: recycle the session. Failed connects through a local proxy (e.g.
  713. # Clash) strand sockets the keepalive reaper never sees; on macOS the 256-fd soft limit
  714. # then yields EMFILE and a crash. Closing the session tears down every socket.
  715. # Clash on 127.0.0.1:7890) can strand sockets that never return to the connector's
  716. # keepalive pool, so the tight keepalive_timeout never reaps them. On macOS the default
  717. # 256-fd soft limit turns that drip into `[Errno 24] Too many open files` and a gateway
  718. # crash (#79889). Closing the session tears down its connector and every socket it
  719. # holds; a fresh session starts the next attempt from zero fds.
  720. await self._recycle_poll_session()
  721. async def _recycle_poll_session(self) -> None:
  722. """Swap in a fresh ``_poll_session`` *then* close the old one, so concurrent tasks never see a closed session."""
  723. if not self._running or aiohttp is None:
  724. return
  725. old, self._poll_session = self._poll_session, _new_session()
  726. if old is not None and not old.closed:
  727. try:
  728. await old.close()
  729. except Exception as exc:
  730. logger.debug("[%s] old poll session close failed: %s", self.name, exc)
  731. async def _process_message_safe(self, message: dict[str, Any]) -> None:
  732. try:
  733. await self._process_message(message)
  734. except Exception as exc:
  735. logger.error("[%s] unhandled inbound error from=%s: %s", self.name, _safe_id(message.get("from_user_id")), exc, exc_info=True)
  736. async def _process_message(self, message: dict[str, Any]) -> None:
  737. assert self._poll_session is not None
  738. sender_id = str(message.get("from_user_id") or "").strip()
  739. message_id = str(message.get("message_id") or "").strip()
  740. if not sender_id or sender_id == self._account_id or (message_id and self._dedup.is_duplicate(message_id)):
  741. return
  742. # Secondary content-fingerprint dedup: upstream re-sends identical text under new message_ids.
  743. item_list = message.get("item_list") or []
  744. text = _extract_text(item_list)
  745. if text and self._dedup.is_duplicate(f"content:{sender_id}:{hashlib.md5(text.encode()).hexdigest()}"):
  746. logger.debug("[%s] Content-dedup: skipping duplicate message from %s", self.name, sender_id)
  747. return
  748. chat_type, effective_chat_id = _guess_chat_type(message, self._account_id)
  749. if chat_type == "group":
  750. if not self._is_group_allowed(effective_chat_id):
  751. return
  752. elif not self._is_dm_intake_allowed(sender_id):
  753. return
  754. context_token = str(message.get("context_token") or "").strip()
  755. if context_token:
  756. await self._token_store.set(self._account_id, sender_id, context_token)
  757. if self._poll_session and self._token and not self._typing_cache.get(sender_id):
  758. asyncio.create_task(self._fetch_typing_ticket(self._poll_session, sender_id, context_token or None, "getConfig failed"))
  759. media_paths, media_types = [], [] # type: List[str], List[str]
  760. for item in item_list:
  761. ref_item = (item.get("ref_msg") or {}).get("message_item")
  762. for candidate in (item, ref_item) if isinstance(ref_item, dict) else (item,):
  763. await self._collect_media(candidate, media_paths, media_types)
  764. if not text and not media_paths:
  765. return
  766. source = self.build_source(chat_id=effective_chat_id, chat_type=chat_type, user_id=sender_id, user_name=sender_id)
  767. event = MessageEvent(
  768. text=text, message_type=_message_type_from_media(media_types, text), source=source, raw_message=message,
  769. message_id=message_id or None, media_urls=media_paths, media_types=media_types, timestamp=datetime.now())
  770. logger.info("[%s] inbound from=%s type=%s media=%d", self.name, _safe_id(sender_id), source.chat_type, len(media_paths))
  771. if event.message_type == MessageType.TEXT:
  772. self._enqueue_text_event(event)
  773. else:
  774. await self.handle_message(event)
  775. async def _collect_media(self, item: dict[str, Any], media_paths: list[str], media_types: list[str]) -> None:
  776. spec = _INBOUND_MEDIA.get(item.get("type"))
  777. path, mime = await self._download_media(item, spec) if spec else (None, "")
  778. if path:
  779. media_paths.append(path)
  780. media_types.append(mime)
  781. async def _download_media(self, item: dict[str, Any], spec: tuple[Any, ...]) -> tuple[Optional[str], str]:
  782. """Download + decrypt one inbound media item -> (cached path or None, mime). Voice is always downloaded
  783. (never trust Tencent's ``voice_item.text``) so gateway/run.py's central STT re-transcribes."""
  784. item_key, timeout_seconds, cache_fn, mime, label = spec
  785. payload = item.get(item_key) or {}
  786. media = payload.get("media") or {}
  787. filename = str(payload.get("file_name") or "document.bin")
  788. mime = mime or mimetypes.guess_type(filename)[0] or "application/octet-stream"
  789. try:
  790. aes_key_b64 = media.get("aes_key")
  791. if item_key == "image_item" and payload.get("aeskey"): # image_item may carry a raw hex ``aeskey`` beside the media block
  792. aes_key_b64 = base64.b64encode(bytes.fromhex(str(payload.get("aeskey")))).decode("ascii") or aes_key_b64
  793. # #27300: previously short-circuited when ``voice_item.text`` was set on the assumption that
  794. # Tencent Cloud's STT was good enough. For non-Chinese audio that text is garbage (e.g. a
  795. # Russian message comes back as English phonemes) — we must always download the raw audio so
  796. # ``gateway/run.py``'s central STT pipeline can re-transcribe with the user's configured
  797. # mlx-whisper / whisper.cpp / faster-whisper backend.
  798. data = await _download_and_decrypt_media(
  799. self._poll_session, cdn_base_url=self._cdn_base_url, encrypted_query_param=media.get("encrypt_query_param"),
  800. aes_key_b64=aes_key_b64, full_url=media.get("full_url"), timeout_seconds=timeout_seconds)
  801. return await cache_fn(data, filename), mime
  802. except Exception as exc:
  803. logger.warning("[%s] %s download failed: %s", self.name, label, exc)
  804. return None, mime
  805. async def _fetch_typing_ticket(self, session: Any, user_id: str, context_token: Optional[str], failure_label: str) -> Optional[str]:
  806. try:
  807. response = await _get_config(session, base_url=self._base_url, token=self._token, user_id=user_id, context_token=context_token)
  808. typing_ticket = str(response.get("typing_ticket") or "")
  809. if typing_ticket:
  810. self._typing_cache.set(user_id, typing_ticket)
  811. return typing_ticket
  812. except Exception as exc:
  813. logger.debug("[%s] %s for %s: %s", self.name, failure_label, _safe_id(user_id), exc)
  814. return None
  815. def _split_text(self, content: str) -> list[str]:
  816. return _split_text_for_weixin_delivery(content, self.MAX_MESSAGE_LENGTH, self._split_multiline_messages)
  817. def _rate_limit_cooldown_remaining(self) -> float:
  818. return max(0.0, self._rate_limit_circuit_until - time.monotonic())
  819. def _record_rate_limit_event(self) -> bool:
  820. now = time.monotonic()
  821. self._rate_limit_events = [ts for ts in self._rate_limit_events if ts >= now - self._rate_limit_circuit_window_seconds] + [now]
  822. if len(self._rate_limit_events) < self._rate_limit_circuit_threshold:
  823. return False
  824. if self._rate_limit_circuit_open_seconds > 0:
  825. self._rate_limit_circuit_until = max(self._rate_limit_circuit_until, time.monotonic() + self._rate_limit_circuit_open_seconds)
  826. return self._rate_limit_cooldown_remaining() > 0
  827. async def _send_text_chunk(self, *, chat_id: str, chunk: str, context_token: Optional[str], client_id: str) -> None:
  828. """Send one text chunk with retry/backoff under the adapter-wide text gate. A stale-session response (``-14``,
  829. or ``-2`` with ``prepare failed``/``unknown error``) is re-sent once *without* ``context_token`` — iLink accepts
  830. tokenless sends as a degraded fallback, which keeps cron pushes working when no user message refreshed the
  831. session. A ``-2`` that survives that fails fast via ``_session_not_ready_error``."""
  832. async with self._send_text_gate:
  833. last_error: Optional[Exception] = None
  834. retried_without_token = False
  835. attempt = 0 # counts real failures only — the tokenless re-send must not eat the retry budget
  836. while True:
  837. if self._rate_limit_cooldown_remaining() > 0:
  838. raise RuntimeError(f"iLink sendmessage rate limited; cooldown active for {self._rate_limit_cooldown_remaining():.1f}s")
  839. try:
  840. resp = await _send_message(
  841. self._send_session, base_url=self._base_url, token=self._token, to=chat_id, text=chunk,
  842. context_token=context_token, client_id=client_id)
  843. ret, errcode = (resp.get("ret"), resp.get("errcode")) if resp and isinstance(resp, dict) else (None, None)
  844. if (ret is not None and ret != 0) or (errcode is not None and errcode != 0):
  845. errmsg = resp.get("errmsg") or resp.get("msg")
  846. if _is_session_expired(resp, ret, errcode) and not retried_without_token and context_token:
  847. retried_without_token, context_token = True, None
  848. self._token_store._cache.pop(self._token_store._key(self._account_id, chat_id), None)
  849. logger.warning("[%s] session expired for %s; retrying without context_token", self.name, _safe_id(chat_id))
  850. continue
  851. if _is_stale_session_ret(ret, errcode, errmsg):
  852. # break, not raise: a raise here is caught below and re-enters the retry ladder.
  853. last_error = _session_not_ready_error(ret, errcode, errmsg)
  854. break
  855. if ret != RATE_LIMIT_ERRCODE and errcode != RATE_LIMIT_ERRCODE:
  856. raise RuntimeError(f"iLink sendmessage error: ret={ret} errcode={errcode} errmsg={errmsg or 'unknown error'}")
  857. # Keep a descriptive error for when the loop exhausts while still limited.
  858. last_error = RuntimeError(f"iLink sendmessage rate limited: ret={ret} errcode={errcode} errmsg={errmsg or 'rate limited'}")
  859. if self._record_rate_limit_event():
  860. last_error = RuntimeError(
  861. f"iLink sendmessage rate limited (ret={ret} errcode={errcode} errmsg={errmsg or 'rate limited'}); "
  862. f"cooldown active for {self._rate_limit_cooldown_remaining():.1f}s")
  863. break
  864. if attempt >= self._send_chunk_retries:
  865. break
  866. attempt += 1
  867. wait = self._send_chunk_retry_delay_seconds * 3 # 3x backoff for rate limit
  868. logger.warning("[%s] rate limited for %s; backing off %.1fs before retry", self.name, _safe_id(chat_id), wait)
  869. await asyncio.sleep(wait)
  870. continue
  871. self._rate_limit_events.clear()
  872. self._rate_limit_circuit_until = 0.0
  873. return
  874. except Exception as exc:
  875. last_error = exc
  876. if attempt >= self._send_chunk_retries:
  877. break
  878. attempt += 1
  879. wait = self._send_chunk_retry_delay_seconds * attempt
  880. logger.warning("[%s] send chunk failed to=%s attempt=%d/%d, retrying in %.2fs: %s",
  881. self.name, _safe_id(chat_id), attempt, self._send_chunk_retries + 1, wait, exc)
  882. if wait > 0:
  883. await asyncio.sleep(wait)
  884. assert last_error is not None
  885. raise last_error
  886. async def send(self, chat_id: str, content: str, reply_to: Optional[str] = None, metadata: Optional[dict[str, Any]] = None) -> SendResult:
  887. if not self._send_session or not self._token:
  888. return SendResult(success=False, error="Not connected")
  889. context_token = self._token_store.get(self._account_id, chat_id)
  890. last_message_id: Optional[str] = None
  891. # Extract MEDIA: tags and bare local file paths before text delivery, under the routed
  892. # profile's scope: Docker MEDIA translation infers the sandbox from the active profile (#109024).
  893. with self._media_delivery_scope(self.build_source(chat_id=chat_id)):
  894. media_files, cleaned_content = self.extract_media(content)
  895. local_files, final_content = self.extract_local_files(self.extract_images(cleaned_content)[1])
  896. deliveries = [(p, v, "media") for p, v in self.filter_media_delivery_paths(media_files)]
  897. deliveries += [(p, False, "local file") for p in self.filter_local_delivery_paths(local_files)]
  898. try:
  899. for path, is_voice, label in deliveries:
  900. ext = Path(path).suffix.lower()
  901. sender, key = next(((m, k) for exts, m, k in _OUTBOUND_BY_EXT if is_voice or ext in exts), ("send_document", "file_path"))
  902. try:
  903. await getattr(self, sender)(chat_id=chat_id, metadata=metadata, **{key: path})
  904. except Exception as exc:
  905. logger.warning("[%s] %s delivery failed for %s: %s", self.name, label, path, exc)
  906. chunks = [c for c in self._split_text(self.format_message(final_content)) if c and c.strip()]
  907. for idx, chunk in enumerate(chunks):
  908. client_id = f"hermes-weixin-{uuid.uuid4().hex}"
  909. await self._send_text_chunk(chat_id=chat_id, chunk=chunk, context_token=context_token, client_id=client_id)
  910. last_message_id = client_id
  911. if idx < len(chunks) - 1 and self._send_chunk_delay_seconds > 0:
  912. await asyncio.sleep(self._send_chunk_delay_seconds)
  913. return SendResult(success=True, message_id=last_message_id)
  914. except Exception as exc:
  915. logger.error("[%s] send failed to=%s: %s", self.name, _safe_id(chat_id), exc)
  916. return SendResult(success=False, error=str(exc))
  917. async def _ensure_typing_ticket(self, chat_id: str) -> Optional[str]:
  918. """Return a valid typing ticket, refreshing via getConfig once the 600s TTL evicts it —
  919. otherwise ``stop_typing`` no-ops and the WeChat client shows the indicator forever."""
  920. ticket = self._typing_cache.get(chat_id)
  921. if ticket or not self._send_session or not self._token:
  922. return ticket or None
  923. return await self._fetch_typing_ticket(
  924. self._send_session, chat_id, self._token_store.get(self._account_id, chat_id), "typing ticket refresh failed")
  925. async def _set_typing(self, chat_id: str, status: int, label: str) -> None:
  926. typing_ticket = await self._ensure_typing_ticket(chat_id)
  927. if not typing_ticket:
  928. return
  929. try:
  930. await _api_post(
  931. self._send_session, base_url=self._base_url, endpoint=EP_SEND_TYPING, token=self._token, timeout_ms=CONFIG_TIMEOUT_MS,
  932. payload={"ilink_user_id": chat_id, "typing_ticket": typing_ticket, "status": status})
  933. except Exception as exc:
  934. logger.debug("[%s] typing %s failed for %s: %s", self.name, label, _safe_id(chat_id), exc)
  935. async def send_typing(self, chat_id: str, metadata: Optional[dict[str, Any]] = None) -> None:
  936. await self._set_typing(chat_id, TYPING_START, "start")
  937. async def stop_typing(self, chat_id: str) -> None:
  938. await self._set_typing(chat_id, TYPING_STOP, "stop")
  939. async def send_image(self, chat_id: str, image_url: str, caption: str, reply_to: Optional[str] = None, metadata=None) -> SendResult:
  940. cleanup = image_url.startswith(("http://", "https://"))
  941. file_path = await self._download_remote_media(image_url) if cleanup else image_url.replace("file://", "")
  942. if not cleanup and not os.path.isabs(file_path):
  943. file_path = os.path.abspath(file_path)
  944. try:
  945. return await self.send_document(chat_id, file_path, caption=caption, metadata=metadata)
  946. finally:
  947. if cleanup and file_path and os.path.exists(file_path):
  948. with contextlib.suppress(OSError):
  949. os.unlink(file_path)
  950. async def send_image_file(self, chat_id: str, image_path: str, caption: Optional[str] = None, reply_to=None, metadata=None, **kwargs) -> SendResult:
  951. return await self.send_document(chat_id=chat_id, file_path=image_path, caption=caption, metadata=metadata)
  952. async def _send_file_result(self, chat_id: str, path: str, caption: str, label: str, **kwargs: Any) -> SendResult:
  953. if not self._send_session or not self._token:
  954. return SendResult(success=False, error="Not connected")
  955. try:
  956. return SendResult(success=True, message_id=await self._send_file(chat_id, path, caption, **kwargs))
  957. except Exception as exc:
  958. logger.error("[%s] %s failed to=%s: %s", self.name, label, _safe_id(chat_id), exc)
  959. return SendResult(success=False, error=str(exc))
  960. async def send_document(
  961. self, chat_id: str, file_path: str, caption: Optional[str] = None, file_name: Optional[str] = None, reply_to: Optional[str] = None,
  962. metadata: Optional[dict[str, Any]] = None, **kwargs,
  963. ) -> SendResult:
  964. return await self._send_file_result(chat_id, file_path, caption or "", "send_document")
  965. async def send_video(self, chat_id: str, video_path: str, caption: Optional[str] = None, reply_to=None, metadata=None) -> SendResult:
  966. return await self._send_file_result(chat_id, video_path, caption or "", "send_video")
  967. async def send_voice(self, chat_id: str, audio_path: str, caption: Optional[str] = None, reply_to=None, metadata=None, **kwargs) -> SendResult:
  968. # Native outbound voice bubbles are not proven-working upstream; a file attachment at least plays (even .silk).
  969. return await self._send_file_result(chat_id, audio_path, caption or self.warning_text(t("platform.weixin.voice_as_attachment")), "send_voice", force_file_attachment=True)
  970. async def _download_remote_media(self, url: str) -> str:
  971. from tools.url_safety import is_safe_url
  972. if not is_safe_url(url):
  973. raise ValueError(f"Blocked unsafe URL (SSRF protection): {url}")
  974. assert self._send_session is not None
  975. data = await _download_bytes(self._send_session, url=url, timeout_seconds=30)
  976. with tempfile.NamedTemporaryFile(delete=False, suffix=Path(url.split("?", 1)[0]).suffix or ".bin") as handle:
  977. handle.write(data)
  978. return handle.name
  979. async def _send_file(self, chat_id: str, path: str, caption: str, force_file_attachment: bool = False) -> str:
  980. assert self._send_session is not None and self._token is not None
  981. plaintext = Path(path).read_bytes()
  982. media_type, item_builder = self._outbound_media_builder(path, force_file_attachment=force_file_attachment)
  983. filekey, aes_key = secrets.token_hex(16), secrets.token_bytes(16)
  984. rawsize, rawfilemd5 = len(plaintext), hashlib.md5(plaintext).hexdigest()
  985. upload_response = await _get_upload_url(
  986. self._send_session, base_url=self._base_url, token=self._token, to_user_id=chat_id, media_type=media_type, filekey=filekey,
  987. rawsize=rawsize, rawfilemd5=rawfilemd5, filesize=((rawsize + 16) // 16) * 16, aeskey_hex=aes_key.hex())
  988. upload_param = str(upload_response.get("upload_param") or "")
  989. ciphertext = _aes128_ecb_encrypt(plaintext, aes_key)
  990. # Prefer upload_full_url (direct CDN), else construct from upload_param. Both use POST — PUT 404s on the CDN.
  991. upload_url = str(upload_response.get("upload_full_url") or "") or (upload_param and (
  992. f"{self._cdn_base_url.rstrip('/')}/upload?encrypted_query_param={quote(upload_param, safe='')}&filekey={quote(filekey, safe='')}"))
  993. if not upload_url:
  994. raise RuntimeError(f"getUploadUrl returned neither upload_param nor upload_full_url: {upload_response}")
  995. encrypted_query_param = await _upload_ciphertext(self._send_session, ciphertext=ciphertext, upload_url=upload_url)
  996. context_token = self._token_store.get(self._account_id, chat_id)
  997. # iLink expects aes_key as base64(hex_string), not base64(raw_bytes) — otherwise images render as grey boxes.
  998. item_kwargs = {
  999. "encrypt_query_param": encrypted_query_param, "aes_key_for_api": base64.b64encode(aes_key.hex().encode("ascii")).decode("ascii"),
  1000. "ciphertext_size": len(ciphertext), "plaintext_size": rawsize, "filename": Path(path).name, "rawfilemd5": rawfilemd5}
  1001. if media_type == MEDIA_VOICE and path.endswith(".silk"):
  1002. item_kwargs.update(encode_type=6, sample_rate=24000, bits_per_sample=16)
  1003. item_lists: list[list[dict[str, Any]]] = [[item_builder(**item_kwargs)]]
  1004. if caption:
  1005. item_lists.insert(0, [{"type": ITEM_TEXT, "text_item": {"text": self.format_message(caption)}}])
  1006. last_message_id = ""
  1007. for item_list in item_lists:
  1008. last_message_id = f"hermes-weixin-{uuid.uuid4().hex}"
  1009. while True:
  1010. resp = await _send_items(
  1011. self._send_session, base_url=self._base_url, token=self._token, to=chat_id, item_list=item_list,
  1012. context_token=context_token, client_id=last_message_id)
  1013. ret, errcode = (resp.get("ret"), resp.get("errcode")) if resp and isinstance(resp, dict) else (None, None)
  1014. if (ret is None or ret == 0) and (errcode is None or errcode == 0):
  1015. break
  1016. # Same stale-session fallback as _send_text_chunk: re-send once without context_token. Clearing the
  1017. # token also covers the remaining item lists (caption, then media) and bounds this loop.
  1018. if _is_session_expired(resp, ret, errcode) and context_token:
  1019. context_token = None
  1020. self._token_store._cache.pop(self._token_store._key(self._account_id, chat_id), None)
  1021. logger.warning("[%s] session expired for %s; re-sending media without context_token", self.name, _safe_id(chat_id))
  1022. continue
  1023. errmsg = resp.get("errmsg") or resp.get("msg")
  1024. if _is_stale_session_ret(ret, errcode, errmsg):
  1025. raise _session_not_ready_error(ret, errcode, errmsg)
  1026. raise RuntimeError(f"iLink sendmessage error: ret={ret} errcode={errcode} errmsg={errmsg or 'unknown error'}")
  1027. return last_message_id
  1028. def _outbound_media_builder(self, path: str, force_file_attachment: bool = False):
  1029. mime = mimetypes.guess_type(path)[0] or "application/octet-stream"
  1030. if mime.startswith("image/"):
  1031. return MEDIA_IMAGE, _image_item
  1032. if mime.startswith("video/"):
  1033. return MEDIA_VIDEO, _video_item
  1034. if path.endswith(".silk") and not force_file_attachment:
  1035. return MEDIA_VOICE, _voice_item
  1036. return MEDIA_FILE, _file_item # audio/* and everything else ship as file attachments
  1037. async def get_chat_info(self, chat_id: str) -> dict[str, Any]:
  1038. return {"name": chat_id, "type": "group" if chat_id.endswith("@chatroom") else "dm", "chat_id": chat_id}
  1039. def format_message(self, content: Optional[str]) -> str:
  1040. return "" if content is None else _wrap_copy_friendly_lines_for_weixin(_normalize_markdown_blocks(content))
  1041. async def _deliver_direct(
  1042. adapter: WeixinAdapter, chat_id: str, message: str, media_files: Optional[list[tuple[str, bool]]], context_token: Optional[str],
  1043. ) -> dict[str, Any]:
  1044. last_result: Optional[SendResult] = None
  1045. cleaned = adapter.format_message(message)
  1046. if cleaned:
  1047. last_result = await adapter.send(chat_id, cleaned)
  1048. if not last_result.success:
  1049. return {"error": f"Weixin send failed: {last_result.error}"}
  1050. for media_path, _is_voice in media_files or []:
  1051. sender = adapter.send_image_file if Path(media_path).suffix.lower() in _DIRECT_IMAGE_EXTS else adapter.send_document
  1052. last_result = await sender(chat_id, media_path)
  1053. if not last_result.success:
  1054. return {"error": f"Weixin media send failed: {last_result.error}"}
  1055. message_id = last_result.message_id if last_result else None
  1056. return {"success": True, "platform": "weixin", "chat_id": chat_id, "message_id": message_id, "context_token_used": bool(context_token)}
  1057. async def send_weixin_direct(
  1058. *, extra: dict[str, Any], token: Optional[str], chat_id: str, message: str, media_files: Optional[list[tuple[str, bool]]] = None,
  1059. ) -> dict[str, Any]:
  1060. """One-shot send for ``send_message``/cron: reuse the live adapter's session on this loop, else a throwaway adapter."""
  1061. account_id = _extra_or_secret(extra, "account_id")
  1062. base_url = _extra_or_secret(extra, "base_url", ILINK_BASE_URL).rstrip("/")
  1063. cdn_base_url = _extra_or_secret(extra, "cdn_base_url", WEIXIN_CDN_BASE_URL).rstrip("/")
  1064. resolved_token = str(token or extra.get("token") or _wx_secret("WEIXIN_TOKEN", "")).strip()
  1065. if not resolved_token:
  1066. return {"error": "Weixin token missing. Configure WEIXIN_TOKEN or platforms.weixin.token."}
  1067. if not account_id:
  1068. return {"error": "Weixin account ID missing. Configure WEIXIN_ACCOUNT_ID or platforms.weixin.extra.account_id."}
  1069. token_store = ContextTokenStore(str(get_hermes_home()))
  1070. token_store.restore(account_id)
  1071. context_token = token_store.get(account_id, chat_id)
  1072. live_adapter = _LIVE_ADAPTERS.get(resolved_token)
  1073. send_session = getattr(live_adapter, '_send_session', None)
  1074. if send_session is not None and not send_session.closed and send_session._loop is asyncio.get_running_loop():
  1075. return await _deliver_direct(live_adapter, chat_id, message, media_files, context_token)
  1076. async with _new_session() as session:
  1077. merged = {**dict(extra or {}), "account_id": account_id, "base_url": base_url, "cdn_base_url": cdn_base_url}
  1078. adapter = WeixinAdapter(PlatformConfig(enabled=True, token=resolved_token, extra=merged))
  1079. adapter._send_session = adapter._session = session
  1080. adapter._token_store = token_store
  1081. return await _deliver_direct(adapter, chat_id, message, media_files, context_token)
  1082. # ---------------------------------------------------------------------------
  1083. # Plugin wiring (formerly core: gateway/run.py _BUILTIN_ADAPTERS row, tools/send_message_senders.py
  1084. # ``_send_weixin``, hermes_cli/gateway_setup_wizard.py ``_setup_weixin``).
  1085. # ---------------------------------------------------------------------------
  1086. _INSTALL_HINT = "Weixin: aiohttp/cryptography not installed"
  1087. _env_configured = env_is_connected("WEIXIN_ACCOUNT_ID", "WEIXIN_TOKEN")
  1088. def _config_connected(config: Any) -> bool:
  1089. """``config_connected_fn``: account_id AND token in the merged PlatformConfig; a token alone is
  1090. not enough, so this platform opts out of the generic ``token or api_key`` shortcut."""
  1091. return bool(config.extra.get("account_id") and (config.token or config.extra.get("token")))
  1092. def _is_connected(config: Any) -> bool:
  1093. """Account ID AND token, from ``config`` or the scoped ``WEIXIN_*`` env (``gateway/config.py``'s
  1094. Weixin connected check reads the same pair), else ``.env``, which a plain CLI process never loads
  1095. into the environment (the old static setup row read it)."""
  1096. extra = getattr(config, "extra", None) or {}
  1097. account = _extra_or_secret(extra, "account_id")
  1098. token = str(getattr(config, "token", None) or extra.get("token") or _wx_secret("WEIXIN_TOKEN", "")).strip()
  1099. return bool(account and token) or _env_configured(config)
  1100. async def _standalone_send(pconfig: Any, chat_id: str, message: str, *, thread_id: Optional[str] = None,
  1101. media_files: Optional[list] = None, force_document: bool = False) -> dict[str, Any]:
  1102. """Out-of-process ``send_message``/cron sender: native text + media via :func:`send_weixin_direct`.
  1103. ``thread_id``/``force_document`` are signature parity only (iLink has neither). Imports nothing from
  1104. other plugins, so an unrelated optional SDK (e.g. lark-oapi) can never block Weixin delivery."""
  1105. from gateway.platforms._shared import send_error
  1106. if not check_weixin_requirements():
  1107. return send_error("Weixin requirements not met. Need aiohttp + cryptography.")
  1108. try:
  1109. return await send_weixin_direct(extra=getattr(pconfig, "extra", None) or {}, token=getattr(pconfig, "token", None),
  1110. chat_id=chat_id, message=message, media_files=media_files)
  1111. except Exception as e:
  1112. logger.debug("Weixin standalone send failed", exc_info=True)
  1113. return send_error(f"Weixin send failed: {e}")
  1114. # Weixin DM policy by menu index (index 2 = allowlist is prompted separately).
  1115. _WEIXIN_DM_POLICIES = {0: ("pairing", "false", "success", " DM pairing enabled."),
  1116. 1: ("open", "true", "warning", " Open DM access enabled for Weixin."),
  1117. 3: ("disabled", "false", "warning", " Direct messages disabled.")}
  1118. _WEIXIN_GROUP_NOTE = (
  1119. " Note: QR login connects an iLink bot identity (e.g. ...@im.bot), not a",
  1120. " scriptable personal WeChat account. Ordinary WeChat groups typically cannot",
  1121. " invite an @im.bot identity, and iLink does not deliver ordinary-group events",
  1122. " to most bot accounts. The settings below only apply when iLink actually",
  1123. " delivers group events for your account type — otherwise DM remains the only",
  1124. " working channel regardless of this choice.",
  1125. )
  1126. def interactive_setup() -> None:
  1127. """``hermes gateway setup`` flow for Weixin / WeChat personal accounts (QR login via :func:`qr_login`).
  1128. Helpers resolve through the ``hermes_cli.gateway`` facade so the prompts/output match every other
  1129. wizard and the facade seams tests patch keep intercepting."""
  1130. from hermes_cli import gateway as gw
  1131. from hermes_cli.gateway_setup_wizard import (
  1132. confirm_reconfigure, offer_home_channel, print_setup_header, prompt_csv, save_env_values)
  1133. print_setup_header("💬 Weixin / WeChat")
  1134. print()
  1135. gw._print_info_lines(
  1136. " 1. Hermes will open Tencent iLink QR login in this terminal.",
  1137. " 2. Use WeChat to scan and confirm the QR code.",
  1138. " 3. Hermes will store the returned account_id/token in ~/.hermes/.env.",
  1139. " 4. This adapter supports native text, image, video, and document delivery.",
  1140. )
  1141. if not confirm_reconfigure("Weixin", "WEIXIN_ACCOUNT_ID", "WEIXIN_TOKEN"):
  1142. return
  1143. if not check_weixin_requirements():
  1144. gw.print_error(" Missing dependencies: Weixin needs aiohttp and cryptography.")
  1145. gw.print_info(" Install them, then rerun `hermes gateway setup`.")
  1146. return
  1147. print()
  1148. if not gw.prompt_yes_no(" Start QR login now?", True):
  1149. gw.print_info(" Cancelled.")
  1150. return
  1151. try:
  1152. credentials = gw.asyncio.run(qr_login(str(gw.get_hermes_home())))
  1153. except KeyboardInterrupt:
  1154. print()
  1155. gw.print_warning(" Weixin setup cancelled.")
  1156. return
  1157. except Exception as exc: # health: allow BLE001 -- interactive wizard: any QR-login failure is shown and setup stops
  1158. gw.print_error(f" QR login failed: {exc}")
  1159. return
  1160. if not credentials:
  1161. gw.print_warning(" QR login did not complete.")
  1162. return
  1163. account_id = credentials.get("account_id", "")
  1164. user_id = credentials.get("user_id", "")
  1165. gw.save_env_value("WEIXIN_ACCOUNT_ID", account_id)
  1166. gw.save_env_value("WEIXIN_TOKEN", credentials.get("token", ""))
  1167. if credentials.get("base_url", ""):
  1168. gw.save_env_value("WEIXIN_BASE_URL", credentials.get("base_url", ""))
  1169. gw.save_env_value("WEIXIN_CDN_BASE_URL", gw.get_env_value("WEIXIN_CDN_BASE_URL") or WEIXIN_CDN_BASE_URL)
  1170. print()
  1171. access_choices = [
  1172. "Use DM pairing approval (recommended)", "Allow all direct messages", "Only allow listed user IDs",
  1173. "Disable direct messages",
  1174. ]
  1175. access_idx = gw.prompt_choice(" How should direct messages be authorized?", access_choices, 0)
  1176. if access_idx == 2:
  1177. allowlist = prompt_csv(" Allowed Weixin user IDs (comma-separated)", user_id or "")
  1178. save_env_values(WEIXIN_DM_POLICY="allowlist", WEIXIN_ALLOW_ALL_USERS="false", WEIXIN_ALLOWED_USERS=allowlist)
  1179. gw.print_success(" Weixin allowlist saved.")
  1180. else:
  1181. policy, allow_all, level, message = _WEIXIN_DM_POLICIES.get(access_idx, _WEIXIN_DM_POLICIES[3])
  1182. save_env_values(WEIXIN_DM_POLICY=policy, WEIXIN_ALLOW_ALL_USERS=allow_all, WEIXIN_ALLOWED_USERS="")
  1183. (gw.print_success if level == "success" else gw.print_warning)(message)
  1184. if access_idx == 0:
  1185. gw.print_info(" Unknown DM users can request access and you approve them with `hermes pairing approve`.")
  1186. print()
  1187. gw._print_info_lines(*_WEIXIN_GROUP_NOTE)
  1188. group_choices = ["Disable group chats (recommended)", "Allow all group chats", "Only allow listed group chat IDs"]
  1189. group_idx = gw.prompt_choice(" How should group chats be handled?", group_choices, 0)
  1190. if group_idx == 0:
  1191. save_env_values(WEIXIN_GROUP_POLICY="disabled", WEIXIN_GROUP_ALLOWED_USERS="")
  1192. gw.print_info(" Group chats disabled.")
  1193. elif group_idx == 1:
  1194. save_env_values(WEIXIN_GROUP_POLICY="open", WEIXIN_GROUP_ALLOWED_USERS="")
  1195. gw.print_warning(" All group chats enabled (only takes effect if iLink delivers group events).")
  1196. else:
  1197. allow_groups = prompt_csv(" Allowed group chat IDs (comma-separated, not member user IDs)", "")
  1198. save_env_values(WEIXIN_GROUP_POLICY="allowlist", WEIXIN_GROUP_ALLOWED_USERS=allow_groups)
  1199. gw.print_success(" Group allowlist saved (only takes effect if iLink delivers group events).")
  1200. if user_id:
  1201. print()
  1202. offer_home_channel("WEIXIN_HOME_CHANNEL", user_id, "your Weixin user ID")
  1203. print()
  1204. gw.print_success("Weixin configured!")
  1205. gw.print_info(f" Account ID: {account_id}")
  1206. if user_id:
  1207. gw.print_info(f" User ID: {user_id}")
  1208. # System-prompt platform hint (PlatformEntry.platform_hint); cached-prompt bytes, keep stable.
  1209. _PLATFORM_HINT = (
  1210. "You are on Weixin/WeChat. Markdown formatting is supported, so you may use it when it improves readability, "
  1211. "but keep the message compact and chat-friendly. You can send media files natively: include "
  1212. "MEDIA:/absolute/path/to/file in your response. Images are sent as native photos, videos play inline when "
  1213. "supported, and other files arrive as downloadable documents. You can also include image URLs in markdown "
  1214. "format ![alt](url) and they will be downloaded and sent as native media when possible."
  1215. )
  1216. _SEND_TARGET_RE = re.compile(r"^\s*((?:wxid|gh|v\d+|wm|wb)_[A-Za-z0-9_-]+|[A-Za-z0-9._-]+@chatroom|filehelper)\s*$")
  1217. def register(ctx) -> None:
  1218. """Plugin entry point — called by the Hermes plugin system."""
  1219. from . import gateway_env # env -> GatewayConfig bridge (gateway.config_env)
  1220. ctx.register_platform(
  1221. target_ref_syntax_fn=regex_groups_parser(_SEND_TARGET_RE, thread_group=False),
  1222. standalone_send=StandaloneSendPolicy(label="Weixin", media=True),
  1223. name="weixin", label="Weixin / WeChat", adapter_factory=WeixinAdapter, check_fn=check_weixin_requirements,
  1224. dashboard_label="Weixin / WeChat (Personal)",
  1225. dashboard_description="Connect a personal WeChat account through Tencent's iLink Bot API.",
  1226. docs_url="https://hermes-agent.nousresearch.com/docs/user-guide/messaging/weixin/",
  1227. dashboard_env_vars=("WEIXIN_ACCOUNT_ID", "WEIXIN_TOKEN", "WEIXIN_BASE_URL"),
  1228. is_connected=_is_connected, required_env=["WEIXIN_ACCOUNT_ID", "WEIXIN_TOKEN"], install_hint=_INSTALL_HINT,
  1229. config_connected_fn=_config_connected,
  1230. setup_fn=interactive_setup,
  1231. cron_deliver_env_var="WEIXIN_HOME_CHANNEL",
  1232. # Out-of-process send_message/cron; the adapter's own send() chunks (MAX_MESSAGE_LENGTH), so
  1233. # max_message_length stays 0: send_message hands the full text + all media to one native send.
  1234. standalone_sender_fn=_standalone_send, max_message_length=0, emoji="💬", allow_update_command=True,
  1235. platform_hint=_PLATFORM_HINT,
  1236. display_tier="low",
  1237. env_overrides=gateway_env.ENV_OVERRIDES,
  1238. env_enable_credentials=gateway_env.ENV_ENABLE_CREDENTIALS, **gateway_env.AUTHZ,
  1239. )