ForcePilot/backend/package/yuxi/channels/adapters/wechat/threading_adapter.py
Kris 05abecc02b feat(wechat): 新增完整微信渠道适配器实现
该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块:
1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等
2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等
3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力
4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等
5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等

实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
2026-05-12 00:51:04 +08:00

64 lines
2.3 KiB
Python

from __future__ import annotations
from typing import Any, Literal
from yuxi.channels.models import ChannelMessage, ChannelResponse
ReplyToMode = Literal["off", "first", "all", "batched"]
class WeChatThreadingAdapter:
@staticmethod
def resolve_reply_to_mode(config: dict[str, Any], chat_type: str) -> ReplyToMode:
if chat_type == "direct":
return config.get("dm_reply_to_mode", "first")
return config.get("group_reply_to_mode", "off")
@staticmethod
def allow_explicit_reply_tags_when_off(config: dict[str, Any]) -> bool:
return config.get("allow_explicit_reply_tags_when_off", False)
@staticmethod
def build_tool_context(message: ChannelMessage, agent_id: str) -> dict[str, Any]:
return {
"channel": "wechat",
"mode": message.metadata.get("wechat_mode", "personal"),
"chat_type": message.metadata.get("chat_type", "direct"),
"sender_id": message.identity.channel_user_id,
"chat_id": message.identity.channel_chat_id,
"agent_id": agent_id,
}
@staticmethod
def resolve_auto_thread_id(message: ChannelMessage) -> str | None:
if message.chat_type.value != "group":
return None
sender_id = message.identity.channel_user_id
chat_id = message.identity.channel_chat_id
return f"auto:{chat_id}:{sender_id}"
@staticmethod
def resolve_current_channel_id(mode: str, account_id: str) -> str:
return f"wechat:{mode}:{account_id}"
@staticmethod
def resolve_reply_transport(response: ChannelResponse) -> dict[str, Any]:
transport: dict[str, Any] = {}
reply_to = response.metadata.get("reply_to_message_id")
if reply_to:
transport["reply_to_message_id"] = reply_to
at_list = response.metadata.get("at_list")
if at_list:
transport["at_list"] = at_list
return transport
@staticmethod
def resolve_focused_binding(config: dict[str, Any], message: ChannelMessage) -> dict[str, Any] | None:
peer_id = message.identity.channel_user_id
bindings = config.get("bindings", [])
for binding in bindings:
peer = binding.get("peer", {})
if peer.get("id") == peer_id:
return {"peer": peer, "agent_id": binding.get("agentId")}
return None