From 90a9e496d947ebca44be80dfa94eb1525902e391 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Thu, 9 Jan 2025 19:45:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=E5=99=A8=E7=B1=BB?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E9=85=8D=E7=BD=AE=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/platform_metadata.py | 4 +++- astrbot/core/platform/register.py | 15 +++++++++++++-- astrbot/core/star/star_manager.py | 2 +- astrbot/dashboard/routes/config.py | 7 +++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/astrbot/core/platform/platform_metadata.py b/astrbot/core/platform/platform_metadata.py index 5a94d8544..28120a1ce 100644 --- a/astrbot/core/platform/platform_metadata.py +++ b/astrbot/core/platform/platform_metadata.py @@ -2,4 +2,6 @@ from dataclasses import dataclass @dataclass class PlatformMetadata(): name: str # 平台的名称 - description: str # 平台的描述 \ No newline at end of file + description: str # 平台的描述 + + default_config_tmpl: dict = None # 平台的默认配置模板 \ No newline at end of file diff --git a/astrbot/core/platform/register.py b/astrbot/core/platform/register.py index 2db6026cd..f451f5b6a 100644 --- a/astrbot/core/platform/register.py +++ b/astrbot/core/platform/register.py @@ -7,15 +7,26 @@ platform_registry: List[PlatformMetadata] = [] platform_cls_map: Dict[str, Type] = {} '''维护了平台适配器名称和适配器类的映射''' -def register_platform_adapter(adapter_name: str, desc: str): - '''用于注册平台适配器的带参装饰器''' +def register_platform_adapter(adapter_name: str, desc: str, default_config_tmpl: dict = None): + '''用于注册平台适配器的带参装饰器。 + + default_config_tmpl 指定了平台适配器的默认配置模板。用户填写好后将会作为 platform_config 传入你的 Platform 类的实现类。 + ''' def decorator(cls): if adapter_name in platform_cls_map: raise ValueError(f"平台适配器 {adapter_name} 已经注册过了,可能发生了适配器命名冲突。") + + # 添加必备选项 + if default_config_tmpl: + if 'type' not in default_config_tmpl: + default_config_tmpl['type'] = adapter_name + if 'enable' not in default_config_tmpl: + default_config_tmpl['enable'] = False pm = PlatformMetadata( name=adapter_name, description=desc, + default_config_tmpl=default_config_tmpl ) platform_registry.append(pm) platform_cls_map[adapter_name] = cls diff --git a/astrbot/core/star/star_manager.py b/astrbot/core/star/star_manager.py index 45f107656..ed5655af2 100644 --- a/astrbot/core/star/star_manager.py +++ b/astrbot/core/star/star_manager.py @@ -102,7 +102,7 @@ class PluginManager: '''更新插件的依赖''' args = ['install', '-r', path, '--trusted-host', 'mirrors.aliyun.com', '-i', 'https://mirrors.aliyun.com/pypi/simple/'] if self.config.pip_install_arg: - args.extend(self.config.pip_install_arg) + args.extend([self.config.pip_install_arg]) result_code = pip_main(args) if result_code != 0: raise Exception(str(result_code)) diff --git a/astrbot/dashboard/routes/config.py b/astrbot/dashboard/routes/config.py index aee6315ca..1f80e50d3 100644 --- a/astrbot/dashboard/routes/config.py +++ b/astrbot/dashboard/routes/config.py @@ -7,6 +7,7 @@ from astrbot.core.config.default import CONFIG_METADATA_2, DEFAULT_VALUE_MAP from astrbot.core.config.astrbot_config import AstrBotConfig from astrbot.core.star.config import update_config from astrbot.core.core_lifecycle import AstrBotCoreLifecycle +from astrbot.core.platform.register import platform_registry def try_cast(value: str, type_: str): if type_ == "int" and value.isdigit(): @@ -121,6 +122,12 @@ class ConfigRoute(Route): async def _get_astrbot_config(self): config = self.config + + platform_default_tmpl = CONFIG_METADATA_2['platform_group']['metadata']['platform']['config_template'] + for platform in platform_registry: + if platform.default_config_tmpl: + platform_default_tmpl[platform.name] = platform.default_config_tmpl + return { "metadata": CONFIG_METADATA_2, "config": config