From a234e0b8aa1a800a0f57010d97e821f83290831a Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Sat, 25 Oct 2025 13:31:22 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=BB=9F=E4=B8=80=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=E5=B9=B6=E8=BF=9B=E8=A1=8C=E7=8E=B0=E4=BB=A3?= =?UTF-8?q?=E5=8C=96=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本次提交主要包含以下内容: - **代码风格统一**:对多个文件进行了格式化,包括移除多余的空行、调整导入顺序、统一字符串引号等,以提高代码一致性和可读性。 - **类型提示现代化**:在多个文件中将旧的 `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 标准。 --- src/chat/utils/prompt_component_manager.py | 7 ++- src/plugin_system/apis/schedule_api.py | 44 +++++++++---------- src/plugin_system/apis/storage_api.py | 2 +- .../proactive_thinker_executor.py | 3 +- .../built_in/proactive_thinker/prompts.py | 2 +- .../tts_voice_plugin/actions/tts_action.py | 9 ++-- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/chat/utils/prompt_component_manager.py b/src/chat/utils/prompt_component_manager.py index c1fb92e13..32c943d9b 100644 --- a/src/chat/utils/prompt_component_manager.py +++ b/src/chat/utils/prompt_component_manager.py @@ -1,6 +1,4 @@ import asyncio -import re -from typing import Type from src.chat.utils.prompt_params import PromptParameters from src.common.logger import get_logger @@ -21,7 +19,7 @@ class PromptComponentManager: 3. 提供一个接口,以便在构建核心Prompt时,能够获取并执行所有相关的组件。 """ - def _get_rules_for(self, target_prompt_name: str) -> list[tuple[InjectionRule, Type[BasePrompt]]]: + def get_components_for(self, injection_point: str) -> list[type[BasePrompt]]: """ 获取指定目标Prompt的所有注入规则及其关联的组件类。 @@ -36,7 +34,8 @@ class PromptComponentManager: enabled_prompts = component_registry.get_enabled_components_by_type(ComponentType.PROMPT) matching_rules = [] - # 遍历所有启用的 Prompt 组件,查找与目标 Prompt 相关的注入规则 + matching_components: list[type[BasePrompt]] = [] + for prompt_name, prompt_info in enabled_prompts.items(): if not isinstance(prompt_info, PromptInfo): continue diff --git a/src/plugin_system/apis/schedule_api.py b/src/plugin_system/apis/schedule_api.py index 4273a8c64..2b456456c 100644 --- a/src/plugin_system/apis/schedule_api.py +++ b/src/plugin_system/apis/schedule_api.py @@ -46,8 +46,8 @@ """ import random -from datetime import datetime, time -from typing import Any, List, Optional, Union +from datetime import datetime +from typing import Any import orjson from sqlalchemy import func, select @@ -62,7 +62,7 @@ logger = get_logger("schedule_api") # --- 内部辅助函数 --- def _format_schedule_list( - items: Union[List[dict[str, Any]], List[MonthlyPlan]], + items: list[dict[str, Any]] | list[MonthlyPlan], template: str, item_type: str, ) -> str: @@ -79,7 +79,7 @@ def _format_schedule_list( return "\\n".join(lines) -async def _get_schedule_from_db(date_str: str) -> Optional[List[dict[str, Any]]]: +async def _get_schedule_from_db(date_str: str) -> list[dict[str, Any]] | None: """从数据库中获取并解析指定日期的日程""" async with get_db_session() as session: result = await session.execute(select(Schedule).filter(Schedule.date == date_str)) @@ -100,10 +100,10 @@ class ScheduleAPI: @staticmethod async def get_schedule( - date: Optional[str] = None, + date: str | None = None, formatted: bool = False, format_template: str = "{time_range}: {activity}", - ) -> Union[List[dict[str, Any]], str, None]: + ) -> list[dict[str, Any]] | str | None: """ (异步) 获取指定日期的日程安排。 @@ -132,7 +132,7 @@ class ScheduleAPI: async def get_current_activity( formatted: bool = False, format_template: str = "{time_range}: {activity}", - ) -> Union[dict[str, Any], str, None]: + ) -> dict[str, Any] | str | None: """ (异步) 获取当前正在进行的活动。 @@ -174,10 +174,10 @@ class ScheduleAPI: async def get_activities_between( start_time: str, end_time: str, - date: Optional[str] = None, + date: str | None = None, formatted: bool = False, format_template: str = "{time_range}: {activity}", - ) -> Union[List[dict[str, Any]], str, None]: + ) -> list[dict[str, Any]] | str | None: """ (异步) 获取指定日期和时间范围内的所有活动。 @@ -223,11 +223,11 @@ class ScheduleAPI: @staticmethod async def get_monthly_plans( - target_month: Optional[str] = None, - random_count: Optional[int] = None, + target_month: str | None = None, + random_count: int | None = None, formatted: bool = False, format_template: str = "- {plan_text}", - ) -> Union[List[MonthlyPlan], str, None]: + ) -> list[MonthlyPlan] | str | None: """ (异步) 获取指定月份的有效月度计划。 @@ -258,7 +258,7 @@ class ScheduleAPI: return None @staticmethod - async def count_monthly_plans(target_month: Optional[str] = None) -> int: + async def count_monthly_plans(target_month: str | None = None) -> int: """ (异步) 获取指定月份的有效月度计划总数。 @@ -288,10 +288,10 @@ class ScheduleAPI: # ============================================================================= async def get_schedule( - date: Optional[str] = None, + date: str | None = None, formatted: bool = False, format_template: str = "{time_range}: {activity}", -) -> Union[List[dict[str, Any]], str, None]: +) -> list[dict[str, Any]] | str | None: """(异步) 获取指定日期的日程安排的便捷函数。""" return await ScheduleAPI.get_schedule(date, formatted, format_template) @@ -299,7 +299,7 @@ async def get_schedule( async def get_current_activity( formatted: bool = False, format_template: str = "{time_range}: {activity}", -) -> Union[dict[str, Any], str, None]: +) -> dict[str, Any] | str | None: """(异步) 获取当前正在进行的活动的便捷函数。""" return await ScheduleAPI.get_current_activity(formatted, format_template) @@ -307,24 +307,24 @@ async def get_current_activity( async def get_activities_between( start_time: str, end_time: str, - date: Optional[str] = None, + date: str | None = None, formatted: bool = False, format_template: str = "{time_range}: {activity}", -) -> Union[List[dict[str, Any]], str, None]: +) -> list[dict[str, Any]] | str | None: """(异步) 获取指定时间范围内活动的便捷函数。""" return await ScheduleAPI.get_activities_between(start_time, end_time, date, formatted, format_template) async def get_monthly_plans( - target_month: Optional[str] = None, - random_count: Optional[int] = None, + target_month: str | None = None, + random_count: int | None = None, formatted: bool = False, format_template: str = "- {plan_text}", -) -> Union[List[MonthlyPlan], str, None]: +) -> list[MonthlyPlan] | str | None: """(异步) 获取月度计划的便捷函数。""" return await ScheduleAPI.get_monthly_plans(target_month, random_count, formatted, format_template) -async def count_monthly_plans(target_month: Optional[str] = None) -> int: +async def count_monthly_plans(target_month: str | None = None) -> int: """(异步) 获取月度计划总数的便捷函数。""" return await ScheduleAPI.count_monthly_plans(target_month) diff --git a/src/plugin_system/apis/storage_api.py b/src/plugin_system/apis/storage_api.py index 952fa8968..e282eb470 100644 --- a/src/plugin_system/apis/storage_api.py +++ b/src/plugin_system/apis/storage_api.py @@ -9,7 +9,7 @@ import json import os import threading -from typing import Any, Dict # noqa: UP035 +from typing import Any from src.common.logger import get_logger diff --git a/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py b/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py index bc7bd374e..21b8ff5bb 100644 --- a/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py +++ b/src/plugins/built_in/proactive_thinker/proactive_thinker_executor.py @@ -9,7 +9,6 @@ from src.chat.utils.prompt import Prompt from src.common.logger import get_logger from src.config.config import global_config, model_config from src.mood.mood_manager import mood_manager -from .prompts import DECISION_PROMPT, PLAN_PROMPT from src.person_info.person_info import get_person_info_manager from src.plugin_system.apis import ( chat_api, @@ -22,6 +21,8 @@ from src.plugin_system.apis import ( send_api, ) +from .prompts import DECISION_PROMPT, PLAN_PROMPT + logger = get_logger(__name__) diff --git a/src/plugins/built_in/proactive_thinker/prompts.py b/src/plugins/built_in/proactive_thinker/prompts.py index eff355aad..af27c9afa 100644 --- a/src/plugins/built_in/proactive_thinker/prompts.py +++ b/src/plugins/built_in/proactive_thinker/prompts.py @@ -94,4 +94,4 @@ PLAN_PROMPT = Prompt( 现在,你说: """ -) \ No newline at end of file +) diff --git a/src/plugins/built_in/tts_voice_plugin/actions/tts_action.py b/src/plugins/built_in/tts_voice_plugin/actions/tts_action.py index f149b8eb2..321f78536 100644 --- a/src/plugins/built_in/tts_voice_plugin/actions/tts_action.py +++ b/src/plugins/built_in/tts_voice_plugin/actions/tts_action.py @@ -2,11 +2,12 @@ TTS 语音合成 Action """ -import toml from pathlib import Path +import toml + from src.common.logger import get_logger -from src.plugin_system.base.base_action import ActionActivationType, BaseAction, ChatMode +from src.plugin_system.base.base_action import BaseAction, ChatMode from ..services.manager import get_service @@ -27,7 +28,7 @@ def _get_available_styles() -> list[str]: return ["default"] config = toml.loads(config_file.read_text(encoding="utf-8")) - + styles_config = config.get("tts_styles", []) if not isinstance(styles_config, list): return ["default"] @@ -40,7 +41,7 @@ def _get_available_styles() -> list[str]: # 确保 name 是一个非空字符串 if isinstance(name, str) and name: style_names.append(name) - + return style_names if style_names else ["default"] except Exception as e: logger.error(f"动态加载TTS风格列表时出错: {e}", exc_info=True)