perf(methods): 通过移除不必要的 self 参数优化方法签名
在包括 chat、plugin_system、schedule 和 mais4u 在内的多个模块中,消除冗余的实例引用。此次改动将无需访问实例状态的实用函数转换为静态方法,从而提升了内存效率,并使方法依赖关系更加清晰。
This commit is contained in:
@@ -125,7 +125,8 @@ class ScheduleLLMGenerator:
|
||||
logger.info("继续重试...")
|
||||
await asyncio.sleep(3)
|
||||
|
||||
def _validate_schedule_with_pydantic(self, schedule_data) -> bool:
|
||||
@staticmethod
|
||||
def _validate_schedule_with_pydantic(schedule_data) -> bool:
|
||||
try:
|
||||
ScheduleData(schedule=schedule_data)
|
||||
logger.info("日程数据Pydantic验证通过")
|
||||
@@ -204,7 +205,8 @@ class MonthlyPlanLLMGenerator:
|
||||
logger.error(" 所有尝试都失败,无法生成月度计划")
|
||||
return []
|
||||
|
||||
def _parse_plans_response(self, response: str) -> List[str]:
|
||||
@staticmethod
|
||||
def _parse_plans_response(response: str) -> List[str]:
|
||||
try:
|
||||
response = response.strip()
|
||||
lines = [line.strip() for line in response.split("\n") if line.strip()]
|
||||
|
||||
@@ -80,7 +80,8 @@ class PlanManager:
|
||||
finally:
|
||||
self.generation_running = False
|
||||
|
||||
def _get_previous_month(self, current_month: str) -> str:
|
||||
@staticmethod
|
||||
def _get_previous_month(current_month: str) -> str:
|
||||
try:
|
||||
year, month = map(int, current_month.split("-"))
|
||||
if month == 1:
|
||||
@@ -90,7 +91,8 @@ class PlanManager:
|
||||
except Exception:
|
||||
return "1900-01"
|
||||
|
||||
async def archive_current_month_plans(self, target_month: Optional[str] = None):
|
||||
@staticmethod
|
||||
async def archive_current_month_plans(target_month: Optional[str] = None):
|
||||
try:
|
||||
if target_month is None:
|
||||
target_month = datetime.now().strftime("%Y-%m")
|
||||
@@ -100,6 +102,7 @@ class PlanManager:
|
||||
except Exception as e:
|
||||
logger.error(f" 归档 {target_month} 月度计划时发生错误: {e}")
|
||||
|
||||
async def get_plans_for_schedule(self, month: str, max_count: int) -> List:
|
||||
@staticmethod
|
||||
async def get_plans_for_schedule(month: str, max_count: int) -> List:
|
||||
avoid_days = global_config.planning_system.avoid_repetition_days
|
||||
return await get_smart_plans_for_daily_schedule(month, max_count=max_count, avoid_days=avoid_days)
|
||||
@@ -3,6 +3,8 @@ import asyncio
|
||||
from datetime import datetime, time, timedelta
|
||||
from typing import Optional, List, Dict, Any
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from src.common.database.sqlalchemy_models import Schedule, get_db_session
|
||||
from src.config.config import global_config
|
||||
from src.common.logger import get_logger
|
||||
@@ -115,7 +117,8 @@ class ScheduleManager:
|
||||
self.schedule_generation_running = False
|
||||
logger.info("日程生成任务结束")
|
||||
|
||||
async def _save_schedule_to_db(self, date_str: str, schedule_data: List[Dict[str, Any]]):
|
||||
@staticmethod
|
||||
async def _save_schedule_to_db(date_str: str, schedule_data: List[Dict[str, Any]]):
|
||||
async with get_db_session() as session:
|
||||
schedule_json = orjson.dumps(schedule_data).decode("utf-8")
|
||||
result = await session.execute(select(Schedule).filter(Schedule.date == date_str))
|
||||
@@ -128,7 +131,8 @@ class ScheduleManager:
|
||||
session.add(new_schedule)
|
||||
await session.commit()
|
||||
|
||||
def _log_generated_schedule(self, date_str: str, schedule_data: List[Dict[str, Any]]):
|
||||
@staticmethod
|
||||
def _log_generated_schedule(date_str: str, schedule_data: List[Dict[str, Any]]):
|
||||
schedule_str = f"✅ 成功生成并保存今天的日程 ({date_str}):\n"
|
||||
for item in schedule_data:
|
||||
schedule_str += f" - {item.get('time_range', '未知时间')}: {item.get('activity', '未知活动')}\n"
|
||||
@@ -153,7 +157,8 @@ class ScheduleManager:
|
||||
logger.warning(f"解析日程事件失败: {event}, 错误: {e}")
|
||||
return None
|
||||
|
||||
def _validate_schedule_with_pydantic(self, schedule_data) -> bool:
|
||||
@staticmethod
|
||||
def _validate_schedule_with_pydantic(schedule_data) -> bool:
|
||||
try:
|
||||
ScheduleData(schedule=schedule_data)
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user