chat-agent-toolkit 1.2.32 → 1.2.34
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/dist/research-agent.cjs.js +1 -1
- package/dist/research-agent.cjs.js.map +1 -1
- package/dist/research-agent.es.js +1 -1
- package/dist/research-agent.es.js.map +1 -1
- package/package.json +1 -1
- package/src/config/config-manager.ts +295 -295
- package/src/config/config-types.ts +65 -65
- package/src/config/environment-variables.ts +16 -16
- package/src/config/index.ts +26 -26
- package/src/config/language-models-database.ts +1434 -1434
- package/src/config/mcp-server-registry.ts +24 -24
- package/src/config/model-registry.ts +271 -271
- package/src/config/model-tester.ts +211 -211
- package/src/config/model-utils.ts +193 -193
- package/src/config/provider-ui-config.ts +196 -196
- package/src/connectors/open-connector.json +130 -130
- package/src/index.ts +26 -26
- package/src/memory/ARCHITECTURE.md +302 -302
- package/src/memory/README.md +224 -224
- package/src/memory/agent-memory-manager.ts +416 -416
- package/src/memory/example.ts +343 -343
- package/src/memory/index.ts +47 -47
- package/src/memory/mastra-integration.ts +604 -604
- package/src/memory/storage/drizzle-storage.ts +236 -236
- package/src/memory/storage/in-memory-storage.ts +551 -551
- package/src/memory/storage/storage-interface.ts +68 -68
- package/src/memory/types.ts +125 -125
- package/src/models/types.ts +4 -4
- package/src/provider-logos/01-ai.png +0 -0
- package/src/provider-logos/anthropic.png +0 -0
- package/src/provider-logos/aws-bedrock.png +0 -0
- package/src/provider-logos/aws-sagemaker.png +0 -0
- package/src/provider-logos/azure-openai-service.png +0 -0
- package/src/provider-logos/chatglm.png +0 -0
- package/src/provider-logos/cohere.png +0 -0
- package/src/provider-logos/gemini.png +0 -0
- package/src/provider-logos/groqcloud.png +0 -0
- package/src/provider-logos/huggingface.png +0 -0
- package/src/provider-logos/jina.png +0 -0
- package/src/provider-logos/localai.png +0 -0
- package/src/provider-logos/mistral-ai.png +0 -0
- package/src/provider-logos/moonshot-ai.png +0 -0
- package/src/provider-logos/ollama.png +0 -0
- package/src/provider-logos/openai.png +0 -0
- package/src/provider-logos/openllm.png +0 -0
- package/src/provider-logos/openrouter.png +0 -0
- package/src/provider-logos/replicate.png +0 -0
- package/src/provider-logos/together-ai.png +0 -0
- package/src/provider-logos/tongyi.png +0 -0
- package/src/provider-logos/xorbits-inference.png +0 -0
- package/src/provider-logos/zhipu-ai.png +0 -0
- package/src/tools/index.ts +13 -13
- package/src/tools/open-connector-mastra.ts +270 -270
- package/src/tools/open-connector-mcp.ts +170 -170
- package/src/tools/qwksearch-api-tools.ts +326 -326
- package/src/tools/search/doc-utils.ts +139 -139
- package/src/tools/search/document.ts +47 -47
- package/src/tools/search/index.ts +14 -14
- package/src/tools/search/link-summarizer.ts +112 -112
- package/src/tools/search/meta-search-types.ts +62 -62
- package/src/tools/search/metaSearchAgent.ts +465 -465
- package/src/tools/search/search-handlers.ts +97 -97
- package/src/tools/search/suggestionGeneratorAgent.ts +56 -56
- package/src/types.d.ts +137 -137
- package/src/utils/index.ts +2 -2
- package/src/utils/markdown-to-html.ts +53 -53
- package/src/utils/outputParser.ts +73 -73
package/src/memory/example.ts
CHANGED
|
@@ -1,343 +1,343 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Memory Module Usage Examples
|
|
3
|
-
*
|
|
4
|
-
* This file demonstrates how to use the new modular memory system
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
DrizzleMemoryStorage,
|
|
9
|
-
SimpleMemory,
|
|
10
|
-
MemoryAgent,
|
|
11
|
-
MEMORY_TYPES,
|
|
12
|
-
type IMemoryStorage,
|
|
13
|
-
type MemoryRecord,
|
|
14
|
-
} from "./index";
|
|
15
|
-
|
|
16
|
-
// ============================================================================
|
|
17
|
-
// Example 1: Basic Memory Usage with Drizzle
|
|
18
|
-
// ============================================================================
|
|
19
|
-
|
|
20
|
-
async function example1_basicMemory(db: any, userMemoriesTable: any) {
|
|
21
|
-
console.log("Example 1: Basic Memory Usage");
|
|
22
|
-
|
|
23
|
-
// Create storage adapter
|
|
24
|
-
const storage = new DrizzleMemoryStorage(db, userMemoriesTable);
|
|
25
|
-
|
|
26
|
-
// Create memory instance
|
|
27
|
-
const memory = new SimpleMemory("user-123", storage, {
|
|
28
|
-
maxMemories: 100,
|
|
29
|
-
enableVectorSearch: true,
|
|
30
|
-
enableAutoSummarization: true,
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// Store some facts
|
|
34
|
-
await memory.storeFact("User prefers dark mode", 8, MEMORY_TYPES.PREFERENCE);
|
|
35
|
-
await memory.storeFact("User is a software engineer", 9, MEMORY_TYPES.PERSONAL);
|
|
36
|
-
await memory.storeFact("User works at Acme Corp", 7, MEMORY_TYPES.WORK);
|
|
37
|
-
|
|
38
|
-
// Recall relevant memories
|
|
39
|
-
const memories = await memory.recallRelevantMemories("work", 10);
|
|
40
|
-
console.log("Relevant memories:", memories);
|
|
41
|
-
|
|
42
|
-
// Add messages to conversation
|
|
43
|
-
memory.addMessage("user", "I need help with React");
|
|
44
|
-
memory.addMessage("assistant", "I can help you with React!");
|
|
45
|
-
|
|
46
|
-
// Get context for next message
|
|
47
|
-
const context = await memory.getMemoryContext("React", true);
|
|
48
|
-
console.log("Memory context:", context);
|
|
49
|
-
|
|
50
|
-
// Get performance metrics
|
|
51
|
-
const metrics = memory.getMetrics();
|
|
52
|
-
console.log("Metrics:", metrics);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// ============================================================================
|
|
56
|
-
// Example 2: Using Memory Agent with LLM
|
|
57
|
-
// ============================================================================
|
|
58
|
-
|
|
59
|
-
async function example2_memoryAgent(db: any, userMemoriesTable: any) {
|
|
60
|
-
console.log("Example 2: Memory Agent with LLM");
|
|
61
|
-
|
|
62
|
-
const storage = new DrizzleMemoryStorage(db, userMemoriesTable);
|
|
63
|
-
|
|
64
|
-
const agent = new MemoryAgent("user-123", storage, {
|
|
65
|
-
defaultProvider: "groq",
|
|
66
|
-
defaultModel: "mixtral-8x7b-32768",
|
|
67
|
-
rateLimit: {
|
|
68
|
-
requests: 10,
|
|
69
|
-
windowMs: 60000, // 1 minute
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Chat with memory context
|
|
74
|
-
const response = await agent.chat("What do you remember about my preferences?", {
|
|
75
|
-
maxMemories: 5,
|
|
76
|
-
minImportance: 5,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
if (response.success) {
|
|
80
|
-
console.log("Response:", response.content);
|
|
81
|
-
console.log("Memory context used:", response.memoryContext);
|
|
82
|
-
console.log("Tokens used:", response.tokensUsed);
|
|
83
|
-
console.log("Response time:", response.responseTime);
|
|
84
|
-
} else {
|
|
85
|
-
console.error("Error:", response.error);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Manually remember something
|
|
89
|
-
await agent.remember("User likes coffee in the morning", 7, MEMORY_TYPES.PREFERENCE);
|
|
90
|
-
|
|
91
|
-
// Get all memories
|
|
92
|
-
const allMemories = await agent.getMemories("", 20);
|
|
93
|
-
console.log("All memories:", allMemories);
|
|
94
|
-
|
|
95
|
-
// Health check
|
|
96
|
-
const health = await agent.healthCheck();
|
|
97
|
-
console.log("Health:", health);
|
|
98
|
-
|
|
99
|
-
// Analytics
|
|
100
|
-
const analytics = agent.getAnalytics();
|
|
101
|
-
console.log("Analytics:", analytics);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// ============================================================================
|
|
105
|
-
// Example 3: Custom Storage Implementation
|
|
106
|
-
// ============================================================================
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Example custom storage implementation using in-memory Map
|
|
110
|
-
* In production, this could be Redis, MongoDB, etc.
|
|
111
|
-
*/
|
|
112
|
-
class InMemoryStorage implements IMemoryStorage {
|
|
113
|
-
private memories: Map<string, MemoryRecord> = new Map();
|
|
114
|
-
private idCounter = 0;
|
|
115
|
-
|
|
116
|
-
async insertMemory(
|
|
117
|
-
userId: string,
|
|
118
|
-
memoryType: any,
|
|
119
|
-
content: string,
|
|
120
|
-
importance: number,
|
|
121
|
-
metadata?: Record<string, any>,
|
|
122
|
-
): Promise<string> {
|
|
123
|
-
const id = `mem-${++this.idCounter}`;
|
|
124
|
-
const memory: MemoryRecord = {
|
|
125
|
-
id,
|
|
126
|
-
user_id: userId,
|
|
127
|
-
memory_type: memoryType,
|
|
128
|
-
content,
|
|
129
|
-
importance,
|
|
130
|
-
access_count: 0,
|
|
131
|
-
metadata: metadata || {},
|
|
132
|
-
created_at: new Date(),
|
|
133
|
-
updated_at: new Date(),
|
|
134
|
-
};
|
|
135
|
-
this.memories.set(id, memory);
|
|
136
|
-
return id;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async findMemories(
|
|
140
|
-
userId: string,
|
|
141
|
-
query?: string,
|
|
142
|
-
limit: number = 10,
|
|
143
|
-
options: any = {},
|
|
144
|
-
): Promise<MemoryRecord[]> {
|
|
145
|
-
let results = Array.from(this.memories.values()).filter(
|
|
146
|
-
(m) => m.user_id === userId,
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
// Filter by query
|
|
150
|
-
if (query) {
|
|
151
|
-
results = results.filter((m) =>
|
|
152
|
-
m.content.toLowerCase().includes(query.toLowerCase()),
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Filter by type
|
|
157
|
-
if (options.memoryType) {
|
|
158
|
-
results = results.filter((m) => m.memory_type === options.memoryType);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Sort by importance and access count
|
|
162
|
-
results.sort((a, b) => {
|
|
163
|
-
if (b.importance !== a.importance) {
|
|
164
|
-
return b.importance - a.importance;
|
|
165
|
-
}
|
|
166
|
-
return b.access_count - a.access_count;
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
return results.slice(0, limit);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async findSimilarMemories(
|
|
173
|
-
userId: string,
|
|
174
|
-
content: string,
|
|
175
|
-
limit: number = 5,
|
|
176
|
-
): Promise<MemoryRecord[]> {
|
|
177
|
-
const words = content.toLowerCase().split(/\s+/).filter((w) => w.length > 2);
|
|
178
|
-
const results = Array.from(this.memories.values())
|
|
179
|
-
.filter((m) => m.user_id === userId)
|
|
180
|
-
.filter((m) =>
|
|
181
|
-
words.some((word) => m.content.toLowerCase().includes(word)),
|
|
182
|
-
)
|
|
183
|
-
.sort((a, b) => b.importance - a.importance)
|
|
184
|
-
.slice(0, limit);
|
|
185
|
-
|
|
186
|
-
return results;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async updateMemory(id: string, updates: any): Promise<void> {
|
|
190
|
-
const memory = this.memories.get(id);
|
|
191
|
-
if (!memory) return;
|
|
192
|
-
|
|
193
|
-
if (updates.importance !== undefined) {
|
|
194
|
-
memory.importance = updates.importance;
|
|
195
|
-
}
|
|
196
|
-
if (updates.access_count !== undefined) {
|
|
197
|
-
if (typeof updates.access_count === "object") {
|
|
198
|
-
memory.access_count += updates.access_count.increment;
|
|
199
|
-
} else {
|
|
200
|
-
memory.access_count = updates.access_count;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
if (updates.updated_at !== undefined) {
|
|
204
|
-
memory.updated_at = updates.updated_at;
|
|
205
|
-
}
|
|
206
|
-
if (updates.metadata !== undefined) {
|
|
207
|
-
memory.metadata = updates.metadata;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
async deleteMemory(id: string): Promise<void> {
|
|
212
|
-
this.memories.delete(id);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
async getMemoryById(id: string): Promise<MemoryRecord | null> {
|
|
216
|
-
return this.memories.get(id) || null;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
async batchUpdateMemories(
|
|
220
|
-
updates: Array<{ id: string; updates: any }>,
|
|
221
|
-
): Promise<void> {
|
|
222
|
-
for (const { id, updates: updateData } of updates) {
|
|
223
|
-
await this.updateMemory(id, updateData);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async function example3_customStorage() {
|
|
229
|
-
console.log("Example 3: Custom Storage Implementation");
|
|
230
|
-
|
|
231
|
-
// Use in-memory storage instead of database
|
|
232
|
-
const storage = new InMemoryStorage();
|
|
233
|
-
|
|
234
|
-
const memory = new SimpleMemory("user-123", storage);
|
|
235
|
-
|
|
236
|
-
// Works exactly the same as with Drizzle!
|
|
237
|
-
await memory.storeFact("This is stored in memory", 5, MEMORY_TYPES.FACT);
|
|
238
|
-
|
|
239
|
-
const memories = await memory.recallRelevantMemories("memory");
|
|
240
|
-
console.log("Memories from custom storage:", memories);
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// ============================================================================
|
|
244
|
-
// Example 4: Testing with Mock Storage
|
|
245
|
-
// ============================================================================
|
|
246
|
-
|
|
247
|
-
class MockStorage implements IMemoryStorage {
|
|
248
|
-
public insertCalls: any[] = [];
|
|
249
|
-
public findCalls: any[] = [];
|
|
250
|
-
|
|
251
|
-
async insertMemory(...args: any[]): Promise<string> {
|
|
252
|
-
this.insertCalls.push(args);
|
|
253
|
-
return "mock-id-" + this.insertCalls.length;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
async findMemories(...args: any[]): Promise<MemoryRecord[]> {
|
|
257
|
-
this.findCalls.push(args);
|
|
258
|
-
return [
|
|
259
|
-
{
|
|
260
|
-
id: "mock-1",
|
|
261
|
-
user_id: args[0],
|
|
262
|
-
memory_type: MEMORY_TYPES.FACT,
|
|
263
|
-
content: "Mock memory",
|
|
264
|
-
importance: 5,
|
|
265
|
-
access_count: 1,
|
|
266
|
-
metadata: {},
|
|
267
|
-
created_at: new Date(),
|
|
268
|
-
updated_at: new Date(),
|
|
269
|
-
},
|
|
270
|
-
];
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
async findSimilarMemories(): Promise<MemoryRecord[]> {
|
|
274
|
-
return [];
|
|
275
|
-
}
|
|
276
|
-
async updateMemory(): Promise<void> {}
|
|
277
|
-
async deleteMemory(): Promise<void> {}
|
|
278
|
-
async getMemoryById(): Promise<MemoryRecord | null> {
|
|
279
|
-
return null;
|
|
280
|
-
}
|
|
281
|
-
async batchUpdateMemories(): Promise<void> {}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
async function example4_testing() {
|
|
285
|
-
console.log("Example 4: Testing with Mock Storage");
|
|
286
|
-
|
|
287
|
-
const mockStorage = new MockStorage();
|
|
288
|
-
const memory = new SimpleMemory("test-user", mockStorage);
|
|
289
|
-
|
|
290
|
-
// Store a fact
|
|
291
|
-
await memory.storeFact("Test fact", 5, MEMORY_TYPES.FACT);
|
|
292
|
-
|
|
293
|
-
// Verify the mock was called
|
|
294
|
-
console.log("Insert calls:", mockStorage.insertCalls.length);
|
|
295
|
-
console.log("First insert call:", mockStorage.insertCalls[0]);
|
|
296
|
-
|
|
297
|
-
// Recall memories
|
|
298
|
-
const memories = await memory.recallRelevantMemories("test");
|
|
299
|
-
|
|
300
|
-
// Verify find was called
|
|
301
|
-
console.log("Find calls:", mockStorage.findCalls.length);
|
|
302
|
-
console.log("Returned memories:", memories);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// ============================================================================
|
|
306
|
-
// Example 5: Advanced Usage - Batch Operations
|
|
307
|
-
// ============================================================================
|
|
308
|
-
|
|
309
|
-
async function example5_batchOperations(db: any, userMemoriesTable: any) {
|
|
310
|
-
console.log("Example 5: Batch Operations");
|
|
311
|
-
|
|
312
|
-
const storage = new DrizzleMemoryStorage(db, userMemoriesTable);
|
|
313
|
-
const memory = new SimpleMemory("user-123", storage, {
|
|
314
|
-
batchSize: 5, // Process 5 facts at a time
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
// Add multiple messages - will trigger auto-summarization
|
|
318
|
-
for (let i = 0; i < 15; i++) {
|
|
319
|
-
memory.addMessage("user", `Message ${i}`, { timestamp: Date.now() });
|
|
320
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
// Force summarization
|
|
324
|
-
const success = await memory.summarizeAndStore();
|
|
325
|
-
console.log("Summarization success:", success);
|
|
326
|
-
|
|
327
|
-
// Clear cache
|
|
328
|
-
memory.clearCache();
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// ============================================================================
|
|
332
|
-
// Export examples
|
|
333
|
-
// ============================================================================
|
|
334
|
-
|
|
335
|
-
export {
|
|
336
|
-
example1_basicMemory,
|
|
337
|
-
example2_memoryAgent,
|
|
338
|
-
example3_customStorage,
|
|
339
|
-
example4_testing,
|
|
340
|
-
example5_batchOperations,
|
|
341
|
-
InMemoryStorage,
|
|
342
|
-
MockStorage,
|
|
343
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Memory Module Usage Examples
|
|
3
|
+
*
|
|
4
|
+
* This file demonstrates how to use the new modular memory system
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
DrizzleMemoryStorage,
|
|
9
|
+
SimpleMemory,
|
|
10
|
+
MemoryAgent,
|
|
11
|
+
MEMORY_TYPES,
|
|
12
|
+
type IMemoryStorage,
|
|
13
|
+
type MemoryRecord,
|
|
14
|
+
} from "./index";
|
|
15
|
+
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// Example 1: Basic Memory Usage with Drizzle
|
|
18
|
+
// ============================================================================
|
|
19
|
+
|
|
20
|
+
async function example1_basicMemory(db: any, userMemoriesTable: any) {
|
|
21
|
+
console.log("Example 1: Basic Memory Usage");
|
|
22
|
+
|
|
23
|
+
// Create storage adapter
|
|
24
|
+
const storage = new DrizzleMemoryStorage(db, userMemoriesTable);
|
|
25
|
+
|
|
26
|
+
// Create memory instance
|
|
27
|
+
const memory = new SimpleMemory("user-123", storage, {
|
|
28
|
+
maxMemories: 100,
|
|
29
|
+
enableVectorSearch: true,
|
|
30
|
+
enableAutoSummarization: true,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Store some facts
|
|
34
|
+
await memory.storeFact("User prefers dark mode", 8, MEMORY_TYPES.PREFERENCE);
|
|
35
|
+
await memory.storeFact("User is a software engineer", 9, MEMORY_TYPES.PERSONAL);
|
|
36
|
+
await memory.storeFact("User works at Acme Corp", 7, MEMORY_TYPES.WORK);
|
|
37
|
+
|
|
38
|
+
// Recall relevant memories
|
|
39
|
+
const memories = await memory.recallRelevantMemories("work", 10);
|
|
40
|
+
console.log("Relevant memories:", memories);
|
|
41
|
+
|
|
42
|
+
// Add messages to conversation
|
|
43
|
+
memory.addMessage("user", "I need help with React");
|
|
44
|
+
memory.addMessage("assistant", "I can help you with React!");
|
|
45
|
+
|
|
46
|
+
// Get context for next message
|
|
47
|
+
const context = await memory.getMemoryContext("React", true);
|
|
48
|
+
console.log("Memory context:", context);
|
|
49
|
+
|
|
50
|
+
// Get performance metrics
|
|
51
|
+
const metrics = memory.getMetrics();
|
|
52
|
+
console.log("Metrics:", metrics);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ============================================================================
|
|
56
|
+
// Example 2: Using Memory Agent with LLM
|
|
57
|
+
// ============================================================================
|
|
58
|
+
|
|
59
|
+
async function example2_memoryAgent(db: any, userMemoriesTable: any) {
|
|
60
|
+
console.log("Example 2: Memory Agent with LLM");
|
|
61
|
+
|
|
62
|
+
const storage = new DrizzleMemoryStorage(db, userMemoriesTable);
|
|
63
|
+
|
|
64
|
+
const agent = new MemoryAgent("user-123", storage, {
|
|
65
|
+
defaultProvider: "groq",
|
|
66
|
+
defaultModel: "mixtral-8x7b-32768",
|
|
67
|
+
rateLimit: {
|
|
68
|
+
requests: 10,
|
|
69
|
+
windowMs: 60000, // 1 minute
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Chat with memory context
|
|
74
|
+
const response = await agent.chat("What do you remember about my preferences?", {
|
|
75
|
+
maxMemories: 5,
|
|
76
|
+
minImportance: 5,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
if (response.success) {
|
|
80
|
+
console.log("Response:", response.content);
|
|
81
|
+
console.log("Memory context used:", response.memoryContext);
|
|
82
|
+
console.log("Tokens used:", response.tokensUsed);
|
|
83
|
+
console.log("Response time:", response.responseTime);
|
|
84
|
+
} else {
|
|
85
|
+
console.error("Error:", response.error);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Manually remember something
|
|
89
|
+
await agent.remember("User likes coffee in the morning", 7, MEMORY_TYPES.PREFERENCE);
|
|
90
|
+
|
|
91
|
+
// Get all memories
|
|
92
|
+
const allMemories = await agent.getMemories("", 20);
|
|
93
|
+
console.log("All memories:", allMemories);
|
|
94
|
+
|
|
95
|
+
// Health check
|
|
96
|
+
const health = await agent.healthCheck();
|
|
97
|
+
console.log("Health:", health);
|
|
98
|
+
|
|
99
|
+
// Analytics
|
|
100
|
+
const analytics = agent.getAnalytics();
|
|
101
|
+
console.log("Analytics:", analytics);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ============================================================================
|
|
105
|
+
// Example 3: Custom Storage Implementation
|
|
106
|
+
// ============================================================================
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Example custom storage implementation using in-memory Map
|
|
110
|
+
* In production, this could be Redis, MongoDB, etc.
|
|
111
|
+
*/
|
|
112
|
+
class InMemoryStorage implements IMemoryStorage {
|
|
113
|
+
private memories: Map<string, MemoryRecord> = new Map();
|
|
114
|
+
private idCounter = 0;
|
|
115
|
+
|
|
116
|
+
async insertMemory(
|
|
117
|
+
userId: string,
|
|
118
|
+
memoryType: any,
|
|
119
|
+
content: string,
|
|
120
|
+
importance: number,
|
|
121
|
+
metadata?: Record<string, any>,
|
|
122
|
+
): Promise<string> {
|
|
123
|
+
const id = `mem-${++this.idCounter}`;
|
|
124
|
+
const memory: MemoryRecord = {
|
|
125
|
+
id,
|
|
126
|
+
user_id: userId,
|
|
127
|
+
memory_type: memoryType,
|
|
128
|
+
content,
|
|
129
|
+
importance,
|
|
130
|
+
access_count: 0,
|
|
131
|
+
metadata: metadata || {},
|
|
132
|
+
created_at: new Date(),
|
|
133
|
+
updated_at: new Date(),
|
|
134
|
+
};
|
|
135
|
+
this.memories.set(id, memory);
|
|
136
|
+
return id;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async findMemories(
|
|
140
|
+
userId: string,
|
|
141
|
+
query?: string,
|
|
142
|
+
limit: number = 10,
|
|
143
|
+
options: any = {},
|
|
144
|
+
): Promise<MemoryRecord[]> {
|
|
145
|
+
let results = Array.from(this.memories.values()).filter(
|
|
146
|
+
(m) => m.user_id === userId,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
// Filter by query
|
|
150
|
+
if (query) {
|
|
151
|
+
results = results.filter((m) =>
|
|
152
|
+
m.content.toLowerCase().includes(query.toLowerCase()),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Filter by type
|
|
157
|
+
if (options.memoryType) {
|
|
158
|
+
results = results.filter((m) => m.memory_type === options.memoryType);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Sort by importance and access count
|
|
162
|
+
results.sort((a, b) => {
|
|
163
|
+
if (b.importance !== a.importance) {
|
|
164
|
+
return b.importance - a.importance;
|
|
165
|
+
}
|
|
166
|
+
return b.access_count - a.access_count;
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
return results.slice(0, limit);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async findSimilarMemories(
|
|
173
|
+
userId: string,
|
|
174
|
+
content: string,
|
|
175
|
+
limit: number = 5,
|
|
176
|
+
): Promise<MemoryRecord[]> {
|
|
177
|
+
const words = content.toLowerCase().split(/\s+/).filter((w) => w.length > 2);
|
|
178
|
+
const results = Array.from(this.memories.values())
|
|
179
|
+
.filter((m) => m.user_id === userId)
|
|
180
|
+
.filter((m) =>
|
|
181
|
+
words.some((word) => m.content.toLowerCase().includes(word)),
|
|
182
|
+
)
|
|
183
|
+
.sort((a, b) => b.importance - a.importance)
|
|
184
|
+
.slice(0, limit);
|
|
185
|
+
|
|
186
|
+
return results;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async updateMemory(id: string, updates: any): Promise<void> {
|
|
190
|
+
const memory = this.memories.get(id);
|
|
191
|
+
if (!memory) return;
|
|
192
|
+
|
|
193
|
+
if (updates.importance !== undefined) {
|
|
194
|
+
memory.importance = updates.importance;
|
|
195
|
+
}
|
|
196
|
+
if (updates.access_count !== undefined) {
|
|
197
|
+
if (typeof updates.access_count === "object") {
|
|
198
|
+
memory.access_count += updates.access_count.increment;
|
|
199
|
+
} else {
|
|
200
|
+
memory.access_count = updates.access_count;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (updates.updated_at !== undefined) {
|
|
204
|
+
memory.updated_at = updates.updated_at;
|
|
205
|
+
}
|
|
206
|
+
if (updates.metadata !== undefined) {
|
|
207
|
+
memory.metadata = updates.metadata;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async deleteMemory(id: string): Promise<void> {
|
|
212
|
+
this.memories.delete(id);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async getMemoryById(id: string): Promise<MemoryRecord | null> {
|
|
216
|
+
return this.memories.get(id) || null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async batchUpdateMemories(
|
|
220
|
+
updates: Array<{ id: string; updates: any }>,
|
|
221
|
+
): Promise<void> {
|
|
222
|
+
for (const { id, updates: updateData } of updates) {
|
|
223
|
+
await this.updateMemory(id, updateData);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async function example3_customStorage() {
|
|
229
|
+
console.log("Example 3: Custom Storage Implementation");
|
|
230
|
+
|
|
231
|
+
// Use in-memory storage instead of database
|
|
232
|
+
const storage = new InMemoryStorage();
|
|
233
|
+
|
|
234
|
+
const memory = new SimpleMemory("user-123", storage);
|
|
235
|
+
|
|
236
|
+
// Works exactly the same as with Drizzle!
|
|
237
|
+
await memory.storeFact("This is stored in memory", 5, MEMORY_TYPES.FACT);
|
|
238
|
+
|
|
239
|
+
const memories = await memory.recallRelevantMemories("memory");
|
|
240
|
+
console.log("Memories from custom storage:", memories);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ============================================================================
|
|
244
|
+
// Example 4: Testing with Mock Storage
|
|
245
|
+
// ============================================================================
|
|
246
|
+
|
|
247
|
+
class MockStorage implements IMemoryStorage {
|
|
248
|
+
public insertCalls: any[] = [];
|
|
249
|
+
public findCalls: any[] = [];
|
|
250
|
+
|
|
251
|
+
async insertMemory(...args: any[]): Promise<string> {
|
|
252
|
+
this.insertCalls.push(args);
|
|
253
|
+
return "mock-id-" + this.insertCalls.length;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async findMemories(...args: any[]): Promise<MemoryRecord[]> {
|
|
257
|
+
this.findCalls.push(args);
|
|
258
|
+
return [
|
|
259
|
+
{
|
|
260
|
+
id: "mock-1",
|
|
261
|
+
user_id: args[0],
|
|
262
|
+
memory_type: MEMORY_TYPES.FACT,
|
|
263
|
+
content: "Mock memory",
|
|
264
|
+
importance: 5,
|
|
265
|
+
access_count: 1,
|
|
266
|
+
metadata: {},
|
|
267
|
+
created_at: new Date(),
|
|
268
|
+
updated_at: new Date(),
|
|
269
|
+
},
|
|
270
|
+
];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async findSimilarMemories(): Promise<MemoryRecord[]> {
|
|
274
|
+
return [];
|
|
275
|
+
}
|
|
276
|
+
async updateMemory(): Promise<void> {}
|
|
277
|
+
async deleteMemory(): Promise<void> {}
|
|
278
|
+
async getMemoryById(): Promise<MemoryRecord | null> {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
async batchUpdateMemories(): Promise<void> {}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function example4_testing() {
|
|
285
|
+
console.log("Example 4: Testing with Mock Storage");
|
|
286
|
+
|
|
287
|
+
const mockStorage = new MockStorage();
|
|
288
|
+
const memory = new SimpleMemory("test-user", mockStorage);
|
|
289
|
+
|
|
290
|
+
// Store a fact
|
|
291
|
+
await memory.storeFact("Test fact", 5, MEMORY_TYPES.FACT);
|
|
292
|
+
|
|
293
|
+
// Verify the mock was called
|
|
294
|
+
console.log("Insert calls:", mockStorage.insertCalls.length);
|
|
295
|
+
console.log("First insert call:", mockStorage.insertCalls[0]);
|
|
296
|
+
|
|
297
|
+
// Recall memories
|
|
298
|
+
const memories = await memory.recallRelevantMemories("test");
|
|
299
|
+
|
|
300
|
+
// Verify find was called
|
|
301
|
+
console.log("Find calls:", mockStorage.findCalls.length);
|
|
302
|
+
console.log("Returned memories:", memories);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// ============================================================================
|
|
306
|
+
// Example 5: Advanced Usage - Batch Operations
|
|
307
|
+
// ============================================================================
|
|
308
|
+
|
|
309
|
+
async function example5_batchOperations(db: any, userMemoriesTable: any) {
|
|
310
|
+
console.log("Example 5: Batch Operations");
|
|
311
|
+
|
|
312
|
+
const storage = new DrizzleMemoryStorage(db, userMemoriesTable);
|
|
313
|
+
const memory = new SimpleMemory("user-123", storage, {
|
|
314
|
+
batchSize: 5, // Process 5 facts at a time
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// Add multiple messages - will trigger auto-summarization
|
|
318
|
+
for (let i = 0; i < 15; i++) {
|
|
319
|
+
memory.addMessage("user", `Message ${i}`, { timestamp: Date.now() });
|
|
320
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Force summarization
|
|
324
|
+
const success = await memory.summarizeAndStore();
|
|
325
|
+
console.log("Summarization success:", success);
|
|
326
|
+
|
|
327
|
+
// Clear cache
|
|
328
|
+
memory.clearCache();
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// ============================================================================
|
|
332
|
+
// Export examples
|
|
333
|
+
// ============================================================================
|
|
334
|
+
|
|
335
|
+
export {
|
|
336
|
+
example1_basicMemory,
|
|
337
|
+
example2_memoryAgent,
|
|
338
|
+
example3_customStorage,
|
|
339
|
+
example4_testing,
|
|
340
|
+
example5_batchOperations,
|
|
341
|
+
InMemoryStorage,
|
|
342
|
+
MockStorage,
|
|
343
|
+
};
|