@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
|
@@ -22,6 +22,7 @@ import type { FtsIndexStats, FtsSearchResult } from './ftsIndex.ts';
|
|
|
22
22
|
import type { OutlineKeyFact, OutlineTocEntry } from './outlineText.ts';
|
|
23
23
|
import type { OutlineSectionSubmission, OutlineSummaryKind } from './outlineGuard.ts';
|
|
24
24
|
import type { OutlineSaveResult } from './ftsIndex.ts';
|
|
25
|
+
import { type CanonicalScope } from './scopeAliases.ts';
|
|
25
26
|
import type { KnowledgeAuditEntry } from './views.ts';
|
|
26
27
|
/** Error codes a tool may report to the model. */
|
|
27
28
|
export type KnowledgeToolErrorCode = 'empty-path' | 'nul-byte' | 'absolute-path' | 'escapes-knowledge-root' | 'forbidden' | 'not-found' | 'exists' | 'too-large' | 'binary' | 'not-a-directory' | 'not-a-file' | 'not-configured' | 'unauthorized' | 'unreachable' | 'bad-response' | 'write-failed' | 'invalid-regex' | 'invalid-citation' | 'invalid-summary' | 'invalid';
|
|
@@ -176,6 +177,14 @@ export interface KnowledgeFileToolHost {
|
|
|
176
177
|
}): Promise<OutlineSaveResult | undefined>;
|
|
177
178
|
canRead(relPath: string): Promise<boolean>;
|
|
178
179
|
canWrite(relPath: string): Promise<boolean>;
|
|
180
|
+
/**
|
|
181
|
+
* Default bucket for a path that names no scope at all.
|
|
182
|
+
*
|
|
183
|
+
* A team deployment answers `personal`: a bare relative path is the caller's
|
|
184
|
+
* own tree, never the shared root that the view renders as `global`. A local
|
|
185
|
+
* deployment omits this, keeping the legacy flat top-level path.
|
|
186
|
+
*/
|
|
187
|
+
defaultScope?(): CanonicalScope | undefined;
|
|
179
188
|
/** One view directory level, already filtered by `canReadPath`. */
|
|
180
189
|
list(relPath: string, caller: KnowledgeToolCaller): Promise<KnowledgeFileEntry[]>;
|
|
181
190
|
/** Search under one view-relative scope. */
|
|
@@ -229,8 +238,15 @@ export declare const asKnowledgeToolError: (error: unknown, path?: string) => Kn
|
|
|
229
238
|
*
|
|
230
239
|
* Empty is allowed only where the root itself is the argument (list/search/extract-all);
|
|
231
240
|
* everything else is refused with the same four codes `checkLocalPath` uses.
|
|
241
|
+
*
|
|
242
|
+
* `defaultScope` decides what a path with NO bucket prefix means. When omitted
|
|
243
|
+
* the legacy behaviour is kept (a bare top-level path). A team deployment passes
|
|
244
|
+
* `personal`, so `个人记忆图谱/…` without a bucket resolves inside the caller's
|
|
245
|
+
* own tree instead of becoming an unowned top-level path that the view shows as
|
|
246
|
+
* `global`. Explicit `global/`, `department/` and `personal/` spellings (and
|
|
247
|
+
* their localized aliases) are never rewritten.
|
|
232
248
|
*/
|
|
233
|
-
export declare const assertToolPath: (value: unknown, allowRoot?: boolean) => string;
|
|
249
|
+
export declare const assertToolPath: (value: unknown, allowRoot?: boolean, defaultScope?: CanonicalScope) => string;
|
|
234
250
|
/**
|
|
235
251
|
* Build the knowledge-base model-facing tools.
|
|
236
252
|
*
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/** What a binding points at. */
|
|
2
|
+
export type BindingSourceType = 'node' | 'edge';
|
|
3
|
+
/** Lifecycle of a personal binding. */
|
|
4
|
+
export type BindingMode = 'pending' | 'subscribe' | 'internalize';
|
|
5
|
+
/** Health of a binding after its source moved on or its policy changed. */
|
|
6
|
+
export type BindingStatus = 'active' | 'stale' | 'revoked';
|
|
7
|
+
/** A concrete mode a pending binding asks the guardian to apply. */
|
|
8
|
+
export type RequestedBindingMode = Exclude<BindingMode, 'pending'>;
|
|
9
|
+
/** Provenance of a personal branch. */
|
|
10
|
+
export interface BindingLineage {
|
|
11
|
+
subscribedAt?: string;
|
|
12
|
+
forkedFromVersion?: number;
|
|
13
|
+
forkedAt?: string;
|
|
14
|
+
divergence?: string[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* One user's relationship to one graph source.
|
|
18
|
+
*
|
|
19
|
+
* `localPath` has two meanings, keyed by `mode`:
|
|
20
|
+
*
|
|
21
|
+
* - `internalize`: the personal copy, relative to `<usersRoot>/<userId>`
|
|
22
|
+
* (`memory/<graphId>/<sourceId>.md`);
|
|
23
|
+
* - `subscribe`: the source document path relative to the graph scope root
|
|
24
|
+
* (e.g. `制度/采购制度.md`). It is a proxy reference only - no body is
|
|
25
|
+
* copied, and the runtime reads the department document live.
|
|
26
|
+
* - `pending`: absent.
|
|
27
|
+
*
|
|
28
|
+
* `requestedMode` is the hand-off used by the UI: a user writes
|
|
29
|
+
* `{ mode: 'pending', requestedMode: 'internalize' }` and the guardian session
|
|
30
|
+
* (the single writer of binding files) performs the copy on its next pass.
|
|
31
|
+
* Without a request, a pending binding is resolved to `subscribe`, which is
|
|
32
|
+
* always safe because it copies no content.
|
|
33
|
+
*/
|
|
34
|
+
export interface PersonalBinding {
|
|
35
|
+
userId: string;
|
|
36
|
+
sourceType: BindingSourceType;
|
|
37
|
+
sourceId: string;
|
|
38
|
+
graphId: string;
|
|
39
|
+
mode: BindingMode;
|
|
40
|
+
sourceVersion: number;
|
|
41
|
+
sourceCommit?: string;
|
|
42
|
+
lineage: BindingLineage;
|
|
43
|
+
status: BindingStatus;
|
|
44
|
+
updatedAt: string;
|
|
45
|
+
localPath?: string;
|
|
46
|
+
requestedMode?: RequestedBindingMode;
|
|
47
|
+
}
|
|
48
|
+
/** Raised when a binding file or a binding value is unusable. */
|
|
49
|
+
export declare class BindingStoreError extends Error {
|
|
50
|
+
readonly filePath?: string;
|
|
51
|
+
constructor(message: string, filePath?: string);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Compose the binding file stem: `<sourceType>-<sourceId>`.
|
|
55
|
+
*
|
|
56
|
+
* The source id becomes part of a file name, so path separators, NUL bytes,
|
|
57
|
+
* leading dots and surrounding whitespace are refused rather than escaped.
|
|
58
|
+
* Legitimate graph ids (ASCII slugs and Chinese ids alike) are untouched.
|
|
59
|
+
*/
|
|
60
|
+
export declare function bindingKey(sourceType: BindingSourceType, sourceId: string): string;
|
|
61
|
+
/** Relative personal copy path for an internalized binding. */
|
|
62
|
+
export declare function memoryRelativePath(graphId: string, sourceId: string): string;
|
|
63
|
+
/** Absolute personal copy path for an internalized binding. */
|
|
64
|
+
export declare function memoryFilePath(usersRoot: string, userId: string, graphId: string, sourceId: string): string;
|
|
65
|
+
/**
|
|
66
|
+
* Atomic UTF-8 write: write a sibling temp file, then `rename` it over the
|
|
67
|
+
* target. Same-directory temp + rename is atomic on POSIX file systems, and on
|
|
68
|
+
* Windows `rename` over an existing file is supported by Node.
|
|
69
|
+
*/
|
|
70
|
+
export declare function writeFileAtomic(filePath: string, content: string, options?: {
|
|
71
|
+
mode?: number;
|
|
72
|
+
}): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Binding store rooted at a users directory. Paths are injected, never read
|
|
75
|
+
* from global config, so tests and the runtime can point at different roots.
|
|
76
|
+
*/
|
|
77
|
+
export declare class BindingStore {
|
|
78
|
+
private readonly usersRoot;
|
|
79
|
+
constructor(usersRoot: string);
|
|
80
|
+
/** Directory holding every binding file for one user. */
|
|
81
|
+
readonlyPath(userId: string): string;
|
|
82
|
+
/** Rebuild the list from the binding directory; no index is kept. */
|
|
83
|
+
list(userId: string): Promise<PersonalBinding[]>;
|
|
84
|
+
/** Read one binding; a missing file is `undefined`, not an error. */
|
|
85
|
+
get(userId: string, sourceType: BindingSourceType, sourceId: string): Promise<PersonalBinding | undefined>;
|
|
86
|
+
/** Create or replace one binding atomically. */
|
|
87
|
+
set(binding: PersonalBinding): Promise<void>;
|
|
88
|
+
/** Delete one binding. Removing a file that is already gone is a no-op. */
|
|
89
|
+
remove(userId: string, sourceType: BindingSourceType, sourceId: string): Promise<void>;
|
|
90
|
+
}
|
|
91
|
+
/** Serialize a binding into the simple YAML dialect described above. */
|
|
92
|
+
export declare function serializeBinding(binding: PersonalBinding): string;
|
|
93
|
+
/** Parse one binding file. Throws `BindingStoreError` when required fields are absent. */
|
|
94
|
+
export declare function parseBinding(source: string, filePath?: string): PersonalBinding;
|
|
95
|
+
/** Scalar values the writer understands. */
|
|
96
|
+
export type YamlScalar = string | number | boolean;
|
|
97
|
+
/** One level of nesting is enough for bindings and memory frontmatter. */
|
|
98
|
+
export type YamlValue = YamlScalar | YamlScalar[] | YamlRecord;
|
|
99
|
+
/** Ordered YAML object. `undefined` entries are omitted. */
|
|
100
|
+
export interface YamlRecord {
|
|
101
|
+
[key: string]: YamlValue | undefined;
|
|
102
|
+
}
|
|
103
|
+
/** Quote a scalar when the tiny parser (or a real YAML parser) could misread it. */
|
|
104
|
+
export declare function formatYamlScalar(value: YamlScalar): string;
|
|
105
|
+
/** Serialize a flat/nested record in the simple dialect. Arrays are inline. */
|
|
106
|
+
export declare function stringifySimpleYaml(record: YamlRecord): string;
|
|
107
|
+
/**
|
|
108
|
+
* Parse the simple dialect into string scalars (numbers, booleans and
|
|
109
|
+
* timestamps stay strings so ids like `00123` survive) plus `string[]` arrays
|
|
110
|
+
* and nested maps. Unknown/invalid lines are ignored; typed readers validate.
|
|
111
|
+
*/
|
|
112
|
+
export declare function parseSimpleYaml(source: string): Record<string, unknown>;
|
|
113
|
+
/**
|
|
114
|
+
* A path segment is safe when it cannot leave its directory and cannot create
|
|
115
|
+
* a hidden file. Unicode letters, spaces inside and dots inside are accepted.
|
|
116
|
+
*/
|
|
117
|
+
export declare function assertPathSegment(value: string, label: string): string;
|
|
118
|
+
/** Validate a `/`-separated relative path (graph-relative or user-relative). */
|
|
119
|
+
export declare function assertRelativePath(value: string, label: string): string;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { GraphManifest, LoadedGraph, MemoryAuthority, MemoryOrigin, MemoryReviewStatus } from './graphModel.ts';
|
|
2
|
+
import { type GraphScopeDescriptor, type GraphViewer } from './graphPolicy.ts';
|
|
3
|
+
import { type BindingSourceType, type BindingStatus, type PersonalBinding } from './memoryBindings.ts';
|
|
4
|
+
/** Default prompt budget for the resolved context. */
|
|
5
|
+
export declare const DEFAULT_MEMORY_CONTEXT_MAX_CHARS = 8000;
|
|
6
|
+
/** Marker the runtime must treat as above personal memory and preferences. */
|
|
7
|
+
export declare const SYSTEM_POLICY_MARKER = "system policy overrides personal memory";
|
|
8
|
+
/** How a memory entry reaches the personal graph. */
|
|
9
|
+
export type MemoryContextMode = 'subscribe' | 'internalize';
|
|
10
|
+
/** One resolved node/edge the runtime may inject. */
|
|
11
|
+
export interface MemoryContextEntry {
|
|
12
|
+
mode: MemoryContextMode;
|
|
13
|
+
graphId: string;
|
|
14
|
+
sourceType: BindingSourceType;
|
|
15
|
+
sourceId: string;
|
|
16
|
+
title: string;
|
|
17
|
+
/** Node type or relation type. */
|
|
18
|
+
type: string;
|
|
19
|
+
authority: MemoryAuthority;
|
|
20
|
+
origin: MemoryOrigin;
|
|
21
|
+
reviewStatus: MemoryReviewStatus;
|
|
22
|
+
/** True for a redline (direct or inherited): personal memory must not override it. */
|
|
23
|
+
nonOverridable: boolean;
|
|
24
|
+
/** Set together with `nonOverridable`. */
|
|
25
|
+
policyNote?: string;
|
|
26
|
+
status: BindingStatus;
|
|
27
|
+
sourceVersion: number;
|
|
28
|
+
sourceCommit?: string;
|
|
29
|
+
/** Live source path for a proxy; personal branch path for an internalized copy. */
|
|
30
|
+
path?: string;
|
|
31
|
+
/** Relation endpoints, when the source is an edge. */
|
|
32
|
+
from?: string;
|
|
33
|
+
to?: string;
|
|
34
|
+
body: string;
|
|
35
|
+
/** Budget cost used by this resolver (title + body + fixed metadata overhead). */
|
|
36
|
+
chars: number;
|
|
37
|
+
}
|
|
38
|
+
/** One entry omitted only because the character budget ran out. */
|
|
39
|
+
export interface MemoryContextDrop {
|
|
40
|
+
graphId: string;
|
|
41
|
+
sourceType: BindingSourceType;
|
|
42
|
+
sourceId: string;
|
|
43
|
+
mode: MemoryContextMode;
|
|
44
|
+
chars: number;
|
|
45
|
+
reason: 'budget';
|
|
46
|
+
}
|
|
47
|
+
/** The resolver output. */
|
|
48
|
+
export interface MemoryContextResult {
|
|
49
|
+
userId: string;
|
|
50
|
+
entries: MemoryContextEntry[];
|
|
51
|
+
dropped: MemoryContextDrop[];
|
|
52
|
+
charCount: number;
|
|
53
|
+
maxChars: number;
|
|
54
|
+
redlineCount: number;
|
|
55
|
+
/** Non-empty when at least one redline is present. */
|
|
56
|
+
systemPolicy: string;
|
|
57
|
+
}
|
|
58
|
+
/** Resolver input. All roots and graphs are injected; no global config is read. */
|
|
59
|
+
export interface MemoryContextInput {
|
|
60
|
+
viewer: GraphViewer;
|
|
61
|
+
usersRoot: string;
|
|
62
|
+
userId: string;
|
|
63
|
+
graphs: LoadedGraph[];
|
|
64
|
+
/** Pre-read bindings, for callers that already listed them. Omit to rebuild from disk. */
|
|
65
|
+
bindings?: PersonalBinding[];
|
|
66
|
+
/** Prompt budget; defaults to {@link DEFAULT_MEMORY_CONTEXT_MAX_CHARS}. */
|
|
67
|
+
maxChars?: number;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Adapt a loaded graph manifest to the visibility descriptor `canViewGraph`
|
|
71
|
+
* expects. `scope` is the machine scope (`global`, `department:<code>`,
|
|
72
|
+
* `personal:<uid>`); `owner` is the private-graph owner when present.
|
|
73
|
+
*/
|
|
74
|
+
export declare function graphScopeDescriptor(manifest: GraphManifest): GraphScopeDescriptor;
|
|
75
|
+
/**
|
|
76
|
+
* Resolve the caller's personal memory context.
|
|
77
|
+
*
|
|
78
|
+
* @param input - viewer, roots, user id, loaded graphs and optional budget.
|
|
79
|
+
* @returns Priority-ordered entries, budget drops and redline counters.
|
|
80
|
+
*/
|
|
81
|
+
export declare function resolveMemoryContext(input: MemoryContextInput): Promise<MemoryContextResult>;
|
|
82
|
+
/** Render a resolved context for a model-facing text tool result. */
|
|
83
|
+
export declare function renderMemoryContextMarkdown(result: MemoryContextResult): string;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensure the personal memory graph exists.
|
|
3
|
+
*
|
|
4
|
+
* The graph tab is a catalogue of `.graphs/<id>/graph.json` manifests. A user
|
|
5
|
+
* with no subscriptions yet has no personal graph at all, which reads as "the
|
|
6
|
+
* memory feature is missing" rather than "there is nothing in it". This creates
|
|
7
|
+
* an empty, private, published manifest for the caller once; it is the stable
|
|
8
|
+
* home the guardian will later fill with subscription proxies and internalized
|
|
9
|
+
* branches.
|
|
10
|
+
*
|
|
11
|
+
* Writing is deliberately best-effort at the call site: a read-only mount must
|
|
12
|
+
* keep the graph tab usable, so a failed bootstrap is not an error for the route.
|
|
13
|
+
*
|
|
14
|
+
* @module @sparkelf/dsh-knowledge-base/memory-graph-bootstrap
|
|
15
|
+
*/
|
|
16
|
+
/** Stable graph id for the caller's personal memory graph. */
|
|
17
|
+
export declare const PERSONAL_MEMORY_GRAPH_ID = "personal-memory";
|
|
18
|
+
/** Display name shown in the graph tree. */
|
|
19
|
+
export declare const PERSONAL_MEMORY_GRAPH_NAME = "\u4E2A\u4EBA\u8BB0\u5FC6\u56FE\u8C31";
|
|
20
|
+
/** One empty manifest as it is written to disk. */
|
|
21
|
+
export interface PersonalMemoryGraphManifest {
|
|
22
|
+
schemaVersion: 1;
|
|
23
|
+
id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
scope: string;
|
|
27
|
+
version: number;
|
|
28
|
+
status: 'draft' | 'published';
|
|
29
|
+
owner: string;
|
|
30
|
+
createdAt: string;
|
|
31
|
+
updatedAt: string;
|
|
32
|
+
publishedAt: string;
|
|
33
|
+
tags: string[];
|
|
34
|
+
visibility: 'private';
|
|
35
|
+
defaultView: {
|
|
36
|
+
layout: string;
|
|
37
|
+
direction: string;
|
|
38
|
+
};
|
|
39
|
+
topics: unknown[];
|
|
40
|
+
nodes: unknown[];
|
|
41
|
+
edges: unknown[];
|
|
42
|
+
}
|
|
43
|
+
/** The manifest a fresh personal memory graph holds. */
|
|
44
|
+
export declare function emptyPersonalMemoryManifest(userId: string, now: string): PersonalMemoryGraphManifest;
|
|
45
|
+
/**
|
|
46
|
+
* Create `<scopeRoot>/.graphs/personal-memory/graph.json` when it is absent.
|
|
47
|
+
*
|
|
48
|
+
* @param options.scopeRoot - absolute personal scope root (`<actualRoot>/personal/<uid>`).
|
|
49
|
+
* @param options.userId - stable caller id; also the manifest owner.
|
|
50
|
+
* @param options.now - injectable clock for tests.
|
|
51
|
+
* @returns whether the manifest was created and its absolute path.
|
|
52
|
+
*/
|
|
53
|
+
export declare function ensurePersonalMemoryGraph(options: {
|
|
54
|
+
scopeRoot: string;
|
|
55
|
+
userId: string;
|
|
56
|
+
now?: string;
|
|
57
|
+
}): Promise<{
|
|
58
|
+
created: boolean;
|
|
59
|
+
path: string;
|
|
60
|
+
}>;
|
|
61
|
+
/** Read the personal memory manifest when it exists, for tests and diagnostics. */
|
|
62
|
+
export declare function readPersonalMemoryGraph(scopeRoot: string): Promise<PersonalMemoryGraphManifest | undefined>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeps the personal-memory graph manifest in sync with the memory files.
|
|
3
|
+
*
|
|
4
|
+
* The memory store owns the documents; this writer owns only
|
|
5
|
+
* `<scopeRoot>/.graphs/personal-memory/graph.json`. It never rewrites a memory
|
|
6
|
+
* document and never duplicates a node:
|
|
7
|
+
*
|
|
8
|
+
* ```text
|
|
9
|
+
* 个人记忆图谱/记忆日志/2026-11-12.md -> node memory-个人记忆图谱-记忆日志-2026-11-12
|
|
10
|
+
* 个人记忆图谱/偏好.md -> node memory-个人记忆图谱-偏好
|
|
11
|
+
* 个人记忆图谱/SOP/指标口径排查.md -> node memory-个人记忆图谱-SOP-指标口径排查
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* Node ids are derived from the scope-relative path and kept stable across
|
|
15
|
+
* writes, so edges added later (daily document -> derived aggregate / SOP) do not
|
|
16
|
+
* dangle. Every manifest write is atomic and validated with `loadGraph` +
|
|
17
|
+
* `validateGraph`; if validation fails the previous manifest is restored.
|
|
18
|
+
*
|
|
19
|
+
* The graph is lazy: the first access creates an empty, valid
|
|
20
|
+
* `personal-memory/graph.json` so "no memories yet" still has a graph home.
|
|
21
|
+
*
|
|
22
|
+
* @module @sparkelf/dsh-knowledge-base/memory-graph-writer
|
|
23
|
+
*/
|
|
24
|
+
/** Default memory tree folder inside a personal scope. */
|
|
25
|
+
export declare const PERSONAL_MEMORY_FOLDER = "\u4E2A\u4EBA\u8BB0\u5FC6\u56FE\u8C31";
|
|
26
|
+
/** Default graph id, matching the design's `personal-memory/graph.json`. */
|
|
27
|
+
export declare const PERSONAL_MEMORY_GRAPH_ID = "personal-memory";
|
|
28
|
+
/** Writer construction options. */
|
|
29
|
+
export interface MemoryGraphWriterOptions {
|
|
30
|
+
/** Personal scope root that owns `.graphs/`. */
|
|
31
|
+
scopeRoot: string;
|
|
32
|
+
/** Memory tree relative to `scopeRoot`; defaults to `个人记忆图谱`. */
|
|
33
|
+
memoryRoot?: string;
|
|
34
|
+
/** Graph directory id under `.graphs/`; defaults to `personal-memory`. */
|
|
35
|
+
graphId?: string;
|
|
36
|
+
/** Manifest `scope` string; defaults to `personal`. */
|
|
37
|
+
scope?: string;
|
|
38
|
+
now?: () => Date;
|
|
39
|
+
}
|
|
40
|
+
/** Result of one manifest reconciliation. */
|
|
41
|
+
export interface PersonalMemoryGraphResult {
|
|
42
|
+
/** Absolute path of `graph.json`. */
|
|
43
|
+
graphPath: string;
|
|
44
|
+
/** Node count after the operation. */
|
|
45
|
+
nodeCount: number;
|
|
46
|
+
/** True when the manifest was rewritten. */
|
|
47
|
+
changed: boolean;
|
|
48
|
+
}
|
|
49
|
+
export type MemoryGraphWriterErrorCode = 'invalid-option' | 'unsafe-path' | 'missing-memory-document' | 'invalid-graph';
|
|
50
|
+
export declare class MemoryGraphWriterError extends Error {
|
|
51
|
+
readonly code: MemoryGraphWriterErrorCode;
|
|
52
|
+
constructor(code: MemoryGraphWriterErrorCode, message: string);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Owns one personal-memory `graph.json`.
|
|
56
|
+
*
|
|
57
|
+
* A single writer is assumed (the guardian session); `registerPath` is the
|
|
58
|
+
* incremental hook after a store write, `removePath` after a SOP delete, and
|
|
59
|
+
* `syncAll` is the reconciliation pass that can be run at any time.
|
|
60
|
+
*/
|
|
61
|
+
export declare class MemoryGraphWriter {
|
|
62
|
+
private readonly scopeRoot;
|
|
63
|
+
private readonly memoryRoot;
|
|
64
|
+
private readonly graphId;
|
|
65
|
+
private readonly scope;
|
|
66
|
+
private readonly clock;
|
|
67
|
+
constructor(options: MemoryGraphWriterOptions);
|
|
68
|
+
/** Absolute manifest path owned by this writer. */
|
|
69
|
+
get graphPath(): string;
|
|
70
|
+
/** Absolute root of the memory documents. */
|
|
71
|
+
get memoryPath(): string;
|
|
72
|
+
/** Create an empty, valid graph on first access; idempotent afterwards. */
|
|
73
|
+
ensureGraph(): Promise<PersonalMemoryGraphResult>;
|
|
74
|
+
/**
|
|
75
|
+
* Register one document that the store just wrote.
|
|
76
|
+
*
|
|
77
|
+
* `memoryRelativePath` is relative to the memory root (for example
|
|
78
|
+
* `记忆日志/2026-11-12.md` or `SOP/指标口径排查.md`). The file must exist: a
|
|
79
|
+
* manifest node pointing at nothing is a validator error.
|
|
80
|
+
*/
|
|
81
|
+
registerPath(memoryRelativePath: string): Promise<PersonalMemoryGraphResult>;
|
|
82
|
+
/** Drop one document node and any edge that referenced it. */
|
|
83
|
+
removePath(memoryRelativePath: string): Promise<PersonalMemoryGraphResult>;
|
|
84
|
+
/**
|
|
85
|
+
* Reconcile the manifest with the memory files that exist on disk.
|
|
86
|
+
*
|
|
87
|
+
* Adds nodes for new files, removes nodes whose memory document disappeared,
|
|
88
|
+
* and drops edges that would no longer resolve. Nodes outside the memory root
|
|
89
|
+
* are left alone; the manifest stays `validateGraph`-clean.
|
|
90
|
+
*/
|
|
91
|
+
syncAll(): Promise<PersonalMemoryGraphResult>;
|
|
92
|
+
private memoryPathsOf;
|
|
93
|
+
private scanMemoryPaths;
|
|
94
|
+
private reconcile;
|
|
95
|
+
private emptyManifest;
|
|
96
|
+
private tryLoadManifest;
|
|
97
|
+
private writeManifest;
|
|
98
|
+
private isFile;
|
|
99
|
+
}
|
|
100
|
+
/** Convenience one-shot reconciliation for wiring. */
|
|
101
|
+
export declare function syncPersonalMemoryGraph(options: MemoryGraphWriterOptions): Promise<PersonalMemoryGraphResult>;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/** Node/edge authority, mirroring `graphModel.ts`. */
|
|
2
|
+
export type MemoryAuthority = 'canonical' | 'advisory' | 'redline';
|
|
3
|
+
/** Where the content came from. */
|
|
4
|
+
export type MemoryOrigin = 'human' | 'ai' | 'mixed';
|
|
5
|
+
/** Human review state. */
|
|
6
|
+
export type MemoryReviewStatus = 'draft' | 'reviewed' | 'approved';
|
|
7
|
+
/** A node as the guardian needs to see it. Attributes are optional; the loader defaults them. */
|
|
8
|
+
export interface GuardianSourceNode {
|
|
9
|
+
id: string;
|
|
10
|
+
path: string;
|
|
11
|
+
type?: string;
|
|
12
|
+
title?: string;
|
|
13
|
+
authority?: MemoryAuthority;
|
|
14
|
+
internalizable?: boolean;
|
|
15
|
+
origin?: MemoryOrigin;
|
|
16
|
+
reviewStatus?: MemoryReviewStatus;
|
|
17
|
+
tags?: string[];
|
|
18
|
+
sourceRefs?: string[];
|
|
19
|
+
missing?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/** A relation. `docPath` points at the optional relation document in `.graphs/<id>/links/`. */
|
|
22
|
+
export interface GuardianSourceEdge {
|
|
23
|
+
id: string;
|
|
24
|
+
from: string;
|
|
25
|
+
to: string;
|
|
26
|
+
docPath?: string;
|
|
27
|
+
type?: string;
|
|
28
|
+
title?: string;
|
|
29
|
+
authority?: MemoryAuthority;
|
|
30
|
+
internalizable?: boolean;
|
|
31
|
+
origin?: MemoryOrigin;
|
|
32
|
+
reviewStatus?: MemoryReviewStatus;
|
|
33
|
+
tags?: string[];
|
|
34
|
+
sourceRefs?: string[];
|
|
35
|
+
missing?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/** The manifest fields the guardian reads. Extra manifest fields are ignored. */
|
|
38
|
+
export interface GuardianGraphManifest {
|
|
39
|
+
schemaVersion?: number;
|
|
40
|
+
id: string;
|
|
41
|
+
name?: string;
|
|
42
|
+
description?: string;
|
|
43
|
+
scope?: string;
|
|
44
|
+
version: number;
|
|
45
|
+
status?: string;
|
|
46
|
+
owner?: string;
|
|
47
|
+
createdAt?: string;
|
|
48
|
+
updatedAt?: string;
|
|
49
|
+
publishedAt?: string;
|
|
50
|
+
tags?: string[];
|
|
51
|
+
visibility?: string;
|
|
52
|
+
commit?: string;
|
|
53
|
+
sourceCommit?: string;
|
|
54
|
+
}
|
|
55
|
+
/** Structural stand-in for `LoadedGraph`. */
|
|
56
|
+
export interface GuardianGraph {
|
|
57
|
+
manifest: GuardianGraphManifest;
|
|
58
|
+
root: string;
|
|
59
|
+
nodes: GuardianSourceNode[];
|
|
60
|
+
edges: GuardianSourceEdge[];
|
|
61
|
+
/** Published git commit for this graph version, when the caller has one. */
|
|
62
|
+
commit?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Input for one guardian pass. */
|
|
65
|
+
export interface GuardianPassInput {
|
|
66
|
+
graph: GuardianGraph;
|
|
67
|
+
usersRoot: string;
|
|
68
|
+
userIds: string[];
|
|
69
|
+
/**
|
|
70
|
+
* When provided, a `department:<code>` graph is only processed for that
|
|
71
|
+
* department. `global` graphs and graphs without a department scope are
|
|
72
|
+
* always eligible. The caller resolves the user's department.
|
|
73
|
+
*/
|
|
74
|
+
departmentCode?: string;
|
|
75
|
+
/** Injectable clock for deterministic tests. Defaults to `new Date().toISOString()`. */
|
|
76
|
+
now?: string;
|
|
77
|
+
}
|
|
78
|
+
/** Input for a pure upstream-version pass. */
|
|
79
|
+
export interface MarkUpstreamUpdatesInput {
|
|
80
|
+
graph: GuardianGraph;
|
|
81
|
+
usersRoot: string;
|
|
82
|
+
userIds: string[];
|
|
83
|
+
/** Injectable clock used for `updatedAt` in tests. */
|
|
84
|
+
now?: string;
|
|
85
|
+
}
|
|
86
|
+
/** Counters returned by a guardian pass. */
|
|
87
|
+
export interface GuardianResult {
|
|
88
|
+
/** Bindings created as `pending` during discovery (resolved in the same pass). */
|
|
89
|
+
createdPending: number;
|
|
90
|
+
/** Bindings that ended up as a live `subscribe` proxy. */
|
|
91
|
+
subscribed: number;
|
|
92
|
+
/** Bindings that copied a body into the personal memory tree. */
|
|
93
|
+
internalized: number;
|
|
94
|
+
/** Internalize requests refused because of redline / `internalizable: false`. */
|
|
95
|
+
rejectedRedline: number;
|
|
96
|
+
/** Bindings whose `sourceVersion` was refreshed (subscribe, pending, stale). */
|
|
97
|
+
upstreamUpdates: number;
|
|
98
|
+
/** Previously internalized branches revoked after a redline upgrade. */
|
|
99
|
+
revoked: number;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Run one guardian pass: refresh versions, discover new sources for every
|
|
103
|
+
* user, resolve pending bindings (nodes before edges so a relation can be
|
|
104
|
+
* internalized in the same pass as its endpoints), and revoke branches whose
|
|
105
|
+
* source became redline or non-internalizable.
|
|
106
|
+
*/
|
|
107
|
+
export declare function runGuardianPass(input: GuardianPassInput): Promise<GuardianResult>;
|
|
108
|
+
/**
|
|
109
|
+
* Advance `sourceVersion` / `sourceCommit` for every binding of this graph.
|
|
110
|
+
*
|
|
111
|
+
* - `subscribe` and `pending` stay active and immediately follow the graph;
|
|
112
|
+
* - `internalize` becomes `stale`, keeps its local body untouched, and will not
|
|
113
|
+
* follow the source again until the user explicitly re-forks;
|
|
114
|
+
* - `revoked` bindings are left alone.
|
|
115
|
+
*
|
|
116
|
+
* Returns the number of bindings that moved to a newer version.
|
|
117
|
+
*/
|
|
118
|
+
export declare function markUpstreamUpdates(input: MarkUpstreamUpdatesInput): Promise<number>;
|
|
119
|
+
/** Split a Markdown document into its frontmatter block and body. */
|
|
120
|
+
export declare function splitFrontMatter(source: string): {
|
|
121
|
+
frontMatter: string | null;
|
|
122
|
+
body: string;
|
|
123
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-facing `memory_guardian_send` tool.
|
|
3
|
+
*
|
|
4
|
+
* Any authenticated caller may notify their own guardian — the tool is a
|
|
5
|
+
* read-group surface (`kb:file:read`) because it never writes knowledge or
|
|
6
|
+
* another user's state. It is deliberately NOT a target-session API: the tool
|
|
7
|
+
* accepts only a reason, the optional source session/compaction context and a
|
|
8
|
+
* note, then resolves the caller's own guardian through guardian.json. There is
|
|
9
|
+
* no `userId` argument and no arbitrary `target` argument, so a model cannot
|
|
10
|
+
* address another person's guardian.
|
|
11
|
+
*
|
|
12
|
+
* Delivery uses {@link sendToGuardian}, the same path the idle/compaction
|
|
13
|
+
* triggers use. A busy guardian queues the message in DSH's own inbox; this
|
|
14
|
+
* tool adds no queue of its own and returns `queued: true` so the caller knows
|
|
15
|
+
* the guardian will process it after the current turn.
|
|
16
|
+
*
|
|
17
|
+
* @module @sparkelf/dsh-knowledge-base/memory-guardian-tool
|
|
18
|
+
*/
|
|
19
|
+
import { type ToolDefinition } from '@deepseek-ai/dsh-tools';
|
|
20
|
+
import { type GuardianReason, type GuardianSessionHost } from './guardianSession.ts';
|
|
21
|
+
/** The Host seam the tool needs. */
|
|
22
|
+
export interface MemoryGuardianToolHost {
|
|
23
|
+
/** The authenticated caller. Only `userId` is used to find the guardian. */
|
|
24
|
+
resolveCaller(): Promise<{
|
|
25
|
+
userId: string;
|
|
26
|
+
}>;
|
|
27
|
+
/** The guardian session seam shared with the triggers. */
|
|
28
|
+
guardian: GuardianSessionHost;
|
|
29
|
+
/** Whether learning is enabled for this user; paused rejects notify/remember. */
|
|
30
|
+
isEnabled?(userId: string): Promise<boolean> | boolean;
|
|
31
|
+
}
|
|
32
|
+
/** Arguments accepted by `memory_guardian_send`. */
|
|
33
|
+
export interface MemoryGuardianSendArgs {
|
|
34
|
+
reason?: string;
|
|
35
|
+
sessionId?: string;
|
|
36
|
+
note?: string;
|
|
37
|
+
}
|
|
38
|
+
/** Result returned to the model. */
|
|
39
|
+
export interface MemoryGuardianSendValue {
|
|
40
|
+
ok: true;
|
|
41
|
+
reason: GuardianReason;
|
|
42
|
+
delivered: boolean;
|
|
43
|
+
queued: boolean;
|
|
44
|
+
/** The caller's guardian session that received the message. */
|
|
45
|
+
sessionId: string;
|
|
46
|
+
/** True when this call lazily created or recreated the guardian. */
|
|
47
|
+
created: boolean;
|
|
48
|
+
rendered: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Build the `memory_guardian_send` tool.
|
|
52
|
+
*
|
|
53
|
+
* @param host - caller resolver and the guardian session seam.
|
|
54
|
+
* @returns Tool definitions for the disclosure coordinator.
|
|
55
|
+
*/
|
|
56
|
+
export declare function createMemoryGuardianTools(host: MemoryGuardianToolHost): ToolDefinition[];
|