flowcraft 1.0.0-beta.1 → 1.0.0-beta.11

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 (150) hide show
  1. package/README.md +65 -17
  2. package/dist/index.d.ts +1076 -0
  3. package/dist/index.js +6 -0
  4. package/dist/index.js.map +1 -0
  5. package/package.json +12 -8
  6. package/.editorconfig +0 -9
  7. package/config/tsconfig.json +0 -21
  8. package/config/tsup.config.ts +0 -11
  9. package/config/vitest.config.ts +0 -11
  10. package/docs/.vitepress/config.ts +0 -105
  11. package/docs/api-reference/builder.md +0 -158
  12. package/docs/api-reference/fn.md +0 -142
  13. package/docs/api-reference/index.md +0 -38
  14. package/docs/api-reference/workflow.md +0 -126
  15. package/docs/guide/advanced-guides/cancellation.md +0 -117
  16. package/docs/guide/advanced-guides/composition.md +0 -68
  17. package/docs/guide/advanced-guides/custom-executor.md +0 -180
  18. package/docs/guide/advanced-guides/error-handling.md +0 -135
  19. package/docs/guide/advanced-guides/logging.md +0 -106
  20. package/docs/guide/advanced-guides/middleware.md +0 -106
  21. package/docs/guide/advanced-guides/observability.md +0 -175
  22. package/docs/guide/best-practices/debugging.md +0 -182
  23. package/docs/guide/best-practices/state-management.md +0 -120
  24. package/docs/guide/best-practices/sub-workflow-data.md +0 -95
  25. package/docs/guide/best-practices/testing.md +0 -187
  26. package/docs/guide/builders.md +0 -157
  27. package/docs/guide/functional-api.md +0 -133
  28. package/docs/guide/index.md +0 -178
  29. package/docs/guide/recipes/creating-a-loop.md +0 -113
  30. package/docs/guide/recipes/data-processing-pipeline.md +0 -123
  31. package/docs/guide/recipes/fan-out-fan-in.md +0 -112
  32. package/docs/guide/recipes/index.md +0 -15
  33. package/docs/guide/recipes/resilient-api-call.md +0 -110
  34. package/docs/guide/tooling/graph-validation.md +0 -160
  35. package/docs/guide/tooling/mermaid.md +0 -156
  36. package/docs/index.md +0 -56
  37. package/eslint.config.js +0 -16
  38. package/pnpm-workspace.yaml +0 -2
  39. package/sandbox/1.basic/README.md +0 -45
  40. package/sandbox/1.basic/package.json +0 -16
  41. package/sandbox/1.basic/src/flow.ts +0 -17
  42. package/sandbox/1.basic/src/main.ts +0 -22
  43. package/sandbox/1.basic/src/nodes.ts +0 -112
  44. package/sandbox/1.basic/src/utils.ts +0 -35
  45. package/sandbox/1.basic/tsconfig.json +0 -3
  46. package/sandbox/2.research/README.md +0 -46
  47. package/sandbox/2.research/package.json +0 -16
  48. package/sandbox/2.research/src/flow.ts +0 -14
  49. package/sandbox/2.research/src/main.ts +0 -31
  50. package/sandbox/2.research/src/nodes.ts +0 -108
  51. package/sandbox/2.research/src/utils.ts +0 -45
  52. package/sandbox/2.research/src/visualize.ts +0 -29
  53. package/sandbox/2.research/tsconfig.json +0 -3
  54. package/sandbox/3.parallel/README.md +0 -65
  55. package/sandbox/3.parallel/package.json +0 -16
  56. package/sandbox/3.parallel/src/main.ts +0 -45
  57. package/sandbox/3.parallel/src/nodes.ts +0 -43
  58. package/sandbox/3.parallel/src/utils.ts +0 -25
  59. package/sandbox/3.parallel/tsconfig.json +0 -3
  60. package/sandbox/4.dag/README.md +0 -179
  61. package/sandbox/4.dag/data/1.blog-post/100.json +0 -60
  62. package/sandbox/4.dag/data/1.blog-post/README.md +0 -25
  63. package/sandbox/4.dag/data/2.job-application/200.json +0 -103
  64. package/sandbox/4.dag/data/2.job-application/201.json +0 -31
  65. package/sandbox/4.dag/data/2.job-application/202.json +0 -31
  66. package/sandbox/4.dag/data/2.job-application/README.md +0 -58
  67. package/sandbox/4.dag/data/3.customer-review/300.json +0 -141
  68. package/sandbox/4.dag/data/3.customer-review/301.json +0 -31
  69. package/sandbox/4.dag/data/3.customer-review/302.json +0 -28
  70. package/sandbox/4.dag/data/3.customer-review/README.md +0 -71
  71. package/sandbox/4.dag/data/4.content-moderation/400.json +0 -161
  72. package/sandbox/4.dag/data/4.content-moderation/401.json +0 -47
  73. package/sandbox/4.dag/data/4.content-moderation/402.json +0 -46
  74. package/sandbox/4.dag/data/4.content-moderation/403.json +0 -31
  75. package/sandbox/4.dag/data/4.content-moderation/README.md +0 -83
  76. package/sandbox/4.dag/package.json +0 -19
  77. package/sandbox/4.dag/src/main.ts +0 -73
  78. package/sandbox/4.dag/src/nodes.ts +0 -134
  79. package/sandbox/4.dag/src/registry.ts +0 -87
  80. package/sandbox/4.dag/src/types.ts +0 -25
  81. package/sandbox/4.dag/src/utils.ts +0 -42
  82. package/sandbox/4.dag/tsconfig.json +0 -3
  83. package/sandbox/5.distributed/.env.example +0 -1
  84. package/sandbox/5.distributed/README.md +0 -88
  85. package/sandbox/5.distributed/data/1.blog-post/100.json +0 -59
  86. package/sandbox/5.distributed/data/1.blog-post/README.md +0 -25
  87. package/sandbox/5.distributed/data/2.job-application/200.json +0 -103
  88. package/sandbox/5.distributed/data/2.job-application/201.json +0 -30
  89. package/sandbox/5.distributed/data/2.job-application/202.json +0 -30
  90. package/sandbox/5.distributed/data/2.job-application/README.md +0 -58
  91. package/sandbox/5.distributed/data/3.customer-review/300.json +0 -141
  92. package/sandbox/5.distributed/data/3.customer-review/301.json +0 -31
  93. package/sandbox/5.distributed/data/3.customer-review/302.json +0 -57
  94. package/sandbox/5.distributed/data/3.customer-review/README.md +0 -71
  95. package/sandbox/5.distributed/data/4.content-moderation/400.json +0 -173
  96. package/sandbox/5.distributed/data/4.content-moderation/401.json +0 -47
  97. package/sandbox/5.distributed/data/4.content-moderation/402.json +0 -46
  98. package/sandbox/5.distributed/data/4.content-moderation/403.json +0 -31
  99. package/sandbox/5.distributed/data/4.content-moderation/README.md +0 -83
  100. package/sandbox/5.distributed/package.json +0 -20
  101. package/sandbox/5.distributed/src/client.ts +0 -124
  102. package/sandbox/5.distributed/src/executor.ts +0 -69
  103. package/sandbox/5.distributed/src/nodes.ts +0 -136
  104. package/sandbox/5.distributed/src/registry.ts +0 -101
  105. package/sandbox/5.distributed/src/types.ts +0 -45
  106. package/sandbox/5.distributed/src/utils.ts +0 -69
  107. package/sandbox/5.distributed/src/worker.ts +0 -217
  108. package/sandbox/5.distributed/tsconfig.json +0 -3
  109. package/sandbox/6.rag/.env.example +0 -1
  110. package/sandbox/6.rag/README.md +0 -60
  111. package/sandbox/6.rag/data/README.md +0 -31
  112. package/sandbox/6.rag/data/rag.json +0 -58
  113. package/sandbox/6.rag/documents/sample-cascade.txt +0 -11
  114. package/sandbox/6.rag/package.json +0 -18
  115. package/sandbox/6.rag/src/main.ts +0 -52
  116. package/sandbox/6.rag/src/nodes/GenerateEmbeddingsNode.ts +0 -54
  117. package/sandbox/6.rag/src/nodes/LLMProcessNode.ts +0 -48
  118. package/sandbox/6.rag/src/nodes/LoadAndChunkNode.ts +0 -40
  119. package/sandbox/6.rag/src/nodes/StoreInVectorDBNode.ts +0 -36
  120. package/sandbox/6.rag/src/nodes/VectorSearchNode.ts +0 -53
  121. package/sandbox/6.rag/src/nodes/index.ts +0 -28
  122. package/sandbox/6.rag/src/registry.ts +0 -23
  123. package/sandbox/6.rag/src/types.ts +0 -44
  124. package/sandbox/6.rag/src/utils.ts +0 -77
  125. package/sandbox/6.rag/tsconfig.json +0 -3
  126. package/sandbox/tsconfig.json +0 -13
  127. package/src/builder/collection.test.ts +0 -287
  128. package/src/builder/collection.ts +0 -269
  129. package/src/builder/graph.test.ts +0 -406
  130. package/src/builder/graph.ts +0 -336
  131. package/src/builder/graph.types.ts +0 -104
  132. package/src/builder/index.ts +0 -3
  133. package/src/context.ts +0 -111
  134. package/src/errors.ts +0 -34
  135. package/src/executor.ts +0 -29
  136. package/src/executors/in-memory.test.ts +0 -93
  137. package/src/executors/in-memory.ts +0 -140
  138. package/src/functions.test.ts +0 -191
  139. package/src/functions.ts +0 -117
  140. package/src/index.ts +0 -5
  141. package/src/logger.ts +0 -41
  142. package/src/types.ts +0 -75
  143. package/src/utils/graph.test.ts +0 -144
  144. package/src/utils/graph.ts +0 -182
  145. package/src/utils/index.ts +0 -3
  146. package/src/utils/mermaid.test.ts +0 -239
  147. package/src/utils/mermaid.ts +0 -133
  148. package/src/utils/sleep.ts +0 -20
  149. package/src/workflow.test.ts +0 -622
  150. package/src/workflow.ts +0 -561
