@fractal-solutions/qflow 1.0.1 → 1.0.3

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 (2) hide show
  1. package/README.md +138 -105
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -100,26 +100,36 @@ Chaining multiple nodes together to form a basic workflow.
100
100
  ```javascript
101
101
  import { Node, Flow } from '@fractal-solutions/qflow';
102
102
 
103
- class AddNode extends Node {
104
- exec(input) { return input + 5; }
105
- }
103
+ let count = 0;
106
104
 
107
- class MultiplyNode extends Node {
108
- exec(input) { return input * 2; }
105
+ class MessageNode extends Node {
106
+ exec() {
107
+ count++;
108
+ console.log(`New Message ${count}`);
109
+ return `default`;
110
+ }
109
111
  }
110
112
 
111
- const add = new AddNode();
112
- const multiply = new MultiplyNode();
113
+ class TimeNode extends Node {
114
+ exec() {
115
+ console.log(`Time ${Date.now()}`);
116
+ return `default`;
117
+ }
118
+ }
113
119
 
114
- // Chain the nodes
115
- add.next(multiply);
120
+ const m1 = new MessageNode();
121
+ const t1 = new TimeNode();
122
+ const m2 = new MessageNode();
116
123
 
117
- // Create the flow, starting with the 'add' node
118
- const flow = new Flow(add);
124
+ m1.next(t1);
125
+ t1.next(m2);
119
126
 
120
- // Run the flow. Initial parameters can be passed via the second argument to _orch.
121
- const result = flow._orch(null, { params: 10 });
122
- console.log('Flow result:', result); // Expected: 30 ((10 + 5) * 2)
127
+ const flow = new Flow(m1);
128
+ flow.run({});
129
+ // Expected output (approximate):
130
+ // New Message 1
131
+ // Time <timestamp>
132
+ // New Message 2
123
133
  ```
124
134
 
125
135
  ### 3. Conditional Flow
@@ -129,40 +139,54 @@ Using `transition()` for dynamic branching based on an action.
129
139
  ```javascript
130
140
  import { Node, Flow } from '@fractal-solutions/qflow';
131
141
 
132
- class CheckValueNode extends Node {
133
- exec(value) {
134
- if (value > 10) {
135
- return 'greater';
142
+ class ConditionalNode extends Node {
143
+ constructor(shouldGoLeft) {
144
+ super();
145
+ this.shouldGoLeft = shouldGoLeft;
146
+ }
147
+
148
+ exec() {
149
+ if (this.shouldGoLeft) {
150
+ console.log('ConditionalNode: Going left');
151
+ return 'left';
136
152
  } else {
137
- return 'lessOrEqual';
153
+ console.log('ConditionalNode: Going right');
154
+ return 'right';
138
155
  }
139
156
  }
140
157
  }
141
158
 
142
- class GreaterNode extends Node {
143
- exec() { return 'Value is greater than 10'; }
144
- }
145
-
146
- class LessOrEqualNode extends Node {
147
- exec() { return 'Value is less than or equal to 10'; }
159
+ // Helper node for conditional transition test
160
+ function MessageNode(message) {
161
+ return new (class extends Node {
162
+ exec() {
163
+ console.log(message);
164
+ return 'default';
165
+ }
166
+ })();
148
167
  }
149
168
 
150
- const check = new CheckValueNode();
151
- const greater = new GreaterNode();
152
- const lessOrEqual = new LessOrEqualNode();
153
-
154
- // Chain the nodes with conditional transitions
155
- check.transition('greater').to(greater);
156
- check.transition('lessOrEqual').to(lessOrEqual);
157
-
158
- // Create the flow, starting with the 'check' node
159
- const flow = new Flow(check);
169
+ const conditionalNode = new ConditionalNode(true);
170
+ const leftNode = MessageNode('Went Left');
171
+ const rightNode = MessageNode('Went Right');
160
172
 
161
- let result1 = flow._orch(null, { params: 15 }); // Pass params directly to _orch
162
- console.log(result1); // Expected: Value is greater than 10
173
+ conditionalNode.next(leftNode, 'left');
174
+ conditionalNode.next(rightNode, 'right');
163
175
 
164
- let result2 = flow._orch(null, { params: 7 });
165
- console.log(result2); // Expected: Value is less than or equal to 10
176
+ const conditionalFlow = new Flow(conditionalNode);
177
+ conditionalFlow.run({});
178
+ // Expected output:
179
+ // ConditionalNode: Going left
180
+ // Went Left
181
+
182
+ const conditionalNode2 = new ConditionalNode(false);
183
+ conditionalNode2.next(leftNode, 'left');
184
+ conditionalNode2.next(rightNode, 'right');
185
+ const conditionalFlow2 = new Flow(conditionalNode2);
186
+ conditionalFlow2.run({});
187
+ // Expected output:
188
+ // ConditionalNode: Going right
189
+ // Went Right
166
190
  ```
