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

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 (156) 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/index.d.ts +8 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +8 -0
  29. package/dist/index.js.map +1 -1
  30. package/dist/learning-bridge.d.ts +137 -0
  31. package/dist/learning-bridge.d.ts.map +1 -0
  32. package/dist/learning-bridge.js +335 -0
  33. package/dist/learning-bridge.js.map +1 -0
  34. package/dist/learning-bridge.test.d.ts +8 -0
  35. package/dist/learning-bridge.test.d.ts.map +1 -0
  36. package/dist/learning-bridge.test.js +578 -0
  37. package/dist/learning-bridge.test.js.map +1 -0
  38. package/dist/memory-graph.d.ts +100 -0
  39. package/dist/memory-graph.d.ts.map +1 -0
  40. package/dist/memory-graph.js +333 -0
  41. package/dist/memory-graph.js.map +1 -0
  42. package/dist/memory-graph.test.d.ts +8 -0
  43. package/dist/memory-graph.test.d.ts.map +1 -0
  44. package/dist/memory-graph.test.js +609 -0
  45. package/dist/memory-graph.test.js.map +1 -0
  46. package/dist/types.d.ts +3 -0
  47. package/dist/types.d.ts.map +1 -1
  48. package/package.json +19 -4
  49. package/.agentic-flow/intelligence.json +0 -16
  50. package/__tests__/coverage/base.css +0 -224
  51. package/__tests__/coverage/block-navigation.js +0 -87
  52. package/__tests__/coverage/coverage-final.json +0 -19
  53. package/__tests__/coverage/favicon.png +0 -0
  54. package/__tests__/coverage/index.html +0 -206
  55. package/__tests__/coverage/lcov-report/base.css +0 -224
  56. package/__tests__/coverage/lcov-report/block-navigation.js +0 -87
  57. package/__tests__/coverage/lcov-report/favicon.png +0 -0
  58. package/__tests__/coverage/lcov-report/index.html +0 -206
  59. package/__tests__/coverage/lcov-report/prettify.css +0 -1
  60. package/__tests__/coverage/lcov-report/prettify.js +0 -2
  61. package/__tests__/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  62. package/__tests__/coverage/lcov-report/sorter.js +0 -210
  63. package/__tests__/coverage/lcov-report/src/agentdb-adapter.ts.html +0 -2737
  64. package/__tests__/coverage/lcov-report/src/agentdb-backend.ts.html +0 -3130
  65. package/__tests__/coverage/lcov-report/src/application/commands/delete-memory.command.ts.html +0 -601
  66. package/__tests__/coverage/lcov-report/src/application/commands/index.html +0 -131
  67. package/__tests__/coverage/lcov-report/src/application/commands/store-memory.command.ts.html +0 -394
  68. package/__tests__/coverage/lcov-report/src/application/queries/index.html +0 -116
  69. package/__tests__/coverage/lcov-report/src/application/queries/search-memory.query.ts.html +0 -796
  70. package/__tests__/coverage/lcov-report/src/application/services/index.html +0 -116
  71. package/__tests__/coverage/lcov-report/src/application/services/memory-application-service.ts.html +0 -793
  72. package/__tests__/coverage/lcov-report/src/cache-manager.ts.html +0 -1633
  73. package/__tests__/coverage/lcov-report/src/database-provider.ts.html +0 -1618
  74. package/__tests__/coverage/lcov-report/src/domain/entities/index.html +0 -116
  75. package/__tests__/coverage/lcov-report/src/domain/entities/memory-entry.ts.html +0 -952
  76. package/__tests__/coverage/lcov-report/src/domain/services/index.html +0 -116
  77. package/__tests__/coverage/lcov-report/src/domain/services/memory-domain-service.ts.html +0 -1294
  78. package/__tests__/coverage/lcov-report/src/hnsw-index.ts.html +0 -3124
  79. package/__tests__/coverage/lcov-report/src/hybrid-backend.ts.html +0 -2167
  80. package/__tests__/coverage/lcov-report/src/index.html +0 -266
  81. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  82. package/__tests__/coverage/lcov-report/src/infrastructure/repositories/index.html +0 -116
  83. package/__tests__/coverage/lcov-report/src/migration.ts.html +0 -2092
  84. package/__tests__/coverage/lcov-report/src/query-builder.ts.html +0 -1711
  85. package/__tests__/coverage/lcov-report/src/sqlite-backend.ts.html +0 -2281
  86. package/__tests__/coverage/lcov-report/src/sqljs-backend.ts.html +0 -2374
  87. package/__tests__/coverage/lcov-report/src/types.ts.html +0 -2266
  88. package/__tests__/coverage/lcov.info +0 -10238
  89. package/__tests__/coverage/prettify.css +0 -1
  90. package/__tests__/coverage/prettify.js +0 -2
  91. package/__tests__/coverage/sort-arrow-sprite.png +0 -0
  92. package/__tests__/coverage/sorter.js +0 -210
  93. package/__tests__/coverage/src/agentdb-adapter.ts.html +0 -2737
  94. package/__tests__/coverage/src/agentdb-backend.ts.html +0 -3130
  95. package/__tests__/coverage/src/application/commands/delete-memory.command.ts.html +0 -601
  96. package/__tests__/coverage/src/application/commands/index.html +0 -131
  97. package/__tests__/coverage/src/application/commands/store-memory.command.ts.html +0 -394
  98. package/__tests__/coverage/src/application/queries/index.html +0 -116
  99. package/__tests__/coverage/src/application/queries/search-memory.query.ts.html +0 -796
  100. package/__tests__/coverage/src/application/services/index.html +0 -116
  101. package/__tests__/coverage/src/application/services/memory-application-service.ts.html +0 -793
  102. package/__tests__/coverage/src/cache-manager.ts.html +0 -1633
  103. package/__tests__/coverage/src/database-provider.ts.html +0 -1618
  104. package/__tests__/coverage/src/domain/entities/index.html +0 -116
  105. package/__tests__/coverage/src/domain/entities/memory-entry.ts.html +0 -952
  106. package/__tests__/coverage/src/domain/services/index.html +0 -116
  107. package/__tests__/coverage/src/domain/services/memory-domain-service.ts.html +0 -1294
  108. package/__tests__/coverage/src/hnsw-index.ts.html +0 -3124
  109. package/__tests__/coverage/src/hybrid-backend.ts.html +0 -2167
  110. package/__tests__/coverage/src/index.html +0 -266
  111. package/__tests__/coverage/src/infrastructure/repositories/hybrid-memory-repository.ts.html +0 -1633
  112. package/__tests__/coverage/src/infrastructure/repositories/index.html +0 -116
  113. package/__tests__/coverage/src/migration.ts.html +0 -2092
  114. package/__tests__/coverage/src/query-builder.ts.html +0 -1711
  115. package/__tests__/coverage/src/sqlite-backend.ts.html +0 -2281
  116. package/__tests__/coverage/src/sqljs-backend.ts.html +0 -2374
  117. package/__tests__/coverage/src/types.ts.html +0 -2266
  118. package/benchmarks/cache-hit-rate.bench.ts +0 -535
  119. package/benchmarks/hnsw-indexing.bench.ts +0 -552
  120. package/benchmarks/memory-write.bench.ts +0 -469
  121. package/benchmarks/vector-search.bench.ts +0 -449
  122. package/docs/AGENTDB-INTEGRATION.md +0 -388
  123. package/docs/CROSS_PLATFORM.md +0 -505
  124. package/docs/WINDOWS_SUPPORT.md +0 -422
  125. package/examples/agentdb-example.ts +0 -345
  126. package/examples/cross-platform-usage.ts +0 -326
  127. package/framework/benchmark.ts +0 -112
  128. package/src/agentdb-adapter.ts +0 -884
  129. package/src/agentdb-backend.test.ts +0 -339
  130. package/src/agentdb-backend.ts +0 -1016
  131. package/src/application/commands/delete-memory.command.ts +0 -172
  132. package/src/application/commands/store-memory.command.ts +0 -103
  133. package/src/application/index.ts +0 -36
  134. package/src/application/queries/search-memory.query.ts +0 -237
  135. package/src/application/services/memory-application-service.ts +0 -236
  136. package/src/cache-manager.ts +0 -516
  137. package/src/database-provider.test.ts +0 -364
  138. package/src/database-provider.ts +0 -511
  139. package/src/domain/entities/memory-entry.ts +0 -289
  140. package/src/domain/index.ts +0 -35
  141. package/src/domain/repositories/memory-repository.interface.ts +0 -120
  142. package/src/domain/services/memory-domain-service.ts +0 -403
  143. package/src/hnsw-index.ts +0 -1013
  144. package/src/hybrid-backend.test.ts +0 -399
  145. package/src/hybrid-backend.ts +0 -694
  146. package/src/index.ts +0 -515
  147. package/src/infrastructure/index.ts +0 -23
  148. package/src/infrastructure/repositories/hybrid-memory-repository.ts +0 -516
  149. package/src/migration.ts +0 -669
  150. package/src/query-builder.ts +0 -542
  151. package/src/sqlite-backend.ts +0 -732
  152. package/src/sqljs-backend.ts +0 -763
  153. package/src/types.ts +0 -727
  154. package/tsconfig.json +0 -9
  155. package/tsconfig.tsbuildinfo +0 -1
  156. package/verify-cross-platform.ts +0 -170
