fix/various fixes

This commit is contained in:
Kevin
2026-03-20 15:15:35 +08:00
parent 7f57f96c25
commit 7317bf10cd
112 changed files with 3790 additions and 2242 deletions

View File

@@ -0,0 +1,33 @@
"""将 NarrativeAgent / LLM 返回的 JSON 或纯文本规范为 markdown 正文。"""
from __future__ import annotations
import json
def narrative_to_markdown(narrative: str) -> str:
"""
将 narrativeJSON 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:
return stripped
return stripped