Spaces:
Runtime error
Runtime error
| """ | |
| PregoPal - 预设文件写入插件 | |
| =========================== | |
| 将今日简报写入 data/presets/ 目录,供次日使用。 | |
| """ | |
| import json | |
| import datetime | |
| from pathlib import Path | |
| from plugins.base import LoopPlugin, PluginResult, LoopStage, LoopContext | |
| from loop import DailyStatus | |
| PRESETS_DIR = Path("data/presets") | |
| class PresetWriterPlugin(LoopPlugin): | |
| """生成明日预设文件""" | |
| def stage(self) -> LoopStage: | |
| return LoopStage.CONSOLIDATE | |
| def name(self) -> str: | |
| return "preset_writer" | |
| async def run(self, ctx: LoopContext) -> PluginResult: | |
| PRESETS_DIR.mkdir(parents=True, exist_ok=True) | |
| today = datetime.date.today().isoformat() | |
| tomorrow = (datetime.date.today() + datetime.timedelta(days=1)).isoformat() | |
| # 构建明日预设 | |
| preset = { | |
| "date": tomorrow, | |
| "generated_from": today, | |
| "generated_at": datetime.datetime.now().isoformat(), | |
| "trimester": ctx.briefing.get("trimester", "孕中期"), | |
| "nutrient_focus": ctx.briefing.get("nutrient_focus", []), | |
| "recommended_foods": ctx.briefing.get("recommended_foods", []), | |
| "need_ask_weight": ctx.briefing.get("need_ask_weight", False), | |
| "need_ask_recipe": ctx.briefing.get("need_ask_recipe", False), | |
| "conversation_topics": ctx.briefing.get("conversation_topics", []), | |
| "family_memory_summary": str(ctx.briefing.get("family_memory", {})), | |
| "three_day_summary": ctx.briefing.get("three_day_summary", {}), | |
| } | |
| # 写入文件 | |
| preset_file = PRESETS_DIR / f"今日简报_{tomorrow}.json" | |
| with open(preset_file, 'w', encoding='utf-8') as f: | |
| json.dump(preset, f, ensure_ascii=False, indent=2) | |
| # 标记今日总结完成 | |
| DailyStatus.mark_summary_done() | |
| return PluginResult( | |
| success=True, | |
| data={"preset_file": str(preset_file)}, | |
| message=f"明日预设已写入 {preset_file.name}" | |
| ) | |