Files
life-echo/api/app/features/story/backfill.py
Kevin 8af37e5e8e 修复:CI 部署环境与 ref 错配、迁移碎片化、图片意图 source_span、章节物化脏版式、会话历史与本地语音不一致
新增:TTS 上传 COS 与分片、章节 reading_segments 物化与快照、markdown 清洗、会话消息 repository、语音 store 重构与相关测试
2026-03-20 16:43:02 +08:00

32 lines
986 B
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.
"""
Story 图片回填 — 将 asset:// 引用追加到 markdown 末尾。
图片生成成功后,在正文最后插入 ![alt](asset://asset_id)。
alt 使用原始 prompt 短文prompt_brief而非模板拼接后的完整出图 prompt。
"""
def _escape_markdown_image_alt(text: str) -> str:
"""在 ![alt](...) 的 alt 中安全转义 \\ 与 ]。"""
return text.replace("\\", "\\\\").replace("]", "\\]")
def backfill_image_into_markdown(
markdown: str,
asset_id: str,
image_alt: str,
) -> str:
"""
将图片引用追加到 markdown 末尾。
格式:![image_alt](asset://asset_id)
image_alt 一般为 intent.prompt_brief原始图片提示短文
"""
raw = (image_alt or "").strip() or "主插图"
alt = _escape_markdown_image_alt(raw)
img_ref = f"![{alt}](asset://{asset_id})"
if not markdown or not markdown.strip():
return img_ref
body = markdown.rstrip()
return f"{body}\n\n{img_ref}\n"