package/src/hnsw-index.ts DELETED
@@ -1,1013 +0,0 @@
1
- /**
2
- * V3 HNSW Vector Index
3
- *
4
- * High-performance Hierarchical Navigable Small World (HNSW) index for
5
- * 150x-12,500x faster vector similarity search compared to brute force.
6
- *
7
- * OPTIMIZATIONS:
8
- * - BinaryMinHeap/BinaryMaxHeap for O(log n) operations (vs O(n log n) Array.sort)
9
- * - Pre-normalized vectors for O(1) cosine similarity (no sqrt needed)
10
- * - Bounded max-heap for efficient top-k tracking
11
- *
12
- * @module v3/memory/hnsw-index
13
- */
14
-
15
- import { EventEmitter } from 'node:events';
16
- import {
17
- DistanceMetric,
18
- HNSWConfig,
19
- HNSWStats,
20
- QuantizationConfig,
21
- SearchResult,
22
- MemoryEntry,
23
- MemoryEvent,
24
- MemoryEventHandler,
25
- } from './types.js';
26
-
27
- /**
28
- * Binary Min Heap for O(log n) priority queue operations
29
- * Used for candidate selection in HNSW search
30
- */
31
- class BinaryMinHeap<T> {
32
- private heap: Array<{ item: T; priority: number }> = [];
33
-
34
- get size(): number {
35
- return this.heap.length;
36
- }
37
-
38
- insert(item: T, priority: number): void {
39
- this.heap.push({ item, priority });
40
- this.bubbleUp(this.heap.length - 1);
41
- }
42
-
43
- extractMin(): T | undefined {
44
- if (this.heap.length === 0) return undefined;
45
- const min = this.heap[0].item;
46
- const last = this.heap.pop()!;
47
- if (this.heap.length > 0) {
48
- this.heap[0] = last;
49
- this.bubbleDown(0);
50
- }
51
- return min;
52
- }
53
-
54
- peek(): T | undefined {
55
- return this.heap[0]?.item;
56
- }
57
-
58
- peekPriority(): number | undefined {
59
- return this.heap[0]?.priority;
60
- }
61
-
62
- isEmpty(): boolean {
63
- return this.heap.length === 0;
64
- }
65
-
66
- toArray(): T[] {
67
- return this.heap
68
- .slice()
69
- .sort((a, b) => a.priority - b.priority)
70
- .map((entry) => entry.item);
71
- }
72
-
73
- private bubbleUp(index: number): void {
74
- while (index > 0) {
75
- const parent = Math.floor((index - 1) / 2);
76
- if (this.heap[parent].priority <= this.heap[index].priority) break;
77
- [this.heap[parent], this.heap[index]] = [this.heap[index], this.heap[parent]];
78
- index = parent;
79
- }
80
- }
81
-
82
- private bubbleDown(index: number): void {
83
- const length = this.heap.length;
84
- while (true) {
85
- let smallest = index;
86
- const left = 2 * index + 1;
87
- const right = 2 * index + 2;
88
- if (left < length && this.heap[left].priority < this.heap[smallest].priority) {
89
- smallest = left;
90
- }
91
- if (right < length && this.heap[right].priority < this.heap[smallest].priority) {
92
- smallest = right;
93
- }
94
- if (smallest === index) break;
95
- [this.heap[smallest], this.heap[index]] = [this.heap[index], this.heap[smallest]];
96
- index = smallest;
97
- }
98
- }
99
- }
100
-
101
- /**
102
- * Binary Max Heap for bounded top-k tracking
103
- * Keeps track of k smallest elements by evicting largest when full
104
- */
105
- class BinaryMaxHeap<T> {
106
- private heap: Array<{ item: T; priority: number }> = [];
107
- private maxSize: number;
108
-
109
- constructor(maxSize: number = Infinity) {
110
- this.maxSize = maxSize;
111
- }
112
-
113
- get size(): number {
114
- return this.heap.length;
115
- }
116
-
117
- insert(item: T, priority: number): boolean {
118
- // If at capacity and new item is worse than worst, reject
119
- if (this.heap.length >= this.maxSize && priority >= this.heap[0]?.priority) {
120
- return false;
121
- }
122
-
123
- if (this.heap.length >= this.maxSize) {
124
- // Replace max element
125
- this.heap[0] = { item, priority };
126
- this.bubbleDown(0);
127
- } else {
128
- this.heap.push({ item, priority });
129
- this.bubbleUp(this.heap.length - 1);
130
- }
131
- return true;
132
- }
133
-
134
- peekMax(): T | undefined {
135
- return this.heap[0]?.item;
136
- }
137
-
138
- peekMaxPriority(): number {
139
- return this.heap[0]?.priority ?? Infinity;
140
- }
141
-
142
- extractMax(): T | undefined {
143
- if (this.heap.length === 0) return undefined;
144
- const max = this.heap[0].item;
145
- const last = this.heap.pop()!;
146
- if (this.heap.length > 0) {
147
- this.heap[0] = last;
148
- this.bubbleDown(0);
149
- }
150
- return max;
151
- }
152
-
153
- isEmpty(): boolean {
154
- return this.heap.length === 0;
155
- }
156
-
157
- toSortedArray(): Array<{ item: T; priority: number }> {
158
- return this.heap.slice().sort((a, b) => a.priority - b.priority);
159
- }
160
-
161
- private bubbleUp(index: number): void {
162
- while (index > 0) {
163
- const parent = Math.floor((index - 1) / 2);
164
- if (this.heap[parent].priority >= this.heap[index].priority) break;
165
- [this.heap[parent], this.heap[index]] = [this.heap[index], this.heap[parent]];
166
- index = parent;
167
- }
168
- }
169
-
170
- private bubbleDown(index: number): void {
171
- const length = this.heap.length;
172
- while (true) {
173
- let largest = index;
174
- const left = 2 * index + 1;
175
- const right = 2 * index + 2;
176
- if (left < length && this.heap[left].priority > this.heap[largest].priority) {
177
- largest = left;
178
- }
179
- if (right < length && this.heap[right].priority > this.heap[largest].priority) {
180
- largest = right;
181
- }
182
- if (largest === index) break;
183
- [this.heap[largest], this.heap[index]] = [this.heap[index], this.heap[largest]];
184
- index = largest;
185
- }
186
- }
187
- }
188
-
189
- /**
190
- * Internal node structure for HNSW graph
191
- */
192
- interface HNSWNode {
193
- /** Node ID (memory entry ID) */
194
- id: string;
195
-
196
- /** Vector embedding (original) */
197
- vector: Float32Array;
198
-
199
- /** Pre-normalized vector for O(1) cosine similarity */
200
- normalizedVector: Float32Array | null;
201
-
202
- /** Connections at each layer */
203
- connections: Map<number, Set<string>>;
204
-
205
- /** Node level (top layer this node appears in) */
206
- level: number;
207
- }
208
-
209
- /**
210
- * HNSW Index implementation for ultra-fast vector similarity search
211
- *
212
- * Performance characteristics:
213
- * - Search: O(log n) approximate nearest neighbor
214
- * - Insert: O(log n) amortized
215
- * - Memory: O(n * M * L) where M is max connections, L is layers
216
- */
217
- export class HNSWIndex extends EventEmitter {
218
- private config: HNSWConfig;
219
- private nodes: Map<string, HNSWNode> = new Map();
220
- private entryPoint: string | null = null;
221
- private maxLevel: number = 0;
222
- private levelMult: number;
223
-
224
- // Performance tracking
225
- private stats: {
226
- searchCount: number;
227
- totalSearchTime: number;
228
- insertCount: number;
229
- totalInsertTime: number;
230
- buildStartTime: number;
231
- } = {
232
- searchCount: 0,
233
- totalSearchTime: 0,
234
- insertCount: 0,
235
- totalInsertTime: 0,
236
- buildStartTime: 0,
237
- };
238
-
239
- // Quantization support
240
- private quantizer: Quantizer | null = null;
241
-
242
- constructor(config: Partial<HNSWConfig> = {}) {
243
- super();
244
- this.config = this.mergeConfig(config);
245
- this.levelMult = 1 / Math.log(this.config.M);
246
-
247
- if (this.config.quantization) {
248
- this.quantizer = new Quantizer(this.config.quantization, this.config.dimensions);
249
- }
250
- }
251
-
252
- /**
253
- * Add a vector to the index
254
- */
255
- async addPoint(id: string, vector: Float32Array): Promise<void> {
256
- const startTime = performance.now();
257
-
258
- if (vector.length !== this.config.dimensions) {
259
- throw new Error(
260
- `Vector dimension mismatch: expected ${this.config.dimensions}, got ${vector.length}`
261
- );
262
- }
263
-
264
- if (this.nodes.size >= this.config.maxElements) {
265
- throw new Error('Index is full, cannot add more elements');
266
- }
267
-
268
- // Quantize if enabled
269
- const storedVector = this.quantizer
270
- ? this.quantizer.encode(vector)
271
- : vector;
272
-
273
- // Pre-normalize vector for O(1) cosine similarity
274
- const normalizedVector = this.config.metric === 'cosine'
275
- ? this.normalizeVector(storedVector)
276
- : null;
277
-
278
- // Generate random level for new node
279
- const level = this.getRandomLevel();
280
-
281
- const node: HNSWNode = {
282
- id,
283
- vector: storedVector,
284
- normalizedVector,
285
- connections: new Map(),
286
- level,
287
- };
288
-
289
- // Initialize connection sets for each layer
290
- for (let l = 0; l <= level; l++) {
291
- node.connections.set(l, new Set());
292
- }
293
-
294
- if (this.entryPoint === null) {
295
- // First node
296
- this.entryPoint = id;
297
- this.maxLevel = level;
298
- this.nodes.set(id, node);
299
- } else {
300
- // Insert new node into the graph
301
- await this.insertNode(node);
302
- }
303
-
304
- const duration = performance.now() - startTime;
305
- this.stats.insertCount++;
306
- this.stats.totalInsertTime += duration;
307
-
308
- this.emit('point:added', { id, level, duration });
309
- }
310
-
311
- /**
312
- * Search for k nearest neighbors
313
- */
314
- async search(
315
- query: Float32Array,
316
- k: number,
317
- ef?: number
318
- ): Promise<Array<{ id: string; distance: number }>> {
319
- const startTime = performance.now();
320
-
321
- if (query.length !== this.config.dimensions) {
322
- throw new Error(
323
- `Query dimension mismatch: expected ${this.config.dimensions}, got ${query.length}`
324
- );
325
- }
326
-
327
- if (this.entryPoint === null) {
328
- return [];
329
- }
330
-
331
- const searchEf = ef || Math.max(k, this.config.efConstruction);
332
-
333
- // Quantize query if needed
334
- const queryVector = this.quantizer
335
- ? this.quantizer.encode(query)
336
- : query;
337
-
338
- // Pre-normalize query for O(1) cosine similarity
339
- const normalizedQuery = this.config.metric === 'cosine'
340
- ? this.normalizeVector(queryVector)
341
- : null;
342
-
343
- // Start from entry point and search down the layers
344
- let currentNode = this.entryPoint;
345
- let currentDist = this.distanceOptimized(
346
- queryVector,
347
- normalizedQuery,
348
- this.nodes.get(currentNode)!
349
- );
350
-
351
- // Search through layers from top to 1
352
- for (let level = this.maxLevel; level > 0; level--) {
353
- const layerResult = this.searchLayerOptimized(
354
- queryVector,
355
- normalizedQuery,
356
- currentNode,
357
- 1,
358
- level
359
- );
360
- currentNode = layerResult[0]?.id || currentNode;
361
- currentDist = this.distanceOptimized(
362
- queryVector,
363
- normalizedQuery,
364
- this.nodes.get(currentNode)!
365
- );
366
- }
367
-
368
- // Search layer 0 with ef candidates using heap-based search
369
- const candidates = this.searchLayerOptimized(
370
- queryVector,
371
- normalizedQuery,
372
- currentNode,
373
- searchEf,
374
- 0
375
- );
376
-
377
- // Return top k results (already sorted by heap)
378
- const results = candidates.slice(0, k);
379
-
380
- const duration = performance.now() - startTime;
381
- this.stats.searchCount++;
382
- this.stats.totalSearchTime += duration;
383
-
384
- return results;
385
- }
386
-
387
- /**
388
- * Search with filters applied post-retrieval
389
- */
390
- async searchWithFilters(
391
- query: Float32Array,
392
- k: number,
393
- filter: (id: string) => boolean,
394
- ef?: number
395
- ): Promise<Array<{ id: string; distance: number }>> {
396
- // Over-fetch to account for filtered results
397
- const overFetchFactor = 3;
398
- const candidates = await this.search(query, k * overFetchFactor, ef);
399
-
400
- return candidates
401
- .filter((c) => filter(c.id))
402
- .slice(0, k);
403
- }
404
-
405
- /**
406
- * Remove a point from the index
407
- */
408
- async removePoint(id: string): Promise<boolean> {
409
- const node = this.nodes.get(id);
410
- if (!node) {
411
- return false;
412
- }
413
-
414
- // Remove all connections to this node
415
- for (let level = 0; level <= node.level; level++) {
416
- const connections = node.connections.get(level);
417
- if (connections) {
418
- for (const connectedId of connections) {
419
- const connectedNode = this.nodes.get(connectedId);
420
- if (connectedNode) {
421
- connectedNode.connections.get(level)?.delete(id);
422
- }
423
- }
424
- }
425
- }
426
-
427
- this.nodes.delete(id);
428
-
429
- // Update entry point if needed
430
- if (this.entryPoint === id) {
431
- if (this.nodes.size === 0) {
432
- this.entryPoint = null;
433
- this.maxLevel = 0;
434
- } else {
435
- // Find new entry point with highest level
436
- let newEntry: string | null = null;
437
- let newMaxLevel = 0;
438
- for (const [nodeId, n] of this.nodes) {
439
- if (newEntry === null || n.level > newMaxLevel) {
440
- newMaxLevel = n.level;
441
- newEntry = nodeId;
442
- }
443
- }
444
- this.entryPoint = newEntry;
445
- this.maxLevel = newMaxLevel;
446
- }
447
- }
448
-
449
- this.emit('point:removed', { id });
450
- return true;
451
- }
452
-
453
- /**
454
- * Rebuild the index from scratch
455
- */
456
- async rebuild(
457
- entries: Array<{ id: string; vector: Float32Array }>
458
- ): Promise<void> {
459
- this.stats.buildStartTime = performance.now();
460
-
461
- this.nodes.clear();
462
- this.entryPoint = null;
463
- this.maxLevel = 0;
464
-
465
- for (const entry of entries) {
466
- await this.addPoint(entry.id, entry.vector);
467
- }
468
-
469
- const buildTime = performance.now() - this.stats.buildStartTime;
470
-
471
- this.emit('index:rebuilt', {
472
- vectorCount: this.nodes.size,
473
- buildTime,
474
- });
475
- }
476
-
477
- /**
478
- * Get index statistics
479
- */
480
- getStats(): HNSWStats {
481
- const vectorCount = this.nodes.size;
482
- const avgSearchTime =
483
- this.stats.searchCount > 0
484
- ? this.stats.totalSearchTime / this.stats.searchCount
485
- : 0;
486
-
487
- // Estimate memory usage
488
- const bytesPerVector = this.config.dimensions * 4; // Float32 = 4 bytes
489
- const connectionOverhead = this.config.M * 8 * (this.maxLevel + 1); // Approximate
490
- const memoryUsage = vectorCount * (bytesPerVector + connectionOverhead);
491
-
492
- return {
493
- vectorCount,
494
- memoryUsage,
495
- avgSearchTime,
496
- buildTime: performance.now() - this.stats.buildStartTime,
497
- compressionRatio: this.quantizer?.getCompressionRatio() || 1.0,
498
- };
499
- }
500
-
501
- /**
502
- * Clear the index
503
- */
504
- clear(): void {
505
- this.nodes.clear();
506
- this.entryPoint = null;
507
- this.maxLevel = 0;
508
- this.stats = {
509
- searchCount: 0,
510
- totalSearchTime: 0,
511
- insertCount: 0,
512
- totalInsertTime: 0,
513
- buildStartTime: 0,
514
- };
515
- }
516
-
517
- /**
518
- * Check if an ID exists in the index
519
- */
520
- has(id: string): boolean {
521
- return this.nodes.has(id);
522
- }
523
-
524
- /**
525
- * Get the number of vectors in the index
526
- */
527
- get size(): number {
528
- return this.nodes.size;
529
- }
530
-
531
- // ===== Private Methods =====
532
-
533
- private mergeConfig(config: Partial<HNSWConfig>): HNSWConfig {
534
- return {
535
- dimensions: config.dimensions || 1536, // OpenAI embedding size
536
- M: config.M || 16,
537
- efConstruction: config.efConstruction || 200,
538
- maxElements: config.maxElements || 1000000,
539
- metric: config.metric || 'cosine',
540
- quantization: config.quantization,
541
- };
542
- }
543
-
544
- private getRandomLevel(): number {
545
- let level = 0;
546
- while (Math.random() < 0.5 && level < 16) {
547
- level++;
548
- }
549
- return level;
550
- }
551
-
552
- private async insertNode(node: HNSWNode): Promise<void> {
553
- const query = node.vector;
554
- const normalizedQuery = node.normalizedVector;
555
- let currentNode = this.entryPoint!;
556
- let currentDist = this.distanceOptimized(
557
- query,
558
- normalizedQuery,
559
- this.nodes.get(currentNode)!
560
- );
561
-
562
- // Find entry point for the node's level
563
- for (let level = this.maxLevel; level > node.level; level--) {
564
- const result = this.searchLayerOptimized(query, normalizedQuery, currentNode, 1, level);
565
- if (result.length > 0 && result[0].distance < currentDist) {
566
- currentNode = result[0].id;
567
- currentDist = result[0].distance;
568
- }
569
- }
570
-
571
- // Insert at each level from node.level down to 0
572
- for (let level = Math.min(node.level, this.maxLevel); level >= 0; level--) {
573
- const neighbors = this.searchLayerOptimized(
574
- query,
575
- normalizedQuery,
576
- currentNode,
577
- this.config.efConstruction,
578
- level
579
- );
580
-
581
- // Select M best neighbors
582
- const selectedNeighbors = this.selectNeighbors(
583
- node.id,
584
- query,
585
- neighbors,
586
- this.config.M
587
- );
588
-
589
- // Add connections
590
- for (const neighbor of selectedNeighbors) {
591
- node.connections.get(level)!.add(neighbor.id);
592
- this.nodes.get(neighbor.id)?.connections.get(level)?.add(node.id);
593
-
594
- // Prune connections if over limit
595
- const neighborNode = this.nodes.get(neighbor.id);
596
- if (neighborNode) {
597
- const neighborConns = neighborNode.connections.get(level)!;
598
- if (neighborConns.size > this.config.M * 2) {
599
- this.pruneConnections(neighborNode, level, this.config.M);
600
- }
601
- }
602
- }
603
-
604
- if (neighbors.length > 0) {
605
- currentNode = neighbors[0].id;
606
- }
607
- }
608
-
609
- this.nodes.set(node.id, node);
610
-
611
- // Update max level if needed
612
- if (node.level > this.maxLevel) {
613
- this.maxLevel = node.level;
614
- this.entryPoint = node.id;
615
- }
616
- }
617
-
618
- private async searchLayer(
619
- query: Float32Array,
620
- entryPoint: string,
621
- ef: number,
622
- level: number
623
- ): Promise<Array<{ id: string; distance: number }>> {
624
- const visited = new Set<string>([entryPoint]);
625
- const candidates: Array<{ id: string; distance: number }> = [];
626
- const results: Array<{ id: string; distance: number }> = [];
627
-
628
- const entryDist = this.distance(query, this.nodes.get(entryPoint)!.vector);
629
- candidates.push({ id: entryPoint, distance: entryDist });
630
- results.push({ id: entryPoint, distance: entryDist });
631
-
632
- while (candidates.length > 0) {
633
- // Get closest candidate
634
- candidates.sort((a, b) => a.distance - b.distance);
635
- const current = candidates.shift()!;
636
-
637
- // Check termination condition
638
- const worstResult = results.length > 0
639
- ? Math.max(...results.map((r) => r.distance))
640
- : Infinity;
641
- if (current.distance > worstResult && results.length >= ef) {
642
- break;
643
- }
644
-
645
- // Explore neighbors
646
- const node = this.nodes.get(current.id);
647
- if (!node) continue;
648
-
649
- const connections = node.connections.get(level);
650
- if (!connections) continue;
651
-
652
- for (const neighborId of connections) {
653
- if (visited.has(neighborId)) continue;
654
- visited.add(neighborId);
655
-
656
- const neighborNode = this.nodes.get(neighborId);
657
- if (!neighborNode) continue;
658
-
659
- const distance = this.distance(query, neighborNode.vector);
660
-
661
- if (results.length < ef || distance < worstResult) {
662
- candidates.push({ id: neighborId, distance });
663
- results.push({ id: neighborId, distance });
664
-
665
- // Keep results bounded
666
- if (results.length > ef) {
667
- results.sort((a, b) => a.distance - b.distance);
668
- results.pop();
669
- }
670
- }
671
- }
672
- }
673
-
674
- return results.sort((a, b) => a.distance - b.distance);
675
- }
676
-
677
- /**
678
- * OPTIMIZED searchLayer using heap-based priority queues
679
- * Performance: O(log n) per operation vs O(n log n) for Array.sort()
680
- * Expected speedup: 3-5x for large result sets
681
- */
682
- private searchLayerOptimized(
683
- query: Float32Array,
684
- normalizedQuery: Float32Array | null,
685
- entryPoint: string,
686
- ef: number,
687
- level: number
688
- ): Array<{ id: string; distance: number }> {
689
- const visited = new Set<string>([entryPoint]);
690
-
691
- // Min-heap for candidates (closest first for expansion)
692
- const candidates = new BinaryMinHeap<string>();
693
-
694
- // Max-heap for results (bounded size, tracks worst distance efficiently)
695
- const results = new BinaryMaxHeap<string>(ef);
696
-
697
- const entryNode = this.nodes.get(entryPoint)!;
698
- const entryDist = this.distanceOptimized(query, normalizedQuery, entryNode);
699
-
700
- candidates.insert(entryPoint, entryDist);
701
- results.insert(entryPoint, entryDist);
702
-
703
- while (!candidates.isEmpty()) {
704
- // Get closest candidate - O(log n)
705
- const currentDist = candidates.peekPriority()!;
706
- const currentId = candidates.extractMin()!;
707
-
708
- // Check termination: if closest candidate is worse than worst result, stop
709
- const worstResultDist = results.peekMaxPriority();
710
- if (currentDist > worstResultDist && results.size >= ef) {
711
- break;
712
- }
713
-
714
- // Explore neighbors
715
- const node = this.nodes.get(currentId);
716
- if (!node) continue;
717
-
718
- const connections = node.connections.get(level);
719
- if (!connections) continue;
720
-
721
- for (const neighborId of connections) {
722
- if (visited.has(neighborId)) continue;
723
- visited.add(neighborId);
724
-
725
- const neighborNode = this.nodes.get(neighborId);
726
- if (!neighborNode) continue;
727
-
728
- const distance = this.distanceOptimized(query, normalizedQuery, neighborNode);
729
-
730
- // Only add if within threshold or results not full
731
- if (results.size < ef || distance < worstResultDist) {
732
- candidates.insert(neighborId, distance);
733
- // Max-heap handles size bounding automatically - O(log n)
734
- results.insert(neighborId, distance);
735
- }
736
- }
737
- }
738
-
739
- // Return sorted results
740
- return results.toSortedArray().map(({ item, priority }) => ({
741
- id: item,
742
- distance: priority,
743
- }));
744
- }
745
-
746
- private selectNeighbors(
747
- nodeId: string,
748
- query: Float32Array,
749
- candidates: Array<{ id: string; distance: number }>,
750
- M: number
751
- ): Array<{ id: string; distance: number }> {
752
- // Simple selection: take M closest
753
- return candidates
754
- .filter((c) => c.id !== nodeId)
755
- .sort((a, b) => a.distance - b.distance)
756
- .slice(0, M);
757
- }
758
-
759
- private pruneConnections(node: HNSWNode, level: number, maxConnections: number): void {
760
- const connections = node.connections.get(level);
761
- if (!connections || connections.size <= maxConnections) return;
762
-
763
- // Calculate distances to all connections
764
- const distances: Array<{ id: string; distance: number }> = [];
765
- for (const connId of connections) {
766
- const connNode = this.nodes.get(connId);
767
- if (connNode) {
768
- distances.push({
769
- id: connId,
770
- distance: this.distance(node.vector, connNode.vector),
771
- });
772
- }
773
- }
774
-
775
- // Keep only the closest ones
776
- distances.sort((a, b) => a.distance - b.distance);
777
- const toKeep = new Set(distances.slice(0, maxConnections).map((d) => d.id));
778
-
779
- // Remove excess connections
780
- for (const connId of connections) {
781
- if (!toKeep.has(connId)) {
782
- connections.delete(connId);
783
- this.nodes.get(connId)?.connections.get(level)?.delete(node.id);
784
- }
785
- }
786
- }
787
-
788
- private distance(a: Float32Array, b: Float32Array): number {
789
- switch (this.config.metric) {
790
- case 'cosine':
791
- return this.cosineDistance(a, b);
792
- case 'euclidean':
793
- return this.euclideanDistance(a, b);
794
- case 'dot':
795
- return this.dotProductDistance(a, b);
796
- case 'manhattan':
797
- return this.manhattanDistance(a, b);
798
- default:
799
- return this.cosineDistance(a, b);
800
- }
801
- }
802
-
803
- private cosineDistance(a: Float32Array, b: Float32Array): number {
804
- let dotProduct = 0;
805
- let normA = 0;
806
- let normB = 0;
807
-
808
- for (let i = 0; i < a.length; i++) {
809
- dotProduct += a[i] * b[i];
810
- normA += a[i] * a[i];
811
- normB += b[i] * b[i];
812
- }
813
-
814
- const similarity = dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
815
- return 1 - similarity; // Convert to distance
816
- }
817
-
818
- /**
819
- * OPTIMIZED: Cosine distance using pre-normalized vectors
820
- * Only requires dot product (no sqrt operations)
821
- * Performance: O(n) with ~2x speedup over standard cosine
822
- */
823
- private cosineDistanceNormalized(a: Float32Array, b: Float32Array): number {
824
- let dotProduct = 0;
825
- for (let i = 0; i < a.length; i++) {
826
- dotProduct += a[i] * b[i];
827
- }
828
- // For normalized vectors: cosine_similarity = dot_product
829
- // Return distance (1 - similarity)
830
- return 1 - dotProduct;
831
- }
832
-
833
- /**
834
- * Normalize a vector to unit length for O(1) cosine similarity
835
- */
836
- private normalizeVector(vector: Float32Array): Float32Array {
837
- let norm = 0;
838
- for (let i = 0; i < vector.length; i++) {
839
- norm += vector[i] * vector[i];
840
- }
841
- norm = Math.sqrt(norm);
842
-
843
- if (norm === 0) {
844
- return vector; // Return as-is if zero vector
845
- }
846
-
847
- const normalized = new Float32Array(vector.length);
848
- for (let i = 0; i < vector.length; i++) {
849
- normalized[i] = vector[i] / norm;
850
- }
851
- return normalized;
852
- }
853
-
854
- /**
855
- * OPTIMIZED distance calculation that uses pre-normalized vectors when available
856
- */
857
- private distanceOptimized(
858
- query: Float32Array,
859
- normalizedQuery: Float32Array | null,
860
- node: HNSWNode
861
- ): number {
862
- // Use optimized path for cosine with pre-normalized vectors
863
- if (
864
- this.config.metric === 'cosine' &&
865
- normalizedQuery !== null &&
866
- node.normalizedVector !== null
867
- ) {
868
- return this.cosineDistanceNormalized(normalizedQuery, node.normalizedVector);
869
- }
870
-
871
- // Fall back to standard distance calculation
872
- return this.distance(query, node.vector);
873
- }
874
-
875
- private euclideanDistance(a: Float32Array, b: Float32Array): number {
876
- let sum = 0;
877
- for (let i = 0; i < a.length; i++) {
878
- const diff = a[i] - b[i];
879
- sum += diff * diff;
880
- }
881
- return Math.sqrt(sum);
882
- }
883
-
884
- private dotProductDistance(a: Float32Array, b: Float32Array): number {
885
- let dotProduct = 0;
886
- for (let i = 0; i < a.length; i++) {
887
- dotProduct += a[i] * b[i];
888
- }
889
- // Negative because higher dot product = more similar
890
- return -dotProduct;
891
- }
892
-
893
- private manhattanDistance(a: Float32Array, b: Float32Array): number {
894
- let sum = 0;
895
- for (let i = 0; i < a.length; i++) {
896
- sum += Math.abs(a[i] - b[i]);
897
- }
898
- return sum;
899
- }
900
- }
901
-
902
- /**
903
- * Quantizer for vector compression
904
- */
905
- class Quantizer {
906
- private config: QuantizationConfig;
907
- private dimensions: number;
908
-
909
- constructor(config: QuantizationConfig, dimensions: number) {
910
- this.config = config;
911
- this.dimensions = dimensions;
912
- }
913
-
914
- /**
915
- * Encode a vector using quantization
916
- */
917
- encode(vector: Float32Array): Float32Array {
918
- switch (this.config.type) {
919
- case 'binary':
920
- return this.binaryQuantize(vector);
921
- case 'scalar':
922
- return this.scalarQuantize(vector);
923
- case 'product':
924
- return this.productQuantize(vector);
925
- default:
926
- return vector;
927
- }
928
- }
929
-
930
- /**
931
- * Get compression ratio
932
- */
933
- getCompressionRatio(): number {
934
- switch (this.config.type) {
935
- case 'binary':
936
- return 32; // 32x compression (32 bits -> 1 bit per dimension)
937
- case 'scalar':
938
- return 32 / (this.config.bits || 8);
939
- case 'product':
940
- return this.config.subquantizers || 8;
941
- default:
942
- return 1;
943
- }
944
- }
945
-
946
- private binaryQuantize(vector: Float32Array): Float32Array {
947
- // Simple binary quantization: > 0 becomes 1, <= 0 becomes 0
948
- // Stored in packed format in a smaller Float32Array
949
- const packedLength = Math.ceil(vector.length / 32);
950
- const packed = new Float32Array(packedLength);
951
-
952
- for (let i = 0; i < vector.length; i++) {
953
- const packedIndex = Math.floor(i / 32);
954
- const bitPosition = i % 32;
955
- if (vector[i] > 0) {
956
- packed[packedIndex] = (packed[packedIndex] || 0) | (1 << bitPosition);
957
- }
958
- }
959
-
960
- return packed;
961
- }
962
-
963
- private scalarQuantize(vector: Float32Array): Float32Array {
964
- // Find min/max for normalization
965
- let min = Infinity;
966
- let max = -Infinity;
967
- for (let i = 0; i < vector.length; i++) {
968
- if (vector[i] < min) min = vector[i];
969
- if (vector[i] > max) max = vector[i];
970
- }
971
-
972
- const range = max - min || 1;
973
- const bits = this.config.bits || 8;
974
- const levels = Math.pow(2, bits);
975
-
976
- // Quantize each value
977
- const quantized = new Float32Array(vector.length + 2); // +2 for min/range
978
- quantized[0] = min;
979
- quantized[1] = range;
980
-
981
- for (let i = 0; i < vector.length; i++) {
982
- const normalized = (vector[i] - min) / range;
983
- quantized[i + 2] = Math.round(normalized * (levels - 1));
984
- }
985
-
986
- return quantized;
987
- }
988
-
989
- private productQuantize(vector: Float32Array): Float32Array {
990
- // Simplified product quantization
991
- // In production, would use trained codebooks
992
- const subquantizers = this.config.subquantizers || 8;
993
- const subvectorSize = Math.ceil(vector.length / subquantizers);
994
-
995
- const quantized = new Float32Array(subquantizers);
996
-
997
- for (let i = 0; i < subquantizers; i++) {
998
- let sum = 0;
999
- const start = i * subvectorSize;
1000
- const end = Math.min(start + subvectorSize, vector.length);
1001
-
1002
- for (let j = start; j < end; j++) {
1003
- sum += vector[j];
1004
- }
1005
-
1006
- quantized[i] = sum / (end - start);
1007
- }
1008
-
1009
- return quantized;
1010
- }
1011
- }
1012
-
1013
- export default HNSWIndex;