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.
Files changed (114) hide show
  1. package/CHANGELOG.md +111 -0
  2. package/LICENSE +21 -0
  3. package/README.md +879 -0
  4. package/dist/builder.d.ts +181 -0
  5. package/dist/builder.d.ts.map +1 -0
  6. package/dist/builder.js +667 -0
  7. package/dist/builder.js.map +1 -0
  8. package/dist/config.d.ts +43 -0
  9. package/dist/config.d.ts.map +1 -0
  10. package/dist/config.js +11 -0
  11. package/dist/config.js.map +1 -0
  12. package/dist/context.d.ts +107 -0
  13. package/dist/context.d.ts.map +1 -0
  14. package/dist/context.js +319 -0
  15. package/dist/context.js.map +1 -0
  16. package/dist/errors.d.ts +31 -0
  17. package/dist/errors.d.ts.map +1 -0
  18. package/dist/errors.js +85 -0
  19. package/dist/errors.js.map +1 -0
  20. package/dist/events.d.ts +219 -0
  21. package/dist/events.d.ts.map +1 -0
  22. package/dist/events.js +395 -0
  23. package/dist/events.js.map +1 -0
  24. package/dist/graph.d.ts +104 -0
  25. package/dist/graph.d.ts.map +1 -0
  26. package/dist/graph.js +366 -0
  27. package/dist/graph.js.map +1 -0
  28. package/dist/index.d.ts +28 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.js +113 -0
  31. package/dist/index.js.map +1 -0
  32. package/dist/logger.d.ts +13 -0
  33. package/dist/logger.d.ts.map +1 -0
  34. package/dist/logger.js +78 -0
  35. package/dist/logger.js.map +1 -0
  36. package/dist/memory.d.ts +146 -0
  37. package/dist/memory.d.ts.map +1 -0
  38. package/dist/memory.js +353 -0
  39. package/dist/memory.js.map +1 -0
  40. package/dist/metrics.d.ts +143 -0
  41. package/dist/metrics.d.ts.map +1 -0
  42. package/dist/metrics.js +271 -0
  43. package/dist/metrics.js.map +1 -0
  44. package/dist/middleware.d.ts +147 -0
  45. package/dist/middleware.d.ts.map +1 -0
  46. package/dist/middleware.js +484 -0
  47. package/dist/middleware.js.map +1 -0
  48. package/dist/models.d.ts +32 -0
  49. package/dist/models.d.ts.map +1 -0
  50. package/dist/models.js +211 -0
  51. package/dist/models.js.map +1 -0
  52. package/dist/patterns.d.ts +6 -0
  53. package/dist/patterns.d.ts.map +1 -0
  54. package/dist/patterns.js +68 -0
  55. package/dist/patterns.js.map +1 -0
  56. package/dist/pipeline.d.ts +84 -0
  57. package/dist/pipeline.d.ts.map +1 -0
  58. package/dist/pipeline.js +569 -0
  59. package/dist/pipeline.js.map +1 -0
  60. package/dist/retry.d.ts +5 -0
  61. package/dist/retry.d.ts.map +1 -0
  62. package/dist/retry.js +70 -0
  63. package/dist/retry.js.map +1 -0
  64. package/dist/society.d.ts +94 -0
  65. package/dist/society.d.ts.map +1 -0
  66. package/dist/society.js +721 -0
  67. package/dist/society.js.map +1 -0
  68. package/dist/strategies.d.ts +55 -0
  69. package/dist/strategies.d.ts.map +1 -0
  70. package/dist/strategies.js +678 -0
  71. package/dist/strategies.js.map +1 -0
  72. package/dist/tools.d.ts +88 -0
  73. package/dist/tools.d.ts.map +1 -0
  74. package/dist/tools.js +366 -0
  75. package/dist/tools.js.map +1 -0
  76. package/dist/types.d.ts +213 -0
  77. package/dist/types.d.ts.map +1 -0
  78. package/dist/types.js +19 -0
  79. package/dist/types.js.map +1 -0
  80. package/dist/validation.d.ts +64 -0
  81. package/dist/validation.d.ts.map +1 -0
  82. package/dist/validation.js +334 -0
  83. package/dist/validation.js.map +1 -0
  84. package/dist/worker-pool.d.ts +17 -0
  85. package/dist/worker-pool.d.ts.map +1 -0
  86. package/dist/worker-pool.js +80 -0
  87. package/dist/worker-pool.js.map +1 -0
  88. package/docs/README.md +468 -0
  89. package/docs/advanced.md +616 -0
  90. package/docs/aggregation-strategies.md +926 -0
  91. package/docs/api-reference.md +771 -0
  92. package/docs/architecture.md +648 -0
  93. package/docs/context-system.md +642 -0
  94. package/docs/event-system.md +1047 -0
  95. package/docs/examples.md +576 -0
  96. package/docs/getting-started.md +564 -0
  97. package/docs/graph-execution.md +389 -0
  98. package/docs/memory-system.md +497 -0
  99. package/docs/metrics-observability.md +560 -0
  100. package/docs/middleware-system.md +1038 -0
  101. package/docs/migration.md +296 -0
  102. package/docs/pipeline-patterns.md +761 -0
  103. package/docs/structured-output.md +612 -0
  104. package/docs/tool-calling.md +491 -0
  105. package/docs/workflows.md +740 -0
  106. package/examples/README.md +234 -0
  107. package/examples/advanced-patterns.ts +115 -0
  108. package/examples/complete-integration.ts +327 -0
  109. package/examples/graph-workflow.ts +161 -0
  110. package/examples/memory-system.ts +155 -0
  111. package/examples/metrics-tracking.ts +243 -0
  112. package/examples/structured-output.ts +231 -0
  113. package/examples/tool-calling.ts +163 -0
  114. package/package.json +94 -0
