@criterionx/generators 0.3.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Tomas Maritano
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,211 @@
1
+ import { Decision } from '@criterionx/core';
2
+
3
+ /**
4
+ * Types for decision specification format
5
+ */
6
+ /**
7
+ * Schema field type specification
8
+ */
9
+ type SchemaFieldType = "string" | "number" | "boolean" | "date" | "array" | "object";
10
+ /**
11
+ * Schema field definition
12
+ */
13
+ interface SchemaField {
14
+ /** Field type */
15
+ type: SchemaFieldType;
16
+ /** Whether field is optional */
17
+ optional?: boolean;
18
+ /** Description for documentation */
19
+ description?: string;
20
+ /** For arrays: item type */
21
+ items?: SchemaFieldType | SchemaSpec;
22
+ /** For objects: nested schema */
23
+ properties?: Record<string, SchemaField>;
24
+ /** Enum values for string fields */
25
+ enum?: string[];
26
+ /** Minimum value for numbers */
27
+ min?: number;
28
+ /** Maximum value for numbers */
29
+ max?: number;
30
+ /** Default value */
31
+ default?: unknown;
32
+ }
33
+ /**
34
+ * Schema specification
35
+ */
36
+ type SchemaSpec = Record<string, SchemaField>;
37
+ /**
38
+ * Rule condition specification
39
+ */
40
+ interface RuleCondition {
41
+ /** Field path (e.g., "input.value", "profile.threshold") */
42
+ field: string;
43
+ /** Comparison operator */
44
+ operator: "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "contains" | "matches";
45
+ /** Value to compare against (can reference other fields with $) */
46
+ value: unknown;
47
+ }
48
+ /**
49
+ * Rule specification
50
+ */
51
+ interface RuleSpec {
52
+ /** Unique rule ID */
53
+ id: string;
54
+ /** Human-readable description */
55
+ description?: string;
56
+ /** Conditions (all must match - AND logic) */
57
+ when: RuleCondition[] | "always";
58
+ /** Output to emit when rule matches */
59
+ emit: Record<string, unknown>;
60
+ /** Priority (lower runs first, default: 0) */
61
+ priority?: number;
62
+ }
63
+ /**
64
+ * Decision specification
65
+ */
66
+ interface DecisionSpec {
67
+ /** Unique decision ID */
68
+ id: string;
69
+ /** Version string */
70
+ version: string;
71
+ /** Human-readable description */
72
+ description?: string;
73
+ /** Input schema */
74
+ input: SchemaSpec;
75
+ /** Output schema */
76
+ output: SchemaSpec;
77
+ /** Profile schema */
78
+ profile: SchemaSpec;
79
+ /** Rules (evaluated in order) */
80
+ rules: RuleSpec[];
81
+ }
82
+ /**
83
+ * Options for code generation
84
+ */
85
+ interface CodeGenOptions {
86
+ /** Include JSDoc comments */
87
+ includeComments?: boolean;
88
+ /** Use const assertions */
89
+ useConst?: boolean;
90
+ /** Export name for the decision */
91
+ exportName?: string;
92
+ /** Include zod import */
93
+ includeImports?: boolean;
94
+ }
95
+ /**
96
+ * Generated code result
97
+ */
98
+ interface GeneratedCode {
99
+ /** Generated TypeScript code */
100
+ code: string;
101
+ /** Decision ID */
102
+ decisionId: string;
103
+ /** Export name used */
104
+ exportName: string;
105
+ }
106
+
107
+ /**
108
+ * Parse decision specifications into runtime decisions
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * import { parseDecisionSpec } from "@criterionx/generators";
113
+ *
114
+ * const spec = {
115
+ * id: "pricing",
116
+ * version: "1.0.0",
117
+ * input: { quantity: { type: "number" } },
118
+ * output: { price: { type: "number" } },
119
+ * profile: { basePrice: { type: "number" } },
120
+ * rules: [
121
+ * {
122
+ * id: "calculate",
123
+ * when: "always",
124
+ * emit: { price: "$input.quantity * $profile.basePrice" },
125
+ * },
126
+ * ],
127
+ * };
128
+ *
129
+ * const decision = parseDecisionSpec(spec);
130
+ * ```
131
+ */
132
+
133
+ /**
134
+ * Parse a decision specification into a runtime Decision
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * const spec: DecisionSpec = {
139
+ * id: "eligibility",
140
+ * version: "1.0.0",
141
+ * input: { age: { type: "number" } },
142
+ * output: { eligible: { type: "boolean" }, reason: { type: "string" } },
143
+ * profile: { minAge: { type: "number" } },
144
+ * rules: [
145
+ * {
146
+ * id: "too-young",
147
+ * when: [{ field: "input.age", operator: "lt", value: "$profile.minAge" }],
148
+ * emit: { eligible: false, reason: "Too young" },
149
+ * },
150
+ * {
151
+ * id: "eligible",
152
+ * when: "always",
153
+ * emit: { eligible: true, reason: "Meets requirements" },
154
+ * },
155
+ * ],
156
+ * };
157
+ *
158
+ * const decision = parseDecisionSpec(spec);
159
+ * const result = engine.run(decision, { age: 25 }, { profile: { minAge: 18 } });
160
+ * ```
161
+ */
162
+ declare function parseDecisionSpec<TInput, TOutput, TProfile>(spec: DecisionSpec): Decision<TInput, TOutput, TProfile>;
163
+ /**
164
+ * Parse multiple decision specifications
165
+ */
166
+ declare function parseDecisionSpecs(specs: DecisionSpec[]): Map<string, Decision<any, any, any>>;
167
+
168
+ /**
169
+ * Generate TypeScript code from decision specifications
170
+ *
171
+ * @example
172
+ * ```typescript
173
+ * import { generateDecisionCode } from "@criterionx/generators";
174
+ *
175
+ * const spec = {
176
+ * id: "pricing",
177
+ * version: "1.0.0",
178
+ * input: { quantity: { type: "number" } },
179
+ * output: { price: { type: "number" } },
180
+ * profile: { basePrice: { type: "number" } },
181
+ * rules: [
182
+ * { id: "calculate", when: "always", emit: { price: 0 } },
183
+ * ],
184
+ * };
185
+ *
186
+ * const { code } = generateDecisionCode(spec);
187
+ * // Outputs TypeScript code defining the decision
188
+ * ```
189
+ */
190
+
191
+ /**
192
+ * Generate TypeScript code for a decision specification
193
+ *
194
+ * @example
195
+ * ```typescript
196
+ * const { code } = generateDecisionCode(spec, {
197
+ * includeComments: true,
198
+ * includeImports: true,
199
+ * });
200
+ *
201
+ * // Write to file
202
+ * fs.writeFileSync("pricing.decision.ts", code);
203
+ * ```
204
+ */
205
+ declare function generateDecisionCode(spec: DecisionSpec, options?: CodeGenOptions): GeneratedCode;
206
+ /**
207
+ * Generate TypeScript code for multiple decisions in a single file
208
+ */
209
+ declare function generateDecisionsFile(specs: DecisionSpec[], options?: CodeGenOptions): string;
210
+
211
+ export { type CodeGenOptions, type DecisionSpec, type GeneratedCode, type RuleCondition, type RuleSpec, type SchemaField, type SchemaFieldType, type SchemaSpec, generateDecisionCode, generateDecisionsFile, parseDecisionSpec, parseDecisionSpecs };