feat(规划): 引入统一的规划系统并重构组件
本次提交引入了一个全新的统一规划系统,取代了独立的日程和月度计划模块。这一变更集中了配置并解耦了职责,以提高可维护性和清晰度。 核心逻辑现在被分解为专门的组件: - PlanManager: 处理日常日程和月度目标的所有数据库交互,取代了已-删除的 monthly_plan_db.py。 - ScheduleLLMGenerator: 封装了与 LLM 交互以生成日程的逻辑,将此职责从 ScheduleManager 中移除。 - MonthlyPlanManager: 现在作为核心 PlanManager 的简化接口。 这次架构性的大修通过将数据持久化和 LLM 生成逻辑从主调度逻辑中抽象出来,简化了整体设计。 重大变更: bot_config.toml 中的配置已被迁移。移除了 [schedule] 和 [monthly_plan_system] 部分。所有相关设置现在必须在新的 [planning_system] 部分下进行配置。
This commit is contained in:
105
src/schedule/plan_manager.py
Normal file
105
src/schedule/plan_manager.py
Normal file
@@ -0,0 +1,105 @@
|
||||
# mmc/src/schedule/plan_manager.py
|
||||
|
||||
from datetime import datetime
|
||||
from typing import List, Optional
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config
|
||||
from .database import (
|
||||
add_new_plans,
|
||||
get_archived_plans_for_month,
|
||||
archive_active_plans_for_month,
|
||||
has_active_plans,
|
||||
get_active_plans_for_month,
|
||||
delete_plans_by_ids,
|
||||
get_smart_plans_for_daily_schedule,
|
||||
)
|
||||
from .llm_generator import MonthlyPlanLLMGenerator
|
||||
|
||||
logger = get_logger("plan_manager")
|
||||
|
||||
|
||||
class PlanManager:
|
||||
def __init__(self):
|
||||
self.llm_generator = MonthlyPlanLLMGenerator()
|
||||
self.generation_running = False
|
||||
|
||||
async def ensure_and_generate_plans_if_needed(self, target_month: Optional[str] = None) -> bool:
|
||||
if target_month is None:
|
||||
target_month = datetime.now().strftime("%Y-%m")
|
||||
|
||||
if not has_active_plans(target_month):
|
||||
logger.info(f" {target_month} 没有任何有效的月度计划,将触发同步生成。")
|
||||
generation_successful = await self._generate_monthly_plans_logic(target_month)
|
||||
return generation_successful
|
||||
else:
|
||||
logger.info(f"{target_month} 已存在有效的月度计划。")
|
||||
plans = get_active_plans_for_month(target_month)
|
||||
max_plans = global_config.planning_system.max_plans_per_month
|
||||
if len(plans) > max_plans:
|
||||
logger.warning(f"当前月度计划数量 ({len(plans)}) 超出上限 ({max_plans}),将自动删除多余的计划。")
|
||||
plans_to_delete = plans[: len(plans) - max_plans]
|
||||
delete_ids = [p.id for p in plans_to_delete]
|
||||
delete_plans_by_ids(delete_ids) # type: ignore
|
||||
plans = get_active_plans_for_month(target_month)
|
||||
|
||||
if plans:
|
||||
plan_texts = "\n".join([f" {i + 1}. {plan.plan_text}" for i, plan in enumerate(plans)])
|
||||
logger.info(f"当前月度计划内容:\n{plan_texts}")
|
||||
return True
|
||||
|
||||
async def _generate_monthly_plans_logic(self, target_month: Optional[str] = None) -> bool:
|
||||
if self.generation_running:
|
||||
logger.info("月度计划生成任务已在运行中,跳过重复启动")
|
||||
return False
|
||||
|
||||
self.generation_running = True
|
||||
try:
|
||||
if target_month is None:
|
||||
target_month = datetime.now().strftime("%Y-%m")
|
||||
|
||||
logger.info(f"开始为 {target_month} 生成月度计划...")
|
||||
if not global_config.planning_system.monthly_plan_enable:
|
||||
logger.info(" 月度计划系统已禁用,跳过计划生成。")
|
||||
return False
|
||||
|
||||
last_month = self._get_previous_month(target_month)
|
||||
archived_plans = get_archived_plans_for_month(last_month)
|
||||
plans = await self.llm_generator.generate_plans_with_llm(target_month, archived_plans)
|
||||
|
||||
if plans:
|
||||
add_new_plans(plans, target_month)
|
||||
logger.info(f"成功为 {target_month} 生成并保存了 {len(plans)} 条月度计划。")
|
||||
return True
|
||||
else:
|
||||
logger.warning(f"未能为 {target_month} 生成有效的月度计划。")
|
||||
return False
|
||||
except Exception as e:
|
||||
logger.error(f" 生成 {target_month} 月度计划时发生错误: {e}")
|
||||
return False
|
||||
finally:
|
||||
self.generation_running = False
|
||||
|
||||
def _get_previous_month(self, current_month: str) -> str:
|
||||
try:
|
||||
year, month = map(int, current_month.split("-"))
|
||||
if month == 1:
|
||||
return f"{year - 1}-12"
|
||||
else:
|
||||
return f"{year}-{month - 1:02d}"
|
||||
except Exception:
|
||||
return "1900-01"
|
||||
|
||||
async def archive_current_month_plans(self, target_month: Optional[str] = None):
|
||||
try:
|
||||
if target_month is None:
|
||||
target_month = datetime.now().strftime("%Y-%m")
|
||||
logger.info(f" 开始归档 {target_month} 的活跃月度计划...")
|
||||
archived_count = archive_active_plans_for_month(target_month)
|
||||
logger.info(f" 成功归档了 {archived_count} 条 {target_month} 的月度计划。")
|
||||
except Exception as e:
|
||||
logger.error(f" 归档 {target_month} 月度计划时发生错误: {e}")
|
||||
|
||||
def get_plans_for_schedule(self, month: str, max_count: int) -> List:
|
||||
avoid_days = global_config.planning_system.avoid_repetition_days
|
||||
return get_smart_plans_for_daily_schedule(month, max_count=max_count, avoid_days=avoid_days)
|
||||
Reference in New Issue
Block a user