From afc46a97cfb6248f99c10cf91aa024285bb9b171 Mon Sep 17 00:00:00 2001 From: SaaS Frontend Agent Date: Fri, 7 Aug 2026 22:27:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20AI=20Code=20Review?= =?UTF-8?q?=20=E9=98=BB=E5=A1=9E=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. can_generate 自动修复中 assign_asset 加 try-except,避免单个失败导致整体崩溃 2. 移除 generation.py 冗余 DB 查询,诊断日志移入 can_generate 内部 3. 增加详细诊断日志:clips_with_asset 数量、config_asset_ids 数量 --- .../api/routes/templates_editor/generation.py | 17 +----------- apps/api/app/services/edit_plan_service.py | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/apps/api/app/api/routes/templates_editor/generation.py b/apps/api/app/api/routes/templates_editor/generation.py index 41fccfdce..c9bda4ce1 100755 --- a/apps/api/app/api/routes/templates_editor/generation.py +++ b/apps/api/app/api/routes/templates_editor/generation.py @@ -74,22 +74,7 @@ def generate_editor_draft( plan_svc, plan_id, plan_check, clips_without_asset, asset_library_repo, asset_repo ) - # ── 防御性诊断日志:fallback 链路结束后、can_generate 前 ── - _post_fallback_plan = plan_svc.get_plan_or_raise(plan_id) - _post_fallback_clips = plan_svc.list_clips(plan_id) - _post_fallback_asset_ids = (_post_fallback_plan.config or {}).get("asset_ids", []) - _post_fallback_clips_with_asset = sum(1 for c in _post_fallback_clips if c.asset_id) - logger.info( - "模板编辑器 fallback 链路完成: plan=%s status=%s " - "total_clips=%d clips_with_asset=%d config_asset_ids_count=%d", - plan_id, - _post_fallback_plan.status, - len(_post_fallback_clips), - _post_fallback_clips_with_asset, - len(_post_fallback_asset_ids), - ) - - # 检查是否可生成(含最后防线自动修复) + # 检查是否可生成(含最后防线自动修复 + 诊断日志) try: can_gen, reason = plan_svc.can_generate(plan_id) except ValueError as exc: diff --git a/apps/api/app/services/edit_plan_service.py b/apps/api/app/services/edit_plan_service.py index ef8e81f35..783ab0c76 100755 --- a/apps/api/app/services/edit_plan_service.py +++ b/apps/api/app/services/edit_plan_service.py @@ -591,6 +591,17 @@ class EditPlanService: # 检查是否至少有一个片段分配了素材 has_asset = any(c.asset_id for c in clips) + config_asset_ids_count = len((plan.config or {}).get("asset_ids", [])) + clips_with_asset_count = sum(1 for c in clips if c.asset_id) + logger.info( + "can_generate 诊断: plan=%s status=%s total_clips=%d " + "clips_with_asset=%d config_asset_ids_count=%d", + plan_id, + plan.status, + len(clips), + clips_with_asset_count, + config_asset_ids_count, + ) if not has_asset: # ── 最后防线:自动从 config.asset_ids 分配素材 ── config_asset_ids = (plan.config or {}).get("asset_ids", []) @@ -602,12 +613,24 @@ class EditPlanService: len(config_asset_ids), ) clips_without_asset = [c for c in clips if not c.asset_id] + assigned_count = 0 for i, clip in enumerate(clips_without_asset): asset_idx = i % len(config_asset_ids) - self.assign_asset(clip.id, config_asset_ids[asset_idx]) + try: + self.assign_asset(clip.id, config_asset_ids[asset_idx]) + assigned_count += 1 + except Exception as exc: + logger.warning( + "can_generate 最后防线: plan=%s clip=%s 分配素材 %s 失败: %s", + plan_id, + clip.id, + config_asset_ids[asset_idx], + exc, + ) logger.info( - "can_generate 最后防线: plan=%s 已为 %d 个片段分配素材", + "can_generate 最后防线: plan=%s 已为 %d/%d 个片段分配素材", plan_id, + assigned_count, len(clips_without_asset), ) # 重新加载 clips 验证分配结果