refactor: 统一类型注解风格并优化代码结构

- 将裸 except 改为显式 Exception 捕获
- 用列表推导式替换冗余 for 循环
- 为类属性添加 ClassVar 注解
- 统一 Union/Optional 写法为 |
- 移除未使用的导入
- 修复 SQLAlchemy 空值比较语法
- 优化字符串拼接与字典更新逻辑
- 补充缺失的 noqa 注释与异常链

BREAKING CHANGE: 所有插件基类的类级字段现要求显式 ClassVar 注解,自定义插件需同步更新
This commit is contained in:
明天好像没什么
2025-10-31 22:42:39 +08:00
parent 5080cfccfc
commit 0e129d385e
105 changed files with 592 additions and 561 deletions

View File

@@ -4,7 +4,7 @@ Bilibili 视频观看体验工具
支持哔哩哔哩视频链接解析和AI视频内容分析
"""
from typing import Any
from typing import Any, ClassVar
from src.common.logger import get_logger
from src.plugin_system import BasePlugin, BaseTool, ComponentInfo, ConfigField, ToolParamType, register_plugin
@@ -21,7 +21,7 @@ class BilibiliTool(BaseTool):
description = "观看用户分享的哔哩哔哩视频,以真实用户视角给出观看感受和评价"
available_for_llm = True
parameters = [
parameters: ClassVar = [
(
"url",
ToolParamType.STRING,
@@ -166,7 +166,7 @@ class BilibiliTool(BaseTool):
return "(有点长,适合闲时观看)"
else:
return "(超长视频,需要耐心)"
except:
except Exception:
return ""
return ""
@@ -192,15 +192,15 @@ class BilibiliPlugin(BasePlugin):
# 插件基本信息
plugin_name: str = "bilibili_video_watcher"
enable_plugin: bool = True
dependencies: list[str] = []
python_dependencies: list[str] = []
dependencies: ClassVar[list[str] ] = []
python_dependencies: ClassVar[list[str] ] = []
config_file_name: str = "config.toml"
# 配置节描述
config_section_descriptions = {"plugin": "插件基本信息", "bilibili": "哔哩哔哩视频观看配置", "tool": "工具配置"}
config_section_descriptions: ClassVar[dict] = {"plugin": "插件基本信息", "bilibili": "哔哩哔哩视频观看配置", "tool": "工具配置"}
# 配置Schema定义
config_schema: dict = {
config_schema: ClassVar[dict] = {
"plugin": {
"name": ConfigField(type=str, default="bilibili_video_watcher", description="插件名称"),
"version": ConfigField(type=str, default="2.0.0", description="插件版本"),

View File

@@ -1,5 +1,5 @@
import random
from typing import Any
from typing import Any, ClassVar
from src.common.logger import get_logger
@@ -29,7 +29,7 @@ class StartupMessageHandler(BaseEventHandler):
handler_name = "hello_world_startup_handler"
handler_description = "在机器人启动时打印一条日志。"
init_subscribe = [EventType.ON_START]
init_subscribe: ClassVar[list[EventType]] = [EventType.ON_START]
async def execute(self, params: dict) -> HandlerResult:
logger.info("🎉 Hello World 插件已启动,准备就绪!")
@@ -42,7 +42,7 @@ class GetSystemInfoTool(BaseTool):
name = "get_system_info"
description = "获取当前系统的模拟版本和状态信息。"
available_for_llm = True
parameters = [
parameters: ClassVar = [
("query", ToolParamType.STRING, "要搜索的关键词或问题。", True, None),
("num_results", ToolParamType.INTEGER, "期望每个搜索引擎返回的搜索结果数量默认为5。", False, None),
(
@@ -63,7 +63,7 @@ class HelloCommand(PlusCommand):
command_name = "hello"
command_description = "向机器人发送一个简单的问候。"
command_aliases = ["hi", "你好"]
command_aliases: ClassVar[list[str]] = ["hi", "你好"]
chat_type_allow = ChatType.ALL
async def execute(self, args: CommandArgs) -> tuple[bool, str | None, bool]:
@@ -79,14 +79,14 @@ class HelloCommand(PlusCommand):
class KeywordActivationExampleAction(BaseAction):
"""关键词激活示例
此示例展示如何使用关键词匹配来激活 Action。
"""
action_name = "keyword_example"
action_description = "当检测到特定关键词时发送回应"
action_require = ["用户提到了问候语"]
associated_types = ["text"]
action_require: ClassVar[list[str]] = ["用户提到了问候语"]
associated_types: ClassVar[list[str]] = ["text"]
async def go_activate(self, chat_content: str = "", llm_judge_model=None) -> bool:
"""关键词激活:检测到"你好""hello""hi"时激活"""
@@ -103,14 +103,14 @@ class KeywordActivationExampleAction(BaseAction):
class LLMJudgeExampleAction(BaseAction):
"""LLM 判断激活示例
此示例展示如何使用 LLM 来智能判断是否激活 Action。
"""
action_name = "llm_judge_example"
action_description = "当用户表达情绪低落时提供安慰"
action_require = ["用户情绪低落", "需要情感支持"]
associated_types = ["text"]
action_require: ClassVar[list[str]] = ["用户情绪低落", "需要情感支持"]
associated_types: ClassVar[list[str]] = ["text"]
async def go_activate(self, chat_content: str = "", llm_judge_model=None) -> bool:
"""LLM 判断激活:判断用户是否情绪低落"""
@@ -133,14 +133,14 @@ class LLMJudgeExampleAction(BaseAction):
class CombinedActivationExampleAction(BaseAction):
"""组合激活条件示例
此示例展示如何组合多种激活条件。
"""
action_name = "combined_example"
action_description = "展示如何组合多种激活条件"
action_require = ["展示灵活的激活逻辑"]
associated_types = ["text"]
action_require: ClassVar[list[str]] = ["展示灵活的激活逻辑"]
associated_types: ClassVar[list[str]] = ["text"]
async def go_activate(self, chat_content: str = "", llm_judge_model=None) -> bool:
"""组合激活:随机 20% 概率,或者匹配特定关键词"""
@@ -162,18 +162,18 @@ class CombinedActivationExampleAction(BaseAction):
class RandomEmojiAction(BaseAction):
"""一个随机发送表情的动作。
此示例展示了如何使用新的 go_activate() 方法来实现随机激活。
"""
action_name = "random_emoji"
action_description = "随机发送一个表情符号,增加聊天的趣味性。"
action_require = ["当对话气氛轻松时", "可以用来回应简单的情感表达"]
associated_types = ["text"]
action_require: ClassVar[list[str]] = ["当对话气氛轻松时", "可以用来回应简单的情感表达"]
associated_types: ClassVar[list[str]] = ["text"]
async def go_activate(self, llm_judge_model=None) -> bool:
"""使用新的激活方式10% 的概率激活
注意:不需要传入 chat_content会自动从实例属性中获取
"""
return await self._random_activation(0.1)
@@ -189,7 +189,7 @@ class WeatherPrompt(BasePrompt):
prompt_name = "weather_info_prompt"
prompt_description = "向Planner注入当前天气信息以丰富对话上下文。"
injection_rules = [InjectionRule(target_prompt="planner_prompt", injection_type=InjectionType.REPLACE, target_content="## 可用动作列表")]
injection_rules: ClassVar[list[InjectionRule]] = [InjectionRule(target_prompt="planner_prompt", injection_type=InjectionType.REPLACE, target_content="## 可用动作列表")]
async def execute(self) -> str:
# 在实际应用中这里可以调用天气API
@@ -203,11 +203,11 @@ class HelloWorldPlugin(BasePlugin):
plugin_name = "hello_world_plugin"
enable_plugin = True
dependencies = []
python_dependencies = []
dependencies: ClassVar = []
python_dependencies: ClassVar = []
config_file_name = "config.toml"
config_schema = {
config_schema: ClassVar = {
"meta": {
"config_version": ConfigField(type=int, default=1, description="配置文件版本,请勿手动修改。"),
},
@@ -224,7 +224,7 @@ class HelloWorldPlugin(BasePlugin):
def get_plugin_components(self) -> list[tuple[ComponentInfo, type]]:
"""根据配置文件动态注册插件的功能组件。"""
components: list[tuple[ComponentInfo, type]] = []
components: ClassVar[list[tuple[ComponentInfo, type]] ] = []
components.append((StartupMessageHandler.get_handler_info(), StartupMessageHandler))
components.append((GetSystemInfoTool.get_tool_info(), GetSystemInfoTool))