From b7fba5c8ed2ab337b61208df4287ba1ee21d749b Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Tue, 23 Sep 2025 14:13:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(schedule):=20=E4=BF=AE=E5=A4=8D=E6=97=A5?= =?UTF-8?q?=E7=A8=8B=E7=94=9F=E6=88=90=E5=9C=A8=E8=BF=9E=E7=BB=AD=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E6=97=A0=E9=99=90=E9=87=8D=E8=AF=95=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 先前的日程生成逻辑使用无限循环进行重试。在 LLM 服务持续失败或返回无效数据的情况下,这可能导致程序陷入死循环。 本次修改将重试机制改为有固定上限(3次)的循环。如果所有尝试均失败,将记录错误并返回 None,从而确保程序的健壮性。 --- src/schedule/llm_generator.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/schedule/llm_generator.py b/src/schedule/llm_generator.py index 35e35b0b5..5c1464c71 100644 --- a/src/schedule/llm_generator.py +++ b/src/schedule/llm_generator.py @@ -94,11 +94,10 @@ class ScheduleLLMGenerator: 请你扮演我,以我的身份和口吻,为我生成一份完整的24小时日程表。 """ - attempt = 0 - while True: - attempt += 1 + max_retries = 3 + for attempt in range(1, max_retries + 1): try: - logger.info(f"正在生成日程 (第 {attempt} 次尝试)") + logger.info(f"正在生成日程 (第 {attempt}/{max_retries} 次尝试)") prompt = base_prompt if attempt > 1: failure_hint = f""" @@ -118,12 +117,16 @@ class ScheduleLLMGenerator: return schedule_data else: logger.warning(f"第 {attempt} 次生成的日程验证失败,继续重试...") - await asyncio.sleep(2) except Exception as e: logger.error(f"第 {attempt} 次生成日程失败: {e}") - logger.info("继续重试...") - await asyncio.sleep(3) + + if attempt < max_retries: + logger.info("2秒后继续重试...") + await asyncio.sleep(2) + + logger.error("所有尝试都失败,无法生成日程,将会在下次启动时自动重试") + return None @staticmethod def _validate_schedule_with_pydantic(schedule_data) -> bool: