ForcePilot/backend/package/yuxi/channels/plugins/feishu/entry.py
Kris 742299cb07 chore: 批量清理报告相关代码并完成多项功能迭代
本次提交包含多维度代码优化与功能增强:
1.  移除报告模块冗余导入与枚举,清理报表相关代码
2.  新增扫码登录支持方法与飞书适配器适配
3.  完善异常日志与健康检查信息
4.  扩展目录、配对管理、能力查询等接口
5.  优化出站管道与事务提交后钩子逻辑
6.  修复飞书消息解析与响应空值问题
7.  重构配置更新与服务账号创建逻辑
8.  统一传输错误分类契约与错误基类扩展
2026-07-06 20:49:35 +08:00

212 lines
8.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""飞书渠道插件入口。
实现 ``CHANNEL_ENTRY`` 契约,注册 18 个适配器与生命周期钩子,返回
``PluginManifest``。由宿主在 loaded 阶段通过 ``importlib`` 导入并调用。
调用顺序(严格):
1. ``getXxxPort`` 获取被驱动端口实例(宿主已从 manifest.accessible_ports
加载权限,无需运行时 declareXxx
2. 实例化 ``FeishuLifecycleHandler`` 与 ``FeishuClient``。
3. 实例化并注册 18 个适配器17 个列表追加 + 1 个 identity_resolver 单实例)。
4. ``registerLifecycleHandler`` 注册生命周期钩子(鸭子类型调用)。
5. 返回 ``PluginManifest``。
F-03 单真相源:本入口仅承接 discover 阶段解析的 ``ChannelManifest``
不再重复构造声明性字段capabilities / config_schema / accessible_ports /
injectable_pipelines / resource_quota / env_vars / credential_strategy 等),
所有声明性字段以 ``manifest.json`` 为唯一真相源。本函数仅负责适配器实例化、
client 实例化、LifecycleHandler 构造与 ``registerAdapter`` 调用。
per-account 适配器处理:``WizardAdapter`` 的 ``__init__`` 需
``account_id``,但 ``CHANNEL_ENTRY`` 在插件加载时不知具体账户,传入空字符串
占位(``_PLACEHOLDER_ACCOUNT_ID``)。``WhitelistAdapter`` 由协议方法传入
``account_id``,无需构造时绑定。``WizardAdapter`` 实际 account_id 由
``LifecycleAdapter.afterAccountConfigWritten`` 在账户配置写入后按账户重建
对应适配器,或由调用方通过 ``ConfigPort`` 动态读取。
WebSocket 长连接管理:飞书 WS 连接由 ``StreamWorker`` 通过
``FeishuStreamConnectorAdapter`` 统一管理(账号启用/禁用由
``ChannelAccountOnline``/``ChannelAccountOffline`` 领域事件触发),
``LifecycleAdapter`` 与 ``LifecycleHandler`` 不再自管理 WS 连接。
依赖方向:仅 import ``yuxi.channels.contract.*`` + 标准库 + 同插件内部模块,
不污染框架层。
"""
from __future__ import annotations
from yuxi.channels.contract.plugin.entry import PluginHost
from yuxi.channels.contract.plugin.manifest import (
ChannelManifest,
PluginManifest,
)
from .adapters.command_adapter import FeishuCommandAdapter
from .adapters.directory_adapter import FeishuDirectoryAdapter
from .adapters.doctor_adapter import FeishuDoctorAdapter
from .adapters.identity_resolver_adapter import FeishuIdentityResolverAdapter
from .adapters.inbound_adapter import FeishuInboundAdapter
from .adapters.lifecycle_adapter import FeishuLifecycleAdapter
from .adapters.login_adapter import FeishuLoginAdapter
from .adapters.mention_adapter import FeishuMentionAdapter
from .adapters.message_ops_adapter import FeishuMessageOpsAdapter
from .adapters.outbound_adapter import FeishuOutboundAdapter
from .adapters.probeable_adapter import FeishuProbeableAdapter
from .adapters.rich_message_adapter import FeishuRichMessageAdapter
from .adapters.session_adapter import FeishuSessionAdapter
from .adapters.status_adapter import FeishuStatusAdapter
from .adapters.stream_connector_adapter import FeishuStreamConnectorAdapter
from .adapters.streaming_adapter import FeishuStreamingAdapter
from .adapters.whitelist_adapter import FeishuWhitelistAdapter
from .adapters.wizard_adapter import FeishuWizardAdapter
from .feishu_client import FeishuClient
from .lifecycle import FeishuLifecycleHandler
# per-account 适配器占位 account_idCHANNEL_ENTRY 在插件加载时不知具体账户,
# WizardAdapter 传入空字符串占位。WhitelistAdapter 由协议方法传入 account_id
# 无需构造时绑定。
# WizardAdapter 实际 account_id 由 LifecycleAdapter 在账户配置写入后按账户重建
# 适配器实例,或由适配器在方法调用时通过 ConfigPort 动态读取。
_PLACEHOLDER_ACCOUNT_ID = ""
# 已注册适配器类型列表(与 registerAdapter 调用顺序一致)。
# 此处为运行时适配器清单,非声明性字段,需与下方 registerAdapter 调用保持一致。
_ADAPTER_TYPES: tuple[str, ...] = (
"inbound",
"outbound",
"session",
"status",
"rich_message",
"streaming",
"mention",
"message_ops",
"directory",
"command",
"whitelist",
"wizard",
"doctor",
"lifecycle",
"probeable",
"login",
"identity_resolver",
"stream_connector",
)
def channel_entry(host: PluginHost, manifest: ChannelManifest) -> PluginManifest:
"""飞书渠道插件入口。
由宿主在 loaded 阶段调用,完成适配器注册、生命周期钩子注册,
返回 ``PluginManifest``。
能力边界accessible_ports / injectable_pipelines / resource_quota /
config_schema / env_vars / credential_strategy 等)由 discover 阶段
解析的 ``manifest`` 静态字段承载,宿主在加载期通过
``PluginCapabilityChecker.check`` 校验,运行时通过
``PluginHostImpl._checkPortAccess`` 从 manifest 读取并强制约束。本函数
不重复构造声明性字段F-03 单真相源)。
参数:
host: 插件宿主,提供端口获取与扩展点注册 API。
manifest: 渠道清单,由 discover 阶段从 ``manifest.json`` 解析得到,
承载所有声明性字段。本函数复用其 ``config_schema`` 注入
``LifecycleHandler``,并直接作为返回 ``PluginManifest.manifest``。
"""
# 1. 获取端口实例(宿主已从 manifest.accessible_ports 加载权限)
config_port = host.getConfigPort()
logger_port = host.getLoggerPort()
cache_port = host.getCachePort()
persistence_port = host.getPersistencePort()
# 2. 实例化 FeishuClient 与 LifecycleHandler
# 依赖关系为单向handler 在 onInit 调 client.attach_http_client 注入
# 连接池client 不引用 handler。先创建 client再创建 handler 并通过
# 构造器注入。config_schema 取自 manifestF-03 单真相源)。
client = FeishuClient(config_port, cache_port, logger_port)
handler = FeishuLifecycleHandler(
config_port,
logger_port,
cache_port,
manifest.config_schema,
client,
)
# 3. 实例化并注册 18 个适配器
# 17 个列表追加 + 1 个 identity_resolver 单实例赋值
host.registerAdapter(
"inbound",
FeishuInboundAdapter(client, config_port, logger_port),
)
host.registerAdapter(
"outbound",
FeishuOutboundAdapter(client, logger_port, cache_port),
)
host.registerAdapter("session", FeishuSessionAdapter())
host.registerAdapter("status", FeishuStatusAdapter(logger_port))
host.registerAdapter("rich_message", FeishuRichMessageAdapter(logger_port))
host.registerAdapter(
"streaming",
FeishuStreamingAdapter(client, logger_port, cache_port),
)
host.registerAdapter("mention", FeishuMentionAdapter(config_port))
host.registerAdapter(
"message_ops",
FeishuMessageOpsAdapter(client, logger_port, cache_port),
)
host.registerAdapter(
"directory",
FeishuDirectoryAdapter(client, cache_port),
)
host.registerAdapter("command", FeishuCommandAdapter(config_port))
host.registerAdapter(
"whitelist",
FeishuWhitelistAdapter(
persistence_port,
logger_port,
),
)
host.registerAdapter(
"wizard",
FeishuWizardAdapter(config_port, client, _PLACEHOLDER_ACCOUNT_ID),
)
host.registerAdapter("doctor", FeishuDoctorAdapter(client, config_port))
host.registerAdapter(
"lifecycle",
FeishuLifecycleAdapter(client, config_port, logger_port),
)
host.registerAdapter(
"probeable",
FeishuProbeableAdapter(client, config_port, persistence_port),
)
host.registerAdapter(
"login",
FeishuLoginAdapter(client, config_port, logger_port),
)
# identity_resolver 为单实例注册(非列表追加)
host.registerAdapter(
"identity_resolver",
FeishuIdentityResolverAdapter(client, cache_port),
)
# stream_connector飞书 WS 长连接适配器,由 StreamWorker 管理连接生命周期。
# 需 ConfigPort 读取 ws_endpoint_path / api_base_url支持按部署环境定制
# WS 端点 API 路径(私有化部署场景)。
host.registerAdapter(
"stream_connector",
FeishuStreamConnectorAdapter(persistence_port, logger_port, config_port),
)
# 4. 注册生命周期钩子
# PluginHost Protocol 未声明 registerLifecycleHandler但 PluginHostImpl
# 已实现,运行时通过鸭子类型调用。
host.registerLifecycleHandler(handler)
# 5. 返回 PluginManifestmanifest 直接复用入参adapters 为运行时清单)
return PluginManifest(
manifest=manifest,
adapters=_ADAPTER_TYPES,
)
CHANNEL_ENTRY = channel_entry