style: 格式化代码

This commit is contained in:
John Richard
2025-10-02 19:38:39 +08:00
parent d5627b0661
commit ecb02cae31
111 changed files with 2344 additions and 2316 deletions

View File

@@ -1,7 +1,7 @@
from pathlib import Path
import re
from typing import TYPE_CHECKING, Dict, List, Optional, Any, Pattern, Tuple, Union, Type
from typing import Dict, List, Optional, Any, Pattern, Tuple, Union, Type
from src.common.logger import get_logger
from src.plugin_system.base.component_types import (
@@ -34,44 +34,46 @@ class ComponentRegistry:
def __init__(self):
# 命名空间式组件名构成法 f"{component_type}.{component_name}"
self._components: Dict[str, 'ComponentInfo'] = {}
self._components: Dict[str, "ComponentInfo"] = {}
"""组件注册表 命名空间式组件名 -> 组件信息"""
self._components_by_type: Dict['ComponentType', Dict[str, 'ComponentInfo']] = {types: {} for types in ComponentType}
self._components_by_type: Dict["ComponentType", Dict[str, "ComponentInfo"]] = {
types: {} for types in ComponentType
}
"""类型 -> 组件原名称 -> 组件信息"""
self._components_classes: Dict[
str, Type[Union['BaseCommand', 'BaseAction', 'BaseTool', 'BaseEventHandler', 'PlusCommand', 'BaseChatter']]
str, Type[Union["BaseCommand", "BaseAction", "BaseTool", "BaseEventHandler", "PlusCommand", "BaseChatter"]]
] = {}
"""命名空间式组件名 -> 组件类"""
# 插件注册表
self._plugins: Dict[str, 'PluginInfo'] = {}
self._plugins: Dict[str, "PluginInfo"] = {}
"""插件名 -> 插件信息"""
# Action特定注册表
self._action_registry: Dict[str, Type['BaseAction']] = {}
self._action_registry: Dict[str, Type["BaseAction"]] = {}
"""Action注册表 action名 -> action类"""
self._default_actions: Dict[str, 'ActionInfo'] = {}
self._default_actions: Dict[str, "ActionInfo"] = {}
"""默认动作集即启用的Action集用于重置ActionManager状态"""
# Command特定注册表
self._command_registry: Dict[str, Type['BaseCommand']] = {}
self._command_registry: Dict[str, Type["BaseCommand"]] = {}
"""Command类注册表 command名 -> command类"""
self._command_patterns: Dict[Pattern, str] = {}
"""编译后的正则 -> command名"""
# 工具特定注册表
self._tool_registry: Dict[str, Type['BaseTool']] = {} # 工具名 -> 工具类
self._llm_available_tools: Dict[str, Type['BaseTool']] = {} # llm可用的工具名 -> 工具类
self._tool_registry: Dict[str, Type["BaseTool"]] = {} # 工具名 -> 工具类
self._llm_available_tools: Dict[str, Type["BaseTool"]] = {} # llm可用的工具名 -> 工具类
# EventHandler特定注册表
self._event_handler_registry: Dict[str, Type['BaseEventHandler']] = {}
self._event_handler_registry: Dict[str, Type["BaseEventHandler"]] = {}
"""event_handler名 -> event_handler类"""
self._enabled_event_handlers: Dict[str, Type['BaseEventHandler']] = {}
self._enabled_event_handlers: Dict[str, Type["BaseEventHandler"]] = {}
"""启用的事件处理器 event_handler名 -> event_handler类"""
self._chatter_registry: Dict[str, Type['BaseChatter']] = {}
self._chatter_registry: Dict[str, Type["BaseChatter"]] = {}
"""chatter名 -> chatter类"""
self._enabled_chatter_registry: Dict[str, Type['BaseChatter']] = {}
self._enabled_chatter_registry: Dict[str, Type["BaseChatter"]] = {}
"""启用的chatter名 -> chatter类"""
logger.info("组件注册中心初始化完成")
@@ -99,7 +101,7 @@ class ComponentRegistry:
def register_component(
self,
component_info: ComponentInfo,
component_class: Type[Union['BaseCommand', 'BaseAction', 'BaseEventHandler', 'BaseTool', 'BaseChatter']],
component_class: Type[Union["BaseCommand", "BaseAction", "BaseEventHandler", "BaseTool", "BaseChatter"]],
) -> bool:
"""注册组件
@@ -172,7 +174,7 @@ class ComponentRegistry:
)
return True
def _register_action_component(self, action_info: 'ActionInfo', action_class: Type['BaseAction']) -> bool:
def _register_action_component(self, action_info: "ActionInfo", action_class: Type["BaseAction"]) -> bool:
"""注册Action组件到Action特定注册表"""
if not (action_name := action_info.name):
logger.error(f"Action组件 {action_class.__name__} 必须指定名称")
@@ -192,7 +194,7 @@ class ComponentRegistry:
return True
def _register_command_component(self, command_info: 'CommandInfo', command_class: Type['BaseCommand']) -> bool:
def _register_command_component(self, command_info: "CommandInfo", command_class: Type["BaseCommand"]) -> bool:
"""注册Command组件到Command特定注册表"""
if not (command_name := command_info.name):
logger.error(f"Command组件 {command_class.__name__} 必须指定名称")
@@ -219,7 +221,7 @@ class ComponentRegistry:
return True
def _register_plus_command_component(
self, plus_command_info: 'PlusCommandInfo', plus_command_class: Type['PlusCommand']
self, plus_command_info: "PlusCommandInfo", plus_command_class: Type["PlusCommand"]
) -> bool:
"""注册PlusCommand组件到特定注册表"""
plus_command_name = plus_command_info.name
@@ -233,7 +235,7 @@ class ComponentRegistry:
# 创建专门的PlusCommand注册表如果还没有
if not hasattr(self, "_plus_command_registry"):
self._plus_command_registry: Dict[str, Type['PlusCommand']] = {}
self._plus_command_registry: Dict[str, Type["PlusCommand"]] = {}
plus_command_class.plugin_name = plus_command_info.plugin_name
# 设置插件配置
@@ -243,7 +245,7 @@ class ComponentRegistry:
logger.debug(f"已注册PlusCommand组件: {plus_command_name}")
return True
def _register_tool_component(self, tool_info: 'ToolInfo', tool_class: Type['BaseTool']) -> bool:
def _register_tool_component(self, tool_info: "ToolInfo", tool_class: Type["BaseTool"]) -> bool:
"""注册Tool组件到Tool特定注册表"""
tool_name = tool_info.name
@@ -259,7 +261,7 @@ class ComponentRegistry:
return True
def _register_event_handler_component(
self, handler_info: 'EventHandlerInfo', handler_class: Type['BaseEventHandler']
self, handler_info: "EventHandlerInfo", handler_class: Type["BaseEventHandler"]
) -> bool:
if not (handler_name := handler_info.name):
logger.error(f"EventHandler组件 {handler_class.__name__} 必须指定名称")
@@ -285,7 +287,7 @@ class ComponentRegistry:
handler_class, self.get_plugin_config(handler_info.plugin_name) or {}
)
def _register_chatter_component(self, chatter_info: 'ChatterInfo', chatter_class: Type['BaseChatter']) -> bool:
def _register_chatter_component(self, chatter_info: "ChatterInfo", chatter_class: Type["BaseChatter"]) -> bool:
"""注册Chatter组件到Chatter特定注册表"""
chatter_name = chatter_info.name
@@ -312,7 +314,7 @@ class ComponentRegistry:
# === 组件移除相关 ===
async def remove_component(self, component_name: str, component_type: 'ComponentType', plugin_name: str) -> bool:
async def remove_component(self, component_name: str, component_type: "ComponentType", plugin_name: str) -> bool:
target_component_class = self.get_component_class(component_name, component_type)
if not target_component_class:
logger.warning(f"组件 {component_name} 未注册,无法移除")
@@ -362,7 +364,7 @@ class ComponentRegistry:
case ComponentType.CHATTER:
# 移除Chatter注册
if hasattr(self, '_chatter_registry'):
if hasattr(self, "_chatter_registry"):
self._chatter_registry.pop(component_name, None)
logger.debug(f"已移除Chatter组件: {component_name}")
@@ -484,8 +486,8 @@ class ComponentRegistry:
# === 组件查询方法 ===
def get_component_info(
self, component_name: str, component_type: Optional['ComponentType'] = None
) -> Optional['ComponentInfo']:
self, component_name: str, component_type: Optional["ComponentType"] = None
) -> Optional["ComponentInfo"]:
# sourcery skip: class-extract-method
"""获取组件信息,支持自动命名空间解析
@@ -529,8 +531,8 @@ class ComponentRegistry:
def get_component_class(
self,
component_name: str,
component_type: Optional['ComponentType'] = None,
) -> Optional[Union[Type['BaseCommand'], Type['BaseAction'], Type['BaseEventHandler'], Type['BaseTool']]]:
component_type: Optional["ComponentType"] = None,
) -> Optional[Union[Type["BaseCommand"], Type["BaseAction"], Type["BaseEventHandler"], Type["BaseTool"]]]:
"""获取组件类,支持自动命名空间解析
Args:
@@ -572,22 +574,22 @@ class ComponentRegistry:
# 4. 都没找到
return None
def get_components_by_type(self, component_type: 'ComponentType') -> Dict[str, 'ComponentInfo']:
def get_components_by_type(self, component_type: "ComponentType") -> Dict[str, "ComponentInfo"]:
"""获取指定类型的所有组件"""
return self._components_by_type.get(component_type, {}).copy()
def get_enabled_components_by_type(self, component_type: 'ComponentType') -> Dict[str, 'ComponentInfo']:
def get_enabled_components_by_type(self, component_type: "ComponentType") -> Dict[str, "ComponentInfo"]:
"""获取指定类型的所有启用组件"""
components = self.get_components_by_type(component_type)
return {name: info for name, info in components.items() if info.enabled}
# === Action特定查询方法 ===
def get_action_registry(self) -> Dict[str, Type['BaseAction']]:
def get_action_registry(self) -> Dict[str, Type["BaseAction"]]:
"""获取Action注册表"""
return self._action_registry.copy()
def get_registered_action_info(self, action_name: str) -> Optional['ActionInfo']:
def get_registered_action_info(self, action_name: str) -> Optional["ActionInfo"]:
"""获取Action信息"""
info = self.get_component_info(action_name, ComponentType.ACTION)
return info if isinstance(info, ActionInfo) else None
@@ -598,11 +600,11 @@ class ComponentRegistry:
# === Command特定查询方法 ===
def get_command_registry(self) -> Dict[str, Type['BaseCommand']]:
def get_command_registry(self) -> Dict[str, Type["BaseCommand"]]:
"""获取Command注册表"""
return self._command_registry.copy()
def get_registered_command_info(self, command_name: str) -> Optional['CommandInfo']:
def get_registered_command_info(self, command_name: str) -> Optional["CommandInfo"]:
"""获取Command信息"""
info = self.get_component_info(command_name, ComponentType.COMMAND)
return info if isinstance(info, CommandInfo) else None
@@ -611,7 +613,7 @@ class ComponentRegistry:
"""获取Command模式注册表"""
return self._command_patterns.copy()
def find_command_by_text(self, text: str) -> Optional[Tuple[Type['BaseCommand'], dict, 'CommandInfo']]:
def find_command_by_text(self, text: str) -> Optional[Tuple[Type["BaseCommand"], dict, "CommandInfo"]]:
# sourcery skip: use-named-expression, use-next
"""根据文本查找匹配的命令
@@ -638,15 +640,15 @@ class ComponentRegistry:
return None
# === Tool 特定查询方法 ===
def get_tool_registry(self) -> Dict[str, Type['BaseTool']]:
def get_tool_registry(self) -> Dict[str, Type["BaseTool"]]:
"""获取Tool注册表"""
return self._tool_registry.copy()
def get_llm_available_tools(self) -> Dict[str, Type['BaseTool']]:
def get_llm_available_tools(self) -> Dict[str, Type["BaseTool"]]:
"""获取LLM可用的Tool列表"""
return self._llm_available_tools.copy()
def get_registered_tool_info(self, tool_name: str) -> Optional['ToolInfo']:
def get_registered_tool_info(self, tool_name: str) -> Optional["ToolInfo"]:
"""获取Tool信息
Args:
@@ -659,13 +661,13 @@ class ComponentRegistry:
return info if isinstance(info, ToolInfo) else None
# === PlusCommand 特定查询方法 ===
def get_plus_command_registry(self) -> Dict[str, Type['PlusCommand']]:
def get_plus_command_registry(self) -> Dict[str, Type["PlusCommand"]]:
"""获取PlusCommand注册表"""
if not hasattr(self, "_plus_command_registry"):
self._plus_command_registry: Dict[str, Type[PlusCommand]] = {}
return self._plus_command_registry.copy()
def get_registered_plus_command_info(self, command_name: str) -> Optional['PlusCommandInfo']:
def get_registered_plus_command_info(self, command_name: str) -> Optional["PlusCommandInfo"]:
"""获取PlusCommand信息
Args:
@@ -679,44 +681,44 @@ class ComponentRegistry:
# === EventHandler 特定查询方法 ===
def get_event_handler_registry(self) -> Dict[str, Type['BaseEventHandler']]:
def get_event_handler_registry(self) -> Dict[str, Type["BaseEventHandler"]]:
"""获取事件处理器注册表"""
return self._event_handler_registry.copy()
def get_registered_event_handler_info(self, handler_name: str) -> Optional['EventHandlerInfo']:
def get_registered_event_handler_info(self, handler_name: str) -> Optional["EventHandlerInfo"]:
"""获取事件处理器信息"""
info = self.get_component_info(handler_name, ComponentType.EVENT_HANDLER)
return info if isinstance(info, EventHandlerInfo) else None
def get_enabled_event_handlers(self) -> Dict[str, Type['BaseEventHandler']]:
def get_enabled_event_handlers(self) -> Dict[str, Type["BaseEventHandler"]]:
"""获取启用的事件处理器"""
return self._enabled_event_handlers.copy()
# === Chatter 特定查询方法 ===
def get_chatter_registry(self) -> Dict[str, Type['BaseChatter']]:
def get_chatter_registry(self) -> Dict[str, Type["BaseChatter"]]:
"""获取Chatter注册表"""
if not hasattr(self, '_chatter_registry'):
if not hasattr(self, "_chatter_registry"):
self._chatter_registry: Dict[str, Type[BaseChatter]] = {}
return self._chatter_registry.copy()
def get_enabled_chatter_registry(self) -> Dict[str, Type['BaseChatter']]:
def get_enabled_chatter_registry(self) -> Dict[str, Type["BaseChatter"]]:
"""获取启用的Chatter注册表"""
if not hasattr(self, '_enabled_chatter_registry'):
if not hasattr(self, "_enabled_chatter_registry"):
self._enabled_chatter_registry: Dict[str, Type[BaseChatter]] = {}
return self._enabled_chatter_registry.copy()
def get_registered_chatter_info(self, chatter_name: str) -> Optional['ChatterInfo']:
def get_registered_chatter_info(self, chatter_name: str) -> Optional["ChatterInfo"]:
"""获取Chatter信息"""
info = self.get_component_info(chatter_name, ComponentType.CHATTER)
return info if isinstance(info, ChatterInfo) else None
# === 插件查询方法 ===
def get_plugin_info(self, plugin_name: str) -> Optional['PluginInfo']:
def get_plugin_info(self, plugin_name: str) -> Optional["PluginInfo"]:
"""获取插件信息"""
return self._plugins.get(plugin_name)
def get_all_plugins(self) -> Dict[str, 'PluginInfo']:
def get_all_plugins(self) -> Dict[str, "PluginInfo"]:
"""获取所有插件"""
return self._plugins.copy()
@@ -724,7 +726,7 @@ class ComponentRegistry:
# """获取所有启用的插件"""
# return {name: info for name, info in self._plugins.items() if info.enabled}
def get_plugin_components(self, plugin_name: str) -> List['ComponentInfo']:
def get_plugin_components(self, plugin_name: str) -> List["ComponentInfo"]:
"""获取插件的所有组件"""
plugin_info = self.get_plugin_info(plugin_name)
return plugin_info.components if plugin_info else []

