kb.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. # -*- coding: utf-8 -*-
  2. """
  3. kb.py — 做课系统知识库工具链
  4. 所有"客观事实"由本脚本负责:文件指纹、差异计算、关系图、索引重建、健康检查。
  5. 所有"主观判断"由 skill(LLM)负责:抽取、归纳、写页、写摘要。
  6. 设计原则:
  7. 1. 脚本不碰内容语义,只维护结构。
  8. 2. 幂等:任何命令重复执行结果一致。
  9. 3. 可回滚:删除先进 _archive,不物理销毁。
  10. 用法:
  11. python kb.py status
  12. python kb.py scan [--json]
  13. python kb.py mark <源相对路径> --pages a.md,b.md
  14. python kb.py retract <源相对路径>
  15. python kb.py move <旧路径> <新路径>
  16. python kb.py graph
  17. python kb.py lint [--json]
  18. """
  19. import sys, os, json, re, hashlib, shutil, argparse, subprocess
  20. from pathlib import Path
  21. from datetime import datetime, timezone, timedelta
  22. if hasattr(sys.stdout, "reconfigure"):
  23. sys.stdout.reconfigure(encoding="utf-8", errors="replace")
  24. ROOT = Path(__file__).resolve().parent.parent
  25. RAW = ROOT / "00-原材料"
  26. WIKI = ROOT / "10-知识库"
  27. META = WIKI / "_meta"
  28. ARCHIVE = WIKI / "_archive"
  29. MANIFEST = META / "manifest.json"
  30. DERIV = META / "derivations.jsonl"
  31. CHANGELOG = META / "changelog.jsonl"
  32. CHANGELOG_MD = META / "CHANGELOG.md"
  33. REBUILD = META / "rebuild_queue.jsonl"
  34. CST = timezone(timedelta(hours=8))
  35. DIRS = ["sources", "topics", "entities", "modules"]
  36. # 原料目录里需要无视的噪声
  37. NOISE_NAMES = {"desktop.ini", "thumbs.db", ".ds_store", ".gitkeep"}
  38. NOISE_TAIL = (".tmp", ".crdownload", ".part", ".download", ".swp", ".lock")
  39. # ---------------------------------------------------------------- 基础工具
  40. def now() -> str:
  41. return datetime.now(CST).isoformat(timespec="seconds")
  42. def rel(p: Path, base: Path = None) -> str:
  43. """统一成 POSIX 风格相对路径,作为全库唯一键。"""
  44. return p.relative_to(base or RAW).as_posix()
  45. def sha256(p: Path, chunk: int = 1 << 20) -> str:
  46. h = hashlib.sha256()
  47. with p.open("rb") as f:
  48. for blk in iter(lambda: f.read(chunk), b""):
  49. h.update(blk)
  50. return h.hexdigest()
  51. def short_id(key: str) -> str:
  52. return "src_" + hashlib.sha1(key.encode("utf-8")).hexdigest()[:8]
  53. def is_noise(p: Path) -> bool:
  54. n = p.name
  55. if n.startswith(".") or n.startswith("~$"):
  56. return True
  57. if n.lower() in NOISE_NAMES:
  58. return True
  59. return n.lower().endswith(NOISE_TAIL)
  60. def load_json(p: Path, default):
  61. if p.exists():
  62. try:
  63. return json.loads(p.read_text(encoding="utf-8"))
  64. except Exception:
  65. pass
  66. return default
  67. def dump_json(p: Path, obj):
  68. p.parent.mkdir(parents=True, exist_ok=True)
  69. p.write_text(json.dumps(obj, ensure_ascii=False, indent=2), encoding="utf-8")
  70. def append_jsonl(p: Path, row: dict):
  71. p.parent.mkdir(parents=True, exist_ok=True)
  72. with p.open("a", encoding="utf-8") as f:
  73. f.write(json.dumps(row, ensure_ascii=False) + "\n")
  74. def read_jsonl(p: Path) -> list:
  75. if not p.exists():
  76. return []
  77. out = []
  78. for line in p.read_text(encoding="utf-8").splitlines():
  79. line = line.strip()
  80. if line:
  81. try:
  82. out.append(json.loads(line))
  83. except Exception:
  84. pass
  85. return out
  86. def log_human(op: str, subject: str, detail: str = ""):
  87. """人读日志,与 changelog.jsonl 双写。
  88. 统一前缀是刻意设计:`grep '^## \\[' CHANGELOG.md | tail -5` 就能取最近 5 条,
  89. 不需要任何工具。这是 Karpathy llm-wiki 里 log.md 的做法,被验证过很好用。
  90. """
  91. if not CHANGELOG_MD.exists():
  92. CHANGELOG_MD.write_text(
  93. "# 变更日志\n\n"
  94. "> 只增不改。统一前缀便于 shell 检索:\n"
  95. "> `grep '^## \\[' CHANGELOG.md | tail -5`\n\n",
  96. encoding="utf-8")
  97. stamp = datetime.now(CST).strftime("%Y-%m-%d %H:%M")
  98. block = f"## [{stamp}] {op} | {subject}\n"
  99. if detail:
  100. block += detail.rstrip() + "\n"
  101. with CHANGELOG_MD.open("a", encoding="utf-8") as f:
  102. f.write(block + "\n")
  103. # ------------------------------------------------------------ frontmatter
  104. def split_fm(text: str):
  105. """返回 (dict, body)。不依赖 pyyaml,只支持本库用到的子集。"""
  106. if not text.startswith("---"):
  107. return {}, text
  108. m = re.match(r"^---\r?\n(.*?)\r?\n---\r?\n?", text, re.S)
  109. if not m:
  110. return {}, text
  111. raw, body = m.group(1), text[m.end():]
  112. data, key = {}, None
  113. for line in raw.splitlines():
  114. if not line.strip() or line.lstrip().startswith("#"):
  115. continue
  116. if re.match(r"^\s+-\s+", line) and key:
  117. if not isinstance(data.get(key), list):
  118. data[key] = []
  119. data[key].append(_scalar(line.strip()[2:]))
  120. continue
  121. km = re.match(r"^([A-Za-z_][\w-]*)\s*:\s*(.*)$", line)
  122. if km:
  123. key, val = km.group(1), km.group(2).strip()
  124. if val == "":
  125. data[key] = []
  126. elif val.startswith("[") and val.endswith("]"):
  127. inner = val[1:-1].strip()
  128. data[key] = [_scalar(x) for x in inner.split(",")] if inner else []
  129. else:
  130. data[key] = _scalar(val)
  131. return data, body
  132. def _scalar(s: str):
  133. s = s.strip()
  134. if len(s) >= 2 and s[0] == s[-1] and s[0] in "\"'":
  135. return s[1:-1]
  136. if s.lower() in ("true", "false"):
  137. return s.lower() == "true"
  138. return s
  139. def dump_fm(fm: dict) -> str:
  140. order = ["id", "type", "title", "summary", "evidence", "tags", "sources",
  141. "related", "status", "stale", "stale_reason", "created", "updated"]
  142. keys = [k for k in order if k in fm] + [k for k in fm if k not in order]
  143. lines = ["---"]
  144. for k in keys:
  145. v = fm[k]
  146. if isinstance(v, list):
  147. if not v:
  148. lines.append(f"{k}: []")
  149. else:
  150. lines.append(f"{k}:")
  151. lines += [f" - {json.dumps(str(x), ensure_ascii=False)}" for x in v]
  152. elif isinstance(v, bool):
  153. lines.append(f"{k}: {'true' if v else 'false'}")
  154. else:
  155. lines.append(f"{k}: {json.dumps(str(v), ensure_ascii=False)}")
  156. lines.append("---")
  157. return "\n".join(lines) + "\n"
  158. def read_page(p: Path):
  159. text = p.read_text(encoding="utf-8", errors="replace")
  160. fm, body = split_fm(text)
  161. return fm, body
  162. def write_page(p: Path, fm: dict, body: str):
  163. p.parent.mkdir(parents=True, exist_ok=True)
  164. p.write_text(dump_fm(fm) + body, encoding="utf-8")
  165. # ---------------------------------------------------------------- scan
  166. def manifest_skeleton():
  167. return {"version": 2, "root": "00-原材料", "updated_at": None,
  168. "files": {}, "tombstones": []}
  169. def collect_raw() -> dict:
  170. """返回 {relpath: {size, mtime_ns, sha256}},大文件用 size+mtime 短路。"""
  171. man = load_json(MANIFEST, manifest_skeleton())
  172. known = man.get("files", {})
  173. out = {}
  174. if not RAW.exists():
  175. return out
  176. for p in sorted(RAW.rglob("*")):
  177. if not p.is_file() or is_noise(p):
  178. continue
  179. r = rel(p)
  180. st = p.stat()
  181. old = known.get(r, {})
  182. if old.get("size") == st.st_size and old.get("mtime_ns") == st.st_mtime_ns and old.get("sha256"):
  183. out[r] = {"size": st.st_size, "mtime_ns": st.st_mtime_ns,
  184. "sha256": old["sha256"], "fast": True}
  185. else:
  186. out[r] = {"size": st.st_size, "mtime_ns": st.st_mtime_ns,
  187. "sha256": sha256(p), "fast": False}
  188. return out
  189. def cmd_scan(args):
  190. man = load_json(MANIFEST, manifest_skeleton())
  191. known = man.get("files", {})
  192. cur = collect_raw()
  193. new_keys = sorted(set(cur) - set(known))
  194. gone_keys = sorted(set(known) - set(cur))
  195. common = sorted(set(cur) & set(known))
  196. changed = [k for k in common if cur[k]["sha256"] != known[k].get("sha256")]
  197. # 移动识别:内容 hash 相同、路径不同
  198. gone_by_hash = {}
  199. for k in gone_keys:
  200. h = known[k].get("sha256")
  201. if h:
  202. gone_by_hash.setdefault(h, []).append(k)
  203. new_by_hash = {}
  204. for k in new_keys:
  205. new_by_hash.setdefault(cur[k]["sha256"], []).append(k)
  206. moved, consumed_new, consumed_gone = [], set(), set()
  207. for h, news in new_by_hash.items():
  208. gones = gone_by_hash.get(h, [])
  209. if len(news) == 1 and len(gones) == 1: # 只在能唯一配对时认定移动
  210. moved.append({"from": gones[0], "to": news[0], "sha256": h})
  211. consumed_new.add(news[0]); consumed_gone.add(gones[0])
  212. result = {
  213. "generated_at": now(),
  214. "new": [k for k in new_keys if k not in consumed_new],
  215. "changed": changed,
  216. "deleted": [k for k in gone_keys if k not in consumed_gone],
  217. "moved": moved,
  218. "unchanged": len(common) - len(changed),
  219. "details": {k: {"size": cur[k]["size"]} for k in
  220. [*new_keys, *changed] if k in cur},
  221. }
  222. if args.json:
  223. print(json.dumps(result, ensure_ascii=False, indent=2))
  224. return
  225. print(f"扫描 {RAW.name}/ @{result['generated_at']}")
  226. print(f" 新增 {len(result['new'])} · 变更 {len(result['changed'])} · "
  227. f"删除 {len(result['deleted'])} · 移动 {len(result['moved'])} · "
  228. f"未变 {result['unchanged']}")
  229. for tag, key in (("NEW", "new"), ("CHANGED", "changed"), ("DELETED", "deleted")):
  230. for k in result[key]:
  231. print(f" [{tag}] {k}")
  232. for m in result["moved"]:
  233. print(f" [MOVED] {m['from']} -> {m['to']}")
  234. if not any(result[k] for k in ("new", "changed", "deleted", "moved")):
  235. print(" 库已是最新,无需同步。")
  236. # ---------------------------------------------------------------- mark
  237. def cmd_mark(args):
  238. man = load_json(MANIFEST, manifest_skeleton())
  239. cur = collect_raw()
  240. r = args.path
  241. if r not in cur:
  242. print(f"[ERR] 原材料中不存在:{r}")
  243. return 1
  244. pages = [x.strip().replace("\\", "/") for x in (args.pages or "").split(",") if x.strip()]
  245. rec = man["files"].get(r, {})
  246. rec.update({"size": cur[r]["size"], "mtime_ns": cur[r]["mtime_ns"],
  247. "sha256": cur[r]["sha256"], "id": rec.get("id") or short_id(r),
  248. "status": "ingested", "ingested_at": now(),
  249. "pages": sorted(set(rec.get("pages", []) + pages))})
  250. man["files"][r] = rec
  251. man["updated_at"] = now()
  252. dump_json(MANIFEST, man)
  253. append_jsonl(CHANGELOG, {"ts": now(), "op": "ingest", "source": r,
  254. "pages": pages, "sha256": rec["sha256"][:12]})
  255. log_human("ingest", r, "\n".join(f"- 派生 `{x}`" for x in pages))
  256. print(f"[OK] 已登记 {r} -> {len(pages)} 页")
  257. # ---------------------------------------------------------------- retract
  258. def _backlinks(target_keys: set) -> dict:
  259. """扫描全库,返回 {被链页面: [引用它的页面]}。用于级联失效。"""
  260. links = {}
  261. for d in DIRS:
  262. for p in (WIKI / d).rglob("*.md"):
  263. if p.name.startswith("_"):
  264. continue
  265. fm, body = read_page(p)
  266. for name in set(re.findall(r"\[\[([^\]|#]+)", body)):
  267. links.setdefault(name.strip(), []).append(rel(p, WIKI))
  268. return links
  269. def cmd_retract(args):
  270. """源文件被删除后回滚知识库。三种情形:独占页归档 / 共享页降级 / 级联标记。"""
  271. src = args.path.replace("\\", "/")
  272. man = load_json(MANIFEST, manifest_skeleton())
  273. if src not in man["files"]:
  274. print(f"[ERR] manifest 中无此源:{src}")
  275. return 1
  276. archived, degraded, cascade = [], [], []
  277. touched_stems = set()
  278. for d in DIRS:
  279. for p in sorted((WIKI / d).rglob("*.md")):
  280. if p.name.startswith("_"):
  281. continue
  282. fm, body = read_page(p)
  283. srcs = fm.get("sources") or []
  284. if not isinstance(srcs, list):
  285. srcs = [srcs]
  286. if src not in srcs:
  287. continue
  288. stamped = datetime.now(CST).strftime("%Y%m%d-%H%M%S")
  289. if len(srcs) == 1:
  290. ARCHIVE.mkdir(parents=True, exist_ok=True)
  291. dest = ARCHIVE / f"{p.stem}.{stamped}.md"
  292. fm["status"] = "archived"
  293. fm["stale_reason"] = f"源文件已删除:{src}"
  294. fm["updated"] = now()
  295. write_page(dest, fm, body)
  296. p.unlink()
  297. archived.append(rel(dest, WIKI))
  298. touched_stems.add(p.stem)
  299. else:
  300. fm["sources"] = [s for s in srcs if s != src]
  301. fm["stale"] = True
  302. fm["stale_reason"] = f"少了一个来源({src}),需重新合成"
  303. fm["updated"] = now()
  304. write_page(p, fm, body)
  305. degraded.append(rel(p, WIKI))
  306. append_jsonl(REBUILD, {"ts": now(), "page": rel(p, WIKI),
  307. "reason": "source_removed", "source": src})
  308. touched_stems.add(p.stem)
  309. # 级联:引用了已归档页面的页面 -> 标记待复核
  310. if touched_stems:
  311. back = _backlinks(touched_stems)
  312. for stem in touched_stems:
  313. for ref in back.get(stem, []):
  314. fp = WIKI / ref
  315. if not fp.exists():
  316. continue
  317. fm, body = read_page(fp)
  318. if fm.get("stale"):
  319. continue
  320. fm["stale"] = True
  321. fm["stale_reason"] = f"引用的页面 {stem} 已因源删除而失效"
  322. fm["updated"] = now()
  323. write_page(fp, fm, body)
  324. cascade.append(ref)
  325. rec = man["files"].pop(src, {})
  326. man.setdefault("tombstones", []).append({"path": src, "removed_at": now(),
  327. "sha256": rec.get("sha256"),
  328. "pages": rec.get("pages", [])})
  329. man["updated_at"] = now()
  330. dump_json(MANIFEST, man)
  331. append_jsonl(CHANGELOG, {"ts": now(), "op": "retract", "source": src,
  332. "archived": archived, "degraded": degraded,
  333. "cascade": sorted(set(cascade))})
  334. log_human("retract", src,
  335. "\n".join([f"- 归档 `{x}`" for x in archived] +
  336. [f"- 降级待重建 `{x}`" for x in degraded] +
  337. [f"- 级联标记 `{x}`" for x in sorted(set(cascade))]) or "- 无派生页")
  338. print(f"[OK] 回滚 {src}")
  339. print(f" 归档页 {len(archived)} · 降级为待重建 {len(degraded)} · 级联标记 {len(set(cascade))}")
  340. for x in archived:
  341. print(f" ARCHIVE {x}")
  342. for x in degraded:
  343. print(f" DEGRADE {x}")
  344. for x in sorted(set(cascade)):
  345. print(f" CASCADE {x}")
  346. if not archived and not degraded:
  347. print(" (该源未派生任何页面,仅清理 manifest)")
  348. def cmd_move(args):
  349. """源文件移动:sha 相同,只改 manifest 和所有 frontmatter 引用。"""
  350. old, new = args.old.replace("\\", "/"), args.new.replace("\\", "/")
  351. man = load_json(MANIFEST, manifest_skeleton())
  352. if old not in man["files"]:
  353. print(f"[ERR] manifest 中无此源:{old}")
  354. return 1
  355. man["files"][new] = man["files"].pop(old)
  356. man["updated_at"] = now()
  357. dump_json(MANIFEST, man)
  358. n = 0
  359. for d in DIRS:
  360. for p in (WIKI / d).rglob("*.md"):
  361. fm, body = read_page(p)
  362. srcs = fm.get("sources")
  363. if isinstance(srcs, list) and old in srcs:
  364. fm["sources"] = [new if s == old else s for s in srcs]
  365. fm["updated"] = now()
  366. write_page(p, fm, body)
  367. n += 1
  368. append_jsonl(CHANGELOG, {"ts": now(), "op": "move", "from": old, "to": new})
  369. log_human("move", f"{old} -> {new}", f"- 更新引用 {n} 页")
  370. print(f"[OK] 移动登记 {old} -> {new},更新引用 {n} 页")
  371. # ---------------------------------------------------------------- graph
  372. def all_pages():
  373. out = []
  374. for d in DIRS:
  375. base = WIKI / d
  376. if not base.exists():
  377. continue
  378. for p in sorted(base.rglob("*.md")):
  379. if p.name.startswith("_"):
  380. continue
  381. fm, body = read_page(p)
  382. out.append({"path": rel(p, WIKI), "stem": p.stem, "fm": fm,
  383. "body": body, "dir": d})
  384. return out
  385. def cmd_graph(args):
  386. pages = all_pages()
  387. edges = []
  388. index_map = {p["stem"]: p for p in pages}
  389. for p in pages:
  390. fm = p["fm"]
  391. for s in (fm.get("sources") or []):
  392. edges.append({"kind": "derives", "from": str(s), "to": p["path"]})
  393. for name in re.findall(r"\[\[([^\]|#]+)", p["body"]):
  394. tgt = index_map.get(name.strip())
  395. if tgt:
  396. edges.append({"kind": "links", "from": p["path"], "to": tgt["path"]})
  397. DERIV.write_text("".join(json.dumps(e, ensure_ascii=False) + "\n" for e in edges),
  398. encoding="utf-8")
  399. # 重建各目录 _index.md
  400. ts = now()
  401. titles = {"sources": "来源页", "topics": "主题页", "entities": "实体页",
  402. "modules": "课程模块"}
  403. for d in DIRS:
  404. group = [p for p in pages if p["dir"] == d]
  405. lines = [f"# {titles[d]}索引\n",
  406. f"> 自动生成于 {ts},共 {len(group)} 条。请勿手改,改页面 frontmatter 后重跑 `kb.py graph`。\n"]
  407. if not group:
  408. lines.append("_(暂无内容)_\n")
  409. else:
  410. lines.append("| 页面 | 一句话 | 标签 | 源数 | 更新 | 状态 |")
  411. lines.append("|---|---|---|---|---|---|")
  412. for p in sorted(group, key=lambda x: x["path"]):
  413. fm = p["fm"]
  414. flag = "⚠️stale" if fm.get("stale") else (fm.get("status") or "ok")
  415. lines.append("| [{}]({}) | {} | {} | {} | {} | {} |".format(
  416. fm.get("title") or p["stem"], p["path"],
  417. (fm.get("summary") or "").replace("|", "/"),
  418. ", ".join(fm.get("tags") or []) or "-",
  419. len(fm.get("sources") or []),
  420. str(fm.get("updated") or "")[:10], flag))
  421. (WIKI / d / "_index.md").write_text("\n".join(lines) + "\n", encoding="utf-8")
  422. print(f"[OK] 关系图重建:{len(pages)} 页,{len(edges)} 条边 -> {rel(DERIV, ROOT)}")
  423. for d in DIRS:
  424. print(f" {d}/_index.md ({len([p for p in pages if p['dir']==d])} 条)")
  425. # ---------------------------------------------------------------- lint
  426. def cmd_lint(args):
  427. pages = all_pages()
  428. stems = {p["stem"] for p in pages}
  429. aliases = set()
  430. for p in pages:
  431. for a in (p["fm"].get("aliases") or []):
  432. aliases.add(str(a))
  433. man = load_json(MANIFEST, manifest_skeleton())
  434. live_sources = set(man["files"].keys())
  435. inbound = {}
  436. for p in pages:
  437. for name in re.findall(r"\[\[([^\]|#]+)", p["body"]):
  438. inbound.setdefault(name.strip(), []).append(p["path"])
  439. issues = []
  440. # 源文件路径 -> 该源的 source 页所标证据等级(用于木桶校验)
  441. # 字母序 A<B<C<D,越靠后越弱,所以「最弱」取 max
  442. src_ev = {}
  443. for p in pages:
  444. if p["fm"].get("type") == "source" and p["fm"].get("evidence"):
  445. for s in (p["fm"].get("sources") or []):
  446. ev = p["fm"]["evidence"]
  447. src_ev[s] = max(src_ev[s], ev) if s in src_ev else ev
  448. for p in pages:
  449. fm = p["fm"]
  450. if not fm:
  451. issues.append(("NO_FM", p["path"], "缺少 frontmatter"))
  452. continue
  453. for f in ("title", "type", "summary"):
  454. if not fm.get(f):
  455. issues.append(("MISSING_FIELD", p["path"], f"缺少字段 {f}"))
  456. ev = fm.get("evidence")
  457. if not ev:
  458. issues.append(("MISSING_EVIDENCE", p["path"], "未标证据等级 A/B/C/D"))
  459. elif ev not in ("A", "B", "C", "D"):
  460. issues.append(("BAD_EVIDENCE", p["path"], f"证据等级非法:{ev}"))
  461. else:
  462. if fm.get("type") in ("topic", "module"):
  463. lows = [src_ev[s] for s in (fm.get("sources") or []) if s in src_ev]
  464. weakest = max(lows) if lows else None # 字母最大 = 证据最弱
  465. if weakest and ev < weakest:
  466. issues.append(("EVIDENCE_INFLATED", p["path"],
  467. f"标了 {ev},但来源最低只有 {weakest}(合成不提升证据强度)"))
  468. if fm.get("type") == "module" and ev == "D":
  469. issues.append(("D_IN_MODULE", p["path"], "D 级内容不许进课件"))
  470. if len(p["body"].strip()) < 100:
  471. issues.append(("THIN", p["path"], f"正文过短({len(p['body'].strip())} 字)"))
  472. for s in (fm.get("sources") or []):
  473. if s not in live_sources:
  474. issues.append(("DANGLING_SOURCE", p["path"], f"引用了不存在的源:{s}"))
  475. for name in set(re.findall(r"\[\[([^\]|#]+)", p["body"])):
  476. if name.strip() not in stems and name.strip() not in aliases:
  477. issues.append(("DEAD_LINK", p["path"], f"[[{name.strip()}]] 无对应页"))
  478. if not (fm.get("sources") or []) and not inbound.get(p["stem"]):
  479. issues.append(("ORPHAN", p["path"], "无来源且无入链"))
  480. if fm.get("stale"):
  481. issues.append(("STALE", p["path"], fm.get("stale_reason") or "待重建"))
  482. if fm.get("type") == "source":
  483. src = (fm.get("sources") or [None])[0]
  484. if src and man["files"].get(src, {}).get("sha256") and \
  485. fm.get("source_sha") and fm["source_sha"] != man["files"][src]["sha256"]:
  486. issues.append(("SOURCE_DRIFT", p["path"], "源文件已变更,页面需重新整理"))
  487. pend = read_jsonl(REBUILD)
  488. summary = {
  489. "pages": len(pages), "sources": len(live_sources),
  490. "issues": len(issues),
  491. "by_kind": {},
  492. "rebuild_queue": len(pend),
  493. "score": max(0, 100 - len(issues) * 3 - len(pend) * 2),
  494. }
  495. for k, _, _ in issues:
  496. summary["by_kind"][k] = summary["by_kind"].get(k, 0) + 1
  497. if args.json:
  498. print(json.dumps({"summary": summary,
  499. "issues": [{"kind": k, "page": p, "detail": d} for k, p, d in issues]},
  500. ensure_ascii=False, indent=2))
  501. return
  502. print(f"健康分 {summary['score']}/100 · {len(pages)} 页 · {len(live_sources)} 源 · "
  503. f"{len(issues)} 问题 · {len(pend)} 项待重建")
  504. if issues:
  505. print("\n按类型:")
  506. for k, v in sorted(summary["by_kind"].items(), key=lambda x: -x[1]):
  507. print(f" {k:<18} {v}")
  508. print("\n明细:")
  509. for k, p, d in issues:
  510. print(f" [{k}] {p} — {d}")
  511. else:
  512. print("未发现问题。")
  513. # ---------------------------------------------------------------- status
  514. def cmd_status(args):
  515. man = load_json(MANIFEST, manifest_skeleton())
  516. pages = all_pages()
  517. pend = read_jsonl(REBUILD)
  518. logs = read_jsonl(CHANGELOG)
  519. cur = collect_raw()
  520. pending_new = set(cur) - set(man["files"])
  521. pending_gone = set(man["files"]) - set(cur)
  522. stale = [p["path"] for p in pages if p["fm"].get("stale")]
  523. print("=" * 56)
  524. print(" 做课系统 · 知识库状态")
  525. print("=" * 56)
  526. print(f" 根目录 {ROOT}")
  527. print(f" 最近同步 {man.get('updated_at') or '从未'}")
  528. print(f" 原料文件 {len(cur)} 个(已整理 {len(man['files'])})")
  529. print(f" 知识页 {len(pages)} 页")
  530. for d in DIRS:
  531. print(f" {d:<10} {len([p for p in pages if p['dir']==d])}")
  532. print(f" 待处理新增 {len(pending_new)}")
  533. print(f" 待处理删除 {len(pending_gone)}")
  534. print(f" 待重建 {len(pend)}")
  535. print(f" 陈旧页 {len(stale)}")
  536. print(f" 历史操作 {len(logs)} 条")
  537. if pending_new:
  538. print("\n 未入库:")
  539. for x in sorted(pending_new)[:12]:
  540. print(f" + {x}")
  541. if len(pending_new) > 12:
  542. print(f" ... 还有 {len(pending_new)-12} 个")
  543. if pending_gone:
  544. print("\n 源已消失(待回滚):")
  545. for x in sorted(pending_gone)[:12]:
  546. print(f" - {x}")
  547. if stale:
  548. print("\n 陈旧页:")
  549. for x in stale[:12]:
  550. print(f" ! {x}")
  551. print("=" * 56)
  552. if pending_new or pending_gone or pend:
  553. print(" → 建议运行:kb-sync")
  554. else:
  555. print(" → 库是干净的")
  556. # ---------------------------------------------------------------- guide
  557. def cmd_guide(args):
  558. """给 AI 的自描述入口(对应 llmwiki 的 guide 工具)。
  559. 为什么要它:每次新会话,AI 都要重建对这套系统的认知。让它读 README 加 8 个
  560. SKILL.md,要烧掉几万 token,读完还不一定知道"现在该干什么"。
  561. 这个命令一次给全:这是什么、铁律是什么、当前什么状态、下一步做什么。
  562. """
  563. man = load_json(MANIFEST, manifest_skeleton())
  564. pages = all_pages()
  565. cur = collect_raw()
  566. pend_new = sorted(set(cur) - set(man["files"]))
  567. pend_gone = sorted(set(man["files"]) - set(cur))
  568. stale = [p["path"] for p in pages if p["fm"].get("stale")]
  569. nreb = len(read_jsonl(REBUILD))
  570. print("=" * 60)
  571. print(" 做课系统 · 给接手者的指南")
  572. print("=" * 60)
  573. print("""
  574. 【这是什么】
  575. 一条单向管道:
  576. 00-原材料/ ──你整理──▶ 10-知识库/ ──取材──▶ 20-课程产出/
  577. (只进不改) (全 md,你维护) (交付物)
  578. 人是资料策展人,你是维护者。人不写知识库,你写。
  579. 【三条铁律】
  580. 1. 差异问脚本,别凭记忆 —— 判断哪些文件是新的,永远跑 kb.py scan
  581. 2. 脚本管事实,你管语义 —— 页数、hash、关系图都问脚本
  582. 3. 补料不许编,取材必标出处 —— 这是两条方向相反的规则,见 AGENTS.md
  583. 【该走哪条路】
  584. 开工第一件事 → kb.py pull (对齐远端,报出别人推了什么)
  585. 丢进了新资料 / 删了源文件 → 30-技能/kb-sync (先 scan 再分流)
  586. 查点东西 → 30-技能/kb-query (好答案记得沉淀回库)
  587. 要做一门课 → 30-技能/course-outline → course-slides
  588. 库看着有点乱 → kb.py check (结构 + 内容一起过)
  589. 改页面格式或加规则 → 10-知识库/_meta/约定.md(改完登记起因)
  590. 收工 → GIT.md 的提交规范 (不推等于白干)
  591. manifest 被 git 搞冲突了 → kb.py repair (从磁盘和页面反推)
  592. 【格式硬约束】(细节见 约定.md)
  593. 每页 frontmatter 必填:type / title / summary / evidence / sources
  594. evidence 只能是 A/B/C/D,且 topic、module 页不得高于其来源最低级
  595. sources 填原材料相对路径,一字不差 —— 这是删除回滚的唯一依据
  596. """)
  597. print("-" * 60)
  598. print("【当前状态】")
  599. print(f" 原料 {len(cur)} 个(已整理 {len(man['files'])})· "
  600. f"知识页 {len(pages)} 页 · 最近同步 {man.get('updated_at') or '从未'}")
  601. for d in DIRS:
  602. n = len([p for p in pages if p["dir"] == d])
  603. if n:
  604. print(f" {d:<10} {n}")
  605. print(f" 待处理新增 {len(pend_new)} · 待处理删除 {len(pend_gone)} · "
  606. f"待重建 {nreb} · 陈旧页 {len(stale)}")
  607. print("\n【下一步】")
  608. todo = []
  609. if pend_new:
  610. todo.append(f"有 {len(pend_new)} 个新源未入库 → kb-sync")
  611. if pend_gone:
  612. todo.append(f"有 {len(pend_gone)} 个源已消失 → kb-sync 的 retract 分支")
  613. if nreb:
  614. todo.append(f"有 {nreb} 页待重建 → 读 _meta/rebuild_queue.jsonl")
  615. if stale:
  616. todo.append(f"有 {len(stale)} 页被标 stale → 处理完记得清标记")
  617. if not pages:
  618. todo.append("库还是空的 → 把资料丢进 00-原材料/,然后说「同步知识库」")
  619. if not todo:
  620. todo.append("库是干净的 → 可以说「做一门课」或「查 XX」")
  621. for t in todo:
  622. print(f" · {t}")
  623. print("=" * 60)
  624. # ---------------------------------------------------------------- pull
  625. def git(*args):
  626. """在仓库根跑 git。Windows 下 git 输出可能是 GBK,两种都试。"""
  627. r = subprocess.run(["git", *args], cwd=str(ROOT), capture_output=True,
  628. text=True, encoding="utf-8", errors="replace")
  629. if r.returncode != 0 and r.stderr and "codec" in r.stderr.lower():
  630. r = subprocess.run(["git", *args], cwd=str(ROOT), capture_output=True,
  631. text=True, encoding="gbk", errors="replace")
  632. return r
  633. def cmd_pull(args):
  634. """远端优先:先拉最新,再告诉你有多少新料要整理。
  635. 设计取向:这个库是要写入的,所以不能像只读知识库那样"拉临时副本用完就扔"。
  636. 正确做法是本地留全量,每次开工先 pull —— pull 只传增量,比全量下载快得多。
  637. """
  638. if git("rev-parse", "--is-inside-work-tree").returncode != 0:
  639. print("[ERR] 这里不是 git 仓库,无法从远端拉取")
  640. print(" 如果是新建的库,先 git init 并配置 origin")
  641. return 1
  642. remotes = git("remote").stdout.split()
  643. if not remotes:
  644. print("[!] 没有配置远端,当前只有本地版本控制")
  645. print(" 配置:git remote add origin <你的本地 git 服务地址>")
  646. print(" 然后照常跑 kb.py scan")
  647. return 0
  648. remote = args.remote or remotes[0]
  649. branch = git("rev-parse", "--abbrev-ref", "HEAD").stdout.strip() or "main"
  650. # 工作区不干净就别拉,免得跟 rebase 打架
  651. dirty = [x for x in git("status", "--porcelain").stdout.splitlines() if x.strip()]
  652. if dirty and not args.force:
  653. print(f"[!] 工作区有 {len(dirty)} 处未提交改动,先处理再拉:")
  654. for d in dirty[:8]:
  655. print(f" {d}")
  656. if len(dirty) > 8:
  657. print(f" ... 还有 {len(dirty)-8} 处")
  658. print("\n 提交:git add -A && git commit -m \"sync: ...\"")
  659. print(" 或暂存:git stash")
  660. print(f" 或强制拉(会 rebase 上去):kb.py pull --force")
  661. return 1
  662. before = git("rev-parse", "HEAD").stdout.strip()
  663. print(f"从 {remote}/{branch} 拉取…")
  664. r = git("pull", "--rebase", remote, branch)
  665. out = (r.stdout + r.stderr).strip()
  666. if r.returncode != 0:
  667. print("[ERR] 拉取失败:")
  668. for line in out.splitlines()[:12]:
  669. print(" ", line)
  670. if "conflict" in out.lower() or "CONFLICT" in out:
  671. print("\n 有冲突。解法见 GIT.md「冲突怎么解」:")
  672. print(" manifest.json → git checkout --ours 后跑 kb.py repair")
  673. print(" */_index.md → git checkout --theirs 后跑 kb.py graph")
  674. return 1
  675. after = git("rev-parse", "HEAD").stdout.strip()
  676. if before == after:
  677. print("已是最新,没有新提交。")
  678. return cmd_scan(argparse.Namespace(json=False))
  679. # 报远端这次带来了什么
  680. diff = git("diff", "--name-status", "-M", f"{before}..{after}", "--", "00-原材料/").stdout
  681. add, mod, dele, mov = [], [], [], []
  682. for line in diff.splitlines():
  683. parts = line.split("\t")
  684. if len(parts) < 2:
  685. continue
  686. st, path = parts[0], parts[-1]
  687. if st.startswith("A"):
  688. add.append(path)
  689. elif st.startswith("M"):
  690. mod.append(path)
  691. elif st.startswith("D"):
  692. dele.append(path)
  693. elif st.startswith("R"):
  694. mov.append((parts[1], parts[2]))
  695. n_commits = git("rev-list", "--count", f"{before}..{after}").stdout.strip()
  696. print(f"拉到 {n_commits} 个新提交。原材料变化:"
  697. f"新增 {len(add)} · 修改 {len(mod)} · 删除 {len(dele)} · 移动 {len(mov)}")
  698. for x in add:
  699. print(f" [NEW] {x}")
  700. for x in mod:
  701. print(f" [CHANGED] {x}")
  702. for x in dele:
  703. print(f" [DELETED] {x}")
  704. for a, b in mov:
  705. print(f" [MOVED] {a} -> {b}")
  706. if any(x.lower().endswith((".pdf", ".mp4", ".mov", ".zip")) for x in add):
  707. print("\n 提示:新增了可能很大的文件。如果仓库用了 git-lfs,")
  708. print(" 确认 lfs 对象已拉下来(git lfs pull),否则整理时读不到内容。")
  709. # 页面上可能也带了变化(别人整理的结果)
  710. page_diff = git("diff", "--stat", f"{before}..{after}", "--", "10-知识库/").stdout
  711. if page_diff.strip():
  712. print(f"\n 知识库页面也有更新:")
  713. for line in page_diff.strip().splitlines()[-6:]:
  714. print(f" {line.strip()}")
  715. print("\n 下一步:让模型按 kb-sync 的流程处理上面这些变化。")
  716. return 0
  717. # ---------------------------------------------------------------- stash
  718. SOURCE_LABEL = {
  719. "gzh": "微信公众号", "ima": "IMA 知识库", "web": "网页",
  720. "feishu": "飞书文档", "yuque": "语雀", "notion": "Notion",
  721. "pdf": "PDF 文件", "doc": "文档", "video": "视频转写",
  722. "audio": "音频转写", "chat": "聊天记录", "other": "其他",
  723. }
  724. # 外部来源默认证据等级的建议(真正定级由 kb-ingest 按内容判断)
  725. SOURCE_HINT = {
  726. "gzh": "公众号属二手解读,默认 C 级。文章里若有可核验的一手数据且标了出处,可升 B",
  727. "ima": "IMA 里的笔记是别人的加工品,默认 C 级。笔记里给出原始出处链接才可升 B",
  728. "web": "按发布方定:官方一手渠道可到 A,媒体转述最多 B",
  729. "feishu": "内部文档:会议纪要/访谈记录属一手,B 级;总结稿属二手,C 级",
  730. "yuque": "同内部文档,按是否一手记录判定",
  731. "notion": "同内部文档,按是否一手记录判定",
  732. "video": "转写文本,按说话人身份判定;讲师经验 C 级,客户原话 B 级",
  733. "audio": "转写文本,按说话人身份判定",
  734. "chat": "聊天记录属一手,但碎片化,B 级",
  735. "other": "按内容性质自行判定",
  736. }
  737. def cmd_stash(args):
  738. """把外部抓来的内容规范落盘到原材料目录。
  739. 外部来源(公众号、IMA、网页)不能直接进知识库 —— 页面回滚靠 sources 里的
  740. 相对路径,内容必须先是磁盘上的一个文件。这个命令负责把「抓来的东西」变成
  741. 「一个带来源信息的原材料文件」。
  742. """
  743. body = ""
  744. if args.body_file:
  745. bp = Path(args.body_file)
  746. if not bp.exists():
  747. print(f"[ERR] 正文文件不存在:{bp}")
  748. return 1
  749. body = bp.read_text(encoding="utf-8", errors="replace")
  750. elif not sys.stdin.isatty():
  751. body = sys.stdin.read()
  752. body = body.strip()
  753. if not body:
  754. print("[ERR] 没有正文。用 --body-file <文件> 或通过管道传入:")
  755. print(" cat article.md | python kb.py stash --title \"...\" --type gzh")
  756. return 1
  757. title = (args.title or "").strip()
  758. if not title: # 没给标题就从正文第一个 H1 里取
  759. m = re.search(r"^#\s+(.+)$", body, re.M)
  760. title = m.group(1).strip() if m else "未命名"
  761. # 正文开头已有同名 H1 就去掉,避免重复
  762. head = f"# {title}"
  763. if body.startswith(head):
  764. rest = body[len(head):]
  765. if rest.startswith("\n") or not rest.strip():
  766. body = rest.lstrip("\n").strip()
  767. date = args.date or datetime.now(CST).strftime("%Y-%m-%d")
  768. typ = args.type
  769. if typ not in SOURCE_LABEL:
  770. print(f"[!] 未知类型 {typ},按 other 处理。可选:{', '.join(SOURCE_LABEL)}")
  771. typ = "other"
  772. dest = RAW / (args.dest or "_inbox")
  773. dest.mkdir(parents=True, exist_ok=True)
  774. slug = re.sub(r'[\\/:*?"<>|、,。!?\s]+', "-", title).strip("-")[:48] or "untitled"
  775. p = dest / f"{date}-{typ}-{slug}.md"
  776. n = 2
  777. while p.exists():
  778. p = dest / f"{date}-{typ}-{slug}-{n}.md"
  779. n += 1
  780. meta = [f"<!-- 来源: {SOURCE_LABEL[typ]}{'「' + args.author + '」' if args.author else ''} -->"]
  781. if args.author:
  782. meta.append(f"<!-- 作者: {args.author} -->")
  783. if args.url:
  784. meta.append(f"<!-- 原链接: {args.url} -->")
  785. if args.archive:
  786. meta.append(f"<!-- 存档: {args.archive} -->")
  787. meta.append(f"<!-- 抓取时间: {datetime.now(CST).strftime('%Y-%m-%d %H:%M')} -->")
  788. meta.append(f"<!-- 证据提示: {SOURCE_HINT.get(typ, SOURCE_HINT['other'])} -->")
  789. p.write_text("\n".join(meta) + f"\n\n# {title}\n\n{body}\n", encoding="utf-8")
  790. rel = p.relative_to(RAW).as_posix()
  791. print(f"[OK] 已落盘:{rel}({len(body)} 字)")
  792. if not args.url:
  793. print(" ⚠️ 没记原链接。将来引用这句话时,无法回溯到原文出处。")
  794. if typ in ("gzh", "web") and not args.archive:
  795. print(f" ⚠️ 没做网页存档。公众号和网页会失效,建议先存一份:")
  796. print(f" https://web.archive.org/save/{args.url or '<原链接>'}")
  797. print("\n 下一步:跑一次同步把它整理进知识库(说「同步知识库」即可)")
  798. return 0
  799. # ---------------------------------------------------------------- repair
  800. def cmd_repair(args):
  801. """从磁盘现状 + 页面 frontmatter 反推 manifest。
  802. 什么时候用:
  803. 1. git 合并把 manifest.json 搞冲突了
  804. 2. manifest 被误删、或从别处只拷了页面没拷 _meta
  805. 3. 换机器接手
  806. 能恢复什么:每个源的指纹、它派生了哪些页、已整理/未整理状态。
  807. 恢复不了什么:整理的具体时间(只能留空)、删除过的源记录(tombstone 会丢)。
  808. """
  809. cur = collect_raw()
  810. derived = {}
  811. # 活跃页 + 归档页都算 —— 归档页的 sources 仍然说明"这个源整理过"
  812. scan_roots = [WIKI / d for d in DIRS]
  813. if ARCHIVE.exists():
  814. scan_roots.append(ARCHIVE)
  815. for root in scan_roots:
  816. if not root.exists():
  817. continue
  818. for p in sorted(root.rglob("*.md")):
  819. if p.name.startswith("_"):
  820. continue
  821. fm, _ = read_page(p)
  822. for s in (fm.get("sources") or []):
  823. near = p if p.parent != ARCHIVE else ARCHIVE / p.name
  824. derived.setdefault(str(s), []).append(rel(near, WIKI))
  825. old = load_json(MANIFEST, manifest_skeleton())
  826. files, orphan_disk, orphan_pages = {}, [], []
  827. for r, meta in cur.items():
  828. ps = sorted(set(derived.pop(r, [])))
  829. prev = old.get("files", {}).get(r, {})
  830. files[r] = {
  831. "size": meta["size"], "mtime_ns": meta["mtime_ns"],
  832. "sha256": meta["sha256"], "id": prev.get("id") or short_id(r),
  833. "status": "ingested" if ps else "pending",
  834. "ingested_at": prev.get("ingested_at"),
  835. "pages": ps,
  836. }
  837. if not ps:
  838. orphan_disk.append(r)
  839. # 页面引用了磁盘上不存在的源
  840. for s, ps in derived.items():
  841. orphan_pages.extend(ps)
  842. man = {"version": 2, "root": "00-原材料", "updated_at": now(),
  843. "files": files, "tombstones": old.get("tombstones", [])}
  844. dump_json(MANIFEST, man)
  845. log_human("repair", f"重建 manifest:{len(files)} 个源",
  846. f"- 已整理 {len(files)-len(orphan_disk)} 个,未整理 {len(orphan_disk)} 个")
  847. print(f"[OK] manifest 已重建:{len(files)} 个源")
  848. print(f" 已整理 {len(files)-len(orphan_disk)} · 未整理 {len(orphan_disk)}")
  849. if orphan_disk:
  850. print(f"\n 磁盘上有、但没派生任何页(下次同步会当新的处理):")
  851. for x in orphan_disk[:10]:
  852. print(f" · {x}")
  853. if len(orphan_disk) > 10:
  854. print(f" ... 还有 {len(orphan_disk)-10} 个")
  855. if orphan_pages:
  856. print(f"\n ⚠️ 有 {len(set(orphan_pages))} 个页面引用了磁盘上已不存在的源")
  857. print(f" → 跑 kb.py lint 看 DANGLING_SOURCE,再决定恢复文件还是 retract")
  858. # ---------------------------------------------------------------- check
  859. def cmd_check(args):
  860. """聚合器:结构检查(本文件)+ 内容检查(check-content.py)。
  861. 两个检查器各自独立可跑,这里是给"提交前一次过"用的。"""
  862. cmd_lint(argparse.Namespace(json=False))
  863. print("\n" + "-" * 56)
  864. script = ROOT / "90-工具" / "check-content.py"
  865. if not script.exists():
  866. print("(未找到 check-content.py,跳过内容检查)")
  867. return 0
  868. r = subprocess.run([sys.executable, str(script)],
  869. capture_output=True, text=True,
  870. encoding="utf-8", errors="replace")
  871. print(r.stdout.strip() or r.stderr.strip()[:800])
  872. return 0 if r.returncode == 0 else 1
  873. # ---------------------------------------------------------------- main
  874. def main():
  875. ap = argparse.ArgumentParser(prog="kb", description="做课系统知识库工具链")
  876. sub = ap.add_subparsers(dest="cmd")
  877. sub.add_parser("status", help="总览")
  878. sub.add_parser("guide", help="给 AI 的自描述指南")
  879. s = sub.add_parser("scan", help="扫描原料目录,输出差异")
  880. s.add_argument("--json", action="store_true")
  881. m = sub.add_parser("mark", help="登记某源已整理")
  882. m.add_argument("path")
  883. m.add_argument("--pages", default="")
  884. r = sub.add_parser("retract", help="源删除后回滚")
  885. r.add_argument("path")
  886. mv = sub.add_parser("move", help="登记源文件移动")
  887. mv.add_argument("old")
  888. mv.add_argument("new")
  889. sub.add_parser("graph", help="重建关系图与索引")
  890. l = sub.add_parser("lint", help="结构健康检查")
  891. l.add_argument("--json", action="store_true")
  892. sub.add_parser("check", help="全量检查:结构 + 内容")
  893. sub.add_parser("repair", help="从磁盘和页面反推 manifest(git 冲突后用)")
  894. st = sub.add_parser("stash", help="把外部抓来的内容规范落盘到原材料目录")
  895. st.add_argument("--title", default="")
  896. st.add_argument("--url", default="")
  897. st.add_argument("--type", default="other",
  898. help="gzh/ima/web/feishu/yuque/notion/pdf/doc/video/audio/chat/other")
  899. st.add_argument("--author", default="")
  900. st.add_argument("--archive", default="", help="网页存档链接")
  901. st.add_argument("--body-file", dest="body_file", default=None)
  902. st.add_argument("--date", default=None)
  903. st.add_argument("--dest", default="_inbox", help="落到哪个子目录,默认 _inbox")
  904. pl = sub.add_parser("pull", help="从远端拉最新并报告原材料变化")
  905. pl.add_argument("--remote", default=None)
  906. pl.add_argument("--force", action="store_true", help="工作区有改动也拉")
  907. args = ap.parse_args()
  908. fn = {"status": cmd_status, "guide": cmd_guide, "scan": cmd_scan, "mark": cmd_mark,
  909. "retract": cmd_retract, "move": cmd_move, "graph": cmd_graph,
  910. "lint": cmd_lint, "check": cmd_check, "repair": cmd_repair,
  911. "pull": cmd_pull, "stash": cmd_stash}.get(args.cmd)
  912. if not fn:
  913. ap.print_help()
  914. return 0
  915. return fn(args) or 0
  916. if __name__ == "__main__":
  917. sys.exit(main())