@lssm/example.versioned-knowledge-base 0.0.0-canary-20251213172311
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/.turbo/turbo-build.log +23 -0
- package/CHANGELOG.md +9 -0
- package/README.md +24 -0
- package/dist/contracts/index.js +1 -0
- package/dist/contracts/kb.js +1 -0
- package/dist/docs/index.js +1 -0
- package/dist/docs/versioned-knowledge-base.docblock.js +20 -0
- package/dist/entities/index.js +1 -0
- package/dist/entities/models.js +1 -0
- package/dist/events.js +1 -0
- package/dist/example.js +1 -0
- package/dist/feature.js +1 -0
- package/dist/handlers/index.js +1 -0
- package/dist/handlers/memory.handlers.js +1 -0
- package/dist/index.js +1 -0
- package/example.ts +1 -0
- package/package.json +64 -0
- package/src/contracts/index.ts +3 -0
- package/src/contracts/kb.ts +191 -0
- package/src/docs/index.ts +3 -0
- package/src/docs/versioned-knowledge-base.docblock.ts +31 -0
- package/src/entities/index.ts +3 -0
- package/src/entities/models.ts +70 -0
- package/src/events.ts +72 -0
- package/src/example.ts +29 -0
- package/src/feature.ts +35 -0
- package/src/handlers/index.ts +3 -0
- package/src/handlers/memory.handlers.test.ts +79 -0
- package/src/handlers/memory.handlers.ts +206 -0
- package/src/index.ts +15 -0
- package/tsconfig.json +11 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsdown.config.js +9 -0
package/src/feature.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { FeatureModuleSpec } from '@lssm/lib.contracts';
|
|
2
|
+
|
|
3
|
+
export const VersionedKnowledgeBaseFeature: FeatureModuleSpec = {
|
|
4
|
+
meta: {
|
|
5
|
+
key: 'versioned-knowledge-base',
|
|
6
|
+
title: 'Versioned Knowledge Base',
|
|
7
|
+
description:
|
|
8
|
+
'Curated KB with immutable sources, rule versions, and published snapshots.',
|
|
9
|
+
domain: 'knowledge',
|
|
10
|
+
owners: ['examples'],
|
|
11
|
+
tags: ['knowledge', 'versioning', 'snapshots'],
|
|
12
|
+
stability: 'experimental',
|
|
13
|
+
},
|
|
14
|
+
operations: [
|
|
15
|
+
{ name: 'kb.ingestSource', version: 1 },
|
|
16
|
+
{ name: 'kb.upsertRuleVersion', version: 1 },
|
|
17
|
+
{ name: 'kb.approveRuleVersion', version: 1 },
|
|
18
|
+
{ name: 'kb.publishSnapshot', version: 1 },
|
|
19
|
+
{ name: 'kb.search', version: 1 },
|
|
20
|
+
],
|
|
21
|
+
events: [
|
|
22
|
+
{ name: 'kb.source.ingested', version: 1 },
|
|
23
|
+
{ name: 'kb.ruleVersion.created', version: 1 },
|
|
24
|
+
{ name: 'kb.ruleVersion.approved', version: 1 },
|
|
25
|
+
{ name: 'kb.snapshot.published', version: 1 },
|
|
26
|
+
],
|
|
27
|
+
presentations: [],
|
|
28
|
+
opToPresentation: [],
|
|
29
|
+
presentationsTargets: [],
|
|
30
|
+
capabilities: {
|
|
31
|
+
requires: [{ key: 'knowledge', version: 1 }],
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { createMemoryKbHandlers, createMemoryKbStore } from './memory.handlers';
|
|
4
|
+
|
|
5
|
+
describe('@lssm/example.versioned-knowledge-base memory handlers', () => {
|
|
6
|
+
it('requires sourceRefs for rule versions (traceability)', async () => {
|
|
7
|
+
const store = createMemoryKbStore();
|
|
8
|
+
const kb = createMemoryKbHandlers(store);
|
|
9
|
+
await kb.createRule({ id: 'rule_1', jurisdiction: 'EU', topicKey: 't1' });
|
|
10
|
+
await expect(
|
|
11
|
+
kb.upsertRuleVersion({ ruleId: 'rule_1', content: 'x', sourceRefs: [] })
|
|
12
|
+
).rejects.toThrow('SOURCE_REFS_REQUIRED');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('snapshot includes only approved rule versions for that jurisdiction', async () => {
|
|
16
|
+
const store = createMemoryKbStore();
|
|
17
|
+
const kb = createMemoryKbHandlers(store);
|
|
18
|
+
|
|
19
|
+
await kb.createRule({ id: 'rule_eu', jurisdiction: 'EU', topicKey: 'tax' });
|
|
20
|
+
await kb.createRule({ id: 'rule_fr', jurisdiction: 'FR', topicKey: 'tax' });
|
|
21
|
+
|
|
22
|
+
const euDraft = await kb.upsertRuleVersion({
|
|
23
|
+
ruleId: 'rule_eu',
|
|
24
|
+
content: 'EU rule content',
|
|
25
|
+
sourceRefs: [{ sourceDocumentId: 'src1' }],
|
|
26
|
+
});
|
|
27
|
+
const frDraft = await kb.upsertRuleVersion({
|
|
28
|
+
ruleId: 'rule_fr',
|
|
29
|
+
content: 'FR rule content',
|
|
30
|
+
sourceRefs: [{ sourceDocumentId: 'src2' }],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const euApproved = await kb.approveRuleVersion({
|
|
34
|
+
ruleVersionId: euDraft.id,
|
|
35
|
+
approver: 'expert_1',
|
|
36
|
+
});
|
|
37
|
+
await kb.approveRuleVersion({
|
|
38
|
+
ruleVersionId: frDraft.id,
|
|
39
|
+
approver: 'expert_1',
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const snapshot = await kb.publishSnapshot({
|
|
43
|
+
jurisdiction: 'EU',
|
|
44
|
+
asOfDate: new Date('2026-01-01T00:00:00.000Z'),
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
expect(snapshot.includedRuleVersionIds).toEqual([euApproved.id]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('search is scoped to snapshot + jurisdiction', async () => {
|
|
51
|
+
const store = createMemoryKbStore();
|
|
52
|
+
const kb = createMemoryKbHandlers(store);
|
|
53
|
+
|
|
54
|
+
await kb.createRule({ id: 'rule_eu', jurisdiction: 'EU', topicKey: 'topic' });
|
|
55
|
+
const euDraft = await kb.upsertRuleVersion({
|
|
56
|
+
ruleId: 'rule_eu',
|
|
57
|
+
content: 'This is about reporting obligations',
|
|
58
|
+
sourceRefs: [{ sourceDocumentId: 'src1' }],
|
|
59
|
+
});
|
|
60
|
+
await kb.approveRuleVersion({ ruleVersionId: euDraft.id, approver: 'u1' });
|
|
61
|
+
const snap = await kb.publishSnapshot({
|
|
62
|
+
jurisdiction: 'EU',
|
|
63
|
+
asOfDate: new Date('2026-01-01T00:00:00.000Z'),
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
await expect(
|
|
67
|
+
kb.search({ snapshotId: snap.id, jurisdiction: 'FR', query: 'reporting' })
|
|
68
|
+
).rejects.toThrow('JURISDICTION_MISMATCH');
|
|
69
|
+
|
|
70
|
+
const ok = await kb.search({
|
|
71
|
+
snapshotId: snap.id,
|
|
72
|
+
jurisdiction: 'EU',
|
|
73
|
+
query: 'reporting',
|
|
74
|
+
});
|
|
75
|
+
expect(ok.items.map((i) => i.ruleVersionId)).toEqual([euDraft.id]);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
type SourceRef = { sourceDocumentId: string; excerpt?: string };
|
|
2
|
+
type SourceDocument = {
|
|
3
|
+
id: string;
|
|
4
|
+
jurisdiction: string;
|
|
5
|
+
authority: string;
|
|
6
|
+
title: string;
|
|
7
|
+
fetchedAt: Date;
|
|
8
|
+
hash: string;
|
|
9
|
+
fileId: string;
|
|
10
|
+
};
|
|
11
|
+
type Rule = { id: string; jurisdiction: string; topicKey: string };
|
|
12
|
+
type RuleVersion = {
|
|
13
|
+
id: string;
|
|
14
|
+
ruleId: string;
|
|
15
|
+
jurisdiction: string;
|
|
16
|
+
topicKey: string;
|
|
17
|
+
version: number;
|
|
18
|
+
content: string;
|
|
19
|
+
sourceRefs: SourceRef[];
|
|
20
|
+
status: 'draft' | 'approved' | 'rejected';
|
|
21
|
+
approvedBy?: string;
|
|
22
|
+
approvedAt?: Date;
|
|
23
|
+
createdAt: Date;
|
|
24
|
+
};
|
|
25
|
+
type KBSnapshot = {
|
|
26
|
+
id: string;
|
|
27
|
+
jurisdiction: string;
|
|
28
|
+
asOfDate: Date;
|
|
29
|
+
includedRuleVersionIds: string[];
|
|
30
|
+
publishedAt: Date;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export interface MemoryKbStore {
|
|
34
|
+
sources: Map<string, SourceDocument>;
|
|
35
|
+
rules: Map<string, Rule>;
|
|
36
|
+
ruleVersions: Map<string, RuleVersion>;
|
|
37
|
+
snapshots: Map<string, KBSnapshot>;
|
|
38
|
+
nextRuleVersionNumberByRuleId: Map<string, number>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createMemoryKbStore(): MemoryKbStore {
|
|
42
|
+
return {
|
|
43
|
+
sources: new Map(),
|
|
44
|
+
rules: new Map(),
|
|
45
|
+
ruleVersions: new Map(),
|
|
46
|
+
snapshots: new Map(),
|
|
47
|
+
nextRuleVersionNumberByRuleId: new Map(),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface MemoryKbHandlers {
|
|
52
|
+
createRule(rule: Rule): Promise<Rule>;
|
|
53
|
+
ingestSource(input: Omit<SourceDocument, 'id'>): Promise<SourceDocument>;
|
|
54
|
+
upsertRuleVersion(input: {
|
|
55
|
+
ruleId: string;
|
|
56
|
+
content: string;
|
|
57
|
+
sourceRefs: SourceRef[];
|
|
58
|
+
}): Promise<RuleVersion>;
|
|
59
|
+
approveRuleVersion(input: {
|
|
60
|
+
ruleVersionId: string;
|
|
61
|
+
approver: string;
|
|
62
|
+
}): Promise<RuleVersion>;
|
|
63
|
+
publishSnapshot(input: {
|
|
64
|
+
jurisdiction: string;
|
|
65
|
+
asOfDate: Date;
|
|
66
|
+
}): Promise<KBSnapshot>;
|
|
67
|
+
search(input: {
|
|
68
|
+
snapshotId: string;
|
|
69
|
+
jurisdiction: string;
|
|
70
|
+
query: string;
|
|
71
|
+
}): Promise<{ items: Array<{ ruleVersionId: string; excerpt?: string }> }>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function stableId(prefix: string, value: string): string {
|
|
75
|
+
return `${prefix}_${value.replace(/[^a-zA-Z0-9_-]/g, '_')}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function createMemoryKbHandlers(store: MemoryKbStore): MemoryKbHandlers {
|
|
79
|
+
async function createRule(rule: Rule): Promise<Rule> {
|
|
80
|
+
store.rules.set(rule.id, rule);
|
|
81
|
+
return rule;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function ingestSource(
|
|
85
|
+
input: Omit<SourceDocument, 'id'>
|
|
86
|
+
): Promise<SourceDocument> {
|
|
87
|
+
const id = stableId('src', `${input.jurisdiction}_${input.hash}`);
|
|
88
|
+
const doc: SourceDocument = { id, ...input };
|
|
89
|
+
store.sources.set(id, doc);
|
|
90
|
+
return doc;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function upsertRuleVersion(input: {
|
|
94
|
+
ruleId: string;
|
|
95
|
+
content: string;
|
|
96
|
+
sourceRefs: SourceRef[];
|
|
97
|
+
}): Promise<RuleVersion> {
|
|
98
|
+
if (!input.sourceRefs.length) {
|
|
99
|
+
throw new Error('SOURCE_REFS_REQUIRED');
|
|
100
|
+
}
|
|
101
|
+
const rule = store.rules.get(input.ruleId);
|
|
102
|
+
if (!rule) {
|
|
103
|
+
throw new Error('RULE_NOT_FOUND');
|
|
104
|
+
}
|
|
105
|
+
const next =
|
|
106
|
+
(store.nextRuleVersionNumberByRuleId.get(input.ruleId) ?? 0) + 1;
|
|
107
|
+
store.nextRuleVersionNumberByRuleId.set(input.ruleId, next);
|
|
108
|
+
const id = stableId('rv', `${input.ruleId}_${next}`);
|
|
109
|
+
const ruleVersion: RuleVersion = {
|
|
110
|
+
id,
|
|
111
|
+
ruleId: input.ruleId,
|
|
112
|
+
jurisdiction: rule.jurisdiction,
|
|
113
|
+
topicKey: rule.topicKey,
|
|
114
|
+
version: next,
|
|
115
|
+
content: input.content,
|
|
116
|
+
sourceRefs: input.sourceRefs,
|
|
117
|
+
status: 'draft',
|
|
118
|
+
createdAt: new Date(),
|
|
119
|
+
approvedAt: undefined,
|
|
120
|
+
approvedBy: undefined,
|
|
121
|
+
};
|
|
122
|
+
store.ruleVersions.set(id, ruleVersion);
|
|
123
|
+
return ruleVersion;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function approveRuleVersion(input: {
|
|
127
|
+
ruleVersionId: string;
|
|
128
|
+
approver: string;
|
|
129
|
+
}): Promise<RuleVersion> {
|
|
130
|
+
const existing = store.ruleVersions.get(input.ruleVersionId);
|
|
131
|
+
if (!existing) {
|
|
132
|
+
throw new Error('RULE_VERSION_NOT_FOUND');
|
|
133
|
+
}
|
|
134
|
+
const approved: RuleVersion = {
|
|
135
|
+
...existing,
|
|
136
|
+
status: 'approved',
|
|
137
|
+
approvedBy: input.approver,
|
|
138
|
+
approvedAt: new Date(),
|
|
139
|
+
};
|
|
140
|
+
store.ruleVersions.set(approved.id, approved);
|
|
141
|
+
return approved;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function publishSnapshot(input: {
|
|
145
|
+
jurisdiction: string;
|
|
146
|
+
asOfDate: Date;
|
|
147
|
+
}): Promise<KBSnapshot> {
|
|
148
|
+
const approved = [...store.ruleVersions.values()].filter(
|
|
149
|
+
(rv) => rv.status === 'approved' && rv.jurisdiction === input.jurisdiction
|
|
150
|
+
);
|
|
151
|
+
if (approved.length === 0) {
|
|
152
|
+
throw new Error('NO_APPROVED_RULES');
|
|
153
|
+
}
|
|
154
|
+
const includedRuleVersionIds = approved.map((rv) => rv.id).sort();
|
|
155
|
+
const id = stableId(
|
|
156
|
+
'snap',
|
|
157
|
+
`${input.jurisdiction}_${input.asOfDate.toISOString().slice(0, 10)}_${includedRuleVersionIds.length}`
|
|
158
|
+
);
|
|
159
|
+
const snapshot: KBSnapshot = {
|
|
160
|
+
id,
|
|
161
|
+
jurisdiction: input.jurisdiction,
|
|
162
|
+
asOfDate: input.asOfDate,
|
|
163
|
+
includedRuleVersionIds,
|
|
164
|
+
publishedAt: new Date(),
|
|
165
|
+
};
|
|
166
|
+
store.snapshots.set(id, snapshot);
|
|
167
|
+
return snapshot;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function search(input: {
|
|
171
|
+
snapshotId: string;
|
|
172
|
+
jurisdiction: string;
|
|
173
|
+
query: string;
|
|
174
|
+
}): Promise<{ items: Array<{ ruleVersionId: string; excerpt?: string }> }> {
|
|
175
|
+
const snapshot = store.snapshots.get(input.snapshotId);
|
|
176
|
+
if (!snapshot) {
|
|
177
|
+
throw new Error('SNAPSHOT_NOT_FOUND');
|
|
178
|
+
}
|
|
179
|
+
if (snapshot.jurisdiction !== input.jurisdiction) {
|
|
180
|
+
throw new Error('JURISDICTION_MISMATCH');
|
|
181
|
+
}
|
|
182
|
+
const q = input.query.toLowerCase();
|
|
183
|
+
const tokens = q.split(/\s+/).map((t) => t.trim()).filter(Boolean);
|
|
184
|
+
const items = snapshot.includedRuleVersionIds
|
|
185
|
+
.map((id) => store.ruleVersions.get(id))
|
|
186
|
+
.filter((rv): rv is RuleVersion => Boolean(rv))
|
|
187
|
+
.filter((rv) => {
|
|
188
|
+
if (tokens.length === 0) return true;
|
|
189
|
+
const hay = rv.content.toLowerCase();
|
|
190
|
+
return tokens.every((token) => hay.includes(token));
|
|
191
|
+
})
|
|
192
|
+
.map((rv) => ({ ruleVersionId: rv.id, excerpt: rv.content.slice(0, 120) }));
|
|
193
|
+
return { items };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
createRule,
|
|
198
|
+
ingestSource,
|
|
199
|
+
upsertRuleVersion,
|
|
200
|
+
approveRuleVersion,
|
|
201
|
+
publishSnapshot,
|
|
202
|
+
search,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Versioned Knowledge Base Example
|
|
3
|
+
*
|
|
4
|
+
* Curated KB with immutable sources, reviewable rule versions, and published snapshots.
|
|
5
|
+
*/
|
|
6
|
+
export * from './entities';
|
|
7
|
+
export * from './contracts';
|
|
8
|
+
export * from './events';
|
|
9
|
+
export * from './handlers';
|
|
10
|
+
export * from './feature';
|
|
11
|
+
export { default as example } from './example';
|
|
12
|
+
|
|
13
|
+
import './docs';
|
|
14
|
+
|
|
15
|
+
|