nx-kv 0.48.2 → 0.50.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (309) hide show
  1. package/CHANGELOG.md +85 -0
  2. package/README.md +103 -216
  3. package/assets/groups.json +1 -1
  4. package/assets/kv-get/SKILL.md +1 -1
  5. package/assets/kv-get/references/localget.md +2 -2
  6. package/assets/kv-get/references/timegoal-workflow.md +3 -3
  7. package/assets/kv-get/references/timegoal.md +1 -1
  8. package/assets/kv-get/references/wait-workflow.md +2 -3
  9. package/assets/kv-get/references/waitloop.md +3 -3
  10. package/assets/kv-repo/SKILL.md +1 -1
  11. package/assets/kv-submit/SKILL.md +3 -3
  12. package/assets/nx-kv/SKILL.md +111 -76
  13. package/assets/nx-kv/references/agent-workflow.md +58 -47
  14. package/assets/nx-kv/references/localtask-commands.md +179 -0
  15. package/package.json +3 -4
  16. package/src/core/attachmentdb/bodies.ts +193 -0
  17. package/src/core/attachmentdb/docs.ts +158 -33
  18. package/src/core/attachmentdb/ids.ts +27 -1
  19. package/src/core/attachmentdb/mdfile.ts +249 -0
  20. package/src/core/attachmentdb/query.ts +14 -1
  21. package/src/core/attachmentdb/resources.ts +28 -2
  22. package/src/core/attachmentdb.ts +27 -0
  23. package/src/core/config.ts +13 -133
  24. package/src/core/db/bootstrap.ts +5 -5
  25. package/src/core/db/import-json/refs.ts +27 -13
  26. package/src/core/db/migrations/index.ts +9 -0
  27. package/src/core/db/migrations/v2-data.ts +4 -4
  28. package/src/core/db/migrations/v5.sql +100 -0
  29. package/src/core/db/migrations/v6-data.ts +261 -0
  30. package/src/core/db/migrations/v6.sql +67 -0
  31. package/src/core/db/migrations/v7.sql +8 -0
  32. package/src/core/db/seed-templates.ts +3 -1
  33. package/src/core/db/version.ts +12 -1
  34. package/src/core/filekind.ts +95 -0
  35. package/src/core/instruction-template.ts +6 -4
  36. package/src/core/localatt.ts +3 -6
  37. package/src/core/paths.ts +11 -1
  38. package/src/index.ts +0 -2
  39. package/src/modules/docs/actions/_manifest.generated.ts +18 -0
  40. package/src/modules/docs/actions/doc-attach.ts +2 -1
  41. package/src/modules/docs/actions/doc-link-file.ts +28 -0
  42. package/src/modules/docs/actions/doc-mdfile.ts +28 -0
  43. package/src/modules/docs/actions/doc-mdfiles.ts +29 -0
  44. package/src/modules/docs/actions/doc-ref.ts +36 -0
  45. package/src/modules/docs/actions/doc-unbind.ts +34 -0
  46. package/src/modules/docs/actions/doc-update.ts +3 -2
  47. package/src/modules/docs/actions/docs-bind.ts +1 -1
  48. package/src/modules/docs/actions/docs-bindings.ts +108 -0
  49. package/src/modules/docs/actions/docs-notebind.ts +1 -1
  50. package/src/modules/docs/attach.ts +16 -3
  51. package/src/modules/docs/bound-docs.ts +74 -0
  52. package/src/modules/docs/docref.ts +18 -3
  53. package/src/modules/docs/knowledge-md.ts +51 -0
  54. package/src/modules/docs/knowledge.ts +112 -112
  55. package/src/modules/docs/links.ts +7 -1
  56. package/src/modules/docs/mdfile.ts +24 -0
  57. package/src/modules/docs/paths.ts +6 -9
  58. package/src/modules/docs/ref-sync.ts +166 -0
  59. package/src/modules/docs/refs.ts +16 -5
  60. package/src/modules/docs/render.ts +33 -15
  61. package/src/modules/docs/seed.ts +1 -1
  62. package/src/modules/docs/service.ts +300 -30
  63. package/src/modules/docs/store.ts +147 -74
  64. package/src/modules/file/actions/_manifest.generated.ts +0 -9
  65. package/src/modules/file/actions/file-content.ts +3 -4
  66. package/src/modules/file/actions/file-get.ts +2 -3
  67. package/src/modules/file/actions/file-upload.ts +1 -4
  68. package/src/modules/file/service.ts +52 -253
  69. package/src/modules/localtask/action-render.ts +5 -5
  70. package/src/modules/localtask/actions/_manifest.generated.ts +0 -6
  71. package/src/modules/localtask/actions/localtask-attach.ts +4 -2
  72. package/src/modules/localtask/actions/localtask-cancelMany.ts +1 -1
  73. package/src/modules/localtask/actions/localtask-closeMany.ts +4 -3
  74. package/src/modules/localtask/actions/localtask-get.ts +1 -1
  75. package/src/modules/localtask/repo/tasks.ts +12 -6
  76. package/src/modules/localtask/service.ts +0 -2
  77. package/src/modules/localtask/task-ops.ts +4 -4
  78. package/src/modules/localtask/task-remove.ts +33 -16
  79. package/src/modules/localtask/task-templates.ts +2 -3
  80. package/src/modules/localtask/task-view.ts +2 -2
  81. package/src/modules/repos/actions/_manifest.generated.ts +0 -3
  82. package/src/modules/repos/actions/repo-update.ts +2 -4
  83. package/src/modules/repos/cli/render.ts +0 -15
  84. package/src/modules/repos/service/bind-topics.ts +8 -18
  85. package/src/modules/repos/service.ts +1 -1
  86. package/src/modules/repos/transfer/export.ts +4 -1
  87. package/src/modules/setting/actions/setting-set.ts +0 -2
  88. package/src/modules/setting/service.ts +6 -25
  89. package/src/modules/system/actions/bootstrap.ts +2 -31
  90. package/src/modules/system/actions/health.ts +4 -31
  91. package/src/modules/system/routes.ts +1 -1
  92. package/src/modules/tasktool/attach-resource.ts +68 -42
  93. package/src/modules/tasktool/attach-template.ts +73 -54
  94. package/src/modules/tasktool/attach.ts +98 -71
  95. package/src/modules/tasktool/delivery-template.ts +14 -4
  96. package/src/modules/tasktool/doc-inline.ts +63 -29
  97. package/src/modules/tasktool/get.ts +1 -1
  98. package/src/modules/tasktool/groupctx.ts +19 -11
  99. package/src/modules/tasktool/plugins/waitgoal-config.ts +24 -55
  100. package/src/modules/tasktool/plugins/waitgoal.ts +0 -2
  101. package/src/modules/tasktool/preview.ts +2 -3
  102. package/src/modules/tasktool/wait.ts +6 -9
  103. package/src/runtime/cli.ts +3 -15
  104. package/src/runtime/container.ts +1 -5
  105. package/src/runtime/help.ts +1 -4
  106. package/src/runtime/registry.ts +0 -6
  107. package/src/shared/prompt-blocks.ts +37 -33
  108. package/src/shared/task-status.ts +3 -3
  109. package/src/web/frontend/App.tsx +3 -3
  110. package/src/web/frontend/api/cli-tree.json +115 -608
  111. package/src/web/frontend/api/client.generated.ts +88 -401
  112. package/src/web/frontend/components/Introduction/index.tsx +26 -14
  113. package/src/web/frontend/components/Introduction/rail.tsx +28 -3
  114. package/src/web/frontend/components/Introduction/style.css +3 -33
  115. package/src/web/frontend/components/Introduction/use-intro.ts +0 -80
  116. package/src/web/frontend/components/{cloud → attach}/upload.ts +7 -20
  117. package/src/web/frontend/components/bindings/bind-tags.tsx +183 -0
  118. package/src/web/frontend/components/doc/doc-head.tsx +29 -26
  119. package/src/web/frontend/components/icons.tsx +0 -1
  120. package/src/web/frontend/components/layout/crumbs.ts +0 -2
  121. package/src/web/frontend/components/md/ack-rail.tsx +10 -142
  122. package/src/web/frontend/components/md/attach-area.tsx +64 -9
  123. package/src/web/frontend/components/md/attach-common.ts +8 -3
  124. package/src/web/frontend/components/md/attach-modal.tsx +22 -17
  125. package/src/web/frontend/components/md/attach-picker.tsx +2 -2
  126. package/src/web/frontend/components/md/attachments.ts +2 -3
  127. package/src/web/frontend/components/popup/dismiss.ts +1 -1
  128. package/src/web/frontend/components/repos/get-loop-modal.tsx +20 -207
  129. package/src/web/frontend/components/sidebar/index.tsx +32 -6
  130. package/src/web/frontend/components/sidebar/model.ts +71 -0
  131. package/src/web/frontend/components/sidebar/task-children.tsx +41 -20
  132. package/src/web/frontend/components/status-tag/model.ts +1 -1
  133. package/src/web/frontend/components/task-panel/index.tsx +4 -1
  134. package/src/web/frontend/components/task-panel/task-row.tsx +2 -2
  135. package/src/web/frontend/components/tool-acts/modules.ts +0 -3
  136. package/src/web/frontend/components/use-rail-drag.ts +177 -0
  137. package/src/web/frontend/modules/board/ack-rail-pos.ts +17 -7
  138. package/src/web/frontend/modules/board/board-view.ts +2 -2
  139. package/src/web/frontend/modules/board/task-search.ts +81 -0
  140. package/src/web/frontend/modules/md/attach-mention.ts +6 -1
  141. package/src/web/frontend/modules/md/doc-bus.ts +22 -0
  142. package/src/web/frontend/modules/md/md-render.ts +9 -2
  143. package/src/web/frontend/modules/md/styles/chrome.css +25 -0
  144. package/src/web/frontend/modules/md/styles/docs.css +32 -21
  145. package/src/web/frontend/modules/md/styles/shell.css +1 -10
  146. package/src/web/frontend/page/dir/api.ts +7 -0
  147. package/src/web/frontend/page/dir/index.tsx +24 -58
  148. package/src/web/frontend/page/dir/landing.tsx +85 -72
  149. package/src/web/frontend/page/dir/task-actions.ts +17 -5
  150. package/src/web/frontend/page/dir/task-dock-panel.tsx +87 -2
  151. package/src/web/frontend/page/dir/task-dock.ts +21 -10
  152. package/src/web/frontend/page/dir/task-goal.ts +2 -2
  153. package/src/web/frontend/page/dir/task-status-bar.tsx +14 -1
  154. package/src/web/frontend/page/dir/task.tsx +5 -3
  155. package/src/web/frontend/page/doc/api.ts +71 -5
  156. package/src/web/frontend/page/doc/bindings.tsx +240 -0
  157. package/src/web/frontend/page/doc/doc-dock.tsx +159 -0
  158. package/src/web/frontend/page/doc/index.tsx +131 -212
  159. package/src/web/frontend/page/doc/use-wiki-nav.ts +101 -0
  160. package/src/web/frontend/page/group/index.tsx +61 -87
  161. package/src/web/frontend/page/overview/index.tsx +1 -2
  162. package/src/web/frontend/page/refs/api.ts +60 -8
  163. package/src/web/frontend/page/refs/index.tsx +177 -90
  164. package/src/web/frontend/page/refs/style.css +45 -2
  165. package/src/web/frontend/page/repos/doc-search-modal.tsx +7 -0
  166. package/src/web/frontend/page/repos/index.tsx +21 -60
  167. package/src/web/frontend/page/repos/repo-table.tsx +85 -77
  168. package/src/web/frontend/page/settings/index.tsx +3 -27
  169. package/src/web/frontend/page/settings/prompt-template-params.ts +18 -28
  170. package/src/web/frontend/panels.tsx +52 -6
  171. package/src/web/frontend/registry.ts +1 -2
  172. package/src/web/frontend/routes/index.ts +1 -2
  173. package/src/web/frontend/routes/static.ts +2 -3
  174. package/src/web/frontend/store.tsx +1 -8
  175. package/src/web/frontend/style.css +1 -0
  176. package/src/web/frontend/styles/base.css +0 -17
  177. package/src/web/frontend/styles/bindings.css +75 -0
  178. package/src/web/frontend/styles/buttons-cards.css +0 -72
  179. package/src/web/frontend/styles/repos-admin.css +7 -5
  180. package/src/web/frontend/styles-v2/dir.css +1 -9
  181. package/src/web/frontend/styles-v2/shell.css +0 -2
  182. package/src/web/frontend/styles-v2/sidebar.css +12 -0
  183. package/src/web/frontend/styles-v2/views.css +22 -102
  184. package/src/web/frontend/workspace/areas.ts +0 -2
  185. package/src/web/public/assets/Introduction-CohW6Xit.js +6 -0
  186. package/src/web/public/assets/Introduction-Iuxssa-b.css +1 -0
  187. package/src/web/public/assets/app-context-BgpTewyA.js +1 -0
  188. package/src/web/public/assets/{bar-B52NTf_v.js → bar-C2mfoqmj.js} +1 -1
  189. package/src/web/public/assets/{board-view-DABoVkaA.js → board-view-aAIEbSQi.js} +1 -1
  190. package/src/web/public/assets/button-xyR2UROd.js +1 -0
  191. package/src/web/public/assets/composer-launch-eTktHT_f.js +1 -0
  192. package/src/web/public/assets/{composer-panel-DpiU2h50.js → composer-panel-C0OOIXxz.js} +1 -1
  193. package/src/web/public/assets/dag-Gm4RcvEg.js +1 -0
  194. package/src/web/public/assets/dir-PjuMRbWo.js +3 -0
  195. package/src/web/public/assets/group-D94y_Lq6.js +1 -0
  196. package/src/web/public/assets/index-CBJKsJU3.css +1 -0
  197. package/src/web/public/assets/index-Crxdmsux.js +2 -0
  198. package/src/web/public/assets/md-history-CU_1I6ht.js +39 -0
  199. package/src/web/public/assets/{md-parse-b6GY5eBl.js → md-parse-CLyUaUto.js} +1 -1
  200. package/src/web/public/assets/{member-picker-B5I7KuOR.js → member-picker-XT12VRe6.js} +1 -1
  201. package/src/web/public/assets/model-CWJ8_2dN.js +2 -0
  202. package/src/web/public/assets/{overview-B3kHI66i.js → overview-BQm8BLCG.js} +1 -1
  203. package/src/web/public/assets/{progress-CdhFz0Lz.js → progress-CNam3Wuj.js} +2 -2
  204. package/src/web/public/assets/refs-C5idWnUj.css +1 -0
  205. package/src/web/public/assets/refs-ClETk1cj.js +2 -0
  206. package/src/web/public/assets/repos-BioGejZo.js +14 -0
  207. package/src/web/public/assets/route-crumb-Fc68LcQ4.js +1 -0
  208. package/src/web/public/assets/settings-BIzumXLD.js +8 -0
  209. package/src/web/public/assets/status-tag-CBFf_-ae.js +1 -0
  210. package/src/web/public/assets/task-CGmO3I2y.js +11 -0
  211. package/src/web/public/assets/task-row-DRBa11tf.js +1 -0
  212. package/src/web/public/assets/{task-status-DSrx0i66.js → task-status-BHF-y9Aq.js} +2 -1
  213. package/src/web/public/assets/{task-utils-_JAWRAz6.js → task-utils-DjODjVdc.js} +2 -2
  214. package/src/web/public/assets/{templates-CbRyW3PJ.js → templates-j8EzXHXf.js} +1 -1
  215. package/src/web/public/assets/ui-Y0aL-bGk.js +1 -0
  216. package/src/web/public/assets/use-rail-drag-DNfz4Gcx.js +1 -0
  217. package/src/web/public/assets/{workspace-4tw7trV7.js → workspace-D_G-B3TX.js} +4 -4
  218. package/src/web/public/index.html +9 -9
  219. package/assets/nx-kv/references/todo-commands.md +0 -269
  220. package/src/core/attachstore.ts +0 -360
  221. package/src/core/fileapi.ts +0 -267
  222. package/src/core/kvapi.ts +0 -237
  223. package/src/modules/auth/actions/_manifest.generated.ts +0 -26
  224. package/src/modules/auth/actions/auth-login.ts +0 -31
  225. package/src/modules/auth/actions/auth-logout.ts +0 -33
  226. package/src/modules/auth/actions/auth-me.ts +0 -33
  227. package/src/modules/auth/actions/auth-status.ts +0 -41
  228. package/src/modules/auth/index.ts +0 -24
  229. package/src/modules/auth/support.ts +0 -28
  230. package/src/modules/file/actions/file-list.ts +0 -31
  231. package/src/modules/file/actions/file-remove.ts +0 -31
  232. package/src/modules/file/actions/file-sync.ts +0 -33
  233. package/src/modules/localtask/actions/localtask-importRemote.ts +0 -51
  234. package/src/modules/localtask/actions/task-move.ts +0 -67
  235. package/src/modules/localtask/move-prims.ts +0 -170
  236. package/src/modules/localtask/move.ts +0 -174
  237. package/src/modules/localtask/remote-import.ts +0 -159
  238. package/src/modules/repos/actions/repo-ref-backfill.ts +0 -33
  239. package/src/modules/repos/ref-backfill.ts +0 -219
  240. package/src/modules/todo/actions/_manifest.generated.ts +0 -89
  241. package/src/modules/todo/actions/group-add.ts +0 -34
  242. package/src/modules/todo/actions/group-current.ts +0 -36
  243. package/src/modules/todo/actions/group-get.ts +0 -43
  244. package/src/modules/todo/actions/group-list.ts +0 -41
  245. package/src/modules/todo/actions/group-members.ts +0 -41
  246. package/src/modules/todo/actions/group-remove.ts +0 -44
  247. package/src/modules/todo/actions/group-update.ts +0 -37
  248. package/src/modules/todo/actions/group-use.ts +0 -43
  249. package/src/modules/todo/actions/prompt-get.ts +0 -31
  250. package/src/modules/todo/actions/prompt-remove.ts +0 -31
  251. package/src/modules/todo/actions/prompt-set.ts +0 -32
  252. package/src/modules/todo/actions/todo-add.ts +0 -34
  253. package/src/modules/todo/actions/todo-archive.ts +0 -33
  254. package/src/modules/todo/actions/todo-attach.ts +0 -37
  255. package/src/modules/todo/actions/todo-attachments.ts +0 -33
  256. package/src/modules/todo/actions/todo-done.ts +0 -33
  257. package/src/modules/todo/actions/todo-freeze.ts +0 -32
  258. package/src/modules/todo/actions/todo-get.ts +0 -33
  259. package/src/modules/todo/actions/todo-list.ts +0 -32
  260. package/src/modules/todo/actions/todo-remove.ts +0 -32
  261. package/src/modules/todo/actions/todo-unfreeze.ts +0 -34
  262. package/src/modules/todo/actions/todo-update.ts +0 -42
  263. package/src/modules/todo/actions/topic-add.ts +0 -31
  264. package/src/modules/todo/actions/topic-list.ts +0 -35
  265. package/src/modules/todo/actions/topic-remove.ts +0 -36
  266. package/src/modules/todo/attachments.ts +0 -194
  267. package/src/modules/todo/group.ts +0 -207
  268. package/src/modules/todo/index.ts +0 -26
  269. package/src/modules/todo/locate.ts +0 -103
  270. package/src/modules/todo/service.ts +0 -476
  271. package/src/modules/todo/store.ts +0 -176
  272. package/src/modules/todo/support.ts +0 -93
  273. package/src/web/frontend/components/Introduction/docs-modal.tsx +0 -338
  274. package/src/web/frontend/components/cloud/editor.tsx +0 -254
  275. package/src/web/frontend/components/cloud/group-popup.tsx +0 -198
  276. package/src/web/frontend/components/cloud/markdown.tsx +0 -60
  277. package/src/web/frontend/components/migrate/dialog.tsx +0 -120
  278. package/src/web/frontend/components/migrate/undo.tsx +0 -73
  279. package/src/web/frontend/components/migrate/use-move.ts +0 -137
  280. package/src/web/frontend/page/cloud/api.ts +0 -99
  281. package/src/web/frontend/page/cloud/index.tsx +0 -516
  282. package/src/web/frontend/page/dir/deleted-panel.tsx +0 -77
  283. package/src/web/public/assets/Introduction-BcPrmROW.css +0 -1
  284. package/src/web/public/assets/Introduction-Du_dycft.js +0 -6
  285. package/src/web/public/assets/app-context-BASCGAA-.js +0 -1
  286. package/src/web/public/assets/cloud-Droja_aT.js +0 -3
  287. package/src/web/public/assets/composer-launch-mK9Nr6mZ.js +0 -1
  288. package/src/web/public/assets/dag-Ca-wjIxQ.js +0 -1
  289. package/src/web/public/assets/deleted-panel-K9GgfR8t.js +0 -1
  290. package/src/web/public/assets/dir-DacRpcBt.js +0 -5
  291. package/src/web/public/assets/frontmatter-CLLxCmTv.js +0 -2
  292. package/src/web/public/assets/group-TPuU-QVc.js +0 -2
  293. package/src/web/public/assets/index-BkLkooKe.js +0 -2
  294. package/src/web/public/assets/index-DP3qBv1o.css +0 -1
  295. package/src/web/public/assets/md-history-0GBWw4qr.js +0 -39
  296. package/src/web/public/assets/model-Bn2fG6CC.js +0 -2
  297. package/src/web/public/assets/prompt-blocks-BovFd039.js +0 -8
  298. package/src/web/public/assets/refs-BDxfuH6R.js +0 -1
  299. package/src/web/public/assets/refs-CY4i7PoC.css +0 -1
  300. package/src/web/public/assets/repos-C8jRM7wS.js +0 -15
  301. package/src/web/public/assets/repos-ZPiaK93i.css +0 -1
  302. package/src/web/public/assets/route-crumb-QvahotvN.js +0 -1
  303. package/src/web/public/assets/settings-BBcdGLTc.js +0 -1
  304. package/src/web/public/assets/status-tag-BheHByek.js +0 -1
  305. package/src/web/public/assets/task-CUkQaLBZ.js +0 -11
  306. package/src/web/public/assets/tool-bar-BAmT-BI-.js +0 -1
  307. package/src/web/public/assets/upload-BcMhRsMn.js +0 -1
  308. /package/src/web/public/assets/{prompt-blocks-DA4lNJeC.css → button-DA4lNJeC.css} +0 -0
  309. /package/src/web/public/assets/{tool-bar-1h2zairf.css → use-rail-drag-1h2zairf.css} +0 -0
