@claude-flow/memory 3.0.0-alpha.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/.agentic-flow/intelligence.json +16 -0
- package/README.md +249 -0
- package/__tests__/coverage/base.css +224 -0
- package/__tests__/coverage/block-navigation.js +87 -0
- package/__tests__/coverage/coverage-final.json +19 -0
- package/__tests__/coverage/favicon.png +0 -0
- package/__tests__/coverage/index.html +206 -0
- package/__tests__/coverage/lcov-report/base.css +224 -0
- package/__tests__/coverage/lcov-report/block-navigation.js +87 -0
- package/__tests__/coverage/lcov-report/favicon.png +0 -0
- package/__tests__/coverage/lcov-report/index.html +206 -0
- package/__tests__/coverage/lcov-report/prettify.css +1 -0
- package/__tests__/coverage/lcov-report/prettify.js +2 -0
- package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/lcov-report/sorter.js +210 -0
- package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +2737 -0
- package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +3130 -0
- package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +601 -0
- package/__tests__/coverage/lcov-report/src/application/commands/index.html +131 -0
- package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +394 -0
- package/__tests__/coverage/lcov-report/src/application/queries/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +796 -0
- package/__tests__/coverage/lcov-report/src/application/services/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +793 -0
- package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +1633 -0
- package/__tests__/coverage/lcov-report/src/database-provider.ts.html +1618 -0
- package/__tests__/coverage/lcov-report/src/domain/entities/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +952 -0
- package/__tests__/coverage/lcov-report/src/domain/services/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +1294 -0
- package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +3124 -0
- package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +2167 -0
- package/__tests__/coverage/lcov-report/src/index.html +266 -0
- package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
- package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +116 -0
- package/__tests__/coverage/lcov-report/src/migration.ts.html +2092 -0
- package/__tests__/coverage/lcov-report/src/query-builder.ts.html +1711 -0
- package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +2281 -0
- package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +2374 -0
- package/__tests__/coverage/lcov-report/src/types.ts.html +2266 -0
- package/__tests__/coverage/lcov.info +10238 -0
- package/__tests__/coverage/prettify.css +1 -0
- package/__tests__/coverage/prettify.js +2 -0
- package/__tests__/coverage/sort-arrow-sprite.png +0 -0
- package/__tests__/coverage/sorter.js +210 -0
- package/__tests__/coverage/src/agentdb-adapter.ts.html +2737 -0
- package/__tests__/coverage/src/agentdb-backend.ts.html +3130 -0
- package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +601 -0
- package/__tests__/coverage/src/application/commands/index.html +131 -0
- package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +394 -0
- package/__tests__/coverage/src/application/queries/index.html +116 -0
- package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +796 -0
- package/__tests__/coverage/src/application/services/index.html +116 -0
- package/__tests__/coverage/src/application/services/memory-application-service.ts.html +793 -0
- package/__tests__/coverage/src/cache-manager.ts.html +1633 -0
- package/__tests__/coverage/src/database-provider.ts.html +1618 -0
- package/__tests__/coverage/src/domain/entities/index.html +116 -0
- package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +952 -0
- package/__tests__/coverage/src/domain/services/index.html +116 -0
- package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +1294 -0
- package/__tests__/coverage/src/hnsw-index.ts.html +3124 -0
- package/__tests__/coverage/src/hybrid-backend.ts.html +2167 -0
- package/__tests__/coverage/src/index.html +266 -0
- package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
- package/__tests__/coverage/src/infrastructure/repositories/index.html +116 -0
- package/__tests__/coverage/src/migration.ts.html +2092 -0
- package/__tests__/coverage/src/query-builder.ts.html +1711 -0
- package/__tests__/coverage/src/sqlite-backend.ts.html +2281 -0
- package/__tests__/coverage/src/sqljs-backend.ts.html +2374 -0
- package/__tests__/coverage/src/types.ts.html +2266 -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/dist/agentdb-adapter.d.ts +146 -0
- package/dist/agentdb-adapter.d.ts.map +1 -0
- package/dist/agentdb-adapter.js +679 -0
- package/dist/agentdb-adapter.js.map +1 -0
- package/dist/agentdb-backend.d.ts +214 -0
- package/dist/agentdb-backend.d.ts.map +1 -0
- package/dist/agentdb-backend.js +827 -0
- package/dist/agentdb-backend.js.map +1 -0
- package/dist/agentdb-backend.test.d.ts +7 -0
- package/dist/agentdb-backend.test.d.ts.map +1 -0
- package/dist/agentdb-backend.test.js +258 -0
- package/dist/agentdb-backend.test.js.map +1 -0
- package/dist/application/commands/delete-memory.command.d.ts +65 -0
- package/dist/application/commands/delete-memory.command.d.ts.map +1 -0
- package/dist/application/commands/delete-memory.command.js +129 -0
- package/dist/application/commands/delete-memory.command.js.map +1 -0
- package/dist/application/commands/store-memory.command.d.ts +48 -0
- package/dist/application/commands/store-memory.command.d.ts.map +1 -0
- package/dist/application/commands/store-memory.command.js +72 -0
- package/dist/application/commands/store-memory.command.js.map +1 -0
- package/dist/application/index.d.ts +12 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +15 -0
- package/dist/application/index.js.map +1 -0
- package/dist/application/queries/search-memory.query.d.ts +72 -0
- package/dist/application/queries/search-memory.query.d.ts.map +1 -0
- package/dist/application/queries/search-memory.query.js +143 -0
- package/dist/application/queries/search-memory.query.js.map +1 -0
- package/dist/application/services/memory-application-service.d.ts +121 -0
- package/dist/application/services/memory-application-service.d.ts.map +1 -0
- package/dist/application/services/memory-application-service.js +190 -0
- package/dist/application/services/memory-application-service.js.map +1 -0
- package/dist/cache-manager.d.ts +134 -0
- package/dist/cache-manager.d.ts.map +1 -0
- package/dist/cache-manager.js +407 -0
- package/dist/cache-manager.js.map +1 -0
- package/dist/database-provider.d.ts +86 -0
- package/dist/database-provider.d.ts.map +1 -0
- package/dist/database-provider.js +385 -0
- package/dist/database-provider.js.map +1 -0
- package/dist/database-provider.test.d.ts +7 -0
- package/dist/database-provider.test.d.ts.map +1 -0
- package/dist/database-provider.test.js +285 -0
- package/dist/database-provider.test.js.map +1 -0
- package/dist/domain/entities/memory-entry.d.ts +143 -0
- package/dist/domain/entities/memory-entry.d.ts.map +1 -0
- package/dist/domain/entities/memory-entry.js +226 -0
- package/dist/domain/entities/memory-entry.js.map +1 -0
- package/dist/domain/index.d.ts +11 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +12 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
- package/dist/domain/repositories/memory-repository.interface.d.ts.map +1 -0
- package/dist/domain/repositories/memory-repository.interface.js +11 -0
- package/dist/domain/repositories/memory-repository.interface.js.map +1 -0
- package/dist/domain/services/memory-domain-service.d.ts +105 -0
- package/dist/domain/services/memory-domain-service.d.ts.map +1 -0
- package/dist/domain/services/memory-domain-service.js +297 -0
- package/dist/domain/services/memory-domain-service.js.map +1 -0
- package/dist/hnsw-index.d.ts +111 -0
- package/dist/hnsw-index.d.ts.map +1 -0
- package/dist/hnsw-index.js +781 -0
- package/dist/hnsw-index.js.map +1 -0
- package/dist/hybrid-backend.d.ts +217 -0
- package/dist/hybrid-backend.d.ts.map +1 -0
- package/dist/hybrid-backend.js +491 -0
- package/dist/hybrid-backend.js.map +1 -0
- package/dist/hybrid-backend.test.d.ts +8 -0
- package/dist/hybrid-backend.test.d.ts.map +1 -0
- package/dist/hybrid-backend.test.js +320 -0
- package/dist/hybrid-backend.test.js.map +1 -0
- package/dist/index.d.ts +188 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +345 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/index.d.ts +17 -0
- package/dist/infrastructure/index.d.ts.map +1 -0
- package/dist/infrastructure/index.js +16 -0
- package/dist/infrastructure/index.js.map +1 -0
- package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
- package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts.map +1 -0
- package/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
- package/dist/infrastructure/repositories/hybrid-memory-repository.js.map +1 -0
- package/dist/migration.d.ts +68 -0
- package/dist/migration.d.ts.map +1 -0
- package/dist/migration.js +513 -0
- package/dist/migration.js.map +1 -0
- package/dist/query-builder.d.ts +211 -0
- package/dist/query-builder.d.ts.map +1 -0
- package/dist/query-builder.js +438 -0
- package/dist/query-builder.js.map +1 -0
- package/dist/sqlite-backend.d.ts +121 -0
- package/dist/sqlite-backend.d.ts.map +1 -0
- package/dist/sqlite-backend.js +564 -0
- package/dist/sqlite-backend.js.map +1 -0
- package/dist/sqljs-backend.d.ts +128 -0
- package/dist/sqljs-backend.d.ts.map +1 -0
- package/dist/sqljs-backend.js +598 -0
- package/dist/sqljs-backend.js.map +1 -0
- package/dist/types.d.ts +481 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +58 -0
- package/dist/types.js.map +1 -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 +31 -0
- package/src/agentdb-adapter.ts +884 -0
- package/src/agentdb-backend.test.ts +339 -0
- package/src/agentdb-backend.ts +1016 -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/types.ts +727 -0
- package/tsconfig.json +9 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/verify-cross-platform.ts +170 -0
|
@@ -0,0 +1,884 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 AgentDB Adapter
|
|
3
|
+
*
|
|
4
|
+
* Unified memory backend implementation using AgentDB with HNSW indexing
|
|
5
|
+
* for 150x-12,500x faster vector search. Implements IMemoryBackend interface.
|
|
6
|
+
*
|
|
7
|
+
* @module v3/memory/agentdb-adapter
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { EventEmitter } from 'node:events';
|
|
11
|
+
import {
|
|
12
|
+
IMemoryBackend,
|
|
13
|
+
MemoryEntry,
|
|
14
|
+
MemoryEntryInput,
|
|
15
|
+
MemoryEntryUpdate,
|
|
16
|
+
MemoryQuery,
|
|
17
|
+
SearchOptions,
|
|
18
|
+
SearchResult,
|
|
19
|
+
BackendStats,
|
|
20
|
+
HealthCheckResult,
|
|
21
|
+
ComponentHealth,
|
|
22
|
+
MemoryType,
|
|
23
|
+
EmbeddingGenerator,
|
|
24
|
+
generateMemoryId,
|
|
25
|
+
createDefaultEntry,
|
|
26
|
+
CacheStats,
|
|
27
|
+
HNSWStats,
|
|
28
|
+
} from './types.js';
|
|
29
|
+
import { HNSWIndex } from './hnsw-index.js';
|
|
30
|
+
import { CacheManager } from './cache-manager.js';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Configuration for AgentDB Adapter
|
|
34
|
+
*/
|
|
35
|
+
export interface AgentDBAdapterConfig {
|
|
36
|
+
/** Vector dimensions for embeddings (default: 1536 for OpenAI) */
|
|
37
|
+
dimensions: number;
|
|
38
|
+
|
|
39
|
+
/** Maximum number of entries */
|
|
40
|
+
maxEntries: number;
|
|
41
|
+
|
|
42
|
+
/** Enable caching */
|
|
43
|
+
cacheEnabled: boolean;
|
|
44
|
+
|
|
45
|
+
/** Maximum cache size */
|
|
46
|
+
cacheSize: number;
|
|
47
|
+
|
|
48
|
+
/** Cache TTL in milliseconds */
|
|
49
|
+
cacheTtl: number;
|
|
50
|
+
|
|
51
|
+
/** HNSW M parameter (max connections per layer) */
|
|
52
|
+
hnswM: number;
|
|
53
|
+
|
|
54
|
+
/** HNSW efConstruction parameter */
|
|
55
|
+
hnswEfConstruction: number;
|
|
56
|
+
|
|
57
|
+
/** Default namespace */
|
|
58
|
+
defaultNamespace: string;
|
|
59
|
+
|
|
60
|
+
/** Embedding generator function */
|
|
61
|
+
embeddingGenerator?: EmbeddingGenerator;
|
|
62
|
+
|
|
63
|
+
/** Enable persistence to disk */
|
|
64
|
+
persistenceEnabled: boolean;
|
|
65
|
+
|
|
66
|
+
/** Persistence path */
|
|
67
|
+
persistencePath?: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Default configuration values
|
|
72
|
+
*/
|
|
73
|
+
const DEFAULT_CONFIG: AgentDBAdapterConfig = {
|
|
74
|
+
dimensions: 1536,
|
|
75
|
+
maxEntries: 1000000,
|
|
76
|
+
cacheEnabled: true,
|
|
77
|
+
cacheSize: 10000,
|
|
78
|
+
cacheTtl: 300000, // 5 minutes
|
|
79
|
+
hnswM: 16,
|
|
80
|
+
hnswEfConstruction: 200,
|
|
81
|
+
defaultNamespace: 'default',
|
|
82
|
+
persistenceEnabled: false,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* AgentDB Memory Backend Adapter
|
|
87
|
+
*
|
|
88
|
+
* Provides unified memory storage with:
|
|
89
|
+
* - HNSW-based vector search (150x-12,500x faster than brute force)
|
|
90
|
+
* - LRU caching with TTL support
|
|
91
|
+
* - Namespace-based organization
|
|
92
|
+
* - Full-text and metadata filtering
|
|
93
|
+
* - Event-driven architecture
|
|
94
|
+
*/
|
|
95
|
+
export class AgentDBAdapter extends EventEmitter implements IMemoryBackend {
|
|
96
|
+
private config: AgentDBAdapterConfig;
|
|
97
|
+
private entries: Map<string, MemoryEntry> = new Map();
|
|
98
|
+
private index: HNSWIndex;
|
|
99
|
+
private cache: CacheManager<MemoryEntry>;
|
|
100
|
+
private namespaceIndex: Map<string, Set<string>> = new Map();
|
|
101
|
+
private keyIndex: Map<string, string> = new Map(); // namespace:key -> id
|
|
102
|
+
private tagIndex: Map<string, Set<string>> = new Map();
|
|
103
|
+
private initialized: boolean = false;
|
|
104
|
+
|
|
105
|
+
// Performance tracking
|
|
106
|
+
private stats = {
|
|
107
|
+
queryCount: 0,
|
|
108
|
+
totalQueryTime: 0,
|
|
109
|
+
searchCount: 0,
|
|
110
|
+
totalSearchTime: 0,
|
|
111
|
+
writeCount: 0,
|
|
112
|
+
totalWriteTime: 0,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
constructor(config: Partial<AgentDBAdapterConfig> = {}) {
|
|
116
|
+
super();
|
|
117
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
118
|
+
|
|
119
|
+
// Initialize HNSW index
|
|
120
|
+
this.index = new HNSWIndex({
|
|
121
|
+
dimensions: this.config.dimensions,
|
|
122
|
+
M: this.config.hnswM,
|
|
123
|
+
efConstruction: this.config.hnswEfConstruction,
|
|
124
|
+
maxElements: this.config.maxEntries,
|
|
125
|
+
metric: 'cosine',
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Initialize cache
|
|
129
|
+
this.cache = new CacheManager<MemoryEntry>({
|
|
130
|
+
maxSize: this.config.cacheSize,
|
|
131
|
+
ttl: this.config.cacheTtl,
|
|
132
|
+
lruEnabled: true,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Forward events
|
|
136
|
+
this.index.on('point:added', (data) => this.emit('index:added', data));
|
|
137
|
+
this.cache.on('cache:hit', (data) => this.emit('cache:hit', data));
|
|
138
|
+
this.cache.on('cache:miss', (data) => this.emit('cache:miss', data));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Initialize the adapter
|
|
143
|
+
*/
|
|
144
|
+
async initialize(): Promise<void> {
|
|
145
|
+
if (this.initialized) return;
|
|
146
|
+
|
|
147
|
+
// Load persisted data if enabled
|
|
148
|
+
if (this.config.persistenceEnabled && this.config.persistencePath) {
|
|
149
|
+
await this.loadFromDisk();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
this.initialized = true;
|
|
153
|
+
this.emit('initialized');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Shutdown the adapter
|
|
158
|
+
*/
|
|
159
|
+
async shutdown(): Promise<void> {
|
|
160
|
+
if (!this.initialized) return;
|
|
161
|
+
|
|
162
|
+
// Persist data if enabled
|
|
163
|
+
if (this.config.persistenceEnabled && this.config.persistencePath) {
|
|
164
|
+
await this.saveToDisk();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this.cache.shutdown();
|
|
168
|
+
this.initialized = false;
|
|
169
|
+
this.emit('shutdown');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Store a memory entry
|
|
174
|
+
*/
|
|
175
|
+
async store(entry: MemoryEntry): Promise<void> {
|
|
176
|
+
const startTime = performance.now();
|
|
177
|
+
|
|
178
|
+
// Generate embedding if content provided but no embedding
|
|
179
|
+
if (entry.content && !entry.embedding && this.config.embeddingGenerator) {
|
|
180
|
+
entry.embedding = await this.config.embeddingGenerator(entry.content);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Store in main storage
|
|
184
|
+
this.entries.set(entry.id, entry);
|
|
185
|
+
|
|
186
|
+
// Update namespace index
|
|
187
|
+
const namespace = entry.namespace || this.config.defaultNamespace;
|
|
188
|
+
if (!this.namespaceIndex.has(namespace)) {
|
|
189
|
+
this.namespaceIndex.set(namespace, new Set());
|
|
190
|
+
}
|
|
191
|
+
this.namespaceIndex.get(namespace)!.add(entry.id);
|
|
192
|
+
|
|
193
|
+
// Update key index
|
|
194
|
+
const keyIndexKey = `${namespace}:${entry.key}`;
|
|
195
|
+
this.keyIndex.set(keyIndexKey, entry.id);
|
|
196
|
+
|
|
197
|
+
// Update tag index
|
|
198
|
+
for (const tag of entry.tags) {
|
|
199
|
+
if (!this.tagIndex.has(tag)) {
|
|
200
|
+
this.tagIndex.set(tag, new Set());
|
|
201
|
+
}
|
|
202
|
+
this.tagIndex.get(tag)!.add(entry.id);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Index embedding if available
|
|
206
|
+
if (entry.embedding) {
|
|
207
|
+
await this.index.addPoint(entry.id, entry.embedding);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Update cache
|
|
211
|
+
if (this.config.cacheEnabled) {
|
|
212
|
+
this.cache.set(entry.id, entry);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const duration = performance.now() - startTime;
|
|
216
|
+
this.stats.writeCount++;
|
|
217
|
+
this.stats.totalWriteTime += duration;
|
|
218
|
+
|
|
219
|
+
this.emit('entry:stored', { id: entry.id, duration });
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Get a memory entry by ID
|
|
224
|
+
*/
|
|
225
|
+
async get(id: string): Promise<MemoryEntry | null> {
|
|
226
|
+
// Check cache first
|
|
227
|
+
if (this.config.cacheEnabled) {
|
|
228
|
+
const cached = this.cache.get(id);
|
|
229
|
+
if (cached) {
|
|
230
|
+
this.updateAccessStats(cached);
|
|
231
|
+
return cached;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const entry = this.entries.get(id);
|
|
236
|
+
if (entry) {
|
|
237
|
+
this.updateAccessStats(entry);
|
|
238
|
+
if (this.config.cacheEnabled) {
|
|
239
|
+
this.cache.set(id, entry);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return entry || null;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Get a memory entry by key within a namespace
|
|
248
|
+
*/
|
|
249
|
+
async getByKey(namespace: string, key: string): Promise<MemoryEntry | null> {
|
|
250
|
+
const keyIndexKey = `${namespace}:${key}`;
|
|
251
|
+
const id = this.keyIndex.get(keyIndexKey);
|
|
252
|
+
if (!id) return null;
|
|
253
|
+
return this.get(id);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Update a memory entry
|
|
258
|
+
*/
|
|
259
|
+
async update(id: string, update: MemoryEntryUpdate): Promise<MemoryEntry | null> {
|
|
260
|
+
const entry = this.entries.get(id);
|
|
261
|
+
if (!entry) return null;
|
|
262
|
+
|
|
263
|
+
// Apply updates
|
|
264
|
+
if (update.content !== undefined) {
|
|
265
|
+
entry.content = update.content;
|
|
266
|
+
// Regenerate embedding if content changed
|
|
267
|
+
if (this.config.embeddingGenerator) {
|
|
268
|
+
entry.embedding = await this.config.embeddingGenerator(entry.content);
|
|
269
|
+
// Re-index
|
|
270
|
+
await this.index.removePoint(id);
|
|
271
|
+
await this.index.addPoint(id, entry.embedding);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (update.tags !== undefined) {
|
|
276
|
+
// Update tag index
|
|
277
|
+
for (const oldTag of entry.tags) {
|
|
278
|
+
this.tagIndex.get(oldTag)?.delete(id);
|
|
279
|
+
}
|
|
280
|
+
entry.tags = update.tags;
|
|
281
|
+
for (const newTag of update.tags) {
|
|
282
|
+
if (!this.tagIndex.has(newTag)) {
|
|
283
|
+
this.tagIndex.set(newTag, new Set());
|
|
284
|
+
}
|
|
285
|
+
this.tagIndex.get(newTag)!.add(id);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (update.metadata !== undefined) {
|
|
290
|
+
entry.metadata = { ...entry.metadata, ...update.metadata };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (update.accessLevel !== undefined) {
|
|
294
|
+
entry.accessLevel = update.accessLevel;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (update.expiresAt !== undefined) {
|
|
298
|
+
entry.expiresAt = update.expiresAt;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (update.references !== undefined) {
|
|
302
|
+
entry.references = update.references;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
entry.updatedAt = Date.now();
|
|
306
|
+
entry.version++;
|
|
307
|
+
|
|
308
|
+
// Update cache
|
|
309
|
+
if (this.config.cacheEnabled) {
|
|
310
|
+
this.cache.set(id, entry);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.emit('entry:updated', { id });
|
|
314
|
+
return entry;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Delete a memory entry
|
|
319
|
+
*/
|
|
320
|
+
async delete(id: string): Promise<boolean> {
|
|
321
|
+
const entry = this.entries.get(id);
|
|
322
|
+
if (!entry) return false;
|
|
323
|
+
|
|
324
|
+
// Remove from main storage
|
|
325
|
+
this.entries.delete(id);
|
|
326
|
+
|
|
327
|
+
// Remove from namespace index
|
|
328
|
+
this.namespaceIndex.get(entry.namespace)?.delete(id);
|
|
329
|
+
|
|
330
|
+
// Remove from key index
|
|
331
|
+
const keyIndexKey = `${entry.namespace}:${entry.key}`;
|
|
332
|
+
this.keyIndex.delete(keyIndexKey);
|
|
333
|
+
|
|
334
|
+
// Remove from tag index
|
|
335
|
+
for (const tag of entry.tags) {
|
|
336
|
+
this.tagIndex.get(tag)?.delete(id);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Remove from vector index
|
|
340
|
+
if (entry.embedding) {
|
|
341
|
+
await this.index.removePoint(id);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// Remove from cache
|
|
345
|
+
if (this.config.cacheEnabled) {
|
|
346
|
+
this.cache.delete(id);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
this.emit('entry:deleted', { id });
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Query memory entries with filters
|
|
355
|
+
*/
|
|
356
|
+
async query(query: MemoryQuery): Promise<MemoryEntry[]> {
|
|
357
|
+
const startTime = performance.now();
|
|
358
|
+
let results: MemoryEntry[] = [];
|
|
359
|
+
|
|
360
|
+
switch (query.type) {
|
|
361
|
+
case 'exact':
|
|
362
|
+
if (query.key && query.namespace) {
|
|
363
|
+
const entry = await this.getByKey(query.namespace, query.key);
|
|
364
|
+
if (entry) results = [entry];
|
|
365
|
+
}
|
|
366
|
+
break;
|
|
367
|
+
|
|
368
|
+
case 'prefix':
|
|
369
|
+
results = this.queryByPrefix(query);
|
|
370
|
+
break;
|
|
371
|
+
|
|
372
|
+
case 'tag':
|
|
373
|
+
results = this.queryByTags(query);
|
|
374
|
+
break;
|
|
375
|
+
|
|
376
|
+
case 'semantic':
|
|
377
|
+
case 'hybrid':
|
|
378
|
+
results = await this.querySemanticWithFilters(query);
|
|
379
|
+
break;
|
|
380
|
+
|
|
381
|
+
default:
|
|
382
|
+
results = this.queryWithFilters(query);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Apply common filters
|
|
386
|
+
results = this.applyFilters(results, query);
|
|
387
|
+
|
|
388
|
+
// Apply pagination
|
|
389
|
+
const offset = query.offset || 0;
|
|
390
|
+
results = results.slice(offset, offset + query.limit);
|
|
391
|
+
|
|
392
|
+
const duration = performance.now() - startTime;
|
|
393
|
+
this.stats.queryCount++;
|
|
394
|
+
this.stats.totalQueryTime += duration;
|
|
395
|
+
|
|
396
|
+
return results;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Semantic vector search
|
|
401
|
+
*/
|
|
402
|
+
async search(
|
|
403
|
+
embedding: Float32Array,
|
|
404
|
+
options: SearchOptions
|
|
405
|
+
): Promise<SearchResult[]> {
|
|
406
|
+
const startTime = performance.now();
|
|
407
|
+
|
|
408
|
+
const indexResults = await this.index.search(embedding, options.k, options.ef);
|
|
409
|
+
|
|
410
|
+
const results: SearchResult[] = [];
|
|
411
|
+
for (const { id, distance } of indexResults) {
|
|
412
|
+
const entry = this.entries.get(id);
|
|
413
|
+
if (!entry) continue;
|
|
414
|
+
|
|
415
|
+
// Apply threshold filter
|
|
416
|
+
const score = 1 - distance; // Convert distance to similarity
|
|
417
|
+
if (options.threshold && score < options.threshold) continue;
|
|
418
|
+
|
|
419
|
+
// Apply additional filters if provided
|
|
420
|
+
if (options.filters) {
|
|
421
|
+
const filtered = this.applyFilters([entry], options.filters);
|
|
422
|
+
if (filtered.length === 0) continue;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
results.push({ entry, score, distance });
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const duration = performance.now() - startTime;
|
|
429
|
+
this.stats.searchCount++;
|
|
430
|
+
this.stats.totalSearchTime += duration;
|
|
431
|
+
|
|
432
|
+
return results;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Bulk insert entries
|
|
437
|
+
*/
|
|
438
|
+
async bulkInsert(entries: MemoryEntry[]): Promise<void> {
|
|
439
|
+
const startTime = performance.now();
|
|
440
|
+
|
|
441
|
+
for (const entry of entries) {
|
|
442
|
+
await this.store(entry);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const duration = performance.now() - startTime;
|
|
446
|
+
this.emit('bulk:inserted', { count: entries.length, duration });
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Bulk delete entries
|
|
451
|
+
*/
|
|
452
|
+
async bulkDelete(ids: string[]): Promise<number> {
|
|
453
|
+
let deleted = 0;
|
|
454
|
+
for (const id of ids) {
|
|
455
|
+
if (await this.delete(id)) {
|
|
456
|
+
deleted++;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return deleted;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Get entry count
|
|
464
|
+
*/
|
|
465
|
+
async count(namespace?: string): Promise<number> {
|
|
466
|
+
if (namespace) {
|
|
467
|
+
return this.namespaceIndex.get(namespace)?.size || 0;
|
|
468
|
+
}
|
|
469
|
+
return this.entries.size;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* List all namespaces
|
|
474
|
+
*/
|
|
475
|
+
async listNamespaces(): Promise<string[]> {
|
|
476
|
+
return Array.from(this.namespaceIndex.keys());
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Clear all entries in a namespace
|
|
481
|
+
*/
|
|
482
|
+
async clearNamespace(namespace: string): Promise<number> {
|
|
483
|
+
const ids = this.namespaceIndex.get(namespace);
|
|
484
|
+
if (!ids) return 0;
|
|
485
|
+
|
|
486
|
+
let deleted = 0;
|
|
487
|
+
for (const id of ids) {
|
|
488
|
+
if (await this.delete(id)) {
|
|
489
|
+
deleted++;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return deleted;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Get backend statistics
|
|
498
|
+
*/
|
|
499
|
+
async getStats(): Promise<BackendStats> {
|
|
500
|
+
const entriesByNamespace: Record<string, number> = {};
|
|
501
|
+
for (const [namespace, ids] of this.namespaceIndex) {
|
|
502
|
+
entriesByNamespace[namespace] = ids.size;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const entriesByType: Record<MemoryType, number> = {
|
|
506
|
+
episodic: 0,
|
|
507
|
+
semantic: 0,
|
|
508
|
+
procedural: 0,
|
|
509
|
+
working: 0,
|
|
510
|
+
cache: 0,
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
for (const entry of this.entries.values()) {
|
|
514
|
+
entriesByType[entry.type]++;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return {
|
|
518
|
+
totalEntries: this.entries.size,
|
|
519
|
+
entriesByNamespace,
|
|
520
|
+
entriesByType,
|
|
521
|
+
memoryUsage: this.estimateMemoryUsage(),
|
|
522
|
+
hnswStats: this.index.getStats(),
|
|
523
|
+
cacheStats: this.cache.getStats(),
|
|
524
|
+
avgQueryTime:
|
|
525
|
+
this.stats.queryCount > 0
|
|
526
|
+
? this.stats.totalQueryTime / this.stats.queryCount
|
|
527
|
+
: 0,
|
|
528
|
+
avgSearchTime:
|
|
529
|
+
this.stats.searchCount > 0
|
|
530
|
+
? this.stats.totalSearchTime / this.stats.searchCount
|
|
531
|
+
: 0,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* Perform health check
|
|
537
|
+
*/
|
|
538
|
+
async healthCheck(): Promise<HealthCheckResult> {
|
|
539
|
+
const issues: string[] = [];
|
|
540
|
+
const recommendations: string[] = [];
|
|
541
|
+
|
|
542
|
+
// Check storage health
|
|
543
|
+
const storageHealth = this.checkStorageHealth(issues, recommendations);
|
|
544
|
+
|
|
545
|
+
// Check index health
|
|
546
|
+
const indexHealth = this.checkIndexHealth(issues, recommendations);
|
|
547
|
+
|
|
548
|
+
// Check cache health
|
|
549
|
+
const cacheHealth = this.checkCacheHealth(issues, recommendations);
|
|
550
|
+
|
|
551
|
+
// Determine overall status
|
|
552
|
+
let status: 'healthy' | 'degraded' | 'unhealthy' = 'healthy';
|
|
553
|
+
if (
|
|
554
|
+
storageHealth.status === 'unhealthy' ||
|
|
555
|
+
indexHealth.status === 'unhealthy' ||
|
|
556
|
+
cacheHealth.status === 'unhealthy'
|
|
557
|
+
) {
|
|
558
|
+
status = 'unhealthy';
|
|
559
|
+
} else if (
|
|
560
|
+
storageHealth.status === 'degraded' ||
|
|
561
|
+
indexHealth.status === 'degraded' ||
|
|
562
|
+
cacheHealth.status === 'degraded'
|
|
563
|
+
) {
|
|
564
|
+
status = 'degraded';
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return {
|
|
568
|
+
status,
|
|
569
|
+
components: {
|
|
570
|
+
storage: storageHealth,
|
|
571
|
+
index: indexHealth,
|
|
572
|
+
cache: cacheHealth,
|
|
573
|
+
},
|
|
574
|
+
timestamp: Date.now(),
|
|
575
|
+
issues,
|
|
576
|
+
recommendations,
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// ===== Convenience Methods =====
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Store a new entry from input
|
|
584
|
+
*/
|
|
585
|
+
async storeEntry(input: MemoryEntryInput): Promise<MemoryEntry> {
|
|
586
|
+
const entry = createDefaultEntry(input);
|
|
587
|
+
await this.store(entry);
|
|
588
|
+
return entry;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* Semantic search by content string
|
|
593
|
+
*/
|
|
594
|
+
async semanticSearch(
|
|
595
|
+
content: string,
|
|
596
|
+
k: number = 10,
|
|
597
|
+
threshold?: number
|
|
598
|
+
): Promise<SearchResult[]> {
|
|
599
|
+
if (!this.config.embeddingGenerator) {
|
|
600
|
+
throw new Error('Embedding generator not configured');
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const embedding = await this.config.embeddingGenerator(content);
|
|
604
|
+
return this.search(embedding, { k, threshold });
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// ===== Private Methods =====
|
|
608
|
+
|
|
609
|
+
private queryByPrefix(query: MemoryQuery): MemoryEntry[] {
|
|
610
|
+
const results: MemoryEntry[] = [];
|
|
611
|
+
const prefix = query.keyPrefix || '';
|
|
612
|
+
const namespace = query.namespace || this.config.defaultNamespace;
|
|
613
|
+
|
|
614
|
+
for (const [key, id] of this.keyIndex) {
|
|
615
|
+
if (key.startsWith(`${namespace}:${prefix}`)) {
|
|
616
|
+
const entry = this.entries.get(id);
|
|
617
|
+
if (entry) results.push(entry);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
return results;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
private queryByTags(query: MemoryQuery): MemoryEntry[] {
|
|
625
|
+
if (!query.tags || query.tags.length === 0) {
|
|
626
|
+
return Array.from(this.entries.values());
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
// Get intersection of entries for all tags
|
|
630
|
+
let matchingIds: Set<string> | null = null;
|
|
631
|
+
|
|
632
|
+
for (const tag of query.tags) {
|
|
633
|
+
const tagIds = this.tagIndex.get(tag);
|
|
634
|
+
if (!tagIds) {
|
|
635
|
+
return []; // Tag doesn't exist
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
if (matchingIds === null) {
|
|
639
|
+
matchingIds = new Set(tagIds);
|
|
640
|
+
} else {
|
|
641
|
+
// Intersect with previous results
|
|
642
|
+
for (const id of matchingIds) {
|
|
643
|
+
if (!tagIds.has(id)) {
|
|
644
|
+
matchingIds.delete(id);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
if (!matchingIds) return [];
|
|
651
|
+
|
|
652
|
+
const results: MemoryEntry[] = [];
|
|
653
|
+
for (const id of matchingIds) {
|
|
654
|
+
const entry = this.entries.get(id);
|
|
655
|
+
if (entry) results.push(entry);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
return results;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
private async querySemanticWithFilters(
|
|
662
|
+
query: MemoryQuery
|
|
663
|
+
): Promise<MemoryEntry[]> {
|
|
664
|
+
if (!query.content && !query.embedding) {
|
|
665
|
+
return this.queryWithFilters(query);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
let embedding = query.embedding;
|
|
669
|
+
if (!embedding && query.content && this.config.embeddingGenerator) {
|
|
670
|
+
embedding = await this.config.embeddingGenerator(query.content);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
if (!embedding) {
|
|
674
|
+
return this.queryWithFilters(query);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
const searchResults = await this.search(embedding, {
|
|
678
|
+
k: query.limit * 2, // Over-fetch for filtering
|
|
679
|
+
threshold: query.threshold,
|
|
680
|
+
filters: query,
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
return searchResults.map((r) => r.entry);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
private queryWithFilters(query: MemoryQuery): MemoryEntry[] {
|
|
687
|
+
let entries: MemoryEntry[] = [];
|
|
688
|
+
|
|
689
|
+
// Start with namespace filter if provided
|
|
690
|
+
if (query.namespace) {
|
|
691
|
+
const namespaceIds = this.namespaceIndex.get(query.namespace);
|
|
692
|
+
if (!namespaceIds) return [];
|
|
693
|
+
for (const id of namespaceIds) {
|
|
694
|
+
const entry = this.entries.get(id);
|
|
695
|
+
if (entry) entries.push(entry);
|
|
696
|
+
}
|
|
697
|
+
} else {
|
|
698
|
+
entries = Array.from(this.entries.values());
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
return entries;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
private applyFilters(
|
|
705
|
+
entries: MemoryEntry[],
|
|
706
|
+
query: MemoryQuery
|
|
707
|
+
): MemoryEntry[] {
|
|
708
|
+
return entries.filter((entry) => {
|
|
709
|
+
// Namespace filter
|
|
710
|
+
if (query.namespace && entry.namespace !== query.namespace) {
|
|
711
|
+
return false;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Memory type filter
|
|
715
|
+
if (query.memoryType && entry.type !== query.memoryType) {
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// Access level filter
|
|
720
|
+
if (query.accessLevel && entry.accessLevel !== query.accessLevel) {
|
|
721
|
+
return false;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
// Owner filter
|
|
725
|
+
if (query.ownerId && entry.ownerId !== query.ownerId) {
|
|
726
|
+
return false;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
// Tags filter
|
|
730
|
+
if (query.tags && query.tags.length > 0) {
|
|
731
|
+
if (!query.tags.every((tag) => entry.tags.includes(tag))) {
|
|
732
|
+
return false;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// Time range filters
|
|
737
|
+
if (query.createdAfter && entry.createdAt < query.createdAfter) {
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
if (query.createdBefore && entry.createdAt > query.createdBefore) {
|
|
741
|
+
return false;
|
|
742
|
+
}
|
|
743
|
+
if (query.updatedAfter && entry.updatedAt < query.updatedAfter) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
if (query.updatedBefore && entry.updatedAt > query.updatedBefore) {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// Expiration filter
|
|
751
|
+
if (!query.includeExpired && entry.expiresAt) {
|
|
752
|
+
if (entry.expiresAt < Date.now()) {
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// Metadata filters
|
|
758
|
+
if (query.metadata) {
|
|
759
|
+
for (const [key, value] of Object.entries(query.metadata)) {
|
|
760
|
+
if (entry.metadata[key] !== value) {
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
return true;
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
private updateAccessStats(entry: MemoryEntry): void {
|
|
771
|
+
entry.accessCount++;
|
|
772
|
+
entry.lastAccessedAt = Date.now();
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
private estimateMemoryUsage(): number {
|
|
776
|
+
let total = 0;
|
|
777
|
+
|
|
778
|
+
// Estimate entry storage
|
|
779
|
+
for (const entry of this.entries.values()) {
|
|
780
|
+
total += this.estimateEntrySize(entry);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// Add index memory
|
|
784
|
+
total += this.index.getStats().memoryUsage;
|
|
785
|
+
|
|
786
|
+
// Add cache memory
|
|
787
|
+
total += this.cache.getStats().memoryUsage;
|
|
788
|
+
|
|
789
|
+
return total;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
private estimateEntrySize(entry: MemoryEntry): number {
|
|
793
|
+
let size = 0;
|
|
794
|
+
|
|
795
|
+
// Base object overhead
|
|
796
|
+
size += 100;
|
|
797
|
+
|
|
798
|
+
// String fields
|
|
799
|
+
size += (entry.id.length + entry.key.length + entry.content.length) * 2;
|
|
800
|
+
|
|
801
|
+
// Embedding (Float32Array)
|
|
802
|
+
if (entry.embedding) {
|
|
803
|
+
size += entry.embedding.length * 4;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// Tags and references
|
|
807
|
+
size += entry.tags.join('').length * 2;
|
|
808
|
+
size += entry.references.join('').length * 2;
|
|
809
|
+
|
|
810
|
+
// Metadata (rough estimate)
|
|
811
|
+
size += JSON.stringify(entry.metadata).length * 2;
|
|
812
|
+
|
|
813
|
+
return size;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
private checkStorageHealth(
|
|
817
|
+
issues: string[],
|
|
818
|
+
recommendations: string[]
|
|
819
|
+
): ComponentHealth {
|
|
820
|
+
const utilizationPercent =
|
|
821
|
+
(this.entries.size / this.config.maxEntries) * 100;
|
|
822
|
+
|
|
823
|
+
if (utilizationPercent > 95) {
|
|
824
|
+
issues.push('Storage utilization critical (>95%)');
|
|
825
|
+
recommendations.push('Increase maxEntries or cleanup old data');
|
|
826
|
+
return { status: 'unhealthy', latency: 0, message: 'Storage near capacity' };
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
if (utilizationPercent > 80) {
|
|
830
|
+
issues.push('Storage utilization high (>80%)');
|
|
831
|
+
recommendations.push('Consider cleanup or capacity increase');
|
|
832
|
+
return { status: 'degraded', latency: 0, message: 'Storage utilization high' };
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
return { status: 'healthy', latency: 0 };
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
private checkIndexHealth(
|
|
839
|
+
issues: string[],
|
|
840
|
+
recommendations: string[]
|
|
841
|
+
): ComponentHealth {
|
|
842
|
+
const stats = this.index.getStats();
|
|
843
|
+
|
|
844
|
+
if (stats.avgSearchTime > 10) {
|
|
845
|
+
issues.push('Index search time degraded (>10ms)');
|
|
846
|
+
recommendations.push('Consider rebuilding index or increasing ef');
|
|
847
|
+
return { status: 'degraded', latency: stats.avgSearchTime };
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
return { status: 'healthy', latency: stats.avgSearchTime };
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
private checkCacheHealth(
|
|
854
|
+
issues: string[],
|
|
855
|
+
recommendations: string[]
|
|
856
|
+
): ComponentHealth {
|
|
857
|
+
const stats = this.cache.getStats();
|
|
858
|
+
|
|
859
|
+
if (stats.hitRate < 0.5) {
|
|
860
|
+
issues.push('Cache hit rate low (<50%)');
|
|
861
|
+
recommendations.push('Consider increasing cache size');
|
|
862
|
+
return {
|
|
863
|
+
status: 'degraded',
|
|
864
|
+
latency: 0,
|
|
865
|
+
message: `Hit rate: ${(stats.hitRate * 100).toFixed(1)}%`,
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
return { status: 'healthy', latency: 0 };
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
private async loadFromDisk(): Promise<void> {
|
|
873
|
+
// Placeholder for persistence implementation
|
|
874
|
+
// Would use SQLite or file-based storage
|
|
875
|
+
this.emit('persistence:loaded');
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
private async saveToDisk(): Promise<void> {
|
|
879
|
+
// Placeholder for persistence implementation
|
|
880
|
+
this.emit('persistence:saved');
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
export default AgentDBAdapter;
|