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,95 @@
1
+ // 文件类型与本地文件名的纯函数(无网络、无 DB)。
2
+ //
3
+ // 从原来的云端 fileapi / attachstore 抽出:删除云端图床后,本地任务与文档
4
+ // 仍需要「按扩展名猜 MIME / 判断图片 / 生成安全文件名」这几个helper。
5
+ import { extname, parse as parsePath } from 'node:path';
6
+
7
+ // 扩展名 → Content-Type。上传时按文件名猜:一律 octet-stream 会让浏览器
8
+ // 拿到 application/octet-stream 而不内联渲染,图片就「预览不出来」了。
9
+ const MIME: Record<string, string> = {
10
+ '.png': 'image/png',
11
+ '.jpg': 'image/jpeg',
12
+ '.jpeg': 'image/jpeg',
13
+ '.webp': 'image/webp',
14
+ '.gif': 'image/gif',
15
+ '.bmp': 'image/bmp',
16
+ '.avif': 'image/avif',
17
+ '.svg': 'image/svg+xml',
18
+ '.ico': 'image/x-icon',
19
+ '.pdf': 'application/pdf',
20
+ '.doc': 'application/msword',
21
+ '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
22
+ '.xls': 'application/vnd.ms-excel',
23
+ '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
24
+ '.ppt': 'application/vnd.ms-powerpoint',
25
+ '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
26
+ '.txt': 'text/plain; charset=utf-8',
27
+ '.md': 'text/markdown; charset=utf-8',
28
+ '.csv': 'text/csv; charset=utf-8',
29
+ '.json': 'application/json; charset=utf-8',
30
+ '.zip': 'application/zip',
31
+ };
32
+
33
+ const IMAGE_EXT = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.avif', '.svg', '.ico']);
34
+
35
+ const extOf = (name: unknown): string => {
36
+ const m = /(\.[A-Za-z0-9]{1,8})$/.exec(String(name || ''));
37
+ return m ? (m[1] ?? '').toLowerCase() : '';
38
+ };
39
+
40
+ export function contentTypeOf(name: unknown, fallback = 'application/octet-stream'): string {
41
+ return MIME[extOf(name)] || fallback;
42
+ }
43
+
44
+ export function isImageName(name: unknown, contentType = ''): boolean {
45
+ const e = extOf(name);
46
+ if (e) return IMAGE_EXT.has(e);
47
+ return /^image\//i.test(String(contentType));
48
+ }
49
+
50
+ // Windows 保留设备名做文件名会让文件无法访问
51
+ const RESERVED = new Set([
52
+ 'con', 'prn', 'aux', 'nul',
53
+ ...Array.from({ length: 9 }, (_, i) => `com${i + 1}`),
54
+ ...Array.from({ length: 9 }, (_, i) => `lpt${i + 1}`),
55
+ ]);
56
+
57
+ /** 收成跨平台可用的单段文件名(保留中文)。 */
58
+ export function safeFileName(name: unknown): string {
59
+ let s = String(name ?? '')
60
+ .replace(/[<>:"/\\|?*\x00-\x1f]/g, '_')
61
+ .replace(/\s+/g, ' ')
62
+ .trim()
63
+ .replace(/^\.+/, '');
64
+ if (!s) s = 'file';
65
+ if (RESERVED.has(parsePath(s).name.toLowerCase())) s = '_' + s;
66
+ return s.slice(0, 120);
67
+ }
68
+
69
+ const EXT_BY_TYPE: Record<string, string> = {
70
+ 'image/png': '.png',
71
+ 'image/jpeg': '.jpg',
72
+ 'image/webp': '.webp',
73
+ 'image/gif': '.gif',
74
+ 'image/svg+xml': '.svg',
75
+ 'application/pdf': '.pdf',
76
+ 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx',
77
+ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
78
+ 'application/vnd.openxmlformats-officedocument.presentationml.presentation': '.pptx',
79
+ 'text/plain; charset=utf-8': '.txt',
80
+ 'text/markdown; charset=utf-8': '.md',
81
+ };
82
+
83
+ /**
84
+ * 本地落点文件名。优先用调用方给的「人读名字」,否则退回 fileId;
85
+ * 两者都没有扩展名时,再按 fileId 自带后缀 / Content-Type 补一个。
86
+ */
87
+ export function localNameFor(fileId: string, preferred: unknown, contentType = ''): string {
88
+ let base = safeFileName(preferred || fileId);
89
+ if (!extname(base)) {
90
+ const fromId = extname(String(fileId));
91
+ const guess = fromId || EXT_BY_TYPE[String(contentType).toLowerCase()] || '';
92
+ if (guess) base += guess;
93
+ }
94
+ return base;
95
+ }
@@ -21,7 +21,8 @@
21
21
  // - `{{#repo}}…{{/repo}}` / `{{#group}}…{{/group}}` 等旧闸门——不再覆盖
22
22
  // `blockHelperMissing`,也不再把它映射成「真值条件」;
23
23
  // - `{{group.peers}}` / `{{repo.docs}}` / `{{attach.list}}` 这类旧纯文本占位符;
24
- // - `{{attach.sync}}` / `{{attach.syncText}}` 旧别名、`{{block.content}}` 旧包装。
24
+ // - `{{attach.sync}}` / `{{attach.syncText}}` / `{{attach.syncSummary}}` 旧别名(云端同步已删除)、
25
+ // `{{block.content}}` 旧包装。
25
26
  // 这些写法在保存校验时直接拒绝,运行期遇到也按坏模板回落内置模板(见下),
26
27
  // 不会悄悄按旧语义生效。
27
28
  //
@@ -266,6 +267,7 @@ const LEGACY_PLACEHOLDERS = new Set([
266
267
  'groups',
267
268
  'attach.sync',
268
269
  'attach.syncText',
270
+ 'attach.syncSummary',
269
271
  'block.content',
270
272
  ]);
271
273
 
@@ -286,10 +288,10 @@ function legacySyntaxError(template: unknown): string | null {
286
288
  while ((m = mustacheRe.exec(src))) {
287
289
  const name = (m[1] ?? '').trim();
288
290
  if (/^else\b/.test(name)) continue;
289
- if (LEGACY_PLACEHOLDERS.has(name) || /^attach\.sync(?!Summary)(?:\.|\s*$)/.test(name)) {
290
- const isSync = name === 'attach.sync' || name === 'attach.syncText' || /^attach\.sync\./.test(name);
291
+ if (LEGACY_PLACEHOLDERS.has(name) || /^attach\.sync/.test(name)) {
292
+ const isSync = name === 'attach.sync' || name === 'attach.syncText' || name === 'attach.syncSummary' || /^attach\.sync\./.test(name);
291
293
  const hint = isSync
292
- ? '同步汇总请改用 {{attach.syncSummary.text}}(并用 {{#if attach.syncSummary}} 判断是否跑过同步);单条附件可达性看 file.localPath / file.exists'
294
+ ? '附件只在本机(本机附件仓),不再有云端同步汇总;单条附件可达性看 file.localPath / file.exists'
293
295
  : '列表请改用 {{#each 列表}}…{{/each}},对象请直接取子字段';
294
296
  return `旧占位符 {{${name}}} 已废弃;${hint}`;
295
297
  }
@@ -2,10 +2,8 @@
2
2
  // **元数据唯一真相在 SQLite 的 attachments 表**(schema v2)。
3
3
  //
4
4
  // 口径(任务 #15 验收反馈 2026-11-04 起,2026-11-08 收口到 DB):
5
- // · 本地任务贴图走本地存储,与云端图床零关联——上传不碰网、预览不透传、
6
- // agent 领取直接读本地路径;
7
- // · 云端清单(todo)的附件仍走云端图床(core/fileapi.js + core/attachstore.js),
8
- // 但云端镜像的元数据同样进 attachments(storage='cloud');
5
+ // · 本地任务贴图走本地存储:上传不碰网、预览不透传、agent 领取直接读本地路径;
6
+ // · 云端任务/图床已移除,这里只服务本地附件;
9
7
  // · 正文引用语法完全不变:`![名](fileId)` / `[名](fileId)`,fileId = attachments.id。
10
8
  //
11
9
  // 目录:~/.nx-kv/attachments/local/(NX_KV_ATTACHMENTS 可改根目录)。
@@ -17,10 +15,9 @@ import { existsSync } from 'node:fs';
17
15
  import { createReadStream } from 'node:fs';
18
16
  import { dirname, join, parse as parsePath } from 'node:path';
19
17
  import { attachmentsRootFromEnv } from './paths.js';
20
- import { contentTypeOf } from './fileapi.js';
18
+ import { contentTypeOf, localNameFor, safeFileName } from './filekind.js';
21
19
  import { getDb } from './db/index.js';
22
20
  import { getAttachment, upsertFileAttachment } from './attachmentdb.js';
23
- import { safeFileName, localNameFor } from './attachstore.js';
24
21
  import type { ReadStream } from 'node:fs';
25
22
  import type { DbApi, Row } from './db/driver.js';
26
23
 
package/src/core/paths.ts CHANGED
@@ -51,7 +51,7 @@ export function backupsDirFromEnv(): string {
51
51
 
52
52
  export const DEFAULT_PORT = 7877;
53
53
 
54
- // 附件的本地镜像根目录。**刻意放在 nx-kv 的用户目录下**(~/.nx-kv/attachments/):
54
+ // 本机附件仓根目录。**刻意放在 nx-kv 的用户目录下**(~/.nx-kv/attachments/):
55
55
  // agent 读附件只认本地路径,放在项目目录里会随 cwd 漂移,还会被 git 捡走。
56
56
  export const ATTACHMENTS_DIR = join(APP_DIR, 'attachments');
57
57
 
@@ -65,6 +65,16 @@ export function attachmentsRootFromEnv(): string {
65
65
  return process.env.NX_KV_ATTACHMENTS || ATTACHMENTS_DIR;
66
66
  }
67
67
 
68
+ // mdfile 的默认附件目录。
69
+ //
70
+ // **语义(2026-11 硬约束)**:这个目录只用来存放 mdfile 的默认附件,
71
+ // 只能通过 attach 访问;文档正文的唯一真相在 `ref_bodies`,这里不再承载文档。
72
+ // 除 attachment 层与迁移脚本外,任何模块不得直接用 fs 打开这个目录下的文件。
73
+ // 环境变量名 `NX_KV_DOCS_DIR` 保留(用户的既有配置不能因为改名作废)。
74
+ export function mdfileDirFromEnv(appDir: string = APP_DIR): string {
75
+ return process.env.NX_KV_DOCS_DIR || join(appDir, 'docs');
76
+ }
77
+
68
78
  // 名称安全校验:拒绝路径穿越(保留中文等合法命名)。
69
79
  //
70
80
  // 用于「这个字符串会被用作**目录名**」的场景:skill 名、内置包名、适配器 id。
package/src/index.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  // 编程入口:供其他工具 / agent 以库方式调用。
2
2
  // 导出的是各模块的 service(业务真相源),不是 CLI/HTTP 壳。
3
3
  export * as config from './core/config.js';
4
- export * as kvapi from './core/kvapi.js';
5
4
  export * as errors from './core/errors.js';
6
- export * as todo from './modules/todo/service.js';
7
5
  export * as localtask from './modules/localtask/service.js';
8
6
 
9
7
  export { ACTIONS, MODULES } from './runtime/registry.js';
@@ -12,10 +12,16 @@ import { DocAttachAction } from './doc-attach.js';
12
12
  import { DocAttachmentsAction } from './doc-attachments.js';
13
13
  import { DocDetachAction } from './doc-detach.js';
14
14
  import { DocGetAction } from './doc-get.js';
15
+ import { DocLinkFileAction } from './doc-link-file.js';
15
16
  import { DocListAction } from './doc-list.js';
17
+ import { DocMdfileAction } from './doc-mdfile.js';
18
+ import { DocMdfilesAction } from './doc-mdfiles.js';
19
+ import { DocRefAddAction } from './doc-ref.js';
16
20
  import { DocRemoveAction } from './doc-remove.js';
21
+ import { DocRefUnbindAction } from './doc-unbind.js';
17
22
  import { DocUpdateAction } from './doc-update.js';
18
23
  import { DocsBindAction } from './docs-bind.js';
24
+ import { DocsBindingsAction } from './docs-bindings.js';
19
25
  import { DocsIndexAction } from './docs-index.js';
20
26
  import { DocsLinkAction } from './docs-link.js';
21
27
  import { DocsNotebindAction } from './docs-notebind.js';
@@ -29,10 +35,16 @@ export const ACTIONS: readonly (new (deps: Container) => ActionBase)[] = [
29
35
  DocAttachmentsAction,
30
36
  DocDetachAction,
31
37
  DocGetAction,
38
+ DocLinkFileAction,
32
39
  DocListAction,
40
+ DocMdfileAction,
41
+ DocMdfilesAction,
42
+ DocRefAddAction,
33
43
  DocRemoveAction,
44
+ DocRefUnbindAction,
34
45
  DocUpdateAction,
35
46
  DocsBindAction,
47
+ DocsBindingsAction,
36
48
  DocsIndexAction,
37
49
  DocsLinkAction,
38
50
  DocsNotebindAction,
@@ -46,10 +58,16 @@ export { DocAttachAction };
46
58
  export { DocAttachmentsAction };
47
59
  export { DocDetachAction };
48
60
  export { DocGetAction };
61
+ export { DocLinkFileAction };
49
62
  export { DocListAction };
63
+ export { DocMdfileAction };
64
+ export { DocMdfilesAction };
65
+ export { DocRefAddAction };
50
66
  export { DocRemoveAction };
67
+ export { DocRefUnbindAction };
51
68
  export { DocUpdateAction };
52
69
  export { DocsBindAction };
70
+ export { DocsBindingsAction };
53
71
  export { DocsIndexAction };
54
72
  export { DocsLinkAction };
55
73
  export { DocsNotebindAction };
@@ -17,6 +17,7 @@ const input = z.object({
17
17
  url: z.string().optional().describe("外部链接(http/https)"),
18
18
  task: z.string().optional().describe("任务全局 id(kind=task)"),
19
19
  doc: z.string().optional().describe("另一篇文档的 ref/path(kind=ref)"),
20
+ mdfile: z.string().optional().describe("本地 .md 文件路径(kind=mdfile:只登记指针,不复制正文)"),
20
21
  name: z.string().optional().describe("显示名(默认取文件名/原名)"),
21
22
  note: z.string().optional().describe("附件区备注"),
22
23
  noEmbed: z.boolean().optional().describe("只写 ref_attach(role=attach),不改正文"),
@@ -28,7 +29,7 @@ export class DocAttachAction extends ActionBase<typeof input> {
28
29
  id: 'doc.attach',
29
30
  cli: ['docs', 'attach'],
30
31
  http: ['POST', '/api/docs/attach'],
31
- summary: '给文档挂资源:--path / --fileId / --url / --task / --doc 之一;默认追加进正文,--no-embed 只挂附件区(不写正文)',
32
+ summary: '给文档挂资源:--path / --fileId / --url / --task / --doc / --mdfile 之一;默认追加进正文,--no-embed 只挂附件区(不写正文)',
32
33
  args: ['ref'],
33
34
  input,
34
35
  render: renderAttach,
@@ -0,0 +1,28 @@
1
+ // doc.link-file —— 登记一个本地 md 文件并挂到文档附件区(不复制正文)。
2
+ import { action } from '../../../runtime/action-decorator.js';
3
+ import { z } from 'zod';
4
+ import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
5
+ import { attachToDoc } from '../attach.js';
6
+
7
+ const input = z.object({
8
+ ref: z.string(),
9
+ path: z.string().describe('本地 .md 文件的绝对路径(相对路径按当前目录解析)'),
10
+ name: z.string().optional().describe('显示名(默认取文件名)'),
11
+ note: z.string().optional().describe('附件区备注'),
12
+ });
13
+
14
+ export class DocLinkFileAction extends ActionBase<typeof input> {
15
+ @action({
16
+ id: 'doc.link-file',
17
+ cli: ['docs', 'link-file'],
18
+ http: ['POST', '/api/docs/link-file'],
19
+ summary: '把本地 md 文件登记为 mdfile 附件并挂到文档附件区:只存路径指针,不复制正文,不改文档正文',
20
+ args: ['ref'],
21
+ input,
22
+ render: (d: { ref: string; attachmentId: string; name?: string }) =>
23
+ `已登记 mdfile ${d.attachmentId}(${d.name || d.ref})→ 文档 ${d.ref} 的附件区`,
24
+ })
25
+ override async run(ctx: ActionInput<this['_input']>) {
26
+ return attachToDoc({ ref: ctx.ref, mdfile: ctx.path, name: ctx.name, note: ctx.note, noEmbed: true });
27
+ }
28
+ }
@@ -0,0 +1,28 @@
1
+ // doc.mdfile —— 读一个本地 md 文件的正文(读时校验指纹)。
2
+ import { action } from '../../../runtime/action-decorator.js';
3
+ import { z } from 'zod';
4
+ import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
5
+ import { readDocMdfile } from '../mdfile.js';
6
+
7
+ const input = z.object({
8
+ attachment: z.string().describe('mdfile 的附件 id(见 docs mdfiles)'),
9
+ });
10
+
11
+ export class DocMdfileAction extends ActionBase<typeof input> {
12
+ @action({
13
+ id: 'doc.mdfile',
14
+ cli: ['docs', 'mdfile'],
15
+ http: ['GET', '/api/docs/mdfile'],
16
+ summary: '读本地 md 文件的正文(只读原文件,核对指纹;文件缺失时返回空正文并标 missing)',
17
+ args: ['attachment'],
18
+ input,
19
+ render: (d: { body: string; missing: boolean; stale: boolean; absPath: string }) => {
20
+ if (d.missing) return `(文件不存在: ${d.absPath})`;
21
+ const head = d.stale ? `(内容已变化,以下为磁盘当前内容: ${d.absPath})\n\n` : '';
22
+ return head + (d.body || '(空)');
23
+ },
24
+ })
25
+ override async run(ctx: ActionInput<this['_input']>) {
26
+ return readDocMdfile(ctx);
27
+ }
28
+ }
@@ -0,0 +1,29 @@
1
+ // doc.mdfiles —— 列出已登记的本地 md 文件(kind=mdfile)。
2
+ import { action } from '../../../runtime/action-decorator.js';
3
+ import { z } from 'zod';
4
+ import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
5
+ import { listDocMdfiles } from '../mdfile.js';
6
+
7
+ const input = z.object({
8
+ missing: z.boolean().optional().describe('只看已失效(missing_at 非空)的行'),
9
+ });
10
+
11
+ export class DocMdfilesAction extends ActionBase<typeof input> {
12
+ @action({
13
+ id: 'doc.mdfiles',
14
+ cli: ['docs', 'mdfiles'],
15
+ http: ['GET', '/api/docs/mdfiles'],
16
+ summary: '列出已登记的本地 md 文件(mdfile 附件;--missing 只看失效行)',
17
+ input,
18
+ render: (rows: Array<{ id: string; name: string; absPath: string; missing: boolean; missingAt: number | null }>) => {
19
+ if (!rows.length) return '没有 mdfile 附件';
20
+ return rows
21
+ .map((r) => `${r.missing ? '!' : '·'} ${r.id} ${r.name} ${r.absPath}` +
22
+ (r.missingAt ? ` (missing since ${new Date(r.missingAt).toISOString()})` : ''))
23
+ .join('\n');
24
+ },
25
+ })
26
+ override async run(ctx: ActionInput<this['_input']>) {
27
+ return listDocMdfiles(ctx);
28
+ }
29
+ }
@@ -0,0 +1,36 @@
1
+ // doc.ref.add —— 新建一个直接 ref(desc + 归属),不涉及任何文件 / 路径。
2
+ //
3
+ // 口径(v7 / 任务 #97):ref 是 DB 实体(refs + ref_bodies + ref_bindings),
4
+ // 身份就是 refId;新建时只给 desc 与宿主,正文为空、可立即编辑。
5
+ import { action } from '../../../runtime/action-decorator.js';
6
+ import { z } from 'zod';
7
+ import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
8
+ import { createRef as _createRef } from '../service.js';
9
+
10
+ const createRef: any = _createRef;
11
+
12
+ /** doc.ref.add 的入参契约(CLI 与 HTTP 共用同一份)。 */
13
+ const input = z.object({
14
+ desc: z.string().optional().describe('短描述(列表 / 提示词模板的快速定位标签)'),
15
+ owner: z.enum(['repo', 'group', 'group_repo']).optional().describe('归属类型:仓库 / 组 / 组下的仓库'),
16
+ repo: z.string().optional().describe('仓库 locator(id / key / 名称;owner=repo 或 group_repo 时必填)'),
17
+ group: z.string().optional().describe('组 locator(id / key / 名称;owner=group 或 group_repo 时必填)'),
18
+ });
19
+
20
+ export class DocRefAddAction extends ActionBase<typeof input> {
21
+ @action({
22
+ id: 'doc.ref.add',
23
+ cli: ['docs', 'ref', 'add'],
24
+ http: ['POST', '/api/docs/ref'],
25
+ summary: '新建文档条目(desc + 归属;正文为空、直接可编辑;不创建任何文件)',
26
+ input,
27
+ render: (d: any) =>
28
+ `已新建 ${d.refId}${d.desc ? `「${d.desc}」` : ''}` +
29
+ (d.bindings?.length
30
+ ? `\n归属: ${d.bindings.map((b: any) => `${b.type}${b.groupKey ? '@' + b.groupKey : ''}`).join('、')}`
31
+ : ''),
32
+ })
33
+ override async run(ctx: ActionInput<this['_input']>) {
34
+ return createRef({ desc: ctx.desc, owner: ctx.owner, repo: ctx.repo, group: ctx.group });
35
+ }
36
+ }
@@ -0,0 +1,34 @@
1
+ // doc.ref.unbind —— 解绑一条直接 ref 的归属(不删 ref、不删正文)。
2
+ //
3
+ // 派生绑定(既有文档由 ext 推导)不在这里处理:摘除仍走宿主的文档集接口。
4
+ import { action } from '../../../runtime/action-decorator.js';
5
+ import { z } from 'zod';
6
+ import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
7
+ import { unbindRef as _unbindRef } from '../service.js';
8
+
9
+ const unbindRef: any = _unbindRef;
10
+
11
+ /** doc.ref.unbind 的入参契约(CLI 与 HTTP 共用同一份)。 */
12
+ const input = z.object({
13
+ ref: z.string().describe('refId(ref_…)'),
14
+ type: z.enum(['repo', 'group', 'group_repo']).describe('归属类型'),
15
+ repo: z.string().describe('归属宿主的 repo_key(type=group 时给组 key)'),
16
+ group: z.string().optional().describe('组 key(type=group_repo 时必填)'),
17
+ });
18
+
19
+ export class DocRefUnbindAction extends ActionBase<typeof input> {
20
+ @action({
21
+ id: 'doc.ref.unbind',
22
+ cli: ['docs', 'ref', 'unbind'],
23
+ http: ['POST', '/api/docs/unbind'],
24
+ summary: '解绑直接 ref 的归属(不删文档;没有归属后回到 orphan)',
25
+ input,
26
+ render: (d: any) =>
27
+ d.removed
28
+ ? `已解绑 ${d.refId}(${d.orphan ? '已无归属' : '仍有其它归属'})`
29
+ : `没有匹配的归属可解绑: ${d.refId}`,
30
+ })
31
+ override async run(ctx: ActionInput<this['_input']>) {
32
+ return unbindRef({ ref: ctx.ref, type: ctx.type, repo: ctx.repo, group: ctx.group });
33
+ }
34
+ }
@@ -13,6 +13,7 @@ const renderWritten: any = _renderWritten;
13
13
  const input = z.object({
14
14
  ref: z.string(),
15
15
  body: z.string().optional().describe("Markdown 全文"),
16
+ desc: z.string().optional().describe("短描述(列表 / 提示词模板的快速定位标签;只给 desc 时不动正文)"),
16
17
  bodyFile: z.string().optional().describe("从文件读全文(--body 优先);`-` = 读标准输入"),
17
18
  'body-file': z.string().optional().describe("同 --bodyFile(连字符写法)"),
18
19
  });
@@ -22,7 +23,7 @@ export class DocUpdateAction extends ActionBase<typeof input> {
22
23
  id: 'doc.update',
23
24
  cli: ['docs', 'set'],
24
25
  http: ['PATCH', '/api/docs/item'],
25
- summary: '写文档正文(面板保存走这里;自有走 own 树、外部走绑定闭包内原文件;写盘后摘要回写 desc / 组内 note)',
26
+ summary: '写文档正文 / 描述(面板保存走这里;ref 是 DB 实体,--ref 可给 refId;只给 --desc 时不动正文)',
26
27
  args: ['ref'],
27
28
  input,
28
29
  render: renderWritten,
@@ -30,7 +31,7 @@ export class DocUpdateAction extends ActionBase<typeof input> {
30
31
  override async run(ctx: ActionInput<this['_input']>, opts: ActionRunMeta = {}) {
31
32
 
32
33
  return updateDoc(
33
- { ref: ctx.ref, body: ctx.body, bodyFile: opts && opts.transport === 'http' ? undefined : (ctx.bodyFile ?? ctx['body-file']) },
34
+ { ref: ctx.ref, body: ctx.body, desc: ctx.desc, bodyFile: opts && opts.transport === 'http' ? undefined : (ctx.bodyFile ?? ctx['body-file']) },
34
35
  opts
35
36
  );
36
37
 
@@ -22,7 +22,7 @@ export class DocsBindAction extends ActionBase<typeof input> {
22
22
  @action({
23
23
  id: 'docs.bind',
24
24
  cli: ['docs', 'bind'],
25
- http: ['POST', '/api/docs/bind'],
25
+ // 仅 CLI:绑定由「文档落约定路径」推导(syncRefIndex),前端不提供直绑入口
26
26
  summary: '绑定项目/组介绍的文档覆盖(--repo 或 --board 二选一 + --rel <自有路径> | --path <外部文件/目录>;--reset 拆回约定路径)',
27
27
  input,
28
28
  render: renderRef,
@@ -0,0 +1,108 @@
1
+ // docs.bindings —— ref 视角的绑定查询(只读)。
2
+ //
3
+ // 与其他绑定接口的关系:写绑定永远走「宿主文档集」那一侧(repo.doc.add / docs.link,
4
+ // 由 syncRefIndex 推导落 ref_bindings);本接口只负责**读**,让文档页能回答
5
+ // 「这篇文档被谁引用」,也让仓库表格一次拿全所有宿主的归属,避免逐宿主 N+1。
6
+ //
7
+ // position=0 即主篇(仓库/组介绍),见 docs/ref-sync.ts 的 desiredBindings。
8
+ import { action } from '../../../runtime/action-decorator.js';
9
+ import { z } from 'zod';
10
+ import { ActionBase, type ActionInput } from '../../../runtime/action-base.js';
11
+ import { getDb } from '../../../core/db/index.js';
12
+ import { refIdOf } from '../../../core/db/import-json.js';
13
+ import { docTitleFromMd } from '../../../shared/doc-title.js';
14
+
15
+ /** 入参 docId(ref:/rel:/path: 或无前缀 locator)→ ref id。 */
16
+ function toRefId(v: string): string {
17
+ const s = String(v || '').trim();
18
+ if (!s) return '';
19
+ if (/^ref_[0-9a-f]{16}$/i.test(s)) return s;
20
+ if (s.startsWith('ref:')) return s.slice(4);
21
+ const locator = s.startsWith('rel:') ? s.slice(4) : s.startsWith('path:') ? s.slice(5) : s;
22
+ if (/^ref_[0-9a-f]{16}$/i.test(locator)) return locator;
23
+ return refIdOf(locator.replace(/\\/g, '/'));
24
+ }
25
+
26
+ const input = z.object({
27
+ ref: z.string().optional().describe('refId / docId(ref:/rel:/path: 或裸 locator)——只看这篇文档的归属'),
28
+ host: z.string().optional().describe('宿主 repo_key —— 只看这个仓库的文档集(含它的组内文档)'),
29
+ group: z.string().optional().describe('宿主组 key —— 只看这个组的文档集(含组内各仓库)'),
30
+ });
31
+
32
+ export class DocsBindingsAction extends ActionBase<typeof input> {
33
+ @action({
34
+ id: 'docs.bindings',
35
+ cli: ['docs', 'bindings'],
36
+ http: ['GET', '/api/docs/bindings'],
37
+ summary: '查文档归属(--ref <refId> 看这篇被谁引用;--host <repo> / --group <组> 看宿主文档集;position=0 为主篇)',
38
+ input,
39
+ // 备注(只有附加篇有:主篇 push 时不带 note)跟着 main 一起打,和前端 tag 上的
40
+ // 「附加 · 备注」保持同一口径——否则 CLI 里看不出自己刚填的备注有没有落库。
41
+ render: (d: any) => (d.bindings || [])
42
+ .map((b: any) => {
43
+ const role = b.main ? '主篇' : b.note ? `附加 · ${b.note}` : '附加';
44
+ const label = b.desc || b.title || b.refId;
45
+ return ` ${b.type} ${b.repoKey}${b.groupKey ? ' @' + b.groupKey : ''} ${role} ${label} [${b.refId}]${b.hasBody ? '' : '(无正文)'}`;
46
+ })
47
+ .join('\n') || '(无归属)',
48
+ })
49
+ override async run(ctx: ActionInput<this['_input']>) {
50
+ const refId = ctx.ref ? toRefId(ctx.ref) : '';
51
+ const host = String(ctx.host || '').trim();
52
+ const group = String(ctx.group || '').trim();
53
+ const db = getDb();
54
+ const where: string[] = ['r.deleted_at IS NULL'];
55
+ const params: Record<string, unknown> = {};
56
+ if (refId) { where.push('b.ref_id = :ref'); params.ref = refId; }
57
+ if (host) { where.push('b.repo_key = :host'); params.host = host; }
58
+ // 组级绑定:repo_key = 组 key、group_key = NULL(type=group);
59
+ // 组内仓库绑定:group_key = 组 key(type=group_repo)。两种都要算进组文档集。
60
+ if (group) {
61
+ where.push("(b.group_key = :group OR (b.type = 'group' AND b.repo_key = :group))");
62
+ params.group = group;
63
+ }
64
+ const rows = db.all(
65
+ `SELECT b.repo_key AS repoKey, b.group_key AS groupKey, b.type, b.position, b.note,
66
+ r.id AS refId, r.rel, r."desc", r.origin, r.locator,
67
+ CASE WHEN rb.ref_id IS NULL THEN 0 ELSE 1 END AS has_body,
68
+ rb.body
69
+ FROM ref_bindings b JOIN refs r ON r.id = b.ref_id
70
+ LEFT JOIN ref_bodies rb ON rb.ref_id = r.id
71
+ WHERE ${where.join(' AND ')}
72
+ ORDER BY CASE b.type WHEN 'repo' THEN 0 WHEN 'group' THEN 1 ELSE 2 END,
73
+ b.position, b.rowid`,
74
+ params
75
+ );
76
+ return {
77
+ bindings: rows.map((r: any) => {
78
+ const refId = String(r.refId);
79
+ const type = String(r.type);
80
+ const position = Number(r.position) || 0;
81
+ const direct = String(r.rel) === refId;
82
+ // 摘除序号只对派生行有意义(直接行走 unbind);主篇的摘除语义见宿主侧。
83
+ const removeIndex = direct || position === 0
84
+ ? null
85
+ : type === 'repo'
86
+ ? position + 1
87
+ : type === 'group'
88
+ ? position
89
+ : null;
90
+ const body = Number(r.has_body) === 1 ? String(r.body ?? '') : '';
91
+ return {
92
+ refId,
93
+ desc: String(r.desc || ''),
94
+ title: body ? docTitleFromMd(body) : '',
95
+ hasBody: Number(r.has_body) === 1,
96
+ repoKey: String(r.repoKey),
97
+ groupKey: r.groupKey == null ? null : String(r.groupKey),
98
+ type,
99
+ position,
100
+ main: position === 0,
101
+ note: String(r.note || ''),
102
+ direct,
103
+ removeIndex,
104
+ };
105
+ }),
106
+ };
107
+ }
108
+ }
@@ -22,7 +22,7 @@ export class DocsNotebindAction extends ActionBase<typeof input> {
22
22
  @action({
23
23
  id: 'docs.notebind',
24
24
  cli: ['docs', 'note-bind'],
25
- http: ['POST', '/api/docs/note-bind'],
25
+ // 仅 CLI:成员描述覆盖同理,绑定走约定路径推导;前端加绑/解绑走宿主文档集接口
26
26
  summary: '绑定组内仓库描述的文档覆盖(--board <看板> --repo <仓库> + --rel/--path/--reset;仓库须为该组成员)',
27
27
  args: ['board'],
28
28
  input,
@@ -13,6 +13,7 @@ import {
13
13
  linkAttachment,
14
14
  listRefAttachments,
15
15
  refIdFor,
16
+ registerMdfile,
16
17
  resolveRefId,
17
18
  unlinkAttachment,
18
19
  updateAttachmentNote,
@@ -21,7 +22,7 @@ import {
21
22
  import type { LinkedAttachmentView } from '../../core/attachmentdb/query.js';
22
23
  import type { DeleteAttachmentResult } from '../../core/attachmentdb/query.js';
23
24
  import { localUpload } from '../../core/localatt.js';
24
- import { contentTypeOf, isImageName } from '../../core/fileapi.js';
25
+ import { contentTypeOf, isImageName } from '../../core/filekind.js';
25
26
  import { attachmentsRootFromEnv } from '../../core/paths.js';
26
27
  import { readFile } from 'node:fs/promises';
27
28
  import { basename, resolve as resolvePath } from 'node:path';
@@ -50,7 +51,7 @@ export async function listDocAttachments({ ref }: { ref?: unknown } = {}): Promi
50
51
  const d = await getDoc({ ref });
51
52
  const locator = String(d.rel || d.path || '').replace(/\\/g, '/');
52
53
  if (!locator) throw badInput('无法定位文档: ' + String(ref || ''));
53
- const refId = refIdFor(locator);
54
+ const refId = String((d as any).refId || refIdFor(locator));
54
55
  const attachments: Array<LinkedAttachmentView & { docTitle?: string; title?: string }> = listRefAttachments(getDb(), refId);
55
56
  // #168:ref 条目补**文档标题**(正文首行的 # 标题)——面板与编辑器都显示它
56
57
  try {
@@ -86,6 +87,8 @@ export interface AttachToDocCtx {
86
87
  url?: unknown;
87
88
  task?: unknown;
88
89
  doc?: unknown;
90
+ /** 本地 md 文件(登记为 kind=mdfile,不复制正文) */
91
+ mdfile?: unknown;
89
92
  noEmbed?: unknown;
90
93
  note?: unknown;
91
94
  [key: string]: unknown;
@@ -179,10 +182,20 @@ export async function attachToDoc(ctx: AttachToDocCtx): Promise<AttachToDocResul
179
182
  if (!attachmentId) throw badInput(`找不到文档: ${ctx.doc}`);
180
183
  kind = 'ref';
181
184
  markdown = `[${displayName || (target.locator.split('/').pop() ?? '')}](nxkv://ref/${refId})`;
185
+ } else if (ctx.mdfile) {
186
+ // 外部 md 文件:只登记指针(kind=mdfile, storage=external),正文仍在磁盘原地
187
+ const rec = tx(
188
+ (conn) => registerMdfile(conn, { path: ctx.mdfile, name: displayName }),
189
+ { db, mode: 'IMMEDIATE' }
190
+ );
191
+ attachmentId = rec.id;
192
+ displayName = displayName || rec.name;
193
+ kind = 'mdfile';
194
+ markdown = `[${displayName}](nxkv://mdfile/${rec.id})`;
182
195
  }
183
196
 
184
197
  if (!attachmentId) {
185
- throw badInput('需要 --path / --fileId / --url / --task / --doc 之一');
198
+ throw badInput('需要 --path / --fileId / --url / --task / --doc / --mdfile 之一');
186
199
  }
187
200
 
188
201
  const noEmbed = !!(ctx.noEmbed ?? ctx['no-embed']);