@@ -0,0 +1,74 @@
1
+ // 按归属读 ref(v7):列表 / 提示词模板的统一读模型。
2
+ //
3
+ // ref 是 DB 实体(refs + ref_bodies + ref_bindings),没有路径概念。
4
+ // 这里只按归属查:type=repo(单仓库)/ group(组)/ group_repo(组下的仓库)。
5
+ // 主篇(position=0)才带正文——提示词只内嵌 repo 主篇,其余只给引导。
6
+ import { getDb } from '../../core/db/index.js';
7
+ import { docTitleFromMd } from '../../shared/doc-title.js';
8
+
9
+ /** 一条带归属的文档(ref_bindings JOIN refs)。 */
10
+ export interface BoundDoc {
11
+ refId: string;
12
+ /** 短描述(v7):快速定位标签 */
13
+ desc: string;
14
+ /** 正文首行标题(没有正文时为空) */
15
+ title: string;
16
+ hasBody: boolean;
17
+ origin: string;
18
+ type: 'repo' | 'group' | 'group_repo';
19
+ repoKey: string;
20
+ groupKey: string | null;
21
+ position: number;
22
+ note: string;
23
+ /** 只有 repo 主篇(position=0)带正文;其余保持空串 */
24
+ body: string;
25
+ }
26
+
27
+ /**
28
+ * 按宿主查绑定文档。
29
+ * repoKey 给仓库 → 该仓库自己的文档(type=repo)
30
+ * groupKey 给组 → 该组的组级文档(type=group);同时给 repoKey 时,
31
+ * 再带上「这个仓库在该组的组内文档」(type=group_repo)
32
+ */
33
+ export function boundDocs({ repoKey = '', groupKey = '' }: { repoKey?: string; groupKey?: string } = {}): BoundDoc[] {
34
+ const repo = String(repoKey || '').trim();
35
+ const group = String(groupKey || '').trim();
36
+ if (!repo && !group) return [];
37
+ const rows: any[] = getDb().all(
38
+ `SELECT b.type, b.repo_key, b.group_key, b.position, b.note,
39
+ r.id AS ref_id, r."desc", r.origin,
40
+ CASE WHEN rb.ref_id IS NULL THEN 0 ELSE 1 END AS has_body,
41
+ rb.body
42
+ FROM ref_bindings b JOIN refs r ON r.id = b.ref_id
43
+ LEFT JOIN ref_bodies rb ON rb.ref_id = r.id
44
+ WHERE r.deleted_at IS NULL AND (
45
+ (:repo <> '' AND b.type = 'repo' AND b.repo_key = :repo)
46
+ OR (:group <> '' AND b.type = 'group' AND b.repo_key = :group)
47
+ OR (:repo <> '' AND :group <> '' AND b.type = 'group_repo' AND b.repo_key = :repo AND b.group_key = :group)
48
+ )
49
+ ORDER BY CASE b.type WHEN 'repo' THEN 0 WHEN 'group' THEN 1 ELSE 2 END,
50
+ b.position, b.rowid`,
51
+ { repo, group }
52
+ );
53
+ return rows.map((x: any) => {
54
+ const type = String(x.type) as BoundDoc['type'];
55
+ const position = Number(x.position) || 0;
56
+ const hasBody = Number(x.has_body) === 1 || String(x.origin) === 'external';
57
+ const rawBody = hasBody ? String(x.body ?? '') : '';
58
+ // 正文只随 repo 主篇返回(提示词只内嵌这一篇);其余只给标题 / desc。
59
+ const body = type === 'repo' && position === 0 ? rawBody : '';
60
+ return {
61
+ refId: String(x.ref_id),
62
+ desc: String(x.desc || ''),
63
+ title: rawBody ? docTitleFromMd(rawBody) : '',
64
+ hasBody,
65
+ origin: String(x.origin || ''),
66
+ type,
67
+ repoKey: String(x.repo_key || ''),
68
+ groupKey: x.group_key == null ? null : String(x.group_key),
69
+ position,
70
+ note: String(x.note || ''),
71
+ body,
72
+ };
73
+ });
74
+ }
@@ -29,15 +29,30 @@ export type DocRefWithMeta = DocRef;
29
29
  // ─── 自有 rel 守卫 ─────────────────────────────────────────────────────
