re-style: 格式化代码
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
# mmc/src/schedule/database.py
|
||||
|
||||
from typing import List
|
||||
from sqlalchemy import select, func, update, delete
|
||||
|
||||
from sqlalchemy import delete, func, select, update
|
||||
|
||||
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
|
||||
@@ -9,7 +10,7 @@ from src.config.config import global_config
|
||||
logger = get_logger("schedule_database")
|
||||
|
||||
|
||||
async def add_new_plans(plans: List[str], month: str):
|
||||
async def add_new_plans(plans: list[str], month: str):
|
||||
"""
|
||||
批量添加新生成的月度计划到数据库,并确保不超过上限。
|
||||
|
||||
@@ -55,7 +56,7 @@ async def add_new_plans(plans: List[str], month: str):
|
||||
raise
|
||||
|
||||
|
||||
async def get_active_plans_for_month(month: str) -> List[MonthlyPlan]:
|
||||
async def get_active_plans_for_month(month: str) -> list[MonthlyPlan]:
|
||||
"""
|
||||
获取指定月份所有状态为 'active' 的计划。
|
||||
|
||||
@@ -75,7 +76,7 @@ async def get_active_plans_for_month(month: str) -> List[MonthlyPlan]:
|
||||
return []
|
||||
|
||||
|
||||
async def mark_plans_completed(plan_ids: List[int]):
|
||||
async def mark_plans_completed(plan_ids: list[int]):
|
||||
"""
|
||||
将指定ID的计划标记为已完成。
|
||||
|
||||
@@ -103,7 +104,7 @@ async def mark_plans_completed(plan_ids: List[int]):
|
||||
raise
|
||||
|
||||
|
||||
async def delete_plans_by_ids(plan_ids: List[int]):
|
||||
async def delete_plans_by_ids(plan_ids: list[int]):
|
||||
"""
|
||||
根据ID列表从数据库中物理删除月度计划。
|
||||
|
||||
@@ -134,7 +135,7 @@ async def delete_plans_by_ids(plan_ids: List[int]):
|
||||
raise
|
||||
|
||||
|
||||
async def update_plan_usage(plan_ids: List[int], used_date: str):
|
||||
async def update_plan_usage(plan_ids: list[int], used_date: str):
|
||||
"""
|
||||
更新计划的使用统计信息。
|
||||
|
||||
@@ -182,7 +183,7 @@ async def update_plan_usage(plan_ids: List[int], used_date: str):
|
||||
raise
|
||||
|
||||
|
||||
async def get_smart_plans_for_daily_schedule(month: str, max_count: int = 3, avoid_days: int = 7) -> List[MonthlyPlan]:
|
||||
async def get_smart_plans_for_daily_schedule(month: str, max_count: int = 3, avoid_days: int = 7) -> list[MonthlyPlan]:
|
||||
"""
|
||||
智能抽取月度计划用于每日日程生成。
|
||||
|
||||
@@ -255,7 +256,7 @@ async def archive_active_plans_for_month(month: str):
|
||||
raise
|
||||
|
||||
|
||||
async def get_archived_plans_for_month(month: str) -> List[MonthlyPlan]:
|
||||
async def get_archived_plans_for_month(month: str) -> list[MonthlyPlan]:
|
||||
"""
|
||||
获取指定月份所有状态为 'archived' 的计划。
|
||||
用于生成下个月计划时的参考。
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
# mmc/src/schedule/llm_generator.py
|
||||
|
||||
import asyncio
|
||||
import orjson
|
||||
from datetime import datetime
|
||||
from typing import List, Optional, Dict, Any
|
||||
from lunar_python import Lunar
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
from json_repair import repair_json
|
||||
from lunar_python import Lunar
|
||||
|
||||
from src.common.database.sqlalchemy_models import MonthlyPlan
|
||||
from src.common.logger import get_logger
|
||||
from src.config.config import global_config, model_config
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.common.logger import get_logger
|
||||
|
||||
from .schemas import ScheduleData
|
||||
|
||||
logger = get_logger("schedule_llm_generator")
|
||||
@@ -37,7 +39,7 @@ class ScheduleLLMGenerator:
|
||||
def __init__(self):
|
||||
self.llm = LLMRequest(model_set=model_config.model_task_config.schedule_generator, request_type="schedule")
|
||||
|
||||
async def generate_schedule_with_llm(self, sampled_plans: List[MonthlyPlan]) -> Optional[List[Dict[str, Any]]]:
|
||||
async def generate_schedule_with_llm(self, sampled_plans: list[MonthlyPlan]) -> list[dict[str, Any]] | None:
|
||||
now = datetime.now()
|
||||
today_str = now.strftime("%Y-%m-%d")
|
||||
weekday = now.strftime("%A")
|
||||
@@ -143,7 +145,7 @@ class MonthlyPlanLLMGenerator:
|
||||
def __init__(self):
|
||||
self.llm = LLMRequest(model_set=model_config.model_task_config.schedule_generator, request_type="monthly_plan")
|
||||
|
||||
async def generate_plans_with_llm(self, target_month: str, archived_plans: List[MonthlyPlan]) -> List[str]:
|
||||
async def generate_plans_with_llm(self, target_month: str, archived_plans: list[MonthlyPlan]) -> list[str]:
|
||||
guidelines = global_config.planning_system.monthly_plan_guidelines or DEFAULT_MONTHLY_PLAN_GUIDELINES
|
||||
personality = global_config.personality.personality_core
|
||||
personality_side = global_config.personality.personality_side
|
||||
@@ -209,7 +211,7 @@ class MonthlyPlanLLMGenerator:
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def _parse_plans_response(response: str) -> List[str]:
|
||||
def _parse_plans_response(response: str) -> list[str]:
|
||||
try:
|
||||
response = response.strip()
|
||||
lines = [line.strip() for line in response.split("\n") if line.strip()]
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
from src.common.logger import get_logger
|
||||
from src.manager.async_task_manager import AsyncTask, async_task_manager
|
||||
|
||||
from .plan_manager import PlanManager
|
||||
|
||||
logger = get_logger("monthly_plan_manager")
|
||||
@@ -31,7 +31,7 @@ class MonthlyPlanManager:
|
||||
else:
|
||||
logger.info(" 每月月度计划生成任务已在运行中。")
|
||||
|
||||
async def ensure_and_generate_plans_if_needed(self, target_month: Optional[str] = None) -> bool:
|
||||
async def ensure_and_generate_plans_if_needed(self, target_month: str | None = None) -> bool:
|
||||
return await self.plan_manager.ensure_and_generate_plans_if_needed(target_month)
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
# 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_active_plans_for_month,
|
||||
get_archived_plans_for_month,
|
||||
get_smart_plans_for_daily_schedule,
|
||||
has_active_plans,
|
||||
)
|
||||
from .llm_generator import MonthlyPlanLLMGenerator
|
||||
|
||||
@@ -24,7 +24,7 @@ class PlanManager:
|
||||
self.llm_generator = MonthlyPlanLLMGenerator()
|
||||
self.generation_running = False
|
||||
|
||||
async def ensure_and_generate_plans_if_needed(self, target_month: Optional[str] = None) -> bool:
|
||||
async def ensure_and_generate_plans_if_needed(self, target_month: str | None = None) -> bool:
|
||||
if target_month is None:
|
||||
target_month = datetime.now().strftime("%Y-%m")
|
||||
|
||||
@@ -48,7 +48,7 @@ class PlanManager:
|
||||
logger.info(f"当前月度计划内容:\n{plan_texts}")
|
||||
return True
|
||||
|
||||
async def _generate_monthly_plans_logic(self, target_month: Optional[str] = None) -> bool:
|
||||
async def _generate_monthly_plans_logic(self, target_month: str | None = None) -> bool:
|
||||
if self.generation_running:
|
||||
logger.info("月度计划生成任务已在运行中,跳过重复启动")
|
||||
return False
|
||||
@@ -90,7 +90,7 @@ class PlanManager:
|
||||
except Exception:
|
||||
return "1900-01"
|
||||
|
||||
async def archive_current_month_plans(self, target_month: Optional[str] = None):
|
||||
async def archive_current_month_plans(self, target_month: str | None = None):
|
||||
try:
|
||||
if target_month is None:
|
||||
target_month = datetime.now().strftime("%Y-%m")
|
||||
@@ -100,6 +100,6 @@ 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:
|
||||
async def get_plans_for_schedule(self, 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)
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import orjson
|
||||
import asyncio
|
||||
from datetime import datetime, time, timedelta
|
||||
from typing import Optional, List, Dict, Any
|
||||
from typing import Any
|
||||
|
||||
import orjson
|
||||
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
|
||||
from src.config.config import global_config
|
||||
from src.manager.async_task_manager import AsyncTask, async_task_manager
|
||||
|
||||
from .database import update_plan_usage
|
||||
from .llm_generator import ScheduleLLMGenerator
|
||||
from .plan_manager import PlanManager
|
||||
@@ -19,7 +20,7 @@ logger = get_logger("schedule_manager")
|
||||
|
||||
class ScheduleManager:
|
||||
def __init__(self):
|
||||
self.today_schedule: Optional[List[Dict[str, Any]]] = None
|
||||
self.today_schedule: list[dict[str, Any]] | None = None
|
||||
self.llm_generator = ScheduleLLMGenerator()
|
||||
self.plan_manager = PlanManager()
|
||||
self.daily_task_started = False
|
||||
@@ -63,7 +64,7 @@ class ScheduleManager:
|
||||
logger.info("尝试生成日程作为备用方案...")
|
||||
await self.generate_and_save_schedule()
|
||||
|
||||
async def _load_schedule_from_db(self, date_str: str) -> Optional[List[Dict[str, Any]]]:
|
||||
async def _load_schedule_from_db(self, 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))
|
||||
schedule_record = result.scalars().first()
|
||||
@@ -118,7 +119,7 @@ class ScheduleManager:
|
||||
logger.info("日程生成任务结束")
|
||||
|
||||
@staticmethod
|
||||
async def _save_schedule_to_db(date_str: str, schedule_data: List[Dict[str, Any]]):
|
||||
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))
|
||||
@@ -132,13 +133,13 @@ class ScheduleManager:
|
||||
await session.commit()
|
||||
|
||||
@staticmethod
|
||||
def _log_generated_schedule(date_str: str, schedule_data: List[Dict[str, Any]]):
|
||||
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"
|
||||
logger.info(schedule_str)
|
||||
|
||||
def get_current_activity(self) -> Optional[str]:
|
||||
def get_current_activity(self) -> str | None:
|
||||
if not global_config.planning_system.schedule_enable or not self.today_schedule:
|
||||
return None
|
||||
now = datetime.now().time()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# mmc/src/schedule/schemas.py
|
||||
|
||||
from datetime import datetime, time
|
||||
from typing import List
|
||||
|
||||
from pydantic import BaseModel, validator
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class ScheduleItem(BaseModel):
|
||||
class ScheduleData(BaseModel):
|
||||
"""完整日程数据的Pydantic模型"""
|
||||
|
||||
schedule: List[ScheduleItem]
|
||||
schedule: list[ScheduleItem]
|
||||
|
||||
@validator("schedule")
|
||||
def validate_schedule_completeness(cls, v):
|
||||
@@ -67,7 +67,7 @@ class ScheduleData(BaseModel):
|
||||
return v
|
||||
|
||||
@staticmethod
|
||||
def _check_24_hour_coverage(time_ranges: List[tuple]) -> bool:
|
||||
def _check_24_hour_coverage(time_ranges: list[tuple]) -> bool:
|
||||
"""检查时间段是否覆盖24小时"""
|
||||
if not time_ranges:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user