style: 统一代码风格并采用现代化类型注解

对整个代码库进行了一次全面的代码风格清理和现代化改造,主要包括:

- 移除了所有文件中多余的行尾空格。
- 将类型提示更新为 PEP 585 和 PEP 604 引入的现代语法(例如,使用 `list` 代替 `List`,使用 `|` 代替 `Optional`)。
- 清理了多个模块中未被使用的导入语句。
- 移除了不含插值变量的冗余 f-string。
- 调整了部分 `__init__.py` 文件中的 `__all__` 导出顺序,以保持一致性。

这些改动旨在提升代码的可读性和可维护性,使其与现代 Python 最佳实践保持一致,但未修改任何核心逻辑。
This commit is contained in:
minecraft1024a
2025-11-12 12:49:40 +08:00
parent daf8ea7e6a
commit 0e1e9935b2
33 changed files with 227 additions and 229 deletions

View File

@@ -362,7 +362,7 @@ class ChatterPlanFilter:
return "最近没有聊天内容。", "没有未读消息。", []
stream_context = chat_stream.context_manager
# 获取真正的已读和未读消息
read_messages = stream_context.context.history_messages # 已读消息存储在history_messages中
if not read_messages:
@@ -652,30 +652,30 @@ class ChatterPlanFilter:
if not action_info:
logger.debug(f"动作 {action_name} 不在可用动作列表中,保留所有参数")
return action_data
# 获取该动作定义的合法参数
defined_params = set(action_info.action_parameters.keys())
# 合法参数集合
valid_params = defined_params
# 过滤参数
filtered_data = {}
removed_params = []
for key, value in action_data.items():
if key in valid_params:
filtered_data[key] = value
else:
removed_params.append(key)
# 记录被移除的参数
if removed_params:
logger.info(
f"🧹 [参数过滤] 动作 '{action_name}' 移除了多余参数: {removed_params}. "
f"合法参数: {sorted(valid_params)}"
)
return filtered_data
def _filter_no_actions(self, action_list: list[ActionPlannerInfo]) -> list[ActionPlannerInfo]:

View File

@@ -545,14 +545,14 @@ async def execute_proactive_thinking(stream_id: str):
# 获取或创建该聊天流的执行锁
if stream_id not in _execution_locks:
_execution_locks[stream_id] = asyncio.Lock()
lock = _execution_locks[stream_id]
# 尝试获取锁,如果已被占用则跳过本次执行(防止重复)
if lock.locked():
logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 已有正在执行的主动思考任务")
return
async with lock:
logger.debug(f"🤔 开始主动思考 {stream_id}")
@@ -563,13 +563,13 @@ async def execute_proactive_thinking(stream_id: str):
from src.chat.message_receive.chat_stream import get_chat_manager
chat_manager = get_chat_manager()
chat_stream = await chat_manager.get_stream(stream_id)
if chat_stream and chat_stream.context_manager.context.is_chatter_processing:
logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 的 chatter 正在处理消息")
return
except Exception as e:
logger.warning(f"检查 chatter 处理状态时出错: {e},继续执行")
# 0.1 检查白名单/黑名单
# 从 stream_id 获取 stream_config 字符串进行验证
try:

View File

@@ -31,4 +31,4 @@ __plugin_meta__ = PluginMetadata(
# 导入插件主类
from .plugin import AntiInjectionPlugin
__all__ = ["__plugin_meta__", "AntiInjectionPlugin"]
__all__ = ["AntiInjectionPlugin", "__plugin_meta__"]

View File

@@ -8,8 +8,8 @@ import time
from src.chat.security.interfaces import (
SecurityAction,
SecurityCheckResult,
SecurityChecker,
SecurityCheckResult,
SecurityLevel,
)
from src.common.logger import get_logger

View File

@@ -4,7 +4,7 @@
处理检测结果,执行相应的动作(允许/监控/加盾/阻止/反击)。
"""
from src.chat.security.interfaces import SecurityAction, SecurityCheckResult
from src.chat.security.interfaces import SecurityCheckResult
from src.common.logger import get_logger
from .counter_attack import CounterAttackGenerator

View File

@@ -64,15 +64,15 @@ class CoreActionsPlugin(BasePlugin):
# --- 根据配置注册组件 ---
components: ClassVar = []
# 注册 reply 动作
if self.get_config("components.enable_reply", True):
components.append((ReplyAction.get_action_info(), ReplyAction))
# 注册 respond 动作
if self.get_config("components.enable_respond", True):
components.append((RespondAction.get_action_info(), RespondAction))
# 注册 emoji 动作
if self.get_config("components.enable_emoji", True):
components.append((EmojiAction.get_action_info(), EmojiAction))

View File

@@ -22,23 +22,23 @@ class ReplyAction(BaseAction):
- 专注于理解和回应单条消息的具体内容
- 适合 Focus 模式下的精准回复
"""
# 动作基本信息
action_name = "reply"
action_description = "针对特定消息进行精准回复。深度理解并回应单条消息的具体内容。需要指定目标消息ID。"
# 激活设置
activation_type = ActionActivationType.ALWAYS # 回复动作总是可用
mode_enable = ChatMode.ALL # 在所有模式下都可用
parallel_action = False # 回复动作不能与其他动作并行
# 动作参数定义
action_parameters: ClassVar = {
"target_message_id": "要回复的目标消息ID必需来自未读消息的 <m...> 标签)",
"content": "回复的具体内容可选由LLM生成",
"should_quote_reply": "是否引用原消息可选true/false默认false。群聊中回复较早消息或需要明确指向时使用true",
}
# 动作使用场景
action_require: ClassVar = [
"需要针对特定消息进行精准回复时使用",
@@ -48,10 +48,10 @@ class ReplyAction(BaseAction):
"群聊中需要明确回应某个特定用户或问题时使用",
"关注单条消息的具体内容和上下文细节",
]
# 关联类型
associated_types: ClassVar[list[str]] = ["text"]
async def execute(self) -> tuple[bool, str]:
"""执行reply动作
@@ -70,21 +70,21 @@ class RespondAction(BaseAction):
- 适合对于群聊消息下的宏观回应
- 避免与单一用户深度对话而忽略其他用户的消息
"""
# 动作基本信息
action_name = "respond"
action_description = "统一回应所有未读消息。理解整体对话动态和话题走向,生成连贯的回复。无需指定目标消息。"
# 激活设置
activation_type = ActionActivationType.ALWAYS # 回应动作总是可用
mode_enable = ChatMode.ALL # 在所有模式下都可用
parallel_action = False # 回应动作不能与其他动作并行
# 动作参数定义
action_parameters: ClassVar = {
"content": "回复的具体内容可选由LLM生成",
}
# 动作使用场景
action_require: ClassVar = [
"需要统一回应多条未读消息时使用Normal 模式专用)",
@@ -94,10 +94,10 @@ class RespondAction(BaseAction):
"适合群聊中的自然对话流,无需精确指向特定消息",
"可以同时回应多个话题或参与者",
]
# 关联类型
associated_types: ClassVar[list[str]] = ["text"]
async def execute(self) -> tuple[bool, str]:
"""执行respond动作

View File

@@ -6,10 +6,10 @@
import asyncio
import base64
import datetime
import filetype
from collections.abc import Callable
import aiohttp
import filetype
from maim_message import UserInfo
from src.chat.message_receive.chat_stream import get_chat_manager