Spaces:
Runtime error
Runtime error
| """ | |
| PregoPal - 昨日饮食总结插件 | |
| =========================== | |
| 读取昨日饮食日志,汇总三餐。 | |
| """ | |
| import datetime | |
| from pathlib import Path | |
| from plugins.base import LoopPlugin, PluginResult, LoopStage, LoopContext | |
| class DietSummaryPlugin(LoopPlugin): | |
| """读取昨日饮食日志,汇总三餐""" | |
| def stage(self) -> LoopStage: | |
| return LoopStage.SUMMARIZE | |
| def name(self) -> str: | |
| return "diet_summary" | |
| async def run(self, ctx: LoopContext) -> PluginResult: | |
| logs_dir = Path("data/logs") | |
| yesterday = (datetime.date.today() - datetime.timedelta(days=1)).isoformat() | |
| log_file = logs_dir / f"饮食日志_{yesterday}.md" | |
| if not log_file.exists(): | |
| ctx.briefing["yesterday_diet"] = {"status": "no_record", "summary": "昨日无饮食记录"} | |
| return PluginResult( | |
| success=True, | |
| data={"status": "no_record"}, | |
| message="昨日无饮食记录" | |
| ) | |
| content = log_file.read_text(encoding='utf-8') | |
| summary = {"date": yesterday, "meals": {}, "mood": "", "notes": ""} | |
| current_meal = None | |
| for line in content.split('\n'): | |
| line = line.strip() | |
| if line.startswith('### '): | |
| current_meal = line.replace('### ', '').strip() | |
| elif line.startswith('- ') and current_meal: | |
| food = line.replace('- ', '').strip() | |
| if current_meal in ("早餐", "午餐", "晚餐", "加餐"): | |
| summary["meals"][current_meal] = food | |
| elif "今日心情" in line: | |
| summary["mood"] = line | |
| ctx.briefing["yesterday_diet"] = { | |
| "status": "found", | |
| "summary": summary, | |
| "meal_count": len(summary["meals"]), | |
| } | |
| return PluginResult( | |
| success=True, | |
| data=summary, | |
| message=f"已读取昨日饮食日志({len(summary['meals'])} 餐)" | |
| ) | |