From 3ce6c8de2c995801d274b4075af4c84c0d2ae9c5 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Fri, 22 Aug 2025 17:49:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(monthly=5Fplan):=20=E4=B8=BA=E6=9C=88?= =?UTF-8?q?=E5=BA=A6=E8=AE=A1=E5=88=92=E6=B7=BB=E5=8A=A0=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E4=B8=8A=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 `max_plans_per_month` 配置项,用于限制每个月可存在的最大月度计划数量。 现在,在向数据库添加新计划时,系统会检查当前月份的计划总数。如果添加新计划会导致总数超过上限,则只会添加允许数量内的计划,以防止计划池无限增长。 --- src/common/database/monthly_plan_db.py | 28 ++++++++++++++++++++++++-- src/config/official_configs.py | 1 + template/bot_config_template.toml | 6 ++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/common/database/monthly_plan_db.py b/src/common/database/monthly_plan_db.py index 811e7c6f5..439cc2044 100644 --- a/src/common/database/monthly_plan_db.py +++ b/src/common/database/monthly_plan_db.py @@ -3,25 +3,49 @@ from typing import List from src.common.database.sqlalchemy_models import MonthlyPlan, get_db_session from src.common.logger import get_logger +from src.config.config import global_config # 需要导入全局配置 logger = get_logger("monthly_plan_db") def add_new_plans(plans: List[str], month: str): """ - 批量添加新生成的月度计划到数据库。 + 批量添加新生成的月度计划到数据库,并确保不超过上限。 :param plans: 计划内容列表。 :param month: 目标月份,格式为 "YYYY-MM"。 """ with get_db_session() as session: try: + # 1. 获取当前有效计划数量 + current_plan_count = session.query(MonthlyPlan).filter( + MonthlyPlan.target_month == month, + MonthlyPlan.is_deleted == False + ).count() + + # 2. 从配置获取上限 + max_plans = global_config.monthly_plan_system.max_plans_per_month + + # 3. 计算还能添加多少计划 + remaining_slots = max_plans - current_plan_count + + if remaining_slots <= 0: + logger.info(f"{month} 的月度计划已达到上限 ({max_plans}条),不再添加新计划。") + return + + # 4. 截取可以添加的计划 + plans_to_add = plans[:remaining_slots] + new_plan_objects = [ MonthlyPlan(plan_text=plan, target_month=month) - for plan in plans + for plan in plans_to_add ] session.add_all(new_plan_objects) session.commit() + logger.info(f"成功向数据库添加了 {len(new_plan_objects)} 条 {month} 的月度计划。") + if len(plans) > len(plans_to_add): + logger.info(f"由于达到月度计划上限,有 {len(plans) - len(plans_to_add)} 条计划未被添加。") + except Exception as e: logger.error(f"添加月度计划时发生错误: {e}") session.rollback() diff --git a/src/config/official_configs.py b/src/config/official_configs.py index 149744962..847f8a015 100644 --- a/src/config/official_configs.py +++ b/src/config/official_configs.py @@ -683,6 +683,7 @@ class MonthlyPlanSystemConfig(ValidatedConfigBase): generation_threshold: int = Field(default=10, ge=0, description="启动时,如果当月计划少于此数量,则触发LLM生成") plans_per_generation: int = Field(default=5, ge=1, description="每次调用LLM期望生成的计划数量") deletion_probability_on_use: float = Field(default=0.5, ge=0.0, le=1.0, description="计划被使用后,被删除的概率") + max_plans_per_month: int = Field(default=20, ge=1, description="每个月允许存在的最大计划数量") class ContextGroup(ValidatedConfigBase): diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index 082ca180f..41246aa0f 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -417,11 +417,13 @@ centralized_config = true # 是否启用插件配置集中化管理 # 是否启用本功能 enable = true # 启动时,如果当月计划少于此数量,则触发LLM生成 -generation_threshold = 30 +generation_threshold = 20 # 每次调用LLM期望生成的计划数量 -plans_per_generation = 5 +plans_per_generation = 4 # 计划被使用后,被删除的概率 (0.0 到 1.0) deletion_probability_on_use = 0.5 +#每个月允许存在的最大计划数量 +max_plans_per_month = 30 [wakeup_system] enable = true #"是否启用唤醒度系统"