alepha 0.19.2 → 0.19.4

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 (241) hide show
  1. package/assets/swagger-ui/swagger-ui-bundle.js +1 -1
  2. package/dist/api/audits/index.d.ts +8 -8
  3. package/dist/api/invitations/index.d.ts +790 -0
  4. package/dist/api/invitations/index.d.ts.map +1 -0
  5. package/dist/api/invitations/index.js +665 -0
  6. package/dist/api/invitations/index.js.map +1 -0
  7. package/dist/api/jobs/index.browser.js +8 -9
  8. package/dist/api/jobs/index.browser.js.map +1 -1
  9. package/dist/api/jobs/index.d.ts +90 -34
  10. package/dist/api/jobs/index.d.ts.map +1 -1
  11. package/dist/api/jobs/index.js +267 -44
  12. package/dist/api/jobs/index.js.map +1 -1
  13. package/dist/api/notifications/index.browser.js +0 -1
  14. package/dist/api/notifications/index.browser.js.map +1 -1
  15. package/dist/api/notifications/index.d.ts +3 -3
  16. package/dist/api/notifications/index.d.ts.map +1 -1
  17. package/dist/api/notifications/index.js +0 -1
  18. package/dist/api/notifications/index.js.map +1 -1
  19. package/dist/api/parameters/index.browser.js +112 -1
  20. package/dist/api/parameters/index.browser.js.map +1 -1
  21. package/dist/api/parameters/index.d.ts +90 -3
  22. package/dist/api/parameters/index.d.ts.map +1 -1
  23. package/dist/api/parameters/index.js +79 -12
  24. package/dist/api/parameters/index.js.map +1 -1
  25. package/dist/{billing → api/payments}/index.d.ts +67 -49
  26. package/dist/api/payments/index.d.ts.map +1 -0
  27. package/dist/{billing → api/payments}/index.js +108 -74
  28. package/dist/api/payments/index.js.map +1 -0
  29. package/dist/api/subscriptions/index.d.ts +1692 -0
  30. package/dist/api/subscriptions/index.d.ts.map +1 -0
  31. package/dist/api/subscriptions/index.js +1870 -0
  32. package/dist/api/subscriptions/index.js.map +1 -0
  33. package/dist/api/users/index.d.ts +27 -21
  34. package/dist/api/users/index.d.ts.map +1 -1
  35. package/dist/api/users/index.js +167 -34
  36. package/dist/api/users/index.js.map +1 -1
  37. package/dist/api/workflows/index.browser.js +246 -0
  38. package/dist/api/workflows/index.browser.js.map +1 -0
  39. package/dist/api/workflows/index.d.ts +1618 -0
  40. package/dist/api/workflows/index.d.ts.map +1 -0
  41. package/dist/api/workflows/index.js +1504 -0
  42. package/dist/api/workflows/index.js.map +1 -0
  43. package/dist/cli/config/index.d.ts +6 -28
  44. package/dist/cli/config/index.d.ts.map +1 -1
  45. package/dist/cli/config/index.js +5 -10
  46. package/dist/cli/config/index.js.map +1 -1
  47. package/dist/cli/core/index.d.ts +11669 -208
  48. package/dist/cli/core/index.d.ts.map +1 -1
  49. package/dist/cli/core/index.js +60 -69
  50. package/dist/cli/core/index.js.map +1 -1
  51. package/dist/cli/devtools/index.d.ts +5 -0
  52. package/dist/cli/devtools/index.d.ts.map +1 -1
  53. package/dist/cli/devtools/index.js +4 -0
  54. package/dist/cli/devtools/index.js.map +1 -1
  55. package/dist/cli/platform/index.d.ts +69 -64
  56. package/dist/cli/platform/index.d.ts.map +1 -1
  57. package/dist/cli/platform/index.js +6 -2
  58. package/dist/cli/platform/index.js.map +1 -1
  59. package/dist/cli/vendor/index.d.ts +38 -10
  60. package/dist/cli/vendor/index.d.ts.map +1 -1
  61. package/dist/cli/vendor/index.js +85 -26
  62. package/dist/cli/vendor/index.js.map +1 -1
  63. package/dist/core/index.browser.js +21 -2
  64. package/dist/core/index.browser.js.map +1 -1
  65. package/dist/core/index.d.ts +33 -2
  66. package/dist/core/index.d.ts.map +1 -1
  67. package/dist/core/index.js +25 -2
  68. package/dist/core/index.js.map +1 -1
  69. package/dist/core/index.native.js +25 -2
  70. package/dist/core/index.native.js.map +1 -1
  71. package/dist/core/index.workerd.js +25 -2
  72. package/dist/core/index.workerd.js.map +1 -1
  73. package/dist/email/smtp/index.js +24 -8
  74. package/dist/email/smtp/index.js.map +1 -1
  75. package/dist/logger/index.d.ts.map +1 -1
  76. package/dist/logger/index.js +1 -1
  77. package/dist/logger/index.js.map +1 -1
  78. package/dist/orm/core/index.browser.js +0 -18
  79. package/dist/orm/core/index.browser.js.map +1 -1
  80. package/dist/orm/core/index.bun.js +25 -73
  81. package/dist/orm/core/index.bun.js.map +1 -1
  82. package/dist/orm/core/index.d.ts +10 -32
  83. package/dist/orm/core/index.d.ts.map +1 -1
  84. package/dist/orm/core/index.js +25 -73
  85. package/dist/orm/core/index.js.map +1 -1
  86. package/dist/orm/postgres/index.bun.js +3 -3
  87. package/dist/orm/postgres/index.bun.js.map +1 -1
  88. package/dist/orm/postgres/index.d.ts +2 -1
  89. package/dist/orm/postgres/index.d.ts.map +1 -1
  90. package/dist/orm/postgres/index.js +3 -3
  91. package/dist/orm/postgres/index.js.map +1 -1
  92. package/dist/react/router/index.browser.js +25 -3
  93. package/dist/react/router/index.browser.js.map +1 -1
  94. package/dist/react/router/index.d.ts +16 -1
  95. package/dist/react/router/index.d.ts.map +1 -1
  96. package/dist/react/router/index.js +25 -3
  97. package/dist/react/router/index.js.map +1 -1
  98. package/dist/security/index.d.ts +28 -0
  99. package/dist/security/index.d.ts.map +1 -1
  100. package/dist/security/index.js +28 -0
  101. package/dist/security/index.js.map +1 -1
  102. package/package.json +37 -20
  103. package/src/api/invitations/__tests__/InvitationService.spec.ts +439 -0
  104. package/src/api/invitations/controllers/AdminInvitationController.ts +86 -0
  105. package/src/api/invitations/controllers/InvitationController.ts +84 -0
  106. package/src/api/invitations/entities/invitations.ts +33 -0
  107. package/src/api/invitations/index.ts +65 -0
  108. package/src/api/invitations/jobs/InvitationJobs.ts +37 -0
  109. package/src/api/invitations/providers/InvitationProvider.ts +45 -0
  110. package/src/api/invitations/schemas/createInvitationSchema.ts +12 -0
  111. package/src/api/invitations/schemas/invitationConfigAtom.ts +20 -0
  112. package/src/api/invitations/schemas/invitationQuerySchema.ts +15 -0
  113. package/src/api/invitations/schemas/invitationResourceSchema.ts +6 -0
  114. package/src/api/invitations/schemas/invitationWithResourceInfoSchema.ts +22 -0
  115. package/src/api/invitations/schemas/myInvitationsQuerySchema.ts +10 -0
  116. package/src/api/invitations/services/InvitationService.ts +556 -0
  117. package/src/api/jobs/__tests__/$job.spec.ts +876 -0
  118. package/src/api/jobs/controllers/AdminJobController.ts +44 -0
  119. package/src/api/jobs/entities/jobExecutionEntity.ts +0 -2
  120. package/src/api/jobs/index.ts +0 -3
  121. package/src/api/jobs/primitives/$job.ts +22 -11
  122. package/src/api/jobs/providers/JobProvider.ts +239 -25
  123. package/src/api/jobs/schemas/jobConfigAtom.ts +4 -0
  124. package/src/api/jobs/schemas/jobCronInfoSchema.ts +1 -0
  125. package/src/api/jobs/schemas/jobExecutionQuerySchema.ts +0 -1
  126. package/src/api/jobs/schemas/jobQueueDepthSchema.ts +1 -0
  127. package/src/api/jobs/schemas/jobRegistrationSchema.ts +1 -6
  128. package/src/api/jobs/services/JobService.ts +51 -12
  129. package/src/api/notifications/schemas/notificationQuerySchema.ts +0 -1
  130. package/src/api/parameters/__tests__/$parameter.spec.ts +327 -0
  131. package/src/api/parameters/controllers/AdminParameterController.ts +29 -3
  132. package/src/api/parameters/index.browser.ts +12 -0
  133. package/src/api/parameters/primitives/$parameter.ts +20 -3
  134. package/src/api/parameters/services/ParameterProvider.ts +48 -7
  135. package/src/{billing → api/payments}/__tests__/PaymentMethodService.spec.ts +32 -6
  136. package/src/api/payments/__tests__/PaymentService.spec.ts +279 -0
  137. package/src/{billing/controllers/AdminBillingController.ts → api/payments/controllers/AdminPaymentController.ts} +26 -21
  138. package/src/{billing/controllers/BillingController.ts → api/payments/controllers/PaymentController.ts} +23 -11
  139. package/src/{billing → api/payments}/entities/paymentIntents.ts +1 -0
  140. package/src/{billing/errors/BillingError.ts → api/payments/errors/PaymentError.ts} +1 -1
  141. package/src/{billing → api/payments}/index.ts +31 -25
  142. package/src/{billing/providers/MemoryBillingProvider.ts → api/payments/providers/MemoryPaymentProvider.ts} +4 -4
  143. package/src/{billing/providers/BillingProvider.ts → api/payments/providers/PaymentProvider.ts} +9 -2
  144. package/src/{billing → api/payments}/services/PaymentMethodService.ts +5 -5
  145. package/src/{billing/services/BillingService.ts → api/payments/services/PaymentService.ts} +94 -18
  146. package/src/api/subscriptions/__tests__/BillingService.spec.ts +218 -0
  147. package/src/api/subscriptions/__tests__/SubscriptionService.spec.ts +278 -0
  148. package/src/api/subscriptions/controllers/AdminSubscriptionController.ts +212 -0
  149. package/src/api/subscriptions/controllers/SubscriptionController.ts +189 -0
  150. package/src/api/subscriptions/entities/subscriptionEvents.ts +54 -0
  151. package/src/api/subscriptions/entities/subscriptions.ts +68 -0
  152. package/src/api/subscriptions/index.ts +144 -0
  153. package/src/api/subscriptions/jobs/SubscriptionJobs.ts +382 -0
  154. package/src/api/subscriptions/middleware/$requireLimit.ts +50 -0
  155. package/src/api/subscriptions/middleware/$requirePlan.ts +49 -0
  156. package/src/api/subscriptions/notifications/SubscriptionNotifications.ts +110 -0
  157. package/src/api/subscriptions/schemas/cancelSubscriptionSchema.ts +8 -0
  158. package/src/api/subscriptions/schemas/changePlanSchema.ts +9 -0
  159. package/src/api/subscriptions/schemas/createSubscriptionSchema.ts +11 -0
  160. package/src/api/subscriptions/schemas/entitlementsSchema.ts +21 -0
  161. package/src/api/subscriptions/schemas/mrrSchema.ts +13 -0
  162. package/src/api/subscriptions/schemas/planDefinitionSchema.ts +71 -0
  163. package/src/api/subscriptions/schemas/planResourceSchema.ts +25 -0
  164. package/src/api/subscriptions/schemas/subscriptionEventResourceSchema.ts +8 -0
  165. package/src/api/subscriptions/schemas/subscriptionQuerySchema.ts +19 -0
  166. package/src/api/subscriptions/schemas/subscriptionResourceSchema.ts +6 -0
  167. package/src/api/subscriptions/schemas/subscriptionSettingsSchema.ts +32 -0
  168. package/src/api/subscriptions/schemas/subscriptionStatsSchema.ts +23 -0
  169. package/src/api/subscriptions/services/BillingService.ts +437 -0
  170. package/src/api/subscriptions/services/SubscriptionConfig.ts +56 -0
  171. package/src/api/subscriptions/services/SubscriptionService.ts +867 -0
  172. package/src/api/subscriptions/services/UsageService.ts +118 -0
  173. package/src/api/users/__tests__/AdminUserController.spec.ts +80 -1
  174. package/src/api/users/__tests__/CredentialService.spec.ts +177 -0
  175. package/src/api/users/__tests__/EmailVerification.spec.ts +29 -18
  176. package/src/api/users/__tests__/PasswordReset.spec.ts +3 -0
  177. package/src/api/users/__tests__/RegistrationService.spec.ts +148 -1
  178. package/src/api/users/__tests__/SessionService.spec.ts +142 -1
  179. package/src/api/users/atoms/realmAuthSettingsAtom.ts +10 -1
  180. package/src/api/users/controllers/UserController.ts +3 -8
  181. package/src/api/users/notifications/UserNotifications.ts +23 -0
  182. package/src/api/users/schemas/loginSchema.ts +1 -1
  183. package/src/api/users/services/CredentialService.ts +51 -4
  184. package/src/api/users/services/RegistrationService.ts +38 -9
  185. package/src/api/users/services/SessionService.ts +62 -9
  186. package/src/api/users/services/UserService.ts +21 -12
  187. package/src/api/workflows/__tests__/$workflow.spec.ts +616 -0
  188. package/src/api/workflows/controllers/AdminWorkflowController.ts +191 -0
  189. package/src/api/workflows/entities/workflowExecutions.ts +74 -0
  190. package/src/api/workflows/entities/workflowStepExecutions.ts +74 -0
  191. package/src/api/workflows/entities/workflowStepLogs.ts +13 -0
  192. package/src/api/workflows/index.browser.ts +22 -0
  193. package/src/api/workflows/index.ts +124 -0
  194. package/src/api/workflows/jobs/WorkflowJobs.ts +77 -0
  195. package/src/api/workflows/primitives/$workflow.ts +202 -0
  196. package/src/api/workflows/providers/WorkflowProvider.ts +1284 -0
  197. package/src/api/workflows/schemas/workflowActivitySchema.ts +15 -0
  198. package/src/api/workflows/schemas/workflowConfigAtom.ts +51 -0
  199. package/src/api/workflows/schemas/workflowExecutionDetailSchema.ts +18 -0
  200. package/src/api/workflows/schemas/workflowExecutionQuerySchema.ts +26 -0
  201. package/src/api/workflows/schemas/workflowExecutionResourceSchema.ts +30 -0
  202. package/src/api/workflows/schemas/workflowRegistrationSchema.ts +26 -0
  203. package/src/api/workflows/schemas/workflowStatsSchema.ts +16 -0
  204. package/src/api/workflows/schemas/workflowStepExecutionResourceSchema.ts +15 -0
  205. package/src/api/workflows/services/WorkflowService.ts +382 -0
  206. package/src/cli/config/defineConfig.ts +17 -46
  207. package/src/cli/core/providers/ViteDevServerProvider.ts +45 -3
  208. package/src/cli/core/services/PackageManagerUtils.ts +3 -1
  209. package/src/cli/core/services/ProjectScaffolder.ts +5 -5
  210. package/src/cli/core/templates/agentMd.ts +14 -5
  211. package/src/cli/core/templates/webAppRouterTs.ts +5 -58
  212. package/src/cli/devtools/index.ts +21 -1
  213. package/src/cli/platform/index.ts +23 -2
  214. package/src/cli/vendor/__tests__/VendorService.spec.ts +283 -178
  215. package/src/cli/vendor/index.ts +20 -3
  216. package/src/cli/vendor/services/VendorService.ts +126 -27
  217. package/src/core/Alepha.ts +10 -0
  218. package/src/core/__tests__/TypeProvider.spec.ts +4 -2
  219. package/src/core/providers/SchemaValidator.ts +1 -1
  220. package/src/core/providers/TypeProvider.ts +46 -3
  221. package/src/logger/index.ts +6 -1
  222. package/src/orm/__tests__/enums.spec.ts +22 -29
  223. package/src/orm/__tests__/orm-showcase-tests.ts +430 -0
  224. package/src/orm/__tests__/orm-showcase.spec.ts +167 -0
  225. package/src/orm/core/providers/DatabaseTypeProvider.ts +0 -29
  226. package/src/orm/core/providers/DrizzleKitProvider.ts +56 -105
  227. package/src/orm/postgres/services/PostgresModelBuilder.ts +3 -6
  228. package/src/react/router/__tests__/$page.browser.spec.tsx +157 -0
  229. package/src/react/router/providers/ReactBrowserProvider.ts +39 -0
  230. package/src/react/router/providers/ReactBrowserRouterProvider.ts +22 -0
  231. package/src/security/__tests__/$secure-combinations.spec.ts +945 -0
  232. package/src/security/primitives/$secure.ts +28 -0
  233. package/tsconfig.base.json +0 -1
  234. package/dist/billing/index.d.ts.map +0 -1
  235. package/dist/billing/index.js.map +0 -1
  236. package/src/billing/__tests__/BillingService.spec.ts +0 -136
  237. /package/src/{billing → api/payments}/entities/paymentMethods.ts +0 -0
  238. /package/src/{billing → api/payments}/entities/refunds.ts +0 -0
  239. /package/src/{billing → api/payments}/schemas/intentSchemas.ts +0 -0
  240. /package/src/{billing → api/payments}/schemas/paymentMethodSchemas.ts +0 -0
  241. /package/src/{billing → api/payments}/schemas/refundSchemas.ts +0 -0
