nx-kv 0.50.0 → 0.52.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 +69 -0
- package/README.md +6 -5
- package/assets/nx-kv/SKILL.md +1 -1
- package/assets/nx-kv/references/localtask-commands.md +4 -4
- package/package.json +2 -1
- package/src/core/attachmentdb/bodies.ts +44 -76
- package/src/core/attachmentdb/docs.ts +68 -96
- package/src/core/attachmentdb/resources.ts +3 -3
- package/src/core/attachmentdb.ts +3 -5
- package/src/core/contract-snapshot.ts +24 -0
- package/src/core/contract-types.ts +106 -0
- package/src/core/db/import-json/refs.ts +17 -23
- package/src/core/db/migrations/index.ts +3 -0
- package/src/core/db/migrations/v8-data.ts +497 -0
- package/src/core/db/migrations/v8.sql +12 -0
- package/src/core/db/version.ts +5 -1
- package/src/core/fstree.ts +9 -9
- package/src/modules/docs/actions/_manifest.generated.ts +0 -6
- package/src/modules/docs/actions/doc-get.ts +1 -1
- package/src/modules/docs/actions/doc-remove.ts +1 -1
- package/src/modules/docs/actions/docs-bind.ts +5 -5
- package/src/modules/docs/actions/docs-bindings.ts +20 -26
- package/src/modules/docs/actions/docs-link.ts +3 -2
- package/src/modules/docs/actions/docs-notebind.ts +5 -5
- package/src/modules/docs/attach.ts +21 -35
- package/src/modules/docs/brief.ts +85 -108
- package/src/modules/docs/docref.ts +27 -71
- package/src/modules/docs/knowledge-md.ts +1 -5
- package/src/modules/docs/knowledge.ts +155 -343
- package/src/modules/docs/links.ts +9 -17
- package/src/modules/docs/paths.ts +14 -99
- package/src/modules/docs/ref-sync.ts +37 -76
- package/src/modules/docs/refs.ts +66 -81
- package/src/modules/docs/render.ts +15 -21
- package/src/modules/docs/seed.ts +3 -146
- package/src/modules/docs/service.ts +223 -208
- package/src/modules/docs/store.ts +75 -120
- package/src/modules/localtask/actions/localtask-list.ts +39 -0
- package/src/modules/localtask/board-target.ts +10 -15
- package/src/modules/repos/actions/repo-bind-topics.ts +0 -1
- package/src/modules/repos/actions/repo-doc-add.ts +4 -6
- package/src/modules/repos/actions/repo-doc-list.ts +3 -4
- package/src/modules/repos/boards/core.ts +71 -69
- package/src/modules/repos/boards/docs.ts +39 -29
- package/src/modules/repos/cli/render.ts +4 -4
- package/src/modules/repos/repo/refs.ts +1 -69
- package/src/modules/repos/service/bind-topics.ts +1 -20
- package/src/modules/repos/service/crud.ts +38 -40
- package/src/modules/repos/transfer/diff.ts +12 -12
- package/src/modules/repos/transfer/export.ts +71 -14
- package/src/modules/repos/transfer/import.ts +42 -15
- package/src/modules/repos/transfer/layout.ts +33 -2
- package/src/modules/repos/transfer/render.ts +3 -3
- package/src/modules/system/actions/_manifest.generated.ts +3 -0
- package/src/modules/system/actions/bootstrap.ts +25 -0
- package/src/modules/system/actions/contract.ts +58 -0
- package/src/modules/tasktool/attach-resource.ts +7 -6
- package/src/modules/tasktool/attach.ts +3 -8
- package/src/modules/tasktool/doc-inline.ts +25 -54
- package/src/modules/tasktool/groupctx.ts +7 -7
- package/src/runtime/action-decorator.ts +20 -2
- package/src/runtime/contract.ts +66 -0
- package/src/runtime/dartgen.ts +347 -0
- package/src/runtime/registry.ts +17 -0
- package/src/runtime/zod-bridge.ts +101 -17
- package/src/shared/wikilink.ts +4 -6
- package/src/web/frontend/App.tsx +17 -14
- package/src/web/frontend/api/cli-tree.json +20 -36
- package/src/web/frontend/api/client.generated.ts +14 -26
- package/src/web/frontend/components/Introduction/index.tsx +3 -4
- package/src/web/frontend/components/Introduction/use-intro.ts +48 -91
- package/src/web/frontend/components/layout/breadcrumb.tsx +44 -35
- package/src/web/frontend/components/layout/crumbs.ts +60 -0
- package/src/web/frontend/components/md/attach-common.ts +5 -6
- package/src/web/frontend/components/md/attach-modal.tsx +2 -2
- package/src/web/frontend/components/md/attach-picker.tsx +2 -2
- package/src/web/frontend/components/md/doc-keys.ts +53 -0
- package/src/web/frontend/components/md/md-panes-root.tsx +9 -2
- package/src/web/frontend/components/sidebar/index.tsx +99 -9
- package/src/web/frontend/components/sidebar/model.ts +18 -4
- package/src/web/frontend/components/sidebar/row-menu.tsx +9 -40
- package/src/web/frontend/components/toast/index.tsx +46 -3
- package/src/web/frontend/modules/md/md-render.ts +1 -1
- package/src/web/frontend/page/dir/task-actions.ts +20 -2
- package/src/web/frontend/page/doc/api.ts +20 -23
- package/src/web/frontend/page/doc/bindings.tsx +18 -40
- package/src/web/frontend/page/doc/index.tsx +46 -45
- package/src/web/frontend/page/doc/use-wiki-nav.ts +1 -1
- package/src/web/frontend/page/group/index.tsx +2 -2
- package/src/web/frontend/page/refs/api.ts +3 -4
- package/src/web/frontend/page/refs/index.tsx +15 -26
- package/src/web/frontend/page/repos/api.ts +3 -3
- package/src/web/frontend/page/repos/board-admin.tsx +4 -7
- package/src/web/frontend/page/repos/doc-search-modal.tsx +68 -69
- package/src/web/frontend/page/repos/filters-bar.tsx +1 -1
- package/src/web/frontend/page/repos/index.tsx +26 -21
- package/src/web/frontend/page/repos/repo-form-modal.tsx +2 -2
- package/src/web/frontend/page/repos/repo-table.tsx +15 -14
- package/src/web/frontend/repo-data.ts +49 -0
- package/src/web/frontend/routes/doc-ref.ts +3 -3
- package/src/web/frontend/styles/repos-admin.css +1 -10
- package/src/web/frontend/styles/repos.css +9 -2
- package/src/web/frontend/styles-v2/dag.css +12 -5
- package/src/web/public/assets/app-context-B0QwHGOd.js +1 -0
- package/src/web/public/assets/{bar-C2mfoqmj.js → bar-Di7RK1fB.js} +1 -1
- package/src/web/public/assets/{board-view-aAIEbSQi.js → board-view-BJCwrtHc.js} +1 -1
- package/src/web/public/assets/{composer-launch-eTktHT_f.js → composer-launch-yGr4Ayzx.js} +1 -1
- package/src/web/public/assets/{composer-panel-C0OOIXxz.js → composer-panel-U2jaQLVQ.js} +1 -1
- package/src/web/public/assets/{dag-Gm4RcvEg.js → dag-DISpxumr.js} +1 -1
- package/src/web/public/assets/dir-BY5ibEGY.js +3 -0
- package/src/web/public/assets/doc-DHi__8Em.js +6 -0
- package/src/web/public/assets/frontmatter-b044qTyx.js +2 -0
- package/src/web/public/assets/group-mIIARpRB.js +1 -0
- package/src/web/public/assets/index-DiOQ8kPZ.js +2 -0
- package/src/web/public/assets/index-j44oS8Y3.css +1 -0
- package/src/web/public/assets/md-history-wRTMWb12.js +39 -0
- package/src/web/public/assets/{md-parse-CLyUaUto.js → md-parse-Bqb35k5w.js} +1 -1
- package/src/web/public/assets/{member-picker-XT12VRe6.js → member-picker-CQksP35E.js} +1 -1
- package/src/web/public/assets/model-lEeblPg0.js +2 -0
- package/src/web/public/assets/overview-DPDbuE5d.js +1 -0
- package/src/web/public/assets/{progress-CNam3Wuj.js → progress-BRSLsiPg.js} +2 -2
- package/src/web/public/assets/refs-CZdjVlxE.js +2 -0
- package/src/web/public/assets/repo-form-modal-dO99i73A.js +1 -0
- package/src/web/public/assets/repos-BaE9B1it.js +14 -0
- package/src/web/public/assets/route-crumb-BHZU-2HE.js +1 -0
- package/src/web/public/assets/{settings-BIzumXLD.js → settings-BX78BaLf.js} +2 -2
- package/src/web/public/assets/status-tag-DC7mdpkI.js +1 -0
- package/src/web/public/assets/task-Cxa641GX.js +11 -0
- package/src/web/public/assets/task-row-3OXcmAJb.js +1 -0
- package/src/web/public/assets/{task-utils-DjODjVdc.js → task-utils-DSZKkq5e.js} +1 -1
- package/src/web/public/assets/{templates-j8EzXHXf.js → templates-CExLUDs6.js} +1 -1
- package/src/web/public/assets/{use-rail-drag-DNfz4Gcx.js → use-rail-drag-D-17tqYm.js} +1 -1
- package/src/web/public/assets/{workspace-D_G-B3TX.js → workspace-fCTiC3R4.js} +1 -1
- package/src/web/public/index.html +18 -10
- package/src/modules/docs/actions/doc-add.ts +0 -38
- package/src/modules/docs/actions/docs-seed.ts +0 -31
- package/src/web/frontend/components/repos/desc-modal.tsx +0 -293
- package/src/web/public/assets/Introduction-CohW6Xit.js +0 -6
- package/src/web/public/assets/app-context-BgpTewyA.js +0 -1
- package/src/web/public/assets/button-xyR2UROd.js +0 -1
- package/src/web/public/assets/dir-PjuMRbWo.js +0 -3
- package/src/web/public/assets/group-D94y_Lq6.js +0 -1
- package/src/web/public/assets/index-CBJKsJU3.css +0 -1
- package/src/web/public/assets/index-Crxdmsux.js +0 -2
- package/src/web/public/assets/md-history-CU_1I6ht.js +0 -39
- package/src/web/public/assets/model-CWJ8_2dN.js +0 -2
- package/src/web/public/assets/overview-BQm8BLCG.js +0 -1
- package/src/web/public/assets/refs-ClETk1cj.js +0 -2
- package/src/web/public/assets/repos-BioGejZo.js +0 -14
- package/src/web/public/assets/route-crumb-Fc68LcQ4.js +0 -1
- package/src/web/public/assets/status-tag-CBFf_-ae.js +0 -1
- package/src/web/public/assets/task-CGmO3I2y.js +0 -11
- package/src/web/public/assets/task-row-DRBa11tf.js +0 -1
- package/src/web/public/assets/task-status-BHF-y9Aq.js +0 -2
- package/src/web/public/assets/ui-Y0aL-bGk.js +0 -1
- /package/src/web/public/assets/{Introduction-Iuxssa-b.css → doc-Iuxssa-b.css} +0 -0
- /package/src/web/public/assets/{task-status-BZrdOH46.css → frontmatter-BZrdOH46.css} +0 -0
- /package/src/web/public/assets/{button-DA4lNJeC.css → repo-form-modal-DA4lNJeC.css} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,75 @@
|
|
|
2
2
|
|
|
3
3
|
本文件记录对外可见的变更。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/)。
|
|
4
4
|
|
|
5
|
+
## [0.52.0] - 2026-11-13 契约派发:出参也进契约,Dart 客户端由生成器产出
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- **`@action` 新增可选 `output`(zod)**:跨语言客户端凭它在**编译期**知道字段类型,
|
|
10
|
+
而不是靠人猜。首批三个只读 action 已声明:`localtask.list`(视图刻意不含 `tasks`)、
|
|
11
|
+
`system.bootstrap`、`system.contract`。
|
|
12
|
+
- **契约快照 + 指纹**:`contract/contract.json` 入库,指纹 = `sha256({app, actions})` 前 16 位。
|
|
13
|
+
算法**刻意不含版本号** —— 版本升级不该让端侧报「契约漂移」。
|
|
14
|
+
- **`GET /api/contract` 与 `nx-kv contract`**:端侧 / CI 据此对账自己是不是跑在旧契约上。
|
|
15
|
+
- **Dart 客户端生成器**:`pnpm run gen:dart`(`--check` 只比对不写),产出零依赖包
|
|
16
|
+
`contract/dart`。方法名用**整条 action id** 驼峰(`localtask.list → localtaskList`),
|
|
17
|
+
信封 `{ok,data}` 的拆解只写一份。
|
|
18
|
+
- **`contract guard` workflow**:快照与源码一致 / 生成物新鲜 / 生成包能被 pub 解析并
|
|
19
|
+
`dart analyze` / 禁入库项(`.dart_tool`、`pubspec.lock`)。带 bootstrap 门控,缺件时跳过而不报红。
|
|
20
|
+
|
|
21
|
+
### Changed
|
|
22
|
+
|
|
23
|
+
- **分层**:`buildContractSnapshot` 会碰 zod,模块不能依赖 runtime 装配层 → 新增
|
|
24
|
+
`core/contract-snapshot.ts` 发布格子,registry 装配期 publish,`system.contract` 只读
|
|
25
|
+
(与 command-table 同款)。
|
|
26
|
+
|
|
27
|
+
### Fixed
|
|
28
|
+
|
|
29
|
+
- **生成的 `pubspec.yaml` 描述改为 ASCII**:pub 读 **git 源**的 pubspec 时按系统 ANSI 码页
|
|
30
|
+
解码(Windows 中文 = GBK),UTF-8 中文尾巴会吞掉行尾换行,于是 `description` 与 `version`
|
|
31
|
+
被拼成一行,报「Mapping values are not allowed here」——错误指向 `version` 的冒号,看着像缩进。
|
|
32
|
+
`path` 源按 UTF-8 读,所以本机联调永远复现不出来。
|
|
33
|
+
|
|
34
|
+
## [0.51.0] - 2026-11-13 ref 只按 id / locator 寻址,rel 概念彻底删除(v8)
|
|
35
|
+
|
|
36
|
+
### Breaking
|
|
37
|
+
|
|
38
|
+
- **删除 `refs.rel` 列与 `ux_refs_rel` 索引**:ref 的地址只剩两种——
|
|
39
|
+
`refs.id`(`ref_<16hex>`,DB 身份)与 `refs.locator`(外部文件 / URL 的实际位置)。
|
|
40
|
+
界面、CLI、模板、docId 一律不再出现文档路径,也不再有「约定路径」这层隐式寻址。
|
|
41
|
+
- **不做运行时兼容**:旧程序读不了 v8 库,也不给旧形状留回退分支;
|
|
42
|
+
升级走**存储版本号 + 迁移脚本**(`db migrate`),升级前自动 `VACUUM INTO` 备份。
|
|
43
|
+
- **导入导出格式升 v5**:`layout.refs` 带 `refId / origin / locator / desc`,
|
|
44
|
+
zip 内文档改按 `docs/<refId>.md` 落盘(不再按路径树)。
|
|
45
|
+
- **CLI 契约**:`docs add` 删除(建 ref 走 `docs ref add`);`docs bind` / `docs link` /
|
|
46
|
+
`docs notebind` / `repo doc add` 改成 `--ref <refId>` 或 `--path <外部绝对路径>`。
|
|
47
|
+
- **`docs get` 只认 refId**(`ref:<id>` / 裸 id);`path:<绝对路径>` 只用于外部目录里的裸文件。
|
|
48
|
+
- **`ref_bindings` 新增 `tag` 列**:组内附加文档的分类标签从 `ext_doc_links.tag` 搬进绑定表。
|
|
49
|
+
|
|
50
|
+
### Changed
|
|
51
|
+
|
|
52
|
+
- **归属成为绑定表唯一语义**:宿主 `ext` 里旧的 `doc` / `docLinks` / `repoDocs`
|
|
53
|
+
全部迁成 `DocRef{ref}`,docId 统一 `ref:<refId>`;`ref_bindings` 是唯一的绑定真源。
|
|
54
|
+
- **仓库 / 组的描述改为读文档集**:不再有「描述字符串」,一律是绑定文档的标题 + desc。
|
|
55
|
+
- **显示名补齐摘要兜底**:正文标题 → desc → 正文首行摘要 → locator 末段 → refId。
|
|
56
|
+
路径删除后,没有 `# 标题` 的旧文档不再一律显示「未命名文档」。
|
|
57
|
+
- **`refs.body_bytes` 跟正文同步**(原来只在迁移时写一次,列表页大小长期失真)。
|
|
58
|
+
|
|
59
|
+
### Fixed
|
|
60
|
+
|
|
61
|
+
- **`repo doc list` 的 `exists` 与其它视图口径一致**:统一为「有正文(或外部镜像)」,
|
|
62
|
+
并补 `title` / `missing` 字段;面板简介轨的标签不再退化成「主篇」。
|
|
63
|
+
- **双链索引的外部 path 精确匹配**:比较时同样走 `normalizeWikiTarget`(剥 `.md`),
|
|
64
|
+
修掉「有目标文件却解析不到」的问题。
|
|
65
|
+
|
|
66
|
+
### Notes
|
|
67
|
+
|
|
68
|
+
- **数据库自动升级 v7 → v8**:迁移决策见 `docs/decisions/versions/2026-11-13-v8.md`;
|
|
69
|
+
升级前自动备份到 `~/.nx-kv/backups/`。
|
|
70
|
+
- 验证:lint 0 error / typecheck / **919 单测** / 冒烟 / build 全通过;
|
|
71
|
+
真实库迁移后回归(`docs list` / `repo doc list` / `repo knowledge` / `docs bindings` /
|
|
72
|
+
导出导入往返)+ 浏览器实机验证:仓库页、文档检索、文档页均无路径与 `rel:` 文案。
|
|
73
|
+
|
|
5
74
|
## [0.50.0] - 2026-11-13 移除云端清单与登录,任务全面本机化(v7)
|
|
6
75
|
|
|
7
76
|
### Breaking
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
本机任务队列 + 文档库 + 仓库登记的一体化工具。一个 `npx` 命令起一个 Web 面板,同时提供同构 CLI——
|
|
4
4
|
**面板上的每个按钮,底层都是同一条 CLI 命令**,输出 `--json` 即可被任何 agent 直接消费。
|
|
5
5
|
|
|
6
|
-
所有数据都在本机 SQLite(`~/.nx-kv/nxkv.db`,schema
|
|
6
|
+
所有数据都在本机 SQLite(`~/.nx-kv/nxkv.db`,schema v8),**不需要登录、不访问任何后端**。
|
|
7
7
|
附件同样只落本机附件仓;没有云端图床、没有同步开关。
|
|
8
8
|
|
|
9
9
|
任务正文可以直接**嵌图片与文档**(标准 markdown:`` / `[文件名](fileId)`):
|
|
@@ -153,13 +153,14 @@ nx-kv file prune # 回收(DB 行 + 附件根内的字节)
|
|
|
153
153
|
|
|
154
154
|
## 文档库(refs)
|
|
155
155
|
|
|
156
|
-
ref 是 DB 实体:`refs`(id /
|
|
157
|
-
|
|
156
|
+
ref 是 DB 实体:`refs`(id / locator / desc / 归属)+ `ref_bodies`(正文)+ `ref_bindings`(挂在哪)。
|
|
157
|
+
**没有文件路径,也没有逻辑名**:地址只有 `refs.id`(`ref_<16hex>`)与 `refs.locator`
|
|
158
|
+
(外部文件 / URL 的实际位置);读取一律按 refId:
|
|
158
159
|
|
|
159
160
|
```bash
|
|
160
161
|
nx-kv docs list --json
|
|
161
|
-
nx-kv docs get --ref <refId> --json #
|
|
162
|
-
nx-kv docs bindings --
|
|
162
|
+
nx-kv docs get --ref <refId> --json # 取正文(只认 refId)
|
|
163
|
+
nx-kv docs bindings --host <repoKey> --json # 这个仓库挂了哪些文档
|
|
163
164
|
nx-kv docs bindings --group <组> --json # 这个组挂了哪些文档
|
|
164
165
|
nx-kv docs ref add --desc "一句话描述" --owner repo --repo <key> # 新建直接 ref
|
|
165
166
|
nx-kv docs ref unbind --ref <refId> # 解绑(不删文档)
|
package/assets/nx-kv/SKILL.md
CHANGED
|
@@ -134,7 +134,7 @@ nx-kv timegoal check --task task:<topicKey>:<taskId> --summary "<本轮现状>"
|
|
|
134
134
|
```bash
|
|
135
135
|
nx-kv docs get --ref <refId> --json # 取文档正文(按 refId)
|
|
136
136
|
nx-kv docs list --json # 文档列表
|
|
137
|
-
nx-kv docs bindings --
|
|
137
|
+
nx-kv docs bindings --host <repoKey> --json # 这个仓库的文档集
|
|
138
138
|
```
|
|
139
139
|
|
|
140
140
|
ref 本身**不是附件、没有本地路径**:它是一篇库内文档 + 归属(仓库 / 组)与一行 desc。
|
|
@@ -147,10 +147,10 @@ nx-kv localtask config --timeout 180 # 等待超时与交付模板(目录
|
|
|
147
147
|
|
|
148
148
|
```bash
|
|
149
149
|
nx-kv docs list --json
|
|
150
|
-
nx-kv docs get --ref <refId> --json #
|
|
151
|
-
nx-kv docs bindings --
|
|
152
|
-
nx-kv docs ref add
|
|
153
|
-
nx-kv docs ref unbind --ref <refId>
|
|
150
|
+
nx-kv docs get --ref <refId> --json # 取正文(docId 形如 ref:ref_xxx)
|
|
151
|
+
nx-kv docs bindings --host <repoKey> --json # 这个仓库的文档集
|
|
152
|
+
nx-kv docs ref add --owner repo --repo <仓库> --desc "<一行简介>" # 建直接 ref(无路径)
|
|
153
|
+
nx-kv docs ref unbind --ref <refId> --type repo --repo <repoKey> # 解绑(不删文档)
|
|
154
154
|
```
|
|
155
155
|
|
|
156
156
|
ref 是**库内文档 + 归属**,不是附件、没有本地路径;读取一律 `docs get --ref`。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nx-kv",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"description": "nx-kv — 本机任务队列(localtask)+ 文档库 + 仓库登记:serve 驱动 CLI 与 Web 面板,同源同构,附带可安装 skill 供 agent 提交 / 领取 / 阻塞接收任务",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"gen:actions": "node --import tsx scripts/gen-actions.mjs",
|
|
15
15
|
"gen:api": "node --import tsx scripts/gen-api-client.mjs",
|
|
16
|
+
"gen:dart": "node --import tsx scripts/gen-dart-client.mjs",
|
|
16
17
|
"gen": "pnpm run gen:actions && pnpm run gen:api",
|
|
17
18
|
"typecheck": "pnpm run gen && tsc -b tsconfig.node.json tsconfig.web.json",
|
|
18
19
|
"build": "pnpm run gen && tsc -b tsconfig.node.json && vite build && node scripts/link-local.mjs --auto",
|
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
// ref 正文(ref_bodies)的读写与条目分类。
|
|
2
2
|
//
|
|
3
3
|
// v6 起 `refs` 是文档身份 + 元数据,正文唯一真相在 `ref_bodies`(1:1,CASCADE)。
|
|
4
|
+
// v8 起 ref 没有 rel:地址只有 `id`(DB 身份)与 `locator`(外部实际位置)。
|
|
4
5
|
// 本模块只做三件事:
|
|
5
6
|
// 1. 正文 CRUD(upsert / get);
|
|
6
|
-
// 2.
|
|
7
|
+
// 2. 按 id / locator 取条目;
|
|
7
8
|
// 3. 条目分类:normal / legacy / external / empty —— 让调用方能区分
|
|
8
9
|
// 「正常文档」与「历史空壳 / 外部文件镜像 / 无正文」。
|
|
9
10
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
// external/<hash> 外部绝对路径 / URL 的镜像行,正文在库外,不进 ref_bodies;
|
|
13
|
-
// empty 其它没有正文的活动行(如盘上文件已删的历史条目);
|
|
14
|
-
// normal 有正文、且不是上面两类命名空间。
|
|
11
|
+
// 分类口径:origin='external' 是外部镜像;没有正文的 import 行是历史索引;
|
|
12
|
+
// 其余没有正文的活动行是 empty;有正文且非外部 = normal。
|
|
15
13
|
import { createHash } from 'node:crypto';
|
|
16
14
|
|
|
17
15
|
/** 一条 ref 条目的读路径类别。 */
|
|
18
|
-
export type
|
|
16
|
+
export type DocKind = 'normal' | 'legacy' | 'external' | 'empty';
|
|
19
17
|
|
|
20
18
|
/** 一条正文记录。 */
|
|
21
19
|
export interface RefBodyRow {
|
|
@@ -29,39 +27,33 @@ export interface RefBodyRow {
|
|
|
29
27
|
/** 列表用的一条 ref 条目(带正文是否存在)。 */
|
|
30
28
|
export interface RefDocRow {
|
|
31
29
|
id: string;
|
|
32
|
-
|
|
30
|
+
/** 外部文件 / URL 的实际位置;自有直接 ref 为空串 */
|
|
31
|
+
locator: string;
|
|
33
32
|
origin: string;
|
|
34
33
|
/** 短描述(v7):列表 / 提示词模板的快速定位标签 */
|
|
35
34
|
desc: string;
|
|
36
35
|
bodyBytes: number;
|
|
37
36
|
hasBody: boolean;
|
|
38
|
-
|
|
37
|
+
kind: DocKind;
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
const sha256 = (s: string): string => createHash('sha256').update(s, 'utf8').digest('hex');
|
|
42
41
|
|
|
43
|
-
/**
|
|
44
|
-
export function
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
return hasBody ? 'normal' : 'empty';
|
|
42
|
+
/** origin + 是否有正文 → 读路径类别。 */
|
|
43
|
+
export function docKindOf(origin: unknown, hasBody: boolean): DocKind {
|
|
44
|
+
if (String(origin || '') === 'external') return 'external';
|
|
45
|
+
if (hasBody) return 'normal';
|
|
46
|
+
return String(origin || '') === 'import' ? 'legacy' : 'empty';
|
|
49
47
|
}
|
|
50
48
|
|
|
51
49
|
/** 无正文条目的原因(给人读;docs get / 交付清单共用)。 */
|
|
52
|
-
export function noBodyReason(kind:
|
|
53
|
-
if (kind === 'legacy') return '
|
|
50
|
+
export function noBodyReason(kind: DocKind): string {
|
|
51
|
+
if (kind === 'legacy') return '历史索引条目:只有身份,没有正文';
|
|
54
52
|
if (kind === 'external') return '外部文件镜像:正文在库外,未入库';
|
|
55
53
|
if (kind === 'empty') return '条目存在但正文缺失(文件已删除或从未入库)';
|
|
56
54
|
return '';
|
|
57
55
|
}
|
|
58
56
|
|
|
59
|
-
/** rel 是否属于「不允许当正常文档展示」的命名空间。 */
|
|
60
|
-
export function isShellRel(rel: unknown): boolean {
|
|
61
|
-
const r = String(rel || '');
|
|
62
|
-
return r.startsWith('legacy/') || r.startsWith('external/');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
57
|
/** 按 refs.id 取正文。 */
|
|
66
58
|
export function getRefBody(conn: any, refId: unknown): RefBodyRow | null {
|
|
67
59
|
const row = conn.get(
|
|
@@ -89,20 +81,12 @@ export function upsertRefBody(
|
|
|
89
81
|
updated_at = excluded.updated_at`,
|
|
90
82
|
{ id: refId, body: text, n: bytes, hash, at: atMs }
|
|
91
83
|
);
|
|
84
|
+
// refs.body_bytes 是**正文大小的缓存列**(列表页读它,免得拖着正文跑)。
|
|
85
|
+
// 它必须跟着正文一起改,否则「写完再列」看到的还是旧大小。
|
|
86
|
+
conn.run('UPDATE refs SET body_bytes = :n WHERE id = :id', { id: refId, n: bytes });
|
|
92
87
|
return { bytes, hash };
|
|
93
88
|
}
|
|
94
89
|
|
|
95
|
-
/** 按 rel 找活动行的 id(rel 是逻辑名,唯一)。 */
|
|
96
|
-
export function refIdByRel(conn: any, rel: unknown): string | null {
|
|
97
|
-
const r = String(rel || '').trim();
|
|
98
|
-
if (!r) return null;
|
|
99
|
-
const row = conn.get(
|
|
100
|
-
'SELECT id FROM refs WHERE rel = :rel AND deleted_at IS NULL',
|
|
101
|
-
{ rel: r }
|
|
102
|
-
);
|
|
103
|
-
return row ? String(row.id) : null;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
90
|
/**
|
|
107
91
|
* 列出 ref 条目。
|
|
108
92
|
* @param includeEmpty=false 只给「有正文且非 legacy/external」的正常文档
|
|
@@ -111,65 +95,38 @@ export function refIdByRel(conn: any, rel: unknown): string | null {
|
|
|
111
95
|
*/
|
|
112
96
|
export function listRefDocs(conn: any, { includeEmpty = false }: { includeEmpty?: boolean } = {}): RefDocRow[] {
|
|
113
97
|
const rows: any[] = conn.all(
|
|
114
|
-
`SELECT r.id, r.
|
|
98
|
+
`SELECT r.id, r.locator, r.origin, r."desc", r.body_bytes,
|
|
115
99
|
CASE WHEN b.ref_id IS NULL THEN 0 ELSE 1 END AS has_body
|
|
116
100
|
FROM refs r
|
|
117
101
|
LEFT JOIN ref_bodies b ON b.ref_id = r.id
|
|
118
|
-
WHERE r.deleted_at IS NULL
|
|
119
|
-
ORDER BY r.
|
|
102
|
+
WHERE r.deleted_at IS NULL
|
|
103
|
+
ORDER BY r.updated_at DESC, r.id`
|
|
120
104
|
);
|
|
121
105
|
const out: RefDocRow[] = [];
|
|
122
106
|
for (const row of rows) {
|
|
123
|
-
const
|
|
107
|
+
const origin = String(row.origin || '');
|
|
124
108
|
const hasBody = Number(row.has_body) === 1;
|
|
125
|
-
const
|
|
126
|
-
if (!includeEmpty && (!hasBody ||
|
|
109
|
+
const kind = docKindOf(origin, hasBody);
|
|
110
|
+
if (!includeEmpty && (!hasBody || kind !== 'normal')) continue;
|
|
127
111
|
out.push({
|
|
128
112
|
id: String(row.id),
|
|
129
|
-
|
|
130
|
-
origin
|
|
113
|
+
locator: String(row.locator || ''),
|
|
114
|
+
origin,
|
|
131
115
|
desc: String(row.desc || ''),
|
|
132
116
|
bodyBytes: Number(row.body_bytes) || 0,
|
|
133
117
|
hasBody,
|
|
134
|
-
|
|
118
|
+
kind,
|
|
135
119
|
});
|
|
136
120
|
}
|
|
137
121
|
return out;
|
|
138
122
|
}
|
|
139
123
|
|
|
140
|
-
/** 按
|
|
141
|
-
export function
|
|
142
|
-
const r = String(rel || '').trim();
|
|
143
|
-
if (!r) return null;
|
|
144
|
-
const row = conn.get(
|
|
145
|
-
`SELECT r.id, r.rel, r.origin, r."desc", r.body_bytes,
|
|
146
|
-
CASE WHEN b.ref_id IS NULL THEN 0 ELSE 1 END AS has_body,
|
|
147
|
-
b.body
|
|
148
|
-
FROM refs r
|
|
149
|
-
LEFT JOIN ref_bodies b ON b.ref_id = r.id
|
|
150
|
-
WHERE r.rel = :rel AND r.deleted_at IS NULL`,
|
|
151
|
-
{ rel: r }
|
|
152
|
-
);
|
|
153
|
-
if (!row) return null;
|
|
154
|
-
const hasBody = Number(row.has_body) === 1;
|
|
155
|
-
return {
|
|
156
|
-
id: String(row.id),
|
|
157
|
-
rel: String(row.rel),
|
|
158
|
-
origin: String(row.origin || ''),
|
|
159
|
-
desc: String(row.desc || ''),
|
|
160
|
-
bodyBytes: Number(row.body_bytes) || 0,
|
|
161
|
-
hasBody,
|
|
162
|
-
relKind: relKindOf(row.rel, hasBody),
|
|
163
|
-
body: hasBody ? String(row.body ?? '') : null,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** 按 refs.id 取条目(docs get 的 refId 寻址 / 归属查询共用)。 */
|
|
168
|
-
export function refDocById(conn: any, id: unknown): (RefDocRow & { body: string | null; locator: string }) | null {
|
|
124
|
+
/** 按 refs.id 取条目状态(含正文);不存在返回 null。 */
|
|
125
|
+
export function refDocById(conn: any, id: unknown): (RefDocRow & { body: string | null }) | null {
|
|
169
126
|
const r = String(id || '').trim();
|
|
170
127
|
if (!r) return null;
|
|
171
128
|
const row = conn.get(
|
|
172
|
-
`SELECT r.id, r.
|
|
129
|
+
`SELECT r.id, r.locator, r.origin, r."desc", r.body_bytes,
|
|
173
130
|
CASE WHEN b.ref_id IS NULL THEN 0 ELSE 1 END AS has_body,
|
|
174
131
|
b.body
|
|
175
132
|
FROM refs r
|
|
@@ -179,15 +136,26 @@ export function refDocById(conn: any, id: unknown): (RefDocRow & { body: string
|
|
|
179
136
|
);
|
|
180
137
|
if (!row) return null;
|
|
181
138
|
const hasBody = Number(row.has_body) === 1;
|
|
139
|
+
const origin = String(row.origin || '');
|
|
182
140
|
return {
|
|
183
141
|
id: String(row.id),
|
|
184
|
-
rel: String(row.rel),
|
|
185
142
|
locator: String(row.locator || ''),
|
|
186
|
-
origin
|
|
143
|
+
origin,
|
|
187
144
|
desc: String(row.desc || ''),
|
|
188
145
|
bodyBytes: Number(row.body_bytes) || 0,
|
|
189
146
|
hasBody,
|
|
190
|
-
|
|
147
|
+
kind: docKindOf(origin, hasBody),
|
|
191
148
|
body: hasBody ? String(row.body ?? '') : null,
|
|
192
149
|
};
|
|
193
150
|
}
|
|
151
|
+
|
|
152
|
+
/** 按 locator(外部绝对路径 / URL)取条目;用于外部绑定闭包与导入核对。 */
|
|
153
|
+
export function refDocByLocator(conn: any, locator: unknown): (RefDocRow & { body: string | null }) | null {
|
|
154
|
+
const loc = String(locator || '').trim().replace(/\\/g, '/');
|
|
155
|
+
if (!loc) return null;
|
|
156
|
+
const row = conn.get(
|
|
157
|
+
'SELECT id FROM refs WHERE locator = :loc AND deleted_at IS NULL ORDER BY id LIMIT 1',
|
|
158
|
+
{ loc }
|
|
159
|
+
);
|
|
160
|
+
return row ? refDocById(conn, row.id) : null;
|
|
161
|
+
}
|
|
@@ -1,114 +1,82 @@
|
|
|
1
1
|
// 文档索引:refs 行的解析/建/删,与任务、文档两块正文的整库重扫(迁移与索引用;幂等)。
|
|
2
2
|
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
3
3
|
import type { Dirent } from 'node:fs';
|
|
4
|
-
import { createHash } from 'node:crypto';
|
|
5
4
|
import { isAbsolute, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
6
6
|
import { refIdFor } from './ids.js';
|
|
7
|
-
import { refIdByRel } from './bodies.js';
|
|
8
7
|
import { syncRefAttachments, syncTaskAttachments } from './relations.js';
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* CLI `--ref` 与面板选择器共用这一条,避免各自猜地址语法。
|
|
14
|
-
*
|
|
15
|
-
* **docId 前缀也认**(#165 二轮:面板选择器给的是 `rel:` / `path:` 形式)。
|
|
10
|
+
* 文档标识 → refs.id:只认 `refs.id`(`ref:<id>` 是面板 docId 的前缀写法)。
|
|
11
|
+
* locator 是外部文件的物理位置,不是身份——不做任何按路径回退。
|
|
16
12
|
*/
|
|
17
13
|
export function resolveRefId(conn: any, input?: string): string | null {
|
|
18
14
|
const raw = String(input || '').trim();
|
|
19
15
|
if (!raw) return null;
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const loc = raw.replace(/\\/g, '/');
|
|
24
|
-
const stripped = /^rel:|^path:/i.test(loc) ? loc.replace(/^rel:|^path:/i, '') : '';
|
|
25
|
-
const candidates = [loc, stripped].filter(Boolean);
|
|
26
|
-
|
|
27
|
-
// v6 之前(迁移链早期步骤)refs 还没有 rel 列,只能走历史 locator 入口
|
|
28
|
-
if (hasRelColumn(conn)) {
|
|
29
|
-
for (const cand of candidates) {
|
|
30
|
-
const id = refIdByRel(conn, cand);
|
|
31
|
-
if (id) return id;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
// 历史入口:locator(v4 及以前的行;外部绝对路径仍靠它命中)
|
|
35
|
-
for (const cand of candidates) {
|
|
36
|
-
const byLoc = conn.get(
|
|
37
|
-
'SELECT id FROM refs WHERE locator = :loc AND deleted_at IS NULL', { loc: cand }
|
|
38
|
-
);
|
|
39
|
-
if (byLoc) return byLoc.id;
|
|
40
|
-
}
|
|
41
|
-
const derived = refIdFor(loc);
|
|
42
|
-
const byDerived = conn.get('SELECT id FROM refs WHERE id = :id AND deleted_at IS NULL', { id: derived });
|
|
43
|
-
return byDerived ? byDerived.id : null;
|
|
16
|
+
const id = raw.replace(/^ref:/i, '');
|
|
17
|
+
const row = conn.get('SELECT id FROM refs WHERE id = :id AND deleted_at IS NULL', { id });
|
|
18
|
+
return row ? String(row.id) : null;
|
|
44
19
|
}
|
|
45
20
|
|
|
46
21
|
export interface EnsureDocRefInput {
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
/**
|
|
22
|
+
/** 已有 ref 的 id;给了且不存在时按它建行(导入用) */
|
|
23
|
+
ref?: string;
|
|
24
|
+
/** 外部绝对路径 / URL / 历史 locator */
|
|
50
25
|
locator?: string;
|
|
51
26
|
atMs?: number;
|
|
52
27
|
}
|
|
53
28
|
|
|
54
29
|
const sha1 = (s: unknown): string => createHash('sha1').update(String(s), 'utf8').digest('hex');
|
|
55
30
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* 迁移链会从旧结构逐级升级:v2-data 也会调 ensureDocRef,那时 rel 还不存在,
|
|
59
|
-
* 不能无条件查询它(否则 1→2 直接失败)。列存在性用 pragma_table_info 判断。
|
|
60
|
-
*/
|
|
61
|
-
function hasRelColumn(conn: any): boolean {
|
|
62
|
-
return !!conn.get("SELECT name FROM pragma_table_info('refs') WHERE name = 'rel'");
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** 外部绝对路径 / URL 的逻辑 rel(v6 迁移同一口径)。 */
|
|
66
|
-
function externalRelOf(input: string): string {
|
|
67
|
-
let seed = isAbsolute(input) ? resolve(input).replace(/\\/g, '/') : input;
|
|
68
|
-
if (/^[A-Z]:/.test(seed)) seed = seed.charAt(0).toLowerCase() + seed.slice(1);
|
|
69
|
-
return 'external/' + sha1(seed).slice(0, 16);
|
|
70
|
-
}
|
|
31
|
+
const isExternal = (input: string): boolean =>
|
|
32
|
+
/^https?:\/\//i.test(input) || isAbsolute(input) || /^[A-Za-z]:[\\/]/.test(input) || input.startsWith('//');
|
|
71
33
|
|
|
72
34
|
/**
|
|
73
|
-
* 确保 refs 行存在(文档保存 /
|
|
74
|
-
*
|
|
35
|
+
* 确保 refs 行存在(文档保存 / 外部绑定 / 导入共用;已有行不重置 state)。
|
|
36
|
+
* 有 locator 时先按 locator 命中;新建行的 id 由 locator 派生(外部)或随机(直接 ref)。
|
|
37
|
+
*
|
|
38
|
+
* 迁移链兼容:v2 的 apply 会调用本函数,那时 refs 还没有 origin 列(v6 才加),
|
|
39
|
+
* 所以 INSERT 的列清单按 pragma_table_info 现取——这不是运行时兼容,是迁移步骤
|
|
40
|
+
* 执行顺序的硬约束。
|
|
75
41
|
*/
|
|
76
|
-
export function ensureDocRef(conn: any, {
|
|
77
|
-
const
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const byRel = refIdByRel(conn, input);
|
|
82
|
-
if (byRel) return byRel;
|
|
42
|
+
export function ensureDocRef(conn: any, { ref, locator, atMs = Date.now() }: EnsureDocRefInput = {}): string | null {
|
|
43
|
+
const refId = String(ref || '').trim();
|
|
44
|
+
if (refId) {
|
|
45
|
+
const byId = conn.get('SELECT id FROM refs WHERE id = :id AND deleted_at IS NULL', { id: refId });
|
|
46
|
+
if (byId) return String(byId.id);
|
|
83
47
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
48
|
+
const input = String(locator ?? '').trim().replace(/\\/g, '/');
|
|
49
|
+
if (!input && !refId) return null;
|
|
50
|
+
if (input) {
|
|
51
|
+
const byLoc = conn.get(
|
|
52
|
+
'SELECT id FROM refs WHERE locator = :loc AND deleted_at IS NULL ORDER BY id LIMIT 1',
|
|
53
|
+
{ loc: input }
|
|
54
|
+
);
|
|
55
|
+
if (byLoc) return String(byLoc.id);
|
|
56
|
+
}
|
|
57
|
+
const external = input ? isExternal(input) : false;
|
|
58
|
+
let id = refId || (external ? refIdFor(input) : 'ref_' + sha1(randomUUID()).slice(0, 16));
|
|
59
|
+
if (conn.get('SELECT 1 AS x FROM refs WHERE id = :id', { id })) {
|
|
60
|
+
// id 被占用:直接 ref 换一个;locator 场景理论上不会撞(已按 locator 查过)
|
|
61
|
+
if (refId) return id;
|
|
62
|
+
id = 'ref_' + sha1(randomUUID()).slice(0, 16);
|
|
63
|
+
}
|
|
64
|
+
const hasOrigin = !!conn.get("SELECT name FROM pragma_table_info('refs') WHERE name = 'origin'");
|
|
65
|
+
if (hasOrigin) {
|
|
66
|
+
conn.run(
|
|
67
|
+
`INSERT INTO refs (id, type, locator, origin, state, version, deleted_at, created_at, updated_at)
|
|
68
|
+
VALUES (:id, 'md', :locator, :origin, 'orphan', 0, NULL, :at, :at)
|
|
69
|
+
ON CONFLICT(id) DO NOTHING`,
|
|
70
|
+
{ id, locator: input, origin: external ? 'external' : 'own', at: atMs }
|
|
71
|
+
);
|
|
72
|
+
} else {
|
|
93
73
|
conn.run(
|
|
94
74
|
`INSERT INTO refs (id, type, locator, state, version, deleted_at, created_at, updated_at)
|
|
95
75
|
VALUES (:id, 'md', :locator, 'orphan', 0, NULL, :at, :at)
|
|
96
|
-
ON CONFLICT(id) DO
|
|
97
|
-
{ id
|
|
76
|
+
ON CONFLICT(id) DO NOTHING`,
|
|
77
|
+
{ id, locator: input, at: atMs }
|
|
98
78
|
);
|
|
99
|
-
return oldId;
|
|
100
79
|
}
|
|
101
|
-
|
|
102
|
-
const external = /^https?:\/\//i.test(input)
|
|
103
|
-
|| isAbsolute(input) || /^[A-Za-z]:[\\/]/.test(input) || input.startsWith('//');
|
|
104
|
-
const targetRel = external ? externalRelOf(input) : input;
|
|
105
|
-
const id = refIdFor(external ? input : targetRel);
|
|
106
|
-
conn.run(
|
|
107
|
-
`INSERT INTO refs (id, type, rel, locator, origin, state, version, deleted_at, created_at, updated_at)
|
|
108
|
-
VALUES (:id, 'md', :rel, :locator, :origin, 'orphan', 0, NULL, :at, :at)
|
|
109
|
-
ON CONFLICT(id) DO NOTHING`,
|
|
110
|
-
{ id, rel: targetRel, locator: input, origin: external ? 'external' : 'own', at: atMs }
|
|
111
|
-
);
|
|
112
80
|
return id;
|
|
113
81
|
}
|
|
114
82
|
|
|
@@ -123,10 +91,8 @@ export function forgetDocRef(conn: any, input?: string): number {
|
|
|
123
91
|
// ─── 宿主绑定(ref_bindings)──────────────────────────────────────────
|
|
124
92
|
//
|
|
125
93
|
// ref_bindings 一行 = 「某个宿主(仓库 / 组 / 组内某仓库)的文档列表里的一项」。
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
// 显示「已绑定」(#183 实锤:4 篇已挂文档,ref_bindings 零行)。这四个
|
|
129
|
-
// 原语给 docs 域的整表重建用,写入方只允许 docs/ref-sync。
|
|
94
|
+
// v8 起宿主 ext(doc/docLinks/repoDocs)是语义,ref_bindings 是索引:
|
|
95
|
+
// 写入统一走 docs/ref-sync 的整表重建,业务读路径只读这张表。
|
|
130
96
|
|
|
131
97
|
export interface RefBindingRow {
|
|
132
98
|
repoKey?: string;
|
|
@@ -135,14 +101,24 @@ export interface RefBindingRow {
|
|
|
135
101
|
type?: string;
|
|
136
102
|
position?: number;
|
|
137
103
|
note?: string;
|
|
104
|
+
tag?: string;
|
|
138
105
|
atMs?: number;
|
|
139
106
|
}
|
|
140
107
|
|
|
141
108
|
/** 绑一条(幂等:唯一键冲突时保留原行)。 */
|
|
142
|
-
export function putRefBinding(conn: any, {
|
|
109
|
+
export function putRefBinding(conn: any, {
|
|
110
|
+
repoKey,
|
|
111
|
+
groupKey = null,
|
|
112
|
+
refId,
|
|
113
|
+
type = 'repo',
|
|
114
|
+
position = 0,
|
|
115
|
+
note = '',
|
|
116
|
+
tag = '',
|
|
117
|
+
atMs = Date.now(),
|
|
118
|
+
}: RefBindingRow = {}): void {
|
|
143
119
|
conn.run(
|
|
144
|
-
`INSERT OR IGNORE INTO ref_bindings (repo_key, group_key, ref_id, type, position, note, created_at)
|
|
145
|
-
VALUES (:repo, :group, :ref, :type, :pos, :note, :at)`,
|
|
120
|
+
`INSERT OR IGNORE INTO ref_bindings (repo_key, group_key, ref_id, type, position, note, tag, created_at)
|
|
121
|
+
VALUES (:repo, :group, :ref, :type, :pos, :note, :tag, :at)`,
|
|
146
122
|
{
|
|
147
123
|
repo: String(repoKey || ''),
|
|
148
124
|
group: groupKey == null ? null : String(groupKey),
|
|
@@ -150,19 +126,15 @@ export function putRefBinding(conn: any, { repoKey, groupKey = null, refId, type
|
|
|
150
126
|
type: String(type || 'repo'),
|
|
151
127
|
pos: Number(position) || 0,
|
|
152
128
|
note: String(note || ''),
|
|
129
|
+
tag: String(tag || ''),
|
|
153
130
|
at: atMs,
|
|
154
131
|
}
|
|
155
132
|
);
|
|
156
133
|
}
|
|
157
134
|
|
|
158
|
-
/**
|
|
159
|
-
* 清空「派生绑定」(v7):只清 rel ≠ id 的 ref 的绑定。
|
|
160
|
-
*
|
|
161
|
-
* rel = id 的行是 v7 新建的直接 ref(ref 是 DB 实体,没有路径):
|
|
162
|
-
* 它的归属由用户直接写入 ref_bindings,不来自 ext,整表重建时不能抹掉。
|
|
163
|
-
*/
|
|
135
|
+
/** 清空绑定索引(整表重建前调用;入参是语义权威时,索引全部可再生)。 */
|
|
164
136
|
export function clearRefBindings(conn: any): void {
|
|
165
|
-
conn.run('DELETE FROM ref_bindings
|
|
137
|
+
conn.run('DELETE FROM ref_bindings');
|
|
166
138
|
}
|
|
167
139
|
|
|
168
140
|
/** 绑定命中的 ref → state 清空(不再是孤儿)。 */
|
|
@@ -228,10 +200,10 @@ export function syncDocAttachmentIndex(conn: any, { docsDir, atMs = Date.now() }
|
|
|
228
200
|
continue;
|
|
229
201
|
}
|
|
230
202
|
const refId = ensureDocRef(conn, { locator, atMs });
|
|
203
|
+
if (!refId) continue;
|
|
231
204
|
n += syncRefAttachments(conn, { refId, text, atMs });
|
|
232
205
|
}
|
|
233
206
|
// 外部 .md(refs.locator 是绝对路径):不在 docs 目录里,但同样是文档库的一行。
|
|
234
|
-
// 迁移必须把它们的正文引用也落库,否则「存量文档的图片」只有正文 fileId 字符串。
|
|
235
207
|
for (const row of conn.all("SELECT id, locator FROM refs WHERE type = 'md'")) {
|
|
236
208
|
const loc = String(row.locator || '');
|
|
237
209
|
if (!isAbsolute(loc) || !existsSync(loc)) continue;
|
|
@@ -222,11 +222,11 @@ 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,
|
|
225
|
+
const row = conn.get('SELECT id, locator, "desc" FROM refs WHERE id = :id', { id: String(refId || '') });
|
|
226
226
|
if (!row) return null;
|
|
227
|
-
//
|
|
227
|
+
// v8:ref 是 DB 实体,附件名取用户填的名字 → desc → locator 末段(去掉 .md),不暴露路径。
|
|
228
228
|
const fallback = String(row.desc || '').trim()
|
|
229
|
-
|| String(row.
|
|
229
|
+
|| String(row.locator || row.id).split('/').pop()!.replace(/\.md$/i, '');
|
|
230
230
|
return upsertDeduped(conn, {
|
|
231
231
|
id: refAttachmentId(row.id),
|
|
232
232
|
kind: 'ref',
|
package/src/core/attachmentdb.ts
CHANGED
|
@@ -47,17 +47,15 @@ export {
|
|
|
47
47
|
updateAttachmentNote,
|
|
48
48
|
} from './attachmentdb/relations.js';
|
|
49
49
|
export {
|
|
50
|
+
docKindOf,
|
|
50
51
|
getRefBody,
|
|
51
|
-
isShellRel,
|
|
52
52
|
listRefDocs,
|
|
53
53
|
noBodyReason,
|
|
54
|
-
refDocByRel,
|
|
55
54
|
refDocById,
|
|
56
|
-
|
|
57
|
-
relKindOf,
|
|
55
|
+
refDocByLocator,
|
|
58
56
|
upsertRefBody,
|
|
59
57
|
} from './attachmentdb/bodies.js';
|
|
60
|
-
export type { RefBodyRow, RefDocRow,
|
|
58
|
+
export type { RefBodyRow, RefDocRow, DocKind } from './attachmentdb/bodies.js';
|
|
61
59
|
export {
|
|
62
60
|
clearRefBindings,
|
|
63
61
|
ensureDocRef,
|