30
30
 
31
31
  /**
32
- * 自有树内相对路径:assertSafeRelPath(拒绝对路径与 .. 段,返回归一化 POSIX)
33
- * + 必须以 .md 结尾。返回归一化 rel,调用方应使用返回值。
32
+ * 自有 rel:assertSafeRelPath(拒绝对路径与 .. 段,返回归一化 POSIX)
33
+ * + 必须是 .md 结尾,或 v7 的直接 ref id(`ref_<16hex>`,ref 是 DB 实体,
34
+ * 没有文件路径——rel 只作内部唯一键,不做文件名)。
35
+ * 返回归一化 rel,调用方应使用返回值。
34
36
  */
35
37
  export function assertOwnedRel(rel: unknown, label = '文档路径'): string {
36
38
  const norm = assertSafeRelPath(rel, { label });
39
+ if (/^ref_[0-9a-f]{16}$/i.test(norm)) return norm;
37
40
  if (!/\.md$/i.test(norm)) throw badInput(label + '必须是 .md 文件: ' + String(rel));
38
41
  return norm;
39
42
  }
40
43
 
44
+ /**
45
+ * 读路径用的 rel 校验:在自有 rel 的基础上,额外放行 v6 的壳行命名空间
46
+ * `legacy/<id>` / `external/<hash>`——它们不是磁盘路径,也不以 .md 结尾,
47
+ * 但必须能被 `docs get` 命中并返回 missing 语义(而不是抛「必须是 .md」)。
48
+ * 写入口仍然用 assertOwnedRel,避免把壳行写成正常文档。
49
+ */
50
+ export function assertDocRel(rel: unknown, label = '文档路径'): string {
51
+ const norm = assertSafeRelPath(rel, { label });
52
+ if (/^(legacy|external)\//.test(norm)) return norm;
53
+ return assertOwnedRel(norm, label);
54
+ }
55
+
41
56
  // ─── DocRef ────────────────────────────────────────────────────────────
42
57
 
43
58
  /** 严格版:形状非法抛 badInput(bind 入口用) */
@@ -105,5 +120,5 @@ export function parseDocId(docId: unknown): ParsedDocId {
105
120
  return { kind: 'external', path: p };
106
121
  }
107
122
  const rel = s.startsWith('rel:') ? s.slice(4).trim() : s;
108
- return { kind: 'own', rel: assertOwnedRel(rel) };
123
+ return { kind: 'own', rel: assertDocRel(rel) };
109
124
  }
