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