@vorionsys/contracts 0.1.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/dist/aci/aci-string.d.ts +539 -0
- package/dist/aci/aci-string.d.ts.map +1 -0
- package/dist/aci/aci-string.js +563 -0
- package/dist/aci/aci-string.js.map +1 -0
- package/dist/aci/attestation.d.ts +648 -0
- package/dist/aci/attestation.d.ts.map +1 -0
- package/dist/aci/attestation.js +289 -0
- package/dist/aci/attestation.js.map +1 -0
- package/dist/aci/domains.d.ts +260 -0
- package/dist/aci/domains.d.ts.map +1 -0
- package/dist/aci/domains.js +322 -0
- package/dist/aci/domains.js.map +1 -0
- package/dist/aci/effective-permission.d.ts +371 -0
- package/dist/aci/effective-permission.d.ts.map +1 -0
- package/dist/aci/effective-permission.js +351 -0
- package/dist/aci/effective-permission.js.map +1 -0
- package/dist/aci/identity.d.ts +1100 -0
- package/dist/aci/identity.d.ts.map +1 -0
- package/dist/aci/identity.js +328 -0
- package/dist/aci/identity.js.map +1 -0
- package/dist/aci/index.d.ts +67 -0
- package/dist/aci/index.d.ts.map +1 -0
- package/dist/aci/index.js +157 -0
- package/dist/aci/index.js.map +1 -0
- package/dist/aci/jwt-claims.d.ts +756 -0
- package/dist/aci/jwt-claims.d.ts.map +1 -0
- package/dist/aci/jwt-claims.js +335 -0
- package/dist/aci/jwt-claims.js.map +1 -0
- package/dist/aci/levels.d.ts +279 -0
- package/dist/aci/levels.d.ts.map +1 -0
- package/dist/aci/levels.js +467 -0
- package/dist/aci/levels.js.map +1 -0
- package/dist/aci/mapping.d.ts +291 -0
- package/dist/aci/mapping.d.ts.map +1 -0
- package/dist/aci/mapping.js +427 -0
- package/dist/aci/mapping.js.map +1 -0
- package/dist/aci/skills.d.ts +314 -0
- package/dist/aci/skills.d.ts.map +1 -0
- package/dist/aci/skills.js +404 -0
- package/dist/aci/skills.js.map +1 -0
- package/dist/aci/tiers.d.ts +403 -0
- package/dist/aci/tiers.d.ts.map +1 -0
- package/dist/aci/tiers.js +659 -0
- package/dist/aci/tiers.js.map +1 -0
- package/dist/canonical/agent.d.ts +796 -0
- package/dist/canonical/agent.d.ts.map +1 -0
- package/dist/canonical/agent.js +527 -0
- package/dist/canonical/agent.js.map +1 -0
- package/dist/canonical/governance.d.ts +905 -0
- package/dist/canonical/governance.d.ts.map +1 -0
- package/dist/canonical/governance.js +454 -0
- package/dist/canonical/governance.js.map +1 -0
- package/dist/canonical/index.d.ts +17 -0
- package/dist/canonical/index.d.ts.map +1 -0
- package/dist/canonical/index.js +21 -0
- package/dist/canonical/index.js.map +1 -0
- package/dist/canonical/intent.d.ts +727 -0
- package/dist/canonical/intent.d.ts.map +1 -0
- package/dist/canonical/intent.js +203 -0
- package/dist/canonical/intent.js.map +1 -0
- package/dist/canonical/risk-level.d.ts +344 -0
- package/dist/canonical/risk-level.d.ts.map +1 -0
- package/dist/canonical/risk-level.js +472 -0
- package/dist/canonical/risk-level.js.map +1 -0
- package/dist/canonical/trust-band.d.ts +239 -0
- package/dist/canonical/trust-band.d.ts.map +1 -0
- package/dist/canonical/trust-band.js +298 -0
- package/dist/canonical/trust-band.js.map +1 -0
- package/dist/canonical/trust-score.d.ts +301 -0
- package/dist/canonical/trust-score.d.ts.map +1 -0
- package/dist/canonical/trust-score.js +390 -0
- package/dist/canonical/trust-score.js.map +1 -0
- package/dist/canonical/trust-signal.d.ts +617 -0
- package/dist/canonical/trust-signal.d.ts.map +1 -0
- package/dist/canonical/trust-signal.js +355 -0
- package/dist/canonical/trust-signal.js.map +1 -0
- package/dist/canonical/validation.d.ts +231 -0
- package/dist/canonical/validation.d.ts.map +1 -0
- package/dist/canonical/validation.js +558 -0
- package/dist/canonical/validation.js.map +1 -0
- package/dist/common/index.d.ts +7 -0
- package/dist/common/index.d.ts.map +1 -0
- package/dist/common/index.js +7 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/primitives.d.ts +56 -0
- package/dist/common/primitives.d.ts.map +1 -0
- package/dist/common/primitives.js +70 -0
- package/dist/common/primitives.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/v2/canary-probe.d.ts +201 -0
- package/dist/v2/canary-probe.d.ts.map +1 -0
- package/dist/v2/canary-probe.js +99 -0
- package/dist/v2/canary-probe.js.map +1 -0
- package/dist/v2/component.d.ts +132 -0
- package/dist/v2/component.d.ts.map +1 -0
- package/dist/v2/component.js +5 -0
- package/dist/v2/component.js.map +1 -0
- package/dist/v2/decision.d.ts +310 -0
- package/dist/v2/decision.d.ts.map +1 -0
- package/dist/v2/decision.js +21 -0
- package/dist/v2/decision.js.map +1 -0
- package/dist/v2/enums.d.ts +185 -0
- package/dist/v2/enums.d.ts.map +1 -0
- package/dist/v2/enums.js +203 -0
- package/dist/v2/enums.js.map +1 -0
- package/dist/v2/evidence.d.ts +368 -0
- package/dist/v2/evidence.d.ts.map +1 -0
- package/dist/v2/evidence.js +152 -0
- package/dist/v2/evidence.js.map +1 -0
- package/dist/v2/execution.d.ts +190 -0
- package/dist/v2/execution.d.ts.map +1 -0
- package/dist/v2/execution.js +5 -0
- package/dist/v2/execution.js.map +1 -0
- package/dist/v2/index.d.ts +19 -0
- package/dist/v2/index.d.ts.map +1 -0
- package/dist/v2/index.js +32 -0
- package/dist/v2/index.js.map +1 -0
- package/dist/v2/intent.d.ts +89 -0
- package/dist/v2/intent.d.ts.map +1 -0
- package/dist/v2/intent.js +5 -0
- package/dist/v2/intent.js.map +1 -0
- package/dist/v2/policy-bundle.d.ts +166 -0
- package/dist/v2/policy-bundle.d.ts.map +1 -0
- package/dist/v2/policy-bundle.js +20 -0
- package/dist/v2/policy-bundle.js.map +1 -0
- package/dist/v2/pre-action-gate.d.ts +185 -0
- package/dist/v2/pre-action-gate.d.ts.map +1 -0
- package/dist/v2/pre-action-gate.js +64 -0
- package/dist/v2/pre-action-gate.js.map +1 -0
- package/dist/v2/proof-event.d.ts +201 -0
- package/dist/v2/proof-event.d.ts.map +1 -0
- package/dist/v2/proof-event.js +5 -0
- package/dist/v2/proof-event.js.map +1 -0
- package/dist/v2/retention.d.ts +329 -0
- package/dist/v2/retention.d.ts.map +1 -0
- package/dist/v2/retention.js +162 -0
- package/dist/v2/retention.js.map +1 -0
- package/dist/v2/trust-delta.d.ts +119 -0
- package/dist/v2/trust-delta.d.ts.map +1 -0
- package/dist/v2/trust-delta.js +28 -0
- package/dist/v2/trust-delta.js.map +1 -0
- package/dist/v2/trust-profile.d.ts +337 -0
- package/dist/v2/trust-profile.d.ts.map +1 -0
- package/dist/v2/trust-profile.js +96 -0
- package/dist/v2/trust-profile.js.map +1 -0
- package/dist/validators/decision.d.ts +430 -0
- package/dist/validators/decision.d.ts.map +1 -0
- package/dist/validators/decision.js +61 -0
- package/dist/validators/decision.js.map +1 -0
- package/dist/validators/enums.d.ts +24 -0
- package/dist/validators/enums.d.ts.map +1 -0
- package/dist/validators/enums.js +24 -0
- package/dist/validators/enums.js.map +1 -0
- package/dist/validators/index.d.ts +30 -0
- package/dist/validators/index.d.ts.map +1 -0
- package/dist/validators/index.js +42 -0
- package/dist/validators/index.js.map +1 -0
- package/dist/validators/intent.d.ts +229 -0
- package/dist/validators/intent.d.ts.map +1 -0
- package/dist/validators/intent.js +47 -0
- package/dist/validators/intent.js.map +1 -0
- package/dist/validators/proof-event.d.ts +981 -0
- package/dist/validators/proof-event.d.ts.map +1 -0
- package/dist/validators/proof-event.js +134 -0
- package/dist/validators/proof-event.js.map +1 -0
- package/dist/validators/trust-profile.d.ts +350 -0
- package/dist/validators/trust-profile.d.ts.map +1 -0
- package/dist/validators/trust-profile.js +65 -0
- package/dist/validators/trust-profile.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Canonical RiskLevel type definitions for the Vorion Platform.
|
|
3
|
+
*
|
|
4
|
+
* This file provides the authoritative definition for risk levels, unifying
|
|
5
|
+
* the various representations found across packages (enum, string union, numeric)
|
|
6
|
+
* into a single canonical source with conversion utilities.
|
|
7
|
+
*
|
|
8
|
+
* Risk levels classify the potential impact or severity of actions, decisions,
|
|
9
|
+
* and events within the platform.
|
|
10
|
+
*
|
|
11
|
+
* @module @vorion/contracts/canonical/risk-level
|
|
12
|
+
*/
|
|
13
|
+
import { z } from 'zod';
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Constants
|
|
16
|
+
// ============================================================================
|
|
17
|
+
/**
|
|
18
|
+
* Array of all risk levels in ascending severity order.
|
|
19
|
+
*
|
|
20
|
+
* Useful for iteration, validation, and UI components.
|
|
21
|
+
*/
|
|
22
|
+
export const RISK_LEVELS = ['low', 'medium', 'high', 'critical'];
|
|
23
|
+
/**
|
|
24
|
+
* Numeric values for risk levels (0-3 scale).
|
|
25
|
+
*
|
|
26
|
+
* Enables numeric comparisons and calculations while maintaining
|
|
27
|
+
* the string type as the canonical representation.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* RISK_LEVEL_VALUES['critical']; // 3
|
|
32
|
+
* RISK_LEVEL_VALUES['low']; // 0
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export const RISK_LEVEL_VALUES = {
|
|
36
|
+
low: 0,
|
|
37
|
+
medium: 1,
|
|
38
|
+
high: 2,
|
|
39
|
+
critical: 3,
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Inverse mapping from numeric values to risk levels.
|
|
43
|
+
*
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
const NUMERIC_TO_RISK_LEVEL = {
|
|
47
|
+
0: 'low',
|
|
48
|
+
1: 'medium',
|
|
49
|
+
2: 'high',
|
|
50
|
+
3: 'critical',
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Human-readable labels for risk levels.
|
|
54
|
+
*
|
|
55
|
+
* Useful for display in UIs and reports.
|
|
56
|
+
*/
|
|
57
|
+
export const RISK_LEVEL_LABELS = {
|
|
58
|
+
low: 'Low Risk',
|
|
59
|
+
medium: 'Medium Risk',
|
|
60
|
+
high: 'High Risk',
|
|
61
|
+
critical: 'Critical Risk',
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Detailed descriptions for each risk level.
|
|
65
|
+
*
|
|
66
|
+
* Provides guidance on what each level means and when to use it.
|
|
67
|
+
*/
|
|
68
|
+
export const RISK_LEVEL_DESCRIPTIONS = {
|
|
69
|
+
low: 'Minimal risk. Standard operation with normal monitoring.',
|
|
70
|
+
medium: 'Moderate risk. Enhanced monitoring and logging recommended.',
|
|
71
|
+
high: 'Significant risk. Additional safeguards and review required.',
|
|
72
|
+
critical: 'Severe risk. Human intervention may be required. Proceed with extreme caution.',
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Color codes for risk levels (for UI display).
|
|
76
|
+
*
|
|
77
|
+
* Uses common color conventions: green for low, yellow/orange for medium,
|
|
78
|
+
* red for high, and dark red for critical.
|
|
79
|
+
*/
|
|
80
|
+
export const RISK_LEVEL_COLORS = {
|
|
81
|
+
low: '#22c55e', // Green
|
|
82
|
+
medium: '#f59e0b', // Amber
|
|
83
|
+
high: '#ef4444', // Red
|
|
84
|
+
critical: '#7f1d1d', // Dark Red
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Default risk level for unclassified items.
|
|
88
|
+
*/
|
|
89
|
+
export const DEFAULT_RISK_LEVEL = 'low';
|
|
90
|
+
// ============================================================================
|
|
91
|
+
// Conversion Functions
|
|
92
|
+
// ============================================================================
|
|
93
|
+
/**
|
|
94
|
+
* Converts a numeric value to a RiskLevel.
|
|
95
|
+
*
|
|
96
|
+
* Handles the 5-level numeric system (0-4) used by some packages
|
|
97
|
+
* by mapping level 4 to 'critical' (same as level 3).
|
|
98
|
+
*
|
|
99
|
+
* @param n - Numeric value (0-4)
|
|
100
|
+
* @returns Corresponding RiskLevel
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* riskLevelFromNumber(0); // 'low'
|
|
105
|
+
* riskLevelFromNumber(2); // 'high'
|
|
106
|
+
* riskLevelFromNumber(4); // 'critical' (Council's L4 maps to critical)
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
export function riskLevelFromNumber(n) {
|
|
110
|
+
if (n <= 0)
|
|
111
|
+
return 'low';
|
|
112
|
+
if (n === 1)
|
|
113
|
+
return 'medium';
|
|
114
|
+
if (n === 2)
|
|
115
|
+
return 'high';
|
|
116
|
+
return 'critical'; // 3, 4, or higher all map to critical
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Converts a RiskLevel to its numeric value.
|
|
120
|
+
*
|
|
121
|
+
* @param level - RiskLevel to convert
|
|
122
|
+
* @returns Numeric value (0-3)
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* riskLevelToNumber('low'); // 0
|
|
127
|
+
* riskLevelToNumber('critical'); // 3
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export function riskLevelToNumber(level) {
|
|
131
|
+
return RISK_LEVEL_VALUES[level];
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Safely parses a value to RiskLevel.
|
|
135
|
+
*
|
|
136
|
+
* Handles various input formats:
|
|
137
|
+
* - String values (case-insensitive)
|
|
138
|
+
* - Numeric values (0-4)
|
|
139
|
+
* - Enum-style uppercase strings
|
|
140
|
+
*
|
|
141
|
+
* @param value - Value to parse
|
|
142
|
+
* @returns RiskLevel or null if unparseable
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* parseRiskLevel('LOW'); // 'low'
|
|
147
|
+
* parseRiskLevel('MEDIUM'); // 'medium'
|
|
148
|
+
* parseRiskLevel(2); // 'high'
|
|
149
|
+
* parseRiskLevel('invalid'); // null
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export function parseRiskLevel(value) {
|
|
153
|
+
if (typeof value === 'number') {
|
|
154
|
+
if (value >= 0 && value <= 4 && Number.isInteger(value)) {
|
|
155
|
+
return riskLevelFromNumber(value);
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
if (typeof value === 'string') {
|
|
160
|
+
const normalized = value.toLowerCase().trim();
|
|
161
|
+
if (RISK_LEVELS.includes(normalized)) {
|
|
162
|
+
return normalized;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Parses a value to RiskLevel with a default fallback.
|
|
169
|
+
*
|
|
170
|
+
* @param value - Value to parse
|
|
171
|
+
* @param defaultLevel - Default level if parsing fails
|
|
172
|
+
* @returns RiskLevel (parsed or default)
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* parseRiskLevelOrDefault('high', 'low'); // 'high'
|
|
177
|
+
* parseRiskLevelOrDefault('invalid', 'low'); // 'low'
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export function parseRiskLevelOrDefault(value, defaultLevel = DEFAULT_RISK_LEVEL) {
|
|
181
|
+
return parseRiskLevel(value) ?? defaultLevel;
|
|
182
|
+
}
|
|
183
|
+
// ============================================================================
|
|
184
|
+
// Comparison Functions
|
|
185
|
+
// ============================================================================
|
|
186
|
+
/**
|
|
187
|
+
* Compares two risk levels.
|
|
188
|
+
*
|
|
189
|
+
* @param a - First risk level
|
|
190
|
+
* @param b - Second risk level
|
|
191
|
+
* @returns -1 if a < b, 0 if equal, 1 if a > b
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* compareRiskLevels('low', 'high'); // -1
|
|
196
|
+
* compareRiskLevels('high', 'medium'); // 1
|
|
197
|
+
* compareRiskLevels('medium', 'medium'); // 0
|
|
198
|
+
* ```
|
|
199
|
+
*/
|
|
200
|
+
export function compareRiskLevels(a, b) {
|
|
201
|
+
const diff = RISK_LEVEL_VALUES[a] - RISK_LEVEL_VALUES[b];
|
|
202
|
+
if (diff < 0)
|
|
203
|
+
return -1;
|
|
204
|
+
if (diff > 0)
|
|
205
|
+
return 1;
|
|
206
|
+
return 0;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Checks if a risk level is at least as severe as another.
|
|
210
|
+
*
|
|
211
|
+
* @param level - Level to check
|
|
212
|
+
* @param threshold - Minimum threshold
|
|
213
|
+
* @returns True if level meets or exceeds threshold
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* isRiskAtLeast('high', 'medium'); // true
|
|
218
|
+
* isRiskAtLeast('low', 'medium'); // false
|
|
219
|
+
* isRiskAtLeast('medium', 'medium'); // true
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
export function isRiskAtLeast(level, threshold) {
|
|
223
|
+
return RISK_LEVEL_VALUES[level] >= RISK_LEVEL_VALUES[threshold];
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Checks if a risk level is more severe than another.
|
|
227
|
+
*
|
|
228
|
+
* @param level - Level to check
|
|
229
|
+
* @param other - Level to compare against
|
|
230
|
+
* @returns True if level is more severe than other
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```typescript
|
|
234
|
+
* isRiskHigherThan('high', 'medium'); // true
|
|
235
|
+
* isRiskHigherThan('medium', 'high'); // false
|
|
236
|
+
* isRiskHigherThan('high', 'high'); // false
|
|
237
|
+
* ```
|
|
238
|
+
*/
|
|
239
|
+
export function isRiskHigherThan(level, other) {
|
|
240
|
+
return RISK_LEVEL_VALUES[level] > RISK_LEVEL_VALUES[other];
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Gets the maximum (most severe) risk level from an array.
|
|
244
|
+
*
|
|
245
|
+
* @param levels - Array of risk levels
|
|
246
|
+
* @returns Most severe risk level, or 'low' if array is empty
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```typescript
|
|
250
|
+
* maxRiskLevel(['low', 'medium', 'high']); // 'high'
|
|
251
|
+
* maxRiskLevel(['low', 'low']); // 'low'
|
|
252
|
+
* maxRiskLevel([]); // 'low'
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
export function maxRiskLevel(levels) {
|
|
256
|
+
if (levels.length === 0)
|
|
257
|
+
return 'low';
|
|
258
|
+
return levels.reduce((max, level) => isRiskHigherThan(level, max) ? level : max);
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Gets the minimum (least severe) risk level from an array.
|
|
262
|
+
*
|
|
263
|
+
* @param levels - Array of risk levels
|
|
264
|
+
* @returns Least severe risk level, or 'critical' if array is empty
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* minRiskLevel(['low', 'medium', 'high']); // 'low'
|
|
269
|
+
* minRiskLevel(['high', 'critical']); // 'high'
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
export function minRiskLevel(levels) {
|
|
273
|
+
if (levels.length === 0)
|
|
274
|
+
return 'critical';
|
|
275
|
+
return levels.reduce((min, level) => isRiskHigherThan(min, level) ? level : min);
|
|
276
|
+
}
|
|
277
|
+
// ============================================================================
|
|
278
|
+
// Escalation Functions
|
|
279
|
+
// ============================================================================
|
|
280
|
+
/**
|
|
281
|
+
* Escalates a risk level by one step (if possible).
|
|
282
|
+
*
|
|
283
|
+
* @param level - Current risk level
|
|
284
|
+
* @returns Escalated risk level (or same if already critical)
|
|
285
|
+
*
|
|
286
|
+
* @example
|
|
287
|
+
* ```typescript
|
|
288
|
+
* escalateRiskLevel('low'); // 'medium'
|
|
289
|
+
* escalateRiskLevel('high'); // 'critical'
|
|
290
|
+
* escalateRiskLevel('critical'); // 'critical'
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
export function escalateRiskLevel(level) {
|
|
294
|
+
const currentValue = RISK_LEVEL_VALUES[level];
|
|
295
|
+
const escalatedValue = Math.min(currentValue + 1, 3);
|
|
296
|
+
return NUMERIC_TO_RISK_LEVEL[escalatedValue] ?? 'critical';
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* De-escalates a risk level by one step (if possible).
|
|
300
|
+
*
|
|
301
|
+
* @param level - Current risk level
|
|
302
|
+
* @returns De-escalated risk level (or same if already low)
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* deescalateRiskLevel('critical'); // 'high'
|
|
307
|
+
* deescalateRiskLevel('medium'); // 'low'
|
|
308
|
+
* deescalateRiskLevel('low'); // 'low'
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
export function deescalateRiskLevel(level) {
|
|
312
|
+
const currentValue = RISK_LEVEL_VALUES[level];
|
|
313
|
+
const deescalatedValue = Math.max(currentValue - 1, 0);
|
|
314
|
+
return NUMERIC_TO_RISK_LEVEL[deescalatedValue] ?? 'low';
|
|
315
|
+
}
|
|
316
|
+
// ============================================================================
|
|
317
|
+
// Type Guards
|
|
318
|
+
// ============================================================================
|
|
319
|
+
/**
|
|
320
|
+
* Type guard to check if a value is a valid RiskLevel.
|
|
321
|
+
*
|
|
322
|
+
* @param value - Value to check
|
|
323
|
+
* @returns True if value is a valid RiskLevel
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```typescript
|
|
327
|
+
* isRiskLevel('high'); // true
|
|
328
|
+
* isRiskLevel('severe'); // false
|
|
329
|
+
* isRiskLevel(2); // false
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
export function isRiskLevel(value) {
|
|
333
|
+
return typeof value === 'string' && RISK_LEVELS.includes(value);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Type guard to check if a risk level requires elevated attention.
|
|
337
|
+
*
|
|
338
|
+
* Returns true for 'high' and 'critical' levels.
|
|
339
|
+
*
|
|
340
|
+
* @param level - Risk level to check
|
|
341
|
+
* @returns True if level requires elevated attention
|
|
342
|
+
*/
|
|
343
|
+
export function requiresElevatedAttention(level) {
|
|
344
|
+
return level === 'high' || level === 'critical';
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Type guard to check if a risk level is critical.
|
|
348
|
+
*
|
|
349
|
+
* @param level - Risk level to check
|
|
350
|
+
* @returns True if level is critical
|
|
351
|
+
*/
|
|
352
|
+
export function isCriticalRisk(level) {
|
|
353
|
+
return level === 'critical';
|
|
354
|
+
}
|
|
355
|
+
// ============================================================================
|
|
356
|
+
// Display Functions
|
|
357
|
+
// ============================================================================
|
|
358
|
+
/**
|
|
359
|
+
* Gets the human-readable label for a risk level.
|
|
360
|
+
*
|
|
361
|
+
* @param level - Risk level
|
|
362
|
+
* @returns Human-readable label
|
|
363
|
+
*/
|
|
364
|
+
export function getRiskLevelLabel(level) {
|
|
365
|
+
return RISK_LEVEL_LABELS[level];
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Gets the description for a risk level.
|
|
369
|
+
*
|
|
370
|
+
* @param level - Risk level
|
|
371
|
+
* @returns Description string
|
|
372
|
+
*/
|
|
373
|
+
export function getRiskLevelDescription(level) {
|
|
374
|
+
return RISK_LEVEL_DESCRIPTIONS[level];
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Gets the color code for a risk level.
|
|
378
|
+
*
|
|
379
|
+
* @param level - Risk level
|
|
380
|
+
* @returns Hex color code
|
|
381
|
+
*/
|
|
382
|
+
export function getRiskLevelColor(level) {
|
|
383
|
+
return RISK_LEVEL_COLORS[level];
|
|
384
|
+
}
|
|
385
|
+
// ============================================================================
|
|
386
|
+
// Zod Schemas
|
|
387
|
+
// ============================================================================
|
|
388
|
+
/**
|
|
389
|
+
* Zod schema for RiskLevel validation.
|
|
390
|
+
*
|
|
391
|
+
* Validates that a value is one of the canonical risk level strings.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```typescript
|
|
395
|
+
* riskLevelSchema.parse('medium'); // Success
|
|
396
|
+
* riskLevelSchema.parse('severe'); // Throws ZodError
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
export const riskLevelSchema = z.enum(['low', 'medium', 'high', 'critical'], {
|
|
400
|
+
errorMap: () => ({
|
|
401
|
+
message: "Invalid risk level. Must be 'low', 'medium', 'high', or 'critical'.",
|
|
402
|
+
}),
|
|
403
|
+
});
|
|
404
|
+
/**
|
|
405
|
+
* Zod schema that accepts numeric input and transforms to RiskLevel.
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* ```typescript
|
|
409
|
+
* riskLevelFromNumberSchema.parse(2); // 'high'
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
412
|
+
export const riskLevelFromNumberSchema = z
|
|
413
|
+
.number()
|
|
414
|
+
.int()
|
|
415
|
+
.min(0)
|
|
416
|
+
.max(4)
|
|
417
|
+
.transform((n) => riskLevelFromNumber(n));
|
|
418
|
+
/**
|
|
419
|
+
* Zod schema that accepts string input (case-insensitive) and transforms to RiskLevel.
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* ```typescript
|
|
423
|
+
* riskLevelFlexibleSchema.parse('HIGH'); // 'high'
|
|
424
|
+
* riskLevelFlexibleSchema.parse('Medium'); // 'medium'
|
|
425
|
+
* ```
|
|
426
|
+
*/
|
|
427
|
+
export const riskLevelFlexibleSchema = z
|
|
428
|
+
.string()
|
|
429
|
+
.transform((s) => s.toLowerCase().trim())
|
|
430
|
+
.pipe(riskLevelSchema);
|
|
431
|
+
/**
|
|
432
|
+
* Zod schema that accepts multiple input formats and normalizes to RiskLevel.
|
|
433
|
+
*
|
|
434
|
+
* Accepts: lowercase strings, uppercase strings, numbers (0-4).
|
|
435
|
+
*/
|
|
436
|
+
export const riskLevelUnionSchema = z.union([
|
|
437
|
+
riskLevelSchema,
|
|
438
|
+
riskLevelFlexibleSchema,
|
|
439
|
+
riskLevelFromNumberSchema,
|
|
440
|
+
]);
|
|
441
|
+
// ============================================================================
|
|
442
|
+
// Legacy Compatibility
|
|
443
|
+
// ============================================================================
|
|
444
|
+
/**
|
|
445
|
+
* Enum-style representation for compatibility with older code.
|
|
446
|
+
*
|
|
447
|
+
* @deprecated Use RiskLevel string type directly. This is for migration only.
|
|
448
|
+
*/
|
|
449
|
+
export const RiskLevelEnum = {
|
|
450
|
+
LOW: 'low',
|
|
451
|
+
MEDIUM: 'medium',
|
|
452
|
+
HIGH: 'high',
|
|
453
|
+
CRITICAL: 'critical',
|
|
454
|
+
};
|
|
455
|
+
/**
|
|
456
|
+
* Maps legacy uppercase enum values to canonical RiskLevel.
|
|
457
|
+
*
|
|
458
|
+
* @deprecated Use RiskLevel string type directly. This is for migration only.
|
|
459
|
+
*/
|
|
460
|
+
export const LEGACY_RISK_LEVEL_MAP = {
|
|
461
|
+
LOW: 'low',
|
|
462
|
+
MEDIUM: 'medium',
|
|
463
|
+
HIGH: 'high',
|
|
464
|
+
CRITICAL: 'critical',
|
|
465
|
+
// Numeric string variants
|
|
466
|
+
'0': 'low',
|
|
467
|
+
'1': 'medium',
|
|
468
|
+
'2': 'high',
|
|
469
|
+
'3': 'critical',
|
|
470
|
+
'4': 'critical',
|
|
471
|
+
};
|
|
472
|
+
//# sourceMappingURL=risk-level.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"risk-level.js","sourceRoot":"","sources":["../../src/canonical/risk-level.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AA0BxB,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAyB,CAAC,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,CAAU,CAAC;AAEhG;;;;;;;;;;;GAWG;AACH,MAAM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