adapter.py 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  1. """Yuanbao platform adapter: WebSocket gateway client (AUTH_BIND, ping/pong heartbeat, reconnect),
  2. inbound middleware pipeline (T05 push → MessageEvent) and outbound sender (T06 text/media).
  3. Config under ``platforms.yuanbao.extra`` (or env): app_id/YUANBAO_APP_ID, app_secret/YUANBAO_APP_SECRET,
  4. bot_id/YUANBAO_BOT_ID (optional, returned by sign-token), ws_url/YUANBAO_WS_URL, api_domain/YUANBAO_API_DOMAIN.
  5. """
  6. from __future__ import annotations
  7. import asyncio
  8. import base64
  9. import binascii
  10. import collections
  11. import contextlib
  12. import dataclasses
  13. import hashlib
  14. import hmac
  15. import json
  16. import logging
  17. import os
  18. import re
  19. import secrets
  20. import sys
  21. import time
  22. import urllib.parse
  23. import uuid
  24. from abc import ABC, abstractmethod
  25. from dataclasses import dataclass, field as dc_field
  26. from datetime import datetime, timezone, timedelta, UTC
  27. from enum import Enum
  28. from pathlib import Path
  29. from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Tuple
  30. import httpx
  31. try:
  32. import websockets
  33. import websockets.exceptions
  34. WEBSOCKETS_AVAILABLE = True
  35. except ImportError:
  36. WEBSOCKETS_AVAILABLE = False
  37. websockets = None # type: ignore[assignment]
  38. from gateway.config import Platform, PlatformConfig
  39. from agent.i18n import t
  40. from gateway.platforms.base import (
  41. BasePlatformAdapter, SendResult,
  42. cache_document_from_bytes_async, cache_image_from_bytes_async, cache_video_from_bytes_async,
  43. )
  44. from gateway.platforms.event import MessageEvent, MessageType
  45. from gateway.platforms import helpers as _mdchunk
  46. from gateway.platforms._shared import get_scoped_secret as _yb_secret, profile_scoped as _profile_scoped
  47. from gateway.platforms.helpers import MessageDeduplicator, cancel_task
  48. from gateway.platforms.access_policy_mixin import OwnAccessPolicyMixin
  49. from plugins.platforms.yuanbao.yuanbao_media import (
  50. download_url as media_download_url, get_cos_credentials, upload_to_cos,
  51. build_image_msg_body, build_file_msg_body, guess_mime_type, md5_hex,
  52. )
  53. from plugins.platforms.yuanbao.yuanbao_proto import (
  54. CMD_TYPE, WS_HEARTBEAT_RUNNING, WS_HEARTBEAT_FINISH, HERMES_INSTANCE_ID,
  55. _fields_to_dict, _get_string, _get_varint, _parse_fields,
  56. decode_conn_msg, decode_inbound_push, decode_forward_msg_data,
  57. decode_query_group_info_rsp, decode_get_group_member_list_rsp,
  58. encode_auth_bind, encode_ping, encode_push_ack, encode_send_c2c_message, encode_send_group_message,
  59. encode_send_private_heartbeat, encode_send_group_heartbeat, encode_query_group_info,
  60. encode_get_group_member_list, next_seq_no,
  61. )
  62. from gateway.session_transcript import TranscriptReadError
  63. logger = logging.getLogger(__name__)
  64. # AUTH_BIND / sign-token header values
  65. from hermes_cli.version_info import get_version_info
  66. _YUANBAO_INSTANCE_ID = str(HERMES_INSTANCE_ID)
  67. _OPERATION_SYSTEM = sys.platform
  68. DEFAULT_WS_GATEWAY_URL = "wss://bot-wss.yuanbao.tencent.com/wss/connection"
  69. DEFAULT_API_DOMAIN = "https://bot.yuanbao.tencent.com"
  70. HEARTBEAT_INTERVAL_SECONDS = 30.0
  71. CONNECT_TIMEOUT_SECONDS = 15.0
  72. AUTH_TIMEOUT_SECONDS = 10.0
  73. MAX_RECONNECT_ATTEMPTS = 100
  74. DEFAULT_SEND_TIMEOUT = 30.0 # WS biz request timeout
  75. # Caps the WS close handshake: websockets' own 5s close_timeout waits for a close echo an idle
  76. # server never sends, stalling shutdown; a responsive server finishes well under 1s.
  77. # Upper bound on the WS close handshake during teardown (#40383). The websockets connection's own
  78. # close_timeout (5s) blocks until the server echoes the close frame; an idle/unresponsive server never
  79. # replies, stalling gateway shutdown by the full timeout. Bounding the close await here keeps teardown fast
  80. # — a responsive server completes the handshake in well under a second, so this only caps the pathological
  81. # hang. Also bounds the reconnect / connect-failure cleanup paths that reuse _cleanup_ws(), where a graceful
  82. # close is unnecessary anyway (the socket is being discarded to redial).
  83. WS_CLOSE_TIMEOUT_S = 1.0
  84. NO_RECONNECT_CLOSE_CODES = {4012, 4013, 4014, 4018, 4019, 4021} # permanent errors — never reconnect
  85. HEARTBEAT_TIMEOUT_THRESHOLD = 2 # consecutive missed pongs before reconnect
  86. REPLY_HEARTBEAT_INTERVAL_S = 2.0 # RUNNING cadence
  87. REPLY_HEARTBEAT_TIMEOUT_S = 30.0 # auto-FINISH after this much inactivity
  88. SLOW_RESPONSE_TIMEOUT_S = 120.0 # push slow_response_message() when the agent is silent this long
  89. def slow_response_message() -> str:
  90. """Patience notice pushed after SLOW_RESPONSE_TIMEOUT_S of agent silence (localized; the
  91. Chinese wording Yuanbao users historically saw lives in locales/zh.yaml)."""
  92. return t("platform.yuanbao.slow_response_notice")
  93. # Cron wrapper markers, mirrored from cron/scheduler_delivery.py (``wrap_response``). The wrapper
  94. # is not keyed yet; when it is, import the same key here so ``strip_cron_wrapper`` keeps matching.
  95. CRON_WRAPPER_HEADER_PREFIX = "Cronjob Response: "
  96. CRON_WRAPPER_DIVIDER = "\n-------------\n\n"
  97. CRON_WRAPPER_FOOTER_PREFIX = '\n\nTo stop or manage this job, send me a new message (e.g. "stop reminder '
  98. # Transcript anchors: [image|ybres:abc] [file:report.pdf|ybres:xyz] [voice|ybres:…]
  99. _YB_RES_REF_RE = re.compile(r"\[(image|voice|video|file(?::[^|\]]*)?)\|ybres:([A-Za-z0-9_\-]+)\]")
  100. # Anchors after local download: [image: /path] [file: report.pdf → /path] [video: /path]
  101. _YB_LOCAL_MEDIA_RE = re.compile(r"\[(\w+):[^\]]*?(/[^\]]+?)\s*\]")
  102. _RESOLVABLE_MEDIA_KINDS = frozenset({"image", "file", "video"}) # kinds injected into model context
  103. _INDICATOR_RE = re.compile(r'\s*\(\d+/\d+\)$') # "(1/3)" page indicators from BasePlatformAdapter
  104. _TEXT_ELEM_TYPE = "TIMTextElem"
  105. OBSERVED_MEDIA_BACKFILL_LOOKBACK = 50 # recent transcript messages scanned for observed media
  106. OBSERVED_MEDIA_BACKFILL_MAX_RESOLVE_PER_TURN = 12
  107. # platforms.yuanbao.extra.media_resolve_concurrency: 1 = sequential rollback knob;
  108. # 6 = browser per-origin HTTP/1.1 ceiling; 12 = backfill cap.
  109. _DEFAULT_RESOLVE_CONCURRENCY = 6
  110. _MIN_RESOLVE_CONCURRENCY = 1
  111. _MAX_RESOLVE_CONCURRENCY = 12
  112. def _iter_ybres_refs(matches) -> Iterator[tuple[str, str, str]]:
  113. """Turn ``_YB_RES_REF_RE`` matches into ``(rid, kind, filename)`` for resolvable kinds only."""
  114. for m in matches:
  115. kind, _, filename = m.group(1).partition(":")
  116. if kind.strip() in _RESOLVABLE_MEDIA_KINDS:
  117. yield m.group(2), kind.strip(), filename.strip()
  118. def _text_elem(text: str) -> dict:
  119. """A TIMTextElem msg_body entry."""
  120. return {"msg_type": _TEXT_ELEM_TYPE, "msg_content": {"text": text}}
  121. def _cancel_all(tasks: dict[str, asyncio.Task]) -> None:
  122. """Cancel every unfinished task in *tasks* and clear the dict."""
  123. for task in list(tasks.values()):
  124. if not task.done():
  125. task.cancel()
  126. tasks.clear()
  127. class MarkdownProcessor:
  128. """Yuanbao's fence/table-aware chunking policy over the shared chunker in gateway.platforms.helpers."""
  129. @classmethod
  130. def chunk_markdown_text(cls, text: str, max_chars: int = 4000, len_fn: Optional[Callable[[str], int]] = None) -> list[str]:
  131. """<= max_chars chunks at paragraph boundaries, never inside a fence or table (an oversized
  132. single block may exceed the limit)."""
  133. return _mdchunk.split_text_fence_aware(text, max_chars, len_fn, prefer_paragraphs=True, balance_fences=False)
  134. class SignManager:
  135. """Sign-token acquisition, caching, signing and retry. All state is class-level so one
  136. shared client serves the whole process."""
  137. TOKEN_PATH = "/api/v5/robotLogic/sign-token"
  138. RETRYABLE_CODE = 10099
  139. MAX_RETRIES = 3
  140. RETRY_DELAY_S = 1.0
  141. CACHE_REFRESH_MARGIN_S = 60 # treat as expiring this many seconds early
  142. HTTP_TIMEOUT_S = 10.0
  143. _cache: dict[str, dict[str, Any]] = {} # app_key → {"token", "bot_id", "expire_ts", ...}
  144. # Per-app_key refresh locks, created lazily from async context so they bind to the running
  145. # loop; disconnect() clears them to avoid stale locks across reconnects.
  146. _locks: dict[str, asyncio.Lock] = {}
  147. @classmethod
  148. def get_refresh_lock(cls, app_key: str) -> asyncio.Lock:
  149. """Per-app_key refresh lock (create on demand). Call only from a running event loop."""
  150. if app_key not in cls._locks:
  151. cls._locks[app_key] = asyncio.Lock()
  152. return cls._locks[app_key]
  153. @staticmethod
  154. def compute_signature(nonce: str, timestamp: str, app_key: str, app_secret: str) -> str:
  155. """HMAC-SHA256(key=app_secret, msg=nonce+timestamp+app_key+app_secret).hexdigest()."""
  156. plain = nonce + timestamp + app_key + app_secret
  157. return hmac.new(app_secret.encode(), plain.encode(), hashlib.sha256).hexdigest()
  158. @staticmethod
  159. def build_timestamp() -> str:
  160. """Beijing-time ISO-8601 timestamp without milliseconds (2006-01-02T15:04:05+08:00)."""
  161. return datetime.now(tz=timezone(timedelta(hours=8))).strftime("%Y-%m-%dT%H:%M:%S+08:00")
  162. @classmethod
  163. def is_cache_valid(cls, entry: dict[str, Any]) -> bool:
  164. return entry["expire_ts"] - time.time() > cls.CACHE_REFRESH_MARGIN_S
  165. @classmethod
  166. def clear_locks(cls) -> None:
  167. cls._locks.clear()
  168. @classmethod
  169. def purge_expired(cls) -> int:
  170. """Drop expired token-cache entries; returns count purged."""
  171. now = time.time()
  172. expired_keys = [k for k, v in cls._cache.items() if now - v.get("expire_ts", 0) > 0]
  173. for k in expired_keys:
  174. cls._cache.pop(k, None)
  175. return len(expired_keys)
  176. @classmethod
  177. async def fetch(cls, app_key: str, app_secret: str, api_domain: str, route_env: str = "") -> dict[str, Any]:
  178. """POST sign-token, retrying RETRYABLE_CODE up to MAX_RETRIES times."""
  179. url = f"{api_domain.rstrip('/')}{cls.TOKEN_PATH}"
  180. async with httpx.AsyncClient(timeout=cls.HTTP_TIMEOUT_S) as client:
  181. for attempt in range(cls.MAX_RETRIES + 1):
  182. nonce = secrets.token_hex(16)
  183. timestamp = cls.build_timestamp()
  184. payload = {"app_key": app_key, "nonce": nonce,
  185. "signature": cls.compute_signature(nonce, timestamp, app_key, app_secret), "timestamp": timestamp}
  186. version = get_version_info().base_version
  187. headers = {"Content-Type": "application/json", "X-AppVersion": version, "X-OperationSystem": _OPERATION_SYSTEM,
  188. "X-Instance-Id": _YUANBAO_INSTANCE_ID, "X-Bot-Version": version}
  189. if route_env:
  190. headers["X-Route-Env"] = route_env
  191. logger.info("Sign token request: url=%s%s", url, f" (retry {attempt}/{cls.MAX_RETRIES})" if attempt > 0 else "")
  192. response = await client.post(url, json=payload, headers=headers)
  193. if response.status_code != 200:
  194. raise RuntimeError(f"Sign token API returned {response.status_code}: {response.text[:200]}")
  195. try:
  196. result_data: dict[str, Any] = response.json()
  197. except Exception as exc:
  198. raise ValueError(f"Sign token response parse error: {exc}") from exc
  199. code = result_data.get("code")
  200. if code == 0:
  201. data = result_data.get("data")
  202. if not isinstance(data, dict):
  203. raise ValueError(f"Sign token response missing 'data' field: {result_data}")
  204. logger.info("Sign token success: bot_id=%s", data.get("bot_id"))
  205. return data
  206. if code != cls.RETRYABLE_CODE or attempt >= cls.MAX_RETRIES:
  207. raise RuntimeError(f"Sign token error: code={code}, msg={result_data.get('msg', '')}")
  208. logger.warning("Sign token retryable: code=%s, retrying in %ss (attempt=%d/%d)",
  209. code, cls.RETRY_DELAY_S, attempt + 1, cls.MAX_RETRIES)
  210. await asyncio.sleep(cls.RETRY_DELAY_S)
  211. raise RuntimeError("Sign token failed: max retries exceeded")
  212. @classmethod
  213. async def _fetch_into_cache(cls, app_key: str, app_secret: str, api_domain: str, route_env: str) -> None:
  214. data = await cls.fetch(app_key, app_secret, api_domain, route_env)
  215. duration: int = data.get("duration", 0)
  216. cls._cache[app_key] = {
  217. "token": data.get("token", ""), "bot_id": data.get("bot_id", ""), "duration": duration,
  218. "product": data.get("product", ""), "source": data.get("source", ""),
  219. "expire_ts": time.time() + (duration if duration > 0 else 3600),
  220. }
  221. @classmethod
  222. async def get_token(cls, app_key: str, app_secret: str, api_domain: str, route_env: str = "") -> dict[str, Any]:
  223. """WS auth token, served from cache while valid (with CACHE_REFRESH_MARGIN_S)."""
  224. cls.purge_expired()
  225. cached = cls._cache.get(app_key)
  226. if cached and cls.is_cache_valid(cached):
  227. logger.info("Using cached token (%ds remaining)", int(cached["expire_ts"] - time.time()))
  228. return dict(cached)
  229. async with cls.get_refresh_lock(app_key):
  230. cached = cls._cache.get(app_key)
  231. if cached and cls.is_cache_valid(cached):
  232. return dict(cached)
  233. await cls._fetch_into_cache(app_key, app_secret, api_domain, route_env)
  234. return dict(cls._cache[app_key])
  235. @classmethod
  236. async def force_refresh(cls, app_key: str, app_secret: str, api_domain: str, route_env: str = "") -> dict[str, Any]:
  237. """Clear the cached token and re-sign."""
  238. logger.warning("[force-refresh] Clearing cache and re-signing token: app_key=****%s", app_key[-4:])
  239. async with cls.get_refresh_lock(app_key):
  240. cls._cache.pop(app_key, None)
  241. await cls._fetch_into_cache(app_key, app_secret, api_domain, route_env)
  242. return dict(cls._cache[app_key])
  243. @dataclass
  244. class InboundContext:
  245. """Mutable context passed through every inbound middleware in registration order."""
  246. adapter: Any # YuanbaoAdapter (forward-ref avoids circular import)
  247. raw_frames: list = dc_field(default_factory=list) # debounce-aggregated raw frames
  248. push: Optional[dict] = None # DecodeMiddleware
  249. decoded_via: str = "" # "json" | "protobuf"
  250. from_account: str = "" # ExtractFieldsMiddleware …
  251. group_code: str = ""
  252. group_name: str = ""
  253. sender_nickname: str = ""
  254. msg_body: list = dc_field(default_factory=list)
  255. msg_id: str = ""
  256. cloud_custom_data: str = ""
  257. chat_id: str = "" # ChatRoutingMiddleware …
  258. chat_type: str = "" # "dm" | "group"
  259. chat_name: str = ""
  260. raw_text: str = "" # ExtractContentMiddleware …
  261. media_refs: list = dc_field(default_factory=list)
  262. forwarded_records: Optional[dict] = None # parsed ForwardMsgData for elem_type 1009
  263. owner_command: Optional[str] = None # OwnerCommandMiddleware
  264. source: Optional[Any] = None # SessionSource, BuildSourceMiddleware
  265. msg_type: Optional[Any] = None # MessageType | YuanbaoMessageType, ClassifyMessageTypeMiddleware
  266. reply_to_message_id: Optional[str] = None # QuoteContextMiddleware …
  267. reply_to_text: Optional[str] = None
  268. quote_media_refs: list = dc_field(default_factory=list) # (rid, kind, filename)
  269. # MediaResolveMiddleware: deduped local paths — own media, then quoted media, else group-observed media
  270. media_urls: list = dc_field(default_factory=list)
  271. media_types: list = dc_field(default_factory=list)
  272. channel_prompt: Optional[str] = None # GroupAttributionMiddleware
  273. class InboundMiddleware(ABC):
  274. """Set class-level ``name`` and implement ``handle(ctx, next_fn)``; ``await next_fn()``
  275. continues the pipeline, returning without it stops."""
  276. name: str = ""
  277. @abstractmethod
  278. async def handle(self, ctx: InboundContext, next_fn: Callable) -> None: ...
  279. async def __call__(self, ctx: InboundContext, next_fn: Callable) -> None:
  280. return await self.handle(ctx, next_fn)
  281. def __repr__(self) -> str:
  282. return f"<{self.__class__.__name__} name={self.name!r}>"
  283. class InboundPipeline:
  284. """Onion-model middleware pipeline: named middlewares, ``when`` guards, use_before/use_after/
  285. remove. Accepts ``InboundMiddleware`` instances or plain ``async def(ctx, next_fn)`` callables."""
  286. def __init__(self) -> None:
  287. self._middlewares: list = [] # (name, handler, when_fn | None)
  288. @staticmethod
  289. def _normalize(name_or_mw, handler=None):
  290. if isinstance(name_or_mw, InboundMiddleware):
  291. return name_or_mw.name, name_or_mw
  292. return name_or_mw, handler
  293. def use(self, name_or_mw, handler=None, when=None) -> InboundPipeline:
  294. """Append ``pipeline.use(SomeMiddleware())`` or ``pipeline.use("name", fn)``."""
  295. return self._insert_relative(None, 0, name_or_mw, handler, when)
  296. def _insert_relative(self, target: Optional[str], offset: int, name_or_mw, handler, when) -> InboundPipeline:
  297. """Insert at index(target)+offset; appends when *target* is None or not registered."""
  298. name, h = self._normalize(name_or_mw, handler)
  299. idx = next((i for i, (n, _, _) in enumerate(self._middlewares) if n == target), None)
  300. self._middlewares.insert(len(self._middlewares) if idx is None else idx + offset, (name, h, when))
  301. return self
  302. def use_before(self, target: str, name_or_mw, handler=None, when=None) -> InboundPipeline:
  303. return self._insert_relative(target, 0, name_or_mw, handler, when)
  304. def use_after(self, target: str, name_or_mw, handler=None, when=None) -> InboundPipeline:
  305. return self._insert_relative(target, 1, name_or_mw, handler, when)
  306. def remove(self, name: str) -> InboundPipeline:
  307. self._middlewares = [(n, h, w) for n, h, w in self._middlewares if n != name]
  308. return self
  309. @property
  310. def middleware_names(self) -> list:
  311. return [n for n, _, _ in self._middlewares]
  312. async def execute(self, ctx: InboundContext) -> None:
  313. """Run the chain; each middleware receives ``(ctx, next_fn)``."""
  314. chain = self._middlewares
  315. index = 0
  316. async def next_fn() -> None:
  317. nonlocal index
  318. while index < len(chain):
  319. name, handler, when_fn = chain[index]
  320. index += 1
  321. if when_fn is not None and not when_fn(ctx):
  322. continue
  323. try:
  324. await handler(ctx, next_fn)
  325. except Exception:
  326. logger.error("[InboundPipeline] middleware [%s] error", name, exc_info=True)
  327. raise
  328. return
  329. await next_fn()
  330. class DecodeMiddleware(InboundMiddleware):
  331. """Decode raw inbound frames (JSON or protobuf via ``decode_inbound_push``) into ctx.push."""
  332. name = "decode"
  333. @staticmethod
  334. def convert_json_msg_body(raw_body: list) -> list:
  335. """Normalize JSON msg_body (PascalCase or snake_case keys) to [{"msg_type", "msg_content"}]."""
  336. result = []
  337. for item in raw_body or []:
  338. if not isinstance(item, dict):
  339. continue
  340. msg_type = item.get("msg_type") or item.get("MsgType", "")
  341. msg_content = item.get("msg_content") or item.get("MsgContent", {})
  342. if isinstance(msg_content, str):
  343. try:
  344. msg_content = json.loads(msg_content)
  345. except Exception:
  346. msg_content = {"text": msg_content}
  347. result.append({"msg_type": msg_type, "msg_content": msg_content or {}})
  348. return result
  349. @staticmethod
  350. def json_sender_fields(raw_json: dict) -> tuple[str, str]:
  351. """(from_account, group_code) accepting both Tencent IM PascalCase and internal snake_case keys."""
  352. from_account = raw_json.get("from_account", "") or raw_json.get("From_Account", "")
  353. group_code = raw_json.get("group_code", "") or raw_json.get("GroupId", "") or raw_json.get("group_id", "")
  354. return from_account, group_code
  355. @staticmethod
  356. def parse_json_push(raw_json: dict) -> dict | None:
  357. """JSON push → dict shaped like ``decode_inbound_push`` output; accepts both the callback
  358. format (callback_command/from_account/msg_body) and legacy keys (GroupId/MsgSeq/MsgKey/MsgBody)."""
  359. if not raw_json:
  360. return None
  361. from_account, group_code = DecodeMiddleware.json_sender_fields(raw_json)
  362. msg_body = DecodeMiddleware.convert_json_msg_body(raw_json.get("msg_body", []) or raw_json.get("MsgBody", []))
  363. # Recall callbacks may have neither from_account nor msg_body.
  364. if not from_account and not msg_body and not raw_json.get("callback_command"):
  365. return None
  366. return {
  367. "callback_command": raw_json.get("callback_command", ""),
  368. "from_account": from_account,
  369. "to_account": raw_json.get("to_account", "") or raw_json.get("To_Account", ""),
  370. "sender_nickname": raw_json.get("sender_nickname", "") or raw_json.get("nick_name", ""),
  371. "group_code": group_code,
  372. "group_name": raw_json.get("group_name", ""),
  373. "msg_seq": raw_json.get("msg_seq", 0) or raw_json.get("MsgSeq", 0),
  374. "msg_id": raw_json.get("msg_id", "") or raw_json.get("msg_key", "") or raw_json.get("MsgKey", ""),
  375. "msg_body": msg_body,
  376. "cloud_custom_data": raw_json.get("cloud_custom_data", "") or raw_json.get("CloudCustomData", ""),
  377. "bot_owner_id": raw_json.get("bot_owner_id", "") or raw_json.get("botOwnerId", ""),
  378. "recall_msg_seq_list": raw_json.get("recall_msg_seq_list") or None,
  379. "trace_id": (raw_json.get("log_ext") or {}).get("trace_id", "") if isinstance(raw_json.get("log_ext"), dict) else "",
  380. }
  381. def _decode_single(self, adapter, data: bytes) -> tuple:
  382. """One raw frame → (push_dict, decoded_via) or (None, '')."""
  383. try:
  384. conn_json = json.loads(data.decode("utf-8"))
  385. except Exception:
  386. conn_json = None
  387. if isinstance(conn_json, dict):
  388. push = self.parse_json_push(conn_json)
  389. return (push, "json") if push else (None, "")
  390. try:
  391. push = decode_inbound_push(data)
  392. except Exception:
  393. push = None
  394. return (push, "protobuf") if push else (None, "")
  395. async def handle(self, ctx: InboundContext, next_fn) -> None:
  396. if not ctx.raw_frames:
  397. return # Stop pipeline — nothing to decode
  398. merged: Optional[dict] = None
  399. for data in ctx.raw_frames:
  400. push, via = self._decode_single(ctx.adapter, data)
  401. if not push:
  402. logger.info("[%s] Push decoded but no valid message. raw hex(first64)=%s",
  403. ctx.adapter.name, data.hex()[:128] if data else "(empty)")
  404. elif merged is None:
  405. merged, ctx.decoded_via = push, via
  406. logger.info("[%s] Frame decoded (via=%s): len=%d", ctx.adapter.name, via, len(data))
  407. elif push.get("msg_body", []): # subsequent pushes: append msg_body, newline-separated
  408. merged["msg_body"] = merged.get("msg_body", []) + [_text_elem("\n")] + push["msg_body"]
  409. logger.info("[%s] Merged %d extra msg_body elements from aggregated push", ctx.adapter.name, len(push["msg_body"]))
  410. if not merged:
  411. return # Stop pipeline
  412. ctx.push = merged
  413. logger.info(
  414. "[%s] Push decoded (via=%s): from=%s group=%s msg_id=%s msg_types=%s",
  415. ctx.adapter.name, ctx.decoded_via, merged.get("from_account", ""), merged.get("group_code", ""),
  416. merged.get("msg_id", ""), [e.get("msg_type", "") for e in merged.get("msg_body", [])],
  417. )
  418. logger.debug("[%s] Push payload: %s", ctx.adapter.name, ctx.push)
  419. await next_fn()
  420. class ExtractFieldsMiddleware(InboundMiddleware):
  421. """Copy common push fields onto ctx."""
  422. name = "extract-fields"
  423. async def handle(self, ctx: InboundContext, next_fn) -> None:
  424. for f in ("from_account", "group_code", "group_name", "sender_nickname", "msg_id", "cloud_custom_data"):
  425. setattr(ctx, f, ctx.push.get(f, ""))
  426. ctx.msg_body = ctx.push.get("msg_body", [])
  427. await next_fn()
  428. class DedupMiddleware(InboundMiddleware):
  429. name = "dedup"
  430. async def handle(self, ctx: InboundContext, next_fn) -> None:
  431. if ctx.msg_id and ctx.adapter._dedup.is_duplicate(ctx.msg_id):
  432. logger.debug("[%s] Duplicate message ignored: msg_id=%s", ctx.adapter.name, ctx.msg_id)
  433. return # Stop pipeline
  434. await next_fn()
  435. def _session_store(adapter):
  436. """Adapter's SessionStore, or None before ``set_session_store`` ran."""
  437. return getattr(adapter, "_session_store", None)
  438. class RecallGuardMiddleware(InboundMiddleware):
  439. """Recall callbacks (Group.CallbackAfterRecallMsg / C2C.CallbackAfterMsgWithDraw).
  440. A: in transcript → redact; B: not in transcript → system note; C: being processed → interrupt + delayed redact."""
  441. name = "recall_guard"
  442. _RECALL_COMMANDS = frozenset({"Group.CallbackAfterRecallMsg", "C2C.CallbackAfterMsgWithDraw"})
  443. _REDACTED = "[This message was recalled/withdrawn by the sender; original content removed]"
  444. async def handle(self, ctx: InboundContext, next_fn) -> None:
  445. cmd = (ctx.push or {}).get("callback_command", "")
  446. if cmd in self._RECALL_COMMANDS:
  447. self._handle_recall(ctx, cmd) # terminal: recalls never dispatch
  448. else:
  449. await next_fn()
  450. @staticmethod
  451. def _build_source(adapter, group_code: str, from_account: str):
  452. return adapter.build_source(
  453. chat_id=(f"group:{group_code}" if group_code else f"direct:{from_account}"),
  454. chat_type="group" if group_code else "dm",
  455. user_id=from_account or None,
  456. thread_id="main" if group_code else None,
  457. )
  458. @classmethod
  459. def _resolve_sid(cls, store, adapter, group_code: str, from_account: str) -> str:
  460. return store.get_or_create_session(cls._build_source(adapter, group_code, from_account)).session_id
  461. @classmethod
  462. def _redact(cls, adapter, store, sid: str, transcript: list, entry: dict, ok_msg: str, fail_msg: str, *ok_args) -> None:
  463. """Blank *entry* in place and persist *transcript* (warns, never raises)."""
  464. entry["content"] = cls._REDACTED
  465. try:
  466. store.rewrite_transcript(sid, transcript, active_only=True)
  467. logger.info(ok_msg, adapter.name, *ok_args)
  468. except Exception as exc:
  469. logger.warning(fail_msg, adapter.name, exc)
  470. def _handle_recall(self, ctx: InboundContext, cmd: str) -> None:
  471. adapter = ctx.adapter
  472. push = ctx.push or {}
  473. if cmd == "Group.CallbackAfterRecallMsg":
  474. seq_list = push.get("recall_msg_seq_list") or []
  475. else:
  476. mid, seq = push.get("msg_id") or "", push.get("msg_seq")
  477. seq_list = [{"msg_id": mid, "msg_seq": seq}] if (mid or seq) else []
  478. if not seq_list:
  479. logger.debug("[%s] Recall callback with empty seq_list, skipping", adapter.name)
  480. return
  481. group_code = (push.get("group_code") or "").strip()
  482. from_account = (push.get("from_account") or "").strip()
  483. for seq_entry in seq_list:
  484. recalled_id = seq_entry.get("msg_id") or str(seq_entry.get("msg_seq") or "")
  485. if not recalled_id:
  486. continue
  487. matched_sk = self._find_processing_session(adapter, recalled_id)
  488. if matched_sk is not None:
  489. self._interrupt_for_recall(adapter, matched_sk, recalled_id, group_code, from_account)
  490. else:
  491. self._patch_transcript(adapter, recalled_id, group_code, from_account, adapter._msg_content_cache.get(recalled_id))
  492. # -- Branch C: interrupt currently-processing message ---------------
  493. @staticmethod
  494. def _find_processing_session(adapter, recalled_id: str) -> Optional[str]:
  495. return next((sk for sk, mid in adapter._processing_msg_ids.items()
  496. if mid == recalled_id and sk in adapter._active_sessions), None)
  497. @classmethod
  498. def _interrupt_for_recall(cls, adapter, session_key: str, recalled_id: str, group_code: str, from_account: str) -> None:
  499. where = f"group {group_code}" if group_code else f"direct chat with {from_account}"
  500. recall_text = (
  501. f"[CRITICAL — MESSAGE RECALLED] The user message that triggered your current task "
  502. f"(message_id=\"{recalled_id}\") in {where} has been recalled/withdrawn by the sender. "
  503. "IGNORE any prior system note asking you to finish processing tool results — the original request is void. "
  504. "Do NOT continue the task, do NOT call more tools, do NOT reference the recalled content. "
  505. "Reply only with a brief acknowledgment such as \"The message has been recalled.\" in the "
  506. "language the user was using."
  507. )
  508. # Set pending + signal directly (bypass handle_message to avoid busy-ack).
  509. # May overwrite a user message pending in the same ~200ms window — acceptable.
  510. adapter._pending_messages[session_key] = MessageEvent(
  511. text=recall_text, message_type=MessageType.TEXT, source=cls._build_source(adapter, group_code, from_account), internal=True)
  512. active_event = adapter._active_sessions.get(session_key)
  513. if active_event is not None:
  514. active_event.set()
  515. logger.info("[%s] Recall interrupt: msg_id=%s session=%s", adapter.name, recalled_id, session_key[:30])
  516. # The interrupted turn persists the recalled content *after* our interrupt — redact later.
  517. recalled_text = adapter._processing_msg_texts.get(session_key, "")
  518. if recalled_text:
  519. cls._schedule_content_redact(adapter, session_key, recalled_text, group_code, from_account)
  520. @classmethod
  521. def _schedule_content_redact(cls, adapter, session_key: str, recalled_text: str, group_code: str, from_account: str) -> None:
  522. async def _redact() -> None:
  523. store = _session_store(adapter)
  524. if not store:
  525. return
  526. try:
  527. sid = cls._resolve_sid(store, adapter, group_code, from_account)
  528. except Exception:
  529. return
  530. # Poll until the recalled content appears — the interrupted turn hasn't finished writing yet.
  531. for _ in range(30):
  532. await asyncio.sleep(0.5)
  533. try:
  534. transcript = store.load_transcript(sid)
  535. except TranscriptReadError as exc:
  536. # No readable rows means nothing to redact; polling on
  537. # would just re-log the same failure (#100788).
  538. logger.warning(
  539. "[%s] Recall redact: transcript unreadable for "
  540. "session %s: %s", adapter.name, sid, exc,
  541. )
  542. return
  543. except Exception:
  544. continue
  545. for entry in transcript:
  546. if entry.get("role") == "user" and entry.get("content") == recalled_text:
  547. cls._redact(adapter, store, sid, transcript, entry, "[%s] Recall redact: session %s",
  548. "[%s] Recall redact failed: %s", session_key[:30])
  549. return
  550. logger.debug("[%s] Recall redact: content not found after polling, session %s", adapter.name, session_key[:30])
  551. adapter._track_task(asyncio.create_task(_redact()))
  552. # -- Branch A/B: patch transcript (session idle) --------------------
  553. @classmethod
  554. def _patch_transcript(cls, adapter, recalled_id: str, group_code: str,
  555. from_account: str, recalled_content: Optional[str] = None) -> None:
  556. store = _session_store(adapter)
  557. if not store:
  558. return
  559. try:
  560. sid = cls._resolve_sid(store, adapter, group_code, from_account)
  561. except Exception as exc:
  562. logger.warning("[%s] Recall: failed to resolve session: %s", adapter.name, exc)
  563. return
  564. try:
  565. # Load transcript from canonical store (state.db). Since PR #29278 added a
  566. # ``platform_message_id`` column to the messages table and ``append_to_transcript`` wires the
  567. # incoming dict's ``message_id`` into it, ``load_transcript`` returns rows with ``message_id``
  568. # set for any message that was observed with one — Branch A1 (exact id match) is the canonical
  569. # path again.
  570. transcript = store.load_transcript(sid)
  571. except TranscriptReadError as exc:
  572. # Not an empty transcript — the rows are unreadable, so recall has
  573. # nothing to match against (#100788).
  574. logger.warning("[%s] Recall: transcript unreadable: %s", adapter.name, exc)
  575. return
  576. except Exception as exc:
  577. logger.warning("[%s] Recall: failed to load transcript: %s", adapter.name, exc)
  578. return
  579. # A1: exact platform message_id match; A2: content-match fallback for rows without a
  580. # platform id (agent-processed @bot messages — run.py doesn't carry msg_id — or pre-column rows).
  581. target = next((e for e in transcript if e.get("message_id") == recalled_id), None)
  582. branch_label = "branch A1: id match"
  583. if target is None and recalled_content:
  584. target = next((e for e in transcript if e.get("role") == "user" and e.get("content") == recalled_content), None)
  585. branch_label = "branch A2: content match"
  586. if target is not None:
  587. cls._redact(adapter, store, sid, transcript, target, "[%s] Recall: redacted msg_id=%s (%s)",
  588. "[%s] Recall: rewrite_transcript failed: %s", recalled_id, branch_label)
  589. return
  590. # Branch B: not found in transcript → append system note
  591. store.append_to_transcript(sid, {
  592. "role": "system", "timestamp": datetime.now(tz=UTC).isoformat(),
  593. "content": f'[recall] message_id="{recalled_id}" has been recalled; do not quote or reference it.',
  594. })
  595. logger.info("[%s] Recall: system note for msg_id=%s (branch B)", adapter.name, recalled_id)
  596. class SkipSelfMiddleware(InboundMiddleware):
  597. """Drop the bot's own messages."""
  598. name = "skip-self"
  599. @staticmethod
  600. def _is_self_reference(from_account: str, bot_id: Optional[str]) -> bool:
  601. return bool(from_account) and from_account == bot_id
  602. async def handle(self, ctx: InboundContext, next_fn) -> None:
  603. if self._is_self_reference(ctx.from_account, ctx.adapter._bot_id):
  604. logger.debug("[%s] Ignoring self-sent message from %s", ctx.adapter.name, ctx.from_account)
  605. return # Stop pipeline
  606. await next_fn()
  607. class ChatRoutingMiddleware(InboundMiddleware):
  608. """Derive chat_id / chat_type / chat_name from push fields."""
  609. name = "chat-routing"
  610. async def handle(self, ctx: InboundContext, next_fn) -> None:
  611. if ctx.group_code:
  612. ctx.chat_id, ctx.chat_type, ctx.chat_name = f"group:{ctx.group_code}", "group", ctx.group_name or ctx.group_code
  613. else:
  614. ctx.chat_id, ctx.chat_type, ctx.chat_name = f"direct:{ctx.from_account}", "dm", ctx.sender_nickname or ctx.from_account
  615. await next_fn()
  616. class AccessPolicy(OwnAccessPolicyMixin):
  617. """DM / group access rules shared by inbound middleware and outbound ``send_dm``."""
  618. ALLOW_ALL_ENV_PREFIX = "YUANBAO"
  619. def __init__(self, dm_policy: str, dm_allow_from: list[str], group_policy: str, group_allow_from: list[str]) -> None:
  620. self._dm_policy = dm_policy
  621. self._allow_from = dm_allow_from
  622. self._group_policy = group_policy
  623. self._group_allow_from = group_allow_from
  624. def is_dm_allowed(self, sender_id: str) -> bool:
  625. """Strict DM authorization — pairing does not imply access."""
  626. return self._is_dm_allowed(sender_id.strip())
  627. def is_dm_intake_allowed(self, sender_id: str) -> bool:
  628. """Whether a DM may reach gateway intake (pairing handshake path)."""
  629. return self._is_dm_intake_allowed(sender_id)
  630. def is_group_allowed(self, group_code: str) -> bool:
  631. """Unlike the shared rule, an ``open`` group still needs the allow-all opt-in: Yuanbao groups
  632. have no runner-side mention gate, so open-without-opt-in would forward every member."""
  633. if self._group_policy == "open":
  634. return self._open_dm_opted_in()
  635. return self._is_group_allowed(group_code.strip())
  636. @property
  637. def dm_policy(self) -> str:
  638. return self._dm_policy
  639. @property
  640. def group_policy(self) -> str:
  641. return self._group_policy
  642. class AccessGuardMiddleware(InboundMiddleware):
  643. """Platform-level DM/group access filter."""
  644. name = "access-guard"
  645. async def handle(self, ctx: InboundContext, next_fn) -> None:
  646. policy: AccessPolicy = ctx.adapter._access_policy
  647. if ctx.chat_type == "dm" and not policy.is_dm_intake_allowed(ctx.from_account):
  648. logger.debug("[%s] DM from %s blocked by dm_policy=%s", ctx.adapter.name, ctx.from_account, policy.dm_policy)
  649. return # Stop pipeline
  650. if ctx.chat_type == "group" and not policy.is_group_allowed(ctx.group_code):
  651. logger.debug("[%s] Group %s blocked by group_policy=%s", ctx.adapter.name, ctx.group_code, policy.group_policy)
  652. return # Stop pipeline
  653. await next_fn()
  654. class AutoSetHomeMiddleware(InboundMiddleware):
  655. """Silently designate the first inbound conversation as home channel (config.yaml + env);
  656. a group home is upgraded by the first DM. Runs after GroupAtGuard so unaddressed group traffic
  657. never claims it; only strictly-authorized senders (allowlist / open opt-in / pairing-approved)
  658. may — intake-only pairing forwards must not."""
  659. name = "auto-sethome"
  660. async def handle(self, ctx: InboundContext, next_fn) -> None:
  661. adapter = ctx.adapter
  662. if not adapter._auto_sethome_done and adapter._sender_may_designate_home(ctx):
  663. _cur_home = _yb_secret("YUANBAO_HOME_CHANNEL", "") or ""
  664. _should_set = not _cur_home or (_cur_home.startswith("group:") and ctx.chat_type == "dm")
  665. if ctx.chat_type == "dm":
  666. adapter._auto_sethome_done = True # DM seen — no further upgrades needed
  667. if _should_set:
  668. self._persist_home(adapter, ctx)
  669. await next_fn()
  670. @staticmethod
  671. def _persist_home(adapter, ctx: InboundContext) -> None:
  672. try:
  673. from gateway.config import HomeChannel, persist_home_channel
  674. home = HomeChannel(platform=Platform.YUANBAO, chat_id=str(ctx.chat_id), name=str(ctx.chat_name or "Home"))
  675. # ``platforms.yuanbao.home_channel`` in the owning profile's config.yaml is the durable record
  676. # ``load_gateway_config`` reads back; the live PlatformConfig is updated so cron/home-channel
  677. # delivery in THIS process has a target without a restart.
  678. persist_home_channel(home)
  679. adapter.config.home_channel = home
  680. # Under a multiplexed secondary's scope the process env is the DEFAULT profile's; writing there
  681. # would make this tenant's chat the default profile's cron/notification home.
  682. if not _profile_scoped():
  683. os.environ["YUANBAO_HOME_CHANNEL"] = str(ctx.chat_id)
  684. logger.info("[%s] Auto-sethome: designated %s (%s) as Yuanbao home channel", adapter.name, ctx.chat_id, ctx.chat_name)
  685. except Exception as e:
  686. logger.warning("[%s] Auto-sethome failed: %s", adapter.name, e)
  687. def _iter_custom_elems(msg_body: list) -> Iterator[tuple[Any, dict]]:
  688. """Yield ``(custom, content)`` for each TIMCustomElem whose ``data`` parses as JSON (any type)."""
  689. for elem in msg_body or []:
  690. if not isinstance(elem, dict) or elem.get("msg_type") != "TIMCustomElem":
  691. continue
  692. content = elem.get("msg_content", {}) or {}
  693. data_str = content.get("data", "") if isinstance(content, dict) else ""
  694. if data_str:
  695. with contextlib.suppress(json.JSONDecodeError, TypeError):
  696. yield json.loads(data_str), content
  697. def _file_name(content: dict) -> str:
  698. """First non-empty of file_name / fileName / filename, stripped."""
  699. return (str(content.get("file_name") or "").strip() or str(content.get("fileName") or "").strip()
  700. or str(content.get("filename") or "").strip())
  701. def _media_ref(kind: str, url: str, name: str = "") -> dict[str, str]:
  702. """media_refs entry; ``name`` is only emitted when non-empty."""
  703. ref: dict[str, str] = {"kind": kind, "url": url}
  704. if name:
  705. ref["name"] = name
  706. return ref
  707. class ExtractContentMiddleware(InboundMiddleware):
  708. """Extract raw text, media refs and forwarded records from msg_body."""
  709. name = "extract-content"
  710. _CARD_CONTENT_MAX_LENGTH = 1000
  711. _UNSUPPORTED = "[unsupported message type]"
  712. @staticmethod
  713. def _format_shared_link(custom: dict) -> str:
  714. """elem_type 1010 (share card) → bracket-placeholder text."""
  715. title, link = custom.get("title", ""), custom.get("link", "")
  716. lines = [f"[share_card: {title} | {link}]" if link else f"[share_card: {title}]"]
  717. max_len = ExtractContentMiddleware._CARD_CONTENT_MAX_LENGTH
  718. preview = next((v for v in (custom.get("card_content"), custom.get("wechat_des")) if v and isinstance(v, str)), None)
  719. if preview:
  720. lines.append(f"Preview: {preview[:max_len] + '...(truncated)' if len(preview) > max_len else preview}")
  721. if link:
  722. lines.append("[visit link for full content]")
  723. return "\n".join(lines)
  724. @staticmethod
  725. def _format_link_understanding(custom: dict) -> Optional[str]:
  726. """elem_type 1007 (link understanding card) → bracket-placeholder text."""
  727. content = custom.get("content")
  728. if not content:
  729. return None
  730. try:
  731. parsed = json.loads(content)
  732. link = parsed.get("link") if isinstance(parsed, dict) else None
  733. except (json.JSONDecodeError, TypeError):
  734. link = None
  735. return f"[link: {link} | visit link for full content]" if link and isinstance(link, str) else None
  736. @staticmethod
  737. def _parse_resource_id(url: str) -> str:
  738. """resourceId from a Yuanbao resource URL's query string, or ""."""
  739. try:
  740. query = urllib.parse.parse_qs(urllib.parse.urlparse(url).query) if url else {}
  741. ids = query.get("resourceId") or query.get("resourceid") or []
  742. return str(ids[0]).strip() if ids else ""
  743. except Exception:
  744. return ""
  745. @staticmethod
  746. def _pick_image_url(content: dict) -> str:
  747. """URL of the medium image (index 1), falling back to index 0, else ""."""
  748. arr = content.get("image_info_array")
  749. arr = arr if isinstance(arr, list) else []
  750. image_info = arr[1] if len(arr) > 1 and isinstance(arr[1], dict) else arr[0] if arr and isinstance(arr[0], dict) else None
  751. return str((image_info or {}).get("url") or "").strip()
  752. @classmethod
  753. def _extract_text(cls, msg_body: list) -> str:
  754. """Plain text from MsgBody: text elems verbatim; media as ``[kind|ybres:RID]`` / ``[kind]``
  755. (file: ``[file:{name}|ybres:RID]``); TIMFaceElem ``[emoji: name]``; custom elems by
  756. elem_type. Parts are space-joined."""
  757. parts: list[str] = []
  758. for elem in msg_body:
  759. elem_type: str = elem.get("msg_type", "")
  760. content: dict = elem.get("msg_content", {})
  761. if elem_type == _TEXT_ELEM_TYPE:
  762. if content.get("text", ""):
  763. parts.append(content["text"])
  764. elif elem_type in ("TIMImageElem", "TIMSoundElem", "TIMVideoFileElem"):
  765. kind = {"TIMImageElem": "image", "TIMSoundElem": "voice", "TIMVideoFileElem": "video"}[elem_type]
  766. url = cls._pick_image_url(content) if kind == "image" else str(content.get("url") or "").strip()
  767. rid = cls._parse_resource_id(url)
  768. parts.append(f"[{kind}|ybres:{rid}]" if rid else f"[{kind}]")
  769. elif elem_type == "TIMFileElem":
  770. filename = content.get("file_name", content.get("fileName", content.get("filename", "")))
  771. rid = cls._parse_resource_id(str(content.get("url") or "").strip())
  772. if rid:
  773. parts.append(f"[file:{filename}|ybres:{rid}]" if filename else f"[file|ybres:{rid}]")
  774. else:
  775. parts.append(f"[file: {filename}]" if filename else "[file]")
  776. elif elem_type == "TIMCustomElem":
  777. parts.append(cls._custom_elem_text(content.get("data", "")))
  778. elif elem_type == "TIMFaceElem":
  779. face_name = ""
  780. if content.get("data", ""):
  781. try:
  782. face_name = (json.loads(content["data"]).get("name") or "").strip()
  783. except (json.JSONDecodeError, TypeError, AttributeError):
  784. pass
  785. parts.append(f"[emoji: {face_name}]" if face_name else "[emoji]")
  786. elif elem_type:
  787. parts.append(f"[{elem_type}]") # unknown element type — keep as placeholder
  788. return " ".join(parts)
  789. @classmethod
  790. def _custom_elem_text(cls, data_val: str) -> str:
  791. """Text for a TIMCustomElem by elem_type: 1002 mention, 1010 share card, 1007 link card,
  792. 1009 forwarded chat-record summary; malformed JSON is passed through verbatim."""
  793. if not data_val:
  794. return cls._UNSUPPORTED
  795. try:
  796. custom = json.loads(data_val)
  797. except (json.JSONDecodeError, TypeError):
  798. return data_val
  799. if not isinstance(custom, dict):
  800. return cls._UNSUPPORTED
  801. ctype = custom.get("elem_type")
  802. if ctype == 1002:
  803. return custom.get("text", "[mention]")
  804. if ctype == 1009:
  805. return custom.get("text", "[chat record]")
  806. if ctype == 1010:
  807. return cls._format_shared_link(custom)
  808. return (cls._format_link_understanding(custom) if ctype == 1007 else None) or cls._UNSUPPORTED
  809. @staticmethod
  810. def _rewrite_slash_command(text: str) -> str:
  811. """Strip; convert a leading full-width slash (Chinese IME) to ASCII so commands match."""
  812. text = text.strip()
  813. return '/' + text[1:] if text.startswith('\uff0f') else text
  814. @staticmethod
  815. def _extract_inbound_media_refs(msg_body: list) -> list[dict[str, str]]:
  816. """Inbound image/file refs: ``[{"kind": "image", "url": ...}, {"kind": "file", "url": ..., "name": ...}]``."""
  817. refs: list[dict[str, str]] = []
  818. for elem in msg_body or []:
  819. if not isinstance(elem, dict):
  820. continue
  821. msg_type = elem.get("msg_type", "")
  822. content = elem.get("msg_content", {}) or {}
  823. if not isinstance(content, dict):
  824. continue
  825. if msg_type == "TIMImageElem":
  826. image_url = ExtractContentMiddleware._pick_image_url(content)
  827. if image_url:
  828. refs.append(_media_ref("image", image_url))
  829. elif msg_type == "TIMFileElem":
  830. file_url = str(content.get("url") or "").strip()
  831. if file_url:
  832. refs.append(_media_ref("file", file_url, _file_name(content)))
  833. return refs
  834. @staticmethod
  835. def _extract_forwarded_records(msg_body: list, user_id: str = "") -> Optional[dict]:
  836. """ForwardMsgData for elem_type 1009 (WeChat forward), or None. Payload lives in
  837. ``msg_content.ext_map`` (pb field 999) under ``wexin_forward_msg_[id]_[userid]`` keys as
  838. base64 protobuf (NOT JSON); the first entry decoding to ``sub_type == 1`` wins."""
  839. for custom, content in _iter_custom_elems(msg_body):
  840. if not (isinstance(custom, dict) and custom.get("elem_type") == 1009):
  841. continue
  842. ext_map = content.get("ext_map") or {}
  843. if not isinstance(ext_map, dict) or not ext_map:
  844. return None
  845. for key, value in ext_map.items():
  846. if not key.startswith("wexin_forward_msg_") or not isinstance(value, str) or not value:
  847. continue
  848. with contextlib.suppress(binascii.Error, ValueError):
  849. data = decode_forward_msg_data(base64.b64decode(value))
  850. if isinstance(data, dict) and data.get("sub_type") == 1:
  851. return data
  852. return None
  853. async def handle(self, ctx: InboundContext, next_fn) -> None:
  854. ctx.raw_text = self._rewrite_slash_command(self._extract_text(ctx.msg_body))
  855. ctx.media_refs = self._extract_inbound_media_refs(ctx.msg_body)
  856. ctx.forwarded_records = self._extract_forwarded_records(ctx.msg_body, ctx.from_account)
  857. await next_fn()
  858. class PlaceholderFilterMiddleware(InboundMiddleware):
  859. """Skip pure placeholder messages (e.g. '[image]' with no media)."""
  860. name = "placeholder-filter"
  861. SKIPPABLE_PLACEHOLDERS: frozenset = frozenset({"[image]", "[图片]", "[file]", "[文件]", "[video]", "[视频]", "[voice]", "[语音]"})
  862. @classmethod
  863. def is_skippable_placeholder(cls, text: str, media_count: int = 0) -> bool:
  864. return media_count <= 0 and text.strip() in cls.SKIPPABLE_PLACEHOLDERS
  865. async def handle(self, ctx: InboundContext, next_fn) -> None:
  866. if self.is_skippable_placeholder(ctx.raw_text, len(ctx.media_refs)):
  867. logger.debug("[%s] Skipping placeholder message: %r", ctx.adapter.name, ctx.raw_text)
  868. return # Stop pipeline
  869. await next_fn()
  870. class OwnerCommandMiddleware(InboundMiddleware):
  871. """Bot-owner slash commands in groups: allowlisted commands skip @Bot; non-owner attempts are rejected."""
  872. name = "owner-command"
  873. ALLOWLIST: frozenset = frozenset({"/new", "/reset", "/retry", "/undo", "/stop", "/approve", "/deny", "/bg", "/btw", "/queue", "/q"})
  874. _rewrite_slash_command = staticmethod(ExtractContentMiddleware._rewrite_slash_command)
  875. @classmethod
  876. def _detect_owner_command(cls, *, push: dict, msg_body: list, chat_type: str, from_account: str) -> tuple[Optional[str], Optional[str], bool]:
  877. """→ (cmd, cmd_line, is_owner); (None, None, False) when not an allowlisted command."""
  878. if chat_type != "group" or not cls.ALLOWLIST:
  879. return None, None, False
  880. # Only recognise commands when there is exactly one text segment.
  881. text_elems = [e for e in (msg_body or []) if e.get("msg_type") == _TEXT_ELEM_TYPE]
  882. if len(text_elems) != 1:
  883. return None, None, False
  884. cmd_line = cls._rewrite_slash_command((text_elems[0].get("msg_content") or {}).get("text", ""))
  885. if not cmd_line.startswith("/"):
  886. return None, None, False
  887. cmd = cmd_line.split(maxsplit=1)[0].lower()
  888. if cmd not in cls.ALLOWLIST:
  889. return None, None, False
  890. # Owner ⇔ push.from_account == push.bot_owner_id; these commands are privileged
  891. # (/approve, /stop, /reset…) so a non-owner must never run them.
  892. owner_id = str((push or {}).get("bot_owner_id") or "").strip()
  893. return cmd, cmd_line, bool(owner_id) and owner_id == from_account
  894. async def handle(self, ctx: InboundContext, next_fn) -> None:
  895. adapter = ctx.adapter
  896. matched_cmd, cmd_line, is_owner = self._detect_owner_command(
  897. push=ctx.push, msg_body=ctx.msg_body, chat_type=ctx.chat_type, from_account=ctx.from_account,
  898. )
  899. if matched_cmd and not is_owner:
  900. logger.info("[%s] Reject non-owner slash command: chat=%s from=%s cmd=%s", adapter.name, ctx.chat_id, ctx.from_account, matched_cmd)
  901. adapter._track_task(asyncio.create_task(
  902. adapter.send(ctx.chat_id, t("platform.yuanbao.owner_command_denied", command=matched_cmd)),
  903. name=f"yuanbao-owner-cmd-denial-{matched_cmd}"))
  904. return # Stop pipeline
  905. if matched_cmd and is_owner and cmd_line:
  906. logger.info("[%s] Bot owner slash command: chat=%s from=%s cmd=%s", adapter.name, ctx.chat_id, ctx.from_account, matched_cmd)
  907. ctx.owner_command = matched_cmd
  908. ctx.raw_text = cmd_line # clean command text
  909. await next_fn()
  910. class BuildSourceMiddleware(InboundMiddleware):
  911. name = "build-source"
  912. async def handle(self, ctx: InboundContext, next_fn) -> None:
  913. ctx.source = ctx.adapter.build_source(
  914. chat_id=ctx.chat_id, chat_type=ctx.chat_type, chat_name=ctx.chat_name,
  915. user_id=ctx.from_account or None, user_name=ctx.sender_nickname or ctx.from_account,
  916. thread_id="main" if ctx.chat_type == "group" else None,
  917. )
  918. await next_fn()
  919. class GroupAtGuardMiddleware(InboundMiddleware):
  920. """Group chat: observe non-@bot messages into the transcript; only @Bot (or owner commands) proceed."""
  921. name = "group-at-guard"
  922. @staticmethod
  923. def _iter_bot_mentions(msg_body: list, bot_id: Optional[str]) -> Iterator[dict]:
  924. """Yield @bot elems: TIMCustomElem whose data JSON has elem_type 1002 and user_id == bot_id."""
  925. if not bot_id:
  926. return
  927. for custom, _content in _iter_custom_elems(msg_body):
  928. if custom.get("elem_type") == 1002 and custom.get("user_id") == bot_id:
  929. yield custom
  930. @classmethod
  931. def _is_at_bot(cls, msg_body: list, bot_id: Optional[str]) -> bool:
  932. return any(True for _ in cls._iter_bot_mentions(msg_body, bot_id))
  933. @classmethod
  934. def _extract_bot_mention_text(cls, msg_body: list, bot_id: Optional[str]) -> str:
  935. """Display text used to @-mention this bot (e.g. ``@yuanbao-bot``), or ""."""
  936. return next((t for t in (str(c.get("text") or "").strip() for c in cls._iter_bot_mentions(msg_body, bot_id)) if t), "")
  937. @staticmethod
  938. def _build_group_channel_prompt(msg_body: list, bot_id: Optional[str]) -> str:
  939. """Per-turn group-chat prompt that highlights which message to respond to."""
  940. bot_mention = GroupAtGuardMiddleware._extract_bot_mention_text(msg_body, bot_id) or "unknown"
  941. return (
  942. "You are handling a Yuanbao group chat message.\n"
  943. f"- Your identity: user_id={bot_id or 'unknown'}, @-mention name in this group={bot_mention}\n"
  944. "- Lines in history prefixed with `[nickname|user_id]` are observed group context "
  945. "and are not necessarily addressed to you.\n"
  946. "- Treat only the current new message as a request explicitly directed at you, "
  947. "and answer it directly."
  948. )
  949. @classmethod
  950. def _observe_group_message(cls, adapter, source, sender_display: str, text: str, *, ctx: InboundContext,
  951. msg_id: Optional[str] = None, forwarded_records: Optional[dict] = None) -> None:
  952. """Record a group message as ``role: user`` ``[nickname|user_id]\\n<content>`` without
  953. invoking the agent, so later @bot turns see the full conversation."""
  954. store = _session_store(adapter)
  955. if not store:
  956. return
  957. try:
  958. session_entry = store.get_or_create_session(source)
  959. body_text = text
  960. if forwarded_records:
  961. summary = ForwardedRecordsParseMiddleware.build_forward_text(forwarded_records, ctx=ctx, is_dispatch=False)
  962. if summary:
  963. body_text = f"{text}\n{summary}" if text else summary
  964. entry: dict = {
  965. "role": "user", "content": f"[{sender_display}|{source.user_id or 'unknown'}]\n{body_text}",
  966. "timestamp": datetime.now(tz=UTC).isoformat(), "observed": True,
  967. }
  968. if msg_id:
  969. entry["message_id"] = msg_id
  970. store.append_to_transcript(session_entry.session_id, entry)
  971. except Exception as exc:
  972. logger.warning("[%s] Failed to observe group message: %s", adapter.name, exc)
  973. async def handle(self, ctx: InboundContext, next_fn) -> None:
  974. adapter = ctx.adapter
  975. if ctx.chat_type == "group" and not ctx.owner_command and not self._is_at_bot(ctx.msg_body, adapter._bot_id):
  976. self._observe_group_message(
  977. adapter, ctx.source, ctx.sender_nickname or ctx.from_account, ctx.raw_text,
  978. msg_id=ctx.msg_id or None, forwarded_records=ctx.forwarded_records, ctx=ctx,
  979. )
  980. logger.info("[%s] Group message observed (no @bot): chat=%s from=%s", adapter.name, ctx.chat_id, ctx.from_account)
  981. return # Stop pipeline — message observed but not dispatched
  982. await next_fn()
  983. class GroupAttributionMiddleware(InboundMiddleware):
  984. """Group @bot turns: build channel_prompt, rewrite raw_text to ``[nickname|user_id]\\n<content>``
  985. (matches observed-history format) and clear ``source.user_name`` to suppress the runner's
  986. ``[user_name]`` prefix."""
  987. name = "group-attribution"
  988. async def handle(self, ctx: InboundContext, next_fn) -> None:
  989. if ctx.chat_type == "group" and not ctx.owner_command:
  990. ctx.channel_prompt = GroupAtGuardMiddleware._build_group_channel_prompt(ctx.msg_body, ctx.adapter._bot_id)
  991. ctx.raw_text = f"[{ctx.sender_nickname or ctx.from_account or 'unknown'}|{ctx.from_account or 'unknown'}]\n{ctx.raw_text}"
  992. if ctx.source is not None:
  993. ctx.source = dataclasses.replace(ctx.source, user_name=None)
  994. await next_fn()
  995. class YuanbaoMessageType(Enum):
  996. CHAT_RECORD = "chat_record" # yuanbao-local subtype; coerced back to MessageType in DispatchMiddleware
  997. _ELEM_MESSAGE_TYPES = {"TIMImageElem": MessageType.PHOTO, "TIMSoundElem": MessageType.VOICE,
  998. "TIMVideoFileElem": MessageType.VIDEO, "TIMFileElem": MessageType.DOCUMENT}
  999. class ClassifyMessageTypeMiddleware(InboundMiddleware):
  1000. """MessageType (or yuanbao-local YuanbaoMessageType) from text and msg_body elements."""
  1001. name = "classify-msg-type"
  1002. @staticmethod
  1003. def _classify(text: str, msg_body: list):
  1004. if text.startswith("/"):
  1005. return MessageType.COMMAND
  1006. for elem in msg_body:
  1007. etype = elem.get("msg_type", "")
  1008. mapped = _ELEM_MESSAGE_TYPES.get(etype)
  1009. if mapped is not None:
  1010. return mapped
  1011. if etype == "TIMCustomElem":
  1012. try:
  1013. custom = json.loads((elem.get("msg_content") or {}).get("data", ""))
  1014. except (json.JSONDecodeError, TypeError):
  1015. custom = None
  1016. if isinstance(custom, dict) and custom.get("elem_type") == 1009:
  1017. return YuanbaoMessageType.CHAT_RECORD
  1018. return MessageType.TEXT
  1019. async def handle(self, ctx: InboundContext, next_fn) -> None:
  1020. ctx.msg_type = self._classify(ctx.raw_text, ctx.msg_body)
  1021. await next_fn()
  1022. class QuoteContextMiddleware(InboundMiddleware):
  1023. """Extract quote/reply context from cloud_custom_data."""
  1024. name = "quote-context"
  1025. def _extract_quote_context(self, cloud_custom_data: str) -> tuple[Optional[str], Optional[str]]:
  1026. """(quote_id, quote_text) from cloud_custom_data → MessageEvent.reply_to_*."""
  1027. try:
  1028. parsed = json.loads(cloud_custom_data) if cloud_custom_data else None
  1029. except (json.JSONDecodeError, TypeError):
  1030. parsed = None
  1031. quote = parsed.get("quote") if isinstance(parsed, dict) else None
  1032. if not isinstance(quote, dict):
  1033. return None, None
  1034. quote_id = str(quote.get("id") or "").strip() or None
  1035. desc = str(quote.get("desc") or "").strip()
  1036. sender = str(quote.get("sender_nickname") or quote.get("sender_id") or "").strip()
  1037. return quote_id, (f"{sender}: {desc}" if sender else desc) if desc else None
  1038. async def _extract_media_refs_from_transcript(self, ctx: InboundContext) -> list[tuple[str, str, str]]:
  1039. """``(rid, kind, filename)`` for ybres anchors in the quoted transcript message; [] when
  1040. there is no reply_to id, no store/source, or no resolvable anchors."""
  1041. if ctx.reply_to_message_id is None:
  1042. return []
  1043. adapter = ctx.adapter
  1044. media_refs: list[tuple[str, str, str]] = []
  1045. try:
  1046. store = _session_store(adapter)
  1047. if not store or ctx.source is None:
  1048. return []
  1049. history = store.load_transcript(store.get_or_create_session(ctx.source).session_id)
  1050. for msg in reversed(history or []):
  1051. mid = msg.get("message_id", "")
  1052. if not mid or mid != ctx.reply_to_message_id:
  1053. continue
  1054. _content = msg.get("content", "")
  1055. if isinstance(_content, str) and "|ybres:" in _content:
  1056. media_refs.extend(_iter_ybres_refs(_YB_RES_REF_RE.finditer(_content)))
  1057. break
  1058. except TranscriptReadError as exc:
  1059. # Quote resolution degrades to "no refs" rather than pretending
  1060. # the quoted message was never seen (#100788).
  1061. logger.warning(
  1062. "[%s] quote transcript lookup: transcript unreadable: %s",
  1063. getattr(adapter, "name", "yuanbao"), exc,
  1064. )
  1065. except Exception as exc:
  1066. logger.warning("[%s] quote transcript lookup failed: %s", getattr(adapter, "name", "yuanbao"), exc)
  1067. return media_refs
  1068. async def handle(self, ctx: InboundContext, next_fn) -> None:
  1069. ctx.reply_to_message_id, ctx.reply_to_text = self._extract_quote_context(ctx.cloud_custom_data)
  1070. ctx.quote_media_refs = await self._extract_media_refs_from_transcript(ctx)
  1071. await next_fn()
  1072. class ForwardedRecordsParseMiddleware(InboundMiddleware):
  1073. """Deep-parse WeChat forwarded chat records (elem_type 1009) on ``ctx.forwarded_records``:
  1074. render media as ``[kind|ybres:RID]``, append refs to ``ctx.media_refs`` and rewrite raw_text.
  1075. No run-time fallback for earlier forwards — GroupAtGuard already rendered summaries at observe
  1076. time. On any failure raw_text is left untouched."""
  1077. name = "forwarded-records-parse"
  1078. FORWARD_MSG_TEXT_MAX_CHARS = 1000 # per-record text cap; record count is NOT capped
  1079. async def handle(self, ctx: InboundContext, next_fn) -> None:
  1080. try:
  1081. if ctx.forwarded_records:
  1082. await self._send_loading_heartbeat(ctx)
  1083. ctx.raw_text = self.build_forward_text(ctx.forwarded_records, ctx=ctx, is_dispatch=True)
  1084. except Exception as exc:
  1085. logger.warning("[%s] forwarded-records deep parse failed: %s", getattr(ctx.adapter, "name", "yuanbao"), exc)
  1086. await next_fn()
  1087. @staticmethod
  1088. async def _send_loading_heartbeat(ctx: InboundContext) -> None:
  1089. """Best-effort RUNNING heartbeat so the user sees a loading bubble."""
  1090. with contextlib.suppress(Exception):
  1091. await ctx.adapter._outbound.heartbeat.send_heartbeat_once(ctx.chat_id, WS_HEARTBEAT_RUNNING)
  1092. @classmethod
  1093. def _media_marker(cls, media: dict, plain_text: str = "") -> tuple[str, Optional[dict[str, str]]]:
  1094. """One ``multimedia`` entry → ``(marker, ref)``: ``[kind|ybres:RID]`` + media_refs dict when a
  1095. RID/URL is usable, else a plain ``[kind] name`` marker and ``ref=None``."""
  1096. media_type = (media.get("type", "") or media.get("doc_type", "")).strip().lower()
  1097. url = str(media.get("url") or "").strip()
  1098. file_name = str(media.get("file_name") or "").strip()
  1099. # media_id is directly usable as a ybres RID; else parse resourceId from the URL.
  1100. rid = str(media.get("media_id") or "").strip() or ExtractContentMiddleware._parse_resource_id(url)
  1101. kind = {"image": "image", "file": "file", "document": "file", "code": "file", "video": "video"}.get(media_type)
  1102. if kind and url and rid:
  1103. return f"[{kind}|ybres:{rid}] {file_name}".rstrip(), _media_ref(kind, url, file_name if kind == "file" else "")
  1104. if kind == "image":
  1105. return f"[image] {file_name or plain_text}".rstrip(), None
  1106. if kind == "file":
  1107. return f"[file] {file_name}".rstrip(), None
  1108. if kind == "video":
  1109. return f"[video] {file_name or url}".rstrip(), None
  1110. if media_type == "url": # link share (e.g. WeChat article) — keep URL for the agent
  1111. return f"[link] {file_name or str(media.get('title') or '')} {url}".rstrip(), None
  1112. return f"[{media_type or 'media'}] {url or file_name}".rstrip(), None
  1113. @classmethod
  1114. def _walk_forward_msgs(cls, forward_data: dict) -> Iterator[tuple[str, str, list[dict[str, str]]]]:
  1115. """Yield ``(sender, body, refs)`` per ``ForwardMsgData['msg']`` record; body capped at
  1116. FORWARD_MSG_TEXT_MAX_CHARS. ``refs`` keeps textual order — PatchAnchorsMiddleware relies on it."""
  1117. for msg in (forward_data.get("msg") if isinstance(forward_data, dict) else None) or []:
  1118. if not isinstance(msg, dict):
  1119. continue
  1120. plain_text = msg.get("plainText", "")
  1121. refs: list[dict[str, str]] = []
  1122. parts: list[str] = []
  1123. for mc in msg.get("msgContent", []) or []:
  1124. if not isinstance(mc, dict):
  1125. continue
  1126. mc_type = mc.get("type", 0) # EnumMsgContentType: 1 TEXT, 2 MULTIMEDIA, 3 nested FORWARD
  1127. if mc_type == 1:
  1128. parts.append(mc.get("text", ""))
  1129. elif mc_type == 2:
  1130. for media in mc.get("multimedia", []) or []:
  1131. if isinstance(media, dict):
  1132. marker, ref = cls._media_marker(media, plain_text)
  1133. parts.append(marker)
  1134. if ref is not None:
  1135. refs.append(ref)
  1136. elif mc_type == 3:
  1137. parts.append("[嵌套聊天记录]")
  1138. elif plain_text:
  1139. parts.append(plain_text)
  1140. rendered = " ".join(p for p in parts if p) or plain_text
  1141. if len(rendered) > cls.FORWARD_MSG_TEXT_MAX_CHARS:
  1142. rendered = rendered[: cls.FORWARD_MSG_TEXT_MAX_CHARS] + "…(已截断)"
  1143. yield msg.get("sender", ""), rendered, refs
  1144. @classmethod
  1145. def build_forward_text(cls, forward_data: dict, *, ctx: InboundContext, is_dispatch: bool) -> str:
  1146. """Render ``ForwardMsgData`` as ``发送人:正文`` lines with media markers. When ``is_dispatch``,
  1147. refs go to ``ctx.media_refs`` and a ``用户附言:`` footer is added (observe-time callers skip both)."""
  1148. lines = [f"当前用户的昵称为{ctx.sender_nickname or '用户'}", "以下为用户的聊天记录"]
  1149. for sender, body, refs in cls._walk_forward_msgs(forward_data):
  1150. lines.append(f"{sender}:{body}")
  1151. if is_dispatch:
  1152. ctx.media_refs.extend(refs)
  1153. text = "\n".join(lines)
  1154. if is_dispatch and ctx.raw_text.strip():
  1155. text += f"\n\n用户附言:{ctx.raw_text.strip()}"
  1156. return text
  1157. class MediaResolveMiddleware(InboundMiddleware):
  1158. """Resolve inbound media references to local cached files. Yuanbao COS hostnames resolve to
  1159. private IPs (tripping vision_tools' SSRF guard), so we download ourselves and hand the model
  1160. local paths."""
  1161. name = "media-resolve"
  1162. # Resource download cache keyed by resourceId: rid -> (local_path, mime, ts)
  1163. _resource_cache: ClassVar[dict[str, tuple[str, str, float]]] = {}
  1164. _RESOURCE_CACHE_TTL_S: ClassVar[int] = 24 * 60 * 60
  1165. _RESOURCE_CACHE_MAX_SIZE: ClassVar[int] = 256
  1166. @classmethod
  1167. def _get_cached_resource(cls, resource_id: str) -> Optional[tuple[str, str]]:
  1168. """Cached ``(local_path, mime)`` if unexpired and the file still exists (cache dir may be swept)."""
  1169. entry = cls._resource_cache.get(resource_id) if resource_id else None
  1170. if entry is None:
  1171. return None
  1172. local_path, mime, ts = entry
  1173. if time.time() - ts > cls._RESOURCE_CACHE_TTL_S or not os.path.isfile(local_path):
  1174. cls._resource_cache.pop(resource_id, None)
  1175. return None
  1176. return local_path, mime
  1177. @classmethod
  1178. def _put_cached_resource(cls, resource_id: str, local_path: str, mime: str) -> None:
  1179. """Cache a download result; evicts the oldest 25% when at capacity."""
  1180. if not resource_id:
  1181. return
  1182. if len(cls._resource_cache) >= cls._RESOURCE_CACHE_MAX_SIZE:
  1183. sorted_keys = sorted(cls._resource_cache, key=lambda k: cls._resource_cache[k][2])
  1184. for k in sorted_keys[: cls._RESOURCE_CACHE_MAX_SIZE // 4]:
  1185. cls._resource_cache.pop(k, None)
  1186. cls._resource_cache[resource_id] = (local_path, mime, time.time())
  1187. @classmethod
  1188. def _append_cached_resource(cls, adapter, resource_id: str, media_paths: list[str], mimes: list[str]) -> bool:
  1189. """Append a cached resource to the output lists; False on miss."""
  1190. hit = cls._get_cached_resource(resource_id)
  1191. if hit is None:
  1192. return False
  1193. logger.debug("[%s] resource cache hit: rid=%s path=%s", adapter.name, resource_id, hit[0])
  1194. media_paths.append(hit[0])
  1195. mimes.append(hit[1])
  1196. return True
  1197. @staticmethod
  1198. def _guess_image_ext_from_url(url: str) -> str:
  1199. ext = os.path.splitext(urllib.parse.urlparse(url).path)[1].lower()
  1200. return ext if ext in {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".heic", ".tiff"} else ".jpg"
  1201. @staticmethod
  1202. async def _fetch_resource_url(adapter, resource_id: str) -> str:
  1203. """Exchange a ``resourceId`` for a direct download URL via ``/api/resource/v1/download``,
  1204. with a single 401-retry after token force-refresh. Raises on failure."""
  1205. resource_id = resource_id.strip()
  1206. if not resource_id:
  1207. raise RuntimeError("missing resource_id")
  1208. def _auth_headers(token_data: dict, fallback_source: str) -> Optional[dict]:
  1209. token = str(token_data.get("token") or "").strip()
  1210. bot_id = str(token_data.get("bot_id") or adapter._bot_id or adapter._app_key).strip()
  1211. if not token or not bot_id:
  1212. return None
  1213. source = str(token_data.get("source") or fallback_source).strip() or "web"
  1214. return {"Content-Type": "application/json", "X-ID": bot_id, "X-Token": token, "X-Source": source}
  1215. headers = _auth_headers(await adapter._get_cached_token(), "web")
  1216. if headers is None:
  1217. raise RuntimeError("missing token or bot_id for resource download")
  1218. api_url = f"{adapter._api_domain}/api/resource/v1/download"
  1219. async with httpx.AsyncClient(timeout=15.0, follow_redirects=True) as client:
  1220. for attempt in range(2):
  1221. resp = await client.get(api_url, params={"resourceId": resource_id}, headers=headers)
  1222. if resp.status_code == 401 and attempt == 0:
  1223. token_data = await SignManager.force_refresh(adapter._app_key, adapter._app_secret, adapter._api_domain)
  1224. headers = _auth_headers(token_data, headers["X-Source"] or "web")
  1225. if headers is None:
  1226. break
  1227. continue
  1228. resp.raise_for_status()
  1229. payload = resp.json()
  1230. code = payload.get("code")
  1231. if code not in {None, 0}:
  1232. raise RuntimeError(f"resource/v1/download failed: code={code}, msg={payload.get('msg', '')}")
  1233. data = payload.get("data") if isinstance(payload.get("data"), dict) else payload
  1234. real_url = str((data or {}).get("url") or (data or {}).get("realUrl") or "").strip()
  1235. if real_url:
  1236. return real_url
  1237. raise RuntimeError("resource/v1/download missing url/realUrl")
  1238. raise RuntimeError("resource/v1/download did not return a URL")
  1239. @staticmethod
  1240. async def _resolve_download_url(adapter, url: str) -> str:
  1241. """Resolve a Yuanbao resource placeholder URL (``…/api/resource/download?resourceId=…``,
  1242. which 401s on direct GET) to a fetchable URL via the business API; passthrough otherwise."""
  1243. resource_id = ExtractContentMiddleware._parse_resource_id(url)
  1244. if not resource_id:
  1245. return url
  1246. try:
  1247. return await MediaResolveMiddleware._fetch_resource_url(adapter, resource_id)
  1248. except Exception:
  1249. return url
  1250. @classmethod
  1251. async def _download_and_cache(
  1252. cls, adapter, *, fetch_url: str, kind: str,
  1253. file_name: Optional[str] = None, log_tag: str = "", resource_id: str = "",
  1254. ) -> Optional[tuple[str, str]]:
  1255. """Download a Yuanbao resource into the local media cache → ``(local_path, mime)`` or None.
  1256. A *resource_id* is checked against the in-memory cache first."""
  1257. if resource_id:
  1258. hit = cls._get_cached_resource(resource_id)
  1259. if hit is not None:
  1260. logger.debug("[%s] resource cache hit: rid=%s path=%s", adapter.name, resource_id, hit[0])
  1261. return hit
  1262. try:
  1263. file_bytes, content_type = await media_download_url(fetch_url, max_size_mb=adapter.MEDIA_MAX_SIZE_MB)
  1264. except Exception as exc:
  1265. logger.warning("[%s] inbound media download failed: kind=%s %s err=%s", adapter.name, kind, log_tag, exc)
  1266. return None
  1267. if kind == "image":
  1268. ext = cls._guess_image_ext_from_url(fetch_url)
  1269. try:
  1270. local_path = await cache_image_from_bytes_async(file_bytes, ext=ext)
  1271. except ValueError as exc:
  1272. logger.warning("[%s] inbound image cache rejected: %s err=%s", adapter.name, log_tag, exc)
  1273. return None
  1274. mime = guess_mime_type(f"image{ext}")
  1275. if not mime.startswith("image/"):
  1276. mime = content_type if content_type.startswith("image/") else "image/jpeg"
  1277. elif kind == "video":
  1278. # Yuanbao video resources carry no reliable extension; default to mp4.
  1279. local_path = await cache_video_from_bytes_async(file_bytes)
  1280. mime = guess_mime_type(local_path) or (content_type if content_type.startswith("video/") else "video/mp4")
  1281. else: # file
  1282. file_name = file_name or os.path.basename(urllib.parse.urlparse(fetch_url).path) or "file"
  1283. try:
  1284. local_path = await cache_document_from_bytes_async(file_bytes, file_name)
  1285. except Exception as exc:
  1286. logger.warning("[%s] inbound file cache failed: %s err=%s", adapter.name, log_tag, exc)
  1287. return None
  1288. mime = guess_mime_type(file_name) or content_type or "application/octet-stream"
  1289. cls._put_cached_resource(resource_id, local_path, mime)
  1290. return local_path, mime
  1291. @classmethod
  1292. async def _resolve_media_urls(cls, adapter, media_refs: list[dict[str, str]]) -> tuple[list[str], list[str]]:
  1293. """Resolve inbound media refs → (local_paths, mime_types); same bounded-concurrency,
  1294. order-preserving, exception-isolated contract as :meth:`_resolve_ybres_refs`."""
  1295. media_urls: list[str] = []
  1296. media_types: list[str] = []
  1297. active: list[tuple[str, str, str, str]] = [] # (kind, filename, rid, url)
  1298. for ref in media_refs:
  1299. kind = str(ref.get("kind") or "").strip().lower()
  1300. url = str(ref.get("url") or "").strip()
  1301. if kind not in _RESOLVABLE_MEDIA_KINDS or not url:
  1302. continue
  1303. rid = ExtractContentMiddleware._parse_resource_id(url)
  1304. if rid and cls._append_cached_resource(adapter, rid, media_urls, media_types):
  1305. continue
  1306. active.append((kind, str(ref.get("name") or "").strip(), rid or "", url))
  1307. if active:
  1308. await cls._gather_resolve(
  1309. adapter, active, "media", media_urls, media_types,
  1310. get_url=lambda url: cls._resolve_download_url(adapter, url),
  1311. fail_fmt="[%s] inbound media resolve failed: kind=%s url=%s err=%s", fail_args=lambda kind, url: (kind, url),
  1312. crash_fmt="[%s] inbound media resolve crashed: kind=%s url=%s err=%s", crash_args=lambda kind, url: (kind, url[:80]),
  1313. log_tag=lambda url: f"placeholder_url={url[:80]}",
  1314. )
  1315. return media_urls, media_types
  1316. @classmethod
  1317. async def _gather_resolve(cls, adapter, active, scope, out_paths, out_mimes, *,
  1318. get_url, fail_fmt, fail_args, crash_fmt, crash_args, log_tag) -> None:
  1319. """Resolve ``(kind, filename, rid, key)`` items under bounded concurrency — ``await get_url(key)``
  1320. then download+cache — appending successes in input order. ``return_exceptions=True`` isolates
  1321. per-item failures; the batch summary line keeps stable fields (concurrency vs elapsed_ms) for
  1322. offline aggregation."""
  1323. semaphore = asyncio.Semaphore(adapter.media_resolve_concurrency)
  1324. async def _one(kind: str, filename: str, rid: str, key: str) -> Optional[tuple[str, str]]:
  1325. async with semaphore:
  1326. try:
  1327. fetch_url = await get_url(key)
  1328. except Exception as exc:
  1329. logger.warning(fail_fmt, adapter.name, *fail_args(kind, key), exc)
  1330. return None
  1331. return await cls._download_and_cache(
  1332. adapter, fetch_url=fetch_url, kind=kind, file_name=filename or None, log_tag=log_tag(key), resource_id=rid,
  1333. )
  1334. _t0 = time.monotonic()
  1335. results = await asyncio.gather(*(_one(*item) for item in active), return_exceptions=True)
  1336. _elapsed_ms = int((time.monotonic() - _t0) * 1000)
  1337. _failed = 0
  1338. for (kind, _filename, _rid, key), result in zip(active, results):
  1339. if isinstance(result, BaseException):
  1340. logger.warning(crash_fmt, adapter.name, *crash_args(kind, key), result)
  1341. if result is None or isinstance(result, BaseException):
  1342. _failed += 1
  1343. else:
  1344. out_paths.append(result[0])
  1345. out_mimes.append(result[1])
  1346. logger.info(
  1347. "[%s] media resolve batch: scope=%s concurrency=%d total=%d ok=%d failed=%d elapsed_ms=%d",
  1348. adapter.name, scope, adapter.media_resolve_concurrency, len(active), len(out_paths), _failed, _elapsed_ms,
  1349. )
  1350. @classmethod
  1351. async def _resolve_ybres_refs(cls, adapter, refs: list[tuple[str, str, str]], *, log_prefix: str) -> tuple[list[str], list[str]]:
  1352. """Resolve ``(rid, kind, filename)`` ybres tuples to local paths (bounded concurrency,
  1353. input order preserved, per-rid failures isolated). Cache hits are served without a fetch."""
  1354. media_paths: list[str] = []
  1355. mimes: list[str] = []
  1356. active = [(kind, filename, rid, rid) for rid, kind, filename in refs
  1357. if kind in _RESOLVABLE_MEDIA_KINDS and not cls._append_cached_resource(adapter, rid, media_paths, mimes)]
  1358. if active:
  1359. await cls._gather_resolve(
  1360. adapter, active, "ybres", media_paths, mimes,
  1361. get_url=lambda rid: cls._fetch_resource_url(adapter, rid),
  1362. fail_fmt="[%s] %s resolve failed: rid=%s kind=%s err=%s", fail_args=lambda kind, rid: (log_prefix, rid, kind),
  1363. crash_fmt="[%s] %s resolve crashed: rid=%s kind=%s err=%s", crash_args=lambda kind, rid: (log_prefix, rid, kind),
  1364. log_tag=lambda rid: f"{log_prefix} rid={rid}",
  1365. )
  1366. return media_paths, mimes
  1367. @classmethod
  1368. async def _collect_observed_media(cls, adapter, source) -> tuple[list[str], list[str]]:
  1369. """Resolve recent observed image/file anchors from the transcript into ``(local_paths, mimes)``."""
  1370. store = _session_store(adapter)
  1371. if not store:
  1372. return [], []
  1373. try:
  1374. history = store.load_transcript(store.get_or_create_session(source).session_id)
  1375. except TranscriptReadError as exc:
  1376. # Hydrate nothing rather than silently acting as if the session had no observed media.
  1377. logger.warning("[%s] Observed-media hydration: transcript unreadable: %s", adapter.name, exc)
  1378. return [], []
  1379. except Exception as exc:
  1380. logger.warning("[%s] Observed-media hydration setup failed: %s", adapter.name, exc)
  1381. return [], []
  1382. # Walk newest→oldest (matches within a message too) so the per-turn cap keeps the
  1383. # *latest* refs; ``order`` is reversed back to chronological before resolving.
  1384. order: list[tuple[str, str, str]] = [] # (rid, kind, filename)
  1385. seen: set = set()
  1386. for msg in reversed((history or [])[-OBSERVED_MEDIA_BACKFILL_LOOKBACK:]):
  1387. content = msg.get("content")
  1388. if not isinstance(content, str) or "|ybres:" not in content:
  1389. continue
  1390. for rid, kind, filename in _iter_ybres_refs(reversed(list(_YB_RES_REF_RE.finditer(content)))):
  1391. if rid not in seen:
  1392. seen.add(rid)
  1393. order.append((rid, kind, filename))
  1394. if len(order) >= OBSERVED_MEDIA_BACKFILL_MAX_RESOLVE_PER_TURN:
  1395. break
  1396. if len(order) >= OBSERVED_MEDIA_BACKFILL_MAX_RESOLVE_PER_TURN:
  1397. break
  1398. if not order:
  1399. return [], []
  1400. return await cls._resolve_ybres_refs(adapter, order[::-1], log_prefix="observed-media")
  1401. @classmethod
  1402. async def _resolve_quote_media(cls, adapter, quote_media_refs: list[tuple[str, str, str]]) -> tuple[list[str], list[str]]:
  1403. """Resolve ybres anchors of the quoted message (from QuoteContextMiddleware)."""
  1404. return await cls._resolve_ybres_refs(adapter, quote_media_refs, log_prefix="quote")
  1405. @staticmethod
  1406. def _collect_quote_local_media(ctx: InboundContext) -> tuple[list[str], list[str]]:
  1407. """DM quote fallback: ``(local_paths, mimes)`` for media PatchAnchorsMiddleware already
  1408. rewrote to ``[image: /path]`` / ``[file: name → /path]`` on the original turn. Unresolved
  1409. anchors were that turn's failure — no re-download here."""
  1410. paths: list[str] = []
  1411. mimes: list[str] = []
  1412. cache = getattr(ctx.adapter, "_msg_content_cache", None)
  1413. text = cache.get(ctx.reply_to_message_id) if ctx.reply_to_message_id and cache else None
  1414. for m in _YB_LOCAL_MEDIA_RE.finditer(text if isinstance(text, str) else ""):
  1415. kind = (m.group(1) or "").strip().lower()
  1416. path = (m.group(2) or "").strip()
  1417. if not path or path in paths or not os.path.exists(path):
  1418. continue
  1419. paths.append(path)
  1420. mimes.append(guess_mime_type(os.path.basename(path)) or ("image/jpeg" if kind == "image" else "application/octet-stream"))
  1421. return paths, mimes
  1422. async def handle(self, ctx: InboundContext, next_fn) -> None:
  1423. # In groups only @bot / owner-command turns reach here (GroupAtGuard short-circuits the
  1424. # rest), so media download and observed-media hydration need no @bot re-check.
  1425. adapter = ctx.adapter
  1426. urls: list[str] = []
  1427. types: list[str] = []
  1428. def _add_unique_pairs(pair_lists: tuple[list[str], list[str]]) -> None:
  1429. for u, m in zip(*pair_lists):
  1430. if u and u not in urls:
  1431. urls.append(u)
  1432. types.append(m)
  1433. own_pairs = await self._resolve_media_urls(adapter, ctx.media_refs) # 1) media carried by this message
  1434. own_count = sum(1 for u in own_pairs[0] if u)
  1435. _add_unique_pairs(own_pairs)
  1436. # 2) Quoted media takes priority; else observed-media backfill in groups only (DM media
  1437. # was already resolved on its own turn).
  1438. if ctx.reply_to_message_id is not None:
  1439. if ctx.quote_media_refs:
  1440. _add_unique_pairs(await self._resolve_quote_media(adapter, ctx.quote_media_refs))
  1441. else: # DM rows carry no platform message_id → recover already-local media from the msg cache.
  1442. _add_unique_pairs(self._collect_quote_local_media(ctx))
  1443. elif ctx.chat_type == "group":
  1444. try:
  1445. _add_unique_pairs(await self._collect_observed_media(adapter, ctx.source))
  1446. except Exception as exc:
  1447. logger.warning("[%s] observed-image hydration raised, continuing anyway: %s", adapter.name, exc)
  1448. ctx.media_urls = urls
  1449. ctx.media_types = types
  1450. # Re-check placeholder using ``own_count``: placeholder text with only quote/observed
  1451. # media (no fresh attachment of its own) is still skippable.
  1452. if PlaceholderFilterMiddleware.is_skippable_placeholder(ctx.raw_text, own_count):
  1453. logger.debug("[%s] Skip placeholder after media download: %r", adapter.name, ctx.raw_text)
  1454. return # Stop pipeline
  1455. await next_fn()
  1456. class PatchAnchorsMiddleware(InboundMiddleware):
  1457. """Replace ``[kind|ybres:RID]`` anchors in raw_text with the local paths MediaResolveMiddleware
  1458. produced, so the transcript records usable paths. Only resolved media (paths starting with
  1459. ``/``) are substituted; other anchors stay untouched."""
  1460. name = "patch-anchors"
  1461. @staticmethod
  1462. def _patch(text: str, urls: list[str], types: list[str]) -> str:
  1463. patched = text
  1464. for u, m in zip(urls, types):
  1465. if not u.startswith("/"):
  1466. continue
  1467. anchor_match = _YB_RES_REF_RE.search(patched)
  1468. if not anchor_match:
  1469. break
  1470. kind, _, filename = anchor_match.group(1).partition(":")
  1471. kind = kind.strip()
  1472. if kind == "image" and m.startswith("image/"):
  1473. replacement = f"[image: {u}]"
  1474. elif kind == "file":
  1475. replacement = f"[file: {filename.strip() or os.path.basename(u)} → {u}]"
  1476. elif kind == "video":
  1477. replacement = f"[video: {u}]"
  1478. else:
  1479. continue
  1480. patched = patched[: anchor_match.start()] + replacement + patched[anchor_match.end():]
  1481. return patched
  1482. async def handle(self, ctx: InboundContext, next_fn) -> None:
  1483. ctx.raw_text = self._patch(ctx.raw_text, ctx.media_urls, ctx.media_types)
  1484. await next_fn()
  1485. class DispatchMiddleware(InboundMiddleware):
  1486. """Build the MessageEvent and dispatch it (groups: serialised per session via a queue)."""
  1487. name = "dispatch"
  1488. async def handle(self, ctx: InboundContext, next_fn) -> None:
  1489. adapter = ctx.adapter
  1490. # The adapter seam: keyed in the owner profile's namespace, same as ``handle_message``.
  1491. _sk = adapter._source_session_key(ctx.source)
  1492. async def _dispatch_inbound_event() -> None:
  1493. if any(mt.startswith(("application/", "text/")) for mt in ctx.media_types):
  1494. # Classification: DOCUMENT wins over PHOTO/VIDEO/AUDIO for mixed attachments — run.py's
  1495. # image handling keys off the per-path image/* mime types regardless of message_type, but
  1496. # document-context injection gates strictly on MessageType.DOCUMENT (same precedence as
  1497. # Email/Signal, PR #44695).
  1498. msg_type = MessageType.DOCUMENT
  1499. else: # yuanbao-local subtypes (CHAT_RECORD) are deep-parsed into text → TEXT downstream
  1500. msg_type = ctx.msg_type if isinstance(ctx.msg_type, MessageType) else MessageType.TEXT
  1501. event = MessageEvent(
  1502. text=ctx.raw_text, message_type=msg_type, source=ctx.source, message_id=ctx.msg_id or None,
  1503. raw_message=ctx.push, media_urls=list(ctx.media_urls), media_types=list(ctx.media_types),
  1504. reply_to_message_id=ctx.reply_to_message_id, reply_to_text=ctx.reply_to_text,
  1505. channel_prompt=ctx.channel_prompt,
  1506. )
  1507. if _sk and ctx.msg_id:
  1508. adapter._processing_msg_ids[_sk] = ctx.msg_id
  1509. adapter._processing_msg_texts[_sk] = ctx.raw_text or ""
  1510. if ctx.msg_id and ctx.raw_text:
  1511. cache = adapter._msg_content_cache
  1512. cache[ctx.msg_id] = ctx.raw_text
  1513. for k in list(cache)[:max(0, len(cache) - 200)]: # bounded: drop oldest
  1514. del cache[k]
  1515. await adapter.handle_message(event)
  1516. if ctx.chat_type == "group":
  1517. is_new = _sk not in adapter._group_queues
  1518. queue = adapter._group_queues.setdefault(_sk, asyncio.Queue())
  1519. queue.put_nowait(_dispatch_inbound_event)
  1520. logger.info("[%s] Group message enqueued (qsize=%d) for %s", adapter.name, queue.qsize(), (_sk or "")[:50])
  1521. if is_new:
  1522. self._track_inbound(adapter, self._consume_group_queue(adapter, _sk), f"yuanbao-group-consumer-{(_sk or '')[:30]}")
  1523. else:
  1524. self._track_inbound(adapter, _dispatch_inbound_event(), f"yuanbao-inbound-{ctx.msg_id or 'unknown'}")
  1525. await next_fn()
  1526. @staticmethod
  1527. def _track_inbound(adapter, coro, name: str) -> None:
  1528. task = asyncio.create_task(coro, name=name)
  1529. adapter._inbound_tasks.add(task)
  1530. task.add_done_callback(adapter._inbound_tasks.discard)
  1531. @staticmethod
  1532. async def _consume_group_queue(adapter: YuanbaoAdapter, session_key: str) -> None:
  1533. """Drain the group queue one dispatch at a time, waiting for each to finish; exits after 2s idle."""
  1534. queue = adapter._group_queues.get(session_key)
  1535. if not queue:
  1536. return
  1537. try:
  1538. while True:
  1539. try:
  1540. dispatch_fn = await asyncio.wait_for(queue.get(), timeout=2.0)
  1541. except TimeoutError:
  1542. break
  1543. logger.debug("[%s] Group queue: dispatching for %s (remaining=%d)", adapter.name, (session_key or "")[:50], queue.qsize())
  1544. try:
  1545. await dispatch_fn()
  1546. while session_key in adapter._active_sessions:
  1547. await asyncio.sleep(0.1)
  1548. except Exception:
  1549. logger.exception("[%s] Group queue consumer error", adapter.name)
  1550. finally:
  1551. adapter._group_queues.pop(session_key, None)
  1552. class InboundPipelineBuilder:
  1553. """Assembles the default Yuanbao inbound pipeline (order matters)."""
  1554. _DEFAULT_MIDDLEWARES: list[type] = [
  1555. DecodeMiddleware, ExtractFieldsMiddleware, RecallGuardMiddleware, DedupMiddleware, SkipSelfMiddleware,
  1556. ChatRoutingMiddleware, AccessGuardMiddleware, ExtractContentMiddleware, PlaceholderFilterMiddleware,
  1557. OwnerCommandMiddleware, BuildSourceMiddleware, GroupAtGuardMiddleware, AutoSetHomeMiddleware,
  1558. GroupAttributionMiddleware, ClassifyMessageTypeMiddleware, QuoteContextMiddleware,
  1559. ForwardedRecordsParseMiddleware, MediaResolveMiddleware, PatchAnchorsMiddleware, DispatchMiddleware,
  1560. ]
  1561. @classmethod
  1562. def build(cls) -> InboundPipeline:
  1563. pipeline = InboundPipeline()
  1564. for mw_cls in cls._DEFAULT_MIDDLEWARES:
  1565. pipeline.use(mw_cls())
  1566. return pipeline
  1567. class ConnectionManager:
  1568. """WebSocket lifecycle: open/close, AUTH_BIND, ping/pong heartbeat, receive loop, backoff reconnect."""
  1569. _DEBOUNCE_WINDOW: float = 1.5 # seconds to wait for companion frames of a multi-part message
  1570. _LOOPS = (("_heartbeat_task", "_heartbeat_loop", "heartbeat"), ("_recv_task", "_receive_loop", "recv"))
  1571. def __init__(self, adapter: YuanbaoAdapter) -> None:
  1572. self._adapter = adapter
  1573. self._ws = None # websockets connection
  1574. self._connect_id: Optional[str] = None
  1575. self._heartbeat_task: Optional[asyncio.Task] = None
  1576. self._recv_task: Optional[asyncio.Task] = None
  1577. self._pending_acks: dict[str, asyncio.Future] = {}
  1578. self._pending_pong: Optional[asyncio.Future] = None
  1579. self._consecutive_hb_timeouts = self._reconnect_attempts = 0
  1580. self._reconnecting: bool = False
  1581. # Debounce buffer aggregating multi-part inbound messages: sender key -> frames / timer
  1582. self._inbound_buffer: dict[str, list] = {}
  1583. self._inbound_timers: dict[str, asyncio.TimerHandle] = {}
  1584. @property
  1585. def ws(self):
  1586. return self._ws
  1587. @property
  1588. def is_connected(self) -> bool:
  1589. """``ws.open`` may be a bool (websockets <14) or a method (>=14)."""
  1590. if self._ws is None:
  1591. return False
  1592. open_attr = getattr(self._ws, "open", None)
  1593. try:
  1594. return open_attr is True or (callable(open_attr) and bool(open_attr()))
  1595. except Exception:
  1596. return False
  1597. async def open(self) -> bool:
  1598. """sign-token → WS connect → AUTH_BIND → start loops. Returns True on success."""
  1599. adapter = self._adapter
  1600. if not WEBSOCKETS_AVAILABLE:
  1601. msg = "Yuanbao startup failed: 'websockets' package not installed"
  1602. adapter._set_fatal_error("yuanbao_missing_dependency", msg, retryable=True)
  1603. logger.warning("[%s] %s. Run: hermes pm repair", adapter.name, msg)
  1604. return False
  1605. if not adapter._app_key or not adapter._app_secret:
  1606. msg = "Yuanbao startup failed: YUANBAO_APP_ID and YUANBAO_APP_SECRET are required"
  1607. adapter._set_fatal_error("yuanbao_missing_credentials", msg, retryable=False)
  1608. logger.error("[%s] %s", adapter.name, msg)
  1609. return False
  1610. if self.is_connected:
  1611. logger.debug("[%s] Already connected, skipping connect()", adapter.name)
  1612. return True
  1613. if not adapter._acquire_platform_lock('yuanbao-app-key', adapter._app_key, 'Yuanbao app key'):
  1614. return False
  1615. try:
  1616. logger.info("[%s] Fetching sign token from %s", adapter.name, adapter._api_domain)
  1617. token_data = await adapter._get_cached_token()
  1618. logger.info("[%s] Connecting to %s", adapter.name, adapter._ws_url)
  1619. if not await self._dial(token_data):
  1620. return False
  1621. adapter._loop = asyncio.get_running_loop()
  1622. self._connected(cancel_existing=False)
  1623. logger.info("[%s] Connected. connectId=%s botId=%s", adapter.name, self._connect_id, adapter._bot_id)
  1624. return True
  1625. except TimeoutError:
  1626. logger.error("[%s] Connection timed out", adapter.name)
  1627. except Exception as exc:
  1628. logger.error("[%s] connect() failed: %s", adapter.name, exc, exc_info=True)
  1629. await self._cleanup_ws()
  1630. adapter._release_platform_lock()
  1631. return False
  1632. async def _dial(self, token_data: dict) -> bool:
  1633. """Adopt the sign-token bot_id, open the WS (built-in ping/pong disabled) and run AUTH_BIND;
  1634. cleans up on auth failure."""
  1635. if token_data.get("bot_id"):
  1636. self._adapter._bot_id = str(token_data["bot_id"])
  1637. self._ws = await asyncio.wait_for(
  1638. websockets.connect( # type: ignore[attr-defined]
  1639. self._adapter._ws_url, ping_interval=None, ping_timeout=None, close_timeout=5,
  1640. happy_eyeballs_delay=0.25, # race IPv6/IPv4 in loop.create_connection (#114265)
  1641. ),
  1642. timeout=CONNECT_TIMEOUT_SECONDS,
  1643. )
  1644. if not await self._authenticate(token_data):
  1645. await self._cleanup_ws()
  1646. return False
  1647. return True
  1648. def _connected(self, *, cancel_existing: bool) -> None:
  1649. """Post-AUTH bookkeeping shared by open() and reconnect: mark connected, (re)start loops,
  1650. register as the active adapter."""
  1651. self._reconnect_attempts = 0
  1652. self._adapter._mark_connected()
  1653. self._start_loops(cancel_existing=cancel_existing)
  1654. YuanbaoAdapter.set_active(self._adapter)
  1655. def _start_loops(self, *, cancel_existing: bool) -> None:
  1656. """(Re)start the heartbeat and receive loops for the current connect_id."""
  1657. for attr, coro_name, tag in self._LOOPS:
  1658. old = getattr(self, attr)
  1659. if cancel_existing and old and not old.done():
  1660. old.cancel()
  1661. setattr(self, attr, asyncio.create_task(getattr(self, coro_name)(), name=f"yuanbao-{tag}-{self._connect_id}"))
  1662. async def close(self) -> None:
  1663. """Cancel background tasks, fail pending futures, and close the WebSocket."""
  1664. for attr, _coro_name, _tag in self._LOOPS:
  1665. task = getattr(self, attr)
  1666. if task:
  1667. await cancel_task(task)
  1668. setattr(self, attr, None)
  1669. disc_exc = RuntimeError("YuanbaoAdapter disconnected")
  1670. for fut in self._pending_acks.values():
  1671. if not fut.done():
  1672. fut.set_exception(disc_exc)
  1673. self._pending_acks.clear()
  1674. SignManager.clear_locks() # avoid stale locks bound to a previous event loop
  1675. await self._cleanup_ws()
  1676. async def _authenticate(self, token_data: dict) -> bool:
  1677. """Send AUTH_BIND and read frames until BIND_ACK; False on failure/timeout."""
  1678. adapter = self._adapter
  1679. if self._ws is None:
  1680. return False
  1681. uid = adapter._bot_id or token_data.get("bot_id", "")
  1682. msg_id, version = str(uuid.uuid4()), get_version_info().base_version
  1683. await self._ws.send(encode_auth_bind(
  1684. biz_id="ybBot", uid=uid, source=token_data.get("source") or "bot", token=token_data.get("token", ""),
  1685. msg_id=msg_id, app_version=version, operation_system=_OPERATION_SYSTEM, bot_version=version,
  1686. route_env=adapter._route_env or token_data.get("route_env", "") or "",
  1687. ))
  1688. logger.debug("[%s] AUTH_BIND sent (msg_id=%s uid=%s)", adapter.name, msg_id, uid)
  1689. try:
  1690. deadline = asyncio.get_running_loop().time() + AUTH_TIMEOUT_SECONDS
  1691. while True:
  1692. remaining = deadline - asyncio.get_running_loop().time()
  1693. if remaining <= 0:
  1694. logger.error("[%s] AUTH_BIND timeout waiting for BIND_ACK", adapter.name)
  1695. return False
  1696. raw = await asyncio.wait_for(self._ws.recv(), timeout=remaining)
  1697. if not isinstance(raw, (bytes, bytearray)):
  1698. continue
  1699. try:
  1700. msg = decode_conn_msg(bytes(raw))
  1701. except Exception:
  1702. continue
  1703. head = msg.get("head", {})
  1704. if head.get("cmd_type", -1) != CMD_TYPE["Response"] or head.get("cmd", "") != "auth-bind":
  1705. continue
  1706. self._connect_id = self._extract_connect_id(msg)
  1707. if not self._connect_id:
  1708. logger.error("[%s] BIND_ACK missing connectId", adapter.name)
  1709. return False
  1710. logger.info("[%s] BIND_ACK received: connectId=%s", adapter.name, self._connect_id)
  1711. return True
  1712. except TimeoutError:
  1713. logger.error("[%s] AUTH_BIND timeout", adapter.name)
  1714. except Exception as exc:
  1715. logger.error("[%s] AUTH_BIND error: %s", adapter.name, exc, exc_info=True)
  1716. return False
  1717. def _pop_pending(self, msg_id: str) -> Optional[asyncio.Future]:
  1718. """Pop the not-yet-done future registered for *msg_id*, if any."""
  1719. fut = self._pending_acks.pop(msg_id, None) if msg_id else None
  1720. return fut if fut is not None and not fut.done() else None
  1721. def _extract_connect_id(self, decoded_msg: dict) -> Optional[str]:
  1722. """connectId from a decoded BIND_ACK, or None (logs AuthBindRsp errors)."""
  1723. data: bytes = decoded_msg.get("data", b"")
  1724. if not data:
  1725. return None
  1726. try:
  1727. fdict = _fields_to_dict(_parse_fields(data))
  1728. code = _get_varint(fdict, 1)
  1729. if code != 0:
  1730. logger.error("[%s] AuthBindRsp error: code=%d message=%r", self._adapter.name, code, _get_string(fdict, 2))
  1731. return None
  1732. return _get_string(fdict, 3) or None
  1733. except Exception as exc:
  1734. logger.warning("[%s] Failed to extract connectId: %s", self._adapter.name, exc)
  1735. return None
  1736. async def _heartbeat_loop(self) -> None:
  1737. """Send PING every HEARTBEAT_INTERVAL_SECONDS; reconnect after HEARTBEAT_TIMEOUT_THRESHOLD misses."""
  1738. adapter = self._adapter
  1739. try:
  1740. while adapter._running:
  1741. await asyncio.sleep(HEARTBEAT_INTERVAL_SECONDS)
  1742. if self._ws is None:
  1743. continue
  1744. try:
  1745. msg_id = str(uuid.uuid4())
  1746. self._pending_pong = pong_future = asyncio.get_running_loop().create_future()
  1747. self._pending_acks[msg_id] = pong_future
  1748. await self._ws.send(encode_ping(msg_id))
  1749. logger.debug("[%s] PING sent (msg_id=%s)", adapter.name, msg_id)
  1750. try:
  1751. await asyncio.wait_for(pong_future, timeout=10.0)
  1752. self._consecutive_hb_timeouts = 0
  1753. except TimeoutError:
  1754. self._pending_acks.pop(msg_id, None)
  1755. self._consecutive_hb_timeouts += 1
  1756. logger.warning("[%s] PONG timeout (%d/%d)", adapter.name, self._consecutive_hb_timeouts, HEARTBEAT_TIMEOUT_THRESHOLD)
  1757. if self._consecutive_hb_timeouts >= HEARTBEAT_TIMEOUT_THRESHOLD:
  1758. logger.warning("[%s] Heartbeat threshold exceeded, triggering reconnect", adapter.name)
  1759. self.schedule_reconnect()
  1760. return
  1761. finally:
  1762. self._pending_acks.pop(msg_id, None)
  1763. self._pending_pong = None
  1764. except Exception as exc:
  1765. logger.debug("[%s] Heartbeat send failed: %s", adapter.name, exc)
  1766. except asyncio.CancelledError:
  1767. pass
  1768. async def _receive_loop(self) -> None:
  1769. """Read WS frames and dispatch by cmd_type; schedule reconnect unless the close code is permanent."""
  1770. adapter = self._adapter
  1771. try:
  1772. async for raw in self._ws: # type: ignore[union-attr]
  1773. if isinstance(raw, (bytes, bytearray)):
  1774. await self._handle_frame(bytes(raw))
  1775. except asyncio.CancelledError:
  1776. pass
  1777. except websockets.exceptions.ConnectionClosed as close_exc: # type: ignore[union-attr]
  1778. close_code = getattr(close_exc, 'code', None)
  1779. logger.warning("[%s] WebSocket connection closed: code=%s reason=%s", adapter.name, close_code, getattr(close_exc, 'reason', ''))
  1780. if close_code and close_code in NO_RECONNECT_CLOSE_CODES:
  1781. logger.error("[%s] Close code %d is non-recoverable, NOT reconnecting", adapter.name, close_code)
  1782. adapter._mark_disconnected()
  1783. else:
  1784. self.schedule_reconnect()
  1785. except Exception as exc:
  1786. logger.warning("[%s] receive_loop exited: %s", adapter.name, exc)
  1787. self.schedule_reconnect()
  1788. async def _handle_frame(self, raw: bytes) -> None:
  1789. adapter = self._adapter
  1790. try:
  1791. msg = decode_conn_msg(raw)
  1792. except Exception as exc:
  1793. logger.debug("[%s] Failed to decode frame: %s", adapter.name, exc)
  1794. return
  1795. head = msg.get("head", {})
  1796. cmd_type = head.get("cmd_type", -1)
  1797. cmd = head.get("cmd", "")
  1798. msg_id = head.get("msg_id", "")
  1799. data: bytes = msg.get("data", b"")
  1800. if cmd_type == CMD_TYPE["Response"]:
  1801. if cmd == "ping": # HEARTBEAT_ACK
  1802. logger.debug("[%s] HEARTBEAT_ACK received (msg_id=%s)", adapter.name, msg_id)
  1803. pong = self._pending_pong if self._pending_pong is not None and not self._pending_pong.done() else self._pop_pending(msg_id)
  1804. if pong is not None:
  1805. pong.set_result(True)
  1806. elif cmd in {"send_group_heartbeat", "send_private_heartbeat"}:
  1807. # Fire-and-forget heartbeat ACKs: nobody awaits them; discard to avoid "Unmatched" noise.
  1808. logger.debug("[%s] Heartbeat ACK received: cmd=%s msg_id=%s", adapter.name, cmd, msg_id)
  1809. elif msg_id and msg_id in self._pending_acks: # response to an outbound RPC
  1810. fut = self._pop_pending(msg_id)
  1811. if fut is not None:
  1812. result = {"head": head}
  1813. if data:
  1814. result["data"] = data
  1815. fut.set_result(result)
  1816. else:
  1817. logger.debug("[%s] Unmatched Response: cmd=%s msg_id=%s", adapter.name, cmd, msg_id)
  1818. return
  1819. if cmd_type == CMD_TYPE["Push"]:
  1820. logger.info("[%s] Push received: cmd=%s msg_id=%s data_len=%d", adapter.name, cmd, msg_id, len(data))
  1821. if head.get("need_ack", False) and self._ws is not None:
  1822. try:
  1823. await self._ws.send(encode_push_ack(head))
  1824. except Exception as ack_exc:
  1825. logger.debug("[%s] Failed to send PushAck: %s", adapter.name, ack_exc)
  1826. if msg_id and msg_id in self._pending_acks:
  1827. fut = self._pop_pending(msg_id)
  1828. if fut is not None:
  1829. try:
  1830. fut.set_result(decode_inbound_push(data) if data else {"head": head})
  1831. except Exception as exc:
  1832. fut.set_exception(exc)
  1833. return
  1834. if data: # genuine inbound message — dispatch to AI
  1835. logger.info("[%s] WS received inbound push, decoding and dispatching: cmd=%s, data_len=%d", adapter.name, cmd, len(data))
  1836. self._push_to_inbound(data)
  1837. return
  1838. logger.debug("[%s] Ignoring frame: cmd_type=%d cmd=%s msg_id=%s", adapter.name, cmd_type, cmd, msg_id)
  1839. def _extract_sender_key(self, raw_data: bytes) -> str:
  1840. """Debounce key 'from_account:group_code' (JSON or protobuf), else a unique fallback."""
  1841. with contextlib.suppress(Exception):
  1842. parsed = json.loads(raw_data.decode("utf-8"))
  1843. if isinstance(parsed, dict):
  1844. from_account, group_code = DecodeMiddleware.json_sender_fields(parsed)
  1845. if from_account:
  1846. return f"{from_account}:{group_code}"
  1847. with contextlib.suppress(Exception):
  1848. push = decode_inbound_push(raw_data)
  1849. if push:
  1850. return f"{push.get('from_account', '')}:{push.get('group_code', '')}"
  1851. return f"__unknown_{id(raw_data)}"
  1852. def _push_to_inbound(self, raw_data: bytes) -> None:
  1853. """Debounced dispatch: frames from one sender within _DEBOUNCE_WINDOW run as ONE pipeline
  1854. execution, merging multi-part messages (e.g. image + text pushed separately)."""
  1855. key = self._extract_sender_key(raw_data)
  1856. existing_timer = self._inbound_timers.pop(key, None)
  1857. if existing_timer:
  1858. existing_timer.cancel()
  1859. self._inbound_buffer.setdefault(key, []).append(raw_data)
  1860. logger.debug("[%s] Debounce: buffered frame for key=%s, count=%d", self._adapter.name, key, len(self._inbound_buffer[key]))
  1861. self._inbound_timers[key] = asyncio.get_running_loop().call_later(self._DEBOUNCE_WINDOW, self._flush_inbound_buffer, key)
  1862. def _flush_inbound_buffer(self, key: str) -> None:
  1863. """Run the pipeline over the buffered frames for *key*."""
  1864. self._inbound_timers.pop(key, None)
  1865. data_list = self._inbound_buffer.pop(key, [])
  1866. if not data_list:
  1867. return
  1868. adapter = self._adapter
  1869. logger.info("[%s] Debounce flush: key=%s, aggregated %d frames", adapter.name, key, len(data_list))
  1870. adapter._track_task(asyncio.create_task(
  1871. adapter._inbound_pipeline.execute(InboundContext(adapter=adapter, raw_frames=data_list)), name=f"yuanbao-pipeline-{key}"))
  1872. async def send_biz_request(self, encoded_conn_msg: bytes, req_id: str, timeout: float = DEFAULT_SEND_TIMEOUT) -> dict:
  1873. """Send a business request and await its response future (pending_acks[req_id]), cleaning up on exit."""
  1874. if self._ws is None:
  1875. raise RuntimeError("Not connected")
  1876. future: asyncio.Future = asyncio.get_running_loop().create_future()
  1877. self._pending_acks[req_id] = future
  1878. try:
  1879. await self._ws.send(encoded_conn_msg)
  1880. return await asyncio.wait_for(asyncio.shield(future), timeout=timeout)
  1881. finally:
  1882. self._pending_acks.pop(req_id, None)
  1883. def schedule_reconnect(self) -> None:
  1884. """Schedule a reconnect only if running and not already reconnecting."""
  1885. if self._adapter._running and not self._reconnecting:
  1886. asyncio.create_task(self._reconnect_with_backoff())
  1887. async def _reconnect_with_backoff(self) -> bool:
  1888. if self._reconnecting:
  1889. logger.debug("[%s] Reconnect already in progress, skipping", self._adapter.name)
  1890. return False
  1891. self._reconnecting = True
  1892. try:
  1893. return await self._do_reconnect()
  1894. finally:
  1895. self._reconnecting = False
  1896. async def _do_reconnect(self) -> bool:
  1897. """Reconnect loop (under the _reconnecting guard) with exponential backoff 1s, 2s, 4s, … capped at 60s."""
  1898. adapter = self._adapter
  1899. for attempt in range(MAX_RECONNECT_ATTEMPTS):
  1900. self._reconnect_attempts = attempt + 1
  1901. wait = min(2 ** attempt, 60)
  1902. logger.info("[%s] Reconnect attempt %d/%d in %ds", adapter.name, attempt + 1, MAX_RECONNECT_ATTEMPTS, wait)
  1903. await asyncio.sleep(wait)
  1904. await self._cleanup_ws()
  1905. try:
  1906. token_data = await SignManager.force_refresh(
  1907. adapter._app_key, adapter._app_secret, adapter._api_domain, route_env=adapter._route_env,
  1908. )
  1909. if not await self._dial(token_data):
  1910. logger.warning("[%s] Re-auth failed on attempt %d", adapter.name, attempt + 1)
  1911. continue
  1912. self._consecutive_hb_timeouts = 0
  1913. self._connected(cancel_existing=True)
  1914. logger.info("[%s] Reconnected on attempt %d. connectId=%s", adapter.name, attempt + 1, self._connect_id)
  1915. return True
  1916. except TimeoutError:
  1917. logger.warning("[%s] Reconnect attempt %d timed out", adapter.name, attempt + 1)
  1918. except Exception as exc:
  1919. logger.warning("[%s] Reconnect attempt %d failed: %s", adapter.name, attempt + 1, exc)
  1920. logger.error("[%s] Giving up after %d reconnect attempts", adapter.name, MAX_RECONNECT_ATTEMPTS)
  1921. adapter._mark_disconnected()
  1922. return False
  1923. async def _cleanup_ws(self) -> None:
  1924. """Close and clear the WS, bounded by WS_CLOSE_TIMEOUT_S so an unresponsive server can't stall teardown."""
  1925. ws = self._ws
  1926. self._ws = None
  1927. if ws is not None:
  1928. try:
  1929. await asyncio.wait_for(ws.close(), timeout=WS_CLOSE_TIMEOUT_S)
  1930. except TimeoutError:
  1931. # No close-frame echo in time; websockets force-closes the transport on cancel.
  1932. logger.debug("[%s] WS close handshake exceeded %.1fs — dropping connection", self._adapter.name, WS_CLOSE_TIMEOUT_S)
  1933. except Exception:
  1934. pass
  1935. def _read_local_file(adapter, label: str, path: str, default_name: str, default_mime: str,
  1936. filename: Optional[str] = None) -> tuple[bytes, str, str]:
  1937. """(bytes, filename, content_type) for a local file; ValueError when missing."""
  1938. if not os.path.isfile(path):
  1939. raise ValueError(f"File not found: {path}")
  1940. logger.info("[%s] %s: reading %s", adapter.name, label, path)
  1941. with open(path, "rb") as f:
  1942. file_bytes = f.read()
  1943. filename = filename or os.path.basename(path) or default_name
  1944. return file_bytes, filename, guess_mime_type(filename) or default_mime
  1945. class MediaSendHandler(ABC):
  1946. """Media send strategy: subclasses provide acquire_file() and build_msg_body(); handle() runs
  1947. the shared flow (check ws → cancel notifier → validate → COS upload → lock → dispatch)."""
  1948. def needs_cos_upload(self) -> bool:
  1949. """Override to return False for non-COS media (sticker)."""
  1950. return True
  1951. @abstractmethod
  1952. async def acquire_file(self, adapter: YuanbaoAdapter, **kwargs: Any) -> tuple[bytes, str, str]:
  1953. """(file_bytes, filename, content_type); raise ValueError when unobtainable."""
  1954. @abstractmethod
  1955. def build_msg_body(self, upload_result: dict, **kwargs: Any) -> list:
  1956. """Platform-specific MsgBody list from the COS upload result."""
  1957. async def handle(self, adapter: YuanbaoAdapter, chat_id: str, reply_to: Optional[str] = None,
  1958. caption: Optional[str] = None, **kwargs: Any) -> SendResult:
  1959. if adapter._connection.ws is None:
  1960. return SendResult(success=False, error="Not connected", retryable=True)
  1961. adapter._outbound.slow_notifier.cancel(chat_id)
  1962. try:
  1963. file_bytes, filename, content_type = await self.acquire_file(adapter, **kwargs)
  1964. if self.needs_cos_upload():
  1965. # Stickers (TIMFaceElem) carry no bytes — validating them would yield "Empty file".
  1966. validation_err = MessageSender.validate_media(file_bytes, filename, adapter.MEDIA_MAX_SIZE_MB)
  1967. if validation_err:
  1968. return SendResult(success=False, error=validation_err)
  1969. token_data = await adapter._get_cached_token()
  1970. credentials = await get_cos_credentials(
  1971. app_key=adapter._app_key, api_domain=adapter._api_domain, token=token_data.get("token", ""),
  1972. filename=filename, bot_id=token_data.get("bot_id", "") or adapter._bot_id or "", route_env=adapter._route_env,
  1973. )
  1974. upload_result = await upload_to_cos(
  1975. file_bytes=file_bytes, filename=filename, content_type=content_type, credentials=credentials,
  1976. bucket=credentials["bucketName"], region=credentials["region"],
  1977. )
  1978. # Explicit keys win over caller kwargs (avoids "multiple values" TypeError).
  1979. msg_body = self.build_msg_body(upload_result, **{
  1980. **kwargs, "file_uuid": md5_hex(file_bytes), "filename": filename, "content_type": content_type})
  1981. else:
  1982. msg_body = self.build_msg_body({}, **kwargs)
  1983. if caption:
  1984. msg_body.append(_text_elem(caption))
  1985. return await adapter._outbound.sender.dispatch_msg_body(chat_id, msg_body, reply_to, group_code=kwargs.get("group_code", ""))
  1986. except ValueError as ve:
  1987. return SendResult(success=False, error=str(ve))
  1988. except Exception as exc:
  1989. logger.error("[%s] %s.handle() failed: %s", adapter.name, type(self).__name__, exc, exc_info=True)
  1990. return SendResult(success=False, error=str(exc) or type(exc).__name__)
  1991. class _ImageHandler(MediaSendHandler):
  1992. """Shared TIMImageElem body builder for image handlers."""
  1993. def build_msg_body(self, upload_result, **kwargs):
  1994. return build_image_msg_body(
  1995. url=upload_result["url"], uuid=kwargs["file_uuid"], filename=kwargs["filename"], size=upload_result["size"],
  1996. width=upload_result.get("width", 0), height=upload_result.get("height", 0), mime_type=kwargs["content_type"],
  1997. )
  1998. class ImageUrlHandler(_ImageHandler):
  1999. """Image from a URL (download → COS → TIMImageElem)."""
  2000. async def acquire_file(self, adapter, **kwargs):
  2001. image_url: str = kwargs["image_url"]
  2002. logger.info("[%s] ImageUrlHandler: downloading %s", adapter.name, image_url)
  2003. file_bytes, content_type = await media_download_url(image_url, max_size_mb=adapter.MEDIA_MAX_SIZE_MB)
  2004. path_part = image_url.split("?")[0]
  2005. if not content_type or content_type == "application/octet-stream":
  2006. content_type = guess_mime_type(path_part) or "image/jpeg"
  2007. return file_bytes, os.path.basename(path_part) or "image.jpg", content_type
  2008. class ImageFileHandler(_ImageHandler):
  2009. """Image from a local path (read → COS → TIMImageElem)."""
  2010. async def acquire_file(self, adapter, **kwargs):
  2011. return _read_local_file(adapter, "ImageFileHandler", kwargs["image_path"], "image.jpg", "image/jpeg")
  2012. class DocumentHandler(MediaSendHandler):
  2013. """Local file/document (read → COS → TIMFileElem)."""
  2014. async def acquire_file(self, adapter, **kwargs):
  2015. return _read_local_file(adapter, "DocumentHandler", kwargs["file_path"], "document", "application/octet-stream", kwargs.get("filename"))
  2016. def build_msg_body(self, upload_result, **kwargs):
  2017. return build_file_msg_body(url=upload_result["url"], filename=kwargs["filename"], uuid=kwargs["file_uuid"], size=upload_result["size"])
  2018. class StickerHandler(MediaSendHandler):
  2019. """Sticker/emoji (TIMFaceElem, no COS upload)."""
  2020. def needs_cos_upload(self) -> bool:
  2021. return False
  2022. async def acquire_file(self, adapter, **kwargs):
  2023. return b"", "sticker", "application/octet-stream" # no file bytes needed
  2024. def build_msg_body(self, upload_result, **kwargs):
  2025. from plugins.platforms.yuanbao.yuanbao_sticker import (
  2026. get_sticker_by_name, get_random_sticker, build_face_msg_body, build_sticker_msg_body,
  2027. )
  2028. sticker_name = kwargs.get("sticker_name")
  2029. if sticker_name is not None:
  2030. sticker = get_sticker_by_name(sticker_name)
  2031. if sticker is None:
  2032. raise ValueError(f"Sticker not found: {sticker_name!r}")
  2033. return build_sticker_msg_body(sticker)
  2034. if kwargs.get("face_index") is not None:
  2035. return build_face_msg_body(face_index=kwargs["face_index"])
  2036. return build_sticker_msg_body(get_random_sticker())
  2037. class HeartbeatManager:
  2038. """Reply heartbeat lifecycle: RUNNING every 2s, auto-FINISH after 30s idle, explicit stop."""
  2039. def __init__(self, adapter: YuanbaoAdapter) -> None:
  2040. self._adapter = adapter
  2041. self._reply_heartbeat_tasks: dict[str, asyncio.Task] = {}
  2042. self._reply_hb_last_active: dict[str, float] = {}
  2043. def _ready(self) -> bool:
  2044. return self._adapter._connection.ws is not None and bool(self._adapter._bot_id)
  2045. async def send_heartbeat_once(self, chat_id: str, heartbeat_val: int) -> None:
  2046. """Send a single heartbeat (RUNNING or FINISH), best effort."""
  2047. adapter = self._adapter
  2048. if not self._ready():
  2049. return
  2050. try:
  2051. if chat_id.startswith("group:"):
  2052. encoded = encode_send_group_heartbeat(from_account=adapter._bot_id, group_code=chat_id[len("group:"):], heartbeat=heartbeat_val)
  2053. else:
  2054. encoded = encode_send_private_heartbeat(from_account=adapter._bot_id, to_account=chat_id.removeprefix("direct:"), heartbeat=heartbeat_val)
  2055. await adapter._connection.ws.send(encoded)
  2056. logger.debug("[%s] Reply heartbeat %s sent: chat=%s", adapter.name,
  2057. "RUNNING" if heartbeat_val == WS_HEARTBEAT_RUNNING else "FINISH", chat_id)
  2058. except Exception as exc:
  2059. logger.debug("[%s] send_heartbeat_once failed: %s", adapter.name, exc)
  2060. async def start(self, chat_id: str) -> None:
  2061. """Start or renew the periodic RUNNING sender."""
  2062. if not self._ready():
  2063. return
  2064. self._reply_hb_last_active[chat_id] = time.time()
  2065. existing = self._reply_heartbeat_tasks.get(chat_id)
  2066. if not existing or existing.done():
  2067. self._reply_heartbeat_tasks[chat_id] = asyncio.create_task(self._worker(chat_id), name=f"yuanbao-reply-hb-{chat_id}")
  2068. async def _worker(self, chat_id: str) -> None:
  2069. """Send RUNNING every 2s; after 30s without renewal (or WS loss) send FINISH and exit.
  2070. A cancelled worker sends no FINISH — stop() decides that."""
  2071. cancelled = False
  2072. try:
  2073. await self.send_heartbeat_once(chat_id, WS_HEARTBEAT_RUNNING)
  2074. while True:
  2075. await asyncio.sleep(REPLY_HEARTBEAT_INTERVAL_S)
  2076. if (time.time() - self._reply_hb_last_active.get(chat_id, 0) > REPLY_HEARTBEAT_TIMEOUT_S
  2077. or self._adapter._connection.ws is None):
  2078. break
  2079. await self.send_heartbeat_once(chat_id, WS_HEARTBEAT_RUNNING)
  2080. except asyncio.CancelledError:
  2081. cancelled = True
  2082. except Exception:
  2083. pass
  2084. finally:
  2085. if not cancelled:
  2086. await self.send_heartbeat_once(chat_id, WS_HEARTBEAT_FINISH)
  2087. self._reply_heartbeat_tasks.pop(chat_id, None)
  2088. self._reply_hb_last_active.pop(chat_id, None)
  2089. async def stop(self, chat_id: str, send_finish: bool = True) -> None:
  2090. """Stop the RUNNING sender and optionally send FINISH."""
  2091. task = self._reply_heartbeat_tasks.pop(chat_id, None)
  2092. if task and not task.done():
  2093. await cancel_task(task)
  2094. if send_finish:
  2095. await self.send_heartbeat_once(chat_id, WS_HEARTBEAT_FINISH)
  2096. async def close(self) -> None:
  2097. _cancel_all(self._reply_heartbeat_tasks)
  2098. self._reply_hb_last_active.clear()
  2099. class SlowResponseNotifier:
  2100. """Per-chat timer that sends a courtesy 'please wait' after SLOW_RESPONSE_TIMEOUT_S without a reply."""
  2101. def __init__(self, adapter: YuanbaoAdapter, sender: MessageSender) -> None:
  2102. self._adapter = adapter
  2103. self._sender = sender
  2104. self._tasks: dict[str, asyncio.Task] = {}
  2105. async def start(self, chat_id: str) -> None:
  2106. self.cancel(chat_id)
  2107. self._tasks[chat_id] = asyncio.create_task(self._notifier(chat_id), name=f"yuanbao-slow-resp-{chat_id}")
  2108. async def _notifier(self, chat_id: str) -> None:
  2109. try:
  2110. await asyncio.sleep(SLOW_RESPONSE_TIMEOUT_S)
  2111. logger.info("[%s] Agent response exceeded %ds for %s, sending wait notice", self._adapter.name, int(SLOW_RESPONSE_TIMEOUT_S), chat_id)
  2112. await self._sender.send_text_chunk(chat_id, slow_response_message())
  2113. except asyncio.CancelledError:
  2114. pass
  2115. except Exception as exc:
  2116. logger.debug("[%s] Slow-response notifier failed: %s", self._adapter.name, exc)
  2117. def cancel(self, chat_id: str) -> None:
  2118. task = self._tasks.pop(chat_id, None)
  2119. if task and not task.done():
  2120. task.cancel()
  2121. async def close(self) -> None:
  2122. _cancel_all(self._tasks)
  2123. class MessageSender:
  2124. """Outbound dispatcher: per-chat locks (serial ordering), chunked text with retry, C2C/group
  2125. encoding, media handler strategies, and send_direct for the send_message tool."""
  2126. IMAGE_EXTS: ClassVar[frozenset] = frozenset({".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"})
  2127. CHAT_DICT_MAX_SIZE: ClassVar[int] = 1000 # Max distinct chat IDs in _chat_locks
  2128. # @nickname bounded by whitespace / line edges
  2129. _AT_USER_RE = re.compile(r'(?:(?<=\s)|(?<=^))@(\S+?)(?=\s|$)', re.MULTILINE)
  2130. def __init__(self, adapter: YuanbaoAdapter) -> None:
  2131. self._adapter = adapter
  2132. self._chat_locks: collections.OrderedDict[str, asyncio.Lock] = collections.OrderedDict()
  2133. self._media_handlers: dict[str, MediaSendHandler] = {
  2134. "image_url": ImageUrlHandler(), "image_file": ImageFileHandler(), "document": DocumentHandler(), "sticker": StickerHandler(),
  2135. }
  2136. def get_chat_lock(self, chat_id: str) -> asyncio.Lock:
  2137. """Per-chat-id lock with LRU eviction (prefers evicting an unlocked entry)."""
  2138. if chat_id in self._chat_locks:
  2139. self._chat_locks.move_to_end(chat_id)
  2140. else:
  2141. if len(self._chat_locks) >= self.CHAT_DICT_MAX_SIZE:
  2142. self._chat_locks.pop(next((k for k in self._chat_locks if not self._chat_locks[k].locked()), next(iter(self._chat_locks))))
  2143. self._chat_locks[chat_id] = asyncio.Lock()
  2144. return self._chat_locks[chat_id]
  2145. async def send_text(self, chat_id: str, content: str, reply_to: Optional[str] = None, group_code: str = "") -> SendResult:
  2146. """Send text with auto-chunking and per-chat-id ordering guarantee."""
  2147. adapter = self._adapter
  2148. if adapter._connection.ws is None:
  2149. return SendResult(success=False, error="Not connected", retryable=True)
  2150. adapter._outbound.slow_notifier.cancel(chat_id)
  2151. async with self.get_chat_lock(chat_id):
  2152. content_to_send = self.strip_cron_wrapper(content)
  2153. chunks = self.truncate_message(content_to_send, adapter.MAX_TEXT_CHUNK)
  2154. logger.info("[%s] truncate_message: input=%d chars, max=%d, output=%d chunk(s) sizes=%s",
  2155. adapter.name, len(content_to_send), adapter.MAX_TEXT_CHUNK, len(chunks), [len(c) for c in chunks])
  2156. for i, chunk in enumerate(chunks):
  2157. result = await self.send_text_chunk(chat_id, chunk, reply_to if i == 0 else None, group_code=group_code)
  2158. if not result.success:
  2159. return result
  2160. with contextlib.suppress(Exception): # delivery done → FINISH heartbeat (RUNNING… → message → FINISH)
  2161. await adapter._outbound.heartbeat.send_heartbeat_once(chat_id, WS_HEARTBEAT_FINISH)
  2162. return SendResult(success=True)
  2163. async def send_media(self, chat_id: str, handler_name: str, reply_to: Optional[str] = None,
  2164. caption: Optional[str] = None, **kwargs: Any) -> SendResult:
  2165. handler = self._media_handlers.get(handler_name)
  2166. if handler is None:
  2167. return SendResult(success=False, error=f"Unknown media handler: {handler_name!r}")
  2168. return await handler.handle(self._adapter, chat_id, reply_to=reply_to, caption=caption, **kwargs)
  2169. async def send_direct(self, chat_id: str, message: str, media_files: Optional[list[tuple[str, bool]]] = None) -> dict[str, Any]:
  2170. """send_message tool entry: text first, then each media file by extension, on the running adapter."""
  2171. adapter = self._adapter
  2172. last_result: Optional[SendResult] = None
  2173. if message.strip():
  2174. last_result = await adapter.send(chat_id, message)
  2175. if not last_result.success:
  2176. return {"error": f"Yuanbao send failed: {last_result.error}"}
  2177. for media_path, _is_voice in media_files or []:
  2178. send = adapter.send_image_file if Path(media_path).suffix.lower() in self.IMAGE_EXTS else adapter.send_document
  2179. last_result = await send(chat_id, media_path)
  2180. if not last_result.success:
  2181. return {"error": f"Yuanbao media send failed: {last_result.error}"}
  2182. if last_result is None:
  2183. return {"error": "No deliverable text or media remained after processing"}
  2184. return {"success": True, "platform": "yuanbao", "chat_id": chat_id, "message_id": last_result.message_id}
  2185. async def dispatch_msg_body(self, chat_id: str, msg_body: list, reply_to: Optional[str] = None, group_code: str = "") -> SendResult:
  2186. """Lock + dispatch an arbitrary MsgBody to C2C or group."""
  2187. async with self.get_chat_lock(chat_id):
  2188. result = await self._send_msg_body(chat_id, msg_body, reply_to, group_code)
  2189. return self._to_send_result(result)
  2190. @staticmethod
  2191. def _to_send_result(raw: dict) -> SendResult:
  2192. if raw.get("success"):
  2193. return SendResult(success=True, message_id=raw.get("msg_key"))
  2194. return SendResult(success=False, error=raw.get("error", "Unknown error"))
  2195. async def send_text_chunk(self, chat_id: str, text: str, reply_to: Optional[str] = None, retry: int = 3, group_code: str = "") -> SendResult:
  2196. """Send a single text chunk with retry (exponential backoff: 1s, 2s, 4s)."""
  2197. adapter = self._adapter
  2198. last_error: str = "Unknown error"
  2199. for attempt in range(retry):
  2200. try:
  2201. if chat_id.startswith("group:"):
  2202. msg_body = self._build_msg_body_with_mentions(text, chat_id[len("group:"):])
  2203. else:
  2204. msg_body = [_text_elem(text)]
  2205. raw = await self._send_msg_body(chat_id, msg_body, reply_to, group_code)
  2206. if raw.get("success"):
  2207. return self._to_send_result(raw)
  2208. last_error = raw.get("error", "Unknown error")
  2209. logger.warning("[%s] send_text_chunk attempt %d/%d failed: %s", adapter.name, attempt + 1, retry, last_error)
  2210. except Exception as exc:
  2211. last_error = str(exc)
  2212. logger.warning("[%s] send_text_chunk attempt %d/%d exception: %s", adapter.name, attempt + 1, retry, last_error)
  2213. if attempt < retry - 1:
  2214. await asyncio.sleep(2 ** attempt)
  2215. logger.error("[%s] send_text_chunk max retries (%d) exceeded. Last error: %s", adapter.name, retry, last_error)
  2216. return SendResult(success=False, error=f"Max retries exceeded: {last_error}")
  2217. async def _send_msg_body(self, chat_id: str, msg_body: list, reply_to: Optional[str], group_code: str) -> dict:
  2218. """Route a MsgBody to group (``group:<code>``) or C2C (``direct:<account>`` / bare account)."""
  2219. if chat_id.startswith("group:"):
  2220. return await self.send_group_msg_body(chat_id[len("group:"):], msg_body, reply_to)
  2221. return await self.send_c2c_msg_body(chat_id.removeprefix("direct:"), msg_body, group_code=group_code)
  2222. def _build_msg_body_with_mentions(self, text: str, group_code: str) -> list:
  2223. """Parse @nickname patterns against the (unexpired) member cache into mixed TIMTextElem +
  2224. TIMCustomElem(elem_type 1002) msg_body; plain text when no members are cached."""
  2225. cached = self._adapter._member_cache.get(group_code)
  2226. if cached and time.time() - cached[0] >= self._adapter.MEMBER_CACHE_TTL_S:
  2227. del self._adapter._member_cache[group_code]
  2228. cached = None
  2229. if not cached or not cached[1]:
  2230. return [_text_elem(text)]
  2231. nickname_to_uid = {}
  2232. for m in cached[1]:
  2233. nick = m.get("nickname") or m.get("nick_name") or ""
  2234. uid = m.get("user_id") or ""
  2235. if nick and uid:
  2236. nickname_to_uid[nick.lower()] = (nick, uid)
  2237. msg_body: list = []
  2238. last_idx = 0
  2239. for match in self._AT_USER_RE.finditer(text):
  2240. seg = text[last_idx:match.start()].strip()
  2241. if seg:
  2242. msg_body.append(_text_elem(seg))
  2243. nickname = match.group(1)
  2244. entry = nickname_to_uid.get(nickname.lower())
  2245. if entry:
  2246. real_nick, uid = entry
  2247. msg_body.append({"msg_type": "TIMCustomElem",
  2248. "msg_content": {"data": json.dumps({"elem_type": 1002, "text": f"@{real_nick}", "user_id": uid})}})
  2249. else:
  2250. msg_body.append(_text_elem(f"@{nickname}"))
  2251. last_idx = match.end()
  2252. tail = text[last_idx:].strip()
  2253. if tail:
  2254. msg_body.append(_text_elem(tail))
  2255. return msg_body or [_text_elem(text)]
  2256. async def send_c2c_msg_body(self, to_account: str, msg_body: list, group_code: str = "") -> dict:
  2257. req_id = f"c2c_{next_seq_no()}"
  2258. return await self._dispatch_encoded(self._adapter, encode_send_c2c_message(
  2259. to_account=to_account, msg_body=msg_body, from_account=self._adapter._bot_id or "", msg_id=req_id, group_code=group_code,
  2260. ), req_id)
  2261. async def send_group_msg_body(self, group_code: str, msg_body: list, reply_to: Optional[str] = None) -> dict:
  2262. req_id = f"grp_{next_seq_no()}"
  2263. return await self._dispatch_encoded(self._adapter, encode_send_group_message(
  2264. group_code=group_code, msg_body=msg_body, from_account=self._adapter._bot_id or "", msg_id=req_id, ref_msg_id=reply_to or "",
  2265. ), req_id)
  2266. @staticmethod
  2267. async def _dispatch_encoded(adapter: YuanbaoAdapter, encoded: bytes, req_id: str) -> dict:
  2268. """Send pre-encoded bytes via WS → ``{"success", "msg_key" | "error"}``."""
  2269. try:
  2270. response = await adapter._connection.send_biz_request(encoded, req_id=req_id)
  2271. return {"success": True, "msg_key": response.get("msg_id", "")}
  2272. except TimeoutError:
  2273. return {"success": False, "error": f"Request timeout after {DEFAULT_SEND_TIMEOUT}s"}
  2274. except Exception as exc:
  2275. return {"success": False, "error": str(exc)}
  2276. @staticmethod
  2277. def validate_media(file_bytes: Optional[bytes], filename: str, max_size_mb: int = 20) -> Optional[str]:
  2278. """Pre-upload check; error description or None."""
  2279. if not file_bytes:
  2280. return f"Empty file: {filename}"
  2281. if len(file_bytes) > max_size_mb * 1024 * 1024:
  2282. return f"File too large: {filename} ({len(file_bytes) / 1024 / 1024:.1f}MB > {max_size_mb}MB)"
  2283. return None
  2284. @staticmethod
  2285. def truncate_message(content: str, max_length: int = 4000, len_fn: Optional[Callable[[str], int]] = None) -> list[str]:
  2286. """Table/fence-aware chunking via MarkdownProcessor, stripping ``(1/3)`` page indicators."""
  2287. if (len_fn or len)(content) <= max_length:
  2288. return [content]
  2289. chunks = [_INDICATOR_RE.sub('', c) for c in MarkdownProcessor.chunk_markdown_text(content, max_length, len_fn=len_fn)]
  2290. return chunks or [content]
  2291. @staticmethod
  2292. def strip_cron_wrapper(content: str) -> str:
  2293. """Strip the scheduler's cron header/footer wrapper; unchanged when the shape doesn't match."""
  2294. if not content.startswith(CRON_WRAPPER_HEADER_PREFIX):
  2295. return content
  2296. divider = CRON_WRAPPER_DIVIDER
  2297. footer_prefix = CRON_WRAPPER_FOOTER_PREFIX
  2298. divider_pos = content.find(divider)
  2299. footer_pos = content.rfind(footer_prefix)
  2300. if divider_pos < 0 or footer_pos < 0 or footer_pos <= divider_pos or "\n(job_id: " not in content[:divider_pos]:
  2301. return content
  2302. return content[divider_pos + len(divider):footer_pos].strip() or content
  2303. async def close(self) -> None:
  2304. self._chat_locks.clear()
  2305. class OutboundManager:
  2306. """Composes MessageSender, HeartbeatManager and SlowResponseNotifier (sender cancels the notifier
  2307. before a send and emits the FINISH heartbeat after)."""
  2308. def __init__(self, adapter: YuanbaoAdapter) -> None:
  2309. self._adapter = adapter
  2310. self.sender: MessageSender = MessageSender(adapter)
  2311. self.heartbeat: HeartbeatManager = HeartbeatManager(adapter)
  2312. self.slow_notifier: SlowResponseNotifier = SlowResponseNotifier(adapter, self.sender)
  2313. async def close(self) -> None:
  2314. await self.sender.close()
  2315. await self.heartbeat.close()
  2316. await self.slow_notifier.close()
  2317. class YuanbaoAdapter(BasePlatformAdapter):
  2318. """Yuanbao AI Bot adapter backed by a persistent WebSocket connection."""
  2319. PLATFORM = Platform.YUANBAO
  2320. MAX_TEXT_CHUNK: int = 4000 # Yuanbao single message character limit
  2321. splits_long_messages = True # send() auto-chunks via truncate_message(MAX_TEXT_CHUNK)
  2322. MEDIA_MAX_SIZE_MB: int = 50
  2323. DM_MAX_CHARS = 10000
  2324. _active_instance: ClassVar[Optional[YuanbaoAdapter]] = None
  2325. # Per Hermes home: a multiplexed gateway runs one Yuanbao adapter per profile, and the tools /
  2326. # send_message read "the" adapter from inside a profile-scoped turn, so last-wins would route
  2327. # profile B's sends through profile A's bot. Registration and lookup both key on the ambient
  2328. # override (connect/reconnect tasks inherit the profile's Context); the slot above serves the
  2329. # unscoped path.
  2330. _active_instances: ClassVar[dict[str, YuanbaoAdapter]] = {}
  2331. @classmethod
  2332. def get_active(cls) -> Optional[YuanbaoAdapter]:
  2333. from hermes_constants import get_hermes_home_override, hermes_home_key
  2334. if get_hermes_home_override() is None:
  2335. return cls._active_instance
  2336. return cls._active_instances.get(hermes_home_key())
  2337. @classmethod
  2338. def set_active(cls, adapter: Optional[YuanbaoAdapter]) -> None:
  2339. from hermes_constants import get_hermes_home_override, hermes_home_key
  2340. if get_hermes_home_override() is None:
  2341. cls._active_instance = adapter
  2342. elif adapter is None:
  2343. cls._active_instances.pop(hermes_home_key(), None)
  2344. else:
  2345. cls._active_instances[hermes_home_key()] = adapter
  2346. def __init__(self, config: PlatformConfig, **kwargs: Any) -> None:
  2347. super().__init__(config, Platform.YUANBAO)
  2348. _extra = config.extra or {}
  2349. self._app_key: str = (_extra.get("app_id") or "").strip()
  2350. self._app_secret: str = (_extra.get("app_secret") or "").strip()
  2351. self._bot_id: Optional[str] = _extra.get("bot_id") or None
  2352. self._ws_url: str = (_extra.get("ws_url") or DEFAULT_WS_GATEWAY_URL).strip()
  2353. self._api_domain: str = (_extra.get("api_domain") or DEFAULT_API_DOMAIN).rstrip("/")
  2354. self._route_env: str = (_extra.get("route_env") or "").strip()
  2355. # Media resolve concurrency clamped to [min, max] so a bad config can't hammer the backend.
  2356. try:
  2357. _raw_concurrency = int(_extra.get("media_resolve_concurrency", _DEFAULT_RESOLVE_CONCURRENCY))
  2358. except (TypeError, ValueError):
  2359. _raw_concurrency = _DEFAULT_RESOLVE_CONCURRENCY
  2360. self.media_resolve_concurrency: int = max(_MIN_RESOLVE_CONCURRENCY, min(_MAX_RESOLVE_CONCURRENCY, _raw_concurrency))
  2361. self._connection: ConnectionManager = ConnectionManager(self)
  2362. self._outbound: OutboundManager = OutboundManager(self)
  2363. self._inbound_tasks: set[asyncio.Task] = set() # cancelled by disconnect()
  2364. self._background_tasks: set[asyncio.Task] = set() # keeps fire-and-forget tasks alive
  2365. # group_code -> (updated_ts, members); used by @mention resolution, stale after MEMBER_CACHE_TTL_S
  2366. self._member_cache: dict[str, tuple[float, list]] = {}
  2367. self.MEMBER_CACHE_TTL_S: float = 300.0
  2368. self._dedup = MessageDeduplicator(ttl_seconds=300) # WS reconnect / network jitter
  2369. self._group_queues: dict[str, asyncio.Queue] = {} # session_key → sequential dispatch queue
  2370. # Recall support: msg_id/text being processed per session_key (RecallGuardMiddleware), plus a
  2371. # bounded msg_id → content cache for content-match redaction when rows lack a message_id.
  2372. self._processing_msg_ids: dict[str, str] = {}
  2373. self._processing_msg_texts: dict[str, str] = {}
  2374. self._msg_content_cache: dict[str, str] = {}
  2375. def _policy(kind: str) -> tuple[str, list[str]]:
  2376. policy = (_extra.get(f"{kind}_policy") or _yb_secret(f"YUANBAO_{kind.upper()}_POLICY") or "pairing").strip().lower()
  2377. raw = _extra.get(f"{kind}_allow_from") or _yb_secret(f"YUANBAO_{kind.upper()}_ALLOW_FROM", "")
  2378. return policy, [x.strip() for x in raw.split(",") if x.strip()]
  2379. self._access_policy = AccessPolicy(*_policy("dm"), *_policy("group"))
  2380. self._inbound_pipeline: InboundPipeline = InboundPipelineBuilder.build()
  2381. # Auto-sethome stays open when no home is set or the home is a group (upgradable by first DM).
  2382. _existing_home = _yb_secret("YUANBAO_HOME_CHANNEL", "") or (config.home_channel.chat_id if config.home_channel else "")
  2383. self._auto_sethome_done: bool = bool(_existing_home) and not _existing_home.startswith("group:")
  2384. def _track_task(self, task: asyncio.Task) -> asyncio.Task:
  2385. """Register a fire-and-forget task so it won't be GC'd prematurely."""
  2386. self._background_tasks.add(task)
  2387. task.add_done_callback(self._background_tasks.discard)
  2388. return task
  2389. @property
  2390. def enforces_own_access_policy(self) -> bool:
  2391. """Intake gating lives in ``AccessPolicy`` (composed, not inherited), so the flag stays here."""
  2392. return True
  2393. def _sender_may_designate_home(self, ctx: InboundContext) -> bool:
  2394. """Sender may persist YUANBAO_HOME_CHANNEL: strict allowlist, open opt-in, or pairing-approved
  2395. (intake-only pairing forwards are excluded)."""
  2396. policy: AccessPolicy = self._access_policy
  2397. sender = str(ctx.from_account or "").strip()
  2398. if not sender:
  2399. return False
  2400. if ctx.chat_type == "dm":
  2401. if policy.is_dm_allowed(sender):
  2402. return True
  2403. if policy.dm_policy == "pairing":
  2404. from gateway.pairing import PairingStore
  2405. return PairingStore().is_approved(Platform.YUANBAO.value, sender)
  2406. return False
  2407. group_code = str(ctx.group_code or "").strip()
  2408. if ctx.chat_type != "group" or not group_code:
  2409. return False
  2410. if policy.group_policy == "allowlist":
  2411. return policy.is_group_allowed(group_code)
  2412. return policy.group_policy == "open" and policy._open_dm_opted_in()
  2413. async def connect(self, *, is_reconnect: bool = False) -> bool:
  2414. ok = await self._connection.open()
  2415. if ok:
  2416. self._wire_plugin_handlers(None) # plugin-registered native handlers
  2417. return ok
  2418. async def disconnect(self) -> None:
  2419. """Cancel background tasks and close the WebSocket connection."""
  2420. if YuanbaoAdapter._active_instance is self:
  2421. YuanbaoAdapter._active_instance = None
  2422. for home_key, active in list(YuanbaoAdapter._active_instances.items()):
  2423. if active is self:
  2424. del YuanbaoAdapter._active_instances[home_key]
  2425. self._running = False
  2426. self._mark_disconnected()
  2427. self._release_platform_lock()
  2428. await self._connection.close()
  2429. await self._outbound.close()
  2430. for task in list(self._inbound_tasks):
  2431. if not task.done():
  2432. task.cancel()
  2433. self._inbound_tasks.clear()
  2434. self._group_queues.clear()
  2435. logger.info("[%s] Disconnected", self.name)
  2436. async def send(self, chat_id: str, content: str, reply_to: Optional[str] = None,
  2437. metadata: Optional[dict[str, Any]] = None, group_code: str = "") -> SendResult:
  2438. return await self._outbound.sender.send_text(chat_id, content, reply_to, group_code=group_code)
  2439. async def get_chat_info(self, chat_id: str) -> dict[str, Any]:
  2440. return {"name": chat_id, "type": "group" if chat_id.startswith("group:") else "dm"}
  2441. async def send_typing(self, chat_id: str, metadata: Optional[dict] = None) -> None:
  2442. """Start the RUNNING heartbeat (best effort)."""
  2443. with contextlib.suppress(Exception):
  2444. await self._outbound.heartbeat.start(chat_id)
  2445. async def stop_typing(self, chat_id: str) -> None:
  2446. """Stop RUNNING without FINISH — send() emits FINISH after delivery so ordering is
  2447. RUNNING… → message → FINISH."""
  2448. with contextlib.suppress(Exception):
  2449. await self._outbound.heartbeat.stop(chat_id, send_finish=False)
  2450. async def _process_message_background(self, event, session_key: str) -> None:
  2451. """Wrap base class processing with a slow-response notifier."""
  2452. chat_id = event.source.chat_id
  2453. await self._outbound.slow_notifier.start(chat_id)
  2454. try:
  2455. await super()._process_message_background(event, session_key)
  2456. finally:
  2457. self._outbound.slow_notifier.cancel(chat_id)
  2458. # Clear RecallGuard tracking only if our msg_id is still current: a concurrent message may have
  2459. # overwritten it (the drain task then owns it); id-less events never wrote one and must not pop.
  2460. msg_id = event.message_id
  2461. if msg_id and self._processing_msg_ids.get(session_key) == msg_id:
  2462. self._processing_msg_ids.pop(session_key, None)
  2463. self._processing_msg_texts.pop(session_key, None)
  2464. async def _ws_query(self, label: str, group_code: str, encoded: bytes, decode_rsp, empty: dict) -> Optional[dict]:
  2465. """Send an encoded group query over WS; return decoded biz payload, *empty* when none, None on failure."""
  2466. if self._connection.ws is None:
  2467. return None
  2468. try:
  2469. response = await self._connection.send_biz_request(encoded, req_id=decode_conn_msg(encoded)["head"]["msg_id"])
  2470. status = response.get("head", {}).get("status", 0)
  2471. if status != 0:
  2472. logger.warning("[%s] %s failed: status=%d", self.name, label, status)
  2473. return None
  2474. biz_data = response.get("data", b"") or response.get("body", b"")
  2475. return decode_rsp(biz_data) if biz_data and isinstance(biz_data, bytes) else empty
  2476. except TimeoutError:
  2477. logger.warning("[%s] %s timeout: group=%s", self.name, label, group_code)
  2478. except Exception as exc:
  2479. logger.warning("[%s] %s failed: %s", self.name, label, exc)
  2480. return None
  2481. async def query_group_info(self, group_code: str) -> Optional[dict]:
  2482. """Group info (name, owner, member count…); None on failure."""
  2483. return await self._ws_query("query_group_info", group_code, encode_query_group_info(group_code),
  2484. decode_query_group_info_rsp, {"group_code": group_code})
  2485. async def get_group_member_list(self, group_code: str, offset: int = 0, limit: int = 200) -> Optional[dict]:
  2486. """Group member list; None on failure. Populates ``_member_cache`` for @mention resolution."""
  2487. result = await self._ws_query(
  2488. "get_group_member_list", group_code, encode_get_group_member_list(group_code, offset=offset, limit=limit),
  2489. decode_get_group_member_list_rsp, {"members": [], "next_offset": 0, "is_complete": True},
  2490. )
  2491. if result and result.get("members"):
  2492. self._member_cache[group_code] = (time.time(), result["members"])
  2493. return result
  2494. async def send_dm(self, user_id: str, text: str, group_code: str = "") -> SendResult:
  2495. """Proactive C2C DM (text capped at DM_MAX_CHARS); group_code marks a group-originated DM."""
  2496. if not self._access_policy.is_dm_allowed(user_id):
  2497. return SendResult(success=False, error="DM access denied for this user")
  2498. if len(text) > self.DM_MAX_CHARS:
  2499. text = text[:self.DM_MAX_CHARS] + t("platform.shared.truncated_suffix")
  2500. return await self.send(f"direct:{user_id}", text, group_code=group_code)
  2501. # Media sends delegate to MessageSender.send_media via the named handler strategy.
  2502. async def send_image(self, chat_id: str, image_url: str, caption: Optional[str] = None,
  2503. reply_to: Optional[str] = None, metadata: Optional[dict] = None, **kwargs: Any) -> SendResult:
  2504. return await self._outbound.sender.send_media(chat_id, "image_url", reply_to=reply_to, caption=caption, image_url=image_url, **kwargs)
  2505. async def send_image_file(self, chat_id: str, image_path: str, caption: Optional[str] = None,
  2506. reply_to: Optional[str] = None, metadata: Optional[dict] = None, **kwargs: Any) -> SendResult:
  2507. return await self._outbound.sender.send_media(chat_id, "image_file", reply_to=reply_to, caption=caption, image_path=image_path, **kwargs)
  2508. async def send_sticker(self, chat_id: str, sticker_name: Optional[str] = None, face_index: Optional[int] = None,
  2509. reply_to: Optional[str] = None, **kwargs: Any) -> SendResult:
  2510. return await self._outbound.sender.send_media(chat_id, "sticker", reply_to=reply_to, sticker_name=sticker_name, face_index=face_index, **kwargs)
  2511. async def send_document(self, chat_id: str, file_path: str, filename: Optional[str] = None, caption: Optional[str] = None,
  2512. reply_to: Optional[str] = None, metadata: Optional[dict] = None, **kwargs: Any) -> SendResult:
  2513. return await self._outbound.sender.send_media(
  2514. chat_id, "document", reply_to=reply_to, caption=caption, file_path=file_path, filename=filename, **kwargs,
  2515. )
  2516. async def _get_cached_token(self) -> dict:
  2517. """Current valid sign token (module-level cache)."""
  2518. return await SignManager.get_token(self._app_key, self._app_secret, self._api_domain, route_env=self._route_env)