From c0906723ac93b2afece11f019cff6c72a38cb023 Mon Sep 17 00:00:00 2001 From: yangshilin <2157598560@qq.com> Date: Fri, 13 Mar 2026 16:24:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=87=8F=E5=B0=91=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=95=B0=E9=87=8F=EF=BC=88=E5=BD=93=E5=89=8D?= =?UTF-8?q?3=E4=B8=AAsection=E5=AF=B9=E5=BA=94=E4=B8=80=E5=BC=A0=E5=9B=BE?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/tasks/memoir_tasks.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/api/tasks/memoir_tasks.py b/api/tasks/memoir_tasks.py index 2a734f9..905defb 100644 --- a/api/tasks/memoir_tasks.py +++ b/api/tasks/memoir_tasks.py @@ -302,22 +302,26 @@ def _save_narrative_to_sections(db: Session, chapter, narrative: str, title: str settings = MemoirImageSettings.from_env() prompt_service = MemoirImagePromptService(llm=None, settings=settings) if settings.enabled else None - effective_max = settings.effective_max_images(len(narrative_to_parse)) if settings.enabled else 0 - all_placeholders = [s["placeholder_info"] for s in segments if s.get("placeholder_info")] - placeholders = _select_placeholders_for_effective_max( - placeholders=all_placeholders, - existing_images=[], - effective_max=effective_max, - ) if settings.enabled else [] - selected_placeholder_set = {p.get("placeholder") for p in placeholders} - # 按顺序创建 section,保证每个 section 的 content 与 image 一一对应(order_index 严格递增) + # 每 3 个 section 对应 1 张图片,其他 section 的 image_id 为空 + def _should_have_image(order_idx: int) -> bool: + return (order_idx % 3) == 2 + + def _placeholder_for_segment(seg: dict, order_idx: int) -> dict | None: + ph = seg.get("placeholder_info") + if ph and ph.get("placeholder") and ph.get("description"): + return ph + content = (seg.get("content") or "").strip() + desc = (content[:50] + "…") if len(content) > 50 else (content or "章节配图") + return {"placeholder": f"{{{{{{{{IMAGE:{desc}}}}}}}}}", "description": desc} + + # 按顺序创建 section,每 3 个 section 对应 1 张配图 for i, seg in enumerate(segments): order_idx = order_base + i content = (seg.get("content") or "").strip() - ph = seg.get("placeholder_info") image_asset = None - if ph and settings.enabled and ph.get("placeholder") in selected_placeholder_set: + if settings.enabled and _should_have_image(order_idx): + ph = _placeholder_for_segment(seg, order_idx) style = prompt_service.CATEGORY_STYLE_MAP.get(category, settings.default_style) if prompt_service else settings.default_style image_asset = build_initial_image_assets( [ph],