@hazeljs/flow 0.2.0-beta.75

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 (117) hide show
  1. package/LICENSE +192 -0
  2. package/README.md +111 -0
  3. package/dist/decorators/flow.decorators.d.ts +32 -0
  4. package/dist/decorators/flow.decorators.d.ts.map +1 -0
  5. package/dist/decorators/flow.decorators.js +100 -0
  6. package/dist/dsl/flow.d.ts +19 -0
  7. package/dist/dsl/flow.d.ts.map +1 -0
  8. package/dist/dsl/flow.js +46 -0
  9. package/dist/dsl/types.d.ts +5 -0
  10. package/dist/dsl/types.d.ts.map +1 -0
  11. package/dist/dsl/types.js +2 -0
  12. package/dist/engine/Executor.d.ts +11 -0
  13. package/dist/engine/Executor.d.ts.map +1 -0
  14. package/dist/engine/Executor.js +113 -0
  15. package/dist/engine/FlowEngine.d.ts +46 -0
  16. package/dist/engine/FlowEngine.d.ts.map +1 -0
  17. package/dist/engine/FlowEngine.js +237 -0
  18. package/dist/engine/Idempotency.d.ts +10 -0
  19. package/dist/engine/Idempotency.d.ts.map +1 -0
  20. package/dist/engine/Idempotency.js +16 -0
  21. package/dist/engine/Locks.d.ts +11 -0
  22. package/dist/engine/Locks.d.ts.map +1 -0
  23. package/dist/engine/Locks.js +68 -0
  24. package/dist/engine/Retry.d.ts +7 -0
  25. package/dist/engine/Retry.d.ts.map +1 -0
  26. package/dist/engine/Retry.js +22 -0
  27. package/dist/engine/Timeout.d.ts +10 -0
  28. package/dist/engine/Timeout.d.ts.map +1 -0
  29. package/dist/engine/Timeout.js +25 -0
  30. package/dist/engine/Transition.d.ts +6 -0
  31. package/dist/engine/Transition.d.ts.map +1 -0
  32. package/dist/engine/Transition.js +19 -0
  33. package/dist/engine/utils.d.ts +5 -0
  34. package/dist/engine/utils.d.ts.map +1 -0
  35. package/dist/engine/utils.js +25 -0
  36. package/dist/examples/branching.d.ts +2 -0
  37. package/dist/examples/branching.d.ts.map +1 -0
  38. package/dist/examples/branching.js +72 -0
  39. package/dist/examples/simple.d.ts +2 -0
  40. package/dist/examples/simple.d.ts.map +1 -0
  41. package/dist/examples/simple.js +65 -0
  42. package/dist/examples/wait_resume.d.ts +2 -0
  43. package/dist/examples/wait_resume.d.ts.map +1 -0
  44. package/dist/examples/wait_resume.js +75 -0
  45. package/dist/generated/prisma/client.d.ts +1 -0
  46. package/dist/generated/prisma/client.js +5 -0
  47. package/dist/generated/prisma/default.d.ts +1 -0
  48. package/dist/generated/prisma/default.js +5 -0
  49. package/dist/generated/prisma/edge.d.ts +1 -0
  50. package/dist/generated/prisma/edge.js +250 -0
  51. package/dist/generated/prisma/index-browser.js +236 -0
  52. package/dist/generated/prisma/index.d.ts +6577 -0
  53. package/dist/generated/prisma/index.js +271 -0
  54. package/dist/generated/prisma/libquery_engine-debian-openssl-3.0.x.so.node +0 -0
  55. package/dist/generated/prisma/package.json +183 -0
  56. package/dist/generated/prisma/query_engine_bg.js +2 -0
  57. package/dist/generated/prisma/query_engine_bg.wasm +0 -0
  58. package/dist/generated/prisma/runtime/edge-esm.js +35 -0
  59. package/dist/generated/prisma/runtime/edge.js +35 -0
  60. package/dist/generated/prisma/runtime/index-browser.d.ts +370 -0
  61. package/dist/generated/prisma/runtime/index-browser.js +17 -0
  62. package/dist/generated/prisma/runtime/library.d.ts +3982 -0
  63. package/dist/generated/prisma/runtime/library.js +147 -0
  64. package/dist/generated/prisma/runtime/react-native.js +84 -0
  65. package/dist/generated/prisma/runtime/wasm-compiler-edge.js +85 -0
  66. package/dist/generated/prisma/runtime/wasm-engine-edge.js +38 -0
  67. package/dist/generated/prisma/schema.prisma +70 -0
  68. package/dist/generated/prisma/wasm-edge-light-loader.mjs +5 -0
  69. package/dist/generated/prisma/wasm-worker-loader.mjs +5 -0
  70. package/dist/generated/prisma/wasm.d.ts +1 -0
  71. package/dist/generated/prisma/wasm.js +257 -0
  72. package/dist/index.d.ts +14 -0
  73. package/dist/index.d.ts.map +1 -0
  74. package/dist/index.js +25 -0
  75. package/dist/persistence/FlowDefinitionRepo.d.ts +14 -0
  76. package/dist/persistence/FlowDefinitionRepo.d.ts.map +1 -0
  77. package/dist/persistence/FlowDefinitionRepo.js +44 -0
  78. package/dist/persistence/FlowEventRepo.d.ts +15 -0
  79. package/dist/persistence/FlowEventRepo.d.ts.map +1 -0
  80. package/dist/persistence/FlowEventRepo.js +33 -0
  81. package/dist/persistence/FlowRunRepo.d.ts +12 -0
  82. package/dist/persistence/FlowRunRepo.d.ts.map +1 -0
  83. package/dist/persistence/FlowRunRepo.js +50 -0
  84. package/dist/persistence/IdempotencyRepo.d.ts +15 -0
  85. package/dist/persistence/IdempotencyRepo.d.ts.map +1 -0
  86. package/dist/persistence/IdempotencyRepo.js +39 -0
  87. package/dist/persistence/memory.d.ts +9 -0
  88. package/dist/persistence/memory.d.ts.map +1 -0
  89. package/dist/persistence/memory.js +154 -0
  90. package/dist/persistence/prisma.d.ts +5 -0
  91. package/dist/persistence/prisma.d.ts.map +1 -0
  92. package/dist/persistence/prisma.js +9 -0
  93. package/dist/persistence/prismaClient.d.ts +21 -0
  94. package/dist/persistence/prismaClient.d.ts.map +1 -0
  95. package/dist/persistence/prismaClient.js +47 -0
  96. package/dist/persistence/prismaStorage.d.ts +11 -0
  97. package/dist/persistence/prismaStorage.d.ts.map +1 -0
  98. package/dist/persistence/prismaStorage.js +24 -0
  99. package/dist/persistence/serialize.d.ts +6 -0
  100. package/dist/persistence/serialize.d.ts.map +1 -0
  101. package/dist/persistence/serialize.js +28 -0
  102. package/dist/persistence/storage.d.ts +75 -0
  103. package/dist/persistence/storage.d.ts.map +1 -0
  104. package/dist/persistence/storage.js +6 -0
  105. package/dist/prisma.d.ts +14 -0
  106. package/dist/prisma.d.ts.map +1 -0
  107. package/dist/prisma.js +25 -0
  108. package/dist/types/Errors.d.ts +21 -0
  109. package/dist/types/Errors.d.ts.map +1 -0
  110. package/dist/types/Errors.js +44 -0
  111. package/dist/types/Events.d.ts +17 -0
  112. package/dist/types/Events.d.ts.map +1 -0
  113. package/dist/types/Events.js +5 -0
  114. package/dist/types/FlowTypes.d.ts +66 -0
  115. package/dist/types/FlowTypes.d.ts.map +1 -0
  116. package/dist/types/FlowTypes.js +5 -0
  117. package/package.json +72 -0