167
191
 
168
192
  ### 4. Asynchronous Flow
@@ -172,41 +196,26 @@ Handling asynchronous operations within a flow.
172
196
  ```javascript
173
197
  import { AsyncNode, AsyncFlow } from '@fractal-solutions/qflow';
174
198
 
175
- class FetchDataNode extends AsyncNode {
176
- async execAsync(url) {
177
- console.log(`Fetching data from: ${url}`);
178
- // Simulate an API call
179
- return new Promise(resolve => setTimeout(() => resolve(`Data from ${url}`), 100));
180
- }
181
- }
182
-
183
- class ProcessDataNode extends AsyncNode {
184
- async execAsync(data) {
185
- console.log(`Processing: ${data}`);
186
- return data.toUpperCase();
199
+ class MyAsyncNode extends AsyncNode {
200
+ async execAsync() {
201
+ console.log('AsyncNode: Starting...');
202
+ await new Promise(resolve => setTimeout(resolve, 100));
203
+ console.log('AsyncNode: Finished!');
204
+ return 'default';
187
205
  }
188
206
  }
189
207
 
190
- const fetchNode = new FetchDataNode();
191
- const processNode = new ProcessDataNode();
192
-
193
- // Chain the nodes
194
- fetchNode.next(processNode);
195
-
196
- // Create the async flow, starting with the 'fetchNode'
197
- const asyncFlow = new AsyncFlow(fetchNode);
198
-
199
- async function runAsyncFlow() {
200
- // Run the flow, passing the URL as an initial parameter to the first node
201
- const result = await asyncFlow._orchAsync(null, { params: 'https://api.example.com/data' });
202
- console.log('Async Flow Result:', result);
203
- }
208
+ const asyncNode1 = new MyAsyncNode();
209
+ const asyncNode2 = new MyAsyncNode();
210
+ asyncNode1.next(asyncNode2);
204
211
 
205
- runAsyncFlow();
212
+ const asyncFlow = new AsyncFlow(asyncNode1);
213
+ await asyncFlow.runAsync({});
206
214
  // Expected output:
207
- // Fetching data from: https://api.example.com/data
208
- // Processing: Data from https://api.example.com/data
209
- // Async Flow Result: DATA FROM HTTPS://API.EXAMPLE.COM/DATA
215
+ // AsyncNode: Starting...
216
+ // AsyncNode: Finished!
217
+ // AsyncNode: Starting...
218
+ // AsyncNode: Finished!
210
219
  ```
211
220
 
212
221
  ### 5. Batch Processing
@@ -214,26 +223,50 @@ runAsyncFlow();
214
223
  Processing multiple items through a flow.
215
224
 
