chat-agent-toolkit 1.2.33 → 1.2.35
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.map +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/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/index.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview AI Agent Toolkit
|
|
3
|
-
*
|
|
4
|
-
* Multi-provider AI agent toolkit for generating language responses, searching the web,
|
|
5
|
-
* extracting page content, and managing long-term memory across 10+ LLM providers.
|
|
6
|
-
*
|
|
7
|
-
* Built on Vercel AI SDK with prompt templates for research, summarization, citation
|
|
8
|
-
* answering, query resolution, and knowledge-graph extraction.
|
|
9
|
-
*
|
|
10
|
-
* @module ai-research-agent
|
|
11
|
-
* @author vtempest <grokthiscontact@gmail.com>
|
|
12
|
-
* @license AGPL-3.0
|
|
13
|
-
* @see {@link https://github.com/vtempest/ai-research-agent}
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
export * from "write-language";
|
|
17
|
-
export * from "./prompts";
|
|
18
|
-
export * from "./memory";
|
|
19
|
-
export * from "./tools";
|
|
20
|
-
// Both write-language and ./tools export AGENT_TOOLS; this package's own wins.
|
|
21
|
-
export { AGENT_TOOLS } from "./tools";
|
|
22
|
-
export * from "./utils";
|
|
23
|
-
export { configManager, ModelRegistry, getEnv, getModelProvidersUIConfigSection } from "./config";
|
|
24
|
-
export type { Config, ConfigModelProvider, MCPServerConfig, UIConfigSections, Model, ModelWithProvider } from "./config";
|
|
25
|
-
export { cropProvider, cropProviderAsBlob, cropProviderAsDataURL, getProviderImage, getProviderNames } from "./utils/provider-image-cropper";
|
|
26
|
-
export type { Provider } from "./utils/provider-image-cropper";
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview AI Agent Toolkit
|
|
3
|
+
*
|
|
4
|
+
* Multi-provider AI agent toolkit for generating language responses, searching the web,
|
|
5
|
+
* extracting page content, and managing long-term memory across 10+ LLM providers.
|
|
6
|
+
*
|
|
7
|
+
* Built on Vercel AI SDK with prompt templates for research, summarization, citation
|
|
8
|
+
* answering, query resolution, and knowledge-graph extraction.
|
|
9
|
+
*
|
|
10
|
+
* @module ai-research-agent
|
|
11
|
+
* @author vtempest <grokthiscontact@gmail.com>
|
|
12
|
+
* @license AGPL-3.0
|
|
13
|
+
* @see {@link https://github.com/vtempest/ai-research-agent}
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export * from "write-language";
|
|
17
|
+
export * from "./prompts";
|
|
18
|
+
export * from "./memory";
|
|
19
|
+
export * from "./tools";
|
|
20
|
+
// Both write-language and ./tools export AGENT_TOOLS; this package's own wins.
|
|
21
|
+
export { AGENT_TOOLS } from "./tools";
|
|
22
|
+
export * from "./utils";
|
|
23
|
+
export { configManager, ModelRegistry, getEnv, getModelProvidersUIConfigSection } from "./config";
|
|
24
|
+
export type { Config, ConfigModelProvider, MCPServerConfig, UIConfigSections, Model, ModelWithProvider } from "./config";
|
|
25
|
+
export { cropProvider, cropProviderAsBlob, cropProviderAsDataURL, getProviderImage, getProviderNames } from "./utils/provider-image-cropper";
|
|
26
|
+
export type { Provider } from "./utils/provider-image-cropper";
|
|
@@ -1,302 +1,302 @@
|
|
|
1
|
-
# Memory Module Architecture
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
|
|
5
|
-
The memory system has been refactored from a single monolithic file into a modular, well-abstracted architecture that separates concerns and eliminates direct dependencies on Drizzle ORM.
|
|
6
|
-
|
|
7
|
-
## Architecture Diagram
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
┌─────────────────────────────────────────────────────────────────┐
|
|
11
|
-
│ Application Layer │
|
|
12
|
-
│ (Your app using the memory module) │
|
|
13
|
-
└────────────────────────────┬────────────────────────────────────┘
|
|
14
|
-
│
|
|
15
|
-
│ imports
|
|
16
|
-
↓
|
|
17
|
-
┌─────────────────────────────────────────────────────────────────┐
|
|
18
|
-
│ index.ts │
|
|
19
|
-
│ (Public API exports) │
|
|
20
|
-
└────────────────────────────┬────────────────────────────────────┘
|
|
21
|
-
│
|
|
22
|
-
┌───────────────────┼───────────────────┐
|
|
23
|
-
│ │ │
|
|
24
|
-
↓ ↓ ↓
|
|
25
|
-
┌─────────────────┐ ┌─────────────────┐ ┌──────────────────┐
|
|
26
|
-
│ memory-agent.ts│ │ simple-memory.ts│ │ types.ts │
|
|
27
|
-
│ │ │ │ │ │
|
|
28
|
-
│ • Rate limiting │ │ • Message mgmt │ │ • MemoryRecord │
|
|
29
|
-
│ • LLM providers │ │ • Caching │ │ • MEMORY_CONFIG │
|
|
30
|
-
│ • Chat handling │ │ • Summarization │ │ • MEMORY_TYPES │
|
|
31
|
-
│ • Analytics │ │ • Batch ops │ │ • Interfaces │
|
|
32
|
-
└────────┬────────┘ └────────┬────────┘ └──────────────────┘
|
|
33
|
-
│ │
|
|
34
|
-
│ │ uses
|
|
35
|
-
│ ↓
|
|
36
|
-
│ ┌─────────────────────┐
|
|
37
|
-
│ │ storage-interface.ts│
|
|
38
|
-
│ │ │
|
|
39
|
-
│ │ IMemoryStorage │
|
|
40
|
-
│ │ (Abstract Interface)│
|
|
41
|
-
│ └──────────┬──────────┘
|
|
42
|
-
│ │
|
|
43
|
-
│ │ implements
|
|
44
|
-
│ ↓
|
|
45
|
-
│ ┌─────────────────────┐
|
|
46
|
-
└─────────→│ drizzle-storage.ts │
|
|
47
|
-
│ │
|
|
48
|
-
│ DrizzleMemoryStorage│
|
|
49
|
-
│ (Concrete Impl) │
|
|
50
|
-
└──────────┬──────────┘
|
|
51
|
-
│
|
|
52
|
-
│ uses
|
|
53
|
-
↓
|
|
54
|
-
┌─────────────────────┐
|
|
55
|
-
│ Drizzle ORM │
|
|
56
|
-
│ (External Dep) │
|
|
57
|
-
└─────────────────────┘
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Component Responsibilities
|
|
61
|
-
|
|
62
|
-
### 1. **types.ts**
|
|
63
|
-
- **Purpose**: Central type definitions and constants
|
|
64
|
-
- **Exports**:
|
|
65
|
-
- `MemoryRecord`, `Message`, `MemoryType`
|
|
66
|
-
- `MEMORY_CONFIG`, `MEMORY_TYPES`
|
|
67
|
-
- All TypeScript interfaces
|
|
68
|
-
- **Dependencies**: None
|
|
69
|
-
- **Used by**: All other modules
|
|
70
|
-
|
|
71
|
-
### 2. **storage-interface.ts**
|
|
72
|
-
- **Purpose**: Abstract interface for storage operations
|
|
73
|
-
- **Exports**: `IMemoryStorage` interface
|
|
74
|
-
- **Dependencies**: `types.ts`
|
|
75
|
-
- **Used by**: `simple-memory.ts`, `memory-agent.ts`
|
|
76
|
-
- **Key Methods**:
|
|
77
|
-
- `insertMemory()`
|
|
78
|
-
- `findMemories()`
|
|
79
|
-
- `findSimilarMemories()`
|
|
80
|
-
- `updateMemory()`
|
|
81
|
-
- `deleteMemory()`
|
|
82
|
-
- `batchUpdateMemories()`
|
|
83
|
-
|
|
84
|
-
### 3. **drizzle-storage.ts**
|
|
85
|
-
- **Purpose**: Drizzle ORM implementation of `IMemoryStorage`
|
|
86
|
-
- **Exports**: `DrizzleMemoryStorage` class
|
|
87
|
-
- **Dependencies**: `drizzle-orm`, `types.ts`, `storage-interface.ts`
|
|
88
|
-
- **Used by**: Application code (when using Drizzle)
|
|
89
|
-
- **Isolation**: This is the ONLY file that imports `drizzle-orm`
|
|
90
|
-
|
|
91
|
-
### 4. **simple-memory.ts**
|
|
92
|
-
- **Purpose**: Core memory management logic
|
|
93
|
-
- **Exports**: `SimpleMemory` class
|
|
94
|
-
- **Dependencies**: `storage-interface.ts`, `types.ts`, `writeLanguageResponse`
|
|
95
|
-
- **Features**:
|
|
96
|
-
- Message deduplication
|
|
97
|
-
- Auto-summarization
|
|
98
|
-
- Caching (in-memory)
|
|
99
|
-
- Batch processing
|
|
100
|
-
- Memory recall with filtering
|
|
101
|
-
|
|
102
|
-
### 5. **memory-agent.ts**
|
|
103
|
-
- **Purpose**: High-level agent with LLM integration
|
|
104
|
-
- **Exports**: `MemoryAgent` class
|
|
105
|
-
- **Dependencies**: `simple-memory.ts`, `storage-interface.ts`, `types.ts`
|
|
106
|
-
- **Features**:
|
|
107
|
-
- Rate limiting
|
|
108
|
-
- Multi-provider LLM support
|
|
109
|
-
- Chat management
|
|
110
|
-
- Session tracking
|
|
111
|
-
- Analytics
|
|
112
|
-
|
|
113
|
-
### 6. **index.ts**
|
|
114
|
-
- **Purpose**: Public API surface
|
|
115
|
-
- **Exports**: All public classes, interfaces, and types
|
|
116
|
-
- **Role**: Single entry point for consumers
|
|
117
|
-
|
|
118
|
-
### 7. **memory.ts** (Deprecated)
|
|
119
|
-
- **Purpose**: Backward compatibility
|
|
120
|
-
- **Exports**: Re-exports from `index.ts`
|
|
121
|
-
- **Status**: Deprecated, will be removed in future version
|
|
122
|
-
|
|
123
|
-
## Data Flow
|
|
124
|
-
|
|
125
|
-
### Storing a Memory
|
|
126
|
-
```
|
|
127
|
-
Application
|
|
128
|
-
↓ memory.storeFact("content", importance, category)
|
|
129
|
-
SimpleMemory
|
|
130
|
-
↓ storage.insertMemory(userId, type, content, importance)
|
|
131
|
-
IMemoryStorage (interface)
|
|
132
|
-
↓
|
|
133
|
-
DrizzleMemoryStorage
|
|
134
|
-
↓ db.insert().values()
|
|
135
|
-
Database
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Recalling Memories
|
|
139
|
-
```
|
|
140
|
-
Application
|
|
141
|
-
↓ memory.recallRelevantMemories(query, limit)
|
|
142
|
-
SimpleMemory
|
|
143
|
-
↓ Check cache
|
|
144
|
-
↓ storage.findMemories(userId, query, limit, options)
|
|
145
|
-
IMemoryStorage (interface)
|
|
146
|
-
↓
|
|
147
|
-
DrizzleMemoryStorage
|
|
148
|
-
↓ db.select().where().orderBy()
|
|
149
|
-
Database
|
|
150
|
-
↓
|
|
151
|
-
SimpleMemory (apply filters, cache results)
|
|
152
|
-
↓
|
|
153
|
-
Application
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## Benefits of New Architecture
|
|
157
|
-
|
|
158
|
-
### 1. **Separation of Concerns**
|
|
159
|
-
- Storage logic is isolated from business logic
|
|
160
|
-
- Each file has a single, clear responsibility
|
|
161
|
-
- Easy to understand and maintain
|
|
162
|
-
|
|
163
|
-
### 2. **Testability**
|
|
164
|
-
```typescript
|
|
165
|
-
// Mock storage for testing
|
|
166
|
-
class MockStorage implements IMemoryStorage {
|
|
167
|
-
private data = new Map();
|
|
168
|
-
|
|
169
|
-
async insertMemory(...) { /* mock */ }
|
|
170
|
-
async findMemories(...) { /* mock */ }
|
|
171
|
-
// ...
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const memory = new SimpleMemory("user", new MockStorage());
|
|
175
|
-
// Test without database!
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
### 3. **Flexibility**
|
|
179
|
-
```typescript
|
|
180
|
-
// Can swap implementations easily
|
|
181
|
-
const sqliteStorage = new SQLiteMemoryStorage(db);
|
|
182
|
-
const postgresStorage = new PostgresMemoryStorage(pool);
|
|
183
|
-
const redisStorage = new RedisMemoryStorage(client);
|
|
184
|
-
|
|
185
|
-
// Same interface, different backends
|
|
186
|
-
const memory = new SimpleMemory("user", sqliteStorage);
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
### 4. **No Vendor Lock-in**
|
|
190
|
-
- Core logic doesn't depend on Drizzle
|
|
191
|
-
- Can migrate to different ORMs
|
|
192
|
-
- Can use raw SQL if needed
|
|
193
|
-
|
|
194
|
-
### 5. **Better Dependency Management**
|
|
195
|
-
- Drizzle is only imported in one file
|
|
196
|
-
- Easier to upgrade or replace
|
|
197
|
-
- Smaller bundle size if using tree-shaking
|
|
198
|
-
|
|
199
|
-
## Migration from Old Structure
|
|
200
|
-
|
|
201
|
-
### Old (Monolithic)
|
|
202
|
-
```typescript
|
|
203
|
-
import { SimpleMemory } from "./agents/memory/memory";
|
|
204
|
-
|
|
205
|
-
const memory = new SimpleMemory(userId, db, options);
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
### New (Modular)
|
|
209
|
-
```typescript
|
|
210
|
-
import { SimpleMemory, DrizzleMemoryStorage } from "./agents/memory";
|
|
211
|
-
|
|
212
|
-
const storage = new DrizzleMemoryStorage(db);
|
|
213
|
-
const memory = new SimpleMemory(userId, storage, options);
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
### Key Changes
|
|
217
|
-
1. **Constructor change**: `db` → `storage` (IMemoryStorage)
|
|
218
|
-
2. **Explicit adapter**: Must create storage adapter first
|
|
219
|
-
3. **Better typing**: Full TypeScript support with interfaces
|
|
220
|
-
|
|
221
|
-
## Design Patterns Used
|
|
222
|
-
|
|
223
|
-
### 1. **Dependency Injection**
|
|
224
|
-
- `SimpleMemory` receives `IMemoryStorage` via constructor
|
|
225
|
-
- Enables loose coupling and testability
|
|
226
|
-
|
|
227
|
-
### 2. **Strategy Pattern**
|
|
228
|
-
- `IMemoryStorage` defines the storage strategy interface
|
|
229
|
-
- Different implementations can be swapped at runtime
|
|
230
|
-
|
|
231
|
-
### 3. **Facade Pattern**
|
|
232
|
-
- `MemoryAgent` provides simple interface to complex subsystems
|
|
233
|
-
- Hides complexity of memory management, LLM calls, rate limiting
|
|
234
|
-
|
|
235
|
-
### 4. **Repository Pattern**
|
|
236
|
-
- Storage layer acts as repository for memory entities
|
|
237
|
-
- Abstracts data access logic
|
|
238
|
-
|
|
239
|
-
## Future Extensions
|
|
240
|
-
|
|
241
|
-
### Adding New Storage Backend
|
|
242
|
-
```typescript
|
|
243
|
-
// 1. Implement IMemoryStorage
|
|
244
|
-
export class MongoMemoryStorage implements IMemoryStorage {
|
|
245
|
-
constructor(private collection: Collection) {}
|
|
246
|
-
|
|
247
|
-
async insertMemory(...) {
|
|
248
|
-
return await this.collection.insertOne({...});
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// ... implement other methods
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// 2. Use it
|
|
255
|
-
const storage = new MongoMemoryStorage(collection);
|
|
256
|
-
const memory = new SimpleMemory("user", storage);
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
### Adding New Features to SimpleMemory
|
|
260
|
-
- Modify only `simple-memory.ts`
|
|
261
|
-
- No need to touch storage layer
|
|
262
|
-
- Business logic stays separate
|
|
263
|
-
|
|
264
|
-
### Replacing Drizzle
|
|
265
|
-
- Create new storage adapter (e.g., `prisma-storage.ts`)
|
|
266
|
-
- Implement `IMemoryStorage` interface
|
|
267
|
-
- Swap at initialization time
|
|
268
|
-
- Core logic remains unchanged
|
|
269
|
-
|
|
270
|
-
## Performance Considerations
|
|
271
|
-
|
|
272
|
-
### Caching Layer
|
|
273
|
-
- In-memory cache in `SimpleMemory`
|
|
274
|
-
- Reduces database queries
|
|
275
|
-
- TTL-based expiration
|
|
276
|
-
|
|
277
|
-
### Batch Operations
|
|
278
|
-
- `batchUpdateMemories()` for bulk updates
|
|
279
|
-
- Reduces round trips to database
|
|
280
|
-
- Better performance for summarization
|
|
281
|
-
|
|
282
|
-
### Query Optimization
|
|
283
|
-
- Indexed queries in storage layer
|
|
284
|
-
- Limit results to prevent over-fetching
|
|
285
|
-
- Efficient filtering and sorting
|
|
286
|
-
|
|
287
|
-
## Security Considerations
|
|
288
|
-
|
|
289
|
-
### Rate Limiting
|
|
290
|
-
- Prevents abuse of memory storage
|
|
291
|
-
- Configurable limits per user
|
|
292
|
-
- Sliding window implementation
|
|
293
|
-
|
|
294
|
-
### Input Validation
|
|
295
|
-
- Validates all inputs in `SimpleMemory`
|
|
296
|
-
- Prevents injection attacks
|
|
297
|
-
- Sanitizes content before storage
|
|
298
|
-
|
|
299
|
-
### Access Control
|
|
300
|
-
- User ID-based isolation
|
|
301
|
-
- Each user only sees their memories
|
|
302
|
-
- No cross-user data leakage
|
|
1
|
+
# Memory Module Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The memory system has been refactored from a single monolithic file into a modular, well-abstracted architecture that separates concerns and eliminates direct dependencies on Drizzle ORM.
|
|
6
|
+
|
|
7
|
+
## Architecture Diagram
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
11
|
+
│ Application Layer │
|
|
12
|
+
│ (Your app using the memory module) │
|
|
13
|
+
└────────────────────────────┬────────────────────────────────────┘
|
|
14
|
+
│
|
|
15
|
+
│ imports
|
|
16
|
+
↓
|
|
17
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
18
|
+
│ index.ts │
|
|
19
|
+
│ (Public API exports) │
|
|
20
|
+
└────────────────────────────┬────────────────────────────────────┘
|
|
21
|
+
│
|
|
22
|
+
┌───────────────────┼───────────────────┐
|
|
23
|
+
│ │ │
|
|
24
|
+
↓ ↓ ↓
|
|
25
|
+
┌─────────────────┐ ┌─────────────────┐ ┌──────────────────┐
|
|
26
|
+
│ memory-agent.ts│ │ simple-memory.ts│ │ types.ts │
|
|
27
|
+
│ │ │ │ │ │
|
|
28
|
+
│ • Rate limiting │ │ • Message mgmt │ │ • MemoryRecord │
|
|
29
|
+
│ • LLM providers │ │ • Caching │ │ • MEMORY_CONFIG │
|
|
30
|
+
│ • Chat handling │ │ • Summarization │ │ • MEMORY_TYPES │
|
|
31
|
+
│ • Analytics │ │ • Batch ops │ │ • Interfaces │
|
|
32
|
+
└────────┬────────┘ └────────┬────────┘ └──────────────────┘
|
|
33
|
+
│ │
|
|
34
|
+
│ │ uses
|
|
35
|
+
│ ↓
|
|
36
|
+
│ ┌─────────────────────┐
|
|
37
|
+
│ │ storage-interface.ts│
|
|
38
|
+
│ │ │
|
|
39
|
+
│ │ IMemoryStorage │
|
|
40
|
+
│ │ (Abstract Interface)│
|
|
41
|
+
│ └──────────┬──────────┘
|
|
42
|
+
│ │
|
|
43
|
+
│ │ implements
|
|
44
|
+
│ ↓
|
|
45
|
+
│ ┌─────────────────────┐
|
|
46
|
+
└─────────→│ drizzle-storage.ts │
|
|
47
|
+
│ │
|
|
48
|
+
│ DrizzleMemoryStorage│
|
|
49
|
+
│ (Concrete Impl) │
|
|
50
|
+
└──────────┬──────────┘
|
|
51
|
+
│
|
|
52
|
+
│ uses
|
|
53
|
+
↓
|
|
54
|
+
┌─────────────────────┐
|
|
55
|
+
│ Drizzle ORM │
|
|
56
|
+
│ (External Dep) │
|
|
57
|
+
└─────────────────────┘
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Component Responsibilities
|
|
61
|
+
|
|
62
|
+
### 1. **types.ts**
|
|
63
|
+
- **Purpose**: Central type definitions and constants
|
|
64
|
+
- **Exports**:
|
|
65
|
+
- `MemoryRecord`, `Message`, `MemoryType`
|
|
66
|
+
- `MEMORY_CONFIG`, `MEMORY_TYPES`
|
|
67
|
+
- All TypeScript interfaces
|
|
68
|
+
- **Dependencies**: None
|
|
69
|
+
- **Used by**: All other modules
|
|
70
|
+
|
|
71
|
+
### 2. **storage-interface.ts**
|
|
72
|
+
- **Purpose**: Abstract interface for storage operations
|
|
73
|
+
- **Exports**: `IMemoryStorage` interface
|
|
74
|
+
- **Dependencies**: `types.ts`
|
|
75
|
+
- **Used by**: `simple-memory.ts`, `memory-agent.ts`
|
|
76
|
+
- **Key Methods**:
|
|
77
|
+
- `insertMemory()`
|
|
78
|
+
- `findMemories()`
|
|
79
|
+
- `findSimilarMemories()`
|
|
80
|
+
- `updateMemory()`
|
|
81
|
+
- `deleteMemory()`
|
|
82
|
+
- `batchUpdateMemories()`
|
|
83
|
+
|
|
84
|
+
### 3. **drizzle-storage.ts**
|
|
85
|
+
- **Purpose**: Drizzle ORM implementation of `IMemoryStorage`
|
|
86
|
+
- **Exports**: `DrizzleMemoryStorage` class
|
|
87
|
+
- **Dependencies**: `drizzle-orm`, `types.ts`, `storage-interface.ts`
|
|
88
|
+
- **Used by**: Application code (when using Drizzle)
|
|
89
|
+
- **Isolation**: This is the ONLY file that imports `drizzle-orm`
|
|
90
|
+
|
|
91
|
+
### 4. **simple-memory.ts**
|
|
92
|
+
- **Purpose**: Core memory management logic
|
|
93
|
+
- **Exports**: `SimpleMemory` class
|
|
94
|
+
- **Dependencies**: `storage-interface.ts`, `types.ts`, `writeLanguageResponse`
|
|
95
|
+
- **Features**:
|
|
96
|
+
- Message deduplication
|
|
97
|
+
- Auto-summarization
|
|
98
|
+
- Caching (in-memory)
|
|
99
|
+
- Batch processing
|
|
100
|
+
- Memory recall with filtering
|
|
101
|
+
|
|
102
|
+
### 5. **memory-agent.ts**
|
|
103
|
+
- **Purpose**: High-level agent with LLM integration
|
|
104
|
+
- **Exports**: `MemoryAgent` class
|
|
105
|
+
- **Dependencies**: `simple-memory.ts`, `storage-interface.ts`, `types.ts`
|
|
106
|
+
- **Features**:
|
|
107
|
+
- Rate limiting
|
|
108
|
+
- Multi-provider LLM support
|
|
109
|
+
- Chat management
|
|
110
|
+
- Session tracking
|
|
111
|
+
- Analytics
|
|
112
|
+
|
|
113
|
+
### 6. **index.ts**
|
|
114
|
+
- **Purpose**: Public API surface
|
|
115
|
+
- **Exports**: All public classes, interfaces, and types
|
|
116
|
+
- **Role**: Single entry point for consumers
|
|
117
|
+
|
|
118
|
+
### 7. **memory.ts** (Deprecated)
|
|
119
|
+
- **Purpose**: Backward compatibility
|
|
120
|
+
- **Exports**: Re-exports from `index.ts`
|
|
121
|
+
- **Status**: Deprecated, will be removed in future version
|
|
122
|
+
|
|
123
|
+
## Data Flow
|
|
124
|
+
|
|
125
|
+
### Storing a Memory
|
|
126
|
+
```
|
|
127
|
+
Application
|
|
128
|
+
↓ memory.storeFact("content", importance, category)
|
|
129
|
+
SimpleMemory
|
|
130
|
+
↓ storage.insertMemory(userId, type, content, importance)
|
|
131
|
+
IMemoryStorage (interface)
|
|
132
|
+
↓
|
|
133
|
+
DrizzleMemoryStorage
|
|
134
|
+
↓ db.insert().values()
|
|
135
|
+
Database
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Recalling Memories
|
|
139
|
+
```
|
|
140
|
+
Application
|
|
141
|
+
↓ memory.recallRelevantMemories(query, limit)
|
|
142
|
+
SimpleMemory
|
|
143
|
+
↓ Check cache
|
|
144
|
+
↓ storage.findMemories(userId, query, limit, options)
|
|
145
|
+
IMemoryStorage (interface)
|
|
146
|
+
↓
|
|
147
|
+
DrizzleMemoryStorage
|
|
148
|
+
↓ db.select().where().orderBy()
|
|
149
|
+
Database
|
|
150
|
+
↓
|
|
151
|
+
SimpleMemory (apply filters, cache results)
|
|
152
|
+
↓
|
|
153
|
+
Application
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Benefits of New Architecture
|
|
157
|
+
|
|
158
|
+
### 1. **Separation of Concerns**
|
|
159
|
+
- Storage logic is isolated from business logic
|
|
160
|
+
- Each file has a single, clear responsibility
|
|
161
|
+
- Easy to understand and maintain
|
|
162
|
+
|
|
163
|
+
### 2. **Testability**
|
|
164
|
+
```typescript
|
|
165
|
+
// Mock storage for testing
|
|
166
|
+
class MockStorage implements IMemoryStorage {
|
|
167
|
+
private data = new Map();
|
|
168
|
+
|
|
169
|
+
async insertMemory(...) { /* mock */ }
|
|
170
|
+
async findMemories(...) { /* mock */ }
|
|
171
|
+
// ...
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const memory = new SimpleMemory("user", new MockStorage());
|
|
175
|
+
// Test without database!
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 3. **Flexibility**
|
|
179
|
+
```typescript
|
|
180
|
+
// Can swap implementations easily
|
|
181
|
+
const sqliteStorage = new SQLiteMemoryStorage(db);
|
|
182
|
+
const postgresStorage = new PostgresMemoryStorage(pool);
|
|
183
|
+
const redisStorage = new RedisMemoryStorage(client);
|
|
184
|
+
|
|
185
|
+
// Same interface, different backends
|
|
186
|
+
const memory = new SimpleMemory("user", sqliteStorage);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 4. **No Vendor Lock-in**
|
|
190
|
+
- Core logic doesn't depend on Drizzle
|
|
191
|
+
- Can migrate to different ORMs
|
|
192
|
+
- Can use raw SQL if needed
|
|
193
|
+
|
|
194
|
+
### 5. **Better Dependency Management**
|
|
195
|
+
- Drizzle is only imported in one file
|
|
196
|
+
- Easier to upgrade or replace
|
|
197
|
+
- Smaller bundle size if using tree-shaking
|
|
198
|
+
|
|
199
|
+
## Migration from Old Structure
|
|
200
|
+
|
|
201
|
+
### Old (Monolithic)
|
|
202
|
+
```typescript
|
|
203
|
+
import { SimpleMemory } from "./agents/memory/memory";
|
|
204
|
+
|
|
205
|
+
const memory = new SimpleMemory(userId, db, options);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### New (Modular)
|
|
209
|
+
```typescript
|
|
210
|
+
import { SimpleMemory, DrizzleMemoryStorage } from "./agents/memory";
|
|
211
|
+
|
|
212
|
+
const storage = new DrizzleMemoryStorage(db);
|
|
213
|
+
const memory = new SimpleMemory(userId, storage, options);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Key Changes
|
|
217
|
+
1. **Constructor change**: `db` → `storage` (IMemoryStorage)
|
|
218
|
+
2. **Explicit adapter**: Must create storage adapter first
|
|
219
|
+
3. **Better typing**: Full TypeScript support with interfaces
|
|
220
|
+
|
|
221
|
+
## Design Patterns Used
|
|
222
|
+
|
|
223
|
+
### 1. **Dependency Injection**
|
|
224
|
+
- `SimpleMemory` receives `IMemoryStorage` via constructor
|
|
225
|
+
- Enables loose coupling and testability
|
|
226
|
+
|
|
227
|
+
### 2. **Strategy Pattern**
|
|
228
|
+
- `IMemoryStorage` defines the storage strategy interface
|
|
229
|
+
- Different implementations can be swapped at runtime
|
|
230
|
+
|
|
231
|
+
### 3. **Facade Pattern**
|
|
232
|
+
- `MemoryAgent` provides simple interface to complex subsystems
|
|
233
|
+
- Hides complexity of memory management, LLM calls, rate limiting
|
|
234
|
+
|
|
235
|
+
### 4. **Repository Pattern**
|
|
236
|
+
- Storage layer acts as repository for memory entities
|
|
237
|
+
- Abstracts data access logic
|
|
238
|
+
|
|
239
|
+
## Future Extensions
|
|
240
|
+
|
|
241
|
+
### Adding New Storage Backend
|
|
242
|
+
```typescript
|
|
243
|
+
// 1. Implement IMemoryStorage
|
|
244
|
+
export class MongoMemoryStorage implements IMemoryStorage {
|
|
245
|
+
constructor(private collection: Collection) {}
|
|
246
|
+
|
|
247
|
+
async insertMemory(...) {
|
|
248
|
+
return await this.collection.insertOne({...});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ... implement other methods
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// 2. Use it
|
|
255
|
+
const storage = new MongoMemoryStorage(collection);
|
|
256
|
+
const memory = new SimpleMemory("user", storage);
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Adding New Features to SimpleMemory
|
|
260
|
+
- Modify only `simple-memory.ts`
|
|
261
|
+
- No need to touch storage layer
|
|
262
|
+
- Business logic stays separate
|
|
263
|
+
|
|
264
|
+
### Replacing Drizzle
|
|
265
|
+
- Create new storage adapter (e.g., `prisma-storage.ts`)
|
|
266
|
+
- Implement `IMemoryStorage` interface
|
|
267
|
+
- Swap at initialization time
|
|
268
|
+
- Core logic remains unchanged
|
|
269
|
+
|
|
270
|
+
## Performance Considerations
|
|
271
|
+
|
|
272
|
+
### Caching Layer
|
|
273
|
+
- In-memory cache in `SimpleMemory`
|
|
274
|
+
- Reduces database queries
|
|
275
|
+
- TTL-based expiration
|
|
276
|
+
|
|
277
|
+
### Batch Operations
|
|
278
|
+
- `batchUpdateMemories()` for bulk updates
|
|
279
|
+
- Reduces round trips to database
|
|
280
|
+
- Better performance for summarization
|
|
281
|
+
|
|
282
|
+
### Query Optimization
|
|
283
|
+
- Indexed queries in storage layer
|
|
284
|
+
- Limit results to prevent over-fetching
|
|
285
|
+
- Efficient filtering and sorting
|
|
286
|
+
|
|
287
|
+
## Security Considerations
|
|
288
|
+
|
|
289
|
+
### Rate Limiting
|
|
290
|
+
- Prevents abuse of memory storage
|
|
291
|
+
- Configurable limits per user
|
|
292
|
+
- Sliding window implementation
|
|
293
|
+
|
|
294
|
+
### Input Validation
|
|
295
|
+
- Validates all inputs in `SimpleMemory`
|
|
296
|
+
- Prevents injection attacks
|
|
297
|
+
- Sanitizes content before storage
|
|
298
|
+
|
|
299
|
+
### Access Control
|
|
300
|
+
- User ID-based isolation
|
|
301
|
+
- Each user only sees their memories
|
|
302
|
+
- No cross-user data leakage
|