agentdb 1.4.4 → 1.4.5

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 (55) hide show
  1. package/dist/agentdb.min.js +4 -4
  2. package/dist/benchmarks/wasm-vector-benchmark.d.ts +10 -0
  3. package/dist/benchmarks/wasm-vector-benchmark.d.ts.map +1 -0
  4. package/dist/benchmarks/wasm-vector-benchmark.js +196 -0
  5. package/dist/benchmarks/wasm-vector-benchmark.js.map +1 -0
  6. package/dist/cli/agentdb-cli.d.ts +1 -1
  7. package/dist/cli/agentdb-cli.d.ts.map +1 -1
  8. package/dist/cli/agentdb-cli.js +38 -1
  9. package/dist/cli/agentdb-cli.js.map +1 -1
  10. package/dist/controllers/EmbeddingService.d.ts.map +1 -1
  11. package/dist/controllers/EmbeddingService.js +7 -3
  12. package/dist/controllers/EmbeddingService.js.map +1 -1
  13. package/dist/controllers/EnhancedEmbeddingService.d.ts +50 -0
  14. package/dist/controllers/EnhancedEmbeddingService.d.ts.map +1 -0
  15. package/dist/controllers/EnhancedEmbeddingService.js +119 -0
  16. package/dist/controllers/EnhancedEmbeddingService.js.map +1 -0
  17. package/dist/controllers/WASMVectorSearch.d.ts +89 -0
  18. package/dist/controllers/WASMVectorSearch.d.ts.map +1 -0
  19. package/dist/controllers/WASMVectorSearch.js +226 -0
  20. package/dist/controllers/WASMVectorSearch.js.map +1 -0
  21. package/dist/controllers/index.d.ts +4 -0
  22. package/dist/controllers/index.d.ts.map +1 -1
  23. package/dist/controllers/index.js +2 -0
  24. package/dist/controllers/index.js.map +1 -1
  25. package/dist/db-fallback.d.ts +4 -0
  26. package/dist/db-fallback.d.ts.map +1 -1
  27. package/dist/db-fallback.js +36 -10
  28. package/dist/db-fallback.js.map +1 -1
  29. package/dist/examples/wasm-vector-usage.d.ts +12 -0
  30. package/dist/examples/wasm-vector-usage.d.ts.map +1 -0
  31. package/dist/examples/wasm-vector-usage.js +190 -0
  32. package/dist/examples/wasm-vector-usage.js.map +1 -0
  33. package/dist/mcp/agentdb-mcp-server.js +54 -27
  34. package/dist/mcp/agentdb-mcp-server.js.map +1 -1
  35. package/dist/optimizations/BatchOperations.d.ts +7 -2
  36. package/dist/optimizations/BatchOperations.d.ts.map +1 -1
  37. package/dist/optimizations/BatchOperations.js +46 -19
  38. package/dist/optimizations/BatchOperations.js.map +1 -1
  39. package/dist/security/input-validation.d.ts +85 -0
  40. package/dist/security/input-validation.d.ts.map +1 -0
  41. package/dist/security/input-validation.js +292 -0
  42. package/dist/security/input-validation.js.map +1 -0
  43. package/package.json +9 -3
  44. package/src/benchmarks/wasm-vector-benchmark.ts +250 -0
  45. package/src/cli/agentdb-cli.ts +40 -1
  46. package/src/controllers/EmbeddingService.ts +7 -3
  47. package/src/controllers/EnhancedEmbeddingService.ts +159 -0
  48. package/src/controllers/WASMVectorSearch.ts +308 -0
  49. package/src/controllers/index.ts +4 -0
  50. package/src/db-fallback.ts +38 -10
  51. package/src/examples/wasm-vector-usage.ts +245 -0
  52. package/src/mcp/agentdb-mcp-server.ts +59 -28
  53. package/src/optimizations/BatchOperations.ts +55 -24
  54. package/src/security/input-validation.ts +377 -0
  55. package/src/tests/wasm-vector-search.test.ts +240 -0
