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.
- package/README.md +65 -17
- package/dist/index.d.ts +1076 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/package.json +12 -8
- package/.editorconfig +0 -9
- package/config/tsconfig.json +0 -21
- package/config/tsup.config.ts +0 -11
- package/config/vitest.config.ts +0 -11
- package/docs/.vitepress/config.ts +0 -105
- package/docs/api-reference/builder.md +0 -158
- package/docs/api-reference/fn.md +0 -142
- package/docs/api-reference/index.md +0 -38
- package/docs/api-reference/workflow.md +0 -126
- package/docs/guide/advanced-guides/cancellation.md +0 -117
- package/docs/guide/advanced-guides/composition.md +0 -68
- package/docs/guide/advanced-guides/custom-executor.md +0 -180
- package/docs/guide/advanced-guides/error-handling.md +0 -135
- package/docs/guide/advanced-guides/logging.md +0 -106
- package/docs/guide/advanced-guides/middleware.md +0 -106
- package/docs/guide/advanced-guides/observability.md +0 -175
- package/docs/guide/best-practices/debugging.md +0 -182
- package/docs/guide/best-practices/state-management.md +0 -120
- package/docs/guide/best-practices/sub-workflow-data.md +0 -95
- package/docs/guide/best-practices/testing.md +0 -187
- package/docs/guide/builders.md +0 -157
- package/docs/guide/functional-api.md +0 -133
- package/docs/guide/index.md +0 -178
- package/docs/guide/recipes/creating-a-loop.md +0 -113
- package/docs/guide/recipes/data-processing-pipeline.md +0 -123
- package/docs/guide/recipes/fan-out-fan-in.md +0 -112
- package/docs/guide/recipes/index.md +0 -15
- package/docs/guide/recipes/resilient-api-call.md +0 -110
- package/docs/guide/tooling/graph-validation.md +0 -160
- package/docs/guide/tooling/mermaid.md +0 -156
- package/docs/index.md +0 -56
- package/eslint.config.js +0 -16
- package/pnpm-workspace.yaml +0 -2
- package/sandbox/1.basic/README.md +0 -45
- package/sandbox/1.basic/package.json +0 -16
- package/sandbox/1.basic/src/flow.ts +0 -17
- package/sandbox/1.basic/src/main.ts +0 -22
- package/sandbox/1.basic/src/nodes.ts +0 -112
- package/sandbox/1.basic/src/utils.ts +0 -35
- package/sandbox/1.basic/tsconfig.json +0 -3
- package/sandbox/2.research/README.md +0 -46
- package/sandbox/2.research/package.json +0 -16
- package/sandbox/2.research/src/flow.ts +0 -14
- package/sandbox/2.research/src/main.ts +0 -31
- package/sandbox/2.research/src/nodes.ts +0 -108
- package/sandbox/2.research/src/utils.ts +0 -45
- package/sandbox/2.research/src/visualize.ts +0 -29
- package/sandbox/2.research/tsconfig.json +0 -3
- package/sandbox/3.parallel/README.md +0 -65
- package/sandbox/3.parallel/package.json +0 -16
- package/sandbox/3.parallel/src/main.ts +0 -45
- package/sandbox/3.parallel/src/nodes.ts +0 -43
- package/sandbox/3.parallel/src/utils.ts +0 -25
- package/sandbox/3.parallel/tsconfig.json +0 -3
- package/sandbox/4.dag/README.md +0 -179
- package/sandbox/4.dag/data/1.blog-post/100.json +0 -60
- package/sandbox/4.dag/data/1.blog-post/README.md +0 -25
- package/sandbox/4.dag/data/2.job-application/200.json +0 -103
- package/sandbox/4.dag/data/2.job-application/201.json +0 -31
- package/sandbox/4.dag/data/2.job-application/202.json +0 -31
- package/sandbox/4.dag/data/2.job-application/README.md +0 -58
- package/sandbox/4.dag/data/3.customer-review/300.json +0 -141
- package/sandbox/4.dag/data/3.customer-review/301.json +0 -31
- package/sandbox/4.dag/data/3.customer-review/302.json +0 -28
- package/sandbox/4.dag/data/3.customer-review/README.md +0 -71
- package/sandbox/4.dag/data/4.content-moderation/400.json +0 -161
- package/sandbox/4.dag/data/4.content-moderation/401.json +0 -47
- package/sandbox/4.dag/data/4.content-moderation/402.json +0 -46
- package/sandbox/4.dag/data/4.content-moderation/403.json +0 -31
- package/sandbox/4.dag/data/4.content-moderation/README.md +0 -83
- package/sandbox/4.dag/package.json +0 -19
- package/sandbox/4.dag/src/main.ts +0 -73
- package/sandbox/4.dag/src/nodes.ts +0 -134
- package/sandbox/4.dag/src/registry.ts +0 -87
- package/sandbox/4.dag/src/types.ts +0 -25
- package/sandbox/4.dag/src/utils.ts +0 -42
- package/sandbox/4.dag/tsconfig.json +0 -3
- package/sandbox/5.distributed/.env.example +0 -1
- package/sandbox/5.distributed/README.md +0 -88
- package/sandbox/5.distributed/data/1.blog-post/100.json +0 -59
- package/sandbox/5.distributed/data/1.blog-post/README.md +0 -25
- package/sandbox/5.distributed/data/2.job-application/200.json +0 -103
- package/sandbox/5.distributed/data/2.job-application/201.json +0 -30
- package/sandbox/5.distributed/data/2.job-application/202.json +0 -30
- package/sandbox/5.distributed/data/2.job-application/README.md +0 -58
- package/sandbox/5.distributed/data/3.customer-review/300.json +0 -141
- package/sandbox/5.distributed/data/3.customer-review/301.json +0 -31
- package/sandbox/5.distributed/data/3.customer-review/302.json +0 -57
- package/sandbox/5.distributed/data/3.customer-review/README.md +0 -71
- package/sandbox/5.distributed/data/4.content-moderation/400.json +0 -173
- package/sandbox/5.distributed/data/4.content-moderation/401.json +0 -47
- package/sandbox/5.distributed/data/4.content-moderation/402.json +0 -46
- package/sandbox/5.distributed/data/4.content-moderation/403.json +0 -31
- package/sandbox/5.distributed/data/4.content-moderation/README.md +0 -83
- package/sandbox/5.distributed/package.json +0 -20
- package/sandbox/5.distributed/src/client.ts +0 -124
- package/sandbox/5.distributed/src/executor.ts +0 -69
- package/sandbox/5.distributed/src/nodes.ts +0 -136
- package/sandbox/5.distributed/src/registry.ts +0 -101
- package/sandbox/5.distributed/src/types.ts +0 -45
- package/sandbox/5.distributed/src/utils.ts +0 -69
- package/sandbox/5.distributed/src/worker.ts +0 -217
- package/sandbox/5.distributed/tsconfig.json +0 -3
- package/sandbox/6.rag/.env.example +0 -1
- package/sandbox/6.rag/README.md +0 -60
- package/sandbox/6.rag/data/README.md +0 -31
- package/sandbox/6.rag/data/rag.json +0 -58
- package/sandbox/6.rag/documents/sample-cascade.txt +0 -11
- package/sandbox/6.rag/package.json +0 -18
- package/sandbox/6.rag/src/main.ts +0 -52
- package/sandbox/6.rag/src/nodes/GenerateEmbeddingsNode.ts +0 -54
- package/sandbox/6.rag/src/nodes/LLMProcessNode.ts +0 -48
- package/sandbox/6.rag/src/nodes/LoadAndChunkNode.ts +0 -40
- package/sandbox/6.rag/src/nodes/StoreInVectorDBNode.ts +0 -36
- package/sandbox/6.rag/src/nodes/VectorSearchNode.ts +0 -53
- package/sandbox/6.rag/src/nodes/index.ts +0 -28
- package/sandbox/6.rag/src/registry.ts +0 -23
- package/sandbox/6.rag/src/types.ts +0 -44
- package/sandbox/6.rag/src/utils.ts +0 -77
- package/sandbox/6.rag/tsconfig.json +0 -3
- package/sandbox/tsconfig.json +0 -13
- package/src/builder/collection.test.ts +0 -287
- package/src/builder/collection.ts +0 -269
- package/src/builder/graph.test.ts +0 -406
- package/src/builder/graph.ts +0 -336
- package/src/builder/graph.types.ts +0 -104
- package/src/builder/index.ts +0 -3
- package/src/context.ts +0 -111
- package/src/errors.ts +0 -34
- package/src/executor.ts +0 -29
- package/src/executors/in-memory.test.ts +0 -93
- package/src/executors/in-memory.ts +0 -140
- package/src/functions.test.ts +0 -191
- package/src/functions.ts +0 -117
- package/src/index.ts +0 -5
- package/src/logger.ts +0 -41
- package/src/types.ts +0 -75
- package/src/utils/graph.test.ts +0 -144
- package/src/utils/graph.ts +0 -182
- package/src/utils/index.ts +0 -3
- package/src/utils/mermaid.test.ts +0 -239
- package/src/utils/mermaid.ts +0 -133
- package/src/utils/sleep.ts +0 -20
- package/src/workflow.test.ts +0 -622
- 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
|
|
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,
|
|
42
|
+
import { Flow, mapNode } from 'flowcraft' // Use mapNode for simple functions
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
-
|
|
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
|
|
228
|
-
- `Flow`: Orchestrates a sequence of nodes
|
|
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).
|