@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,420 @@
1
+ /**
2
+ * Praxis Logic Generator
3
+ *
4
+ * Generates logic module code from schema definitions.
5
+ */
6
+
7
+ import type {
8
+ FactDefinition,
9
+ EventDefinition,
10
+ RuleDefinition,
11
+ } from '../schema/types.js';
12
+ import type { NormalizedSchema } from '../schema/normalize.js';
13
+
14
+ /**
15
+ * Logic generation options
16
+ */
17
+ export interface LogicGeneratorOptions {
18
+ /** Output directory */
19
+ outputDir: string;
20
+ /** Generate TypeScript */
21
+ typescript?: boolean;
22
+ /** Include documentation comments */
23
+ includeDocs?: boolean;
24
+ }
25
+
26
+ /**
27
+ * Generated logic file
28
+ */
29
+ export interface GeneratedLogicFile {
30
+ /** File path */
31
+ path: string;
32
+ /** File content */
33
+ content: string;
34
+ /** File type */
35
+ type: 'facts' | 'events' | 'rules' | 'engine' | 'index';
36
+ }
37
+
38
+ /**
39
+ * Logic generator class
40
+ */
41
+ export class LogicGenerator {
42
+ private options: LogicGeneratorOptions;
43
+
44
+ constructor(options: LogicGeneratorOptions) {
45
+ this.options = {
46
+ typescript: true,
47
+ includeDocs: true,
48
+ ...options,
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Generate all logic files from schema
54
+ */
55
+ generateLogic(schema: NormalizedSchema): GeneratedLogicFile[] {
56
+ const files: GeneratedLogicFile[] = [];
57
+
58
+ // Collect all logic definitions
59
+ const allLogic = schema.logic || [];
60
+
61
+ if (allLogic.length === 0) {
62
+ // Generate minimal structure even without logic definitions
63
+ files.push(this.generateFactsFile([]));
64
+ files.push(this.generateEventsFile([]));
65
+ files.push(this.generateRulesFile([]));
66
+ files.push(this.generateEngineFile(schema));
67
+ files.push(this.generateIndexFile());
68
+ return files;
69
+ }
70
+
71
+ // Collect all facts, events, and rules from logic definitions
72
+ const allFacts = allLogic.flatMap((logic) => logic.facts || []);
73
+ const allEvents = allLogic.flatMap((logic) => logic.events || []);
74
+ const allRules = allLogic.flatMap((logic) => logic.rules || []);
75
+
76
+ // Generate individual files
77
+ files.push(this.generateFactsFile(allFacts));
78
+ files.push(this.generateEventsFile(allEvents));
79
+ files.push(this.generateRulesFile(allRules));
80
+ files.push(this.generateEngineFile(schema));
81
+ files.push(this.generateIndexFile());
82
+
83
+ return files;
84
+ }
85
+
86
+ /**
87
+ * Generate facts.ts file
88
+ */
89
+ private generateFactsFile(facts: FactDefinition[]): GeneratedLogicFile {
90
+ const ext = this.options.typescript ? 'ts' : 'js';
91
+ const lines: string[] = [];
92
+
93
+ lines.push("import { defineFact } from '@plures/praxis';");
94
+ lines.push('');
95
+
96
+ if (this.options.includeDocs) {
97
+ lines.push('/**');
98
+ lines.push(' * Fact definitions');
99
+ lines.push(' */');
100
+ lines.push('');
101
+ }
102
+
103
+ if (facts.length === 0) {
104
+ lines.push('// No facts defined in schema');
105
+ lines.push('// Example:');
106
+ lines.push('// export const UserCreated = defineFact<"UserCreated", { userId: string }>("UserCreated");');
107
+ } else {
108
+ for (const fact of facts) {
109
+ if (this.options.includeDocs && fact.description) {
110
+ lines.push(`/** ${fact.description} */`);
111
+ }
112
+
113
+ const payloadType = this.generatePayloadType(fact.payload);
114
+ lines.push(
115
+ `export const ${fact.tag} = defineFact<"${fact.tag}", ${payloadType}>("${fact.tag}");`
116
+ );
117
+ lines.push('');
118
+ }
119
+ }
120
+
121
+ return {
122
+ path: `${this.options.outputDir}/facts.${ext}`,
123
+ content: lines.join('\n'),
124
+ type: 'facts',
125
+ };
126
+ }
127
+
128
+ /**
129
+ * Generate events.ts file
130
+ */
131
+ private generateEventsFile(events: EventDefinition[]): GeneratedLogicFile {
132
+ const ext = this.options.typescript ? 'ts' : 'js';
133
+ const lines: string[] = [];
134
+
135
+ lines.push("import { defineEvent } from '@plures/praxis';");
136
+ lines.push('');
137
+
138
+ if (this.options.includeDocs) {
139
+ lines.push('/**');
140
+ lines.push(' * Event definitions');
141
+ lines.push(' */');
142
+ lines.push('');
143
+ }
144
+
145
+ if (events.length === 0) {
146
+ lines.push('// No events defined in schema');
147
+ lines.push('// Example:');
148
+ lines.push('// export const CreateUser = defineEvent<"CREATE_USER", { name: string; email: string }>("CREATE_USER");');
149
+ } else {
150
+ for (const event of events) {
151
+ if (this.options.includeDocs && event.description) {
152
+ lines.push(`/** ${event.description} */`);
153
+ }
154
+
155
+ const payloadType = this.generatePayloadType(event.payload);
156
+ lines.push(
157
+ `export const ${event.tag} = defineEvent<"${event.tag}", ${payloadType}>("${event.tag}");`
158
+ );
159
+ lines.push('');
160
+ }
161
+ }
162
+
163
+ return {
164
+ path: `${this.options.outputDir}/events.${ext}`,
165
+ content: lines.join('\n'),
166
+ type: 'events',
167
+ };
168
+ }
169
+
170
+ /**
171
+ * Generate rules.ts file
172
+ */
173
+ private generateRulesFile(rules: RuleDefinition[]): GeneratedLogicFile {
174
+ const ext = this.options.typescript ? 'ts' : 'js';
175
+ const lines: string[] = [];
176
+
177
+ lines.push("import { defineRule } from '@plures/praxis';");
178
+ lines.push("import * as Facts from './facts.js';");
179
+ lines.push("import * as Events from './events.js';");
180
+ lines.push('');
181
+
182
+ if (this.options.includeDocs) {
183
+ lines.push('/**');
184
+ lines.push(' * Rule definitions');
185
+ lines.push(' */');
186
+ lines.push('');
187
+ }
188
+
189
+ if (rules.length === 0) {
190
+ lines.push('// No rules defined in schema');
191
+ lines.push('// Example:');
192
+ lines.push('// export const createUserRule = defineRule({');
193
+ lines.push("// id: 'user.create',");
194
+ lines.push("// description: 'Create user on CREATE_USER event',");
195
+ lines.push('// impl: (state, events) => {');
196
+ lines.push('// const event = events.find(Events.CreateUser.is);');
197
+ lines.push('// if (event) {');
198
+ lines.push('// return [Facts.UserCreated.create({ userId: "new-id" })];');
199
+ lines.push('// }');
200
+ lines.push('// return [];');
201
+ lines.push('// },');
202
+ lines.push('// });');
203
+ lines.push('');
204
+ lines.push('export const rules = [];');
205
+ } else {
206
+ for (const rule of rules) {
207
+ if (this.options.includeDocs && rule.description) {
208
+ lines.push(`/** ${rule.description} */`);
209
+ }
210
+
211
+ lines.push(`export const ${this.sanitizeIdentifier(rule.id)}Rule = defineRule({`);
212
+ lines.push(` id: '${rule.id}',`);
213
+ lines.push(` description: '${rule.description}',`);
214
+
215
+ if (rule.priority !== undefined) {
216
+ lines.push(` priority: ${rule.priority},`);
217
+ }
218
+
219
+ lines.push(' impl: (state, events) => {');
220
+ lines.push(` // TODO: Implement rule logic for "${rule.id}"`);
221
+ lines.push(' // Condition: ' + (rule.when || 'always'));
222
+ lines.push(' // Action: ' + rule.then);
223
+ lines.push(' return [];');
224
+ lines.push(' },');
225
+ lines.push('});');
226
+ lines.push('');
227
+ }
228
+
229
+ // Export all rules as array
230
+ lines.push('export const rules = [');
231
+ for (const rule of rules) {
232
+ lines.push(` ${this.sanitizeIdentifier(rule.id)}Rule,`);
233
+ }
234
+ lines.push('];');
235
+ }
236
+
237
+ return {
238
+ path: `${this.options.outputDir}/rules.${ext}`,
239
+ content: lines.join('\n'),
240
+ type: 'rules',
241
+ };
242
+ }
243
+
244
+ /**
245
+ * Generate engine.ts file
246
+ */
247
+ private generateEngineFile(schema: NormalizedSchema): GeneratedLogicFile {
248
+ const ext = this.options.typescript ? 'ts' : 'js';
249
+ const lines: string[] = [];
250
+
251
+ lines.push("import { createPraxisEngine, PraxisRegistry } from '@plures/praxis';");
252
+ lines.push("import { rules } from './rules.js';");
253
+ lines.push('');
254
+
255
+ if (this.options.includeDocs) {
256
+ lines.push('/**');
257
+ lines.push(` * ${schema.name} Logic Engine`);
258
+ if (schema.description) {
259
+ lines.push(` * ${schema.description}`);
260
+ }
261
+ lines.push(' */');
262
+ lines.push('');
263
+ }
264
+
265
+ // Generate context type from models
266
+ if (this.options.typescript) {
267
+ lines.push('/**');
268
+ lines.push(' * Application context type');
269
+ lines.push(' */');
270
+ lines.push('export interface AppContext {');
271
+
272
+ if (schema.models && schema.models.length > 0) {
273
+ for (const model of schema.models) {
274
+ lines.push(` ${model.name.toLowerCase()}s: ${model.name}[];`);
275
+ }
276
+ } else {
277
+ lines.push(' // Add your context properties here');
278
+ }
279
+
280
+ lines.push('}');
281
+ lines.push('');
282
+
283
+ // Generate model types
284
+ if (schema.models && schema.models.length > 0) {
285
+ for (const model of schema.models) {
286
+ lines.push(`export interface ${model.name} {`);
287
+ for (const field of model.fields) {
288
+ const optional = field.optional ? '?' : '';
289
+ const type = this.mapFieldType(field.type);
290
+ lines.push(` ${field.name}${optional}: ${type};`);
291
+ }
292
+ lines.push('}');
293
+ lines.push('');
294
+ }
295
+ }
296
+ }
297
+
298
+ // Create registry and engine
299
+ lines.push('/**');
300
+ lines.push(' * Create the logic engine');
301
+ lines.push(' */');
302
+ lines.push('export function createEngine() {');
303
+ lines.push(' const registry = new PraxisRegistry<AppContext>();');
304
+ lines.push('');
305
+ lines.push(' // Register all rules');
306
+ lines.push(' for (const rule of rules) {');
307
+ lines.push(' registry.registerRule(rule);');
308
+ lines.push(' }');
309
+ lines.push('');
310
+ lines.push(' // Create engine with initial context');
311
+ lines.push(' const initialContext: AppContext = {');
312
+
313
+ if (schema.models && schema.models.length > 0) {
314
+ for (const model of schema.models) {
315
+ lines.push(` ${model.name.toLowerCase()}s: [],`);
316
+ }
317
+ } else {
318
+ lines.push(' // Initialize your context here');
319
+ }
320
+
321
+ lines.push(' };');
322
+ lines.push('');
323
+ lines.push(' return createPraxisEngine({');
324
+ lines.push(' initialContext,');
325
+ lines.push(' registry,');
326
+ lines.push(' });');
327
+ lines.push('}');
328
+
329
+ return {
330
+ path: `${this.options.outputDir}/engine.${ext}`,
331
+ content: lines.join('\n'),
332
+ type: 'engine',
333
+ };
334
+ }
335
+
336
+ /**
337
+ * Generate index.ts file
338
+ */
339
+ private generateIndexFile(): GeneratedLogicFile {
340
+ const ext = this.options.typescript ? 'ts' : 'js';
341
+ const lines: string[] = [];
342
+
343
+ lines.push("export * from './facts.js';");
344
+ lines.push("export * from './events.js';");
345
+ lines.push("export * from './rules.js';");
346
+ lines.push("export * from './engine.js';");
347
+
348
+ return {
349
+ path: `${this.options.outputDir}/index.${ext}`,
350
+ content: lines.join('\n'),
351
+ type: 'index',
352
+ };
353
+ }
354
+
355
+ /**
356
+ * Generate TypeScript type from payload definition
357
+ */
358
+ private generatePayloadType(payload: Record<string, string>): string {
359
+ const fields = Object.entries(payload)
360
+ .map(([key, type]) => `${key}: ${type}`)
361
+ .join('; ');
362
+ return `{ ${fields} }`;
363
+ }
364
+
365
+ /**
366
+ * Map field type to TypeScript type
367
+ */
368
+ private mapFieldType(type: any): string {
369
+ if (typeof type === 'string') {
370
+ switch (type) {
371
+ case 'string':
372
+ return 'string';
373
+ case 'number':
374
+ return 'number';
375
+ case 'boolean':
376
+ return 'boolean';
377
+ case 'date':
378
+ return 'Date';
379
+ case 'array':
380
+ return 'unknown[]';
381
+ case 'object':
382
+ return 'Record<string, unknown>';
383
+ default:
384
+ return 'unknown';
385
+ }
386
+ }
387
+
388
+ if (typeof type === 'object') {
389
+ if ('array' in type) {
390
+ const innerType = this.mapFieldType(type.array);
391
+ return `${innerType}[]`;
392
+ }
393
+ if ('reference' in type) {
394
+ return type.reference;
395
+ }
396
+ }
397
+
398
+ return 'unknown';
399
+ }
400
+
401
+ /**
402
+ * Sanitize identifier for variable names
403
+ */
404
+ private sanitizeIdentifier(id: string): string {
405
+ return id.replace(/[^a-zA-Z0-9_]/g, '_');
406
+ }
407
+ }
408
+
409
+ /**
410
+ * Create a logic generator
411
+ */
412
+ export function createLogicGenerator(
413
+ outputDir: string,
414
+ options?: Partial<LogicGeneratorOptions>
415
+ ): LogicGenerator {
416
+ return new LogicGenerator({
417
+ outputDir,
418
+ ...options,
419
+ });
420
+ }
@@ -0,0 +1,229 @@
1
+ /**
2
+ * PluresDB Config Generator
3
+ *
4
+ * Generates PluresDB configuration from schema definitions.
5
+ */
6
+
7
+ import type { NormalizedSchema, NormalizedModel } from '../schema/normalize.js';
8
+
9
+ /**
10
+ * PluresDB config generation options
11
+ */
12
+ export interface PluresDBGeneratorOptions {
13
+ /** Output directory */
14
+ outputDir: string;
15
+ /** Database name */
16
+ dbName?: string;
17
+ /** Database version */
18
+ dbVersion?: number;
19
+ /** Enable sync */
20
+ enableSync?: boolean;
21
+ /** Sync endpoint */
22
+ syncEndpoint?: string;
23
+ /** Auto-index strategy: 'all' indexes all string/number/date fields, 'explicit' only indexes fields defined in schema, 'none' disables auto-indexing */
24
+ autoIndex?: 'all' | 'explicit' | 'none';
25
+ }
26
+
27
+ /**
28
+ * Generated PluresDB config file
29
+ */
30
+ export interface GeneratedPluresDBFile {
31
+ /** File path */
32
+ path: string;
33
+ /** File content */
34
+ content: string;
35
+ /** File type */
36
+ type: 'config' | 'types';
37
+ }
38
+
39
+ /**
40
+ * PluresDB store definition
41
+ */
42
+ interface StoreDefinition {
43
+ keyPath: string;
44
+ indexes: string[];
45
+ }
46
+
47
+ /**
48
+ * PluresDB generator class
49
+ */
50
+ export class PluresDBGenerator {
51
+ private options: PluresDBGeneratorOptions;
52
+
53
+ constructor(options: PluresDBGeneratorOptions) {
54
+ this.options = {
55
+ dbVersion: 1,
56
+ enableSync: false,
57
+ autoIndex: 'all', // Default to indexing all fields for backward compatibility
58
+ ...options,
59
+ };
60
+ }
61
+
62
+ /**
63
+ * Generate PluresDB configuration from schema
64
+ */
65
+ generateConfig(schema: NormalizedSchema): GeneratedPluresDBFile[] {
66
+ const files: GeneratedPluresDBFile[] = [];
67
+
68
+ // Generate the main config file
69
+ files.push(this.generateConfigFile(schema));
70
+
71
+ return files;
72
+ }
73
+
74
+ /**
75
+ * Generate pluresdb-config.ts file
76
+ */
77
+ private generateConfigFile(schema: NormalizedSchema): GeneratedPluresDBFile {
78
+ const lines: string[] = [];
79
+ const dbName = this.options.dbName || schema.name.toLowerCase();
80
+
81
+ lines.push('/**');
82
+ lines.push(` * PluresDB Configuration for ${schema.name}`);
83
+ lines.push(' * Generated from Praxis schema');
84
+ lines.push(' */');
85
+ lines.push('');
86
+
87
+ // Import statement (for future PluresDB integration)
88
+ lines.push('// import { createPluresDB } from \'@plures/pluresdb\';');
89
+ lines.push('');
90
+
91
+ // Generate store definitions
92
+ lines.push('/**');
93
+ lines.push(' * Database store configuration');
94
+ lines.push(' * ');
95
+
96
+ // Document indexing behavior based on configuration
97
+ const autoIndexStrategy = this.options.autoIndex || 'all';
98
+ if (autoIndexStrategy === 'all') {
99
+ lines.push(' * Indexing: All string, number, and date fields are auto-indexed by default.');
100
+ lines.push(' * For large datasets, consider using autoIndex: "explicit" to only index');
101
+ lines.push(' * fields explicitly defined in the schema.');
102
+ } else if (autoIndexStrategy === 'explicit') {
103
+ lines.push(' * Indexing: Only fields explicitly defined in schema indexes are indexed.');
104
+ } else if (autoIndexStrategy === 'none') {
105
+ lines.push(' * Indexing: Auto-indexing disabled. Only explicit schema indexes are used.');
106
+ }
107
+ lines.push(' */');
108
+ lines.push('export const stores = {');
109
+
110
+ if (schema.models && schema.models.length > 0) {
111
+ for (const model of schema.models) {
112
+ const storeName = model.name.toLowerCase() + 's';
113
+ const storeConfig = this.generateStoreConfig(model);
114
+
115
+ lines.push(` ${storeName}: {`);
116
+ lines.push(` keyPath: '${storeConfig.keyPath}',`);
117
+
118
+ if (storeConfig.indexes.length > 0) {
119
+ lines.push(` indexes: [${storeConfig.indexes.map(idx => `'${idx}'`).join(', ')}],`);
120
+ }
121
+
122
+ lines.push(' },');
123
+ }
124
+ } else {
125
+ lines.push(' // No models defined in schema');
126
+ }
127
+
128
+ lines.push('};');
129
+ lines.push('');
130
+
131
+ // Generate main config
132
+ lines.push('/**');
133
+ lines.push(' * Database configuration');
134
+ lines.push(' */');
135
+ lines.push('export const dbConfig = {');
136
+ lines.push(` name: '${dbName}',`);
137
+ lines.push(` version: ${this.options.dbVersion},`);
138
+ lines.push(' stores,');
139
+
140
+ if (this.options.enableSync) {
141
+ lines.push(' sync: {');
142
+ lines.push(' enabled: true,');
143
+
144
+ if (this.options.syncEndpoint) {
145
+ lines.push(` endpoint: '${this.options.syncEndpoint}',`);
146
+ } else {
147
+ lines.push(' endpoint: \'ws://localhost:8080/sync\',');
148
+ }
149
+
150
+ lines.push(' conflictResolution: \'last-write-wins\',');
151
+ lines.push(' },');
152
+ }
153
+
154
+ lines.push('};');
155
+ lines.push('');
156
+
157
+ // Generate initialization function
158
+ lines.push('/**');
159
+ lines.push(' * Initialize PluresDB');
160
+ lines.push(' */');
161
+ lines.push('export function initDB() {');
162
+ lines.push(' // TODO: Implement PluresDB initialization');
163
+ lines.push(' // return createPluresDB(dbConfig);');
164
+ lines.push(' console.log(\'PluresDB config ready:\', dbConfig);');
165
+ lines.push(' return null;');
166
+ lines.push('}');
167
+
168
+ return {
169
+ path: `${this.options.outputDir}/pluresdb-config.ts`,
170
+ content: lines.join('\n'),
171
+ type: 'config',
172
+ };
173
+ }
174
+
175
+ /**
176
+ * Generate store configuration for a model
177
+ */
178
+ private generateStoreConfig(model: NormalizedModel): StoreDefinition {
179
+ // Find the ID field (or use 'id' as default)
180
+ const idField = model.fields.find(f => f.name === 'id' || f.name === '_id');
181
+ const keyPath = idField ? idField.name : 'id';
182
+
183
+ const indexes: string[] = [];
184
+
185
+ // Apply auto-indexing based on configuration
186
+ const autoIndexStrategy = this.options.autoIndex || 'all';
187
+
188
+ if (autoIndexStrategy === 'all') {
189
+ // Auto-index all string, number, and date fields for query performance
190
+ for (const field of model.fields) {
191
+ if (field.name !== keyPath) {
192
+ if (field.type === 'string' || field.type === 'number' || field.type === 'date') {
193
+ indexes.push(field.name);
194
+ }
195
+ }
196
+ }
197
+ }
198
+ // For 'explicit' and 'none', we only add indexes explicitly defined in the schema
199
+
200
+ // Always add indexes from schema index definitions (overrides auto-indexing)
201
+ if (model.indexes) {
202
+ for (const indexDef of model.indexes) {
203
+ for (const fieldName of indexDef.fields) {
204
+ if (!indexes.includes(fieldName) && fieldName !== keyPath) {
205
+ indexes.push(fieldName);
206
+ }
207
+ }
208
+ }
209
+ }
210
+
211
+ return {
212
+ keyPath,
213
+ indexes,
214
+ };
215
+ }
216
+ }
217
+
218
+ /**
219
+ * Create a PluresDB generator
220
+ */
221
+ export function createPluresDBGenerator(
222
+ outputDir: string,
223
+ options?: Partial<PluresDBGeneratorOptions>
224
+ ): PluresDBGenerator {
225
+ return new PluresDBGenerator({
226
+ outputDir,
227
+ ...options,
228
+ });
229
+ }