@@ -0,0 +1,191 @@
1
+ import { $inject, t } from "alepha";
2
+ import { $secure } from "alepha/security";
3
+ import { $action, okSchema } from "alepha/server";
4
+ import {
5
+ workflowActivityPointSchema,
6
+ workflowActivityQuerySchema,
7
+ } from "../schemas/workflowActivitySchema.ts";
8
+ import { workflowExecutionDetailSchema } from "../schemas/workflowExecutionDetailSchema.ts";
9
+ import { workflowExecutionQuerySchema } from "../schemas/workflowExecutionQuerySchema.ts";
10
+ import { workflowExecutionResourceSchema } from "../schemas/workflowExecutionResourceSchema.ts";
11
+ import { workflowRegistrationSchema } from "../schemas/workflowRegistrationSchema.ts";
12
+ import { workflowStatsSchema } from "../schemas/workflowStatsSchema.ts";
13
+ import { WorkflowService } from "../services/WorkflowService.ts";
14
+
15
+ export class AdminWorkflowController {
16
+ protected readonly url: string = "/workflows";
17
+ protected readonly group: string = "admin:workflows";
18
+ protected readonly workflowService = $inject(WorkflowService);
19
+
20
+ public readonly getRegistry = $action({
21
+ path: this.url,
22
+ group: this.group,
23
+ use: [$secure({ permissions: ["admin:workflow:read"] })],
24
+ schema: {
25
+ response: t.array(workflowRegistrationSchema),
26
+ },
27
+ handler: () => this.workflowService.getRegistry(),
28
+ });
29
+
30
+ public readonly getStats = $action({
31
+ path: `${this.url}/stats`,
32
+ group: this.group,
33
+ use: [$secure({ permissions: ["admin:workflow:read"] })],
34
+ schema: {
35
+ query: workflowActivityQuerySchema,
36
+ response: workflowStatsSchema,
37
+ },
38
+ handler: ({ query }) => this.workflowService.getStats(query.days),
39
+ });
40
+
41
+ public readonly getActivity = $action({
42
+ path: `${this.url}/activity`,
43
+ group: this.group,
44
+ use: [$secure({ permissions: ["admin:workflow:read"] })],
45
+ schema: {
46
+ query: workflowActivityQuerySchema,
47
+ response: t.array(workflowActivityPointSchema),
48
+ },
49
+ handler: ({ query }) => this.workflowService.getActivity(query.days),
50
+ });
51
+
52
+ public readonly findExecutions = $action({
53
+ path: `${this.url}/executions`,
54
+ group: this.group,
55
+ use: [$secure({ permissions: ["admin:workflow:read"] })],
56
+ schema: {
57
+ query: workflowExecutionQuerySchema,
58
+ response: t.page(workflowExecutionResourceSchema),
59
+ },
60
+ handler: ({ query }) => this.workflowService.findExecutions(query),
61
+ });
62
+
63
+ public readonly getExecution = $action({
64
+ path: `${this.url}/executions/:id`,
65
+ group: this.group,
66
+ use: [$secure({ permissions: ["admin:workflow:read"] })],
67
+ schema: {
68
+ params: t.object({
69
+ id: t.uuid(),
70
+ }),
71
+ response: workflowExecutionDetailSchema,
72
+ },
73
+ handler: ({ params }) => this.workflowService.getExecution(params.id),
74
+ });
75
+
76
+ public readonly startWorkflow = $action({
77
+ method: "POST",
78
+ path: `${this.url}/start`,
79
+ group: this.group,
80
+ use: [$secure({ permissions: ["admin:workflow:create"] })],
81
+ schema: {
82
+ body: t.object({
83
+ name: t.text(),
84
+ payload: t.optional(t.record(t.text(), t.any())),
85
+ key: t.optional(t.text()),
86
+ tags: t.optional(t.array(t.text())),
87
+ }),
88
+ response: t.object({ id: t.uuid() }),
89
+ },
90
+ handler: async ({ body, user }) => {
91
+ return this.workflowService.triggerWorkflow(body.name, body.payload, {
92
+ key: body.key,
93
+ tags: body.tags,
94
+ triggeredBy: user?.id,
95
+ triggeredByName: user?.name,
96
+ });
97
+ },
98
+ });
99
+
100
+ public readonly signalStep = $action({
101
+ method: "POST",
102
+ path: `${this.url}/executions/:id/signal`,
103
+ group: this.group,
104
+ use: [$secure({ permissions: ["admin:workflow:update"] })],
105
+ schema: {
106
+ params: t.object({
107
+ id: t.uuid(),
108
+ }),
109
+ body: t.object({
110
+ stepName: t.text(),
111
+ payload: t.optional(t.record(t.text(), t.any())),
112
+ }),
113
+ response: okSchema,
114
+ },
115
+ handler: async ({ params, body, user }) => {
116
+ return this.workflowService.signalExecution(
117
+ params.id,
118
+ body.stepName,
119
+ body.payload,
120
+ user?.id,
121
+ );
122
+ },
123
+ });
124
+
125
+ public readonly cancelExecution = $action({
126
+ method: "POST",
127
+ path: `${this.url}/executions/:id/cancel`,
128
+ group: this.group,
129
+ use: [$secure({ permissions: ["admin:workflow:update"] })],
130
+ schema: {
131
+ params: t.object({
132
+ id: t.uuid(),
133
+ }),
134
+ response: okSchema,
135
+ },
136
+ handler: async ({ params, user }) => {
137
+ return this.workflowService.cancelExecution(params.id, {
138
+ cancelledBy: user?.id,
139
+ cancelledByName: user?.name,
140
+ });
141
+ },
142
+ });
143
+
144
+ public readonly retryExecution = $action({
145
+ method: "POST",
146
+ path: `${this.url}/executions/:id/retry`,
147
+ group: this.group,
148
+ use: [$secure({ permissions: ["admin:workflow:update"] })],
149
+ schema: {
150
+ params: t.object({
151
+ id: t.uuid(),
152
+ }),
153
+ response: okSchema,
154
+ },
155
+ handler: async ({ params }) => {
156
+ return this.workflowService.retryExecution(params.id);
157
+ },
158
+ });
159
+
160
+ public readonly restartExecution = $action({
161
+ method: "POST",
162
+ path: `${this.url}/executions/:id/restart`,
163
+ group: this.group,
164
+ use: [$secure({ permissions: ["admin:workflow:create"] })],
165
+ schema: {
166
+ params: t.object({
167
+ id: t.uuid(),
168
+ }),
169
+ response: t.object({ id: t.uuid() }),
170
+ },
171
+ handler: async ({ params }) => {
172
+ return this.workflowService.restartExecution(params.id);
173
+ },
174
+ });
175
+
176
+ public readonly compensateExecution = $action({
177
+ method: "POST",
178
+ path: `${this.url}/executions/:id/compensate`,
179
+ group: this.group,
180
+ use: [$secure({ permissions: ["admin:workflow:update"] })],
181
+ schema: {
182
+ params: t.object({
183
+ id: t.uuid(),
184
+ }),
185
+ response: okSchema,
186
+ },
187
+ handler: async ({ params }) => {
188
+ return this.workflowService.compensateExecution(params.id);
189
+ },
190
+ });
191
+ }
@@ -0,0 +1,74 @@
1
+ import { type Static, t } from "alepha";
2
+ import { $entity, db, sql } from "alepha/orm";
3
+
4
+ export const workflowExecutions = $entity({
5
+ name: "workflow_executions",
6
+ schema: t.object({
7
+ id: db.primaryKey(t.uuid()),
8
+ createdAt: db.createdAt(),
9
+ updatedAt: db.updatedAt(),
10
+
11
+ workflowName: t.text(),
12
+ tags: t.optional(t.array(t.text())),
13
+
14
+ payload: t.optional(t.record(t.text(), t.any())),
15
+
16
+ status: db.default(
17
+ t.enum([
18
+ "pending",
19
+ "running",
20
+ "waiting_for_signal",
21
+ "completed",
22
+ "failed",
23
+ "timed_out",
24
+ "compensating",
25
+ "compensated",
26
+ "compensation_failed",
27
+ "cancelled",
28
+ ]),
29
+ "pending",
30
+ ),
31
+ currentStep: t.optional(t.text()),
32
+
33
+ startedAt: t.optional(t.datetime()),
34
+ completedAt: t.optional(t.datetime()),
35
+ deadlineAt: t.optional(t.datetime()),
36
+
37
+ error: t.optional(t.text()),
38
+ errorStep: t.optional(t.text()),
39
+
40
+ triggeredBy: t.optional(t.text()),
41
+ triggeredByName: t.optional(t.text()),
42
+ cancelledBy: t.optional(t.text()),
43
+ cancelledByName: t.optional(t.text()),
44
+
45
+ key: t.optional(t.nullable(t.text())),
46
+
47
+ priority: db.default(t.integer({ minimum: 0, maximum: 3 }), 2),
48
+ }),
49
+ indexes: [
50
+ { columns: ["workflowName", "status"] },
51
+ { columns: ["workflowName", "status", "createdAt"] },
52
+ {
53
+ columns: ["workflowName", "key"],
54
+ unique: true,
55
+ where: sql`status NOT IN ('completed', 'failed', 'compensated', 'compensation_failed', 'cancelled')`,
56
+ },
57
+ { columns: ["status", "deadlineAt"] },
58
+ { columns: ["completedAt"] },
59
+ ],
60
+ });
61
+
62
+ export type WorkflowExecutionEntity = Static<typeof workflowExecutions.schema>;
63
+
64
+ export type WorkflowStatus =
65
+ | "pending"
66
+ | "running"
67
+ | "waiting_for_signal"
68
+ | "completed"
69
+ | "failed"
70
+ | "timed_out"
71
+ | "compensating"
72
+ | "compensated"
73
+ | "compensation_failed"
74
+ | "cancelled";
@@ -0,0 +1,74 @@
1
+ import { type Static, t } from "alepha";
2
+ import { $entity, db } from "alepha/orm";
3
+ import { workflowExecutions } from "./workflowExecutions.ts";
4
+
5
+ export const workflowStepExecutions = $entity({
6
+ name: "workflow_step_executions",
7
+ schema: t.object({
8
+ id: db.primaryKey(t.uuid()),
9
+ createdAt: db.createdAt(),
10
+ updatedAt: db.updatedAt(),
11
+
12
+ workflowExecutionId: db.ref(t.uuid(), () => workflowExecutions.cols.id, {
13
+ onDelete: "cascade",
14
+ }),
15
+
16
+ stepName: t.text(),
17
+ stepIndex: t.integer(),
18
+ stepType: db.default(t.enum(["handler", "signal", "parallel"]), "handler"),
19
+
20
+ parentStepId: t.optional(t.uuid()),
21
+ branchName: t.optional(t.text()),
22
+
23
+ status: db.default(
24
+ t.enum([
25
+ "pending",
26
+ "running",
27
+ "completed",
28
+ "failed",
29
+ "skipped",
30
+ "waiting",
31
+ "compensating",
32
+ "compensated",
33
+ "compensation_failed",
34
+ "cancelled",
35
+ ]),
36
+ "pending",
37
+ ),
38
+ attempt: db.default(t.integer(), 0),
39
+ maxAttempts: db.default(t.integer(), 1),
40
+
41
+ result: t.optional(t.record(t.text(), t.any())),
42
+ error: t.optional(t.text()),
43
+
44
+ startedAt: t.optional(t.datetime()),
45
+ completedAt: t.optional(t.datetime()),
46
+ deadlineAt: t.optional(t.datetime()),
47
+
48
+ signalPayload: t.optional(t.record(t.text(), t.any())),
49
+ signalledBy: t.optional(t.text()),
50
+ signalledAt: t.optional(t.datetime()),
51
+ }),
52
+ indexes: [
53
+ { columns: ["workflowExecutionId", "stepName"] },
54
+ { columns: ["workflowExecutionId", "stepIndex"] },
55
+ { columns: ["workflowExecutionId", "status"] },
56
+ { columns: ["status", "deadlineAt"] },
57
+ ],
58
+ });
59
+
60
+ export type WorkflowStepExecutionEntity = Static<
61
+ typeof workflowStepExecutions.schema
62
+ >;
63
+
64
+ export type WorkflowStepStatus =
65
+ | "pending"
66
+ | "running"
67
+ | "completed"
68
+ | "failed"
69
+ | "skipped"
70
+ | "waiting"
71
+ | "compensating"
72
+ | "compensated"
73
+ | "compensation_failed"
74
+ | "cancelled";
@@ -0,0 +1,13 @@
1
+ import { type Static, t } from "alepha";
2
+ import { logEntrySchema } from "alepha/logger";
3
+ import { $entity, db } from "alepha/orm";
4
+
5
+ export const workflowStepLogs = $entity({
6
+ name: "workflow_step_logs",
7
+ schema: t.object({
8
+ id: db.primaryKey(t.uuid()),
9
+ logs: t.array(logEntrySchema),
10
+ }),
11
+ });
12
+
13
+ export type WorkflowStepLogEntity = Static<typeof workflowStepLogs.schema>;
@@ -0,0 +1,22 @@
1
+ import { $module } from "alepha";
2
+
3
+ // -----------------------------------------------------------------------------------------------------------------
4
+
5
+ export * from "./entities/workflowExecutions.ts";
6
+ export * from "./entities/workflowStepExecutions.ts";
7
+ export * from "./entities/workflowStepLogs.ts";
8
+ export * from "./schemas/workflowActivitySchema.ts";
9
+ export * from "./schemas/workflowConfigAtom.ts";
10
+ export * from "./schemas/workflowExecutionDetailSchema.ts";
11
+ export * from "./schemas/workflowExecutionQuerySchema.ts";
12
+ export * from "./schemas/workflowExecutionResourceSchema.ts";
13
+ export * from "./schemas/workflowRegistrationSchema.ts";
14
+ export * from "./schemas/workflowStatsSchema.ts";
15
+ export * from "./schemas/workflowStepExecutionResourceSchema.ts";
16
+
17
+ // -----------------------------------------------------------------------------------------------------------------
18
+
19
+ export const AlephaApiWorkflows = $module({
20
+ name: "alepha.api.workflows",
21
+ services: [],
22
+ });
@@ -0,0 +1,124 @@
1
+ import { $module, type Alepha } from "alepha";
2
+ import { AlephaApiJobs } from "alepha/api/jobs";
3
+ import { AlephaLock } from "alepha/lock";
4
+ import { AdminWorkflowController } from "./controllers/AdminWorkflowController.ts";
5
+ import { WorkflowJobs } from "./jobs/WorkflowJobs.ts";
6
+ import { $workflow } from "./primitives/$workflow.ts";
7
+ import { WorkflowProvider } from "./providers/WorkflowProvider.ts";
8
+ import { WorkflowService } from "./services/WorkflowService.ts";
9
+
10
+ // -----------------------------------------------------------------------------------------------------------------
11
+
12
+ export * from "./controllers/AdminWorkflowController.ts";
13
+ export * from "./entities/workflowExecutions.ts";
14
+ export * from "./entities/workflowStepExecutions.ts";
15
+ export * from "./entities/workflowStepLogs.ts";
16
+ export * from "./primitives/$workflow.ts";
17
+ export * from "./providers/WorkflowProvider.ts";
18
+ export * from "./schemas/workflowActivitySchema.ts";
19
+ export * from "./schemas/workflowConfigAtom.ts";
20
+ export * from "./schemas/workflowExecutionDetailSchema.ts";
21
+ export * from "./schemas/workflowExecutionQuerySchema.ts";
22
+ export * from "./schemas/workflowExecutionResourceSchema.ts";
23
+ export * from "./schemas/workflowRegistrationSchema.ts";
24
+ export * from "./schemas/workflowStatsSchema.ts";
25
+ export * from "./schemas/workflowStepExecutionResourceSchema.ts";
26
+ export * from "./services/WorkflowService.ts";
27
+
28
+ // -----------------------------------------------------------------------------------------------------------------
29
+
30
+ declare module "alepha" {
31
+ interface Hooks {
32
+ "workflow:started": { workflowName: string; workflowId: string };
33
+ "workflow:step:begin": {
34
+ workflowName: string;
35
+ workflowId: string;
36
+ stepName: string;
37
+ };
38
+ "workflow:step:completed": {
39
+ workflowName: string;
40
+ workflowId: string;
41
+ stepName: string;
42
+ result: unknown;
43
+ };
44
+ "workflow:step:failed": {
45
+ workflowName: string;
46
+ workflowId: string;
47
+ stepName: string;
48
+ error: Error;
49
+ };
50
+ "workflow:step:skipped": {
51
+ workflowName: string;
52
+ workflowId: string;
53
+ stepName: string;
54
+ };
55
+ "workflow:signal:received": {
56
+ workflowName: string;
57
+ workflowId: string;
58
+ stepName: string;
59
+ payload: unknown;
60
+ };
61
+ "workflow:signal:timeout": {
62
+ workflowName: string;
63
+ workflowId: string;
64
+ stepName: string;
65
+ };
66
+ "workflow:completed": { workflowName: string; workflowId: string };
67
+ "workflow:failed": {
68
+ workflowName: string;
69
+ workflowId: string;
70
+ error: Error;
71
+ stepName: string;
72
+ };
73
+ "workflow:compensating": {
74
+ workflowName: string;
75
+ workflowId: string;
76
+ stepName: string;
77
+ };
78
+ "workflow:compensated": { workflowName: string; workflowId: string };
79
+ "workflow:compensation:failed": {
80
+ workflowName: string;
81
+ workflowId: string;
82
+ stepName: string;
83
+ error: Error;
84
+ };
85
+ "workflow:cancelled": { workflowName: string; workflowId: string };
86
+ "workflow:timed_out": { workflowName: string; workflowId: string };
87
+ }
88
+ }
89
+
90
+ // -----------------------------------------------------------------------------------------------------------------
91
+
92
+ /**
93
+ * Durable workflow engine for long-running business processes.
94
+ *
95
+ * **Features:**
96
+ * - Declarative, multi-step workflows with typed payloads
97
+ * - Saga-pattern compensation for failure recovery
98
+ * - Per-step retry with exponential backoff
99
+ * - Workflow-level timeout and cancellation
100
+ * - Deduplication via unique keys
101
+ * - Per-execution log capture
102
+ *
103
+ * @module alepha.api.workflows
104
+ */
105
+ export const AlephaApiWorkflows = $module({
106
+ name: "alepha.api.workflows",
107
+ primitives: [$workflow],
108
+ services: [
109
+ AlephaApiJobs,
110
+ AlephaLock,
111
+ WorkflowProvider,
112
+ WorkflowService,
113
+ WorkflowJobs,
114
+ AdminWorkflowController,
115
+ ],
116
+ register: (alepha: Alepha) => {
117
+ alepha.with(AlephaApiJobs);
118
+ alepha.with(AlephaLock);
119
+ alepha.with(WorkflowProvider);
120
+ alepha.with(WorkflowService);
121
+ alepha.with(WorkflowJobs);
122
+ alepha.with(AdminWorkflowController);
123
+ },
124
+ });
@@ -0,0 +1,77 @@
1
+ import { $hook, $inject, t } from "alepha";
2
+ import { $job } from "alepha/api/jobs";
3
+ import { WorkflowProvider } from "../providers/WorkflowProvider.ts";
4
+
5
+ // -----------------------------------------------------------------------------------------------------------------
6
+
7
+ export class WorkflowJobs {
8
+ protected readonly workflowProvider = $inject(WorkflowProvider);
9
+
10
+ protected readonly dispatchStep = $job({
11
+ schema: t.object({
12
+ workflowId: t.uuid(),
13
+ stepName: t.text(),
14
+ }),
15
+ retry: { retries: 2, backoff: [1, "second"] },
16
+ timeout: [10, "minute"],
17
+ concurrency: 10,
18
+ handler: async ({ items }) => {
19
+ for (const item of items) {
20
+ await this.workflowProvider.processStep(
21
+ item.payload.workflowId,
22
+ item.payload.stepName,
23
+ );
24
+ }
25
+ },
26
+ });
27
+
28
+ protected readonly timeoutSweep = $job({
29
+ cron: "* * * * *",
30
+ lock: true,
31
+ handler: async () => {
32
+ await this.workflowProvider.timeoutSweep();
33
+ },
34
+ });
35
+
36
+ protected readonly purge = $job({
37
+ cron: "0 3 * * *",
38
+ lock: true,
39
+ handler: async () => {
40
+ await this.workflowProvider.purge();
41
+ },
42
+ });
43
+
44
+ protected readonly recoverySweep = $job({
45
+ cron: "*/5 * * * *",
46
+ lock: true,
47
+ handler: async () => {
48
+ await this.workflowProvider.recoverySweep();
49
+ },
50
+ });
51
+
52
+ protected readonly onStart = $hook({
53
+ on: "start",
54
+ handler: async () => {
55
+ // Wire up queue dispatch
56
+ this.workflowProvider.stepDispatch = async (
57
+ workflowId,
58
+ stepName,
59
+ priority,
60
+ ) => {
61
+ const priorityMap: Record<
62
+ number,
63
+ "critical" | "high" | "normal" | "low"
64
+ > = {
65
+ 0: "critical",
66
+ 1: "high",
67
+ 2: "normal",
68
+ 3: "low",
69
+ };
70
+ await this.dispatchStep.push(
71
+ { workflowId, stepName },
72
+ { priority: priorityMap[priority] ?? "normal" },
73
+ );
74
+ };
75
+ },
76
+ });
77
+ }