@sebasoft/neuron-js 0.3.0 → 0.5.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.
Files changed (178) hide show
  1. package/README.md +84 -45
  2. package/dist/commonjs/Synapse.d.ts +18 -0
  3. package/dist/commonjs/Synapse.d.ts.map +1 -1
  4. package/dist/commonjs/Synapse.js +18 -0
  5. package/dist/commonjs/Synapse.js.map +1 -1
  6. package/dist/commonjs/abstracts/AbstractAction.d.ts +15 -0
  7. package/dist/commonjs/abstracts/AbstractAction.d.ts.map +1 -0
  8. package/dist/commonjs/abstracts/AbstractAction.js +25 -0
  9. package/dist/commonjs/abstracts/AbstractAction.js.map +1 -0
  10. package/dist/commonjs/abstracts/AbstractCondition.d.ts +15 -0
  11. package/dist/commonjs/abstracts/AbstractCondition.d.ts.map +1 -0
  12. package/dist/commonjs/abstracts/AbstractCondition.js +25 -0
  13. package/dist/commonjs/abstracts/AbstractCondition.js.map +1 -0
  14. package/dist/commonjs/contracts/explain.d.ts +9 -0
  15. package/dist/commonjs/contracts/explain.d.ts.map +1 -0
  16. package/dist/commonjs/contracts/explain.js +51 -0
  17. package/dist/commonjs/contracts/explain.js.map +1 -0
  18. package/dist/commonjs/contracts/validation.d.ts +40 -0
  19. package/dist/commonjs/contracts/validation.d.ts.map +1 -0
  20. package/dist/commonjs/contracts/validation.js +263 -0
  21. package/dist/commonjs/contracts/validation.js.map +1 -0
  22. package/dist/commonjs/index.d.ts +64 -0
  23. package/dist/commonjs/index.d.ts.map +1 -1
  24. package/dist/commonjs/index.js +74 -1
  25. package/dist/commonjs/index.js.map +1 -1
  26. package/dist/commonjs/interfaces/Action.d.ts +14 -0
  27. package/dist/commonjs/interfaces/Action.d.ts.map +1 -1
  28. package/dist/commonjs/interfaces/Condition.d.ts +21 -0
  29. package/dist/commonjs/interfaces/Condition.d.ts.map +1 -1
  30. package/dist/commonjs/interfaces/Element.d.ts +16 -0
  31. package/dist/commonjs/interfaces/Element.d.ts.map +1 -1
  32. package/dist/commonjs/interfaces/HookEvents.d.ts +16 -0
  33. package/dist/commonjs/interfaces/HookEvents.d.ts.map +1 -1
  34. package/dist/commonjs/interfaces/HookEvents.js +16 -0
  35. package/dist/commonjs/interfaces/HookEvents.js.map +1 -1
  36. package/dist/commonjs/interfaces/Parameter.d.ts +16 -0
  37. package/dist/commonjs/interfaces/Parameter.d.ts.map +1 -1
  38. package/dist/commonjs/interfaces/Rule.d.ts +23 -0
  39. package/dist/commonjs/interfaces/Rule.d.ts.map +1 -1
  40. package/dist/commonjs/interfaces/Script.d.ts +10 -0
  41. package/dist/commonjs/interfaces/Script.d.ts.map +1 -1
  42. package/dist/commonjs/runtime/ActionRuntime.js +1 -1
  43. package/dist/commonjs/runtime/ActionRuntime.js.map +1 -1
  44. package/dist/commonjs/runtime/ConditionRuntime.js +3 -3
  45. package/dist/commonjs/runtime/ConditionRuntime.js.map +1 -1
  46. package/dist/commonjs/runtime/RuleRuntime.js +1 -1
  47. package/dist/commonjs/runtime/RuleRuntime.js.map +1 -1
  48. package/dist/commonjs/types/ExecutionContext.d.ts +13 -0
  49. package/dist/commonjs/types/ExecutionContext.d.ts.map +1 -1
  50. package/dist/commonjs/types/ExecutionContext.js +3 -0
  51. package/dist/commonjs/types/ExecutionContext.js.map +1 -1
  52. package/dist/commonjs/types/ExecutionResult.d.ts +14 -0
  53. package/dist/commonjs/types/ExecutionResult.d.ts.map +1 -1
  54. package/dist/commonjs/types/ExecutionResult.js +14 -0
  55. package/dist/commonjs/types/ExecutionResult.js.map +1 -1
  56. package/dist/commonjs/types/HookEmitter.d.ts +8 -1
  57. package/dist/commonjs/types/HookEmitter.d.ts.map +1 -1
  58. package/dist/esm/Synapse.d.ts +18 -0
  59. package/dist/esm/Synapse.d.ts.map +1 -1
  60. package/dist/esm/Synapse.js +18 -0
  61. package/dist/esm/Synapse.js.map +1 -1
  62. package/dist/esm/abstracts/AbstractAction.d.ts +15 -0
  63. package/dist/esm/abstracts/AbstractAction.d.ts.map +1 -0
  64. package/dist/esm/abstracts/AbstractAction.js +21 -0
  65. package/dist/esm/abstracts/AbstractAction.js.map +1 -0
  66. package/dist/esm/abstracts/AbstractCondition.d.ts +15 -0
  67. package/dist/esm/abstracts/AbstractCondition.d.ts.map +1 -0
  68. package/dist/esm/abstracts/AbstractCondition.js +21 -0
  69. package/dist/esm/abstracts/AbstractCondition.js.map +1 -0
  70. package/dist/esm/contracts/explain.d.ts +9 -0
  71. package/dist/esm/contracts/explain.d.ts.map +1 -0
  72. package/dist/esm/contracts/explain.js +48 -0
  73. package/dist/esm/contracts/explain.js.map +1 -0
  74. package/dist/esm/contracts/validation.d.ts +40 -0
  75. package/dist/esm/contracts/validation.d.ts.map +1 -0
  76. package/dist/esm/contracts/validation.js +255 -0
  77. package/dist/esm/contracts/validation.js.map +1 -0
  78. package/dist/esm/index.d.ts +64 -0
  79. package/dist/esm/index.d.ts.map +1 -1
  80. package/dist/esm/index.js +50 -0
  81. package/dist/esm/index.js.map +1 -1
  82. package/dist/esm/interfaces/Action.d.ts +14 -0
  83. package/dist/esm/interfaces/Action.d.ts.map +1 -1
  84. package/dist/esm/interfaces/Condition.d.ts +21 -0
  85. package/dist/esm/interfaces/Condition.d.ts.map +1 -1
  86. package/dist/esm/interfaces/Element.d.ts +16 -0
  87. package/dist/esm/interfaces/Element.d.ts.map +1 -1
  88. package/dist/esm/interfaces/HookEvents.d.ts +16 -0
  89. package/dist/esm/interfaces/HookEvents.d.ts.map +1 -1
  90. package/dist/esm/interfaces/HookEvents.js +16 -0
  91. package/dist/esm/interfaces/HookEvents.js.map +1 -1
  92. package/dist/esm/interfaces/Parameter.d.ts +16 -0
  93. package/dist/esm/interfaces/Parameter.d.ts.map +1 -1
  94. package/dist/esm/interfaces/Rule.d.ts +23 -0
  95. package/dist/esm/interfaces/Rule.d.ts.map +1 -1
  96. package/dist/esm/interfaces/Script.d.ts +10 -0
  97. package/dist/esm/interfaces/Script.d.ts.map +1 -1
  98. package/dist/esm/runtime/ActionRuntime.js +1 -1
  99. package/dist/esm/runtime/ActionRuntime.js.map +1 -1
  100. package/dist/esm/runtime/ConditionRuntime.js +3 -3
  101. package/dist/esm/runtime/ConditionRuntime.js.map +1 -1
  102. package/dist/esm/runtime/RuleRuntime.js +1 -1
  103. package/dist/esm/runtime/RuleRuntime.js.map +1 -1
  104. package/dist/esm/types/ExecutionContext.d.ts +13 -0
  105. package/dist/esm/types/ExecutionContext.d.ts.map +1 -1
  106. package/dist/esm/types/ExecutionContext.js +3 -0
  107. package/dist/esm/types/ExecutionContext.js.map +1 -1
  108. package/dist/esm/types/ExecutionResult.d.ts +14 -0
  109. package/dist/esm/types/ExecutionResult.d.ts.map +1 -1
  110. package/dist/esm/types/ExecutionResult.js +14 -0
  111. package/dist/esm/types/ExecutionResult.js.map +1 -1
  112. package/dist/esm/types/HookEmitter.d.ts +8 -1
  113. package/dist/esm/types/HookEmitter.d.ts.map +1 -1
  114. package/examples/README.md +24 -0
  115. package/examples/eligibility-check/README.md +31 -0
  116. package/examples/eligibility-check/expected-output.json +7 -0
  117. package/examples/eligibility-check/input.json +6 -0
  118. package/examples/eligibility-check/rules.json +32 -0
  119. package/examples/eligibility-check/run.ts +128 -0
  120. package/examples/pricing-rules/README.md +31 -0
  121. package/examples/pricing-rules/expected-output.json +7 -0
  122. package/examples/pricing-rules/input.json +7 -0
  123. package/examples/pricing-rules/rules.json +32 -0
  124. package/examples/pricing-rules/run.ts +136 -0
  125. package/examples/workflow-routing/README.md +31 -0
  126. package/examples/workflow-routing/expected-output.json +7 -0
  127. package/examples/workflow-routing/input.json +6 -0
  128. package/examples/workflow-routing/rules.json +33 -0
  129. package/examples/workflow-routing/run.ts +130 -0
  130. package/package.json +31 -4
  131. package/schemas/execution-context.schema.json +23 -0
  132. package/schemas/execution-output.schema.json +16 -0
  133. package/schemas/explanation-trace.schema.json +32 -0
  134. package/schemas/script.schema.json +90 -0
  135. package/schemas/validation-error.schema.json +13 -0
  136. package/src/Synapse.ts +18 -0
  137. package/src/abstracts/AbstractAction.ts +34 -0
  138. package/src/abstracts/AbstractCondition.ts +34 -0
  139. package/src/contracts/explain.ts +66 -0
  140. package/src/contracts/validation.ts +348 -0
  141. package/src/index.ts +116 -0
  142. package/src/interfaces/Action.ts +14 -0
  143. package/src/interfaces/Condition.ts +23 -0
  144. package/src/interfaces/Element.ts +18 -0
  145. package/src/interfaces/HookEvents.ts +16 -0
  146. package/src/interfaces/Parameter.ts +18 -0
  147. package/src/interfaces/Rule.ts +24 -0
  148. package/src/interfaces/Script.ts +11 -0
  149. package/src/runtime/ActionRuntime.ts +1 -1
  150. package/src/runtime/ConditionRuntime.ts +3 -3
  151. package/src/runtime/RuleRuntime.ts +1 -1
  152. package/src/types/ExecutionContext.ts +13 -0
  153. package/src/types/ExecutionResult.ts +14 -0
  154. package/src/types/HookEmitter.ts +5 -0
  155. package/dist/commonjs/Synapse.test.d.ts +0 -2
  156. package/dist/commonjs/Synapse.test.d.ts.map +0 -1
  157. package/dist/commonjs/Synapse.test.js +0 -15
  158. package/dist/commonjs/Synapse.test.js.map +0 -1
  159. package/dist/commonjs/index.test.d.ts +0 -2
  160. package/dist/commonjs/index.test.d.ts.map +0 -1
  161. package/dist/commonjs/index.test.js +0 -132
  162. package/dist/commonjs/index.test.js.map +0 -1
  163. package/dist/commonjs/runtime/ConditionRuntime.test.d.ts +0 -2
  164. package/dist/commonjs/runtime/ConditionRuntime.test.d.ts.map +0 -1
  165. package/dist/commonjs/runtime/ConditionRuntime.test.js +0 -70
  166. package/dist/commonjs/runtime/ConditionRuntime.test.js.map +0 -1
  167. package/dist/esm/Synapse.test.d.ts +0 -2
  168. package/dist/esm/Synapse.test.d.ts.map +0 -1
  169. package/dist/esm/Synapse.test.js +0 -13
  170. package/dist/esm/Synapse.test.js.map +0 -1
  171. package/dist/esm/index.test.d.ts +0 -2
  172. package/dist/esm/index.test.d.ts.map +0 -1
  173. package/dist/esm/index.test.js +0 -130
  174. package/dist/esm/index.test.js.map +0 -1
  175. package/dist/esm/runtime/ConditionRuntime.test.d.ts +0 -2
  176. package/dist/esm/runtime/ConditionRuntime.test.d.ts.map +0 -1
  177. package/dist/esm/runtime/ConditionRuntime.test.js +0 -68
  178. package/dist/esm/runtime/ConditionRuntime.test.js.map +0 -1
