mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 17:47:06 +08:00
* feat: add one-line deploy script (deploy-cli.sh) and update cli.md - Add docs/scripts/deploy-cli.sh for one-command deployment (Linux/macOS/WSL) - Update docs/zh/deploy/astrbot/cli.md to reference local script - Replace non-existent https://astrbot.app/deploy.sh with raw.githubusercontent.com URL - Add local run tip and WSL-based Windows support * fix: address PR review feedback for deploy-cli.sh - Replace sort -V with Python-native version check (macOS BSD compat) - Bump Python requirement from >=3.10 to >=3.12 (match pyproject.toml) - Detect current directory as project root (avoid nested clone) - Handle existing non-git directory explicitly - Add curl dependency check - Support ASTRBOT_REPO and ASTRBOT_DIR env vars for forks/mirrors - Update cli.md version description to match * feat: add Windows PowerShell deploy script and update docs - Add deploy-cli.ps1 for Windows native environment (PowerShell 7+) - Update cli.md to document Windows one-liner deployment - Add local script execution instructions for both bash and ps1 * refactor: standardize log messages and improve clarity in various modules Co-authored-by: Copilot <copilot@github.com> * docs: 移除一行命令快速部署部分,简化安装说明 * feat: 添加脚本以复制部署 CLI 文件并更新构建命令 * refactor: 更新日志消息以提高可读性,统一英文提示信息 --------- Co-authored-by: Soulter <905617992@qq.com> Co-authored-by: Copilot <copilot@github.com> Co-authored-by: Weilong Liao <37870767+Soulter@users.noreply.github.com>
92 lines
3.3 KiB
Python
92 lines
3.3 KiB
Python
from astrbot.core import logger
|
||
|
||
from .platform_metadata import PlatformMetadata
|
||
|
||
platform_registry: list[PlatformMetadata] = []
|
||
"""维护了通过装饰器注册的平台适配器"""
|
||
platform_cls_map: dict[str, type] = {}
|
||
"""维护了平台适配器名称和适配器类的映射"""
|
||
|
||
|
||
def register_platform_adapter(
|
||
adapter_name: str,
|
||
desc: str,
|
||
default_config_tmpl: dict | None = None,
|
||
adapter_display_name: str | None = None,
|
||
logo_path: str | None = None,
|
||
support_streaming_message: bool = True,
|
||
i18n_resources: dict[str, dict] | None = None,
|
||
config_metadata: dict | None = None,
|
||
):
|
||
"""用于注册平台适配器的带参装饰器。
|
||
|
||
default_config_tmpl 指定了平台适配器的默认配置模板。用户填写好后将会作为 platform_config 传入你的 Platform 类的实现类。
|
||
logo_path 指定了平台适配器的 logo 文件路径,是相对于插件目录的路径。
|
||
config_metadata 指定了配置项的元数据,用于 WebUI 生成表单。如果不指定,WebUI 将会把配置项渲染为原始的键值对编辑框。
|
||
"""
|
||
|
||
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
|
||
if "id" not in default_config_tmpl:
|
||
default_config_tmpl["id"] = adapter_name
|
||
|
||
# Get the module path of the class being decorated
|
||
module_path = cls.__module__
|
||
|
||
pm = PlatformMetadata(
|
||
name=adapter_name,
|
||
description=desc,
|
||
id=adapter_name,
|
||
default_config_tmpl=default_config_tmpl,
|
||
adapter_display_name=adapter_display_name,
|
||
logo_path=logo_path,
|
||
support_streaming_message=support_streaming_message,
|
||
module_path=module_path,
|
||
i18n_resources=i18n_resources,
|
||
config_metadata=config_metadata,
|
||
)
|
||
platform_registry.append(pm)
|
||
platform_cls_map[adapter_name] = cls
|
||
logger.debug("Platform adapter registered: %s", adapter_name)
|
||
return cls
|
||
|
||
return decorator
|
||
|
||
|
||
def unregister_platform_adapters_by_module(module_path_prefix: str) -> list[str]:
|
||
"""根据模块路径前缀注销平台适配器。
|
||
|
||
在插件热重载时调用,用于清理该插件注册的所有平台适配器。
|
||
|
||
Args:
|
||
module_path_prefix: 模块路径前缀,如 "data.plugins.my_plugin"
|
||
|
||
Returns:
|
||
被注销的平台适配器名称列表
|
||
"""
|
||
unregistered = []
|
||
to_remove = []
|
||
|
||
for pm in platform_registry:
|
||
if pm.module_path and pm.module_path.startswith(module_path_prefix):
|
||
to_remove.append(pm)
|
||
unregistered.append(pm.name)
|
||
|
||
for pm in to_remove:
|
||
platform_registry.remove(pm)
|
||
if pm.name in platform_cls_map:
|
||
del platform_cls_map[pm.name]
|
||
logger.debug(f"平台适配器 {pm.name} 已注销 (来自模块 {pm.module_path})")
|
||
|
||
return unregistered
|