75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from enum import Enum
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from yuxi.utils.logging_config import logger
|
||
|
|
|
||
|
|
|
||
|
|
class IntegrationStatus(str, Enum):
|
||
|
|
NOT_CONFIGURED = "not_configured"
|
||
|
|
CONFIGURED = "configured"
|
||
|
|
CONNECTING = "connecting"
|
||
|
|
CONNECTED = "connected"
|
||
|
|
ERROR = "error"
|
||
|
|
DISCONNECTED = "disconnected"
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatIntegrationStatus:
|
||
|
|
@staticmethod
|
||
|
|
def resolve_status(config: dict[str, Any], connection_status: str) -> IntegrationStatus:
|
||
|
|
has_config = any(config.get(k) for k in ("corp_id", "app_id", "bridge_url"))
|
||
|
|
if not has_config:
|
||
|
|
return IntegrationStatus.NOT_CONFIGURED
|
||
|
|
status_map = {
|
||
|
|
"connected": IntegrationStatus.CONNECTED,
|
||
|
|
"connecting": IntegrationStatus.CONNECTING,
|
||
|
|
"disconnected": IntegrationStatus.DISCONNECTED,
|
||
|
|
"error": IntegrationStatus.ERROR,
|
||
|
|
}
|
||
|
|
return status_map.get(connection_status, IntegrationStatus.CONFIGURED)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_status_summary(status: IntegrationStatus) -> dict[str, Any]:
|
||
|
|
return {
|
||
|
|
"status": status.value,
|
||
|
|
"is_operational": status in (IntegrationStatus.CONNECTED,),
|
||
|
|
"is_configured": status != IntegrationStatus.NOT_CONFIGURED,
|
||
|
|
}
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def log_self_id(
|
||
|
|
config: dict[str, Any],
|
||
|
|
mode: str = "wecom",
|
||
|
|
user_id: str = "",
|
||
|
|
group_ids: list[str] | None = None,
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
identity_info: dict[str, Any] = {
|
||
|
|
"mode": mode,
|
||
|
|
"user_id": user_id or config.get("agent_id", config.get("app_id", "")),
|
||
|
|
"group_ids": group_ids or [],
|
||
|
|
}
|
||
|
|
|
||
|
|
if mode == "wecom":
|
||
|
|
identity_info.update(
|
||
|
|
{
|
||
|
|
"corp_id": config.get("corp_id", "")[:8] + "***",
|
||
|
|
"agent_id": config.get("agent_id", ""),
|
||
|
|
}
|
||
|
|
)
|
||
|
|
elif mode == "mp":
|
||
|
|
identity_info.update(
|
||
|
|
{
|
||
|
|
"app_id": config.get("app_id", "")[:8] + "***",
|
||
|
|
}
|
||
|
|
)
|
||
|
|
elif mode == "personal":
|
||
|
|
identity_info.update(
|
||
|
|
{
|
||
|
|
"bridge_url": config.get("bridge_url", ""),
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
logger.info(f"[WeChat/Status] Self identity: {identity_info}")
|
||
|
|
return identity_info
|