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