ruff归零

This commit is contained in:
明天好像没什么
2025-11-01 21:32:41 +08:00
committed by Windpicker-owo
parent f816150782
commit 3db00aa8f3
20 changed files with 121 additions and 61 deletions

View File

@@ -4,6 +4,7 @@ import time
from datetime import datetime
from typing import Any
import aiofiles
import orjson
from sqlalchemy import select
@@ -718,8 +719,9 @@ class ExpressionLearnerManager:
continue
try:
with open(expr_file, encoding="utf-8") as f:
expressions = orjson.loads(f.read())
async with aiofiles.open(expr_file, encoding="utf-8") as f:
content = await f.read()
expressions = orjson.loads(content)
for chat_id in chat_ids:
expr_file = os.path.join(type_dir, chat_id, "expressions.json")
@@ -780,21 +782,23 @@ class ExpressionLearnerManager:
except Exception as e:
logger.error(f"迁移表达方式 {expr_file} 失败: {e}")
# 检查并处理grammar表达删除
if not os.path.exists(done_flag2):
logger.info("开始删除所有grammar类型的表达...")
try:
deleted_count = self.delete_all_grammar_expressions()
logger.info(f"grammar表达删除完成共删除 {deleted_count} 个表达")
# 创建done.done2标记文件
with open(done_flag2, "w", encoding="utf-8") as f:
f.write("done\n")
logger.info("已创建done.done2标记文件grammar表达删除标记完成")
except Exception as e:
logger.error(f"删除grammar表达或创建标记文件失败: {e}")
else:
logger.info("grammar表达已删除跳过重复删除")
# 标记迁移完成
try:
# 确保done.done文件的父目录存在
done_parent_dir = os.path.dirname(done_flag)
if not os.path.exists(done_parent_dir):
os.makedirs(done_parent_dir, exist_ok=True)
logger.debug(f"为done.done创建父目录: {done_parent_dir}")
async with aiofiles.open(done_flag, "w", encoding="utf-8") as f:
await f.write("done\n")
logger.info(f"表达方式JSON迁移已完成共迁移 {migrated_count} 个表达方式,已写入done.done标记文件")
except PermissionError as e:
logger.error(f"权限不足无法写入done.done标记文件: {e}")
except OSError as e:
logger.error(f"文件系统错误无法写入done.done标记文件: {e}")
except Exception as e:
logger.error(f"写入done.done标记文件失败: {e}")
@staticmethod
async def _migrate_old_data_create_date():

View File

@@ -4,6 +4,7 @@ import os
from dataclasses import dataclass
# import tqdm
import aiofiles
import faiss
import numpy as np
import orjson
@@ -194,8 +195,8 @@ class EmbeddingStore:
test_vectors[str(idx)] = []
with open(self.get_test_file_path(), "w", encoding="utf-8") as f:
f.write(orjson.dumps(test_vectors, option=orjson.OPT_INDENT_2).decode("utf-8"))
async with aiofiles.open(self.get_test_file_path(), "w", encoding="utf-8") as f:
await f.write(orjson.dumps(test_vectors, option=orjson.OPT_INDENT_2).decode("utf-8"))
logger.info("测试字符串嵌入向量保存完成")

View File

@@ -25,6 +25,9 @@ from src.llm_models.utils_model import LLMRequest
logger = get_logger(__name__)
# 全局背景任务集合
_background_tasks = set()
@dataclass
class HippocampusSampleConfig:
@@ -89,7 +92,9 @@ class HippocampusSampler:
task_config = getattr(model_config.model_task_config, "utils", None)
if task_config:
self.memory_builder_model = LLMRequest(model_set=task_config, request_type="memory.hippocampus_build")
asyncio.create_task(self.start_background_sampling())
task = asyncio.create_task(self.start_background_sampling())
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
logger.info("✅ 海马体采样器初始化成功")
else:
raise RuntimeError("未找到记忆构建模型配置")

View File

