ForcePilot/backend/package/yuxi/channel/channels/feishu/adapter.py
Kris 9e503becd3
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat(plugin): 实现完整的插件注册管理系统
新增了插件相关的完整领域模型、应用服务、基础设施实现,包括:
1. 插件状态、注册模式、来源等基础枚举和数据结构
2. 插件清单解析、发现、加载工具类
3. 插件注册表领域服务和内存存储实现
4. 插件相关的命令、查询、事件定义
5. 插件REST API接口和DTO映射
6. 集成了原有通道适配器到插件系统
7. 新增内置插件注册和自动发现能力
2026-05-31 16:44:13 +08:00

114 lines
3.7 KiB
Python

from __future__ import annotations
import logging
from yuxi.channel.channels.feishu.config import FeishuConfig
from yuxi.channel.channels.feishu.outbound import FEISHU_CAPABILITIES, FeishuOutbound
from yuxi.channel.channels.feishu.translator import FeishuTranslator
from yuxi.channel.channels.feishu.ws_connection import FeishuWsConnection
from yuxi.channel.domain.model.message.dispatch_result import SendResult
from yuxi.channel.domain.model.message.unified_message import UnifiedMessage
from yuxi.channel.domain.model.shared.channel_capabilities import ChannelCapabilities
from yuxi.channel.domain.model.shared.channel_type import ChannelType
from yuxi.channel.interfaces.rest.router.contributor import ChannelRouteContributor
from yuxi.channel.domain.port.ws_connection_port import WsConnectionPort
logger = logging.getLogger(__name__)
class _FeishuRouteContributor:
@property
def router(self) -> object:
from yuxi.channel.channels.feishu.routes import router
return router
class FeishuAdapter:
def __init__(
self,
*,
app_id: str = "",
app_secret: str = "",
verification_token: str = "",
encrypt_key: str = "",
ws_enabled: bool = True,
) -> None:
self._outbound = FeishuOutbound(app_id=app_id, app_secret=app_secret)
self._verification_token = verification_token
self._encrypt_key = encrypt_key
self._ws_enabled = ws_enabled
self._ws: FeishuWsConnection | None = None
if ws_enabled and app_id and app_secret:
self._ws = FeishuWsConnection(app_id=app_id, app_secret=app_secret)
self._opened = False
@property
def capabilities(self) -> ChannelCapabilities:
return FEISHU_CAPABILITIES
@property
def bot_id(self) -> str:
return self._outbound.app_id
@property
def channel_type(self) -> str:
return ChannelType.FEISHU.value
@property
def ws_connection(self) -> WsConnectionPort | None:
return self._ws
@property
def route_contributor(self) -> ChannelRouteContributor | None:
return _FeishuRouteContributor()
@classmethod
def get_default_config(cls) -> dict:
return {
"app_id": "",
"app_secret": "",
"verification_token": "",
"encrypt_key": "",
"ws_enabled": True,
}
@classmethod
def from_config(cls, config: FeishuConfig) -> FeishuAdapter:
return cls(
app_id=config.app_id,
app_secret=config.app_secret,
verification_token=config.verification_token,
encrypt_key=config.encrypt_key,
ws_enabled=config.ws_enabled,
)
async def open(self) -> None:
await self._outbound.start()
self._opened = True
async def close(self) -> None:
self._opened = False
async def receive_message(self, raw: dict) -> UnifiedMessage:
return FeishuTranslator.translate(raw)
async def send_message(self, session_id: str, content: str, *, channel_type: str, metadata: dict) -> SendResult:
try:
ok = await self._outbound.send_text(session_id, content)
return SendResult(success=ok, error="" if ok else "feishu_send_failed")
except Exception as exc:
return SendResult(success=False, error=str(exc))
async def send_typing(self, session_id: str) -> None:
pass
async def send_media(self, session_id: str, *, url: str, media_type: str, metadata: dict) -> bool:
logger.warning("send_media not implemented for feishu channel")
return False
async def is_healthy(self) -> bool:
if self._ws is not None:
return self._ws.is_connected
return self._opened