Skip to content

The agent that grows with you

refactor(yuanbao): plugin manifest, register() and import paths

Teknium337ac57c6e79
diff --git a/plugins/platforms/yuanbao/__init__.py b/plugins/platforms/yuanbao/__init__.py
new file mode 100644
index 00000000000..d3197d8288b
--- /dev/null
+++ b/plugins/platforms/yuanbao/__init__.py
@@ -0,0 +1,7 @@
+# Absolute import (not ``from .adapter``): the plugin loader executes this package as
+# ``hermes_plugins.yuanbao_platform``; importing the adapter by its canonical module name keeps
+# ONE ``YuanbaoAdapter`` class object, so ``YuanbaoAdapter.get_active()`` from tools/yuanbao_tools.py
+# and the standalone sender see the instance the gateway registry created.
+from plugins.platforms.yuanbao.adapter import register
+
+__all__ = ["register"]
diff --git a/plugins/platforms/yuanbao/adapter.py b/plugins/platforms/yuanbao/adapter.py
index fa0008a318d..2dcea695453 100644
--- a/plugins/platforms/yuanbao/adapter.py
+++ b/plugins/platforms/yuanbao/adapter.py
@@ -49,14 +49,14 @@ from gateway.platforms.base import (
)
from gateway.platforms.event import MessageEvent, MessageType
from gateway.platforms import helpers as _mdchunk
-from gateway.platforms._shared import get_scoped_secret as _yb_secret, profile_scoped as _profile_scoped
+from gateway.platforms._shared import get_scoped_secret as _yb_secret, profile_scoped as _profile_scoped, send_error
from gateway.platforms.helpers import MessageDeduplicator, cancel_task
from gateway.platforms.access_policy_mixin import OwnAccessPolicyMixin
-from gateway.platforms.yuanbao_media import (
+from plugins.platforms.yuanbao.yuanbao_media import (
download_url as media_download_url, get_cos_credentials, upload_to_cos,
build_image_msg_body, build_file_msg_body, guess_mime_type, md5_hex,
)
-from gateway.platforms.yuanbao_proto import (
+from plugins.platforms.yuanbao.yuanbao_proto import (
CMD_TYPE, WS_HEARTBEAT_RUNNING, WS_HEARTBEAT_FINISH, HERMES_INSTANCE_ID,
_fields_to_dict, _get_string, _get_varint, _parse_fields,
decode_conn_msg, decode_inbound_push, decode_forward_msg_data,
@@ -2261,7 +2261,7 @@ class StickerHandler(MediaSendHandler):
return b"", "sticker", "application/octet-stream" # no file bytes needed
def build_msg_body(self, upload_result, **kwargs):
- from gateway.platforms.yuanbao_sticker import (
+ from plugins.platforms.yuanbao.yuanbao_sticker import (
get_sticker_by_name, get_random_sticker, build_face_msg_body, build_sticker_msg_body,
)
sticker_name = kwargs.get("sticker_name")
@@ -2818,3 +2818,70 @@ class YuanbaoAdapter(BasePlatformAdapter):
async def _get_cached_token(self) -> dict:
"""Current valid sign token (module-level cache)."""
return await SignManager.get_token(self._app_key, self._app_secret, self._api_domain, route_env=self._route_env)
+
+
+# ---------------------------------------------------------------------------
+# Plugin registration
+# ---------------------------------------------------------------------------
+
+_INSTALL_HINT = "Yuanbao: websockets not installed. Run: pip install websockets"
+
+_SETUP_PLATFORM = {
+ "key": "yuanbao", "label": "Yuanbao", "emoji": "💎", "token_var": "YUANBAO_APP_ID",
+ "setup_instructions": [
+ "1. Download the Yuanbao app from https://yuanbao.tencent.com/",
+ "2. In the app, go to PAI → My Bot and create a new bot",
+ "3. After the bot is created, copy the App ID and App Secret",
+ "4. Enter them below and Hermes will connect automatically over WebSocket",
+ ],
+ "vars": [
+ {"name": "YUANBAO_APP_ID", "prompt": "App ID", "password": False,
+ "help": "The App ID from your Yuanbao IM Bot credentials."},
+ {"name": "YUANBAO_APP_SECRET", "prompt": "App Secret", "password": True,
+ "help": "The App Secret (used for HMAC signing) from your Yuanbao IM Bot."},
+ ],
+}
+
+
+def check_yuanbao_requirements() -> bool:
+ """Runtime deps present (the websockets client)."""
+ return WEBSOCKETS_AVAILABLE
+
+
+def interactive_setup() -> None:
+ """``hermes gateway setup`` flow: the standard App ID / App Secret prompts."""
+ from hermes_cli import gateway as gateway_mod
+ gateway_mod._setup_standard_platform(dict(_SETUP_PLATFORM))
+
+
+def _is_connected(config) -> bool:
+ """Configured = App ID (or legacy App Key) + App Secret, from config extras or env."""
+ extra = getattr(config, "extra", {}) or {}
+ import hermes_cli.gateway as gateway_mod
+ app_id = (extra.get("app_id") or gateway_mod.get_env_value("YUANBAO_APP_ID")
+ or gateway_mod.get_env_value("YUANBAO_APP_KEY") or "")
+ secret = extra.get("app_secret") or gateway_mod.get_env_value("YUANBAO_APP_SECRET") or ""
+ return bool(str(app_id).strip() and str(secret).strip())
+
+
+async def _standalone_send(pconfig, chat_id, message, *, thread_id=None, media_files=None, **_kwargs):
+ """standalone_sender_fn: send via the running adapter's persistent WebSocket (no throwaway
+ client possible — Yuanbao needs the live signed connection)."""
+ adapter = YuanbaoAdapter.get_active()
+ if adapter is None:
+ return send_error("Yuanbao adapter is not running. Start the gateway with yuanbao platform enabled first.")
+ try:
+ return await adapter._outbound.sender.send_direct(chat_id, message, media_files)
+ except Exception as e:
+ return send_error(f"Yuanbao send failed: {e}")
+
+
+def register(ctx) -> None:
+ """Plugin entry point — called by the Hermes plugin system."""
+ ctx.register_platform(
+ name="yuanbao", label="Yuanbao", adapter_factory=YuanbaoAdapter,
+ check_fn=check_yuanbao_requirements, is_connected=_is_connected,
+ required_env=["YUANBAO_APP_ID", "YUANBAO_APP_SECRET"], install_hint=_INSTALL_HINT,
+ setup_fn=interactive_setup, allowed_users_env="YUANBAO_ALLOWED_USERS",
+ allow_all_env="YUANBAO_ALLOW_ALL_USERS", cron_deliver_env_var="YUANBAO_HOME_CHANNEL",
+ standalone_sender_fn=_standalone_send, emoji="💎", allow_update_command=False)
diff --git a/plugins/platforms/yuanbao/plugin.yaml b/plugins/platforms/yuanbao/plugin.yaml
new file mode 100644
index 00000000000..1f098e49652
--- /dev/null
+++ b/plugins/platforms/yuanbao/plugin.yaml
@@ -0,0 +1,68 @@
+name: yuanbao-platform
+label: Yuanbao
+kind: platform
+version: 1.0.0
+description: >
+ Tencent Yuanbao gateway adapter for Hermes Agent.
+ Connects to Yuanbao IM Bot over a persistent WebSocket (HMAC-signed sign token)
+ and relays DMs and group messages between Yuanbao and the Hermes agent.
+ Supports native media/stickers, DM/group access policies, and home-channel cron delivery.
+author: NousResearch
+requires_env:
+ - name: YUANBAO_APP_ID
+ description: "The App ID from your Yuanbao IM Bot credentials (legacy alias: YUANBAO_APP_KEY)"
+ prompt: "App ID"
+ password: false
+ - name: YUANBAO_APP_SECRET
+ description: "The App Secret (used for HMAC signing) from your Yuanbao IM Bot"
+ prompt: "App Secret"
+ password: true
+optional_env:
+ - name: YUANBAO_BOT_ID
+ description: "Bot account ID (auto-detected from the sign token when unset)"
+ prompt: "Bot ID"
+ password: false
+ - name: YUANBAO_WS_URL
+ description: "Override the Yuanbao WebSocket gateway URL"
+ prompt: "WebSocket URL"
+ password: false
+ - name: YUANBAO_API_DOMAIN
+ description: "Override the Yuanbao API domain (sign-token endpoint)"
+ prompt: "API domain"
+ password: false
+ - name: YUANBAO_ROUTE_ENV
+ description: "Routing environment header for non-production Yuanbao backends"
+ prompt: "Route env"
+ password: false
+ - name: YUANBAO_DM_POLICY
+ description: "DM access policy: open, allowlist or disabled"
+ prompt: "DM policy (open|allowlist|disabled)"
+ password: false
+ - name: YUANBAO_DM_ALLOW_FROM
+ description: "Comma-separated user IDs allowed to DM the bot (allowlist policy)"
+ prompt: "DM allowlist (comma-separated)"
+ password: false
+ - name: YUANBAO_GROUP_POLICY
+ description: "Group access policy: open, allowlist or disabled"
+ prompt: "Group policy (open|allowlist|disabled)"
+ password: false
+ - name: YUANBAO_GROUP_ALLOW_FROM
+ description: "Comma-separated group IDs allowed (allowlist policy)"
+ prompt: "Group allowlist (comma-separated)"
+ password: false
+ - name: YUANBAO_ALLOWED_USERS
+ description: "Comma-separated Yuanbao user IDs allowed to talk to the bot"
+ prompt: "Allowed users (comma-separated)"
+ password: false
+ - name: YUANBAO_ALLOW_ALL_USERS
+ description: "Allow any Yuanbao user to trigger the bot (dev only)"
+ prompt: "Allow all users? (true/false)"
+ password: false
+ - name: YUANBAO_HOME_CHANNEL
+ description: "Default chat ID for cron / notification delivery"
+ prompt: "Home channel ID"
+ password: false
+ - name: YUANBAO_HOME_CHANNEL_NAME
+ description: "Display name for the home channel"
+ prompt: "Home channel name"
+ password: false
diff --git a/tools/yuanbao_tools.py b/tools/yuanbao_tools.py
index 0688db3550d..7a84c7b391f 100644
--- a/tools/yuanbao_tools.py
+++ b/tools/yuanbao_tools.py
@@ -3,7 +3,7 @@
get_group_info / query_group_members / search_sticker / send_sticker / send_dm. Sticker
flow mirrors chatbot-web's sticker-search/sticker-send: the LLM should search_sticker for
a sticker_id (or pass the Chinese name), then send_sticker — never bare Unicode emoji.
-The active adapter singleton lives in ``gateway.platforms.yuanbao.YuanbaoAdapter.get_active``.
+The active adapter singleton lives in ``plugins.platforms.yuanbao.adapter.YuanbaoAdapter.get_active``.
"""
from __future__ import annotations
@@ -54,9 +54,9 @@ def _yb_tool(label: str):
def _get_active_adapter():
- """Lazy import to avoid ImportError when gateway.platforms.yuanbao is unavailable."""
+ """Lazy import to avoid ImportError when plugins.platforms.yuanbao is unavailable."""
with suppress(ImportError):
- from gateway.platforms.yuanbao import YuanbaoAdapter
+ from plugins.platforms.yuanbao.adapter import YuanbaoAdapter
return YuanbaoAdapter.get_active()
return None
@@ -163,7 +163,7 @@ async def query_group_members(args) -> dict:
@_yb_tool("search_sticker")
async def search_sticker(args) -> dict:
"""在内置贴纸表中按关键词模糊搜索,返回 Top-N 候选(空 query 返回前 N 条)。"""
- from gateway.platforms.yuanbao_sticker import search_stickers
+ from plugins.platforms.yuanbao.yuanbao_sticker import search_stickers
query, limit = args.get("query", ""), args.get("limit", 10)
try:
@@ -183,7 +183,7 @@ async def send_sticker(args) -> dict:
"""向 chat_id(缺省取当前会话 HERMES_SESSION_CHAT_ID)发送一张内置贴纸(TIMFaceElem)。
``sticker``: 名称(如 "六六六")或 sticker_id(如 "278");为空时随机发送。
``chat_id``: ``direct:{account_id}`` / ``group:{group_code}`` / 裸 account_id。"""
- from gateway.platforms.yuanbao_sticker import get_sticker_by_id, get_sticker_by_name, get_random_sticker
+ from plugins.platforms.yuanbao.yuanbao_sticker import get_sticker_by_id, get_sticker_by_name, get_random_sticker
target = (args.get("chat_id", "") or "").strip() or _session_env("HERMES_SESSION_CHAT_ID")
if not target: