@contractspec/example.versioned-knowledge-base 3.7.16 → 3.7.18

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.
Files changed (42) hide show
  1. package/.turbo/turbo-build.log +42 -42
  2. package/CHANGELOG.md +22 -0
  3. package/dist/browser/docs/index.js +3 -27
  4. package/dist/browser/docs/versioned-knowledge-base.docblock.js +3 -27
  5. package/dist/browser/entities/index.js +1 -74
  6. package/dist/browser/entities/models.js +1 -74
  7. package/dist/browser/events.js +1 -101
  8. package/dist/browser/example.js +1 -35
  9. package/dist/browser/handlers/index.js +1 -115
  10. package/dist/browser/handlers/memory.handlers.js +1 -115
  11. package/dist/browser/index.js +4 -617
  12. package/dist/browser/knowledge-snapshot-publication.migration.js +2 -44
  13. package/dist/browser/operations/index.js +1 -257
  14. package/dist/browser/operations/kb.js +1 -257
  15. package/dist/browser/versioned-knowledge-base.feature.js +1 -41
  16. package/dist/docs/index.js +3 -27
  17. package/dist/docs/versioned-knowledge-base.docblock.js +3 -27
  18. package/dist/entities/index.js +1 -74
  19. package/dist/entities/models.js +1 -74
  20. package/dist/events.js +1 -101
  21. package/dist/example.js +1 -35
  22. package/dist/handlers/index.js +1 -115
  23. package/dist/handlers/memory.handlers.js +1 -115
  24. package/dist/index.js +4 -617
  25. package/dist/knowledge-snapshot-publication.migration.js +2 -44
  26. package/dist/node/docs/index.js +3 -27
  27. package/dist/node/docs/versioned-knowledge-base.docblock.js +3 -27
  28. package/dist/node/entities/index.js +1 -74
  29. package/dist/node/entities/models.js +1 -74
  30. package/dist/node/events.js +1 -101
  31. package/dist/node/example.js +1 -35
  32. package/dist/node/handlers/index.js +1 -115
  33. package/dist/node/handlers/memory.handlers.js +1 -115
  34. package/dist/node/index.js +4 -617
  35. package/dist/node/knowledge-snapshot-publication.migration.js +2 -44
  36. package/dist/node/operations/index.js +1 -257
  37. package/dist/node/operations/kb.js +1 -257
  38. package/dist/node/versioned-knowledge-base.feature.js +1 -41
  39. package/dist/operations/index.js +1 -257
  40. package/dist/operations/kb.js +1 -257
  41. package/dist/versioned-knowledge-base.feature.js +1 -41
  42. package/package.json +7 -7
@@ -1,15 +1,4 @@
1
- // src/docs/versioned-knowledge-base.docblock.ts
2
- import { registerDocBlocks } from "@contractspec/lib.contracts-spec/docs";
3
- var docBlocks = [
4
- {
5
- id: "docs.examples.versioned-knowledge-base.goal",
6
- title: "Versioned Knowledge Base — Goal",
7
- summary: "Curated KB with immutable sources, versioned rules, and published snapshots referenced by answers.",
8
- kind: "goal",
9
- visibility: "public",
10
- route: "/docs/examples/versioned-knowledge-base/goal",
11
- tags: ["knowledge", "versioning", "snapshots", "traceability"],
12
- body: `## Why it matters
1
+ import{registerDocBlocks as B}from"@contractspec/lib.contracts-spec/docs";var S=[{id:"docs.examples.versioned-knowledge-base.goal",title:"Versioned Knowledge Base — Goal",summary:"Curated KB with immutable sources, versioned rules, and published snapshots referenced by answers.",kind:"goal",visibility:"public",route:"/docs/examples/versioned-knowledge-base/goal",tags:["knowledge","versioning","snapshots","traceability"],body:`## Why it matters
13
2
  - Separates raw sources from curated knowledge.
14
3
  - Ensures assistant answers cite a published snapshot.
15
4
  - Makes change review and safe regeneration possible.
@@ -17,17 +6,7 @@ var docBlocks = [
17
6
  ## Core invariants
18
7
  - Sources are immutable and content-addressed (hash).
19
8
  - Rule versions must cite at least one source.
20
- - Snapshots include only approved rule versions.`
21
- },
22
- {
23
- id: "docs.examples.versioned-knowledge-base.reference",
24
- title: "Versioned Knowledge Base — Reference",
25
- summary: "Entities, contracts, and events for the versioned KB example.",
26
- kind: "reference",
27
- visibility: "public",
28
- route: "/docs/examples/versioned-knowledge-base",
29
- tags: ["knowledge", "reference"],
30
- body: `## Contracts
9
+ - Snapshots include only approved rule versions.`},{id:"docs.examples.versioned-knowledge-base.reference",title:"Versioned Knowledge Base — Reference",summary:"Entities, contracts, and events for the versioned KB example.",kind:"reference",visibility:"public",route:"/docs/examples/versioned-knowledge-base",tags:["knowledge","reference"],body:`## Contracts
31
10
  - kb.ingestSource
32
11
  - kb.upsertRuleVersion
33
12
  - kb.approveRuleVersion
@@ -38,603 +17,11 @@ var docBlocks = [
38
17
  - kb.source.ingested
39
18
  - kb.ruleVersion.created
40
19
  - kb.ruleVersion.approved
41
- - kb.snapshot.published`
42
- }
43
- ];
44
- registerDocBlocks(docBlocks);
45
- // src/entities/models.ts
46
- import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
47
- var SourceDocumentModel = defineSchemaModel({
48
- name: "SourceDocument",
49
- description: "Immutable raw source document metadata referencing a stored file.",
50
- fields: {
51
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
52
- jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
53
- authority: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
54
- title: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
55
- fetchedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
56
- hash: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
57
- fileId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
58
- }
59
- });
60
- var SourceRefModel = defineSchemaModel({
61
- name: "SourceRef",
62
- description: "Reference to a source document used to justify a rule version.",
63
- fields: {
64
- sourceDocumentId: {
65
- type: ScalarTypeEnum.String_unsecure(),
66
- isOptional: false
67
- },
68
- excerpt: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
69
- }
70
- });
71
- var RuleModel = defineSchemaModel({
72
- name: "Rule",
73
- description: "Curated rule (stable identity) with topic + jurisdiction scope.",
74
- fields: {
75
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
76
- jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
77
- topicKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
78
- }
79
- });
80
- var RuleVersionModel = defineSchemaModel({
81
- name: "RuleVersion",
82
- description: "A versioned rule content with source references and approval status.",
83
- fields: {
84
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
85
- ruleId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
86
- jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
87
- topicKey: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
88
- version: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
89
- content: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
90
- sourceRefs: { type: SourceRefModel, isArray: true, isOptional: false },
91
- status: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
92
- approvedBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
93
- approvedAt: { type: ScalarTypeEnum.DateTime(), isOptional: true },
94
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
95
- }
96
- });
97
- var KBSnapshotModel = defineSchemaModel({
98
- name: "KBSnapshot",
99
- description: "Published KB snapshot (as-of) referencing approved rule versions.",
100
- fields: {
101
- id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
102
- jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
103
- asOfDate: { type: ScalarTypeEnum.DateTime(), isOptional: false },
104
- includedRuleVersionIds: {
105
- type: ScalarTypeEnum.String_unsecure(),
106
- isArray: true,
107
- isOptional: false
108
- },
109
- publishedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
110
- }
111
- });
112
- // src/events.ts
113
- import { defineEvent } from "@contractspec/lib.contracts-spec";
114
- import { defineSchemaModel as defineSchemaModel2, ScalarTypeEnum as ScalarTypeEnum2 } from "@contractspec/lib.schema";
115
- var KbSourceIngestedPayload = defineSchemaModel2({
116
- name: "KbSourceIngestedPayload",
117
- description: "Emitted when a source document is ingested.",
118
- fields: {
119
- sourceDocumentId: {
120
- type: ScalarTypeEnum2.String_unsecure(),
121
- isOptional: false
122
- },
123
- jurisdiction: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
124
- hash: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false }
125
- }
126
- });
127
- var KbSourceIngestedEvent = defineEvent({
128
- meta: {
129
- key: "kb.source.ingested",
130
- version: "1.0.0",
131
- description: "Source document ingested (immutable).",
132
- stability: "experimental",
133
- owners: ["@examples"],
134
- tags: ["knowledge"]
135
- },
136
- payload: KbSourceIngestedPayload
137
- });
138
- var KbRuleVersionCreatedPayload = defineSchemaModel2({
139
- name: "KbRuleVersionCreatedPayload",
140
- description: "Emitted when a rule version draft is created.",
141
- fields: {
142
- ruleVersionId: {
143
- type: ScalarTypeEnum2.String_unsecure(),
144
- isOptional: false
145
- },
146
- ruleId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
147
- jurisdiction: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
148
- status: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false }
149
- }
150
- });
151
- var KbRuleVersionCreatedEvent = defineEvent({
152
- meta: {
153
- key: "kb.ruleVersion.created",
154
- version: "1.0.0",
155
- description: "Rule version created (draft).",
156
- stability: "experimental",
157
- owners: ["@examples"],
158
- tags: ["knowledge"]
159
- },
160
- payload: KbRuleVersionCreatedPayload
161
- });
162
- var KbRuleVersionApprovedPayload = defineSchemaModel2({
163
- name: "KbRuleVersionApprovedPayload",
164
- description: "Emitted when a rule version is approved.",
165
- fields: {
166
- ruleVersionId: {
167
- type: ScalarTypeEnum2.String_unsecure(),
168
- isOptional: false
169
- },
170
- approver: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false }
171
- }
172
- });
173
- var KbRuleVersionApprovedEvent = defineEvent({
174
- meta: {
175
- key: "kb.ruleVersion.approved",
176
- version: "1.0.0",
177
- description: "Rule version approved (human verified).",
178
- stability: "experimental",
179
- owners: ["@examples"],
180
- tags: ["knowledge"]
181
- },
182
- payload: KbRuleVersionApprovedPayload
183
- });
184
- var KbSnapshotPublishedPayload = defineSchemaModel2({
185
- name: "KbSnapshotPublishedPayload",
186
- description: "Emitted when a KB snapshot is published.",
187
- fields: {
188
- snapshotId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
189
- jurisdiction: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
190
- includedRuleVersionsCount: {
191
- type: ScalarTypeEnum2.Int_unsecure(),
192
- isOptional: false
193
- }
194
- }
195
- });
196
- var KbSnapshotPublishedEvent = defineEvent({
197
- meta: {
198
- key: "kb.snapshot.published",
199
- version: "1.0.0",
200
- description: "KB snapshot published.",
201
- stability: "experimental",
202
- owners: ["@examples"],
203
- tags: ["knowledge"]
204
- },
205
- payload: KbSnapshotPublishedPayload
206
- });
207
-
208
- // src/example.ts
209
- import { defineExample } from "@contractspec/lib.contracts-spec";
210
- var example = defineExample({
211
- meta: {
212
- key: "versioned-knowledge-base",
213
- version: "1.0.0",
214
- title: "Versioned Knowledge Base",
215
- description: "Curated KB with immutable sources, reviewable rule versions, and published snapshots.",
216
- kind: "knowledge",
217
- visibility: "public",
218
- stability: "experimental",
219
- owners: ["@platform.core"],
220
- tags: ["knowledge", "versioning", "snapshots"]
221
- },
222
- docs: {
223
- rootDocId: "docs.examples.versioned-knowledge-base"
224
- },
225
- entrypoints: {
226
- packageName: "@contractspec/example.versioned-knowledge-base",
227
- feature: "./feature",
228
- contracts: "./contracts",
229
- handlers: "./handlers",
230
- docs: "./docs"
231
- },
232
- surfaces: {
233
- templates: true,
234
- sandbox: { enabled: true, modes: ["markdown", "specs", "builder"] },
235
- studio: { enabled: true, installable: true },
236
- mcp: { enabled: true }
237
- }
238
- });
239
- var example_default = example;
240
-
241
- // src/handlers/memory.handlers.ts
242
- function createMemoryKbStore() {
243
- return {
244
- sources: new Map,
245
- rules: new Map,
246
- ruleVersions: new Map,
247
- snapshots: new Map,
248
- nextRuleVersionNumberByRuleId: new Map
249
- };
250
- }
251
- function stableId(prefix, value) {
252
- return `${prefix}_${value.replace(/[^a-zA-Z0-9_-]/g, "_")}`;
253
- }
254
- function createMemoryKbHandlers(store) {
255
- async function createRule(rule) {
256
- store.rules.set(rule.id, rule);
257
- return rule;
258
- }
259
- async function ingestSource(input) {
260
- const id = stableId("src", `${input.jurisdiction}_${input.hash}`);
261
- const doc = { id, ...input };
262
- store.sources.set(id, doc);
263
- return doc;
264
- }
265
- async function upsertRuleVersion(input) {
266
- if (!input.sourceRefs.length) {
267
- throw new Error("SOURCE_REFS_REQUIRED");
268
- }
269
- const rule = store.rules.get(input.ruleId);
270
- if (!rule) {
271
- throw new Error("RULE_NOT_FOUND");
272
- }
273
- const next = (store.nextRuleVersionNumberByRuleId.get(input.ruleId) ?? 0) + 1;
274
- const id = stableId("rv", `${input.ruleId}_${next}`);
275
- const ruleVersion = {
276
- id,
277
- ruleId: input.ruleId,
278
- jurisdiction: rule.jurisdiction,
279
- topicKey: rule.topicKey,
280
- version: next.toString(),
281
- content: input.content,
282
- sourceRefs: input.sourceRefs,
283
- status: "draft",
284
- createdAt: new Date,
285
- approvedAt: undefined,
286
- approvedBy: undefined
287
- };
288
- store.ruleVersions.set(id, ruleVersion);
289
- return ruleVersion;
290
- }
291
- async function approveRuleVersion(input) {
292
- const existing = store.ruleVersions.get(input.ruleVersionId);
293
- if (!existing) {
294
- throw new Error("RULE_VERSION_NOT_FOUND");
295
- }
296
- const approved = {
297
- ...existing,
298
- status: "approved",
299
- approvedBy: input.approver,
300
- approvedAt: new Date
301
- };
302
- store.ruleVersions.set(approved.id, approved);
303
- return approved;
304
- }
305
- async function publishSnapshot(input) {
306
- const approved = [...store.ruleVersions.values()].filter((rv) => rv.status === "approved" && rv.jurisdiction === input.jurisdiction);
307
- if (approved.length === 0) {
308
- throw new Error("NO_APPROVED_RULES");
309
- }
310
- const includedRuleVersionIds = approved.map((rv) => rv.id).sort();
311
- const id = stableId("snap", `${input.jurisdiction}_${input.asOfDate.toISOString().slice(0, 10)}_${includedRuleVersionIds.length}`);
312
- const snapshot = {
313
- id,
314
- jurisdiction: input.jurisdiction,
315
- asOfDate: input.asOfDate,
316
- includedRuleVersionIds,
317
- publishedAt: new Date
318
- };
319
- store.snapshots.set(id, snapshot);
320
- return snapshot;
321
- }
322
- async function search(input) {
323
- const snapshot = store.snapshots.get(input.snapshotId);
324
- if (!snapshot) {
325
- throw new Error("SNAPSHOT_NOT_FOUND");
326
- }
327
- if (snapshot.jurisdiction !== input.jurisdiction) {
328
- throw new Error("JURISDICTION_MISMATCH");
329
- }
330
- const q = input.query.toLowerCase();
331
- const tokens = q.split(/\s+/).map((t) => t.trim()).filter(Boolean);
332
- const items = snapshot.includedRuleVersionIds.map((id) => store.ruleVersions.get(id)).filter((rv) => Boolean(rv)).filter((rv) => {
333
- if (tokens.length === 0)
334
- return true;
335
- const hay = rv.content.toLowerCase();
336
- return tokens.every((token) => hay.includes(token));
337
- }).map((rv) => ({
338
- ruleVersionId: rv.id,
339
- excerpt: rv.content.slice(0, 120)
340
- }));
341
- return { items };
342
- }
343
- return {
344
- createRule,
345
- ingestSource,
346
- upsertRuleVersion,
347
- approveRuleVersion,
348
- publishSnapshot,
349
- search
350
- };
351
- }
352
- // src/knowledge-snapshot-publication.migration.ts
353
- import {
354
- OwnersEnum,
355
- StabilityEnum,
356
- TagsEnum
357
- } from "@contractspec/lib.contracts-spec/ownership";
358
- var KnowledgeSnapshotPublicationMigration = {
359
- meta: {
360
- key: "versioned-knowledge-base.migration.snapshot-publication",
361
- version: "1.0.0",
362
- title: "Knowledge Snapshot Publication Migration",
363
- description: "Adds publication audit fields and validation checks for published knowledge snapshots.",
364
- domain: "knowledge",
365
- owners: [OwnersEnum.PlatformContent],
366
- tags: ["knowledge", "migration", "snapshots", TagsEnum.Hygiene],
367
- stability: StabilityEnum.Experimental
368
- },
369
- dependencies: ["versioned-knowledge-base.migration.bootstrap"],
370
- plan: {
371
- up: [
372
- {
373
- kind: "schema",
374
- description: "Create the published snapshot audit table.",
375
- sql: `
20
+ - kb.snapshot.published`}];B(S);import{defineSchemaModel as c,ScalarTypeEnum as t}from"@contractspec/lib.schema";var V=c({name:"SourceDocument",description:"Immutable raw source document metadata referencing a stored file.",fields:{id:{type:t.String_unsecure(),isOptional:!1},jurisdiction:{type:t.String_unsecure(),isOptional:!1},authority:{type:t.String_unsecure(),isOptional:!1},title:{type:t.String_unsecure(),isOptional:!1},fetchedAt:{type:t.DateTime(),isOptional:!1},hash:{type:t.String_unsecure(),isOptional:!1},fileId:{type:t.String_unsecure(),isOptional:!1}}}),O=c({name:"SourceRef",description:"Reference to a source document used to justify a rule version.",fields:{sourceDocumentId:{type:t.String_unsecure(),isOptional:!1},excerpt:{type:t.String_unsecure(),isOptional:!0}}}),T=c({name:"Rule",description:"Curated rule (stable identity) with topic + jurisdiction scope.",fields:{id:{type:t.String_unsecure(),isOptional:!1},jurisdiction:{type:t.String_unsecure(),isOptional:!1},topicKey:{type:t.String_unsecure(),isOptional:!1}}}),_=c({name:"RuleVersion",description:"A versioned rule content with source references and approval status.",fields:{id:{type:t.String_unsecure(),isOptional:!1},ruleId:{type:t.String_unsecure(),isOptional:!1},jurisdiction:{type:t.String_unsecure(),isOptional:!1},topicKey:{type:t.String_unsecure(),isOptional:!1},version:{type:t.String_unsecure(),isOptional:!1},content:{type:t.String_unsecure(),isOptional:!1},sourceRefs:{type:O,isArray:!0,isOptional:!1},status:{type:t.String_unsecure(),isOptional:!1},approvedBy:{type:t.String_unsecure(),isOptional:!0},approvedAt:{type:t.DateTime(),isOptional:!0},createdAt:{type:t.DateTime(),isOptional:!1}}}),y=c({name:"KBSnapshot",description:"Published KB snapshot (as-of) referencing approved rule versions.",fields:{id:{type:t.String_unsecure(),isOptional:!1},jurisdiction:{type:t.String_unsecure(),isOptional:!1},asOfDate:{type:t.DateTime(),isOptional:!1},includedRuleVersionIds:{type:t.String_unsecure(),isArray:!0,isOptional:!1},publishedAt:{type:t.DateTime(),isOptional:!1}}});import{defineEvent as g}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as d,ScalarTypeEnum as u}from"@contractspec/lib.schema";var K=d({name:"KbSourceIngestedPayload",description:"Emitted when a source document is ingested.",fields:{sourceDocumentId:{type:u.String_unsecure(),isOptional:!1},jurisdiction:{type:u.String_unsecure(),isOptional:!1},hash:{type:u.String_unsecure(),isOptional:!1}}}),ie=g({meta:{key:"kb.source.ingested",version:"1.0.0",description:"Source document ingested (immutable).",stability:"experimental",owners:["@examples"],tags:["knowledge"]},payload:K}),N=d({name:"KbRuleVersionCreatedPayload",description:"Emitted when a rule version draft is created.",fields:{ruleVersionId:{type:u.String_unsecure(),isOptional:!1},ruleId:{type:u.String_unsecure(),isOptional:!1},jurisdiction:{type:u.String_unsecure(),isOptional:!1},status:{type:u.String_unsecure(),isOptional:!1}}}),ne=g({meta:{key:"kb.ruleVersion.created",version:"1.0.0",description:"Rule version created (draft).",stability:"experimental",owners:["@examples"],tags:["knowledge"]},payload:N}),P=d({name:"KbRuleVersionApprovedPayload",description:"Emitted when a rule version is approved.",fields:{ruleVersionId:{type:u.String_unsecure(),isOptional:!1},approver:{type:u.String_unsecure(),isOptional:!1}}}),oe=g({meta:{key:"kb.ruleVersion.approved",version:"1.0.0",description:"Rule version approved (human verified).",stability:"experimental",owners:["@examples"],tags:["knowledge"]},payload:P}),m=d({name:"KbSnapshotPublishedPayload",description:"Emitted when a KB snapshot is published.",fields:{snapshotId:{type:u.String_unsecure(),isOptional:!1},jurisdiction:{type:u.String_unsecure(),isOptional:!1},includedRuleVersionsCount:{type:u.Int_unsecure(),isOptional:!1}}}),ue=g({meta:{key:"kb.snapshot.published",version:"1.0.0",description:"KB snapshot published.",stability:"experimental",owners:["@examples"],tags:["knowledge"]},payload:m});import{defineExample as M}from"@contractspec/lib.contracts-spec";var $=M({meta:{key:"versioned-knowledge-base",version:"1.0.0",title:"Versioned Knowledge Base",description:"Curated KB with immutable sources, reviewable rule versions, and published snapshots.",kind:"knowledge",visibility:"public",stability:"experimental",owners:["@platform.core"],tags:["knowledge","versioning","snapshots"]},docs:{rootDocId:"docs.examples.versioned-knowledge-base"},entrypoints:{packageName:"@contractspec/example.versioned-knowledge-base",feature:"./feature",contracts:"./contracts",handlers:"./handlers",docs:"./docs"},surfaces:{templates:!0,sandbox:{enabled:!0,modes:["markdown","specs","builder"]},studio:{enabled:!0,installable:!0},mcp:{enabled:!0}}}),U=$;function ce(){return{sources:new Map,rules:new Map,ruleVersions:new Map,snapshots:new Map,nextRuleVersionNumberByRuleId:new Map}}function D(n,R){return`${n}_${R.replace(/[^a-zA-Z0-9_-]/g,"_")}`}function ge(n){async function R(e){return n.rules.set(e.id,e),e}async function I(e){let s=D("src",`${e.jurisdiction}_${e.hash}`),o={id:s,...e};return n.sources.set(s,o),o}async function h(e){if(!e.sourceRefs.length)throw Error("SOURCE_REFS_REQUIRED");let s=n.rules.get(e.ruleId);if(!s)throw Error("RULE_NOT_FOUND");let o=(n.nextRuleVersionNumberByRuleId.get(e.ruleId)??0)+1,p=D("rv",`${e.ruleId}_${o}`),l={id:p,ruleId:e.ruleId,jurisdiction:s.jurisdiction,topicKey:s.topicKey,version:o.toString(),content:e.content,sourceRefs:e.sourceRefs,status:"draft",createdAt:new Date,approvedAt:void 0,approvedBy:void 0};return n.ruleVersions.set(p,l),l}async function j(e){let s=n.ruleVersions.get(e.ruleVersionId);if(!s)throw Error("RULE_VERSION_NOT_FOUND");let o={...s,status:"approved",approvedBy:e.approver,approvedAt:new Date};return n.ruleVersions.set(o.id,o),o}async function w(e){let s=[...n.ruleVersions.values()].filter((i)=>i.status==="approved"&&i.jurisdiction===e.jurisdiction);if(s.length===0)throw Error("NO_APPROVED_RULES");let o=s.map((i)=>i.id).sort(),p=D("snap",`${e.jurisdiction}_${e.asOfDate.toISOString().slice(0,10)}_${o.length}`),l={id:p,jurisdiction:e.jurisdiction,asOfDate:e.asOfDate,includedRuleVersionIds:o,publishedAt:new Date};return n.snapshots.set(p,l),l}async function v(e){let s=n.snapshots.get(e.snapshotId);if(!s)throw Error("SNAPSHOT_NOT_FOUND");if(s.jurisdiction!==e.jurisdiction)throw Error("JURISDICTION_MISMATCH");let p=e.query.toLowerCase().split(/\s+/).map((i)=>i.trim()).filter(Boolean);return{items:s.includedRuleVersionIds.map((i)=>n.ruleVersions.get(i)).filter((i)=>Boolean(i)).filter((i)=>{if(p.length===0)return!0;let x=i.content.toLowerCase();return p.every((A)=>x.includes(A))}).map((i)=>({ruleVersionId:i.id,excerpt:i.content.slice(0,120)}))}}return{createRule:R,ingestSource:I,upsertRuleVersion:h,approveRuleVersion:j,publishSnapshot:w,search:v}}import{OwnersEnum as q,StabilityEnum as H,TagsEnum as L}from"@contractspec/lib.contracts-spec/ownership";var _e={meta:{key:"versioned-knowledge-base.migration.snapshot-publication",version:"1.0.0",title:"Knowledge Snapshot Publication Migration",description:"Adds publication audit fields and validation checks for published knowledge snapshots.",domain:"knowledge",owners:[q.PlatformContent],tags:["knowledge","migration","snapshots",L.Hygiene],stability:H.Experimental},dependencies:["versioned-knowledge-base.migration.bootstrap"],plan:{up:[{kind:"schema",description:"Create the published snapshot audit table.",sql:`
376
21
  CREATE TABLE kb_snapshot_publications (
377
22
  snapshot_id TEXT PRIMARY KEY,
378
23
  published_by TEXT NOT NULL,
379
24
  published_at TIMESTAMPTZ NOT NULL,
380
25
  source_count INTEGER NOT NULL,
381
26
  rule_version_count INTEGER NOT NULL
382
- );`
383
- },
384
- {
385
- kind: "validation",
386
- description: "Ensure snapshots have at least one approved rule version.",
387
- assertion: "SELECT COUNT(*) = 0 FROM kb_snapshots WHERE array_length(included_rule_version_ids, 1) = 0"
388
- }
389
- ],
390
- down: [
391
- {
392
- kind: "schema",
393
- description: "Remove the snapshot publication audit table.",
394
- sql: "DROP TABLE IF EXISTS kb_snapshot_publications;"
395
- }
396
- ]
397
- }
398
- };
399
-
400
- // src/operations/kb.ts
401
- import { defineCommand, defineQuery } from "@contractspec/lib.contracts-spec";
402
- import { defineSchemaModel as defineSchemaModel3, ScalarTypeEnum as ScalarTypeEnum3 } from "@contractspec/lib.schema";
403
- var IngestSourceInput = defineSchemaModel3({
404
- name: "KbIngestSourceInput",
405
- description: "Ingest immutable source metadata referencing a stored file.",
406
- fields: {
407
- jurisdiction: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
408
- authority: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
409
- title: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
410
- fetchedAt: { type: ScalarTypeEnum3.DateTime(), isOptional: false },
411
- hash: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
412
- fileId: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false }
413
- }
414
- });
415
- var UpsertRuleVersionInput = defineSchemaModel3({
416
- name: "KbUpsertRuleVersionInput",
417
- description: "Create a new draft rule version (immutable history).",
418
- fields: {
419
- ruleId: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
420
- content: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
421
- sourceRefs: { type: SourceRefModel, isArray: true, isOptional: false }
422
- }
423
- });
424
- var ApproveRuleVersionInput = defineSchemaModel3({
425
- name: "KbApproveRuleVersionInput",
426
- description: "Approve a rule version (human verification).",
427
- fields: {
428
- ruleVersionId: {
429
- type: ScalarTypeEnum3.String_unsecure(),
430
- isOptional: false
431
- },
432
- approver: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false }
433
- }
434
- });
435
- var PublishSnapshotInput = defineSchemaModel3({
436
- name: "KbPublishSnapshotInput",
437
- description: "Publish a snapshot for a jurisdiction as-of a date.",
438
- fields: {
439
- jurisdiction: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
440
- asOfDate: { type: ScalarTypeEnum3.DateTime(), isOptional: false }
441
- }
442
- });
443
- var SearchKbInput = defineSchemaModel3({
444
- name: "KbSearchInput",
445
- description: "Search within a published snapshot.",
446
- fields: {
447
- snapshotId: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
448
- jurisdiction: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
449
- query: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false }
450
- }
451
- });
452
- var SearchKbResultItem = defineSchemaModel3({
453
- name: "KbSearchResultItem",
454
- description: "Search result referencing a specific rule version.",
455
- fields: {
456
- ruleVersionId: {
457
- type: ScalarTypeEnum3.String_unsecure(),
458
- isOptional: false
459
- },
460
- excerpt: { type: ScalarTypeEnum3.String_unsecure(), isOptional: true }
461
- }
462
- });
463
- var SearchKbOutput = defineSchemaModel3({
464
- name: "KbSearchOutput",
465
- description: "Search results constrained to snapshot + jurisdiction.",
466
- fields: {
467
- items: { type: SearchKbResultItem, isArray: true, isOptional: false }
468
- }
469
- });
470
- var KbIngestSourceContract = defineCommand({
471
- meta: {
472
- key: "kb.ingestSource",
473
- title: "Ingest Source",
474
- version: "1.0.0",
475
- stability: "experimental",
476
- owners: ["@examples"],
477
- tags: ["knowledge", "sources", "ingestion"],
478
- description: "Ingest immutable source document metadata.",
479
- goal: "Store traceable source documents for curated KB.",
480
- context: "Called when an admin uploads/records authoritative sources."
481
- },
482
- io: {
483
- input: IngestSourceInput,
484
- output: SourceDocumentModel
485
- },
486
- policy: { auth: "user" }
487
- });
488
- var KbUpsertRuleVersionContract = defineCommand({
489
- meta: {
490
- key: "kb.upsertRuleVersion",
491
- title: "Upsert Rule Version",
492
- version: "1.0.0",
493
- stability: "experimental",
494
- owners: ["@examples"],
495
- tags: ["knowledge", "rules", "versioning"],
496
- description: "Create a new draft rule version with source references.",
497
- goal: "Propose curated knowledge updates with traceability.",
498
- context: "Automation or curators propose draft rule versions."
499
- },
500
- io: {
501
- input: UpsertRuleVersionInput,
502
- output: RuleVersionModel,
503
- errors: {
504
- SOURCE_REFS_REQUIRED: {
505
- description: "Rule version must cite at least one sourceRef",
506
- http: 400,
507
- gqlCode: "SOURCE_REFS_REQUIRED",
508
- when: "sourceRefs is empty"
509
- },
510
- RULE_NOT_FOUND: {
511
- description: "Rule does not exist",
512
- http: 404,
513
- gqlCode: "RULE_NOT_FOUND",
514
- when: "ruleId is unknown"
515
- }
516
- }
517
- },
518
- policy: { auth: "user" }
519
- });
520
- var KbApproveRuleVersionContract = defineCommand({
521
- meta: {
522
- key: "kb.approveRuleVersion",
523
- title: "Approve Rule Version",
524
- version: "1.0.0",
525
- stability: "experimental",
526
- owners: ["@examples"],
527
- tags: ["knowledge", "rules", "approval"],
528
- description: "Approve a draft rule version.",
529
- goal: "Human verification step before publishing snapshots.",
530
- context: "Curators/experts approve proposed KB changes."
531
- },
532
- io: {
533
- input: ApproveRuleVersionInput,
534
- output: RuleVersionModel
535
- },
536
- policy: { auth: "user" }
537
- });
538
- var KbPublishSnapshotContract = defineCommand({
539
- meta: {
540
- key: "kb.publishSnapshot",
541
- title: "Publish Snapshot",
542
- version: "1.0.0",
543
- stability: "experimental",
544
- owners: ["@examples"],
545
- tags: ["knowledge", "snapshots", "publishing"],
546
- description: "Publish a KB snapshot for a jurisdiction.",
547
- goal: "Create a stable snapshot that assistant answers can cite.",
548
- context: "Publishing happens after approvals; snapshot is referenced by answers."
549
- },
550
- io: {
551
- input: PublishSnapshotInput,
552
- output: KBSnapshotModel,
553
- errors: {
554
- NO_APPROVED_RULES: {
555
- description: "No approved rule versions available to publish",
556
- http: 409,
557
- gqlCode: "NO_APPROVED_RULES",
558
- when: "jurisdiction has zero approved rule versions"
559
- }
560
- }
561
- },
562
- policy: { auth: "user" }
563
- });
564
- var KbSearchContract = defineQuery({
565
- meta: {
566
- key: "kb.search",
567
- title: "Search KB",
568
- version: "1.0.0",
569
- stability: "experimental",
570
- owners: ["@examples"],
571
- tags: ["knowledge", "search", "snapshots"],
572
- description: "Search within a published KB snapshot.",
573
- goal: "Provide scoped retrieval for assistant answers.",
574
- context: "Assistant queries curated rules from a specific snapshot."
575
- },
576
- io: {
577
- input: SearchKbInput,
578
- output: SearchKbOutput
579
- },
580
- policy: { auth: "user" }
581
- });
582
- // src/versioned-knowledge-base.feature.ts
583
- import { defineFeature } from "@contractspec/lib.contracts-spec";
584
- var VersionedKnowledgeBaseFeature = defineFeature({
585
- meta: {
586
- key: "versioned-knowledge-base",
587
- version: "1.0.0",
588
- title: "Versioned Knowledge Base",
589
- description: "Curated KB with immutable sources, rule versions, and published snapshots.",
590
- domain: "knowledge",
591
- owners: ["@examples"],
592
- tags: ["knowledge", "versioning", "snapshots"],
593
- stability: "experimental"
594
- },
595
- operations: [
596
- { key: "kb.ingestSource", version: "1.0.0" },
597
- { key: "kb.upsertRuleVersion", version: "1.0.0" },
598
- { key: "kb.approveRuleVersion", version: "1.0.0" },
599
- { key: "kb.publishSnapshot", version: "1.0.0" },
600
- { key: "kb.search", version: "1.0.0" }
601
- ],
602
- events: [
603
- { key: "kb.source.ingested", version: "1.0.0" },
604
- { key: "kb.ruleVersion.created", version: "1.0.0" },
605
- { key: "kb.ruleVersion.approved", version: "1.0.0" },
606
- { key: "kb.snapshot.published", version: "1.0.0" }
607
- ],
608
- presentations: [],
609
- opToPresentation: [],
610
- presentationsTargets: [],
611
- capabilities: {
612
- requires: [{ key: "knowledge", version: "1.0.0" }]
613
- },
614
- knowledge: [{ key: "kb.knowledge.rules", version: "1.0.0" }],
615
- docs: [
616
- "docs.examples.versioned-knowledge-base.goal",
617
- "docs.examples.versioned-knowledge-base.reference"
618
- ]
619
- });
620
- export {
621
- example_default as example,
622
- createMemoryKbStore,
623
- createMemoryKbHandlers,
624
- VersionedKnowledgeBaseFeature,
625
- SourceRefModel,
626
- SourceDocumentModel,
627
- RuleVersionModel,
628
- RuleModel,
629
- KnowledgeSnapshotPublicationMigration,
630
- KbUpsertRuleVersionContract,
631
- KbSourceIngestedEvent,
632
- KbSnapshotPublishedEvent,
633
- KbSearchContract,
634
- KbRuleVersionCreatedEvent,
635
- KbRuleVersionApprovedEvent,
636
- KbPublishSnapshotContract,
637
- KbIngestSourceContract,
638
- KbApproveRuleVersionContract,
639
- KBSnapshotModel
640
- };
27
+ );`},{kind:"validation",description:"Ensure snapshots have at least one approved rule version.",assertion:"SELECT COUNT(*) = 0 FROM kb_snapshots WHERE array_length(included_rule_version_ids, 1) = 0"}],down:[{kind:"schema",description:"Remove the snapshot publication audit table.",sql:"DROP TABLE IF EXISTS kb_snapshot_publications;"}]}};import{defineCommand as f,defineQuery as F}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as a,ScalarTypeEnum as r}from"@contractspec/lib.schema";var z=a({name:"KbIngestSourceInput",description:"Ingest immutable source metadata referencing a stored file.",fields:{jurisdiction:{type:r.String_unsecure(),isOptional:!1},authority:{type:r.String_unsecure(),isOptional:!1},title:{type:r.String_unsecure(),isOptional:!1},fetchedAt:{type:r.DateTime(),isOptional:!1},hash:{type:r.String_unsecure(),isOptional:!1},fileId:{type:r.String_unsecure(),isOptional:!1}}}),C=a({name:"KbUpsertRuleVersionInput",description:"Create a new draft rule version (immutable history).",fields:{ruleId:{type:r.String_unsecure(),isOptional:!1},content:{type:r.String_unsecure(),isOptional:!1},sourceRefs:{type:O,isArray:!0,isOptional:!1}}}),J=a({name:"KbApproveRuleVersionInput",description:"Approve a rule version (human verification).",fields:{ruleVersionId:{type:r.String_unsecure(),isOptional:!1},approver:{type:r.String_unsecure(),isOptional:!1}}}),Z=a({name:"KbPublishSnapshotInput",description:"Publish a snapshot for a jurisdiction as-of a date.",fields:{jurisdiction:{type:r.String_unsecure(),isOptional:!1},asOfDate:{type:r.DateTime(),isOptional:!1}}}),G=a({name:"KbSearchInput",description:"Search within a published snapshot.",fields:{snapshotId:{type:r.String_unsecure(),isOptional:!1},jurisdiction:{type:r.String_unsecure(),isOptional:!1},query:{type:r.String_unsecure(),isOptional:!1}}}),Q=a({name:"KbSearchResultItem",description:"Search result referencing a specific rule version.",fields:{ruleVersionId:{type:r.String_unsecure(),isOptional:!1},excerpt:{type:r.String_unsecure(),isOptional:!0}}}),W=a({name:"KbSearchOutput",description:"Search results constrained to snapshot + jurisdiction.",fields:{items:{type:Q,isArray:!0,isOptional:!1}}}),he=f({meta:{key:"kb.ingestSource",title:"Ingest Source",version:"1.0.0",stability:"experimental",owners:["@examples"],tags:["knowledge","sources","ingestion"],description:"Ingest immutable source document metadata.",goal:"Store traceable source documents for curated KB.",context:"Called when an admin uploads/records authoritative sources."},io:{input:z,output:V},policy:{auth:"user"}}),je=f({meta:{key:"kb.upsertRuleVersion",title:"Upsert Rule Version",version:"1.0.0",stability:"experimental",owners:["@examples"],tags:["knowledge","rules","versioning"],description:"Create a new draft rule version with source references.",goal:"Propose curated knowledge updates with traceability.",context:"Automation or curators propose draft rule versions."},io:{input:C,output:_,errors:{SOURCE_REFS_REQUIRED:{description:"Rule version must cite at least one sourceRef",http:400,gqlCode:"SOURCE_REFS_REQUIRED",when:"sourceRefs is empty"},RULE_NOT_FOUND:{description:"Rule does not exist",http:404,gqlCode:"RULE_NOT_FOUND",when:"ruleId is unknown"}}},policy:{auth:"user"}}),we=f({meta:{key:"kb.approveRuleVersion",title:"Approve Rule Version",version:"1.0.0",stability:"experimental",owners:["@examples"],tags:["knowledge","rules","approval"],description:"Approve a draft rule version.",goal:"Human verification step before publishing snapshots.",context:"Curators/experts approve proposed KB changes."},io:{input:J,output:_},policy:{auth:"user"}}),ve=f({meta:{key:"kb.publishSnapshot",title:"Publish Snapshot",version:"1.0.0",stability:"experimental",owners:["@examples"],tags:["knowledge","snapshots","publishing"],description:"Publish a KB snapshot for a jurisdiction.",goal:"Create a stable snapshot that assistant answers can cite.",context:"Publishing happens after approvals; snapshot is referenced by answers."},io:{input:Z,output:y,errors:{NO_APPROVED_RULES:{description:"No approved rule versions available to publish",http:409,gqlCode:"NO_APPROVED_RULES",when:"jurisdiction has zero approved rule versions"}}},policy:{auth:"user"}}),xe=F({meta:{key:"kb.search",title:"Search KB",version:"1.0.0",stability:"experimental",owners:["@examples"],tags:["knowledge","search","snapshots"],description:"Search within a published KB snapshot.",goal:"Provide scoped retrieval for assistant answers.",context:"Assistant queries curated rules from a specific snapshot."},io:{input:G,output:W},policy:{auth:"user"}});import{defineFeature as X}from"@contractspec/lib.contracts-spec";var Ne=X({meta:{key:"versioned-knowledge-base",version:"1.0.0",title:"Versioned Knowledge Base",description:"Curated KB with immutable sources, rule versions, and published snapshots.",domain:"knowledge",owners:["@examples"],tags:["knowledge","versioning","snapshots"],stability:"experimental"},operations:[{key:"kb.ingestSource",version:"1.0.0"},{key:"kb.upsertRuleVersion",version:"1.0.0"},{key:"kb.approveRuleVersion",version:"1.0.0"},{key:"kb.publishSnapshot",version:"1.0.0"},{key:"kb.search",version:"1.0.0"}],events:[{key:"kb.source.ingested",version:"1.0.0"},{key:"kb.ruleVersion.created",version:"1.0.0"},{key:"kb.ruleVersion.approved",version:"1.0.0"},{key:"kb.snapshot.published",version:"1.0.0"}],presentations:[],opToPresentation:[],presentationsTargets:[],capabilities:{requires:[{key:"knowledge",version:"1.0.0"}]},knowledge:[{key:"kb.knowledge.rules",version:"1.0.0"}],docs:["docs.examples.versioned-knowledge-base.goal","docs.examples.versioned-knowledge-base.reference"]});export{U as example,ce as createMemoryKbStore,ge as createMemoryKbHandlers,Ne as VersionedKnowledgeBaseFeature,O as SourceRefModel,V as SourceDocumentModel,_ as RuleVersionModel,T as RuleModel,_e as KnowledgeSnapshotPublicationMigration,je as KbUpsertRuleVersionContract,ie as KbSourceIngestedEvent,ue as KbSnapshotPublishedEvent,xe as KbSearchContract,ne as KbRuleVersionCreatedEvent,oe as KbRuleVersionApprovedEvent,ve as KbPublishSnapshotContract,he as KbIngestSourceContract,we as KbApproveRuleVersionContract,y as KBSnapshotModel};