@sparkelf/dsh-knowledge-base 0.5.4 → 0.6.1
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 +57 -0
- package/lib/client.js +1240 -637
- package/lib/client.js.map +1 -1
- package/lib/index.js +1066 -105
- package/lib/types/client/KnowledgeMemory.d.ts +76 -0
- package/lib/types/client/locales.d.ts +33 -0
- package/lib/types/guardianSession.d.ts +23 -0
- package/lib/types/index.d.ts +21 -0
- package/lib/types/memoryPromotion.d.ts +180 -0
- package/lib/types/memoryPromotionRoutes.d.ts +80 -0
- package/lib/types/memoryRoutes.d.ts +7 -1
- package/lib/types/memorySettings.d.ts +83 -12
- package/package.json +1 -1
|
@@ -58,6 +58,65 @@ export interface MemoryListSnapshot {
|
|
|
58
58
|
entries: MemoryEntryView[];
|
|
59
59
|
error?: string;
|
|
60
60
|
}
|
|
61
|
+
/** One promotion as the page shows it. */
|
|
62
|
+
export interface MemoryPromotionView {
|
|
63
|
+
id: string;
|
|
64
|
+
memoryId: string;
|
|
65
|
+
title?: string;
|
|
66
|
+
type?: string;
|
|
67
|
+
/** Promoted body; the review row shows a bounded preview. */
|
|
68
|
+
content?: string;
|
|
69
|
+
status: 'pending' | 'approved' | 'rejected';
|
|
70
|
+
targetScope: 'department' | 'global';
|
|
71
|
+
departmentCode?: string;
|
|
72
|
+
requestedAt?: string;
|
|
73
|
+
reviewedAt?: string;
|
|
74
|
+
reviewNote?: string;
|
|
75
|
+
selfApproval?: boolean;
|
|
76
|
+
}
|
|
77
|
+
/** One queued promotion plus whether this reviewer submitted it. */
|
|
78
|
+
export interface MemoryReviewItem {
|
|
79
|
+
promotion: MemoryPromotionView;
|
|
80
|
+
selfApproval: boolean;
|
|
81
|
+
}
|
|
82
|
+
/** One read-only department/global memory row. */
|
|
83
|
+
export interface SharedMemoryView {
|
|
84
|
+
id: string;
|
|
85
|
+
title: string;
|
|
86
|
+
type?: string;
|
|
87
|
+
content?: string;
|
|
88
|
+
scope: 'department' | 'global';
|
|
89
|
+
departmentCode?: string;
|
|
90
|
+
promotedAt?: string;
|
|
91
|
+
promotedBy?: string;
|
|
92
|
+
}
|
|
93
|
+
/** The promotion state the page renders; absent on a caller without review access. */
|
|
94
|
+
export interface MemoryPromotionUiState {
|
|
95
|
+
promotions: MemoryPromotionView[];
|
|
96
|
+
reviewQueue: MemoryReviewItem[];
|
|
97
|
+
shared: SharedMemoryView[];
|
|
98
|
+
canPromoteGlobal: boolean;
|
|
99
|
+
canPromoteDepartments: string[];
|
|
100
|
+
canReview: boolean;
|
|
101
|
+
busy: boolean;
|
|
102
|
+
error?: string;
|
|
103
|
+
notice?: string;
|
|
104
|
+
/** Open target picker, when the user asked to promote one memory. */
|
|
105
|
+
target?: {
|
|
106
|
+
entry: MemoryEntryView;
|
|
107
|
+
scope: 'department' | 'global';
|
|
108
|
+
confirm: boolean;
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/** Callbacks the container owns for the promotion surface. */
|
|
112
|
+
export interface MemoryPromotionActions {
|
|
113
|
+
onStart: (entry: MemoryEntryView) => void;
|
|
114
|
+
onCancel: () => void;
|
|
115
|
+
onScopeChange: (scope: 'department' | 'global') => void;
|
|
116
|
+
onConfirmChange: (confirm: boolean) => void;
|
|
117
|
+
onSubmit: () => void;
|
|
118
|
+
onReview: (id: string, decision: 'approve' | 'reject') => void;
|
|
119
|
+
}
|
|
61
120
|
/** Props for the pure memory renderer. */
|
|
62
121
|
export interface KnowledgeMemoryViewProps {
|
|
63
122
|
t: (key: string) => string;
|
|
@@ -65,6 +124,19 @@ export interface KnowledgeMemoryViewProps {
|
|
|
65
124
|
enabled: boolean;
|
|
66
125
|
settingsLoading: boolean;
|
|
67
126
|
settingsError?: string;
|
|
127
|
+
/** Saved consolidation interval, in minutes. */
|
|
128
|
+
consolidationIntervalMinutes: number;
|
|
129
|
+
/** Allowed interval bounds; reported by the host so the page never guesses. */
|
|
130
|
+
intervalMin: number;
|
|
131
|
+
intervalMax: number;
|
|
132
|
+
/** The number currently typed in the interval field. */
|
|
133
|
+
intervalDraft: string;
|
|
134
|
+
intervalBusy: boolean;
|
|
135
|
+
intervalError?: string;
|
|
136
|
+
/** True after a successful save until the field changes again. */
|
|
137
|
+
intervalSaved: boolean;
|
|
138
|
+
onIntervalDraftChange: (value: string) => void;
|
|
139
|
+
onSaveInterval: () => void;
|
|
68
140
|
list: MemoryListSnapshot;
|
|
69
141
|
query: string;
|
|
70
142
|
typeFilter: string;
|
|
@@ -102,6 +174,10 @@ export interface KnowledgeMemoryViewProps {
|
|
|
102
174
|
onRetryList: () => void;
|
|
103
175
|
/** Open the source conversation; omitted hides 查看原文. */
|
|
104
176
|
onOpenSource?: (entry: MemoryEntryView) => void;
|
|
177
|
+
/** Promotion state; omitted hides the promotion surface entirely. */
|
|
178
|
+
promotion?: MemoryPromotionUiState;
|
|
179
|
+
/** Promotion handlers; required only when `promotion` is supplied. */
|
|
180
|
+
promotionActions?: MemoryPromotionActions;
|
|
105
181
|
}
|
|
106
182
|
/** Props for the stateful memory page. */
|
|
107
183
|
export interface KnowledgeMemoryProps {
|
|
@@ -679,6 +679,12 @@ export declare const en: {
|
|
|
679
679
|
readonly memoryEnabled: "On";
|
|
680
680
|
readonly memoryPaused: "Paused";
|
|
681
681
|
readonly memoryPausedNotice: "Memory is paused. No new memories are learned; existing memories stay visible and can still be forgotten.";
|
|
682
|
+
readonly memoryIntervalLabel: "Consolidation interval";
|
|
683
|
+
readonly memoryIntervalHint: "15–1440 minutes. When the interval elapses, the guardian consolidates memory.";
|
|
684
|
+
readonly memoryIntervalMinutes: "min";
|
|
685
|
+
readonly memorySaveInterval: "Save";
|
|
686
|
+
readonly memoryIntervalSaved: "Saved";
|
|
687
|
+
readonly memoryIntervalInvalid: "Enter a whole number between 15 and 1440 minutes.";
|
|
682
688
|
readonly memorySettingsFailed: "Could not read the memory setting.";
|
|
683
689
|
readonly memoryEnableTitle: "Turn on memory";
|
|
684
690
|
readonly memoryEnableBody: "When memory is on, the assistant learns preferences, facts, pitfalls and methods from your conversations, and they may affect later answers and actions. Existing memories are kept, and you can pause or forget any single memory at any time.";
|
|
@@ -737,6 +743,33 @@ export declare const en: {
|
|
|
737
743
|
readonly memoryForgetBody: "The memory will no longer appear or affect later answers. It cannot be undone from this page.";
|
|
738
744
|
readonly memoryForgetConfirm: "Forget";
|
|
739
745
|
readonly memoryForgetFailed: "Could not forget this memory.";
|
|
746
|
+
readonly memoryActionPromote: "Promote";
|
|
747
|
+
readonly memoryPromotionPending: "Pending review";
|
|
748
|
+
readonly memoryPromotionApproved: "Promoted";
|
|
749
|
+
readonly memoryPromotionRejected: "Rejected";
|
|
750
|
+
readonly memoryPromotionNone: "—";
|
|
751
|
+
readonly memoryPromotionTargetGlobal: "Global";
|
|
752
|
+
readonly memoryPromotionTargetDepartment: "Department";
|
|
753
|
+
readonly memoryPromotionTarget: "Target";
|
|
754
|
+
readonly memoryPromotionRequested: "Requested";
|
|
755
|
+
readonly memoryReviewTitle: "Promotion requests";
|
|
756
|
+
readonly memoryReviewHint: "Approving copies the memory into the department or global shared memory. You cannot approve your own request.";
|
|
757
|
+
readonly memoryReviewApprove: "Approve";
|
|
758
|
+
readonly memoryReviewReject: "Reject";
|
|
759
|
+
readonly memoryReviewSelfApproval: "Another administrator must approve your own request.";
|
|
760
|
+
readonly memoryReviewApproved: "Approved and promoted.";
|
|
761
|
+
readonly memoryReviewRejected: "Rejected.";
|
|
762
|
+
readonly memorySharedTitle: "Department and global memory";
|
|
763
|
+
readonly memorySharedHint: "Read-only memory promoted by an administrator; personal memories are never listed here.";
|
|
764
|
+
readonly memoryPromoteTitle: "Promote memory";
|
|
765
|
+
readonly memoryPromoteTarget: "Target";
|
|
766
|
+
readonly memoryPromoteSubmit: "Submit for review";
|
|
767
|
+
readonly memoryPromoteConfirmNow: "Promote now";
|
|
768
|
+
readonly memoryPromoteConfirmLabel: "Promote immediately (administrator confirmation)";
|
|
769
|
+
readonly memoryPromoteConfirmHint: "The memory is copied to the target scope now and the action is recorded in the audit trail.";
|
|
770
|
+
readonly memoryPromoteReviewHint: "The request waits for another administrator to review it.";
|
|
771
|
+
readonly memoryPromoteApplied: "Promoted.";
|
|
772
|
+
readonly memoryPromotePending: "Submitted for review.";
|
|
740
773
|
};
|
|
741
774
|
/** Stable key set shared by every knowledge-base locale. */
|
|
742
775
|
export type KnowledgeKey = keyof typeof en;
|
|
@@ -44,6 +44,13 @@ export interface GuardianSessionHost {
|
|
|
44
44
|
usersRoot: string;
|
|
45
45
|
/** Absolute session cwd; defaults to the host process cwd. */
|
|
46
46
|
cwd?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Optional bound for session resolution and message delivery.
|
|
49
|
+
*
|
|
50
|
+
* Omitted keeps the previous unbounded behavior for embedders that already
|
|
51
|
+
* impose their own timeout; the plugin passes {@link DEFAULT_GUARDIAN_TIMEOUT_MS}.
|
|
52
|
+
*/
|
|
53
|
+
timeoutMs?: number;
|
|
47
54
|
/** `ctx.agents.get(sessionId)`. */
|
|
48
55
|
getAgent(sessionId: string): GuardianAgentRef | undefined;
|
|
49
56
|
/** `ctx.agentLoop.createAgent(ctx, { sessionId, meta: { cwd } })` with NO agentPreset. */
|
|
@@ -82,6 +89,22 @@ export interface GuardianSendResult {
|
|
|
82
89
|
/** True when this call lazily created or recreated the session. */
|
|
83
90
|
created: boolean;
|
|
84
91
|
}
|
|
92
|
+
/**
|
|
93
|
+
* Default bound for guardian session resolution and message delivery.
|
|
94
|
+
*
|
|
95
|
+
* The guardian TURN itself is asynchronous (DSH's own inbox is the queue), so
|
|
96
|
+
* this cannot bound model latency. What it does bound is the plugin's wait for
|
|
97
|
+
* a standard-mode agent to be created/resumed or for `followup` to accept the
|
|
98
|
+
* message; without it a hung agent loop can block a normal session's tool call
|
|
99
|
+
* indefinitely.
|
|
100
|
+
*/
|
|
101
|
+
export declare const DEFAULT_GUARDIAN_TIMEOUT_MS = 30000;
|
|
102
|
+
/** A guardian operation exceeded its bounded wait. */
|
|
103
|
+
export declare class GuardianTimeoutError extends Error {
|
|
104
|
+
readonly name = "GuardianTimeoutError";
|
|
105
|
+
readonly code = "unreachable";
|
|
106
|
+
constructor(message: string);
|
|
107
|
+
}
|
|
85
108
|
/** Title is display-only but stable, so an operator can recognize the session. */
|
|
86
109
|
export declare const GUARDIAN_SESSION_TITLE = "\u8BB0\u5FC6\u5B88\u62A4\u4F1A\u8BDD";
|
|
87
110
|
/** Skill the guardian must load before doing anything else. */
|
package/lib/types/index.d.ts
CHANGED
|
@@ -92,6 +92,18 @@ export interface Config {
|
|
|
92
92
|
memorySkillsRoot: string;
|
|
93
93
|
/** Idle minutes before a normal session notifies the memory guardian. */
|
|
94
94
|
memoryIdleMinutes: number;
|
|
95
|
+
/**
|
|
96
|
+
* DataOps permission whose holders start memory enabled when they have no
|
|
97
|
+
* settings file yet.
|
|
98
|
+
*
|
|
99
|
+
* The default `kb:config:manage` is granted by the DataOps permission
|
|
100
|
+
* manifest to the global administrator and super administrator roles only.
|
|
101
|
+
* A deployment can point this at a stricter key, or set it to an empty string
|
|
102
|
+
* to disable the default entirely; the KB package never hard-codes a role
|
|
103
|
+
* name. An explicit `memoryEnabled: false` in the user's settings file always
|
|
104
|
+
* wins over this default.
|
|
105
|
+
*/
|
|
106
|
+
memoryDefaultEnabledPermission: string;
|
|
95
107
|
/** Comma-separated export spec of clients allowed to mount the NFS export. */
|
|
96
108
|
permitted: string;
|
|
97
109
|
/** Host serving the export, as an address or `address:/path` pseudo root. */
|
|
@@ -707,6 +719,15 @@ export interface KnowledgeCaller {
|
|
|
707
719
|
departmentPathCodes?: string[];
|
|
708
720
|
/** The organization path's display names, index-aligned with the codes. */
|
|
709
721
|
departmentPathNames?: string[];
|
|
722
|
+
/**
|
|
723
|
+
* DataOps permission keys resolved for the caller by `/auth/me`.
|
|
724
|
+
*
|
|
725
|
+
* Used by product defaults that must follow the existing permission model
|
|
726
|
+
* (for example the administrator memory default). Authorization decisions
|
|
727
|
+
* still go through the backend; this is the projection the plugin already
|
|
728
|
+
* receives, not a second policy source.
|
|
729
|
+
*/
|
|
730
|
+
permissions?: string[];
|
|
710
731
|
}
|
|
711
732
|
/**
|
|
712
733
|
* Read a `{ code, name }` pair DataOps nests under the profile.
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import type { AnyMemoryEntry, MemoryType, Privacy } from './memoryTypes.ts';
|
|
2
|
+
import type { ListedMemoryEntry } from './memoryStore.ts';
|
|
3
|
+
/** The two scopes a personal memory may be promoted into. */
|
|
4
|
+
export declare const PROMOTION_TARGET_SCOPES: readonly ["department", "global"];
|
|
5
|
+
export type PromotionTargetScope = typeof PROMOTION_TARGET_SCOPES[number];
|
|
6
|
+
/** Review lifecycle. `approved` and `rejected` are terminal. */
|
|
7
|
+
export declare const PROMOTION_STATUSES: readonly ["pending", "approved", "rejected"];
|
|
8
|
+
export type PromotionStatus = typeof PROMOTION_STATUSES[number];
|
|
9
|
+
/** Graph id that owns promoted department/global memory inside one scope. */
|
|
10
|
+
export declare const SHARED_MEMORY_GRAPH_ID = "shared-memory";
|
|
11
|
+
/** Folder (relative to the scope root) that holds one document per promotion. */
|
|
12
|
+
export declare const SHARED_MEMORY_FOLDER = "\u8BB0\u5FC6\u5171\u4EAB";
|
|
13
|
+
/** Display name per graph scope. */
|
|
14
|
+
export declare function sharedMemoryGraphName(scope: PromotionTargetScope): string;
|
|
15
|
+
/** A structured refusal the route maps to a status code. */
|
|
16
|
+
export declare class MemoryPromotionError extends Error {
|
|
17
|
+
readonly code: 'invalid' | 'forbidden' | 'not-found' | 'conflict' | 'privacy' | 'invalid-graph' | 'io';
|
|
18
|
+
readonly name = "MemoryPromotionError";
|
|
19
|
+
constructor(code: 'invalid' | 'forbidden' | 'not-found' | 'conflict' | 'privacy' | 'invalid-graph' | 'io', message: string);
|
|
20
|
+
}
|
|
21
|
+
/** One actor in the promotion audit trail. */
|
|
22
|
+
export interface PromotionActor {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
readonly name: string;
|
|
25
|
+
}
|
|
26
|
+
/** One durable step in a promotion's history. */
|
|
27
|
+
export interface PromotionAuditEntry {
|
|
28
|
+
readonly at: string;
|
|
29
|
+
readonly actorId: string;
|
|
30
|
+
readonly actorName: string;
|
|
31
|
+
readonly action: 'requested' | 'confirmed' | 'approved' | 'rejected';
|
|
32
|
+
readonly detail?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
/** The personal entry a promotion was made from. */
|
|
35
|
+
export interface PromotionSource {
|
|
36
|
+
readonly userId: string;
|
|
37
|
+
readonly userName?: string;
|
|
38
|
+
readonly memoryId: string;
|
|
39
|
+
readonly memoryType: MemoryType;
|
|
40
|
+
readonly title: string;
|
|
41
|
+
readonly content: string;
|
|
42
|
+
readonly privacy: Privacy;
|
|
43
|
+
readonly sourceLabel?: string;
|
|
44
|
+
}
|
|
45
|
+
/** Where the promotion lands. `departmentPath` is snapshotted at request time. */
|
|
46
|
+
export interface PromotionTarget {
|
|
47
|
+
readonly scope: PromotionTargetScope;
|
|
48
|
+
readonly departmentCode?: string;
|
|
49
|
+
readonly departmentName?: string;
|
|
50
|
+
readonly departmentPath?: readonly string[];
|
|
51
|
+
readonly graphId: typeof SHARED_MEMORY_GRAPH_ID;
|
|
52
|
+
readonly graphName: string;
|
|
53
|
+
/** Scope-relative document path (`记忆共享/<id>.md`). */
|
|
54
|
+
readonly docPath: string;
|
|
55
|
+
}
|
|
56
|
+
/** One promotion as stored. */
|
|
57
|
+
export interface MemoryPromotionRecord {
|
|
58
|
+
readonly id: string;
|
|
59
|
+
readonly status: PromotionStatus;
|
|
60
|
+
readonly source: PromotionSource;
|
|
61
|
+
readonly target: PromotionTarget;
|
|
62
|
+
readonly requestedBy: PromotionActor;
|
|
63
|
+
readonly requestedAt: string;
|
|
64
|
+
readonly reviewedBy?: PromotionActor;
|
|
65
|
+
readonly reviewedAt?: string;
|
|
66
|
+
readonly reviewNote?: string;
|
|
67
|
+
/** True when an administrator applied it directly instead of the review queue. */
|
|
68
|
+
readonly confirmed: boolean;
|
|
69
|
+
readonly appliedAt?: string;
|
|
70
|
+
readonly audit: readonly PromotionAuditEntry[];
|
|
71
|
+
}
|
|
72
|
+
/** The whole store file. */
|
|
73
|
+
export interface MemoryPromotionStore {
|
|
74
|
+
readonly version: 1;
|
|
75
|
+
readonly promotions: readonly MemoryPromotionRecord[];
|
|
76
|
+
}
|
|
77
|
+
/** The caller projection a promotion decision needs. */
|
|
78
|
+
export interface PromotionReviewer {
|
|
79
|
+
readonly userId: string;
|
|
80
|
+
readonly name: string;
|
|
81
|
+
/** Administrator/super administrator: sees and manages every scope. */
|
|
82
|
+
readonly isGlobalAdmin: boolean;
|
|
83
|
+
/** Holds the DataOps `kb:config:manage` permission. */
|
|
84
|
+
readonly canManageConfig: boolean;
|
|
85
|
+
readonly departmentCode?: string;
|
|
86
|
+
readonly departmentName?: string;
|
|
87
|
+
readonly departmentPathCodes?: readonly string[];
|
|
88
|
+
/** Departments whose review queue this caller may decide. */
|
|
89
|
+
readonly reviewDepartments: readonly string[];
|
|
90
|
+
/** Extra departments the backend's data scope grants. */
|
|
91
|
+
readonly visibleDepartmentCodes?: readonly string[];
|
|
92
|
+
}
|
|
93
|
+
/** A scope decision, with the department path the write will use. */
|
|
94
|
+
export type PromotionScopeDecision = {
|
|
95
|
+
readonly ok: true;
|
|
96
|
+
readonly departmentCode?: string;
|
|
97
|
+
readonly departmentPath?: readonly string[];
|
|
98
|
+
} | {
|
|
99
|
+
readonly ok: false;
|
|
100
|
+
readonly code: 'forbidden' | 'invalid';
|
|
101
|
+
readonly message: string;
|
|
102
|
+
};
|
|
103
|
+
/** The submitter may request a promotion only inside a scope they can review. */
|
|
104
|
+
export declare function decidePromotionScope(reviewer: PromotionReviewer, scope: PromotionTargetScope, departmentCode?: string): PromotionScopeDecision;
|
|
105
|
+
/** Whether one reviewer may decide a promotion aimed at its target scope. */
|
|
106
|
+
export declare function canReviewPromotion(reviewer: PromotionReviewer, target: PromotionTarget): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Whether one approved promotion is visible to a viewer.
|
|
109
|
+
*
|
|
110
|
+
* A department promotion is visible to the viewer whose organization path contains
|
|
111
|
+
* that department (own department and its ancestors) or whose data scope names it.
|
|
112
|
+
* The source owner always keeps seeing their own approved memory.
|
|
113
|
+
*/
|
|
114
|
+
export declare function isPromotionVisibleTo(record: MemoryPromotionRecord, viewer: PromotionReviewer): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* The scope root one target writes under, WITHOUT a trailing slash.
|
|
117
|
+
*
|
|
118
|
+
* DataOps grants are matched against this exact resource id (a trailing slash is a
|
|
119
|
+
* different id and matches nothing), so every permission question must use this
|
|
120
|
+
* spelling. Directory joining adds the slash separately.
|
|
121
|
+
*/
|
|
122
|
+
export declare function targetScopeRoot(target: PromotionTarget): string;
|
|
123
|
+
/** The scope-relative prefix one target writes under (`global/`, `department/A/B/`). */
|
|
124
|
+
export declare function targetScopePrefix(target: PromotionTarget): string;
|
|
125
|
+
/** Where the review queue lives for one deployment's per-user memory root. */
|
|
126
|
+
export declare function promotionStorePath(memoryUsersRoot: string): string;
|
|
127
|
+
/** Map a stored memory type onto the graph's smaller node vocabulary. */
|
|
128
|
+
export declare function sharedNodeTypeOf(type: MemoryType): 'policy' | 'fact' | 'procedure' | 'case' | 'skill' | 'topic';
|
|
129
|
+
/** The text a promotion copies into the shared document. */
|
|
130
|
+
export declare function promotionContentOf(entry: AnyMemoryEntry): string;
|
|
131
|
+
/** The short title a shared row shows; mirrors the personal page's rules. */
|
|
132
|
+
export declare function promotionTitleOf(entry: ListedMemoryEntry): string;
|
|
133
|
+
/** Read the queue, treating absence as empty and quarantining corrupt JSON. */
|
|
134
|
+
export declare function readPromotionStore(path: string): Promise<MemoryPromotionStore>;
|
|
135
|
+
/** Commit the queue atomically. */
|
|
136
|
+
export declare function writePromotionStore(path: string, store: MemoryPromotionStore): Promise<void>;
|
|
137
|
+
/** Read every promotion, oldest first. */
|
|
138
|
+
export declare function listPromotionRecords(path: string): Promise<readonly MemoryPromotionRecord[]>;
|
|
139
|
+
/** The validated pieces of a new request, before an id or timestamps are assigned. */
|
|
140
|
+
export interface PromotionDraft {
|
|
141
|
+
readonly source: PromotionSource;
|
|
142
|
+
readonly target: PromotionTarget;
|
|
143
|
+
}
|
|
144
|
+
/** Reject a request that cannot be shared safely or written to a graph. */
|
|
145
|
+
export declare function normalizePromotionDraft(draft: PromotionDraft): PromotionDraft;
|
|
146
|
+
/** Create one pending request, or return the identical pending one. */
|
|
147
|
+
export declare function createPromotionRequest(path: string, draft: PromotionDraft, actor: PromotionActor, now?: () => Date): Promise<{
|
|
148
|
+
record: MemoryPromotionRecord;
|
|
149
|
+
duplicate: boolean;
|
|
150
|
+
}>;
|
|
151
|
+
/** Load one promotion by id. */
|
|
152
|
+
export declare function getPromotionRecord(path: string, id: string): Promise<MemoryPromotionRecord | undefined>;
|
|
153
|
+
/** Mark one pending promotion approved after its shared document is on disk. */
|
|
154
|
+
export declare function markPromotionApproved(path: string, id: string, reviewer: PromotionActor, appliedPath: string, options: {
|
|
155
|
+
confirmed: boolean;
|
|
156
|
+
note?: string;
|
|
157
|
+
}, now?: () => Date): Promise<MemoryPromotionRecord>;
|
|
158
|
+
/** Mark one pending promotion rejected, keeping the reason. */
|
|
159
|
+
export declare function markPromotionRejected(path: string, id: string, reviewer: PromotionActor, note: string | undefined, now?: () => Date): Promise<MemoryPromotionRecord>;
|
|
160
|
+
/** Compose the shared document: front matter for the graph loader, then the body. */
|
|
161
|
+
export declare function renderSharedMemoryDocument(record: MemoryPromotionRecord, now: string): string;
|
|
162
|
+
/** The result of materializing one approved promotion. */
|
|
163
|
+
export interface AppliedPromotion {
|
|
164
|
+
readonly docPath: string;
|
|
165
|
+
readonly graphPath: string;
|
|
166
|
+
readonly graphId: string;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Write an approved promotion into the target scope and register it in the shared
|
|
170
|
+
* memory graph.
|
|
171
|
+
*
|
|
172
|
+
* The document is written first and the manifest second: if the manifest write
|
|
173
|
+
* fails, the next approval retries both and the orphan document is harmless (it is
|
|
174
|
+
* not referenced). The reverse order would publish a broken node.
|
|
175
|
+
*/
|
|
176
|
+
export declare function applyPromotionToSharedMemory(input: {
|
|
177
|
+
record: MemoryPromotionRecord;
|
|
178
|
+
scopeRoot: string;
|
|
179
|
+
now?: string;
|
|
180
|
+
}): Promise<AppliedPromotion>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP seam for memory promotion and shared (department/global) memory.
|
|
3
|
+
*
|
|
4
|
+
* `memoryRoutes.ts` owns the caller's personal page. This module owns the one
|
|
5
|
+
* operation that crosses a scope boundary — promoting a personal memory into a
|
|
6
|
+
* department or global memory — plus the review queue and the read-only shared
|
|
7
|
+
* list. It is separate because the trust question is different: the personal page
|
|
8
|
+
* acts on the caller's own tree, while every promotion decision needs a reviewer
|
|
9
|
+
* projection and a DataOps write/read decision for the TARGET path.
|
|
10
|
+
*
|
|
11
|
+
* Authorization has two layers, on purpose:
|
|
12
|
+
*
|
|
13
|
+
* 1. the product layer ({@link decidePromotionScope}, {@link canReviewPromotion})
|
|
14
|
+
* answers "is this reviewer's product scope the one this record targets", which
|
|
15
|
+
* is what makes the queue and the 403 messages understandable;
|
|
16
|
+
* 2. DataOps stays authoritative for the write and the read through the injected
|
|
17
|
+
* `canWrite` / `canRead` seams. A projection that disagrees with the backend
|
|
18
|
+
* therefore narrows access, never widens it.
|
|
19
|
+
*
|
|
20
|
+
* The submitter is never allowed to approve their own pending request. An
|
|
21
|
+
* administrator who wants to share their own memory uses `confirm: true`, which is
|
|
22
|
+
* recorded as a distinct `confirmed` audit action.
|
|
23
|
+
*
|
|
24
|
+
* @module @sparkelf/dsh-knowledge-base/memory-promotion-routes
|
|
25
|
+
*/
|
|
26
|
+
import type { MemoryStore } from './memoryStore.ts';
|
|
27
|
+
import { SHARED_MEMORY_FOLDER, SHARED_MEMORY_GRAPH_ID, type MemoryPromotionRecord, type PromotionActor, type PromotionReviewer, type PromotionTargetScope } from './memoryPromotion.ts';
|
|
28
|
+
/** The methods this handler answers. */
|
|
29
|
+
export type MemoryPromotionMethod = 'memory.promote' | 'memory.promotions' | 'memory.review-promotion' | 'memory.shared';
|
|
30
|
+
/** Injected filesystem/permission seams; tests use plain functions. */
|
|
31
|
+
export interface MemoryPromotionRouteInput {
|
|
32
|
+
readonly method: MemoryPromotionMethod;
|
|
33
|
+
/** Parsed JSON body; methods without one may omit it. */
|
|
34
|
+
readonly body?: unknown;
|
|
35
|
+
readonly actor: PromotionActor;
|
|
36
|
+
readonly reviewer: PromotionReviewer;
|
|
37
|
+
/** The caller's personal memory store; the source entry is read from here. */
|
|
38
|
+
readonly store: Pick<MemoryStore, 'getEntry'>;
|
|
39
|
+
/** Absolute path of the promotion queue JSON file. */
|
|
40
|
+
readonly queuePath: string;
|
|
41
|
+
/** Authoritative knowledge root (`/nas/kb/actual`). */
|
|
42
|
+
readonly actualRoot: string;
|
|
43
|
+
/** DataOps write decision for one scope-relative path. */
|
|
44
|
+
readonly canWrite?: (relPath: string) => Promise<boolean>;
|
|
45
|
+
/** DataOps read decision for one scope-relative path. */
|
|
46
|
+
readonly canRead?: (relPath: string) => Promise<boolean>;
|
|
47
|
+
/** Injectable clock for deterministic tests. */
|
|
48
|
+
readonly now?: () => Date;
|
|
49
|
+
}
|
|
50
|
+
/** One response the route switch can pass straight to `writeJson`. */
|
|
51
|
+
export interface MemoryPromotionRouteResponse {
|
|
52
|
+
readonly status: number;
|
|
53
|
+
readonly body: Record<string, unknown>;
|
|
54
|
+
}
|
|
55
|
+
/** The externally visible shape of one promotion; never the source user id. */
|
|
56
|
+
export declare function publicPromotion(record: MemoryPromotionRecord, viewerId: string): Record<string, unknown>;
|
|
57
|
+
/** One read-only row of the department/global shared list. */
|
|
58
|
+
export interface SharedMemoryRow {
|
|
59
|
+
id: string;
|
|
60
|
+
title: string;
|
|
61
|
+
type: string;
|
|
62
|
+
content: string;
|
|
63
|
+
scope: PromotionTargetScope;
|
|
64
|
+
departmentCode?: string;
|
|
65
|
+
departmentName?: string;
|
|
66
|
+
promotedAt: string;
|
|
67
|
+
promotedBy?: string;
|
|
68
|
+
}
|
|
69
|
+
/** Build the row without any personal identifier or source session. */
|
|
70
|
+
export declare function sharedRowOf(record: MemoryPromotionRecord): SharedMemoryRow;
|
|
71
|
+
/**
|
|
72
|
+
* Handle one promotion/shared-memory method.
|
|
73
|
+
*
|
|
74
|
+
* Expected failures are returned as status/body pairs. Unexpected errors are
|
|
75
|
+
* rethrown so the route's outer handler reports a real 500 rather than a
|
|
76
|
+
* misleading 400.
|
|
77
|
+
*/
|
|
78
|
+
export declare function handleMemoryPromotionRoute(input: MemoryPromotionRouteInput): Promise<MemoryPromotionRouteResponse>;
|
|
79
|
+
/** Re-exported so the host can build the same target shape in its tests. */
|
|
80
|
+
export { SHARED_MEMORY_GRAPH_ID, SHARED_MEMORY_FOLDER, type PromotionTargetScope };
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* `source.sessionId` only because the detail drawer needs it for 「查看原文」.
|
|
18
18
|
*
|
|
19
19
|
* Methods: `memory.list`, `memory.get`, `memory.update`, `memory.forget`,
|
|
20
|
-
* `memory.settings`, `memory.set-enabled`.
|
|
20
|
+
* `memory.settings`, `memory.set-enabled`, `memory.save-settings`.
|
|
21
21
|
*
|
|
22
22
|
* @module @sparkelf/dsh-knowledge-base/memory-routes
|
|
23
23
|
*/
|
|
@@ -38,6 +38,12 @@ export interface MemoryRouteInput {
|
|
|
38
38
|
caller: MemoryRouteCaller;
|
|
39
39
|
store: MemoryStore;
|
|
40
40
|
settings: MemorySettingsStore;
|
|
41
|
+
/**
|
|
42
|
+
* First-read default for this caller, already resolved from the DataOps
|
|
43
|
+
* principal by the host. Omitted means the store's injected policy decides;
|
|
44
|
+
* `false` is a real value and must not be confused with "not supplied".
|
|
45
|
+
*/
|
|
46
|
+
defaultEnabled?: boolean;
|
|
41
47
|
/** Manifest reconciliation after a write/delete; failure never undoes the memory write. */
|
|
42
48
|
graphWriter: Pick<MemoryGraphWriter, 'syncAll'>;
|
|
43
49
|
}
|
|
@@ -1,40 +1,111 @@
|
|
|
1
1
|
/** File name inside one user's directory. */
|
|
2
2
|
export declare const MEMORY_SETTINGS_FILE = "settings.json";
|
|
3
|
+
/** Current persisted schema version. */
|
|
4
|
+
export declare const MEMORY_SETTINGS_VERSION = 2;
|
|
5
|
+
/** Shortest allowed consolidation interval, in minutes. */
|
|
6
|
+
export declare const MEMORY_CONSOLIDATION_INTERVAL_MIN = 15;
|
|
7
|
+
/** Longest allowed consolidation interval, in minutes. */
|
|
8
|
+
export declare const MEMORY_CONSOLIDATION_INTERVAL_MAX = 1440;
|
|
9
|
+
/** Default consolidation interval, in minutes. */
|
|
10
|
+
export declare const MEMORY_CONSOLIDATION_INTERVAL_DEFAULT = 60;
|
|
3
11
|
/** Persisted settings shape. */
|
|
4
12
|
export interface MemorySettings {
|
|
5
|
-
version:
|
|
13
|
+
version: typeof MEMORY_SETTINGS_VERSION;
|
|
6
14
|
/** `false` means paused: no new memories are learned. */
|
|
7
15
|
memoryEnabled: boolean;
|
|
16
|
+
/** How often the guardian consolidates memory, in minutes (15–1440). */
|
|
17
|
+
consolidationIntervalMinutes: number;
|
|
8
18
|
}
|
|
9
19
|
/** The paused default for a user who has never confirmed the enable dialog. */
|
|
10
20
|
export declare const DEFAULT_MEMORY_SETTINGS: Readonly<MemorySettings>;
|
|
21
|
+
/** A malformed interval or write patch; the route maps this to HTTP 400. */
|
|
22
|
+
export declare class MemorySettingsError extends Error {
|
|
23
|
+
readonly code: 'invalid-interval' | 'invalid-settings';
|
|
24
|
+
readonly name = "MemorySettingsError";
|
|
25
|
+
constructor(code: 'invalid-interval' | 'invalid-settings', message: string);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Where the first-read default for a principal comes from.
|
|
29
|
+
*
|
|
30
|
+
* The store deliberately does not know what an administrator is. The Host
|
|
31
|
+
* injects this seam with the existing DataOps principal projection, so the KB
|
|
32
|
+
* package keeps no hard-coded role names and a deployment can change the rule
|
|
33
|
+
* without editing this module.
|
|
34
|
+
*/
|
|
35
|
+
export interface MemorySettingsDefaultPolicy {
|
|
36
|
+
/**
|
|
37
|
+
* Whether a user with no settings file starts enabled.
|
|
38
|
+
*
|
|
39
|
+
* A rejection is treated as "paused": a backend hiccup must not turn memory on
|
|
40
|
+
* silently, and it must not make the settings page unreadable either.
|
|
41
|
+
*/
|
|
42
|
+
memoryEnabled(userId: string): boolean | Promise<boolean>;
|
|
43
|
+
}
|
|
44
|
+
/** Optional per-read override, used by the route that already resolved the principal. */
|
|
45
|
+
export interface MemorySettingsReadOptions {
|
|
46
|
+
/** Explicit first-read default; when omitted the constructor policy decides. */
|
|
47
|
+
defaultEnabled?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/** A partial write; at least one field must be present. */
|
|
50
|
+
export interface MemorySettingsPatch {
|
|
51
|
+
memoryEnabled?: boolean;
|
|
52
|
+
consolidationIntervalMinutes?: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Validate one consolidation interval.
|
|
56
|
+
*
|
|
57
|
+
* The guard is strict on purpose: a fractional value or a value outside
|
|
58
|
+
* 15–1440 is a client bug, not a number to clamp silently. The route turns this
|
|
59
|
+
* error into a 400 so the page can show the allowed range.
|
|
60
|
+
*
|
|
61
|
+
* @param value - raw value from an API body or a hand-edited file.
|
|
62
|
+
* @returns the accepted integer.
|
|
63
|
+
* @throws {MemorySettingsError} when the value is not an integer in range.
|
|
64
|
+
*/
|
|
65
|
+
export declare function normalizeConsolidationInterval(value: unknown): number;
|
|
11
66
|
/**
|
|
12
67
|
* Atomic settings store over `<usersRoot>/<userId>/settings.json`.
|
|
13
68
|
*
|
|
14
69
|
* `read()` tolerates a corrupt file by returning the paused default; the next
|
|
15
|
-
*
|
|
16
|
-
*
|
|
70
|
+
* write rewrites it. This keeps the memory page usable after a partial write or
|
|
71
|
+
* a hand-edited file instead of failing the whole route.
|
|
17
72
|
*/
|
|
18
73
|
export declare class MemorySettingsStore {
|
|
19
74
|
private readonly usersRoot;
|
|
20
|
-
|
|
75
|
+
private readonly defaultPolicy?;
|
|
76
|
+
constructor(usersRoot: string, defaultPolicy?: MemorySettingsDefaultPolicy);
|
|
21
77
|
/** Absolute settings file for one user. */
|
|
22
78
|
filePath(userId: string): string;
|
|
79
|
+
/** The first-read default for one user, from the injected policy. */
|
|
80
|
+
private defaultFor;
|
|
23
81
|
/**
|
|
24
82
|
* Read one user's settings.
|
|
25
83
|
*
|
|
26
|
-
* Missing
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
84
|
+
* Missing content uses {@link MemorySettingsReadOptions.defaultEnabled} or the
|
|
85
|
+
* injected policy. Malformed content stays paused even for an administrator:
|
|
86
|
+
* a corrupt file is not evidence that the administrator opted in. A file with
|
|
87
|
+
* a boolean `memoryEnabled` is accepted even if `version`/the interval is
|
|
88
|
+
* missing, so old settings files keep their explicit choice.
|
|
30
89
|
*/
|
|
31
|
-
read(userId: string): Promise<MemorySettings>;
|
|
90
|
+
read(userId: string, options?: MemorySettingsReadOptions): Promise<MemorySettings>;
|
|
32
91
|
/** Whether learning is enabled for this user (default `false`). */
|
|
33
|
-
isEnabled(userId: string): Promise<boolean>;
|
|
92
|
+
isEnabled(userId: string, options?: MemorySettingsReadOptions): Promise<boolean>;
|
|
93
|
+
/**
|
|
94
|
+
* Persist a partial settings change in one atomic write.
|
|
95
|
+
*
|
|
96
|
+
* The current value is read first so a page that saves only the interval
|
|
97
|
+
* cannot accidentally pause memory (nor enable it). An explicit boolean in
|
|
98
|
+
* the patch always wins; an absent field is preserved.
|
|
99
|
+
*
|
|
100
|
+
* @returns the normalized settings actually written.
|
|
101
|
+
*/
|
|
102
|
+
save(userId: string, patch: MemorySettingsPatch, options?: MemorySettingsReadOptions): Promise<MemorySettings>;
|
|
34
103
|
/**
|
|
35
|
-
* Persist the enable/pause switch atomically.
|
|
104
|
+
* Persist the enable/pause switch atomically, preserving the interval.
|
|
36
105
|
*
|
|
37
106
|
* @returns the normalized settings actually written.
|
|
38
107
|
*/
|
|
39
|
-
setEnabled(userId: string, enabled: boolean): Promise<MemorySettings>;
|
|
108
|
+
setEnabled(userId: string, enabled: boolean, options?: MemorySettingsReadOptions): Promise<MemorySettings>;
|
|
109
|
+
/** Persist one interval while preserving the switch. */
|
|
110
|
+
setConsolidationInterval(userId: string, minutes: number, options?: MemorySettingsReadOptions): Promise<MemorySettings>;
|
|
40
111
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sparkelf/dsh-knowledge-base",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
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",
|