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
@@ -3,12 +3,12 @@
|
||||
用于统一管理所有notice消息,将notice与正常消息分离
|
||||
"""
|
||||
|
||||
import time
|
||||
import threading
|
||||
import time
|
||||
from collections import defaultdict, deque
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Dict, List, Optional, Any
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
from src.common.data_models.database_data_model import DatabaseMessages
|
||||
from src.common.logger import get_logger
|
||||
@@ -27,7 +27,7 @@ class NoticeMessage:
|
||||
"""Notice消息数据结构"""
|
||||
message: DatabaseMessages
|
||||
scope: NoticeScope
|
||||
target_stream_id: Optional[str] = None # 如果是STREAM类型,指定目标流ID
|
||||
target_stream_id: str | None = None # 如果是STREAM类型,指定目标流ID
|
||||
timestamp: float = field(default_factory=time.time)
|
||||
ttl: int = 3600 # 默认1小时过期
|
||||
|
||||
@@ -56,11 +56,11 @@ class GlobalNoticeManager:
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
if hasattr(self, '_initialized'):
|
||||
if hasattr(self, "_initialized"):
|
||||
return
|
||||
|
||||
self._initialized = True
|
||||
self._notices: Dict[str, deque[NoticeMessage]] = defaultdict(deque)
|
||||
self._notices: dict[str, deque[NoticeMessage]] = defaultdict(deque)
|
||||
self._max_notices_per_type = 100 # 每种类型最大存储数量
|
||||
self._cleanup_interval = 300 # 5分钟清理一次过期消息
|
||||
self._last_cleanup_time = time.time()
|
||||
@@ -80,8 +80,8 @@ class GlobalNoticeManager:
|
||||
self,
|
||||
message: DatabaseMessages,
|
||||
scope: NoticeScope = NoticeScope.STREAM,
|
||||
target_stream_id: Optional[str] = None,
|
||||
ttl: Optional[int] = None
|
||||
target_stream_id: str | None = None,
|
||||
ttl: int | None = None
|
||||
) -> bool:
|
||||
"""添加notice消息
|
||||
|
||||
@@ -142,7 +142,7 @@ class GlobalNoticeManager:
|
||||
logger.error(f"添加notice消息失败: {e}")
|
||||
return False
|
||||
|
||||
def get_accessible_notices(self, stream_id: str, limit: int = 20) -> List[NoticeMessage]:
|
||||
def get_accessible_notices(self, stream_id: str, limit: int = 20) -> list[NoticeMessage]:
|
||||
"""获取指定聊天流可访问的notice消息
|
||||
|
||||
Args:
|
||||
@@ -231,7 +231,7 @@ class GlobalNoticeManager:
|
||||
logger.error(f"获取notice文本失败: {e}", exc_info=True)
|
||||
return ""
|
||||
|
||||
def clear_notices(self, stream_id: Optional[str] = None, notice_type: Optional[str] = None) -> int:
|
||||
def clear_notices(self, stream_id: str | None = None, notice_type: str | None = None) -> int:
|
||||
"""清理notice消息
|
||||
|
||||
Args:
|
||||
@@ -289,14 +289,14 @@ class GlobalNoticeManager:
|
||||
logger.error(f"清理notice消息失败: {e}")
|
||||
return 0
|
||||
|
||||
def get_stats(self) -> Dict[str, Any]:
|
||||
def get_stats(self) -> dict[str, Any]:
|
||||
"""获取统计信息"""
|
||||
# 更新实时统计
|
||||
total_active_notices = sum(len(notices) for notices in self._notices.values())
|
||||
self.stats["total_notices"] = total_active_notices
|
||||
self.stats["active_keys"] = len(self._notices)
|
||||
self.stats["last_cleanup_time"] = int(self._last_cleanup_time)
|
||||
|
||||
|
||||
# 添加详细的存储键信息
|
||||
storage_keys_info = {}
|
||||
for key, notices in self._notices.items():
|
||||
@@ -313,11 +313,11 @@ class GlobalNoticeManager:
|
||||
"""检查消息是否为notice类型"""
|
||||
try:
|
||||
# 首先检查消息的is_notify字段
|
||||
if hasattr(message, 'is_notify') and message.is_notify:
|
||||
if hasattr(message, "is_notify") and message.is_notify:
|
||||
return True
|
||||
|
||||
# 检查消息的附加配置
|
||||
if hasattr(message, 'additional_config') and message.additional_config:
|
||||
if hasattr(message, "additional_config") and message.additional_config:
|
||||
if isinstance(message.additional_config, dict):
|
||||
return message.additional_config.get("is_notice", False)
|
||||
elif isinstance(message.additional_config, str):
|
||||
@@ -333,7 +333,7 @@ class GlobalNoticeManager:
|
||||
logger.debug(f"检查notice类型失败: {e}")
|
||||
return False
|
||||
|
||||
def _get_storage_key(self, scope: NoticeScope, target_stream_id: Optional[str], message: DatabaseMessages) -> str:
|
||||
def _get_storage_key(self, scope: NoticeScope, target_stream_id: str | None, message: DatabaseMessages) -> str:
|
||||
"""生成存储键"""
|
||||
if scope == NoticeScope.PUBLIC:
|
||||
return "public"
|
||||
@@ -341,10 +341,10 @@ class GlobalNoticeManager:
|
||||
notice_type = self._get_notice_type(message) or "default"
|
||||
return f"stream_{target_stream_id}_{notice_type}"
|
||||
|
||||
def _get_notice_type(self, message: DatabaseMessages) -> Optional[str]:
|
||||
def _get_notice_type(self, message: DatabaseMessages) -> str | None:
|
||||
"""获取notice类型"""
|
||||
try:
|
||||
if hasattr(message, 'additional_config') and message.additional_config:
|
||||
if hasattr(message, "additional_config") and message.additional_config:
|
||||
if isinstance(message.additional_config, dict):
|
||||
return message.additional_config.get("notice_type")
|
||||
elif isinstance(message.additional_config, str):
|
||||
@@ -397,4 +397,4 @@ class GlobalNoticeManager:
|
||||
|
||||
|
||||
# 创建全局单例实例
|
||||
global_notice_manager = GlobalNoticeManager()
|
||||
global_notice_manager = GlobalNoticeManager()
|
||||
|
||||
@@ -7,7 +7,7 @@ import asyncio
|
||||
import random
|
||||
import time
|
||||
from collections import defaultdict, deque
|
||||
from typing import TYPE_CHECKING, Any, Dict, Optional
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from src.chat.chatter_manager import ChatterManager
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
@@ -19,8 +19,8 @@ from src.config.config import global_config
|
||||
from src.plugin_system.apis.chat_api import get_chat_manager
|
||||
|
||||
from .distribution_manager import stream_loop_manager
|
||||
from .global_notice_manager import NoticeScope, global_notice_manager
|
||||
from .sleep_system.state_manager import SleepState, sleep_state_manager
|
||||
from .global_notice_manager import global_notice_manager, NoticeScope
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
@@ -162,7 +162,7 @@ class MessageManager:
|
||||
# Notice消息处理 - 添加到全局管理器
|
||||
logger.info(f"📢 检测到notice消息: message_id={message.message_id}, is_notify={message.is_notify}, notice_type={getattr(message, 'notice_type', None)}")
|
||||
await self._handle_notice_message(stream_id, message)
|
||||
|
||||
|
||||
# 根据配置决定是否继续处理(触发聊天流程)
|
||||
if not global_config.notice.enable_notice_trigger_chat:
|
||||
logger.info(f"根据配置,流 {stream_id} 的Notice消息将被忽略,不触发聊天流程。")
|
||||
@@ -665,11 +665,11 @@ class MessageManager:
|
||||
"""检查消息是否为notice类型"""
|
||||
try:
|
||||
# 首先检查消息的is_notify字段
|
||||
if hasattr(message, 'is_notify') and message.is_notify:
|
||||
if hasattr(message, "is_notify") and message.is_notify:
|
||||
return True
|
||||
|
||||
# 检查消息的附加配置
|
||||
if hasattr(message, 'additional_config') and message.additional_config:
|
||||
if hasattr(message, "additional_config") and message.additional_config:
|
||||
if isinstance(message.additional_config, dict):
|
||||
return message.additional_config.get("is_notice", False)
|
||||
elif isinstance(message.additional_config, str):
|
||||
@@ -715,7 +715,7 @@ class MessageManager:
|
||||
"""
|
||||
try:
|
||||
# 检查附加配置中的公共notice标志
|
||||
if hasattr(message, 'additional_config') and message.additional_config:
|
||||
if hasattr(message, "additional_config") and message.additional_config:
|
||||
if isinstance(message.additional_config, dict):
|
||||
is_public = message.additional_config.get("is_public_notice", False)
|
||||
elif isinstance(message.additional_config, str):
|
||||
@@ -736,10 +736,10 @@ class MessageManager:
|
||||
logger.debug(f"确定notice作用域失败: {e}")
|
||||
return NoticeScope.STREAM
|
||||
|
||||
def _get_notice_type(self, message: DatabaseMessages) -> Optional[str]:
|
||||
def _get_notice_type(self, message: DatabaseMessages) -> str | None:
|
||||
"""获取notice类型"""
|
||||
try:
|
||||
if hasattr(message, 'additional_config') and message.additional_config:
|
||||
if hasattr(message, "additional_config") and message.additional_config:
|
||||
if isinstance(message.additional_config, dict):
|
||||
return message.additional_config.get("notice_type")
|
||||
elif isinstance(message.additional_config, str):
|
||||
@@ -780,7 +780,7 @@ class MessageManager:
|
||||
logger.error(f"获取notice文本失败: {e}")
|
||||
return ""
|
||||
|
||||
def clear_notices(self, stream_id: Optional[str] = None, notice_type: Optional[str] = None) -> int:
|
||||
def clear_notices(self, stream_id: str | None = None, notice_type: str | None = None) -> int:
|
||||
"""清理notice消息"""
|
||||
try:
|
||||
return self.notice_manager.clear_notices(stream_id, notice_type)
|
||||
@@ -788,7 +788,7 @@ class MessageManager:
|
||||
logger.error(f"清理notice失败: {e}")
|
||||
return 0
|
||||
|
||||
def get_notice_stats(self) -> Dict[str, Any]:
|
||||
def get_notice_stats(self) -> dict[str, Any]:
|
||||
"""获取notice管理器统计信息"""
|
||||
try:
|
||||
return self.notice_manager.get_stats()
|
||||
|
||||
Reference in New Issue
Block a user