新增大量渠道适配器相关的协议、策略、工具类与基础设施代码,包括: 1. 多协议定义:认证、消息、配置、网关等核心接口 2. 策略模块:上下文、群聊、去重、防抖等业务策略 3. 工具集:重试、去重、文本分块、消息格式化等SDK工具 4. 基础设施:外部进程管理、事件广播、熔断机制等 5. 账户与管道系统:账户管理、消息处理管道实现 6. 运行时服务:状态收集、维护任务、日志等后台服务
105 lines
3.5 KiB
Python
105 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
if TYPE_CHECKING:
|
|
from yuxi.channels.manager import ChannelManager
|
|
|
|
|
|
@dataclass
|
|
class DiagnosisIssue:
|
|
severity: str
|
|
channel_id: str
|
|
category: str
|
|
message: str
|
|
auto_fixable: bool = False
|
|
|
|
|
|
class ConfigDoctor:
|
|
def __init__(self, channel_manager: ChannelManager):
|
|
self._manager = channel_manager
|
|
|
|
async def diagnose(self, channel_id: str | None = None) -> list[DiagnosisIssue]:
|
|
issues: list[DiagnosisIssue] = []
|
|
|
|
channel_ids = [channel_id] if channel_id else self._manager._registry.list_channels()
|
|
for cid in channel_ids:
|
|
issues.extend(await self._diagnose_channel(cid))
|
|
|
|
return issues
|
|
|
|
async def auto_fix(self, issue: DiagnosisIssue) -> bool:
|
|
if not issue.auto_fixable:
|
|
return False
|
|
|
|
try:
|
|
if issue.category == "connectivity":
|
|
await self._manager.restart_channel(issue.channel_id)
|
|
return True
|
|
if issue.category == "credential":
|
|
return False
|
|
except Exception:
|
|
logger.exception(f"Auto-fix failed for {issue.channel_id}: {issue.category}")
|
|
return False
|
|
|
|
async def suggest_migration(self) -> list[str]:
|
|
return []
|
|
|
|
async def _diagnose_channel(self, channel_id: str) -> list[DiagnosisIssue]:
|
|
issues: list[DiagnosisIssue] = []
|
|
|
|
channel_config = self._manager._channels_config.get(channel_id, {})
|
|
|
|
adapter = self._manager._adapters.get(channel_id)
|
|
if adapter and hasattr(adapter, "diagnose_channel"):
|
|
try:
|
|
result = await adapter.diagnose_channel()
|
|
for finding in result.get("findings", []):
|
|
severity = finding.get("severity", "warning")
|
|
if severity == "ok":
|
|
continue
|
|
issues.append(
|
|
DiagnosisIssue(
|
|
severity="error" if severity == "error" else "warning",
|
|
channel_id=channel_id,
|
|
category=finding.get("category", "connectivity"),
|
|
message=finding.get("message", str(finding)),
|
|
auto_fixable=finding.get("auto_fixable", False),
|
|
)
|
|
)
|
|
except Exception:
|
|
logger.exception(f"Adapter diagnose failed for {channel_id}")
|
|
|
|
if not channel_config.get("enabled"):
|
|
return issues
|
|
|
|
required_fields = channel_config.get("required_fields", [])
|
|
for field in required_fields:
|
|
if not channel_config.get(field):
|
|
issues.append(
|
|
DiagnosisIssue(
|
|
severity="error",
|
|
channel_id=channel_id,
|
|
category="credential",
|
|
message=f"Missing required config field: {field}",
|
|
auto_fixable=False,
|
|
)
|
|
)
|
|
|
|
adapter_cls = self._manager._registry.get(channel_id)
|
|
if adapter_cls is None:
|
|
issues.append(
|
|
DiagnosisIssue(
|
|
severity="error",
|
|
channel_id=channel_id,
|
|
category="registry",
|
|
message=f"No adapter registered for channel {channel_id}",
|
|
auto_fixable=False,
|
|
)
|
|
)
|
|
|
|
return issues
|