本次 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 映射等回归测试,并加入图片占位符退役设计文档。
106 lines
3.0 KiB
Python
106 lines
3.0 KiB
Python
"""migrate sections to canonical_markdown
|
||
|
||
将旧章节(有 sections 但 canonical_markdown 为空)从 sections 推导并写入 canonical_markdown。
|
||
同时创建 chapter_version 记录(source_type=migration)。
|
||
|
||
Revision ID: 0004_migrate_md
|
||
Revises: 0003_story_first
|
||
Create Date: 2026-03-19
|
||
|
||
"""
|
||
|
||
from typing import Sequence, Union
|
||
|
||
import sqlalchemy as sa
|
||
from alembic import op
|
||
from sqlalchemy.orm import Session, selectinload
|
||
|
||
revision: str = "0004_migrate_md"
|
||
down_revision: Union[str, Sequence[str], None] = "0003_story_first"
|
||
branch_labels: Union[str, Sequence[str], None] = None
|
||
depends_on: Union[str, Sequence[str], None] = None
|
||
|
||
|
||
def _sections_to_markdown(chapter) -> str:
|
||
"""从 sections 推导 markdown,与 helpers.sections_to_content_and_images 一致。"""
|
||
sections = getattr(chapter, "sections", None) or []
|
||
ordered = sorted(sections, key=lambda s: getattr(s, "order_index", 0))
|
||
parts = []
|
||
for s in ordered:
|
||
text = (getattr(s, "content", None) or "").strip()
|
||
if text:
|
||
parts.append(text)
|
||
img = _section_image_to_dict(s)
|
||
if img:
|
||
placeholder = (img.get("placeholder") or "").strip()
|
||
if placeholder:
|
||
parts.append(placeholder)
|
||
return "\n\n".join(parts) if parts else ""
|
||
|
||
|
||
def _section_image_to_dict(section) -> dict | None:
|
||
"""与 helpers.section_image_to_dict 一致。"""
|
||
from app.features.memoir.memoir_images.serializers import memoir_image_to_dict
|
||
|
||
if getattr(section, "image_record", None):
|
||
return memoir_image_to_dict(section.image_record)
|
||
return None
|
||
|
||
|
||
def upgrade() -> None:
|
||
from app.features.memoir.models import Chapter, ChapterSection, ChapterVersion
|
||
|
||
conn = op.get_bind()
|
||
session = Session(bind=conn)
|
||
|
||
chapters = (
|
||
session.query(Chapter)
|
||
.options(
|
||
selectinload(Chapter.sections).selectinload(ChapterSection.image_record),
|
||
)
|
||
.filter(
|
||
sa.or_(
|
||
Chapter.canonical_markdown.is_(None),
|
||
Chapter.canonical_markdown == "",
|
||
),
|
||
)
|
||
.all()
|
||
)
|
||
|
||
for ch in chapters:
|
||
md = _sections_to_markdown(ch)
|
||
if not md.strip():
|
||
continue
|
||
|
||
# 创建 chapter_version(source_type=migration)
|
||
import uuid
|
||
|
||
from sqlalchemy import func
|
||
|
||
count_stmt = sa.select(func.count(ChapterVersion.id)).where(
|
||
ChapterVersion.chapter_id == ch.id
|
||
)
|
||
version_no = (session.execute(count_stmt).scalar() or 0) + 1
|
||
|
||
version = ChapterVersion(
|
||
id=str(uuid.uuid4()),
|
||
chapter_id=ch.id,
|
||
version_no=version_no,
|
||
markdown_snapshot=md,
|
||
actor_type="system",
|
||
source_type="migration",
|
||
)
|
||
session.add(version)
|
||
session.flush()
|
||
|
||
ch.canonical_markdown = md
|
||
ch.current_version_id = version.id
|
||
|
||
# 由 alembic context 管理事务提交
|
||
session.close()
|
||
|
||
|
||
def downgrade() -> None:
|
||
# 数据迁移不可逆,downgrade 不清理 canonical_markdown
|
||
pass
|