ForcePilot/backend/package/yuxi/channels/adapters/wechat/lifecycle.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

95 lines
3.5 KiB
Python

from __future__ import annotations
from typing import Any
from yuxi.utils.logging_config import logger
LEGACY_CONFIG_KEYS = ("wx_corp_id", "wx_corp_secret", "wx_agent_id", "wx_app_id", "wx_app_secret")
MIGRATION_MAP = {
"wx_corp_id": "corp_id",
"wx_corp_secret": "corp_secret",
"wx_agent_id": "agent_id",
"wx_app_id": "app_id",
"wx_app_secret": "app_secret",
}
class WeChatLifecycleAdapter:
def __init__(self):
self._migration_from: str | None = None
self._migration_log: list[dict[str, Any]] = []
def detect_legacy_config(self, config: dict[str, Any]) -> list[str]:
found: list[str] = []
for key in LEGACY_CONFIG_KEYS:
if key in config:
found.append(key)
if found:
logger.info(f"[WeChat/Lifecycle] Legacy config keys detected: {found}")
return found
def migrate_config(self, config: dict[str, Any]) -> dict[str, Any]:
legacy_keys = self.detect_legacy_config(config)
if not legacy_keys:
return config
migrated_config = dict(config)
for legacy_key, new_key in MIGRATION_MAP.items():
if legacy_key in migrated_config:
if new_key not in migrated_config:
migrated_config[new_key] = migrated_config.pop(legacy_key)
self._migration_log.append(
{
"from_key": legacy_key,
"to_key": new_key,
"action": "migrated",
}
)
else:
migrated_config.pop(legacy_key)
self._migration_log.append(
{
"from_key": legacy_key,
"to_key": new_key,
"action": "skipped (target exists)",
}
)
self._migration_from = "v1"
logger.info(f"[WeChat/Lifecycle] Migrated {len(self._migration_log)} key(s) from v1 config")
return migrated_config
def detect_state_migration(self, config: dict[str, Any]) -> dict[str, Any]:
issues: list[str] = []
if not config.get("corp_id") and config.get("corp_secret"):
issues.append("corp_secret exists without corp_id — possible incomplete migration")
if not config.get("app_id") and config.get("app_secret"):
issues.append("app_secret exists without app_id — possible incomplete migration")
if config.get("agent_id") and not config.get("corp_id"):
issues.append("agent_id exists without corp_id — possible incomplete migration")
mode_count = sum(
[
all(config.get(k) for k in ("corp_id", "corp_secret", "agent_id")),
all(config.get(k) for k in ("app_id", "app_secret")),
bool(config.get("bridge_url")),
]
)
if mode_count > 1:
issues.append(f"Multiple modes configured ({mode_count}) — verify intentionally")
return {
"migration_source": self._migration_from,
"legacy_keys_found": bool(self.detect_legacy_config(config)),
"issues": issues,
"migration_log": list(self._migration_log),
}
def get_migration_report(self) -> dict[str, Any]:
return {
"source_version": self._migration_from,
"total_migrations": len(self._migration_log),
"details": list(self._migration_log),
}