@fractal-solutions/qflow 1.0.47 → 1.0.50
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 +195 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,22 +46,212 @@ bunx create-qflow@latest <project-name>
|
|
|
46
46
|
|
|
47
47
|
## Core Abstractions
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
`qflow` is built around a few core abstractions that enable powerful and flexible workflow definitions.
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
### Shared State (`shared` object)
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
A central, mutable JavaScript object that is passed through the entire flow. Nodes can read from and write to this `shared` object, making it the primary mechanism for passing data and context between different nodes in a workflow. This is particularly useful for accumulating results or maintaining state across multiple steps.
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
### Node
|
|
56
|
+
|
|
57
|
+
The fundamental building block of any `qflow` workflow. A `Node` represents a single, atomic operation or step in your flow.
|
|
58
|
+
|
|
59
|
+
* `prep(shared)`: Prepares data for execution. Receives the `shared` object.
|
|
60
|
+
* `exec(prepRes)`: Executes the node's primary logic.
|
|
61
|
+
* `post(shared, prepRes, execRes)`: Processes the result of `exec`. Receives the `shared` object.
|
|
62
|
+
* `setParams(params)`: Configures the node with specific parameters. Parameters are accessible via `this.params`.
|
|
63
|
+
* `next(node, action = "default")`: Chains this node to another, defining the next step in the flow.
|
|
64
|
+
|
|
65
|
+
**Asynchronous Nodes (`AsyncNode`, `AsyncBatchNode`, `AsyncParallelBatchNode`)**
|
|
66
|
+
For operations that involve I/O or are inherently asynchronous, `qflow` provides `AsyncNode` and its variants. These nodes leverage `async`/`await` for non-blocking execution. When working within `AsyncFlow`s, it's crucial to implement the `async` versions of the lifecycle methods:
|
|
67
|
+
* `prepAsync(shared)`
|
|
68
|
+
* `execAsync(prepRes, shared)`
|
|
69
|
+
* `postAsync(shared, prepRes, execRes)`
|
|
70
|
+
* `preparePrompt(shared)` (specifically for LLM nodes, allowing prompt construction based on `shared` state)
|
|
56
71
|
|
|
57
|
-
|
|
72
|
+
These `async` methods ensure proper awaiting and data propagation within asynchronous workflows.
|
|
58
73
|
|
|
74
|
+
### Flow
|
|
75
|
+
|
|
76
|
+
A `Flow` orchestrates the execution of a sequence of `Node`s. It defines the overall path and manages the transitions between nodes.
|
|
77
|
+
|
|
78
|
+
* `start(startNode)`: Sets the initial node for the flow. (Note: In practice, you often pass the start node directly to the `Flow` constructor for conciseness).
|
|
79
|
+
* `_orch(shared, params)` / `_orchAsync(shared, params)`: Internal methods used to run the flow, especially when passing initial parameters to the starting node. For most use cases, `flow.run(sharedState)` or `await flow.runAsync(sharedState)` is sufficient.
|
|
80
|
+
|
|
81
|
+
**Batch Flows (`BatchFlow`, `AsyncBatchFlow`, `AsyncParallelBatchFlow`)**
|
|
82
|
+
These specialized flows are designed to process collections of items. They run a sub-flow for each item in the batch. `AsyncParallelBatchFlow` is particularly useful for concurrently processing batch items, significantly speeding up operations.
|
|
59
83
|
|
|
60
84
|
## Basic Usage & Examples
|
|
61
85
|
|
|
86
|
+
### 1. Simple Node
|
|
87
|
+
|
|
88
|
+
A basic example of defining and running a single node.
|
|
89
|
+
|
|
90
|
+
```javascript
|
|
91
|
+
import { Node } from '@fractal-solutions/qflow';
|
|
92
|
+
|
|
93
|
+
class MySimpleNode extends Node {
|
|
94
|
+
prep(shared) {
|
|
95
|
+
console.log('Preparing data...');
|
|
96
|
+
return shared.inputData * 2;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
exec(prepRes) {
|
|
100
|
+
console.log('Executing with prepared data:', prepRes);
|
|
101
|
+
return prepRes + 10;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
post(shared, prepRes, execRes) {
|
|
105
|
+
console.log('Post-processing result:', execRes);
|
|
106
|
+
return { finalResult: execRes, originalInput: shared.inputData };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const node = new MySimpleNode();
|
|
111
|
+
const result = node.run({ inputData: 5 });
|
|
112
|
+
console.log('Node run result:', result);
|
|
113
|
+
// Expected output:
|
|
114
|
+
// Preparing data...
|
|
115
|
+
// Executing with prepared data: 10
|
|
116
|
+
// Post-processing result: 20
|
|
117
|
+
// Node run result: { finalResult: 20, originalInput: 5 }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 2. Simple Flow
|
|
121
|
+
|
|
122
|
+
Chaining multiple nodes together to form a basic workflow.
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
import { Node, Flow } from '@fractal-solutions/qflow';
|
|
126
|
+
|
|
127
|
+
let count = 0;
|
|
128
|
+
|
|
129
|
+
class MessageNode extends Node {
|
|
130
|
+
exec() {
|
|
131
|
+
count++;
|
|
132
|
+
console.log(`New Message ${count}`);
|
|
133
|
+
return `default`;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
class TimeNode extends Node {
|
|
138
|
+
exec() {
|
|
139
|
+
console.log(`Time ${Date.now()}`);
|
|
140
|
+
return `default`;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const m1 = new MessageNode();
|
|
145
|
+
const t1 = new TimeNode();
|
|
146
|
+
const m2 = new MessageNode();
|
|
147
|
+
|
|
148
|
+
m1.next(t1);
|
|
149
|
+
t1.next(m2);
|
|
150
|
+
|
|
151
|
+
const flow = new Flow(m1);
|
|
152
|
+
flow.run({});
|
|
153
|
+
// Expected output (approximate):
|
|
154
|
+
// New Message 1
|
|
155
|
+
// Time <timestamp>
|
|
156
|
+
// New Message 2
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 3. Conditional Flow
|
|
160
|
+
|
|
161
|
+
Using `transition()` for dynamic branching based on an action. This example demonstrates configuring a node using `setParams` for a cleaner API.
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
import { Node, Flow } from '@fractal-solutions/qflow';
|
|
165
|
+
|
|
166
|
+
class ConditionalNode extends Node {
|
|
167
|
+
exec() {
|
|
168
|
+
// Access shouldGoLeft from this.params, which is set via setParams
|
|
169
|
+
if (this.params.shouldGoLeft) {
|
|
170
|
+
console.log('ConditionalNode: Going left');
|
|
171
|
+
return 'left';
|
|
172
|
+
} else {
|
|
173
|
+
console.log('ConditionalNode: Going right');
|
|
174
|
+
return 'right';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Helper node for conditional transition test
|
|
180
|
+
function MessageNode(message) {
|
|
181
|
+
return new (class extends Node {
|
|
182
|
+
exec() {
|
|
183
|
+
console.log(message);
|
|
184
|
+
return 'default';
|
|
185
|
+
}
|
|
186
|
+
})();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const conditionalNode = new ConditionalNode();
|
|
190
|
+
conditionalNode.setParams({ shouldGoLeft: true }); // Configure via setParams
|
|
191
|
+
|
|
192
|
+
const leftNode = MessageNode('Went Left');
|
|
193
|
+
const rightNode = MessageNode('Went Right');
|
|
194
|
+
|
|
195
|
+
conditionalNode.next(leftNode, 'left');
|
|
196
|
+
conditionalNode.next(rightNode, 'right');
|
|
197
|
+
|
|
198
|
+
const conditionalFlow = new Flow(conditionalNode);
|
|
199
|
+
conditionalFlow.run({});
|
|
200
|
+
// Expected output:
|
|
201
|
+
// ConditionalNode: Going left
|
|
202
|
+
// Went Left
|
|
203
|
+
|
|
204
|
+
const conditionalNode2 = new ConditionalNode();
|
|
205
|
+
conditionalNode2.setParams({ shouldGoLeft: false }); // Configure via setParams
|
|
206
|
+
|
|
207
|
+
conditionalNode2.next(leftNode, 'left');
|
|
208
|
+
conditionalNode2.next(rightNode, 'right');
|
|
209
|
+
const conditionalFlow2 = new Flow(conditionalNode2);
|
|
210
|
+
conditionalFlow2.run({});
|
|
211
|
+
// Expected output:
|
|
212
|
+
// ConditionalNode: Going right
|
|
213
|
+
// Went Right
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### 4. Asynchronous Flow
|
|
217
|
+
|
|
218
|
+
Handling asynchronous operations within a flow.
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
import { AsyncNode, AsyncFlow } from '@fractal-solutions/qflow';
|
|
222
|
+
|
|
223
|
+
class MyAsyncNode extends AsyncNode {
|
|
224
|
+
async execAsync() {
|
|
225
|
+
console.log('AsyncNode: Starting...');
|
|
226
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
227
|
+
console.log('AsyncNode: Finished!');
|
|
228
|
+
return 'default';
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const asyncNode1 = new MyAsyncNode();
|
|
233
|
+
const asyncNode2 = new MyAsyncNode();
|
|
234
|
+
asyncNode1.next(asyncNode2);
|
|
235
|
+
|
|
236
|
+
const asyncFlow = new AsyncFlow(asyncNode1);
|
|
237
|
+
await asyncFlow.runAsync({});
|
|
238
|
+
// Expected output:
|
|
239
|
+
// AsyncNode: Starting...
|
|
240
|
+
// AsyncNode: Finished!
|
|
241
|
+
// AsyncNode: Starting...
|
|
242
|
+
// AsyncNode: Finished!
|
|
243
|
+
```
|
|
244
|
+
|
|
62
245
|
See [documentation/examples.md](documentation/examples.md) for a full list of examples.
|
|
63
246
|
|
|
64
247
|
|
|
248
|
+
## Agents
|
|
249
|
+
|
|
250
|
+
For a detailed explanation of the agents, see the [Agents documentation](documentation/agents.md). The tools available to agents are documented in the [Tools documentation](documentation/integrated_nodes.md).
|
|
251
|
+
|
|
252
|
+
## Integrated Nodes and their Examples
|
|
253
|
+
|
|
254
|
+
For a detailed explanation of the integrated nodes and their examples, see the [Integrated Nodes documentation](documentation/nodes/index.md).
|
|
65
255
|
|
|
66
256
|
|
|
67
257
|
## Error Handling
|
package/package.json
CHANGED