feat: add runtime capability probing
这个提交存在于:
父节点
f7c29f0e54
当前提交
796e379e74
共有 2 个文件被更改,包括 152 次插入 和 0 次删除
19
main.py
19
main.py
|
|
@ -157,6 +157,7 @@ except ModuleNotFoundError as exc:
|
|||
logger.warning("[PrivateCompanion] self_timeline.py 缺失,已跳过 Bot 自身时间线注入能力。请重新安装完整版本。")
|
||||
from .core_store import CoreStoreMixin
|
||||
from .platform_compat import PlatformCompatibilityMixin
|
||||
from .runtime_compat import probe_runtime_capabilities
|
||||
from .integration_status import IntegrationStatusMixin
|
||||
from .astrbot_knowledge import AstrBotKnowledgeMixin
|
||||
from .atrelay import AtRelayMixin
|
||||
|
|
@ -239,6 +240,14 @@ class PrivateCompanionExtensionAPI:
|
|||
def __init__(self, plugin: "PrivateCompanionPlugin") -> None:
|
||||
self._plugin = plugin
|
||||
|
||||
def probe_runtime_capabilities(self) -> dict[str, Any]:
|
||||
capabilities = getattr(self._plugin, "runtime_capabilities", None)
|
||||
if capabilities is None:
|
||||
return {"compatibility_level": "unknown", "capabilities": {}}
|
||||
getter = getattr(capabilities, "to_dict", None)
|
||||
result = getter() if callable(getter) else capabilities
|
||||
return result if isinstance(result, dict) else {"compatibility_level": "unknown", "capabilities": {}}
|
||||
|
||||
def register_proactive_ability(self, spec: dict[str, Any]) -> bool:
|
||||
return self._plugin.register_external_proactive_ability(spec)
|
||||
|
||||
|
|
@ -978,6 +987,16 @@ class PrivateCompanionPlugin(
|
|||
global _private_companion_plugin
|
||||
_private_companion_plugin = self
|
||||
self.extension_api = PrivateCompanionExtensionAPI(self)
|
||||
self.runtime_capabilities = probe_runtime_capabilities(
|
||||
context=context, plugin_name=PLUGIN_NAME, plugin_version=PLUGIN_VERSION
|
||||
)
|
||||
if self.runtime_capabilities.compatibility_level != "full":
|
||||
logger.warning(
|
||||
"[PrivateCompanion] AstrBot 运行时兼容级别=%s missing=%s warnings=%s",
|
||||
self.runtime_capabilities.compatibility_level,
|
||||
self.runtime_capabilities.missing_required,
|
||||
self.runtime_capabilities.warnings,
|
||||
)
|
||||
self._external_proactive_abilities: dict[str, dict[str, Any]] = {}
|
||||
self._external_realtime_activities: dict[str, dict[str, Any]] = {}
|
||||
self.config = config
|
||||
|
|
|
|||
133
runtime_compat.py
普通文件
133
runtime_compat.py
普通文件
|
|
@ -0,0 +1,133 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import importlib.metadata
|
||||
from dataclasses import asdict, dataclass, field
|
||||
from typing import Any
|
||||
|
||||
|
||||
_REQUIRED_CAPABILITIES = (
|
||||
"star_register",
|
||||
"data_dir",
|
||||
"event_hooks",
|
||||
"provider_request",
|
||||
"message_components",
|
||||
)
|
||||
_OPTIONAL_IMPORTS = {
|
||||
"star_register": ("astrbot.api.star", "register"),
|
||||
"data_dir": ("astrbot.api.star", "StarTools"),
|
||||
"event_hooks": ("astrbot.api.event", "filter"),
|
||||
"provider_request": ("astrbot.api.provider", "ProviderRequest"),
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RuntimeCapabilities:
|
||||
plugin_name: str
|
||||
plugin_version: str
|
||||
astrbot_version: str = "unknown"
|
||||
api_generation: str = "unknown"
|
||||
compatibility_level: str = "unsupported"
|
||||
capabilities: dict[str, bool] = field(default_factory=dict)
|
||||
missing_required: tuple[str, ...] = ()
|
||||
warnings: tuple[str, ...] = ()
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
result = asdict(self)
|
||||
result["missing_required"] = list(self.missing_required)
|
||||
result["warnings"] = list(self.warnings)
|
||||
return result
|
||||
|
||||
|
||||
def _version_from_module() -> str:
|
||||
for module_name in ("astrbot", "astrbot.core"):
|
||||
try:
|
||||
module = importlib.import_module(module_name)
|
||||
except Exception:
|
||||
continue
|
||||
for attr_name in ("__version__", "VERSION", "version"):
|
||||
value = getattr(module, attr_name, "")
|
||||
if value:
|
||||
return str(value)
|
||||
for distribution_name in ("astrbot", "AstrBot"):
|
||||
try:
|
||||
return importlib.metadata.version(distribution_name)
|
||||
except importlib.metadata.PackageNotFoundError:
|
||||
continue
|
||||
except Exception:
|
||||
break
|
||||
return "unknown"
|
||||
|
||||
|
||||
def _probe_import(module_name: str, attribute_name: str) -> bool:
|
||||
try:
|
||||
module = importlib.import_module(module_name)
|
||||
return hasattr(module, attribute_name)
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _probe_filter_capabilities() -> dict[str, bool]:
|
||||
try:
|
||||
module = importlib.import_module("astrbot.api.event")
|
||||
event_filter = getattr(module, "filter", None)
|
||||
except Exception:
|
||||
event_filter = None
|
||||
return {
|
||||
"llm_request_hook": callable(getattr(event_filter, "on_llm_request", None)),
|
||||
"llm_response_hook": callable(getattr(event_filter, "on_llm_response", None)),
|
||||
"event_message_type": callable(getattr(event_filter, "event_message_type", None)),
|
||||
"command_group": callable(getattr(event_filter, "command_group", None)),
|
||||
}
|
||||
|
||||
|
||||
def probe_runtime_capabilities(
|
||||
*,
|
||||
context: Any = None,
|
||||
event: Any = None,
|
||||
plugin_name: str = "",
|
||||
plugin_version: str = "",
|
||||
) -> RuntimeCapabilities:
|
||||
capabilities = {
|
||||
name: _probe_import(*target) for name, target in _OPTIONAL_IMPORTS.items()
|
||||
}
|
||||
capabilities.update(_probe_filter_capabilities())
|
||||
capabilities["message_components"] = _probe_import("astrbot.api.message_components", "Plain") or _probe_import(
|
||||
"astrbot.core.message.components", "Plain"
|
||||
)
|
||||
capabilities["web_api"] = callable(getattr(context, "register_web_api", None))
|
||||
capabilities["platform_manager"] = getattr(context, "platform_manager", None) is not None
|
||||
capabilities["event_unified_msg_origin"] = bool(
|
||||
event is not None and hasattr(event, "unified_msg_origin")
|
||||
)
|
||||
capabilities["event_send"] = any(
|
||||
callable(getattr(event, name, None)) for name in ("send", "send_message", "plain_result")
|
||||
)
|
||||
capabilities["event_stop"] = callable(getattr(event, "stop_event", None))
|
||||
|
||||
missing_required = tuple(name for name in _REQUIRED_CAPABILITIES if not capabilities.get(name, False))
|
||||
warnings: list[str] = []
|
||||
if not capabilities.get("web_api"):
|
||||
warnings.append("当前上下文没有 register_web_api,管理页面/扩展 API 需要降级或关闭")
|
||||
if not capabilities.get("platform_manager"):
|
||||
warnings.append("当前上下文没有 platform_manager,平台实例选择将依赖事件或配置")
|
||||
if not capabilities.get("event_unified_msg_origin"):
|
||||
warnings.append("启动探测阶段没有事件对象,消息 scope 需在收到事件后再次确认")
|
||||
if missing_required:
|
||||
level = "unsupported"
|
||||
elif len(warnings) > 1:
|
||||
level = "degraded"
|
||||
else:
|
||||
level = "full"
|
||||
version = _version_from_module()
|
||||
api_generation = "4.x" if version.startswith("4.") else "unknown"
|
||||
return RuntimeCapabilities(
|
||||
plugin_name=plugin_name,
|
||||
plugin_version=plugin_version,
|
||||
astrbot_version=version,
|
||||
api_generation=api_generation,
|
||||
compatibility_level=level,
|
||||
capabilities=capabilities,
|
||||
missing_required=missing_required,
|
||||
warnings=tuple(warnings),
|
||||
)
|
||||
正在加载…
添加表格
在新工单中引用