societyai 0.0.1
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/CHANGELOG.md +111 -0
- package/LICENSE +21 -0
- package/README.md +879 -0
- package/dist/builder.d.ts +181 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +667 -0
- package/dist/builder.js.map +1 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +11 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +107 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +319 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +85 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +219 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +395 -0
- package/dist/events.js.map +1 -0
- package/dist/graph.d.ts +104 -0
- package/dist/graph.d.ts.map +1 -0
- package/dist/graph.js +366 -0
- package/dist/graph.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +113 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +13 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +78 -0
- package/dist/logger.js.map +1 -0
- package/dist/memory.d.ts +146 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +353 -0
- package/dist/memory.js.map +1 -0
- package/dist/metrics.d.ts +143 -0
- package/dist/metrics.d.ts.map +1 -0
- package/dist/metrics.js +271 -0
- package/dist/metrics.js.map +1 -0
- package/dist/middleware.d.ts +147 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +484 -0
- package/dist/middleware.js.map +1 -0
- package/dist/models.d.ts +32 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +211 -0
- package/dist/models.js.map +1 -0
- package/dist/patterns.d.ts +6 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +68 -0
- package/dist/patterns.js.map +1 -0
- package/dist/pipeline.d.ts +84 -0
- package/dist/pipeline.d.ts.map +1 -0
- package/dist/pipeline.js +569 -0
- package/dist/pipeline.js.map +1 -0
- package/dist/retry.d.ts +5 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +70 -0
- package/dist/retry.js.map +1 -0
- package/dist/society.d.ts +94 -0
- package/dist/society.d.ts.map +1 -0
- package/dist/society.js +721 -0
- package/dist/society.js.map +1 -0
- package/dist/strategies.d.ts +55 -0
- package/dist/strategies.d.ts.map +1 -0
- package/dist/strategies.js +678 -0
- package/dist/strategies.js.map +1 -0
- package/dist/tools.d.ts +88 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +366 -0
- package/dist/tools.js.map +1 -0
- package/dist/types.d.ts +213 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +64 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +334 -0
- package/dist/validation.js.map +1 -0
- package/dist/worker-pool.d.ts +17 -0
- package/dist/worker-pool.d.ts.map +1 -0
- package/dist/worker-pool.js +80 -0
- package/dist/worker-pool.js.map +1 -0
- package/docs/README.md +468 -0
- package/docs/advanced.md +616 -0
- package/docs/aggregation-strategies.md +926 -0
- package/docs/api-reference.md +771 -0
- package/docs/architecture.md +648 -0
- package/docs/context-system.md +642 -0
- package/docs/event-system.md +1047 -0
- package/docs/examples.md +576 -0
- package/docs/getting-started.md +564 -0
- package/docs/graph-execution.md +389 -0
- package/docs/memory-system.md +497 -0
- package/docs/metrics-observability.md +560 -0
- package/docs/middleware-system.md +1038 -0
- package/docs/migration.md +296 -0
- package/docs/pipeline-patterns.md +761 -0
- package/docs/structured-output.md +612 -0
- package/docs/tool-calling.md +491 -0
- package/docs/workflows.md +740 -0
- package/examples/README.md +234 -0
- package/examples/advanced-patterns.ts +115 -0
- package/examples/complete-integration.ts +327 -0
- package/examples/graph-workflow.ts +161 -0
- package/examples/memory-system.ts +155 -0
- package/examples/metrics-tracking.ts +243 -0
- package/examples/structured-output.ts +231 -0
- package/examples/tool-calling.ts +163 -0
- package/package.json +94 -0
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
# Workflow Patterns
|
|
2
|
+
|
|
3
|
+
This guide covers common workflow patterns and how to implement them in SocietyAI.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Execution Types](#execution-types)
|
|
8
|
+
- [Common Patterns](#common-patterns)
|
|
9
|
+
- [Pattern Library](#pattern-library)
|
|
10
|
+
- [Custom Patterns](#custom-patterns)
|
|
11
|
+
- [Best Practices](#best-practices)
|
|
12
|
+
|
|
13
|
+
## Execution Types
|
|
14
|
+
|
|
15
|
+
### Sequential Execution
|
|
16
|
+
|
|
17
|
+
Agents execute one after another. Each agent can access results from previous agents.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
const step = StepBuilder.create()
|
|
21
|
+
.withId('sequential-step')
|
|
22
|
+
.withAgents(['agent-1', 'agent-2', 'agent-3'])
|
|
23
|
+
.withExecutionType('sequential')
|
|
24
|
+
.build();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Execution Order**:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
Agent 1 → Agent 2 → Agent 3
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**When to use**:
|
|
34
|
+
|
|
35
|
+
- Tasks with dependencies
|
|
36
|
+
- Progressive refinement
|
|
37
|
+
- Quality review processes
|
|
38
|
+
- Step-by-step analysis
|
|
39
|
+
|
|
40
|
+
### Parallel Execution
|
|
41
|
+
|
|
42
|
+
All agents execute simultaneously for maximum speed.
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
const step = StepBuilder.create()
|
|
46
|
+
.withId('parallel-step')
|
|
47
|
+
.withAgents(['agent-1', 'agent-2', 'agent-3'])
|
|
48
|
+
.withExecutionType('parallel')
|
|
49
|
+
.build();
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Execution Order**:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
┌─ Agent 1 ─┐
|
|
56
|
+
├─ Agent 2 ─┤ → All at once
|
|
57
|
+
└─ Agent 3 ─┘
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**When to use**:
|
|
61
|
+
|
|
62
|
+
- Independent analyses
|
|
63
|
+
- Multiple perspectives
|
|
64
|
+
- Speed optimization
|
|
65
|
+
- Batch processing
|
|
66
|
+
|
|
67
|
+
### Collaborative Execution
|
|
68
|
+
|
|
69
|
+
Agents exchange messages across multiple iterations.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const step = StepBuilder.create()
|
|
73
|
+
.withId('collaborative-step')
|
|
74
|
+
.withAgents(['agent-1', 'agent-2', 'agent-3'])
|
|
75
|
+
.withExecutionType('collaborative')
|
|
76
|
+
.withMaxIterations(3)
|
|
77
|
+
.withCompletionCondition((results, iteration) => {
|
|
78
|
+
// Custom completion logic
|
|
79
|
+
return iteration >= 2;
|
|
80
|
+
})
|
|
81
|
+
.build();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Execution Order**:
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
Round 1: Agent 1, Agent 2, Agent 3 (all share messages)
|
|
88
|
+
Round 2: Agent 1, Agent 2, Agent 3 (consider round 1)
|
|
89
|
+
Round 3: Agent 1, Agent 2, Agent 3 (consider rounds 1-2)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**When to use**:
|
|
93
|
+
|
|
94
|
+
- Discussions and debates
|
|
95
|
+
- Consensus building
|
|
96
|
+
- Iterative refinement
|
|
97
|
+
- Complex decision-making
|
|
98
|
+
|
|
99
|
+
### Conditional Execution
|
|
100
|
+
|
|
101
|
+
Steps execute only when conditions are met.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const step = StepBuilder.create()
|
|
105
|
+
.withId('conditional-step')
|
|
106
|
+
.withAgents(['agent-1'])
|
|
107
|
+
.withExecutionType('conditional')
|
|
108
|
+
.withCondition((previousResults) => {
|
|
109
|
+
const prevStep = previousResults.get('previous-step-id');
|
|
110
|
+
return prevStep?.[0]?.success === true;
|
|
111
|
+
})
|
|
112
|
+
.build();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
**When to use**:
|
|
116
|
+
|
|
117
|
+
- Error handling
|
|
118
|
+
- Dynamic workflows
|
|
119
|
+
- Optimization (skip unnecessary work)
|
|
120
|
+
- Branching logic
|
|
121
|
+
|
|
122
|
+
## Common Patterns
|
|
123
|
+
|
|
124
|
+
### 1. Pipeline Pattern
|
|
125
|
+
|
|
126
|
+
Process data through a sequence of transformations.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
130
|
+
.withId('pipeline')
|
|
131
|
+
.withName('Data Processing Pipeline')
|
|
132
|
+
.addAgents([collectorAgent, analyzerAgent, formatterAgent])
|
|
133
|
+
.addSteps([
|
|
134
|
+
// Step 1: Collect data
|
|
135
|
+
StepBuilder.create()
|
|
136
|
+
.withId('collect')
|
|
137
|
+
.withAgents(['collector'])
|
|
138
|
+
.withExecutionType('sequential')
|
|
139
|
+
.withInstructions('Gather and organize raw data.')
|
|
140
|
+
.build(),
|
|
141
|
+
// Step 2: Analyze
|
|
142
|
+
StepBuilder.create()
|
|
143
|
+
.withId('analyze')
|
|
144
|
+
.withAgents(['analyzer'])
|
|
145
|
+
.withExecutionType('sequential')
|
|
146
|
+
.withInstructions('Analyze the collected data.')
|
|
147
|
+
.build(),
|
|
148
|
+
// Step 3: Format
|
|
149
|
+
StepBuilder.create()
|
|
150
|
+
.withId('format')
|
|
151
|
+
.withAgents(['formatter'])
|
|
152
|
+
.withExecutionType('sequential')
|
|
153
|
+
.withInstructions('Format the analysis for presentation.')
|
|
154
|
+
.build(),
|
|
155
|
+
])
|
|
156
|
+
.build();
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
**Flow**: Collector → Analyzer → Formatter
|
|
160
|
+
|
|
161
|
+
### 2. Parallel Analysis + Synthesis
|
|
162
|
+
|
|
163
|
+
Multiple agents analyze independently, then synthesize results.
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
167
|
+
.withId('parallel-synthesis')
|
|
168
|
+
.withName('Multi-Perspective Analysis')
|
|
169
|
+
.addAgents([analyst1, analyst2, analyst3, synthesizer])
|
|
170
|
+
.addSteps([
|
|
171
|
+
// Step 1: Parallel analysis
|
|
172
|
+
StepBuilder.create()
|
|
173
|
+
.withId('analyze')
|
|
174
|
+
.withAgents(['analyst-1', 'analyst-2', 'analyst-3'])
|
|
175
|
+
.withExecutionType('parallel')
|
|
176
|
+
.withInstructions('Analyze from your unique perspective.')
|
|
177
|
+
.build(),
|
|
178
|
+
// Step 2: Synthesize
|
|
179
|
+
StepBuilder.create()
|
|
180
|
+
.withId('synthesize')
|
|
181
|
+
.withAgents(['synthesizer'])
|
|
182
|
+
.withExecutionType('sequential')
|
|
183
|
+
.withInstructions('Combine all perspectives into a unified analysis.')
|
|
184
|
+
.build(),
|
|
185
|
+
])
|
|
186
|
+
.build();
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
**Flow**: [Analyst 1, Analyst 2, Analyst 3] → Synthesizer
|
|
190
|
+
|
|
191
|
+
### 3. Review Loop
|
|
192
|
+
|
|
193
|
+
One agent produces, another reviews, with potential iteration.
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
197
|
+
.withId('review-loop')
|
|
198
|
+
.withName('Producer-Reviewer Loop')
|
|
199
|
+
.addAgents([producer, reviewer])
|
|
200
|
+
.addSteps([
|
|
201
|
+
// Step 1: Production
|
|
202
|
+
StepBuilder.create()
|
|
203
|
+
.withId('produce')
|
|
204
|
+
.withAgents(['producer'])
|
|
205
|
+
.withExecutionType('sequential')
|
|
206
|
+
.withInstructions('Create the initial version.')
|
|
207
|
+
.build(),
|
|
208
|
+
// Step 2: Review
|
|
209
|
+
StepBuilder.create()
|
|
210
|
+
.withId('review')
|
|
211
|
+
.withAgents(['reviewer'])
|
|
212
|
+
.withExecutionType('sequential')
|
|
213
|
+
.withInstructions('Review and provide feedback.')
|
|
214
|
+
.withNextStepResolver((results) => {
|
|
215
|
+
const hasIssues = results[0]?.content.includes('NEEDS_REVISION');
|
|
216
|
+
return hasIssues ? 'produce' : null; // Loop back or finish
|
|
217
|
+
})
|
|
218
|
+
.build(),
|
|
219
|
+
])
|
|
220
|
+
.build();
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
**Flow**: Producer → Reviewer → (loop if issues) → Producer → ...
|
|
224
|
+
|
|
225
|
+
### 4. Hierarchical Pattern
|
|
226
|
+
|
|
227
|
+
Coordinator distributes work, team executes, coordinator validates.
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
231
|
+
.withId('hierarchical')
|
|
232
|
+
.withName('Hierarchical Team')
|
|
233
|
+
.addAgents([coordinator, worker1, worker2, worker3])
|
|
234
|
+
.addSteps([
|
|
235
|
+
// Step 1: Planning
|
|
236
|
+
StepBuilder.create()
|
|
237
|
+
.withId('plan')
|
|
238
|
+
.withAgents(['coordinator'])
|
|
239
|
+
.withExecutionType('sequential')
|
|
240
|
+
.withInstructions('Break down the task and assign work.')
|
|
241
|
+
.build(),
|
|
242
|
+
// Step 2: Execution
|
|
243
|
+
StepBuilder.create()
|
|
244
|
+
.withId('execute')
|
|
245
|
+
.withAgents(['worker-1', 'worker-2', 'worker-3'])
|
|
246
|
+
.withExecutionType('parallel')
|
|
247
|
+
.withInstructions('Complete your assigned task.')
|
|
248
|
+
.build(),
|
|
249
|
+
// Step 3: Validation
|
|
250
|
+
StepBuilder.create()
|
|
251
|
+
.withId('validate')
|
|
252
|
+
.withAgents(['coordinator'])
|
|
253
|
+
.withExecutionType('sequential')
|
|
254
|
+
.withInstructions('Validate and combine team results.')
|
|
255
|
+
.build(),
|
|
256
|
+
])
|
|
257
|
+
.build();
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
**Flow**: Coordinator → [Worker 1, Worker 2, Worker 3] → Coordinator
|
|
261
|
+
|
|
262
|
+
### 5. Consensus Pattern
|
|
263
|
+
|
|
264
|
+
Agents discuss until they reach agreement.
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
268
|
+
.withId('consensus')
|
|
269
|
+
.withName('Consensus Building')
|
|
270
|
+
.addAgents([agent1, agent2, agent3])
|
|
271
|
+
.addSteps([
|
|
272
|
+
StepBuilder.create()
|
|
273
|
+
.withId('discussion')
|
|
274
|
+
.withAgents(['agent-1', 'agent-2', 'agent-3'])
|
|
275
|
+
.withExecutionType('collaborative')
|
|
276
|
+
.withMaxIterations(5)
|
|
277
|
+
.withCompletionCondition((results, iteration) => {
|
|
278
|
+
// Check if all agents agree
|
|
279
|
+
const contents = results.filter((r) => r.iteration === iteration).map((r) => r.content);
|
|
280
|
+
return checkConsensus(contents) || iteration >= 4;
|
|
281
|
+
})
|
|
282
|
+
.build(),
|
|
283
|
+
])
|
|
284
|
+
.build();
|
|
285
|
+
|
|
286
|
+
function checkConsensus(contents: string[]): boolean {
|
|
287
|
+
// Custom logic to detect consensus
|
|
288
|
+
const keywords = ['agreed', 'consensus', 'accept'];
|
|
289
|
+
return contents.every((c) => keywords.some((k) => c.toLowerCase().includes(k)));
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**Flow**: Multiple rounds of discussion until consensus
|
|
294
|
+
|
|
295
|
+
## Pattern Library
|
|
296
|
+
|
|
297
|
+
### Software Development Team
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
const devWorkflow = WorkflowConfigBuilder.create()
|
|
301
|
+
.withId('dev-team')
|
|
302
|
+
.withName('Software Development Workflow')
|
|
303
|
+
.addAgents([pm, architect, dev1, dev2, qa])
|
|
304
|
+
.addSteps([
|
|
305
|
+
// Planning
|
|
306
|
+
StepBuilder.create()
|
|
307
|
+
.withId('planning')
|
|
308
|
+
.withAgents(['pm'])
|
|
309
|
+
.withExecutionType('sequential')
|
|
310
|
+
.build(),
|
|
311
|
+
// Architecture
|
|
312
|
+
StepBuilder.create()
|
|
313
|
+
.withId('architecture')
|
|
314
|
+
.withAgents(['architect'])
|
|
315
|
+
.withExecutionType('sequential')
|
|
316
|
+
.build(),
|
|
317
|
+
// Implementation (parallel)
|
|
318
|
+
StepBuilder.create()
|
|
319
|
+
.withId('implementation')
|
|
320
|
+
.withAgents(['dev-1', 'dev-2'])
|
|
321
|
+
.withExecutionType('parallel')
|
|
322
|
+
.build(),
|
|
323
|
+
// Testing
|
|
324
|
+
StepBuilder.create()
|
|
325
|
+
.withId('testing')
|
|
326
|
+
.withAgents(['qa'])
|
|
327
|
+
.withExecutionType('sequential')
|
|
328
|
+
.build(),
|
|
329
|
+
// Review (conditional - only if tests fail)
|
|
330
|
+
StepBuilder.create()
|
|
331
|
+
.withId('review')
|
|
332
|
+
.withAgents(['architect'])
|
|
333
|
+
.withExecutionType('conditional')
|
|
334
|
+
.withCondition((results) => {
|
|
335
|
+
const testResults = results.get('testing');
|
|
336
|
+
return testResults?.[0]?.content.includes('FAILED');
|
|
337
|
+
})
|
|
338
|
+
.build(),
|
|
339
|
+
])
|
|
340
|
+
.build();
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### Research Team
|
|
344
|
+
|
|
345
|
+
```typescript
|
|
346
|
+
const researchWorkflow = WorkflowConfigBuilder.create()
|
|
347
|
+
.withId('research-team')
|
|
348
|
+
.withName('Research Workflow')
|
|
349
|
+
.addAgents([researcher1, researcher2, statistician, writer])
|
|
350
|
+
.addSteps([
|
|
351
|
+
// Literature review (parallel)
|
|
352
|
+
StepBuilder.create()
|
|
353
|
+
.withId('literature')
|
|
354
|
+
.withAgents(['researcher-1', 'researcher-2'])
|
|
355
|
+
.withExecutionType('parallel')
|
|
356
|
+
.withInstructions('Review relevant literature.')
|
|
357
|
+
.build(),
|
|
358
|
+
// Statistical analysis
|
|
359
|
+
StepBuilder.create()
|
|
360
|
+
.withId('analysis')
|
|
361
|
+
.withAgents(['statistician'])
|
|
362
|
+
.withExecutionType('sequential')
|
|
363
|
+
.withInstructions('Perform statistical analysis.')
|
|
364
|
+
.build(),
|
|
365
|
+
// Paper writing
|
|
366
|
+
StepBuilder.create()
|
|
367
|
+
.withId('writing')
|
|
368
|
+
.withAgents(['writer'])
|
|
369
|
+
.withExecutionType('sequential')
|
|
370
|
+
.withInstructions('Write the research paper.')
|
|
371
|
+
.build(),
|
|
372
|
+
// Peer review (collaborative)
|
|
373
|
+
StepBuilder.create()
|
|
374
|
+
.withId('peer-review')
|
|
375
|
+
.withAgents(['researcher-1', 'researcher-2', 'statistician'])
|
|
376
|
+
.withExecutionType('collaborative')
|
|
377
|
+
.withMaxIterations(2)
|
|
378
|
+
.build(),
|
|
379
|
+
])
|
|
380
|
+
.build();
|
|
381
|
+
```
|
|
382
|
+
|
|
383
|
+
### Content Creation
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
const contentWorkflow = WorkflowConfigBuilder.create()
|
|
387
|
+
.withId('content-creation')
|
|
388
|
+
.withName('Content Production Workflow')
|
|
389
|
+
.addAgents([researcher, writer, editor, designer])
|
|
390
|
+
.addSteps([
|
|
391
|
+
// Research
|
|
392
|
+
StepBuilder.create()
|
|
393
|
+
.withId('research')
|
|
394
|
+
.withAgents(['researcher'])
|
|
395
|
+
.withExecutionType('sequential')
|
|
396
|
+
.build(),
|
|
397
|
+
// Writing
|
|
398
|
+
StepBuilder.create()
|
|
399
|
+
.withId('writing')
|
|
400
|
+
.withAgents(['writer'])
|
|
401
|
+
.withExecutionType('sequential')
|
|
402
|
+
.build(),
|
|
403
|
+
// Parallel: Editing & Design
|
|
404
|
+
StepBuilder.create()
|
|
405
|
+
.withId('polish')
|
|
406
|
+
.withAgents(['editor', 'designer'])
|
|
407
|
+
.withExecutionType('parallel')
|
|
408
|
+
.build(),
|
|
409
|
+
])
|
|
410
|
+
.build();
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Business Analysis
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
const businessWorkflow = WorkflowConfigBuilder.create()
|
|
417
|
+
.withId('business-analysis')
|
|
418
|
+
.withName('Business Analysis Workflow')
|
|
419
|
+
.addAgents([marketAnalyst, financialAnalyst, strategist, executive])
|
|
420
|
+
.addSteps([
|
|
421
|
+
// Market & Financial analysis (parallel)
|
|
422
|
+
StepBuilder.create()
|
|
423
|
+
.withId('analysis')
|
|
424
|
+
.withAgents(['market-analyst', 'financial-analyst'])
|
|
425
|
+
.withExecutionType('parallel')
|
|
426
|
+
.build(),
|
|
427
|
+
// Strategic recommendations
|
|
428
|
+
StepBuilder.create()
|
|
429
|
+
.withId('strategy')
|
|
430
|
+
.withAgents(['strategist'])
|
|
431
|
+
.withExecutionType('sequential')
|
|
432
|
+
.build(),
|
|
433
|
+
// Executive review and decision
|
|
434
|
+
StepBuilder.create()
|
|
435
|
+
.withId('decision')
|
|
436
|
+
.withAgents(['executive'])
|
|
437
|
+
.withExecutionType('sequential')
|
|
438
|
+
.build(),
|
|
439
|
+
])
|
|
440
|
+
.build();
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
## Custom Patterns
|
|
444
|
+
|
|
445
|
+
### Dynamic Routing
|
|
446
|
+
|
|
447
|
+
Route to different steps based on results:
|
|
448
|
+
|
|
449
|
+
```typescript
|
|
450
|
+
StepBuilder.create()
|
|
451
|
+
.withId('router')
|
|
452
|
+
.withAgents(['router-agent'])
|
|
453
|
+
.withExecutionType('sequential')
|
|
454
|
+
.withNextStepResolver((results) => {
|
|
455
|
+
const content = results[0]?.content || '';
|
|
456
|
+
|
|
457
|
+
if (content.includes('TECHNICAL')) return 'technical-step';
|
|
458
|
+
if (content.includes('BUSINESS')) return 'business-step';
|
|
459
|
+
if (content.includes('DESIGN')) return 'design-step';
|
|
460
|
+
|
|
461
|
+
return 'default-step';
|
|
462
|
+
})
|
|
463
|
+
.build();
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
### Iterative Refinement
|
|
467
|
+
|
|
468
|
+
```typescript
|
|
469
|
+
let iteration = 0;
|
|
470
|
+
const maxIterations = 3;
|
|
471
|
+
|
|
472
|
+
StepBuilder.create()
|
|
473
|
+
.withId('refine')
|
|
474
|
+
.withAgents(['refiner'])
|
|
475
|
+
.withExecutionType('sequential')
|
|
476
|
+
.withNextStepResolver((results) => {
|
|
477
|
+
iteration++;
|
|
478
|
+
const quality = assessQuality(results[0]?.content);
|
|
479
|
+
|
|
480
|
+
if (quality >= 0.9 || iteration >= maxIterations) {
|
|
481
|
+
return null; // Done
|
|
482
|
+
}
|
|
483
|
+
return 'refine'; // Loop back
|
|
484
|
+
})
|
|
485
|
+
.build();
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
### Conditional Branching
|
|
489
|
+
|
|
490
|
+
```typescript
|
|
491
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
492
|
+
.addSteps([
|
|
493
|
+
// Main step
|
|
494
|
+
StepBuilder.create()
|
|
495
|
+
.withId('main')
|
|
496
|
+
.withAgents(['main-agent'])
|
|
497
|
+
.withExecutionType('sequential')
|
|
498
|
+
.build(),
|
|
499
|
+
|
|
500
|
+
// Branch A (if condition 1)
|
|
501
|
+
StepBuilder.create()
|
|
502
|
+
.withId('branch-a')
|
|
503
|
+
.withAgents(['agent-a'])
|
|
504
|
+
.withExecutionType('conditional')
|
|
505
|
+
.withCondition((results) => checkConditionA(results))
|
|
506
|
+
.build(),
|
|
507
|
+
|
|
508
|
+
// Branch B (if condition 2)
|
|
509
|
+
StepBuilder.create()
|
|
510
|
+
.withId('branch-b')
|
|
511
|
+
.withAgents(['agent-b'])
|
|
512
|
+
.withExecutionType('conditional')
|
|
513
|
+
.withCondition((results) => checkConditionB(results))
|
|
514
|
+
.build(),
|
|
515
|
+
|
|
516
|
+
// Merge step
|
|
517
|
+
StepBuilder.create()
|
|
518
|
+
.withId('merge')
|
|
519
|
+
.withAgents(['merger'])
|
|
520
|
+
.withExecutionType('sequential')
|
|
521
|
+
.build(),
|
|
522
|
+
])
|
|
523
|
+
.build();
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
### Progressive Depth
|
|
527
|
+
|
|
528
|
+
Start shallow, go deeper based on needs:
|
|
529
|
+
|
|
530
|
+
```typescript
|
|
531
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
532
|
+
.addSteps([
|
|
533
|
+
// Level 1: Quick analysis
|
|
534
|
+
StepBuilder.create()
|
|
535
|
+
.withId('level-1')
|
|
536
|
+
.withAgents(['quick-analyst'])
|
|
537
|
+
.withExecutionType('sequential')
|
|
538
|
+
.withNextStepResolver((results) => {
|
|
539
|
+
const needsDeeper = results[0]?.content.includes('COMPLEX');
|
|
540
|
+
return needsDeeper ? 'level-2' : null;
|
|
541
|
+
})
|
|
542
|
+
.build(),
|
|
543
|
+
|
|
544
|
+
// Level 2: Deeper analysis (conditional)
|
|
545
|
+
StepBuilder.create()
|
|
546
|
+
.withId('level-2')
|
|
547
|
+
.withAgents(['deep-analyst'])
|
|
548
|
+
.withExecutionType('conditional')
|
|
549
|
+
.withCondition((results) => results.has('level-1'))
|
|
550
|
+
.withNextStepResolver((results) => {
|
|
551
|
+
const needsExpert = results[0]?.content.includes('EXPERT_NEEDED');
|
|
552
|
+
return needsExpert ? 'level-3' : null;
|
|
553
|
+
})
|
|
554
|
+
.build(),
|
|
555
|
+
|
|
556
|
+
// Level 3: Expert analysis (conditional)
|
|
557
|
+
StepBuilder.create()
|
|
558
|
+
.withId('level-3')
|
|
559
|
+
.withAgents(['expert'])
|
|
560
|
+
.withExecutionType('conditional')
|
|
561
|
+
.withCondition((results) => results.has('level-2'))
|
|
562
|
+
.build(),
|
|
563
|
+
])
|
|
564
|
+
.build();
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
## Best Practices
|
|
568
|
+
|
|
569
|
+
### 1. Choose the Right Execution Type
|
|
570
|
+
|
|
571
|
+
| Pattern | Use Sequential | Use Parallel | Use Collaborative |
|
|
572
|
+
| --------------------- | -------------- | ------------ | ----------------- |
|
|
573
|
+
| Dependent tasks | ✅ | ❌ | ❌ |
|
|
574
|
+
| Independent tasks | ❌ | ✅ | ❌ |
|
|
575
|
+
| Need discussion | ❌ | ❌ | ✅ |
|
|
576
|
+
| Quality review | ✅ | ❌ | ❌ |
|
|
577
|
+
| Multiple perspectives | ❌ | ✅ | ✅ |
|
|
578
|
+
| Consensus building | ❌ | ❌ | ✅ |
|
|
579
|
+
|
|
580
|
+
### 2. Optimize for Performance
|
|
581
|
+
|
|
582
|
+
- **Use parallel** when tasks are independent
|
|
583
|
+
- **Limit iterations** in collaborative steps
|
|
584
|
+
- **Set timeouts** for each step
|
|
585
|
+
- **Cache expensive results** in context
|
|
586
|
+
|
|
587
|
+
### 3. Design for Maintainability
|
|
588
|
+
|
|
589
|
+
- **Clear step names** describe what happens
|
|
590
|
+
- **Meaningful IDs** for steps and agents
|
|
591
|
+
- **Document conditions** and routing logic
|
|
592
|
+
- **Use result transformers** to normalize data
|
|
593
|
+
|
|
594
|
+
### 4. Handle Errors Gracefully
|
|
595
|
+
|
|
596
|
+
- **Add conditional steps** for error recovery
|
|
597
|
+
- **Implement fallback paths** in resolvers
|
|
598
|
+
- **Use onAfterStep hooks** to validate results
|
|
599
|
+
- **Store error context** for debugging
|
|
600
|
+
|
|
601
|
+
### 5. Context Management
|
|
602
|
+
|
|
603
|
+
```typescript
|
|
604
|
+
// Store intermediate results
|
|
605
|
+
context.sharedData.set('step1-result', data);
|
|
606
|
+
|
|
607
|
+
// Retrieve for later steps
|
|
608
|
+
const previous = context.sharedData.get('step1-result');
|
|
609
|
+
|
|
610
|
+
// Clean up when done
|
|
611
|
+
context.sharedData.delete('temporary-data');
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
### 6. Testing Workflows
|
|
615
|
+
|
|
616
|
+
```typescript
|
|
617
|
+
// Test with simulated models
|
|
618
|
+
class TestModel extends StandardModelBase {
|
|
619
|
+
constructor(name: string, mockResponse: string) {
|
|
620
|
+
super({ name }, async () => mockResponse);
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// Test individual steps
|
|
625
|
+
const testResult = await executor.executeStep(step, agentsMap, testContext);
|
|
626
|
+
|
|
627
|
+
// Test complete workflows
|
|
628
|
+
const testWorkflow = workflow; // Your workflow
|
|
629
|
+
const result = await executor.execute(testWorkflow, 'test input');
|
|
630
|
+
|
|
631
|
+
expect(result.success).toBe(true);
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
## Example: Complete Custom Pattern
|
|
635
|
+
|
|
636
|
+
```typescript
|
|
637
|
+
/**
|
|
638
|
+
* Research-Debate-Conclude Pattern
|
|
639
|
+
*
|
|
640
|
+
* 1. Multiple researchers gather information (parallel)
|
|
641
|
+
* 2. Experts debate findings (collaborative)
|
|
642
|
+
* 3. Synthesizer creates final report (sequential)
|
|
643
|
+
*/
|
|
644
|
+
const researchDebateWorkflow = WorkflowConfigBuilder.create()
|
|
645
|
+
.withId('research-debate')
|
|
646
|
+
.withName('Research-Debate-Conclude')
|
|
647
|
+
.addAgents([
|
|
648
|
+
researcher1Agent,
|
|
649
|
+
researcher2Agent,
|
|
650
|
+
expert1Agent,
|
|
651
|
+
expert2Agent,
|
|
652
|
+
expert3Agent,
|
|
653
|
+
synthesizerAgent,
|
|
654
|
+
])
|
|
655
|
+
.addSteps([
|
|
656
|
+
// Phase 1: Research (parallel)
|
|
657
|
+
StepBuilder.create()
|
|
658
|
+
.withId('research')
|
|
659
|
+
.withName('Information Gathering')
|
|
660
|
+
.withAgents(['researcher-1', 'researcher-2'])
|
|
661
|
+
.withExecutionType('parallel')
|
|
662
|
+
.withInstructions('Gather comprehensive information on the topic.')
|
|
663
|
+
.withResultTransformer((results) => {
|
|
664
|
+
// Combine research findings
|
|
665
|
+
return {
|
|
666
|
+
findings: results.map((r) => r.content),
|
|
667
|
+
sources: results.length,
|
|
668
|
+
};
|
|
669
|
+
})
|
|
670
|
+
.build(),
|
|
671
|
+
|
|
672
|
+
// Phase 2: Expert debate (collaborative)
|
|
673
|
+
StepBuilder.create()
|
|
674
|
+
.withId('debate')
|
|
675
|
+
.withName('Expert Discussion')
|
|
676
|
+
.withAgents(['expert-1', 'expert-2', 'expert-3'])
|
|
677
|
+
.withExecutionType('collaborative')
|
|
678
|
+
.withMaxIterations(3)
|
|
679
|
+
.withInstructions('Discuss the research findings and reach expert consensus.')
|
|
680
|
+
.withCompletionCondition((results, iteration) => {
|
|
681
|
+
if (iteration < 2) return false;
|
|
682
|
+
|
|
683
|
+
// Check for consensus keywords
|
|
684
|
+
const lastRound = results.filter((r) => r.iteration === iteration);
|
|
685
|
+
const consensusCount = lastRound.filter(
|
|
686
|
+
(r) =>
|
|
687
|
+
r.content.toLowerCase().includes('agree') ||
|
|
688
|
+
r.content.toLowerCase().includes('consensus')
|
|
689
|
+
).length;
|
|
690
|
+
|
|
691
|
+
return consensusCount >= 2; // Majority agreement
|
|
692
|
+
})
|
|
693
|
+
.build(),
|
|
694
|
+
|
|
695
|
+
// Phase 3: Synthesis (sequential)
|
|
696
|
+
StepBuilder.create()
|
|
697
|
+
.withId('synthesis')
|
|
698
|
+
.withName('Final Report')
|
|
699
|
+
.withAgents(['synthesizer'])
|
|
700
|
+
.withExecutionType('sequential')
|
|
701
|
+
.withInstructions(
|
|
702
|
+
'Create a comprehensive final report synthesizing ' + 'the research and expert debate.'
|
|
703
|
+
)
|
|
704
|
+
.build(),
|
|
705
|
+
])
|
|
706
|
+
.withFinalResultGenerator(async (stepResults, context) => {
|
|
707
|
+
const research = stepResults.get('research');
|
|
708
|
+
const debate = stepResults.get('debate');
|
|
709
|
+
const synthesis = stepResults.get('synthesis');
|
|
710
|
+
|
|
711
|
+
return `
|
|
712
|
+
# Research-Debate-Conclude Report
|
|
713
|
+
|
|
714
|
+
## Research Phase
|
|
715
|
+
${research?.map((r) => `- ${r.agentId}: ${r.content}`).join('\n')}
|
|
716
|
+
|
|
717
|
+
## Expert Debate (${debate?.length} contributions)
|
|
718
|
+
${debate
|
|
719
|
+
?.slice(-3)
|
|
720
|
+
.map((r) => `- ${r.agentId}: ${r.content.substring(0, 100)}...`)
|
|
721
|
+
.join('\n')}
|
|
722
|
+
|
|
723
|
+
## Final Synthesis
|
|
724
|
+
${synthesis?.[0]?.content || 'No synthesis available'}
|
|
725
|
+
`.trim();
|
|
726
|
+
})
|
|
727
|
+
.build();
|
|
728
|
+
|
|
729
|
+
// Execute
|
|
730
|
+
const result = await executor.execute(
|
|
731
|
+
researchDebateWorkflow,
|
|
732
|
+
'What are the implications of quantum computing on cryptography?'
|
|
733
|
+
);
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
---
|
|
737
|
+
|
|
738
|
+
**Next**: [API Reference](./api-reference.md) →
|
|
739
|
+
|
|
740
|
+
**Previous**: [Architecture](./architecture.md) ←
|