package/LICENSE ADDED
@@ -0,0 +1,192 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ Copyright 2024 HazelJS Team
6
+
7
+ Licensed under the Apache License, Version 2.0 (the "License");
8
+ you may not use this file except in compliance with the License.
9
+ You may obtain a copy of the License at
10
+
11
+ http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ Unless required by applicable law or agreed to in writing, software
14
+ distributed under the License is distributed on an "AS IS" BASIS,
15
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ See the License for the specific language governing permissions and
17
+ limitations under the License.
18
+
19
+ ---
20
+
21
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
22
+
23
+ 1. Definitions.
24
+
25
+ "License" shall mean the terms and conditions for use, reproduction,
26
+ and distribution as defined by Sections 1 through 9 of this document.
27
+
28
+ "Licensor" shall mean the copyright owner or entity authorized by
29
+ the copyright owner that is granting the License.
30
+
31
+ "Legal Entity" shall mean the union of the acting entity and all
32
+ other entities that control, are controlled by, or are under common
33
+ control with that entity. For the purposes of this definition,
34
+ "control" means (i) the power, direct or indirect, to cause the
35
+ direction or management of such entity, whether by contract or
36
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
37
+ outstanding shares, or (iii) beneficial ownership of such entity.
38
+
39
+ "You" (or "Your") shall mean an individual or Legal Entity
40
+ exercising permissions granted by this License.
41
+
42
+ "Source" form shall mean the preferred form for making modifications,
43
+ including but not limited to software source code, documentation
44
+ source, and configuration files.
45
+
46
+ "Object" form shall mean any form resulting from mechanical
47
+ transformation or translation of a Source form, including but
48
+ not limited to compiled object code, generated documentation,
49
+ and conversions to other media types.
50
+
51
+ "Work" shall mean the work of authorship, whether in Source or
52
+ Object form, made available under the License, as indicated by a
53
+ copyright notice that is included in or attached to the work
54
+ (an example is provided in the Appendix below).
55
+
56
+ "Derivative Works" shall mean any work, whether in Source or Object
57
+ form, that is based on (or derived from) the Work and for which the
58
+ editorial revisions, annotations, elaborations, or other modifications
59
+ represent, as a whole, an original work of authorship. For the purposes
60
+ of this License, Derivative Works shall not include works that remain
61
+ separable from, or merely link (or bind by name) to the interfaces of,
62
+ the Work and Derivative Works thereof.
63
+
64
+ "Contribution" shall mean any work of authorship, including
65
+ the original version of the Work and any modifications or additions
66
+ to that Work or Derivative Works thereof, that is intentionally
67
+ submitted to Licensor for inclusion in the Work by the copyright owner
68
+ or by an individual or Legal Entity authorized to submit on behalf of
69
+ the copyright owner. For the purposes of this definition, "submitted"
70
+ means any form of electronic, verbal, or written communication sent
71
+ to the Licensor or its representatives, including but not limited to
72
+ communication on electronic mailing lists, source code control systems,
73
+ and issue tracking systems that are managed by, or on behalf of, the
74
+ Licensor for the purpose of discussing and improving the Work, but
75
+ excluding communication that is conspicuously marked or otherwise
76
+ designated in writing by the copyright owner as "Not a Contribution."
77
+
78
+ "Contributor" shall mean Licensor and any individual or Legal Entity
79
+ on behalf of whom a Contribution has been received by Licensor and
80
+ subsequently incorporated within the Work.
81
+
82
+ 2. Grant of Copyright License. Subject to the terms and conditions of
83
+ this License, each Contributor hereby grants to You a perpetual,
84
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
85
+ copyright license to reproduce, prepare Derivative Works of,
86
+ publicly display, publicly perform, sublicense, and distribute the
87
+ Work and such Derivative Works in Source or Object form.
88
+
89
+ 3. Grant of Patent License. Subject to the terms and conditions of
90
+ this License, each Contributor hereby grants to You a perpetual,
91
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
92
+ (except as stated in this section) patent license to make, have made,
93
+ use, offer to sell, sell, import, and otherwise transfer the Work,
94
+ where such license applies only to those patent claims licensable
95
+ by such Contributor that are necessarily infringed by their
96
+ Contribution(s) alone or by combination of their Contribution(s)
97
+ with the Work to which such Contribution(s) was submitted. If You
98
+ institute patent litigation against any entity (including a
99
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
100
+ or a Contribution incorporated within the Work constitutes direct
101
+ or contributory patent infringement, then any patent licenses
102
+ granted to You under this License for that Work shall terminate
103
+ as of the date such litigation is filed.
104
+
105
+ 4. Redistribution. You may reproduce and distribute copies of the
106
+ Work or Derivative Works thereof in any medium, with or without
107
+ modifications, and in Source or Object form, provided that You
108
+ meet the following conditions:
109
+
110
+ (a) You must give any other recipients of the Work or
111
+ Derivative Works a copy of this License; and
112
+
113
+ (b) You must cause any modified files to carry prominent notices
114
+ stating that You changed the files; and
115
+
116
+ (c) You must retain, in the Source form of any Derivative Works
117
+ that You distribute, all copyright, patent, trademark, and
118
+ attribution notices from the Source form of the Work,
119
+ excluding those notices that do not pertain to any part of
120
+ the Derivative Works; and
121
+
122
+ (d) If the Work includes a "NOTICE" text file as part of its
123
+ distribution, then any Derivative Works that You distribute must
124
+ include a readable copy of the attribution notices contained
125
+ within such NOTICE file, excluding those notices that do not
126
+ pertain to any part of the Derivative Works, in at least one
127
+ of the following places: within a NOTICE text file distributed
128
+ as part of the Derivative Works; within the Source form or
129
+ documentation, if provided along with the Derivative Works; or,
130
+ within a display generated by the Derivative Works, if and
131
+ wherever such third-party notices normally appear. The contents
132
+ of the NOTICE file are for informational purposes only and
133
+ do not modify the License. You may add Your own attribution
134
+ notices within Derivative Works that You distribute, alongside
135
+ or as an addendum to the NOTICE text from the Work, provided
136
+ that such additional attribution notices cannot be construed
137
+ as modifying the License.
138
+
139
+ You may add Your own copyright statement to Your modifications and
140
+ may provide additional or different license terms and conditions
141
+ for use, reproduction, or distribution of Your modifications, or
142
+ for any such Derivative Works as a whole, provided Your use,
143
+ reproduction, and distribution of the Work otherwise complies with
144
+ the conditions stated in this License.
145
+
146
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
147
+ any Contribution intentionally submitted for inclusion in the Work
148
+ by You to the Licensor shall be under the terms and conditions of
149
+ this License, without any additional terms or conditions.
150
+ Notwithstanding the above, nothing herein shall supersede or modify
151
+ the terms of any separate license agreement you may have executed
152
+ with Licensor regarding such Contributions.
153
+
154
+ 6. Trademarks. This License does not grant permission to use the trade
155
+ names, trademarks, service marks, or product names of the Licensor,
156
+ except as required for reasonable and customary use in describing the
157
+ origin of the Work and reproducing the content of the NOTICE file.
158
+
159
+ 7. Disclaimer of Warranty. Unless required by applicable law or
160
+ agreed to in writing, Licensor provides the Work (and each
161
+ Contributor provides its Contributions) on an "AS IS" BASIS,
162
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
163
+ implied, including, without limitation, any warranties or conditions
164
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
165
+ PARTICULAR PURPOSE. You are solely responsible for determining the
166
+ appropriateness of using or redistributing the Work and assume any
167
+ risks associated with Your exercise of permissions under this License.
168
+
169
+ 8. Limitation of Liability. In no event and under no legal theory,
170
+ whether in tort (including negligence), contract, or otherwise,
171
+ unless required by applicable law (such as deliberate and grossly
172
+ negligent acts) or agreed to in writing, shall any Contributor be
173
+ liable to You for damages, including any direct, indirect, special,
174
+ incidental, or consequential damages of any character arising as a
175
+ result of this License or out of the use or inability to use the
176
+ Work (including but not limited to damages for loss of goodwill,
177
+ work stoppage, computer failure or malfunction, or any and all
178
+ other commercial damages or losses), even if such Contributor
179
+ has been advised of the possibility of such damages.
180
+
181
+ 9. Accepting Warranty or Additional Liability. While redistributing
182
+ the Work or Derivative Works thereof, You may choose to offer,
183
+ and charge a fee for, acceptance of support, warranty, indemnity,
184
+ or other liability obligations and/or rights consistent with this
185
+ License. However, in accepting such obligations, You may act only
186
+ on Your own behalf and on Your sole responsibility, not on behalf
187
+ of any other Contributor, and only if You agree to indemnify,
188
+ defend, and hold each Contributor harmless for any liability
189
+ incurred by, or claims asserted against, such Contributor by reason
190
+ of your accepting any such warranty or additional liability.
191
+
192
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,111 @@
1
+ # @hazeljs/flow
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@hazeljs/flow.svg)](https://www.npmjs.com/package/@hazeljs/flow)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@hazeljs/flow)](https://www.npmjs.com/package/@hazeljs/flow)
5
+ [![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
6
+
7
+ Durable execution graph engine ("workflow OS kernel"). Fully independent of Hazel core.
8
+
9
+ **Storage:** By default the engine uses **in-memory storage** (no database). For durable persistence, install `@prisma/client` and use the Prisma storage adapter (see [Persistence](#persistence)).
10
+
11
+ ## Features
12
+
13
+ - Flow definitions (nodes + edges)
14
+ - Flow runs with stateful execution
15
+ - **In-memory by default**; optional Postgres persistence via Prisma
16
+ - Audit event timeline
17
+ - Retry/backoff, timeouts
18
+ - Idempotency keys
19
+ - WAIT state + resume
20
+ - Optional Postgres advisory locks when using Prisma storage
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ pnpm add @hazeljs/flow
26
+ ```
27
+
28
+ No database or env vars are required for the default in-memory mode.
29
+
30
+ ## Usage
31
+
32
+ ### Decorator-based (recommended)
33
+
34
+ ```typescript
35
+ import { FlowEngine, Flow, Entry, Node, Edge, buildFlowDefinition } from '@hazeljs/flow';
36
+ import type { FlowContext, NodeResult } from '@hazeljs/flow';
37
+
38
+ @Flow('my-flow', '1.0.0')
39
+ class MyFlow {
40
+ @Entry()
41
+ @Node('start')
42
+ @Edge('end')
43
+ async start(): Promise<NodeResult> {
44
+ return { status: 'ok', output: 1 };
45
+ }
46
+
47
+ @Node('end')
48
+ async end(ctx: FlowContext): Promise<NodeResult> {
49
+ return { status: 'ok', output: ctx.outputs.start };
50
+ }
51
+ }
52
+
53
+ const engine = new FlowEngine();
54
+ const def = buildFlowDefinition(MyFlow);
55
+ await engine.registerDefinition(def);
56
+
57
+ const { runId } = await engine.startRun({
58
+ flowId: 'my-flow',
59
+ version: '1.0.0',
60
+ input: {},
61
+ });
62
+
63
+ let run = await engine.getRun(runId);
64
+ while (run?.status === 'RUNNING') {
65
+ run = await engine.tick(runId);
66
+ }
67
+ ```
68
+
69
+ ### Functional builder (alternative)
70
+
71
+ ```typescript
72
+ import { FlowEngine, flow } from '@hazeljs/flow';
73
+
74
+ const def = flow('my-flow', '1.0.0')
75
+ .entry('start')
76
+ .node('start', async (ctx) => ({ status: 'ok', output: 1 }))
77
+ .node('end', async (ctx) => ({ status: 'ok', output: ctx.outputs.start }))
78
+ .edge('start', 'end')
79
+ .build();
80
+ ```
81
+
82
+ ## Persistence
83
+
84
+ By default the engine uses in-memory storage. To persist runs and definitions to Postgres:
85
+
86
+ 1. Install Prisma client: `pnpm add @prisma/client`
87
+ 2. Set `DATABASE_URL` and run migrations (from this package’s `prisma/` schema).
88
+ 3. Create the engine with Prisma storage:
89
+
90
+ ```typescript
91
+ import { FlowEngine, buildFlowDefinition } from '@hazeljs/flow';
92
+ import { createPrismaStorage, createFlowPrismaClient } from '@hazeljs/flow/prisma';
93
+
94
+ const prisma = createFlowPrismaClient();
95
+ const engine = new FlowEngine({ storage: createPrismaStorage(prisma) });
96
+ // ... register definitions, start runs, etc.
97
+ ```
98
+
99
+ The migration SQL in `prisma/migrations/` is only needed when you use this adapter.
100
+
101
+ ## Scripts
102
+
103
+ - `prisma:generate` - Generate Prisma client
104
+ - `prisma:migrate` - Run migrations (dev)
105
+ - `prisma:deploy` - Deploy migrations (prod)
106
+ - `prisma:studio` - Open Prisma Studio
107
+ - `test` - Run Vitest tests
108
+
109
+ ## License
110
+
111
+ Apache 2.0 © [HazelJS](https://hazeljs.com)
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Decorator-based flow definitions
3
+ */
4
+ import 'reflect-metadata';
5
+ import type { FlowDefinition, FlowContext, RetryPolicy } from '../types/FlowTypes.js';
6
+ export interface NodeDecoratorOptions {
7
+ name?: string;
8
+ retry?: RetryPolicy;
9
+ timeoutMs?: number;
10
+ idempotencyKey?: (ctx: FlowContext) => string;
11
+ }
12
+ /**
13
+ * Mark a class as a flow definition
14
+ */
15
+ export declare function Flow(flowId: string, version: string): ClassDecorator;
16
+ /**
17
+ * Mark a node as the entry point
18
+ */
19
+ export declare function Entry(): MethodDecorator;
20
+ /**
21
+ * Mark a method as a flow node. Method name is used as nodeId if not provided.
22
+ */
23
+ export declare function Node(nodeIdOrOptions?: string | NodeDecoratorOptions, options?: NodeDecoratorOptions): MethodDecorator;
24
+ /**
25
+ * Add an edge from this node to another. Stack multiple for branching.
26
+ */
27
+ export declare function Edge(to: string, when?: (ctx: FlowContext) => boolean, priority?: number): MethodDecorator;
28
+ /**
29
+ * Build a FlowDefinition from a decorated flow class
30
+ */
31
+ export declare function buildFlowDefinition(FlowClass: new () => object): FlowDefinition;
32
+ //# sourceMappingURL=flow.decorators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow.decorators.d.ts","sourceRoot":"","sources":["../../src/decorators/flow.decorators.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EACV,cAAc,EAGd,WAAW,EAEX,WAAW,EACZ,MAAM,uBAAuB,CAAC;AAO/B,MAAM,WAAW,oBAAoB;IACnC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,WAAW,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,MAAM,CAAC;CAC/C;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,cAAc,CAIpE;AAED;;GAEG;AACH,wBAAgB,KAAK,IAAI,eAAe,CAIvC;AAED;;GAEG;AACH,wBAAgB,IAAI,CAClB,eAAe,CAAC,EAAE,MAAM,GAAG,oBAAoB,EAC/C,OAAO,CAAC,EAAE,oBAAoB,GAC7B,eAAe,CAQjB;AAED;;GAEG;AACH,wBAAgB,IAAI,CAClB,EAAE,EAAE,MAAM,EACV,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,EACpC,QAAQ,CAAC,EAAE,MAAM,GAChB,eAAe,CAQjB;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,UAAU,MAAM,GAAG,cAAc,CAuD/E"}
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Flow = Flow;
4
+ exports.Entry = Entry;
5
+ exports.Node = Node;
6
+ exports.Edge = Edge;
7
+ exports.buildFlowDefinition = buildFlowDefinition;
8
+ /**
9
+ * Decorator-based flow definitions
10
+ */
11
+ require("reflect-metadata");
12
+ const FLOW_METADATA_KEY = 'flow:definition';
13
+ const NODE_METADATA_KEY = 'flow:nodes';
14
+ const EDGE_METADATA_KEY = 'flow:edges';
15
+ const ENTRY_METADATA_KEY = 'flow:entry';
16
+ /**
17
+ * Mark a class as a flow definition
18
+ */
19
+ function Flow(flowId, version) {
20
+ return (target) => {
21
+ Reflect.defineMetadata(FLOW_METADATA_KEY, { flowId, version }, target);
22
+ };
23
+ }
24
+ /**
25
+ * Mark a node as the entry point
26
+ */
27
+ function Entry() {
28
+ return (target, propertyKey) => {
29
+ Reflect.defineMetadata(ENTRY_METADATA_KEY, true, target, propertyKey);
30
+ };
31
+ }
32
+ /**
33
+ * Mark a method as a flow node. Method name is used as nodeId if not provided.
34
+ */
35
+ function Node(nodeIdOrOptions, options) {
36
+ const nodeId = typeof nodeIdOrOptions === 'string' ? nodeIdOrOptions : undefined;
37
+ const opts = (typeof nodeIdOrOptions === 'object' ? nodeIdOrOptions : options) ?? {};
38
+ return (target, propertyKey, descriptor) => {
39
+ const id = nodeId ?? (typeof propertyKey === 'string' ? propertyKey : String(propertyKey));
40
+ Reflect.defineMetadata(NODE_METADATA_KEY, { id, options: opts }, target, propertyKey);
41
+ return descriptor;
42
+ };
43
+ }
44
+ /**
45
+ * Add an edge from this node to another. Stack multiple for branching.
46
+ */
47
+ function Edge(to, when, priority) {
48
+ return (target, propertyKey) => {
49
+ const from = typeof propertyKey === 'string' ? propertyKey : String(propertyKey);
50
+ const edges = Reflect.getMetadata(EDGE_METADATA_KEY, target, propertyKey) ?? [];
51
+ edges.push({ from, to, when, priority: priority ?? 0 });
52
+ Reflect.defineMetadata(EDGE_METADATA_KEY, edges, target, propertyKey);
53
+ };
54
+ }
55
+ /**
56
+ * Build a FlowDefinition from a decorated flow class
57
+ */
58
+ function buildFlowDefinition(FlowClass) {
59
+ const flowMeta = Reflect.getMetadata(FLOW_METADATA_KEY, FlowClass);
60
+ if (!flowMeta) {
61
+ throw new Error(`Class ${FlowClass.name} is not decorated with @Flow(flowId, version)`);
62
+ }
63
+ const { flowId, version } = flowMeta;
64
+ const prototype = FlowClass.prototype;
65
+ const nodes = {};
66
+ const edges = [];
67
+ let entryNodeId = null;
68
+ const methodNames = Object.getOwnPropertyNames(prototype).filter((k) => k !== 'constructor' && typeof prototype[k] === 'function');
69
+ const instance = new FlowClass();
70
+ for (const methodName of methodNames) {
71
+ const nodeMeta = Reflect.getMetadata(NODE_METADATA_KEY, prototype, methodName);
72
+ if (!nodeMeta)
73
+ continue;
74
+ const { id: nodeId, options } = nodeMeta;
75
+ const handler = prototype[methodName].bind(instance);
76
+ nodes[nodeId] = {
77
+ id: nodeId,
78
+ name: options?.name,
79
+ handler: handler,
80
+ retry: options?.retry,
81
+ timeoutMs: options?.timeoutMs,
82
+ idempotencyKey: options?.idempotencyKey,
83
+ };
84
+ if (Reflect.getMetadata(ENTRY_METADATA_KEY, prototype, methodName)) {
85
+ entryNodeId = nodeId;
86
+ }
87
+ const nodeEdges = Reflect.getMetadata(EDGE_METADATA_KEY, prototype, methodName) ?? [];
88
+ edges.push(...nodeEdges);
89
+ }
90
+ if (!entryNodeId) {
91
+ throw new Error(`Flow ${flowId}@${version} must have exactly one @Entry() node`);
92
+ }
93
+ return {
94
+ flowId,
95
+ version,
96
+ entry: entryNodeId,
97
+ nodes,
98
+ edges,
99
+ };
100
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Fluent DSL builder for flow definitions
3
+ */
4
+ import type { FlowDefinition, FlowContext, NodeResult, RetryPolicy } from '../types/FlowTypes.js';
5
+ type NodeId = string;
6
+ interface FlowBuilder {
7
+ entry(nodeId: NodeId): FlowBuilder;
8
+ node(nodeId: NodeId, handler: (ctx: FlowContext) => Promise<NodeResult>, options?: {
9
+ name?: string;
10
+ retry?: RetryPolicy;
11
+ timeoutMs?: number;
12
+ idempotencyKey?: (ctx: FlowContext) => string;
13
+ }): FlowBuilder;
14
+ edge(from: NodeId, to: NodeId, when?: (ctx: FlowContext) => boolean, priority?: number): FlowBuilder;
15
+ build(): FlowDefinition;
16
+ }
17
+ export declare function flow(flowId: string, version: string): FlowBuilder;
18
+ export {};
19
+ //# sourceMappingURL=flow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["../../src/dsl/flow.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EACV,cAAc,EAGd,WAAW,EACX,UAAU,EACV,WAAW,EACZ,MAAM,uBAAuB,CAAC;AAE/B,KAAK,MAAM,GAAG,MAAM,CAAC;AAErB,UAAU,WAAW;IACnB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAAC;IACnC,IAAI,CACF,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,UAAU,CAAC,EAClD,OAAO,CAAC,EAAE;QACR,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,KAAK,CAAC,EAAE,WAAW,CAAC;QACpB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,cAAc,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,MAAM,CAAC;KAC/C,GACA,WAAW,CAAC;IACf,IAAI,CACF,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,IAAI,CAAC,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,EACpC,QAAQ,CAAC,EAAE,MAAM,GAChB,WAAW,CAAC;IACf,KAAK,IAAI,cAAc,CAAC;CACzB;AAED,wBAAgB,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,WAAW,CA4DjE"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flow = flow;
4
+ function flow(flowId, version) {
5
+ const nodes = {};
6
+ const edges = [];
7
+ let entryNodeId = null;
8
+ return {
9
+ entry(nodeId) {
10
+ entryNodeId = nodeId;
11
+ return this;
12
+ },
13
+ node(nodeId, handler, options) {
14
+ nodes[nodeId] = {
15
+ id: nodeId,
16
+ name: options?.name,
17
+ handler,
18
+ retry: options?.retry,
19
+ timeoutMs: options?.timeoutMs,
20
+ idempotencyKey: options?.idempotencyKey,
21
+ };
22
+ return this;
23
+ },
24
+ edge(from, to, when, priority) {
25
+ edges.push({
26
+ from,
27
+ to,
28
+ when,
29
+ priority: priority ?? 0,
30
+ });
31
+ return this;
32
+ },
33
+ build() {
34
+ if (!entryNodeId) {
35
+ throw new Error('Flow must have an entry node. Call .entry(nodeId) first.');
36
+ }
37
+ return {
38
+ flowId,
39
+ version,
40
+ entry: entryNodeId,
41
+ nodes,
42
+ edges,
43
+ };
44
+ },
45
+ };
46
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Re-export DSL-related types
3
+ */
4
+ export type { FlowDefinition, NodeDefinition, EdgeDefinition, FlowContext, NodeResult, RetryPolicy, } from '../types/FlowTypes.js';
5
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/dsl/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,YAAY,EACV,cAAc,EACd,cAAc,EACd,cAAc,EACd,WAAW,EACX,UAAU,EACV,WAAW,GACZ,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Executes a single node within a flow run
3
+ */
4
+ import type { FlowContext, NodeDefinition, NodeResult } from '../types/FlowTypes.js';
5
+ import type { IFlowEventRepo, IIdempotencyRepo } from '../persistence/storage.js';
6
+ export declare function executeNode(node: NodeDefinition, ctx: FlowContext, eventRepo: IFlowEventRepo, idempotencyRepo: IIdempotencyRepo): Promise<{
7
+ result: NodeResult;
8
+ cached: boolean;
9
+ }>;
10
+ export declare function applyPatch(state: Record<string, unknown>, patch?: Record<string, unknown>): Record<string, unknown>;
11
+ //# sourceMappingURL=Executor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Executor.d.ts","sourceRoot":"","sources":["../../src/engine/Executor.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACrF,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAQlF,wBAAsB,WAAW,CAC/B,IAAI,EAAE,cAAc,EACpB,GAAG,EAAE,WAAW,EAChB,SAAS,EAAE,cAAc,EACzB,eAAe,EAAE,gBAAgB,GAChC,OAAO,CAAC;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC,CAkHlD;AAED,wBAAgB,UAAU,CACxB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9B,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAGzB"}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executeNode = executeNode;
4
+ exports.applyPatch = applyPatch;
5
+ const Idempotency_js_1 = require("./Idempotency.js");
6
+ const Timeout_js_1 = require("./Timeout.js");
7
+ const Retry_js_1 = require("./Retry.js");
8
+ const utils_js_1 = require("./utils.js");
9
+ const RETRYABLE_ERROR_CODES = new Set(['TIMEOUT', 'NETWORK_ERROR', 'RATE_LIMIT']);
10
+ async function executeNode(node, ctx, eventRepo, idempotencyRepo) {
11
+ const attempt = (ctx.meta.attempts[node.id] ?? 0) + 1;
12
+ ctx.meta.attempts[node.id] = attempt;
13
+ await eventRepo.append(ctx.runId, 'NODE_STARTED', { nodeId: node.id, attempt });
14
+ // Idempotency check
15
+ if (node.idempotencyKey) {
16
+ const key = node.idempotencyKey(ctx);
17
+ const cached = await (0, Idempotency_js_1.checkIdempotency)(idempotencyRepo, key);
18
+ if (cached) {
19
+ await eventRepo.append(ctx.runId, 'NODE_FINISHED', {
20
+ nodeId: node.id,
21
+ attempt,
22
+ cached: true,
23
+ });
24
+ return {
25
+ result: {
26
+ status: 'ok',
27
+ output: cached.output,
28
+ patch: cached.patch,
29
+ },
30
+ cached: true,
31
+ };
32
+ }
33
+ }
34
+ let lastError = null;
35
+ const maxAttempts = node.retry?.maxAttempts ?? 1;
36
+ for (let a = 0; a < maxAttempts; a++) {
37
+ try {
38
+ let handlerPromise = node.handler(ctx);
39
+ if (node.timeoutMs) {
40
+ handlerPromise = (0, Timeout_js_1.withTimeout)(handlerPromise, node.timeoutMs, node.id);
41
+ }
42
+ const result = await handlerPromise;
43
+ if (result.status === 'ok') {
44
+ if (node.idempotencyKey) {
45
+ const key = node.idempotencyKey(ctx);
46
+ await (0, Idempotency_js_1.storeIdempotency)(idempotencyRepo, key, ctx.runId, node.id, result.output, result.patch);
47
+ }
48
+ await eventRepo.append(ctx.runId, 'NODE_FINISHED', {
49
+ nodeId: node.id,
50
+ attempt: a + 1,
51
+ cached: false,
52
+ });
53
+ return { result, cached: false };
54
+ }
55
+ if (result.status === 'wait') {
56
+ await eventRepo.append(ctx.runId, 'NODE_FINISHED', {
57
+ nodeId: node.id,
58
+ attempt: a + 1,
59
+ cached: false,
60
+ });
61
+ return { result, cached: false };
62
+ }
63
+ // result.status === 'error'
64
+ lastError = result.error;
65
+ await eventRepo.append(ctx.runId, 'NODE_FAILED', {
66
+ nodeId: node.id,
67
+ attempt: a + 1,
68
+ error: result.error,
69
+ });
70
+ const retryable = result.error.retryable ?? RETRYABLE_ERROR_CODES.has(result.error.code);
71
+ if (!retryable || a === maxAttempts - 1) {
72
+ return { result, cached: false };
73
+ }
74
+ const delayMs = node.retry ? (0, Retry_js_1.getRetryDelayMs)(a, node.retry) : -1;
75
+ if (delayMs > 0) {
76
+ await (0, Retry_js_1.delay)(delayMs);
77
+ }
78
+ }
79
+ catch (err) {
80
+ const message = err instanceof Error ? err.message : String(err);
81
+ const code = err instanceof Error && 'code' in err ? String(err.code) : 'UNKNOWN';
82
+ lastError = { code, message, retryable: code === 'TIMEOUT' };
83
+ await eventRepo.append(ctx.runId, 'NODE_FAILED', {
84
+ nodeId: node.id,
85
+ attempt: a + 1,
86
+ error: lastError,
87
+ });
88
+ if (code === 'TIMEOUT' && node.retry && a < maxAttempts - 1) {
89
+ const delayMs = (0, Retry_js_1.getRetryDelayMs)(a, node.retry);
90
+ if (delayMs > 0)
91
+ await (0, Retry_js_1.delay)(delayMs);
92
+ }
93
+ else {
94
+ return {
95
+ result: { status: 'error', error: lastError },
96
+ cached: false,
97
+ };
98
+ }
99
+ }
100
+ }
101
+ return {
102
+ result: {
103
+ status: 'error',
104
+ error: lastError ?? { code: 'UNKNOWN', message: 'Max retries exceeded' },
105
+ },
106
+ cached: false,
107
+ };
108
+ }
109
+ function applyPatch(state, patch) {
110
+ if (!patch || Object.keys(patch).length === 0)
111
+ return state;
112
+ return (0, utils_js_1.deepMerge)(state, patch);
113
+ }