2026-03-20 15:15:35 +08:00
|
|
|
|
"""将 NarrativeAgent / LLM 返回的 JSON 或纯文本规范为 markdown 正文。"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def narrative_to_markdown(narrative: str) -> str:
|
|
|
|
|
|
"""
|
|
|
|
|
|
将 narrative(JSON paragraphs 或纯文本)转为 markdown。
|
|
|
|
|
|
与已删除的 ChapterComposerOrchestrator._to_markdown 行为一致。
|
|
|
|
|
|
"""
|
|
|
|
|
|
if not narrative or not str(narrative).strip():
|
|
|
|
|
|
return ""
|
|
|
|
|
|
stripped = narrative.strip()
|
|
|
|
|
|
if stripped.startswith("{") and "paragraphs" in stripped:
|
|
|
|
|
|
try:
|
|
|
|
|
|
data = json.loads(stripped)
|
|
|
|
|
|
paras = data.get("paragraphs", [])
|
|
|
|
|
|
if isinstance(paras, list):
|
|
|
|
|
|
parts = []
|
|
|
|
|
|
for p in paras:
|
|
|
|
|
|
if isinstance(p, dict):
|
|
|
|
|
|
text = p.get("content", p.get("text", ""))
|
|
|
|
|
|
else:
|
|
|
|
|
|
text = str(p)
|
|
|
|
|
|
if text.strip():
|
|
|
|
|
|
parts.append(text.strip())
|
|
|
|
|
|
return "\n\n".join(parts)
|
|
|
|
|
|
return stripped
|
|
|
|
|
|
except json.JSONDecodeError:
|
2026-04-03 10:12:59 +08:00
|
|
|
|
# 不得以伪 JSON 字符串落库;上层 _coalesce_story_markdown 会回退口述/旧文
|
|
|
|
|
|
return ""
|
2026-03-20 15:15:35 +08:00
|
|
|
|
return stripped
|