""" PregoPal - 今日简报生成器插件 ============================= 汇总所有分析结果,生成结构化今日简报 JSON。 """ import json import datetime from pathlib import Path from plugins.base import LoopPlugin, PluginResult, LoopStage, LoopContext PRESETS_DIR = Path("data/presets") class BriefingGeneratorPlugin(LoopPlugin): """汇总分析为结构化 JSON 简报""" def stage(self) -> LoopStage: return LoopStage.BRIEF def name(self) -> str: return "briefing_generator" async def run(self, ctx: LoopContext) -> PluginResult: today = datetime.date.today().isoformat() # 构建简报 briefing = { "date": today, "generated_at": datetime.datetime.now().isoformat(), "trimester": ctx.briefing.get("trimester", "孕中期"), "weight_check": { "need_ask": ctx.briefing.get("need_ask_weight", False), "message": ctx.briefing.get("weight_quiz_message", ""), "evaluation": ctx.briefing.get("weight_evaluation", {}), }, "recipe_check": { "need_ask": ctx.briefing.get("need_ask_recipe", False), "message": ctx.briefing.get("recipe_quiz_message", ""), "available_recipes": [r.get("name", "") for r in ctx.family_recipes], }, "yesterday_diet": ctx.briefing.get("yesterday_diet", {}), "nutrient_focus": ctx.briefing.get("dri_analysis", {}).get("focus_nutrients", []), "recommended_foods": ctx.briefing.get("recommended_foods", []), "dri_summary": ctx.briefing.get("dri_analysis", {}).get("summary", ""), "family_memory": ctx.briefing.get("family_memory", {}), "conversation_topics": self._generate_topics(ctx), "thinking_keywords": "简报已生成", } ctx.briefing = briefing return PluginResult( success=True, data=briefing, message=f"今日简报已生成({today})" ) def _generate_topics(self, ctx: LoopContext) -> list[str]: """生成建议的对话话题""" topics = [] # 根据营养分析生成话题 focus = ctx.briefing.get("dri_analysis", {}).get("focus_nutrients", []) if focus: topics.append(f"今天想吃什么来补充{'、'.join(focus[:2])}?") # 根据体重检查生成话题 if ctx.briefing.get("need_ask_weight"): topics.append("今天称体重了吗?") # 根据菜谱检查生成话题 if ctx.briefing.get("need_ask_recipe"): topics.append("家里人会做什么菜呢?可以告诉我,我帮你们规划食谱!") if not topics: topics.append("今天感觉怎么样?有什么想吃的吗?") return topics