@claude-flow/memory 3.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/.agentic-flow/intelligence.json +16 -0
  2. package/README.md +249 -0
  3. package/__tests__/coverage/base.css +224 -0
  4. package/__tests__/coverage/block-navigation.js +87 -0
  5. package/__tests__/coverage/coverage-final.json +19 -0
  6. package/__tests__/coverage/favicon.png +0 -0
  7. package/__tests__/coverage/index.html +206 -0
  8. package/__tests__/coverage/lcov-report/base.css +224 -0
  9. package/__tests__/coverage/lcov-report/block-navigation.js +87 -0
  10. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  11. package/__tests__/coverage/lcov-report/index.html +206 -0
  12. package/__tests__/coverage/lcov-report/prettify.css +1 -0
  13. package/__tests__/coverage/lcov-report/prettify.js +2 -0
  14. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  15. package/__tests__/coverage/lcov-report/sorter.js +210 -0
  16. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +2737 -0
  17. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +3130 -0
  18. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +601 -0
  19. package/__tests__/coverage/lcov-report/src/application/commands/index.html +131 -0
  20. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +394 -0
  21. package/__tests__/coverage/lcov-report/src/application/queries/index.html +116 -0
  22. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +796 -0
  23. package/__tests__/coverage/lcov-report/src/application/services/index.html +116 -0
  24. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +793 -0
  25. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +1633 -0
  26. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +1618 -0
  27. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +116 -0
  28. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +952 -0
  29. package/__tests__/coverage/lcov-report/src/domain/services/index.html +116 -0
  30. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +1294 -0
  31. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +3124 -0
  32. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +2167 -0
  33. package/__tests__/coverage/lcov-report/src/index.html +266 -0
  34. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  35. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +116 -0
  36. package/__tests__/coverage/lcov-report/src/migration.ts.html +2092 -0
  37. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +1711 -0
  38. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +2281 -0
  39. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +2374 -0
  40. package/__tests__/coverage/lcov-report/src/types.ts.html +2266 -0
  41. package/__tests__/coverage/lcov.info +10238 -0
  42. package/__tests__/coverage/prettify.css +1 -0
  43. package/__tests__/coverage/prettify.js +2 -0
  44. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  45. package/__tests__/coverage/sorter.js +210 -0
  46. package/__tests__/coverage/src/agentdb-adapter.ts.html +2737 -0
  47. package/__tests__/coverage/src/agentdb-backend.ts.html +3130 -0
  48. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +601 -0
  49. package/__tests__/coverage/src/application/commands/index.html +131 -0
  50. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +394 -0
  51. package/__tests__/coverage/src/application/queries/index.html +116 -0
  52. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +796 -0
  53. package/__tests__/coverage/src/application/services/index.html +116 -0
  54. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +793 -0
  55. package/__tests__/coverage/src/cache-manager.ts.html +1633 -0
  56. package/__tests__/coverage/src/database-provider.ts.html +1618 -0
  57. package/__tests__/coverage/src/domain/entities/index.html +116 -0
  58. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +952 -0
  59. package/__tests__/coverage/src/domain/services/index.html +116 -0
  60. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +1294 -0
  61. package/__tests__/coverage/src/hnsw-index.ts.html +3124 -0
  62. package/__tests__/coverage/src/hybrid-backend.ts.html +2167 -0
  63. package/__tests__/coverage/src/index.html +266 -0
  64. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +1633 -0
  65. package/__tests__/coverage/src/infrastructure/repositories/index.html +116 -0
  66. package/__tests__/coverage/src/migration.ts.html +2092 -0
  67. package/__tests__/coverage/src/query-builder.ts.html +1711 -0
  68. package/__tests__/coverage/src/sqlite-backend.ts.html +2281 -0
  69. package/__tests__/coverage/src/sqljs-backend.ts.html +2374 -0
  70. package/__tests__/coverage/src/types.ts.html +2266 -0
  71. package/benchmarks/cache-hit-rate.bench.ts +535 -0
  72. package/benchmarks/hnsw-indexing.bench.ts +552 -0
  73. package/benchmarks/memory-write.bench.ts +469 -0
  74. package/benchmarks/vector-search.bench.ts +449 -0
  75. package/dist/agentdb-adapter.d.ts +146 -0
  76. package/dist/agentdb-adapter.d.ts.map +1 -0
  77. package/dist/agentdb-adapter.js +679 -0
  78. package/dist/agentdb-adapter.js.map +1 -0
  79. package/dist/agentdb-backend.d.ts +214 -0
  80. package/dist/agentdb-backend.d.ts.map +1 -0
  81. package/dist/agentdb-backend.js +827 -0
  82. package/dist/agentdb-backend.js.map +1 -0
  83. package/dist/agentdb-backend.test.d.ts +7 -0
  84. package/dist/agentdb-backend.test.d.ts.map +1 -0
  85. package/dist/agentdb-backend.test.js +258 -0
  86. package/dist/agentdb-backend.test.js.map +1 -0
  87. package/dist/application/commands/delete-memory.command.d.ts +65 -0
  88. package/dist/application/commands/delete-memory.command.d.ts.map +1 -0
  89. package/dist/application/commands/delete-memory.command.js +129 -0
  90. package/dist/application/commands/delete-memory.command.js.map +1 -0
  91. package/dist/application/commands/store-memory.command.d.ts +48 -0
  92. package/dist/application/commands/store-memory.command.d.ts.map +1 -0
  93. package/dist/application/commands/store-memory.command.js +72 -0
  94. package/dist/application/commands/store-memory.command.js.map +1 -0
  95. package/dist/application/index.d.ts +12 -0
  96. package/dist/application/index.d.ts.map +1 -0
  97. package/dist/application/index.js +15 -0
  98. package/dist/application/index.js.map +1 -0
  99. package/dist/application/queries/search-memory.query.d.ts +72 -0
  100. package/dist/application/queries/search-memory.query.d.ts.map +1 -0
  101. package/dist/application/queries/search-memory.query.js +143 -0
  102. package/dist/application/queries/search-memory.query.js.map +1 -0
  103. package/dist/application/services/memory-application-service.d.ts +121 -0
  104. package/dist/application/services/memory-application-service.d.ts.map +1 -0
  105. package/dist/application/services/memory-application-service.js +190 -0
  106. package/dist/application/services/memory-application-service.js.map +1 -0
  107. package/dist/cache-manager.d.ts +134 -0
  108. package/dist/cache-manager.d.ts.map +1 -0
  109. package/dist/cache-manager.js +407 -0
  110. package/dist/cache-manager.js.map +1 -0
  111. package/dist/database-provider.d.ts +86 -0
  112. package/dist/database-provider.d.ts.map +1 -0
  113. package/dist/database-provider.js +385 -0
  114. package/dist/database-provider.js.map +1 -0
  115. package/dist/database-provider.test.d.ts +7 -0
  116. package/dist/database-provider.test.d.ts.map +1 -0
  117. package/dist/database-provider.test.js +285 -0
  118. package/dist/database-provider.test.js.map +1 -0
  119. package/dist/domain/entities/memory-entry.d.ts +143 -0
  120. package/dist/domain/entities/memory-entry.d.ts.map +1 -0
  121. package/dist/domain/entities/memory-entry.js +226 -0
  122. package/dist/domain/entities/memory-entry.js.map +1 -0
  123. package/dist/domain/index.d.ts +11 -0
  124. package/dist/domain/index.d.ts.map +1 -0
  125. package/dist/domain/index.js +12 -0
  126. package/dist/domain/index.js.map +1 -0
  127. package/dist/domain/repositories/memory-repository.interface.d.ts +102 -0
  128. package/dist/domain/repositories/memory-repository.interface.d.ts.map +1 -0
  129. package/dist/domain/repositories/memory-repository.interface.js +11 -0
  130. package/dist/domain/repositories/memory-repository.interface.js.map +1 -0
  131. package/dist/domain/services/memory-domain-service.d.ts +105 -0
  132. package/dist/domain/services/memory-domain-service.d.ts.map +1 -0
  133. package/dist/domain/services/memory-domain-service.js +297 -0
  134. package/dist/domain/services/memory-domain-service.js.map +1 -0
  135. package/dist/hnsw-index.d.ts +111 -0
  136. package/dist/hnsw-index.d.ts.map +1 -0
  137. package/dist/hnsw-index.js +781 -0
  138. package/dist/hnsw-index.js.map +1 -0
  139. package/dist/hybrid-backend.d.ts +217 -0
  140. package/dist/hybrid-backend.d.ts.map +1 -0
  141. package/dist/hybrid-backend.js +491 -0
  142. package/dist/hybrid-backend.js.map +1 -0
  143. package/dist/hybrid-backend.test.d.ts +8 -0
  144. package/dist/hybrid-backend.test.d.ts.map +1 -0
  145. package/dist/hybrid-backend.test.js +320 -0
  146. package/dist/hybrid-backend.test.js.map +1 -0
  147. package/dist/index.d.ts +188 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +345 -0
  150. package/dist/index.js.map +1 -0
  151. package/dist/infrastructure/index.d.ts +17 -0
  152. package/dist/infrastructure/index.d.ts.map +1 -0
  153. package/dist/infrastructure/index.js +16 -0
  154. package/dist/infrastructure/index.js.map +1 -0
  155. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts +66 -0
  156. package/dist/infrastructure/repositories/hybrid-memory-repository.d.ts.map +1 -0
  157. package/dist/infrastructure/repositories/hybrid-memory-repository.js +409 -0
  158. package/dist/infrastructure/repositories/hybrid-memory-repository.js.map +1 -0
  159. package/dist/migration.d.ts +68 -0
  160. package/dist/migration.d.ts.map +1 -0
  161. package/dist/migration.js +513 -0
  162. package/dist/migration.js.map +1 -0
  163. package/dist/query-builder.d.ts +211 -0
  164. package/dist/query-builder.d.ts.map +1 -0
  165. package/dist/query-builder.js +438 -0
  166. package/dist/query-builder.js.map +1 -0
  167. package/dist/sqlite-backend.d.ts +121 -0
  168. package/dist/sqlite-backend.d.ts.map +1 -0
  169. package/dist/sqlite-backend.js +564 -0
  170. package/dist/sqlite-backend.js.map +1 -0
  171. package/dist/sqljs-backend.d.ts +128 -0
  172. package/dist/sqljs-backend.d.ts.map +1 -0
  173. package/dist/sqljs-backend.js +598 -0
  174. package/dist/sqljs-backend.js.map +1 -0
  175. package/dist/types.d.ts +481 -0
  176. package/dist/types.d.ts.map +1 -0
  177. package/dist/types.js +58 -0
  178. package/dist/types.js.map +1 -0
  179. package/docs/AGENTDB-INTEGRATION.md +388 -0
  180. package/docs/CROSS_PLATFORM.md +505 -0
  181. package/docs/WINDOWS_SUPPORT.md +422 -0
  182. package/examples/agentdb-example.ts +345 -0
  183. package/examples/cross-platform-usage.ts +326 -0
  184. package/framework/benchmark.ts +112 -0
  185. package/package.json +31 -0
  186. package/src/agentdb-adapter.ts +884 -0
  187. package/src/agentdb-backend.test.ts +339 -0
  188. package/src/agentdb-backend.ts +1016 -0
  189. package/src/application/commands/delete-memory.command.ts +172 -0
  190. package/src/application/commands/store-memory.command.ts +103 -0
  191. package/src/application/index.ts +36 -0
  192. package/src/application/queries/search-memory.query.ts +237 -0
  193. package/src/application/services/memory-application-service.ts +236 -0
  194. package/src/cache-manager.ts +516 -0
  195. package/src/database-provider.test.ts +364 -0
  196. package/src/database-provider.ts +511 -0
  197. package/src/domain/entities/memory-entry.ts +289 -0
  198. package/src/domain/index.ts +35 -0
  199. package/src/domain/repositories/memory-repository.interface.ts +120 -0
  200. package/src/domain/services/memory-domain-service.ts +403 -0
  201. package/src/hnsw-index.ts +1013 -0
  202. package/src/hybrid-backend.test.ts +399 -0
  203. package/src/hybrid-backend.ts +694 -0
  204. package/src/index.ts +515 -0
  205. package/src/infrastructure/index.ts +23 -0
  206. package/src/infrastructure/repositories/hybrid-memory-repository.ts +516 -0
  207. package/src/migration.ts +669 -0
  208. package/src/query-builder.ts +542 -0
  209. package/src/sqlite-backend.ts +732 -0
  210. package/src/sqljs-backend.ts +763 -0
  211. package/src/types.ts +727 -0
  212. package/tsconfig.json +9 -0
  213. package/tsconfig.tsbuildinfo +1 -0
  214. package/verify-cross-platform.ts +170 -0
@@ -0,0 +1,732 @@
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;