feat(api): initialize memoir chapter image assets on creation

Made-with: Cursor
This commit is contained in:
Kevin
2026-03-10 16:03:49 +08:00
parent 67a469f380
commit d5ed2940aa
4 changed files with 63 additions and 3 deletions

View File

@@ -2,6 +2,6 @@
Celery 任务模块
"""
from .celery_app import celery_app
from .memoir_tasks import process_memoir_segments
from .memoir_tasks import process_memoir_segments, generate_chapter_images
__all__ = ["celery_app", "process_memoir_segments"]
__all__ = ["celery_app", "process_memoir_segments", "generate_chapter_images"]

View File

@@ -26,6 +26,9 @@ from agents.prompts.memory_prompts import (
CHAPTER_CATEGORIES,
)
from agents.prompts.profile_prompts import format_user_profile_context
from services.memoir_images.parser import build_initial_image_assets, parse_image_placeholders
from services.memoir_images.prompting import MemoirImagePromptService
from services.memoir_images.settings import MemoirImageSettings
logger = logging.getLogger(__name__)
@@ -71,6 +74,28 @@ def _update_task_status_sync(user_id: str, task_id: str, status: str, result: Di
except Exception as e:
logger.error(f"更新任务状态失败: {e}")
def initialize_chapter_images(chapter) -> list[dict]:
"""Parse IMAGE placeholders from chapter content and build pending image assets."""
settings = MemoirImageSettings.from_env()
if not settings.enabled:
chapter.images = []
return chapter.images
prompt_service = MemoirImagePromptService(llm=None, settings=settings)
placeholders = parse_image_placeholders(chapter.content, settings.max_per_chapter)
style = prompt_service.CATEGORY_STYLE_MAP.get(chapter.category, settings.default_style)
chapter.images = build_initial_image_assets(
placeholders=placeholders,
provider=settings.provider,
style=style,
size=settings.default_size,
now_iso=datetime.now(timezone.utc).isoformat(),
)
if chapter.images:
generate_chapter_images.delay(chapter.id)
return chapter.images
STAGE_KEYWORDS = {
"childhood": ["童年", "小时候", "出生", "家乡", "小镇"],
"education": ["上学", "学校", "老师", "同学", "教育", "大学"],
@@ -371,6 +396,8 @@ def process_memoir_segments(self, user_id: str, segment_ids: List[str]):
db.flush()
initialize_chapter_images(chapter)
# 更新 Book
stmt_book = select(Book).where(Book.user_id == user_id).order_by(Book.updated_at.desc())
result_book = db.execute(stmt_book)
@@ -497,3 +524,10 @@ def generate_chapter_content(self, user_id: str, stage: str, new_content: str):
except Exception as e:
logger.error(f"章节生成失败: {e}")
raise self.retry(exc=e)
@shared_task(bind=True, max_retries=3, default_retry_delay=30)
def generate_chapter_images(self, chapter_id: str):
"""Async task to generate images for a chapter's pending image assets."""
logger.info(f"图片生成任务(桩): chapter_id={chapter_id}")
return {"status": "stub", "chapter_id": chapter_id}