context_manager.py 30 KB

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