@sparkelf/dsh-knowledge-base 0.4.2 → 0.4.3

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.
@@ -17,6 +17,15 @@ import { type OutlineSection, type OutlineTocEntry } from './outlineText.ts';
17
17
  import { type OutlineSaveRejection, type OutlineSectionSubmission, type OutlineSummaryKind } from './outlineGuard.ts';
18
18
  /** Prefix used for the sidecar directory when no explicit index dir is configured. */
19
19
  export declare const DEFAULT_INDEX_DIR = "/nas/kb/index";
20
+ /**
21
+ * Whether a path's bytes are plain text the index can read directly.
22
+ *
23
+ * Shared with the write paths so "index this after the write" and "the index walk
24
+ * will find this file" cannot disagree: both answer from the same extension set.
25
+ * @param relPath - the document's path.
26
+ * @returns true when {@link syncIndex} would read this file as text.
27
+ */
28
+ export declare function isIndexableTextPath(relPath: string): boolean;
20
29
  /** One search hit returned to the model. */
21
30
  /** Result of one kb_search call, including whether it had to relax. */
22
31
  export interface FtsSearchResult {
@@ -175,6 +184,29 @@ export interface FtsCallerScope {
175
184
  }
176
185
  /** The index path for one deployment. */
177
186
  export declare function indexPathFor(indexDir: string | undefined): string;
187
+ /** What the sidecar index knows about one document. */
188
+ export interface DocumentIndexStatus {
189
+ found: boolean;
190
+ docId?: number;
191
+ path?: string;
192
+ scope?: string;
193
+ chunks?: number;
194
+ indexedAt?: number;
195
+ }
196
+ /**
197
+ * Read one document's row from the sidecar index.
198
+ *
199
+ * Used by the write paths to report whether the document they just wrote is really
200
+ * searchable, and by the `index.status` route. The lookup accepts the view spelling
201
+ * too (`global/…` may be stored as the actual path, or a personal view path drops the
202
+ * owner segment), so an exact miss falls back to a suffix match before reporting
203
+ * `found: false`.
204
+ *
205
+ * @param indexPath - the deployment's SQLite index file.
206
+ * @param relPath - the document's authoritative or view-relative path.
207
+ * @returns the row, or `found: false` when nothing matches or the index is absent.
208
+ */
209
+ export declare function documentIndexStatus(indexPath: string, relPath: string): DocumentIndexStatus;
178
210
  /**
179
211
  * Refuse an index path inside the browsable/authoritative trees.
180
212
  *
@@ -19,10 +19,16 @@
19
19
  * and calls again with `confirm: true` after approval. This is the §7 trigger rule
20
20
  * (user sovereignty) expressed in the tool protocol rather than in a hook.
21
21
  *
22
+ * `kg_add` also creates a graph: `kind: 'graph'` writes a new
23
+ * `.graphs/<id>/graph.json` under the caller's writable scope, so a model can seed
24
+ * a knowledge graph instead of being stuck at "no readable graph". Creating a graph
25
+ * follows the same draft → confirm protocol, id validation, `canWrite` check and
26
+ * audit record as every other write.
27
+ *
22
28
  * @module @sparkelf/dsh-knowledge-base/graph-tools
23
29
  */
24
30
  import { type KnowledgeFileToolHost } from './knowledgeTools.ts';
25
- import type { KnowledgeGraphNode } from './graphTypes.ts';
31
+ import { type KnowledgeGraphNode } from './graphTypes.ts';
26
32
  /** The Host operations graph tools compose. Actual paths are authoritative. */
27
33
  export interface KnowledgeGraphToolHost extends Pick<KnowledgeFileToolHost, 'resolveCaller' | 'canWrite' | 'createEntry' | 'overwriteFile' | 'moveEntry' | 'statEntry' | 'refreshView' | 'appendAudit' | 'viewDir'> {
28
34
  /** Every graph the caller may read, with Host-only `actualPath`. */
@@ -337,7 +337,17 @@ export declare function classifyMount(config: Config): Promise<KnowledgeMountSta
337
337
  * @param paths - candidate relative paths; omitted means walk the authoritative root.
338
338
  * @returns The sync report, or a refusal the caller should relay.
339
339
  */
340
- export declare function syncCallerView(host: Context, config: Config, paths?: string[]): Promise<(ViewDelta & SyncReportFields & {
340
+ export declare function syncCallerView(host: Context, config: Config, paths?: string[], options?: {
341
+ /**
342
+ * Called after a file that existed only in the view was imported into the
343
+ * authoritative tree. The caller owns extraction/indexing because only it knows
344
+ * the deployment's parser settings and queue.
345
+ */
346
+ onAdopted?: (adopted: {
347
+ viewPath: string;
348
+ actualRelPath: string;
349
+ }) => void | Promise<void>;
350
+ }): Promise<(ViewDelta & SyncReportFields & {
341
351
  status: number;
342
352
  error?: never;
343
353
  }) | {
@@ -847,6 +857,42 @@ export declare function createKnowledgeEntry(config: Config, relPath: string, op
847
857
  * `no such document`.
848
858
  */
849
859
  export declare function actualRelPathForView(ctx: Context, config: Config, relPath: string, caller?: KnowledgeCaller): Promise<string>;
860
+ /** Outcome of importing a file that exists only in a caller's view. */
861
+ export interface ViewAdoptionResult {
862
+ /** Whether the bytes now live at an authoritative path. */
863
+ adopted: boolean;
864
+ /** The authoritative path, relative to `actualRoot`, when adopted. */
865
+ actualRelPath?: string;
866
+ /** Why the file was not adopted. */
867
+ reason?: string;
868
+ }
869
+ /**
870
+ * Import one view-only file into the authoritative tree.
871
+ *
872
+ * A view is a hardlink mirror of `actualRoot`; a new file placed in it by a shell
873
+ * `cp` (or an editor's save-as) has no authoritative counterpart. The view sync
874
+ * treats such a file as an orphan and unlinks it, which deletes the only copy the
875
+ * reader has. This function is the alternative: copy the bytes to the authoritative
876
+ * path the view path maps to, then replace the view entry with a hardlink to that
877
+ * copy. The next sync then sees one file, DataOps keeps resolving it, and extraction
878
+ * and indexing have a real document to work on.
879
+ *
880
+ * Refuses without a write grant, without a non-empty path, or when a DIFFERENT
881
+ * authoritative file already owns the destination name — importing must never
882
+ * overwrite a document.
883
+ *
884
+ * @param host - Host context, for the caller and the write grant.
885
+ * @param config - resolved plugin config.
886
+ * @param relPath - the view-relative path (as the reader sees it).
887
+ * @param options - caller and the already-resolved view file, when known.
888
+ * @returns whether the import happened, and where.
889
+ */
890
+ export declare function adoptViewFile(host: Context, config: Config, relPath: string, options?: {
891
+ caller?: {
892
+ userId: string;
893
+ };
894
+ fullPath?: string;
895
+ }): Promise<ViewAdoptionResult>;
850
896
  /**
851
897
  * The canonical VIEW path a model-facing file-tool path names.
852
898
  *
@@ -198,12 +198,21 @@ export interface KnowledgeFileToolHost {
198
198
  createEntry(relPath: string, options: {
199
199
  directory: boolean;
200
200
  content?: string;
201
+ bytes?: Uint8Array;
201
202
  }): Promise<{
202
203
  relPath: string;
203
204
  kind: 'file' | 'directory';
204
205
  }>;
205
206
  /** Overwrite an existing file in place, preserving its inode/view hardlink. */
206
207
  overwriteFile(relPath: string, content: string): Promise<void>;
208
+ /**
209
+ * Overwrite an existing BINARY file in place, preserving its inode/view hardlink.
210
+ *
211
+ * Separate from {@link overwriteFile} because a text body and a byte body are not
212
+ * interchangeable: decoding bytes as UTF-8 replaces every non-text byte. Omitting it
213
+ * makes a base64 overwrite fail with a clear error instead of writing a broken file.
214
+ */
215
+ overwriteFileBytes?(relPath: string, bytes: Uint8Array): Promise<void>;
207
216
  moveEntry(from: string, to: string, caller: KnowledgeToolCaller): Promise<{
208
217
  from: string;
209
218
  to: string;
@@ -216,8 +225,31 @@ export interface KnowledgeFileToolHost {
216
225
  mineruBaseUrl?: string;
217
226
  }>;
218
227
  parkWrite(caller: KnowledgeToolCaller, relPath: string, content: string): Promise<void>;
228
+ /**
229
+ * Index one document that was just written and report whether it is searchable.
230
+ *
231
+ * Called only for text formats, where indexing is the whole pipeline: a document
232
+ * with a reader (PDF/DOCX/XLSX) goes through {@link enqueueExtraction} instead,
233
+ * whose worker indexes the derived Markdown once it exists. Omitting the method
234
+ * makes a tool report `skipped` rather than claiming a write was indexed.
235
+ */
236
+ indexAfterWrite?(relPath: string): Promise<{
237
+ status: 'indexed' | 'indexing' | 'skipped' | 'failed';
238
+ docId?: number;
239
+ chunks?: number;
240
+ indexedAt?: number;
241
+ reason?: string;
242
+ }>;
219
243
  /** Enqueue one document; mirrors the manual extraction route's outcomes. */
220
- enqueueExtraction(relPath: string, caller: KnowledgeToolCaller): Promise<{
244
+ enqueueExtraction(relPath: string, caller: KnowledgeToolCaller, options?: {
245
+ /**
246
+ * Replace an existing derived `.md` sibling.
247
+ *
248
+ * Set when the source was overwritten: the sibling beside it describes the old
249
+ * bytes, and the create-only extraction path would refuse to touch it.
250
+ */
251
+ replaceSibling?: boolean;
252
+ }): Promise<{
221
253
  outcome: 'already-markdown' | 'unsupported' | 'exists' | 'queued';
222
254
  markdown?: string;
223
255
  task?: unknown;
@@ -33,6 +33,17 @@ export interface ViewDelta {
33
33
  unlinked: string[];
34
34
  /** Paths left alone because the view already matched. */
35
35
  unchanged: number;
36
+ /**
37
+ * View paths that existed only in the view and were imported into the
38
+ * authoritative tree instead of being deleted.
39
+ *
40
+ * A file can land in a view without going through this plugin (a shell `cp`, an
41
+ * editor's save-as). The view is the reader's only writable surface, so treating
42
+ * such a file as an orphan would silently destroy the user's document on the next
43
+ * sync. `adoptOrphan` decides, per path, whether it is imported; these are the
44
+ * paths where that succeeded.
45
+ */
46
+ adopted?: string[];
36
47
  /**
37
48
  * Directories removed because a revoke emptied them.
38
49
  *
@@ -107,6 +118,20 @@ export interface ViewSyncOptions extends ViewOptions {
107
118
  * a permission decision, and must not delete the reader's links.
108
119
  */
109
120
  trustResolvedSet?: boolean;
121
+ /**
122
+ * Import one view-only file into the authoritative tree before it is unlinked.
123
+ *
124
+ * Called only for a regular file the resolved set does not name, and only when the
125
+ * prune pass is otherwise about to delete it. Returning true means the file now
126
+ * lives at the authoritative path (and, ideally, that the view entry was relinked
127
+ * to it), so the view keeps it. Returning false falls through to the ordinary
128
+ * unlink, which stays the answer for a file the caller may not write or may not
129
+ * import.
130
+ *
131
+ * The callback owns permission and destination decisions: `syncView` only knows
132
+ * about files and links, not about DataOps grants or bucket layouts.
133
+ */
134
+ adoptOrphan?: (relPath: string, fullPath: string) => Promise<boolean>;
110
135
  }
111
136
  /**
112
137
  * The three top-level buckets every view exposes, in display order.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sparkelf/dsh-knowledge-base",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "DataOps-backed knowledge base for DeepSeek Harness: a left-sidebar destination, Settings configuration, and index tooling over a mounted NAS view.",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -22,6 +22,18 @@ description: 读写团队知识库里的知识图谱:主题卡、关系文档
22
22
  - 主题文件夹名可读、可改;关系引用 `index.md` 里的 `id`,不引用文件夹名,所以改名不断边。
23
23
  - 一个图谱一条 grant,覆盖主题文档与 `.kg`;写操作走 `kg_add` / `kg_set` / `kg_del`,不要用 shell 直接写文件。
24
24
 
25
+ 当前图谱模型同时支持 graph-json(新建图谱默认用它):
26
+
27
+ ```text
28
+ <scope>/.graphs/<graph-id>/graph.json # 拓扑与元数据:nodes/edges + name/scope/visibility
29
+ <scope>/.graphs/<graph-id>/links/<edge-id>.md # 可选的关系正文
30
+ <scope>/<分类>/<文档>.md # 节点正文,仍是普通知识库文档
31
+ ```
32
+
33
+ - 节点属性(type/authority/origin/reviewStatus/tags)写在节点文档的 front matter,`graph.json` 只存 `{id,path}` 与边。
34
+ - 图谱页把每个节点投影成一张卡片;空图谱也会出现在图谱树里,加上第一个节点后画布就有内容。
35
+ - `.graphs/` 是隐藏目录,不要用文件树或 shell 直接编辑;新建用 `kg_add kind=graph`,改拓扑用 `kg_add` / `kg_set` / `kg_del`。
36
+
25
37
  ## 2. 主题卡与关系文档
26
38
 
27
39
  主题卡好例子:
@@ -70,18 +82,35 @@ updated: 2026-10-31
70
82
 
71
83
  | 动作 | 做什么 |
72
84
  |---|---|
73
- | `kg_add` | `kind: topic` 新增一条线(文件夹 + 卡);`kind: link` 新增一条关系 |
85
+ | `kg_add` | `kind: graph` 新建 graph-json 图谱;`kind: topic` 新增一条线(文件夹 + 卡);`kind: link` 新增一条关系;`kind: node` / `kind: edge` 给 graph-json 图谱加节点/关系 |
74
86
  | `kg_get` | 不带 `graph`/`id` 返回全部图谱总览;带 `graph` 返回该图谱;带 `graph`+`id` 返回卡、文档、关系 |
75
- | `kg_set` | 改一个主题的 `status` / `depth` / `title` / `domain` / 摘要 / 正文 |
87
+ | `kg_set` | 改一个主题的 `status` / `depth` / `title` / `domain` / 摘要 / 正文;graph-json 图谱改节点/关系字段 |
76
88
  | `kg_del` | 把主题移到 `<graph>/_trash/`;有关联关系时需 `force: true` 一并移走,**不真删** |
77
89
 
78
- ### 五个剧本
90
+ ### 新建图谱(graph-json)
91
+
92
+ `.graphs/<id>/graph.json` 是当前图谱模型。新建图谱**不需要先有可见文件夹**,也不要手工拼路径或写 shell:
93
+
94
+ 1. `kg_add {"kind":"graph","scope":"personal","id":"nenghui-governance","name":"能汇制度图谱"}`
95
+ 返回完整 manifest 草稿(`schemaVersion/id/name/scope/visibility/nodes/edges`),此时不写盘。
96
+ 2. 把草稿给用户看;确认后同参数再加 `"confirm":true`,写入 `<scope>/.graphs/<id>/graph.json`。
97
+ 写成功后图谱页/`kg_get` 概览立刻能看到它(`format: "graph-json"`)。
98
+ 3. 继续加内容:`kg_add {"graph":"personal/.graphs/nenghui-governance","kind":"node","id":"policy-1","path":"制度/采购制度.md","title":"采购制度","type":"policy"}`,
99
+ 同样是先草稿、再 `confirm:true`。节点正文是普通 `.md` 文档,图谱只记录 `{id,path}` 拓扑。
100
+
101
+ - `scope`:`personal`(默认,写调用者自己的个人范围)、`department`(复用已有部门图谱的部门路径;首次可显式传 `department/<部门链>`)、`global`(通常需要管理员写权限)。权限由 Host 的 `canWrite` 判定,越权返回 `forbidden`。
102
+ - `id` 只能是小写字母、数字、连字符;同一范围内重复 id **或重名**都会返回 `exists`。
103
+ - `kg_add` / `kg_get` 报 `no readable graph matches ...` 时,按错误里的提示先用 `kind:"graph"` 建图;不要用 `find`/`map` 或 shell 绕开工具。
104
+ - 新建图谱的 `status` 是 `draft`;发布/审核是后续动作,不要为了“让它出现”改 manifest。
105
+
106
+ ### 六个剧本
79
107
 
80
- 1. **新建一条线**:`kg_get` 看当前图谱 → `kg_add {kind:"topic"}` 草稿 → 用户确认 → `confirm:true`。
81
- 2. **接续**:`kg_get {graph,id}` 读卡与关系 → 更新正文/摘要 → `kg_set` 草稿 → 确认。
82
- 3. **寻址**:`kg_get` 不带 id 看总览 → 按名称找 graph → `kg_get {graph,id}` 读一条线;不要用 `find`/`map` 另造工具。
83
- 4. **沉淀**:把一次会话的结论写成文档(`kb_file_write` 到主题文件夹),再用 `kg_set` 更新卡摘要;图谱不复制文档内容。
84
- 5. **复盘**:`kg_get` 总览 → 对每条线读 `kg_get {graph,id}` → 把 `status`/`depth` 调整回真实状态;`kg_del` 只移 `_trash`,不删。
108
+ 1. **新建图谱**:`kg_get` 总览里没有目标图谱(或 `kg_add` 报 `no readable graph matches`)→ `kg_add {kind:"graph"}` 草稿 → 用户确认 → `confirm:true` 建出 `.graphs/<id>/graph.json`。
109
+ 2. **新建一条线**:`kg_get` 看当前图谱 → `kg_add {kind:"topic"}` 草稿 → 用户确认 → `confirm:true`。
110
+ 3. **接续**:`kg_get {graph,id}` 读卡与关系 → 更新正文/摘要 → `kg_set` 草稿 → 确认。
111
+ 4. **寻址**:`kg_get` 不带 id 看总览 → 按名称找 graph → `kg_get {graph,id}` 读一条线;不要用 `find`/`map` 另造工具。
112
+ 5. **沉淀**:把一次会话的结论写成文档(`kb_file_write` 到主题文件夹),再用 `kg_set` 更新卡摘要;图谱不复制文档内容。
113
+ 6. **复盘**:`kg_get` 总览 → 对每条线读 `kg_get {graph,id}` → 把 `status`/`depth` 调整回真实状态;`kg_del` 只移 `_trash`,不删。
85
114
 
86
115
  ## 4. 扫历史会话(shell,不配工具)
87
116