moflo 4.0.2 → 4.0.4

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