Fix dynamic memoir image limits
This commit is contained in:
@@ -168,6 +168,34 @@ def chapter_has_images_to_generate(images: list[dict] | None) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _select_placeholders_for_effective_max(
|
||||
placeholders: list[dict],
|
||||
existing_images: list[dict] | None,
|
||||
effective_max: int,
|
||||
) -> list[dict]:
|
||||
existing_placeholders = {
|
||||
item.get("placeholder")
|
||||
for item in normalize_image_assets(existing_images)
|
||||
if item.get("placeholder")
|
||||
}
|
||||
existing_count_in_content = sum(
|
||||
1 for item in placeholders if item.get("placeholder") in existing_placeholders
|
||||
)
|
||||
remaining_new_slots = max(0, effective_max - existing_count_in_content)
|
||||
|
||||
selected: list[dict] = []
|
||||
for item in placeholders:
|
||||
if item.get("placeholder") in existing_placeholders:
|
||||
selected.append(item)
|
||||
continue
|
||||
if remaining_new_slots <= 0:
|
||||
continue
|
||||
selected.append(item)
|
||||
remaining_new_slots -= 1
|
||||
|
||||
return [{**item, "index": index} for index, item in enumerate(selected)]
|
||||
|
||||
|
||||
def initialize_chapter_images(chapter) -> list[dict]:
|
||||
"""Parse IMAGE placeholders from chapter content and build pending image assets."""
|
||||
settings = MemoirImageSettings.from_env()
|
||||
@@ -177,7 +205,13 @@ def initialize_chapter_images(chapter) -> list[dict]:
|
||||
return chapter.images
|
||||
|
||||
prompt_service = MemoirImagePromptService(llm=None, settings=settings)
|
||||
placeholders = parse_image_placeholders(chapter.content, settings.max_per_chapter)
|
||||
effective_max = settings.effective_max_images(len(chapter.content or ""))
|
||||
all_placeholders = parse_image_placeholders(chapter.content, max_images=None)
|
||||
placeholders = _select_placeholders_for_effective_max(
|
||||
placeholders=all_placeholders,
|
||||
existing_images=chapter.images,
|
||||
effective_max=effective_max,
|
||||
)
|
||||
style = prompt_service.CATEGORY_STYLE_MAP.get(chapter.category, settings.default_style)
|
||||
chapter.images = _merge_chapter_image_assets(
|
||||
existing_images=chapter.images,
|
||||
@@ -188,8 +222,10 @@ def initialize_chapter_images(chapter) -> list[dict]:
|
||||
now_iso=datetime.now(timezone.utc).isoformat(),
|
||||
)
|
||||
logger.info(
|
||||
"章节图片初始化完成: chapter=%s, placeholders=%d, images=%d, statuses=%s",
|
||||
"章节图片初始化完成: chapter=%s, effective_max=%d, total_placeholders=%d, selected_placeholders=%d, images=%d, statuses=%s",
|
||||
chapter.id,
|
||||
effective_max,
|
||||
len(all_placeholders),
|
||||
len(placeholders),
|
||||
len(chapter.images or []),
|
||||
[item.get("status") for item in (chapter.images or [])],
|
||||
|
||||
Reference in New Issue
Block a user