From 46ef76c168aab772517aed275464948dc92bca41 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sat, 22 Mar 2025 19:54:54 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=94=AF=E6=8C=81=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=B9=B3=E5=8F=B0=E7=9A=84=E7=83=AD=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/manager.py | 64 ++++++++++++++-------------- astrbot/core/platform/platform.py | 2 + astrbot/dashboard/routes/config.py | 6 ++- dashboard/src/views/PlatformPage.vue | 4 -- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/astrbot/core/platform/manager.py b/astrbot/core/platform/manager.py index 0e738ca31..22a06b739 100644 --- a/astrbot/core/platform/manager.py +++ b/astrbot/core/platform/manager.py @@ -85,8 +85,11 @@ class PlatformManager: ) return cls_type = platform_cls_map[platform_config["type"]] - inst = cls_type(platform_config, self.settings, self.event_queue) - self._inst_map[platform_config["id"]] = inst + inst: Platform = cls_type(platform_config, self.settings, self.event_queue) + self._inst_map[platform_config["id"]] = { + "inst": inst, + "client_id": inst.client_self_id, + } self.platform_insts.append(inst) asyncio.create_task( @@ -110,38 +113,37 @@ class PlatformManager: logger.error("-------") async def reload(self, platform_config: dict): - # 还未实现完成,不要调用此方法 - - if platform_config["id"] in self._inst_map: - # 正在运行 - if getattr(self._inst_map[platform_config["id"]], "terminate", None): - logger.info(f"正在尝试终止 {platform_config['id']} 平台适配器 ...") - await self._inst_map[platform_config["id"]].terminate() - logger.info(f"{platform_config['id']} 平台适配器已终止。") - del self._inst_map[platform_config["id"]] - self.platform_insts.remove(self._inst_map[platform_config["id"]]) - else: - logger.warning(f"可能无法正常终止 {platform_config['id']} 平台适配器。") - - # 再启动新的实例 + await self.terminate_platform(platform_config["id"]) + if platform_config["enable"]: await self.load_platform(platform_config) - else: - # 先将 _inst_map 中在 platform_config 中不存在的实例删除 - config_ids = [platform["id"] for platform in self.platforms_config] - for key in list(self._inst_map.keys()): - if key not in config_ids: - if getattr(self._inst_map[key], "terminate", None): - logger.info(f"正在尝试终止 {key} 平台适配器 ...") - await self._inst_map[key].terminate() - logger.info(f"{key} 平台适配器已终止。") - del self._inst_map[key] - self.platform_insts.remove(self._inst_map[key]) - else: - logger.warning(f"可能无法正常终止 {key} 平台适配器。") + # 和配置文件保持同步 + config_ids = [provider["id"] for provider in self.platforms_config] + for key in list(self._inst_map.keys()): + if key not in config_ids: + await self.terminate_platform(key) - # 再启动新的实例 - await self.load_platform(platform_config) + async def terminate_platform(self, platform_id: str): + if platform_id in self._inst_map: + logger.info(f"正在尝试终止 {platform_id} 平台适配器 ...") + + # client_id = self._inst_map.pop(platform_id, None) + info = self._inst_map.pop(platform_id, None) + client_id = info["client_id"] + inst = info["inst"] + try: + self.platform_insts.remove( + next( + inst + for inst in self.platform_insts + if inst.client_self_id == client_id + ) + ) + except Exception: + logger.warning(f"可能未完全移除 {platform_id} 平台适配器") + + if getattr(inst, "terminate", None): + await inst.terminate() async def terminate(self): for inst in self.platform_insts: diff --git a/astrbot/core/platform/platform.py b/astrbot/core/platform/platform.py index bcda23e06..6ed53fe0e 100644 --- a/astrbot/core/platform/platform.py +++ b/astrbot/core/platform/platform.py @@ -1,4 +1,5 @@ import abc +import uuid from typing import Awaitable, Any from asyncio import Queue from .platform_metadata import PlatformMetadata @@ -13,6 +14,7 @@ class Platform(abc.ABC): super().__init__() # 维护了消息平台的事件队列,EventBus 会从这里取出事件并处理。 self._event_queue = event_queue + self.client_self_id = uuid.uuid4().hex @abc.abstractmethod def run(self) -> Awaitable[Any]: diff --git a/astrbot/dashboard/routes/config.py b/astrbot/dashboard/routes/config.py index 04f3aa27e..14af21bbc 100644 --- a/astrbot/dashboard/routes/config.py +++ b/astrbot/dashboard/routes/config.py @@ -219,7 +219,8 @@ class ConfigRoute(Route): return Response().error("未找到对应平台").__dict__ try: - await self._save_astrbot_configs(self.config) + save_config(self.config, self.config, is_core=True) + await self.core_lifecycle.platform_manager.reload(new_config) except Exception as e: return Response().error(str(e)).__dict__ return Response().ok(None, "更新平台配置成功~").__dict__ @@ -255,7 +256,8 @@ class ConfigRoute(Route): else: return Response().error("未找到对应平台").__dict__ try: - await self._save_astrbot_configs(self.config) + save_config(self.config, self.config, is_core=True) + await self.core_lifecycle.platform_manager.terminate_platform(platform_id) except Exception as e: return Response().error(str(e)).__dict__ return Response().ok(None, "删除平台配置成功~").__dict__ diff --git a/dashboard/src/views/PlatformPage.vue b/dashboard/src/views/PlatformPage.vue index 7f83cc124..ac3d24ad5 100644 --- a/dashboard/src/views/PlatformPage.vue +++ b/dashboard/src/views/PlatformPage.vue @@ -99,7 +99,6 @@ {{ save_message }} -