@plures/praxis 0.2.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 (263) hide show
  1. package/FRAMEWORK.md +420 -0
  2. package/LICENSE +21 -0
  3. package/README.md +1310 -0
  4. package/dist/adapters/cli.d.ts +43 -0
  5. package/dist/adapters/cli.d.ts.map +1 -0
  6. package/dist/adapters/cli.js +126 -0
  7. package/dist/adapters/cli.js.map +1 -0
  8. package/dist/cli/commands/auth.d.ts +26 -0
  9. package/dist/cli/commands/auth.d.ts.map +1 -0
  10. package/dist/cli/commands/auth.js +233 -0
  11. package/dist/cli/commands/auth.js.map +1 -0
  12. package/dist/cli/commands/cloud.d.ts +27 -0
  13. package/dist/cli/commands/cloud.d.ts.map +1 -0
  14. package/dist/cli/commands/cloud.js +232 -0
  15. package/dist/cli/commands/cloud.js.map +1 -0
  16. package/dist/cli/commands/generate.d.ts +25 -0
  17. package/dist/cli/commands/generate.d.ts.map +1 -0
  18. package/dist/cli/commands/generate.js +168 -0
  19. package/dist/cli/commands/generate.js.map +1 -0
  20. package/dist/cli/index.d.ts +8 -0
  21. package/dist/cli/index.d.ts.map +1 -0
  22. package/dist/cli/index.js +179 -0
  23. package/dist/cli/index.js.map +1 -0
  24. package/dist/cloud/auth.d.ts +51 -0
  25. package/dist/cloud/auth.d.ts.map +1 -0
  26. package/dist/cloud/auth.js +194 -0
  27. package/dist/cloud/auth.js.map +1 -0
  28. package/dist/cloud/billing.d.ts +184 -0
  29. package/dist/cloud/billing.d.ts.map +1 -0
  30. package/dist/cloud/billing.js +179 -0
  31. package/dist/cloud/billing.js.map +1 -0
  32. package/dist/cloud/client.d.ts +39 -0
  33. package/dist/cloud/client.d.ts.map +1 -0
  34. package/dist/cloud/client.js +176 -0
  35. package/dist/cloud/client.js.map +1 -0
  36. package/dist/cloud/index.d.ts +44 -0
  37. package/dist/cloud/index.d.ts.map +1 -0
  38. package/dist/cloud/index.js +44 -0
  39. package/dist/cloud/index.js.map +1 -0
  40. package/dist/cloud/marketplace.d.ts +166 -0
  41. package/dist/cloud/marketplace.d.ts.map +1 -0
  42. package/dist/cloud/marketplace.js +159 -0
  43. package/dist/cloud/marketplace.js.map +1 -0
  44. package/dist/cloud/provisioning.d.ts +110 -0
  45. package/dist/cloud/provisioning.d.ts.map +1 -0
  46. package/dist/cloud/provisioning.js +148 -0
  47. package/dist/cloud/provisioning.js.map +1 -0
  48. package/dist/cloud/relay/endpoints.d.ts +62 -0
  49. package/dist/cloud/relay/endpoints.d.ts.map +1 -0
  50. package/dist/cloud/relay/endpoints.js +217 -0
  51. package/dist/cloud/relay/endpoints.js.map +1 -0
  52. package/dist/cloud/relay/health/index.d.ts +5 -0
  53. package/dist/cloud/relay/health/index.d.ts.map +1 -0
  54. package/dist/cloud/relay/health/index.js +9 -0
  55. package/dist/cloud/relay/health/index.js.map +1 -0
  56. package/dist/cloud/relay/stats/index.d.ts +5 -0
  57. package/dist/cloud/relay/stats/index.d.ts.map +1 -0
  58. package/dist/cloud/relay/stats/index.js +9 -0
  59. package/dist/cloud/relay/stats/index.js.map +1 -0
  60. package/dist/cloud/relay/sync/index.d.ts +5 -0
  61. package/dist/cloud/relay/sync/index.d.ts.map +1 -0
  62. package/dist/cloud/relay/sync/index.js +9 -0
  63. package/dist/cloud/relay/sync/index.js.map +1 -0
  64. package/dist/cloud/relay/usage/index.d.ts +5 -0
  65. package/dist/cloud/relay/usage/index.d.ts.map +1 -0
  66. package/dist/cloud/relay/usage/index.js +9 -0
  67. package/dist/cloud/relay/usage/index.js.map +1 -0
  68. package/dist/cloud/sponsors.d.ts +81 -0
  69. package/dist/cloud/sponsors.d.ts.map +1 -0
  70. package/dist/cloud/sponsors.js +130 -0
  71. package/dist/cloud/sponsors.js.map +1 -0
  72. package/dist/cloud/types.d.ts +169 -0
  73. package/dist/cloud/types.d.ts.map +1 -0
  74. package/dist/cloud/types.js +7 -0
  75. package/dist/cloud/types.js.map +1 -0
  76. package/dist/components/index.d.ts +43 -0
  77. package/dist/components/index.d.ts.map +1 -0
  78. package/dist/components/index.js +17 -0
  79. package/dist/components/index.js.map +1 -0
  80. package/dist/core/actors.d.ts +95 -0
  81. package/dist/core/actors.d.ts.map +1 -0
  82. package/dist/core/actors.js +158 -0
  83. package/dist/core/actors.js.map +1 -0
  84. package/dist/core/component/generator.d.ts +122 -0
  85. package/dist/core/component/generator.d.ts.map +1 -0
  86. package/dist/core/component/generator.js +307 -0
  87. package/dist/core/component/generator.js.map +1 -0
  88. package/dist/core/engine.d.ts +92 -0
  89. package/dist/core/engine.d.ts.map +1 -0
  90. package/dist/core/engine.js +199 -0
  91. package/dist/core/engine.js.map +1 -0
  92. package/dist/core/introspection.d.ts +141 -0
  93. package/dist/core/introspection.d.ts.map +1 -0
  94. package/dist/core/introspection.js +208 -0
  95. package/dist/core/introspection.js.map +1 -0
  96. package/dist/core/logic/generator.d.ts +76 -0
  97. package/dist/core/logic/generator.d.ts.map +1 -0
  98. package/dist/core/logic/generator.js +339 -0
  99. package/dist/core/logic/generator.js.map +1 -0
  100. package/dist/core/pluresdb/generator.d.ts +58 -0
  101. package/dist/core/pluresdb/generator.d.ts.map +1 -0
  102. package/dist/core/pluresdb/generator.js +162 -0
  103. package/dist/core/pluresdb/generator.js.map +1 -0
  104. package/dist/core/protocol.d.ts +121 -0
  105. package/dist/core/protocol.d.ts.map +1 -0
  106. package/dist/core/protocol.js +46 -0
  107. package/dist/core/protocol.js.map +1 -0
  108. package/dist/core/rules.d.ts +120 -0
  109. package/dist/core/rules.d.ts.map +1 -0
  110. package/dist/core/rules.js +81 -0
  111. package/dist/core/rules.js.map +1 -0
  112. package/dist/core/schema/loader.d.ts +47 -0
  113. package/dist/core/schema/loader.d.ts.map +1 -0
  114. package/dist/core/schema/loader.js +189 -0
  115. package/dist/core/schema/loader.js.map +1 -0
  116. package/dist/core/schema/normalize.d.ts +72 -0
  117. package/dist/core/schema/normalize.d.ts.map +1 -0
  118. package/dist/core/schema/normalize.js +190 -0
  119. package/dist/core/schema/normalize.js.map +1 -0
  120. package/dist/core/schema/types.d.ts +370 -0
  121. package/dist/core/schema/types.d.ts.map +1 -0
  122. package/dist/core/schema/types.js +161 -0
  123. package/dist/core/schema/types.js.map +1 -0
  124. package/dist/dsl/index.d.ts +152 -0
  125. package/dist/dsl/index.d.ts.map +1 -0
  126. package/dist/dsl/index.js +132 -0
  127. package/dist/dsl/index.js.map +1 -0
  128. package/dist/dsl.d.ts +124 -0
  129. package/dist/dsl.d.ts.map +1 -0
  130. package/dist/dsl.js +130 -0
  131. package/dist/dsl.js.map +1 -0
  132. package/dist/examples/advanced-todo/index.d.ts +55 -0
  133. package/dist/examples/advanced-todo/index.d.ts.map +1 -0
  134. package/dist/examples/advanced-todo/index.js +222 -0
  135. package/dist/examples/advanced-todo/index.js.map +1 -0
  136. package/dist/examples/auth-basic/index.d.ts +17 -0
  137. package/dist/examples/auth-basic/index.d.ts.map +1 -0
  138. package/dist/examples/auth-basic/index.js +122 -0
  139. package/dist/examples/auth-basic/index.js.map +1 -0
  140. package/dist/examples/cart/index.d.ts +19 -0
  141. package/dist/examples/cart/index.d.ts.map +1 -0
  142. package/dist/examples/cart/index.js +202 -0
  143. package/dist/examples/cart/index.js.map +1 -0
  144. package/dist/examples/hero-ecommerce/index.d.ts +39 -0
  145. package/dist/examples/hero-ecommerce/index.d.ts.map +1 -0
  146. package/dist/examples/hero-ecommerce/index.js +506 -0
  147. package/dist/examples/hero-ecommerce/index.js.map +1 -0
  148. package/dist/examples/svelte-counter/index.d.ts +31 -0
  149. package/dist/examples/svelte-counter/index.d.ts.map +1 -0
  150. package/dist/examples/svelte-counter/index.js +123 -0
  151. package/dist/examples/svelte-counter/index.js.map +1 -0
  152. package/dist/flows.d.ts +125 -0
  153. package/dist/flows.d.ts.map +1 -0
  154. package/dist/flows.js +160 -0
  155. package/dist/flows.js.map +1 -0
  156. package/dist/index.d.ts +67 -0
  157. package/dist/index.d.ts.map +1 -0
  158. package/dist/index.js +59 -0
  159. package/dist/index.js.map +1 -0
  160. package/dist/integrations/pluresdb.d.ts +56 -0
  161. package/dist/integrations/pluresdb.d.ts.map +1 -0
  162. package/dist/integrations/pluresdb.js +46 -0
  163. package/dist/integrations/pluresdb.js.map +1 -0
  164. package/dist/integrations/svelte.d.ts +306 -0
  165. package/dist/integrations/svelte.d.ts.map +1 -0
  166. package/dist/integrations/svelte.js +447 -0
  167. package/dist/integrations/svelte.js.map +1 -0
  168. package/dist/registry.d.ts +94 -0
  169. package/dist/registry.d.ts.map +1 -0
  170. package/dist/registry.js +181 -0
  171. package/dist/registry.js.map +1 -0
  172. package/dist/runtime/terminal-adapter.d.ts +105 -0
  173. package/dist/runtime/terminal-adapter.d.ts.map +1 -0
  174. package/dist/runtime/terminal-adapter.js +113 -0
  175. package/dist/runtime/terminal-adapter.js.map +1 -0
  176. package/dist/step.d.ts +34 -0
  177. package/dist/step.d.ts.map +1 -0
  178. package/dist/step.js +111 -0
  179. package/dist/step.js.map +1 -0
  180. package/dist/types.d.ts +63 -0
  181. package/dist/types.d.ts.map +1 -0
  182. package/dist/types.js +6 -0
  183. package/dist/types.js.map +1 -0
  184. package/docs/MONETIZATION.md +394 -0
  185. package/docs/TERMINAL_NODE.md +588 -0
  186. package/docs/guides/canvas.md +389 -0
  187. package/docs/guides/getting-started.md +347 -0
  188. package/docs/guides/history-state-pattern.md +618 -0
  189. package/docs/guides/orchestration.md +617 -0
  190. package/docs/guides/parallel-state-pattern.md +767 -0
  191. package/docs/guides/svelte-integration.md +691 -0
  192. package/package.json +96 -0
  193. package/src/__tests__/actors.test.ts +270 -0
  194. package/src/__tests__/billing.test.ts +175 -0
  195. package/src/__tests__/cloud.test.ts +247 -0
  196. package/src/__tests__/dsl.test.ts +154 -0
  197. package/src/__tests__/edge-cases.test.ts +475 -0
  198. package/src/__tests__/engine.test.ts +137 -0
  199. package/src/__tests__/generators.test.ts +270 -0
  200. package/src/__tests__/introspection.test.ts +321 -0
  201. package/src/__tests__/protocol.test.ts +40 -0
  202. package/src/__tests__/provisioning.test.ts +162 -0
  203. package/src/__tests__/schema.test.ts +241 -0
  204. package/src/__tests__/svelte-integration.test.ts +431 -0
  205. package/src/__tests__/terminal-node.test.ts +352 -0
  206. package/src/adapters/cli.ts +175 -0
  207. package/src/cli/commands/auth.ts +271 -0
  208. package/src/cli/commands/cloud.ts +281 -0
  209. package/src/cli/commands/generate.ts +225 -0
  210. package/src/cli/index.ts +190 -0
  211. package/src/cloud/README.md +383 -0
  212. package/src/cloud/auth.ts +245 -0
  213. package/src/cloud/billing.ts +336 -0
  214. package/src/cloud/client.ts +221 -0
  215. package/src/cloud/index.ts +121 -0
  216. package/src/cloud/marketplace.ts +303 -0
  217. package/src/cloud/provisioning.ts +254 -0
  218. package/src/cloud/relay/endpoints.ts +307 -0
  219. package/src/cloud/relay/health/function.json +17 -0
  220. package/src/cloud/relay/health/index.ts +10 -0
  221. package/src/cloud/relay/host.json +15 -0
  222. package/src/cloud/relay/local.settings.json +8 -0
  223. package/src/cloud/relay/stats/function.json +17 -0
  224. package/src/cloud/relay/stats/index.ts +10 -0
  225. package/src/cloud/relay/sync/function.json +17 -0
  226. package/src/cloud/relay/sync/index.ts +10 -0
  227. package/src/cloud/relay/usage/function.json +17 -0
  228. package/src/cloud/relay/usage/index.ts +10 -0
  229. package/src/cloud/sponsors.ts +213 -0
  230. package/src/cloud/types.ts +198 -0
  231. package/src/components/README.md +125 -0
  232. package/src/components/TerminalNode.svelte +457 -0
  233. package/src/components/index.ts +46 -0
  234. package/src/core/actors.ts +205 -0
  235. package/src/core/component/generator.ts +432 -0
  236. package/src/core/engine.ts +243 -0
  237. package/src/core/introspection.ts +329 -0
  238. package/src/core/logic/generator.ts +420 -0
  239. package/src/core/pluresdb/generator.ts +229 -0
  240. package/src/core/protocol.ts +132 -0
  241. package/src/core/rules.ts +167 -0
  242. package/src/core/schema/loader.ts +247 -0
  243. package/src/core/schema/normalize.ts +322 -0
  244. package/src/core/schema/types.ts +557 -0
  245. package/src/dsl/index.ts +218 -0
  246. package/src/dsl.ts +214 -0
  247. package/src/examples/advanced-todo/App.svelte +506 -0
  248. package/src/examples/advanced-todo/README.md +371 -0
  249. package/src/examples/advanced-todo/index.ts +309 -0
  250. package/src/examples/auth-basic/index.ts +163 -0
  251. package/src/examples/cart/index.ts +259 -0
  252. package/src/examples/hero-ecommerce/index.ts +657 -0
  253. package/src/examples/svelte-counter/index.ts +168 -0
  254. package/src/flows.ts +268 -0
  255. package/src/index.ts +154 -0
  256. package/src/integrations/pluresdb.ts +93 -0
  257. package/src/integrations/svelte.ts +617 -0
  258. package/src/registry.ts +223 -0
  259. package/src/runtime/terminal-adapter.ts +175 -0
  260. package/src/step.ts +151 -0
  261. package/src/types.ts +70 -0
  262. package/templates/basic-app/README.md +147 -0
  263. package/templates/fullstack-app/README.md +279 -0
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Registry for managing rules and constraints in Praxis.
3
+ * Provides registration, retrieval, and execution capabilities.
4
+ */
5
+
6
+ import type { PraxisState, PraxisEvent, Effect } from './types.js';
7
+ import type { Rule, Constraint, ConstraintViolation } from './dsl.js';
8
+
9
+ /**
10
+ * Registry for managing rules and constraints.
11
+ */
12
+ export class Registry<
13
+ S extends PraxisState = PraxisState,
14
+ E extends PraxisEvent = PraxisEvent
15
+ > {
16
+ private rules: Map<string, Rule<S, E>> = new Map();
17
+ private constraints: Map<string, Constraint<S>> = new Map();
18
+
19
+ /**
20
+ * Register a new rule.
21
+ * @throws Error if a rule with the same ID already exists
22
+ */
23
+ registerRule(rule: Rule<S, E>): void {
24
+ if (this.rules.has(rule.id)) {
25
+ throw new Error(`Rule with id '${rule.id}' already exists`);
26
+ }
27
+ this.rules.set(rule.id, rule);
28
+ }
29
+
30
+ /**
31
+ * Register multiple rules at once.
32
+ */
33
+ registerRules(rules: Rule<S, E>[]): void {
34
+ for (const rule of rules) {
35
+ this.registerRule(rule);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Unregister a rule by ID.
41
+ */
42
+ unregisterRule(id: string): boolean {
43
+ return this.rules.delete(id);
44
+ }
45
+
46
+ /**
47
+ * Get a rule by ID.
48
+ */
49
+ getRule(id: string): Rule<S, E> | undefined {
50
+ return this.rules.get(id);
51
+ }
52
+
53
+ /**
54
+ * Get all registered rules.
55
+ */
56
+ getAllRules(): Rule<S, E>[] {
57
+ return Array.from(this.rules.values());
58
+ }
59
+
60
+ /**
61
+ * Get rules that match a specific event type.
62
+ */
63
+ getRulesForEvent(eventType: string): Rule<S, E>[] {
64
+ return Array.from(this.rules.values()).filter(
65
+ (rule) => !rule.eventType || rule.eventType === eventType
66
+ );
67
+ }
68
+
69
+ /**
70
+ * Register a new constraint.
71
+ * @throws Error if a constraint with the same ID already exists
72
+ */
73
+ registerConstraint(constraint: Constraint<S>): void {
74
+ if (this.constraints.has(constraint.id)) {
75
+ throw new Error(`Constraint with id '${constraint.id}' already exists`);
76
+ }
77
+ this.constraints.set(constraint.id, constraint);
78
+ }
79
+
80
+ /**
81
+ * Register multiple constraints at once.
82
+ */
83
+ registerConstraints(constraints: Constraint<S>[]): void {
84
+ for (const constraint of constraints) {
85
+ this.registerConstraint(constraint);
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Unregister a constraint by ID.
91
+ */
92
+ unregisterConstraint(id: string): boolean {
93
+ return this.constraints.delete(id);
94
+ }
95
+
96
+ /**
97
+ * Get a constraint by ID.
98
+ */
99
+ getConstraint(id: string): Constraint<S> | undefined {
100
+ return this.constraints.get(id);
101
+ }
102
+
103
+ /**
104
+ * Get all registered constraints.
105
+ */
106
+ getAllConstraints(): Constraint<S>[] {
107
+ return Array.from(this.constraints.values());
108
+ }
109
+
110
+ /**
111
+ * Evaluate all rules for a given state and event.
112
+ * Returns all effects produced by rules that fire.
113
+ */
114
+ evaluateRules(state: S, event: E): Effect[] {
115
+ const effects: Effect[] = [];
116
+
117
+ // Get rules that match the event type
118
+ const applicableRules = this.getRulesForEvent(event.type);
119
+
120
+ // Sort by priority (higher priority first)
121
+ const sortedRules = applicableRules.sort(
122
+ (a, b) => (b.priority ?? 0) - (a.priority ?? 0)
123
+ );
124
+
125
+ // Evaluate each rule
126
+ for (const rule of sortedRules) {
127
+ try {
128
+ if (rule.when(state, event)) {
129
+ const ruleEffects = rule.then(state, event);
130
+ effects.push(...ruleEffects);
131
+ }
132
+ } catch (error) {
133
+ console.error(`Error evaluating rule '${rule.id}':`, error);
134
+ }
135
+ }
136
+
137
+ return effects;
138
+ }
139
+
140
+ /**
141
+ * Check all constraints for a given state.
142
+ * Returns an array of violations (empty if all constraints pass).
143
+ */
144
+ checkConstraints(state: S): ConstraintViolation[] {
145
+ const violations: ConstraintViolation[] = [];
146
+
147
+ for (const constraint of this.constraints.values()) {
148
+ try {
149
+ if (!constraint.check(state)) {
150
+ violations.push({
151
+ constraintId: constraint.id,
152
+ message:
153
+ constraint.errorMessage ||
154
+ `Constraint '${constraint.id}' violated`,
155
+ state,
156
+ });
157
+ }
158
+ } catch (error) {
159
+ violations.push({
160
+ constraintId: constraint.id,
161
+ message: `Error checking constraint '${constraint.id}': ${error}`,
162
+ state,
163
+ });
164
+ }
165
+ }
166
+
167
+ return violations;
168
+ }
169
+
170
+ /**
171
+ * Clear all rules from the registry.
172
+ */
173
+ clearRules(): void {
174
+ this.rules.clear();
175
+ }
176
+
177
+ /**
178
+ * Clear all constraints from the registry.
179
+ */
180
+ clearConstraints(): void {
181
+ this.constraints.clear();
182
+ }
183
+
184
+ /**
185
+ * Clear all rules and constraints from the registry.
186
+ */
187
+ clear(): void {
188
+ this.clearRules();
189
+ this.clearConstraints();
190
+ }
191
+
192
+ /**
193
+ * Get statistics about the registry.
194
+ */
195
+ getStats(): {
196
+ ruleCount: number;
197
+ constraintCount: number;
198
+ rulesByEventType: Record<string, number>;
199
+ } {
200
+ const rulesByEventType: Record<string, number> = {};
201
+
202
+ for (const rule of this.rules.values()) {
203
+ const eventType = rule.eventType || '*';
204
+ rulesByEventType[eventType] = (rulesByEventType[eventType] || 0) + 1;
205
+ }
206
+
207
+ return {
208
+ ruleCount: this.rules.size,
209
+ constraintCount: this.constraints.size,
210
+ rulesByEventType,
211
+ };
212
+ }
213
+ }
214
+
215
+ /**
216
+ * Create a new registry instance.
217
+ */
218
+ export function createRegistry<
219
+ S extends PraxisState = PraxisState,
220
+ E extends PraxisEvent = PraxisEvent
221
+ >(): Registry<S, E> {
222
+ return new Registry<S, E>();
223
+ }
@@ -0,0 +1,175 @@
1
+ /**
2
+ * Terminal Node Runtime Adapter
3
+ *
4
+ * Handles terminal command execution and state management.
5
+ * Integrates with pluresDB for state synchronization.
6
+ */
7
+
8
+ import type { TerminalNodeProps } from '../core/schema/types.js';
9
+
10
+ /**
11
+ * Terminal command execution result
12
+ */
13
+ export interface TerminalExecutionResult {
14
+ /** Command that was executed */
15
+ command: string;
16
+ /** Output from the command */
17
+ output: string;
18
+ /** Exit code (0 for success) */
19
+ exitCode: number;
20
+ /** Execution timestamp */
21
+ timestamp: number;
22
+ /** Error message if execution failed */
23
+ error?: string;
24
+ }
25
+
26
+ /**
27
+ * Terminal node state
28
+ */
29
+ export interface TerminalNodeState extends TerminalNodeProps {
30
+ /** Node identifier */
31
+ nodeId: string;
32
+ /** Current working directory */
33
+ cwd?: string;
34
+ /** Environment variables */
35
+ env?: Record<string, string>;
36
+ }
37
+
38
+ /**
39
+ * Terminal adapter options
40
+ */
41
+ export interface TerminalAdapterOptions {
42
+ /** Node identifier */
43
+ nodeId: string;
44
+ /** Initial props */
45
+ props?: Partial<TerminalNodeProps>;
46
+ /** PluresDB path for input binding */
47
+ inputPath?: string;
48
+ /** PluresDB path for output binding */
49
+ outputPath?: string;
50
+ }
51
+
52
+ /**
53
+ * Terminal Runtime Adapter
54
+ *
55
+ * Manages terminal node execution and state.
56
+ *
57
+ * Note: PluresDB input/output path bindings will be implemented
58
+ * when pluresDB integration is complete.
59
+ */
60
+ export class TerminalAdapter {
61
+ private state: TerminalNodeState;
62
+ private outputPath?: string;
63
+
64
+ constructor(options: TerminalAdapterOptions) {
65
+ this.state = {
66
+ nodeId: options.nodeId,
67
+ inputMode: options.props?.inputMode || 'text',
68
+ history: options.props?.history || [],
69
+ lastOutput: options.props?.lastOutput || null,
70
+ };
71
+ // TODO: Store inputPath when pluresDB integration is complete
72
+ // const inputPath = options.inputPath;
73
+ this.outputPath = options.outputPath;
74
+ }
75
+
76
+ /**
77
+ * Execute a terminal command
78
+ *
79
+ * @param command - Command to execute
80
+ * @returns Execution result
81
+ */
82
+ async executeCommand(command: string): Promise<TerminalExecutionResult> {
83
+ // Add to history
84
+ this.state.history.push(command);
85
+
86
+ // TODO: Integrate with RuneBook execution model
87
+ // For now, return a stubbed response
88
+ const result: TerminalExecutionResult = {
89
+ command,
90
+ output: `[Stub] Command received: ${command}\nIntegration with RuneBook pending.`,
91
+ exitCode: 0,
92
+ timestamp: Date.now(),
93
+ };
94
+
95
+ // Update last output
96
+ this.state.lastOutput = result.output;
97
+
98
+ // TODO: Sync to pluresDB output path when integration is available
99
+ if (this.outputPath) {
100
+ await this.syncToPluresDB(this.outputPath, result);
101
+ }
102
+
103
+ return result;
104
+ }
105
+
106
+ /**
107
+ * Get current terminal state
108
+ */
109
+ getState(): Readonly<TerminalNodeState> {
110
+ return { ...this.state };
111
+ }
112
+
113
+ /**
114
+ * Update terminal props
115
+ */
116
+ updateProps(props: Partial<TerminalNodeProps>): void {
117
+ this.state = {
118
+ ...this.state,
119
+ ...props,
120
+ };
121
+ }
122
+
123
+ /**
124
+ * Clear command history
125
+ */
126
+ clearHistory(): void {
127
+ this.state.history = [];
128
+ }
129
+
130
+ /**
131
+ * Get command history
132
+ */
133
+ getHistory(): ReadonlyArray<string> {
134
+ return [...this.state.history];
135
+ }
136
+
137
+ /**
138
+ * Sync state to pluresDB (placeholder)
139
+ *
140
+ * @param _path - PluresDB path (unused until integration is complete)
141
+ * @param _data - Data to sync (unused until integration is complete)
142
+ */
143
+ private async syncToPluresDB(_path: string, _data: unknown): Promise<void> {
144
+ // TODO: Implement pluresDB sync when integration is available
145
+ // When implemented, this will sync terminal output to the specified pluresDB path
146
+ // for reactive state management across the application
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Create a terminal adapter instance
152
+ *
153
+ * @param options - Terminal adapter options
154
+ * @returns Terminal adapter instance
155
+ */
156
+ export function createTerminalAdapter(
157
+ options: TerminalAdapterOptions
158
+ ): TerminalAdapter {
159
+ return new TerminalAdapter(options);
160
+ }
161
+
162
+ /**
163
+ * Run a terminal command (convenience function)
164
+ *
165
+ * @param nodeId - Terminal node identifier
166
+ * @param command - Command to execute
167
+ * @returns Execution result
168
+ */
169
+ export async function runTerminalCommand(
170
+ nodeId: string,
171
+ command: string
172
+ ): Promise<TerminalExecutionResult> {
173
+ const adapter = createTerminalAdapter({ nodeId });
174
+ return adapter.executeCommand(command);
175
+ }
package/src/step.ts ADDED
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Core step function implementation for Praxis.
3
+ * Pure function that transitions from one state to another given an event.
4
+ */
5
+
6
+ import type { PraxisState, PraxisEvent, StepResult, StepFunction } from './types.js';
7
+ import type { Registry } from './registry.js';
8
+
9
+ /**
10
+ * Options for creating a step function.
11
+ */
12
+ export interface StepOptions<
13
+ S extends PraxisState = PraxisState,
14
+ E extends PraxisEvent = PraxisEvent
15
+ > {
16
+ /** Registry containing rules and constraints */
17
+ registry?: Registry<S, E>;
18
+ /** Whether to check constraints before and after state transitions */
19
+ checkConstraints?: boolean;
20
+ /** Custom reducer function for state transitions */
21
+ reducer?: (state: S, event: E) => S;
22
+ }
23
+
24
+ /**
25
+ * Create a step function that integrates with the registry.
26
+ * This is the main entry point for creating state transition logic.
27
+ */
28
+ export function createStepFunction<
29
+ S extends PraxisState = PraxisState,
30
+ E extends PraxisEvent = PraxisEvent
31
+ >(options: StepOptions<S, E> = {}): StepFunction<S, E> {
32
+ const { registry, checkConstraints = true, reducer } = options;
33
+
34
+ return (state: S, event: E): StepResult<S> => {
35
+ const errors: string[] = [];
36
+
37
+ // Check constraints before transition
38
+ if (checkConstraints && registry) {
39
+ const violations = registry.checkConstraints(state);
40
+ if (violations.length > 0) {
41
+ errors.push(
42
+ ...violations.map((v) => `Pre-condition: ${v.message}`)
43
+ );
44
+ // Return current state with errors
45
+ return { state, errors };
46
+ }
47
+ }
48
+
49
+ // Apply the reducer to get the new state
50
+ let newState: S;
51
+ if (reducer) {
52
+ try {
53
+ newState = reducer(state, event);
54
+ } catch (error) {
55
+ errors.push(`Reducer error: ${error}`);
56
+ return { state, errors };
57
+ }
58
+ } else {
59
+ // Default behavior: merge event data into facts
60
+ newState = {
61
+ ...state,
62
+ facts: {
63
+ ...state.facts,
64
+ ...(event.data || {}),
65
+ },
66
+ metadata: {
67
+ ...state.metadata,
68
+ lastUpdated: event.timestamp,
69
+ version: (state.metadata?.version || 0) + 1,
70
+ },
71
+ } as S;
72
+ }
73
+
74
+ // Evaluate rules to get effects
75
+ const effects = registry ? registry.evaluateRules(newState, event) : [];
76
+
77
+ // Check constraints after transition
78
+ if (checkConstraints && registry) {
79
+ const violations = registry.checkConstraints(newState);
80
+ if (violations.length > 0) {
81
+ errors.push(
82
+ ...violations.map((v) => `Post-condition: ${v.message}`)
83
+ );
84
+ // Return previous state with errors to maintain invariants
85
+ return { state, errors };
86
+ }
87
+ }
88
+
89
+ return {
90
+ state: newState,
91
+ effects,
92
+ errors: errors.length > 0 ? errors : undefined,
93
+ };
94
+ };
95
+ }
96
+
97
+ /**
98
+ * Simple step function that just applies a reducer.
99
+ * Useful for basic state transitions without rules or constraints.
100
+ */
101
+ export function step<
102
+ S extends PraxisState = PraxisState,
103
+ E extends PraxisEvent = PraxisEvent
104
+ >(reducer: (state: S, event: E) => S): StepFunction<S, E> {
105
+ return (state: S, event: E): StepResult<S> => {
106
+ try {
107
+ const newState = reducer(state, event);
108
+ return { state: newState };
109
+ } catch (error) {
110
+ return {
111
+ state,
112
+ errors: [`Step error: ${error}`],
113
+ };
114
+ }
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Compose multiple step functions into a single step function.
120
+ * Each step function is applied in sequence, with the output of one
121
+ * becoming the input to the next.
122
+ */
123
+ export function compose<
124
+ S extends PraxisState = PraxisState,
125
+ E extends PraxisEvent = PraxisEvent
126
+ >(...steps: StepFunction<S, E>[]): StepFunction<S, E> {
127
+ return (state: S, event: E): StepResult<S> => {
128
+ let currentState = state;
129
+ const allEffects: StepResult<S>['effects'] = [];
130
+ const allErrors: string[] = [];
131
+
132
+ for (const stepFn of steps) {
133
+ const result = stepFn(currentState, event);
134
+ currentState = result.state;
135
+
136
+ if (result.effects) {
137
+ allEffects.push(...result.effects);
138
+ }
139
+
140
+ if (result.errors) {
141
+ allErrors.push(...result.errors);
142
+ }
143
+ }
144
+
145
+ return {
146
+ state: currentState,
147
+ effects: allEffects.length > 0 ? allEffects : undefined,
148
+ errors: allErrors.length > 0 ? allErrors : undefined,
149
+ };
150
+ };
151
+ }
package/src/types.ts ADDED
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Core types for Praxis - A TypeScript library for typed, functional application logic.
3
+ * All types are designed to be JSON-friendly for serialization and interoperability.
4
+ */
5
+
6
+ /**
7
+ * Base type for all Praxis events.
8
+ * Events represent things that have happened and trigger state transitions.
9
+ */
10
+ export interface PraxisEvent {
11
+ /** Unique identifier for the event type */
12
+ type: string;
13
+ /** Timestamp when the event occurred */
14
+ timestamp: number;
15
+ /** Additional event data (must be JSON-serializable) */
16
+ data?: Record<string, unknown>;
17
+ /** Optional metadata for tracing and debugging */
18
+ metadata?: {
19
+ correlationId?: string;
20
+ source?: string;
21
+ [key: string]: unknown;
22
+ };
23
+ }
24
+
25
+ /**
26
+ * Base type for all Praxis state.
27
+ * State represents the current facts and context of the application.
28
+ */
29
+ export interface PraxisState {
30
+ /** Current facts - immutable truths about the system */
31
+ facts: Record<string, unknown>;
32
+ /** Optional metadata for debugging and inspection */
33
+ metadata?: {
34
+ version?: number;
35
+ lastUpdated?: number;
36
+ [key: string]: unknown;
37
+ };
38
+ }
39
+
40
+ /**
41
+ * Result of a state transition step.
42
+ * Contains the new state and any side effects to be executed.
43
+ */
44
+ export interface StepResult<S extends PraxisState = PraxisState> {
45
+ /** The new state after applying the event */
46
+ state: S;
47
+ /** Side effects to be executed (e.g., commands, events to emit) */
48
+ effects?: Effect[];
49
+ /** Optional errors or warnings that occurred during the step */
50
+ errors?: string[];
51
+ }
52
+
53
+ /**
54
+ * Represents a side effect to be executed outside the pure step function.
55
+ */
56
+ export interface Effect {
57
+ /** Type of the effect */
58
+ type: string;
59
+ /** Effect payload (must be JSON-serializable) */
60
+ payload?: Record<string, unknown>;
61
+ }
62
+
63
+ /**
64
+ * A pure function that transitions from one state to another given an event.
65
+ * This is the core abstraction of Praxis.
66
+ */
67
+ export type StepFunction<
68
+ S extends PraxisState = PraxisState,
69
+ E extends PraxisEvent = PraxisEvent
70
+ > = (state: S, event: E) => StepResult<S>;