@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.
Files changed (214) hide show
  1. package/.agentic-flow/intelligence.json +16 -0
  2. package/README.md +249 -0
  3. package/__tests__/coverage/base.css +224 -0
  4. package/__tests__/coverage/block-navigation.js +87 -0
  5. package/__tests__/coverage/coverage-final.json +19 -0
  6. package/__tests__/coverage/favicon.png +0 -0
  7. package/__tests__/coverage/index.html +206 -0
  8. package/__tests__/coverage/lcov-report/base.css +224 -0
  9. package/__tests__/coverage/lcov-report/block-navigation.js +87 -0
  10. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  11. package/__tests__/coverage/lcov-report/index.html +206 -0
  12. package/__tests__/coverage/lcov-report/prettify.css +1 -0
  13. package/__tests__/coverage/lcov-report/prettify.js +2 -0
  14. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/__tests__/coverage/lcov-report/sorter.js +210 -0
  16. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +2737 -0
  17. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +3130 -0
  18. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +601 -0
  19. package/__tests__/coverage/lcov-report/src/application/commands/index.html +131 -0
  20. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +394 -0
  21. package/__tests__/coverage/lcov-report/src/application/queries/index.html +116 -0
  22. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +796 -0
  23. package/__tests__/coverage/lcov-report/src/application/services/index.html +116 -0
  24. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +793 -0
  25. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +1633 -0
  26. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +1618 -0
  27. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +116 -0
  28. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +952 -0
  29. package/__tests__/coverage/lcov-report/src/domain/services/index.html +116 -0
  30. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +1294 -0
  31. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +3124 -0
  32. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +2167 -0
  33. package/__tests__/coverage/lcov-report/src/index.html +266 -0
  34. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  35. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +116 -0
  36. package/__tests__/coverage/lcov-report/src/migration.ts.html +2092 -0
  37. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +1711 -0
  38. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +2281 -0
  39. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +2374 -0
  40. package/__tests__/coverage/lcov-report/src/types.ts.html +2266 -0
  41. package/__tests__/coverage/lcov.info +10238 -0
  42. package/__tests__/coverage/prettify.css +1 -0
  43. package/__tests__/coverage/prettify.js +2 -0
  44. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  45. package/__tests__/coverage/sorter.js +210 -0
  46. package/__tests__/coverage/src/agentdb-adapter.ts.html +2737 -0
  47. package/__tests__/coverage/src/agentdb-backend.ts.html +3130 -0
  48. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +601 -0
  49. package/__tests__/coverage/src/application/commands/index.html +131 -0
  50. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +394 -0
  51. package/__tests__/coverage/src/application/queries/index.html +116 -0
  52. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +796 -0
  53. package/__tests__/coverage/src/application/services/index.html +116 -0
  54. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +793 -0
  55. package/__tests__/coverage/src/cache-manager.ts.html +1633 -0
  56. package/__tests__/coverage/src/database-provider.ts.html +1618 -0
  57. package/__tests__/coverage/src/domain/entities/index.html +116 -0
  58. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +952 -0
  59. package/__tests__/coverage/src/domain/services/index.html +116 -0
  60. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +1294 -0
  61. package/__tests__/coverage/src/hnsw-index.ts.html +3124 -0
  62. package/__tests__/coverage/src/hybrid-backend.ts.html +2167 -0
  63. package/__tests__/coverage/src/index.html +266 -0
  64. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  65. package/__tests__/coverage/src/infrastructure/repositories/index.html +116 -0
  66. package/__tests__/coverage/src/migration.ts.html +2092 -0
  67. package/__tests__/coverage/src/query-builder.ts.html +1711 -0
  68. package/__tests__/coverage/src/sqlite-backend.ts.html +2281 -0
  69. package/__tests__/coverage/src/sqljs-backend.ts.html +2374 -0
  70. package/__tests__/coverage/src/types.ts.html +2266 -0
  71. package/benchmarks/cache-hit-rate.bench.ts +535 -0
  72. package/benchmarks/hnsw-indexing.bench.ts +552 -0
  73. package/benchmarks/memory-write.bench.ts +469 -0
  74. package/benchmarks/vector-search.bench.ts +449 -0
  75. package/dist/agentdb-adapter.d.ts +146 -0
  76. package/dist/agentdb-adapter.d.ts.map +1 -0
  77. package/dist/agentdb-adapter.js +679 -0
  78. package/dist/agentdb-adapter.js.map +1 -0
  79. package/dist/agentdb-backend.d.ts +214 -0
  80. package/dist/agentdb-backend.d.ts.map +1 -0
  81. package/dist/agentdb-backend.js +827 -0
  82. package/dist/agentdb-backend.js.map +1 -0
  83. package/dist/agentdb-backend.test.d.ts +7 -0
  84. package/dist/agentdb-backend.test.d.ts.map +1 -0
  85. package/dist/agentdb-backend.test.js +258 -0
  86. package/dist/agentdb-backend.test.js.map +1 -0
  87. package/dist/application/commands/delete-memory.command.d.ts +65 -0
  88. package/dist/application/commands/delete-memory.command.d.ts.map +1 -0
  89. package/dist/application/commands/delete-memory.command.js +129 -0
  90. package/dist/application/commands/delete-memory.command.js.map +1 -0
  91. package/dist/application/commands/store-memory.command.d.ts +48 -0
  92. package/dist/application/commands/store-memory.command.d.ts.map +1 -0
  93. package/dist/application/commands/store-memory.command.js +72 -0
  94. package/dist/application/commands/store-memory.command.js.map +1 -0
  95. package/dist/application/index.d.ts +12 -0
  96. package/dist/application/index.d.ts.map +1 -0
  97. package/dist/application/index.js +15 -0
  98. package/dist/application/index.js.map +1 -0
  99. package/dist/application/queries/search-memory.query.d.ts +72 -0
  100. package/dist/application/queries/search-memory.query.d.ts.map +1 -0
  101. package/dist/application/queries/search-memory.query.js +143 -0
  102. package/dist/application/queries/search-memory.query.js.map +1 -0
  103. package/dist/application/services/memory-application-service.d.ts +121 -0
  104. package/dist/application/services/memory-application-service.d.ts.map +1 -0
  105. package/dist/application/services/memory-application-service.js +190 -0
  106. package/dist/application/services/memory-application-service.js.map +1 -0
  107. package/dist/cache-manager.d.ts +134 -0
  108. package/dist/cache-manager.d.ts.map +1 -0
  109. package/dist/cache-manager.js +407 -0
  110. package/dist/cache-manager.js.map +1 -0
  111. package/dist/database-provider.d.ts +86 -0
  112. package/dist/database-provider.d.ts.map +1 -0
  113. package/dist/database-provider.js +385 -0
  114. package/dist/database-provider.js.map +1 -0
  115. package/dist/database-provider.test.d.ts +7 -0
  116. package/dist/database-provider.test.d.ts.map +1 -0
  117. package/dist/database-provider.test.js +285 -0
  118. package/dist/database-provider.test.js.map +1 -0
  119. package/dist/domain/entities/memory-entry.d.ts +143 -0
  120. package/dist/domain/entities/memory-entry.d.ts.map +1 -0
  121. package/dist/domain/entities/memory-entry.js +226 -0
  122. package/dist/domain/entities/memory-entry.js.map +1 -0
  123. package/dist/domain/index.d.ts +11 -0
  124. package/dist/domain/index.d.ts.map +1 -0
  125. package/dist/domain/index.js +12 -0
  126. package/dist/domain/index.js.map +1 -0
  127. package/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
  128. package/dist/domain/repositories/memory-repository.interface.d.ts.map +1 -0
  129. package/dist/domain/repositories/memory-repository.interface.js +11 -0
  130. package/dist/domain/repositories/memory-repository.interface.js.map +1 -0
  131. package/dist/domain/services/memory-domain-service.d.ts +105 -0
  132. package/dist/domain/services/memory-domain-service.d.ts.map +1 -0
  133. package/dist/domain/services/memory-domain-service.js +297 -0
  134. package/dist/domain/services/memory-domain-service.js.map +1 -0
  135. package/dist/hnsw-index.d.ts +111 -0
  136. package/dist/hnsw-index.d.ts.map +1 -0
  137. package/dist/hnsw-index.js +781 -0
  138. package/dist/hnsw-index.js.map +1 -0
  139. package/dist/hybrid-backend.d.ts +217 -0
  140. package/dist/hybrid-backend.d.ts.map +1 -0
  141. package/dist/hybrid-backend.js +491 -0
  142. package/dist/hybrid-backend.js.map +1 -0
  143. package/dist/hybrid-backend.test.d.ts +8 -0
  144. package/dist/hybrid-backend.test.d.ts.map +1 -0
  145. package/dist/hybrid-backend.test.js +320 -0
  146. package/dist/hybrid-backend.test.js.map +1 -0
  147. package/dist/index.d.ts +188 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +345 -0
  150. package/dist/index.js.map +1 -0
  151. package/dist/infrastructure/index.d.ts +17 -0
  152. package/dist/infrastructure/index.d.ts.map +1 -0
  153. package/dist/infrastructure/index.js +16 -0
  154. package/dist/infrastructure/index.js.map +1 -0
  155. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
  156. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts.map +1 -0
  157. package/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
  158. package/dist/infrastructure/repositories/hybrid-memory-repository.js.map +1 -0
  159. package/dist/migration.d.ts +68 -0
  160. package/dist/migration.d.ts.map +1 -0
  161. package/dist/migration.js +513 -0
  162. package/dist/migration.js.map +1 -0
  163. package/dist/query-builder.d.ts +211 -0
  164. package/dist/query-builder.d.ts.map +1 -0
  165. package/dist/query-builder.js +438 -0
  166. package/dist/query-builder.js.map +1 -0
  167. package/dist/sqlite-backend.d.ts +121 -0
  168. package/dist/sqlite-backend.d.ts.map +1 -0
  169. package/dist/sqlite-backend.js +564 -0
  170. package/dist/sqlite-backend.js.map +1 -0
  171. package/dist/sqljs-backend.d.ts +128 -0
  172. package/dist/sqljs-backend.d.ts.map +1 -0
  173. package/dist/sqljs-backend.js +598 -0
  174. package/dist/sqljs-backend.js.map +1 -0
  175. package/dist/types.d.ts +481 -0
  176. package/dist/types.d.ts.map +1 -0
  177. package/dist/types.js +58 -0
  178. package/dist/types.js.map +1 -0
  179. package/docs/AGENTDB-INTEGRATION.md +388 -0
  180. package/docs/CROSS_PLATFORM.md +505 -0
  181. package/docs/WINDOWS_SUPPORT.md +422 -0
  182. package/examples/agentdb-example.ts +345 -0
  183. package/examples/cross-platform-usage.ts +326 -0
  184. package/framework/benchmark.ts +112 -0
  185. package/package.json +31 -0
  186. package/src/agentdb-adapter.ts +884 -0
  187. package/src/agentdb-backend.test.ts +339 -0
  188. package/src/agentdb-backend.ts +1016 -0
  189. package/src/application/commands/delete-memory.command.ts +172 -0
  190. package/src/application/commands/store-memory.command.ts +103 -0
  191. package/src/application/index.ts +36 -0
  192. package/src/application/queries/search-memory.query.ts +237 -0
  193. package/src/application/services/memory-application-service.ts +236 -0
  194. package/src/cache-manager.ts +516 -0
  195. package/src/database-provider.test.ts +364 -0
  196. package/src/database-provider.ts +511 -0
  197. package/src/domain/entities/memory-entry.ts +289 -0
  198. package/src/domain/index.ts +35 -0
  199. package/src/domain/repositories/memory-repository.interface.ts +120 -0
  200. package/src/domain/services/memory-domain-service.ts +403 -0
  201. package/src/hnsw-index.ts +1013 -0
  202. package/src/hybrid-backend.test.ts +399 -0
  203. package/src/hybrid-backend.ts +694 -0
  204. package/src/index.ts +515 -0
  205. package/src/infrastructure/index.ts +23 -0
  206. package/src/infrastructure/repositories/hybrid-memory-repository.ts +516 -0
  207. package/src/migration.ts +669 -0
  208. package/src/query-builder.ts +542 -0
  209. package/src/sqlite-backend.ts +732 -0
  210. package/src/sqljs-backend.ts +763 -0
  211. package/src/types.ts +727 -0
  212. package/tsconfig.json +9 -0
  213. package/tsconfig.tsbuildinfo +1 -0
  214. package/verify-cross-platform.ts +170 -0
