88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any, Protocol
|
||
|
|
|
||
|
|
from yuxi.utils.logging_config import logger
|
||
|
|
|
||
|
|
from .accounts import WeChatAccount, WeChatAccountManager
|
||
|
|
|
||
|
|
|
||
|
|
class GatewayContext(Protocol):
|
||
|
|
cfg: dict[str, Any]
|
||
|
|
account: Any
|
||
|
|
account_id: str
|
||
|
|
runtime: dict[str, Any]
|
||
|
|
log: Any
|
||
|
|
abort_signal: Any
|
||
|
|
|
||
|
|
def set_status(self, status: dict[str, Any]) -> None: ...
|
||
|
|
def get_status(self) -> dict[str, Any]: ...
|
||
|
|
|
||
|
|
|
||
|
|
class WeChatGatewayAdapter:
|
||
|
|
_account_mgr = WeChatAccountManager()
|
||
|
|
|
||
|
|
async def start_account(self, ctx: GatewayContext) -> None:
|
||
|
|
try:
|
||
|
|
ctx.set_status({"running": True, "connected": False})
|
||
|
|
logger.info(f"[WeChat/Gateway] startAccount called for {ctx.account_id}")
|
||
|
|
ctx.set_status({"running": True, "connected": True})
|
||
|
|
except Exception as e:
|
||
|
|
ctx.set_status({"running": False, "connected": False, "error": str(e)})
|
||
|
|
raise
|
||
|
|
|
||
|
|
async def login_with_qr_start(
|
||
|
|
self,
|
||
|
|
account_id: str | None = None,
|
||
|
|
force: bool = False,
|
||
|
|
timeout_ms: int = 120_000,
|
||
|
|
verbose: bool = False,
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
return {"status": "pending", "message": "QR login start not yet triggered from adapter"}
|
||
|
|
|
||
|
|
async def login_with_qr_wait(
|
||
|
|
self,
|
||
|
|
account_id: str | None = None,
|
||
|
|
timeout_ms: int = 120_000,
|
||
|
|
current_qr_data_url: str | None = None,
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
return {"success": False, "message": "QR login wait not available"}
|
||
|
|
|
||
|
|
async def logout_account(
|
||
|
|
self,
|
||
|
|
account_id: str | None = None,
|
||
|
|
cfg: dict[str, Any] | None = None,
|
||
|
|
account: WeChatAccount | None = None,
|
||
|
|
) -> dict[str, Any]:
|
||
|
|
logger.info(f"[WeChat/Gateway] logoutAccount: {account_id}")
|
||
|
|
|
||
|
|
cleared_credentials = []
|
||
|
|
actual_account_id = account_id or (account.account_id if account else "default")
|
||
|
|
|
||
|
|
if cfg:
|
||
|
|
keys_to_clear = [
|
||
|
|
"access_token",
|
||
|
|
"token",
|
||
|
|
"app_secret",
|
||
|
|
"corp_secret",
|
||
|
|
]
|
||
|
|
for key in keys_to_clear:
|
||
|
|
if key in cfg:
|
||
|
|
del cfg[key]
|
||
|
|
cleared_credentials.append(key)
|
||
|
|
|
||
|
|
logger.info(f"[WeChat/Gateway] logoutAccount completed for {actual_account_id}, cleared: {cleared_credentials}")
|
||
|
|
return {
|
||
|
|
"cleared": True,
|
||
|
|
"loggedOut": True,
|
||
|
|
"account_id": actual_account_id,
|
||
|
|
"cleared_credentials": cleared_credentials,
|
||
|
|
}
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def get_account_id(config: dict[str, Any]) -> str:
|
||
|
|
return config.get("account_id", "default")
|
||
|
|
|
||
|
|
def resolve_account(self, config: dict[str, Any], account_id: str = "default") -> WeChatAccount:
|
||
|
|
return self._account_mgr.resolve_account(config, account_id)
|