chore: 统一代码风格并进行现代化改造
本次提交主要包含以下内容: - **代码风格统一**:对多个文件进行了格式化,包括移除多余的空行、调整导入顺序、统一字符串引号等,以提高代码一致性和可读性。 - **类型提示现代化**:在多个文件中将旧的 `typing` 模块类型提示(如 `Optional[T]`、`List[T]`、`Union[T, U]`)更新为现代 Python 语法(`T | None`、`list[T]`、`T | U`)。 - **f-string 格式化**:在 `scripts/convert_manifest.py` 中,将 `.format()` 调用更新为更现代和易读的 f-string `!r` 表示法。 - **文件末尾换行符**:为多个文件添加或修正了文件末尾的换行符,遵循 POSIX 标准。
This commit is contained in:
@@ -550,7 +550,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,将其直接替换为[图片:描述]格式"""
|
||||
@@ -978,7 +978,7 @@ async def build_readable_messages(
|
||||
return ""
|
||||
|
||||
copy_messages = [msg.copy() for msg in messages]
|
||||
|
||||
|
||||
if not copy_messages:
|
||||
return ""
|
||||
|
||||
@@ -1092,7 +1092,7 @@ async def build_readable_messages(
|
||||
)
|
||||
|
||||
read_mark_line = "\n--- 以上消息是你已经看过,请关注以下未读的新消息---\n"
|
||||
|
||||
|
||||
# 组合结果
|
||||
result_parts = []
|
||||
if formatted_before and formatted_after:
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))}")
|
||||
# --- 帧选择逻辑结束 ---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user