chore: 代码格式化与类型注解优化
对项目中的多个文件进行了代码风格调整和类型注解更新。
- 使用 ruff 工具对代码进行自动格式化,主要包括:
- 统一 import 语句的顺序和风格。
- 移除未使用的 import。
- 调整代码间距和空行。
- 将部分 `Optional[str]` 和 `List[T]` 等旧式类型注解更新为现代的 `str | None` 和 `list[T]` 语法。
- 修复了一些小的代码风格问题,例如将 `f'...'` 更改为 `f"..."`。
This commit is contained in:
committed by
Windpicker-owo
parent
9380231019
commit
f1dfe64f88
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import traceback
|
||||
from typing import Any
|
||||
|
||||
@@ -12,7 +11,7 @@ from src.chat.message_manager import message_manager
|
||||
from src.chat.message_receive.chat_stream import ChatStream, get_chat_manager
|
||||
from src.chat.message_receive.message import MessageRecv, MessageRecvS4U
|
||||
from src.chat.message_receive.storage import MessageStorage
|
||||
from src.chat.utils.prompt import Prompt, global_prompt_manager, create_prompt_async
|
||||
from src.chat.utils.prompt import create_prompt_async, global_prompt_manager
|
||||
from src.chat.utils.utils import is_mentioned_bot_in_message
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
@@ -321,12 +320,12 @@ class ChatBot:
|
||||
else:
|
||||
logger.debug("notice消息触发聊天流程(配置已开启)")
|
||||
return False # 返回False表示继续处理,触发聊天流程
|
||||
|
||||
|
||||
# 兼容旧的notice判断方式
|
||||
if message.message_info.message_id == "notice":
|
||||
message.is_notify = True
|
||||
logger.info("旧格式notice消息")
|
||||
|
||||
|
||||
# 同样根据配置决定
|
||||
if not global_config.notice.enable_notice_trigger_chat:
|
||||
return True
|
||||
@@ -481,17 +480,18 @@ class ChatBot:
|
||||
if notice_handled:
|
||||
# notice消息已处理,需要先添加到message_manager再存储
|
||||
try:
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
import time
|
||||
|
||||
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
|
||||
message_info = message.message_info
|
||||
msg_user_info = getattr(message_info, "user_info", None)
|
||||
stream_user_info = getattr(message.chat_stream, "user_info", None)
|
||||
group_info = getattr(message.chat_stream, "group_info", None)
|
||||
|
||||
|
||||
message_id = message_info.message_id or ""
|
||||
message_time = message_info.time if message_info.time is not None else time.time()
|
||||
|
||||
|
||||
user_id = ""
|
||||
user_nickname = ""
|
||||
user_cardname = None
|
||||
@@ -506,16 +506,16 @@ class ChatBot:
|
||||
user_nickname = getattr(stream_user_info, "user_nickname", "") or ""
|
||||
user_cardname = getattr(stream_user_info, "user_cardname", None)
|
||||
user_platform = getattr(stream_user_info, "platform", "") or ""
|
||||
|
||||
|
||||
chat_user_id = str(getattr(stream_user_info, "user_id", "") or "")
|
||||
chat_user_nickname = getattr(stream_user_info, "user_nickname", "") or ""
|
||||
chat_user_cardname = getattr(stream_user_info, "user_cardname", None)
|
||||
chat_user_platform = getattr(stream_user_info, "platform", "") or ""
|
||||
|
||||
|
||||
group_id = getattr(group_info, "group_id", None)
|
||||
group_name = getattr(group_info, "group_name", None)
|
||||
group_platform = getattr(group_info, "platform", None)
|
||||
|
||||
|
||||
# 构建additional_config,确保包含is_notice标志
|
||||
import json
|
||||
additional_config_dict = {
|
||||
@@ -523,9 +523,9 @@ class ChatBot:
|
||||
"notice_type": message.notice_type or "unknown",
|
||||
"is_public_notice": bool(message.is_public_notice),
|
||||
}
|
||||
|
||||
|
||||
# 如果message_info有additional_config,合并进来
|
||||
if hasattr(message_info, 'additional_config') and message_info.additional_config:
|
||||
if hasattr(message_info, "additional_config") and message_info.additional_config:
|
||||
if isinstance(message_info.additional_config, dict):
|
||||
additional_config_dict.update(message_info.additional_config)
|
||||
elif isinstance(message_info.additional_config, str):
|
||||
@@ -534,9 +534,9 @@ class ChatBot:
|
||||
additional_config_dict.update(existing_config)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
additional_config_json = json.dumps(additional_config_dict)
|
||||
|
||||
|
||||
# 创建数据库消息对象
|
||||
db_message = DatabaseMessages(
|
||||
message_id=message_id,
|
||||
@@ -564,14 +564,14 @@ class ChatBot:
|
||||
chat_info_group_name=group_name,
|
||||
chat_info_group_platform=group_platform,
|
||||
)
|
||||
|
||||
|
||||
# 添加到message_manager(这会将notice添加到全局notice管理器)
|
||||
await message_manager.add_message(message.chat_stream.stream_id, db_message)
|
||||
logger.info(f"✅ Notice消息已添加到message_manager: type={message.notice_type}, stream={message.chat_stream.stream_id}")
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Notice消息添加到message_manager失败: {e}", exc_info=True)
|
||||
|
||||
|
||||
# 存储后直接返回
|
||||
await MessageStorage.store_message(message, chat)
|
||||
logger.debug("notice消息已存储,跳过后续处理")
|
||||
@@ -622,9 +622,10 @@ class ChatBot:
|
||||
template_group_name = None
|
||||
|
||||
async def preprocess():
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
import time
|
||||
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
|
||||
message_info = message.message_info
|
||||
msg_user_info = getattr(message_info, "user_info", None)
|
||||
stream_user_info = getattr(message.chat_stream, "user_info", None)
|
||||
|
||||
@@ -133,7 +133,7 @@ class MessageRecv(Message):
|
||||
|
||||
self.key_words = []
|
||||
self.key_words_lite = []
|
||||
|
||||
|
||||
# 解析additional_config中的notice信息
|
||||
if self.message_info.additional_config and isinstance(self.message_info.additional_config, dict):
|
||||
self.is_notify = self.message_info.additional_config.get("is_notice", False)
|
||||
|
||||
@@ -198,7 +198,7 @@ class MessageStorage:
|
||||
async def replace_image_descriptions(text: str) -> str:
|
||||
"""异步地将文本中的所有[图片:描述]标记替换为[picid:image_id]"""
|
||||
pattern = r"\[图片:([^\]]+)\]"
|
||||
|
||||
|
||||
# 如果没有匹配项,提前返回以提高效率
|
||||
if not re.search(pattern, text):
|
||||
return text
|
||||
@@ -209,7 +209,7 @@ class MessageStorage:
|
||||
for match in re.finditer(pattern, text):
|
||||
# 添加上一个匹配到当前匹配之间的文本
|
||||
new_text.append(text[last_end:match.start()])
|
||||
|
||||
|
||||
description = match.group(1).strip()
|
||||
replacement = match.group(0) # 默认情况下,替换为原始匹配文本
|
||||
try:
|
||||
@@ -236,7 +236,7 @@ class MessageStorage:
|
||||
|
||||
# 添加最后一个匹配到字符串末尾的文本
|
||||
new_text.append(text[last_end:])
|
||||
|
||||
|
||||
return "".join(new_text)
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user