@@ -19,6 +19,9 @@ from src.chat.memory_system.memory_builder import MemoryBuilder, MemoryExtractio
from src.chat.memory_system.memory_chunk import MemoryChunk
from src.chat.memory_system.memory_fusion import MemoryFusionEngine
from src.chat.memory_system.memory_query_planner import MemoryQueryPlanner
# 全局背景任务集合
_background_tasks = set()
from src.chat.memory_system.message_collection_storage import MessageCollectionStorage
@@ -1613,7 +1616,9 @@ class MemorySystem:
def start_hippocampus_sampling(self):
"""启动海马体采样"""
if self.hippocampus_sampler:
asyncio.create_task(self.hippocampus_sampler.start_background_sampling())
task = asyncio.create_task(self.hippocampus_sampler.start_background_sampling())
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
logger.info("海马体后台采样已启动")
else:
logger.warning("海马体采样器未初始化,无法启动采样")

View File

@@ -19,6 +19,9 @@ from .distribution_manager import stream_loop_manager
logger = get_logger("context_manager")
# 全局背景任务集合
_background_tasks = set()
class SingleStreamContextManager:
"""单流上下文管理器 - 每个实例只管理一个 stream 的上下文"""
@@ -42,7 +45,9 @@ class SingleStreamContextManager:
logger.debug(f"单流上下文管理器初始化: {stream_id}")
# 异步初始化历史消息(不阻塞构造函数)
asyncio.create_task(self._initialize_history_from_db())
task = asyncio.create_task(self._initialize_history_from_db())
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
def get_context(self) -> StreamContext:
"""获取流上下文"""
@@ -93,7 +98,9 @@ class SingleStreamContextManager:
logger.debug(f"消息已缓存,等待当前处理完成: stream={self.stream_id}")
# 启动流的循环任务(如果还未启动)
asyncio.create_task(stream_loop_manager.start_stream_loop(self.stream_id))
task = asyncio.create_task(stream_loop_manager.start_stream_loop(self.stream_id))
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
logger.debug(f"添加消息到缓存系统: {self.stream_id}")
return True
else:
@@ -113,7 +120,9 @@ class SingleStreamContextManager:
self.total_messages += 1
self.last_access_time = time.time()
# 启动流的循环任务(如果还未启动)
asyncio.create_task(stream_loop_manager.start_stream_loop(self.stream_id))
task = asyncio.create_task(stream_loop_manager.start_stream_loop(self.stream_id))
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
logger.debug(f"添加消息{message.processed_plain_text}到单流上下文: {self.stream_id}")
return True
except Exception as e:

View File

@@ -3,6 +3,8 @@ from collections import defaultdict
from datetime import datetime, timedelta
from typing import Any
import aiofiles
from src.common.database.compatibility import db_get, db_query
from src.common.database.core.models import LLMUsage, Messages, OnlineTime
from src.common.logger import get_logger
@@ -1014,8 +1016,8 @@ class StatisticOutputTask(AsyncTask):
"""
)
with open(self.record_file_path, "w", encoding="utf-8") as f:
f.write(html_template)
async with aiofiles.open(self.record_file_path, "w", encoding="utf-8") as f:
await f.write(html_template)
async def _generate_chart_data(self, stat: dict[str, Any]) -> dict:
"""生成图表数据 (异步)"""

View File

@@ -7,6 +7,7 @@ import time
import uuid
from typing import Any
import aiofiles
import numpy as np
from PIL import Image
from rich.traceback import install
@@ -198,8 +199,8 @@ class ImageManager:
os.makedirs(emoji_dir, exist_ok=True)
file_path = os.path.join(emoji_dir, filename)
with open(file_path, "wb") as f:
f.write(image_bytes)
async with aiofiles.open(file_path, "wb") as f:
await f.write(image_bytes)
logger.info(f"新表情包已保存至待注册目录: {file_path}")
except Exception as e:
logger.error(f"保存待注册表情包文件失败: {e!s}")
@@ -436,8 +437,8 @@ class ImageManager:
os.makedirs(image_dir, exist_ok=True)
file_path = os.path.join(image_dir, filename)
with open(file_path, "wb") as f:
f.write(image_bytes)
async with aiofiles.open(file_path, "wb") as f:
await f.write(image_bytes)
new_img = Images(
image_id=image_id,