部分类型注解修复,优化import顺序,删除无用API文件

This commit is contained in:
UnCLAS-Prommer
2025-07-12 00:34:49 +08:00
parent 3165a0f8df
commit b303a95f61
44 changed files with 405 additions and 1166 deletions

View File

@@ -32,10 +32,10 @@ class BaseAction(ABC):
reasoning: str,
cycle_timers: dict,
thinking_id: str,
chat_stream: ChatStream = None,
chat_stream: ChatStream,
log_prefix: str = "",
shutting_down: bool = False,
plugin_config: dict = None,
plugin_config: Optional[dict] = None,
**kwargs,
):
"""初始化Action组件

View File

@@ -29,7 +29,7 @@ class BaseCommand(ABC):
command_examples: List[str] = []
intercept_message: bool = True # 默认拦截消息,不继续处理
def __init__(self, message: MessageRecv, plugin_config: dict = None):
def __init__(self, message: MessageRecv, plugin_config: Optional[dict] = None):
"""初始化Command组件
Args:

View File

@@ -66,7 +66,7 @@ class BasePlugin(ABC):
config_section_descriptions: Dict[str, str] = {}
def __init__(self, plugin_dir: str = None):
def __init__(self, plugin_dir: str):
"""初始化插件
Args:
@@ -526,7 +526,7 @@ class BasePlugin(ABC):
# 从配置中更新 enable_plugin
if "plugin" in self.config and "enabled" in self.config["plugin"]:
self.enable_plugin = self.config["plugin"]["enabled"]
self.enable_plugin = self.config["plugin"]["enabled"] # type: ignore
logger.debug(f"{self.log_prefix} 从配置更新插件启用状态: {self.enable_plugin}")
else:
logger.warning(f"{self.log_prefix} 不支持的配置文件格式: {file_ext},仅支持 .toml")

View File

@@ -81,7 +81,9 @@ class ComponentInfo:
class ActionInfo(ComponentInfo):
"""动作组件信息"""
action_parameters: Dict[str, str] = field(default_factory=dict) # 动作参数与描述,例如 {"param1": "描述1", "param2": "描述2"}
action_parameters: Dict[str, str] = field(
default_factory=dict
) # 动作参数与描述,例如 {"param1": "描述1", "param2": "描述2"}
action_require: List[str] = field(default_factory=list) # 动作需求说明
associated_types: List[str] = field(default_factory=list) # 关联的消息类型
# 激活类型相关

View File

@@ -1,4 +1,4 @@
from typing import Dict, List, Optional, Any, Pattern, Union
from typing import Dict, List, Optional, Any, Pattern, Tuple, Union, Type
import re
from src.common.logger import get_logger
from src.plugin_system.base.component_types import (
@@ -28,25 +28,25 @@ class ComponentRegistry:
ComponentType.ACTION: {},
ComponentType.COMMAND: {},
}
self._component_classes: Dict[str, Union[BaseCommand, BaseAction]] = {} # 组件名 -> 组件类
self._component_classes: Dict[str, Union[Type[BaseCommand], Type[BaseAction]]] = {} # 组件名 -> 组件类
# 插件注册表
self._plugins: Dict[str, PluginInfo] = {} # 插件名 -> 插件信息
# Action特定注册表
self._action_registry: Dict[str, BaseAction] = {} # action名 -> action类
# self._action_descriptions: Dict[str, str] = {} # 启用的action名 -> 描述
self._action_registry: Dict[str, Type[BaseAction]] = {} # action名 -> action类
self._default_actions: Dict[str, ActionInfo] = {} # 默认动作集即启用的Action集用于重置ActionManager状态
# Command特定注册表
self._command_registry: Dict[str, BaseCommand] = {} # command名 -> command类
self._command_patterns: Dict[Pattern, BaseCommand] = {} # 编译后的正则 -> command类
self._command_registry: Dict[str, Type[BaseCommand]] = {} # command名 -> command类
self._command_patterns: Dict[Pattern, Type[BaseCommand]] = {} # 编译后的正则 -> command类
logger.info("组件注册中心初始化完成")
# === 通用组件注册方法 ===
def register_component(
self, component_info: ComponentInfo, component_class: Union[BaseCommand, BaseAction]
self, component_info: ComponentInfo, component_class: Union[Type[BaseCommand], Type[BaseAction]]
) -> bool:
"""注册组件
@@ -88,9 +88,9 @@ class ComponentRegistry:
# 根据组件类型进行特定注册(使用原始名称)
if component_type == ComponentType.ACTION:
self._register_action_component(component_info, component_class)
self._register_action_component(component_info, component_class) # type: ignore
elif component_type == ComponentType.COMMAND:
self._register_command_component(component_info, component_class)
self._register_command_component(component_info, component_class) # type: ignore
logger.debug(
f"已注册{component_type.value}组件: '{component_name}' -> '{namespaced_name}' "
@@ -98,7 +98,7 @@ class ComponentRegistry:
)
return True
def _register_action_component(self, action_info: ActionInfo, action_class: BaseAction):
def _register_action_component(self, action_info: ActionInfo, action_class: Type[BaseAction]):
# -------------------------------- NEED REFACTORING --------------------------------
# -------------------------------- LOGIC ERROR -------------------------------------
"""注册Action组件到Action特定注册表"""
@@ -106,11 +106,10 @@ class ComponentRegistry:
self._action_registry[action_name] = action_class
# 如果启用,添加到默认动作集
# ---- HERE ----
# if action_info.enabled:
# self._action_descriptions[action_name] = action_info.description
if action_info.enabled:
self._default_actions[action_name] = action_info
def _register_command_component(self, command_info: CommandInfo, command_class: BaseCommand):
def _register_command_component(self, command_info: CommandInfo, command_class: Type[BaseCommand]):
"""注册Command组件到Command特定注册表"""
command_name = command_info.name
self._command_registry[command_name] = command_class
@@ -122,7 +121,7 @@ class ComponentRegistry:
# === 组件查询方法 ===
def get_component_info(self, component_name: str, component_type: ComponentType = None) -> Optional[ComponentInfo]:
def get_component_info(self, component_name: str, component_type: ComponentType = None) -> Optional[ComponentInfo]: # type: ignore
# sourcery skip: class-extract-method
"""获取组件信息,支持自动命名空间解析
@@ -170,8 +169,10 @@ class ComponentRegistry:
return None
def get_component_class(
self, component_name: str, component_type: ComponentType = None
) -> Optional[Union[BaseCommand, BaseAction]]:
self,
component_name: str,
component_type: ComponentType = None, # type: ignore
) -> Optional[Union[Type[BaseCommand], Type[BaseAction]]]:
"""获取组件类,支持自动命名空间解析
Args:
@@ -230,7 +231,7 @@ class ComponentRegistry:
# === Action特定查询方法 ===
def get_action_registry(self) -> Dict[str, BaseAction]:
def get_action_registry(self) -> Dict[str, Type[BaseAction]]:
"""获取Action注册表用于兼容现有系统"""
return self._action_registry.copy()
@@ -239,13 +240,17 @@ class ComponentRegistry:
info = self.get_component_info(action_name, ComponentType.ACTION)
return info if isinstance(info, ActionInfo) else None
def get_default_actions(self) -> Dict[str, ActionInfo]:
"""获取默认动作集"""
return self._default_actions.copy()
# === Command特定查询方法 ===
def get_command_registry(self) -> Dict[str, BaseCommand]:
def get_command_registry(self) -> Dict[str, Type[BaseCommand]]:
"""获取Command注册表用于兼容现有系统"""
return self._command_registry.copy()
def get_command_patterns(self) -> Dict[Pattern, BaseCommand]:
def get_command_patterns(self) -> Dict[Pattern, Type[BaseCommand]]:
"""获取Command模式注册表用于兼容现有系统"""
return self._command_patterns.copy()
@@ -254,7 +259,7 @@ class ComponentRegistry:
info = self.get_component_info(command_name, ComponentType.COMMAND)
return info if isinstance(info, CommandInfo) else None
def find_command_by_text(self, text: str) -> Optional[tuple[BaseCommand, dict, bool, str]]:
def find_command_by_text(self, text: str) -> Optional[Tuple[Type[BaseCommand], dict, bool, str]]:
# sourcery skip: use-named-expression, use-next
"""根据文本查找匹配的命令
@@ -262,7 +267,7 @@ class ComponentRegistry:
text: 输入文本
Returns:
Optional[tuple[BaseCommand, dict, bool, str]]: (命令类, 匹配的命名组, 是否拦截消息, 插件名) 或 None
Tuple: (命令类, 匹配的命名组, 是否拦截消息, 插件名) 或 None
"""
for pattern, command_class in self._command_patterns.items():