ForcePilot/backend/package/yuxi/channel/extensions/matrix/status.py
Kris 4e9c6dd8ab feat(channel): 添加 Matrix 渠道扩展
新增 Matrix 渠道扩展,支持在 Yuxi 平台中集成 Matrix 去中心化通讯协议。

包含以下功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- crypto: 端到端加密
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- room_resolver: 房间解析
- dm_tracker: 私聊追踪
- rate_limiter: 速率限制
- actions: 动作处理
- constants: 常量定义
- utils: 工具函数
- types: 类型定义
2026-05-21 11:18:13 +08:00

68 lines
1.9 KiB
Python

from __future__ import annotations
import logging
from .config import _apply_env_overrides, _dict_to_account
from .types import MatrixProbe
from .utils import get_nio
logger = logging.getLogger(__name__)
async def probe(config: dict, account_id: str = "default") -> MatrixProbe:
nio = get_nio()
aid = account_id or "default"
account_data = config.get("accounts", {}).get(aid, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
if not account.homeserver or not account.access_token:
return MatrixProbe(
ok=False,
homeserver=account.homeserver,
error="homeserver or access_token not configured",
)
client = nio.AsyncClient(
homeserver=account.homeserver,
user=account.user_id,
)
client.access_token = account.access_token
try:
resp = await client.whoami()
return MatrixProbe(
ok=True,
user_id=resp.user_id,
device_id=resp.device_id or account.device_id,
homeserver=account.homeserver,
)
except Exception as e:
logger.warning("Matrix probe failed: %s", e)
return MatrixProbe(
ok=False,
homeserver=account.homeserver,
error=str(e),
)
finally:
await client.close()
def build_summary(config: dict, account_id: str = "default") -> dict:
aid = account_id or "default"
account_data = config.get("accounts", {}).get(aid, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
return {
"channel": "matrix",
"account_id": account.account_id,
"homeserver": account.homeserver,
"user_id": account.user_id,
"device_name": account.device_name,
"encryption": account.encryption,
"dm_policy": account.dm_policy,
"group_policy": account.group_policy,
"streaming": account.streaming,
}