@cogitator-ai/core 0.2.0 → 0.3.0

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 (157) hide show
  1. package/README.md +920 -15
  2. package/dist/cogitator.d.ts +31 -1
  3. package/dist/cogitator.d.ts.map +1 -1
  4. package/dist/cogitator.js +127 -6
  5. package/dist/cogitator.js.map +1 -1
  6. package/dist/constitutional/constitution.d.ts +9 -0
  7. package/dist/constitutional/constitution.d.ts.map +1 -0
  8. package/dist/constitutional/constitution.js +215 -0
  9. package/dist/constitutional/constitution.js.map +1 -0
  10. package/dist/constitutional/constitutional-ai.d.ts +36 -0
  11. package/dist/constitutional/constitutional-ai.d.ts.map +1 -0
  12. package/dist/constitutional/constitutional-ai.js +163 -0
  13. package/dist/constitutional/constitutional-ai.js.map +1 -0
  14. package/dist/constitutional/critique-reviser.d.ts +20 -0
  15. package/dist/constitutional/critique-reviser.d.ts.map +1 -0
  16. package/dist/constitutional/critique-reviser.js +98 -0
  17. package/dist/constitutional/critique-reviser.js.map +1 -0
  18. package/dist/constitutional/index.d.ts +13 -0
  19. package/dist/constitutional/index.d.ts.map +1 -0
  20. package/dist/constitutional/index.js +8 -0
  21. package/dist/constitutional/index.js.map +1 -0
  22. package/dist/constitutional/input-filter.d.ts +19 -0
  23. package/dist/constitutional/input-filter.d.ts.map +1 -0
  24. package/dist/constitutional/input-filter.js +88 -0
  25. package/dist/constitutional/input-filter.js.map +1 -0
  26. package/dist/constitutional/output-filter.d.ts +19 -0
  27. package/dist/constitutional/output-filter.d.ts.map +1 -0
  28. package/dist/constitutional/output-filter.js +86 -0
  29. package/dist/constitutional/output-filter.js.map +1 -0
  30. package/dist/constitutional/prompts.d.ts +11 -0
  31. package/dist/constitutional/prompts.d.ts.map +1 -0
  32. package/dist/constitutional/prompts.js +202 -0
  33. package/dist/constitutional/prompts.js.map +1 -0
  34. package/dist/constitutional/tool-guard.d.ts +18 -0
  35. package/dist/constitutional/tool-guard.d.ts.map +1 -0
  36. package/dist/constitutional/tool-guard.js +125 -0
  37. package/dist/constitutional/tool-guard.js.map +1 -0
  38. package/dist/cost-routing/budget-enforcer.d.ts +26 -0
  39. package/dist/cost-routing/budget-enforcer.d.ts.map +1 -0
  40. package/dist/cost-routing/budget-enforcer.js +86 -0
  41. package/dist/cost-routing/budget-enforcer.js.map +1 -0
  42. package/dist/cost-routing/cost-router.d.ts +34 -0
  43. package/dist/cost-routing/cost-router.d.ts.map +1 -0
  44. package/dist/cost-routing/cost-router.js +80 -0
  45. package/dist/cost-routing/cost-router.js.map +1 -0
  46. package/dist/cost-routing/cost-tracker.d.ts +20 -0
  47. package/dist/cost-routing/cost-tracker.d.ts.map +1 -0
  48. package/dist/cost-routing/cost-tracker.js +85 -0
  49. package/dist/cost-routing/cost-tracker.js.map +1 -0
  50. package/dist/cost-routing/index.d.ts +6 -0
  51. package/dist/cost-routing/index.d.ts.map +1 -0
  52. package/dist/cost-routing/index.js +6 -0
  53. package/dist/cost-routing/index.js.map +1 -0
  54. package/dist/cost-routing/model-selector.d.ts +15 -0
  55. package/dist/cost-routing/model-selector.d.ts.map +1 -0
  56. package/dist/cost-routing/model-selector.js +216 -0
  57. package/dist/cost-routing/model-selector.js.map +1 -0
  58. package/dist/cost-routing/task-analyzer.d.ts +13 -0
  59. package/dist/cost-routing/task-analyzer.d.ts.map +1 -0
  60. package/dist/cost-routing/task-analyzer.js +185 -0
  61. package/dist/cost-routing/task-analyzer.js.map +1 -0
  62. package/dist/index.d.ts +13 -2
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +7 -2
  65. package/dist/index.js.map +1 -1
  66. package/dist/learning/ab-testing.d.ts +45 -0
  67. package/dist/learning/ab-testing.d.ts.map +1 -0
  68. package/dist/learning/ab-testing.js +267 -0
  69. package/dist/learning/ab-testing.js.map +1 -0
  70. package/dist/learning/agent-optimizer.d.ts.map +1 -1
  71. package/dist/learning/agent-optimizer.js +26 -21
  72. package/dist/learning/agent-optimizer.js.map +1 -1
  73. package/dist/learning/auto-optimizer.d.ts +38 -0
  74. package/dist/learning/auto-optimizer.d.ts.map +1 -0
  75. package/dist/learning/auto-optimizer.js +229 -0
  76. package/dist/learning/auto-optimizer.js.map +1 -0
  77. package/dist/learning/demo-selector.d.ts.map +1 -1
  78. package/dist/learning/demo-selector.js +7 -7
  79. package/dist/learning/demo-selector.js.map +1 -1
  80. package/dist/learning/index.d.ts +13 -1
  81. package/dist/learning/index.d.ts.map +1 -1
  82. package/dist/learning/index.js +7 -1
  83. package/dist/learning/index.js.map +1 -1
  84. package/dist/learning/instruction-optimizer.d.ts.map +1 -1
  85. package/dist/learning/instruction-optimizer.js +7 -11
  86. package/dist/learning/instruction-optimizer.js.map +1 -1
  87. package/dist/learning/metrics.d.ts.map +1 -1
  88. package/dist/learning/metrics.js +26 -16
  89. package/dist/learning/metrics.js.map +1 -1
  90. package/dist/learning/postgres-trace-store.d.ts +53 -0
  91. package/dist/learning/postgres-trace-store.d.ts.map +1 -0
  92. package/dist/learning/postgres-trace-store.js +692 -0
  93. package/dist/learning/postgres-trace-store.js.map +1 -0
  94. package/dist/learning/prompt-logger.d.ts +29 -0
  95. package/dist/learning/prompt-logger.d.ts.map +1 -0
  96. package/dist/learning/prompt-logger.js +157 -0
  97. package/dist/learning/prompt-logger.js.map +1 -0
  98. package/dist/learning/prompt-monitor.d.ts +29 -0
  99. package/dist/learning/prompt-monitor.d.ts.map +1 -0
  100. package/dist/learning/prompt-monitor.js +243 -0
  101. package/dist/learning/prompt-monitor.js.map +1 -0
  102. package/dist/learning/prompts.d.ts.map +1 -1
  103. package/dist/learning/prompts.js +24 -13
  104. package/dist/learning/prompts.js.map +1 -1
  105. package/dist/learning/rollback-manager.d.ts +36 -0
  106. package/dist/learning/rollback-manager.d.ts.map +1 -0
  107. package/dist/learning/rollback-manager.js +177 -0
  108. package/dist/learning/rollback-manager.js.map +1 -0
  109. package/dist/learning/trace-store.d.ts.map +1 -1
  110. package/dist/learning/trace-store.js +8 -10
  111. package/dist/learning/trace-store.js.map +1 -1
  112. package/dist/reasoning/branch-evaluator.d.ts.map +1 -1
  113. package/dist/reasoning/branch-evaluator.js +14 -8
  114. package/dist/reasoning/branch-evaluator.js.map +1 -1
  115. package/dist/reasoning/branch-generator.d.ts.map +1 -1
  116. package/dist/reasoning/branch-generator.js +5 -3
  117. package/dist/reasoning/branch-generator.js.map +1 -1
  118. package/dist/reasoning/prompts.d.ts.map +1 -1
  119. package/dist/reasoning/prompts.js +7 -5
  120. package/dist/reasoning/prompts.js.map +1 -1
  121. package/dist/reasoning/thought-tree.d.ts.map +1 -1
  122. package/dist/reasoning/thought-tree.js +9 -11
  123. package/dist/reasoning/thought-tree.js.map +1 -1
  124. package/dist/reflection/insight-store.d.ts.map +1 -1
  125. package/dist/reflection/insight-store.js +8 -6
  126. package/dist/reflection/insight-store.js.map +1 -1
  127. package/dist/reflection/prompts.d.ts.map +1 -1
  128. package/dist/reflection/prompts.js +11 -6
  129. package/dist/reflection/prompts.js.map +1 -1
  130. package/dist/reflection/reflection-engine.d.ts.map +1 -1
  131. package/dist/reflection/reflection-engine.js +8 -10
  132. package/dist/reflection/reflection-engine.js.map +1 -1
  133. package/dist/time-travel/checkpoint-store.d.ts +34 -0
  134. package/dist/time-travel/checkpoint-store.d.ts.map +1 -0
  135. package/dist/time-travel/checkpoint-store.js +240 -0
  136. package/dist/time-travel/checkpoint-store.js.map +1 -0
  137. package/dist/time-travel/comparator.d.ts +26 -0
  138. package/dist/time-travel/comparator.d.ts.map +1 -0
  139. package/dist/time-travel/comparator.js +253 -0
  140. package/dist/time-travel/comparator.js.map +1 -0
  141. package/dist/time-travel/forker.d.ts +22 -0
  142. package/dist/time-travel/forker.d.ts.map +1 -0
  143. package/dist/time-travel/forker.js +118 -0
  144. package/dist/time-travel/forker.js.map +1 -0
  145. package/dist/time-travel/index.d.ts +6 -0
  146. package/dist/time-travel/index.d.ts.map +1 -0
  147. package/dist/time-travel/index.js +6 -0
  148. package/dist/time-travel/index.js.map +1 -0
  149. package/dist/time-travel/replayer.d.ts +20 -0
  150. package/dist/time-travel/replayer.d.ts.map +1 -0
  151. package/dist/time-travel/replayer.js +147 -0
  152. package/dist/time-travel/replayer.js.map +1 -0
  153. package/dist/time-travel/time-travel.d.ts +41 -0
  154. package/dist/time-travel/time-travel.d.ts.map +1 -0
  155. package/dist/time-travel/time-travel.js +127 -0
  156. package/dist/time-travel/time-travel.js.map +1 -0
  157. package/package.json +13 -5
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @cogitator-ai/core
2
2
 
