chore: perform widespread code cleanup and formatting
Perform a comprehensive code cleanup across multiple modules to improve code quality, consistency, and maintainability. Key changes include: - Removing numerous unused imports. - Standardizing import order. - Eliminating trailing whitespace and inconsistent newlines. - Updating legacy type hints to modern syntax (e.g., `List` -> `list`). - Making minor improvements for code robustness and style.
This commit is contained in:
@@ -47,10 +47,10 @@ class BlockShuffler:
|
||||
|
||||
# 复制上下文以避免修改原始字典
|
||||
shuffled_context = context_data.copy()
|
||||
|
||||
|
||||
# 示例:假设模板中的占位符格式为 {block_name}
|
||||
# 我们需要解析模板,找到可重排的组,并重新构建模板字符串。
|
||||
|
||||
|
||||
# 注意:这是一个复杂的逻辑,通常需要一个简单的模板引擎或正则表达式来完成。
|
||||
# 为保持此函数职责单一,这里仅演示核心的重排逻辑,
|
||||
# 完整的模板重建逻辑应在调用此函数的地方处理。
|
||||
@@ -58,14 +58,14 @@ class BlockShuffler:
|
||||
for group in BlockShuffler.SWAPPABLE_BLOCK_GROUPS:
|
||||
# 过滤出在当前上下文中实际存在的、非空的block
|
||||
existing_blocks = [
|
||||
block for block in group if block in context_data and context_data[block]
|
||||
block for block in group if context_data.get(block)
|
||||
]
|
||||
|
||||
if len(existing_blocks) > 1:
|
||||
# 随机打乱顺序
|
||||
random.shuffle(existing_blocks)
|
||||
logger.debug(f"重排block组: {group} -> {existing_blocks}")
|
||||
|
||||
|
||||
# 这里的实现需要调用者根据 `existing_blocks` 的新顺序
|
||||
# 去动态地重新组织 `prompt_template` 字符串。
|
||||
# 例如,找到模板中与 `group` 相关的占位符部分,然后按新顺序替换它们。
|
||||
|
||||
@@ -2,7 +2,6 @@ import asyncio
|
||||
import copy
|
||||
import re
|
||||
from collections.abc import Awaitable, Callable
|
||||
from typing import List
|
||||
|
||||
from src.chat.utils.prompt_params import PromptParameters
|
||||
from src.common.logger import get_logger
|
||||
@@ -119,7 +118,7 @@ class PromptComponentManager:
|
||||
async def add_injection_rule(
|
||||
self,
|
||||
prompt_name: str,
|
||||
rules: List[InjectionRule],
|
||||
rules: list[InjectionRule],
|
||||
content_provider: Callable[..., Awaitable[str]],
|
||||
source: str = "runtime",
|
||||
) -> bool:
|
||||
@@ -521,7 +520,7 @@ class PromptComponentManager:
|
||||
else:
|
||||
for name, (rule, _, _) in rules_for_target.items():
|
||||
target_copy[name] = rule
|
||||
|
||||
|
||||
if target_copy:
|
||||
rules_copy[target] = target_copy
|
||||
|
||||
|
||||
@@ -63,7 +63,7 @@ class PromptParameters:
|
||||
action_descriptions: str = ""
|
||||
notice_block: str = ""
|
||||
group_chat_reminder_block: str = ""
|
||||
|
||||
|
||||
# 可用动作信息
|
||||
available_actions: dict[str, Any] | None = None
|
||||
|
||||
|
||||
@@ -228,9 +228,9 @@ class HTMLReportGenerator:
|
||||
|
||||
# 渲染模板
|
||||
# 读取CSS和JS文件内容
|
||||
async with aiofiles.open(os.path.join(self.jinja_env.loader.searchpath[0], "report.css"), "r", encoding="utf-8") as f:
|
||||
async with aiofiles.open(os.path.join(self.jinja_env.loader.searchpath[0], "report.css"), encoding="utf-8") as f:
|
||||
report_css = await f.read()
|
||||
async with aiofiles.open(os.path.join(self.jinja_env.loader.searchpath[0], "report.js"), "r", encoding="utf-8") as f:
|
||||
async with aiofiles.open(os.path.join(self.jinja_env.loader.searchpath[0], "report.js"), encoding="utf-8") as f:
|
||||
report_js = await f.read()
|
||||
# 渲染模板
|
||||
template = self.jinja_env.get_template("report.html")
|
||||
|
||||
@@ -3,8 +3,6 @@ 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
|
||||
@@ -16,7 +14,7 @@ logger = get_logger("maibot_statistic")
|
||||
# 彻底异步化:删除原同步包装器 _sync_db_get,所有数据库访问统一使用 await db_get。
|
||||
|
||||
|
||||
from .report_generator import HTMLReportGenerator, format_online_time
|
||||
from .report_generator import HTMLReportGenerator
|
||||
from .statistic_keys import *
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
该模块用于存放统计数据相关的常量键名。
|
||||
"""
|
||||
@@ -61,4 +60,4 @@ STD_TIME_COST_BY_PROVIDER = "std_time_costs_by_provider"
|
||||
PIE_CHART_COST_BY_PROVIDER = "pie_chart_cost_by_provider"
|
||||
PIE_CHART_REQ_BY_PROVIDER = "pie_chart_req_by_provider"
|
||||
BAR_CHART_COST_BY_MODEL = "bar_chart_cost_by_model"
|
||||
BAR_CHART_REQ_BY_MODEL = "bar_chart_req_by_model"
|
||||
BAR_CHART_REQ_BY_MODEL = "bar_chart_req_by_model"
|
||||
|
||||
Reference in New Issue
Block a user