package/src/index.ts CHANGED
@@ -11,10 +11,17 @@ import { SimpleRule } from "./plugins/rules/SimpleRule.js";
11
11
  import type { ExecutionContext } from "./types/ExecutionContext.js";
12
12
  import type { ExecutionResult } from "./types/ExecutionResult.js";
13
13
 
14
+ /**
15
+ * Interface for all executable element instances.
16
+ */
14
17
  export interface IElementInstance {
18
+ /**
19
+ * Executes the element logic within the given context.
20
+ */
15
21
  execute(context: ExecutionContext): ExecutionResult<any>;
16
22
  }
17
23
 
24
+ /** Constructor type for Parameters. */
18
25
  export type ParameterConstructor<T = any> = new (
19
26
  id: string,
20
27
  type: string,
@@ -24,6 +31,7 @@ export type ParameterConstructor<T = any> = new (
24
31
  defaultValue?: any,
25
32
  ) => IElementInstance & { getValue(context: ExecutionContext): T | null };
26
33
 
34
+ /** Constructor type for Actions. */
27
35
  export type ActionConstructor = new (
28
36
  id: string,
29
37
  type: string,
@@ -32,6 +40,7 @@ export type ActionConstructor = new (
32
40
  neuron: Neuron,
33
41
  ) => IElementInstance;
34
42
 
43
+ /** Constructor type for Conditions. */
35
44
  export type ConditionConstructor = new (
36
45
  id: string,
37
46
  type: string,
@@ -40,6 +49,7 @@ export type ConditionConstructor = new (
40
49
  neuron: Neuron,
41
50
  ) => IElementInstance;
42
51
 
52
+ /** Constructor type for Rules. */
43
53
  export type RuleConstructor = new (
44
54
  id: string,
45
55
  type: string,
@@ -48,6 +58,10 @@ export type RuleConstructor = new (
48
58
  options: any,
49
59
  ) => IElementInstance;
50
60
 
61
+ /**
62
+ * The Neuron class serves as the central component registry for the rules engine.
63
+ * It manages the availability and instantiation of Parameters, Conditions, Actions, and Rules.
64
+ */
51
65
  export class Neuron {
52
66
  private registries = {
53
67
  parameters: new Map<string, ParameterConstructor>(),
@@ -56,6 +70,9 @@ export class Neuron {
56
70
  rules: new Map<string, RuleConstructor>(),
57
71
  };
58
72
 
73
+ /**
74
+ * Creates a new Neuron instance and registers the default built-in plugins.
75
+ */
59
76
  constructor() {
60
77
  this.registerParameter(
61
78
  SimpleNumberParameter.TYPE,
@@ -84,35 +101,134 @@ export class Neuron {
84
101
  this.registerRule(SimpleRule.TYPE, SimpleRule as any);
85
102
  }
86
103
 
104
+ /** Registers a custom parameter type. */
87
105
  registerParameter(type: string, ctor: ParameterConstructor) {
88
106
  this.registries.parameters.set(type, ctor);
89
107
  }
90
108
 
109
+ /** Retrieves a parameter constructor by type. */
91
110
  getParameter(type: string): ParameterConstructor | undefined {
92
111
  return this.registries.parameters.get(type);
93
112
  }
94
113
 
114
+ /**
115
+ * Converts serializable parameter definitions into a name-keyed Map of parameter instances.
116
+ *
117
+ * Plugin authors still receive JSON-friendly arrays at the script boundary, but base
118
+ * classes can expose a Map for ergonomic `this.params.get("name")` access.
119
+ */
120
+ createParameterMap(
121
+ params: ParameterInterface[],
122
+ ): Map<
123
+ string,
124
+ IElementInstance & { getValue(context: ExecutionContext): unknown | null }
125
+ > {
126
+ const parameterMap = new Map<
127
+ string,
128
+ IElementInstance & { getValue(context: ExecutionContext): unknown | null }
129
+ >();
130
+
131
+ for (const param of params) {
132
+ const ParamCtor = this.getParameter(param.type);
133
+ if (!ParamCtor) {
134
+ continue;
135
+ }
136
+
137
+ parameterMap.set(
138
+ param.name,
139
+ new ParamCtor(
140
+ param.id,
141
+ param.type,
142
+ param.name,
143
+ param.value,
144
+ param.options,
145
+ param.defaultValue,
146
+ ),
147
+ );
148
+ }
149
+
150
+ return parameterMap;
151
+ }
152
+
153
+ /** Registers a custom condition type. */
95
154
  registerCondition(type: string, ctor: ConditionConstructor) {
96
155
  this.registries.conditions.set(type, ctor);
97
156
  }
98
157
 
158
+ /** Retrieves a condition constructor by type. */
99
159
  getCondition(type: string): ConditionConstructor | undefined {
100
160
  return this.registries.conditions.get(type);
101
161
  }
102
162
 
163
+ /** Registers a custom action type. */
103
164
  registerAction(type: string, ctor: ActionConstructor) {
104
165
  this.registries.actions.set(type, ctor);
105
166
  }
106
167
 
168
+ /** Retrieves an action constructor by type. */
107
169
  getAction(type: string): ActionConstructor | undefined {
108
170
  return this.registries.actions.get(type);
109
171
  }
110
172
 
173
+ /** Registers a custom rule type. */
111
174
  registerRule(type: string, ctor: RuleConstructor) {
112
175
  this.registries.rules.set(type, ctor);
113
176
  }
114
177
 
178
+ /** Retrieves a rule constructor by type. */
115
179
  getRule(type: string): RuleConstructor | undefined {
116
180
  return this.registries.rules.get(type);
117
181
  }
118
182
  }
183
+
184
+ export { AbstractAction } from "./abstracts/AbstractAction.js";
185
+ export { AbstractCondition } from "./abstracts/AbstractCondition.js";
186
+ export { AbstractElement } from "./abstracts/AbstractElement.js";
187
+ export { AbstractParameter } from "./abstracts/AbstractParameter.js";
188
+ export { AbstractRule } from "./abstracts/AbstractRule.js";
189
+ export type { ExplainExecutionOptions } from "./contracts/explain.js";
190
+ export { explainExecution } from "./contracts/explain.js";
191
+ export type {
192
+ ExecutionExplanation,
193
+ ExecutionExplanationEvent,
194
+ ValidationError,
195
+ ValidationResult,
196
+ } from "./contracts/validation.js";
197
+ export {
198
+ summarizeExecutionOutput,
199
+ validateExecutionContext,
200
+ validateExecutionExplanation,
201
+ validateExecutionOutput,
202
+ validateScript,
203
+ validateValidationErrors,
204
+ } from "./contracts/validation.js";
205
+ export type { ActionInterface, ActionOptions } from "./interfaces/Action.js";
206
+ export type {
207
+ ConditionInterface,
208
+ ConditionOptions,
209
+ } from "./interfaces/Condition.js";
210
+ export type { ElementInterface } from "./interfaces/Element.js";
211
+ export { HookEvents } from "./interfaces/HookEvents.js";
212
+ export type { ParameterInterface } from "./interfaces/Parameter.js";
213
+ export type { RuleInterface, RuleOptions } from "./interfaces/Rule.js";
214
+ export type { ScriptInterface } from "./interfaces/Script.js";
215
+ export { AddTwoNumbersAction } from "./plugins/actions/AddTwoNumbersAction.js";
216
+ export { CompareTwoNumbersCondition } from "./plugins/conditions/CompareTwoNumbersCondition.js";
217
+ export type { Comparator } from "./plugins/parameters/ComparatorParameter.js";
218
+ export { ComparatorParameter } from "./plugins/parameters/ComparatorParameter.js";
219
+ export { SimpleNumberParameter } from "./plugins/parameters/SimpleNumberParameter.js";
220
+ export type {
221
+ SelectOption,
222
+ SelectOptions,
223
+ } from "./plugins/parameters/SimpleSelectParameter.js";
224
+ export { SimpleSelectParameter } from "./plugins/parameters/SimpleSelectParameter.js";
225
+ export { SimpleStringParameter } from "./plugins/parameters/SimpleStringParameter.js";
226
+ export { SimpleRule } from "./plugins/rules/SimpleRule.js";
227
+ export { Synapse } from "./Synapse.js";
228
+ export type {
229
+ ExecutionContext,
230
+ ExecutionMessage,
231
+ } from "./types/ExecutionContext.js";
232
+ export { MessageType } from "./types/ExecutionContext.js";
233
+ export { ExecutionResult } from "./types/ExecutionResult.js";
234
+ export type { HookEmitter } from "./types/HookEmitter.js";
@@ -1,11 +1,25 @@
1
1
  import type { ElementInterface } from "./Element.js";
2
2
  import type { ParameterInterface } from "./Parameter.js";
3
3
 
4
+ /**
5
+ * Configuration options for an Action.
6
+ */
4
7
  export interface ActionOptions {
8
+ /**
9
+ * Whether the action is disabled and should be skipped during execution.
10
+ */
5
11
  disabled?: boolean;
6
12
  }
7
13
 
14
+ /**
15
+ * Represents an operation to be performed when a Rule's conditions are met.
16
+ *
17
+ * @template TOptions - The type of configuration options for this action.
18
+ */
8
19
  export interface ActionInterface<TOptions extends ActionOptions = ActionOptions>
9
20
  extends ElementInterface<TOptions> {
21
+ /**
22
+ * The collection of input parameters required by this action.
23
+ */
10
24
  params: ParameterInterface[];
11
25
  }
@@ -1,14 +1,37 @@
1
1
  import type { ElementInterface } from "./Element.js";
2
2
  import type { ParameterInterface } from "./Parameter.js";
3
3
 
4
+ /**
5
+ * Configuration options for a Condition.
6
+ */
4
7
  export interface ConditionOptions {
8
+ /**
9
+ * If true, this condition is treated as part of an OR group.
10
+ * By default, conditions are evaluated with AND logic.
11
+ */
5
12
  orCondition?: boolean;
13
+
14
+ /**
15
+ * If true, the result of the condition evaluation is flipped.
16
+ */
6
17
  inverted?: boolean;
18
+
19
+ /**
20
+ * Whether the condition is disabled and should be treated as always true.
21
+ */
7
22
  disabled?: boolean;
8
23
  }
9
24
 
25
+ /**
26
+ * Represents a logical predicate used to determine if a Rule should execute its actions.
27
+ *
28
+ * @template TOptions - The type of configuration options for this condition.
29
+ */
10
30
  export interface ConditionInterface<
11
31
  TOptions extends ConditionOptions = ConditionOptions,
12
32
  > extends ElementInterface<TOptions> {
33
+ /**
34
+ * The collection of input parameters used to evaluate the condition.
35
+ */
13
36
  params: ParameterInterface[];
14
37
  }
@@ -1,5 +1,23 @@
1
+ /**
2
+ * Represents the base interface for all elements within the rules engine.
3
+ * Elements are the building blocks of scripts, pulses, actions, conditions, and parameters.
4
+ *
5
+ * @template TOptions - The type of configuration options for this element.
6
+ */
1
7
  export interface ElementInterface<TOptions = any> {
8
+ /**
9
+ * Unique identifier for the element instance.
10
+ */
2
11
  id: string;
12
+
13
+ /**
14
+ * The registered type name of the element.
15
+ * This type is used by the Registry (Neuron) to look up the correct implementation.
16
+ */
3
17
  type: string;
18
+
19
+ /**
20
+ * Configuration options specific to this element instance.
21
+ */
4
22
  options: TOptions;
5
23
  }
@@ -1,14 +1,30 @@
1
+ /**
2
+ * Enumeration of all lifecycle events emitted during script execution.
3
+ * These can be used with a HookEmitter to monitor performance, log data, or handle side effects.
4
+ */
1
5
  export enum HookEvents {
6
+ /** Emitted when the execution of a script starts. */
2
7
  ON_SCRIPT_START = "on_script_start",
8
+ /** Emitted when a specific rule begins evaluation. */
3
9
  ON_RULE_START = "on_rule_start",
10
+ /** Emitted before a condition starts evaluation. */
4
11
  ON_CONDITION_START = "pre_condition_start",
12
+ /** Emitted before an action starts execution. */
5
13
  ON_ACTION_START = "pre_action_start",
14
+ /** Emitted when a script successfully completes all rules. */
6
15
  ON_SCRIPT_END = "pre_script_end",
16
+ /** Emitted when a rule finishes evaluation (regardless of outcome). */
7
17
  ON_RULE_END = "on_rule_end",
18
+ /** Emitted after a condition evaluation completes. */
8
19
  ON_CONDITION_END = "pre_condition_end",
20
+ /** Emitted after an action execution completes. */
9
21
  ON_ACTION_END = "pre_action_end",
22
+ /** Emitted if an unhandled error occurs during script execution. */
10
23
  ON_SCRIPT_ERROR = "on_script_error",
24
+ /** Emitted if an error occurs while evaluating a specific rule. */
11
25
  ON_RULE_ERROR = "on_rule_error",
26
+ /** Emitted if an error occurs during condition evaluation. */
12
27
  ON_CONDITION_ERROR = "on_condition_error",
28
+ /** Emitted if an error occurs during action execution. */
13
29
  ON_ACTION_ERROR = "on_action_error",
14
30
  }
@@ -1,8 +1,26 @@
1
1
  import type { ElementInterface } from "./Element.js";
2
2
 
3
+ /**
4
+ * Represents a configurable input value for Actions and Conditions.
5
+ * Parameters allow for reusable logic templates by decoupling the implementation from the values.
6
+ *
7
+ * @template TValue - The primitive or complex type of the parameter's value.
8
+ * @template TOptions - The type of configuration options for this parameter.
9
+ */
3
10
  export interface ParameterInterface<TValue = any, TOptions = any>
4
11
  extends ElementInterface<TOptions> {
12
+ /**
13
+ * The semantic name of the parameter (e.g., "recipient", "threshold").
14
+ */
5
15
  name: string;
16
+
17
+ /**
18
+ * The actual value assigned to the parameter.
19
+ */
6
20
  value: TValue | null;
21
+
22
+ /**
23
+ * An optional fallback value if the primary value is missing or null.
24
+ */
7
25
  defaultValue?: TValue;
8
26
  }
@@ -2,12 +2,36 @@ import type { ActionInterface } from "./Action.js";
2
2
  import type { ConditionInterface } from "./Condition.js";
3
3
  import type { ElementInterface } from "./Element.js";
4
4
 
5
+ /**
6
+ * Configuration options for a Rule.
7
+ */
5
8
  export interface RuleOptions {
9
+ /**
10
+ * Whether the rule is disabled and should be skipped.
11
+ */
6
12
  disabled?: boolean;
7
13
  }
8
14
 
15
+ /**
16
+ * Represents a logical unit containing conditions and actions.
17
+ * A Rule executes its actions only if its conditions evaluate to true.
18
+ *
19
+ * - **Empty Conditions**: The rule is considered "Always" and will automatically execute actions.
20
+ * - **Empty Actions**: The rule will "Do Nothing" (no side effects).
21
+ *
22
+ * @template TOptions - The type of configuration options for this rule.
23
+ */
9
24
  export interface RuleInterface<TOptions extends RuleOptions = RuleOptions>
10
25
  extends ElementInterface<TOptions> {
26
+ /**
27
+ * List of conditions that must be satisfied for the actions to execute.
28
+ * If empty, actions are always executed.
29
+ */
11
30
  conditions: ConditionInterface[];
31
+
32
+ /**
33
+ * List of actions to perform if the conditions are met.
34
+ * If empty, the rule has no side effects.
35
+ */
12
36
  actions: ActionInterface[];
13
37
  }
@@ -1,6 +1,17 @@
1
1
  import type { RuleInterface } from "./Rule.js";
2
2
 
3
+ /**
4
+ * Represents a complete set of rules to be executed by the engine.
5
+ * The script is the top-level serializable container for all logic.
6
+ */
3
7
  export interface ScriptInterface {
8
+ /**
9
+ * Unique identifier for the script.
10
+ */
4
11
  id: string;
12
+
13
+ /**
14
+ * The ordered list of rules that comprise the script's logic.
15
+ */
5
16
  rules: RuleInterface[];
6
17
  }
@@ -18,7 +18,7 @@ export class ActionRuntime {
18
18
  let actionsExecuted = 0;
19
19
 
20
20
  for (const actionItem of this._actions) {
21
- if (actionItem.options.disabled) {
21
+ if (actionItem.options?.disabled) {
22
22
  continue;
23
23
  }
24
24
 
@@ -22,14 +22,14 @@ export class ConditionRuntime {
22
22
  let groupIndex = 0;
23
23
 
24
24
  for (const conditionItem of this._conditions) {
25
- if (conditionItem.options.disabled) {
25
+ if (conditionItem.options?.disabled) {
26
26
  continue;
27
27
  }
28
28
 
29
29
  this._hookEmitter?.(HookEvents.ON_CONDITION_START, context);
30
30
 
31
31
  if (
32
- conditionItem.options.orCondition &&
32
+ conditionItem.options?.orCondition &&
33
33
  orGroups[groupIndex].length > 0
34
34
  ) {
35
35
  groupIndex++;
@@ -61,7 +61,7 @@ export class ConditionRuntime {
61
61
  return new ExecutionResult(false, context, false, messageList);
62
62
  }
63
63
 
64
- const verdict = conditionItem.options.inverted
64
+ const verdict = conditionItem.options?.inverted
65
65
  ? !conditionResult.value
66
66
  : !!conditionResult.value;
67
67
 
@@ -20,7 +20,7 @@ export class RuleRuntime {
20
20
  let rulesExecuted = 0;
21
21
 
22
22
  for (const ruleItem of this._rules) {
23
- if (ruleItem.options.disabled) {
23
+ if (ruleItem.options?.disabled) {
24
24
  continue;
25
25
  }
26
26
 
@@ -1,3 +1,6 @@
1
+ /**
2
+ * Severity levels for execution messages.
3
+ */
1
4
  export enum MessageType {
2
5
  DEBUG = "debug",
3
6
  INFO = "info",
@@ -5,12 +8,22 @@ export enum MessageType {
5
8
  ERROR = "error",
6
9
  }
7
10
 
11
+ /**
12
+ * Represents a log message or audit entry generated during execution.
13
+ */
8
14
  export interface ExecutionMessage {
15
+ /** The severity level of the message. */
9
16
  type: MessageType;
17
+ /** The descriptive text of the message. */
10
18
  text: string;
11
19
  }
12
20
 
21
+ /**
22
+ * The shared state container that travels through the execution engine.
23
+ */
13
24
  export interface ExecutionContext {
25
+ /** Accumulation of all messages generated during the current script run. */
14
26
  messages: ExecutionMessage[];
27
+ /** The primary data object being processed and mutated by actions. */
15
28
  state: Record<string, any>;
16
29
  }
@@ -1,6 +1,17 @@
1
1
  import type { ExecutionContext } from "./ExecutionContext.js";
2
2
 
3
+ /**
4
+ * Represents the outcome of an execution unit (Script, Rule, Action, or Condition).
5
+ *
6
+ * @template TValue - The type of the value produced by the execution (e.g., number of rules for a Script, boolean for a Condition).
7
+ */
3
8
  export class ExecutionResult<TValue = any> {
9
+ /**
10
+ * @param success - Whether the execution completed without errors.
11
+ * @param context - The state of the ExecutionContext after the run.
12
+ * @param value - The primary output of the execution.
13
+ * @param messages - Any informational or error messages generated.
14
+ */
4
15
  constructor(
5
16
  public readonly success: boolean,
6
17
  public readonly context: ExecutionContext,
@@ -8,6 +19,9 @@ export class ExecutionResult<TValue = any> {
8
19
  public readonly messages: string[] = [],
9
20
  ) {}
10
21
 
22
+ /**
23
+ * Helper to check if the execution was successful.
24
+ */
11
25
  isSuccessful(): boolean {
12
26
  return this.success;
13
27
  }
@@ -1,7 +1,12 @@
1
1
  import type { HookEvents } from "../interfaces/HookEvents.js";
2
2
  import type { ExecutionContext } from "../types/ExecutionContext.js";
3
3
 
4
+ /**
5
+ * Functional type for lifecycle hook callbacks.
6
+ */
4
7
  export type HookEmitter = (
8
+ /** The lifecycle event being triggered. */
5
9
  event: HookEvents,
10
+ /** The current state of the execution context. */
6
11
  context: ExecutionContext,
7
12
  ) => void;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=Synapse.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Synapse.test.d.ts","sourceRoot":"","sources":["../../src/Synapse.test.ts"],"names":[],"mappings":""}
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const vitest_1 = require("vitest");
4
- const index_js_1 = require("./index.js");
5
- const Synapse_js_1 = require("./Synapse.js");
6
- (0, vitest_1.test)("Synapse can execute an empty script", () => {
7
- const neuron = new index_js_1.Neuron();
8
- const synapse = new Synapse_js_1.Synapse(neuron);
9
- const context = { messages: [], state: {} };
10
- const script = { id: "test", rules: [] };
11
- const result = synapse.execute(script, context);
12
- (0, vitest_1.expect)(result.isSuccessful()).toBe(true);
13
- (0, vitest_1.expect)(result.value).toBe(0);
14
- });
15
- //# sourceMappingURL=Synapse.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Synapse.test.js","sourceRoot":"","sources":["../../src/Synapse.test.ts"],"names":[],"mappings":";;AAAA,mCAAsC;AACtC,yCAAoC;AACpC,6CAAuC;AAEvC,IAAA,aAAI,EAAC,qCAAqC,EAAE,GAAG,EAAE;IAC/C,MAAM,MAAM,GAAG,IAAI,iBAAM,EAAE,CAAC;IAC5B,MAAM,OAAO,GAAG,IAAI,oBAAO,CAAC,MAAM,CAAC,CAAC;IACpC,MAAM,OAAO,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;IAC5C,MAAM,MAAM,GAAG,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;IAEzC,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChD,IAAA,eAAM,EAAC,MAAM,CAAC,YAAY,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzC,IAAA,eAAM,EAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC,CAAC,CAAC"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=index.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/index.test.ts"],"names":[],"mappings":""}