@sparkelf/dsh-knowledge-base 0.1.35 → 0.4.2
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/README.md +34 -0
- package/lib/client.js +2516 -1378
- package/lib/client.js.map +1 -1
- package/lib/index.js +17516 -7806
- package/lib/types/client/KnowledgeMemory.d.ts +127 -0
- package/lib/types/client/KnowledgePermissionOverview.d.ts +113 -0
- package/lib/types/client/graph/OriginBadges.d.ts +55 -0
- package/lib/types/client/locales.d.ts +66 -0
- package/lib/types/client/roleSummary.d.ts +47 -0
- package/lib/types/graph.d.ts +1 -0
- package/lib/types/graphCanvas.d.ts +1 -1
- package/lib/types/graphJsonProjection.d.ts +71 -0
- package/lib/types/graphMigration.d.ts +118 -0
- package/lib/types/graphModel.d.ts +194 -0
- package/lib/types/graphPolicy.d.ts +68 -0
- package/lib/types/graphTools.d.ts +23 -4
- package/lib/types/graphTypes.d.ts +26 -2
- package/lib/types/guardianSession.d.ts +135 -0
- package/lib/types/index.d.ts +92 -0
- package/lib/types/knowledgeTools.d.ts +17 -1
- package/lib/types/memoryBindings.d.ts +119 -0
- package/lib/types/memoryContext.d.ts +83 -0
- package/lib/types/memoryGraphBootstrap.d.ts +62 -0
- package/lib/types/memoryGraphWriter.d.ts +101 -0
- package/lib/types/memoryGuardian.d.ts +123 -0
- package/lib/types/memoryGuardianTool.d.ts +56 -0
- package/lib/types/memoryPolicy.d.ts +106 -0
- package/lib/types/memoryRecordTool.d.ts +39 -0
- package/lib/types/memoryRoutes.d.ts +85 -0
- package/lib/types/memorySettings.d.ts +40 -0
- package/lib/types/memorySkillPromotion.d.ts +62 -0
- package/lib/types/memoryStore.d.ts +297 -0
- package/lib/types/memoryTools.d.ts +71 -0
- package/lib/types/memoryTriggers.d.ts +65 -0
- package/lib/types/memoryTypes.d.ts +241 -0
- package/lib/types/summaryIndex.d.ts +112 -0
- package/lib/types/summaryWriteback.d.ts +87 -0
- package/package.json +2 -2
- package/skills/memory-extract/SKILL.md +43 -0
- package/skills/memory-guardian/SKILL.md +97 -0
- package/skills/memory-sop-evaluate/SKILL.md +43 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `.graphs/` memory-graph model: manifest types, loader, validator and the
|
|
3
|
+
* dependency-free front-matter reader the rest of the memory graph modules use.
|
|
4
|
+
*
|
|
5
|
+
* A graph is metadata plus topology; it is not a second document store:
|
|
6
|
+
*
|
|
7
|
+
* ```text
|
|
8
|
+
* <scope>/.graphs/<graphId>/graph.json # manifest: nodes + edges
|
|
9
|
+
* <scope>/.graphs/<graphId>/links/<edgeId>.md # optional relation body
|
|
10
|
+
* <scope>/<node path>.md # the node body, an ordinary document
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* The split is deliberate. The manifest only references node documents by scope
|
|
14
|
+
* relative path and records edge topology; every human-facing attribute (`type`,
|
|
15
|
+
* `authority`, `origin`, `reviewStatus`, ...) lives in the node document's front
|
|
16
|
+
* matter, so moving or editing a document never duplicates its body here.
|
|
17
|
+
*
|
|
18
|
+
* Failure policy: only a missing or non-JSON `graph.json` throws
|
|
19
|
+
* ({@link GraphLoadError}). Everything a reader can still describe — a missing
|
|
20
|
+
* body, a missing relation document, a duplicate id, an edge endpoint the
|
|
21
|
+
* manifest never declared, a path that escapes the scope — becomes a
|
|
22
|
+
* {@link GraphValidationIssue} on the returned graph. That keeps one bad document
|
|
23
|
+
* from hiding an otherwise renderable graph.
|
|
24
|
+
*
|
|
25
|
+
* @module @sparkelf/dsh-knowledge-base/graph-model
|
|
26
|
+
*/
|
|
27
|
+
/** The memory meaning a node carries; independent of the document's folder. */
|
|
28
|
+
export type MemoryNodeType = 'policy' | 'fact' | 'procedure' | 'case' | 'skill' | 'decision' | 'topic';
|
|
29
|
+
/** How strongly a node overrides other memory. `redline` may only be subscribed. */
|
|
30
|
+
export type MemoryAuthority = 'canonical' | 'advisory' | 'redline';
|
|
31
|
+
/** Who authored the node body. AI content is publishable but must stay labelled. */
|
|
32
|
+
export type MemoryOrigin = 'human' | 'ai' | 'mixed';
|
|
33
|
+
/** Review state shown in the UI. */
|
|
34
|
+
export type MemoryReviewStatus = 'draft' | 'reviewed' | 'approved';
|
|
35
|
+
/** One node reference in `graph.json`; the body lives at `path`. */
|
|
36
|
+
export interface GraphManifestNode {
|
|
37
|
+
id: string;
|
|
38
|
+
path: string;
|
|
39
|
+
}
|
|
40
|
+
/** One edge in `graph.json`. `doc` is an optional scope-relative relation document. */
|
|
41
|
+
export interface GraphManifestEdge {
|
|
42
|
+
id: string;
|
|
43
|
+
from: string;
|
|
44
|
+
to: string;
|
|
45
|
+
type: string;
|
|
46
|
+
doc?: string;
|
|
47
|
+
}
|
|
48
|
+
/** The `graph.json` document as written on disk. */
|
|
49
|
+
export interface GraphManifest {
|
|
50
|
+
schemaVersion: 1;
|
|
51
|
+
id: string;
|
|
52
|
+
name: string;
|
|
53
|
+
description?: string;
|
|
54
|
+
scope: string;
|
|
55
|
+
version: number;
|
|
56
|
+
status: 'draft' | 'published' | 'archived';
|
|
57
|
+
owner?: string;
|
|
58
|
+
createdAt: string;
|
|
59
|
+
updatedAt: string;
|
|
60
|
+
publishedAt?: string;
|
|
61
|
+
tags: string[];
|
|
62
|
+
visibility: 'private' | 'department' | 'global';
|
|
63
|
+
defaultView?: {
|
|
64
|
+
layout?: string;
|
|
65
|
+
direction?: string;
|
|
66
|
+
};
|
|
67
|
+
topics?: Array<{
|
|
68
|
+
id: string;
|
|
69
|
+
title: string;
|
|
70
|
+
description?: string;
|
|
71
|
+
}>;
|
|
72
|
+
nodes: GraphManifestNode[];
|
|
73
|
+
edges: GraphManifestEdge[];
|
|
74
|
+
}
|
|
75
|
+
/** Attributes read from a node document's front matter. */
|
|
76
|
+
export interface GraphNodeAttributes {
|
|
77
|
+
id: string;
|
|
78
|
+
type: MemoryNodeType;
|
|
79
|
+
title: string;
|
|
80
|
+
authority: MemoryAuthority;
|
|
81
|
+
internalizable: boolean;
|
|
82
|
+
origin: MemoryOrigin;
|
|
83
|
+
reviewStatus: MemoryReviewStatus;
|
|
84
|
+
tags: string[];
|
|
85
|
+
sourceRefs: string[];
|
|
86
|
+
}
|
|
87
|
+
/** A node as loaded: attributes plus where its body lives and whether it was found. */
|
|
88
|
+
export interface LoadedGraphNode extends GraphNodeAttributes {
|
|
89
|
+
path: string;
|
|
90
|
+
missing: boolean;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* An edge as loaded.
|
|
94
|
+
*
|
|
95
|
+
* The manifest's edge `type` is a RELATION type (for example `depends_on`), not a
|
|
96
|
+
* {@link MemoryNodeType}. The contract describes this interface as extending
|
|
97
|
+
* `GraphNodeAttributes`; carrying a relation type in `type` is the only reading
|
|
98
|
+
* that keeps `graph.json` authoritative for topology, so the relation type is
|
|
99
|
+
* exposed here and the node-only `type` field is omitted rather than narrowed to
|
|
100
|
+
* an enum that cannot express it.
|
|
101
|
+
*/
|
|
102
|
+
export interface LoadedGraphEdge extends Omit<GraphNodeAttributes, 'type'> {
|
|
103
|
+
/** Relation type from `graph.json` (e.g. `depends_on`). */
|
|
104
|
+
type: string;
|
|
105
|
+
from: string;
|
|
106
|
+
to: string;
|
|
107
|
+
docPath?: string;
|
|
108
|
+
missing: boolean;
|
|
109
|
+
}
|
|
110
|
+
/** One non-fatal (or validation-level fatal) problem found while loading a graph. */
|
|
111
|
+
export interface GraphValidationIssue {
|
|
112
|
+
severity: 'error' | 'warning';
|
|
113
|
+
code: 'missing-node' | 'missing-edge-doc' | 'duplicate-id' | 'unknown-endpoint' | 'invalid-manifest' | 'invalid-frontmatter' | 'path-escape' | 'orphan-edge';
|
|
114
|
+
message: string;
|
|
115
|
+
ref?: string;
|
|
116
|
+
}
|
|
117
|
+
/** A fully loaded graph: manifest, resolved root, documents, edges and issues. */
|
|
118
|
+
export interface LoadedGraph {
|
|
119
|
+
manifest: GraphManifest;
|
|
120
|
+
root: string;
|
|
121
|
+
nodes: LoadedGraphNode[];
|
|
122
|
+
edges: LoadedGraphEdge[];
|
|
123
|
+
issues: GraphValidationIssue[];
|
|
124
|
+
}
|
|
125
|
+
/** Why {@link loadGraph} could not even read a manifest. */
|
|
126
|
+
export type GraphLoadErrorCode = 'manifest-missing' | 'manifest-invalid-json' | 'invalid-graph-id';
|
|
127
|
+
/**
|
|
128
|
+
* Thrown only when the graph manifest cannot be read at all.
|
|
129
|
+
*
|
|
130
|
+
* Validation problems discovered after the manifest parses are returned as
|
|
131
|
+
* issues on the graph instead of thrown.
|
|
132
|
+
*/
|
|
133
|
+
export declare class GraphLoadError extends Error {
|
|
134
|
+
readonly code: GraphLoadErrorCode;
|
|
135
|
+
readonly graphPath: string;
|
|
136
|
+
constructor(code: GraphLoadErrorCode, graphPath: string, message: string);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Read a node document's front matter.
|
|
140
|
+
*
|
|
141
|
+
* Supported syntax is the documented subset: `key: value`, quoted strings and
|
|
142
|
+
* `[a, b]` inline arrays. Optional attributes fall back to `type=fact`,
|
|
143
|
+
* `authority=advisory`, `internalizable=true`, `origin=human`,
|
|
144
|
+
* `reviewStatus=draft`, `tags=[]` and `sourceRefs=[]`. `id` and `title` are
|
|
145
|
+
* required here because this function has no manifest or file name to fall back
|
|
146
|
+
* to; {@link loadGraph} supplies both when a manifest already names the node.
|
|
147
|
+
*
|
|
148
|
+
* Invalid enum values are errors rather than defaults: silently downgrading an
|
|
149
|
+
* unknown `authority` to `advisory` could turn a mistyped redline into
|
|
150
|
+
* internalizable memory.
|
|
151
|
+
*/
|
|
152
|
+
export declare function parseNodeFrontMatter(source: string): {
|
|
153
|
+
attributes: GraphNodeAttributes | null;
|
|
154
|
+
body: string;
|
|
155
|
+
issues: GraphValidationIssue[];
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* Load one graph from `<scopeRoot>/.graphs/<graphId>/graph.json`.
|
|
159
|
+
*
|
|
160
|
+
* Node bodies resolve against `scopeRoot` (not against the graph directory), so a
|
|
161
|
+
* node path is exactly the path an ordinary browser shows. Missing documents,
|
|
162
|
+
* duplicate ids, unknown edge endpoints and unsafe paths are reported on
|
|
163
|
+
* {@link LoadedGraph.issues}; only a missing or non-JSON manifest throws
|
|
164
|
+
* {@link GraphLoadError}.
|
|
165
|
+
*
|
|
166
|
+
* @param scopeRoot - directory that owns `.graphs/`; may be relative.
|
|
167
|
+
* @param graphId - the `.graphs/<graphId>` directory name (one path segment).
|
|
168
|
+
* @returns the loaded graph, with issue severity already classified.
|
|
169
|
+
*/
|
|
170
|
+
export declare function loadGraph(scopeRoot: string, graphId: string): Promise<LoadedGraph>;
|
|
171
|
+
/**
|
|
172
|
+
* Validate an already-loaded graph and return its error-level issues.
|
|
173
|
+
*
|
|
174
|
+
* Warnings (a missing optional edge document, an orphan relation file) are not
|
|
175
|
+
* returned: the contract's validator answers "may this graph be published", and
|
|
176
|
+
* those problems never make the topology unusable. Checks are recomputed from
|
|
177
|
+
* the loaded data so a manually constructed graph is validated as thoroughly as
|
|
178
|
+
* one returned by {@link loadGraph}.
|
|
179
|
+
*/
|
|
180
|
+
export declare function validateGraph(graph: LoadedGraph): GraphValidationIssue[];
|
|
181
|
+
/**
|
|
182
|
+
* List the graphs under `<scopeRoot>/.graphs/`, sorted by directory id.
|
|
183
|
+
*
|
|
184
|
+
* `id` is the `.graphs/<id>` directory name, which is what {@link loadGraph}
|
|
185
|
+
* takes; `manifest.id` is normally identical. Directories that contain no
|
|
186
|
+
* readable `graph.json` are skipped rather than invented: a caller that knows a
|
|
187
|
+
* graph should exist can call {@link loadGraph} and receive the precise
|
|
188
|
+
* {@link GraphLoadError}.
|
|
189
|
+
*/
|
|
190
|
+
export declare function listGraphs(scopeRoot: string): Promise<Array<{
|
|
191
|
+
id: string;
|
|
192
|
+
manifest: GraphManifest;
|
|
193
|
+
issueCount: number;
|
|
194
|
+
}>>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Visibility and management policy for memory graphs.
|
|
3
|
+
*
|
|
4
|
+
* This module is the pure decision layer only: callers resolve the viewer's
|
|
5
|
+
* department subtree (`visibleDepartmentCodes`) and manageable subtree from
|
|
6
|
+
* DataOps, then ask one graph at a time. It never reads configuration, never
|
|
7
|
+
* touches the file system, and never invents a broader scope than it was handed.
|
|
8
|
+
*
|
|
9
|
+
* The rules come from the memory-graph design:
|
|
10
|
+
*
|
|
11
|
+
* - `private` belongs to its owner; administrators and super administrators do
|
|
12
|
+
* not see it by default (breaking the glass is a separate, audited action);
|
|
13
|
+
* - `global` is readable by any authenticated viewer;
|
|
14
|
+
* - `department` requires the graph's department code in the viewer's visible
|
|
15
|
+
* subtree. Department administrators understand their subtree through
|
|
16
|
+
* `manageableDepartmentCodes`; a plain member has neither the role nor the
|
|
17
|
+
* list.
|
|
18
|
+
*
|
|
19
|
+
* @module @sparkelf/dsh-knowledge-base/graph-policy
|
|
20
|
+
*/
|
|
21
|
+
/** The authenticated caller asking about a graph. */
|
|
22
|
+
export interface GraphViewer {
|
|
23
|
+
userId: string;
|
|
24
|
+
isAdmin: boolean;
|
|
25
|
+
isSuperAdmin: boolean;
|
|
26
|
+
departmentCode?: string;
|
|
27
|
+
/** Visible department subtree (including the caller's own); members see only their own. */
|
|
28
|
+
visibleDepartmentCodes: string[];
|
|
29
|
+
/** Department subtree the caller may manage; empty for ordinary members. */
|
|
30
|
+
manageableDepartmentCodes?: string[];
|
|
31
|
+
}
|
|
32
|
+
/** The scope a graph lives in, as resolved from its manifest. */
|
|
33
|
+
export interface GraphScopeDescriptor {
|
|
34
|
+
visibility: 'private' | 'department' | 'global';
|
|
35
|
+
ownerId?: string;
|
|
36
|
+
departmentCode?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether the viewer may read the graph.
|
|
40
|
+
*
|
|
41
|
+
* Private graphs are owner-only before any role check runs, so neither an
|
|
42
|
+
* administrator nor a super administrator can discover someone else's private
|
|
43
|
+
* graph through this function. Department access is exact membership: the caller
|
|
44
|
+
* is responsible for expanding `visibleDepartmentCodes` to the subtree, which
|
|
45
|
+
* keeps hierarchy logic in the one place that already knows it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function canViewGraph(viewer: GraphViewer, scope: GraphScopeDescriptor): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Whether the viewer may manage the graph (edit metadata, publish, change
|
|
50
|
+
* authority).
|
|
51
|
+
*
|
|
52
|
+
* Management always implies visibility. Private graphs stay owner-only. Global
|
|
53
|
+
* graphs need an administrator role. Department graphs need the department in
|
|
54
|
+
* `manageableDepartmentCodes`; a super administrator covers every department,
|
|
55
|
+
* and the two lists in {@link GraphViewer} are trusted as already-resolved
|
|
56
|
+
* authorization input rather than re-derived here.
|
|
57
|
+
*/
|
|
58
|
+
export declare function canManageGraph(viewer: GraphViewer, scope: GraphScopeDescriptor): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Filter graphs down to the ones the viewer may read, preserving input order.
|
|
61
|
+
*
|
|
62
|
+
* Order is preserved on purpose: the caller already sorted for display, and this
|
|
63
|
+
* function must not introduce a second ordering rule. A denial is a removal, not
|
|
64
|
+
* an error; callers never receive a name they may not see.
|
|
65
|
+
*/
|
|
66
|
+
export declare function filterVisibleGraphs<T extends {
|
|
67
|
+
scope: GraphScopeDescriptor;
|
|
68
|
+
}>(viewer: GraphViewer, graphs: T[]): T[];
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Model-facing knowledge-graph tools (`kg_add` / `kg_get` / `kg_set` / `kg_del`).
|
|
3
3
|
*
|
|
4
|
-
* The four verbs mirror `kg.py`'s CLI.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* every write, `readAuthorized` for every read, audit entries recorded beside the
|
|
4
|
+
* The four verbs mirror `kg.py`'s CLI. Two storage layouts are addressed through
|
|
5
|
+
* the same Host seam the HTTP routes use: one grant per graph, `canWrite` before
|
|
6
|
+
* every write, `readText` for every read, audit entries recorded beside the
|
|
8
7
|
* caller's view, and `_trash` instead of deletion.
|
|
9
8
|
*
|
|
9
|
+
* - **Legacy folder graphs** (`<scope>/知识图谱/<graph>/<topic>/index.md`, relations
|
|
10
|
+
* under `.kg/links/`) keep their original behaviour untouched.
|
|
11
|
+
* - **`.graphs/<id>/graph.json` graphs** (`format: 'graph-json'`) carry topology in
|
|
12
|
+
* the manifest and attributes in each node document's front matter. The tools
|
|
13
|
+
* read both, refuse duplicate ids / unknown endpoints / unsafe paths, and write
|
|
14
|
+
* the manifest through a temp-file rename; after every write the loaded graph is
|
|
15
|
+
* re-validated and the change is rolled back when the validator reports an error.
|
|
16
|
+
*
|
|
10
17
|
* Writes are two-step by design. A call without `confirm: true` returns the exact
|
|
11
18
|
* draft it would write and changes nothing; the model shows that draft to the user
|
|
12
19
|
* and calls again with `confirm: true` after approval. This is the §7 trigger rule
|
|
@@ -22,6 +29,18 @@ export interface KnowledgeGraphToolHost extends Pick<KnowledgeFileToolHost, 'res
|
|
|
22
29
|
listGraphs(): Promise<KnowledgeGraphNode[]>;
|
|
23
30
|
/** Read one view-relative document through the permission-checked route. */
|
|
24
31
|
readText(viewPath: string): Promise<string>;
|
|
32
|
+
/**
|
|
33
|
+
* Read one knowledge-root-relative path that may be hidden from the view tree
|
|
34
|
+
* (`.graphs/<id>/graph.json`, relation docs). The Host applies the same path
|
|
35
|
+
* permission check as {@link readText}; absent means the Host cannot read
|
|
36
|
+
* hidden paths and the tool falls back to the view spelling.
|
|
37
|
+
*/
|
|
38
|
+
readActual?(relPath: string): Promise<string>;
|
|
39
|
+
/** Stat one knowledge-root-relative path, for the hidden `.graphs` store. */
|
|
40
|
+
statActual?(relPath: string): Promise<{
|
|
41
|
+
kind: 'file' | 'directory';
|
|
42
|
+
size?: number;
|
|
43
|
+
} | undefined>;
|
|
25
44
|
}
|
|
26
45
|
/**
|
|
27
46
|
* Build the four graph tools.
|
|
@@ -21,11 +21,17 @@ export declare const KNOWLEDGE_GRAPH_FOLDER = "\u77E5\u8BC6\u56FE\u8C31";
|
|
|
21
21
|
export declare const KNOWLEDGE_GRAPH_META_FOLDER = ".kg";
|
|
22
22
|
/** The trash folder `kg_del` moves topics into; never a topic itself. */
|
|
23
23
|
export declare const KNOWLEDGE_GRAPH_TRASH_FOLDER = "_trash";
|
|
24
|
+
/** How a document or node was produced. Absent on content that predates the field. */
|
|
25
|
+
export type KnowledgeGraphOrigin = 'human' | 'ai' | 'mixed';
|
|
26
|
+
/** Review lifecycle. Absent means the scanner did not report one. */
|
|
27
|
+
export type KnowledgeGraphReviewStatus = 'draft' | 'reviewed' | 'approved';
|
|
28
|
+
/** Authority level. Absent means the scanner did not report one. */
|
|
29
|
+
export type KnowledgeGraphAuthority = 'canonical' | 'advisory' | 'redline';
|
|
24
30
|
/** One document inside a topic folder, as the detail panel lists it. */
|
|
25
31
|
export interface KnowledgeGraphDocument {
|
|
26
32
|
/** Human title: the document's first H1, else the file name without extension. */
|
|
27
33
|
title: string;
|
|
28
|
-
/**
|
|
34
|
+
/** Path relative to the topic folder, e.g. `岗位/张三.md`. */
|
|
29
35
|
file: string;
|
|
30
36
|
/** View-relative path, for opening the document from a future write surface. */
|
|
31
37
|
path: string;
|
|
@@ -33,6 +39,12 @@ export interface KnowledgeGraphDocument {
|
|
|
33
39
|
date?: string;
|
|
34
40
|
/** First paragraph after the H1, when the file has one. */
|
|
35
41
|
summary?: string;
|
|
42
|
+
/** Optional provenance: `ai`/`mixed` renders the [AI 生成] badge. */
|
|
43
|
+
origin?: KnowledgeGraphOrigin;
|
|
44
|
+
/** Optional review lifecycle: `reviewed`/`approved` renders [已审核]. */
|
|
45
|
+
reviewStatus?: KnowledgeGraphReviewStatus;
|
|
46
|
+
/** Optional authority: `redline` renders [红线] and only permits subscription. */
|
|
47
|
+
authority?: KnowledgeGraphAuthority;
|
|
36
48
|
}
|
|
37
49
|
/** One topic: a visible folder whose `index.md` is its card. */
|
|
38
50
|
export interface KnowledgeGraphTopic {
|
|
@@ -56,8 +68,14 @@ export interface KnowledgeGraphTopic {
|
|
|
56
68
|
folder: string;
|
|
57
69
|
/** View-relative topic folder path. */
|
|
58
70
|
path: string;
|
|
59
|
-
/** Documents
|
|
71
|
+
/** Documents below the topic folder, including nested document folders. */
|
|
60
72
|
documents: KnowledgeGraphDocument[];
|
|
73
|
+
/** Optional provenance for the topic card; mirrors the node-level field. */
|
|
74
|
+
origin?: KnowledgeGraphOrigin;
|
|
75
|
+
/** Optional review lifecycle for the topic card. */
|
|
76
|
+
reviewStatus?: KnowledgeGraphReviewStatus;
|
|
77
|
+
/** Optional authority for the topic card; `redline` is subscribe-only. */
|
|
78
|
+
authority?: KnowledgeGraphAuthority;
|
|
61
79
|
}
|
|
62
80
|
/** One relation document under `<graph>/.kg/links/`. */
|
|
63
81
|
export interface KnowledgeGraphLink {
|
|
@@ -96,6 +114,12 @@ export interface KnowledgeGraphNode {
|
|
|
96
114
|
links: number;
|
|
97
115
|
documents: number;
|
|
98
116
|
};
|
|
117
|
+
/** Optional provenance for the graph row; absent on graphs scanned before it existed. */
|
|
118
|
+
origin?: KnowledgeGraphOrigin;
|
|
119
|
+
/** Optional review lifecycle for the graph row. */
|
|
120
|
+
reviewStatus?: KnowledgeGraphReviewStatus;
|
|
121
|
+
/** Optional authority for the graph row; `redline` is subscribe-only. */
|
|
122
|
+
authority?: KnowledgeGraphAuthority;
|
|
99
123
|
/**
|
|
100
124
|
* Authoritative path relative to the knowledge root, used by Host-side graph
|
|
101
125
|
* tools. Stripped before the tree is sent to the browser.
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/** Why a guardian message was produced. */
|
|
2
|
+
export type GuardianReason = 'idle' | 'compaction' | 'dream' | 'manual';
|
|
3
|
+
/** One message to deliver to the user's guardian session. */
|
|
4
|
+
export interface GuardianMessage {
|
|
5
|
+
reason: GuardianReason;
|
|
6
|
+
/** The ordinary session that triggered this message (idle/compaction). */
|
|
7
|
+
sessionId?: string;
|
|
8
|
+
/** The compaction transaction that triggered this message. */
|
|
9
|
+
compactionId?: string;
|
|
10
|
+
/** Short free-form context; never a document body. */
|
|
11
|
+
note?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Durable guardian identity. */
|
|
14
|
+
export interface GuardianSessionState {
|
|
15
|
+
version: 1;
|
|
16
|
+
userId: string;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
/** Display-only title; never used as the identity. */
|
|
19
|
+
title: string;
|
|
20
|
+
createdAt: string;
|
|
21
|
+
updatedAt: string;
|
|
22
|
+
}
|
|
23
|
+
/** The live-agent facts this module needs. Structural on purpose. */
|
|
24
|
+
export interface GuardianAgentRef {
|
|
25
|
+
readonly id: string;
|
|
26
|
+
/** `running` means a further followup is queued behind the current turn. */
|
|
27
|
+
readonly status?: 'idle' | 'running';
|
|
28
|
+
}
|
|
29
|
+
/** Result of one text delivery. */
|
|
30
|
+
export interface GuardianDelivery {
|
|
31
|
+
delivered?: boolean;
|
|
32
|
+
queued?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/** Inputs for a standard-mode session creation. */
|
|
35
|
+
export interface CreateGuardianAgentInput {
|
|
36
|
+
sessionId: string;
|
|
37
|
+
/** Absolute working directory required by the DSH session boundary. */
|
|
38
|
+
cwd: string;
|
|
39
|
+
title: string;
|
|
40
|
+
}
|
|
41
|
+
/** Host seam implemented by the plugin wiring against the real DSH services. */
|
|
42
|
+
export interface GuardianSessionHost {
|
|
43
|
+
/** `<actualRoot>/.memory/users` or the configured `memoryUsersRoot`. */
|
|
44
|
+
usersRoot: string;
|
|
45
|
+
/** Absolute session cwd; defaults to the host process cwd. */
|
|
46
|
+
cwd?: string;
|
|
47
|
+
/** `ctx.agents.get(sessionId)`. */
|
|
48
|
+
getAgent(sessionId: string): GuardianAgentRef | undefined;
|
|
49
|
+
/** `ctx.agentLoop.createAgent(ctx, { sessionId, meta: { cwd } })` with NO agentPreset. */
|
|
50
|
+
createAgent(input: CreateGuardianAgentInput): Promise<GuardianAgentRef>;
|
|
51
|
+
/** `ctx.agentLoop.resume(ctx, { resumeSessionId })`. */
|
|
52
|
+
resumeAgent(input: {
|
|
53
|
+
sessionId: string;
|
|
54
|
+
cwd: string;
|
|
55
|
+
}): Promise<GuardianAgentRef>;
|
|
56
|
+
/** Build a user message and call `agent.followup(...)`; report whether it queued. */
|
|
57
|
+
deliverText(agent: GuardianAgentRef, text: string): Promise<GuardianDelivery> | GuardianDelivery;
|
|
58
|
+
/**
|
|
59
|
+
* Optional title lookup used only when guardian.json is lost. The design says:
|
|
60
|
+
* find a unique title match before creating another guardian.
|
|
61
|
+
*/
|
|
62
|
+
findByTitle?(title: string): Promise<string | undefined>;
|
|
63
|
+
/** Test seam: deterministic session id. Defaults to `session-<uuid>`. */
|
|
64
|
+
newSessionId?(): string;
|
|
65
|
+
/** Optional explicit rename; implementation may append `session/title`. */
|
|
66
|
+
setTitle?(agent: GuardianAgentRef, title: string): Promise<void> | void;
|
|
67
|
+
}
|
|
68
|
+
/** A resolved (possibly just-created) guardian. */
|
|
69
|
+
export interface ResolvedGuardian {
|
|
70
|
+
state: GuardianSessionState;
|
|
71
|
+
agent: GuardianAgentRef;
|
|
72
|
+
created: boolean;
|
|
73
|
+
}
|
|
74
|
+
/** Outcome of {@link sendToGuardian}. */
|
|
75
|
+
export interface GuardianSendResult {
|
|
76
|
+
/** The host accepted the message (`followup` returned without throwing). */
|
|
77
|
+
delivered: boolean;
|
|
78
|
+
/** The agent was busy, so the message sits in the DSH inbox behind the turn. */
|
|
79
|
+
queued: boolean;
|
|
80
|
+
/** Guardian session id the message went to. */
|
|
81
|
+
sessionId: string;
|
|
82
|
+
/** True when this call lazily created or recreated the session. */
|
|
83
|
+
created: boolean;
|
|
84
|
+
}
|
|
85
|
+
/** Title is display-only but stable, so an operator can recognize the session. */
|
|
86
|
+
export declare const GUARDIAN_SESSION_TITLE = "\u8BB0\u5FC6\u5B88\u62A4\u4F1A\u8BDD";
|
|
87
|
+
/** Skill the guardian must load before doing anything else. */
|
|
88
|
+
export declare const GUARDIAN_SKILL_NAME = "memory-guardian";
|
|
89
|
+
/**
|
|
90
|
+
* The preset first message. It intentionally stays product-level: read the
|
|
91
|
+
* skill, check memory state, ensure the daily dream task exists.
|
|
92
|
+
*/
|
|
93
|
+
export declare function guardianSeedText(): string;
|
|
94
|
+
/** Render one trigger message for the guardian. */
|
|
95
|
+
export declare function renderGuardianMessage(message: GuardianMessage): string;
|
|
96
|
+
/**
|
|
97
|
+
* Durable guardian state under `<memoryUsersRoot>/<userId>/guardian.json`.
|
|
98
|
+
*
|
|
99
|
+
* Reads tolerate a corrupt file as "state lost": the resolver then uses the
|
|
100
|
+
* title fallback or creates a fresh session instead of failing every message.
|
|
101
|
+
* Writes are atomic (tmp + rename) and private.
|
|
102
|
+
*/
|
|
103
|
+
export declare class GuardianSessionStore {
|
|
104
|
+
private readonly usersRoot;
|
|
105
|
+
/** Positive identity cache for trigger filtering; never used as the source of truth. */
|
|
106
|
+
private readonly knownSessions;
|
|
107
|
+
constructor(usersRoot: string);
|
|
108
|
+
/** Absolute path of one user's guardian state file. */
|
|
109
|
+
filePath(userId: string): string;
|
|
110
|
+
/** Read the state file; missing or malformed state is `undefined`. */
|
|
111
|
+
read(userId: string): Promise<GuardianSessionState | undefined>;
|
|
112
|
+
/** Create or replace the state file atomically. */
|
|
113
|
+
write(state: GuardianSessionState): Promise<void>;
|
|
114
|
+
/** Remove one user's guardian identity. Idempotent. */
|
|
115
|
+
remove(userId: string): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Whether a session id belongs to any user's guardian. Negative answers are
|
|
118
|
+
* not cached because a session can become a guardian later; positive answers
|
|
119
|
+
* are, so the common turn-event path stays cheap.
|
|
120
|
+
*/
|
|
121
|
+
isGuardianSession(sessionId: string): Promise<boolean>;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Resolve the user's guardian, creating it lazily when needed.
|
|
125
|
+
*
|
|
126
|
+
* Existing live agent → reuse. Persisted agent → resume. Resume failure or a
|
|
127
|
+
* missing state file → create one standard-mode session, write the identity
|
|
128
|
+
* first (so a failed seed cannot orphan it), then deliver the seed message.
|
|
129
|
+
*/
|
|
130
|
+
export declare function resolveOrCreateGuardian(host: GuardianSessionHost, userId: string): Promise<ResolvedGuardian>;
|
|
131
|
+
/**
|
|
132
|
+
* Deliver one trigger message to the user's guardian, reusing the resolved
|
|
133
|
+
* session. This is the ONLY send path used by the tool and the triggers.
|
|
134
|
+
*/
|
|
135
|
+
export declare function sendToGuardian(host: GuardianSessionHost, userId: string, message: GuardianMessage): Promise<GuardianSendResult>;
|
package/lib/types/index.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ import { type WorkflowActor } from './workflowPublish.ts';
|
|
|
27
27
|
import z from '@deepseek-ai/schemastery';
|
|
28
28
|
import { type KnowledgeConfig, type KnowledgeGrant, type KnowledgeGrantRequest, type KnowledgeRevokeRequest, type KnowledgeSshView, type KnowledgeStatus } from './protocol.ts';
|
|
29
29
|
import { type ScopeLocale } from './scopeAliases.ts';
|
|
30
|
+
import { type KnowledgeGraphScopeRoot } from './graph.ts';
|
|
30
31
|
import { type KnowledgeStorageSettings, type ViewDelta } from './views.ts';
|
|
31
32
|
import { type DeployPlanContext, type MineruBackend, type MineruTier } from './deploy.ts';
|
|
32
33
|
import { type RemoteHost, type RemoteRunner } from './remote.ts';
|
|
@@ -42,6 +43,7 @@ export declare const name = "dsh-knowledge-base";
|
|
|
42
43
|
* reaches the next request without a restart. `webServer` serves the
|
|
43
44
|
* same-origin routes the Settings panel reads. The SSH transport is reached
|
|
44
45
|
* over HTTP at the ssh-manager's own route, so it is not injected here.
|
|
46
|
+
* `systemPrompt` is the runtime memory instruction section's registry.
|
|
45
47
|
*/
|
|
46
48
|
export declare const inject: string[];
|
|
47
49
|
/** Plugin configuration. */
|
|
@@ -84,6 +86,12 @@ export interface Config {
|
|
|
84
86
|
viewRoot: string;
|
|
85
87
|
/** Local deployments: authoritative storage base, default `~/kb`. */
|
|
86
88
|
storageRoot: string;
|
|
89
|
+
/** Where personal memory bindings live; empty means `<actualRoot>/.memory/users`. */
|
|
90
|
+
memoryUsersRoot: string;
|
|
91
|
+
/** Where promoted skills are written; empty means `$DSH_HOME/skills` (or `~/.dsh/skills`). */
|
|
92
|
+
memorySkillsRoot: string;
|
|
93
|
+
/** Idle minutes before a normal session notifies the memory guardian. */
|
|
94
|
+
memoryIdleMinutes: number;
|
|
87
95
|
/** Comma-separated export spec of clients allowed to mount the NFS export. */
|
|
88
96
|
permitted: string;
|
|
89
97
|
/** Host serving the export, as an address or `address:/path` pseudo root. */
|
|
@@ -839,6 +847,44 @@ export declare function createKnowledgeEntry(config: Config, relPath: string, op
|
|
|
839
847
|
* `no such document`.
|
|
840
848
|
*/
|
|
841
849
|
export declare function actualRelPathForView(ctx: Context, config: Config, relPath: string, caller?: KnowledgeCaller): Promise<string>;
|
|
850
|
+
/**
|
|
851
|
+
* The canonical VIEW path a model-facing file-tool path names.
|
|
852
|
+
*
|
|
853
|
+
* The `kb_file_*` tools speak view coordinates (`global/…`, `department/…`,
|
|
854
|
+
* `personal/…` or a bare path). A bare path has no bucket, and the old code left
|
|
855
|
+
* it untouched: `createKnowledgeEntry` then wrote it at the authoritative top
|
|
856
|
+
* level, which the view renders as `global/…` because {@link bucketOf} treats an
|
|
857
|
+
* unknown head as global. That is how a personal memory write silently became a
|
|
858
|
+
* global document.
|
|
859
|
+
*
|
|
860
|
+
* On a team deployment a bare path belongs to the caller's PERSONAL bucket — the
|
|
861
|
+
* only bucket one caller owns — so it is spelled `personal/<path>`. Explicit
|
|
862
|
+
* scope spellings, including the localized aliases, are already canonical after
|
|
863
|
+
* {@link resolveScopeAlias} and are returned unchanged. A local deployment keeps
|
|
864
|
+
* the legacy flat tree, where there are no buckets, so nothing is prefixed.
|
|
865
|
+
*
|
|
866
|
+
* @param relPath - model-supplied path, already validated/alias-resolved or not.
|
|
867
|
+
* @param team - `isTeamDeployment(config)`; false keeps the flat local layout.
|
|
868
|
+
*/
|
|
869
|
+
export declare function toolScopedViewPath(relPath: string, team: boolean): string;
|
|
870
|
+
/**
|
|
871
|
+
* The AUTHORITATIVE path a model-facing file-tool path names.
|
|
872
|
+
*
|
|
873
|
+
* A view drops the personal owner segment (`personal/note.md`), but the actual
|
|
874
|
+
* tree keeps it (`personal/<uid>/note.md`). Writes, overwrites and existence
|
|
875
|
+
* checks must use this form; reads/listings stay in view coordinates and use the
|
|
876
|
+
* view root. Global and department paths are unchanged: their view and actual
|
|
877
|
+
* spellings are already identical (or handled by their own layout rules).
|
|
878
|
+
*
|
|
879
|
+
* A path that already carries the caller's own id is returned untouched, so the
|
|
880
|
+
* resolver is idempotent and graph tools (which pass actual paths) are safe.
|
|
881
|
+
*
|
|
882
|
+
* @param relPath - model-supplied path (view or already-actual).
|
|
883
|
+
* @param userId - the caller whose personal tree owns a bare/personal path.
|
|
884
|
+
* @param team - `isTeamDeployment(config)`; false keeps the flat local layout.
|
|
885
|
+
*/
|
|
886
|
+
export declare function toolScopedActualPath(relPath: string, userId: string, team: boolean): string;
|
|
887
|
+
export declare function knowledgeGraphScopeRoots(host: Context, config: Config, caller: KnowledgeCaller): Promise<KnowledgeGraphScopeRoot[]>;
|
|
842
888
|
export declare function extractBeside(config: Config, relPath: string, mineruUrl?: string, readSharedUrl?: () => Promise<string | undefined>, ctx?: Context): Promise<{
|
|
843
889
|
markdown: string;
|
|
844
890
|
via?: string;
|
|
@@ -1088,6 +1134,18 @@ export declare function kbSummarizeSkillPath(): string;
|
|
|
1088
1134
|
export declare function kbVectorizeSkillPath(): string;
|
|
1089
1135
|
/** Where the bundled `knowledge-graph` skill lives. */
|
|
1090
1136
|
export declare function knowledgeGraphSkillPath(): string;
|
|
1137
|
+
/** The guardian's own operating skill. */
|
|
1138
|
+
export declare const MEMORY_GUARDIAN_SKILL_NAME = "memory-guardian";
|
|
1139
|
+
/** The extraction subagent's skill. */
|
|
1140
|
+
export declare const MEMORY_EXTRACT_SKILL_NAME = "memory-extract";
|
|
1141
|
+
/** The SOP evaluation subagent's skill. */
|
|
1142
|
+
export declare const MEMORY_SOP_EVALUATE_SKILL_NAME = "memory-sop-evaluate";
|
|
1143
|
+
/** Where the bundled `memory-guardian` skill lives. */
|
|
1144
|
+
export declare function memoryGuardianSkillPath(): string;
|
|
1145
|
+
/** Where the bundled `memory-extract` skill lives. */
|
|
1146
|
+
export declare function memoryExtractSkillPath(): string;
|
|
1147
|
+
/** Where the bundled `memory-sop-evaluate` skill lives. */
|
|
1148
|
+
export declare function memorySopEvaluateSkillPath(): string;
|
|
1091
1149
|
/**
|
|
1092
1150
|
* Where this package's tutorial document lives, from either build layout.
|
|
1093
1151
|
*
|
|
@@ -1182,3 +1240,37 @@ export declare function knowledgeBaseToolDisclosure(host: object): ToolDisclosur
|
|
|
1182
1240
|
export declare function apply(ctx: Context, deploymentConfig: Config): void;
|
|
1183
1241
|
/** Re-exported for tests and for callers that need the host shape. */
|
|
1184
1242
|
export type { RemoteHost, RemoteRunner };
|
|
1243
|
+
export { GraphLoadError, listGraphs, loadGraph, parseNodeFrontMatter, validateGraph, } from './graphModel.ts';
|
|
1244
|
+
export type { GraphManifest, GraphManifestEdge, GraphManifestNode, GraphValidationIssue, LoadedGraph, LoadedGraphEdge, LoadedGraphNode, MemoryAuthority, MemoryNodeType, MemoryOrigin, MemoryReviewStatus, } from './graphModel.ts';
|
|
1245
|
+
export { canManageGraph, canViewGraph, filterVisibleGraphs, } from './graphPolicy.ts';
|
|
1246
|
+
export type { GraphScopeDescriptor, GraphViewer } from './graphPolicy.ts';
|
|
1247
|
+
export { BindingStore, bindingKey, } from './memoryBindings.ts';
|
|
1248
|
+
export type { BindingLineage, BindingMode, BindingSourceType, BindingStatus, PersonalBinding, } from './memoryBindings.ts';
|
|
1249
|
+
export { markUpstreamUpdates, runGuardianPass } from './memoryGuardian.ts';
|
|
1250
|
+
export { decideMemoryAction, runtimeInjectionTags } from './memoryPolicy.ts';
|
|
1251
|
+
export { diffSnapshots, parentChain, refreshDirtySummaries, scanKnowledgeTree, } from './summaryIndex.ts';
|
|
1252
|
+
export type { ChangeSet, ChangeSnapshot } from './summaryIndex.ts';
|
|
1253
|
+
export { applyMigration, planMigration, renderMigrationReport, } from './graphMigration.ts';
|
|
1254
|
+
export type { MigrationOptions, MigrationReport } from './graphMigration.ts';
|
|
1255
|
+
export { applySummaryWriteback, summarySnapshotPathFor } from './summaryWriteback.ts';
|
|
1256
|
+
export type { SummaryWritebackOptions, SummaryWritebackResult } from './summaryWriteback.ts';
|
|
1257
|
+
export { resolveMemoryContext, renderMemoryContextMarkdown } from './memoryContext.ts';
|
|
1258
|
+
export type { MemoryContextResult } from './memoryContext.ts';
|
|
1259
|
+
export { createMemoryTools } from './memoryTools.ts';
|
|
1260
|
+
export type { MemoryContextToolResult, MemoryGuardianToolResult, MemoryToolHost } from './memoryTools.ts';
|
|
1261
|
+
export { MemoryStore, MemoryStoreError, MEMORY_LOG_DIR, MEMORY_SOP_DIR, MEMORY_PREFERENCE_FILE, MEMORY_PITFALL_FILE, MEMORY_FACT_FILE, } from './memoryStore.ts';
|
|
1262
|
+
export type { MemoryStoreOptions, MemoryIndex, MemoryLogSummary, AppendLogOptions, AppendLogResult, PreferenceUpsertResult, PitfallUpsertResult, FactUpsertResult, SopWriteResult, SopUpdateResult, SopPatch, } from './memoryStore.ts';
|
|
1263
|
+
export { MemoryGraphWriter, MemoryGraphWriterError, syncPersonalMemoryGraph, PERSONAL_MEMORY_FOLDER, PERSONAL_MEMORY_GRAPH_ID, } from './memoryGraphWriter.ts';
|
|
1264
|
+
export type { MemoryGraphWriterOptions, PersonalMemoryGraphResult } from './memoryGraphWriter.ts';
|
|
1265
|
+
export { MEMORY_TYPES, PRIVACY_VALUES, FACT_KINDS, SOP_STATUSES, MEMORY_ENTRY_MARKER, isMemoryType, isPrivacy, isFactKind, isSopStatus, isSafeMemorySlug, validateMemoryEntry, assertValidMemoryEntry, parseMemoryEntries, serializeMemoryEntry, newMemoryId, } from './memoryTypes.ts';
|
|
1266
|
+
export type { MemoryType, Privacy, FactKind, AppId, SopStatus, MemoryEntry, Preference, Pitfall, Episodic, Fact, Sop, AnyMemoryEntry, MemoryEntryInput, MemoryValidationIssue, MemoryValidationError, } from './memoryTypes.ts';
|
|
1267
|
+
export { GuardianSessionStore, resolveOrCreateGuardian, sendToGuardian, GUARDIAN_SESSION_TITLE, GUARDIAN_SKILL_NAME, } from './guardianSession.ts';
|
|
1268
|
+
export type { GuardianReason, GuardianMessage, GuardianSessionState, GuardianAgentRef, GuardianDelivery, CreateGuardianAgentInput, GuardianSessionHost, ResolvedGuardian, GuardianSendResult, } from './guardianSession.ts';
|
|
1269
|
+
export { createMemoryGuardianTools } from './memoryGuardianTool.ts';
|
|
1270
|
+
export type { MemoryGuardianToolHost, MemoryGuardianSendArgs, MemoryGuardianSendValue } from './memoryGuardianTool.ts';
|
|
1271
|
+
export { installMemoryTriggers, IDLE_DEBOUNCE_MS } from './memoryTriggers.ts';
|
|
1272
|
+
export type { MemoryTriggerHost, MemoryTriggerSession, MemoryTriggerEvent, MemoryTriggerTimers } from './memoryTriggers.ts';
|
|
1273
|
+
export { createMemoryRecordTools } from './memoryRecordTool.ts';
|
|
1274
|
+
export type { MemoryRecordToolHost } from './memoryRecordTool.ts';
|
|
1275
|
+
export { promoteSopToSkill, deleteSopAfterPromotion } from './memorySkillPromotion.ts';
|
|
1276
|
+
export type { PromoteSopInput, DeleteSopInput, SopDraft } from './memorySkillPromotion.ts';
|