ruvector 0.1.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.
- package/.claude-flow/metrics/agent-metrics.json +1 -0
- package/.claude-flow/metrics/performance.json +87 -0
- package/.claude-flow/metrics/task-metrics.json +10 -0
- package/PACKAGE_SUMMARY.md +409 -0
- package/README.md +132 -0
- package/bin/cli.js +287 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +88 -0
- package/dist/types.d.ts +145 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/examples/api-usage.js +211 -0
- package/examples/cli-demo.sh +85 -0
- package/package.json +61 -0
package/bin/cli.js
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { Command } = require('commander');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
const ora = require('ora');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
// Import ruvector
|
|
10
|
+
let VectorDB, getVersion, getImplementationType;
|
|
11
|
+
try {
|
|
12
|
+
const ruvector = require('../dist/index.js');
|
|
13
|
+
VectorDB = ruvector.VectorDB;
|
|
14
|
+
getVersion = ruvector.getVersion;
|
|
15
|
+
getImplementationType = ruvector.getImplementationType;
|
|
16
|
+
} catch (e) {
|
|
17
|
+
console.error(chalk.red('Error: Failed to load ruvector. Please run: npm run build'));
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const program = new Command();
|
|
22
|
+
|
|
23
|
+
// Version and description
|
|
24
|
+
const versionInfo = getVersion();
|
|
25
|
+
program
|
|
26
|
+
.name('ruvector')
|
|
27
|
+
.description(`${chalk.cyan('ruvector')} - High-performance vector database CLI\nUsing: ${chalk.yellow(versionInfo.implementation)} implementation`)
|
|
28
|
+
.version(versionInfo.version);
|
|
29
|
+
|
|
30
|
+
// Create database
|
|
31
|
+
program
|
|
32
|
+
.command('create <path>')
|
|
33
|
+
.description('Create a new vector database')
|
|
34
|
+
.option('-d, --dimension <number>', 'Vector dimension', '384')
|
|
35
|
+
.option('-m, --metric <type>', 'Distance metric (cosine|euclidean|dot)', 'cosine')
|
|
36
|
+
.action((dbPath, options) => {
|
|
37
|
+
const spinner = ora('Creating database...').start();
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const dimension = parseInt(options.dimension);
|
|
41
|
+
const db = new VectorDB({
|
|
42
|
+
dimension,
|
|
43
|
+
metric: options.metric,
|
|
44
|
+
path: dbPath,
|
|
45
|
+
autoPersist: true
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
db.save(dbPath);
|
|
49
|
+
spinner.succeed(chalk.green(`Database created: ${dbPath}`));
|
|
50
|
+
console.log(chalk.gray(` Dimension: ${dimension}`));
|
|
51
|
+
console.log(chalk.gray(` Metric: ${options.metric}`));
|
|
52
|
+
console.log(chalk.gray(` Implementation: ${getImplementationType()}`));
|
|
53
|
+
} catch (error) {
|
|
54
|
+
spinner.fail(chalk.red('Failed to create database'));
|
|
55
|
+
console.error(chalk.red(error.message));
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Insert vectors
|
|
61
|
+
program
|
|
62
|
+
.command('insert <database> <file>')
|
|
63
|
+
.description('Insert vectors from JSON file')
|
|
64
|
+
.option('-b, --batch-size <number>', 'Batch size for insertion', '1000')
|
|
65
|
+
.action((dbPath, file, options) => {
|
|
66
|
+
const spinner = ora('Loading database...').start();
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
// Read database metadata to get dimension
|
|
70
|
+
let dimension = 384; // default
|
|
71
|
+
if (fs.existsSync(dbPath)) {
|
|
72
|
+
const dbData = fs.readFileSync(dbPath, 'utf8');
|
|
73
|
+
const parsed = JSON.parse(dbData);
|
|
74
|
+
dimension = parsed.dimension || 384;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const db = new VectorDB({ dimension });
|
|
78
|
+
|
|
79
|
+
if (fs.existsSync(dbPath)) {
|
|
80
|
+
db.load(dbPath);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
spinner.text = 'Reading vectors...';
|
|
84
|
+
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
85
|
+
const vectors = Array.isArray(data) ? data : [data];
|
|
86
|
+
|
|
87
|
+
spinner.text = `Inserting ${vectors.length} vectors...`;
|
|
88
|
+
const batchSize = parseInt(options.batchSize);
|
|
89
|
+
|
|
90
|
+
for (let i = 0; i < vectors.length; i += batchSize) {
|
|
91
|
+
const batch = vectors.slice(i, i + batchSize);
|
|
92
|
+
db.insertBatch(batch);
|
|
93
|
+
spinner.text = `Inserted ${Math.min(i + batchSize, vectors.length)}/${vectors.length} vectors...`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
db.save(dbPath);
|
|
97
|
+
spinner.succeed(chalk.green(`Inserted ${vectors.length} vectors`));
|
|
98
|
+
|
|
99
|
+
const stats = db.stats();
|
|
100
|
+
console.log(chalk.gray(` Total vectors: ${stats.count}`));
|
|
101
|
+
} catch (error) {
|
|
102
|
+
spinner.fail(chalk.red('Failed to insert vectors'));
|
|
103
|
+
console.error(chalk.red(error.message));
|
|
104
|
+
process.exit(1);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Search vectors
|
|
109
|
+
program
|
|
110
|
+
.command('search <database>')
|
|
111
|
+
.description('Search for similar vectors')
|
|
112
|
+
.requiredOption('-v, --vector <json>', 'Query vector as JSON array')
|
|
113
|
+
.option('-k, --top-k <number>', 'Number of results', '10')
|
|
114
|
+
.option('-t, --threshold <number>', 'Similarity threshold', '0.0')
|
|
115
|
+
.option('-f, --filter <json>', 'Metadata filter as JSON')
|
|
116
|
+
.action((dbPath, options) => {
|
|
117
|
+
const spinner = ora('Loading database...').start();
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
// Read database metadata
|
|
121
|
+
const dbData = fs.readFileSync(dbPath, 'utf8');
|
|
122
|
+
const parsed = JSON.parse(dbData);
|
|
123
|
+
const dimension = parsed.dimension || 384;
|
|
124
|
+
|
|
125
|
+
const db = new VectorDB({ dimension });
|
|
126
|
+
db.load(dbPath);
|
|
127
|
+
|
|
128
|
+
spinner.text = 'Searching...';
|
|
129
|
+
|
|
130
|
+
const vector = JSON.parse(options.vector);
|
|
131
|
+
const query = {
|
|
132
|
+
vector,
|
|
133
|
+
k: parseInt(options.topK),
|
|
134
|
+
threshold: parseFloat(options.threshold)
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
if (options.filter) {
|
|
138
|
+
query.filter = JSON.parse(options.filter);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const results = db.search(query);
|
|
142
|
+
spinner.succeed(chalk.green(`Found ${results.length} results`));
|
|
143
|
+
|
|
144
|
+
console.log(chalk.cyan('\nSearch Results:'));
|
|
145
|
+
results.forEach((result, i) => {
|
|
146
|
+
console.log(chalk.white(`\n${i + 1}. ID: ${result.id}`));
|
|
147
|
+
console.log(chalk.yellow(` Score: ${result.score.toFixed(4)}`));
|
|
148
|
+
if (result.metadata) {
|
|
149
|
+
console.log(chalk.gray(` Metadata: ${JSON.stringify(result.metadata)}`));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
} catch (error) {
|
|
153
|
+
spinner.fail(chalk.red('Failed to search'));
|
|
154
|
+
console.error(chalk.red(error.message));
|
|
155
|
+
process.exit(1);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Show stats
|
|
160
|
+
program
|
|
161
|
+
.command('stats <database>')
|
|
162
|
+
.description('Show database statistics')
|
|
163
|
+
.action((dbPath) => {
|
|
164
|
+
const spinner = ora('Loading database...').start();
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
const dbData = fs.readFileSync(dbPath, 'utf8');
|
|
168
|
+
const parsed = JSON.parse(dbData);
|
|
169
|
+
const dimension = parsed.dimension || 384;
|
|
170
|
+
|
|
171
|
+
const db = new VectorDB({ dimension });
|
|
172
|
+
db.load(dbPath);
|
|
173
|
+
|
|
174
|
+
const stats = db.stats();
|
|
175
|
+
spinner.succeed(chalk.green('Database statistics'));
|
|
176
|
+
|
|
177
|
+
console.log(chalk.cyan('\nDatabase Stats:'));
|
|
178
|
+
console.log(chalk.white(` Vector Count: ${chalk.yellow(stats.count)}`));
|
|
179
|
+
console.log(chalk.white(` Dimension: ${chalk.yellow(stats.dimension)}`));
|
|
180
|
+
console.log(chalk.white(` Metric: ${chalk.yellow(stats.metric)}`));
|
|
181
|
+
console.log(chalk.white(` Implementation: ${chalk.yellow(getImplementationType())}`));
|
|
182
|
+
|
|
183
|
+
if (stats.memoryUsage) {
|
|
184
|
+
const mb = (stats.memoryUsage / (1024 * 1024)).toFixed(2);
|
|
185
|
+
console.log(chalk.white(` Memory Usage: ${chalk.yellow(mb + ' MB')}`));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const fileStats = fs.statSync(dbPath);
|
|
189
|
+
const fileMb = (fileStats.size / (1024 * 1024)).toFixed(2);
|
|
190
|
+
console.log(chalk.white(` File Size: ${chalk.yellow(fileMb + ' MB')}`));
|
|
191
|
+
} catch (error) {
|
|
192
|
+
spinner.fail(chalk.red('Failed to load database'));
|
|
193
|
+
console.error(chalk.red(error.message));
|
|
194
|
+
process.exit(1);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Benchmark
|
|
199
|
+
program
|
|
200
|
+
.command('benchmark')
|
|
201
|
+
.description('Run performance benchmarks')
|
|
202
|
+
.option('-d, --dimension <number>', 'Vector dimension', '384')
|
|
203
|
+
.option('-n, --num-vectors <number>', 'Number of vectors', '10000')
|
|
204
|
+
.option('-q, --num-queries <number>', 'Number of queries', '1000')
|
|
205
|
+
.action((options) => {
|
|
206
|
+
console.log(chalk.cyan('\nruvector Performance Benchmark'));
|
|
207
|
+
console.log(chalk.gray(`Implementation: ${getImplementationType()}\n`));
|
|
208
|
+
|
|
209
|
+
const dimension = parseInt(options.dimension);
|
|
210
|
+
const numVectors = parseInt(options.numVectors);
|
|
211
|
+
const numQueries = parseInt(options.numQueries);
|
|
212
|
+
|
|
213
|
+
let spinner = ora('Creating database...').start();
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
const db = new VectorDB({ dimension, metric: 'cosine' });
|
|
217
|
+
spinner.succeed();
|
|
218
|
+
|
|
219
|
+
// Insert benchmark
|
|
220
|
+
spinner = ora(`Inserting ${numVectors} vectors...`).start();
|
|
221
|
+
const insertStart = Date.now();
|
|
222
|
+
|
|
223
|
+
const vectors = [];
|
|
224
|
+
for (let i = 0; i < numVectors; i++) {
|
|
225
|
+
vectors.push({
|
|
226
|
+
id: `vec_${i}`,
|
|
227
|
+
vector: Array.from({ length: dimension }, () => Math.random()),
|
|
228
|
+
metadata: { index: i, batch: Math.floor(i / 1000) }
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
db.insertBatch(vectors);
|
|
233
|
+
const insertTime = Date.now() - insertStart;
|
|
234
|
+
const insertRate = (numVectors / (insertTime / 1000)).toFixed(0);
|
|
235
|
+
|
|
236
|
+
spinner.succeed(chalk.green(`Inserted ${numVectors} vectors in ${insertTime}ms`));
|
|
237
|
+
console.log(chalk.gray(` Rate: ${chalk.yellow(insertRate)} vectors/sec`));
|
|
238
|
+
|
|
239
|
+
// Search benchmark
|
|
240
|
+
spinner = ora(`Running ${numQueries} searches...`).start();
|
|
241
|
+
const searchStart = Date.now();
|
|
242
|
+
|
|
243
|
+
for (let i = 0; i < numQueries; i++) {
|
|
244
|
+
const query = {
|
|
245
|
+
vector: Array.from({ length: dimension }, () => Math.random()),
|
|
246
|
+
k: 10
|
|
247
|
+
};
|
|
248
|
+
db.search(query);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const searchTime = Date.now() - searchStart;
|
|
252
|
+
const searchRate = (numQueries / (searchTime / 1000)).toFixed(0);
|
|
253
|
+
const avgLatency = (searchTime / numQueries).toFixed(2);
|
|
254
|
+
|
|
255
|
+
spinner.succeed(chalk.green(`Completed ${numQueries} searches in ${searchTime}ms`));
|
|
256
|
+
console.log(chalk.gray(` Rate: ${chalk.yellow(searchRate)} queries/sec`));
|
|
257
|
+
console.log(chalk.gray(` Avg Latency: ${chalk.yellow(avgLatency)}ms`));
|
|
258
|
+
|
|
259
|
+
// Stats
|
|
260
|
+
const stats = db.stats();
|
|
261
|
+
console.log(chalk.cyan('\nFinal Stats:'));
|
|
262
|
+
console.log(chalk.white(` Vector Count: ${chalk.yellow(stats.count)}`));
|
|
263
|
+
console.log(chalk.white(` Dimension: ${chalk.yellow(stats.dimension)}`));
|
|
264
|
+
console.log(chalk.white(` Implementation: ${chalk.yellow(getImplementationType())}`));
|
|
265
|
+
|
|
266
|
+
} catch (error) {
|
|
267
|
+
spinner.fail(chalk.red('Benchmark failed'));
|
|
268
|
+
console.error(chalk.red(error.message));
|
|
269
|
+
process.exit(1);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// Info command
|
|
274
|
+
program
|
|
275
|
+
.command('info')
|
|
276
|
+
.description('Show ruvector information')
|
|
277
|
+
.action(() => {
|
|
278
|
+
const info = getVersion();
|
|
279
|
+
console.log(chalk.cyan('\nruvector Information'));
|
|
280
|
+
console.log(chalk.white(` Version: ${chalk.yellow(info.version)}`));
|
|
281
|
+
console.log(chalk.white(` Implementation: ${chalk.yellow(info.implementation)}`));
|
|
282
|
+
console.log(chalk.white(` Node Version: ${chalk.yellow(process.version)}`));
|
|
283
|
+
console.log(chalk.white(` Platform: ${chalk.yellow(process.platform)}`));
|
|
284
|
+
console.log(chalk.white(` Architecture: ${chalk.yellow(process.arch)}`));
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
program.parse();
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ruvector - High-performance vector database for Node.js
|
|
3
|
+
*
|
|
4
|
+
* This package automatically detects and uses the best available implementation:
|
|
5
|
+
* 1. Native (Rust-based, fastest) - if available for your platform
|
|
6
|
+
* 2. WASM (WebAssembly, universal fallback) - works everywhere
|
|
7
|
+
*/
|
|
8
|
+
export * from './types';
|
|
9
|
+
declare let implementation: any;
|
|
10
|
+
/**
|
|
11
|
+
* Get the current implementation type
|
|
12
|
+
*/
|
|
13
|
+
export declare function getImplementationType(): 'native' | 'wasm';
|
|
14
|
+
/**
|
|
15
|
+
* Check if native implementation is being used
|
|
16
|
+
*/
|
|
17
|
+
export declare function isNative(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Check if WASM implementation is being used
|
|
20
|
+
*/
|
|
21
|
+
export declare function isWasm(): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Get version information
|
|
24
|
+
*/
|
|
25
|
+
export declare function getVersion(): {
|
|
26
|
+
version: string;
|
|
27
|
+
implementation: string;
|
|
28
|
+
};
|
|
29
|
+
export declare const VectorDB: any;
|
|
30
|
+
export default implementation;
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,cAAc,SAAS,CAAC;AAExB,QAAA,IAAI,cAAc,EAAE,GAAG,CAAC;AA+BxB;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,QAAQ,GAAG,MAAM,CAEzD;AAED;;GAEG;AACH,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAED;;GAEG;AACH,wBAAgB,MAAM,IAAI,OAAO,CAEhC;AAED;;GAEG;AACH,wBAAgB,UAAU,IAAI;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,MAAM,CAAA;CAAE,CAMxE;AAGD,eAAO,MAAM,QAAQ,KAA0B,CAAC;AAGhD,eAAe,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ruvector - High-performance vector database for Node.js
|
|
4
|
+
*
|
|
5
|
+
* This package automatically detects and uses the best available implementation:
|
|
6
|
+
* 1. Native (Rust-based, fastest) - if available for your platform
|
|
7
|
+
* 2. WASM (WebAssembly, universal fallback) - works everywhere
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
+
}
|
|
15
|
+
Object.defineProperty(o, k2, desc);
|
|
16
|
+
}) : (function(o, m, k, k2) {
|
|
17
|
+
if (k2 === undefined) k2 = k;
|
|
18
|
+
o[k2] = m[k];
|
|
19
|
+
}));
|
|
20
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
21
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
22
|
+
};
|
|
23
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
+
exports.VectorDB = void 0;
|
|
25
|
+
exports.getImplementationType = getImplementationType;
|
|
26
|
+
exports.isNative = isNative;
|
|
27
|
+
exports.isWasm = isWasm;
|
|
28
|
+
exports.getVersion = getVersion;
|
|
29
|
+
__exportStar(require("./types"), exports);
|
|
30
|
+
let implementation;
|
|
31
|
+
let implementationType = 'wasm';
|
|
32
|
+
try {
|
|
33
|
+
// Try to load native module first
|
|
34
|
+
implementation = require('@ruvector/core');
|
|
35
|
+
implementationType = 'native';
|
|
36
|
+
// Verify it's actually working
|
|
37
|
+
if (typeof implementation.VectorDB !== 'function') {
|
|
38
|
+
throw new Error('Native module loaded but VectorDB not found');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
// Fallback to WASM
|
|
43
|
+
if (process.env.RUVECTOR_DEBUG) {
|
|
44
|
+
console.warn('[ruvector] Native module not available:', e.message);
|
|
45
|
+
console.warn('[ruvector] Falling back to WASM implementation');
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
implementation = require('@ruvector/wasm');
|
|
49
|
+
implementationType = 'wasm';
|
|
50
|
+
}
|
|
51
|
+
catch (wasmError) {
|
|
52
|
+
throw new Error(`Failed to load ruvector: Neither native nor WASM implementation available.\n` +
|
|
53
|
+
`Native error: ${e.message}\n` +
|
|
54
|
+
`WASM error: ${wasmError.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get the current implementation type
|
|
59
|
+
*/
|
|
60
|
+
function getImplementationType() {
|
|
61
|
+
return implementationType;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Check if native implementation is being used
|
|
65
|
+
*/
|
|
66
|
+
function isNative() {
|
|
67
|
+
return implementationType === 'native';
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Check if WASM implementation is being used
|
|
71
|
+
*/
|
|
72
|
+
function isWasm() {
|
|
73
|
+
return implementationType === 'wasm';
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get version information
|
|
77
|
+
*/
|
|
78
|
+
function getVersion() {
|
|
79
|
+
const pkg = require('../package.json');
|
|
80
|
+
return {
|
|
81
|
+
version: pkg.version,
|
|
82
|
+
implementation: implementationType
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Export the VectorDB class
|
|
86
|
+
exports.VectorDB = implementation.VectorDB;
|
|
87
|
+
// Export everything from the implementation
|
|
88
|
+
exports.default = implementation;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector entry representing a document with its embedding
|
|
3
|
+
*/
|
|
4
|
+
export interface VectorEntry {
|
|
5
|
+
/** Unique identifier for the vector */
|
|
6
|
+
id: string;
|
|
7
|
+
/** Vector embedding (array of floats) */
|
|
8
|
+
vector: number[];
|
|
9
|
+
/** Optional metadata associated with the vector */
|
|
10
|
+
metadata?: Record<string, any>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Search query parameters
|
|
14
|
+
*/
|
|
15
|
+
export interface SearchQuery {
|
|
16
|
+
/** Query vector to search for */
|
|
17
|
+
vector: number[];
|
|
18
|
+
/** Number of results to return */
|
|
19
|
+
k?: number;
|
|
20
|
+
/** Optional metadata filters */
|
|
21
|
+
filter?: Record<string, any>;
|
|
22
|
+
/** Minimum similarity threshold (0-1) */
|
|
23
|
+
threshold?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Search result containing matched vector and similarity score
|
|
27
|
+
*/
|
|
28
|
+
export interface SearchResult {
|
|
29
|
+
/** ID of the matched vector */
|
|
30
|
+
id: string;
|
|
31
|
+
/** Similarity score (0-1, higher is better) */
|
|
32
|
+
score: number;
|
|
33
|
+
/** Vector data */
|
|
34
|
+
vector: number[];
|
|
35
|
+
/** Associated metadata */
|
|
36
|
+
metadata?: Record<string, any>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Database configuration options
|
|
40
|
+
*/
|
|
41
|
+
export interface DbOptions {
|
|
42
|
+
/** Vector dimension size */
|
|
43
|
+
dimension: number;
|
|
44
|
+
/** Distance metric to use */
|
|
45
|
+
metric?: 'cosine' | 'euclidean' | 'dot';
|
|
46
|
+
/** Path to persist database */
|
|
47
|
+
path?: string;
|
|
48
|
+
/** Enable auto-persistence */
|
|
49
|
+
autoPersist?: boolean;
|
|
50
|
+
/** HNSW index parameters */
|
|
51
|
+
hnsw?: {
|
|
52
|
+
/** Maximum number of connections per layer */
|
|
53
|
+
m?: number;
|
|
54
|
+
/** Size of the dynamic candidate list */
|
|
55
|
+
efConstruction?: number;
|
|
56
|
+
/** Size of the dynamic candidate list for search */
|
|
57
|
+
efSearch?: number;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Database statistics
|
|
62
|
+
*/
|
|
63
|
+
export interface DbStats {
|
|
64
|
+
/** Total number of vectors */
|
|
65
|
+
count: number;
|
|
66
|
+
/** Vector dimension */
|
|
67
|
+
dimension: number;
|
|
68
|
+
/** Distance metric */
|
|
69
|
+
metric: string;
|
|
70
|
+
/** Memory usage in bytes */
|
|
71
|
+
memoryUsage?: number;
|
|
72
|
+
/** Index type */
|
|
73
|
+
indexType?: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Main VectorDB class interface
|
|
77
|
+
*/
|
|
78
|
+
export interface VectorDB {
|
|
79
|
+
/**
|
|
80
|
+
* Create a new vector database
|
|
81
|
+
* @param options Database configuration
|
|
82
|
+
*/
|
|
83
|
+
new (options: DbOptions): VectorDB;
|
|
84
|
+
/**
|
|
85
|
+
* Insert a single vector
|
|
86
|
+
* @param entry Vector entry to insert
|
|
87
|
+
*/
|
|
88
|
+
insert(entry: VectorEntry): void;
|
|
89
|
+
/**
|
|
90
|
+
* Insert multiple vectors in batch
|
|
91
|
+
* @param entries Array of vector entries
|
|
92
|
+
*/
|
|
93
|
+
insertBatch(entries: VectorEntry[]): void;
|
|
94
|
+
/**
|
|
95
|
+
* Search for similar vectors
|
|
96
|
+
* @param query Search query parameters
|
|
97
|
+
* @returns Array of search results
|
|
98
|
+
*/
|
|
99
|
+
search(query: SearchQuery): SearchResult[];
|
|
100
|
+
/**
|
|
101
|
+
* Get vector by ID
|
|
102
|
+
* @param id Vector ID
|
|
103
|
+
* @returns Vector entry or null
|
|
104
|
+
*/
|
|
105
|
+
get(id: string): VectorEntry | null;
|
|
106
|
+
/**
|
|
107
|
+
* Delete vector by ID
|
|
108
|
+
* @param id Vector ID
|
|
109
|
+
* @returns true if deleted, false if not found
|
|
110
|
+
*/
|
|
111
|
+
delete(id: string): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Update vector metadata
|
|
114
|
+
* @param id Vector ID
|
|
115
|
+
* @param metadata New metadata
|
|
116
|
+
*/
|
|
117
|
+
updateMetadata(id: string, metadata: Record<string, any>): void;
|
|
118
|
+
/**
|
|
119
|
+
* Get database statistics
|
|
120
|
+
*/
|
|
121
|
+
stats(): DbStats;
|
|
122
|
+
/**
|
|
123
|
+
* Save database to disk
|
|
124
|
+
* @param path Optional path (uses configured path if not provided)
|
|
125
|
+
*/
|
|
126
|
+
save(path?: string): void;
|
|
127
|
+
/**
|
|
128
|
+
* Load database from disk
|
|
129
|
+
* @param path Path to database file
|
|
130
|
+
*/
|
|
131
|
+
load(path: string): void;
|
|
132
|
+
/**
|
|
133
|
+
* Clear all vectors from database
|
|
134
|
+
*/
|
|
135
|
+
clear(): void;
|
|
136
|
+
/**
|
|
137
|
+
* Build HNSW index for faster search
|
|
138
|
+
*/
|
|
139
|
+
buildIndex(): void;
|
|
140
|
+
/**
|
|
141
|
+
* Optimize database (rebuild indices, compact storage)
|
|
142
|
+
*/
|
|
143
|
+
optimize(): void;
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,uCAAuC;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,yCAAyC;IACzC,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,mDAAmD;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,iCAAiC;IACjC,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,kCAAkC;IAClC,CAAC,CA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