feat(monthly_plan): 为月度计划添加数量上限

新增 `max_plans_per_month` 配置项,用于限制每个月可存在的最大月度计划数量。

现在,在向数据库添加新计划时,系统会检查当前月份的计划总数。如果添加新计划会导致总数超过上限,则只会添加允许数量内的计划,以防止计划池无限增长。
This commit is contained in:
minecraft1024a
2025-08-22 17:49:26 +08:00
parent e72b5064f5
commit 5b27ff197d
3 changed files with 31 additions and 4 deletions

View File

@@ -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()

View File

@@ -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):

View File

@@ -416,11 +416,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 #"是否启用唤醒度系统"