ruvector 0.1.21 → 0.1.23
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 +1479 -250
- package/bin/cli.js +858 -0
- package/dist/index.d.ts +20 -84
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +77 -330
- 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 +31 -49
- package/bin/ruvector.js +0 -805
- package/dist/index.d.mts +0 -95
- package/dist/index.mjs +0 -306
package/dist/index.d.ts
CHANGED
|
@@ -1,95 +1,31 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
interface Vector {
|
|
6
|
-
id: string;
|
|
7
|
-
values: number[];
|
|
8
|
-
metadata?: Record<string, any>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface SearchResult {
|
|
12
|
-
id: string;
|
|
13
|
-
score: number;
|
|
14
|
-
metadata?: Record<string, any>;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
interface IndexStats {
|
|
18
|
-
vectorCount: number;
|
|
19
|
-
dimension: number;
|
|
20
|
-
indexType: string;
|
|
21
|
-
memoryUsage?: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
interface CreateIndexOptions {
|
|
25
|
-
dimension: number;
|
|
26
|
-
metric?: 'cosine' | 'euclidean' | 'dot';
|
|
27
|
-
indexType?: 'flat' | 'hnsw';
|
|
28
|
-
hnswConfig?: {
|
|
29
|
-
m?: number;
|
|
30
|
-
efConstruction?: number;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface SearchOptions {
|
|
35
|
-
k?: number;
|
|
36
|
-
ef?: number;
|
|
37
|
-
filter?: Record<string, any>;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface BatchInsertOptions {
|
|
41
|
-
batchSize?: number;
|
|
42
|
-
progressCallback?: (progress: number) => void;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Backend information
|
|
47
|
-
*/
|
|
48
|
-
interface BackendInfo {
|
|
49
|
-
type: 'native' | 'wasm';
|
|
50
|
-
version: string;
|
|
51
|
-
features: string[];
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* rUvector - High-performance vector database
|
|
2
|
+
* ruvector - High-performance vector database for Node.js
|
|
56
3
|
*
|
|
57
|
-
*
|
|
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
|
|
58
7
|
*/
|
|
59
|
-
|
|
8
|
+
export * from './types';
|
|
9
|
+
declare let implementation: any;
|
|
60
10
|
/**
|
|
61
|
-
*
|
|
11
|
+
* Get the current implementation type
|
|
62
12
|
*/
|
|
63
|
-
declare
|
|
64
|
-
private index;
|
|
65
|
-
constructor(options: CreateIndexOptions);
|
|
66
|
-
insert(vector: Vector): Promise<void>;
|
|
67
|
-
insertBatch(vectors: Vector[], options?: BatchInsertOptions): Promise<void>;
|
|
68
|
-
search(query: number[], options?: SearchOptions): Promise<SearchResult[]>;
|
|
69
|
-
get(id: string): Promise<Vector | null>;
|
|
70
|
-
delete(id: string): Promise<boolean>;
|
|
71
|
-
stats(): Promise<IndexStats>;
|
|
72
|
-
save(path: string): Promise<void>;
|
|
73
|
-
static load(path: string): Promise<VectorIndex>;
|
|
74
|
-
clear(): Promise<void>;
|
|
75
|
-
optimize(): Promise<void>;
|
|
76
|
-
}
|
|
13
|
+
export declare function getImplementationType(): 'native' | 'wasm';
|
|
77
14
|
/**
|
|
78
|
-
*
|
|
15
|
+
* Check if native implementation is being used
|
|
79
16
|
*/
|
|
80
|
-
declare
|
|
81
|
-
cosineSimilarity(a: number[], b: number[]): number;
|
|
82
|
-
euclideanDistance(a: number[], b: number[]): number;
|
|
83
|
-
normalize(vector: number[]): number[];
|
|
84
|
-
randomVector(dimension: number): number[];
|
|
85
|
-
};
|
|
17
|
+
export declare function isNative(): boolean;
|
|
86
18
|
/**
|
|
87
|
-
*
|
|
19
|
+
* Check if WASM implementation is being used
|
|
88
20
|
*/
|
|
89
|
-
declare function
|
|
21
|
+
export declare function isWasm(): boolean;
|
|
90
22
|
/**
|
|
91
|
-
*
|
|
23
|
+
* Get version information
|
|
92
24
|
*/
|
|
93
|
-
declare function
|
|
94
|
-
|
|
95
|
-
|
|
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;AA0BxB;;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
CHANGED
|
@@ -1,336 +1,83 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
var __copyProps = (to, from, except, desc) => {
|
|
16
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
-
for (let key of __getOwnPropNames(from))
|
|
18
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
-
mod
|
|
30
|
-
));
|
|
31
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
-
|
|
33
|
-
// ../node_modules/@ruvector/core/dist/index.cjs
|
|
34
|
-
var require_dist = __commonJS({
|
|
35
|
-
"../node_modules/@ruvector/core/dist/index.cjs"(exports2, module2) {
|
|
36
|
-
"use strict";
|
|
37
|
-
Object.defineProperty(exports2, "__esModule", { value: true });
|
|
38
|
-
exports2.DistanceMetric = void 0;
|
|
39
|
-
var node_os_1 = require("os");
|
|
40
|
-
var DistanceMetric;
|
|
41
|
-
(function(DistanceMetric2) {
|
|
42
|
-
DistanceMetric2["Euclidean"] = "euclidean";
|
|
43
|
-
DistanceMetric2["Cosine"] = "cosine";
|
|
44
|
-
DistanceMetric2["DotProduct"] = "dot";
|
|
45
|
-
})(DistanceMetric || (exports2.DistanceMetric = DistanceMetric = {}));
|
|
46
|
-
function getPlatformPackage() {
|
|
47
|
-
const plat = (0, node_os_1.platform)();
|
|
48
|
-
const architecture = (0, node_os_1.arch)();
|
|
49
|
-
const packageMap = {
|
|
50
|
-
"linux-x64": "ruvector-core-linux-x64-gnu",
|
|
51
|
-
"linux-arm64": "ruvector-core-linux-arm64-gnu",
|
|
52
|
-
"darwin-x64": "ruvector-core-darwin-x64",
|
|
53
|
-
"darwin-arm64": "ruvector-core-darwin-arm64",
|
|
54
|
-
"win32-x64": "ruvector-core-win32-x64-msvc"
|
|
55
|
-
};
|
|
56
|
-
const key = `${plat}-${architecture}`;
|
|
57
|
-
const packageName = packageMap[key];
|
|
58
|
-
if (!packageName) {
|
|
59
|
-
throw new Error(`Unsupported platform: ${plat}-${architecture}. Supported platforms: ${Object.keys(packageMap).join(", ")}`);
|
|
60
|
-
}
|
|
61
|
-
return packageName;
|
|
62
|
-
}
|
|
63
|
-
function loadNativeBinding() {
|
|
64
|
-
const packageName = getPlatformPackage();
|
|
65
|
-
try {
|
|
66
|
-
return require(packageName);
|
|
67
|
-
} catch (error) {
|
|
68
|
-
try {
|
|
69
|
-
const plat = (0, node_os_1.platform)();
|
|
70
|
-
const architecture = (0, node_os_1.arch)();
|
|
71
|
-
const platformKey = `${plat}-${architecture}`;
|
|
72
|
-
const platformMap = {
|
|
73
|
-
"linux-x64": "linux-x64-gnu",
|
|
74
|
-
"linux-arm64": "linux-arm64-gnu",
|
|
75
|
-
"darwin-x64": "darwin-x64",
|
|
76
|
-
"darwin-arm64": "darwin-arm64",
|
|
77
|
-
"win32-x64": "win32-x64-msvc"
|
|
78
|
-
};
|
|
79
|
-
const localPath = `../platforms/${platformMap[platformKey]}/ruvector.node`;
|
|
80
|
-
return require(localPath);
|
|
81
|
-
} catch (fallbackError) {
|
|
82
|
-
throw new Error(`Failed to load native binding: ${error.message}
|
|
83
|
-
Fallback also failed: ${fallbackError.message}
|
|
84
|
-
Platform: ${(0, node_os_1.platform)()}-${(0, node_os_1.arch)()}
|
|
85
|
-
Expected package: ${packageName}`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
var nativeBinding = loadNativeBinding();
|
|
90
|
-
module2.exports = nativeBinding;
|
|
91
|
-
module2.exports.default = nativeBinding;
|
|
92
|
-
module2.exports.DistanceMetric = DistanceMetric;
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
// package.json
|
|
97
|
-
var require_package = __commonJS({
|
|
98
|
-
"package.json"(exports2, module2) {
|
|
99
|
-
module2.exports = {
|
|
100
|
-
name: "ruvector",
|
|
101
|
-
version: "0.1.21",
|
|
102
|
-
description: "All-in-one vector database: HNSW search, Cypher queries, GNN layers, compression. Pinecone + Neo4j + PyTorch in one package.",
|
|
103
|
-
main: "./dist/index.js",
|
|
104
|
-
types: "./dist/index.d.ts",
|
|
105
|
-
bin: {
|
|
106
|
-
ruvector: "./bin/ruvector.js"
|
|
107
|
-
},
|
|
108
|
-
exports: {
|
|
109
|
-
".": {
|
|
110
|
-
types: "./dist/index.d.ts",
|
|
111
|
-
require: "./dist/index.js",
|
|
112
|
-
import: "./dist/index.mjs"
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
files: [
|
|
116
|
-
"dist",
|
|
117
|
-
"bin",
|
|
118
|
-
"README.md"
|
|
119
|
-
],
|
|
120
|
-
scripts: {
|
|
121
|
-
build: "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
122
|
-
dev: "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
123
|
-
typecheck: "tsc --noEmit",
|
|
124
|
-
prepublishOnly: "npm run build"
|
|
125
|
-
},
|
|
126
|
-
keywords: [
|
|
127
|
-
"vector",
|
|
128
|
-
"database",
|
|
129
|
-
"embeddings",
|
|
130
|
-
"similarity-search",
|
|
131
|
-
"machine-learning",
|
|
132
|
-
"ai",
|
|
133
|
-
"rust",
|
|
134
|
-
"napi",
|
|
135
|
-
"wasm",
|
|
136
|
-
"graph",
|
|
137
|
-
"cypher",
|
|
138
|
-
"neo4j",
|
|
139
|
-
"gnn",
|
|
140
|
-
"neural-network",
|
|
141
|
-
"compression",
|
|
142
|
-
"hnsw",
|
|
143
|
-
"rag"
|
|
144
|
-
],
|
|
145
|
-
author: "rUv",
|
|
146
|
-
license: "MIT",
|
|
147
|
-
repository: {
|
|
148
|
-
type: "git",
|
|
149
|
-
url: "https://github.com/ruvnet/ruvector.git",
|
|
150
|
-
directory: "npm/ruvector"
|
|
151
|
-
},
|
|
152
|
-
dependencies: {
|
|
153
|
-
commander: "^11.1.0",
|
|
154
|
-
chalk: "^4.1.2",
|
|
155
|
-
ora: "^5.4.1",
|
|
156
|
-
"cli-table3": "^0.6.3",
|
|
157
|
-
inquirer: "^8.2.6"
|
|
158
|
-
},
|
|
159
|
-
optionalDependencies: {
|
|
160
|
-
"@ruvector/core": "^0.1.1",
|
|
161
|
-
"@ruvector/graph-node": "^0.1.0",
|
|
162
|
-
"@ruvector/graph-wasm": "^0.1.0",
|
|
163
|
-
"@ruvector/gnn-node": "^0.1.0",
|
|
164
|
-
"@ruvector/gnn-wasm": "^0.1.0"
|
|
165
|
-
},
|
|
166
|
-
devDependencies: {
|
|
167
|
-
"@types/node": "^20.10.0",
|
|
168
|
-
"@types/inquirer": "^8.2.10",
|
|
169
|
-
typescript: "^5.3.3",
|
|
170
|
-
tsup: "^8.0.0"
|
|
171
|
-
},
|
|
172
|
-
engines: {
|
|
173
|
-
node: ">=16.0.0"
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// src/index.ts
|
|
180
|
-
var index_exports = {};
|
|
181
|
-
__export(index_exports, {
|
|
182
|
-
Utils: () => Utils,
|
|
183
|
-
VectorIndex: () => VectorIndex,
|
|
184
|
-
default: () => index_default,
|
|
185
|
-
getBackendInfo: () => getBackendInfo,
|
|
186
|
-
isNativeAvailable: () => isNativeAvailable
|
|
187
|
-
});
|
|
188
|
-
module.exports = __toCommonJS(index_exports);
|
|
189
|
-
var backend;
|
|
190
|
-
var backendType = "wasm";
|
|
191
|
-
function loadBackend() {
|
|
192
|
-
if (backend) {
|
|
193
|
-
return backend;
|
|
194
|
-
}
|
|
195
|
-
try {
|
|
196
|
-
backend = require_dist();
|
|
197
|
-
backendType = "native";
|
|
198
|
-
console.log("\u2713 Loaded native rUvector bindings");
|
|
199
|
-
return backend;
|
|
200
|
-
} catch (e) {
|
|
201
|
-
try {
|
|
202
|
-
backend = require("@ruvector/wasm");
|
|
203
|
-
backendType = "wasm";
|
|
204
|
-
console.warn("\u26A0 Native bindings not available, using WASM fallback");
|
|
205
|
-
console.warn(" For better performance, install @ruvector/core");
|
|
206
|
-
return backend;
|
|
207
|
-
} catch (wasmError) {
|
|
208
|
-
throw new Error(
|
|
209
|
-
`Failed to load rUvector backend. Please ensure either @ruvector/core or @ruvector/wasm is installed.
|
|
210
|
-
Native error: ${e}
|
|
211
|
-
WASM error: ${wasmError}`
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
var VectorIndex = class _VectorIndex {
|
|
217
|
-
constructor(options) {
|
|
218
|
-
const backend2 = loadBackend();
|
|
219
|
-
this.index = new backend2.VectorIndex(options);
|
|
220
|
-
}
|
|
221
|
-
async insert(vector) {
|
|
222
|
-
return this.index.insert(vector);
|
|
223
|
-
}
|
|
224
|
-
async insertBatch(vectors, options) {
|
|
225
|
-
if (this.index.insertBatch) {
|
|
226
|
-
return this.index.insertBatch(vectors, options);
|
|
227
|
-
}
|
|
228
|
-
const batchSize = options?.batchSize || 1e3;
|
|
229
|
-
const total = vectors.length;
|
|
230
|
-
for (let i = 0; i < total; i += batchSize) {
|
|
231
|
-
const batch = vectors.slice(i, Math.min(i + batchSize, total));
|
|
232
|
-
await Promise.all(batch.map((v) => this.insert(v)));
|
|
233
|
-
if (options?.progressCallback) {
|
|
234
|
-
options.progressCallback(Math.min(i + batchSize, total) / total);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
async search(query, options) {
|
|
239
|
-
return this.index.search(query, options);
|
|
240
|
-
}
|
|
241
|
-
async get(id) {
|
|
242
|
-
return this.index.get(id);
|
|
243
|
-
}
|
|
244
|
-
async delete(id) {
|
|
245
|
-
return this.index.delete(id);
|
|
246
|
-
}
|
|
247
|
-
async stats() {
|
|
248
|
-
return this.index.stats();
|
|
249
|
-
}
|
|
250
|
-
async save(path) {
|
|
251
|
-
return this.index.save(path);
|
|
252
|
-
}
|
|
253
|
-
static async load(path) {
|
|
254
|
-
const backend2 = loadBackend();
|
|
255
|
-
const index = await backend2.VectorIndex.load(path);
|
|
256
|
-
const wrapper = Object.create(_VectorIndex.prototype);
|
|
257
|
-
wrapper.index = index;
|
|
258
|
-
return wrapper;
|
|
259
|
-
}
|
|
260
|
-
async clear() {
|
|
261
|
-
return this.index.clear();
|
|
262
|
-
}
|
|
263
|
-
async optimize() {
|
|
264
|
-
if (this.index.optimize) {
|
|
265
|
-
return this.index.optimize();
|
|
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]; } };
|
|
266
14
|
}
|
|
267
|
-
|
|
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);
|
|
268
22
|
};
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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');
|
|
273
39
|
}
|
|
274
|
-
let dotProduct = 0;
|
|
275
|
-
let normA = 0;
|
|
276
|
-
let normB = 0;
|
|
277
|
-
for (let i = 0; i < a.length; i++) {
|
|
278
|
-
dotProduct += a[i] * b[i];
|
|
279
|
-
normA += a[i] * a[i];
|
|
280
|
-
normB += b[i] * b[i];
|
|
281
|
-
}
|
|
282
|
-
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
|
283
|
-
},
|
|
284
|
-
euclideanDistance(a, b) {
|
|
285
|
-
if (a.length !== b.length) {
|
|
286
|
-
throw new Error("Vectors must have the same dimension");
|
|
287
|
-
}
|
|
288
|
-
let sum = 0;
|
|
289
|
-
for (let i = 0; i < a.length; i++) {
|
|
290
|
-
const diff = a[i] - b[i];
|
|
291
|
-
sum += diff * diff;
|
|
292
|
-
}
|
|
293
|
-
return Math.sqrt(sum);
|
|
294
|
-
},
|
|
295
|
-
normalize(vector) {
|
|
296
|
-
const norm = Math.sqrt(vector.reduce((sum, val) => sum + val * val, 0));
|
|
297
|
-
return vector.map((val) => val / norm);
|
|
298
|
-
},
|
|
299
|
-
randomVector(dimension) {
|
|
300
|
-
const vector = new Array(dimension);
|
|
301
|
-
for (let i = 0; i < dimension; i++) {
|
|
302
|
-
vector[i] = Math.random() * 2 - 1;
|
|
303
|
-
}
|
|
304
|
-
return this.normalize(vector);
|
|
305
|
-
}
|
|
306
|
-
};
|
|
307
|
-
function getBackendInfo() {
|
|
308
|
-
loadBackend();
|
|
309
|
-
const features = [];
|
|
310
|
-
if (backendType === "native") {
|
|
311
|
-
features.push("SIMD", "Multi-threading", "Memory-mapped I/O");
|
|
312
|
-
} else {
|
|
313
|
-
features.push("Browser-compatible", "No native dependencies");
|
|
314
|
-
}
|
|
315
|
-
return {
|
|
316
|
-
type: backendType,
|
|
317
|
-
version: require_package().version,
|
|
318
|
-
features
|
|
319
|
-
};
|
|
320
40
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
41
|
+
catch (e) {
|
|
42
|
+
// No WASM fallback available yet
|
|
43
|
+
throw new Error(`Failed to load ruvector native module.\n` +
|
|
44
|
+
`Error: ${e.message}\n` +
|
|
45
|
+
`\nSupported platforms:\n` +
|
|
46
|
+
`- Linux x64/ARM64\n` +
|
|
47
|
+
`- macOS Intel/Apple Silicon\n` +
|
|
48
|
+
`- Windows x64\n` +
|
|
49
|
+
`\nIf you're on a supported platform, try:\n` +
|
|
50
|
+
` npm install --force @ruvector/core`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Get the current implementation type
|
|
54
|
+
*/
|
|
55
|
+
function getImplementationType() {
|
|
56
|
+
return implementationType;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Check if native implementation is being used
|
|
60
|
+
*/
|
|
61
|
+
function isNative() {
|
|
62
|
+
return implementationType === 'native';
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Check if WASM implementation is being used
|
|
66
|
+
*/
|
|
67
|
+
function isWasm() {
|
|
68
|
+
return implementationType === 'wasm';
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get version information
|
|
72
|
+
*/
|
|
73
|
+
function getVersion() {
|
|
74
|
+
const pkg = require('../package.json');
|
|
75
|
+
return {
|
|
76
|
+
version: pkg.version,
|
|
77
|
+
implementation: implementationType
|
|
78
|
+
};
|
|
328
79
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
VectorIndex,
|
|
334
|
-
getBackendInfo,
|
|
335
|
-
isNativeAvailable
|
|
336
|
-
});
|
|
80
|
+
// Export the VectorDB class
|
|
81
|
+
exports.VectorDB = implementation.VectorDB;
|
|
82
|
+
// Export everything from the implementation
|
|
83
|
+
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":"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"}
|
package/dist/types.js
ADDED