@@ -0,0 +1,576 @@
1
+ # Examples Index
2
+
3
+ This document provides an overview of all examples in the `examples/` directory and references to comprehensive feature guides.
4
+
5
+ ## Overview
6
+
7
+ The examples demonstrate various SocietyAI features from basic usage to advanced patterns. Each example is fully functional and includes detailed comments.
8
+
9
+ ## Available Examples
10
+
11
+ ### Graph Workflow (`graph-workflow.ts`)
12
+
13
+ **Description**: Demonstrates graph-based execution with DAG and cyclic workflows
14
+ **Features**:
15
+
16
+ - START, END, AGENT, PARALLEL, CONDITION nodes
17
+ - Conditional branching based on runtime conditions
18
+ - Parallel execution of multiple agents
19
+ - Cyclic graphs with loop detection
20
+
21
+ **Related guide**: [Graph Execution](./graph-execution.md)
22
+
23
+ ```bash
24
+ npx ts-node examples/graph-workflow.ts
25
+ ```
26
+
27
+ ### Tool Calling (`tool-calling.ts`)
28
+
29
+ **Description**: Shows how to give agents access to external tools
30
+ **Features**:
31
+
32
+ - Tool definition with ToolBuilder
33
+ - ToolRegistry for managing multiple tools
34
+ - Parameter validation with JSON Schema
35
+ - Built-in tools (calculator, string manipulation)
36
+ - Agent-tool interaction loop
37
+
38
+ **Related guide**: [Tool Calling](./tool-calling.md)
39
+
40
+ ```bash
41
+ npx ts-node examples/tool-calling.ts
42
+ ```
43
+
44
+ ### Memory System (`memory-system.ts`)
45
+
46
+ **Description**: Demonstrates multi-level memory management
47
+ **Features**:
48
+
49
+ - ShortTermMemory with decay and auto-summarization
50
+ - LongTermMemory with RAG integration
51
+ - EntityMemory for tracking facts
52
+ - Memory importance scoring
53
+ - Pruning strategies (LRU, importance-based, FIFO)
54
+
55
+ **Related guide**: [Memory System](./memory-system.md)
56
+
57
+ ```bash
58
+ npx ts-node examples/memory-system.ts
59
+ ```
60
+
61
+ ### Structured Output (`structured-output.ts`)
62
+
63
+ **Description**: Validates AI outputs against JSON schemas
64
+ **Features**:
65
+
66
+ - JSON Schema validation
67
+ - Automatic retry with error feedback
68
+ - Complex schema support (nested objects, arrays)
69
+ - Schema definition helpers
70
+ - Markdown code block extraction
71
+
72
+ **Related guide**: [Structured Output](./structured-output.md)
73
+
74
+ ```bash
75
+ npx ts-node examples/structured-output.ts
76
+ ```
77
+
78
+ ### Metrics Tracking (`metrics-tracking.ts`)
79
+
80
+ **Description**: Tracks performance, tokens, and costs
81
+ **Features**:
82
+
83
+ - MetricsTracker for workflow metrics
84
+ - TokenCounter for cost estimation
85
+ - Cost configuration for major AI models
86
+ - Custom metrics tracking
87
+ - OpenTelemetry export
88
+
89
+ **Related guide**: [Metrics & Observability](./metrics-observability.md)
90
+
91
+ ```bash
92
+ npx ts-node examples/metrics-tracking.ts
93
+ ```
94
+
95
+ ### Complete Integration (`complete-integration.ts`)
96
+
97
+ **Description**: Full-featured example combining all systems
98
+ **Features**:
99
+
100
+ - Graph-based workflow
101
+ - Tool calling integration
102
+ - Memory system integration
103
+ - Structured output validation
104
+ - Metrics tracking
105
+ - End-to-end multi-agent system
106
+
107
+ **Related guides**: All feature guides
108
+
109
+ ```bash
110
+ npx ts-node examples/complete-integration.ts
111
+ ```
112
+
113
+ ## Running Examples
114
+
115
+ ### Prerequisites
116
+
117
+ Install dependencies:
118
+
119
+ ```bash
120
+ npm install
121
+ ```
122
+
123
+ Set up environment variables (if using real AI models):
124
+
125
+ ```bash
126
+ export OPENAI_API_KEY="your-key-here"
127
+ # or
128
+ export ANTHROPIC_API_KEY="your-key-here"
129
+ ```
130
+
131
+ ### Run Specific Example
132
+
133
+ ```bash
134
+ npx ts-node examples/<example-name>.ts
135
+ ```
136
+
137
+ ### Run All Examples
138
+
139
+ ```bash
140
+ npm run examples
141
+ ```
142
+
143
+ ## Feature Guides
144
+
145
+ For comprehensive documentation on each feature:
146
+
147
+ - **[Graph Execution](./graph-execution.md)**: DAG/Cyclic workflows, node types, conditional branching
148
+ - **[Tool Calling](./tool-calling.md)**: Tool definition, registry, executor, parameter validation
149
+ - **[Memory System](./memory-system.md)**: Short-term, long-term, entity memory, RAG integration
150
+ - **[Structured Output](./structured-output.md)**: JSON Schema validation, automatic retry
151
+ - **[Metrics & Observability](./metrics-observability.md)**: Performance tracking, cost calculation
152
+
153
+ ## Example Categories
154
+
155
+ ### Basic Usage
156
+
157
+ Perfect for getting started:
158
+
159
+ - `graph-workflow.ts` - Basic graph patterns
160
+ - `tool-calling.ts` - Simple tool integration
161
+ - `structured-output.ts` - Output validation
162
+
163
+ ### Advanced Patterns
164
+
165
+ For production systems:
166
+
167
+ - `memory-system.ts` - Context management
168
+ - `metrics-tracking.ts` - Observability
169
+ - `complete-integration.ts` - Full system integration
170
+
171
+ ## Code Snippets
172
+
173
+ ### Quick Graph Example
174
+
175
+ ```typescript
176
+ import { GraphBuilder, NodeType } from 'societyai';
177
+
178
+ const graph = GraphBuilder.create()
179
+ .addNode('start', NodeType.START)
180
+ .addNode('agent1', NodeType.AGENT, { agentId: 'analyst' })
181
+ .addNode('end', NodeType.END)
182
+ .addEdge('start', 'agent1')
183
+ .addEdge('agent1', 'end')
184
+ .build();
185
+
186
+ const result = await graph.execute('Analyze data', agents);
187
+ ```
188
+
189
+ ### Quick Tool Example
190
+
191
+ ```typescript
192
+ import { ToolBuilder, ToolRegistry } from 'societyai';
193
+
194
+ const tool = ToolBuilder.create()
195
+ .withName('add')
196
+ .withDescription('Add two numbers')
197
+ .withParameter('a', 'number', 'First number', true)
198
+ .withParameter('b', 'number', 'Second number', true)
199
+ .withExecutor(async ({ a, b }) => a + b)
200
+ .build();
201
+
202
+ const registry = new ToolRegistry();
203
+ registry.register(tool);
204
+ ```
205
+
206
+ ### Quick Memory Example
207
+
208
+ ```typescript
209
+ import { MemoryBuilder } from 'societyai';
210
+
211
+ const memory = MemoryBuilder.create()
212
+ .withShortTermMemory({ maxSize: 10 })
213
+ .withLongTermMemory({ maxSize: 100 })
214
+ .build();
215
+
216
+ await memory.addShortTerm('Important context', 0.9);
217
+ ```
218
+
219
+ ## Troubleshooting
220
+
221
+ ### TypeScript Errors
222
+
223
+ Ensure your `tsconfig.json` includes:
224
+
225
+ ```json
226
+ {
227
+ "compilerOptions": {
228
+ "target": "ES2020",
229
+ "module": "commonjs",
230
+ "esModuleInterop": true,
231
+ "strict": true
232
+ }
233
+ }
234
+ ```
235
+
236
+ ### Module Not Found
237
+
238
+ Install dependencies:
239
+
240
+ ```bash
241
+ npm install
242
+ ```
243
+
244
+ ### Examples Not Working
245
+
246
+ Make sure you have:
247
+
248
+ 1. Installed dependencies (`npm install`)
249
+ 2. Built the project (`npm run build`)
250
+ 3. Set environment variables (if using real AI models)
251
+
252
+ ## Next Steps
253
+
254
+ - Read the [Getting Started Guide](./getting-started.md)
255
+ - Explore [Architecture Overview](./architecture.md)
256
+ - Check [API Reference](./api-reference.md)
257
+ - Review [Best Practices](./workflows.md)
258
+
259
+ ---
260
+
261
+ **Questions?** Open an issue on [GitHub](https://github.com/benoitpetit/societyai-package/issues)**When to use**: Collaborative workflows, information sharing
262
+
263
+ ## 03-workflows/ - Workflow Patterns
264
+
265
+ ###
266
+
267
+ **Description**: Step-by-step processing pipeline
268
+ **Covers**:
269
+
270
+ - Sequential execution
271
+ - Data flow between steps
272
+ - Pipeline patterns
273
+
274
+ **When to use**: Dependent tasks, progressive refinement, quality review
275
+
276
+ ###
277
+
278
+ **Description**: Concurrent agent execution
279
+ **Covers**:
280
+
281
+ - Parallel execution
282
+ - Worker pools
283
+ - Result aggregation
284
+
285
+ **When to use**: Independent tasks, speed optimization, multiple perspectives
286
+
287
+ ###
288
+
289
+ **Description**: Agents discussing and iterating together
290
+ **Covers**:
291
+
292
+ - Collaborative execution
293
+ - Multiple iterations
294
+ - Completion conditions
295
+ - Consensus building
296
+
297
+ **When to use**: Discussions, debates, iterative refinement
298
+
299
+ ###
300
+
301
+ **Description**: Dynamic workflows with branching logic
302
+ **Covers**:
303
+
304
+ - Conditional execution
305
+ - Dynamic routing
306
+ - Step conditions
307
+ - Error recovery
308
+
309
+ **When to use**: Error handling, optimization, adaptive workflows
310
+
311
+ ## 04-domains/ - Domain-Specific Examples
312
+
313
+ ###
314
+
315
+ **Description**: Software development team workflow
316
+ **Covers**:
317
+
318
+ - Project manager role
319
+ - Developer roles
320
+ - QA testing
321
+ - Code review process
322
+
323
+ **Roles**: Project Manager, Architect, Developers, QA Tester
324
+ **Pattern**: Hierarchical with parallel development
325
+
326
+ ###
327
+
328
+ **Description**: Academic research workflow
329
+ **Covers**:
330
+
331
+ - Literature review
332
+ - Statistical analysis
333
+ - Paper writing
334
+ - Peer review
335
+
336
+ **Roles**: Researchers, Statistician, Writer
337
+ **Pattern**: Parallel research + synthesis
338
+
339
+ ###
340
+
341
+ **Description**: Content creation workflow
342
+ **Covers**:
343
+
344
+ - Research
345
+ - Writing
346
+ - Editing
347
+ - Design
348
+
349
+ **Roles**: Researcher, Writer, Editor, Designer
350
+ **Pattern**: Sequential with parallel polish
351
+
352
+ ###
353
+
354
+ **Description**: Business analysis workflow
355
+ **Covers**:
356
+
357
+ - Market analysis
358
+ - Financial analysis
359
+ - Strategic planning
360
+ - Executive decision
361
+
362
+ **Roles**: Market Analyst, Financial Analyst, Strategist, Executive
363
+ **Pattern**: Parallel analysis + synthesis
364
+
365
+ ## 05-integrations/ - AI Service Integrations
366
+
367
+ ###
368
+
369
+ **Description**: OpenAI GPT models integration
370
+ **Covers**:
371
+
372
+ - OpenAI API client
373
+ - Chat completions
374
+ - Streaming responses
375
+ - Error handling
376
+
377
+ **Models**: GPT-4, GPT-3.5-turbo
378
+
379
+ ###
380
+
381
+ **Description**: Anthropic Claude integration
382
+ **Covers**:
383
+
384
+ - Anthropic API client
385
+ - Message formatting
386
+ - Claude-specific features
387
+
388
+ **Models**: Claude 3 Opus, Sonnet, Haiku
389
+
390
+ ###
391
+
392
+ **Description**: Custom AI API integration
393
+ **Covers**:
394
+
395
+ - HTTP API wrapper
396
+ - Authentication
397
+ - Request/response formatting
398
+ - Rate limiting
399
+
400
+ **Use**: Local models, custom APIs, other AI services
401
+
402
+ ## 06-advanced/ - Advanced Features
403
+
404
+ ###
405
+
406
+ **Description**: Comprehensive error handling
407
+ **Covers**:
408
+
409
+ - Try-catch patterns
410
+ - Error types
411
+ - Retry logic
412
+ - Fallback strategies
413
+ - Error recovery steps
414
+
415
+ **When to use**: Production systems, reliability requirements
416
+
417
+ ###
418
+
419
+ **Description**: Timeout and cancellation patterns
420
+ **Covers**:
421
+
422
+ - AbortController
423
+ - Timeouts
424
+ - User cancellation
425
+ - Graceful shutdown
426
+
427
+ **When to use**: Long-running operations, user-facing applications
428
+
429
+ ###
430
+
431
+ **Description**: Workflow lifecycle hooks
432
+ **Covers**:
433
+
434
+ - onBeforeStep hooks
435
+ - onAfterStep hooks
436
+ - Context manipulation
437
+ - Dynamic workflow modification
438
+
439
+ **When to use**: Logging, metrics, dynamic behavior
440
+
441
+ ###
442
+
443
+ **Description**: Custom result processing
444
+ **Covers**:
445
+
446
+ - Result transformers
447
+ - Data normalization
448
+ - Custom output generation
449
+ - Aggregation patterns
450
+
451
+ **When to use**: Custom output formats, data processing
452
+
453
+ ## Running Examples
454
+
455
+ ### Prerequisites
456
+
457
+ 1. Install dependencies:
458
+
459
+ ```bash
460
+ npm install
461
+ ```
462
+
463
+ 2. Set up environment variables:
464
+
465
+ ```bash
466
+ export OPENAI_API_KEY="your-key-here"
467
+ export ANTHROPIC_API_KEY="your-key-here"
468
+ ```
469
+
470
+ 3. Build the library:
471
+
472
+ ```bash
473
+ npm run build
474
+ ```
475
+
476
+ ### Run an Example
477
+
478
+ ```bash
479
+ # Using ts-node
480
+ npx ts-node examples/01-basic/simple-society.ts
481
+
482
+ # Or compile and run
483
+ npm run build
484
+ node dist/examples/01-basic/simple-society.js
485
+ ```
486
+
487
+ ## Learning Path
488
+
489
+ ### Beginner
490
+
491
+ 1. simple-society.ts - Understand basics
492
+ 2. custom-roles.ts - Create roles
493
+ 3. - Build workflows
494
+
495
+ ### Intermediate
496
+
497
+ 1. - Optimize with parallelism
498
+ 2. - Agent discussions
499
+ 3. - Complete use case
500
+
501
+ ### Advanced
502
+
503
+ 1. - Production-ready code
504
+ 2. - Advanced patterns
505
+ 3. - Dynamic routing
506
+
507
+ ## Example Code Snippets
508
+
509
+ ### Quick Start
510
+
511
+ ```typescript
512
+ import {
513
+ RoleBuilder,
514
+ AgentBuilder,
515
+ StepBuilder,
516
+ WorkflowConfigBuilder,
517
+ DefaultWorkflowExecutor,
518
+ } from 'societyai';
519
+
520
+ // See examples/01-basic/simple-society.ts
521
+ ```
522
+
523
+ ### Custom Role
524
+
525
+ ```typescript
526
+ const role = RoleBuilder.create().withId('analyst').withSystemPrompt('You analyze data.').build();
527
+
528
+ // See examples/02-roles-and-agents/custom-roles.ts
529
+ ```
530
+
531
+ ### Parallel Execution
532
+
533
+ ```typescript
534
+ const step = StepBuilder.create()
535
+ .withAgents(['agent-1', 'agent-2'])
536
+ .withExecutionType('parallel')
537
+ .build();
538
+
539
+ // See examples/03-workflows/parallel-workflow.ts
540
+ ```
541
+
542
+ ### Error Handling
543
+
544
+ ```typescript
545
+ try {
546
+ const result = await executor.execute(workflow, input);
547
+ } catch (error) {
548
+ if (error instanceof ProcessingFailedError) {
549
+ // Handle error
550
+ }
551
+ }
552
+
553
+ // Examples are being added progressively in `examples/`.
554
+ ```
555
+
556
+ ## Contributing Examples
557
+
558
+ We welcome example contributions! Please:
559
+
560
+ 1. Follow the existing structure
561
+ 2. Include clear comments
562
+ 3. Add to this index
563
+ 4. Test your example
564
+ 5. Submit a pull request
565
+
566
+ ## Further Reading
567
+
568
+ - [Getting Started Guide](./getting-started.md)
569
+ - [Architecture Overview](./architecture.md)
570
+ - [Workflow Patterns](./workflows.md)
571
+ - [API Reference](./api-reference.md)
572
+ - [Advanced Features](./advanced.md)
573
+
574
+ ---
575
+
576
+ **Previous**: [Migration Guide](./migration.md) ←