package/README.md CHANGED
@@ -10,7 +10,7 @@ Build complex, multi-step processes, from simple sequences to dynamic, graph-dri
10
10
 
11
11
  - **Extensible Execution Engine**: A pluggable `Executor` pattern enables in-memory or distributed flows.
12
12
 
13
- - **Type-Safe**: Written in TypeScript to provide strong typing for your workflow definitions and context.
13
+ - **Type-Safe**: Written in TypeScript to provide strong typing for your workflow definitions, context, and node parameters.
14
14
 
15
15
  - **Async by Default**: Built on an asynchronous foundation to handle I/O-bound and CPU-bound tasks.
16
16
 
@@ -39,12 +39,11 @@ npm install flowcraft
39
39
  Create and run a simple workflow in a few lines of code.
40
40
 
41
41
  ```typescript
42
- import { Flow, Node } from 'flowcraft'
42
+ import { Flow, mapNode } from 'flowcraft' // Use mapNode for simple functions
43
43
 
44
- const greetNode = new Node()
45
- // The exec method contains the core logic of the node.
46
- .exec(() => 'Hello, World!')
47
- // Functional helpers make common tasks easy.
44
+ // Create a node from a simple function that takes params and returns a value.
45
+ const greetNode = mapNode(() => 'Hello, World!')
46
+ // Functional helpers make common tasks easy.
48
47
  .tap(console.log)
49
48
 
50
49
  const flow = new Flow(greetNode)
@@ -170,22 +169,59 @@ graph TD
170
169
 
171
170
  ### Node
172
171
 
173
- The `Node` is the fundamental building block of a workflow. It represents a single, potentially asynchronous unit of work with a three-phase lifecycle:
172
+ The `Node` is the fundamental building block of a workflow. It represents a single unit of work and is fully generic, allowing you to define types for its lifecycle results and its static parameters.
174
173
 
175
- 1. `prep(args)`: Prepare data for execution (e.g., fetch from `Context`).
176
- 2. `exec(args)`: Perform the core logic, isolated from the context.
177
- 3. `post(args)`: Process results, update the `Context`, and return an "action" string to determine the next step.
174
+ The class signature is: `Node<PrepRes, ExecRes, PostRes, TParams>`
175
+
176
+ - `PrepRes`: The type of data returned by the `prep` phase.
177
+ - `ExecRes`: The type of data returned by the `exec` phase.
178
+ - `PostRes`: The type of the "action" returned by the `post` phase.
179
+ - `TParams`: The type of the static parameters object for the node, accessible via `.withParams()`.
180
+
181
+ **Example: A Type-Safe GreetNode**
182
+
183
+ For nodes that do require static configuration, you can provide a type for the parameters to get full autocompletion and compile-time safety.
184
+
185
+ ```typescript
186
+ // 1. Define the shape of the node's parameters.
187
+ interface GreetParams {
188
+ greeting: string
189
+ loudly: boolean
190
+ }
191
+
192
+ // 2. Define the node with the TParams generic.
193
+ class GreetNode extends Node<void, string, any, GreetParams> {
194
+ // 3. The 'exec' method can safely access typed parameters.
195
+ async exec({ params }: NodeArgs<GreetParams>): Promise<string> {
196
+ let message = `${params.greeting}, World!`
197
+ if (params.loudly) {
198
+ message = message.toUpperCase()
199
+ }
200
+ return message
201
+ }
202
+ }
203
+
204
+ // 4. Instantiate and configure with type-safe .withParams()
205
+ const greetNode = new GreetNode()
206
+ .withParams({ greeting: 'Hello', loudly: true }) // Autocompletes and type-checks!
207
+ .tap(console.log) // "HELLO, WORLD!"
208
+
209
+ // TypeScript would throw an error on this line:
210
+ // greetNode.withParams({ greting: 'Hi', loudly: false }); // Property 'greting' does not exist.
211
+ ```
178
212
 
179
213
  A chainable API on the `Node` class has a set of functional helpers:
180
214
 
215
+ - `.withParams(params)`: Sets type-safe static parameters for the node.
181
216
  - `.map(fn)`: Transform the output of a node.
182
217
  - `.filter(predicate)`: Conditionally proceed based on a node's output.
183
218
  - `.tap(fn)`: Perform a side-effect without changing the output.
184
219
  - `.toContext(key)`: Store a node's result in the context.
220
+ - `.withLens(lens, value)`: Applies a context mutation before the node executes
185
221
 
186
222
  ### Flow
187
223
 
188
- A `Flow` is a special type of `Node` that orchestrates a sequence of other nodes. It contains the logic for traversing its own graph of operations, making it a powerful, self-contained unit of work.
224
+ A `Flow` is a special type of `Node` that orchestrates a sequence of other nodes. It is also generic (`Flow<PrepRes, ExecRes, TParams>`) and can be configured with its own parameters and middleware. It contains the logic for traversing its own graph of operations, making it a powerful, self-contained unit of work.
189
225
 
190
226
  ### Executor
191
227
 
@@ -217,15 +253,15 @@ To simplify the creation of common and complex patterns, the framework provides
217
253
  > For clear, focused examples of specific, individual features (like retries, middleware, cancellation, and composition), the unit tests are an excellent resource.
218
254
 
219
255
  - Core workflow tests: [`src/workflow.test.ts`](src/workflow.test.ts)
220
- - Collections tests: [`src/builder/collection.test.ts`](src/builder/collection.test.ts)
256
+ - Patterns tests: [`src/builder/patterns.test.ts`](src/builder/patterns.test.ts)
221
257
  - Graph builder tests: [`src/builder/graph.test.ts`](src/builder/graph.test.ts)
222
258
 
223
259
  ## API Reference
224
260
 
225
261
  ### Core Classes
226
262
 
227
- - `Node`: The base class for a unit of work with built-in retry logic and a fluent API (`.map`, `.filter`, etc.).
228
- - `Flow`: Orchestrates a sequence of nodes and supports middleware via `.use()`.
263
+ - `Node`: The base class for a unit of work: `Node<PrepRes, ExecRes, PostRes, TParams>`. It has built-in retry logic and a fluent API (`.map`, `.filter`, etc.).
264
+ - `Flow`: Orchestrates a sequence of nodes: `Flow<PrepRes, ExecRes, TParams>`. Supports middleware via `.use()`.
229
265
  - `InMemoryExecutor`: The default `IExecutor` implementation for running flows in-memory.
230
266
  - `TypedContext`: The standard `Map`-based implementation for the `Context` interface.
231
267
  - `ConsoleLogger`, `NullLogger`: Pre-built logger implementations.
@@ -234,16 +270,28 @@ To simplify the creation of common and complex patterns, the framework provides
234
270
 
235
271
  A collection of functions for creating nodes and pipelines in a more functional style.
236
272
 
237
- - `mapNode`: Creates a `Node` from a simple, pure function.
238
- - `contextNode`: Creates a `Node` from a function that requires access to the `Context`.
273
+ - `mapNode`: Creates a `Node` from a simple, pure function: `mapNode<TIn extends Params, TOut>(fn)`. The function receives the node's `params` as its input.
274
+ - `contextNode`: Creates a `Node` from a function that requires access to the `Context`: `contextNode<TIn extends Params, TOut>(fn)`. The function receives the context and the node's `params`.
275
+ - `transformNode`: Creates a `Node` that applies one or more `ContextTransform` functions (often created with a `lens`). This is ideal for declarative state management.
239
276
  - `pipeline`: A functional alias for creating a linear sequence of nodes.
277
+ - `mapCollection`: Creates a `Flow` that applies a function to each item in a collection in parallel, returning an array of the results.
278
+ - `filterCollection`: Creates a `Flow` that filters a collection in parallel based on a predicate function.
279
+ - `reduceCollection`: Creates a `Flow` that reduces a collection to a single value by applying a reducer function sequentially.
240
280
 
241
281
  ### Builder Classes
242
282
 
243
283
  - `SequenceFlow`: A `Flow` that creates a linear flow from a sequence of nodes.
244
284
  - `BatchFlow`: A `Flow` that processes a collection of items sequentially.
245
285
  - `ParallelBatchFlow`: A `Flow` that processes a collection of items in parallel.
246
- - `GraphBuilder`: Constructs a `Flow` from a declarative graph definition.
286
+ - `GraphBuilder`: Constructs a `Flow` from a declarative graph definition. Its `.build()` method returns a `BuildResult` object containing:
287
+ - `flow`: The executable `Flow` instance.
288
+ - `nodeMap`: A `Map` of all instantiated nodes, keyed by their ID.
289
+ - `predecessorCountMap`: A `Map` of node IDs to their total number of incoming connections.
290
+ - `predecessorIdMap`: A `Map` of node IDs to an array of their direct predecessor IDs, useful for advanced executors.
291
+
292
+ ## Documentation
293
+
294
+ The complete [Flowcraft documentation](https://flowcraft-docs.netlify.app) is available on the website.
247
295
 
248
296
  ---
249
297
  Licensed under the [MIT License](./LICENSE).