@claude-flow/memory 3.0.0-alpha.1 → 3.0.0-alpha.11

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 (163) hide show
  1. package/README.md +356 -18
  2. package/dist/agent-memory-scope.d.ts +131 -0
  3. package/dist/agent-memory-scope.d.ts.map +1 -0
  4. package/dist/agent-memory-scope.js +215 -0
  5. package/dist/agent-memory-scope.js.map +1 -0
  6. package/dist/agent-memory-scope.test.d.ts +8 -0
  7. package/dist/agent-memory-scope.test.d.ts.map +1 -0
  8. package/dist/agent-memory-scope.test.js +463 -0
  9. package/dist/agent-memory-scope.test.js.map +1 -0
  10. package/dist/agentdb-adapter.d.ts +22 -3
  11. package/dist/agentdb-adapter.d.ts.map +1 -1
  12. package/dist/agentdb-adapter.js +135 -8
  13. package/dist/agentdb-adapter.js.map +1 -1
  14. package/dist/auto-memory-bridge.d.ts +226 -0
  15. package/dist/auto-memory-bridge.d.ts.map +1 -0
  16. package/dist/auto-memory-bridge.js +709 -0
  17. package/dist/auto-memory-bridge.js.map +1 -0
  18. package/dist/auto-memory-bridge.test.d.ts +8 -0
  19. package/dist/auto-memory-bridge.test.d.ts.map +1 -0
  20. package/dist/auto-memory-bridge.test.js +754 -0
  21. package/dist/auto-memory-bridge.test.js.map +1 -0
  22. package/dist/benchmark.test.d.ts +2 -0
  23. package/dist/benchmark.test.d.ts.map +1 -0
  24. package/dist/benchmark.test.js +277 -0
  25. package/dist/benchmark.test.js.map +1 -0
  26. package/dist/hybrid-backend.d.ts.map +1 -1
  27. package/dist/hybrid-backend.js +29 -4
  28. package/dist/hybrid-backend.js.map +1 -1
  29. package/dist/index.d.ts +8 -0
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +8 -0
  32. package/dist/index.js.map +1 -1
  33. package/dist/learning-bridge.d.ts +137 -0
  34. package/dist/learning-bridge.d.ts.map +1 -0
  35. package/dist/learning-bridge.js +335 -0
  36. package/dist/learning-bridge.js.map +1 -0
  37. package/dist/learning-bridge.test.d.ts +8 -0
  38. package/dist/learning-bridge.test.d.ts.map +1 -0
  39. package/dist/learning-bridge.test.js +578 -0
  40. package/dist/learning-bridge.test.js.map +1 -0
  41. package/dist/memory-graph.d.ts +100 -0
  42. package/dist/memory-graph.d.ts.map +1 -0
  43. package/dist/memory-graph.js +333 -0
  44. package/dist/memory-graph.js.map +1 -0
  45. package/dist/memory-graph.test.d.ts +8 -0
  46. package/dist/memory-graph.test.d.ts.map +1 -0
  47. package/dist/memory-graph.test.js +609 -0
  48. package/dist/memory-graph.test.js.map +1 -0
  49. package/dist/sqlite-backend.js +3 -3
  50. package/dist/sqljs-backend.d.ts.map +1 -1
  51. package/dist/sqljs-backend.js +5 -2
  52. package/dist/sqljs-backend.js.map +1 -1
  53. package/dist/types.d.ts +3 -0
  54. package/dist/types.d.ts.map +1 -1
  55. package/package.json +20 -5
  56. package/.agentic-flow/intelligence.json +0 -16
  57. package/__tests__/coverage/base.css +0 -224
  58. package/__tests__/coverage/block-navigation.js +0 -87
  59. package/__tests__/coverage/coverage-final.json +0 -19
  60. package/__tests__/coverage/favicon.png +0 -0
  61. package/__tests__/coverage/index.html +0 -206
  62. package/__tests__/coverage/lcov-report/base.css +0 -224
  63. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  64. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  65. package/__tests__/coverage/lcov-report/index.html +0 -206
  66. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  67. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  68. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  69. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  70. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +0 -2737
  71. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +0 -3130
  72. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +0 -601
  73. package/__tests__/coverage/lcov-report/src/application/commands/index.html +0 -131
  74. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +0 -394
  75. package/__tests__/coverage/lcov-report/src/application/queries/index.html +0 -116
  76. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +0 -796
  77. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  78. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +0 -793
  79. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +0 -1633
  80. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +0 -1618
  81. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  82. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +0 -952
  83. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  84. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +0 -1294
  85. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +0 -3124
  86. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +0 -2167
  87. package/__tests__/coverage/lcov-report/src/index.html +0 -266
  88. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  89. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +0 -116
  90. package/__tests__/coverage/lcov-report/src/migration.ts.html +0 -2092
  91. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +0 -1711
  92. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +0 -2281
  93. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +0 -2374
  94. package/__tests__/coverage/lcov-report/src/types.ts.html +0 -2266
  95. package/__tests__/coverage/lcov.info +0 -10238
  96. package/__tests__/coverage/prettify.css +0 -1
  97. package/__tests__/coverage/prettify.js +0 -2
  98. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  99. package/__tests__/coverage/sorter.js +0 -210
  100. package/__tests__/coverage/src/agentdb-adapter.ts.html +0 -2737
  101. package/__tests__/coverage/src/agentdb-backend.ts.html +0 -3130
  102. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +0 -601
  103. package/__tests__/coverage/src/application/commands/index.html +0 -131
  104. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +0 -394
  105. package/__tests__/coverage/src/application/queries/index.html +0 -116
  106. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +0 -796
  107. package/__tests__/coverage/src/application/services/index.html +0 -116
  108. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +0 -793
  109. package/__tests__/coverage/src/cache-manager.ts.html +0 -1633
  110. package/__tests__/coverage/src/database-provider.ts.html +0 -1618
  111. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  112. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +0 -952
  113. package/__tests__/coverage/src/domain/services/index.html +0 -116
  114. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +0 -1294
  115. package/__tests__/coverage/src/hnsw-index.ts.html +0 -3124
  116. package/__tests__/coverage/src/hybrid-backend.ts.html +0 -2167
  117. package/__tests__/coverage/src/index.html +0 -266
  118. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  119. package/__tests__/coverage/src/infrastructure/repositories/index.html +0 -116
  120. package/__tests__/coverage/src/migration.ts.html +0 -2092
  121. package/__tests__/coverage/src/query-builder.ts.html +0 -1711
  122. package/__tests__/coverage/src/sqlite-backend.ts.html +0 -2281
  123. package/__tests__/coverage/src/sqljs-backend.ts.html +0 -2374
  124. package/__tests__/coverage/src/types.ts.html +0 -2266
  125. package/benchmarks/cache-hit-rate.bench.ts +0 -535
  126. package/benchmarks/hnsw-indexing.bench.ts +0 -552
  127. package/benchmarks/memory-write.bench.ts +0 -469
  128. package/benchmarks/vector-search.bench.ts +0 -449
  129. package/docs/AGENTDB-INTEGRATION.md +0 -388
  130. package/docs/CROSS_PLATFORM.md +0 -505
  131. package/docs/WINDOWS_SUPPORT.md +0 -422
  132. package/examples/agentdb-example.ts +0 -345
  133. package/examples/cross-platform-usage.ts +0 -326
  134. package/framework/benchmark.ts +0 -112
  135. package/src/agentdb-adapter.ts +0 -884
  136. package/src/agentdb-backend.test.ts +0 -339
  137. package/src/agentdb-backend.ts +0 -1016
  138. package/src/application/commands/delete-memory.command.ts +0 -172
  139. package/src/application/commands/store-memory.command.ts +0 -103
  140. package/src/application/index.ts +0 -36
  141. package/src/application/queries/search-memory.query.ts +0 -237
  142. package/src/application/services/memory-application-service.ts +0 -236
  143. package/src/cache-manager.ts +0 -516
  144. package/src/database-provider.test.ts +0 -364
  145. package/src/database-provider.ts +0 -511
  146. package/src/domain/entities/memory-entry.ts +0 -289
  147. package/src/domain/index.ts +0 -35
  148. package/src/domain/repositories/memory-repository.interface.ts +0 -120
  149. package/src/domain/services/memory-domain-service.ts +0 -403
  150. package/src/hnsw-index.ts +0 -1013
  151. package/src/hybrid-backend.test.ts +0 -399
  152. package/src/hybrid-backend.ts +0 -694
  153. package/src/index.ts +0 -515
  154. package/src/infrastructure/index.ts +0 -23
  155. package/src/infrastructure/repositories/hybrid-memory-repository.ts +0 -516
  156. package/src/migration.ts +0 -669
  157. package/src/query-builder.ts +0 -542
  158. package/src/sqlite-backend.ts +0 -732
  159. package/src/sqljs-backend.ts +0 -763
  160. package/src/types.ts +0 -727
  161. package/tsconfig.json +0 -9
  162. package/tsconfig.tsbuildinfo +0 -1
  163. package/verify-cross-platform.ts +0 -170
