@contractspec/example.locale-jurisdiction-gate 1.44.0
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$colon$bundle.log +56 -0
- package/.turbo/turbo-build.log +57 -0
- package/CHANGELOG.md +161 -0
- package/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/docs/index.d.ts +1 -0
- package/dist/docs/index.js +1 -0
- package/dist/docs/locale-jurisdiction-gate.docblock.d.ts +1 -0
- package/dist/docs/locale-jurisdiction-gate.docblock.js +53 -0
- package/dist/docs/locale-jurisdiction-gate.docblock.js.map +1 -0
- package/dist/entities/index.d.ts +2 -0
- package/dist/entities/index.js +3 -0
- package/dist/entities/models.d.ts +186 -0
- package/dist/entities/models.d.ts.map +1 -0
- package/dist/entities/models.js +168 -0
- package/dist/entities/models.js.map +1 -0
- package/dist/events.d.ts +69 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +123 -0
- package/dist/events.js.map +1 -0
- package/dist/example.d.ts +36 -0
- package/dist/example.d.ts.map +1 -0
- package/dist/example.js +40 -0
- package/dist/example.js.map +1 -0
- package/dist/handlers/demo.handlers.d.ts +59 -0
- package/dist/handlers/demo.handlers.d.ts.map +1 -0
- package/dist/handlers/demo.handlers.js +86 -0
- package/dist/handlers/demo.handlers.js.map +1 -0
- package/dist/handlers/index.d.ts +2 -0
- package/dist/handlers/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +12 -0
- package/dist/locale-jurisdiction-gate.feature.d.ts +7 -0
- package/dist/locale-jurisdiction-gate.feature.d.ts.map +1 -0
- package/dist/locale-jurisdiction-gate.feature.js +51 -0
- package/dist/locale-jurisdiction-gate.feature.js.map +1 -0
- package/dist/operations/assistant.d.ts +245 -0
- package/dist/operations/assistant.d.ts.map +1 -0
- package/dist/operations/assistant.js +115 -0
- package/dist/operations/assistant.js.map +1 -0
- package/dist/operations/index.d.ts +2 -0
- package/dist/operations/index.js +3 -0
- package/dist/policy/guard.d.ts +27 -0
- package/dist/policy/guard.d.ts.map +1 -0
- package/dist/policy/guard.js +73 -0
- package/dist/policy/guard.js.map +1 -0
- package/dist/policy/index.d.ts +3 -0
- package/dist/policy/index.js +3 -0
- package/dist/policy/types.d.ts +16 -0
- package/dist/policy/types.d.ts.map +1 -0
- package/dist/policy/types.js +0 -0
- package/example.ts +1 -0
- package/package.json +80 -0
- package/src/docs/index.ts +1 -0
- package/src/docs/locale-jurisdiction-gate.docblock.ts +46 -0
- package/src/entities/index.ts +1 -0
- package/src/entities/models.ts +110 -0
- package/src/events.ts +74 -0
- package/src/example.ts +27 -0
- package/src/handlers/demo.handlers.test.ts +54 -0
- package/src/handlers/demo.handlers.ts +160 -0
- package/src/handlers/index.ts +1 -0
- package/src/index.ts +15 -0
- package/src/locale-jurisdiction-gate.feature.ts +30 -0
- package/src/operations/assistant.ts +98 -0
- package/src/operations/index.ts +1 -0
- package/src/policy/guard.test.ts +25 -0
- package/src/policy/guard.ts +102 -0
- package/src/policy/index.ts +2 -0
- package/src/policy/types.ts +18 -0
- package/tsconfig.json +17 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/tsdown.config.js +17 -0
package/package.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@contractspec/example.locale-jurisdiction-gate",
|
|
3
|
+
"version": "1.44.0",
|
|
4
|
+
"description": "Example: enforce locale + jurisdiction + kbSnapshotId + allowed scope for assistant calls (fail-closed).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./dist/index.js",
|
|
11
|
+
"./docs": "./dist/docs/index.js",
|
|
12
|
+
"./docs/locale-jurisdiction-gate.docblock": "./dist/docs/locale-jurisdiction-gate.docblock.js",
|
|
13
|
+
"./entities": "./dist/entities/index.js",
|
|
14
|
+
"./entities/models": "./dist/entities/models.js",
|
|
15
|
+
"./events": "./dist/events.js",
|
|
16
|
+
"./example": "./dist/example.js",
|
|
17
|
+
"./handlers": "./dist/handlers/index.js",
|
|
18
|
+
"./handlers/demo.handlers": "./dist/handlers/demo.handlers.js",
|
|
19
|
+
"./locale-jurisdiction-gate.feature": "./dist/locale-jurisdiction-gate.feature.js",
|
|
20
|
+
"./operations": "./dist/operations/index.js",
|
|
21
|
+
"./operations/assistant": "./dist/operations/assistant.js",
|
|
22
|
+
"./policy": "./dist/policy/index.js",
|
|
23
|
+
"./policy/guard": "./dist/policy/guard.js",
|
|
24
|
+
"./policy/types": "./dist/policy/types.js",
|
|
25
|
+
"./*": "./*"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
|
|
29
|
+
"publish:pkg:canary": "bun publish:pkg --tag canary",
|
|
30
|
+
"build": "bun build:types && bun build:bundle",
|
|
31
|
+
"build:bundle": "tsdown",
|
|
32
|
+
"build:types": "tsc --noEmit",
|
|
33
|
+
"dev": "bun build:bundle --watch",
|
|
34
|
+
"clean": "rimraf dist .turbo",
|
|
35
|
+
"lint": "bun lint:fix",
|
|
36
|
+
"lint:fix": "eslint src --fix",
|
|
37
|
+
"lint:check": "eslint src",
|
|
38
|
+
"test": "bun test"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@contractspec/lib.contracts": "1.44.0",
|
|
42
|
+
"@contractspec/lib.schema": "1.44.0",
|
|
43
|
+
"zod": "^4.1.13"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@contractspec/tool.tsdown": "1.44.0",
|
|
47
|
+
"@contractspec/tool.typescript": "1.44.0",
|
|
48
|
+
"tsdown": "^0.18.3",
|
|
49
|
+
"typescript": "^5.9.3"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"exports": {
|
|
54
|
+
".": "./dist/index.js",
|
|
55
|
+
"./contracts": "./dist/contracts/index.js",
|
|
56
|
+
"./contracts/assistant": "./dist/contracts/assistant.js",
|
|
57
|
+
"./docs": "./dist/docs/index.js",
|
|
58
|
+
"./docs/locale-jurisdiction-gate.docblock": "./dist/docs/locale-jurisdiction-gate.docblock.js",
|
|
59
|
+
"./entities": "./dist/entities/index.js",
|
|
60
|
+
"./entities/models": "./dist/entities/models.js",
|
|
61
|
+
"./events": "./dist/events.js",
|
|
62
|
+
"./example": "./dist/example.js",
|
|
63
|
+
"./handlers": "./dist/handlers/index.js",
|
|
64
|
+
"./handlers/demo.handlers": "./dist/handlers/demo.handlers.js",
|
|
65
|
+
"./locale-jurisdiction-gate.feature": "./dist/locale-jurisdiction-gate.feature.js",
|
|
66
|
+
"./policy": "./dist/policy/index.js",
|
|
67
|
+
"./policy/guard": "./dist/policy/guard.js",
|
|
68
|
+
"./policy/types": "./dist/policy/types.js",
|
|
69
|
+
"./*": "./*"
|
|
70
|
+
},
|
|
71
|
+
"registry": "https://registry.npmjs.org/"
|
|
72
|
+
},
|
|
73
|
+
"license": "MIT",
|
|
74
|
+
"repository": {
|
|
75
|
+
"type": "git",
|
|
76
|
+
"url": "https://github.com/lssm-tech/contractspec.git",
|
|
77
|
+
"directory": "packages/examples/locale-jurisdiction-gate"
|
|
78
|
+
},
|
|
79
|
+
"homepage": "https://contractspec.io"
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './locale-jurisdiction-gate.docblock';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { DocBlock } from '@contractspec/lib.contracts/docs';
|
|
2
|
+
import { registerDocBlocks } from '@contractspec/lib.contracts/docs';
|
|
3
|
+
|
|
4
|
+
const docBlocks: DocBlock[] = [
|
|
5
|
+
{
|
|
6
|
+
id: 'docs.examples.locale-jurisdiction-gate.goal',
|
|
7
|
+
title: 'Locale/Jurisdiction Gate — Goal',
|
|
8
|
+
summary:
|
|
9
|
+
'Fail-closed gate that forces locale + jurisdiction + kbSnapshotId + allowedScope for assistant calls.',
|
|
10
|
+
kind: 'goal',
|
|
11
|
+
visibility: 'public',
|
|
12
|
+
route: '/docs/examples/locale-jurisdiction-gate/goal',
|
|
13
|
+
tags: ['assistant', 'policy', 'locale', 'jurisdiction', 'knowledge'],
|
|
14
|
+
body: `## Why it matters
|
|
15
|
+
- Forces all assistant behavior to be bound to explicit inputs (no guessing).
|
|
16
|
+
- Requires KB snapshot citations to make answers traceable and regenerable.
|
|
17
|
+
|
|
18
|
+
## Guardrails
|
|
19
|
+
- Missing locale/jurisdiction/snapshot/scope => refuse (structured).
|
|
20
|
+
- Missing citations => refuse.
|
|
21
|
+
- Scope violations under education_only => refuse/escalate.`,
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
id: 'docs.examples.locale-jurisdiction-gate.reference',
|
|
25
|
+
title: 'Locale/Jurisdiction Gate — Reference',
|
|
26
|
+
summary: 'Contracts, models, and events exposed by the gate example.',
|
|
27
|
+
kind: 'reference',
|
|
28
|
+
visibility: 'public',
|
|
29
|
+
route: '/docs/examples/locale-jurisdiction-gate',
|
|
30
|
+
tags: ['assistant', 'policy', 'reference'],
|
|
31
|
+
body: `## Contracts
|
|
32
|
+
- assistant.answer (v1)
|
|
33
|
+
- assistant.explainConcept (v1)
|
|
34
|
+
|
|
35
|
+
## Models
|
|
36
|
+
- LLMCallEnvelope (locale, regulatoryContext, kbSnapshotId, allowedScope, traceId)
|
|
37
|
+
- AssistantAnswerIR (sections, citations, disclaimers, riskFlags)
|
|
38
|
+
|
|
39
|
+
## Events
|
|
40
|
+
- assistant.answer.requested
|
|
41
|
+
- assistant.answer.blocked
|
|
42
|
+
- assistant.answer.delivered`,
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
registerDocBlocks(docBlocks);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './models';
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ScalarTypeEnum,
|
|
3
|
+
defineEnum,
|
|
4
|
+
defineSchemaModel,
|
|
5
|
+
} from '@contractspec/lib.schema';
|
|
6
|
+
|
|
7
|
+
export const AllowedScopeEnum = defineEnum('AllowedScope', [
|
|
8
|
+
'education_only',
|
|
9
|
+
'generic_info',
|
|
10
|
+
'escalation_required',
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
export const UserProfileModel = defineSchemaModel({
|
|
14
|
+
name: 'UserProfile',
|
|
15
|
+
description: 'User profile inputs used to derive regulatory context.',
|
|
16
|
+
fields: {
|
|
17
|
+
preferredLocale: {
|
|
18
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
19
|
+
isOptional: true,
|
|
20
|
+
},
|
|
21
|
+
residencyCountry: {
|
|
22
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
23
|
+
isOptional: true,
|
|
24
|
+
},
|
|
25
|
+
taxResidenceCountry: {
|
|
26
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
27
|
+
isOptional: true,
|
|
28
|
+
},
|
|
29
|
+
clientType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export const RegulatoryContextModel = defineSchemaModel({
|
|
34
|
+
name: 'RegulatoryContext',
|
|
35
|
+
description: 'Explicit regulatory context (no guessing).',
|
|
36
|
+
fields: {
|
|
37
|
+
jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
38
|
+
region: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
39
|
+
clientType: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
40
|
+
allowedScope: { type: AllowedScopeEnum, isOptional: false },
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const LLMCallEnvelopeModel = defineSchemaModel({
|
|
45
|
+
name: 'LLMCallEnvelope',
|
|
46
|
+
description:
|
|
47
|
+
'Mandatory envelope for assistant calls. All fields are explicit and required for policy gating.',
|
|
48
|
+
fields: {
|
|
49
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
50
|
+
locale: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
51
|
+
regulatoryContext: { type: RegulatoryContextModel, isOptional: false },
|
|
52
|
+
kbSnapshotId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
53
|
+
allowedScope: { type: AllowedScopeEnum, isOptional: false },
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export const AssistantCitationModel = defineSchemaModel({
|
|
58
|
+
name: 'AssistantCitation',
|
|
59
|
+
description:
|
|
60
|
+
'Citation referencing a KB snapshot + a specific item within it.',
|
|
61
|
+
fields: {
|
|
62
|
+
kbSnapshotId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
63
|
+
sourceType: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
64
|
+
sourceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
65
|
+
title: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
66
|
+
excerpt: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export const AssistantAnswerSectionModel = defineSchemaModel({
|
|
71
|
+
name: 'AssistantAnswerSection',
|
|
72
|
+
description: 'Structured answer section.',
|
|
73
|
+
fields: {
|
|
74
|
+
heading: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
75
|
+
body: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export const AssistantAnswerIRModel = defineSchemaModel({
|
|
80
|
+
name: 'AssistantAnswerIR',
|
|
81
|
+
description:
|
|
82
|
+
'Structured assistant answer with mandatory citations and explicit locale/jurisdiction.',
|
|
83
|
+
fields: {
|
|
84
|
+
locale: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
85
|
+
jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
86
|
+
allowedScope: { type: AllowedScopeEnum, isOptional: false },
|
|
87
|
+
sections: {
|
|
88
|
+
type: AssistantAnswerSectionModel,
|
|
89
|
+
isArray: true,
|
|
90
|
+
isOptional: false,
|
|
91
|
+
},
|
|
92
|
+
citations: {
|
|
93
|
+
type: AssistantCitationModel,
|
|
94
|
+
isArray: true,
|
|
95
|
+
isOptional: false,
|
|
96
|
+
},
|
|
97
|
+
disclaimers: {
|
|
98
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
99
|
+
isArray: true,
|
|
100
|
+
isOptional: true,
|
|
101
|
+
},
|
|
102
|
+
riskFlags: {
|
|
103
|
+
type: ScalarTypeEnum.String_unsecure(),
|
|
104
|
+
isArray: true,
|
|
105
|
+
isOptional: true,
|
|
106
|
+
},
|
|
107
|
+
refused: { type: ScalarTypeEnum.Boolean(), isOptional: true },
|
|
108
|
+
refusalReason: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
|
|
109
|
+
},
|
|
110
|
+
});
|
package/src/events.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { defineEvent, defineSchemaModel } from '@contractspec/lib.contracts';
|
|
2
|
+
import { ScalarTypeEnum } from '@contractspec/lib.schema';
|
|
3
|
+
|
|
4
|
+
const AssistantAnswerRequestedPayload = defineSchemaModel({
|
|
5
|
+
name: 'AssistantAnswerRequestedPayload',
|
|
6
|
+
description: 'Emitted when an assistant answer is requested (pre-gate).',
|
|
7
|
+
fields: {
|
|
8
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
9
|
+
locale: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
10
|
+
jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
11
|
+
kbSnapshotId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
12
|
+
allowedScope: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const AssistantAnswerRequestedEvent = defineEvent({
|
|
17
|
+
meta: {
|
|
18
|
+
key: 'assistant.answer.requested',
|
|
19
|
+
version: 1,
|
|
20
|
+
description: 'Assistant answer requested (policy gate will run).',
|
|
21
|
+
stability: 'experimental',
|
|
22
|
+
owners: ['@examples'],
|
|
23
|
+
tags: ['assistant', 'policy'],
|
|
24
|
+
},
|
|
25
|
+
payload: AssistantAnswerRequestedPayload,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const AssistantAnswerBlockedPayload = defineSchemaModel({
|
|
29
|
+
name: 'AssistantAnswerBlockedPayload',
|
|
30
|
+
description: 'Emitted when a request is blocked by the gate.',
|
|
31
|
+
fields: {
|
|
32
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
33
|
+
reasonCode: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
34
|
+
reason: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export const AssistantAnswerBlockedEvent = defineEvent({
|
|
39
|
+
meta: {
|
|
40
|
+
key: 'assistant.answer.blocked',
|
|
41
|
+
version: 1,
|
|
42
|
+
description: 'Assistant answer blocked (fail-closed).',
|
|
43
|
+
stability: 'experimental',
|
|
44
|
+
owners: ['@examples'],
|
|
45
|
+
tags: ['assistant', 'policy', 'blocked'],
|
|
46
|
+
},
|
|
47
|
+
payload: AssistantAnswerBlockedPayload,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const AssistantAnswerDeliveredPayload = defineSchemaModel({
|
|
51
|
+
name: 'AssistantAnswerDeliveredPayload',
|
|
52
|
+
description: 'Emitted when a structured, cited answer is delivered.',
|
|
53
|
+
fields: {
|
|
54
|
+
traceId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
55
|
+
locale: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
56
|
+
jurisdiction: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
57
|
+
kbSnapshotId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
58
|
+
allowedScope: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
|
|
59
|
+
citationsCount: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
export const AssistantAnswerDeliveredEvent = defineEvent({
|
|
64
|
+
meta: {
|
|
65
|
+
key: 'assistant.answer.delivered',
|
|
66
|
+
version: 1,
|
|
67
|
+
description:
|
|
68
|
+
'Assistant answer delivered (must include KB snapshot citations).',
|
|
69
|
+
stability: 'experimental',
|
|
70
|
+
owners: ['@examples'],
|
|
71
|
+
tags: ['assistant', 'policy', 'delivered'],
|
|
72
|
+
},
|
|
73
|
+
payload: AssistantAnswerDeliveredPayload,
|
|
74
|
+
});
|
package/src/example.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const example = {
|
|
2
|
+
id: 'locale-jurisdiction-gate',
|
|
3
|
+
title: 'Locale / Jurisdiction Gate',
|
|
4
|
+
summary:
|
|
5
|
+
'Fail-closed gating for assistant calls: locale + jurisdiction + kbSnapshotId + allowedScope must be explicit, answers must cite a snapshot.',
|
|
6
|
+
tags: ['policy', 'locale', 'jurisdiction', 'assistant', 'gating'],
|
|
7
|
+
kind: 'knowledge',
|
|
8
|
+
visibility: 'public',
|
|
9
|
+
docs: {
|
|
10
|
+
rootDocId: 'docs.examples.locale-jurisdiction-gate',
|
|
11
|
+
},
|
|
12
|
+
entrypoints: {
|
|
13
|
+
packageName: '@contractspec/example.locale-jurisdiction-gate',
|
|
14
|
+
feature: './feature',
|
|
15
|
+
contracts: './contracts',
|
|
16
|
+
handlers: './handlers',
|
|
17
|
+
docs: './docs',
|
|
18
|
+
},
|
|
19
|
+
surfaces: {
|
|
20
|
+
templates: true,
|
|
21
|
+
sandbox: { enabled: true, modes: ['markdown', 'specs'] },
|
|
22
|
+
studio: { enabled: true, installable: true },
|
|
23
|
+
mcp: { enabled: true },
|
|
24
|
+
},
|
|
25
|
+
} as const;
|
|
26
|
+
|
|
27
|
+
export default example;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
|
|
3
|
+
import { createDemoAssistantHandlers } from './demo.handlers';
|
|
4
|
+
|
|
5
|
+
describe('@contractspec/example.locale-jurisdiction-gate demo handlers', () => {
|
|
6
|
+
it('blocks when locale is missing', async () => {
|
|
7
|
+
const handlers = createDemoAssistantHandlers();
|
|
8
|
+
const result = await handlers.answer({
|
|
9
|
+
envelope: {
|
|
10
|
+
traceId: 't1',
|
|
11
|
+
locale: '',
|
|
12
|
+
kbSnapshotId: 'snap_1',
|
|
13
|
+
allowedScope: 'education_only',
|
|
14
|
+
regulatoryContext: { jurisdiction: 'EU' },
|
|
15
|
+
},
|
|
16
|
+
question: 'What is a snapshot?',
|
|
17
|
+
});
|
|
18
|
+
expect(result.refused).toBeTrue();
|
|
19
|
+
expect(result.refusalReason).toBe('LOCALE_REQUIRED');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('blocks when kbSnapshotId is missing', async () => {
|
|
23
|
+
const handlers = createDemoAssistantHandlers();
|
|
24
|
+
const result = await handlers.answer({
|
|
25
|
+
envelope: {
|
|
26
|
+
traceId: 't2',
|
|
27
|
+
locale: 'en-GB',
|
|
28
|
+
kbSnapshotId: '',
|
|
29
|
+
allowedScope: 'education_only',
|
|
30
|
+
regulatoryContext: { jurisdiction: 'EU' },
|
|
31
|
+
},
|
|
32
|
+
question: 'What is a snapshot?',
|
|
33
|
+
});
|
|
34
|
+
expect(result.refused).toBeTrue();
|
|
35
|
+
expect(result.refusalReason).toBe('KB_SNAPSHOT_REQUIRED');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('blocks education_only answers that include buy/sell language', async () => {
|
|
39
|
+
const handlers = createDemoAssistantHandlers();
|
|
40
|
+
const result = await handlers.answer({
|
|
41
|
+
envelope: {
|
|
42
|
+
traceId: 't3',
|
|
43
|
+
locale: 'en-GB',
|
|
44
|
+
kbSnapshotId: 'snap_1',
|
|
45
|
+
allowedScope: 'education_only',
|
|
46
|
+
regulatoryContext: { jurisdiction: 'EU' },
|
|
47
|
+
},
|
|
48
|
+
question: 'Should I buy now?',
|
|
49
|
+
});
|
|
50
|
+
// demo handler echoes question; question includes forbidden phrase \"buy\"
|
|
51
|
+
expect(result.refused).toBeTrue();
|
|
52
|
+
expect(result.refusalReason).toBe('SCOPE_VIOLATION');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import {
|
|
2
|
+
enforceAllowedScope,
|
|
3
|
+
enforceCitations,
|
|
4
|
+
validateEnvelope,
|
|
5
|
+
} from '../policy/guard';
|
|
6
|
+
|
|
7
|
+
type AllowedScope = 'education_only' | 'generic_info' | 'escalation_required';
|
|
8
|
+
|
|
9
|
+
interface AssistantAnswerIR {
|
|
10
|
+
locale: string;
|
|
11
|
+
jurisdiction: string;
|
|
12
|
+
allowedScope: AllowedScope;
|
|
13
|
+
sections: { heading: string; body: string }[];
|
|
14
|
+
citations: {
|
|
15
|
+
kbSnapshotId: string;
|
|
16
|
+
sourceType: string;
|
|
17
|
+
sourceId: string;
|
|
18
|
+
title?: string;
|
|
19
|
+
excerpt?: string;
|
|
20
|
+
}[];
|
|
21
|
+
disclaimers?: string[];
|
|
22
|
+
riskFlags?: string[];
|
|
23
|
+
refused?: boolean;
|
|
24
|
+
refusalReason?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface DemoAssistantHandlers {
|
|
28
|
+
answer(input: {
|
|
29
|
+
envelope: {
|
|
30
|
+
traceId: string;
|
|
31
|
+
locale: string;
|
|
32
|
+
kbSnapshotId: string;
|
|
33
|
+
allowedScope: AllowedScope;
|
|
34
|
+
regulatoryContext: { jurisdiction: string };
|
|
35
|
+
};
|
|
36
|
+
question: string;
|
|
37
|
+
}): Promise<AssistantAnswerIR>;
|
|
38
|
+
|
|
39
|
+
explainConcept(input: {
|
|
40
|
+
envelope: {
|
|
41
|
+
traceId: string;
|
|
42
|
+
locale: string;
|
|
43
|
+
kbSnapshotId: string;
|
|
44
|
+
allowedScope: AllowedScope;
|
|
45
|
+
regulatoryContext: { jurisdiction: string };
|
|
46
|
+
};
|
|
47
|
+
conceptKey: string;
|
|
48
|
+
}): Promise<AssistantAnswerIR>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Deterministic demo assistant handlers (no LLM).
|
|
53
|
+
*
|
|
54
|
+
* - Validates envelope
|
|
55
|
+
* - Requires citations
|
|
56
|
+
* - Enforces allowedScope (education_only blocks actionable language)
|
|
57
|
+
*/
|
|
58
|
+
export function createDemoAssistantHandlers(): DemoAssistantHandlers {
|
|
59
|
+
async function answer(input: {
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
envelope: DemoAssistantHandlers['answer'] extends (a: infer A) => any
|
|
62
|
+
? A extends { envelope: infer E }
|
|
63
|
+
? E
|
|
64
|
+
: never
|
|
65
|
+
: never;
|
|
66
|
+
question: string;
|
|
67
|
+
}): Promise<AssistantAnswerIR> {
|
|
68
|
+
const env = validateEnvelope(input.envelope);
|
|
69
|
+
if (!env.ok) {
|
|
70
|
+
return {
|
|
71
|
+
locale: input.envelope.locale ?? 'en-US',
|
|
72
|
+
jurisdiction:
|
|
73
|
+
input.envelope.regulatoryContext?.jurisdiction ?? 'UNKNOWN',
|
|
74
|
+
allowedScope: input.envelope.allowedScope ?? 'education_only',
|
|
75
|
+
sections: [
|
|
76
|
+
{
|
|
77
|
+
heading: 'Request blocked',
|
|
78
|
+
body: env.error.message,
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
citations: [],
|
|
82
|
+
disclaimers: [
|
|
83
|
+
'This system refuses to answer without a valid envelope.',
|
|
84
|
+
],
|
|
85
|
+
riskFlags: [env.error.code],
|
|
86
|
+
refused: true,
|
|
87
|
+
refusalReason: env.error.code,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const draft: AssistantAnswerIR = {
|
|
92
|
+
locale: env.value.locale,
|
|
93
|
+
jurisdiction: env.value.regulatoryContext?.jurisdiction ?? 'UNKNOWN',
|
|
94
|
+
allowedScope: env.value.allowedScope ?? 'education_only',
|
|
95
|
+
sections: [
|
|
96
|
+
{
|
|
97
|
+
heading: 'Answer (demo)',
|
|
98
|
+
body: `You asked: "${input.question}". This demo answer is derived from the KB snapshot only.`,
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
citations: [
|
|
102
|
+
{
|
|
103
|
+
kbSnapshotId: env.value.kbSnapshotId ?? 'unknown',
|
|
104
|
+
sourceType: 'ruleVersion',
|
|
105
|
+
sourceId: 'rv_demo',
|
|
106
|
+
title: 'Demo rule version',
|
|
107
|
+
excerpt: 'Demo excerpt',
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
disclaimers: ['Educational demo only.'],
|
|
111
|
+
riskFlags: [],
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const scope = enforceAllowedScope(env.value.allowedScope, draft);
|
|
115
|
+
if (!scope.ok) {
|
|
116
|
+
return {
|
|
117
|
+
...draft,
|
|
118
|
+
sections: [
|
|
119
|
+
{ heading: 'Escalation required', body: scope.error.message },
|
|
120
|
+
],
|
|
121
|
+
citations: draft.citations,
|
|
122
|
+
refused: true,
|
|
123
|
+
refusalReason: scope.error.code,
|
|
124
|
+
riskFlags: [...(draft.riskFlags ?? []), scope.error.code],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const cited = enforceCitations(draft);
|
|
129
|
+
if (!cited.ok) {
|
|
130
|
+
return {
|
|
131
|
+
...draft,
|
|
132
|
+
sections: [{ heading: 'Request blocked', body: cited.error.message }],
|
|
133
|
+
citations: [],
|
|
134
|
+
refused: true,
|
|
135
|
+
refusalReason: cited.error.code,
|
|
136
|
+
riskFlags: [...(draft.riskFlags ?? []), cited.error.code],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return draft;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function explainConcept(input: {
|
|
144
|
+
envelope: DemoAssistantHandlers['explainConcept'] extends (
|
|
145
|
+
a: infer A
|
|
146
|
+
) => any // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
147
|
+
? A extends { envelope: infer E }
|
|
148
|
+
? E
|
|
149
|
+
: never
|
|
150
|
+
: never;
|
|
151
|
+
conceptKey: string;
|
|
152
|
+
}): Promise<AssistantAnswerIR> {
|
|
153
|
+
return await answer({
|
|
154
|
+
envelope: input.envelope,
|
|
155
|
+
question: `Explain concept: ${input.conceptKey}`,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return { answer, explainConcept };
|
|
160
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './demo.handlers';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Locale/Jurisdiction Gate Example
|
|
3
|
+
*
|
|
4
|
+
* Fail-closed gating for assistant calls: locale + jurisdiction + kbSnapshotId +
|
|
5
|
+
* allowedScope must be explicit, and answers must cite a KB snapshot.
|
|
6
|
+
*/
|
|
7
|
+
export * from './entities';
|
|
8
|
+
export * from './operations';
|
|
9
|
+
export * from './events';
|
|
10
|
+
export * from './policy';
|
|
11
|
+
export * from './handlers';
|
|
12
|
+
export * from './locale-jurisdiction-gate.feature';
|
|
13
|
+
export { default as example } from './example';
|
|
14
|
+
|
|
15
|
+
import './docs';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { FeatureModuleSpec } from '@contractspec/lib.contracts';
|
|
2
|
+
|
|
3
|
+
export const LocaleJurisdictionGateFeature: FeatureModuleSpec = {
|
|
4
|
+
meta: {
|
|
5
|
+
key: 'locale-jurisdiction-gate',
|
|
6
|
+
version: 1,
|
|
7
|
+
title: 'Locale + Jurisdiction Gate',
|
|
8
|
+
description:
|
|
9
|
+
'Fail-closed gating for assistant calls requiring locale/jurisdiction/snapshot/scope and citations.',
|
|
10
|
+
domain: 'knowledge',
|
|
11
|
+
owners: ['@examples'],
|
|
12
|
+
tags: ['assistant', 'policy', 'locale', 'jurisdiction', 'knowledge'],
|
|
13
|
+
stability: 'experimental',
|
|
14
|
+
},
|
|
15
|
+
operations: [
|
|
16
|
+
{ key: 'assistant.answer', version: 1 },
|
|
17
|
+
{ key: 'assistant.explainConcept', version: 1 },
|
|
18
|
+
],
|
|
19
|
+
events: [
|
|
20
|
+
{ key: 'assistant.answer.requested', version: 1 },
|
|
21
|
+
{ key: 'assistant.answer.blocked', version: 1 },
|
|
22
|
+
{ key: 'assistant.answer.delivered', version: 1 },
|
|
23
|
+
],
|
|
24
|
+
presentations: [],
|
|
25
|
+
opToPresentation: [],
|
|
26
|
+
presentationsTargets: [],
|
|
27
|
+
capabilities: {
|
|
28
|
+
requires: [{ key: 'knowledge', version: 1 }],
|
|
29
|
+
},
|
|
30
|
+
};
|