3
- Core runtime for Cogitator AI agents. Build and run LLM-powered agents with tool calling, streaming, and multi-provider support.
3
+ Core runtime for Cogitator AI agents. Build and run LLM-powered agents with tool calling, streaming, reflection, Tree-of-Thought reasoning, learning optimization, and time-travel debugging.
4
4
 
5
5
  ## Installation
6
6
 
@@ -14,7 +14,6 @@ pnpm add @cogitator-ai/core
14
14
  import { Cogitator, Agent, tool } from '@cogitator-ai/core';
15
15
  import { z } from 'zod';
16
16
 
17
- // Create a tool
18
17
  const calculator = tool({
19
18
  name: 'calculator',
20
19
  description: 'Evaluate a math expression',
@@ -26,15 +25,13 @@ const calculator = tool({
26
25
  },
27
26
  });
28
27
 
29
- // Create an agent
30
28
  const agent = new Agent({
31
29
  name: 'math-assistant',
32
30
  instructions: 'You are a helpful math assistant',
33
- model: 'ollama/llama3.2:3b',
31
+ model: 'openai/gpt-4o',
34
32
  tools: [calculator],
35
33
  });
36
34
 
37
- // Run the agent
38
35
  const cog = new Cogitator();
39
36
  const result = await cog.run(agent, {
40
37
  input: 'What is 25 * 4?',
@@ -45,31 +42,939 @@ console.log(result.output);
45
42
 
46
43
  ## Features
47
44
 
48
- - Multiple LLM backends (Ollama, OpenAI, Anthropic, Google)
49
- - Type-safe tool definitions with Zod
50
- - Streaming responses
51
- - Memory integration
52
- - Built-in tools (calculator, datetime, filesystem, HTTP, etc.)
45
+ - **Multi-Provider LLM Support** - Ollama, OpenAI, Anthropic, Google, vLLM
46
+ - **Type-Safe Tools** - Zod-validated tool definitions
47
+ - **Streaming Responses** - Real-time token streaming
48
+ - **Memory Integration** - Redis, PostgreSQL, in-memory adapters
49
+ - **20+ Built-in Tools** - Calculator, filesystem, HTTP, regex, and more
50
+ - **Reflection Engine** - Self-improvement through tool call analysis
51
+ - **Tree-of-Thought** - Advanced reasoning with branch exploration
52
+ - **Agent Optimizer** - DSPy-style learning from traces
53
+ - **Time Travel** - Checkpoint, replay, fork, and compare executions
54
+ - **Resilience** - Retry, circuit breaker, and fallback patterns
55
+ - **Observability** - Full tracing with spans and callbacks
56
+
57
+ ---
53
58
 
54
59
  ## LLM Backends
55
60
 
61
+ ### Supported Providers
62
+
56
63
  ```typescript
57
- // Ollama (default)
64
+ // Ollama (local, default)
58
65
  const cog = new Cogitator({ defaultModel: 'ollama/llama3.2:3b' });
59
66
 
60
67
  // OpenAI
61
68
  const cog = new Cogitator({ defaultModel: 'openai/gpt-4o' });
62
69
 
63
- // Anthropic
70
+ // Anthropic Claude
64
71
  const cog = new Cogitator({ defaultModel: 'anthropic/claude-sonnet-4-20250514' });
65
72
 
66
- // Google
73
+ // Google Gemini
67
74
  const cog = new Cogitator({ defaultModel: 'google/gemini-1.5-flash' });
75
+
76
+ // vLLM
77
+ const cog = new Cogitator({ defaultModel: 'vllm/mistral-7b' });
78
+ ```
79
+
80
+ ### Backend Configuration
81
+
82
+ ```typescript
83
+ import { Cogitator, OllamaBackend, OpenAIBackend, AnthropicBackend } from '@cogitator-ai/core';
84
+
85
+ const cog = new Cogitator({
86
+ llm: {
87
+ defaultProvider: 'openai',
88
+ ollama: {
89
+ baseUrl: 'http://localhost:11434',
90
+ },
91
+ openai: {
92
+ apiKey: process.env.OPENAI_API_KEY,
93
+ organization: 'org-xxx',
94
+ },
95
+ anthropic: {
96
+ apiKey: process.env.ANTHROPIC_API_KEY,
97
+ },
98
+ google: {
99
+ apiKey: process.env.GOOGLE_API_KEY,
100
+ },
101
+ },
102
+ });
103
+ ```
104
+
105
+ ### Direct Backend Usage
106
+
107
+ ```typescript
108
+ import { createLLMBackend, parseModel } from '@cogitator-ai/core';
109
+
110
+ const backend = createLLMBackend('openai', { apiKey: process.env.OPENAI_API_KEY });
111
+
112
+ const response = await backend.chat({
113
+ model: 'gpt-4o',
114
+ messages: [
115
+ { role: 'system', content: 'You are helpful.' },
116
+ { role: 'user', content: 'Hello!' },
117
+ ],
118
+ });
119
+ ```
120
+
121
+ ---
122
+
123
+ ## Agent Configuration
124
+
125
+ ```typescript
126
+ import { Agent } from '@cogitator-ai/core';
127
+
128
+ const agent = new Agent({
129
+ id: 'custom-id',
130
+ name: 'research-assistant',
131
+ instructions: 'You research topics thoroughly',
132
+ model: 'openai/gpt-4o',
133
+ tools: [webSearch, calculator],
134
+
135
+ temperature: 0.7,
136
+ topP: 0.9,
137
+ maxTokens: 4096,
138
+ maxIterations: 15,
139
+ timeout: 120_000,
140
+ stopSequences: ['DONE'],
141
+ });
142
+
143
+ // Clone with modifications
144
+ const variant = agent.clone({
145
+ name: 'fast-assistant',
146
+ temperature: 0.3,
147
+ maxTokens: 1024,
148
+ });
149
+ ```
150
+
151
+ ---
152
+
153
+ ## Tools
154
+
155
+ ### Creating Tools
156
+
157
+ ```typescript
158
+ import { tool } from '@cogitator-ai/core';
159
+ import { z } from 'zod';
160
+
161
+ const weatherTool = tool({
162
+ name: 'get_weather',
163
+ description: 'Get current weather for a location',
164
+ parameters: z.object({
165
+ city: z.string().describe('City name'),
166
+ units: z.enum(['celsius', 'fahrenheit']).optional(),
167
+ }),
168
+ execute: async ({ city, units = 'celsius' }, context) => {
169
+ console.log(`Run ID: ${context.runId}`);
170
+ return { temperature: 22, units, city };
171
+ },
172
+ });
173
+ ```
174
+
175
+ ### Tool Context
176
+
177
+ Every tool receives a context object:
178
+
179
+ ```typescript
180
+ interface ToolContext {
181
+ agentId: string;
182
+ runId: string;
183
+ signal: AbortSignal;
184
+ }
185
+ ```
186
+
187
+ ### Sandboxed Tools
188
+
189
+ Execute tools in isolated Docker or WASM environments:
190
+
191
+ ```typescript
192
+ const shellTool = tool({
193
+ name: 'run_shell',
194
+ description: 'Execute shell commands safely',
195
+ parameters: z.object({
196
+ command: z.string(),
197
+ }),
198
+ sandbox: {
199
+ type: 'docker',
200
+ image: 'ubuntu:22.04',
201
+ },
202
+ timeout: 30000,
203
+ execute: async ({ command }) => command,
204
+ });
205
+ ```
206
+
207
+ ### Tool Registry
208
+
209
+ ```typescript
210
+ import { ToolRegistry } from '@cogitator-ai/core';
211
+
212
+ const registry = new ToolRegistry();
213
+
214
+ registry.register(calculator);
215
+ registry.registerMany([datetime, webSearch, fileRead]);
216
+
217
+ const tool = registry.get('calculator');
218
+ const names = registry.getNames();
219
+ const schemas = registry.getSchemas();
220
+ ```
221
+
222
+ ### Built-in Tools
223
+
224
+ | Tool | Description |
225
+ | --------------- | -------------------------------- |
226
+ | `calculator` | Evaluate math expressions |
227
+ | `datetime` | Get current date/time |
228
+ | `uuid` | Generate UUIDs |
229
+ | `randomNumber` | Random number generation |
230
+ | `randomString` | Random string generation |
231
+ | `hash` | Hash strings (md5, sha256, etc.) |
232
+ | `base64Encode` | Encode to base64 |
233
+ | `base64Decode` | Decode from base64 |
234
+ | `sleep` | Delay execution |
235
+ | `jsonParse` | Parse JSON strings |
236
+ | `jsonStringify` | Stringify to JSON |
237
+ | `regexMatch` | Match regex patterns |
238
+ | `regexReplace` | Replace with regex |
239
+ | `fileRead` | Read file contents |
240
+ | `fileWrite` | Write to files |
241
+ | `fileList` | List directory contents |
242
+ | `fileExists` | Check if file exists |
243
+ | `fileDelete` | Delete files |
244
+ | `httpRequest` | Make HTTP requests |
245
+ | `exec` | Execute shell commands |
246
+
247
+ ```typescript
248
+ import { builtinTools, calculator, datetime } from '@cogitator-ai/core';
249
+
250
+ const agent = new Agent({
251
+ name: 'utility-agent',
252
+ instructions: 'Use your tools to help users',
253
+ model: 'openai/gpt-4o',
254
+ tools: builtinTools,
255
+ });
256
+ ```
257
+
258
+ ---
259
+
260
+ ## Run Options
261
+
262
+ ```typescript
263
+ import { Cogitator, Agent } from '@cogitator-ai/core';
264
+
265
+ const cog = new Cogitator();
266
+ const agent = new Agent({
267
+ /* ... */
268
+ });
269
+
270
+ const result = await cog.run(agent, {
271
+ input: 'Analyze this data...',
272
+
273
+ threadId: 'thread_123',
274
+ context: { userId: 'user_456', task: 'analysis' },
275
+
276
+ timeout: 60000,
277
+ stream: true,
278
+ onToken: (token) => process.stdout.write(token),
279
+
280
+ useMemory: true,
281
+ loadHistory: true,
282
+ saveHistory: true,
283
+
284
+ onToolCall: (call) => console.log('Tool:', call.name),
285
+ onToolResult: (result) => console.log('Result:', result.result),
286
+ onSpan: (span) => console.log('Span:', span.name),
287
+ onRunStart: ({ runId }) => console.log('Started:', runId),
288
+ onRunComplete: (result) => console.log('Completed'),
289
+ onRunError: (error) => console.error('Error:', error),
290
+ onMemoryError: (error, op) => console.warn(`Memory ${op} failed`),
291
+ });
292
+ ```
293
+
294
+ ### Run Result
295
+
296
+ ```typescript
297
+ interface RunResult {
298
+ output: string;
299
+ runId: string;
300
+ agentId: string;
301
+ threadId: string;
302
+ usage: {
303
+ inputTokens: number;
304
+ outputTokens: number;
305
+ totalTokens: number;
306
+ cost: number;
307
+ duration: number;
308
+ };
309
+ toolCalls: ToolCall[];
310
+ messages: Message[];
311
+ trace: { traceId: string; spans: Span[] };
312
+ reflections?: Reflection[];
313
+ reflectionSummary?: ReflectionSummary;
314
+ }
315
+ ```
316
+
317
+ ---
318
+
319
+ ## Memory Integration
320
+
321
+ Configure memory for conversation persistence:
322
+
323
+ ```typescript
324
+ const cog = new Cogitator({
325
+ memory: {
326
+ adapter: 'redis',
327
+ redis: {
328
+ url: 'redis://localhost:6379',
329
+ keyPrefix: 'cogitator:',
330
+ },
331
+ contextBuilder: {
332
+ maxTokens: 4000,
333
+ strategy: 'recent',
334
+ },
335
+ },
336
+ });
337
+
338
+ // Or PostgreSQL
339
+ const cog = new Cogitator({
340
+ memory: {
341
+ adapter: 'postgres',
342
+ postgres: {
343
+ connectionString: 'postgresql://...',
344
+ },
345
+ },
346
+ });
347
+
348
+ // Or in-memory
349
+ const cog = new Cogitator({
350
+ memory: {
351
+ adapter: 'memory',
352
+ inMemory: {
353
+ maxEntries: 1000,
354
+ },
355
+ },
356
+ });
357
+ ```
358
+
359
+ ---
360
+
361
+ ## Reflection Engine
362
+
363
+ Enable self-improvement through reflection on tool calls and runs:
364
+
365
+ ```typescript
366
+ import { Cogitator, ReflectionEngine, InMemoryInsightStore } from '@cogitator-ai/core';
367
+
368
+ const cog = new Cogitator({
369
+ reflection: {
370
+ enabled: true,
371
+ reflectionModel: 'openai/gpt-4o-mini',
372
+ reflectAfterToolCall: true,
373
+ reflectAtEnd: true,
374
+ minConfidenceToStore: 0.7,
375
+ maxInsightsPerAgent: 50,
376
+ },
377
+ });
378
+
379
+ const result = await cog.run(agent, { input: 'Complete this task...' });
380
+
381
+ console.log('Reflections:', result.reflections);
382
+ console.log('Summary:', result.reflectionSummary);
383
+
384
+ const insights = await cog.getInsights(agent.id);
385
+ console.log('Learned insights:', insights);
386
+ ```
387
+
388
+ ### Standalone Reflection Engine
389
+
390
+ ```typescript
391
+ import { ReflectionEngine, InMemoryInsightStore, createLLMBackend } from '@cogitator-ai/core';
392
+
393
+ const backend = createLLMBackend('openai', { apiKey: '...' });
394
+ const insightStore = new InMemoryInsightStore();
395
+
396
+ const engine = new ReflectionEngine({
397
+ llm: backend,
398
+ insightStore,
399
+ config: {
400
+ reflectAfterToolCall: true,
401
+ minConfidenceToStore: 0.7,
402
+ },
403
+ });
404
+
405
+ const result = await engine.reflectOnToolCall(action, agentContext);
406
+ if (result.shouldAdjustStrategy) {
407
+ console.log('Suggested action:', result.suggestedAction);
408
+ }
409
+ ```
410
+
411
+ ---
412
+
413
+ ## Tree-of-Thought Reasoning
414
+
415
+ Explore multiple reasoning paths before deciding:
416
+
417
+ ```typescript
418
+ import { ThoughtTreeExecutor, BranchGenerator, BranchEvaluator } from '@cogitator-ai/core';
419
+
420
+ const executor = new ThoughtTreeExecutor(cogitator, {
421
+ maxBranches: 5,
422
+ maxDepth: 3,
423
+ explorationStrategy: 'best-first',
424
+ pruneThreshold: 0.3,
425
+ });
426
+
427
+ const result = await executor.run(agent, {
428
+ input: 'Solve this complex problem...',
429
+ explorationBudget: 10,
430
+ });
431
+
432
+ console.log('Best path:', result.bestPath);
433
+ console.log('All branches explored:', result.tree.branches.length);
434
+ console.log('Stats:', result.stats);
435
+ ```
436
+
437
+ ### ToT Configuration
438
+
439
+ ```typescript
440
+ const executor = new ThoughtTreeExecutor(cogitator, {
441
+ maxBranches: 5,
442
+ maxDepth: 3,
443
+ explorationStrategy: 'breadth-first',
444
+ pruneThreshold: 0.3,
445
+ branchTemperature: 0.8,
446
+ evaluationModel: 'openai/gpt-4o-mini',
447
+ });
448
+ ```
449
+
450
+ ---
451
+
452
+ ## Agent Optimizer (Learning)
453
+
454
+ DSPy-style optimization through execution traces:
455
+
456
+ ```typescript
457
+ import {
458
+ AgentOptimizer,
459
+ InMemoryTraceStore,
460
+ createSuccessMetric,
461
+ createContainsMetric,
462
+ } from '@cogitator-ai/core';
463
+
464
+ const traceStore = new InMemoryTraceStore();
465
+ const optimizer = new AgentOptimizer(cogitator, {
466
+ traceStore,
467
+ optimizationModel: 'openai/gpt-4o',
468
+ maxCandidates: 5,
469
+ evaluationRuns: 3,
470
+ });
471
+
472
+ const result = await optimizer.compile(agent, {
473
+ demos: [
474
+ { input: 'Calculate 2+2', expectedOutput: '4' },
475
+ { input: 'Calculate 10*5', expectedOutput: '50' },
476
+ ],
477
+ metric: createSuccessMetric(),
478
+ maxIterations: 10,
479
+ });
480
+
481
+ console.log('Optimized instructions:', result.optimizedAgent.instructions);
482
+ console.log('Improvement:', result.improvement);
483
+ ```
484
+
485
+ ### Built-in Metrics
486
+
487
+ ```typescript
488
+ import {
489
+ createSuccessMetric,
490
+ createExactMatchMetric,
491
+ createContainsMetric,
492
+ MetricEvaluator,
493
+ } from '@cogitator-ai/core';
494
+
495
+ const successMetric = createSuccessMetric();
496
+
497
+ const exactMatch = createExactMatchMetric();
498
+
499
+ const containsMetric = createContainsMetric(['error', 'failed'], { negate: true });
500
+ ```
501
+
502
+ ### Demo Selection
503
+
504
+ ```typescript
505
+ import { DemoSelector } from '@cogitator-ai/core';
506
+
507
+ const selector = new DemoSelector({
508
+ strategy: 'diverse',
509
+ maxDemos: 5,
510
+ });
511
+
512
+ const selectedDemos = selector.select(allDemos, currentInput);
513
+ ```
514
+
515
+ ---
516
+
517
+ ## Prompt Auto-Optimization
518
+
519
+ Capture prompts, run A/B tests, monitor performance, and automatically optimize agent instructions.
520
+
521
+ ### Prompt Logger
522
+
523
+ Wrap any LLM backend to capture all prompts:
524
+
525
+ ```typescript
526
+ import { wrapWithPromptLogger, PostgresTraceStore } from '@cogitator-ai/core';
527
+
528
+ const store = new PostgresTraceStore({
529
+ connectionString: process.env.DATABASE_URL!,
530
+ });
531
+
532
+ const wrappedBackend = wrapWithPromptLogger(openaiBackend, store, {
533
+ captureSystemPrompt: true,
534
+ captureTools: true,
535
+ captureResponse: true,
536
+ });
537
+ ```
538
+
539
+ ### A/B Testing Framework
540
+
541
+ Test instruction variants with statistical analysis:
542
+
543
+ ```typescript
544
+ import { ABTestingFramework } from '@cogitator-ai/core';
545
+
546
+ const abTesting = new ABTestingFramework({
547
+ store: abTestStore,
548
+ defaultConfidenceLevel: 0.95,
549
+ defaultMinSampleSize: 50,
550
+ });
551
+
552
+ const test = await abTesting.createTest({
553
+ agentId: 'agent-1',
554
+ name: 'Instruction Experiment',
555
+ controlInstructions: 'You are a helpful assistant.',
556
+ treatmentInstructions: 'You are an expert assistant. Be concise.',
557
+ treatmentAllocation: 0.5,
558
+ });
559
+
560
+ await abTesting.startTest(test.id);
561
+
562
+ const variant = abTesting.selectVariant(test);
563
+ const instructions = abTesting.getInstructionsForVariant(test, variant);
564
+
565
+ await abTesting.recordResult(test.id, variant, score, latency, cost);
566
+
567
+ const { test: completed, outcome } = await abTesting.completeTest(test.id);
568
+ console.log('Winner:', outcome.winner);
569
+ console.log('p-value:', outcome.pValue);
570
+ console.log('Effect size:', outcome.effectSize);
68
571
  ```
69
572
 
70
- ## Documentation
573
+ ### Prompt Monitor
574
+
575
+ Real-time performance monitoring with degradation detection:
576
+
577
+ ```typescript
578
+ import { PromptMonitor } from '@cogitator-ai/core';
579
+
580
+ const monitor = new PromptMonitor({
581
+ windowSize: 60 * 60 * 1000,
582
+ scoreDropThreshold: 0.15,
583
+ latencySpikeThreshold: 2.0,
584
+ errorRateThreshold: 0.1,
585
+ enableAutoRollback: true,
586
+ onAlert: (alert) => {
587
+ console.log(`Alert: ${alert.type} (${alert.severity})`);
588
+ },
589
+ });
590
+
591
+ const alerts = monitor.recordExecution(trace);
592
+
593
+ const metrics = monitor.getCurrentMetrics('agent-1');
594
+ console.log('Avg score:', metrics.avgScore);
595
+ console.log('P95 latency:', metrics.p95Latency);
596
+ ```
597
+
598
+ ### Rollback Manager
599
+
600
+ Version control for agent instructions:
601
+
602
+ ```typescript
603
+ import { RollbackManager } from '@cogitator-ai/core';
604
+
605
+ const rollback = new RollbackManager({ store: versionStore });
606
+
607
+ const version = await rollback.deployVersion(
608
+ 'agent-1',
609
+ 'New optimized instructions',
610
+ 'optimization',
611
+ 'opt-run-123'
612
+ );
613
+
614
+ const result = await rollback.rollbackToPrevious('agent-1');
615
+ if (result.success) {
616
+ console.log('Rolled back to version:', result.previousVersion?.version);
617
+ }
618
+
619
+ const history = await rollback.getVersionHistory('agent-1', 10);
620
+ ```
621
+
622
+ ### Auto-Optimizer
623
+
624
+ Automated optimization pipeline with A/B testing and rollback:
625
+
626
+ ```typescript
627
+ import { AutoOptimizer } from '@cogitator-ai/core';
628
+
629
+ const optimizer = new AutoOptimizer({
630
+ enabled: true,
631
+ triggerAfterRuns: 100,
632
+ minRunsForOptimization: 20,
633
+ requireABTest: true,
634
+ maxOptimizationsPerDay: 3,
635
+ agentOptimizer,
636
+ abTesting,
637
+ monitor,
638
+ rollbackManager,
639
+ onOptimizationComplete: (run) => {
640
+ console.log('Optimization completed:', run.status);
641
+ },
642
+ onRollback: (agentId, reason) => {
643
+ console.log('Rollback triggered:', reason);
644
+ },
645
+ });
646
+
647
+ await optimizer.recordExecution(trace);
648
+ ```
649
+
650
+ ---
651
+
652
+ ## Time Travel Debugging
653
+
654
+ Checkpoint, replay, fork, and compare agent executions:
655
+
656
+ ```typescript
657
+ import { TimeTravel, InMemoryCheckpointStore } from '@cogitator-ai/core';
658
+
659
+ const timeTravel = new TimeTravel(cogitator);
660
+
661
+ const result = await cogitator.run(agent, { input: 'Original task...' });
662
+ const checkpoints = await timeTravel.checkpointAll(result, 'original');
663
+
664
+ const replayResult = await timeTravel.replayLive(agent, checkpoints[2].id);
665
+ console.log('Replayed from step 2:', replayResult.result.output);
666
+
667
+ const forkResult = await timeTravel.fork(agent, checkpoints[2].id, {
668
+ newInput: 'Modified task...',
669
+ });
670
+ console.log('Forked result:', forkResult.result.output);
671
+
672
+ const diff = await timeTravel.compareWithOriginal(forkResult);
673
+ console.log(timeTravel.formatDiff(diff));
674
+ ```
675
+
676
+ ### Forking Variants
677
+
678
+ ```typescript
679
+ const forkWithContext = await timeTravel.forkWithContext(
680
+ agent,
681
+ checkpointId,
682
+ 'Additional context: the user is an expert'
683
+ );
684
+
685
+ const forkWithMock = await timeTravel.forkWithMockedTool(agent, checkpointId, 'api_call', {
686
+ status: 'success',
687
+ data: 'mocked data',
688
+ });
689
+
690
+ const forkWithMocks = await timeTravel.forkWithMockedTools(agent, checkpointId, {
691
+ api_call: { status: 'success' },
692
+ database_query: { rows: [] },
693
+ });
694
+
695
+ const forkWithNewInput = await timeTravel.forkWithNewInput(
696
+ agent,
697
+ checkpointId,
698
+ 'Completely different task...'
699
+ );
700
+
701
+ const variants = await timeTravel.forkMultiple(agent, checkpointId, [
702
+ { newInput: 'Variant A' },
703
+ { newInput: 'Variant B' },
704
+ { additionalContext: 'Be more concise' },
705
+ ]);
706
+ ```
707
+
708
+ ### Replay Modes
709
+
710
+ ```typescript
711
+ const deterministicReplay = await timeTravel.replayDeterministic(agent, checkpointId);
712
+
713
+ const liveReplay = await timeTravel.replayLive(agent, checkpointId, {
714
+ maxSteps: 5,
715
+ });
716
+ ```
717
+
718
+ ---
719
+
720
+ ## Error Handling & Resilience
721
+
722
+ ### Retry with Backoff
723
+
724
+ ```typescript
725
+ import { withRetry, retryable } from '@cogitator-ai/core';
726
+
727
+ const result = await withRetry(() => unreliableApiCall(), {
728
+ maxRetries: 5,
729
+ baseDelay: 1000,
730
+ maxDelay: 30000,
731
+ backoff: 'exponential',
732
+ jitter: 0.1,
733
+ onRetry: (error, attempt, delay) => {
734
+ console.log(`Retry ${attempt} after ${delay}ms: ${error.message}`);
735
+ },
736
+ });
737
+
738
+ const retryableFetch = retryable(fetch, { maxRetries: 3 });
739
+ const response = await retryableFetch('https://api.example.com');
740
+ ```
741
+
742
+ ### Circuit Breaker
743
+
744
+ ```typescript
745
+ import { CircuitBreaker, CircuitBreakerRegistry } from '@cogitator-ai/core';
746
+
747
+ const breaker = new CircuitBreaker({
748
+ threshold: 5,
749
+ resetTimeout: 30000,
750
+ successThreshold: 2,
751
+ });
752
+
753
+ if (breaker.canExecute()) {
754
+ try {
755
+ const result = await riskyOperation();
756
+ breaker.recordSuccess();
757
+ } catch (error) {
758
+ breaker.recordFailure();
759
+ throw error;
760
+ }
761
+ }
762
+
763
+ breaker.onStateChange((state) => {
764
+ console.log('Circuit state:', state);
765
+ });
766
+ ```
767
+
768
+ ### Fallback Patterns
769
+
770
+ ```typescript
771
+ import {
772
+ withFallback,
773
+ withGracefulDegradation,
774
+ createLLMFallbackExecutor,
775
+ } from '@cogitator-ai/core';
776
+
777
+ const result = await withFallback(
778
+ () => primaryCall(),
779
+ () => fallbackCall()
780
+ );
781
+
782
+ const degraded = await withGracefulDegradation(
783
+ () => fullFeatureCall(),
784
+ [() => reducedFeatureCall(), () => minimalCall(), () => cachedResult()]
785
+ );
786
+
787
+ const llmExecutor = createLLMFallbackExecutor([
788
+ { provider: 'openai', model: 'gpt-4o' },
789
+ { provider: 'anthropic', model: 'claude-sonnet-4-20250514' },
790
+ { provider: 'ollama', model: 'llama3.2:70b' },
791
+ ]);
792
+ const response = await llmExecutor.chat(request);
793
+ ```
794
+
795
+ ---
796
+
797
+ ## Logging
798
+
799
+ ```typescript
800
+ import { Logger, getLogger, setLogger, createLogger } from '@cogitator-ai/core';
801
+
802
+ const logger = createLogger({
803
+ level: 'debug',
804
+ prefix: '[MyApp]',
805
+ timestamps: true,
806
+ });
807
+
808
+ setLogger(logger);
809
+
810
+ getLogger().info('Agent started', { agentId: agent.id });
811
+ getLogger().debug('Tool call', { tool: 'calculator', args: { expression: '2+2' } });
812
+ getLogger().warn('Rate limited', { retryAfter: 60 });
813
+ getLogger().error('Failed', { error: 'Connection timeout' });
814
+ ```
815
+
816
+ ---
817
+
818
+ ## Type Reference
819
+
820
+ ### Core Types
821
+
822
+ ```typescript
823
+ import type {
824
+ Agent,
825
+ AgentConfig,
826
+ Tool,
827
+ ToolConfig,
828
+ ToolContext,
829
+ Message,
830
+ MessageRole,
831
+ ToolCall,
832
+ ToolResult,
833
+ CogitatorConfig,
834
+ RunOptions,
835
+ RunResult,
836
+ Span,
837
+ } from '@cogitator-ai/core';
838
+ ```
839
+
840
+ ### LLM Types
841
+
842
+ ```typescript
843
+ import type {
844
+ LLMBackend,
845
+ LLMProvider,
846
+ LLMConfig,
847
+ ChatRequest,
848
+ ChatResponse,
849
+ ChatStreamChunk,
850
+ } from '@cogitator-ai/core';
851
+ ```
852
+
853
+ ### Reasoning Types
854
+
855
+ ```typescript
856
+ import type {
857
+ ToTConfig,
858
+ ToTResult,
859
+ ToTStats,
860
+ ThoughtTree,
861
+ ThoughtNode,
862
+ ThoughtBranch,
863
+ BranchScore,
864
+ ExplorationStrategy,
865
+ } from '@cogitator-ai/core';
866
+ ```
867
+
868
+ ### Learning Types
869
+
870
+ ```typescript
871
+ import type {
872
+ ExecutionTrace,
873
+ ExecutionStep,
874
+ TraceStore,
875
+ Demo,
876
+ MetricFn,
877
+ MetricResult,
878
+ OptimizerConfig,
879
+ OptimizationResult,
880
+ } from '@cogitator-ai/core';
881
+ ```
882
+
883
+ ### Time Travel Types
884
+
885
+ ```typescript
886
+ import type {
887
+ ExecutionCheckpoint,
888
+ ReplayOptions,
889
+ ReplayResult,
890
+ ForkOptions,
891
+ ForkResult,
892
+ TraceDiff,
893
+ TimeTravelConfig,
894
+ } from '@cogitator-ai/core';
895
+ ```
896
+
897
+ ### Error Types
898
+
899
+ ```typescript
900
+ import { CogitatorError, ErrorCode, isRetryableError, getRetryDelay } from '@cogitator-ai/core';
901
+
902
+ try {
903
+ await riskyOperation();
904
+ } catch (error) {
905
+ if (error instanceof CogitatorError) {
906
+ console.log('Code:', error.code);
907
+ console.log('Retryable:', isRetryableError(error));
908
+ console.log('Retry delay:', getRetryDelay(error, 1000));
909
+ }
910
+ }
911
+ ```
912
+
913
+ ---
914
+
915
+ ## Examples
916
+
917
+ ### Research Agent with Memory
918
+
919
+ ```typescript
920
+ import { Cogitator, Agent, tool } from '@cogitator-ai/core';
921
+
922
+ const webSearch = tool({
923
+ name: 'web_search',
924
+ description: 'Search the web',
925
+ parameters: z.object({ query: z.string() }),
926
+ execute: async ({ query }) => {
927
+ return { results: await searchApi(query) };
928
+ },
929
+ });
930
+
931
+ const cog = new Cogitator({
932
+ memory: { adapter: 'redis', redis: { url: 'redis://localhost:6379' } },
933
+ reflection: { enabled: true },
934
+ });
935
+
936
+ const researcher = new Agent({
937
+ name: 'researcher',
938
+ instructions: 'Research topics thoroughly using web search',
939
+ model: 'openai/gpt-4o',
940
+ tools: [webSearch],
941
+ });
942
+
943
+ const result = await cog.run(researcher, {
944
+ input: 'Research the latest AI developments',
945
+ threadId: 'research-session-1',
946
+ });
947
+ ```
948
+
949
+ ### Streaming Response
950
+
951
+ ```typescript
952
+ const result = await cog.run(agent, {
953
+ input: 'Write a story about...',
954
+ stream: true,
955
+ onToken: (token) => process.stdout.write(token),
956
+ });
957
+ ```
958
+
959
+ ### Full Observability
960
+
961
+ ```typescript
962
+ const result = await cog.run(agent, {
963
+ input: 'Analyze this data...',
964
+ onRunStart: ({ runId }) => console.log(`Run ${runId} started`),
965
+ onToolCall: (call) => console.log(`Calling ${call.name}`),
966
+ onToolResult: (result) => console.log(`Result: ${JSON.stringify(result.result)}`),
967
+ onSpan: (span) => {
968
+ console.log(`[${span.name}] ${span.duration}ms`);
969
+ },
970
+ onRunComplete: (result) => {
971
+ console.log(`Cost: $${result.usage.cost.toFixed(4)}`);
972
+ console.log(`Tokens: ${result.usage.totalTokens}`);
973
+ },
974
+ });
975
+ ```
71
976
 
72
- See the [Cogitator documentation](https://github.com/eL1fe/cogitator) for full API reference.
977
+ ---
73
978
 
74
979
  ## License
75
980