ruvector 0.2.16 → 0.2.17
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/dist/core/onnx/pkg/ruvector_onnx_embeddings_wasm_cjs.js +127 -0
- package/dist/core/onnx-llm.d.ts +206 -0
- package/dist/core/onnx-llm.d.ts.map +1 -0
- package/dist/core/onnx-llm.js +430 -0
- package/dist/core/sona-wrapper.d.ts +5 -0
- package/dist/core/sona-wrapper.d.ts.map +1 -1
- package/dist/core/sona-wrapper.js +17 -1
- package/package.json +7 -3
- package/dist/core/core/agentdb-fast.d.ts +0 -148
- package/dist/core/core/agentdb-fast.js +0 -301
- package/dist/core/core/intelligence-engine.d.ts +0 -257
- package/dist/core/core/intelligence-engine.js +0 -1030
- package/dist/core/core/onnx-embedder.d.ts +0 -104
- package/dist/core/core/onnx-embedder.js +0 -410
- package/dist/core/core/parallel-intelligence.d.ts +0 -108
- package/dist/core/core/parallel-intelligence.js +0 -340
- package/dist/core/core/sona-wrapper.d.ts +0 -214
- package/dist/core/core/sona-wrapper.js +0 -258
- package/dist/core/types.d.ts +0 -144
- package/dist/core/types.js +0 -2
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ONNX WASM Embedder - Semantic embeddings for hooks
|
|
3
|
-
*
|
|
4
|
-
* Provides real transformer-based embeddings using all-MiniLM-L6-v2
|
|
5
|
-
* running in pure WASM (no native dependencies).
|
|
6
|
-
*
|
|
7
|
-
* Uses bundled ONNX WASM files from src/core/onnx/
|
|
8
|
-
*
|
|
9
|
-
* Features:
|
|
10
|
-
* - 384-dimensional semantic embeddings
|
|
11
|
-
* - Real semantic understanding (not hash-based)
|
|
12
|
-
* - Cached model loading (downloads from HuggingFace on first use)
|
|
13
|
-
* - Batch embedding support
|
|
14
|
-
* - Optional parallel workers for 3.8x batch speedup
|
|
15
|
-
*/
|
|
16
|
-
declare global {
|
|
17
|
-
var __ruvector_require: NodeRequire | undefined;
|
|
18
|
-
}
|
|
19
|
-
export interface OnnxEmbedderConfig {
|
|
20
|
-
modelId?: string;
|
|
21
|
-
maxLength?: number;
|
|
22
|
-
normalize?: boolean;
|
|
23
|
-
cacheDir?: string;
|
|
24
|
-
/**
|
|
25
|
-
* Enable parallel workers for batch operations
|
|
26
|
-
* - 'auto' (default): Enable for long-running processes, skip for CLI
|
|
27
|
-
* - true: Always enable workers
|
|
28
|
-
* - false: Never use workers
|
|
29
|
-
*/
|
|
30
|
-
enableParallel?: boolean | 'auto';
|
|
31
|
-
/** Number of worker threads (default: CPU cores - 1) */
|
|
32
|
-
numWorkers?: number;
|
|
33
|
-
/** Minimum batch size to use parallel processing (default: 4) */
|
|
34
|
-
parallelThreshold?: number;
|
|
35
|
-
}
|
|
36
|
-
export interface EmbeddingResult {
|
|
37
|
-
embedding: number[];
|
|
38
|
-
dimension: number;
|
|
39
|
-
timeMs: number;
|
|
40
|
-
}
|
|
41
|
-
export interface SimilarityResult {
|
|
42
|
-
similarity: number;
|
|
43
|
-
timeMs: number;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Check if ONNX embedder is available (bundled files exist)
|
|
47
|
-
*/
|
|
48
|
-
export declare function isOnnxAvailable(): boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Initialize the ONNX embedder (downloads model if needed)
|
|
51
|
-
*/
|
|
52
|
-
export declare function initOnnxEmbedder(config?: OnnxEmbedderConfig): Promise<boolean>;
|
|
53
|
-
/**
|
|
54
|
-
* Generate embedding for text
|
|
55
|
-
*/
|
|
56
|
-
export declare function embed(text: string): Promise<EmbeddingResult>;
|
|
57
|
-
/**
|
|
58
|
-
* Generate embeddings for multiple texts
|
|
59
|
-
* Uses parallel workers automatically for batches >= parallelThreshold
|
|
60
|
-
*/
|
|
61
|
-
export declare function embedBatch(texts: string[]): Promise<EmbeddingResult[]>;
|
|
62
|
-
/**
|
|
63
|
-
* Calculate cosine similarity between two texts
|
|
64
|
-
*/
|
|
65
|
-
export declare function similarity(text1: string, text2: string): Promise<SimilarityResult>;
|
|
66
|
-
/**
|
|
67
|
-
* Calculate cosine similarity between two embeddings
|
|
68
|
-
*/
|
|
69
|
-
export declare function cosineSimilarity(a: number[], b: number[]): number;
|
|
70
|
-
/**
|
|
71
|
-
* Get embedding dimension
|
|
72
|
-
*/
|
|
73
|
-
export declare function getDimension(): number;
|
|
74
|
-
/**
|
|
75
|
-
* Check if embedder is ready
|
|
76
|
-
*/
|
|
77
|
-
export declare function isReady(): boolean;
|
|
78
|
-
/**
|
|
79
|
-
* Get embedder stats including SIMD and parallel capabilities
|
|
80
|
-
*/
|
|
81
|
-
export declare function getStats(): {
|
|
82
|
-
ready: boolean;
|
|
83
|
-
dimension: number;
|
|
84
|
-
model: string;
|
|
85
|
-
simd: boolean;
|
|
86
|
-
parallel: boolean;
|
|
87
|
-
parallelWorkers: number;
|
|
88
|
-
parallelThreshold: number;
|
|
89
|
-
};
|
|
90
|
-
/**
|
|
91
|
-
* Shutdown parallel workers (call on exit)
|
|
92
|
-
*/
|
|
93
|
-
export declare function shutdown(): Promise<void>;
|
|
94
|
-
export declare class OnnxEmbedder {
|
|
95
|
-
private config;
|
|
96
|
-
constructor(config?: OnnxEmbedderConfig);
|
|
97
|
-
init(): Promise<boolean>;
|
|
98
|
-
embed(text: string): Promise<number[]>;
|
|
99
|
-
embedBatch(texts: string[]): Promise<number[][]>;
|
|
100
|
-
similarity(text1: string, text2: string): Promise<number>;
|
|
101
|
-
get dimension(): number;
|
|
102
|
-
get ready(): boolean;
|
|
103
|
-
}
|
|
104
|
-
export default OnnxEmbedder;
|
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* ONNX WASM Embedder - Semantic embeddings for hooks
|
|
4
|
-
*
|
|
5
|
-
* Provides real transformer-based embeddings using all-MiniLM-L6-v2
|
|
6
|
-
* running in pure WASM (no native dependencies).
|
|
7
|
-
*
|
|
8
|
-
* Uses bundled ONNX WASM files from src/core/onnx/
|
|
9
|
-
*
|
|
10
|
-
* Features:
|
|
11
|
-
* - 384-dimensional semantic embeddings
|
|
12
|
-
* - Real semantic understanding (not hash-based)
|
|
13
|
-
* - Cached model loading (downloads from HuggingFace on first use)
|
|
14
|
-
* - Batch embedding support
|
|
15
|
-
* - Optional parallel workers for 3.8x batch speedup
|
|
16
|
-
*/
|
|
17
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
-
if (k2 === undefined) k2 = k;
|
|
19
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
20
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
-
}
|
|
23
|
-
Object.defineProperty(o, k2, desc);
|
|
24
|
-
}) : (function(o, m, k, k2) {
|
|
25
|
-
if (k2 === undefined) k2 = k;
|
|
26
|
-
o[k2] = m[k];
|
|
27
|
-
}));
|
|
28
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
29
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
-
}) : function(o, v) {
|
|
31
|
-
o["default"] = v;
|
|
32
|
-
});
|
|
33
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
34
|
-
var ownKeys = function(o) {
|
|
35
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
36
|
-
var ar = [];
|
|
37
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
38
|
-
return ar;
|
|
39
|
-
};
|
|
40
|
-
return ownKeys(o);
|
|
41
|
-
};
|
|
42
|
-
return function (mod) {
|
|
43
|
-
if (mod && mod.__esModule) return mod;
|
|
44
|
-
var result = {};
|
|
45
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
46
|
-
__setModuleDefault(result, mod);
|
|
47
|
-
return result;
|
|
48
|
-
};
|
|
49
|
-
})();
|
|
50
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
exports.OnnxEmbedder = void 0;
|
|
52
|
-
exports.isOnnxAvailable = isOnnxAvailable;
|
|
53
|
-
exports.initOnnxEmbedder = initOnnxEmbedder;
|
|
54
|
-
exports.embed = embed;
|
|
55
|
-
exports.embedBatch = embedBatch;
|
|
56
|
-
exports.similarity = similarity;
|
|
57
|
-
exports.cosineSimilarity = cosineSimilarity;
|
|
58
|
-
exports.getDimension = getDimension;
|
|
59
|
-
exports.isReady = isReady;
|
|
60
|
-
exports.getStats = getStats;
|
|
61
|
-
exports.shutdown = shutdown;
|
|
62
|
-
const path = __importStar(require("path"));
|
|
63
|
-
const fs = __importStar(require("fs"));
|
|
64
|
-
const url_1 = require("url");
|
|
65
|
-
const module_1 = require("module");
|
|
66
|
-
// Set up ESM-compatible require for WASM module (fixes Windows/ESM compatibility)
|
|
67
|
-
// The WASM bindings use module.require for Node.js crypto, this provides a fallback
|
|
68
|
-
if (typeof globalThis !== 'undefined' && !globalThis.__ruvector_require) {
|
|
69
|
-
try {
|
|
70
|
-
// In ESM context, use createRequire with __filename
|
|
71
|
-
globalThis.__ruvector_require = (0, module_1.createRequire)(__filename);
|
|
72
|
-
}
|
|
73
|
-
catch {
|
|
74
|
-
// Fallback: require should be available in CommonJS
|
|
75
|
-
try {
|
|
76
|
-
globalThis.__ruvector_require = require;
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
// Neither available - WASM will fall back to crypto.getRandomValues
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
// Force native dynamic import (avoids TypeScript transpiling to require)
|
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
85
|
-
const dynamicImport = new Function('specifier', 'return import(specifier)');
|
|
86
|
-
// Capability detection
|
|
87
|
-
let simdAvailable = false;
|
|
88
|
-
let parallelAvailable = false;
|
|
89
|
-
// Lazy-loaded module state
|
|
90
|
-
let wasmModule = null;
|
|
91
|
-
let embedder = null;
|
|
92
|
-
let parallelEmbedder = null;
|
|
93
|
-
let loadError = null;
|
|
94
|
-
let loadPromise = null;
|
|
95
|
-
let isInitialized = false;
|
|
96
|
-
let parallelEnabled = false;
|
|
97
|
-
let parallelThreshold = 4;
|
|
98
|
-
// Default model
|
|
99
|
-
const DEFAULT_MODEL = 'all-MiniLM-L6-v2';
|
|
100
|
-
/**
|
|
101
|
-
* Check if ONNX embedder is available (bundled files exist)
|
|
102
|
-
*/
|
|
103
|
-
function isOnnxAvailable() {
|
|
104
|
-
try {
|
|
105
|
-
const pkgPath = path.join(__dirname, 'onnx', 'pkg', 'ruvector_onnx_embeddings_wasm.js');
|
|
106
|
-
return fs.existsSync(pkgPath);
|
|
107
|
-
}
|
|
108
|
-
catch {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Check if parallel workers are available (npm package installed)
|
|
114
|
-
*/
|
|
115
|
-
async function detectParallelAvailable() {
|
|
116
|
-
try {
|
|
117
|
-
await dynamicImport('ruvector-onnx-embeddings-wasm/parallel');
|
|
118
|
-
parallelAvailable = true;
|
|
119
|
-
return true;
|
|
120
|
-
}
|
|
121
|
-
catch {
|
|
122
|
-
parallelAvailable = false;
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Check if SIMD is available (from WASM module)
|
|
128
|
-
*/
|
|
129
|
-
function detectSimd() {
|
|
130
|
-
try {
|
|
131
|
-
if (wasmModule && typeof wasmModule.simd_available === 'function') {
|
|
132
|
-
simdAvailable = wasmModule.simd_available();
|
|
133
|
-
return simdAvailable;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
catch { }
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Try to load ParallelEmbedder from npm package (optional)
|
|
141
|
-
*/
|
|
142
|
-
async function tryInitParallel(config) {
|
|
143
|
-
// Skip if explicitly disabled
|
|
144
|
-
if (config.enableParallel === false)
|
|
145
|
-
return false;
|
|
146
|
-
// For 'auto' or true, try to initialize
|
|
147
|
-
try {
|
|
148
|
-
const parallelModule = await dynamicImport('ruvector-onnx-embeddings-wasm/parallel');
|
|
149
|
-
const { ParallelEmbedder } = parallelModule;
|
|
150
|
-
parallelEmbedder = new ParallelEmbedder({
|
|
151
|
-
numWorkers: config.numWorkers,
|
|
152
|
-
});
|
|
153
|
-
await parallelEmbedder.init(config.modelId || DEFAULT_MODEL);
|
|
154
|
-
parallelThreshold = config.parallelThreshold || 4;
|
|
155
|
-
parallelEnabled = true;
|
|
156
|
-
parallelAvailable = true;
|
|
157
|
-
console.error(`Parallel embedder ready: ${parallelEmbedder.numWorkers} workers, SIMD: ${simdAvailable}`);
|
|
158
|
-
return true;
|
|
159
|
-
}
|
|
160
|
-
catch (e) {
|
|
161
|
-
parallelAvailable = false;
|
|
162
|
-
if (config.enableParallel === true) {
|
|
163
|
-
// Only warn if explicitly requested
|
|
164
|
-
console.error(`Parallel embedder not available: ${e.message}`);
|
|
165
|
-
}
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Initialize the ONNX embedder (downloads model if needed)
|
|
171
|
-
*/
|
|
172
|
-
async function initOnnxEmbedder(config = {}) {
|
|
173
|
-
if (isInitialized)
|
|
174
|
-
return true;
|
|
175
|
-
if (loadError)
|
|
176
|
-
throw loadError;
|
|
177
|
-
if (loadPromise) {
|
|
178
|
-
await loadPromise;
|
|
179
|
-
return isInitialized;
|
|
180
|
-
}
|
|
181
|
-
loadPromise = (async () => {
|
|
182
|
-
try {
|
|
183
|
-
// Paths to bundled ONNX files
|
|
184
|
-
const pkgPath = path.join(__dirname, 'onnx', 'pkg', 'ruvector_onnx_embeddings_wasm.js');
|
|
185
|
-
const loaderPath = path.join(__dirname, 'onnx', 'loader.js');
|
|
186
|
-
if (!fs.existsSync(pkgPath)) {
|
|
187
|
-
throw new Error('ONNX WASM files not bundled. The onnx/ directory is missing.');
|
|
188
|
-
}
|
|
189
|
-
// Convert paths to file:// URLs for cross-platform ESM compatibility (Windows fix)
|
|
190
|
-
const pkgUrl = (0, url_1.pathToFileURL)(pkgPath).href;
|
|
191
|
-
const loaderUrl = (0, url_1.pathToFileURL)(loaderPath).href;
|
|
192
|
-
// Dynamic import of bundled modules using file:// URLs
|
|
193
|
-
wasmModule = await dynamicImport(pkgUrl);
|
|
194
|
-
// Initialize WASM module (loads the .wasm file)
|
|
195
|
-
const wasmPath = path.join(__dirname, 'onnx', 'pkg', 'ruvector_onnx_embeddings_wasm_bg.wasm');
|
|
196
|
-
if (wasmModule.default && typeof wasmModule.default === 'function') {
|
|
197
|
-
// For bundler-style initialization, pass the wasm buffer
|
|
198
|
-
const wasmBytes = fs.readFileSync(wasmPath);
|
|
199
|
-
await wasmModule.default(wasmBytes);
|
|
200
|
-
}
|
|
201
|
-
const loaderModule = await dynamicImport(loaderUrl);
|
|
202
|
-
const { ModelLoader } = loaderModule;
|
|
203
|
-
// Create model loader with caching
|
|
204
|
-
const modelLoader = new ModelLoader({
|
|
205
|
-
cache: true,
|
|
206
|
-
cacheDir: config.cacheDir || path.join(process.env.HOME || '/tmp', '.ruvector', 'models'),
|
|
207
|
-
});
|
|
208
|
-
// Load model (downloads from HuggingFace on first use)
|
|
209
|
-
const modelId = config.modelId || DEFAULT_MODEL;
|
|
210
|
-
console.error(`Loading ONNX model: ${modelId}...`);
|
|
211
|
-
const { modelBytes, tokenizerJson, config: modelConfig } = await modelLoader.loadModel(modelId);
|
|
212
|
-
// Create embedder with config
|
|
213
|
-
const embedderConfig = new wasmModule.WasmEmbedderConfig()
|
|
214
|
-
.setMaxLength(config.maxLength || modelConfig.maxLength || 256)
|
|
215
|
-
.setNormalize(config.normalize !== false)
|
|
216
|
-
.setPooling(0); // Mean pooling
|
|
217
|
-
embedder = wasmModule.WasmEmbedder.withConfig(modelBytes, tokenizerJson, embedderConfig);
|
|
218
|
-
// Detect SIMD capability
|
|
219
|
-
detectSimd();
|
|
220
|
-
console.error(`ONNX embedder ready: ${embedder.dimension()}d, SIMD: ${simdAvailable}`);
|
|
221
|
-
isInitialized = true;
|
|
222
|
-
// Determine if we should use parallel workers
|
|
223
|
-
// - true: always enable
|
|
224
|
-
// - false: never enable
|
|
225
|
-
// - 'auto'/undefined: enable for long-running processes (MCP, servers), skip for CLI
|
|
226
|
-
let shouldTryParallel = false;
|
|
227
|
-
if (config.enableParallel === true) {
|
|
228
|
-
shouldTryParallel = true;
|
|
229
|
-
}
|
|
230
|
-
else if (config.enableParallel === false) {
|
|
231
|
-
shouldTryParallel = false;
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
// Auto-detect: check if running as CLI hook or long-running process
|
|
235
|
-
const isCLI = process.argv[1]?.includes('cli.js') ||
|
|
236
|
-
process.argv[1]?.includes('bin/ruvector') ||
|
|
237
|
-
process.env.RUVECTOR_CLI === '1';
|
|
238
|
-
const isMCP = process.env.MCP_SERVER === '1' ||
|
|
239
|
-
process.argv.some(a => a.includes('mcp'));
|
|
240
|
-
const forceParallel = process.env.RUVECTOR_PARALLEL === '1';
|
|
241
|
-
// Enable parallel for MCP/servers or if explicitly requested, skip for CLI
|
|
242
|
-
shouldTryParallel = forceParallel || (isMCP && !isCLI);
|
|
243
|
-
}
|
|
244
|
-
if (shouldTryParallel) {
|
|
245
|
-
await tryInitParallel(config);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
catch (e) {
|
|
249
|
-
loadError = new Error(`Failed to initialize ONNX embedder: ${e.message}`);
|
|
250
|
-
throw loadError;
|
|
251
|
-
}
|
|
252
|
-
})();
|
|
253
|
-
await loadPromise;
|
|
254
|
-
return isInitialized;
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Generate embedding for text
|
|
258
|
-
*/
|
|
259
|
-
async function embed(text) {
|
|
260
|
-
if (!isInitialized) {
|
|
261
|
-
await initOnnxEmbedder();
|
|
262
|
-
}
|
|
263
|
-
if (!embedder) {
|
|
264
|
-
throw new Error('ONNX embedder not initialized');
|
|
265
|
-
}
|
|
266
|
-
const start = performance.now();
|
|
267
|
-
const embedding = embedder.embedOne(text);
|
|
268
|
-
const timeMs = performance.now() - start;
|
|
269
|
-
return {
|
|
270
|
-
embedding: Array.from(embedding),
|
|
271
|
-
dimension: embedding.length,
|
|
272
|
-
timeMs,
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Generate embeddings for multiple texts
|
|
277
|
-
* Uses parallel workers automatically for batches >= parallelThreshold
|
|
278
|
-
*/
|
|
279
|
-
async function embedBatch(texts) {
|
|
280
|
-
if (!isInitialized) {
|
|
281
|
-
await initOnnxEmbedder();
|
|
282
|
-
}
|
|
283
|
-
if (!embedder) {
|
|
284
|
-
throw new Error('ONNX embedder not initialized');
|
|
285
|
-
}
|
|
286
|
-
const start = performance.now();
|
|
287
|
-
// Use parallel workers for large batches
|
|
288
|
-
if (parallelEnabled && parallelEmbedder && texts.length >= parallelThreshold) {
|
|
289
|
-
const batchResults = await parallelEmbedder.embedBatch(texts);
|
|
290
|
-
const totalTime = performance.now() - start;
|
|
291
|
-
const dimension = parallelEmbedder.dimension || 384;
|
|
292
|
-
return batchResults.map((emb) => ({
|
|
293
|
-
embedding: Array.from(emb),
|
|
294
|
-
dimension,
|
|
295
|
-
timeMs: totalTime / texts.length,
|
|
296
|
-
}));
|
|
297
|
-
}
|
|
298
|
-
// Sequential fallback
|
|
299
|
-
const batchEmbeddings = embedder.embedBatch(texts);
|
|
300
|
-
const totalTime = performance.now() - start;
|
|
301
|
-
const dimension = embedder.dimension();
|
|
302
|
-
const results = [];
|
|
303
|
-
for (let i = 0; i < texts.length; i++) {
|
|
304
|
-
const embedding = batchEmbeddings.slice(i * dimension, (i + 1) * dimension);
|
|
305
|
-
results.push({
|
|
306
|
-
embedding: Array.from(embedding),
|
|
307
|
-
dimension,
|
|
308
|
-
timeMs: totalTime / texts.length,
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
return results;
|
|
312
|
-
}
|
|
313
|
-
/**
|
|
314
|
-
* Calculate cosine similarity between two texts
|
|
315
|
-
*/
|
|
316
|
-
async function similarity(text1, text2) {
|
|
317
|
-
if (!isInitialized) {
|
|
318
|
-
await initOnnxEmbedder();
|
|
319
|
-
}
|
|
320
|
-
if (!embedder) {
|
|
321
|
-
throw new Error('ONNX embedder not initialized');
|
|
322
|
-
}
|
|
323
|
-
const start = performance.now();
|
|
324
|
-
const sim = embedder.similarity(text1, text2);
|
|
325
|
-
const timeMs = performance.now() - start;
|
|
326
|
-
return { similarity: sim, timeMs };
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Calculate cosine similarity between two embeddings
|
|
330
|
-
*/
|
|
331
|
-
function cosineSimilarity(a, b) {
|
|
332
|
-
if (a.length !== b.length) {
|
|
333
|
-
throw new Error('Embeddings must have same dimension');
|
|
334
|
-
}
|
|
335
|
-
let dotProduct = 0;
|
|
336
|
-
let normA = 0;
|
|
337
|
-
let normB = 0;
|
|
338
|
-
for (let i = 0; i < a.length; i++) {
|
|
339
|
-
dotProduct += a[i] * b[i];
|
|
340
|
-
normA += a[i] * a[i];
|
|
341
|
-
normB += b[i] * b[i];
|
|
342
|
-
}
|
|
343
|
-
const magnitude = Math.sqrt(normA) * Math.sqrt(normB);
|
|
344
|
-
return magnitude === 0 ? 0 : dotProduct / magnitude;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* Get embedding dimension
|
|
348
|
-
*/
|
|
349
|
-
function getDimension() {
|
|
350
|
-
return embedder ? embedder.dimension() : 384;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Check if embedder is ready
|
|
354
|
-
*/
|
|
355
|
-
function isReady() {
|
|
356
|
-
return isInitialized;
|
|
357
|
-
}
|
|
358
|
-
/**
|
|
359
|
-
* Get embedder stats including SIMD and parallel capabilities
|
|
360
|
-
*/
|
|
361
|
-
function getStats() {
|
|
362
|
-
return {
|
|
363
|
-
ready: isInitialized,
|
|
364
|
-
dimension: embedder ? embedder.dimension() : 384,
|
|
365
|
-
model: DEFAULT_MODEL,
|
|
366
|
-
simd: simdAvailable,
|
|
367
|
-
parallel: parallelEnabled,
|
|
368
|
-
parallelWorkers: parallelEmbedder?.numWorkers || 0,
|
|
369
|
-
parallelThreshold,
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Shutdown parallel workers (call on exit)
|
|
374
|
-
*/
|
|
375
|
-
async function shutdown() {
|
|
376
|
-
if (parallelEmbedder) {
|
|
377
|
-
await parallelEmbedder.shutdown();
|
|
378
|
-
parallelEmbedder = null;
|
|
379
|
-
parallelEnabled = false;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
// Export class wrapper for compatibility
|
|
383
|
-
class OnnxEmbedder {
|
|
384
|
-
constructor(config = {}) {
|
|
385
|
-
this.config = config;
|
|
386
|
-
}
|
|
387
|
-
async init() {
|
|
388
|
-
return initOnnxEmbedder(this.config);
|
|
389
|
-
}
|
|
390
|
-
async embed(text) {
|
|
391
|
-
const result = await embed(text);
|
|
392
|
-
return result.embedding;
|
|
393
|
-
}
|
|
394
|
-
async embedBatch(texts) {
|
|
395
|
-
const results = await embedBatch(texts);
|
|
396
|
-
return results.map(r => r.embedding);
|
|
397
|
-
}
|
|
398
|
-
async similarity(text1, text2) {
|
|
399
|
-
const result = await similarity(text1, text2);
|
|
400
|
-
return result.similarity;
|
|
401
|
-
}
|
|
402
|
-
get dimension() {
|
|
403
|
-
return getDimension();
|
|
404
|
-
}
|
|
405
|
-
get ready() {
|
|
406
|
-
return isReady();
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
exports.OnnxEmbedder = OnnxEmbedder;
|
|
410
|
-
exports.default = OnnxEmbedder;
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parallel Intelligence - Worker-based acceleration for IntelligenceEngine
|
|
3
|
-
*
|
|
4
|
-
* Provides parallel processing for:
|
|
5
|
-
* - Q-learning batch updates (3-4x faster)
|
|
6
|
-
* - Multi-file pattern matching
|
|
7
|
-
* - Background memory indexing
|
|
8
|
-
* - Parallel similarity search
|
|
9
|
-
* - Multi-file code analysis
|
|
10
|
-
* - Parallel git commit analysis
|
|
11
|
-
*
|
|
12
|
-
* Uses worker_threads for CPU-bound operations, keeping hooks non-blocking.
|
|
13
|
-
*/
|
|
14
|
-
export interface ParallelConfig {
|
|
15
|
-
/** Number of worker threads (default: CPU cores - 1) */
|
|
16
|
-
numWorkers?: number;
|
|
17
|
-
/** Enable parallel processing (default: true for MCP, false for CLI) */
|
|
18
|
-
enabled?: boolean;
|
|
19
|
-
/** Minimum batch size to use parallel (default: 4) */
|
|
20
|
-
batchThreshold?: number;
|
|
21
|
-
}
|
|
22
|
-
export interface BatchEpisode {
|
|
23
|
-
state: string;
|
|
24
|
-
action: string;
|
|
25
|
-
reward: number;
|
|
26
|
-
nextState: string;
|
|
27
|
-
done: boolean;
|
|
28
|
-
metadata?: Record<string, any>;
|
|
29
|
-
}
|
|
30
|
-
export interface PatternMatchResult {
|
|
31
|
-
file: string;
|
|
32
|
-
patterns: Array<{
|
|
33
|
-
pattern: string;
|
|
34
|
-
confidence: number;
|
|
35
|
-
}>;
|
|
36
|
-
}
|
|
37
|
-
export interface CoEditAnalysis {
|
|
38
|
-
file1: string;
|
|
39
|
-
file2: string;
|
|
40
|
-
commits: string[];
|
|
41
|
-
strength: number;
|
|
42
|
-
}
|
|
43
|
-
export declare class ParallelIntelligence {
|
|
44
|
-
private workers;
|
|
45
|
-
private taskQueue;
|
|
46
|
-
private busyWorkers;
|
|
47
|
-
private config;
|
|
48
|
-
private initialized;
|
|
49
|
-
constructor(config?: ParallelConfig);
|
|
50
|
-
/**
|
|
51
|
-
* Initialize worker pool
|
|
52
|
-
*/
|
|
53
|
-
init(): Promise<void>;
|
|
54
|
-
private processQueue;
|
|
55
|
-
/**
|
|
56
|
-
* Execute task in worker pool
|
|
57
|
-
*/
|
|
58
|
-
private executeInWorker;
|
|
59
|
-
/**
|
|
60
|
-
* Batch Q-learning episode recording (3-4x faster)
|
|
61
|
-
*/
|
|
62
|
-
recordEpisodesBatch(episodes: BatchEpisode[]): Promise<void>;
|
|
63
|
-
/**
|
|
64
|
-
* Multi-file pattern matching (parallel pretrain)
|
|
65
|
-
*/
|
|
66
|
-
matchPatternsParallel(files: string[]): Promise<PatternMatchResult[]>;
|
|
67
|
-
/**
|
|
68
|
-
* Background memory indexing (non-blocking)
|
|
69
|
-
*/
|
|
70
|
-
indexMemoriesBackground(memories: Array<{
|
|
71
|
-
content: string;
|
|
72
|
-
type: string;
|
|
73
|
-
}>): Promise<void>;
|
|
74
|
-
/**
|
|
75
|
-
* Parallel similarity search with sharding
|
|
76
|
-
*/
|
|
77
|
-
searchParallel(query: string, topK?: number): Promise<Array<{
|
|
78
|
-
content: string;
|
|
79
|
-
score: number;
|
|
80
|
-
}>>;
|
|
81
|
-
/**
|
|
82
|
-
* Multi-file AST analysis for routing
|
|
83
|
-
*/
|
|
84
|
-
analyzeFilesParallel(files: string[]): Promise<Map<string, {
|
|
85
|
-
agent: string;
|
|
86
|
-
confidence: number;
|
|
87
|
-
}>>;
|
|
88
|
-
/**
|
|
89
|
-
* Parallel git commit analysis for co-edit detection
|
|
90
|
-
*/
|
|
91
|
-
analyzeCommitsParallel(commits: string[]): Promise<CoEditAnalysis[]>;
|
|
92
|
-
/**
|
|
93
|
-
* Get worker pool stats
|
|
94
|
-
*/
|
|
95
|
-
getStats(): {
|
|
96
|
-
enabled: boolean;
|
|
97
|
-
workers: number;
|
|
98
|
-
busy: number;
|
|
99
|
-
queued: number;
|
|
100
|
-
};
|
|
101
|
-
/**
|
|
102
|
-
* Shutdown worker pool
|
|
103
|
-
*/
|
|
104
|
-
shutdown(): Promise<void>;
|
|
105
|
-
}
|
|
106
|
-
export declare function getParallelIntelligence(config?: ParallelConfig): ParallelIntelligence;
|
|
107
|
-
export declare function initParallelIntelligence(config?: ParallelConfig): Promise<ParallelIntelligence>;
|
|
108
|
-
export default ParallelIntelligence;
|