@@ -0,0 +1,51 @@
1
+ // repo knowledge 的 markdown 渲染(`repo knowledge --md`)。
2
+ // 从 knowledge.ts 拆出:知识聚合与交付层读取分开,主文件保持在行数门禁内。
3
+ import { getDb } from '../../core/db/index.js';
4
+ import type { KnowledgeDoc, RepoKnowledgeResult, IntroSet } from './knowledge.js';
5
+
6
+ /** 文档集 → markdown 行(主篇正文 + 其余各篇的取用指令) */
7
+ export function introMdLines(set: IntroSet | null | undefined, fallback: KnowledgeDoc | null | undefined): string[] {
8
+ const out: string[] = [];
9
+ const main = (set && set.main) || (fallback && !fallback.missing ? fallback : null);
10
+ if (main && main.body) out.push(String(main.body).trim());
11
+ const extras = (set && set.extras) || [];
12
+ if (extras.length) {
13
+ out.push('', '其余介绍文档(可用命令读取):');
14
+ for (const d of extras) {
15
+ // ref 是 DB 实体:给 refId 让调用方按 id 取,不展示路径。
16
+ const refId = d.rel
17
+ ? String(getDb().get('SELECT id FROM refs WHERE rel = :rel AND deleted_at IS NULL', { rel: d.rel })?.id || '')
18
+ : '';
19
+ out.push(`- nx-kv docs get --ref ${refId || d.docId || ''}`);
20
+ }
21
+ }
22
+ return out;
23
+ }
24
+
25
+ /** 单篇 markdown:项目介绍 → 各组(介绍/组内描述/绑定文档)→ 参考仓库 */
26
+ export function renderRepoKnowledgeMd(k: RepoKnowledgeResult): string {
27
+ const lines: string[] = [];
28
+ const heading = (t: string) => (lines.length ? lines.push('', t) : lines.push(t));
29
+ heading(`# ${k.repo.name}`);
30
+ lines.push(...introMdLines(k.introSet, k.intro));
31
+ for (const b of k.boards) {
32
+ heading(`## 组:${b.name}`);
33
+ lines.push(...introMdLines(b.introSet, b.intro));
34
+ if (b.note && !b.note.missing) lines.push(b.note.body!.trim());
35
+ for (const d of b.docLinks) {
36
+ if (!d.missing && d.body) lines.push(d.body.trim());
37
+ }
38
+ for (const dir of b.externalDirs) {
39
+ lines.push('', `外部目录「${dir.path}」(${dir.files!.length} 个文档,按需读盘):`);
40
+ for (const f of dir.files!) lines.push(`- ${f}`);
41
+ }
42
+ }
43
+ if (k.refRepos.length) {
44
+ heading('## 参考仓库');
45
+ for (const r of k.refRepos) {
46
+ const src = r.direct && !r.groups.length ? '[直接绑定]' : r.direct ? '[组 + 直接绑定]' : '';
47
+ lines.push(`- ${r.name}${r.remote ? `(${r.remote})` : ''}${src}${r.note ? ` —— ${r.note.split('\n')[0] ?? ''}` : ''}`);
48
+ }
49
+ }
50
+ return lines.join('\n');
51
+ }
@@ -1,18 +1,18 @@
1
- // 复合知识查询:一个仓库的完整上下文,一次取齐。
1
+ // 复合知识查询:一个仓库的完整上下文,一次取齐(agent 领取任务后的供料口)。
2
+ // `repo knowledge --md` 给出项目介绍 + 组介绍 + 组内描述 + 组绑定文档 + 参考仓库文本;
3
+ // 自有文档正文来自库内 `ref_bodies`,不读盘。
2
4
  //
