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.
- package/CHANGELOG.md +85 -0
- package/README.md +103 -216
- package/assets/groups.json +1 -1
- package/assets/kv-get/SKILL.md +1 -1
- package/assets/kv-get/references/localget.md +2 -2
- package/assets/kv-get/references/timegoal-workflow.md +3 -3
- package/assets/kv-get/references/timegoal.md +1 -1
- package/assets/kv-get/references/wait-workflow.md +2 -3
- package/assets/kv-get/references/waitloop.md +3 -3
- package/assets/kv-repo/SKILL.md +1 -1
- package/assets/kv-submit/SKILL.md +3 -3
- package/assets/nx-kv/SKILL.md +111 -76
- package/assets/nx-kv/references/agent-workflow.md +58 -47
- package/assets/nx-kv/references/localtask-commands.md +179 -0
- package/package.json +3 -4
- package/src/core/attachmentdb/bodies.ts +193 -0
- package/src/core/attachmentdb/docs.ts +158 -33
- package/src/core/attachmentdb/ids.ts +27 -1
- package/src/core/attachmentdb/mdfile.ts +249 -0
- package/src/core/attachmentdb/query.ts +14 -1
- package/src/core/attachmentdb/resources.ts +28 -2
- package/src/core/attachmentdb.ts +27 -0
- package/src/core/config.ts +13 -133
- package/src/core/db/bootstrap.ts +5 -5
- package/src/core/db/import-json/refs.ts +27 -13
- package/src/core/db/migrations/index.ts +9 -0
- package/src/core/db/migrations/v2-data.ts +4 -4
- package/src/core/db/migrations/v5.sql +100 -0
- package/src/core/db/migrations/v6-data.ts +261 -0
- package/src/core/db/migrations/v6.sql +67 -0
- package/src/core/db/migrations/v7.sql +8 -0
- package/src/core/db/seed-templates.ts +3 -1
- package/src/core/db/version.ts +12 -1
- package/src/core/filekind.ts +95 -0
- package/src/core/instruction-template.ts +6 -4
- package/src/core/localatt.ts +3 -6
- package/src/core/paths.ts +11 -1
- package/src/index.ts +0 -2
- package/src/modules/docs/actions/_manifest.generated.ts +18 -0
- package/src/modules/docs/actions/doc-attach.ts +2 -1
- package/src/modules/docs/actions/doc-link-file.ts +28 -0
- package/src/modules/docs/actions/doc-mdfile.ts +28 -0
- package/src/modules/docs/actions/doc-mdfiles.ts +29 -0
- package/src/modules/docs/actions/doc-ref.ts +36 -0
- package/src/modules/docs/actions/doc-unbind.ts +34 -0
- package/src/modules/docs/actions/doc-update.ts +3 -2
- package/src/modules/docs/actions/docs-bind.ts +1 -1
- package/src/modules/docs/actions/docs-bindings.ts +108 -0
- package/src/modules/docs/actions/docs-notebind.ts +1 -1
- package/src/modules/docs/attach.ts +16 -3
- package/src/modules/docs/bound-docs.ts +74 -0
- package/src/modules/docs/docref.ts +18 -3
- package/src/modules/docs/knowledge-md.ts +51 -0
- package/src/modules/docs/knowledge.ts +112 -112
- package/src/modules/docs/links.ts +7 -1
- package/src/modules/docs/mdfile.ts +24 -0
- package/src/modules/docs/paths.ts +6 -9
- package/src/modules/docs/ref-sync.ts +166 -0
- package/src/modules/docs/refs.ts +16 -5
- package/src/modules/docs/render.ts +33 -15
- package/src/modules/docs/seed.ts +1 -1
- package/src/modules/docs/service.ts +300 -30
- package/src/modules/docs/store.ts +147 -74
- package/src/modules/file/actions/_manifest.generated.ts +0 -9
- package/src/modules/file/actions/file-content.ts +3 -4
- package/src/modules/file/actions/file-get.ts +2 -3
- package/src/modules/file/actions/file-upload.ts +1 -4
- package/src/modules/file/service.ts +52 -253
- package/src/modules/localtask/action-render.ts +5 -5
- package/src/modules/localtask/actions/_manifest.generated.ts +0 -6
- package/src/modules/localtask/actions/localtask-attach.ts +4 -2
- package/src/modules/localtask/actions/localtask-cancelMany.ts +1 -1
- package/src/modules/localtask/actions/localtask-closeMany.ts +4 -3
- package/src/modules/localtask/actions/localtask-get.ts +1 -1
- package/src/modules/localtask/repo/tasks.ts +12 -6
- package/src/modules/localtask/service.ts +0 -2
- package/src/modules/localtask/task-ops.ts +4 -4
- package/src/modules/localtask/task-remove.ts +33 -16
- package/src/modules/localtask/task-templates.ts +2 -3
- package/src/modules/localtask/task-view.ts +2 -2
- package/src/modules/repos/actions/_manifest.generated.ts +0 -3
- package/src/modules/repos/actions/repo-update.ts +2 -4
- package/src/modules/repos/cli/render.ts +0 -15
- package/src/modules/repos/service/bind-topics.ts +8 -18
- package/src/modules/repos/service.ts +1 -1
- package/src/modules/repos/transfer/export.ts +4 -1
- package/src/modules/setting/actions/setting-set.ts +0 -2
- package/src/modules/setting/service.ts +6 -25
- package/src/modules/system/actions/bootstrap.ts +2 -31
- package/src/modules/system/actions/health.ts +4 -31
- package/src/modules/system/routes.ts +1 -1
- package/src/modules/tasktool/attach-resource.ts +68 -42
- package/src/modules/tasktool/attach-template.ts +73 -54
- package/src/modules/tasktool/attach.ts +98 -71
- package/src/modules/tasktool/delivery-template.ts +14 -4
- package/src/modules/tasktool/doc-inline.ts +63 -29
- package/src/modules/tasktool/get.ts +1 -1
- package/src/modules/tasktool/groupctx.ts +19 -11
- package/src/modules/tasktool/plugins/waitgoal-config.ts +24 -55
- package/src/modules/tasktool/plugins/waitgoal.ts +0 -2
- package/src/modules/tasktool/preview.ts +2 -3
- package/src/modules/tasktool/wait.ts +6 -9
- package/src/runtime/cli.ts +3 -15
- package/src/runtime/container.ts +1 -5
- package/src/runtime/help.ts +1 -4
- package/src/runtime/registry.ts +0 -6
- package/src/shared/prompt-blocks.ts +37 -33
- package/src/shared/task-status.ts +3 -3
- package/src/web/frontend/App.tsx +3 -3
- package/src/web/frontend/api/cli-tree.json +115 -608
- package/src/web/frontend/api/client.generated.ts +88 -401
- package/src/web/frontend/components/Introduction/index.tsx +26 -14
- package/src/web/frontend/components/Introduction/rail.tsx +28 -3
- package/src/web/frontend/components/Introduction/style.css +3 -33
- package/src/web/frontend/components/Introduction/use-intro.ts +0 -80
- package/src/web/frontend/components/{cloud → attach}/upload.ts +7 -20
- package/src/web/frontend/components/bindings/bind-tags.tsx +183 -0
- package/src/web/frontend/components/doc/doc-head.tsx +29 -26
- package/src/web/frontend/components/icons.tsx +0 -1
- package/src/web/frontend/components/layout/crumbs.ts +0 -2
- package/src/web/frontend/components/md/ack-rail.tsx +10 -142
- package/src/web/frontend/components/md/attach-area.tsx +64 -9
- package/src/web/frontend/components/md/attach-common.ts +8 -3
- package/src/web/frontend/components/md/attach-modal.tsx +22 -17
- package/src/web/frontend/components/md/attach-picker.tsx +2 -2
- package/src/web/frontend/components/md/attachments.ts +2 -3
- package/src/web/frontend/components/popup/dismiss.ts +1 -1
- package/src/web/frontend/components/repos/get-loop-modal.tsx +20 -207
- package/src/web/frontend/components/sidebar/index.tsx +32 -6
- package/src/web/frontend/components/sidebar/model.ts +71 -0
- package/src/web/frontend/components/sidebar/task-children.tsx +41 -20
- package/src/web/frontend/components/status-tag/model.ts +1 -1
- package/src/web/frontend/components/task-panel/index.tsx +4 -1
- package/src/web/frontend/components/task-panel/task-row.tsx +2 -2
- package/src/web/frontend/components/tool-acts/modules.ts +0 -3
- package/src/web/frontend/components/use-rail-drag.ts +177 -0
- package/src/web/frontend/modules/board/ack-rail-pos.ts +17 -7
- package/src/web/frontend/modules/board/board-view.ts +2 -2
- package/src/web/frontend/modules/board/task-search.ts +81 -0
- package/src/web/frontend/modules/md/attach-mention.ts +6 -1
- package/src/web/frontend/modules/md/doc-bus.ts +22 -0
- package/src/web/frontend/modules/md/md-render.ts +9 -2
- package/src/web/frontend/modules/md/styles/chrome.css +25 -0
- package/src/web/frontend/modules/md/styles/docs.css +32 -21
- package/src/web/frontend/modules/md/styles/shell.css +1 -10
- package/src/web/frontend/page/dir/api.ts +7 -0
- package/src/web/frontend/page/dir/index.tsx +24 -58
- package/src/web/frontend/page/dir/landing.tsx +85 -72
- package/src/web/frontend/page/dir/task-actions.ts +17 -5
- package/src/web/frontend/page/dir/task-dock-panel.tsx +87 -2
- package/src/web/frontend/page/dir/task-dock.ts +21 -10
- package/src/web/frontend/page/dir/task-goal.ts +2 -2
- package/src/web/frontend/page/dir/task-status-bar.tsx +14 -1
- package/src/web/frontend/page/dir/task.tsx +5 -3
- package/src/web/frontend/page/doc/api.ts +71 -5
- package/src/web/frontend/page/doc/bindings.tsx +240 -0
- package/src/web/frontend/page/doc/doc-dock.tsx +159 -0
- package/src/web/frontend/page/doc/index.tsx +131 -212
- package/src/web/frontend/page/doc/use-wiki-nav.ts +101 -0
- package/src/web/frontend/page/group/index.tsx +61 -87
- package/src/web/frontend/page/overview/index.tsx +1 -2
- package/src/web/frontend/page/refs/api.ts +60 -8
- package/src/web/frontend/page/refs/index.tsx +177 -90
- package/src/web/frontend/page/refs/style.css +45 -2
- package/src/web/frontend/page/repos/doc-search-modal.tsx +7 -0
- package/src/web/frontend/page/repos/index.tsx +21 -60
- package/src/web/frontend/page/repos/repo-table.tsx +85 -77
- package/src/web/frontend/page/settings/index.tsx +3 -27
- package/src/web/frontend/page/settings/prompt-template-params.ts +18 -28
- package/src/web/frontend/panels.tsx +52 -6
- package/src/web/frontend/registry.ts +1 -2
- package/src/web/frontend/routes/index.ts +1 -2
- package/src/web/frontend/routes/static.ts +2 -3
- package/src/web/frontend/store.tsx +1 -8
- package/src/web/frontend/style.css +1 -0
- package/src/web/frontend/styles/base.css +0 -17
- package/src/web/frontend/styles/bindings.css +75 -0
- package/src/web/frontend/styles/buttons-cards.css +0 -72
- package/src/web/frontend/styles/repos-admin.css +7 -5
- package/src/web/frontend/styles-v2/dir.css +1 -9
- package/src/web/frontend/styles-v2/shell.css +0 -2
- package/src/web/frontend/styles-v2/sidebar.css +12 -0
- package/src/web/frontend/styles-v2/views.css +22 -102
- package/src/web/frontend/workspace/areas.ts +0 -2
- package/src/web/public/assets/Introduction-CohW6Xit.js +6 -0
- package/src/web/public/assets/Introduction-Iuxssa-b.css +1 -0
- package/src/web/public/assets/app-context-BgpTewyA.js +1 -0
- package/src/web/public/assets/{bar-B52NTf_v.js → bar-C2mfoqmj.js} +1 -1
- package/src/web/public/assets/{board-view-DABoVkaA.js → board-view-aAIEbSQi.js} +1 -1
- package/src/web/public/assets/button-xyR2UROd.js +1 -0
- package/src/web/public/assets/composer-launch-eTktHT_f.js +1 -0
- package/src/web/public/assets/{composer-panel-DpiU2h50.js → composer-panel-C0OOIXxz.js} +1 -1
- package/src/web/public/assets/dag-Gm4RcvEg.js +1 -0
- package/src/web/public/assets/dir-PjuMRbWo.js +3 -0
- package/src/web/public/assets/group-D94y_Lq6.js +1 -0
- package/src/web/public/assets/index-CBJKsJU3.css +1 -0
- package/src/web/public/assets/index-Crxdmsux.js +2 -0
- package/src/web/public/assets/md-history-CU_1I6ht.js +39 -0
- package/src/web/public/assets/{md-parse-b6GY5eBl.js → md-parse-CLyUaUto.js} +1 -1
- package/src/web/public/assets/{member-picker-B5I7KuOR.js → member-picker-XT12VRe6.js} +1 -1
- package/src/web/public/assets/model-CWJ8_2dN.js +2 -0
- package/src/web/public/assets/{overview-B3kHI66i.js → overview-BQm8BLCG.js} +1 -1
- package/src/web/public/assets/{progress-CdhFz0Lz.js → progress-CNam3Wuj.js} +2 -2
- package/src/web/public/assets/refs-C5idWnUj.css +1 -0
- package/src/web/public/assets/refs-ClETk1cj.js +2 -0
- package/src/web/public/assets/repos-BioGejZo.js +14 -0
- package/src/web/public/assets/route-crumb-Fc68LcQ4.js +1 -0
- package/src/web/public/assets/settings-BIzumXLD.js +8 -0
- package/src/web/public/assets/status-tag-CBFf_-ae.js +1 -0
- package/src/web/public/assets/task-CGmO3I2y.js +11 -0
- package/src/web/public/assets/task-row-DRBa11tf.js +1 -0
- package/src/web/public/assets/{task-status-DSrx0i66.js → task-status-BHF-y9Aq.js} +2 -1
- package/src/web/public/assets/{task-utils-_JAWRAz6.js → task-utils-DjODjVdc.js} +2 -2
- package/src/web/public/assets/{templates-CbRyW3PJ.js → templates-j8EzXHXf.js} +1 -1
- package/src/web/public/assets/ui-Y0aL-bGk.js +1 -0
- package/src/web/public/assets/use-rail-drag-DNfz4Gcx.js +1 -0
- package/src/web/public/assets/{workspace-4tw7trV7.js → workspace-D_G-B3TX.js} +4 -4
- package/src/web/public/index.html +9 -9
- package/assets/nx-kv/references/todo-commands.md +0 -269
- package/src/core/attachstore.ts +0 -360
- package/src/core/fileapi.ts +0 -267
- package/src/core/kvapi.ts +0 -237
- package/src/modules/auth/actions/_manifest.generated.ts +0 -26
- package/src/modules/auth/actions/auth-login.ts +0 -31
- package/src/modules/auth/actions/auth-logout.ts +0 -33
- package/src/modules/auth/actions/auth-me.ts +0 -33
- package/src/modules/auth/actions/auth-status.ts +0 -41
- package/src/modules/auth/index.ts +0 -24
- package/src/modules/auth/support.ts +0 -28
- package/src/modules/file/actions/file-list.ts +0 -31
- package/src/modules/file/actions/file-remove.ts +0 -31
- package/src/modules/file/actions/file-sync.ts +0 -33
- package/src/modules/localtask/actions/localtask-importRemote.ts +0 -51
- package/src/modules/localtask/actions/task-move.ts +0 -67
- package/src/modules/localtask/move-prims.ts +0 -170
- package/src/modules/localtask/move.ts +0 -174
- package/src/modules/localtask/remote-import.ts +0 -159
- package/src/modules/repos/actions/repo-ref-backfill.ts +0 -33
- package/src/modules/repos/ref-backfill.ts +0 -219
- package/src/modules/todo/actions/_manifest.generated.ts +0 -89
- package/src/modules/todo/actions/group-add.ts +0 -34
- package/src/modules/todo/actions/group-current.ts +0 -36
- package/src/modules/todo/actions/group-get.ts +0 -43
- package/src/modules/todo/actions/group-list.ts +0 -41
- package/src/modules/todo/actions/group-members.ts +0 -41
- package/src/modules/todo/actions/group-remove.ts +0 -44
- package/src/modules/todo/actions/group-update.ts +0 -37
- package/src/modules/todo/actions/group-use.ts +0 -43
- package/src/modules/todo/actions/prompt-get.ts +0 -31
- package/src/modules/todo/actions/prompt-remove.ts +0 -31
- package/src/modules/todo/actions/prompt-set.ts +0 -32
- package/src/modules/todo/actions/todo-add.ts +0 -34
- package/src/modules/todo/actions/todo-archive.ts +0 -33
- package/src/modules/todo/actions/todo-attach.ts +0 -37
- package/src/modules/todo/actions/todo-attachments.ts +0 -33
- package/src/modules/todo/actions/todo-done.ts +0 -33
- package/src/modules/todo/actions/todo-freeze.ts +0 -32
- package/src/modules/todo/actions/todo-get.ts +0 -33
- package/src/modules/todo/actions/todo-list.ts +0 -32
- package/src/modules/todo/actions/todo-remove.ts +0 -32
- package/src/modules/todo/actions/todo-unfreeze.ts +0 -34
- package/src/modules/todo/actions/todo-update.ts +0 -42
- package/src/modules/todo/actions/topic-add.ts +0 -31
- package/src/modules/todo/actions/topic-list.ts +0 -35
- package/src/modules/todo/actions/topic-remove.ts +0 -36
- package/src/modules/todo/attachments.ts +0 -194
- package/src/modules/todo/group.ts +0 -207
- package/src/modules/todo/index.ts +0 -26
- package/src/modules/todo/locate.ts +0 -103
- package/src/modules/todo/service.ts +0 -476
- package/src/modules/todo/store.ts +0 -176
- package/src/modules/todo/support.ts +0 -93
- package/src/web/frontend/components/Introduction/docs-modal.tsx +0 -338
- package/src/web/frontend/components/cloud/editor.tsx +0 -254
- package/src/web/frontend/components/cloud/group-popup.tsx +0 -198
- package/src/web/frontend/components/cloud/markdown.tsx +0 -60
- package/src/web/frontend/components/migrate/dialog.tsx +0 -120
- package/src/web/frontend/components/migrate/undo.tsx +0 -73
- package/src/web/frontend/components/migrate/use-move.ts +0 -137
- package/src/web/frontend/page/cloud/api.ts +0 -99
- package/src/web/frontend/page/cloud/index.tsx +0 -516
- package/src/web/frontend/page/dir/deleted-panel.tsx +0 -77
- package/src/web/public/assets/Introduction-BcPrmROW.css +0 -1
- package/src/web/public/assets/Introduction-Du_dycft.js +0 -6
- package/src/web/public/assets/app-context-BASCGAA-.js +0 -1
- package/src/web/public/assets/cloud-Droja_aT.js +0 -3
- package/src/web/public/assets/composer-launch-mK9Nr6mZ.js +0 -1
- package/src/web/public/assets/dag-Ca-wjIxQ.js +0 -1
- package/src/web/public/assets/deleted-panel-K9GgfR8t.js +0 -1
- package/src/web/public/assets/dir-DacRpcBt.js +0 -5
- package/src/web/public/assets/frontmatter-CLLxCmTv.js +0 -2
- package/src/web/public/assets/group-TPuU-QVc.js +0 -2
- package/src/web/public/assets/index-BkLkooKe.js +0 -2
- package/src/web/public/assets/index-DP3qBv1o.css +0 -1
- package/src/web/public/assets/md-history-0GBWw4qr.js +0 -39
- package/src/web/public/assets/model-Bn2fG6CC.js +0 -2
- package/src/web/public/assets/prompt-blocks-BovFd039.js +0 -8
- package/src/web/public/assets/refs-BDxfuH6R.js +0 -1
- package/src/web/public/assets/refs-CY4i7PoC.css +0 -1
- package/src/web/public/assets/repos-C8jRM7wS.js +0 -15
- package/src/web/public/assets/repos-ZPiaK93i.css +0 -1
- package/src/web/public/assets/route-crumb-QvahotvN.js +0 -1
- package/src/web/public/assets/settings-BBcdGLTc.js +0 -1
- package/src/web/public/assets/status-tag-BheHByek.js +0 -1
- package/src/web/public/assets/task-CUkQaLBZ.js +0 -11
- package/src/web/public/assets/tool-bar-BAmT-BI-.js +0 -1
- package/src/web/public/assets/upload-BcMhRsMn.js +0 -1
- /package/src/web/public/assets/{prompt-blocks-DA4lNJeC.css → button-DA4lNJeC.css} +0 -0
- /package/src/web/public/assets/{tool-bar-1h2zairf.css → use-rail-drag-1h2zairf.css} +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// 标识与正文解析:fileId / ref / url / task 四类资源的稳定 id 公式,
|
|
2
2
|
// 以及 markdown 正文 → 资源引用清单的纯函数。本文件不碰数据库与文件系统。
|
|
3
3
|
import { createHash } from 'node:crypto';
|
|
4
|
+
import { resolve } from 'node:path';
|
|
4
5
|
|
|
5
6
|
// 不 import fileapi:它经 config → db/index → bootstrap → migrator 会绕回本模块。
|
|
6
7
|
// 这里只需要「按扩展名补一个够用的 MIME」,真实 MIME 由上传/同步路径写入。
|
|
@@ -23,6 +24,7 @@ const FILE_ID_RE = /^[0-9a-fA-F]{16,}(\.[A-Za-z0-9]{1,8})?$/;
|
|
|
23
24
|
const MD_LINK_RE = /!?\[([^\]]*)\]\(\s*([^)\s]+)\s*\)/g;
|
|
24
25
|
const NXKV_TASK_RE = /^nxkv:\/\/task\/(\d+)$/i;
|
|
25
26
|
const NXKV_REF_RE = /^nxkv:\/\/ref\/([A-Za-z0-9_-]+)$/i;
|
|
27
|
+
const NXKV_MDFILE_RE = /^nxkv:\/\/mdfile\/([A-Za-z0-9_-]+)$/i;
|
|
26
28
|
|
|
27
29
|
const sha1 = (s: unknown): string => createHash('sha1').update(String(s), 'utf8').digest('hex');
|
|
28
30
|
|
|
@@ -31,12 +33,13 @@ export type AttachmentRole = 'inline' | 'reference' | 'doc' | 'attach';
|
|
|
31
33
|
|
|
32
34
|
/** 正文里解析出的一条引用;position / role 在入清单时补齐。 */
|
|
33
35
|
export interface AttachmentRef {
|
|
34
|
-
kind: 'file' | 'url' | 'task' | 'ref';
|
|
36
|
+
kind: 'file' | 'url' | 'task' | 'ref' | 'mdfile';
|
|
35
37
|
id?: string;
|
|
36
38
|
name: string;
|
|
37
39
|
isImage?: boolean;
|
|
38
40
|
targetTaskId?: number;
|
|
39
41
|
targetRefId?: string;
|
|
42
|
+
targetMdfileId?: string;
|
|
40
43
|
url?: string;
|
|
41
44
|
position?: number;
|
|
42
45
|
role?: AttachmentRole;
|
|
@@ -63,6 +66,26 @@ export function refAttachmentId(refId?: unknown): string {
|
|
|
63
66
|
return 'att_ref_' + sha1(String(refId || '')).slice(0, 16);
|
|
64
67
|
}
|
|
65
68
|
|
|
69
|
+
/**
|
|
70
|
+
* 规范化 mdfile 的绝对路径:解析相对路径、压掉 `.`/`..`、统一分隔符语义
|
|
71
|
+
* (存储时保留本机原生分隔符;身份种子按 POSIX + 小写,Windows 大小写不敏感)。
|
|
72
|
+
*/
|
|
73
|
+
export function normalizeMdfilePath(input: unknown): string {
|
|
74
|
+
const raw = String(input ?? '').trim();
|
|
75
|
+
return raw ? resolve(raw) : '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 本地 md 文件的稳定 id:`att_mdfile_<sha1(规范化绝对路径).slice(0,16)>`。
|
|
80
|
+
* 与 file/url/task/ref 共用 `att_<kind>_<hash>` 命名族;同一路径幂等。
|
|
81
|
+
*/
|
|
82
|
+
export function mdfileAttachmentId(path?: unknown): string {
|
|
83
|
+
let seed = normalizeMdfilePath(path).replace(/\\/g, '/');
|
|
84
|
+
// Windows 盘符统一小写(c:/...);其余部分保留原大小写(不做 realpath)
|
|
85
|
+
if (/^[A-Z]:/.test(seed)) seed = seed.charAt(0).toLowerCase() + seed.slice(1);
|
|
86
|
+
return 'att_mdfile_' + sha1(seed).slice(0, 16);
|
|
87
|
+
}
|
|
88
|
+
|
|
66
89
|
export function metaOf(row?: any): Record<string, unknown> {
|
|
67
90
|
try {
|
|
68
91
|
const v = JSON.parse(String((row && row.meta) || '{}'));
|
|
@@ -93,14 +116,17 @@ export function parseAttachmentRefs(text?: unknown): AttachmentRef[] {
|
|
|
93
116
|
} else {
|
|
94
117
|
const task = target.match(NXKV_TASK_RE);
|
|
95
118
|
const doc = target.match(NXKV_REF_RE);
|
|
119
|
+
const md = target.match(NXKV_MDFILE_RE);
|
|
96
120
|
if (task) ref = { kind: 'task', targetTaskId: Number(task[1]), name: name || `任务 ${task[1]}` };
|
|
97
121
|
else if (doc) ref = { kind: 'ref', targetRefId: doc[1], name: name || (doc[1] ?? '') };
|
|
122
|
+
else if (md) ref = { kind: 'mdfile', targetMdfileId: md[1], name: name || (md[1] ?? '') };
|
|
98
123
|
else if (/^https?:\/\//i.test(target)) ref = { kind: 'url', url: target, name: name || target };
|
|
99
124
|
}
|
|
100
125
|
if (!ref) continue;
|
|
101
126
|
// 同一资源出现多次只保留第一条(关系表一资源一 role 一行,position 取首次出现)
|
|
102
127
|
const key = ref.id || (ref.kind === 'task' ? `task:${ref.targetTaskId}` : '')
|
|
103
128
|
|| (ref.kind === 'ref' ? `ref:${ref.targetRefId}` : '')
|
|
129
|
+
|| (ref.kind === 'mdfile' ? `mdfile:${ref.targetMdfileId}` : '')
|
|
104
130
|
|| (ref.kind === 'url' ? `url:${(ref.url ?? '').toLowerCase()}` : '');
|
|
105
131
|
if (seen.has(key)) continue;
|
|
106
132
|
seen.add(key);
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// mdfile(本地 md 文件指针)的登记、读取与校验。
|
|
2
|
+
//
|
|
3
|
+
// 语义(docs/decisions/2026-11-12-refs-body-in-db.md §5 / O7 + 2026-11 收口):
|
|
4
|
+
// · 字节留在用户磁盘原地:storage='external',库里只存 abs_path + hash;
|
|
5
|
+
// · 登记时算 sha256 指纹;读取时现读并核对指纹,不一致只报 stale,**不自动改库**;
|
|
6
|
+
// · 文件不存在只标 missing_at,不软删、不删行、不碰外部文件;
|
|
7
|
+
// · 相对链接锚点 = 登记时文件所在目录(anchor_dir),不是任何「文档根」;
|
|
8
|
+
// · `NX_KV_DOCS_DIR` 只是 mdfile 的默认附件目录,不承载文档正文——
|
|
9
|
+
// mdfile 是**唯一**允许直接读该目录(以及任意外部 md)的入口。
|
|
10
|
+
//
|
|
11
|
+
// 本模块只碰 attachments 一张表与文件系统,不做业务编排(宿主编排在各模块里)。
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { existsSync, readFileSync, statSync } from 'node:fs';
|
|
14
|
+
import { basename, dirname } from 'node:path';
|
|
15
|
+
import { mdfileAttachmentId, normalizeMdfilePath } from './ids.js';
|
|
16
|
+
import { badInput, conflict } from '../errors.js';
|
|
17
|
+
|
|
18
|
+
const MD_EXT_RE = /\.(md|markdown)$/i;
|
|
19
|
+
|
|
20
|
+
/** 外部 md 必须走文本对面,MIME 固定(不靠扩展名猜,也不影响渲染分支)。 */
|
|
21
|
+
export const MDFILE_MIME = 'text/markdown; charset=utf-8';
|
|
22
|
+
|
|
23
|
+
function sha256(buf: Buffer): string {
|
|
24
|
+
return createHash('sha256').update(buf).digest('hex');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface MdfileRecord {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
absPath: string;
|
|
31
|
+
anchorDir: string;
|
|
32
|
+
size: number;
|
|
33
|
+
hash: string;
|
|
34
|
+
missingAt: number | null;
|
|
35
|
+
lastSeenAt: number | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface RegisterMdfileInput {
|
|
39
|
+
/** 文件路径(相对路径按当前工作目录解析) */
|
|
40
|
+
path?: unknown;
|
|
41
|
+
/** 显示名(默认取文件名) */
|
|
42
|
+
name?: unknown;
|
|
43
|
+
atMs?: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 登记一个本地 md 文件(幂等:同一路径重登记只刷新指纹与时间)。
|
|
48
|
+
* 默认目录(NX_KV_DOCS_DIR)只约束「放哪」,不限制登记:
|
|
49
|
+
* 该目录下的文件同样只能经 attach 访问,所以登记一律放行。
|
|
50
|
+
* @throws 路径非法 / 非 .md / 文件不存在
|
|
51
|
+
*/
|
|
52
|
+
export function registerMdfile(conn: any, {
|
|
53
|
+
path, name = '', atMs = Date.now(),
|
|
54
|
+
}: RegisterMdfileInput = {}): MdfileRecord {
|
|
55
|
+
const absPath = normalizeMdfilePath(path);
|
|
56
|
+
if (!absPath) throw badInput('mdfile 需要文件路径');
|
|
57
|
+
if (!MD_EXT_RE.test(absPath)) {
|
|
58
|
+
throw badInput(`mdfile 只接受 .md / .markdown 文件: ${absPath}`);
|
|
59
|
+
}
|
|
60
|
+
if (!existsSync(absPath)) throw badInput(`文件不存在: ${absPath}`);
|
|
61
|
+
const st = statSync(absPath);
|
|
62
|
+
if (!st.isFile()) throw badInput(`不是普通文件: ${absPath}`);
|
|
63
|
+
|
|
64
|
+
const buf = readFileSync(absPath);
|
|
65
|
+
const hash = sha256(buf);
|
|
66
|
+
const size = buf.length;
|
|
67
|
+
const anchorDir = dirname(absPath);
|
|
68
|
+
let id = mdfileAttachmentId(absPath);
|
|
69
|
+
const displayName = String(name || '').trim() || basename(absPath);
|
|
70
|
+
|
|
71
|
+
const cur = conn.get('SELECT id, kind FROM attachments WHERE id = :id', { id });
|
|
72
|
+
if (cur && cur.kind !== 'mdfile') {
|
|
73
|
+
throw conflict(`附件 id 冲突:${id} 已被 kind=${cur.kind} 占用,无法登记为 mdfile`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const upsert = (targetId: string): void => conn.run(
|
|
77
|
+
`INSERT INTO attachments
|
|
78
|
+
(id, kind, name, local_path, url, mime, size, hash, storage, group_id,
|
|
79
|
+
target_kind, target_key, target_seq, dedupe_key, meta,
|
|
80
|
+
abs_path, anchor_dir, missing_at, last_seen_at, deleted_at, created_at, updated_at)
|
|
81
|
+
VALUES (:id, 'mdfile', :name, '', '', :mime, :size, :hash, 'external', 0,
|
|
82
|
+
'', '', NULL, '', '{}',
|
|
83
|
+
:absPath, :anchorDir, NULL, :at, NULL, :at, :at)
|
|
84
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
85
|
+
name = excluded.name,
|
|
86
|
+
mime = excluded.mime,
|
|
87
|
+
size = excluded.size,
|
|
88
|
+
hash = excluded.hash,
|
|
89
|
+
storage = 'external',
|
|
90
|
+
abs_path = excluded.abs_path,
|
|
91
|
+
anchor_dir = excluded.anchor_dir,
|
|
92
|
+
missing_at = NULL,
|
|
93
|
+
last_seen_at = excluded.last_seen_at,
|
|
94
|
+
deleted_at = NULL,
|
|
95
|
+
updated_at = excluded.updated_at`,
|
|
96
|
+
{
|
|
97
|
+
id: targetId,
|
|
98
|
+
name: displayName,
|
|
99
|
+
mime: MDFILE_MIME,
|
|
100
|
+
size,
|
|
101
|
+
hash,
|
|
102
|
+
absPath,
|
|
103
|
+
anchorDir,
|
|
104
|
+
at: atMs,
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
upsert(id);
|
|
110
|
+
} catch (e) {
|
|
111
|
+
// Windows:同一文件用不同大小写登记 → 不同 id 撞 ux_attachments_mdfile_path;
|
|
112
|
+
// 回落到已登记行,避免出现两条同文件记录。
|
|
113
|
+
const other = conn.get(
|
|
114
|
+
"SELECT id FROM attachments WHERE kind = 'mdfile' AND deleted_at IS NULL AND abs_path = :absPath COLLATE NOCASE",
|
|
115
|
+
{ absPath }
|
|
116
|
+
);
|
|
117
|
+
if (!other) throw e;
|
|
118
|
+
id = String(other.id);
|
|
119
|
+
upsert(id);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const saved = conn.get('SELECT id, name, abs_path, anchor_dir FROM attachments WHERE id = :id', { id });
|
|
123
|
+
return {
|
|
124
|
+
id,
|
|
125
|
+
name: String(saved?.name || displayName),
|
|
126
|
+
absPath: String(saved?.abs_path || absPath),
|
|
127
|
+
anchorDir: String(saved?.anchor_dir || anchorDir),
|
|
128
|
+
size,
|
|
129
|
+
hash,
|
|
130
|
+
missingAt: null,
|
|
131
|
+
lastSeenAt: atMs,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface ReadMdfileResult {
|
|
136
|
+
id: string;
|
|
137
|
+
name: string;
|
|
138
|
+
absPath: string;
|
|
139
|
+
anchorDir: string;
|
|
140
|
+
body: string;
|
|
141
|
+
size: number;
|
|
142
|
+
/** 现读指纹(文件缺失时为空) */
|
|
143
|
+
hash: string;
|
|
144
|
+
/** 文件内容与登记时的指纹不一致(不自动改库) */
|
|
145
|
+
stale: boolean;
|
|
146
|
+
/** 文件当前不存在(已标 missing_at) */
|
|
147
|
+
missing: boolean;
|
|
148
|
+
missingAt: number | null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* 读一个 mdfile 的正文并做读时校验。
|
|
153
|
+
* - 文件不存在:标 `missing_at`,返回空正文 + `missing: true`(不软删行);
|
|
154
|
+
* - 指纹不一致:返回正文 + `stale: true`(不自动改库);
|
|
155
|
+
* - 正常:刷新 `last_seen_at` 并清掉 `missing_at`。
|
|
156
|
+
* @returns null = 没有这条 mdfile(或已软删)
|
|
157
|
+
*/
|
|
158
|
+
export function readMdfile(conn: any, id: unknown, { atMs = Date.now() }: { atMs?: number } = {}): ReadMdfileResult | null {
|
|
159
|
+
const row = conn.get(
|
|
160
|
+
"SELECT * FROM attachments WHERE id = :id AND kind = 'mdfile' AND deleted_at IS NULL",
|
|
161
|
+
{ id: String(id || '') }
|
|
162
|
+
);
|
|
163
|
+
if (!row) return null;
|
|
164
|
+
|
|
165
|
+
const absPath = String(row.abs_path || '');
|
|
166
|
+
const name = String(row.name || '') || (absPath ? basename(absPath) : String(row.id));
|
|
167
|
+
const anchorDir = String(row.anchor_dir || '') || (absPath ? dirname(absPath) : '');
|
|
168
|
+
const recorded = String(row.hash || '');
|
|
169
|
+
|
|
170
|
+
if (!absPath || !existsSync(absPath)) {
|
|
171
|
+
conn.run('UPDATE attachments SET missing_at = :at WHERE id = :id', { at: atMs, id: row.id });
|
|
172
|
+
return {
|
|
173
|
+
id: String(row.id), name, absPath, anchorDir, body: '',
|
|
174
|
+
size: Number(row.size) || 0, hash: '', stale: false, missing: true, missingAt: atMs,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const buf = readFileSync(absPath);
|
|
179
|
+
const hash = sha256(buf);
|
|
180
|
+
const stale = !!recorded && recorded !== hash;
|
|
181
|
+
// 文件存在就清 missing_at:stale 只表达指纹漂移,两种状态互不排斥。
|
|
182
|
+
conn.run('UPDATE attachments SET last_seen_at = :at, missing_at = NULL WHERE id = :id', {
|
|
183
|
+
at: atMs,
|
|
184
|
+
id: row.id,
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
id: String(row.id),
|
|
188
|
+
name,
|
|
189
|
+
absPath,
|
|
190
|
+
anchorDir,
|
|
191
|
+
body: buf.toString('utf8'),
|
|
192
|
+
size: buf.length,
|
|
193
|
+
hash,
|
|
194
|
+
stale,
|
|
195
|
+
missing: false,
|
|
196
|
+
missingAt: null,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface MdfileRow extends MdfileRecord {
|
|
201
|
+
/** 台账上的失效时间(不代表此刻一定不存在;以 readMdfile 为准) */
|
|
202
|
+
missing: boolean;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** 按绝对路径找已登记的 mdfile(Windows 大小写不敏感);没有则 null。 */
|
|
206
|
+
export function findMdfileByPath(conn: any, absPath: unknown): MdfileRecord | null {
|
|
207
|
+
const abs = normalizeMdfilePath(absPath);
|
|
208
|
+
if (!abs) return null;
|
|
209
|
+
const row = conn.get(
|
|
210
|
+
`SELECT id, name, abs_path, anchor_dir, size, hash, missing_at, last_seen_at
|
|
211
|
+
FROM attachments
|
|
212
|
+
WHERE kind = 'mdfile' AND deleted_at IS NULL AND abs_path = :abs COLLATE NOCASE`,
|
|
213
|
+
{ abs }
|
|
214
|
+
);
|
|
215
|
+
if (!row) return null;
|
|
216
|
+
return {
|
|
217
|
+
id: String(row.id),
|
|
218
|
+
name: String(row.name || ''),
|
|
219
|
+
absPath: String(row.abs_path || ''),
|
|
220
|
+
anchorDir: String(row.anchor_dir || ''),
|
|
221
|
+
size: Number(row.size) || 0,
|
|
222
|
+
hash: String(row.hash || ''),
|
|
223
|
+
missingAt: row.missing_at === null || row.missing_at === undefined ? null : Number(row.missing_at),
|
|
224
|
+
lastSeenAt: row.last_seen_at === null || row.last_seen_at === undefined ? null : Number(row.last_seen_at),
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** 列出全部已登记的 mdfile(含已失标的,供巡检)。 */
|
|
229
|
+
export function listMdfiles(conn: any, { missingOnly = false }: { missingOnly?: boolean } = {}): MdfileRow[] {
|
|
230
|
+
const rows: any[] = conn.all(
|
|
231
|
+
`SELECT id, name, abs_path, anchor_dir, size, hash, missing_at, last_seen_at
|
|
232
|
+
FROM attachments
|
|
233
|
+
WHERE kind = 'mdfile' AND deleted_at IS NULL
|
|
234
|
+
ORDER BY updated_at DESC, id`
|
|
235
|
+
);
|
|
236
|
+
return rows
|
|
237
|
+
.map((r: any): MdfileRow => ({
|
|
238
|
+
id: String(r.id),
|
|
239
|
+
name: String(r.name || ''),
|
|
240
|
+
absPath: String(r.abs_path || ''),
|
|
241
|
+
anchorDir: String(r.anchor_dir || ''),
|
|
242
|
+
size: Number(r.size) || 0,
|
|
243
|
+
hash: String(r.hash || ''),
|
|
244
|
+
missingAt: r.missing_at === null || r.missing_at === undefined ? null : Number(r.missing_at),
|
|
245
|
+
lastSeenAt: r.last_seen_at === null || r.last_seen_at === undefined ? null : Number(r.last_seen_at),
|
|
246
|
+
missing: r.missing_at !== null && r.missing_at !== undefined,
|
|
247
|
+
}))
|
|
248
|
+
.filter((r) => !missingOnly || r.missing);
|
|
249
|
+
}
|
|
@@ -5,7 +5,9 @@ import { join, resolve } from 'node:path';
|
|
|
5
5
|
import { metaOf } from './ids.js';
|
|
6
6
|
|
|
7
7
|
const ATT_SELECT = `SELECT id, kind, name, local_path, url, mime, size, hash, storage, group_id,
|
|
8
|
-
target_kind, target_key, target_seq, meta,
|
|
8
|
+
target_kind, target_key, target_seq, meta,
|
|
9
|
+
abs_path, anchor_dir, missing_at, last_seen_at,
|
|
10
|
+
deleted_at, created_at, updated_at
|
|
9
11
|
FROM attachments`;
|
|
10
12
|
|
|
11
13
|
export interface AttachmentView {
|
|
@@ -24,6 +26,11 @@ export interface AttachmentView {
|
|
|
24
26
|
/** 行值可能为 NULL(SQLite),消费方各自 Number() 归一,故这里不做窄化。 */
|
|
25
27
|
targetSeq: any;
|
|
26
28
|
meta: Record<string, unknown>;
|
|
29
|
+
/** mdfile 专用:规范化绝对路径 / 相对链接锚点(其它 kind 为空) */
|
|
30
|
+
absPath: string;
|
|
31
|
+
anchorDir: string;
|
|
32
|
+
missingAt: number | null;
|
|
33
|
+
lastSeenAt: number | null;
|
|
27
34
|
deletedAt: string | null;
|
|
28
35
|
createdAt: number;
|
|
29
36
|
updatedAt: number;
|
|
@@ -51,6 +58,10 @@ function rowView(row: any): AttachmentView {
|
|
|
51
58
|
targetKey: row.target_key,
|
|
52
59
|
targetSeq: row.target_seq === null || row.target_seq === undefined ? null : Number(row.target_seq),
|
|
53
60
|
meta: metaOf(row),
|
|
61
|
+
absPath: String(row.abs_path || ''),
|
|
62
|
+
anchorDir: String(row.anchor_dir || ''),
|
|
63
|
+
missingAt: row.missing_at === null || row.missing_at === undefined ? null : Number(row.missing_at),
|
|
64
|
+
lastSeenAt: row.last_seen_at === null || row.last_seen_at === undefined ? null : Number(row.last_seen_at),
|
|
54
65
|
deletedAt: row.deleted_at,
|
|
55
66
|
createdAt: Number(row.created_at) || 0,
|
|
56
67
|
updatedAt: Number(row.updated_at) || 0,
|
|
@@ -67,6 +78,7 @@ export function listTaskAttachments(conn: any, taskId?: string | number): Linked
|
|
|
67
78
|
.all(
|
|
68
79
|
`SELECT a.id, a.kind, a.name, a.local_path, a.url, a.mime, a.size, a.hash, a.storage,
|
|
69
80
|
a.group_id, a.target_kind, a.target_key, a.target_seq, a.meta,
|
|
81
|
+
a.abs_path, a.anchor_dir, a.missing_at, a.last_seen_at,
|
|
70
82
|
a.deleted_at, a.created_at, a.updated_at, ta.role, ta.position, ta.note
|
|
71
83
|
FROM task_attach ta JOIN attachments a ON a.id = ta.attachment_id
|
|
72
84
|
WHERE ta.task_id = :id AND a.deleted_at IS NULL
|
|
@@ -93,6 +105,7 @@ export function listRefAttachments(conn: any, refId?: string | number): LinkedAt
|
|
|
93
105
|
.all(
|
|
94
106
|
`SELECT a.id, a.kind, a.name, a.local_path, a.url, a.mime, a.size, a.hash, a.storage,
|
|
95
107
|
a.group_id, a.target_kind, a.target_key, a.target_seq, a.meta,
|
|
108
|
+
a.abs_path, a.anchor_dir, a.missing_at, a.last_seen_at,
|
|
96
109
|
a.deleted_at, a.created_at, a.updated_at, ra.role, ra.position, ra.note
|
|
97
110
|
FROM ref_attach ra JOIN attachments a ON a.id = ra.attachment_id
|
|
98
111
|
WHERE ra.ref_id = :id AND a.deleted_at IS NULL
|
|
@@ -222,12 +222,15 @@ export function ensureTaskAttachment(conn: any, { taskId, atMs = Date.now(), nam
|
|
|
222
222
|
export function ensureRefAttachment(conn: any, { refId, atMs = Date.now(), name = '' }: {
|
|
223
223
|
refId?: string | null; atMs?: number; name?: string;
|
|
224
224
|
} = {}): string | null {
|
|
225
|
-
const row = conn.get('SELECT id, locator FROM refs WHERE id = :id', { id: String(refId || '') });
|
|
225
|
+
const row = conn.get('SELECT id, rel, locator, "desc" FROM refs WHERE id = :id', { id: String(refId || '') });
|
|
226
226
|
if (!row) return null;
|
|
227
|
+
// v7:ref 是 DB 实体,附件名取用户填的名字 → desc → rel 末段(去掉 .md),不暴露路径。
|
|
228
|
+
const fallback = String(row.desc || '').trim()
|
|
229
|
+
|| String(row.rel || row.locator || row.id).split('/').pop()!.replace(/\.md$/i, '');
|
|
227
230
|
return upsertDeduped(conn, {
|
|
228
231
|
id: refAttachmentId(row.id),
|
|
229
232
|
kind: 'ref',
|
|
230
|
-
name: name ||
|
|
233
|
+
name: name || fallback,
|
|
231
234
|
dedupeKey: `ref:${row.id}`,
|
|
232
235
|
targetKind: 'ref',
|
|
233
236
|
targetKey: String(row.id),
|
|
@@ -236,6 +239,28 @@ export function ensureRefAttachment(conn: any, { refId, atMs = Date.now(), name
|
|
|
236
239
|
});
|
|
237
240
|
}
|
|
238
241
|
|
|
242
|
+
/**
|
|
243
|
+
* mdfile 是「已登记的外部文件指针」:正文引用只能命中已有行,**不能凭 id 造行**
|
|
244
|
+
* (没有路径就无从读取)。未登记时返回 null,由调用方报「先 docs attach --mdfile」。
|
|
245
|
+
*/
|
|
246
|
+
export function ensureMdfileAttachment(conn: any, { id, name = '', atMs = Date.now() }: {
|
|
247
|
+
id?: string | null; name?: string; atMs?: number;
|
|
248
|
+
} = {}): string | null {
|
|
249
|
+
const attId = String(id || '').trim();
|
|
250
|
+
if (!attId) return null;
|
|
251
|
+
const row = conn.get(
|
|
252
|
+
"SELECT id, name FROM attachments WHERE id = :id AND kind = 'mdfile' AND deleted_at IS NULL",
|
|
253
|
+
{ id: attId }
|
|
254
|
+
);
|
|
255
|
+
if (!row) return null;
|
|
256
|
+
if (name && !row.name) {
|
|
257
|
+
conn.run('UPDATE attachments SET name = :name, updated_at = :at WHERE id = :id', {
|
|
258
|
+
id: attId, name: String(name), at: atMs,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
return attId;
|
|
262
|
+
}
|
|
263
|
+
|
|
239
264
|
/** 一条正文引用 → 资源 id(无法解析到真实目标的返回 null)。 */
|
|
240
265
|
export function ensureAttachment(conn: any, ref: AttachmentRef | null | undefined, { atMs = Date.now() }: {
|
|
241
266
|
atMs?: number;
|
|
@@ -256,5 +281,6 @@ export function ensureAttachment(conn: any, ref: AttachmentRef | null | undefine
|
|
|
256
281
|
return ensureTaskAttachment(conn, { taskId: ref.targetTaskId, name: ref.name, atMs });
|
|
257
282
|
}
|
|
258
283
|
if (ref.kind === 'ref') return ensureRefAttachment(conn, { refId: ref.targetRefId, name: ref.name, atMs });
|
|
284
|
+
if (ref.kind === 'mdfile') return ensureMdfileAttachment(conn, { id: ref.targetMdfileId, name: ref.name, atMs });
|
|
259
285
|
return null;
|
|
260
286
|
}
|
package/src/core/attachmentdb.ts
CHANGED
|
@@ -15,6 +15,8 @@
|
|
|
15
15
|
// 本文件只是对外入口:公共 API 全部 re-export,import 路径不变。
|
|
16
16
|
export {
|
|
17
17
|
isFileId,
|
|
18
|
+
mdfileAttachmentId,
|
|
19
|
+
normalizeMdfilePath,
|
|
18
20
|
parseAttachmentRefs,
|
|
19
21
|
refAttachmentId,
|
|
20
22
|
refIdFor,
|
|
@@ -23,11 +25,20 @@ export {
|
|
|
23
25
|
} from './attachmentdb/ids.js';
|
|
24
26
|
export {
|
|
25
27
|
ensureAttachment,
|
|
28
|
+
ensureMdfileAttachment,
|
|
26
29
|
ensureRefAttachment,
|
|
27
30
|
ensureTaskAttachment,
|
|
28
31
|
ensureUrlAttachment,
|
|
29
32
|
upsertFileAttachment,
|
|
30
33
|
} from './attachmentdb/resources.js';
|
|
34
|
+
export {
|
|
35
|
+
MDFILE_MIME,
|
|
36
|
+
listMdfiles,
|
|
37
|
+
findMdfileByPath,
|
|
38
|
+
readMdfile,
|
|
39
|
+
registerMdfile,
|
|
40
|
+
} from './attachmentdb/mdfile.js';
|
|
41
|
+
export type { MdfileRecord, MdfileRow, ReadMdfileResult } from './attachmentdb/mdfile.js';
|
|
31
42
|
export {
|
|
32
43
|
linkAttachment,
|
|
33
44
|
syncRefAttachments,
|
|
@@ -36,8 +47,24 @@ export {
|
|
|
36
47
|
updateAttachmentNote,
|
|
37
48
|
} from './attachmentdb/relations.js';
|
|
38
49
|
export {
|
|
50
|
+
getRefBody,
|
|
51
|
+
isShellRel,
|
|
52
|
+
listRefDocs,
|
|
53
|
+
noBodyReason,
|
|
54
|
+
refDocByRel,
|
|
55
|
+
refDocById,
|
|
56
|
+
refIdByRel,
|
|
57
|
+
relKindOf,
|
|
58
|
+
upsertRefBody,
|
|
59
|
+
} from './attachmentdb/bodies.js';
|
|
60
|
+
export type { RefBodyRow, RefDocRow, RelKind } from './attachmentdb/bodies.js';
|
|
61
|
+
export {
|
|
62
|
+
clearRefBindings,
|
|
39
63
|
ensureDocRef,
|
|
40
64
|
forgetDocRef,
|
|
65
|
+
markRefBound,
|
|
66
|
+
markUnboundRefsOrphan,
|
|
67
|
+
putRefBinding,
|
|
41
68
|
resolveRefId,
|
|
42
69
|
syncDocAttachmentIndex,
|
|
43
70
|
syncTaskAttachmentIndex,
|
package/src/core/config.ts
CHANGED
|
@@ -1,53 +1,20 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 本机偏好:全部进库(settings 表,`app.*` 键),不再有 config.json 凭据。
|
|
2
2
|
//
|
|
3
|
-
//
|
|
3
|
+
// 云端任务 / 登录移除后,config.json 里的 baseUrl / token / email / userId /
|
|
4
|
+
// groupId 一并废弃;旧文件即使还在也不会被读取(多余键自然忽略)。
|
|
4
5
|
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// —— 业务配置,与数据同生命周期(备份 / VACUUM INTO 跟着库走)。
|
|
8
|
-
// localtask 的 5 个键(lt.*)T03 已进库,见 localtask/repo/settings.js。
|
|
9
|
-
//
|
|
10
|
-
// 留文件(config.json,**精确 5 个字段**):
|
|
11
|
-
// baseUrl / token / email / userId / groupId
|
|
12
|
-
// —— 凭据与会话身份。**绝不进库**:库会被 `VACUUM INTO` 备份、被
|
|
13
|
-
// `db inspect` dump、被人拷走排障,凭据一旦进库就跟着这些通道扩散。
|
|
14
|
-
//
|
|
15
|
-
// `loadConfig()` / `saveConfig()` / `updateConfig()` 的签名与返回形状**完全不变**
|
|
16
|
-
// —— 内部拼「文件 5 项 + settings 表」再 normalize 一次返回,调用方零感知。
|
|
17
|
-
//
|
|
18
|
-
// 白名单纪律(#82 的教训):CREDENTIAL_KEYS / PREF_KEYS **显式枚举**每个字段。
|
|
19
|
-
// 不在白名单里的字段,两边都不写 —— 新增配置项必须同时在 normalize 与对应
|
|
20
|
-
// 白名单里登记,漏一处就是「设置了但下次启动丢掉」。
|
|
6
|
+
// 白名单纪律(#82 的教训):PREF_KEYS 显式枚举每个字段;不在白名单里的字段
|
|
7
|
+
// 不写 —— 新增配置项必须同时在 normalize 与 PREF_KEYS 里登记。
|
|
21
8
|
//
|
|
22
9
|
// settings 读取**不做长缓存**:单条 SELECT 是微秒级,缓存只会带来
|
|
23
10
|
// 「serve 改了设置、CLI 读到过期值」这类最难查的跨进程过期。
|
|
24
|
-
import fsp from 'node:fs/promises';
|
|
25
|
-
import { dirname, join } from 'node:path';
|
|
26
|
-
import { homedir } from 'node:os';
|
|
27
|
-
import { badInput } from './errors.js';
|
|
28
11
|
import { readSettingRaw, writeSettingRaw } from './db/settings.js';
|
|
29
12
|
import { tx } from './db/tx.js';
|
|
30
13
|
import { openStore } from './db/index.js';
|
|
31
14
|
import { nowMs } from './db/time.js';
|
|
32
15
|
|
|
33
|
-
export const CONFIG_PATH = join(homedir(), '.nx-kv', 'config.json');
|
|
34
|
-
|
|
35
|
-
// 测试与多实例覆盖:NX_KV_CONFIG 环境变量优先,其次是 NX_KV_STORE 所在目录
|
|
36
|
-
export function configPathFromEnv(): string {
|
|
37
|
-
return process.env.NX_KV_CONFIG || CONFIG_PATH;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export const DEFAULT_BASE_URL = 'http://192.0.2.47:8988';
|
|
41
|
-
|
|
42
|
-
// ── 显式白名单(#82 的教训:不列出的字段两边都不写)────────────────────
|
|
43
|
-
|
|
44
|
-
/** 留在 config.json 的字段(凭据 + 会话身份)。config.json 的键集合精确等于它。 */
|
|
45
|
-
export const CREDENTIAL_KEYS = Object.freeze(['baseUrl', 'token', 'email', 'userId', 'groupId'] as const);
|
|
46
|
-
|
|
47
16
|
/** 进 settings 表(`app.*` 前缀)的偏好字段。 */
|
|
48
17
|
export const PREF_KEYS = Object.freeze([
|
|
49
|
-
'autoSyncAttachments',
|
|
50
|
-
'attachmentsDir',
|
|
51
18
|
'gitRepoRoot',
|
|
52
19
|
'gitDevRoot',
|
|
53
20
|
] as const);
|
|
@@ -55,33 +22,14 @@ export const PREF_KEYS = Object.freeze([
|
|
|
55
22
|
/** settings 表里偏好字段的键名。 */
|
|
56
23
|
export const APP_SETTING_KEYS = Object.freeze(PREF_KEYS.map((k) => 'app.' + k));
|
|
57
24
|
|
|
58
|
-
/**
|
|
25
|
+
/** 归一后的本机偏好形状。 */
|
|
59
26
|
export interface AppConfig {
|
|
60
|
-
baseUrl: string;
|
|
61
|
-
token: string;
|
|
62
|
-
email: string;
|
|
63
|
-
userId: number;
|
|
64
|
-
groupId: number;
|
|
65
|
-
autoSyncAttachments: boolean;
|
|
66
|
-
attachmentsDir: string;
|
|
67
27
|
gitRepoRoot: string;
|
|
68
28
|
gitDevRoot: string;
|
|
69
29
|
}
|
|
70
30
|
|
|
71
31
|
const EMPTY = (): AppConfig => ({
|
|
72
|
-
|
|
73
|
-
token: '',
|
|
74
|
-
email: '',
|
|
75
|
-
userId: 0,
|
|
76
|
-
// 当前工作空间:0 = 不传 groupId(后端回落默认组)
|
|
77
|
-
groupId: 0,
|
|
78
|
-
// 读任务时是否自动把附件拉到本机。默认 **关**:开启意味着每次 list/get 都会
|
|
79
|
-
// 触网下载,对「只想看一眼清单」的场景是纯负担;开关在面板「清单」页或
|
|
80
|
-
// `nx-kv setting set --autoSyncAttachments`。
|
|
81
|
-
autoSyncAttachments: false,
|
|
82
|
-
// 附件本地目录覆盖;空 = 用默认 ~/.nx-kv/attachments/<groupId>/
|
|
83
|
-
attachmentsDir: '',
|
|
84
|
-
// 参考仓库(type=ref)的存储根目录。**空 = clone 拒绝执行**:用户必须显式指一个
|
|
32
|
+
// 参考仓库(type=ref)的存储根。**空 = clone 拒绝执行**:用户必须显式指一个
|
|
85
33
|
// 非 C 盘的目录(参考仓库动辄几十 MB~GB 级,不能默默堆进 homedir)。
|
|
86
34
|
gitRepoRoot: '',
|
|
87
35
|
// 导入布局时开发仓库(type=dev)的默认克隆根;空 = 导入时 dev 只登记不克隆。
|
|
@@ -89,21 +37,13 @@ const EMPTY = (): AppConfig => ({
|
|
|
89
37
|
});
|
|
90
38
|
|
|
91
39
|
let cache: AppConfig | null = null;
|
|
92
|
-
let cacheMtime = -1;
|
|
93
40
|
|
|
94
|
-
/**
|
|
41
|
+
/** 全量归一(保证形状与类型稳定)。 */
|
|
95
42
|
function normalize(data: unknown): AppConfig {
|
|
96
43
|
const base = EMPTY();
|
|
97
44
|
if (!data || typeof data !== 'object') return base;
|
|
98
45
|
const d = data as Record<string, unknown>;
|
|
99
46
|
return {
|
|
100
|
-
baseUrl: String(d.baseUrl || base.baseUrl),
|
|
101
|
-
token: String(d.token || ''),
|
|
102
|
-
email: String(d.email || ''),
|
|
103
|
-
userId: Number(d.userId) || 0,
|
|
104
|
-
groupId: Number(d.groupId) || 0,
|
|
105
|
-
autoSyncAttachments: d.autoSyncAttachments === true,
|
|
106
|
-
attachmentsDir: String(d.attachmentsDir || ''),
|
|
107
47
|
gitRepoRoot: String(d.gitRepoRoot || ''),
|
|
108
48
|
gitDevRoot: String(d.gitDevRoot || ''),
|
|
109
49
|
};
|
|
@@ -134,45 +74,15 @@ function writePrefs(next: AppConfig): void {
|
|
|
134
74
|
);
|
|
135
75
|
}
|
|
136
76
|
|
|
137
|
-
export async function loadConfig(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
const st = await fsp.stat(p);
|
|
142
|
-
if (!(cache && cacheMtime === st.mtimeMs)) {
|
|
143
|
-
cache = normalize(JSON.parse(await fsp.readFile(p, 'utf8')));
|
|
144
|
-
cacheMtime = st.mtimeMs;
|
|
145
|
-
}
|
|
146
|
-
filePart = cache;
|
|
147
|
-
} catch {
|
|
148
|
-
// 不存在或损坏:返回默认结构(首次运行;也允许外部修好后自动恢复)
|
|
149
|
-
filePart = normalize(null);
|
|
150
|
-
cache = filePart;
|
|
151
|
-
cacheMtime = -1;
|
|
152
|
-
}
|
|
153
|
-
// 偏好从 settings 表现读(迁移后文件里不再有它们),覆盖合并后归一
|
|
154
|
-
return normalize({ ...filePart, ...readPrefs() });
|
|
77
|
+
export async function loadConfig(): Promise<AppConfig> {
|
|
78
|
+
cache = normalize(readPrefs());
|
|
79
|
+
return cache;
|
|
155
80
|
}
|
|
156
81
|
|
|
157
|
-
export async function saveConfig(next: unknown
|
|
82
|
+
export async function saveConfig(next: unknown): Promise<AppConfig> {
|
|
158
83
|
const full = normalize(next);
|
|
159
|
-
// ① 文件:只写凭据白名单里的字段(**键集合精确等于 CREDENTIAL_KEYS** ——
|
|
160
|
-
// 偏好进了 settings,再写回文件会让「凭据文件」再次长出业务配置)。
|
|
161
|
-
const filePart: Record<string, unknown> = {};
|
|
162
|
-
for (const k of CREDENTIAL_KEYS) filePart[k] = full[k];
|
|
163
|
-
const p = explicitPath || configPathFromEnv();
|
|
164
|
-
await fsp.mkdir(dirname(p), { recursive: true });
|
|
165
|
-
const tmp = p + '.tmp';
|
|
166
|
-
await fsp.writeFile(tmp, JSON.stringify(filePart, null, 2), 'utf8');
|
|
167
|
-
await fsp.rename(tmp, p);
|
|
168
|
-
// ② settings:偏好白名单(值不变就不写占位 —— loadConfig 侧缺键回落默认)
|
|
169
84
|
writePrefs(full);
|
|
170
|
-
cache =
|
|
171
|
-
try {
|
|
172
|
-
cacheMtime = (await fsp.stat(p)).mtimeMs;
|
|
173
|
-
} catch {
|
|
174
|
-
cacheMtime = -1;
|
|
175
|
-
}
|
|
85
|
+
cache = full;
|
|
176
86
|
return full;
|
|
177
87
|
}
|
|
178
88
|
|
|
@@ -180,33 +90,3 @@ export async function updateConfig(patch: Partial<AppConfig>): Promise<AppConfig
|
|
|
180
90
|
const cur = await loadConfig();
|
|
181
91
|
return saveConfig({ ...cur, ...patch });
|
|
182
92
|
}
|
|
183
|
-
|
|
184
|
-
// 读配置并要求已登录。所有需要 token 的 action 都先过这里,
|
|
185
|
-
// 报错文案里的「未登录」是 agent 的失败分类锚点(与 kvcli 保持一致)。
|
|
186
|
-
export async function requireAuth(): Promise<AppConfig> {
|
|
187
|
-
const cfg = await loadConfig();
|
|
188
|
-
if (!cfg.token) {
|
|
189
|
-
throw badInput('未登录(先执行 nx-kv auth login <email>)');
|
|
190
|
-
}
|
|
191
|
-
return cfg;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// groupId 归一:0 / 负数 / 空 → undefined(不传参,由后端回落默认组)
|
|
195
|
-
export function normalizeGroupId(v: unknown): number | undefined {
|
|
196
|
-
const n = Number(v);
|
|
197
|
-
return Number.isFinite(n) && n > 0 ? n : undefined;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 解析「这次操作作用于哪个工作空间」:显式传入的 --group 优先,否则回落到本机配置
|
|
202
|
-
* 里的当前工作空间。
|
|
203
|
-
*
|
|
204
|
-
* 放进 core 是因为**不止一个模块要用同一条规则**:todo 拿它读写 KV,
|
|
205
|
-
* file 拿它选后端分组与本地附件目录。两处各写一遍的话,一旦有人只改了一处,
|
|
206
|
-
* 就会出现「从 A 组读任务、往 B 组同步附件」—— 这类错不报错,只是静默地对不上。
|
|
207
|
-
*/
|
|
208
|
-
export async function resolveGroupId(groupId: unknown): Promise<number | undefined> {
|
|
209
|
-
if (groupId !== undefined && groupId !== null && groupId !== '') return normalizeGroupId(groupId);
|
|
210
|
-
const cfg = await loadConfig();
|
|
211
|
-
return normalizeGroupId(cfg.groupId);
|
|
212
|
-
}
|