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,497 @@
|
|
|
1
|
+
# Memory System
|
|
2
|
+
|
|
3
|
+
The Memory System provides intelligent context management for AI agents with three levels of memory: short-term, long-term, and entity-based. This enables agents to maintain conversation context, recall important facts, and track entities across interactions.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The Memory System provides:
|
|
8
|
+
|
|
9
|
+
- **ShortTermMemory**: Recent conversation context with automatic decay
|
|
10
|
+
- **LongTermMemory**: Persistent facts with semantic search (RAG support)
|
|
11
|
+
- **EntityMemory**: Track entities (people, places, objects) and their facts
|
|
12
|
+
- **Unified Interface**: MemorySystem combines all three levels
|
|
13
|
+
- **Auto-Summarization**: Automatic summarization of long conversations
|
|
14
|
+
- **Importance Scoring**: Track memory importance for prioritization
|
|
15
|
+
- **RAG Integration**: Optional vector database integration for semantic search
|
|
16
|
+
|
|
17
|
+
## Core Components
|
|
18
|
+
|
|
19
|
+
### MemoryItem
|
|
20
|
+
|
|
21
|
+
Base structure for all memories:
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
interface MemoryItem {
|
|
25
|
+
id: string; // Unique identifier
|
|
26
|
+
content: string; // Memory content
|
|
27
|
+
timestamp: number; // Creation time
|
|
28
|
+
importance: number; // Importance score (0-1)
|
|
29
|
+
metadata?: Record<string, unknown>; // Additional data
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### ShortTermMemory
|
|
34
|
+
|
|
35
|
+
Manages recent conversation context:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import { ShortTermMemory } from 'societyai';
|
|
39
|
+
|
|
40
|
+
const memory = new ShortTermMemory({
|
|
41
|
+
maxMessages: 20, // Max messages to keep
|
|
42
|
+
summarizeAfter: 50, // Summarize after N messages
|
|
43
|
+
decayRate: 0.1, // Importance decay rate
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Add messages
|
|
47
|
+
memory.add('User asked about weather');
|
|
48
|
+
memory.add('Agent provided weather info');
|
|
49
|
+
|
|
50
|
+
// Retrieve recent messages
|
|
51
|
+
const recent = memory.getRecent(10); // Last 10 messages
|
|
52
|
+
|
|
53
|
+
// Search by content
|
|
54
|
+
const results = memory.search('weather');
|
|
55
|
+
|
|
56
|
+
// Filter by metadata
|
|
57
|
+
const important = memory.filter({ type: 'important' });
|
|
58
|
+
|
|
59
|
+
// Clear all
|
|
60
|
+
memory.clear();
|
|
61
|
+
|
|
62
|
+
// Get statistics
|
|
63
|
+
const stats = memory.getStats();
|
|
64
|
+
console.log(stats); // { messages: 2, avgImportance: 0.5, oldestTimestamp: ... }
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### LongTermMemory
|
|
68
|
+
|
|
69
|
+
Stores persistent facts with optional semantic search:
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { LongTermMemory } from 'societyai';
|
|
73
|
+
|
|
74
|
+
const memory = new LongTermMemory({
|
|
75
|
+
maxEntries: 1000, // Max entries to store
|
|
76
|
+
pruneStrategy: 'lru', // or 'importance', 'oldest'
|
|
77
|
+
provider: vectorDB, // Optional RAG provider
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Add facts
|
|
81
|
+
const id1 = await memory.add('Paris is the capital of France', {
|
|
82
|
+
type: 'fact',
|
|
83
|
+
importance: 0.9,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const id2 = await memory.add('The Eiffel Tower is in Paris', {
|
|
87
|
+
type: 'fact',
|
|
88
|
+
related: ['paris', 'landmarks'],
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Retrieve by query (semantic search if provider available)
|
|
92
|
+
const result = await memory.retrieve({
|
|
93
|
+
query: 'French capital city',
|
|
94
|
+
limit: 5,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
console.log(result.memories); // Relevant memories
|
|
98
|
+
console.log(result.scores); // Relevance scores
|
|
99
|
+
|
|
100
|
+
// Get specific memory
|
|
101
|
+
const fact = await memory.get(id1);
|
|
102
|
+
|
|
103
|
+
// Delete memory
|
|
104
|
+
await memory.delete(id1);
|
|
105
|
+
|
|
106
|
+
// Clear all
|
|
107
|
+
await memory.clear();
|
|
108
|
+
|
|
109
|
+
// Statistics
|
|
110
|
+
const stats = memory.getStats();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### EntityMemory
|
|
114
|
+
|
|
115
|
+
Track entities and their associated facts:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { EntityMemory } from 'societyai';
|
|
119
|
+
|
|
120
|
+
const memory = new EntityMemory({
|
|
121
|
+
maxEntities: 500,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Upsert entity (create or update)
|
|
125
|
+
memory.upsert({
|
|
126
|
+
name: 'Alice',
|
|
127
|
+
type: 'person',
|
|
128
|
+
facts: ['Works at TechCorp', 'Lives in Paris', 'Knows Python'],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Update existing entity
|
|
132
|
+
memory.upsert({
|
|
133
|
+
name: 'Alice',
|
|
134
|
+
type: 'person',
|
|
135
|
+
facts: ['Works at TechCorp', 'Lives in Paris', 'Knows Python', 'Attended AI conference'],
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Get entity
|
|
139
|
+
const alice = memory.get('Alice');
|
|
140
|
+
|
|
141
|
+
// Search entities
|
|
142
|
+
const people = memory.search('python'); // Entities with 'python' in facts
|
|
143
|
+
|
|
144
|
+
// Filter by type
|
|
145
|
+
const allPeople = memory.getByType('person');
|
|
146
|
+
|
|
147
|
+
// Delete entity
|
|
148
|
+
memory.delete('Alice');
|
|
149
|
+
|
|
150
|
+
// Statistics
|
|
151
|
+
const stats = memory.getStats();
|
|
152
|
+
console.log(stats); // { total: 1, byType: { person: 1 } }
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### MemorySystem
|
|
156
|
+
|
|
157
|
+
Unified interface combining all memory types:
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { MemoryBuilder } from 'societyai';
|
|
161
|
+
|
|
162
|
+
// Create integrated memory system
|
|
163
|
+
const memory = MemoryBuilder.create()
|
|
164
|
+
.withShortTermMemory({
|
|
165
|
+
maxMessages: 20,
|
|
166
|
+
decayRate: 0.1,
|
|
167
|
+
})
|
|
168
|
+
.withLongTermMemory({
|
|
169
|
+
maxEntries: 1000,
|
|
170
|
+
pruneStrategy: 'importance',
|
|
171
|
+
})
|
|
172
|
+
.withEntityMemory({
|
|
173
|
+
maxEntities: 500,
|
|
174
|
+
})
|
|
175
|
+
.build();
|
|
176
|
+
|
|
177
|
+
// Add to appropriate memory level
|
|
178
|
+
await memory.add('User greeted', { type: 'conversation' }); // Short-term
|
|
179
|
+
await memory.add('Important fact about X', { type: 'fact', importance: 0.9 }); // Long-term
|
|
180
|
+
|
|
181
|
+
// Retrieve combined context
|
|
182
|
+
const context = await memory.retrieve('conversation about X', {
|
|
183
|
+
includeShortTerm: true,
|
|
184
|
+
includeLongTerm: true,
|
|
185
|
+
includeEntities: true,
|
|
186
|
+
limit: 10,
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
console.log(context);
|
|
190
|
+
// Output:
|
|
191
|
+
// ## Recent Context
|
|
192
|
+
// User greeted
|
|
193
|
+
// ...
|
|
194
|
+
//
|
|
195
|
+
// ## Relevant Facts
|
|
196
|
+
// Important fact about X
|
|
197
|
+
// ...
|
|
198
|
+
//
|
|
199
|
+
// ## Related Entities
|
|
200
|
+
// X (concept): fact1, fact2
|
|
201
|
+
|
|
202
|
+
// Get all statistics
|
|
203
|
+
const stats = memory.getStats();
|
|
204
|
+
console.log(stats);
|
|
205
|
+
// {
|
|
206
|
+
// shortTerm: { messages: 5, avgImportance: 0.5 },
|
|
207
|
+
// longTerm: { total: 100, avgImportance: 0.7 },
|
|
208
|
+
// entities: { total: 20, byType: { person: 10, place: 5 } }
|
|
209
|
+
// }
|
|
210
|
+
|
|
211
|
+
// Clear all memory
|
|
212
|
+
await memory.clearAll();
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Complete Example
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
import { MemoryBuilder, StandardModelBase } from 'societyai';
|
|
219
|
+
|
|
220
|
+
// Create memory-aware agent
|
|
221
|
+
class MemoryAwareAgent extends StandardModelBase {
|
|
222
|
+
constructor(
|
|
223
|
+
private memory: MemorySystem,
|
|
224
|
+
name: string
|
|
225
|
+
) {
|
|
226
|
+
super({ name }, async (prompt: unknown) => {
|
|
227
|
+
const promptStr = typeof prompt === 'string' ? prompt : JSON.stringify(prompt);
|
|
228
|
+
|
|
229
|
+
// Retrieve relevant context
|
|
230
|
+
const context = await this.memory.retrieve(promptStr, {
|
|
231
|
+
includeShortTerm: true,
|
|
232
|
+
includeLongTerm: true,
|
|
233
|
+
limit: 5,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// Add context to prompt
|
|
237
|
+
const enhancedPrompt = `
|
|
238
|
+
Context from memory:
|
|
239
|
+
${context}
|
|
240
|
+
|
|
241
|
+
User: ${promptStr}
|
|
242
|
+
`;
|
|
243
|
+
|
|
244
|
+
// Process with context
|
|
245
|
+
const response = `Response considering: ${context}`;
|
|
246
|
+
|
|
247
|
+
// Store interaction
|
|
248
|
+
await this.memory.add(promptStr, {
|
|
249
|
+
type: 'conversation',
|
|
250
|
+
importance: 0.5,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
await this.memory.add(response, {
|
|
254
|
+
type: 'conversation',
|
|
255
|
+
importance: 0.5,
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
return response;
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Setup
|
|
264
|
+
const memory = MemoryBuilder.create()
|
|
265
|
+
.withShortTermMemory({ maxMessages: 20 })
|
|
266
|
+
.withLongTermMemory({ maxEntries: 1000 })
|
|
267
|
+
.withEntityMemory({ maxEntities: 500 })
|
|
268
|
+
.build();
|
|
269
|
+
|
|
270
|
+
const agent = new MemoryAwareAgent(memory, 'assistant');
|
|
271
|
+
|
|
272
|
+
// Use agent
|
|
273
|
+
await agent.process('Hello!');
|
|
274
|
+
await agent.process('What did we just talk about?');
|
|
275
|
+
// Agent can recall the greeting from short-term memory
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Auto-Summarization
|
|
279
|
+
|
|
280
|
+
ShortTermMemory automatically summarizes old messages:
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
const memory = new ShortTermMemory({
|
|
284
|
+
maxMessages: 10,
|
|
285
|
+
summarizeAfter: 20,
|
|
286
|
+
summarizer: async (messages) => {
|
|
287
|
+
// Custom summarization logic
|
|
288
|
+
return `Summary of ${messages.length} messages: ...`;
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// After 20 messages, older messages are summarized
|
|
293
|
+
for (let i = 0; i < 25; i++) {
|
|
294
|
+
memory.add(`Message ${i}`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Recent messages + summary available
|
|
298
|
+
const recent = memory.getRecent(10);
|
|
299
|
+
const summary = memory.getSummary();
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Importance Scoring
|
|
303
|
+
|
|
304
|
+
Memories can have importance scores that affect retrieval:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
// High importance
|
|
308
|
+
await memory.add('Critical system update', {
|
|
309
|
+
type: 'alert',
|
|
310
|
+
importance: 1.0,
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// Medium importance
|
|
314
|
+
await memory.add('User preference noted', {
|
|
315
|
+
type: 'setting',
|
|
316
|
+
importance: 0.6,
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// Low importance
|
|
320
|
+
await memory.add('Casual greeting', {
|
|
321
|
+
type: 'conversation',
|
|
322
|
+
importance: 0.2,
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
// Retrieve prioritizes high importance
|
|
326
|
+
const important = memory.filter({ importance: { min: 0.7 } });
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## Time-Based Decay
|
|
330
|
+
|
|
331
|
+
ShortTermMemory importance decays over time:
|
|
332
|
+
|
|
333
|
+
```typescript
|
|
334
|
+
const memory = new ShortTermMemory({
|
|
335
|
+
maxMessages: 100,
|
|
336
|
+
decayRate: 0.1, // Decay 10% per hour
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
memory.add('Important message', { importance: 1.0 });
|
|
340
|
+
|
|
341
|
+
// After 1 hour, importance is ~0.9
|
|
342
|
+
// After 10 hours, importance is ~0.35
|
|
343
|
+
// Older messages naturally become less important
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## RAG Integration
|
|
347
|
+
|
|
348
|
+
Integrate vector databases for semantic search:
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
interface VectorProvider {
|
|
352
|
+
add(id: string, text: string, embedding: number[]): Promise<void>;
|
|
353
|
+
search(embedding: number[], limit: number): Promise<SearchResult[]>;
|
|
354
|
+
delete(id: string): Promise<void>;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
class PineconeProvider implements VectorProvider {
|
|
358
|
+
// Implementation using Pinecone
|
|
359
|
+
async add(id: string, text: string, embedding: number[]): Promise<void> {
|
|
360
|
+
// Store in Pinecone
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async search(embedding: number[], limit: number): Promise<SearchResult[]> {
|
|
364
|
+
// Search Pinecone
|
|
365
|
+
return [];
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
async delete(id: string): Promise<void> {
|
|
369
|
+
// Delete from Pinecone
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Use with long-term memory
|
|
374
|
+
const memory = new LongTermMemory({
|
|
375
|
+
maxEntries: 10000,
|
|
376
|
+
provider: new PineconeProvider(),
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Searches use semantic similarity
|
|
380
|
+
const results = await memory.retrieve({
|
|
381
|
+
query: 'artificial intelligence applications',
|
|
382
|
+
limit: 5,
|
|
383
|
+
});
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
## Entity Tracking Example
|
|
387
|
+
|
|
388
|
+
Track conversation participants and subjects:
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
const memory = new EntityMemory({ maxEntities: 1000 });
|
|
392
|
+
|
|
393
|
+
// Track people
|
|
394
|
+
memory.upsert({
|
|
395
|
+
name: 'Alice',
|
|
396
|
+
type: 'person',
|
|
397
|
+
facts: ['Software engineer', 'Interested in AI', 'Asked about memory systems'],
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
memory.upsert({
|
|
401
|
+
name: 'Bob',
|
|
402
|
+
type: 'person',
|
|
403
|
+
facts: ['Project manager', 'Managing AI project', 'Needs documentation'],
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
// Track projects
|
|
407
|
+
memory.upsert({
|
|
408
|
+
name: 'Project X',
|
|
409
|
+
type: 'project',
|
|
410
|
+
facts: ['AI-powered analytics', 'Led by Bob', 'Team includes Alice'],
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
// Search related entities
|
|
414
|
+
const team = memory.search('AI project');
|
|
415
|
+
// Returns: Alice, Bob, Project X
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
## Memory Pruning Strategies
|
|
419
|
+
|
|
420
|
+
Control how long-term memory is pruned when full:
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
// Least Recently Used (LRU)
|
|
424
|
+
const lruMemory = new LongTermMemory({
|
|
425
|
+
maxEntries: 100,
|
|
426
|
+
pruneStrategy: 'lru',
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
// By importance (keep most important)
|
|
430
|
+
const importanceMemory = new LongTermMemory({
|
|
431
|
+
maxEntries: 100,
|
|
432
|
+
pruneStrategy: 'importance',
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
// Oldest first (FIFO)
|
|
436
|
+
const fifoMemory = new LongTermMemory({
|
|
437
|
+
maxEntries: 100,
|
|
438
|
+
pruneStrategy: 'oldest',
|
|
439
|
+
});
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
## Integration with Graph
|
|
443
|
+
|
|
444
|
+
Use memory in graph-based workflows:
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
const memory = MemoryBuilder.create()
|
|
448
|
+
.withShortTermMemory({ maxMessages: 20 })
|
|
449
|
+
.withLongTermMemory({ maxEntries: 1000 })
|
|
450
|
+
.build();
|
|
451
|
+
|
|
452
|
+
// Create memory-aware agent
|
|
453
|
+
const agent = AgentBuilder.create()
|
|
454
|
+
.withId('memory-agent')
|
|
455
|
+
.withRole(role)
|
|
456
|
+
.withModel(new MemoryAwareModel(memory))
|
|
457
|
+
.build();
|
|
458
|
+
|
|
459
|
+
// Use in graph
|
|
460
|
+
const graph = GraphBuilder.create()
|
|
461
|
+
.addNode('start', NodeType.START)
|
|
462
|
+
.addNode('agent', NodeType.AGENT, { agentId: 'memory-agent' })
|
|
463
|
+
.addNode('end', NodeType.END)
|
|
464
|
+
.addEdge('start', 'agent')
|
|
465
|
+
.addEdge('agent', 'end')
|
|
466
|
+
.build();
|
|
467
|
+
|
|
468
|
+
const result = await graph.execute('User input', [agent]);
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
## Best Practices
|
|
472
|
+
|
|
473
|
+
1. **Choose Right Memory Level**:
|
|
474
|
+
- Short-term: Conversations, temporary context
|
|
475
|
+
- Long-term: Facts, knowledge, persistent data
|
|
476
|
+
- Entity: People, places, objects being discussed
|
|
477
|
+
|
|
478
|
+
2. **Set Importance**: Assign meaningful importance scores to prioritize memories
|
|
479
|
+
|
|
480
|
+
3. **Use Metadata**: Tag memories with metadata for easy filtering
|
|
481
|
+
|
|
482
|
+
4. **Regular Cleanup**: Clear old memories to maintain performance
|
|
483
|
+
|
|
484
|
+
5. **RAG for Scale**: Use vector databases for large knowledge bases
|
|
485
|
+
|
|
486
|
+
6. **Monitor Stats**: Track memory usage with getStats()
|
|
487
|
+
|
|
488
|
+
7. **Summarize Wisely**: Implement meaningful summarization for conversations
|
|
489
|
+
|
|
490
|
+
8. **Entity Normalization**: Normalize entity names (e.g., "Alice" vs "alice")
|
|
491
|
+
|
|
492
|
+
## Next Steps
|
|
493
|
+
|
|
494
|
+
- See [Graph Execution](./graph-execution.md) for complex workflows
|
|
495
|
+
- See [Tool Calling](./tool-calling.md) for external interactions
|
|
496
|
+
- See [Structured Output](./structured-output.md) for validation
|
|
497
|
+
- See [Examples](./examples.md) for complete implementations
|