3
- // 这是「get 时封装上下文」的供料口:agent 领到某个仓库的任务后,
4
- // `nx-kv repo knowledge --md` 给出项目介绍 + 所属组介绍 + 组内描述 + 组绑定文档 +
5
- // 参考仓库的单篇文本——文档全是磁盘真实路径,agent 拿到就能直接读盘。
6
- //
7
- // 读取规则:
8
- // - 绑定优先(DocRef),缺省走约定路径;约定路径缺失时惰性播种(字符串字段非空)
9
- // - 外部目录绑定只列文件清单,不进全文(agent 自己读盘——目录全文没有边界)
10
- // - 单文件 256KB 截断:知识是给人/模型读的,不是备份通道
11
- import { join, sep } from 'node:path';
5
+ // 读取规则:绑定优先(DocRef),缺省走约定路径并惰性播种;外部目录只列清单;
6
+ // 单文件 256KB 截断:知识是给人/模型读的,不是备份通道。
7
+ import { sep } from 'node:path';
12
8
  import * as paths from './paths.js';
13
9
  import * as store from './store.js';
14
10
  import { ensureSeedForRel } from './seed.js';
15
11
  import { reposBridge } from './repos-bridge.js';
12
+ import { boundDocs } from './bound-docs.js';
13
+ import type { BoundDoc } from './bound-docs.js';
14
+ import { getDb } from '../../core/db/index.js';
15
+ import { renderRepoKnowledgeMd } from './knowledge-md.js';
16
16
  import type { DocRef } from './docref.js';
17
17
 
18
18
  const MAX_DOC_BYTES = 256 * 1024;
@@ -63,6 +63,15 @@ export interface RefRepoItem {
63
63
  direct: boolean;
64
64
  }
65
65
 
66
+ // ─── ref + 归属(v7):读模型在 bound-docs.ts ──────────────────────────
67
+ /** docId(rel:/ref:)→ refId;查不到返回空串(老数据回落用)。 */
68
+ function refIdOfDocId(docId: unknown): string {
69
+ const s = String(docId || '');
70
+ if (s.startsWith('ref:')) return s.slice(4);
71
+ if (!s.startsWith('rel:')) return '';
72
+ return String(getDb().get('SELECT id FROM refs WHERE rel = :rel AND deleted_at IS NULL', { rel: s.slice(4) })?.id || '');
73
+ }
74
+
66
75
  /** 仓库知识报告(repoKnowledge 的出参)。 */
