context_manager.py 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. ContextManager - assemble context packs with weighted priorities.
  5. """
  6. from __future__ import annotations
  7. import json
  8. import re
  9. from pathlib import Path
  10. from typing import Any, Dict, List, Optional
  11. from .config import get_config
  12. from .index_manager import IndexManager, WritingChecklistScoreMeta
  13. from .context_ranker import ContextRanker
  14. from .snapshot_manager import SnapshotManager, SnapshotVersionMismatch
  15. class ContextManager:
  16. DEFAULT_TEMPLATE = "plot"
  17. TEMPLATE_WEIGHTS = {
  18. "plot": {"core": 0.40, "scene": 0.35, "global": 0.25},
  19. "battle": {"core": 0.35, "scene": 0.45, "global": 0.20},
  20. "emotion": {"core": 0.45, "scene": 0.35, "global": 0.20},
  21. "transition": {"core": 0.50, "scene": 0.25, "global": 0.25},
  22. }
  23. TEMPLATE_WEIGHTS_DYNAMIC = {
  24. "early": {
  25. "plot": {"core": 0.48, "scene": 0.39, "global": 0.13},
  26. "battle": {"core": 0.42, "scene": 0.50, "global": 0.08},
  27. "emotion": {"core": 0.52, "scene": 0.38, "global": 0.10},
  28. "transition": {"core": 0.56, "scene": 0.28, "global": 0.16},
  29. },
  30. "mid": {
  31. "plot": {"core": 0.40, "scene": 0.35, "global": 0.25},
  32. "battle": {"core": 0.35, "scene": 0.45, "global": 0.20},
  33. "emotion": {"core": 0.45, "scene": 0.35, "global": 0.20},
  34. "transition": {"core": 0.50, "scene": 0.25, "global": 0.25},
  35. },
  36. "late": {
  37. "plot": {"core": 0.36, "scene": 0.29, "global": 0.35},
  38. "battle": {"core": 0.31, "scene": 0.39, "global": 0.30},
  39. "emotion": {"core": 0.41, "scene": 0.29, "global": 0.30},
  40. "transition": {"core": 0.46, "scene": 0.21, "global": 0.33},
  41. },
  42. }
  43. EXTRA_SECTIONS = {
  44. "story_skeleton",
  45. "memory",
  46. "preferences",
  47. "alerts",
  48. "reader_signal",
  49. "genre_profile",
  50. "writing_guidance",
  51. }
  52. SECTION_ORDER = [
  53. "core",
  54. "scene",
  55. "global",
  56. "reader_signal",
  57. "genre_profile",
  58. "writing_guidance",
  59. "story_skeleton",
  60. "memory",
  61. "preferences",
  62. "alerts",
  63. ]
  64. SUMMARY_SECTION_RE = re.compile(r"##\s*剧情摘要\s*\r?\n(.*?)(?=\r?\n##|\Z)", re.DOTALL)
  65. def __init__(self, config=None, snapshot_manager: Optional[SnapshotManager] = None):
  66. self.config = config or get_config()
  67. self.snapshot_manager = snapshot_manager or SnapshotManager(self.config)
  68. self.index_manager = IndexManager(self.config)
  69. self.context_ranker = ContextRanker(self.config)
  70. def _is_snapshot_compatible(self, cached: Dict[str, Any], template: str) -> bool:
  71. """判断快照是否可用于当前模板。"""
  72. if not isinstance(cached, dict):
  73. return False
  74. meta = cached.get("meta")
  75. if not isinstance(meta, dict):
  76. # 兼容旧快照:未记录 template 时仅允许默认模板复用
  77. return template == self.DEFAULT_TEMPLATE
  78. cached_template = meta.get("template")
  79. if not isinstance(cached_template, str):
  80. return template == self.DEFAULT_TEMPLATE
  81. return cached_template == template
  82. def build_context(
  83. self,
  84. chapter: int,
  85. template: str | None = None,
  86. use_snapshot: bool = True,
  87. save_snapshot: bool = True,
  88. max_chars: Optional[int] = None,
  89. ) -> Dict[str, Any]:
  90. template = template or self.DEFAULT_TEMPLATE
  91. self._active_template = template
  92. if template not in self.TEMPLATE_WEIGHTS:
  93. template = self.DEFAULT_TEMPLATE
  94. self._active_template = template
  95. if use_snapshot:
  96. try:
  97. cached = self.snapshot_manager.load_snapshot(chapter)
  98. if cached and self._is_snapshot_compatible(cached, template):
  99. return cached.get("payload", cached)
  100. except SnapshotVersionMismatch:
  101. # Snapshot incompatible; rebuild below.
  102. pass
  103. pack = self._build_pack(chapter)
  104. if getattr(self.config, "context_ranker_enabled", True):
  105. pack = self.context_ranker.rank_pack(pack, chapter)
  106. assembled = self.assemble_context(pack, template=template, max_chars=max_chars)
  107. if save_snapshot:
  108. meta = {"template": template}
  109. self.snapshot_manager.save_snapshot(chapter, assembled, meta=meta)
  110. return assembled
  111. def assemble_context(
  112. self,
  113. pack: Dict[str, Any],
  114. template: str = DEFAULT_TEMPLATE,
  115. max_chars: Optional[int] = None,
  116. ) -> Dict[str, Any]:
  117. chapter = int((pack.get("meta") or {}).get("chapter") or 0)
  118. weights = self._resolve_template_weights(template=template, chapter=chapter)
  119. max_chars = max_chars or 8000
  120. extra_budget = int(self.config.context_extra_section_budget or 0)
  121. sections = {}
  122. for section_name in self.SECTION_ORDER:
  123. if section_name in pack:
  124. sections[section_name] = pack[section_name]
  125. assembled: Dict[str, Any] = {"meta": pack.get("meta", {}), "sections": {}}
  126. for name, content in sections.items():
  127. weight = weights.get(name, 0.0)
  128. if weight > 0:
  129. budget = int(max_chars * weight)
  130. elif name in self.EXTRA_SECTIONS and extra_budget > 0:
  131. budget = extra_budget
  132. else:
  133. budget = None
  134. text = self._compact_json_text(content, budget)
  135. assembled["sections"][name] = {"content": content, "text": text, "budget": budget}
  136. assembled["template"] = template
  137. assembled["weights"] = weights
  138. if chapter > 0:
  139. assembled.setdefault("meta", {})["context_weight_stage"] = self._resolve_context_stage(chapter)
  140. return assembled
  141. def filter_invalid_items(self, items: List[Dict[str, Any]], source_type: str, id_key: str) -> List[Dict[str, Any]]:
  142. confirmed = self.index_manager.get_invalid_ids(source_type, status="confirmed")
  143. pending = self.index_manager.get_invalid_ids(source_type, status="pending")
  144. result = []
  145. for item in items:
  146. item_id = str(item.get(id_key, ""))
  147. if item_id in confirmed:
  148. continue
  149. if item_id in pending:
  150. item = dict(item)
  151. item["warning"] = "pending_invalid"
  152. result.append(item)
  153. return result
  154. def apply_confidence_filter(self, items: List[Dict[str, Any]], min_confidence: float) -> List[Dict[str, Any]]:
  155. filtered: List[Dict[str, Any]] = []
  156. for item in items:
  157. conf = item.get("confidence")
  158. if conf is None or conf >= min_confidence:
  159. filtered.append(item)
  160. return filtered
  161. def _build_pack(self, chapter: int) -> Dict[str, Any]:
  162. state = self._load_state()
  163. core = {
  164. "chapter_outline": self._load_outline(chapter),
  165. "protagonist_snapshot": state.get("protagonist_state", {}),
  166. "recent_summaries": self._load_recent_summaries(
  167. chapter,
  168. window=self.config.context_recent_summaries_window,
  169. ),
  170. "recent_meta": self._load_recent_meta(
  171. state,
  172. chapter,
  173. window=self.config.context_recent_meta_window,
  174. ),
  175. }
  176. scene = {
  177. "location_context": state.get("protagonist_state", {}).get("location", {}),
  178. "appearing_characters": self._load_recent_appearances(
  179. limit=self.config.context_max_appearing_characters,
  180. ),
  181. }
  182. scene["appearing_characters"] = self.filter_invalid_items(
  183. scene["appearing_characters"], source_type="entity", id_key="entity_id"
  184. )
  185. global_ctx = {
  186. "worldview_skeleton": self._load_setting("世界观"),
  187. "power_system_skeleton": self._load_setting("力量体系"),
  188. "style_contract_ref": self._load_setting("风格契约"),
  189. }
  190. preferences = self._load_json_optional(self.config.webnovel_dir / "preferences.json")
  191. memory = self._load_json_optional(self.config.webnovel_dir / "project_memory.json")
  192. story_skeleton = self._load_story_skeleton(chapter)
  193. alert_slice = max(0, int(self.config.context_alerts_slice))
  194. reader_signal = self._load_reader_signal(chapter)
  195. genre_profile = self._load_genre_profile(state)
  196. writing_guidance = self._build_writing_guidance(chapter, reader_signal, genre_profile)
  197. return {
  198. "meta": {"chapter": chapter},
  199. "core": core,
  200. "scene": scene,
  201. "global": global_ctx,
  202. "reader_signal": reader_signal,
  203. "genre_profile": genre_profile,
  204. "writing_guidance": writing_guidance,
  205. "story_skeleton": story_skeleton,
  206. "preferences": preferences,
  207. "memory": memory,
  208. "alerts": {
  209. "disambiguation_warnings": (
  210. state.get("disambiguation_warnings", [])[-alert_slice:] if alert_slice else []
  211. ),
  212. "disambiguation_pending": (
  213. state.get("disambiguation_pending", [])[-alert_slice:] if alert_slice else []
  214. ),
  215. },
  216. }
  217. def _load_reader_signal(self, chapter: int) -> Dict[str, Any]:
  218. if not getattr(self.config, "context_reader_signal_enabled", True):
  219. return {}
  220. recent_limit = max(1, int(getattr(self.config, "context_reader_signal_recent_limit", 5)))
  221. pattern_window = max(1, int(getattr(self.config, "context_reader_signal_window_chapters", 20)))
  222. review_window = max(1, int(getattr(self.config, "context_reader_signal_review_window", 5)))
  223. include_debt = bool(getattr(self.config, "context_reader_signal_include_debt", False))
  224. recent_power = self.index_manager.get_recent_reading_power(limit=recent_limit)
  225. pattern_stats = self.index_manager.get_pattern_usage_stats(last_n_chapters=pattern_window)
  226. hook_stats = self.index_manager.get_hook_type_stats(last_n_chapters=pattern_window)
  227. review_trend = self.index_manager.get_review_trend_stats(last_n=review_window)
  228. low_score_ranges: List[Dict[str, Any]] = []
  229. for row in review_trend.get("recent_ranges", []):
  230. score = row.get("overall_score")
  231. if isinstance(score, (int, float)) and float(score) < 75:
  232. low_score_ranges.append(
  233. {
  234. "start_chapter": row.get("start_chapter"),
  235. "end_chapter": row.get("end_chapter"),
  236. "overall_score": score,
  237. }
  238. )
  239. signal: Dict[str, Any] = {
  240. "recent_reading_power": recent_power,
  241. "pattern_usage": pattern_stats,
  242. "hook_type_usage": hook_stats,
  243. "review_trend": review_trend,
  244. "low_score_ranges": low_score_ranges,
  245. "next_chapter": chapter,
  246. }
  247. if include_debt:
  248. signal["debt_summary"] = self.index_manager.get_debt_summary()
  249. return signal
  250. def _load_genre_profile(self, state: Dict[str, Any]) -> Dict[str, Any]:
  251. if not getattr(self.config, "context_genre_profile_enabled", True):
  252. return {}
  253. fallback = str(getattr(self.config, "context_genre_profile_fallback", "shuangwen") or "shuangwen")
  254. genre_raw = str((state.get("project") or {}).get("genre") or fallback)
  255. genres = self._parse_genre_tokens(genre_raw)
  256. if not genres:
  257. genres = [fallback]
  258. max_genres = max(1, int(getattr(self.config, "context_genre_profile_max_genres", 2)))
  259. genres = genres[:max_genres]
  260. primary_genre = genres[0]
  261. secondary_genres = genres[1:]
  262. composite = len(genres) > 1
  263. profile_path = self.config.project_root / ".claude" / "references" / "genre-profiles.md"
  264. taxonomy_path = self.config.project_root / ".claude" / "references" / "reading-power-taxonomy.md"
  265. profile_text = profile_path.read_text(encoding="utf-8") if profile_path.exists() else ""
  266. taxonomy_text = taxonomy_path.read_text(encoding="utf-8") if taxonomy_path.exists() else ""
  267. profile_excerpt = self._extract_genre_section(profile_text, primary_genre)
  268. taxonomy_excerpt = self._extract_genre_section(taxonomy_text, primary_genre)
  269. secondary_profiles: List[str] = []
  270. secondary_taxonomies: List[str] = []
  271. for extra in secondary_genres:
  272. secondary_profiles.append(self._extract_genre_section(profile_text, extra))
  273. secondary_taxonomies.append(self._extract_genre_section(taxonomy_text, extra))
  274. refs = self._extract_markdown_refs(
  275. "\n".join([profile_excerpt] + secondary_profiles),
  276. max_items=int(getattr(self.config, "context_genre_profile_max_refs", 8)),
  277. )
  278. composite_hints = self._build_composite_genre_hints(genres, refs)
  279. return {
  280. "genre": primary_genre,
  281. "genre_raw": genre_raw,
  282. "genres": genres,
  283. "composite": composite,
  284. "secondary_genres": secondary_genres,
  285. "profile_excerpt": profile_excerpt,
  286. "taxonomy_excerpt": taxonomy_excerpt,
  287. "secondary_profile_excerpts": secondary_profiles,
  288. "secondary_taxonomy_excerpts": secondary_taxonomies,
  289. "reference_hints": refs,
  290. "composite_hints": composite_hints,
  291. }
  292. def _build_writing_guidance(
  293. self,
  294. chapter: int,
  295. reader_signal: Dict[str, Any],
  296. genre_profile: Dict[str, Any],
  297. ) -> Dict[str, Any]:
  298. if not getattr(self.config, "context_writing_guidance_enabled", True):
  299. return {}
  300. guidance: List[str] = []
  301. limit = max(1, int(getattr(self.config, "context_writing_guidance_max_items", 6)))
  302. low_score_threshold = float(
  303. getattr(self.config, "context_writing_guidance_low_score_threshold", 75.0)
  304. )
  305. low_ranges = reader_signal.get("low_score_ranges") or []
  306. if low_ranges:
  307. worst = min(
  308. low_ranges,
  309. key=lambda row: float(row.get("overall_score", 9999)),
  310. )
  311. guidance.append(
  312. f"第{chapter}章优先修复近期低分段问题:参考{worst.get('start_chapter')}-{worst.get('end_chapter')}章,强化冲突推进与结尾钩子。"
  313. )
  314. hook_usage = reader_signal.get("hook_type_usage") or {}
  315. if hook_usage and getattr(self.config, "context_writing_guidance_hook_diversify", True):
  316. dominant_hook = max(hook_usage.items(), key=lambda kv: kv[1])[0]
  317. guidance.append(
  318. f"近期钩子类型“{dominant_hook}”使用偏多,本章建议做钩子差异化,避免连续同构。"
  319. )
  320. pattern_usage = reader_signal.get("pattern_usage") or {}
  321. if pattern_usage:
  322. top_pattern = max(pattern_usage.items(), key=lambda kv: kv[1])[0]
  323. guidance.append(
  324. f"爽点模式“{top_pattern}”近期高频,本章可保留主爽点但叠加一个新爽点副轴。"
  325. )
  326. review_trend = reader_signal.get("review_trend") or {}
  327. overall_avg = review_trend.get("overall_avg")
  328. if isinstance(overall_avg, (int, float)) and float(overall_avg) < low_score_threshold:
  329. guidance.append(
  330. f"最近审查均分{overall_avg:.1f}低于阈值{low_score_threshold:.1f},建议先保稳:减少跳场、每段补动作结果闭环。"
  331. )
  332. genre = str(genre_profile.get("genre") or "").strip()
  333. refs = genre_profile.get("reference_hints") or []
  334. if genre:
  335. guidance.append(f"题材锚定:按“{genre}”叙事主线推进,保持题材读者预期稳定兑现。")
  336. if refs:
  337. guidance.append(f"题材策略可执行提示:{refs[0]}")
  338. guidance.append("网文节奏基线:章首300字内给出目标与阻力,章末保留未闭合问题。")
  339. guidance.append("兑现密度基线:每600-900字给一次微兑现,并确保本章至少1处可量化变化。")
  340. genre_aliases = {
  341. "修仙": "xianxia",
  342. "玄幻": "xianxia",
  343. "高武": "xianxia",
  344. "西幻": "xianxia",
  345. "都市异能": "urban-power",
  346. "都市脑洞": "urban-power",
  347. "都市日常": "urban-power",
  348. "狗血言情": "romance",
  349. "古言": "romance",
  350. "青春甜宠": "romance",
  351. "替身文": "substitute",
  352. "规则怪谈": "rules-mystery",
  353. "悬疑脑洞": "mystery",
  354. "悬疑灵异": "mystery",
  355. "知乎短篇": "zhihu-short",
  356. "电竞": "esports",
  357. "直播文": "livestream",
  358. "克苏鲁": "cosmic-horror",
  359. }
  360. normalized_genre = genre_aliases.get(genre, genre.lower())
  361. genre_guidance = {
  362. "xianxia": "题材加权:强化升级/对抗结果的可见反馈,术语解释后置。",
  363. "shuangwen": "题材加权:维持高爽点密度,主爽点外叠加一个副轴反差。",
  364. "urban-power": "题材加权:优先写社会反馈链(他人反应→资源变化→地位变化)。",
  365. "romance": "题材加权:每章推进关系位移,避免情绪原地打转。",
  366. "mystery": "题材加权:线索必须可回收,优先以规则冲突制造悬念。",
  367. "rules-mystery": "题材加权:规则先于解释,代价先于胜利。",
  368. "zhihu-short": "题材加权:压缩铺垫,优先反转与高强度结尾钩。",
  369. "substitute": "题材加权:强化误解-拉扯-决断链路,避免重复虐点。",
  370. "esports": "题材加权:每场对抗至少写清一个战术决策点与其后果。",
  371. "livestream": "题材加权:强化“外部反馈→主角反制→数据变化”即时闭环。",
  372. "cosmic-horror": "题材加权:恐怖来源于规则与代价,不依赖空泛惊悚形容。",
  373. }
  374. genre_hint = genre_guidance.get(normalized_genre) or genre_guidance.get(genre)
  375. if genre_hint:
  376. guidance.append(genre_hint)
  377. composite_hints = genre_profile.get("composite_hints") or []
  378. if composite_hints:
  379. guidance.append(f"复合题材协同:{composite_hints[0]}")
  380. if not guidance:
  381. guidance.append("本章执行默认高可读策略:冲突前置、信息后置、段末留钩。")
  382. checklist = self._build_writing_checklist(
  383. chapter=chapter,
  384. guidance_items=guidance,
  385. reader_signal=reader_signal,
  386. genre_profile=genre_profile,
  387. )
  388. checklist_score = self._compute_writing_checklist_score(
  389. chapter=chapter,
  390. checklist=checklist,
  391. reader_signal=reader_signal,
  392. )
  393. if getattr(self.config, "context_writing_score_persist_enabled", True):
  394. self._persist_writing_checklist_score(checklist_score)
  395. return {
  396. "chapter": chapter,
  397. "guidance_items": guidance[:limit],
  398. "checklist": checklist,
  399. "checklist_score": checklist_score,
  400. "signals_used": {
  401. "has_low_score_ranges": bool(low_ranges),
  402. "hook_types": list(hook_usage.keys())[:3],
  403. "top_patterns": sorted(
  404. pattern_usage,
  405. key=pattern_usage.get,
  406. reverse=True,
  407. )[:3],
  408. "genre": genre,
  409. },
  410. }
  411. def _compute_writing_checklist_score(
  412. self,
  413. chapter: int,
  414. checklist: List[Dict[str, Any]],
  415. reader_signal: Dict[str, Any],
  416. ) -> Dict[str, Any]:
  417. total_items = len(checklist)
  418. required_items = 0
  419. completed_items = 0
  420. completed_required = 0
  421. total_weight = 0.0
  422. completed_weight = 0.0
  423. pending_labels: List[str] = []
  424. for item in checklist:
  425. if not isinstance(item, dict):
  426. continue
  427. required = bool(item.get("required"))
  428. weight = float(item.get("weight") or 1.0)
  429. total_weight += weight
  430. if required:
  431. required_items += 1
  432. completed = self._is_checklist_item_completed(item, reader_signal)
  433. if completed:
  434. completed_items += 1
  435. completed_weight += weight
  436. if required:
  437. completed_required += 1
  438. else:
  439. pending_labels.append(str(item.get("label") or item.get("id") or "未命名项"))
  440. completion_rate = (completed_items / total_items) if total_items > 0 else 1.0
  441. weighted_rate = (completed_weight / total_weight) if total_weight > 0 else completion_rate
  442. required_rate = (completed_required / required_items) if required_items > 0 else 1.0
  443. score = 100.0 * (0.5 * weighted_rate + 0.3 * required_rate + 0.2 * completion_rate)
  444. if getattr(self.config, "context_writing_score_include_reader_trend", True):
  445. trend_window = max(1, int(getattr(self.config, "context_writing_score_trend_window", 10)))
  446. trend = self.index_manager.get_writing_checklist_score_trend(last_n=trend_window)
  447. baseline = float(trend.get("score_avg") or 0.0)
  448. if baseline > 0:
  449. score += max(-10.0, min(10.0, (score - baseline) * 0.1))
  450. score = round(max(0.0, min(100.0, score)), 2)
  451. return {
  452. "chapter": chapter,
  453. "score": score,
  454. "completion_rate": round(completion_rate, 4),
  455. "weighted_completion_rate": round(weighted_rate, 4),
  456. "required_completion_rate": round(required_rate, 4),
  457. "total_items": total_items,
  458. "required_items": required_items,
  459. "completed_items": completed_items,
  460. "completed_required": completed_required,
  461. "total_weight": round(total_weight, 2),
  462. "completed_weight": round(completed_weight, 2),
  463. "pending_items": pending_labels,
  464. "trend_window": int(getattr(self.config, "context_writing_score_trend_window", 10)),
  465. }
  466. def _is_checklist_item_completed(self, item: Dict[str, Any], reader_signal: Dict[str, Any]) -> bool:
  467. item_id = str(item.get("id") or "")
  468. if item_id in {"fix_low_score_range", "readability_loop"}:
  469. review_trend = reader_signal.get("review_trend") or {}
  470. overall = review_trend.get("overall_avg")
  471. return isinstance(overall, (int, float)) and float(overall) >= 75.0
  472. if item_id == "hook_diversification":
  473. hook_usage = reader_signal.get("hook_type_usage") or {}
  474. return len(hook_usage) >= 2
  475. if item_id == "coolpoint_combo":
  476. pattern_usage = reader_signal.get("pattern_usage") or {}
  477. return len(pattern_usage) >= 2
  478. if item_id == "genre_anchor_consistency":
  479. return True
  480. source = str(item.get("source") or "")
  481. if source.startswith("fallback"):
  482. return True
  483. return False
  484. def _persist_writing_checklist_score(self, checklist_score: Dict[str, Any]) -> None:
  485. if not checklist_score:
  486. return
  487. try:
  488. self.index_manager.save_writing_checklist_score(
  489. WritingChecklistScoreMeta(
  490. chapter=int(checklist_score.get("chapter") or 0),
  491. template=str(getattr(self, "_active_template", self.DEFAULT_TEMPLATE) or self.DEFAULT_TEMPLATE),
  492. total_items=int(checklist_score.get("total_items") or 0),
  493. required_items=int(checklist_score.get("required_items") or 0),
  494. completed_items=int(checklist_score.get("completed_items") or 0),
  495. completed_required=int(checklist_score.get("completed_required") or 0),
  496. total_weight=float(checklist_score.get("total_weight") or 0.0),
  497. completed_weight=float(checklist_score.get("completed_weight") or 0.0),
  498. completion_rate=float(checklist_score.get("completion_rate") or 0.0),
  499. score=float(checklist_score.get("score") or 0.0),
  500. score_breakdown={
  501. "weighted_completion_rate": checklist_score.get("weighted_completion_rate"),
  502. "required_completion_rate": checklist_score.get("required_completion_rate"),
  503. "trend_window": checklist_score.get("trend_window"),
  504. },
  505. pending_items=list(checklist_score.get("pending_items") or []),
  506. source="context_manager",
  507. )
  508. )
  509. except Exception:
  510. pass
  511. def _resolve_context_stage(self, chapter: int) -> str:
  512. early = max(1, int(getattr(self.config, "context_dynamic_budget_early_chapter", 30)))
  513. late = max(early + 1, int(getattr(self.config, "context_dynamic_budget_late_chapter", 120)))
  514. if chapter <= early:
  515. return "early"
  516. if chapter >= late:
  517. return "late"
  518. return "mid"
  519. def _resolve_template_weights(self, template: str, chapter: int) -> Dict[str, float]:
  520. template_key = template if template in self.TEMPLATE_WEIGHTS else self.DEFAULT_TEMPLATE
  521. base = dict(self.TEMPLATE_WEIGHTS.get(template_key, self.TEMPLATE_WEIGHTS[self.DEFAULT_TEMPLATE]))
  522. if not getattr(self.config, "context_dynamic_budget_enabled", True):
  523. return base
  524. stage = self._resolve_context_stage(chapter)
  525. staged = self.TEMPLATE_WEIGHTS_DYNAMIC.get(stage, {}).get(template_key)
  526. if staged:
  527. return dict(staged)
  528. return base
  529. def _parse_genre_tokens(self, genre_raw: str) -> List[str]:
  530. text = str(genre_raw or "").strip()
  531. if not text:
  532. return []
  533. if not getattr(self.config, "context_genre_profile_support_composite", True):
  534. return [text]
  535. separators = getattr(self.config, "context_genre_profile_separators", ("+", "/", "|", ",", ",", "、"))
  536. pattern = "|".join(re.escape(str(token)) for token in separators if str(token))
  537. if not pattern:
  538. return [text]
  539. tokens = [chunk.strip() for chunk in re.split(pattern, text) if chunk and chunk.strip()]
  540. deduped: List[str] = []
  541. seen = set()
  542. for token in tokens:
  543. lower = token.lower()
  544. if lower in seen:
  545. continue
  546. seen.add(lower)
  547. deduped.append(token)
  548. return deduped or [text]
  549. def _build_composite_genre_hints(self, genres: List[str], refs: List[str]) -> List[str]:
  550. if len(genres) <= 1:
  551. return []
  552. primary = genres[0]
  553. secondaries = genres[1:]
  554. hints: List[str] = []
  555. hints.append(
  556. f"以“{primary}”作为主引擎推进主线,每章至少保留1处“{'/'.join(secondaries)}”特征表达。"
  557. )
  558. if refs:
  559. hints.append(f"复合题材执行参考:{refs[0]}")
  560. hints.append("主辅题材冲突时,优先保证主题材读者承诺,辅题材用于制造新鲜感。")
  561. return hints
  562. def _build_writing_checklist(
  563. self,
  564. chapter: int,
  565. guidance_items: List[str],
  566. reader_signal: Dict[str, Any],
  567. genre_profile: Dict[str, Any],
  568. ) -> List[Dict[str, Any]]:
  569. if not getattr(self.config, "context_writing_checklist_enabled", True):
  570. return []
  571. min_items = max(1, int(getattr(self.config, "context_writing_checklist_min_items", 3)))
  572. max_items = max(min_items, int(getattr(self.config, "context_writing_checklist_max_items", 6)))
  573. default_weight = float(getattr(self.config, "context_writing_checklist_default_weight", 1.0))
  574. if default_weight <= 0:
  575. default_weight = 1.0
  576. items: List[Dict[str, Any]] = []
  577. def _add_item(
  578. item_id: str,
  579. label: str,
  580. *,
  581. weight: Optional[float] = None,
  582. required: bool = False,
  583. source: str = "writing_guidance",
  584. verify_hint: str = "",
  585. ) -> None:
  586. if len(items) >= max_items:
  587. return
  588. if any(row.get("id") == item_id for row in items):
  589. return
  590. item_weight = float(weight if weight is not None else default_weight)
  591. if item_weight <= 0:
  592. item_weight = default_weight
  593. items.append(
  594. {
  595. "id": item_id,
  596. "label": label,
  597. "weight": round(item_weight, 2),
  598. "required": bool(required),
  599. "source": source,
  600. "verify_hint": verify_hint,
  601. }
  602. )
  603. low_ranges = reader_signal.get("low_score_ranges") or []
  604. if low_ranges:
  605. worst = min(low_ranges, key=lambda row: float(row.get("overall_score", 9999)))
  606. span = f"{worst.get('start_chapter')}-{worst.get('end_chapter')}"
  607. _add_item(
  608. "fix_low_score_range",
  609. f"修复低分区间问题(参考第{span}章)",
  610. weight=max(default_weight, 1.4),
  611. required=True,
  612. source="reader_signal.low_score_ranges",
  613. verify_hint="至少完成1处冲突升级,并在段末留下钩子。",
  614. )
  615. hook_usage = reader_signal.get("hook_type_usage") or {}
  616. if hook_usage:
  617. dominant_hook = max(hook_usage.items(), key=lambda kv: kv[1])[0]
  618. _add_item(
  619. "hook_diversification",
  620. f"钩子差异化(避免继续单一“{dominant_hook}”)",
  621. weight=max(default_weight, 1.2),
  622. required=True,
  623. source="reader_signal.hook_type_usage",
  624. verify_hint="结尾钩子类型与近20章主类型至少有一处差异。",
  625. )
  626. pattern_usage = reader_signal.get("pattern_usage") or {}
  627. if pattern_usage:
  628. top_pattern = max(pattern_usage.items(), key=lambda kv: kv[1])[0]
  629. _add_item(
  630. "coolpoint_combo",
  631. f"主爽点+副爽点组合(主爽点:{top_pattern})",
  632. weight=default_weight,
  633. required=False,
  634. source="reader_signal.pattern_usage",
  635. verify_hint="新增至少1个副爽点,并与主爽点形成因果链。",
  636. )
  637. review_trend = reader_signal.get("review_trend") or {}
  638. overall_avg = review_trend.get("overall_avg")
  639. if isinstance(overall_avg, (int, float)):
  640. _add_item(
  641. "readability_loop",
  642. "段落可读性闭环(动作→结果→情绪)",
  643. weight=max(default_weight, 1.1),
  644. required=True,
  645. source="reader_signal.review_trend",
  646. verify_hint="抽查3段,均包含动作结果闭环。",
  647. )
  648. genre = str(genre_profile.get("genre") or "").strip()
  649. if genre:
  650. _add_item(
  651. "genre_anchor_consistency",
  652. f"题材锚定一致性({genre})",
  653. weight=max(default_weight, 1.1),
  654. required=True,
  655. source="genre_profile.genre",
  656. verify_hint="主冲突与题材核心承诺保持一致。",
  657. )
  658. for idx, text in enumerate(guidance_items, start=1):
  659. if len(items) >= max_items:
  660. break
  661. label = str(text).strip()
  662. if not label:
  663. continue
  664. _add_item(
  665. f"guidance_item_{idx}",
  666. label,
  667. weight=default_weight,
  668. required=False,
  669. source="writing_guidance.guidance_items",
  670. verify_hint="完成后可在正文中定位对应段落。",
  671. )
  672. fallback_items = [
  673. (
  674. "opening_conflict",
  675. "开篇300字内给出冲突触发",
  676. "开头段出现明确目标与阻力。",
  677. ),
  678. (
  679. "scene_goal_block",
  680. "场景目标与阻力清晰",
  681. "每个场景至少有1个可验证目标。",
  682. ),
  683. (
  684. "ending_hook",
  685. "段末留钩并引出下一问",
  686. "结尾出现未解问题或下一步行动。",
  687. ),
  688. ]
  689. for item_id, label, verify_hint in fallback_items:
  690. if len(items) >= min_items or len(items) >= max_items:
  691. break
  692. _add_item(
  693. item_id,
  694. label,
  695. weight=default_weight,
  696. required=False,
  697. source="fallback",
  698. verify_hint=verify_hint,
  699. )
  700. return items[:max_items]
  701. def _compact_json_text(self, content: Any, budget: Optional[int]) -> str:
  702. raw = json.dumps(content, ensure_ascii=False)
  703. if budget is None or len(raw) <= budget:
  704. return raw
  705. if not getattr(self.config, "context_compact_text_enabled", True):
  706. return raw[:budget]
  707. min_budget = max(1, int(getattr(self.config, "context_compact_min_budget", 120)))
  708. if budget <= min_budget:
  709. return raw[:budget]
  710. head_ratio = float(getattr(self.config, "context_compact_head_ratio", 0.65))
  711. head_budget = int(budget * max(0.2, min(0.9, head_ratio)))
  712. tail_budget = max(0, budget - head_budget - 10)
  713. compact = f"{raw[:head_budget]}…[TRUNCATED]{raw[-tail_budget:] if tail_budget else ''}"
  714. return compact[:budget]
  715. def _extract_genre_section(self, text: str, genre: str) -> str:
  716. if not text:
  717. return ""
  718. lines = text.splitlines()
  719. capture: List[str] = []
  720. active = False
  721. target = genre.strip().lower()
  722. for line in lines:
  723. normalized = line.strip().lower()
  724. if normalized.startswith("## ") or normalized.startswith("### "):
  725. if active:
  726. break
  727. active = target in normalized
  728. if active:
  729. capture.append(line)
  730. continue
  731. if active:
  732. capture.append(line)
  733. if capture:
  734. return "\n".join(capture).strip()
  735. return "\n".join(lines[:80]).strip()
  736. def _extract_markdown_refs(self, text: str, max_items: int = 8) -> List[str]:
  737. if not text:
  738. return []
  739. refs: List[str] = []
  740. for line in text.splitlines():
  741. row = line.strip().lstrip("-*").strip()
  742. if not row or row.startswith("#"):
  743. continue
  744. refs.append(row)
  745. if len(refs) >= max(1, max_items):
  746. break
  747. return refs
  748. def _load_state(self) -> Dict[str, Any]:
  749. path = self.config.state_file
  750. if not path.exists():
  751. return {}
  752. return json.loads(path.read_text(encoding="utf-8"))
  753. def _load_outline(self, chapter: int) -> str:
  754. outline_dir = self.config.outline_dir
  755. patterns = [
  756. f"第{chapter}章*.md",
  757. f"第{chapter:02d}章*.md",
  758. f"第{chapter:03d}章*.md",
  759. f"第{chapter:04d}章*.md",
  760. ]
  761. for pattern in patterns:
  762. matches = list(outline_dir.glob(pattern))
  763. if matches:
  764. return matches[0].read_text(encoding="utf-8")
  765. return f"[大纲未找到: 第{chapter}章]"
  766. def _load_recent_summaries(self, chapter: int, window: int = 3) -> List[Dict[str, Any]]:
  767. summaries = []
  768. for ch in range(max(1, chapter - window), chapter):
  769. summary = self._load_summary_text(ch)
  770. if summary:
  771. summaries.append(summary)
  772. return summaries
  773. def _load_recent_meta(self, state: Dict[str, Any], chapter: int, window: int = 3) -> List[Dict[str, Any]]:
  774. meta = state.get("chapter_meta", {}) or {}
  775. results = []
  776. for ch in range(max(1, chapter - window), chapter):
  777. for key in (f"{ch:04d}", str(ch)):
  778. if key in meta:
  779. results.append({"chapter": ch, **meta.get(key, {})})
  780. break
  781. return results
  782. def _load_recent_appearances(self, limit: Optional[int] = None) -> List[Dict[str, Any]]:
  783. appearances = self.index_manager.get_recent_appearances(limit=limit)
  784. return appearances or []
  785. def _load_setting(self, keyword: str) -> str:
  786. settings_dir = self.config.settings_dir
  787. candidates = [
  788. settings_dir / f"{keyword}.md",
  789. ]
  790. for path in candidates:
  791. if path.exists():
  792. return path.read_text(encoding="utf-8")
  793. # fallback: any file containing keyword
  794. matches = list(settings_dir.glob(f"*{keyword}*.md"))
  795. if matches:
  796. return matches[0].read_text(encoding="utf-8")
  797. return f"[{keyword}设定未找到]"
  798. def _extract_summary_excerpt(self, text: str, max_chars: int) -> str:
  799. if not text:
  800. return ""
  801. match = self.SUMMARY_SECTION_RE.search(text)
  802. excerpt = match.group(1).strip() if match else text.strip()
  803. if max_chars > 0 and len(excerpt) > max_chars:
  804. return excerpt[:max_chars].rstrip()
  805. return excerpt
  806. def _load_summary_text(self, chapter: int, snippet_chars: Optional[int] = None) -> Optional[Dict[str, Any]]:
  807. summary_path = self.config.webnovel_dir / "summaries" / f"ch{chapter:04d}.md"
  808. if not summary_path.exists():
  809. return None
  810. text = summary_path.read_text(encoding="utf-8")
  811. if snippet_chars:
  812. summary_text = self._extract_summary_excerpt(text, snippet_chars)
  813. else:
  814. summary_text = text
  815. return {"chapter": chapter, "summary": summary_text}
  816. def _load_story_skeleton(self, chapter: int) -> List[Dict[str, Any]]:
  817. interval = max(1, int(self.config.context_story_skeleton_interval))
  818. max_samples = max(0, int(self.config.context_story_skeleton_max_samples))
  819. snippet_chars = int(self.config.context_story_skeleton_snippet_chars)
  820. if max_samples <= 0 or chapter <= interval:
  821. return []
  822. samples: List[Dict[str, Any]] = []
  823. cursor = chapter - interval
  824. while cursor >= 1 and len(samples) < max_samples:
  825. summary = self._load_summary_text(cursor, snippet_chars=snippet_chars)
  826. if summary and summary.get("summary"):
  827. samples.append(summary)
  828. cursor -= interval
  829. samples.reverse()
  830. return samples
  831. def _load_json_optional(self, path: Path) -> Dict[str, Any]:
  832. if not path.exists():
  833. return {}
  834. try:
  835. return json.loads(path.read_text(encoding="utf-8"))
  836. except json.JSONDecodeError:
  837. return {}
  838. def main():
  839. import argparse
  840. from .cli_output import print_success, print_error
  841. parser = argparse.ArgumentParser(description="Context Manager CLI")
  842. parser.add_argument("--project-root", type=str, help="项目根目录")
  843. parser.add_argument("--chapter", type=int, required=True)
  844. parser.add_argument("--template", type=str, default=ContextManager.DEFAULT_TEMPLATE)
  845. parser.add_argument("--no-snapshot", action="store_true")
  846. parser.add_argument("--max-chars", type=int, default=8000)
  847. args = parser.parse_args()
  848. config = None
  849. if args.project_root:
  850. from .config import DataModulesConfig
  851. config = DataModulesConfig.from_project_root(args.project_root)
  852. manager = ContextManager(config)
  853. try:
  854. payload = manager.build_context(
  855. chapter=args.chapter,
  856. template=args.template,
  857. use_snapshot=not args.no_snapshot,
  858. save_snapshot=True,
  859. max_chars=args.max_chars,
  860. )
  861. print_success(payload, message="context_built")
  862. try:
  863. manager.index_manager.log_tool_call("context_manager:build", True, chapter=args.chapter)
  864. except Exception:
  865. pass
  866. except Exception as exc:
  867. print_error("CONTEXT_BUILD_FAILED", str(exc), suggestion="请检查项目结构与依赖文件")
  868. try:
  869. manager.index_manager.log_tool_call(
  870. "context_manager:build", False, error_code="CONTEXT_BUILD_FAILED", error_message=str(exc), chapter=args.chapter
  871. )
  872. except Exception:
  873. pass
  874. if __name__ == "__main__":
  875. import sys
  876. if sys.platform == "win32":
  877. import io
  878. sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
  879. sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8")
  880. main()