重构回忆录为 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 映射等回归测试,并加入图片占位符退役设计文档。
This commit is contained in:
@@ -2,6 +2,8 @@ import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.features.memoir.asset_resolver import strip_legacy_image_placeholders
|
||||
|
||||
from .json_payload import extract_json_payload
|
||||
from .schema import IMAGE_STATUS_PENDING
|
||||
|
||||
@@ -12,6 +14,7 @@ PLACEHOLDER_RE = re.compile(
|
||||
|
||||
|
||||
def parse_image_placeholders(content: str, max_images: int) -> list[dict[str, Any]]:
|
||||
"""离线迁移/调试用:解析正文中的 IMAGE 占位符。"""
|
||||
items: list[dict[str, Any]] = []
|
||||
for match in PLACEHOLDER_RE.finditer(content or ""):
|
||||
description = (match.group(1) or match.group(2) or "").strip()
|
||||
@@ -56,44 +59,12 @@ def build_initial_image_assets(
|
||||
]
|
||||
|
||||
|
||||
def split_narrative_to_sections(narrative: str) -> list[dict[str, Any]]:
|
||||
"""
|
||||
将带 {{IMAGE:...}} 占位符的正文按占位符拆成多段。
|
||||
返回 list[dict],每项含:
|
||||
- content: 本段纯文本(不含占位符)
|
||||
- placeholder_info: 本段后的配图占位信息,或 None(最后一段无图)
|
||||
"""
|
||||
if not (narrative or narrative.strip()):
|
||||
return []
|
||||
placeholders = parse_image_placeholders(narrative, max_images=None)
|
||||
sections: list[dict[str, Any]] = []
|
||||
for i in range(len(placeholders) + 1):
|
||||
if i == 0:
|
||||
start = 0
|
||||
else:
|
||||
prev = placeholders[i - 1]
|
||||
start = prev["start_offset"] + len(prev["placeholder"])
|
||||
if i < len(placeholders):
|
||||
end = placeholders[i]["start_offset"]
|
||||
placeholder_info = placeholders[i]
|
||||
else:
|
||||
end = len(narrative)
|
||||
placeholder_info = None
|
||||
content = narrative[start:end]
|
||||
if isinstance(content, str):
|
||||
content = content.strip()
|
||||
sections.append(
|
||||
{"content": content or "", "placeholder_info": placeholder_info}
|
||||
)
|
||||
return sections
|
||||
|
||||
|
||||
def parse_narrative_json(raw: str) -> list[dict[str, Any]]:
|
||||
"""
|
||||
解析 LLM 输出的 JSON 格式叙事。
|
||||
返回与 split_narrative_to_sections 相同结构:list[dict],每项含 content、placeholder_info。
|
||||
解析 LLM 输出的 JSON 叙事(paragraphs)。
|
||||
不根据 image_description 生成配图占位;插图由 story/chapter 结构化流程单独处理。
|
||||
"""
|
||||
if not (raw or raw.strip()):
|
||||
if not raw or not str(raw).strip():
|
||||
return []
|
||||
try:
|
||||
payload = extract_json_payload(raw)
|
||||
@@ -105,33 +76,34 @@ def parse_narrative_json(raw: str) -> list[dict[str, Any]]:
|
||||
return []
|
||||
|
||||
result: list[dict[str, Any]] = []
|
||||
for i, p in enumerate(paragraphs):
|
||||
for p in paragraphs:
|
||||
if not isinstance(p, dict):
|
||||
continue
|
||||
content = (p.get("content") or "").strip()
|
||||
desc = (p.get("image_description") or "").strip()
|
||||
placeholder_info = None
|
||||
if desc:
|
||||
placeholder_info = {
|
||||
"placeholder": f"{{{{IMAGE:{desc}}}}}",
|
||||
"description": desc,
|
||||
"index": i,
|
||||
"start_offset": 0,
|
||||
}
|
||||
result.append({"content": content, "placeholder_info": placeholder_info})
|
||||
if content:
|
||||
result.append({"content": content, "placeholder_info": None})
|
||||
return result
|
||||
|
||||
|
||||
def split_plain_narrative_into_sections(narrative: str) -> list[dict[str, Any]]:
|
||||
"""非 JSON 叙事:去掉遗留占位符后按空行拆段,不产生段落配图。"""
|
||||
text = strip_legacy_image_placeholders(narrative or "")
|
||||
if not text.strip():
|
||||
return []
|
||||
parts = [p.strip() for p in text.split("\n\n") if p.strip()]
|
||||
return [{"content": p, "placeholder_info": None} for p in parts]
|
||||
|
||||
|
||||
def parse_narrative_to_sections(narrative: str) -> list[dict[str, Any]]:
|
||||
"""
|
||||
将 narrative 解析为 sections。优先尝试 JSON 格式,失败则回退到占位符解析。
|
||||
返回与 split_narrative_to_sections 相同结构。
|
||||
将 narrative 解析为 sections。
|
||||
JSON(paragraphs)走 parse_narrative_json;否则剥离占位符后按段拆分。
|
||||
"""
|
||||
if not (narrative or narrative.strip()):
|
||||
if not narrative or not str(narrative).strip():
|
||||
return []
|
||||
stripped = narrative.strip()
|
||||
if stripped.startswith("{") and "paragraphs" in stripped:
|
||||
segments = parse_narrative_json(narrative)
|
||||
if segments:
|
||||
return segments
|
||||
return split_narrative_to_sections(narrative)
|
||||
return split_plain_narrative_into_sections(narrative)
|
||||
|
||||
Reference in New Issue
Block a user