View File

@@ -95,17 +95,16 @@ class PermissionManager(IPermissionManager):
# 检查用户是否有明确的权限设置
result = await session.execute(
select(UserPermissions)
.filter_by(platform=user.platform, user_id=user.user_id, permission_node=permission_node)
select(UserPermissions).filter_by(
platform=user.platform, user_id=user.user_id, permission_node=permission_node
)
)
user_perm = result.scalar_one_or_none()
if user_perm:
# 有明确设置,返回设置的值
res = user_perm.granted
logger.debug(
f"用户 {user.platform}:{user.user_id} 对权限节点 {permission_node} 的明确设置: {res}"
)
logger.debug(f"用户 {user.platform}:{user.user_id} 对权限节点 {permission_node} 的明确设置: {res}")
return res
else:
# 没有明确设置,使用默认值
@@ -191,8 +190,9 @@ class PermissionManager(IPermissionManager):
# 检查是否已有权限记录
result = await session.execute(
select(UserPermissions)
.filter_by(platform=user.platform, user_id=user.user_id, permission_node=permission_node)
select(UserPermissions).filter_by(
platform=user.platform, user_id=user.user_id, permission_node=permission_node
)
)
existing_perm = result.scalar_one_or_none()
@@ -244,8 +244,9 @@ class PermissionManager(IPermissionManager):
# 检查是否已有权限记录
result = await session.execute(
select(UserPermissions)
.filter_by(platform=user.platform, user_id=user.user_id, permission_node=permission_node)
select(UserPermissions).filter_by(
platform=user.platform, user_id=user.user_id, permission_node=permission_node
)
)
existing_perm = result.scalar_one_or_none()
@@ -303,8 +304,9 @@ class PermissionManager(IPermissionManager):
for node in all_nodes:
# 检查用户是否有明确的权限设置
result = await session.execute(
select(UserPermissions)
.filter_by(platform=user.platform, user_id=user.user_id, permission_node=node.node_name)
select(UserPermissions).filter_by(
platform=user.platform, user_id=user.user_id, permission_node=node.node_name
)
)
user_perm = result.scalar_one_or_none()
@@ -408,8 +410,7 @@ class PermissionManager(IPermissionManager):
# 删除用户权限记录
result = await session.execute(
delete(UserPermissions)
.where(UserPermissions.permission_node.in_(node_names))
delete(UserPermissions).where(UserPermissions.permission_node.in_(node_names))
)
deleted_user_perms = result.rowcount

