🎈perf: 优化删除插件目录的逻辑,抛出异常细节;完善 mcp 未安装时的提示

This commit is contained in:
Soulter
2025-03-24 23:07:42 +08:00
parent af6467fb3d
commit 8047f82bfd
3 changed files with 13 additions and 13 deletions
+6 -4
View File
@@ -3,17 +3,19 @@ import json
import textwrap
import os
import asyncio
import mcp
import copy
from typing import Dict, List, Awaitable, Literal, Any
from dataclasses import dataclass
from typing import Optional
from contextlib import AsyncExitStack
from mcp.client.stdio import stdio_client
from astrbot import logger
try:
import mcp
except (ModuleNotFoundError, ImportError):
logger.warning("警告: 缺少依赖库 'mcp',将无法使用 MCP 服务。")
DEFAULT_MCP_CONFIG = {"mcpServers": {}}
SUPPORTED_TYPES = [
@@ -99,7 +101,7 @@ class MCPClient:
)
stdio_transport = await self.exit_stack.enter_async_context(
stdio_client(server_params)
mcp.stdio_client(server_params)
)
self.stdio, self.write = stdio_transport
self.session = await self.exit_stack.enter_async_context(
+4 -2
View File
@@ -471,9 +471,11 @@ class PluginManager:
# 从 star_registry 和 star_map 中删除
await self._unbind_plugin(plugin_name, plugin.module_path)
if not remove_dir(os.path.join(ppath, root_dir_name)):
try:
remove_dir(os.path.join(ppath, root_dir_name))
except Exception as e:
raise Exception(
"移除插件成功,但是删除插件文件夹失败。您可以手动删除该文件夹,位于 addons/plugins/ 下。"
f"移除插件成功,但是删除插件文件夹失败: {str(e)}。您可以手动删除该文件夹,位于 addons/plugins/ 下。"
)
async def _unbind_plugin(self, plugin_name: str, plugin_module_path: str):
+3 -7
View File
@@ -20,24 +20,20 @@ def on_error(func, path, exc_info):
"""
a callback of the rmtree function.
"""
print(f"remove {path} failed.")
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
raise exc_info[1]
def remove_dir(file_path) -> bool:
if not os.path.exists(file_path):
return True
try:
shutil.rmtree(file_path, onerror=on_error)
return True
except BaseException:
return False
shutil.rmtree(file_path, onerror=on_error)
return True
def port_checker(port: int, host: str = "localhost"):