@@ -0,0 +1,240 @@
1
+ /**
2
+ * WASM Vector Search Tests
3
+ *
4
+ * Integration tests for WASM-accelerated vector operations
5
+ */
6
+
7
+ import { describe, it, expect, beforeEach } from 'vitest';
8
+ import { WASMVectorSearch } from '../controllers/WASMVectorSearch.js';
9
+ import { EnhancedEmbeddingService } from '../controllers/EnhancedEmbeddingService.js';
10
+
11
+ describe('WASMVectorSearch', () => {
12
+ let mockDb: any;
13
+ let wasmSearch: WASMVectorSearch;
14
+
15
+ beforeEach(() => {
16
+ mockDb = {
17
+ prepare: () => ({ all: () => [], get: () => null, run: () => ({ lastInsertRowid: 1, changes: 1 }) }),
18
+ exec: () => {},
19
+ };
20
+
21
+ wasmSearch = new WASMVectorSearch(mockDb);
22
+ });
23
+
24
+ describe('Cosine Similarity', () => {
25
+ it('should calculate cosine similarity correctly', () => {
26
+ const vectorA = new Float32Array([1, 0, 0]);
27
+ const vectorB = new Float32Array([1, 0, 0]);
28
+
29
+ const similarity = wasmSearch.cosineSimilarity(vectorA, vectorB);
30
+ expect(similarity).toBeCloseTo(1.0, 5);
31
+ });
32
+
33
+ it('should handle orthogonal vectors', () => {
34
+ const vectorA = new Float32Array([1, 0, 0]);
35
+ const vectorB = new Float32Array([0, 1, 0]);
36
+
37
+ const similarity = wasmSearch.cosineSimilarity(vectorA, vectorB);
38
+ expect(similarity).toBeCloseTo(0.0, 5);
39
+ });
40
+
41
+ it('should handle opposite vectors', () => {
42
+ const vectorA = new Float32Array([1, 0, 0]);
43
+ const vectorB = new Float32Array([-1, 0, 0]);
44
+
45
+ const similarity = wasmSearch.cosineSimilarity(vectorA, vectorB);
46
+ expect(similarity).toBeCloseTo(-1.0, 5);
47
+ });
48
+
49
+ it('should throw error for mismatched dimensions', () => {
50
+ const vectorA = new Float32Array([1, 0, 0]);
51
+ const vectorB = new Float32Array([1, 0]);
52
+
53
+ expect(() => wasmSearch.cosineSimilarity(vectorA, vectorB)).toThrow();
54
+ });
55
+ });
56
+
57
+ describe('Batch Similarity', () => {
58
+ it('should calculate batch similarities', () => {
59
+ const query = new Float32Array([1, 0, 0]);
60
+ const vectors = [
61
+ new Float32Array([1, 0, 0]),
62
+ new Float32Array([0, 1, 0]),
63
+ new Float32Array([0, 0, 1]),
64
+ ];
65
+
66
+ const similarities = wasmSearch.batchSimilarity(query, vectors);
67
+
68
+ expect(similarities).toHaveLength(3);
69
+ expect(similarities[0]).toBeCloseTo(1.0, 5);
70
+ expect(similarities[1]).toBeCloseTo(0.0, 5);
71
+ expect(similarities[2]).toBeCloseTo(0.0, 5);
72
+ });
73
+
74
+ it('should handle large batches efficiently', () => {
75
+ const query = new Float32Array(384).fill(0.5);
76
+ const vectors: Float32Array[] = [];
77
+
78
+ for (let i = 0; i < 1000; i++) {
79
+ vectors.push(new Float32Array(384).fill(Math.random()));
80
+ }
81
+
82
+ const startTime = performance.now();
83
+ const similarities = wasmSearch.batchSimilarity(query, vectors);
84
+ const duration = performance.now() - startTime;
85
+
86
+ expect(similarities).toHaveLength(1000);
87
+ expect(duration).toBeLessThan(1000); // Should complete in under 1 second
88
+ });
89
+ });
90
+
91
+ describe('Vector Index', () => {
92
+ it('should build index for large datasets', () => {
93
+ const vectors: Float32Array[] = [];
94
+ const ids: number[] = [];
95
+
96
+ for (let i = 0; i < 1500; i++) {
97
+ vectors.push(new Float32Array(128).fill(Math.random()));
98
+ ids.push(i);
99
+ }
100
+
101
+ wasmSearch.buildIndex(vectors, ids);
102
+
103
+ const stats = wasmSearch.getStats();
104
+ expect(stats.indexBuilt).toBe(true);
105
+ expect(stats.indexSize).toBe(1500);
106
+ });
107
+
108
+ it('should search index correctly', () => {
109
+ const vectors: Float32Array[] = [
110
+ new Float32Array([1, 0, 0]),
111
+ new Float32Array([0, 1, 0]),
112
+ new Float32Array([0, 0, 1]),
113
+ new Float32Array([0.7, 0.7, 0]),
114
+ ];
115
+ const ids = [1, 2, 3, 4];
116
+
117
+ wasmSearch = new WASMVectorSearch(mockDb, { indexThreshold: 3 });
118
+ wasmSearch.buildIndex(vectors, ids);
119
+
120
+ const query = new Float32Array([1, 0, 0]);
121
+ const results = wasmSearch.searchIndex(query, 2);
122
+
123
+ expect(results).toHaveLength(2);
124
+ expect(results[0].id).toBe(1); // Most similar
125
+ expect(results[0].similarity).toBeCloseTo(1.0, 5);
126
+ });
127
+
128
+ it('should clear index', () => {
129
+ const vectors = [new Float32Array([1, 0, 0])];
130
+ const ids = [1];
131
+
132
+ wasmSearch = new WASMVectorSearch(mockDb, { indexThreshold: 0 });
133
+ wasmSearch.buildIndex(vectors, ids);
134
+
135
+ let stats = wasmSearch.getStats();
136
+ expect(stats.indexBuilt).toBe(true);
137
+
138
+ wasmSearch.clearIndex();
139
+ stats = wasmSearch.getStats();
140
+ expect(stats.indexBuilt).toBe(false);
141
+ });
142
+ });
143
+
144
+ describe('Statistics', () => {
145
+ it('should report correct stats', () => {
146
+ const stats = wasmSearch.getStats();
147
+
148
+ expect(stats).toHaveProperty('wasmAvailable');
149
+ expect(stats).toHaveProperty('simdAvailable');
150
+ expect(stats).toHaveProperty('indexBuilt');
151
+ expect(stats).toHaveProperty('indexSize');
152
+ expect(typeof stats.wasmAvailable).toBe('boolean');
153
+ expect(typeof stats.simdAvailable).toBe('boolean');
154
+ });
155
+ });
156
+ });
157
+
158
+ describe('EnhancedEmbeddingService', () => {
159
+ let service: EnhancedEmbeddingService;
160
+
161
+ beforeEach(async () => {
162
+ service = new EnhancedEmbeddingService({
163
+ model: 'mock-model',
164
+ dimension: 384,
165
+ provider: 'local',
166
+ enableWASM: true,
167
+ enableBatchProcessing: true,
168
+ batchSize: 50,
169
+ });
170
+
171
+ await service.initialize();
172
+ });
173
+
174
+ describe('Batch Processing', () => {
175
+ it('should embed batch of texts', async () => {
176
+ const texts = ['hello', 'world', 'test', 'embedding', 'service'];
177
+ const embeddings = await service.embedBatch(texts);
178
+
179
+ expect(embeddings).toHaveLength(5);
180
+ embeddings.forEach(emb => {
181
+ expect(emb).toBeInstanceOf(Float32Array);
182
+ expect(emb.length).toBe(384);
183
+ });
184
+ });
185
+
186
+ it('should handle large batches efficiently', async () => {
187
+ const texts = Array.from({ length: 200 }, (_, i) => `text ${i}`);
188
+
189
+ const startTime = performance.now();
190
+ const embeddings = await service.embedBatch(texts);
191
+ const duration = performance.now() - startTime;
192
+
193
+ expect(embeddings).toHaveLength(200);
194
+ console.log(`Batch embedding of 200 texts took ${duration.toFixed(2)}ms`);
195
+ });
196
+ });
197
+
198
+ describe('Similarity', () => {
199
+ it('should calculate text similarity', async () => {
200
+ const similarity = await service.similarity('hello world', 'hello world');
201
+ expect(similarity).toBeCloseTo(1.0, 5);
202
+ });
203
+
204
+ it('should find most similar texts', async () => {
205
+ const corpus = [
206
+ 'machine learning',
207
+ 'artificial intelligence',
208
+ 'deep learning',
209
+ 'cooking recipes',
210
+ 'neural networks',
211
+ ];
212
+
213
+ const results = await service.findMostSimilar('AI and ML', corpus, 3);
214
+
215
+ expect(results).toHaveLength(3);
216
+ expect(results[0]).toHaveProperty('text');
217
+ expect(results[0]).toHaveProperty('similarity');
218
+ expect(results[0]).toHaveProperty('index');
219
+
220
+ // Note: Mock embeddings are deterministic but not semantic,
221
+ // so we just verify the structure is correct
222
+ results.forEach(result => {
223
+ expect(result.similarity).toBeGreaterThanOrEqual(-1);
224
+ expect(result.similarity).toBeLessThanOrEqual(1);
225
+ expect(corpus).toContain(result.text);
226
+ });
227
+ });
228
+ });
229
+
230
+ describe('Statistics', () => {
231
+ it('should provide service statistics', () => {
232
+ const stats = service.getStats();
233
+
234
+ expect(stats).toHaveProperty('cacheSize');
235
+ expect(stats).toHaveProperty('wasmEnabled');
236
+ expect(stats).toHaveProperty('simdEnabled');
237
+ expect(typeof stats.cacheSize).toBe('number');
238
+ });
239
+ });
240
+ });