1
0

extract_chapter_context.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. extract_chapter_context.py - extract chapter writing context
  5. Features:
  6. - chapter outline snippet
  7. - previous chapter summaries (prefers .webnovel/summaries)
  8. - compact state summary
  9. - ContextManager contract sections (reader_signal / genre_profile / writing_guidance)
  10. """
  11. from __future__ import annotations
  12. import argparse
  13. import json
  14. import re
  15. import sys
  16. from pathlib import Path
  17. from typing import Any, Dict, List
  18. from runtime_compat import enable_windows_utf8_stdio
  19. try:
  20. from chapter_paths import find_chapter_file
  21. except ImportError: # pragma: no cover
  22. from scripts.chapter_paths import find_chapter_file
  23. def _ensure_scripts_path():
  24. scripts_dir = Path(__file__).resolve().parent
  25. if str(scripts_dir) not in sys.path:
  26. sys.path.insert(0, str(scripts_dir))
  27. def find_project_root(start_path: Path | None = None) -> Path:
  28. """Find project root containing `.webnovel` directory."""
  29. if start_path is None:
  30. start_path = Path.cwd()
  31. search_paths = [
  32. start_path,
  33. start_path / "webnovel-project",
  34. start_path.parent,
  35. ]
  36. for path in search_paths:
  37. if (path / ".webnovel").exists():
  38. return path
  39. raise FileNotFoundError("未找到 .webnovel 目录,请确认项目路径")
  40. def extract_chapter_outline(project_root: Path, chapter_num: int) -> str:
  41. """Extract chapter outline segment from volume outline file."""
  42. volume_num = (chapter_num - 1) // 50 + 1
  43. outline_candidates = [
  44. project_root / "大纲" / f"第{volume_num}卷-详细大纲.md",
  45. project_root / "大纲" / f"第{volume_num}卷 详细大纲.md",
  46. project_root / "大纲" / f"第{volume_num}卷详细大纲.md",
  47. ]
  48. outline_file = next((p for p in outline_candidates if p.exists()), None)
  49. if outline_file is None:
  50. tried = " / ".join(str(p) for p in outline_candidates)
  51. return f"⚠️ 大纲文件不存在,已尝试: {tried}"
  52. content = outline_file.read_text(encoding="utf-8")
  53. pattern = rf"###\s*第\s*{chapter_num}\s*章[::]\s*(.+?)(?=###\s*第\s*\d+\s*章|##\s|$)"
  54. match = re.search(pattern, content, re.DOTALL)
  55. if not match:
  56. pattern2 = rf"###\s*第{chapter_num}章[::]\s*(.+?)(?=###\s*第\d+章|##\s|$)"
  57. match = re.search(pattern2, content, re.DOTALL)
  58. if match:
  59. outline = match.group(0).strip()
  60. if len(outline) > 1500:
  61. outline = outline[:1500] + "\n...(已截断)"
  62. return outline
  63. return f"⚠️ 未找到第 {chapter_num} 章的大纲"
  64. def _load_summary_file(project_root: Path, chapter_num: int) -> str:
  65. """Load summary section from `.webnovel/summaries/chNNNN.md`."""
  66. summary_path = project_root / ".webnovel" / "summaries" / f"ch{chapter_num:04d}.md"
  67. if not summary_path.exists():
  68. return ""
  69. text = summary_path.read_text(encoding="utf-8")
  70. summary_match = re.search(r"##\s*剧情摘要\s*\r?\n(.+?)(?=\r?\n##|$)", text, re.DOTALL)
  71. if summary_match:
  72. return summary_match.group(1).strip()
  73. return ""
  74. def extract_chapter_summary(project_root: Path, chapter_num: int) -> str:
  75. """Extract chapter summary, fallback to chapter body head."""
  76. summary = _load_summary_file(project_root, chapter_num)
  77. if summary:
  78. return summary
  79. chapter_file = find_chapter_file(project_root, chapter_num)
  80. if not chapter_file or not chapter_file.exists():
  81. return f"⚠️ 第{chapter_num}章文件不存在"
  82. content = chapter_file.read_text(encoding="utf-8")
  83. summary_match = re.search(r"##\s*本章摘要\s*\r?\n(.+?)(?=\r?\n##|$)", content, re.DOTALL)
  84. if summary_match:
  85. return summary_match.group(1).strip()
  86. stats_match = re.search(r"##\s*本章统计\s*\r?\n(.+?)(?=\r?\n##|$)", content, re.DOTALL)
  87. if stats_match:
  88. return f"[无摘要,仅统计]\n{stats_match.group(1).strip()}"
  89. lines = content.split("\n")
  90. text_lines = [line for line in lines if not line.startswith("#") and line.strip()]
  91. text = "\n".join(text_lines)[:500]
  92. return f"[自动截取前500字]\n{text}..."
  93. def extract_state_summary(project_root: Path) -> str:
  94. """Extract key fields from `.webnovel/state.json`."""
  95. state_file = project_root / ".webnovel" / "state.json"
  96. if not state_file.exists():
  97. return "⚠️ state.json 不存在"
  98. state = json.loads(state_file.read_text(encoding="utf-8"))
  99. summary_parts: List[str] = []
  100. if "progress" in state:
  101. progress = state["progress"]
  102. summary_parts.append(
  103. f"**进度**: 第{progress.get('current_chapter', '?')}章 / {progress.get('total_words', '?')}字"
  104. )
  105. if "protagonist_state" in state:
  106. ps = state["protagonist_state"]
  107. power = ps.get("power", {})
  108. summary_parts.append(f"**主角实力**: {power.get('realm', '?')} {power.get('layer', '?')}层")
  109. summary_parts.append(f"**当前位置**: {ps.get('location', '?')}")
  110. golden_finger = ps.get("golden_finger", {})
  111. summary_parts.append(
  112. f"**金手指**: {golden_finger.get('name', '?')} Lv.{golden_finger.get('level', '?')}"
  113. )
  114. if "strand_tracker" in state:
  115. tracker = state["strand_tracker"]
  116. history = tracker.get("history", [])[-5:]
  117. if history:
  118. items: List[str] = []
  119. for row in history:
  120. if not isinstance(row, dict):
  121. continue
  122. chapter = row.get("chapter", "?")
  123. strand = row.get("strand") or row.get("dominant") or "unknown"
  124. items.append(f"Ch{chapter}:{strand}")
  125. if items:
  126. summary_parts.append(f"**近5章Strand**: {', '.join(items)}")
  127. plot_threads = state.get("plot_threads", {}) if isinstance(state.get("plot_threads"), dict) else {}
  128. foreshadowing = plot_threads.get("foreshadowing", [])
  129. if isinstance(foreshadowing, list) and foreshadowing:
  130. active = [row for row in foreshadowing if row.get("status") in {"active", "未回收"}]
  131. urgent = [row for row in active if row.get("urgency", 0) > 50]
  132. if urgent:
  133. urgent_list = [
  134. f"{row.get('content', '?')[:30]}... (紧急度:{row.get('urgency')})"
  135. for row in urgent[:3]
  136. ]
  137. summary_parts.append(f"**紧急伏笔**: {'; '.join(urgent_list)}")
  138. return "\n".join(summary_parts)
  139. def _load_contract_context(project_root: Path, chapter_num: int) -> Dict[str, Any]:
  140. """Build context via ContextManager and return selected sections."""
  141. _ensure_scripts_path()
  142. from data_modules.config import DataModulesConfig
  143. from data_modules.context_manager import ContextManager
  144. config = DataModulesConfig.from_project_root(project_root)
  145. manager = ContextManager(config)
  146. payload = manager.build_context(
  147. chapter=chapter_num,
  148. template="plot",
  149. use_snapshot=True,
  150. save_snapshot=True,
  151. max_chars=8000,
  152. )
  153. sections = payload.get("sections", {})
  154. return {
  155. "context_contract_version": (payload.get("meta") or {}).get("context_contract_version"),
  156. "context_weight_stage": (payload.get("meta") or {}).get("context_weight_stage"),
  157. "reader_signal": (sections.get("reader_signal") or {}).get("content", {}),
  158. "genre_profile": (sections.get("genre_profile") or {}).get("content", {}),
  159. "writing_guidance": (sections.get("writing_guidance") or {}).get("content", {}),
  160. }
  161. def build_chapter_context_payload(project_root: Path, chapter_num: int) -> Dict[str, Any]:
  162. """Assemble full chapter context payload for text/json output."""
  163. outline = extract_chapter_outline(project_root, chapter_num)
  164. prev_summaries = []
  165. for prev_ch in range(max(1, chapter_num - 2), chapter_num):
  166. summary = extract_chapter_summary(project_root, prev_ch)
  167. prev_summaries.append(f"### 第{prev_ch}章摘要\n{summary}")
  168. state_summary = extract_state_summary(project_root)
  169. contract_context = _load_contract_context(project_root, chapter_num)
  170. return {
  171. "chapter": chapter_num,
  172. "outline": outline,
  173. "previous_summaries": prev_summaries,
  174. "state_summary": state_summary,
  175. "context_contract_version": contract_context.get("context_contract_version"),
  176. "context_weight_stage": contract_context.get("context_weight_stage"),
  177. "reader_signal": contract_context.get("reader_signal", {}),
  178. "genre_profile": contract_context.get("genre_profile", {}),
  179. "writing_guidance": contract_context.get("writing_guidance", {}),
  180. }
  181. def _render_text(payload: Dict[str, Any]) -> str:
  182. chapter_num = payload.get("chapter")
  183. lines: List[str] = []
  184. lines.append(f"# 第 {chapter_num} 章创作上下文")
  185. lines.append("")
  186. lines.append("## 本章大纲")
  187. lines.append("")
  188. lines.append(str(payload.get("outline", "")))
  189. lines.append("")
  190. lines.append("---")
  191. lines.append("")
  192. lines.append("## 前文摘要")
  193. lines.append("")
  194. for item in payload.get("previous_summaries", []):
  195. lines.append(item)
  196. lines.append("")
  197. lines.append("---")
  198. lines.append("")
  199. lines.append("## 当前状态")
  200. lines.append("")
  201. lines.append(str(payload.get("state_summary", "")))
  202. lines.append("")
  203. contract_version = payload.get("context_contract_version")
  204. if contract_version:
  205. lines.append(f"## Contract ({contract_version})")
  206. lines.append("")
  207. stage = payload.get("context_weight_stage")
  208. if stage:
  209. lines.append(f"- 上下文阶段权重: {stage}")
  210. lines.append("")
  211. writing_guidance = payload.get("writing_guidance") or {}
  212. guidance_items = writing_guidance.get("guidance_items") or []
  213. checklist = writing_guidance.get("checklist") or []
  214. checklist_score = writing_guidance.get("checklist_score") or {}
  215. if guidance_items or checklist:
  216. lines.append("## 写作执行建议")
  217. lines.append("")
  218. for idx, item in enumerate(guidance_items, start=1):
  219. lines.append(f"{idx}. {item}")
  220. if checklist:
  221. total_weight = 0.0
  222. required_count = 0
  223. for row in checklist:
  224. if isinstance(row, dict):
  225. try:
  226. total_weight += float(row.get("weight") or 0)
  227. except (TypeError, ValueError):
  228. pass
  229. if row.get("required"):
  230. required_count += 1
  231. lines.append("")
  232. lines.append("### 执行检查清单(可评分)")
  233. lines.append("")
  234. lines.append(f"- 项目数: {len(checklist)}")
  235. lines.append(f"- 总权重: {total_weight:.2f}")
  236. lines.append(f"- 必做项: {required_count}")
  237. lines.append("")
  238. for idx, row in enumerate(checklist, start=1):
  239. if not isinstance(row, dict):
  240. lines.append(f"{idx}. {row}")
  241. continue
  242. label = str(row.get("label") or "").strip() or "未命名项"
  243. weight = row.get("weight")
  244. required_tag = "必做" if row.get("required") else "可选"
  245. verify_hint = str(row.get("verify_hint") or "").strip()
  246. lines.append(f"{idx}. [{required_tag}][w={weight}] {label}")
  247. if verify_hint:
  248. lines.append(f" - 验收: {verify_hint}")
  249. if checklist_score:
  250. lines.append("")
  251. lines.append("### 执行评分")
  252. lines.append("")
  253. lines.append(f"- 评分: {checklist_score.get('score')}")
  254. lines.append(f"- 完成率: {checklist_score.get('completion_rate')}")
  255. lines.append(f"- 必做完成率: {checklist_score.get('required_completion_rate')}")
  256. lines.append("")
  257. reader_signal = payload.get("reader_signal") or {}
  258. review_trend = reader_signal.get("review_trend") or {}
  259. if review_trend:
  260. overall_avg = review_trend.get("overall_avg")
  261. lines.append("## 追读信号")
  262. lines.append("")
  263. lines.append(f"- 最近审查均分: {overall_avg}")
  264. low_ranges = reader_signal.get("low_score_ranges") or []
  265. if low_ranges:
  266. lines.append(f"- 低分区间数: {len(low_ranges)}")
  267. lines.append("")
  268. genre_profile = payload.get("genre_profile") or {}
  269. if genre_profile.get("genre"):
  270. lines.append("## 题材锚定")
  271. lines.append("")
  272. lines.append(f"- 题材: {genre_profile.get('genre')}")
  273. genres = genre_profile.get("genres") or []
  274. if len(genres) > 1:
  275. lines.append(f"- 复合题材: {' + '.join(str(token) for token in genres)}")
  276. composite_hints = genre_profile.get("composite_hints") or []
  277. for row in composite_hints[:2]:
  278. lines.append(f"- {row}")
  279. refs = genre_profile.get("reference_hints") or []
  280. for row in refs[:3]:
  281. lines.append(f"- {row}")
  282. lines.append("")
  283. return "\n".join(lines).rstrip() + "\n"
  284. def main():
  285. parser = argparse.ArgumentParser(description="提取章节创作所需的精简上下文")
  286. parser.add_argument("--chapter", type=int, required=True, help="目标章节号")
  287. parser.add_argument("--project-root", type=str, help="项目根目录")
  288. parser.add_argument("--format", choices=["text", "json"], default="text", help="输出格式")
  289. args = parser.parse_args()
  290. try:
  291. project_root = Path(args.project_root) if args.project_root else find_project_root()
  292. payload = build_chapter_context_payload(project_root, args.chapter)
  293. if args.format == "json":
  294. print(json.dumps(payload, ensure_ascii=False, indent=2))
  295. else:
  296. print(_render_text(payload), end="")
  297. except Exception as exc:
  298. print(f"❌ 错误: {exc}", file=sys.stderr)
  299. sys.exit(1)
  300. if __name__ == "__main__":
  301. if sys.platform == "win32":
  302. enable_windows_utf8_stdio()
  303. main()