@sparkleideas/memory 3.0.0-alpha.7
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/README.md +249 -0
- package/benchmarks/cache-hit-rate.bench.ts +535 -0
- package/benchmarks/hnsw-indexing.bench.ts +552 -0
- package/benchmarks/memory-write.bench.ts +469 -0
- package/benchmarks/vector-search.bench.ts +449 -0
- package/docs/AGENTDB-INTEGRATION.md +388 -0
- package/docs/CROSS_PLATFORM.md +505 -0
- package/docs/WINDOWS_SUPPORT.md +422 -0
- package/examples/agentdb-example.ts +345 -0
- package/examples/cross-platform-usage.ts +326 -0
- package/framework/benchmark.ts +112 -0
- package/package.json +42 -0
- package/src/agentdb-adapter.ts +1037 -0
- package/src/agentdb-backend.test.ts +339 -0
- package/src/agentdb-backend.ts +1016 -0
- package/src/agents/architect.yaml +11 -0
- package/src/agents/coder.yaml +11 -0
- package/src/agents/reviewer.yaml +10 -0
- package/src/agents/security-architect.yaml +10 -0
- package/src/agents/tester.yaml +10 -0
- package/src/application/commands/delete-memory.command.ts +172 -0
- package/src/application/commands/store-memory.command.ts +103 -0
- package/src/application/index.ts +36 -0
- package/src/application/queries/search-memory.query.ts +237 -0
- package/src/application/services/memory-application-service.ts +236 -0
- package/src/cache-manager.ts +516 -0
- package/src/database-provider.test.ts +364 -0
- package/src/database-provider.ts +511 -0
- package/src/domain/entities/memory-entry.ts +289 -0
- package/src/domain/index.ts +35 -0
- package/src/domain/repositories/memory-repository.interface.ts +120 -0
- package/src/domain/services/memory-domain-service.ts +403 -0
- package/src/hnsw-index.ts +1013 -0
- package/src/hybrid-backend.test.ts +399 -0
- package/src/hybrid-backend.ts +694 -0
- package/src/index.ts +515 -0
- package/src/infrastructure/index.ts +23 -0
- package/src/infrastructure/repositories/hybrid-memory-repository.ts +516 -0
- package/src/migration.ts +669 -0
- package/src/query-builder.ts +542 -0
- package/src/sqlite-backend.ts +732 -0
- package/src/sqljs-backend.ts +763 -0
- package/src/tmp.json +0 -0
- package/src/types.ts +727 -0
- package/tmp.json +0 -0
- package/tsconfig.json +11 -0
- package/verify-cross-platform.ts +170 -0
package/README.md
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# @claude-flow/memory
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@claude-flow/memory)
|
|
4
|
+
[](https://www.npmjs.com/package/@claude-flow/memory)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
[](https://github.com/ruvnet/claude-flow)
|
|
8
|
+
|
|
9
|
+
> High-performance memory module for Claude Flow V3 - AgentDB unification, HNSW indexing, vector search, and hybrid SQLite+AgentDB backend (ADR-009).
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **150x-12,500x Faster Search** - HNSW (Hierarchical Navigable Small World) vector index for ultra-fast similarity search
|
|
14
|
+
- **Hybrid Backend** - SQLite for structured data + AgentDB for vectors (ADR-009)
|
|
15
|
+
- **Vector Quantization** - Binary, scalar, and product quantization for 4-32x memory reduction
|
|
16
|
+
- **Multiple Distance Metrics** - Cosine, Euclidean, dot product, and Manhattan distance
|
|
17
|
+
- **Query Builder** - Fluent API for building complex memory queries
|
|
18
|
+
- **Cache Manager** - LRU caching with configurable size and TTL
|
|
19
|
+
- **Migration Tools** - Seamless migration from V2 memory systems
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @claude-flow/memory
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { HNSWIndex, AgentDBAdapter, CacheManager } from '@claude-flow/memory';
|
|
31
|
+
|
|
32
|
+
// Create HNSW index for vector search
|
|
33
|
+
const index = new HNSWIndex({
|
|
34
|
+
dimensions: 1536, // OpenAI embedding size
|
|
35
|
+
M: 16, // Max connections per node
|
|
36
|
+
efConstruction: 200,
|
|
37
|
+
metric: 'cosine'
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Add vectors
|
|
41
|
+
await index.addPoint('memory-1', new Float32Array(embedding));
|
|
42
|
+
await index.addPoint('memory-2', new Float32Array(embedding2));
|
|
43
|
+
|
|
44
|
+
// Search for similar vectors
|
|
45
|
+
const results = await index.search(queryVector, 10);
|
|
46
|
+
// [{ id: 'memory-1', distance: 0.05 }, { id: 'memory-2', distance: 0.12 }]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API Reference
|
|
50
|
+
|
|
51
|
+
### HNSW Index
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { HNSWIndex } from '@claude-flow/memory';
|
|
55
|
+
|
|
56
|
+
const index = new HNSWIndex({
|
|
57
|
+
dimensions: 1536,
|
|
58
|
+
M: 16, // Max connections per layer
|
|
59
|
+
efConstruction: 200, // Construction-time search depth
|
|
60
|
+
maxElements: 1000000, // Max vectors
|
|
61
|
+
metric: 'cosine', // 'cosine' | 'euclidean' | 'dot' | 'manhattan'
|
|
62
|
+
quantization: { // Optional quantization
|
|
63
|
+
type: 'scalar', // 'binary' | 'scalar' | 'product'
|
|
64
|
+
bits: 8
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Add vectors
|
|
69
|
+
await index.addPoint(id: string, vector: Float32Array);
|
|
70
|
+
|
|
71
|
+
// Search
|
|
72
|
+
const results = await index.search(
|
|
73
|
+
query: Float32Array,
|
|
74
|
+
k: number,
|
|
75
|
+
ef?: number // Search-time depth (higher = more accurate)
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// Search with filters
|
|
79
|
+
const filtered = await index.searchWithFilters(
|
|
80
|
+
query,
|
|
81
|
+
k,
|
|
82
|
+
(id) => id.startsWith('session-')
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
// Remove vectors
|
|
86
|
+
await index.removePoint(id);
|
|
87
|
+
|
|
88
|
+
// Get statistics
|
|
89
|
+
const stats = index.getStats();
|
|
90
|
+
// { vectorCount, memoryUsage, avgSearchTime, compressionRatio }
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### AgentDB Adapter
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { AgentDBAdapter } from '@claude-flow/memory';
|
|
97
|
+
|
|
98
|
+
const adapter = new AgentDBAdapter({
|
|
99
|
+
dimension: 1536,
|
|
100
|
+
indexType: 'hnsw',
|
|
101
|
+
metric: 'cosine',
|
|
102
|
+
hnswM: 16,
|
|
103
|
+
hnswEfConstruction: 200,
|
|
104
|
+
enableCache: true,
|
|
105
|
+
cacheSizeMb: 256
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await adapter.initialize();
|
|
109
|
+
|
|
110
|
+
// Store memory
|
|
111
|
+
await adapter.store({
|
|
112
|
+
id: 'mem-123',
|
|
113
|
+
content: 'User prefers dark mode',
|
|
114
|
+
embedding: vector,
|
|
115
|
+
metadata: { type: 'preference', agentId: 'agent-1' }
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// Semantic search
|
|
119
|
+
const memories = await adapter.search(queryVector, {
|
|
120
|
+
limit: 10,
|
|
121
|
+
threshold: 0.7,
|
|
122
|
+
filter: { type: 'preference' }
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Cross-agent memory sharing
|
|
126
|
+
await adapter.enableCrossAgentSharing({
|
|
127
|
+
shareTypes: ['patterns', 'preferences'],
|
|
128
|
+
excludeTypes: ['secrets']
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Cache Manager
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { CacheManager } from '@claude-flow/memory';
|
|
136
|
+
|
|
137
|
+
const cache = new CacheManager({
|
|
138
|
+
maxSize: 1000,
|
|
139
|
+
ttlMs: 3600000, // 1 hour
|
|
140
|
+
strategy: 'lru'
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Cache operations
|
|
144
|
+
cache.set('key', value);
|
|
145
|
+
const value = cache.get('key');
|
|
146
|
+
const exists = cache.has('key');
|
|
147
|
+
cache.delete('key');
|
|
148
|
+
cache.clear();
|
|
149
|
+
|
|
150
|
+
// Statistics
|
|
151
|
+
const stats = cache.getStats();
|
|
152
|
+
// { size, hits, misses, hitRate }
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Query Builder
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { QueryBuilder } from '@claude-flow/memory';
|
|
159
|
+
|
|
160
|
+
const results = await new QueryBuilder()
|
|
161
|
+
.semantic(queryVector)
|
|
162
|
+
.where('agentId', '=', 'agent-1')
|
|
163
|
+
.where('type', 'in', ['pattern', 'strategy'])
|
|
164
|
+
.where('createdAt', '>', Date.now() - 86400000)
|
|
165
|
+
.orderBy('relevance', 'desc')
|
|
166
|
+
.limit(20)
|
|
167
|
+
.execute();
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Migration
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { MemoryMigration } from '@claude-flow/memory';
|
|
174
|
+
|
|
175
|
+
const migration = new MemoryMigration({
|
|
176
|
+
source: './data/v2-memory.db',
|
|
177
|
+
destination: './data/v3-memory.db'
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Dry run
|
|
181
|
+
const preview = await migration.preview();
|
|
182
|
+
console.log(`Will migrate ${preview.recordCount} records`);
|
|
183
|
+
|
|
184
|
+
// Execute migration
|
|
185
|
+
await migration.execute({
|
|
186
|
+
batchSize: 1000,
|
|
187
|
+
onProgress: (progress) => console.log(`${progress.percent}%`)
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Performance Benchmarks
|
|
192
|
+
|
|
193
|
+
| Operation | V2 Performance | V3 Performance | Improvement |
|
|
194
|
+
|-----------|---------------|----------------|-------------|
|
|
195
|
+
| Vector Search | 150ms | <1ms | **150x** |
|
|
196
|
+
| Bulk Insert | 500ms | 5ms | **100x** |
|
|
197
|
+
| Memory Write | 50ms | <5ms | **10x** |
|
|
198
|
+
| Cache Hit | 5ms | <0.1ms | **50x** |
|
|
199
|
+
| Index Build | 10s | 800ms | **12.5x** |
|
|
200
|
+
|
|
201
|
+
## Quantization Options
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
// Binary quantization (32x compression)
|
|
205
|
+
const binaryIndex = new HNSWIndex({
|
|
206
|
+
dimensions: 1536,
|
|
207
|
+
quantization: { type: 'binary' }
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Scalar quantization (4x compression)
|
|
211
|
+
const scalarIndex = new HNSWIndex({
|
|
212
|
+
dimensions: 1536,
|
|
213
|
+
quantization: { type: 'scalar', bits: 8 }
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// Product quantization (8x compression)
|
|
217
|
+
const productIndex = new HNSWIndex({
|
|
218
|
+
dimensions: 1536,
|
|
219
|
+
quantization: { type: 'product', subquantizers: 8 }
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## TypeScript Types
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import type {
|
|
227
|
+
HNSWConfig,
|
|
228
|
+
HNSWStats,
|
|
229
|
+
SearchResult,
|
|
230
|
+
MemoryEntry,
|
|
231
|
+
QuantizationConfig,
|
|
232
|
+
DistanceMetric
|
|
233
|
+
} from '@claude-flow/memory';
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Dependencies
|
|
237
|
+
|
|
238
|
+
- `agentdb` - Vector database engine
|
|
239
|
+
- `better-sqlite3` - SQLite driver (native)
|
|
240
|
+
- `sql.js` - SQLite driver (WASM fallback)
|
|
241
|
+
|
|
242
|
+
## Related Packages
|
|
243
|
+
|
|
244
|
+
- [@claude-flow/neural](../neural) - Neural learning integration
|
|
245
|
+
- [@claude-flow/shared](../shared) - Shared types and utilities
|
|
246
|
+
|
|
247
|
+
## License
|
|
248
|
+
|
|
249
|
+
MIT
|