216
225
  ```javascript
217
- import { Node, BatchFlow } from '@fractal-solutions/qflow';
226
+ import { Node, BatchFlow, AsyncParallelBatchFlow, AsyncNode } from '@fractal-solutions/qflow';
227
+
228
+ // Synchronous Batch Flow
229
+ console.log('\n--- Running Synchronous Batch Flow ---');
218
230
 
219
- class ProcessItemNode extends Node {
220
- exec(item) {
221
- return `Processed: ${item}`;
231
+ class MyBatchNode extends Node {
232
+ exec() {
233
+ console.log(`BatchNode: Processing item ${this.params.item}`);
234
+ return 'default';
222
235
  }
223
236
  }
224
237
 
225
- const processItem = new ProcessItemNode();
226
-
227
- // Create the batch flow, starting with the 'processItem' node
228
- const batchFlow = new BatchFlow(processItem);
229
-
230
- // Define the items to process. For BatchFlow, the prep method should return an array.
231
- batchFlow.prep = () => ['itemA', 'itemB', 'itemC'];
232
-
233
- const results = batchFlow.run({}); // Run the batch flow
234
- console.log('Batch Flow Results:', results);
238
+ const batchNode = new MyBatchNode();
239
+ const batchFlow = new BatchFlow(batchNode);
240
+ batchFlow.prep = () => [ { item: 1 }, { item: 2 }, { item: 3 } ];
241
+ batchFlow.run({});
235
242
  // Expected output:
236
- // Batch Flow Results: [ 'Processed: itemA', 'Processed: itemB', 'Processed: itemC' ]
243
+ // BatchNode: Processing item 1
244
+ // BatchNode: Processing item 2
245
+ // BatchNode: Processing item 3
246
+
247
+ // Asynchronous Parallel Batch Flow
248
+ console.log('\n--- Running Asynchronous Parallel Batch Flow ---');
249
+
250
+ class MyAsyncParallelBatchNode extends AsyncNode {
251
+ async execAsync() {
252
+ console.log(`AsyncParallelBatchNode: Starting item ${this.params.item}`);
253
+ await new Promise(resolve => setTimeout(resolve, Math.random() * 100));
254
+ console.log(`AsyncParallelBatchNode: Finished item ${this.params.item}`);
255
+ return 'default';
256
+ }
257
+ }
258
+
259
+ const asyncParallelBatchNode = new MyAsyncParallelBatchNode();
260
+ const asyncParallelBatchFlow = new AsyncParallelBatchFlow(asyncParallelBatchNode);
261
+ asyncParallelBatchFlow.prepAsync = async () => [ { item: 1 }, { item: 2 }, { item: 3 }, { item: 4 }, { item: 5 } ];
262
+ await asyncParallelBatchFlow.runAsync({});
263
+ // Expected output (order may vary due to parallel execution):
264
+ // AsyncParallelBatchNode: Starting item 1
265
+ // AsyncParallelBatchNode: Starting item 2
266
+ // ...
267
+ // AsyncParallelBatchNode: Finished item 1
268
+ // AsyncParallelBatchNode: Finished item 2
269
+ // ...
237
270
  ```
238
271
 
239
272
  ### 6. Retry Mechanism
@@ -241,37 +274,37 @@ console.log('Batch Flow Results:', results);
241
274
  Configuring a node to retry on failure.
242
275
 
243
276
  ```javascript
244
- import { Node } from '@fractal-solutions/qflow';
277
+ import { Node, Flow } from '@fractal-solutions/qflow';
245
278
 
246
- let attempt = 0;
247
- class FlakyNode extends Node {
279
+ let retryCount = 0;
280
+ class RetryNode extends Node {
248
281
  constructor() {
249
- super(3, 0.1); // maxRetries = 3, wait = 0.1 seconds
282
+ super(3, 0.1); // 3 retries, 0.1s wait
250
283
  }
251
284
 
252
- exec(input) {
253
- attempt++;
254
- console.log(`FlakyNode attempt ${attempt} for input: ${input}`);
255
- if (attempt < 3) {
256
- throw new Error('Simulated failure');
285
+ exec() {
286
+ retryCount++;
287
+ if (retryCount < 3) {
288
+ console.log(`RetryNode: Failing, attempt ${retryCount}`);
289
+ throw new Error('Failed!');
290
+ } else {
291
+ console.log('RetryNode: Succeeded!');
292
+ return 'default';
257
293
  }
258
- return `Success on attempt ${attempt}`;
259
294
  }
260
295
 
261
296
  execFallback(prepRes, error) {
262
- console.error('FlakyNode failed after retries:', error.message);
263
- return 'Fallback result due to persistent failure';
297
+ console.log('RetryNode: Fallback executed');
264
298
  }
265
299
  }
266
300
 
267
- const flakyNode = new FlakyNode();
268
- const result = flakyNode.run('test');
269
- console.log('Flaky Node Result:', result);
270
- // Expected output (may vary slightly due to timing):
271
- // FlakyNode attempt 1 for input: test
272
- // FlakyNode attempt 2 for input: test
273
- // FlakyNode attempt 3 for input: test
274
- // Flaky Node Result: Success on attempt 3
301
+ const retryNode = new RetryNode();
302
+ const retryFlow = new Flow(retryNode);
303
+ retryFlow.run({});
304
+ // Expected output:
305
+ // RetryNode: Failing, attempt 1
306
+ // RetryNode: Failing, attempt 2
307
+ // RetryNode: Succeeded!
275
308
  ```
276
309
 
277
310
  ## Built-in Nodes
@@ -330,4 +363,4 @@ runScrapeFlow();
330
363
 
331
364
  We welcome contributions! Please see our GitHub repository for more details on how to contribute.
332
365
 
333
- ---
366
+ ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fractal-solutions/qflow",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A lightweight and flexible JavaScript library for creating and managing workflows and agents.",
5
5
  "main": "dist/qflow.js",
6
6
  "types": "dist/qflow.d.ts",