chore: 代码格式化与类型注解优化

对项目中的多个文件进行了代码风格调整和类型注解更新。

- 使用 ruff 工具对代码进行自动格式化,主要包括:
    - 统一 import 语句的顺序和风格。
    - 移除未使用的 import。
    - 调整代码间距和空行。
- 将部分 `Optional[str]` 和 `List[T]` 等旧式类型注解更新为现代的 `str | None` 和 `list[T]` 语法。
- 修复了一些小的代码风格问题,例如将 `f'...'` 更改为 `f"..."`。
This commit is contained in:
minecraft1024a
2025-10-24 19:08:32 +08:00
committed by Windpicker-owo
parent 9380231019
commit f1dfe64f88
27 changed files with 100 additions and 101 deletions

View File

@@ -547,7 +547,7 @@ async def _build_readable_messages_internal(
if pic_id_mapping is None:
pic_id_mapping = {}
current_pic_counter = pic_counter
# --- 异步图片ID处理器 (修复核心问题) ---
async def process_pic_ids(content: str) -> str:
"""异步处理内容中的图片ID将其直接替换为[图片:描述]格式"""
@@ -979,7 +979,7 @@ async def build_readable_messages(
return ""
copy_messages = [msg.copy() for msg in messages]
if not copy_messages:
return ""
@@ -1093,7 +1093,7 @@ async def build_readable_messages(
)
read_mark_line = "\n--- 以上消息是你已经看过,请关注以下未读的新消息---\n"
# 组合结果
result_parts = []
if formatted_before and formatted_after:

View File

@@ -1,5 +1,4 @@
import asyncio
from typing import Type
from src.chat.utils.prompt_params import PromptParameters
from src.common.logger import get_logger
@@ -20,7 +19,7 @@ class PromptComponentManager:
3. 提供一个接口以便在构建核心Prompt时能够获取并执行所有相关的组件。
"""
def get_components_for(self, injection_point: str) -> list[Type[BasePrompt]]:
def get_components_for(self, injection_point: str) -> list[type[BasePrompt]]:
"""
获取指定注入点的所有已注册组件类。
@@ -33,7 +32,7 @@ class PromptComponentManager:
# 从组件注册中心获取所有启用的Prompt组件
enabled_prompts = component_registry.get_enabled_components_by_type(ComponentType.PROMPT)
matching_components: list[Type[BasePrompt]] = []
matching_components: list[type[BasePrompt]] = []
for prompt_name, prompt_info in enabled_prompts.items():
# 确保 prompt_info 是 PromptInfo 类型
@@ -106,4 +105,4 @@ class PromptComponentManager:
# 创建全局单例
prompt_component_manager = PromptComponentManager()
prompt_component_manager = PromptComponentManager()

View File

@@ -77,4 +77,4 @@ class PromptParameters:
errors.append("prompt_mode必须是's4u''normal''minimal'")
if self.max_context_messages <= 0:
errors.append("max_context_messages必须大于0")
return errors
return errors

View File

@@ -1,5 +1,5 @@
import base64
import asyncio
import base64
import hashlib
import io
import os
@@ -174,7 +174,7 @@ class ImageManager:
# 3. 查询通用图片描述缓存ImageDescriptions表
if cached_description := await self._get_description_from_db(image_hash, "emoji"):
logger.info(f"[缓存命中] 使用通用图片缓存(ImageDescriptions表)中的描述")
logger.info("[缓存命中] 使用通用图片缓存(ImageDescriptions表)中的描述")
refined_part = cached_description.split(" Keywords:")[0]
return f"[表情包:{refined_part}]"
@@ -185,7 +185,7 @@ class ImageManager:
if not full_description:
logger.warning("未能通过新逻辑生成有效描述")
return "[表情包(描述生成失败)]"
# 4. (可选) 如果启用了“偷表情包”,则将图片和完整描述存入待注册区
if global_config.emoji.steal_emoji:
logger.debug(f"偷取表情包功能已开启,保存待注册表情包: {image_hash}")
@@ -231,7 +231,7 @@ class ImageManager:
if existing_image and existing_image.description:
logger.debug(f"[缓存命中] 使用Images表中的图片描述: {existing_image.description[:50]}...")
return f"[图片:{existing_image.description}]"
# 3. 其次查询 ImageDescriptions 表缓存
if cached_description := await self._get_description_from_db(image_hash, "image"):
logger.debug(f"[缓存命中] 使用ImageDescriptions表中的描述: {cached_description[:50]}...")
@@ -256,9 +256,9 @@ class ImageManager:
break # 成功获取描述则跳出循环
except Exception as e:
logger.error(f"VLM调用失败 (第 {i+1}/3 次): {e}", exc_info=True)
if i < 2: # 如果不是最后一次则等待1秒
logger.warning(f"识图失败将在1秒后重试...")
logger.warning("识图失败将在1秒后重试...")
await asyncio.sleep(1)
if not description or not description.strip():
@@ -278,7 +278,7 @@ class ImageManager:
logger.debug(f"[数据库] 为现有图片记录补充描述: {image_hash[:8]}...")
# 注意这里不创建新的Images记录因为process_image会负责创建
await session.commit()
logger.info(f"新生成的图片描述已存入缓存 (Hash: {image_hash[:8]}...)")
return f"[图片:{description}]"
@@ -330,7 +330,7 @@ class ImageManager:
# 使用linspace计算4个均匀分布的索引
indices = np.linspace(0, num_frames - 1, 4, dtype=int)
selected_frames = [all_frames[i] for i in indices]
logger.debug(f"GIF Frame Analysis: Total frames={num_frames}, Selected indices={indices if num_frames > 4 else list(range(num_frames))}")
# --- 帧选择逻辑结束 ---