ForcePilot/backend/package/yuxi/channel/infrastructure/configuration/channel_config.py
Kris c61d5f0163 feat: 完成通道服务多轮功能迭代
本次提交完成了一系列核心功能迭代与优化:
1.  新增并完善了多个领域模型与端口定义,补充了`__all__`导出规范
2.  优化了会话、绑定、出箱等模块的数据模型,修复了时间字段类型不一致问题
3.  新增了代理ID解析、缓存发布等接口,扩展了系统能力
4.  重构了去重中间件逻辑,优化了空内容校验规则
5.  新增了认证中间件的匿名访问支持,完善了鉴权流程
6.  优化了SSE连接管理,增加了单会话连接上限限制
7.  重构了消息日志与仓储相关代码,将数据类迁移至对应模型目录
8.  新增了重复绑定校验、绑定更新接口,完善了绑定服务逻辑
9.  优化了健康检查逻辑,新增了环境变量控制启动时间线展示
10. 重构了出箱重试工作线程,使用缓存端口替代直接redis操作,新增了消息处理标记逻辑
11. 完善了飞书、Web、钩子等通道的翻译器逻辑,补充了账户ID传递
12. 新增了多种自定义异常类型,优化了异常映射与错误处理流程
13. 完善了配置热重载逻辑,同步认证凭证与校验器配置
14. 重构了Redis缓存实现,增加了异常捕获与包装
2026-05-31 21:42:03 +08:00

125 lines
4.0 KiB
Python

from __future__ import annotations
from yuxi.utils.logging_config import logger
class ChannelConfig:
def __init__(self, yaml_path: str):
self._yaml_path = yaml_path
self._data: dict = {}
self._load()
def _load(self) -> None:
try:
import yaml
with open(self._yaml_path) as f:
self._data = yaml.safe_load(f) or {}
except FileNotFoundError:
logger.warning("channel config not found: %s, using defaults", self._yaml_path)
self._data = {}
except Exception as exc:
logger.warning("channel config load error: %s, using defaults", exc)
self._data = {}
async def reload(self) -> None:
self._load()
logger.info("channel config reloaded from %s", self._yaml_path)
async def on_config_updated(self, config: dict) -> list[str]:
old_auth_token = self._data.get("auth", {}).get("token")
old_auth_password = self._data.get("auth", {}).get("password")
old_feishu_encrypt_key = self._data.get("feishu", {}).get("encrypt_key")
self._data = config
updated = []
new_auth_token = config.get("auth", {}).get("token")
new_auth_password = config.get("auth", {}).get("password")
new_feishu_encrypt_key = config.get("feishu", {}).get("encrypt_key")
if old_auth_token != new_auth_token:
updated.append("auth_token")
if old_auth_password != new_auth_password:
updated.append("auth_password")
if old_feishu_encrypt_key != new_feishu_encrypt_key:
updated.append("feishu_encrypt_key")
return updated
@property
def auth_token(self) -> str | None:
return self._data.get("auth", {}).get("token")
@property
def auth_password(self) -> str | None:
return self._data.get("auth", {}).get("password")
@property
def access_policies(self) -> dict:
return self._data.get("access_policies", {})
@property
def allow_from(self) -> dict:
return self._data.get("allow_from", {})
@property
def keyword_blocklist(self) -> set[str]:
keywords = self._data.get("keyword_blocklist", [])
return set(kw.lower() for kw in keywords)
@property
def hook_mappings(self) -> list[dict]:
return self._data.get("hooks", {}).get("mappings", [])
@property
def feishu_verification_token(self) -> str | None:
return self._data.get("feishu", {}).get("verification_token")
@property
def feishu_encrypt_key(self) -> str | None:
return self._data.get("feishu", {}).get("encrypt_key")
@property
def max_auth_attempts(self) -> int:
return self._data.get("auth", {}).get("max_attempts", 5)
@property
def lockout_seconds(self) -> int:
return self._data.get("auth", {}).get("lockout_seconds", 300)
@property
def allow_anonymous(self) -> bool:
return self._data.get("auth", {}).get("allow_anonymous", False)
@property
def mention_gate_config(self) -> dict:
return self._data.get("mention_gate", {})
@property
def feishu_ws_config(self) -> dict | None:
feishu = self._data.get("feishu", {})
mode = feishu.get("mode", "webhook")
if mode not in ("websocket", "both"):
return None
return {
"app_id": feishu.get("app_id", ""),
"app_secret": feishu.get("app_secret", ""),
"mode": mode,
}
@property
def dingtalk_ws_config(self) -> dict | None:
dingtalk = self._data.get("dingtalk", {})
mode = dingtalk.get("mode", "webhook")
if mode not in ("websocket", "both"):
return None
return {
"client_id": dingtalk.get("client_id", ""),
"client_secret": dingtalk.get("client_secret", ""),
"mode": mode,
}
def get_channel_config(self, channel_name: str) -> dict:
return self._data.get(channel_name, {})
@property
def raw_data(self) -> dict:
return self._data