Files
life-echo/api/app/features/memoir/chapter_cover.py
Kevin 7f57f96c25 重构回忆录为 story-first / markdown-first 架构并整合图片意图与前端 UI 修复
本次 squash merge 将 codex-story-first-image-intent 的整体改动合入 development,核心内容包括:

1. 后端数据与迁移:新增 stories、story_versions、story_image_intents、chapter_cover_intents、assets 等模型与 Alembic 迁移,建立 story-first、markdown-first、asset-first 的主数据链路。

2. 生成与任务链:引入 StoryBuilderOrchestrator、ChapterComposerOrchestrator、story_image_tasks、chapter_cover_tasks,图片生成从正文占位符改为结构化 intent -> asset -> markdown 回填。

3. 并发与一致性:为 story/chapter intent 增加 claim_token、claimed_at、attempt_count,采用数据库原子 claim 为主、Redis 锁为辅,避免重复生成、锁误删和 processing 卡死。

4. Memoir 读写路径:章节 canonical_markdown 成为正文真源,列表/详情接口补齐 markdown、cover_asset、word_count 等字段,PDF 与 asset 解析链路同步升级。

5. Memory / Retrieval:扩展 transcript ingest、chunking、evidence 检索与 story 聚合基础设施,为后续 story-first RAG 与多 agent 编排提供底座。

6. App 端体验:章节页继续走 MarkdownRenderer 阅读链,同时吸收 fix3-19 的跨平台 UI glitch 修复;更新对话页、首页、文案资源与章节列表映射逻辑。

7. 测试与文档:补充 asset resolver、story image task、章节封面派发、markdown 映射等回归测试,并加入图片占位符退役设计文档。
2026-03-20 10:31:51 +08:00

61 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Chapter 封面意图 — 从本章 stories 或章节内容聚合生成封面 prompt。
封面不回写进 chapter 正文 markdown绑定到 chapters.cover_asset_id。
"""
from __future__ import annotations
def aggregate_cover_prompt_from_stories(
stories: list,
*,
chapter_title: str = "",
chapter_category: str = "",
) -> str:
"""
从本章 stories 聚合封面 prompt。
人物、地点、时间、情绪、时代背景。
"""
parts = []
if chapter_title:
parts.append(chapter_title)
if chapter_category:
parts.append(chapter_category)
for s in (stories or [])[:5]:
title = getattr(s, "title", None) or (
s.get("title") if isinstance(s, dict) else None
)
stage = getattr(s, "stage", None) or (
s.get("stage") if isinstance(s, dict) else None
)
summary = getattr(s, "summary", None) or (
s.get("summary") if isinstance(s, dict) else None
)
if title:
parts.append(title)
if stage:
parts.append(stage)
if summary:
parts.append((summary or "")[:100])
return "".join(p for p in parts if p)
def aggregate_cover_prompt_from_chapter(
chapter_title: str = "",
chapter_category: str = "",
markdown_excerpt: str = "",
) -> str:
"""
从章节标题、分类、正文摘要聚合封面 prompt。
用于无 story_links 的章节(兼容旧 memoir 流程)。
"""
parts = []
if chapter_title:
parts.append(chapter_title)
if chapter_category:
parts.append(chapter_category)
if markdown_excerpt:
parts.append(markdown_excerpt[:200].strip())
return "".join(p for p in parts if p) or "人生回忆录章节"