context_manager.py 30 KB

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