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,296 @@
1
+ # Migration Guide
2
+
3
+ Guide for migrating from the legacy function helpers to the workflow-based API.
4
+
5
+ ## Overview
6
+
7
+ SocietyAI has evolved from a simple function-based API to a powerful workflow builder API. This guide helps you migrate existing code.
8
+
9
+ ## Why Migrate?
10
+
11
+ The new API offers:
12
+
13
+ - **More control**: Define custom roles, behaviors, and capabilities
14
+ - **Better composition**: Build complex workflows from simple steps
15
+ - **Type safety**: Full TypeScript support with compile-time checking
16
+ - **Flexibility**: Sequential, parallel, collaborative, and conditional execution
17
+ - **Observability**: Better monitoring and debugging
18
+ - **Performance**: Optimized execution with worker pools
19
+
20
+ ## Legacy API (Old)
21
+
22
+ > Note: the legacy helpers (`society`, `societyCollaborative`, `runSociety*`, etc.) are no longer exported.
23
+ > If you used them in older code, migrate using the patterns below.
24
+
25
+ ```typescript
26
+ import { society, societyCollaborative } from 'societyai';
27
+
28
+ // Simple usage
29
+ const result = await society('What are the benefits of AI?', 3, [model], false);
30
+
31
+ // Collaborative
32
+ const result = await societyCollaborative('How can we solve this problem?', 4, [model], false);
33
+ ```
34
+
35
+ ## New API (Recommended)
36
+
37
+ ```typescript
38
+ import {
39
+ RoleBuilder,
40
+ AgentBuilder,
41
+ StepBuilder,
42
+ WorkflowConfigBuilder,
43
+ DefaultWorkflowExecutor,
44
+ } from 'societyai';
45
+
46
+ // Define roles
47
+ const role = RoleBuilder.create()
48
+ .withId('analyst')
49
+ .withSystemPrompt('You analyze information objectively.')
50
+ .build();
51
+
52
+ // Create agents
53
+ const agents = [
54
+ AgentBuilder.create().withId('agent-1').withRole(role).withModel(model).build(),
55
+ // ...
56
+ ];
57
+
58
+ // Build workflow
59
+ const workflow = WorkflowConfigBuilder.create()
60
+ .withId('my-workflow')
61
+ .addAgents(agents)
62
+ .addStep(
63
+ StepBuilder.create()
64
+ .withId('analysis')
65
+ .withAgents(['agent-1', 'agent-2'])
66
+ .withExecutionType('parallel')
67
+ .build()
68
+ )
69
+ .build();
70
+
71
+ // Execute
72
+ const executor = new DefaultWorkflowExecutor();
73
+ const result = await executor.execute(workflow, 'Your input here');
74
+ ```
75
+
76
+ ## Migration Examples
77
+
78
+ ### Example 1: Simple Society
79
+
80
+ **Before**:
81
+
82
+ ```typescript
83
+ const result = await society('Analyze this data', 3, [model], false, observer);
84
+ ```
85
+
86
+ **After**:
87
+
88
+ ```typescript
89
+ const analyst = RoleBuilder.create()
90
+ .withId('analyst')
91
+ .withSystemPrompt('Analyze this data')
92
+ .build();
93
+
94
+ const agents = Array.from({ length: 3 }, (_, i) =>
95
+ AgentBuilder.create().withId(`analyst-${i}`).withRole(analyst).withModel(model).build()
96
+ );
97
+
98
+ const workflow = WorkflowConfigBuilder.create()
99
+ .withId('analysis')
100
+ .addAgents(agents)
101
+ .addStep(
102
+ StepBuilder.create()
103
+ .withId('analyze')
104
+ .withAgents(agents.map((a) => a.id))
105
+ .withExecutionType('parallel')
106
+ .build()
107
+ )
108
+ .build();
109
+
110
+ const executor = new DefaultWorkflowExecutor(observer);
111
+ const result = await executor.execute(workflow, 'Analyze this data');
112
+ ```
113
+
114
+ ### Example 2: Collaborative Society
115
+
116
+ **Before**:
117
+
118
+ ```typescript
119
+ const result = await societyCollaborative('Discuss this topic', 4, [model], false, observer);
120
+ ```
121
+
122
+ **After**:
123
+
124
+ ```typescript
125
+ const discussant = RoleBuilder.create()
126
+ .withId('discussant')
127
+ .withSystemPrompt('Participate in discussions thoughtfully.')
128
+ .build();
129
+
130
+ const agents = Array.from({ length: 4 }, (_, i) =>
131
+ AgentBuilder.create().withId(`discussant-${i}`).withRole(discussant).withModel(model).build()
132
+ );
133
+
134
+ const workflow = WorkflowConfigBuilder.create()
135
+ .withId('discussion')
136
+ .addAgents(agents)
137
+ .addStep(
138
+ StepBuilder.create()
139
+ .withId('discuss')
140
+ .withAgents(agents.map((a) => a.id))
141
+ .withExecutionType('collaborative')
142
+ .withMaxIterations(3)
143
+ .build()
144
+ )
145
+ .build();
146
+
147
+ const executor = new DefaultWorkflowExecutor(observer);
148
+ const result = await executor.execute(workflow, 'Discuss this topic');
149
+ ```
150
+
151
+ ### Example 3: Custom Perspectives
152
+
153
+ **Before**:
154
+
155
+ ```typescript
156
+ const result = await runSociety(
157
+ {
158
+ prompt: 'Evaluate this proposal',
159
+ agentCount: 3,
160
+ agentPerspectives: [
161
+ 'From a technical perspective: ',
162
+ 'From a business perspective: ',
163
+ 'From a user perspective: ',
164
+ ],
165
+ },
166
+ [model]
167
+ );
168
+ ```
169
+
170
+ **After**:
171
+
172
+ ```typescript
173
+ const perspectives = [
174
+ { id: 'tech', prompt: 'Evaluate from a technical perspective' },
175
+ { id: 'business', prompt: 'Evaluate from a business perspective' },
176
+ { id: 'user', prompt: 'Evaluate from a user perspective' },
177
+ ];
178
+
179
+ const agents = perspectives.map((p) =>
180
+ AgentBuilder.create()
181
+ .withId(p.id)
182
+ .withRole(RoleBuilder.create().withId(p.id).withSystemPrompt(p.prompt).build())
183
+ .withModel(model)
184
+ .build()
185
+ );
186
+
187
+ const workflow = WorkflowConfigBuilder.create()
188
+ .withId('evaluation')
189
+ .addAgents(agents)
190
+ .addStep(
191
+ StepBuilder.create()
192
+ .withId('evaluate')
193
+ .withAgents(agents.map((a) => a.id))
194
+ .withExecutionType('parallel')
195
+ .build()
196
+ )
197
+ .build();
198
+
199
+ const executor = new DefaultWorkflowExecutor();
200
+ const result = await executor.execute(workflow, 'Evaluate this proposal');
201
+ ```
202
+
203
+ ### Example 4: Custom Dimensions
204
+
205
+ **Before**:
206
+
207
+ ```typescript
208
+ const result = await runSocietyCollaborative(
209
+ {
210
+ prompt: 'Analyze market trends',
211
+ agentCount: 3,
212
+ dimensions: ['Economic factors', 'Technological disruption', 'Consumer behavior'],
213
+ },
214
+ [model]
215
+ );
216
+ ```
217
+
218
+ **After**:
219
+
220
+ ```typescript
221
+ const dimensions = ['Economic factors', 'Technological disruption', 'Consumer behavior'];
222
+
223
+ const agents = dimensions.map((dim, i) =>
224
+ AgentBuilder.create()
225
+ .withId(`analyst-${i}`)
226
+ .withRole(
227
+ RoleBuilder.create()
228
+ .withId(`dim-${i}`)
229
+ .withSystemPrompt(`Analyze focusing on: ${dim}`)
230
+ .build()
231
+ )
232
+ .withModel(model)
233
+ .build()
234
+ );
235
+
236
+ const workflow = WorkflowConfigBuilder.create()
237
+ .withId('market-analysis')
238
+ .addAgents(agents)
239
+ .addSteps([
240
+ // Parallel analysis of each dimension
241
+ StepBuilder.create()
242
+ .withId('analyze-dimensions')
243
+ .withAgents(agents.map((a) => a.id))
244
+ .withExecutionType('parallel')
245
+ .build(),
246
+
247
+ // Collaborative integration
248
+ StepBuilder.create()
249
+ .withId('integrate')
250
+ .withAgents(agents.map((a) => a.id))
251
+ .withExecutionType('collaborative')
252
+ .withMaxIterations(2)
253
+ .build(),
254
+ ])
255
+ .build();
256
+
257
+ const executor = new DefaultWorkflowExecutor();
258
+ const result = await executor.execute(workflow, 'Analyze market trends');
259
+ ```
260
+
261
+ ## Migration Checklist
262
+
263
+ - [ ] Replace `society()` calls with workflow builders
264
+ - [ ] Replace `societyCollaborative()` calls with collaborative steps
265
+ - [ ] Convert perspectives to roles
266
+ - [ ] Convert dimensions to multiple agents or steps
267
+ - [ ] Update observers to use `DefaultWorkflowExecutor` constructor
268
+ - [ ] Test with new API
269
+ - [ ] Update error handling for new error types
270
+ - [ ] Review and optimize execution types (sequential vs parallel)
271
+
272
+ ## Backward Compatibility
273
+
274
+ The legacy helpers have been removed to keep the public API smaller and more consistent.
275
+
276
+ ## Benefits After Migration
277
+
278
+ 1. **More Control**: Define exact agent behaviors
279
+ 2. **Better Workflows**: Combine sequential, parallel, and collaborative steps
280
+ 3. **Type Safety**: Catch errors at compile time
281
+ 4. **Reusability**: Share roles and workflows across projects
282
+ 5. **Testing**: Easier to test individual components
283
+ 6. **Performance**: Better control over parallelization
284
+ 7. **Monitoring**: Rich observability through hooks and observers
285
+
286
+ ## Getting Help
287
+
288
+ If you encounter issues during migration:
289
+
290
+ - Check the docs in `docs/` (workflows, advanced, API reference)
291
+ - Read the [API Reference](./api-reference.md)
292
+ - Open an issue on [GitHub](https://github.com/benoitpetit/societyai-package/issues)
293
+
294
+ ---
295
+
296
+ **Previous**: [Advanced Features](./advanced.md) ←