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,771 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
Complete API documentation for SocietyAI.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Builders](#builders)
|
|
8
|
+
- [Core Interfaces](#core-interfaces)
|
|
9
|
+
- [Executors](#executors)
|
|
10
|
+
- [Models](#models)
|
|
11
|
+
- [Communication](#communication)
|
|
12
|
+
- [Utilities](#utilities)
|
|
13
|
+
- [Error Types](#error-types)
|
|
14
|
+
|
|
15
|
+
## Builders
|
|
16
|
+
|
|
17
|
+
### RoleBuilder
|
|
18
|
+
|
|
19
|
+
Create agent roles with specific behaviors.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
class RoleBuilder {
|
|
23
|
+
static create(): RoleBuilder;
|
|
24
|
+
|
|
25
|
+
withId(id: string): this;
|
|
26
|
+
withName(name: string): this;
|
|
27
|
+
withDescription(description: string): this;
|
|
28
|
+
withSystemPrompt(prompt: string): this;
|
|
29
|
+
withCapabilities(capabilities: string[]): this;
|
|
30
|
+
withConstraints(constraints: string[]): this;
|
|
31
|
+
withPromptTemplate(template: string): this;
|
|
32
|
+
|
|
33
|
+
build(): AgentRole;
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Example**:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
const role = RoleBuilder.create()
|
|
41
|
+
.withId('analyst')
|
|
42
|
+
.withName('Data Analyst')
|
|
43
|
+
.withSystemPrompt('You analyze data objectively.')
|
|
44
|
+
.withCapabilities(['data-analysis', 'statistics'])
|
|
45
|
+
.withConstraints(['No subjective opinions'])
|
|
46
|
+
.build();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### AgentBuilder
|
|
50
|
+
|
|
51
|
+
Configure individual agents.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
class AgentBuilder {
|
|
55
|
+
static create(): AgentBuilder;
|
|
56
|
+
|
|
57
|
+
withId(id: string): this;
|
|
58
|
+
withName(name: string): this;
|
|
59
|
+
withRole(role: AgentRole): this;
|
|
60
|
+
withModel(model: AIModel): this;
|
|
61
|
+
canCommunicateWith(agentIds: string[]): this;
|
|
62
|
+
withPriority(priority: number): this;
|
|
63
|
+
withInitialContext(context: Record<string, unknown>): this;
|
|
64
|
+
|
|
65
|
+
build(): AgentConfig;
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**Example**:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
const agent = AgentBuilder.create()
|
|
73
|
+
.withId('analyst-1')
|
|
74
|
+
.withRole(analystRole)
|
|
75
|
+
.withModel(model)
|
|
76
|
+
.canCommunicateWith(['reviewer-1'])
|
|
77
|
+
.withPriority(1)
|
|
78
|
+
.build();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### StepBuilder
|
|
82
|
+
|
|
83
|
+
Define workflow steps.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
class StepBuilder {
|
|
87
|
+
static create(): StepBuilder;
|
|
88
|
+
|
|
89
|
+
withId(id: string): this;
|
|
90
|
+
withName(name: string): this;
|
|
91
|
+
withDescription(description: string): this;
|
|
92
|
+
withAgents(agentIds: string[]): this;
|
|
93
|
+
withExecutionType(type: WorkflowStepExecutionType): this;
|
|
94
|
+
withInstructions(instructions: string): this;
|
|
95
|
+
withPromptTemplate(template: string): this;
|
|
96
|
+
withMaxIterations(max: number): this;
|
|
97
|
+
withCompletionCondition(condition: (results: StepResult[], iteration: number) => boolean): this;
|
|
98
|
+
withResultTransformer(transformer: (results: StepResult[] | StepResult) => unknown): this;
|
|
99
|
+
withCondition(condition: (previousResults: Map<string, StepResult[]>) => boolean): this;
|
|
100
|
+
withNextSteps(stepIds: string[]): this;
|
|
101
|
+
withNextStepResolver(resolver: (results: StepResult[]) => string | null): this;
|
|
102
|
+
|
|
103
|
+
build(): WorkflowStep;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
**Example**:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
const step = StepBuilder.create()
|
|
111
|
+
.withId('analysis')
|
|
112
|
+
.withName('Data Analysis')
|
|
113
|
+
.withAgents(['analyst-1', 'analyst-2'])
|
|
114
|
+
.withExecutionType('parallel')
|
|
115
|
+
.withInstructions('Analyze the data thoroughly.')
|
|
116
|
+
.withMaxIterations(3)
|
|
117
|
+
.build();
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### WorkflowConfigBuilder
|
|
121
|
+
|
|
122
|
+
Build complete workflows.
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
class WorkflowConfigBuilder {
|
|
126
|
+
static create(): WorkflowConfigBuilder;
|
|
127
|
+
|
|
128
|
+
withId(id: string): this;
|
|
129
|
+
withName(name: string): this;
|
|
130
|
+
withDescription(description: string): this;
|
|
131
|
+
addAgent(agent: AgentConfig): this;
|
|
132
|
+
addAgents(agents: AgentConfig[]): this;
|
|
133
|
+
addStep(step: WorkflowStep): this;
|
|
134
|
+
addSteps(steps: WorkflowStep[]): this;
|
|
135
|
+
withEntryStep(stepId: string): this;
|
|
136
|
+
withGlobalContext(context: Record<string, unknown>): this;
|
|
137
|
+
onBeforeStep(handler: (step: WorkflowStep, context: WorkflowContext) => Promise<void>): this;
|
|
138
|
+
onAfterStep(
|
|
139
|
+
handler: (step: WorkflowStep, results: StepResult[], context: WorkflowContext) => Promise<void>
|
|
140
|
+
): this;
|
|
141
|
+
withFinalResultGenerator(
|
|
142
|
+
generator: (results: Map<string, StepResult[]>, context: WorkflowContext) => Promise<string>
|
|
143
|
+
): this;
|
|
144
|
+
|
|
145
|
+
build(): WorkflowConfig;
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**Example**:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
const workflow = WorkflowConfigBuilder.create()
|
|
153
|
+
.withId('my-workflow')
|
|
154
|
+
.withName('My Workflow')
|
|
155
|
+
.addAgents([agent1, agent2])
|
|
156
|
+
.addSteps([step1, step2])
|
|
157
|
+
.withGlobalContext({ version: '1.0' })
|
|
158
|
+
.onBeforeStep(async (step, ctx) => {
|
|
159
|
+
console.log(`Starting ${step.name}`);
|
|
160
|
+
})
|
|
161
|
+
.withFinalResultGenerator(async (results, ctx) => {
|
|
162
|
+
return `Generated output`;
|
|
163
|
+
})
|
|
164
|
+
.build();
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Core Interfaces
|
|
168
|
+
|
|
169
|
+
### AIModel
|
|
170
|
+
|
|
171
|
+
Interface for AI models.
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
interface AIModel {
|
|
175
|
+
process(prompt: unknown, signal?: AbortSignal): Promise<string>;
|
|
176
|
+
name(): string;
|
|
177
|
+
supportsPromptType(promptType: string): boolean;
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### AgentRole
|
|
182
|
+
|
|
183
|
+
Defines agent behavior.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
interface AgentRole {
|
|
187
|
+
id: string;
|
|
188
|
+
name: string;
|
|
189
|
+
description?: string;
|
|
190
|
+
systemPrompt: string;
|
|
191
|
+
capabilities?: string[];
|
|
192
|
+
constraints?: string[];
|
|
193
|
+
promptTemplate?: string;
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### AgentConfig
|
|
198
|
+
|
|
199
|
+
Complete agent configuration.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
interface AgentConfig {
|
|
203
|
+
id: string;
|
|
204
|
+
name?: string;
|
|
205
|
+
role: AgentRole;
|
|
206
|
+
model: AIModel;
|
|
207
|
+
canCommunicateWith?: string[];
|
|
208
|
+
priority?: number;
|
|
209
|
+
initialContext?: Record<string, unknown>;
|
|
210
|
+
retryConfig?: {
|
|
211
|
+
maxRetries?: number;
|
|
212
|
+
initialBackoff?: number;
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### WorkflowStep
|
|
218
|
+
|
|
219
|
+
A step in the workflow.
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
type WorkflowStepExecutionType = 'sequential' | 'parallel' | 'collaborative' | 'conditional';
|
|
223
|
+
|
|
224
|
+
interface WorkflowStep {
|
|
225
|
+
id: string;
|
|
226
|
+
name: string;
|
|
227
|
+
description?: string;
|
|
228
|
+
agentIds: string[];
|
|
229
|
+
executionType: WorkflowStepExecutionType;
|
|
230
|
+
instructions?: string;
|
|
231
|
+
promptTemplate?: string;
|
|
232
|
+
maxIterations?: number;
|
|
233
|
+
completionCondition?: (results: StepResult[], iteration: number) => boolean;
|
|
234
|
+
resultTransformer?: (results: StepResult[] | StepResult) => unknown;
|
|
235
|
+
condition?: (previousResults: Map<string, StepResult[]>) => boolean;
|
|
236
|
+
nextSteps?: string[];
|
|
237
|
+
nextStepResolver?: (results: StepResult[]) => string | null;
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### StepResult
|
|
242
|
+
|
|
243
|
+
Result from a step execution.
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
interface StepResult {
|
|
247
|
+
agentId: string;
|
|
248
|
+
stepId: string;
|
|
249
|
+
content: string;
|
|
250
|
+
metadata?: Record<string, unknown>;
|
|
251
|
+
timestamp: number;
|
|
252
|
+
success: boolean;
|
|
253
|
+
error?: Error;
|
|
254
|
+
iteration?: number;
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### WorkflowConfig
|
|
259
|
+
|
|
260
|
+
Complete workflow definition.
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
interface WorkflowConfig {
|
|
264
|
+
id: string;
|
|
265
|
+
name: string;
|
|
266
|
+
description?: string;
|
|
267
|
+
steps: WorkflowStep[];
|
|
268
|
+
entryStepId?: string;
|
|
269
|
+
agents: AgentConfig[];
|
|
270
|
+
globalContext?: Record<string, unknown>;
|
|
271
|
+
onBeforeStep?: (step: WorkflowStep, context: WorkflowContext) => Promise<void>;
|
|
272
|
+
onAfterStep?: (
|
|
273
|
+
step: WorkflowStep,
|
|
274
|
+
results: StepResult[],
|
|
275
|
+
context: WorkflowContext
|
|
276
|
+
) => Promise<void>;
|
|
277
|
+
finalResultGenerator?: (
|
|
278
|
+
results: Map<string, StepResult[]>,
|
|
279
|
+
context: WorkflowContext
|
|
280
|
+
) => Promise<string>;
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### WorkflowContext
|
|
285
|
+
|
|
286
|
+
Execution context.
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
interface WorkflowContext {
|
|
290
|
+
input: string;
|
|
291
|
+
sharedData: Map<string, unknown>;
|
|
292
|
+
stepResults: Map<string, StepResult[]>;
|
|
293
|
+
messageHistory: AgentMessage[];
|
|
294
|
+
metadata: Record<string, unknown>;
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### WorkflowResult
|
|
299
|
+
|
|
300
|
+
Final workflow result.
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
interface WorkflowResult {
|
|
304
|
+
success: boolean;
|
|
305
|
+
output: string;
|
|
306
|
+
stepResults: Map<string, StepResult[]>;
|
|
307
|
+
messages: AgentMessage[];
|
|
308
|
+
duration: number;
|
|
309
|
+
errors?: Error[];
|
|
310
|
+
}
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
## Executors
|
|
314
|
+
|
|
315
|
+
### DefaultWorkflowExecutor
|
|
316
|
+
|
|
317
|
+
Executes workflows.
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
class DefaultWorkflowExecutor implements WorkflowExecutor {
|
|
321
|
+
constructor(observer?: SocietyObserver);
|
|
322
|
+
|
|
323
|
+
execute(workflow: WorkflowConfig, input: string, signal?: AbortSignal): Promise<WorkflowResult>;
|
|
324
|
+
|
|
325
|
+
executeStep(
|
|
326
|
+
step: WorkflowStep,
|
|
327
|
+
agents: Map<string, AgentConfig>,
|
|
328
|
+
context: WorkflowContext,
|
|
329
|
+
signal?: AbortSignal
|
|
330
|
+
): Promise<StepResult[]>;
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**Example**:
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
const executor = new DefaultWorkflowExecutor(observer);
|
|
338
|
+
const result = await executor.execute(workflow, 'Input text');
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## Models
|
|
342
|
+
|
|
343
|
+
### StandardModelBase
|
|
344
|
+
|
|
345
|
+
Base class for AI models.
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
class StandardModelBase implements AIModel {
|
|
349
|
+
constructor(
|
|
350
|
+
options?: Partial<StandardModelOptions>,
|
|
351
|
+
processFunc?: (prompt: unknown, signal?: AbortSignal) => Promise<string>
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
process(prompt: unknown, signal?: AbortSignal): Promise<string>;
|
|
355
|
+
name(): string;
|
|
356
|
+
supportsPromptType(promptType: string): boolean;
|
|
357
|
+
|
|
358
|
+
withName(name: string): this;
|
|
359
|
+
withAdapter(adapter: ModelAdapter): this;
|
|
360
|
+
withTimeout(timeout: number): this;
|
|
361
|
+
withSupportedPromptTypes(types: string[]): this;
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
**Example**:
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
class MyModel extends StandardModelBase {
|
|
369
|
+
constructor() {
|
|
370
|
+
super({ name: 'MyModel', timeout: 30000 }, async (prompt) => {
|
|
371
|
+
// Your AI API call here
|
|
372
|
+
return response;
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### StandardModelOptions
|
|
379
|
+
|
|
380
|
+
Options for StandardModelBase.
|
|
381
|
+
|
|
382
|
+
```typescript
|
|
383
|
+
interface StandardModelOptions {
|
|
384
|
+
name: string;
|
|
385
|
+
timeout: number;
|
|
386
|
+
retryOptions: RetryOptions;
|
|
387
|
+
logger: Logger;
|
|
388
|
+
adapter?: ModelAdapter;
|
|
389
|
+
}
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
### ModelAdapter
|
|
393
|
+
|
|
394
|
+
Adapter for different prompt formats.
|
|
395
|
+
|
|
396
|
+
```typescript
|
|
397
|
+
interface ModelAdapter {
|
|
398
|
+
convertPrompt(genericPrompt: unknown): Promise<unknown>;
|
|
399
|
+
convertResponse(specificResponse: unknown): Promise<string>;
|
|
400
|
+
getSupportedPromptTypes(): string[];
|
|
401
|
+
}
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
### TextModelAdapter
|
|
405
|
+
|
|
406
|
+
Simple text-based adapter.
|
|
407
|
+
|
|
408
|
+
```typescript
|
|
409
|
+
class TextModelAdapter implements ModelAdapter {
|
|
410
|
+
async convertPrompt(genericPrompt: unknown): Promise<unknown>;
|
|
411
|
+
async convertResponse(specificResponse: unknown): Promise<string>;
|
|
412
|
+
getSupportedPromptTypes(): string[];
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
## Communication
|
|
417
|
+
|
|
418
|
+
### MessageBus
|
|
419
|
+
|
|
420
|
+
Communication channel for agents.
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
class MessageBus implements CommunicationChannel {
|
|
424
|
+
async send(message: AgentMessage): Promise<void>;
|
|
425
|
+
subscribe(agentId: string, handler: (message: AgentMessage) => void): void;
|
|
426
|
+
unsubscribe(agentId: string): void;
|
|
427
|
+
getHistory(filter?: { from?: string; to?: string; type?: string }): AgentMessage[];
|
|
428
|
+
clearHistory(): void;
|
|
429
|
+
}
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### AgentMessage
|
|
433
|
+
|
|
434
|
+
Message between agents.
|
|
435
|
+
|
|
436
|
+
```typescript
|
|
437
|
+
interface AgentMessage {
|
|
438
|
+
from: string;
|
|
439
|
+
to: string | 'broadcast';
|
|
440
|
+
type: 'request' | 'response' | 'notification' | 'data' | 'feedback' | 'validation';
|
|
441
|
+
content: string;
|
|
442
|
+
data?: Record<string, unknown>;
|
|
443
|
+
timestamp: number;
|
|
444
|
+
messageId: string;
|
|
445
|
+
replyTo?: string;
|
|
446
|
+
}
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Utilities
|
|
450
|
+
|
|
451
|
+
### Logging
|
|
452
|
+
|
|
453
|
+
```typescript
|
|
454
|
+
enum LogLevel {
|
|
455
|
+
SILENT = 0,
|
|
456
|
+
ERROR = 1,
|
|
457
|
+
INFO = 2,
|
|
458
|
+
DEBUG = 3,
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function setGlobalLogLevel(level: LogLevel): void;
|
|
462
|
+
function getLogger(): Logger;
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
**Example**:
|
|
466
|
+
|
|
467
|
+
```typescript
|
|
468
|
+
import { setGlobalLogLevel, LogLevel } from 'societyai';
|
|
469
|
+
setGlobalLogLevel(LogLevel.DEBUG);
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Retry
|
|
473
|
+
|
|
474
|
+
```typescript
|
|
475
|
+
interface RetryOptions {
|
|
476
|
+
maxRetries: number;
|
|
477
|
+
initialBackoff: number;
|
|
478
|
+
maxBackoff: number;
|
|
479
|
+
backoffFactor: number;
|
|
480
|
+
jitter: boolean;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
function defaultRetryOptions(): RetryOptions;
|
|
484
|
+
|
|
485
|
+
async function withRetry<T>(
|
|
486
|
+
fn: () => Promise<T>,
|
|
487
|
+
options: RetryOptions,
|
|
488
|
+
signal?: AbortSignal
|
|
489
|
+
): Promise<T>;
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
### WorkerPool
|
|
493
|
+
|
|
494
|
+
Parallel task execution.
|
|
495
|
+
|
|
496
|
+
```typescript
|
|
497
|
+
class WorkerPool {
|
|
498
|
+
constructor(maxWorkers: number, signal?: AbortSignal);
|
|
499
|
+
|
|
500
|
+
submit<T>(task: () => Promise<T>): Promise<T>;
|
|
501
|
+
waitAll(): Promise<void>;
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
## Error Types
|
|
506
|
+
|
|
507
|
+
### SocietyError
|
|
508
|
+
|
|
509
|
+
Base error class.
|
|
510
|
+
|
|
511
|
+
```typescript
|
|
512
|
+
class SocietyError extends Error {
|
|
513
|
+
readonly code: string;
|
|
514
|
+
constructor(message: string, code?: string);
|
|
515
|
+
}
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
### Specific Error Types
|
|
519
|
+
|
|
520
|
+
```typescript
|
|
521
|
+
class ModelNotSupportedError extends SocietyError {}
|
|
522
|
+
class ProcessingFailedError extends SocietyError {}
|
|
523
|
+
class InvalidAgentCountError extends SocietyError {}
|
|
524
|
+
class NoModelsSpecifiedError extends SocietyError {}
|
|
525
|
+
class SynthesisModelRequiredError extends SocietyError {}
|
|
526
|
+
class OperationCancelledError extends SocietyError {}
|
|
527
|
+
class TimeoutError extends SocietyError {}
|
|
528
|
+
class InvalidConfigurationError extends SocietyError {}
|
|
529
|
+
```
|
|
530
|
+
|
|
531
|
+
### Error Utilities
|
|
532
|
+
|
|
533
|
+
```typescript
|
|
534
|
+
function isAbortError(error: Error): boolean;
|
|
535
|
+
function wrapError(error: Error, message: string): SocietyError;
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
## Observer Pattern
|
|
539
|
+
|
|
540
|
+
### SocietyObserver
|
|
541
|
+
|
|
542
|
+
Monitor workflow execution.
|
|
543
|
+
|
|
544
|
+
```typescript
|
|
545
|
+
interface SocietyObserver {
|
|
546
|
+
onSocietyStart(prompt: string, agentCount: number): void;
|
|
547
|
+
onAgentStart(agentId: number, modelName: string, prompt: unknown): void;
|
|
548
|
+
onAgentComplete(agentId: number, modelName: string, result: string): void;
|
|
549
|
+
onAgentError(agentId: number, modelName: string, error: Error): void;
|
|
550
|
+
onPhaseStart(phase: string): void;
|
|
551
|
+
onPhaseComplete(phase: string): void;
|
|
552
|
+
onSocietyComplete(finalResult: string): void;
|
|
553
|
+
}
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
**Example**:
|
|
557
|
+
|
|
558
|
+
```typescript
|
|
559
|
+
const observer: SocietyObserver = {
|
|
560
|
+
onSocietyStart: (prompt, count) => console.log(`Starting with ${count} agents`),
|
|
561
|
+
onAgentStart: (id, model, prompt) => console.log(`Agent ${id} started`),
|
|
562
|
+
onAgentComplete: (id, model, result) => console.log(`Agent ${id} completed`),
|
|
563
|
+
onAgentError: (id, model, error) => console.error(`Agent ${id} error:`, error),
|
|
564
|
+
onPhaseStart: (phase) => console.log(`Phase: ${phase}`),
|
|
565
|
+
onPhaseComplete: (phase) => console.log(`Phase ${phase} done`),
|
|
566
|
+
onSocietyComplete: (result) => console.log('Complete!'),
|
|
567
|
+
};
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
## Fluent Society Builder API
|
|
571
|
+
|
|
572
|
+
### Society
|
|
573
|
+
|
|
574
|
+
Main entry point for creating agent societies with fluent API.
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
class Society {
|
|
578
|
+
static create(): Society;
|
|
579
|
+
|
|
580
|
+
withName(name: string): this;
|
|
581
|
+
withGlobalContext(context: Record<string, unknown>): this;
|
|
582
|
+
withObserver(observer: SocietyObserver): this;
|
|
583
|
+
|
|
584
|
+
addAgent(builderFn: (builder: FluentAgentBuilder) => FluentAgentBuilder): this;
|
|
585
|
+
addAgents(configs: AgentConfig[]): this;
|
|
586
|
+
|
|
587
|
+
// Pipeline patterns
|
|
588
|
+
chain(agentIds: string[]): this;
|
|
589
|
+
scatterGather(aggregateFn: (results: StepResult[]) => string): this;
|
|
590
|
+
usePipeline(builderFn: (builder: FluentPipelineBuilder) => FluentPipelineBuilder): this;
|
|
591
|
+
|
|
592
|
+
execute(input: string, signal?: AbortSignal): Promise<WorkflowResult>;
|
|
593
|
+
}
|
|
594
|
+
```
|
|
595
|
+
|
|
596
|
+
**Example**:
|
|
597
|
+
|
|
598
|
+
```typescript
|
|
599
|
+
const result = await Society.create()
|
|
600
|
+
.withName('Analysis Team')
|
|
601
|
+
.addAgent((a) =>
|
|
602
|
+
a
|
|
603
|
+
.withId('analyst')
|
|
604
|
+
.withRole((r) => r.withSystemPrompt('Analyze data'))
|
|
605
|
+
.withModel(model)
|
|
606
|
+
)
|
|
607
|
+
.scatterGather(Strategies.consensus(0.7).aggregate)
|
|
608
|
+
.execute('Analyze market trends');
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
### Pipelines
|
|
612
|
+
|
|
613
|
+
Pre-built pipeline patterns for common workflows.
|
|
614
|
+
|
|
615
|
+
```typescript
|
|
616
|
+
const Pipelines = {
|
|
617
|
+
// Sequential chain execution
|
|
618
|
+
chain: (agentIds: string[]) => Pipeline;
|
|
619
|
+
|
|
620
|
+
// Parallel scatter-gather
|
|
621
|
+
scatterGather: (agentIds: string[], strategy: AggregationStrategy) => Pipeline;
|
|
622
|
+
|
|
623
|
+
// Iterative refinement
|
|
624
|
+
iterativeRefinement: (agentId: string, iterations: number) => Pipeline;
|
|
625
|
+
|
|
626
|
+
// Debate pattern
|
|
627
|
+
debate: (agent1Id: string, agent2Id: string, judgeId: string, rounds: number) => Pipeline;
|
|
628
|
+
|
|
629
|
+
// Review pattern
|
|
630
|
+
review: (drafterId: string, reviewerId: string) => Pipeline;
|
|
631
|
+
|
|
632
|
+
// Consensus building
|
|
633
|
+
consensus: (agentIds: string[], finalizerId: string, threshold: number) => Pipeline;
|
|
634
|
+
};
|
|
635
|
+
```
|
|
636
|
+
|
|
637
|
+
**Example**:
|
|
638
|
+
|
|
639
|
+
```typescript
|
|
640
|
+
const pipeline = Pipelines.review('writer', 'editor');
|
|
641
|
+
const result = await pipeline.execute(input, agents);
|
|
642
|
+
```
|
|
643
|
+
|
|
644
|
+
### Strategies
|
|
645
|
+
|
|
646
|
+
Result aggregation strategies for combining multiple agent outputs.
|
|
647
|
+
|
|
648
|
+
```typescript
|
|
649
|
+
const Strategies = {
|
|
650
|
+
// Basic strategies
|
|
651
|
+
concat: (separator?: string) => AggregationStrategy;
|
|
652
|
+
merge: () => AggregationStrategy;
|
|
653
|
+
first: () => AggregationStrategy;
|
|
654
|
+
last: () => AggregationStrategy;
|
|
655
|
+
longest: () => AggregationStrategy;
|
|
656
|
+
shortest: () => AggregationStrategy;
|
|
657
|
+
|
|
658
|
+
// Voting strategies
|
|
659
|
+
majority: () => AggregationStrategy;
|
|
660
|
+
consensus: (threshold: number) => AggregationStrategy;
|
|
661
|
+
weighted: (weights: Record<string, number>) => AggregationStrategy;
|
|
662
|
+
|
|
663
|
+
// Formatting
|
|
664
|
+
format: (format: 'json' | 'markdown' | 'yaml') => AggregationStrategy;
|
|
665
|
+
|
|
666
|
+
// Composition
|
|
667
|
+
compose: (...strategies: AggregationStrategy[]) => AggregationStrategy;
|
|
668
|
+
fallback: (primary: AggregationStrategy, backup: AggregationStrategy) => AggregationStrategy;
|
|
669
|
+
};
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
**Example**:
|
|
673
|
+
|
|
674
|
+
```typescript
|
|
675
|
+
// Use consensus with 70% threshold
|
|
676
|
+
const strategy = Strategies.consensus(0.7);
|
|
677
|
+
|
|
678
|
+
// Compose multiple strategies
|
|
679
|
+
const composedStrategy = Strategies.compose(
|
|
680
|
+
Strategies.filter((r) => r.success),
|
|
681
|
+
Strategies.longest(),
|
|
682
|
+
Strategies.format('markdown')
|
|
683
|
+
);
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
### Middlewares
|
|
687
|
+
|
|
688
|
+
Composable middleware for cross-cutting concerns.
|
|
689
|
+
|
|
690
|
+
```typescript
|
|
691
|
+
const Middlewares = {
|
|
692
|
+
// Logging
|
|
693
|
+
logging: (options?: { logInput?: boolean; logOutput?: boolean }) => Middleware;
|
|
694
|
+
|
|
695
|
+
// Performance
|
|
696
|
+
timing: (onComplete?: (duration: number) => void) => Middleware;
|
|
697
|
+
|
|
698
|
+
// Reliability
|
|
699
|
+
retry: (options: { maxAttempts: number; delay?: number }) => Middleware;
|
|
700
|
+
cache: (options: { ttl: number }) => Middleware;
|
|
701
|
+
circuitBreaker: (options: { threshold: number; timeout: number }) => Middleware;
|
|
702
|
+
|
|
703
|
+
// Rate limiting
|
|
704
|
+
rateLimit: (options: { maxRequests: number; windowMs: number }) => Middleware;
|
|
705
|
+
timeout: (ms: number) => Middleware;
|
|
706
|
+
|
|
707
|
+
// Utilities
|
|
708
|
+
dedupe: () => Middleware;
|
|
709
|
+
transform: (transformFn: (input: unknown) => unknown) => Middleware;
|
|
710
|
+
};
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
**Example**:
|
|
714
|
+
|
|
715
|
+
```typescript
|
|
716
|
+
const middleware = MiddlewareChain.create()
|
|
717
|
+
.use(Middlewares.logging({ logInput: true }))
|
|
718
|
+
.use(Middlewares.retry({ maxAttempts: 3 }))
|
|
719
|
+
.use(Middlewares.cache({ ttl: 60000 }))
|
|
720
|
+
.build();
|
|
721
|
+
|
|
722
|
+
const enhancedModel = middleware.wrap(originalModel);
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
### Helper Functions
|
|
726
|
+
|
|
727
|
+
Quick helpers for creating roles and agents.
|
|
728
|
+
|
|
729
|
+
```typescript
|
|
730
|
+
function createRole(
|
|
731
|
+
id: string,
|
|
732
|
+
systemPrompt: string,
|
|
733
|
+
options?: {
|
|
734
|
+
name?: string;
|
|
735
|
+
description?: string;
|
|
736
|
+
capabilities?: string[];
|
|
737
|
+
constraints?: string[];
|
|
738
|
+
promptTemplate?: string;
|
|
739
|
+
}
|
|
740
|
+
): AgentRole;
|
|
741
|
+
|
|
742
|
+
function createAgent(
|
|
743
|
+
id: string,
|
|
744
|
+
role: AgentRole,
|
|
745
|
+
model: AIModel,
|
|
746
|
+
options?: {
|
|
747
|
+
name?: string;
|
|
748
|
+
priority?: number;
|
|
749
|
+
canCommunicateWith?: string[];
|
|
750
|
+
}
|
|
751
|
+
): AgentConfig;
|
|
752
|
+
```
|
|
753
|
+
|
|
754
|
+
**Example**:
|
|
755
|
+
|
|
756
|
+
```typescript
|
|
757
|
+
const analystRole = createRole('analyst', 'You analyze data objectively', {
|
|
758
|
+
capabilities: ['data-analysis', 'statistics'],
|
|
759
|
+
constraints: ['No subjective opinions'],
|
|
760
|
+
});
|
|
761
|
+
|
|
762
|
+
const agent = createAgent('analyst-1', analystRole, model, {
|
|
763
|
+
priority: 10,
|
|
764
|
+
});
|
|
765
|
+
```
|
|
766
|
+
|
|
767
|
+
---
|
|
768
|
+
|
|
769
|
+
**Next**: [Advanced Features](./advanced.md) →
|
|
770
|
+
|
|
771
|
+
**Previous**: [Workflow Patterns](./workflows.md) ←
|