@@ -0,0 +1,1016 @@
1
+ /**
2
+ * AgentDB Backend - Integration with agentdb@2.0.0-alpha.3.4
3
+ *
4
+ * Provides IMemoryBackend implementation using AgentDB with:
5
+ * - HNSW vector search (150x-12,500x faster than brute-force)
6
+ * - Native or WASM backend support with graceful fallback
7
+ * - Optional dependency handling (works without hnswlib-node)
8
+ * - Seamless integration with HybridBackend
9
+ *
10
+ * @module v3/memory/agentdb-backend
11
+ */
12
+
13
+ import { EventEmitter } from 'node:events';
14
+ import {
15
+ IMemoryBackend,
16
+ MemoryEntry,
17
+ MemoryEntryInput,
18
+ MemoryEntryUpdate,
19
+ MemoryQuery,
20
+ SearchOptions,
21
+ SearchResult,
22
+ BackendStats,
23
+ HealthCheckResult,
24
+ ComponentHealth,
25
+ MemoryType,
26
+ EmbeddingGenerator,
27
+ generateMemoryId,
28
+ createDefaultEntry,
29
+ CacheStats,
30
+ HNSWStats,
31
+ } from './types.js';
32
+
33
+ // ===== AgentDB Optional Import =====
34
+
35
+ let AgentDB: any;
36
+ let HNSWIndex: any;
37
+ let isHnswlibAvailable: (() => Promise<boolean>) | undefined;
38
+
39
+ // Dynamically import agentdb (handled at runtime)
40
+ let agentdbImportPromise: Promise<void> | undefined;
41
+
42
+ function ensureAgentDBImport(): Promise<void> {
43
+ if (!agentdbImportPromise) {
44
+ agentdbImportPromise = (async () => {
45
+ try {
46
+ const agentdbModule = await import('agentdb');
47
+ AgentDB = agentdbModule.AgentDB || agentdbModule.default;
48
+ HNSWIndex = agentdbModule.HNSWIndex;
49
+ isHnswlibAvailable = agentdbModule.isHnswlibAvailable;
50
+ } catch (error) {
51
+ // AgentDB not available - will use fallback
52
+ }
53
+ })();
54
+ }
55
+ return agentdbImportPromise;
56
+ }
57
+
58
+ // ===== Configuration =====
59
+
60
+ /**
61
+ * Configuration for AgentDB Backend
62
+ */
63
+ export interface AgentDBBackendConfig {
64
+ /** Database path for persistence */
65
+ dbPath?: string;
66
+
67
+ /** Namespace for memory organization */
68
+ namespace?: string;
69
+
70
+ /** Force WASM backend (skip native hnswlib) */
71
+ forceWasm?: boolean;
72
+
73
+ /** Vector backend: 'auto', 'ruvector', 'hnswlib' */
74
+ vectorBackend?: 'auto' | 'ruvector' | 'hnswlib';
75
+
76
+ /** Vector dimensions (default: 1536) */
77
+ vectorDimension?: number;
78
+
79
+ /** HNSW M parameter */
80
+ hnswM?: number;
81
+
82
+ /** HNSW efConstruction parameter */
83
+ hnswEfConstruction?: number;
84
+
85
+ /** HNSW efSearch parameter */
86
+ hnswEfSearch?: number;
87
+
88
+ /** Enable caching */
89
+ cacheEnabled?: boolean;
90
+
91
+ /** Embedding generator function */
92
+ embeddingGenerator?: EmbeddingGenerator;
93
+
94
+ /** Maximum entries */
95
+ maxEntries?: number;
96
+ }
97
+
98
+ /**
99
+ * Default configuration
100
+ */
101
+ const DEFAULT_CONFIG: Required<
102
+ Omit<AgentDBBackendConfig, 'dbPath' | 'embeddingGenerator'>
103
+ > = {
104
+ namespace: 'default',
105
+ forceWasm: false,
106
+ vectorBackend: 'auto',
107
+ vectorDimension: 1536,
108
+ hnswM: 16,
109
+ hnswEfConstruction: 200,
110
+ hnswEfSearch: 100,
111
+ cacheEnabled: true,
112
+ maxEntries: 1000000,
113
+ };
114
+
115
+ // ===== AgentDB Backend Implementation =====
116
+
117
+ /**
118
+ * AgentDB Backend
119
+ *
120
+ * Integrates AgentDB for vector search with the V3 memory system.
121
+ * Provides 150x-12,500x faster search compared to brute-force approaches.
122
+ *
123
+ * Features:
124
+ * - HNSW indexing for fast approximate nearest neighbor search
125
+ * - Automatic fallback: native hnswlib → ruvector → WASM
126
+ * - Graceful handling of optional native dependencies
127
+ * - Semantic search with filtering
128
+ * - Compatible with HybridBackend for combined SQLite+AgentDB queries
129
+ */
130
+ export class AgentDBBackend extends EventEmitter implements IMemoryBackend {
131
+ private config: Required<
132
+ Omit<AgentDBBackendConfig, 'dbPath' | 'embeddingGenerator'>
133
+ > & {
134
+ dbPath?: string;
135
+ embeddingGenerator?: EmbeddingGenerator;
136
+ };
137
+ private agentdb: any;
138
+ private initialized: boolean = false;
139
+ private available: boolean = false;
140
+
141
+ // In-memory storage for compatibility
142
+ private entries: Map<string, MemoryEntry> = new Map();
143
+ private namespaceIndex: Map<string, Set<string>> = new Map();
144
+ private keyIndex: Map<string, string> = new Map();
145
+
146
+ // O(1) reverse lookup for numeric ID -> string ID (fixes O(n) linear scan)
147
+ private numericToStringIdMap: Map<number, string> = new Map();
148
+
149
+ // Performance tracking
150
+ private stats = {
151
+ queryCount: 0,
152
+ totalQueryTime: 0,
153
+ searchCount: 0,
154
+ totalSearchTime: 0,
155
+ };
156
+
157
+ constructor(config: AgentDBBackendConfig = {}) {
158
+ super();
159
+ this.config = { ...DEFAULT_CONFIG, ...config };
160
+ this.available = false; // Will be set during initialization
161
+ }
162
+
163
+ /**
164
+ * Initialize AgentDB
165
+ */
166
+ async initialize(): Promise<void> {
167
+ if (this.initialized) return;
168
+
169
+ // Try to import AgentDB
170
+ await ensureAgentDBImport();
171
+
172
+ this.available = AgentDB !== undefined;
173
+
174
+ if (!this.available) {
175
+ console.warn('AgentDB not available, using fallback in-memory storage');
176
+ this.initialized = true;
177
+ return;
178
+ }
179
+
180
+ try {
181
+ // Initialize AgentDB with config
182
+ this.agentdb = new AgentDB({
183
+ dbPath: this.config.dbPath || ':memory:',
184
+ namespace: this.config.namespace,
185
+ forceWasm: this.config.forceWasm,
186
+ vectorBackend: this.config.vectorBackend,
187
+ vectorDimension: this.config.vectorDimension,
188
+ });
189
+
190
+ await this.agentdb.initialize();
191
+
192
+ // Create memory_entries table if it doesn't exist
193
+ await this.createSchema();
194
+
195
+ this.initialized = true;
196
+ this.emit('initialized', {
197
+ backend: this.agentdb.vectorBackendName,
198
+ isWasm: this.agentdb.isWasm,
199
+ });
200
+ } catch (error) {
201
+ console.error('Failed to initialize AgentDB:', error);
202
+ this.available = false;
203
+ this.initialized = true;
204
+ this.emit('initialization:failed', { error });
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Shutdown AgentDB
210
+ */
211
+ async shutdown(): Promise<void> {
212
+ if (!this.initialized) return;
213
+
214
+ if (this.agentdb) {
215
+ await this.agentdb.close();
216
+ }
217
+
218
+ this.initialized = false;
219
+ this.emit('shutdown');
220
+ }
221
+
222
+ /**
223
+ * Store a memory entry
224
+ */
225
+ async store(entry: MemoryEntry): Promise<void> {
226
+ // Generate embedding if needed
227
+ if (entry.content && !entry.embedding && this.config.embeddingGenerator) {
228
+ entry.embedding = await this.config.embeddingGenerator(entry.content);
229
+ }
230
+
231
+ // Store in-memory for quick access
232
+ this.entries.set(entry.id, entry);
233
+
234
+ // Register ID mapping for O(1) reverse lookup
235
+ this.registerIdMapping(entry.id);
236
+
237
+ // Update indexes
238
+ this.updateIndexes(entry);
239
+
240
+ // Store in AgentDB if available
241
+ if (this.agentdb) {
242
+ await this.storeInAgentDB(entry);
243
+ }
244
+
245
+ this.emit('entry:stored', { id: entry.id });
246
+ }
247
+
248
+ /**
249
+ * Get entry by ID
250
+ */
251
+ async get(id: string): Promise<MemoryEntry | null> {
252
+ // Check in-memory first
253
+ const cached = this.entries.get(id);
254
+ if (cached) return cached;
255
+
256
+ // Query AgentDB if available
257
+ if (this.agentdb) {
258
+ return this.getFromAgentDB(id);
259
+ }
260
+
261
+ return null;
262
+ }
263
+
264
+ /**
265
+ * Get entry by key
266
+ */
267
+ async getByKey(namespace: string, key: string): Promise<MemoryEntry | null> {
268
+ const keyIndexKey = `${namespace}:${key}`;
269
+ const id = this.keyIndex.get(keyIndexKey);
270
+ if (!id) return null;
271
+ return this.get(id);
272
+ }
273
+
274
+ /**
275
+ * Update entry
276
+ */
277
+ async update(id: string, update: MemoryEntryUpdate): Promise<MemoryEntry | null> {
278
+ const entry = this.entries.get(id);
279
+ if (!entry) return null;
280
+
281
+ // Apply updates
282
+ if (update.content !== undefined) {
283
+ entry.content = update.content;
284
+ // Regenerate embedding if needed
285
+ if (this.config.embeddingGenerator) {
286
+ entry.embedding = await this.config.embeddingGenerator(entry.content);
287
+ }
288
+ }
289
+
290
+ if (update.tags !== undefined) {
291
+ entry.tags = update.tags;
292
+ }
293
+
294
+ if (update.metadata !== undefined) {
295
+ entry.metadata = { ...entry.metadata, ...update.metadata };
296
+ }
297
+
298
+ if (update.accessLevel !== undefined) {
299
+ entry.accessLevel = update.accessLevel;
300
+ }
301
+
302
+ if (update.expiresAt !== undefined) {
303
+ entry.expiresAt = update.expiresAt;
304
+ }
305
+
306
+ if (update.references !== undefined) {
307
+ entry.references = update.references;
308
+ }
309
+
310
+ entry.updatedAt = Date.now();
311
+ entry.version++;
312
+
313
+ // Update in AgentDB
314
+ if (this.agentdb) {
315
+ await this.updateInAgentDB(entry);
316
+ }
317
+
318
+ this.emit('entry:updated', { id });
319
+ return entry;
320
+ }
321
+
322
+ /**
323
+ * Delete entry
324
+ */
325
+ async delete(id: string): Promise<boolean> {
326
+ const entry = this.entries.get(id);
327
+ if (!entry) return false;
328
+
329
+ // Remove from indexes
330
+ this.entries.delete(id);
331
+ this.unregisterIdMapping(id); // Clean up reverse lookup map
332
+ this.namespaceIndex.get(entry.namespace)?.delete(id);
333
+ const keyIndexKey = `${entry.namespace}:${entry.key}`;
334
+ this.keyIndex.delete(keyIndexKey);
335
+
336
+ // Delete from AgentDB
337
+ if (this.agentdb) {
338
+ await this.deleteFromAgentDB(id);
339
+ }
340
+
341
+ this.emit('entry:deleted', { id });
342
+ return true;
343
+ }
344
+
345
+ /**
346
+ * Query entries
347
+ */
348
+ async query(query: MemoryQuery): Promise<MemoryEntry[]> {
349
+ const startTime = performance.now();
350
+ let results: MemoryEntry[] = [];
351
+
352
+ if (query.type === 'semantic' && (query.embedding || query.content)) {
353
+ // Use semantic search
354
+ const searchResults = await this.semanticSearch(query);
355
+ results = searchResults.map((r) => r.entry);
356
+ } else {
357
+ // Fallback to in-memory filtering
358
+ results = this.queryInMemory(query);
359
+ }
360
+
361
+ const duration = performance.now() - startTime;
362
+ this.stats.queryCount++;
363
+ this.stats.totalQueryTime += duration;
364
+
365
+ return results;
366
+ }
367
+
368
+ /**
369
+ * Semantic vector search
370
+ */
371
+ async search(
372
+ embedding: Float32Array,
373
+ options: SearchOptions
374
+ ): Promise<SearchResult[]> {
375
+ const startTime = performance.now();
376
+
377
+ if (!this.agentdb) {
378
+ // Fallback to brute-force search
379
+ return this.bruteForceSearch(embedding, options);
380
+ }
381
+
382
+ try {
383
+ // Use AgentDB HNSW search
384
+ const results = await this.searchWithAgentDB(embedding, options);
385
+
386
+ const duration = performance.now() - startTime;
387
+ this.stats.searchCount++;
388
+ this.stats.totalSearchTime += duration;
389
+
390
+ return results;
391
+ } catch (error) {
392
+ console.error('AgentDB search failed, falling back to brute-force:', error);
393
+ return this.bruteForceSearch(embedding, options);
394
+ }
395
+ }
396
+
397
+ /**
398
+ * Bulk insert
399
+ */
400
+ async bulkInsert(entries: MemoryEntry[]): Promise<void> {
401
+ for (const entry of entries) {
402
+ await this.store(entry);
403
+ }
404
+ }
405
+
406
+ /**
407
+ * Bulk delete
408
+ */
409
+ async bulkDelete(ids: string[]): Promise<number> {
410
+ let deleted = 0;
411
+ for (const id of ids) {
412
+ if (await this.delete(id)) {
413
+ deleted++;
414
+ }
415
+ }
416
+ return deleted;
417
+ }
418
+
419
+ /**
420
+ * Count entries
421
+ */
422
+ async count(namespace?: string): Promise<number> {
423
+ if (namespace) {
424
+ return this.namespaceIndex.get(namespace)?.size || 0;
425
+ }
426
+ return this.entries.size;
427
+ }
428
+
429
+ /**
430
+ * List namespaces
431
+ */
432
+ async listNamespaces(): Promise<string[]> {
433
+ return Array.from(this.namespaceIndex.keys());
434
+ }
435
+
436
+ /**
437
+ * Clear namespace
438
+ */
439
+ async clearNamespace(namespace: string): Promise<number> {
440
+ const ids = this.namespaceIndex.get(namespace);
441
+ if (!ids) return 0;
442
+
443
+ let deleted = 0;
444
+ for (const id of ids) {
445
+ if (await this.delete(id)) {
446
+ deleted++;
447
+ }
448
+ }
449
+
450
+ return deleted;
451
+ }
452
+
453
+ /**
454
+ * Get statistics
455
+ */
456
+ async getStats(): Promise<BackendStats> {
457
+ const entriesByNamespace: Record<string, number> = {};
458
+ for (const [namespace, ids] of this.namespaceIndex) {
459
+ entriesByNamespace[namespace] = ids.size;
460
+ }
461
+
462
+ const entriesByType: Record<MemoryType, number> = {
463
+ episodic: 0,
464
+ semantic: 0,
465
+ procedural: 0,
466
+ working: 0,
467
+ cache: 0,
468
+ };
469
+
470
+ for (const entry of this.entries.values()) {
471
+ entriesByType[entry.type]++;
472
+ }
473
+
474
+ // Get HNSW stats if available
475
+ let hnswStats: HNSWStats | undefined;
476
+ if (this.agentdb && HNSWIndex) {
477
+ try {
478
+ const hnsw = this.agentdb.getController('hnsw');
479
+ if (hnsw) {
480
+ const stats = hnsw.getStats();
481
+ hnswStats = {
482
+ vectorCount: stats.numElements || 0,
483
+ memoryUsage: 0,
484
+ avgSearchTime: stats.avgSearchTimeMs || 0,
485
+ buildTime: stats.lastBuildTime || 0,
486
+ compressionRatio: 1.0,
487
+ };
488
+ }
489
+ } catch {
490
+ // HNSW not available
491
+ }
492
+ }
493
+
494
+ return {
495
+ totalEntries: this.entries.size,
496
+ entriesByNamespace,
497
+ entriesByType,
498
+ memoryUsage: this.estimateMemoryUsage(),
499
+ hnswStats,
500
+ avgQueryTime:
501
+ this.stats.queryCount > 0
502
+ ? this.stats.totalQueryTime / this.stats.queryCount
503
+ : 0,
504
+ avgSearchTime:
505
+ this.stats.searchCount > 0
506
+ ? this.stats.totalSearchTime / this.stats.searchCount
507
+ : 0,
508
+ };
509
+ }
510
+
511
+ /**
512
+ * Health check
513
+ */
514
+ async healthCheck(): Promise<HealthCheckResult> {
515
+ const issues: string[] = [];
516
+ const recommendations: string[] = [];
517
+
518
+ // Check AgentDB availability
519
+ const storageHealth: ComponentHealth = this.agentdb
520
+ ? { status: 'healthy', latency: 0 }
521
+ : {
522
+ status: 'degraded',
523
+ latency: 0,
524
+ message: 'AgentDB not available, using fallback',
525
+ };
526
+
527
+ // Check index health
528
+ const indexHealth: ComponentHealth = { status: 'healthy', latency: 0 };
529
+ if (!this.agentdb) {
530
+ indexHealth.status = 'degraded';
531
+ indexHealth.message = 'HNSW index not available';
532
+ recommendations.push('Install agentdb for 150x-12,500x faster vector search');
533
+ }
534
+
535
+ // Check cache health
536
+ const cacheHealth: ComponentHealth = { status: 'healthy', latency: 0 };
537
+
538
+ const status =
539
+ storageHealth.status === 'unhealthy' || indexHealth.status === 'unhealthy'
540
+ ? 'unhealthy'
541
+ : storageHealth.status === 'degraded' || indexHealth.status === 'degraded'
542
+ ? 'degraded'
543
+ : 'healthy';
544
+
545
+ return {
546
+ status,
547
+ components: {
548
+ storage: storageHealth,
549
+ index: indexHealth,
550
+ cache: cacheHealth,
551
+ },
552
+ timestamp: Date.now(),
553
+ issues,
554
+ recommendations,
555
+ };
556
+ }
557
+
558
+ // ===== Private Methods =====
559
+
560
+ /**
561
+ * Create database schema
562
+ */
563
+ private async createSchema(): Promise<void> {
564
+ if (!this.agentdb) return;
565
+
566
+ const db = this.agentdb.database;
567
+ if (!db || typeof db.run !== 'function') {
568
+ // AgentDB doesn't expose raw database - using native API
569
+ return;
570
+ }
571
+
572
+ try {
573
+ // Create memory_entries table
574
+ await db.run(`
575
+ CREATE TABLE IF NOT EXISTS memory_entries (
576
+ id TEXT PRIMARY KEY,
577
+ key TEXT NOT NULL,
578
+ content TEXT NOT NULL,
579
+ embedding BLOB,
580
+ type TEXT NOT NULL,
581
+ namespace TEXT NOT NULL,
582
+ tags TEXT,
583
+ metadata TEXT,
584
+ owner_id TEXT,
585
+ access_level TEXT NOT NULL,
586
+ created_at INTEGER NOT NULL,
587
+ updated_at INTEGER NOT NULL,
588
+ expires_at INTEGER,
589
+ version INTEGER NOT NULL,
590
+ references TEXT,
591
+ access_count INTEGER DEFAULT 0,
592
+ last_accessed_at INTEGER
593
+ )
594
+ `);
595
+
596
+ // Create indexes
597
+ await db.run(
598
+ 'CREATE INDEX IF NOT EXISTS idx_namespace ON memory_entries(namespace)'
599
+ );
600
+ await db.run('CREATE INDEX IF NOT EXISTS idx_key ON memory_entries(key)');
601
+ await db.run('CREATE INDEX IF NOT EXISTS idx_type ON memory_entries(type)');
602
+ } catch {
603
+ // Schema creation failed - using in-memory only
604
+ }
605
+ }
606
+
607
+ /**
608
+ * Store entry in AgentDB
609
+ */
610
+ private async storeInAgentDB(entry: MemoryEntry): Promise<void> {
611
+ if (!this.agentdb) return;
612
+
613
+ // Try to use agentdb's native store method if available
614
+ try {
615
+ if (typeof this.agentdb.store === 'function') {
616
+ await this.agentdb.store(entry.id, {
617
+ key: entry.key,
618
+ content: entry.content,
619
+ embedding: entry.embedding,
620
+ type: entry.type,
621
+ namespace: entry.namespace,
622
+ tags: entry.tags,
623
+ metadata: entry.metadata,
624
+ });
625
+ return;
626
+ }
627
+
628
+ // Fallback: use database directly if available
629
+ const db = this.agentdb.database;
630
+ if (!db || typeof db.run !== 'function') {
631
+ // No compatible database interface - skip agentdb storage
632
+ // Entry is already stored in-memory
633
+ return;
634
+ }
635
+
636
+ await db.run(
637
+ `
638
+ INSERT OR REPLACE INTO memory_entries
639
+ (id, key, content, embedding, type, namespace, tags, metadata, owner_id,
640
+ access_level, created_at, updated_at, expires_at, version, references,
641
+ access_count, last_accessed_at)
642
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
643
+ `,
644
+ [
645
+ entry.id,
646
+ entry.key,
647
+ entry.content,
648
+ entry.embedding ? Buffer.from(entry.embedding.buffer) : null,
649
+ entry.type,
650
+ entry.namespace,
651
+ JSON.stringify(entry.tags),
652
+ JSON.stringify(entry.metadata),
653
+ entry.ownerId || null,
654
+ entry.accessLevel,
655
+ entry.createdAt,
656
+ entry.updatedAt,
657
+ entry.expiresAt || null,
658
+ entry.version,
659
+ JSON.stringify(entry.references),
660
+ entry.accessCount,
661
+ entry.lastAccessedAt,
662
+ ]
663
+ );
664
+ } catch {
665
+ // AgentDB storage failed - entry is already in-memory
666
+ }
667
+
668
+ // Add to vector index if HNSW is available
669
+ if (entry.embedding && HNSWIndex) {
670
+ try {
671
+ const hnsw = this.agentdb.getController('hnsw');
672
+ if (hnsw) {
673
+ // Convert string ID to number for HNSW (use hash)
674
+ const numericId = this.stringIdToNumeric(entry.id);
675
+ hnsw.addVector(numericId, entry.embedding);
676
+ }
677
+ } catch {
678
+ // HNSW not available
679
+ }
680
+ }
681
+ }
682
+
683
+ /**
684
+ * Get entry from AgentDB
685
+ */
686
+ private async getFromAgentDB(id: string): Promise<MemoryEntry | null> {
687
+ if (!this.agentdb) return null;
688
+
689
+ try {
690
+ // Try native get method first
691
+ if (typeof this.agentdb.get === 'function') {
692
+ const data = await this.agentdb.get(id);
693
+ if (data) return this.dataToEntry(id, data);
694
+ }
695
+
696
+ // Fallback to database
697
+ const db = this.agentdb.database;
698
+ if (!db || typeof db.get !== 'function') return null;
699
+
700
+ const row = await db.get('SELECT * FROM memory_entries WHERE id = ?', [id]);
701
+ if (!row) return null;
702
+ return this.rowToEntry(row);
703
+ } catch {
704
+ return null;
705
+ }
706
+ }
707
+
708
+ /**
709
+ * Convert agentdb data to MemoryEntry
710
+ */
711
+ private dataToEntry(id: string, data: any): MemoryEntry {
712
+ const now = Date.now();
713
+ return {
714
+ id,
715
+ key: data.key || id,
716
+ content: data.content || '',
717
+ embedding: data.embedding,
718
+ type: data.type || 'semantic',
719
+ namespace: data.namespace || this.config.namespace,
720
+ tags: data.tags || [],
721
+ metadata: data.metadata || {},
722
+ ownerId: data.ownerId,
723
+ accessLevel: data.accessLevel || 'private',
724
+ createdAt: data.createdAt || now,
725
+ updatedAt: data.updatedAt || now,
726
+ expiresAt: data.expiresAt,
727
+ version: data.version || 1,
728
+ references: data.references || [],
729
+ accessCount: data.accessCount || 0,
730
+ lastAccessedAt: data.lastAccessedAt || now,
731
+ };
732
+ }
733
+
734
+ /**
735
+ * Update entry in AgentDB
736
+ */
737
+ private async updateInAgentDB(entry: MemoryEntry): Promise<void> {
738
+ await this.storeInAgentDB(entry);
739
+ }
740
+
741
+ /**
742
+ * Delete entry from AgentDB
743
+ */
744
+ private async deleteFromAgentDB(id: string): Promise<void> {
745
+ if (!this.agentdb) return;
746
+
747
+ try {
748
+ // Try native delete method first
749
+ if (typeof this.agentdb.delete === 'function') {
750
+ await this.agentdb.delete(id);
751
+ return;
752
+ }
753
+
754
+ // Fallback to database
755
+ const db = this.agentdb.database;
756
+ if (!db || typeof db.run !== 'function') return;
757
+
758
+ await db.run('DELETE FROM memory_entries WHERE id = ?', [id]);
759
+ } catch {
760
+ // Delete failed - entry removed from in-memory
761
+ }
762
+ }
763
+
764
+ /**
765
+ * Search with AgentDB HNSW
766
+ */
767
+ private async searchWithAgentDB(
768
+ embedding: Float32Array,
769
+ options: SearchOptions
770
+ ): Promise<SearchResult[]> {
771
+ if (!this.agentdb || !HNSWIndex) {
772
+ return [];
773
+ }
774
+
775
+ try {
776
+ const hnsw = this.agentdb.getController('hnsw');
777
+ if (!hnsw) {
778
+ return this.bruteForceSearch(embedding, options);
779
+ }
780
+
781
+ const results = await hnsw.search(embedding, options.k, {
782
+ threshold: options.threshold,
783
+ });
784
+
785
+ const searchResults: SearchResult[] = [];
786
+
787
+ for (const result of results) {
788
+ const id = this.numericIdToString(result.id);
789
+ const entry = await this.get(id);
790
+ if (!entry) continue;
791
+
792
+ searchResults.push({
793
+ entry,
794
+ score: result.similarity,
795
+ distance: result.distance,
796
+ });
797
+ }
798
+
799
+ return searchResults;
800
+ } catch (error) {
801
+ console.error('HNSW search failed:', error);
802
+ return this.bruteForceSearch(embedding, options);
803
+ }
804
+ }
805
+
806
+ /**
807
+ * Brute-force vector search fallback
808
+ */
809
+ private bruteForceSearch(
810
+ embedding: Float32Array,
811
+ options: SearchOptions
812
+ ): SearchResult[] {
813
+ const results: SearchResult[] = [];
814
+
815
+ for (const entry of this.entries.values()) {
816
+ if (!entry.embedding) continue;
817
+
818
+ const score = this.cosineSimilarity(embedding, entry.embedding);
819
+ const distance = 1 - score;
820
+
821
+ if (options.threshold && score < options.threshold) continue;
822
+
823
+ results.push({ entry, score, distance });
824
+ }
825
+
826
+ // Sort by score descending
827
+ results.sort((a, b) => b.score - a.score);
828
+
829
+ return results.slice(0, options.k);
830
+ }
831
+
832
+ /**
833
+ * Semantic search helper
834
+ */
835
+ private async semanticSearch(query: MemoryQuery): Promise<SearchResult[]> {
836
+ let embedding = query.embedding;
837
+
838
+ if (!embedding && query.content && this.config.embeddingGenerator) {
839
+ embedding = await this.config.embeddingGenerator(query.content);
840
+ }
841
+
842
+ if (!embedding) {
843
+ return [];
844
+ }
845
+
846
+ return this.search(embedding, {
847
+ k: query.limit,
848
+ threshold: query.threshold,
849
+ filters: query,
850
+ });
851
+ }
852
+
853
+ /**
854
+ * In-memory query fallback
855
+ */
856
+ private queryInMemory(query: MemoryQuery): MemoryEntry[] {
857
+ let results = Array.from(this.entries.values());
858
+
859
+ // Apply filters
860
+ if (query.namespace) {
861
+ results = results.filter((e) => e.namespace === query.namespace);
862
+ }
863
+
864
+ if (query.key) {
865
+ results = results.filter((e) => e.key === query.key);
866
+ }
867
+
868
+ if (query.keyPrefix) {
869
+ results = results.filter((e) => e.key.startsWith(query.keyPrefix!));
870
+ }
871
+
872
+ if (query.tags && query.tags.length > 0) {
873
+ results = results.filter((e) =>
874
+ query.tags!.every((tag) => e.tags.includes(tag))
875
+ );
876
+ }
877
+
878
+ return results.slice(0, query.limit);
879
+ }
880
+
881
+ /**
882
+ * Update in-memory indexes
883
+ */
884
+ private updateIndexes(entry: MemoryEntry): void {
885
+ const namespace = entry.namespace;
886
+
887
+ if (!this.namespaceIndex.has(namespace)) {
888
+ this.namespaceIndex.set(namespace, new Set());
889
+ }
890
+ this.namespaceIndex.get(namespace)!.add(entry.id);
891
+
892
+ const keyIndexKey = `${namespace}:${entry.key}`;
893
+ this.keyIndex.set(keyIndexKey, entry.id);
894
+ }
895
+
896
+ /**
897
+ * Convert DB row to MemoryEntry
898
+ */
899
+ private rowToEntry(row: any): MemoryEntry {
900
+ return {
901
+ id: row.id,
902
+ key: row.key,
903
+ content: row.content,
904
+ embedding: row.embedding
905
+ ? new Float32Array(new Uint8Array(row.embedding).buffer)
906
+ : undefined,
907
+ type: row.type,
908
+ namespace: row.namespace,
909
+ tags: JSON.parse(row.tags || '[]'),
910
+ metadata: JSON.parse(row.metadata || '{}'),
911
+ ownerId: row.owner_id,
912
+ accessLevel: row.access_level,
913
+ createdAt: row.created_at,
914
+ updatedAt: row.updated_at,
915
+ expiresAt: row.expires_at,
916
+ version: row.version,
917
+ references: JSON.parse(row.references || '[]'),
918
+ accessCount: row.access_count || 0,
919
+ lastAccessedAt: row.last_accessed_at || row.created_at,
920
+ };
921
+ }
922
+
923
+ /**
924
+ * Convert string ID to numeric for HNSW
925
+ */
926
+ private stringIdToNumeric(id: string): number {
927
+ let hash = 0;
928
+ for (let i = 0; i < id.length; i++) {
929
+ hash = (hash << 5) - hash + id.charCodeAt(i);
930
+ hash |= 0;
931
+ }
932
+ return Math.abs(hash);
933
+ }
934
+
935
+ /**
936
+ * Convert numeric ID back to string using O(1) reverse lookup
937
+ * PERFORMANCE FIX: Uses pre-built reverse map instead of O(n) linear scan
938
+ */
939
+ private numericIdToString(numericId: number): string {
940
+ // Use O(1) reverse lookup map
941
+ const stringId = this.numericToStringIdMap.get(numericId);
942
+ if (stringId) {
943
+ return stringId;
944
+ }
945
+ // Fallback for unmapped IDs
946
+ return String(numericId);
947
+ }
948
+
949
+ /**
950
+ * Register string ID in reverse lookup map
951
+ * Called when storing entries to maintain bidirectional mapping
952
+ */
953
+ private registerIdMapping(stringId: string): void {
954
+ const numericId = this.stringIdToNumeric(stringId);
955
+ this.numericToStringIdMap.set(numericId, stringId);
956
+ }
957
+
958
+ /**
959
+ * Unregister string ID from reverse lookup map
960
+ * Called when deleting entries
961
+ */
962
+ private unregisterIdMapping(stringId: string): void {
963
+ const numericId = this.stringIdToNumeric(stringId);
964
+ this.numericToStringIdMap.delete(numericId);
965
+ }
966
+
967
+ /**
968
+ * Cosine similarity (returns value in range [0, 1] where 1 = identical)
969
+ */
970
+ private cosineSimilarity(a: Float32Array, b: Float32Array): number {
971
+ let dotProduct = 0;
972
+ let normA = 0;
973
+ let normB = 0;
974
+
975
+ for (let i = 0; i < a.length; i++) {
976
+ dotProduct += a[i] * b[i];
977
+ normA += a[i] * a[i];
978
+ normB += b[i] * b[i];
979
+ }
980
+
981
+ const magnitude = Math.sqrt(normA) * Math.sqrt(normB);
982
+ return magnitude === 0 ? 0 : dotProduct / magnitude;
983
+ }
984
+
985
+ /**
986
+ * Estimate memory usage
987
+ */
988
+ private estimateMemoryUsage(): number {
989
+ let total = 0;
990
+
991
+ for (const entry of this.entries.values()) {
992
+ total += entry.content.length * 2;
993
+ if (entry.embedding) {
994
+ total += entry.embedding.length * 4;
995
+ }
996
+ }
997
+
998
+ return total;
999
+ }
1000
+
1001
+ /**
1002
+ * Check if AgentDB is available
1003
+ */
1004
+ isAvailable(): boolean {
1005
+ return this.available;
1006
+ }
1007
+
1008
+ /**
1009
+ * Get underlying AgentDB instance
1010
+ */
1011
+ getAgentDB(): any {
1012
+ return this.agentdb;
1013
+ }
1014
+ }
1015
+
1016
+ export default AgentDBBackend;