67
76
  export interface RepoKnowledgeResult {
68
77
  repo: { key: string; name: string; path: string; desc: string; remote: string; type: string };
@@ -275,37 +284,11 @@ export async function repoKnowledge(locator?: string, { md = false }: { md?: boo
275
284
  refRepos,
276
285
  warnings,
277
286
  };
278
- if (md) out.md = renderMd(out);
287
+ if (md) out.md = renderRepoKnowledgeMd(out);
279
288
  return out;
280
289
  }
281
290
 
282
- // 单篇 markdown:项目介绍 → 各组(介绍/组内描述/绑定文档)→ 参考仓库
283
- function renderMd(k: RepoKnowledgeResult): string {
284
- const lines: string[] = [];
285
- const heading = (t: string) => (lines.length ? lines.push('', t) : lines.push(t));
286
- heading(`# ${k.repo.name}`);
287
- lines.push(...introMdLines(k.introSet, k.intro));
288
- for (const b of k.boards) {
289
- heading(`## 组:${b.name}`);
290
- lines.push(...introMdLines(b.introSet, b.intro));
291
- if (b.note && !b.note.missing) lines.push(b.note.body!.trim());
292
- for (const d of b.docLinks) {
293
- if (!d.missing && d.body) lines.push(d.body.trim());
294
- }
295
- for (const dir of b.externalDirs) {
296
- lines.push('', `外部目录「${dir.path}」(${dir.files!.length} 个文档,按需读盘):`);
297
- for (const f of dir.files!) lines.push(`- ${f}`);
298
- }
299
- }
300
- if (k.refRepos.length) {
301
- heading('## 参考仓库');
302
- for (const r of k.refRepos) {
303
- const src = r.direct && !r.groups.length ? '[直接绑定]' : r.direct ? '[组 + 直接绑定]' : '';
304
- lines.push(`- ${r.name}${r.remote ? `(${r.remote})` : ''}${src}${r.note ? ` —— ${r.note.split('\n')[0] ?? ''}` : ''}`);
305
- }
306
- }
307
- return lines.join('\n');
308
- }
291
+ // 单篇 markdown 渲染在 knowledge-md.ts(保持本文件在行数门禁内)
309
292
 
310
293
 
311
294
  /**
@@ -327,22 +310,41 @@ export async function groupContextFor(cwd: unknown, { board = '' }: { board?: st
327
310
  if (!repo) return null; // 没登记的目录:没有项目,也没有组
328
311
  const rk = deriveKey(repo);
329
312
  const warnings: string[] = [];
330
- const closure = await store.externalClosure(s);
331
313
  const groups: GroupContextGroup[] = [];
332
314
 
333
- // 项目介绍(项目级任务也要带——它回答「这个项目是干什么的」)。
334
- // 介绍是**文档集**(主篇 + 其余各篇),不是孤立的一篇——#58。
335
- const repoIntroSet = await readIntroSet({
336
- dirRel: paths.repoDocDirRel(rk),
337
- legacyRel: paths.repoDocRel(rk),
338
- ref: repo.doc,
339
- links: repo.docLinks,
340
- warnings,
341
- label: '项目介绍',
342
- });
343
- const repoIntro = mainAsIntro(repoIntroSet);
344
- // 项目自己登记的补充文档(外部 / ref 仓库里的文件)——交付时给可读路径
345
- const repoDocLinks = linkEntries(repo.docLinks, closure);
315
+ // 项目主篇:只认 type=repo / position=0 的绑定(正文唯一自动内嵌的入口)。
316
+ // ref 是 DB 实体,没有路径;其余绑定只给 desc + 取用指令。
317
+ const repoBound = boundDocs({ repoKey: rk });
318
+ const repoMain = repoBound.find((d) => d.type === 'repo' && d.position === 0 && d.hasBody);
319
+ let repoIntro = repoMain
320
+ ? { body: repoMain.body, path: '', title: repoMain.title, refId: repoMain.refId, desc: repoMain.desc }
321
+ : { body: '', path: '', title: '', refId: '', desc: '' };
322
+ let repoDocLinks: BoundDoc[] = repoBound.filter((d) => !(d.type === 'repo' && d.position === 0));
323
+ // 绑定索引还没覆盖到的库(例如刚写入、尚未 syncRefIndex):回落到 ext / 约定路径
324
+ if (!repoBound.length) {
325
+ const set = await readIntroSet({
326
+ dirRel: paths.repoDocDirRel(rk),
327
+ legacyRel: paths.repoDocRel(rk),
328
+ ref: repo.doc,
329
+ links: repo.docLinks,
330
+ warnings,
331
+ label: '项目介绍',
332
+ });
333
+ if (set.main && set.main.body) {
334
+ repoIntro = {
335
+ body: set.main.body,
336
+ path: set.main.abs || '',
337
+ title: set.main.title || '',
338
+ refId: refIdOfDocId(set.main.docId),
339
+ desc: '',
340
+ };
341
+ }
342
+ repoDocLinks = (Array.isArray(repo.docLinks) ? repo.docLinks : []).map((l: any) => ({
343
+ refId: refIdOfDocId(l.rel ? 'rel:' + l.rel : ''), desc: l.note || '', title: '',
344
+ hasBody: !!(l.rel || l.path), origin: '', type: 'repo' as const, repoKey: rk,
345
+ groupKey: null, position: 0, note: l.note || '', body: '',
346
+ }));
347
+ }
346
348
 
347
349
  // 参考仓库(repos 里 type=ref,不是文档库 refs):本仓库直接绑定与组级绑定
348
350
  // 是两类并列来源,交付时要给「名称 + 本机路径 + 一句描述」。
@@ -365,17 +367,43 @@ export async function groupContextFor(cwd: unknown, { board = '' }: { board?: st
365
367
  if (!members.includes(rk)) continue;
366
368
  // 组任务只挂**它归属的那个组**(id / key / 名称都认,与 --board 的寻址口径一致);
367
369
  // 归属值对不上任何组(陈旧数据)时同纯项目任务一样不挂,避免错挂别组。
368
- if (![String(b.id || ''), boardKeyOf(b.name), String(b.name || '')].includes(wantBoard)) continue;
369
- // 组介绍(绑定优先 → 介绍目录主篇 → 旧单文件;是文档集,不是一篇)
370
- const introSet = await readIntroSet({
371
- dirRel: paths.boardDocDirRel(b.name),
372
- legacyRel: paths.boardDocRel(b.name),
373
- ref: b.doc,
374
- links: b.docLinks,
375
- warnings,
376
- label: `组介绍·${b.name}`,
377
- });
378
- const intro = mainAsIntro(introSet);
370
+ if (![String(b.id || ''), String(b.key || ''), boardKeyOf(b.name), String(b.name || '')].includes(wantBoard)) continue;
371
+ // 组主篇:type=group / position=0;正文不内嵌,只给 desc + 取用指令。
372
+ // 组的身份是冻结 key(group/xxx),不是按名字现算的 board/xxx。
373
+ const gkey = String(b.key || boardKeyOf(b.name));
374
+ const gBound = boundDocs({ repoKey: rk, groupKey: gkey });
375
+ const gMain = gBound.find((d) => d.type === 'group' && d.position === 0 && d.hasBody);
376
+ let intro = gMain
377
+ ? { body: gMain.body, path: '', title: gMain.title, refId: gMain.refId, desc: gMain.desc }
378
+ : { body: '', path: '', title: '', refId: '', desc: '' };
379
+ let docLinks: BoundDoc[] = gBound.filter(
380
+ (d) => d.type !== 'repo' && !(d.type === 'group' && d.position === 0)
381
+ );
382
+ // 绑定索引尚未覆盖时回落到 ext / 约定路径(只影响数据来源,模板仍不内嵌组正文)
383
+ if (!gBound.length) {
384
+ const set = await readIntroSet({
385
+ dirRel: paths.boardDocDirRel(b.name),
386
+ legacyRel: paths.boardDocRel(b.name),
387
+ ref: b.doc,
388
+ links: b.docLinks,
389
+ warnings,
390
+ label: `组介绍·${b.name}`,
391
+ });
392
+ if (set.main && set.main.body) {
393
+ intro = {
394
+ body: set.main.body,
395
+ path: set.main.abs || '',
396
+ title: set.main.title || '',
397
+ refId: refIdOfDocId(set.main.docId),
398
+ desc: '',
399
+ };
400
+ }
401
+ docLinks = (Array.isArray(b.docLinks) ? b.docLinks : []).map((l: any) => ({
402
+ refId: refIdOfDocId(l.rel ? 'rel:' + l.rel : ''), desc: l.note || '', title: '',
403
+ hasBody: !!(l.rel || l.path), origin: '', type: 'group' as const,
404
+ repoKey: gkey, groupKey: null, position: 0, note: l.note || '', body: '',
405
+ }));
406
+ }
379
407
  // 本项目在组里的位置
380
408
  const noteRef = (b.repoDocs || {})[rk];
381
409
  const noteOwn = await readRef(noteRef, paths.groupNoteRel(b.name, rk), { warnings, label: `组内描述·${b.name}` });
@@ -397,14 +425,12 @@ export async function groupContextFor(cwd: unknown, { board = '' }: { board?: st
397
425
  path: (mr && mr.path) || '',
398
426
  });
399
427
  }
400
- // 组绑定参考文件(给可读路径——agent 直接读盘);tag 随条目走(有才带)
401
- const docLinks = linkEntries(b.docLinks, closure);
402
428
  groups.push({
403
429
  name: b.name,
404
- key: boardKeyOf(b.name),
430
+ key: gkey,
405
431
  desc: b.desc || '',
406
432
  intro,
407
- introSet,
433
+ introSet: { main: null, extras: [], count: 0 },
408
434
  note: noteOwn.body || (b.repoNotes || {})[rk] || '',
409
435
  peers,
410
436
  docLinks,
@@ -412,21 +438,30 @@ export async function groupContextFor(cwd: unknown, { board = '' }: { board?: st
412
438
  });
413
439
  }
414
440
  return {
415
- repo: { name: repo.name, key: rk, intro: repoIntro, introSet: repoIntroSet, docLinks: repoDocLinks, refRepos: repoRefRepos },
441
+ repo: { name: repo.name, key: rk, intro: repoIntro, introSet: { main: null, extras: [], count: 0 }, docLinks: repoDocLinks, refRepos: repoRefRepos },
416
442
  groups,
417
443
  };
418
444
  }
419
445
 
446
+ /** 介绍主篇(提示词用):正文 + desc + refId;不再有路径。 */
447
+ export interface ContextIntro {
448
+ body: string;
449
+ path: string;
450
+ title: string;
451
+ refId: string;
452
+ desc: string;
453
+ }
454
+
420
455
  /** groupContextFor 的组条目。 */
421
456
  export interface GroupContextGroup {
422
457
  name: string;
423
458
  key: string;
424
459
  desc: string;
425
- intro: { body: string; path: string; title: string };
460
+ intro: ContextIntro;
426
461
  introSet: IntroSet;
427
462
  note: string;
428
463
  peers: Array<{ name: string; note: string; path: string }>;
429
- docLinks: KnowledgeDoc[];
464
+ docLinks: BoundDoc[];
430
465
  refRepos: Array<{ key: string; name: string; path: string; note: string }>;
431
466
  }
432
467
 
@@ -435,9 +470,9 @@ export interface GroupContextResult {
435
470
  repo: {
436
471
  name: string;
437
472
  key: string;
438
- intro: { body: string; path: string; title: string };
473
+ intro: ContextIntro;
439
474
  introSet: IntroSet;
440
- docLinks: KnowledgeDoc[];
475
+ docLinks: BoundDoc[];
441
476
  refRepos: Array<{ key: string; name: string; path: string; note: string }>;
442
477
  };
443
478
  groups: GroupContextGroup[];
@@ -456,7 +491,7 @@ export interface GroupContextResult {
456
491
  *
457
492
  * 每篇都带两套坐标:
458
493
  * docId 面板导航用('rel:…' 自有树 / 'path:…' 外部绝对路径)
459
- * abs **磁盘绝对路径**——agent 拿到就能直接读盘(交付层用这个)
494
+ * abs 仅外部文档有:磁盘绝对路径(自有文档正文在 DB,没有磁盘副本)
460
495
  *
461
496
  * 为什么描述是「列表」而不是一篇:介绍往往不止主篇——还有架构、问答、规范等补充
462
497
  * 说明。只给一篇,等于让 agent 拿着半张地图干活(#58)。
@@ -482,7 +517,7 @@ export async function readIntroSet({
482
517
  const one = await readOwn(ref.rel, { warnings, label });
483
518
  if (one.missing) return emptyIntroSet();
484
519
  return {
485
- main: { docId: paths.ownDocId(ref.rel), title: one.title, body: one.body, abs: join(paths.docsRoot(), ref.rel) },
520
+ main: { docId: paths.ownDocId(ref.rel), title: one.title, body: one.body },
486
521
  extras: [],
487
522
  count: 1,
488
523
  };
@@ -537,14 +572,13 @@ export async function readIntroSet({
537
572
  docId: paths.ownDocId(d.rel),
538
573
  name: d.name,
539
574
  rel: d.rel,
540
- abs: d.abs || join(paths.docsRoot(), d.rel),
541
575
  isReference: String(d.name || '').toLowerCase().startsWith(paths.DOC_REFERENCE_DIR + '/'),
542
576
  }));
543
577
  if (mainRel && (await store.ownDocExists(mainRel))) {
544
578
  const one = await readOwn(mainRel, { warnings, label });
545
579
  const extras = extrasOf(mainRel);
546
580
  return {
547
- main: { docId: paths.ownDocId(mainRel), title: one.title, body: one.body, abs: join(paths.docsRoot(), mainRel) },
581
+ main: { docId: paths.ownDocId(mainRel), title: one.title, body: one.body },
548
582
  extras,
549
583
  count: 1 + extras.length,
550
584
  };
@@ -554,7 +588,7 @@ export async function readIntroSet({
554
588
  if (!one.missing) {
555
589
  const extras = extrasOf(legacyRel);
556
590
  return {
557
- main: { docId: paths.ownDocId(legacyRel), title: one.title, body: one.body, abs: join(paths.docsRoot(), legacyRel) },
591
+ main: { docId: paths.ownDocId(legacyRel), title: one.title, body: one.body },
558
592
  extras,
559
593
  count: 1 + extras.length,
560
594
  };
@@ -568,7 +602,7 @@ export async function readIntroSet({
568
602
  return {
569
603
  main: first.missing
570
604
  ? null
571
- : { docId: paths.ownDocId(head.rel), title: first.title, body: first.body, abs: head.abs || join(paths.docsRoot(), head.rel) },
605
+ : { docId: paths.ownDocId(head.rel), title: first.title, body: first.body },
572
606
  extras,
573
607
  count: 1 + extras.length,
574
608
  };
@@ -576,40 +610,6 @@ export async function readIntroSet({
576
610
  return emptyIntroSet();
577
611
  }
578
612
 
579
- /** 文档集的主篇 → 旧的单篇形状({ body, path }),兼容既有消费方 */
580
- function mainAsIntro(set: IntroSet | null | undefined): { body: string; path: string; title: string } {
581
- const m = set && set.main;
582
- return m ? { body: m.body || '', path: m.abs || '', title: m.title || '' } : { body: '', path: '', title: '' };
583
- }
584
-
585
- /** 登记在 store 里的补充文档(rel 自有树 / path 外部)→ 可读绝对路径条目 */
586
- function linkEntries(links: unknown, closure: readonly store.ClosureEntry[]): KnowledgeDoc[] {
587
- const out: KnowledgeDoc[] = [];
588
- for (const l of Array.isArray(links) ? (links as Array<DocRef & { note?: string; tag?: string }>) : []) {
589
- if (l.rel) {
590
- out.push({ rel: l.rel, abs: join(paths.docsRoot(), l.rel), note: l.note || '', ...(l.tag ? { tag: l.tag } : {}) });
591
- continue;
592
- }
593
- const abs = l.path && store.resolveExternal(l.path, closure);
594
- if (!abs) continue;
595
- out.push({ path: abs, abs, note: l.note || '', ...(l.tag ? { tag: l.tag } : {}) });
596
- }
597
- return out;
598
- }
599
-
600
- /** 文档集 → markdown 行(主篇正文 + 其余各篇的可读路径) */
601
- function introMdLines(set: IntroSet | null | undefined, fallback: KnowledgeDoc | null | undefined): string[] {
602
- const out: string[] = [];
603
- const main = (set && set.main) || (fallback && !fallback.missing ? fallback : null);
604
- if (main && main.body) out.push(String(main.body).trim());
605
- const extras = (set && set.extras) || [];
606
- if (extras.length) {
607
- out.push('', '其余介绍文档(可直接读取):');
608
- for (const d of extras) out.push(`- ${d.abs || d.docId || d.rel || ''}`);
609
- }
610
- return out;
611
- }
612
-
613
613
  /** 空文档集(没有主篇、也没有其余) */
614
614
  function emptyIntroSet(): IntroSet {
615
615
  return { main: null, extras: [], count: 0 };
@@ -5,6 +5,7 @@
5
5
  import * as paths from './paths.js';
6
6
  import * as store from './store.js';
7
7
  import { reposBridge } from './repos-bridge.js';
8
+ import { syncRefIndex } from './ref-sync.js';
8
9
  import { parseWikiLinks, resolveWikiLink, normalizeWikiTarget } from '../../shared/wikilink.js';
9
10
  import type { WikiIndexEntry, WikiResolveResult } from '../../shared/wikilink.js';
10
11
 
@@ -19,8 +20,13 @@ export async function buildIndex(): Promise<WikiIndexEntry[]> {
19
20
  const { loadStore } = await reposBridge();
20
21
  const s = await loadStore();
21
22
  const closure = await store.externalClosure(s);
23
+ const own = await store.listOwnDocs();
24
+ // 顺手把「盘上有、库里没有」的文档补进 refs,并按 ext 重建绑定(#183):
25
+ // 双链索引是面板打开时拉一次的地方,放在这里既不会拖热路径,也不会漏掉
26
+ // 用户手工拷进 docs 树的 md。
27
+ await syncRefIndex({ files: own.map((f) => f.rel), store: s }).catch(() => null);
22
28
  const index: WikiIndexEntry[] = [];
23
- for (const f of await store.listOwnDocs()) {
29
+ for (const f of own) {
24
30
  index.push({ docId: paths.ownDocId(f.rel), name: normalizeWikiTarget(baseNameOf(f.rel)), rel: f.rel });
25
31
  }
26
32
  for (const abs of await store.listExternalDocs(closure)) {
@@ -0,0 +1,24 @@
1
+ // 文档侧 mdfile 的读取与巡检(业务编排,核心读写仍在 core/attachmentdb/mdfile)。
2
+ import { getDb } from '../../core/db/index.js';
3
+ import { tx } from '../../core/db/tx.js';
4
+ import { listMdfiles, readMdfile } from '../../core/attachmentdb.js';
5
+ import type { MdfileRow, ReadMdfileResult } from '../../core/attachmentdb.js';
6
+ import { badInput, notFound } from '../../core/errors.js';
7
+
8
+ /** 读一个 mdfile 的正文(读时校验指纹;缺失只标 missing_at)。 */
9
+ export async function readDocMdfile({ attachment }: { attachment?: unknown } = {}): Promise<ReadMdfileResult> {
10
+ const id = String(attachment || '').trim();
11
+ if (!id) throw badInput('需要 --attachment <mdfile 的附件 id>(见 docs mdfiles)');
12
+ const db = getDb();
13
+ let out: ReadMdfileResult | null = null;
14
+ tx((conn) => {
15
+ out = readMdfile(conn, id);
16
+ }, { db, mode: 'IMMEDIATE' });
17
+ if (!out) throw notFound(`没有这条 mdfile 附件: ${id}`);
18
+ return out;
19
+ }
20
+
21
+ /** 列出全部已登记的 mdfile(--missing 只看已失效的)。 */
22
+ export async function listDocMdfiles({ missing = false }: { missing?: unknown } = {}): Promise<MdfileRow[]> {
23
+ return listMdfiles(getDb(), { missingOnly: !!missing });
24
+ }
@@ -1,4 +1,4 @@
1
- // docs 自有树的路径权威:根目录、段级 slug、约定路径。
1
+ // docs 自有树的路径权威:约定路径与段级 slug。
2
2
  // (DocRef / docId 校验在 docref.js——那里刻意不依赖 repos/service,见其文件头。)
3
3
  //
4
4
  // 目录树承载语义(键位约定,导出 zip 原样带走):
@@ -7,18 +7,15 @@
7
7
  // group/<组 slug>/<key 分段>.md 组内描述 group/nx系列/github.com/me/x.md
8
8
  // 其余任意 .md 自由文档(同样可索引/双链/绑定)
9
9
  //
10
+ // **磁盘不再是文档来源**(v6 + DOCS_DIR 收口):自有文档正文只从 `ref_bodies` 读,
11
+ // mdfile 默认附件目录定义在 core/paths(环境变量名保持不变)。
12
+ // 这里保留的是 **rel 的命名规则**,与文件系统无关。
13
+ //
10
14
  // key 全局唯一(repos 模块保证)→ 约定路径不可能互相撞,不需要指纹后缀。
11
15
  // 段级 slug 只兜 Windows 的两个坑:段尾点(资源管理器拒绝创建)与保留设备名。
12
- import { join } from 'node:path';
13
- import { APP_DIR } from '../../core/paths.js';
14
16
  import { normalizeKey } from '../../shared/repo-key.js';
15
17
  import { localKeyOf } from '../../core/keys.js';
16
18
 
17
- // 允许测试与多实例覆盖文档树根:与 NX_KV_REPOS_STORE 同一模式
18
- export function docsRoot(): string {
19
- return process.env.NX_KV_DOCS_DIR || join(APP_DIR, 'docs');
20
- }
21
-
22
19
  // Windows 保留设备名(不分大小写):命中即追加下划线
23
20
  const RESERVED = new Set([
24
21
  'con', 'prn', 'aux', 'nul',
@@ -61,7 +58,7 @@ export function groupNoteRel(boardName: unknown, repoKey: unknown): string {
61
58
  }
62
59
 
63
60
  // DocRef / docId 校验(docref.js):这里转发导出,调用方统一从 paths 拿
64
- export { assertOwnedRel, assertDocRef, sanitizeDocRef, parseDocId, ownDocId, extDocId } from './docref.js';
61
+ export { assertOwnedRel, assertDocRel, assertDocRef, sanitizeDocRef, parseDocId, ownDocId, extDocId } from './docref.js';
65
62
  export type { DocRef, DocRefWithMeta, ParsedDocId } from './docref.js';
66
63
 
67
64
  // ─── 介绍目录(多篇)──────────────────────────────────────────────────