@@ -1,732 +0,0 @@
1
- /**
2
- * SQLite Memory Backend
3
- *
4
- * Provides structured storage for memory entries using SQLite.
5
- * Optimized for ACID transactions, exact matches, and complex queries.
6
- * Part of ADR-009: Hybrid Memory Backend (SQLite + AgentDB)
7
- *
8
- * @module v3/memory/sqlite-backend
9
- */
10
-
11
- import { EventEmitter } from 'node:events';
12
- import Database from 'better-sqlite3';
13
- import {
14
- IMemoryBackend,
15
- MemoryEntry,
16
- MemoryEntryInput,
17
- MemoryEntryUpdate,
18
- MemoryQuery,
19
- SearchOptions,
20
- SearchResult,
21
- BackendStats,
22
- HealthCheckResult,
23
- ComponentHealth,
24
- MemoryType,
25
- EmbeddingGenerator,
26
- generateMemoryId,
27
- createDefaultEntry,
28
- } from './types.js';
29
-
30
- /**
31
- * Configuration for SQLite Backend
32
- */
33
- export interface SQLiteBackendConfig {
34
- /** Path to SQLite database file (:memory: for in-memory) */
35
- databasePath: string;
36
-
37
- /** Enable WAL mode for better concurrency */
38
- walMode: boolean;
39
-
40
- /** Enable query optimization */
41
- optimize: boolean;
42
-
43
- /** Default namespace */
44
- defaultNamespace: string;
45
-
46
- /** Embedding generator (for compatibility with hybrid mode) */
47
- embeddingGenerator?: EmbeddingGenerator;
48
-
49
- /** Maximum entries before auto-cleanup */
50
- maxEntries: number;
51
-
52
- /** Enable verbose logging */
53
- verbose: boolean;
54
- }
55
-
56
- /**
57
- * Default configuration values
58
- */
59
- const DEFAULT_CONFIG: SQLiteBackendConfig = {
60
- databasePath: ':memory:',
61
- walMode: true,
62
- optimize: true,
63
- defaultNamespace: 'default',
64
- maxEntries: 1000000,
65
- verbose: false,
66
- };
67
-
68
- /**
69
- * SQLite Backend for Structured Memory Storage
70
- *
71
- * Provides:
72
- * - ACID transactions for data consistency
73
- * - Efficient indexing for exact matches and prefix queries
74
- * - Full-text search capabilities
75
- * - Complex SQL queries with joins and aggregations
76
- * - Persistent storage with WAL mode
77
- */
78
- export class SQLiteBackend extends EventEmitter implements IMemoryBackend {
79
- private config: SQLiteBackendConfig;
80
- private db: Database.Database | null = null;
81
- private initialized: boolean = false;
82
-
83
- // Performance tracking
84
- private stats = {
85
- queryCount: 0,
86
- totalQueryTime: 0,
87
- writeCount: 0,
88
- totalWriteTime: 0,
89
- };
90
-
91
- constructor(config: Partial<SQLiteBackendConfig> = {}) {
92
- super();
93
- this.config = { ...DEFAULT_CONFIG, ...config };
94
- }
95
-
96
- /**
97
- * Initialize the SQLite backend
98
- */
99
- async initialize(): Promise<void> {
100
- if (this.initialized) return;
101
-
102
- // Open database connection
103
- this.db = new Database(this.config.databasePath, {
104
- verbose: this.config.verbose ? console.log : undefined,
105
- });
106
-
107
- // Enable WAL mode for better concurrency
108
- if (this.config.walMode) {
109
- this.db.pragma('journal_mode = WAL');
110
- }
111
-
112
- // Performance optimizations
113
- if (this.config.optimize) {
114
- this.db.pragma('synchronous = NORMAL');
115
- this.db.pragma('cache_size = 10000');
116
- this.db.pragma('temp_store = MEMORY');
117
- }
118
-
119
- // Create schema
120
- this.createSchema();
121
-
122
- this.initialized = true;
123
- this.emit('initialized');
124
- }
125
-
126
- /**
127
- * Shutdown the backend
128
- */
129
- async shutdown(): Promise<void> {
130
- if (!this.initialized || !this.db) return;
131
-
132
- // Optimize database before closing
133
- if (this.config.optimize) {
134
- this.db.pragma('optimize');
135
- }
136
-
137
- this.db.close();
138
- this.db = null;
139
- this.initialized = false;
140
- this.emit('shutdown');
141
- }
142
-
143
- /**
144
- * Store a memory entry
145
- */
146
- async store(entry: MemoryEntry): Promise<void> {
147
- this.ensureInitialized();
148
- const startTime = performance.now();
149
-
150
- const stmt = this.db!.prepare(`
151
- INSERT OR REPLACE INTO memory_entries (
152
- id, key, content, type, namespace, tags, metadata,
153
- owner_id, access_level, created_at, updated_at, expires_at,
154
- version, references, access_count, last_accessed_at
155
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
156
- `);
157
-
158
- stmt.run(
159
- entry.id,
160
- entry.key,
161
- entry.content,
162
- entry.type,
163
- entry.namespace,
164
- JSON.stringify(entry.tags),
165
- JSON.stringify(entry.metadata),
166
- entry.ownerId || null,
167
- entry.accessLevel,
168
- entry.createdAt,
169
- entry.updatedAt,
170
- entry.expiresAt || null,
171
- entry.version,
172
- JSON.stringify(entry.references),
173
- entry.accessCount,
174
- entry.lastAccessedAt
175
- );
176
-
177
- // Store embedding separately (as BLOB)
178
- if (entry.embedding) {
179
- const embeddingStmt = this.db!.prepare(`
180
- INSERT OR REPLACE INTO memory_embeddings (entry_id, embedding)
181
- VALUES (?, ?)
182
- `);
183
- embeddingStmt.run(entry.id, Buffer.from(entry.embedding.buffer));
184
- }
185
-
186
- const duration = performance.now() - startTime;
187
- this.stats.writeCount++;
188
- this.stats.totalWriteTime += duration;
189
-
190
- this.emit('entry:stored', { id: entry.id, duration });
191
- }
192
-
193
- /**
194
- * Get a memory entry by ID
195
- */
196
- async get(id: string): Promise<MemoryEntry | null> {
197
- this.ensureInitialized();
198
-
199
- const stmt = this.db!.prepare('SELECT * FROM memory_entries WHERE id = ?');
200
- const row = stmt.get(id) as any;
201
-
202
- if (!row) return null;
203
-
204
- return this.rowToEntry(row);
205
- }
206
-
207
- /**
208
- * Get a memory entry by key within a namespace
209
- */
210
- async getByKey(namespace: string, key: string): Promise<MemoryEntry | null> {
211
- this.ensureInitialized();
212
-
213
- const stmt = this.db!.prepare(`
214
- SELECT * FROM memory_entries
215
- WHERE namespace = ? AND key = ?
216
- `);
217
- const row = stmt.get(namespace, key) as any;
218
-
219
- if (!row) return null;
220
-
221
- return this.rowToEntry(row);
222
- }
223
-
224
- /**
225
- * Update a memory entry
226
- */
227
- async update(id: string, update: MemoryEntryUpdate): Promise<MemoryEntry | null> {
228
- this.ensureInitialized();
229
-
230
- const entry = await this.get(id);
231
- if (!entry) return null;
232
-
233
- // Apply updates
234
- if (update.content !== undefined) entry.content = update.content;
235
- if (update.tags !== undefined) entry.tags = update.tags;
236
- if (update.metadata !== undefined) {
237
- entry.metadata = { ...entry.metadata, ...update.metadata };
238
- }
239
- if (update.accessLevel !== undefined) entry.accessLevel = update.accessLevel;
240
- if (update.expiresAt !== undefined) entry.expiresAt = update.expiresAt;
241
- if (update.references !== undefined) entry.references = update.references;
242
-
243
- entry.updatedAt = Date.now();
244
- entry.version++;
245
-
246
- // Store updated entry
247
- await this.store(entry);
248
-
249
- this.emit('entry:updated', { id });
250
- return entry;
251
- }
252
-
253
- /**
254
- * Delete a memory entry
255
- */
256
- async delete(id: string): Promise<boolean> {
257
- this.ensureInitialized();
258
-
259
- const deleteEntry = this.db!.prepare('DELETE FROM memory_entries WHERE id = ?');
260
- const deleteEmbedding = this.db!.prepare('DELETE FROM memory_embeddings WHERE entry_id = ?');
261
-
262
- const result = deleteEntry.run(id);
263
- deleteEmbedding.run(id);
264
-
265
- if (result.changes > 0) {
266
- this.emit('entry:deleted', { id });
267
- return true;
268
- }
269
-
270
- return false;
271
- }
272
-
273
- /**
274
- * Query memory entries with filters
275
- */
276
- async query(query: MemoryQuery): Promise<MemoryEntry[]> {
277
- this.ensureInitialized();
278
- const startTime = performance.now();
279
-
280
- let sql = 'SELECT * FROM memory_entries WHERE 1=1';
281
- const params: any[] = [];
282
-
283
- // Build WHERE clauses
284
- if (query.namespace) {
285
- sql += ' AND namespace = ?';
286
- params.push(query.namespace);
287
- }
288
-
289
- if (query.key) {
290
- sql += ' AND key = ?';
291
- params.push(query.key);
292
- }
293
-
294
- if (query.keyPrefix) {
295
- sql += ' AND key LIKE ?';
296
- params.push(`${query.keyPrefix}%`);
297
- }
298
-
299
- if (query.memoryType) {
300
- sql += ' AND type = ?';
301
- params.push(query.memoryType);
302
- }
303
-
304
- if (query.accessLevel) {
305
- sql += ' AND access_level = ?';
306
- params.push(query.accessLevel);
307
- }
308
-
309
- if (query.ownerId) {
310
- sql += ' AND owner_id = ?';
311
- params.push(query.ownerId);
312
- }
313
-
314
- if (query.createdAfter) {
315
- sql += ' AND created_at >= ?';
316
- params.push(query.createdAfter);
317
- }
318
-
319
- if (query.createdBefore) {
320
- sql += ' AND created_at <= ?';
321
- params.push(query.createdBefore);
322
- }
323
-
324
- if (query.updatedAfter) {
325
- sql += ' AND updated_at >= ?';
326
- params.push(query.updatedAfter);
327
- }
328
-
329
- if (query.updatedBefore) {
330
- sql += ' AND updated_at <= ?';
331
- params.push(query.updatedBefore);
332
- }
333
-
334
- if (!query.includeExpired) {
335
- sql += ' AND (expires_at IS NULL OR expires_at > ?)';
336
- params.push(Date.now());
337
- }
338
-
339
- // Tag filtering (safe parameterized query)
340
- if (query.tags && query.tags.length > 0) {
341
- // Validate tags before using in query
342
- for (const tag of query.tags) {
343
- if (typeof tag !== 'string' || !/^[a-zA-Z0-9_\-.:]+$/.test(tag)) {
344
- throw new Error(`Invalid tag format: ${tag}`);
345
- }
346
- }
347
- // Use parameterized query with JSON functions
348
- const tagPlaceholders = query.tags.map(() => '?').join(', ');
349
- sql += ` AND EXISTS (
350
- SELECT 1 FROM json_each(tags) AS t
351
- WHERE t.value IN (${tagPlaceholders})
352
- )`;
353
- params.push(...query.tags);
354
- }
355
-
356
- // Pagination
357
- sql += ' ORDER BY created_at DESC';
358
- if (query.limit) {
359
- sql += ' LIMIT ?';
360
- params.push(query.limit);
361
- }
362
- if (query.offset) {
363
- sql += ' OFFSET ?';
364
- params.push(query.offset);
365
- }
366
-
367
- const stmt = this.db!.prepare(sql);
368
- const rows = stmt.all(...params) as any[];
369
-
370
- const results = rows.map((row) => this.rowToEntry(row));
371
-
372
- const duration = performance.now() - startTime;
373
- this.stats.queryCount++;
374
- this.stats.totalQueryTime += duration;
375
-
376
- return results;
377
- }
378
-
379
- /**
380
- * Semantic vector search (not optimized for SQLite, returns empty)
381
- * Use HybridBackend for semantic search with AgentDB
382
- */
383
- async search(
384
- embedding: Float32Array,
385
- options: SearchOptions
386
- ): Promise<SearchResult[]> {
387
- // SQLite is not optimized for vector search
388
- // This method returns empty to encourage use of HybridBackend
389
- console.warn(
390
- 'SQLiteBackend.search(): Vector search not optimized. Use HybridBackend for semantic search.'
391
- );
392
- return [];
393
- }
394
-
395
- /**
396
- * Bulk insert entries
397
- */
398
- async bulkInsert(entries: MemoryEntry[]): Promise<void> {
399
- this.ensureInitialized();
400
-
401
- const transaction = this.db!.transaction((entries: MemoryEntry[]) => {
402
- for (const entry of entries) {
403
- this.storeSync(entry);
404
- }
405
- });
406
-
407
- transaction(entries);
408
- this.emit('bulk:inserted', { count: entries.length });
409
- }
410
-
411
- /**
412
- * Bulk delete entries
413
- */
414
- async bulkDelete(ids: string[]): Promise<number> {
415
- this.ensureInitialized();
416
-
417
- const deleteEntry = this.db!.prepare('DELETE FROM memory_entries WHERE id = ?');
418
- const deleteEmbedding = this.db!.prepare('DELETE FROM memory_embeddings WHERE entry_id = ?');
419
-
420
- const transaction = this.db!.transaction((ids: string[]) => {
421
- let deleted = 0;
422
- for (const id of ids) {
423
- const result = deleteEntry.run(id);
424
- deleteEmbedding.run(id);
425
- if (result.changes > 0) deleted++;
426
- }
427
- return deleted;
428
- });
429
-
430
- return transaction(ids);
431
- }
432
-
433
- /**
434
- * Get entry count
435
- */
436
- async count(namespace?: string): Promise<number> {
437
- this.ensureInitialized();
438
-
439
- let sql = 'SELECT COUNT(*) as count FROM memory_entries';
440
- const params: any[] = [];
441
-
442
- if (namespace) {
443
- sql += ' WHERE namespace = ?';
444
- params.push(namespace);
445
- }
446
-
447
- const stmt = this.db!.prepare(sql);
448
- const result = stmt.get(...params) as any;
449
- return result.count;
450
- }
451
-
452
- /**
453
- * List all namespaces
454
- */
455
- async listNamespaces(): Promise<string[]> {
456
- this.ensureInitialized();
457
-
458
- const stmt = this.db!.prepare('SELECT DISTINCT namespace FROM memory_entries');
459
- const rows = stmt.all() as any[];
460
- return rows.map((row) => row.namespace);
461
- }
462
-
463
- /**
464
- * Clear all entries in a namespace
465
- */
466
- async clearNamespace(namespace: string): Promise<number> {
467
- this.ensureInitialized();
468
-
469
- const deleteEntries = this.db!.prepare('DELETE FROM memory_entries WHERE namespace = ?');
470
- const result = deleteEntries.run(namespace);
471
-
472
- // Clean up orphaned embeddings
473
- this.db!.prepare(`
474
- DELETE FROM memory_embeddings
475
- WHERE entry_id NOT IN (SELECT id FROM memory_entries)
476
- `).run();
477
-
478
- return result.changes;
479
- }
480
-
481
- /**
482
- * Get backend statistics
483
- */
484
- async getStats(): Promise<BackendStats> {
485
- this.ensureInitialized();
486
-
487
- // Count by namespace
488
- const namespaceStmt = this.db!.prepare(`
489
- SELECT namespace, COUNT(*) as count
490
- FROM memory_entries
491
- GROUP BY namespace
492
- `);
493
- const namespaceRows = namespaceStmt.all() as any[];
494
- const entriesByNamespace: Record<string, number> = {};
495
- for (const row of namespaceRows) {
496
- entriesByNamespace[row.namespace] = row.count;
497
- }
498
-
499
- // Count by type
500
- const typeStmt = this.db!.prepare(`
501
- SELECT type, COUNT(*) as count
502
- FROM memory_entries
503
- GROUP BY type
504
- `);
505
- const typeRows = typeStmt.all() as any[];
506
- const entriesByType: Record<MemoryType, number> = {
507
- episodic: 0,
508
- semantic: 0,
509
- procedural: 0,
510
- working: 0,
511
- cache: 0,
512
- };
513
- for (const row of typeRows) {
514
- entriesByType[row.type as MemoryType] = row.count;
515
- }
516
-
517
- // Get database size
518
- const pageCount = this.db!.pragma('page_count', { simple: true }) as number;
519
- const pageSize = this.db!.pragma('page_size', { simple: true }) as number;
520
- const memoryUsage = pageCount * pageSize;
521
-
522
- const totalEntries = await this.count();
523
-
524
- return {
525
- totalEntries,
526
- entriesByNamespace,
527
- entriesByType,
528
- memoryUsage,
529
- avgQueryTime:
530
- this.stats.queryCount > 0
531
- ? this.stats.totalQueryTime / this.stats.queryCount
532
- : 0,
533
- avgSearchTime: 0, // Not applicable for SQLite
534
- };
535
- }
536
-
537
- /**
538
- * Perform health check
539
- */
540
- async healthCheck(): Promise<HealthCheckResult> {
541
- const issues: string[] = [];
542
- const recommendations: string[] = [];
543
-
544
- if (!this.initialized || !this.db) {
545
- return {
546
- status: 'unhealthy',
547
- components: {
548
- storage: { status: 'unhealthy', latency: 0, message: 'Not initialized' },
549
- index: { status: 'healthy', latency: 0 },
550
- cache: { status: 'healthy', latency: 0 },
551
- },
552
- timestamp: Date.now(),
553
- issues: ['Backend not initialized'],
554
- recommendations: ['Call initialize() before using'],
555
- };
556
- }
557
-
558
- // Check database integrity
559
- let storageHealth: ComponentHealth;
560
- try {
561
- const integrityCheck = this.db.pragma('integrity_check', { simple: true });
562
- if (integrityCheck === 'ok') {
563
- storageHealth = { status: 'healthy', latency: 0 };
564
- } else {
565
- issues.push('Database integrity check failed');
566
- recommendations.push('Run VACUUM to repair database');
567
- storageHealth = { status: 'unhealthy', latency: 0, message: 'Integrity check failed' };
568
- }
569
- } catch (error) {
570
- issues.push('Failed to check database integrity');
571
- storageHealth = { status: 'unhealthy', latency: 0, message: String(error) };
572
- }
573
-
574
- // Check utilization
575
- const totalEntries = await this.count();
576
- const utilizationPercent = (totalEntries / this.config.maxEntries) * 100;
577
-
578
- if (utilizationPercent > 95) {
579
- issues.push('Storage utilization critical (>95%)');
580
- recommendations.push('Cleanup old data or increase maxEntries');
581
- storageHealth = { status: 'unhealthy', latency: 0, message: 'Near capacity' };
582
- } else if (utilizationPercent > 80) {
583
- issues.push('Storage utilization high (>80%)');
584
- recommendations.push('Consider cleanup');
585
- if (storageHealth.status === 'healthy') {
586
- storageHealth = { status: 'degraded', latency: 0, message: 'High utilization' };
587
- }
588
- }
589
-
590
- const status =
591
- storageHealth.status === 'unhealthy'
592
- ? 'unhealthy'
593
- : storageHealth.status === 'degraded'
594
- ? 'degraded'
595
- : 'healthy';
596
-
597
- return {
598
- status,
599
- components: {
600
- storage: storageHealth,
601
- index: { status: 'healthy', latency: 0 },
602
- cache: { status: 'healthy', latency: 0 },
603
- },
604
- timestamp: Date.now(),
605
- issues,
606
- recommendations,
607
- };
608
- }
609
-
610
- // ===== Private Methods =====
611
-
612
- private ensureInitialized(): void {
613
- if (!this.initialized || !this.db) {
614
- throw new Error('SQLiteBackend not initialized. Call initialize() first.');
615
- }
616
- }
617
-
618
- private createSchema(): void {
619
- if (!this.db) return;
620
-
621
- // Main entries table
622
- this.db.exec(`
623
- CREATE TABLE IF NOT EXISTS memory_entries (
624
- id TEXT PRIMARY KEY,
625
- key TEXT NOT NULL,
626
- content TEXT NOT NULL,
627
- type TEXT NOT NULL,
628
- namespace TEXT NOT NULL,
629
- tags TEXT NOT NULL,
630
- metadata TEXT NOT NULL,
631
- owner_id TEXT,
632
- access_level TEXT NOT NULL,
633
- created_at INTEGER NOT NULL,
634
- updated_at INTEGER NOT NULL,
635
- expires_at INTEGER,
636
- version INTEGER NOT NULL,
637
- references TEXT NOT NULL,
638
- access_count INTEGER NOT NULL,
639
- last_accessed_at INTEGER NOT NULL
640
- );
641
-
642
- CREATE INDEX IF NOT EXISTS idx_namespace ON memory_entries(namespace);
643
- CREATE INDEX IF NOT EXISTS idx_key ON memory_entries(key);
644
- CREATE INDEX IF NOT EXISTS idx_namespace_key ON memory_entries(namespace, key);
645
- CREATE INDEX IF NOT EXISTS idx_type ON memory_entries(type);
646
- CREATE INDEX IF NOT EXISTS idx_owner_id ON memory_entries(owner_id);
647
- CREATE INDEX IF NOT EXISTS idx_created_at ON memory_entries(created_at);
648
- CREATE INDEX IF NOT EXISTS idx_updated_at ON memory_entries(updated_at);
649
- CREATE INDEX IF NOT EXISTS idx_expires_at ON memory_entries(expires_at);
650
-
651
- CREATE TABLE IF NOT EXISTS memory_embeddings (
652
- entry_id TEXT PRIMARY KEY,
653
- embedding BLOB,
654
- FOREIGN KEY (entry_id) REFERENCES memory_entries(id) ON DELETE CASCADE
655
- );
656
- `);
657
- }
658
-
659
- private rowToEntry(row: any): MemoryEntry {
660
- // Get embedding if exists
661
- let embedding: Float32Array | undefined;
662
- const embeddingStmt = this.db!.prepare(
663
- 'SELECT embedding FROM memory_embeddings WHERE entry_id = ?'
664
- );
665
- const embeddingRow = embeddingStmt.get(row.id) as any;
666
- if (embeddingRow && embeddingRow.embedding) {
667
- embedding = new Float32Array(embeddingRow.embedding.buffer);
668
- }
669
-
670
- return {
671
- id: row.id,
672
- key: row.key,
673
- content: row.content,
674
- embedding,
675
- type: row.type as MemoryType,
676
- namespace: row.namespace,
677
- tags: JSON.parse(row.tags),
678
- metadata: JSON.parse(row.metadata),
679
- ownerId: row.owner_id,
680
- accessLevel: row.access_level,
681
- createdAt: row.created_at,
682
- updatedAt: row.updated_at,
683
- expiresAt: row.expires_at,
684
- version: row.version,
685
- references: JSON.parse(row.references),
686
- accessCount: row.access_count,
687
- lastAccessedAt: row.last_accessed_at,
688
- };
689
- }
690
-
691
- /**
692
- * Synchronous store for use in transactions
693
- */
694
- private storeSync(entry: MemoryEntry): void {
695
- const stmt = this.db!.prepare(`
696
- INSERT OR REPLACE INTO memory_entries (
697
- id, key, content, type, namespace, tags, metadata,
698
- owner_id, access_level, created_at, updated_at, expires_at,
699
- version, references, access_count, last_accessed_at
700
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
701
- `);
702
-
703
- stmt.run(
704
- entry.id,
705
- entry.key,
706
- entry.content,
707
- entry.type,
708
- entry.namespace,
709
- JSON.stringify(entry.tags),
710
- JSON.stringify(entry.metadata),
711
- entry.ownerId || null,
712
- entry.accessLevel,
713
- entry.createdAt,
714
- entry.updatedAt,
715
- entry.expiresAt || null,
716
- entry.version,
717
- JSON.stringify(entry.references),
718
- entry.accessCount,
719
- entry.lastAccessedAt
720
- );
721
-
722
- if (entry.embedding) {
723
- const embeddingStmt = this.db!.prepare(`
724
- INSERT OR REPLACE INTO memory_embeddings (entry_id, embedding)
725
- VALUES (?, ?)
726
- `);
727
- embeddingStmt.run(entry.id, Buffer.from(entry.embedding.buffer));
728
- }
729
- }
730
- }
731
-
732
- export default SQLiteBackend;