View File

@@ -1,7 +1,5 @@
import asyncio
import os
import shutil
import hashlib
import traceback
import importlib
@@ -106,7 +104,6 @@ class PluginManager:
if not plugin_dir:
return False, 1
plugin_instance = plugin_class(plugin_dir=plugin_dir) # 实例化插件可能因为缺少manifest而失败
if not plugin_instance:
logger.error(f"插件 {plugin_name} 实例化失败")
@@ -545,9 +542,7 @@ class PluginManager:
try:
loop = asyncio.get_event_loop()
if loop.is_running():
fut = asyncio.run_coroutine_threadsafe(
component_registry.unregister_plugin(plugin_name), loop
)
fut = asyncio.run_coroutine_threadsafe(component_registry.unregister_plugin(plugin_name), loop)
fut.result(timeout=5)
else:
asyncio.run(component_registry.unregister_plugin(plugin_name))

View File

@@ -116,17 +116,17 @@ class ToolExecutor:
def _get_tool_definitions(self) -> List[Dict[str, Any]]:
all_tools = get_llm_available_tool_definitions()
user_disabled_tools = global_announcement_manager.get_disabled_chat_tools(self.chat_id)
# 获取基础工具定义(包括二步工具的第一步)
tool_definitions = [definition for name, definition in all_tools if name not in user_disabled_tools]
# 检查是否有待处理的二步工具第二步调用
pending_step_two = getattr(self, '_pending_step_two_tools', {})
pending_step_two = getattr(self, "_pending_step_two_tools", {})
if pending_step_two:
# 添加第二步工具定义
for tool_name, step_two_def in pending_step_two.items():
tool_definitions.append(step_two_def)
return tool_definitions
async def execute_tool_calls(self, tool_calls: Optional[List[ToolCall]]) -> Tuple[List[Dict[str, Any]], List[str]]:
@@ -266,7 +266,7 @@ class ToolExecutor:
f"{self.log_prefix} 正在执行工具: [bold green]{function_name}[/bold green] | 参数: {function_args}"
)
function_args["llm_called"] = True # 标记为LLM调用
# 检查是否是二步工具的第二步调用
if "_" in function_name and function_name.count("_") >= 1:
# 可能是二步工具的第二步调用,格式为 "tool_name_sub_tool_name"
@@ -274,14 +274,14 @@ class ToolExecutor:
if len(parts) == 2:
base_tool_name, sub_tool_name = parts
base_tool_instance = get_tool_instance(base_tool_name)
if base_tool_instance and base_tool_instance.is_two_step_tool:
logger.info(f"{self.log_prefix}执行二步工具第二步: {base_tool_name}.{sub_tool_name}")
result = await base_tool_instance.execute_step_two(sub_tool_name, function_args)
# 清理待处理的第二步工具
self._pending_step_two_tools.pop(base_tool_name, None)
if result:
logger.debug(f"{self.log_prefix}二步工具第二步 {function_name} 执行成功")
return {
@@ -291,7 +291,7 @@ class ToolExecutor:
"type": "function",
"content": result.get("content", ""),
}
# 获取对应工具实例
tool_instance = tool_instance or get_tool_instance(function_name)
if not tool_instance:
@@ -301,7 +301,7 @@ class ToolExecutor:
# 执行工具并记录日志
logger.debug(f"{self.log_prefix}执行工具 {function_name},参数: {function_args}")
result = await tool_instance.execute(function_args)
# 检查是否是二步工具的第一步结果
if result and result.get("type") == "two_step_tool_step_one":
logger.info(f"{self.log_prefix}二步工具第一步完成: {function_name}")
@@ -310,7 +310,7 @@ class ToolExecutor:
if next_tool_def:
self._pending_step_two_tools[function_name] = next_tool_def
logger.debug(f"{self.log_prefix}已保存第二步工具定义: {next_tool_def['name']}")
if result:
logger.debug(f"{self.log_prefix}工具 {function_name} 执行成功,结果: {result}")
return {