新增 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: 类型定义
73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SESSION_KEY_SEPARATOR = ":"
|
|
|
|
|
|
def build_dm_key_per_user(user_id: str) -> str:
|
|
return f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}direct{SESSION_KEY_SEPARATOR}{user_id}"
|
|
|
|
|
|
def build_dm_key_per_room(user_id: str, room_id: str) -> str:
|
|
return (
|
|
f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}matrix"
|
|
f"{SESSION_KEY_SEPARATOR}direct{SESSION_KEY_SEPARATOR}{user_id}"
|
|
f"{SESSION_KEY_SEPARATOR}{room_id}"
|
|
)
|
|
|
|
|
|
def build_dm_key_main() -> str:
|
|
return f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}main"
|
|
|
|
|
|
def build_group_key(room_id: str) -> str:
|
|
return (
|
|
f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}matrix"
|
|
f"{SESSION_KEY_SEPARATOR}group{SESSION_KEY_SEPARATOR}{room_id}"
|
|
)
|
|
|
|
|
|
def build_thread_key(room_id: str, thread_root_id: str) -> str:
|
|
return (
|
|
f"agent{SESSION_KEY_SEPARATOR}main{SESSION_KEY_SEPARATOR}matrix"
|
|
f"{SESSION_KEY_SEPARATOR}group{SESSION_KEY_SEPARATOR}{room_id}"
|
|
f"{SESSION_KEY_SEPARATOR}thread{SESSION_KEY_SEPARATOR}{thread_root_id}"
|
|
)
|
|
|
|
|
|
def extract_thread_id(msg: object) -> str | None:
|
|
if hasattr(msg, "thread_parent_id") and msg.thread_parent_id:
|
|
return msg.thread_parent_id
|
|
if hasattr(msg, "message_thread_id") and msg.message_thread_id:
|
|
return msg.message_thread_id
|
|
return None
|
|
|
|
|
|
def resolve_reply_transport(msg: object, thread_id: str | None):
|
|
from yuxi.channel.protocols import ReplyTransport
|
|
|
|
transport = ReplyTransport()
|
|
if thread_id and hasattr(msg, "group") and msg.group:
|
|
room_id = msg.group.id if hasattr(msg.group, "id") else ""
|
|
transport.thread_id = thread_id
|
|
transport.target_id = room_id or transport.target_id
|
|
return transport
|
|
|
|
|
|
def get_thread_replies_mode(account: dict) -> str:
|
|
if isinstance(account, dict):
|
|
return account.get("thread_replies", "inbound")
|
|
return getattr(account, "thread_replies", "inbound")
|
|
|
|
|
|
def should_thread_reply(msg: object, account: dict) -> bool:
|
|
mode = get_thread_replies_mode(account)
|
|
if mode == "always":
|
|
return True
|
|
if mode == "inbound":
|
|
return extract_thread_id(msg) is not None
|
|
return False
|