context_manager.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. import sys
  10. import logging
  11. from pathlib import Path
  12. from runtime_compat import enable_windows_utf8_stdio
  13. from typing import Any, Dict, List, Optional
  14. from .config import get_config
  15. from .index_manager import IndexManager, WritingChecklistScoreMeta
  16. from .context_ranker import ContextRanker
  17. from .snapshot_manager import SnapshotManager, SnapshotVersionMismatch
  18. from .context_weights import (
  19. DEFAULT_TEMPLATE as CONTEXT_DEFAULT_TEMPLATE,
  20. TEMPLATE_WEIGHTS as CONTEXT_TEMPLATE_WEIGHTS,
  21. TEMPLATE_WEIGHTS_DYNAMIC_DEFAULT as CONTEXT_TEMPLATE_WEIGHTS_DYNAMIC_DEFAULT,
  22. )
  23. from .genre_aliases import normalize_genre_token, to_profile_key
  24. from .genre_profile_builder import (
  25. build_composite_genre_hints,
  26. extract_genre_section,
  27. extract_markdown_refs,
  28. parse_genre_tokens,
  29. )
  30. from .writing_guidance_builder import (
  31. build_methodology_guidance_items,
  32. build_methodology_strategy_card,
  33. build_guidance_items,
  34. build_writing_checklist,
  35. is_checklist_item_completed,
  36. )
  37. logger = logging.getLogger(__name__)
  38. class ContextManager:
  39. DEFAULT_TEMPLATE = CONTEXT_DEFAULT_TEMPLATE
  40. TEMPLATE_WEIGHTS = CONTEXT_TEMPLATE_WEIGHTS
  41. TEMPLATE_WEIGHTS_DYNAMIC = CONTEXT_TEMPLATE_WEIGHTS_DYNAMIC_DEFAULT
  42. EXTRA_SECTIONS = {
  43. "story_skeleton",
  44. "memory",
  45. "preferences",
  46. "alerts",
  47. "reader_signal",
  48. "genre_profile",
  49. "writing_guidance",
  50. }
  51. SECTION_ORDER = [
  52. "core",
  53. "scene",
  54. "global",
  55. "reader_signal",
  56. "genre_profile",
  57. "writing_guidance",
  58. "story_skeleton",
  59. "memory",
  60. "preferences",
  61. "alerts",
  62. ]
  63. SUMMARY_SECTION_RE = re.compile(r"##\s*剧情摘要\s*\r?\n(.*?)(?=\r?\n##|\Z)", re.DOTALL)
  64. def __init__(self, config=None, snapshot_manager: Optional[SnapshotManager] = None):
  65. self.config = config or get_config()
  66. self.snapshot_manager = snapshot_manager or SnapshotManager(self.config)
  67. self.index_manager = IndexManager(self.config)
  68. self.context_ranker = ContextRanker(self.config)
  69. def _is_snapshot_compatible(self, cached: Dict[str, Any], template: str) -> bool:
  70. """判断快照是否可用于当前模板。"""
  71. if not isinstance(cached, dict):
  72. return False
  73. meta = cached.get("meta")
  74. if not isinstance(meta, dict):
  75. # 兼容旧快照:未记录 template 时仅允许默认模板复用
  76. return template == self.DEFAULT_TEMPLATE
  77. cached_template = meta.get("template")
  78. if not isinstance(cached_template, str):
  79. return template == self.DEFAULT_TEMPLATE
  80. return cached_template == template
  81. def build_context(
  82. self,
  83. chapter: int,
  84. template: str | None = None,
  85. use_snapshot: bool = True,
  86. save_snapshot: bool = True,
  87. max_chars: Optional[int] = None,
  88. ) -> Dict[str, Any]:
  89. template = template or self.DEFAULT_TEMPLATE
  90. self._active_template = template
  91. if template not in self.TEMPLATE_WEIGHTS:
  92. template = self.DEFAULT_TEMPLATE
  93. self._active_template = template
  94. if use_snapshot:
  95. try:
  96. cached = self.snapshot_manager.load_snapshot(chapter)
  97. if cached and self._is_snapshot_compatible(cached, template):
  98. return cached.get("payload", cached)
  99. except SnapshotVersionMismatch:
  100. # Snapshot incompatible; rebuild below.
  101. pass
  102. pack = self._build_pack(chapter)
  103. if getattr(self.config, "context_ranker_enabled", True):
  104. pack = self.context_ranker.rank_pack(pack, chapter)
  105. assembled = self.assemble_context(pack, template=template, max_chars=max_chars)
  106. if save_snapshot:
  107. meta = {"template": template}
  108. self.snapshot_manager.save_snapshot(chapter, assembled, meta=meta)
  109. return assembled
  110. def assemble_context(
  111. self,
  112. pack: Dict[str, Any],
  113. template: str = DEFAULT_TEMPLATE,
  114. max_chars: Optional[int] = None,
  115. ) -> Dict[str, Any]:
  116. chapter = int((pack.get("meta") or {}).get("chapter") or 0)
  117. weights = self._resolve_template_weights(template=template, chapter=chapter)
  118. max_chars = max_chars or 8000
  119. extra_budget = int(self.config.context_extra_section_budget or 0)
  120. sections = {}
  121. for section_name in self.SECTION_ORDER:
  122. if section_name in pack:
  123. sections[section_name] = pack[section_name]
  124. assembled: Dict[str, Any] = {"meta": pack.get("meta", {}), "sections": {}}
  125. for name, content in sections.items():
  126. weight = weights.get(name, 0.0)
  127. if weight > 0:
  128. budget = int(max_chars * weight)
  129. elif name in self.EXTRA_SECTIONS and extra_budget > 0:
  130. budget = extra_budget
  131. else:
  132. budget = None
  133. text = self._compact_json_text(content, budget)
  134. assembled["sections"][name] = {"content": content, "text": text, "budget": budget}
  135. assembled["template"] = template
  136. assembled["weights"] = weights
  137. if chapter > 0:
  138. assembled.setdefault("meta", {})["context_weight_stage"] = self._resolve_context_stage(chapter)
  139. return assembled
  140. def filter_invalid_items(self, items: List[Dict[str, Any]], source_type: str, id_key: str) -> List[Dict[str, Any]]:
  141. confirmed = self.index_manager.get_invalid_ids(source_type, status="confirmed")
  142. pending = self.index_manager.get_invalid_ids(source_type, status="pending")
  143. result = []
  144. for item in items:
  145. item_id = str(item.get(id_key, ""))
  146. if item_id in confirmed:
  147. continue
  148. if item_id in pending:
  149. item = dict(item)
  150. item["warning"] = "pending_invalid"
  151. result.append(item)
  152. return result
  153. def apply_confidence_filter(self, items: List[Dict[str, Any]], min_confidence: float) -> List[Dict[str, Any]]:
  154. filtered: List[Dict[str, Any]] = []
  155. for item in items:
  156. conf = item.get("confidence")
  157. if conf is None or conf >= min_confidence:
  158. filtered.append(item)
  159. return filtered
  160. def _build_pack(self, chapter: int) -> Dict[str, Any]:
  161. state = self._load_state()
  162. core = {
  163. "chapter_outline": self._load_outline(chapter),
  164. "protagonist_snapshot": state.get("protagonist_state", {}),
  165. "recent_summaries": self._load_recent_summaries(
  166. chapter,
  167. window=self.config.context_recent_summaries_window,
  168. ),
  169. "recent_meta": self._load_recent_meta(
  170. state,
  171. chapter,
  172. window=self.config.context_recent_meta_window,
  173. ),
  174. }
  175. scene = {
  176. "location_context": state.get("protagonist_state", {}).get("location", {}),
  177. "appearing_characters": self._load_recent_appearances(
  178. limit=self.config.context_max_appearing_characters,
  179. ),
  180. }
  181. scene["appearing_characters"] = self.filter_invalid_items(
  182. scene["appearing_characters"], source_type="entity", id_key="entity_id"
  183. )
  184. global_ctx = {
  185. "worldview_skeleton": self._load_setting("世界观"),
  186. "power_system_skeleton": self._load_setting("力量体系"),
  187. "style_contract_ref": self._load_setting("风格契约"),
  188. }
  189. preferences = self._load_json_optional(self.config.webnovel_dir / "preferences.json")
  190. memory = self._load_json_optional(self.config.webnovel_dir / "project_memory.json")
  191. story_skeleton = self._load_story_skeleton(chapter)
  192. alert_slice = max(0, int(self.config.context_alerts_slice))
  193. reader_signal = self._load_reader_signal(chapter)
  194. genre_profile = self._load_genre_profile(state)
  195. writing_guidance = self._build_writing_guidance(chapter, reader_signal, genre_profile)
  196. return {
  197. "meta": {"chapter": chapter},
  198. "core": core,
  199. "scene": scene,
  200. "global": global_ctx,
  201. "reader_signal": reader_signal,
  202. "genre_profile": genre_profile,
  203. "writing_guidance": writing_guidance,
  204. "story_skeleton": story_skeleton,
  205. "preferences": preferences,
  206. "memory": memory,
  207. "alerts": {
  208. "disambiguation_warnings": (
  209. state.get("disambiguation_warnings", [])[-alert_slice:] if alert_slice else []
  210. ),
  211. "disambiguation_pending": (
  212. state.get("disambiguation_pending", [])[-alert_slice:] if alert_slice else []
  213. ),
  214. },
  215. }
  216. def _load_reader_signal(self, chapter: int) -> Dict[str, Any]:
  217. if not getattr(self.config, "context_reader_signal_enabled", True):
  218. return {}
  219. recent_limit = max(1, int(getattr(self.config, "context_reader_signal_recent_limit", 5)))
  220. pattern_window = max(1, int(getattr(self.config, "context_reader_signal_window_chapters", 20)))
  221. review_window = max(1, int(getattr(self.config, "context_reader_signal_review_window", 5)))
  222. include_debt = bool(getattr(self.config, "context_reader_signal_include_debt", False))
  223. recent_power = self.index_manager.get_recent_reading_power(limit=recent_limit)
  224. pattern_stats = self.index_manager.get_pattern_usage_stats(last_n_chapters=pattern_window)
  225. hook_stats = self.index_manager.get_hook_type_stats(last_n_chapters=pattern_window)
  226. review_trend = self.index_manager.get_review_trend_stats(last_n=review_window)
  227. low_score_ranges: List[Dict[str, Any]] = []
  228. for row in review_trend.get("recent_ranges", []):
  229. score = row.get("overall_score")
  230. if isinstance(score, (int, float)) and float(score) < 75:
  231. low_score_ranges.append(
  232. {
  233. "start_chapter": row.get("start_chapter"),
  234. "end_chapter": row.get("end_chapter"),
  235. "overall_score": score,
  236. }
  237. )
  238. signal: Dict[str, Any] = {
  239. "recent_reading_power": recent_power,
  240. "pattern_usage": pattern_stats,
  241. "hook_type_usage": hook_stats,
  242. "review_trend": review_trend,
  243. "low_score_ranges": low_score_ranges,
  244. "next_chapter": chapter,
  245. }
  246. if include_debt:
  247. signal["debt_summary"] = self.index_manager.get_debt_summary()
  248. return signal
  249. def _load_genre_profile(self, state: Dict[str, Any]) -> Dict[str, Any]:
  250. if not getattr(self.config, "context_genre_profile_enabled", True):
  251. return {}
  252. fallback = str(getattr(self.config, "context_genre_profile_fallback", "shuangwen") or "shuangwen")
  253. project = state.get("project") or {}
  254. project_info = state.get("project_info") or {}
  255. genre_raw = str(project.get("genre") or project_info.get("genre") or fallback)
  256. genres = self._parse_genre_tokens(genre_raw)
  257. if not genres:
  258. genres = [fallback]
  259. max_genres = max(1, int(getattr(self.config, "context_genre_profile_max_genres", 2)))
  260. genres = genres[:max_genres]
  261. primary_genre = genres[0]
  262. secondary_genres = genres[1:]
  263. composite = len(genres) > 1
  264. profile_path = self.config.project_root / ".claude" / "references" / "genre-profiles.md"
  265. taxonomy_path = self.config.project_root / ".claude" / "references" / "reading-power-taxonomy.md"
  266. profile_text = profile_path.read_text(encoding="utf-8") if profile_path.exists() else ""
  267. taxonomy_text = taxonomy_path.read_text(encoding="utf-8") if taxonomy_path.exists() else ""
  268. profile_excerpt = self._extract_genre_section(profile_text, primary_genre)
  269. taxonomy_excerpt = self._extract_genre_section(taxonomy_text, primary_genre)
  270. secondary_profiles: List[str] = []
  271. secondary_taxonomies: List[str] = []
  272. for extra in secondary_genres:
  273. secondary_profiles.append(self._extract_genre_section(profile_text, extra))
  274. secondary_taxonomies.append(self._extract_genre_section(taxonomy_text, extra))
  275. refs = self._extract_markdown_refs(
  276. "\n".join([profile_excerpt] + secondary_profiles),
  277. max_items=int(getattr(self.config, "context_genre_profile_max_refs", 8)),
  278. )
  279. composite_hints = self._build_composite_genre_hints(genres, refs)
  280. return {
  281. "genre": primary_genre,
  282. "genre_raw": genre_raw,
  283. "genres": genres,
  284. "composite": composite,
  285. "secondary_genres": secondary_genres,
  286. "profile_excerpt": profile_excerpt,
  287. "taxonomy_excerpt": taxonomy_excerpt,
  288. "secondary_profile_excerpts": secondary_profiles,
  289. "secondary_taxonomy_excerpts": secondary_taxonomies,
  290. "reference_hints": refs,
  291. "composite_hints": composite_hints,
  292. }
  293. def _build_writing_guidance(
  294. self,
  295. chapter: int,
  296. reader_signal: Dict[str, Any],
  297. genre_profile: Dict[str, Any],
  298. ) -> Dict[str, Any]:
  299. if not getattr(self.config, "context_writing_guidance_enabled", True):
  300. return {}
  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. guidance_bundle = build_guidance_items(
  306. chapter=chapter,
  307. reader_signal=reader_signal,
  308. genre_profile=genre_profile,
  309. low_score_threshold=low_score_threshold,
  310. hook_diversify_enabled=bool(
  311. getattr(self.config, "context_writing_guidance_hook_diversify", True)
  312. ),
  313. )
  314. guidance = list(guidance_bundle.get("guidance") or [])
  315. methodology_strategy: Dict[str, Any] = {}
  316. if self._is_methodology_enabled_for_genre(genre_profile):
  317. methodology_strategy = build_methodology_strategy_card(
  318. chapter=chapter,
  319. reader_signal=reader_signal,
  320. genre_profile=genre_profile,
  321. label=str(getattr(self.config, "context_methodology_label", "digital-serial-v1")),
  322. )
  323. guidance.extend(build_methodology_guidance_items(methodology_strategy))
  324. checklist = self._build_writing_checklist(
  325. chapter=chapter,
  326. guidance_items=guidance,
  327. reader_signal=reader_signal,
  328. genre_profile=genre_profile,
  329. strategy_card=methodology_strategy,
  330. )
  331. checklist_score = self._compute_writing_checklist_score(
  332. chapter=chapter,
  333. checklist=checklist,
  334. reader_signal=reader_signal,
  335. )
  336. if getattr(self.config, "context_writing_score_persist_enabled", True):
  337. self._persist_writing_checklist_score(checklist_score)
  338. low_ranges = guidance_bundle.get("low_ranges") or []
  339. hook_usage = guidance_bundle.get("hook_usage") or {}
  340. pattern_usage = guidance_bundle.get("pattern_usage") or {}
  341. genre = str(guidance_bundle.get("genre") or genre_profile.get("genre") or "").strip()
  342. hook_types = list(hook_usage.keys())[:3] if isinstance(hook_usage, dict) else []
  343. top_patterns = (
  344. sorted(pattern_usage, key=pattern_usage.get, reverse=True)[:3]
  345. if isinstance(pattern_usage, dict)
  346. else []
  347. )
  348. return {
  349. "chapter": chapter,
  350. "guidance_items": guidance[:limit],
  351. "checklist": checklist,
  352. "checklist_score": checklist_score,
  353. "methodology": methodology_strategy,
  354. "signals_used": {
  355. "has_low_score_ranges": bool(low_ranges),
  356. "hook_types": hook_types,
  357. "top_patterns": top_patterns,
  358. "genre": genre,
  359. "methodology_enabled": bool(methodology_strategy.get("enabled")),
  360. },
  361. }
  362. def _compute_writing_checklist_score(
  363. self,
  364. chapter: int,
  365. checklist: List[Dict[str, Any]],
  366. reader_signal: Dict[str, Any],
  367. ) -> Dict[str, Any]:
  368. total_items = len(checklist)
  369. required_items = 0
  370. completed_items = 0
  371. completed_required = 0
  372. total_weight = 0.0
  373. completed_weight = 0.0
  374. pending_labels: List[str] = []
  375. for item in checklist:
  376. if not isinstance(item, dict):
  377. continue
  378. required = bool(item.get("required"))
  379. weight = float(item.get("weight") or 1.0)
  380. total_weight += weight
  381. if required:
  382. required_items += 1
  383. completed = self._is_checklist_item_completed(item, reader_signal)
  384. if completed:
  385. completed_items += 1
  386. completed_weight += weight
  387. if required:
  388. completed_required += 1
  389. else:
  390. pending_labels.append(str(item.get("label") or item.get("id") or "未命名项"))
  391. completion_rate = (completed_items / total_items) if total_items > 0 else 1.0
  392. weighted_rate = (completed_weight / total_weight) if total_weight > 0 else completion_rate
  393. required_rate = (completed_required / required_items) if required_items > 0 else 1.0
  394. score = 100.0 * (0.5 * weighted_rate + 0.3 * required_rate + 0.2 * completion_rate)
  395. if getattr(self.config, "context_writing_score_include_reader_trend", True):
  396. trend_window = max(1, int(getattr(self.config, "context_writing_score_trend_window", 10)))
  397. trend = self.index_manager.get_writing_checklist_score_trend(last_n=trend_window)
  398. baseline = float(trend.get("score_avg") or 0.0)
  399. if baseline > 0:
  400. score += max(-10.0, min(10.0, (score - baseline) * 0.1))
  401. score = round(max(0.0, min(100.0, score)), 2)
  402. return {
  403. "chapter": chapter,
  404. "score": score,
  405. "completion_rate": round(completion_rate, 4),
  406. "weighted_completion_rate": round(weighted_rate, 4),
  407. "required_completion_rate": round(required_rate, 4),
  408. "total_items": total_items,
  409. "required_items": required_items,
  410. "completed_items": completed_items,
  411. "completed_required": completed_required,
  412. "total_weight": round(total_weight, 2),
  413. "completed_weight": round(completed_weight, 2),
  414. "pending_items": pending_labels,
  415. "trend_window": int(getattr(self.config, "context_writing_score_trend_window", 10)),
  416. }
  417. def _is_checklist_item_completed(self, item: Dict[str, Any], reader_signal: Dict[str, Any]) -> bool:
  418. return is_checklist_item_completed(item, reader_signal)
  419. def _persist_writing_checklist_score(self, checklist_score: Dict[str, Any]) -> None:
  420. if not checklist_score:
  421. return
  422. try:
  423. self.index_manager.save_writing_checklist_score(
  424. WritingChecklistScoreMeta(
  425. chapter=int(checklist_score.get("chapter") or 0),
  426. template=str(getattr(self, "_active_template", self.DEFAULT_TEMPLATE) or self.DEFAULT_TEMPLATE),
  427. total_items=int(checklist_score.get("total_items") or 0),
  428. required_items=int(checklist_score.get("required_items") or 0),
  429. completed_items=int(checklist_score.get("completed_items") or 0),
  430. completed_required=int(checklist_score.get("completed_required") or 0),
  431. total_weight=float(checklist_score.get("total_weight") or 0.0),
  432. completed_weight=float(checklist_score.get("completed_weight") or 0.0),
  433. completion_rate=float(checklist_score.get("completion_rate") or 0.0),
  434. score=float(checklist_score.get("score") or 0.0),
  435. score_breakdown={
  436. "weighted_completion_rate": checklist_score.get("weighted_completion_rate"),
  437. "required_completion_rate": checklist_score.get("required_completion_rate"),
  438. "trend_window": checklist_score.get("trend_window"),
  439. },
  440. pending_items=list(checklist_score.get("pending_items") or []),
  441. source="context_manager",
  442. )
  443. )
  444. except Exception as exc:
  445. logger.warning("failed to persist writing checklist score: %s", exc)
  446. def _resolve_context_stage(self, chapter: int) -> str:
  447. early = max(1, int(getattr(self.config, "context_dynamic_budget_early_chapter", 30)))
  448. late = max(early + 1, int(getattr(self.config, "context_dynamic_budget_late_chapter", 120)))
  449. if chapter <= early:
  450. return "early"
  451. if chapter >= late:
  452. return "late"
  453. return "mid"
  454. def _resolve_template_weights(self, template: str, chapter: int) -> Dict[str, float]:
  455. template_key = template if template in self.TEMPLATE_WEIGHTS else self.DEFAULT_TEMPLATE
  456. base = dict(self.TEMPLATE_WEIGHTS.get(template_key, self.TEMPLATE_WEIGHTS[self.DEFAULT_TEMPLATE]))
  457. if not getattr(self.config, "context_dynamic_budget_enabled", True):
  458. return base
  459. stage = self._resolve_context_stage(chapter)
  460. dynamic_weights = getattr(self.config, "context_template_weights_dynamic", None)
  461. if not isinstance(dynamic_weights, dict):
  462. dynamic_weights = self.TEMPLATE_WEIGHTS_DYNAMIC
  463. stage_weights = dynamic_weights.get(stage, {}) if isinstance(dynamic_weights.get(stage, {}), dict) else {}
  464. staged = stage_weights.get(template_key)
  465. if isinstance(staged, dict):
  466. return dict(staged)
  467. return base
  468. def _parse_genre_tokens(self, genre_raw: str) -> List[str]:
  469. support_composite = bool(getattr(self.config, "context_genre_profile_support_composite", True))
  470. separators_raw = getattr(self.config, "context_genre_profile_separators", ("+", "/", "|", ","))
  471. separators = tuple(str(token) for token in separators_raw if str(token))
  472. return parse_genre_tokens(
  473. genre_raw,
  474. support_composite=support_composite,
  475. separators=separators,
  476. )
  477. def _normalize_genre_token(self, token: str) -> str:
  478. return normalize_genre_token(token)
  479. def _build_composite_genre_hints(self, genres: List[str], refs: List[str]) -> List[str]:
  480. return build_composite_genre_hints(genres, refs)
  481. def _build_writing_checklist(
  482. self,
  483. chapter: int,
  484. guidance_items: List[str],
  485. reader_signal: Dict[str, Any],
  486. genre_profile: Dict[str, Any],
  487. strategy_card: Dict[str, Any] | None = None,
  488. ) -> List[Dict[str, Any]]:
  489. _ = chapter
  490. if not getattr(self.config, "context_writing_checklist_enabled", True):
  491. return []
  492. min_items = max(1, int(getattr(self.config, "context_writing_checklist_min_items", 3)))
  493. max_items = max(min_items, int(getattr(self.config, "context_writing_checklist_max_items", 6)))
  494. default_weight = float(getattr(self.config, "context_writing_checklist_default_weight", 1.0))
  495. if default_weight <= 0:
  496. default_weight = 1.0
  497. return build_writing_checklist(
  498. guidance_items=guidance_items,
  499. reader_signal=reader_signal,
  500. genre_profile=genre_profile,
  501. strategy_card=strategy_card,
  502. min_items=min_items,
  503. max_items=max_items,
  504. default_weight=default_weight,
  505. )
  506. def _is_methodology_enabled_for_genre(self, genre_profile: Dict[str, Any]) -> bool:
  507. if not bool(getattr(self.config, "context_methodology_enabled", False)):
  508. return False
  509. whitelist_raw = getattr(self.config, "context_methodology_genre_whitelist", ("*",))
  510. if isinstance(whitelist_raw, str):
  511. whitelist_iter = [whitelist_raw]
  512. else:
  513. whitelist_iter = list(whitelist_raw or [])
  514. whitelist = {str(token).strip().lower() for token in whitelist_iter if str(token).strip()}
  515. if not whitelist:
  516. return True
  517. if "*" in whitelist or "all" in whitelist:
  518. return True
  519. genre = str((genre_profile or {}).get("genre") or "").strip()
  520. if not genre:
  521. return False
  522. profile_key = to_profile_key(genre)
  523. return profile_key in whitelist
  524. def _compact_json_text(self, content: Any, budget: Optional[int]) -> str:
  525. raw = json.dumps(content, ensure_ascii=False)
  526. if budget is None or len(raw) <= budget:
  527. return raw
  528. if not getattr(self.config, "context_compact_text_enabled", True):
  529. return raw[:budget]
  530. min_budget = max(1, int(getattr(self.config, "context_compact_min_budget", 120)))
  531. if budget <= min_budget:
  532. return raw[:budget]
  533. head_ratio = float(getattr(self.config, "context_compact_head_ratio", 0.65))
  534. head_budget = int(budget * max(0.2, min(0.9, head_ratio)))
  535. tail_budget = max(0, budget - head_budget - 10)
  536. compact = f"{raw[:head_budget]}…[TRUNCATED]{raw[-tail_budget:] if tail_budget else ''}"
  537. return compact[:budget]
  538. def _extract_genre_section(self, text: str, genre: str) -> str:
  539. return extract_genre_section(text, genre)
  540. def _extract_markdown_refs(self, text: str, max_items: int = 8) -> List[str]:
  541. return extract_markdown_refs(text, max_items=max_items)
  542. def _load_state(self) -> Dict[str, Any]:
  543. path = self.config.state_file
  544. if not path.exists():
  545. return {}
  546. return json.loads(path.read_text(encoding="utf-8"))
  547. def _load_outline(self, chapter: int) -> str:
  548. outline_dir = self.config.outline_dir
  549. patterns = [
  550. f"第{chapter}章*.md",
  551. f"第{chapter:02d}章*.md",
  552. f"第{chapter:03d}章*.md",
  553. f"第{chapter:04d}章*.md",
  554. ]
  555. for pattern in patterns:
  556. matches = list(outline_dir.glob(pattern))
  557. if matches:
  558. return matches[0].read_text(encoding="utf-8")
  559. return f"[大纲未找到: 第{chapter}章]"
  560. def _load_recent_summaries(self, chapter: int, window: int = 3) -> List[Dict[str, Any]]:
  561. summaries = []
  562. for ch in range(max(1, chapter - window), chapter):
  563. summary = self._load_summary_text(ch)
  564. if summary:
  565. summaries.append(summary)
  566. return summaries
  567. def _load_recent_meta(self, state: Dict[str, Any], chapter: int, window: int = 3) -> List[Dict[str, Any]]:
  568. meta = state.get("chapter_meta", {}) or {}
  569. results = []
  570. for ch in range(max(1, chapter - window), chapter):
  571. for key in (f"{ch:04d}", str(ch)):
  572. if key in meta:
  573. results.append({"chapter": ch, **meta.get(key, {})})
  574. break
  575. return results
  576. def _load_recent_appearances(self, limit: Optional[int] = None) -> List[Dict[str, Any]]:
  577. appearances = self.index_manager.get_recent_appearances(limit=limit)
  578. return appearances or []
  579. def _load_setting(self, keyword: str) -> str:
  580. settings_dir = self.config.settings_dir
  581. candidates = [
  582. settings_dir / f"{keyword}.md",
  583. ]
  584. for path in candidates:
  585. if path.exists():
  586. return path.read_text(encoding="utf-8")
  587. # fallback: any file containing keyword
  588. matches = list(settings_dir.glob(f"*{keyword}*.md"))
  589. if matches:
  590. return matches[0].read_text(encoding="utf-8")
  591. return f"[{keyword}设定未找到]"
  592. def _extract_summary_excerpt(self, text: str, max_chars: int) -> str:
  593. if not text:
  594. return ""
  595. match = self.SUMMARY_SECTION_RE.search(text)
  596. excerpt = match.group(1).strip() if match else text.strip()
  597. if max_chars > 0 and len(excerpt) > max_chars:
  598. return excerpt[:max_chars].rstrip()
  599. return excerpt
  600. def _load_summary_text(self, chapter: int, snippet_chars: Optional[int] = None) -> Optional[Dict[str, Any]]:
  601. summary_path = self.config.webnovel_dir / "summaries" / f"ch{chapter:04d}.md"
  602. if not summary_path.exists():
  603. return None
  604. text = summary_path.read_text(encoding="utf-8")
  605. if snippet_chars:
  606. summary_text = self._extract_summary_excerpt(text, snippet_chars)
  607. else:
  608. summary_text = text
  609. return {"chapter": chapter, "summary": summary_text}
  610. def _load_story_skeleton(self, chapter: int) -> List[Dict[str, Any]]:
  611. interval = max(1, int(self.config.context_story_skeleton_interval))
  612. max_samples = max(0, int(self.config.context_story_skeleton_max_samples))
  613. snippet_chars = int(self.config.context_story_skeleton_snippet_chars)
  614. if max_samples <= 0 or chapter <= interval:
  615. return []
  616. samples: List[Dict[str, Any]] = []
  617. cursor = chapter - interval
  618. while cursor >= 1 and len(samples) < max_samples:
  619. summary = self._load_summary_text(cursor, snippet_chars=snippet_chars)
  620. if summary and summary.get("summary"):
  621. samples.append(summary)
  622. cursor -= interval
  623. samples.reverse()
  624. return samples
  625. def _load_json_optional(self, path: Path) -> Dict[str, Any]:
  626. if not path.exists():
  627. return {}
  628. try:
  629. return json.loads(path.read_text(encoding="utf-8"))
  630. except json.JSONDecodeError:
  631. return {}
  632. def main():
  633. import argparse
  634. from .cli_output import print_success, print_error
  635. parser = argparse.ArgumentParser(description="Context Manager CLI")
  636. parser.add_argument("--project-root", type=str, help="项目根目录")
  637. parser.add_argument("--chapter", type=int, required=True)
  638. parser.add_argument("--template", type=str, default=ContextManager.DEFAULT_TEMPLATE)
  639. parser.add_argument("--no-snapshot", action="store_true")
  640. parser.add_argument("--max-chars", type=int, default=8000)
  641. args = parser.parse_args()
  642. config = None
  643. if args.project_root:
  644. # 允许传入“工作区根目录”,统一解析到真正的 book project_root(必须包含 .webnovel/state.json)
  645. from project_locator import resolve_project_root
  646. from .config import DataModulesConfig
  647. resolved_root = resolve_project_root(args.project_root)
  648. config = DataModulesConfig.from_project_root(resolved_root)
  649. manager = ContextManager(config)
  650. try:
  651. payload = manager.build_context(
  652. chapter=args.chapter,
  653. template=args.template,
  654. use_snapshot=not args.no_snapshot,
  655. save_snapshot=True,
  656. max_chars=args.max_chars,
  657. )
  658. print_success(payload, message="context_built")
  659. try:
  660. manager.index_manager.log_tool_call("context_manager:build", True, chapter=args.chapter)
  661. except Exception as exc:
  662. logger.warning("failed to log successful tool call: %s", exc)
  663. except Exception as exc:
  664. print_error("CONTEXT_BUILD_FAILED", str(exc), suggestion="请检查项目结构与依赖文件")
  665. try:
  666. manager.index_manager.log_tool_call(
  667. "context_manager:build", False, error_code="CONTEXT_BUILD_FAILED", error_message=str(exc), chapter=args.chapter
  668. )
  669. except Exception as log_exc:
  670. logger.warning("failed to log failed tool call: %s", log_exc)
  671. if __name__ == "__main__":
  672. import sys
  673. if sys.platform == "win32":
  674. enable_windows_utf8_stdio()
  675. main()