ruvector 0.1.26 → 0.1.28

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attention-fallbacks.d.ts","sourceRoot":"","sources":["../../src/core/attention-fallbacks.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA8CH;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,qCAAqC;IACrC,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,2DAA2D;IAC3D,GAAG,EAAE,YAAY,CAAC;CACnB;AAED;;;;GAIG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,KAAK,CAAM;IACnB,SAAgB,GAAG,EAAE,MAAM,CAAC;IAC5B,SAAgB,QAAQ,EAAE,MAAM,CAAC;IAEjC;;;;;OAKG;gBACS,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAOzC;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,OAAO,CACL,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY,EAC9B,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,EAAE,EACjC,MAAM,EAAE,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,EAAE,GAClC,eAAe;IAYlB;;OAEG;IACH,UAAU,CACR,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,YAAY,EAAE,EACpB,MAAM,EAAE,YAAY,EAAE,GACrB,YAAY;IAIf,IAAI,OAAO,IAAI,MAAM,CAEpB;CACF;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,KAAK,CAAM;IACnB,SAAgB,GAAG,EAAE,MAAM,CAAC;IAC5B,SAAgB,SAAS,EAAE,MAAM,CAAC;IAElC;;;;;OAKG;gBACS,GAAG,EAAE,MAAM,EAAE,SAAS,GAAE,MAAY;IAOhD;;OAEG;IACH,OAAO,CACL,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY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,YAAY,CAAC,EAAE,GAClC,eAAe;IAYlB,UAAU,CACR,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,YAAY,EAAE,EACpB,MAAM,EAAE,YAAY,EAAE,GACrB,YAAY;CAGhB;AAID;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,EAAE,GAAG,YAAY,EAC/B,SAAS,GAAE,MAAY,GACtB,MAAM,EAAE,CAIV;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,CAAC,EAAE,MAAM,EAAE,GAAG,YAAY,EAC1B,CAAC,EAAE,MAAM,EAAE,GAAG,YAAY,EAC1B,SAAS,GAAE,MAAY,GACtB,MAAM,CAGR;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC5B,CAAC,EAAE,MAAM,EAAE,GAAG,YAAY,EAC1B,CAAC,EAAE,MAAM,EAAE,GAAG,YAAY,EAC1B,SAAS,GAAE,MAAY,GACtB,MAAM,EAAE,CAIV;AAED;;GAEG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EAAE,GAAG,YAAY,EAC7B,OAAO,EAAE,MAAM,EAAE,GAAG,YAAY,EAChC,SAAS,GAAE,MAAY,GACtB,MAAM,EAAE,CAIV;AAED;;GAEG;AACH,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EAAE,GAAG,YAAY,EAC7B,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY,EAC9B,SAAS,GAAE,MAAY,GACtB,MAAM,EAAE,CAIV;AAED;;GAEG;AACH,wBAAgB,oBAAoB,IAAI,OAAO,CAO9C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,GAAG,IAAI,CAOnD;;;;;;;;;;;;;;;;AAED,wBAcE"}
@@ -0,0 +1,361 @@
1
+ "use strict";
2
+ /**
3
+ * Attention Fallbacks - Safe wrapper around @ruvector/attention with automatic array conversion
4
+ *
5
+ * This wrapper handles the array type conversion automatically, allowing users
6
+ * to pass either regular arrays or Float32Arrays.
7
+ *
8
+ * @ruvector/attention requires Float32Array inputs.
9
+ * This wrapper handles the conversion automatically.
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MoEAttention = exports.LocalGlobalAttention = exports.LinearAttention = exports.HyperbolicAttention = exports.FlashAttention = exports.MultiHeadAttention = void 0;
13
+ exports.projectToPoincareBall = projectToPoincareBall;
14
+ exports.poincareDistance = poincareDistance;
15
+ exports.mobiusAddition = mobiusAddition;
16
+ exports.expMap = expMap;
17
+ exports.logMap = logMap;
18
+ exports.isAttentionAvailable = isAttentionAvailable;
19
+ exports.getAttentionVersion = getAttentionVersion;
20
+ // Lazy load to avoid import errors if not installed
21
+ let attentionModule = null;
22
+ let loadError = null;
23
+ function getAttentionModule() {
24
+ if (attentionModule)
25
+ return attentionModule;
26
+ if (loadError)
27
+ throw loadError;
28
+ try {
29
+ attentionModule = require('@ruvector/attention');
30
+ return attentionModule;
31
+ }
32
+ catch (e) {
33
+ loadError = new Error(`@ruvector/attention is not installed or failed to load: ${e.message}\n` +
34
+ `Install with: npm install @ruvector/attention`);
35
+ throw loadError;
36
+ }
37
+ }
38
+ /**
39
+ * Convert any array-like input to Float32Array
40
+ */
41
+ function toFloat32Array(input) {
42
+ if (input instanceof Float32Array) {
43
+ return input;
44
+ }
45
+ return new Float32Array(input);
46
+ }
47
+ /**
48
+ * Convert nested arrays to Float32Arrays
49
+ */
50
+ function toFloat32Arrays(inputs) {
51
+ return inputs.map(arr => toFloat32Array(arr));
52
+ }
53
+ /**
54
+ * Convert Float32Array result back to regular array if needed
55
+ */
56
+ function fromFloat32Array(input) {
57
+ return Array.from(input);
58
+ }
59
+ /**
60
+ * Multi-head attention mechanism
61
+ *
62
+ * This wrapper automatically converts array inputs to Float32Array.
63
+ */
64
+ class MultiHeadAttention {
65
+ /**
66
+ * Create a new multi-head attention instance
67
+ *
68
+ * @param dim - Embedding dimension (must be divisible by numHeads)
69
+ * @param numHeads - Number of attention heads
70
+ */
71
+ constructor(dim, numHeads) {
72
+ const attention = getAttentionModule();
73
+ this.inner = new attention.MultiHeadAttention(dim, numHeads);
74
+ this.dim = dim;
75
+ this.numHeads = numHeads;
76
+ }
77
+ /**
78
+ * Compute multi-head attention
79
+ *
80
+ * @param query - Query vector
81
+ * @param keys - Array of key vectors
82
+ * @param values - Array of value vectors
83
+ * @returns Attention output
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const mha = new MultiHeadAttention(64, 4);
88
+ *
89
+ * // Works with regular arrays
90
+ * const result1 = mha.compute([...64 values], [[...64], [...64]], [[...64], [...64]]);
91
+ *
92
+ * // Also works with Float32Array
93
+ * const q = new Float32Array(64);
94
+ * const k = [new Float32Array(64)];
95
+ * const v = [new Float32Array(64)];
96
+ * const result2 = mha.compute(q, k, v);
97
+ * ```
98
+ */
99
+ compute(query, keys, values) {
100
+ const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
101
+ return {
102
+ values: fromFloat32Array(raw),
103
+ raw
104
+ };
105
+ }
106
+ /**
107
+ * Compute and return raw Float32Array (faster, no conversion)
108
+ */
109
+ computeRaw(query, keys, values) {
110
+ return this.inner.compute(query, keys, values);
111
+ }
112
+ get headDim() {
113
+ return this.dim / this.numHeads;
114
+ }
115
+ }
116
+ exports.MultiHeadAttention = MultiHeadAttention;
117
+ /**
118
+ * Flash attention with tiled computation
119
+ */
120
+ class FlashAttention {
121
+ /**
122
+ * Create a new flash attention instance
123
+ *
124
+ * @param dim - Embedding dimension
125
+ * @param blockSize - Block size for tiled computation (default: 512)
126
+ */
127
+ constructor(dim, blockSize = 512) {
128
+ const attention = getAttentionModule();
129
+ this.inner = new attention.FlashAttention(dim, blockSize);
130
+ this.dim = dim;
131
+ this.blockSize = blockSize;
132
+ }
133
+ /**
134
+ * Compute flash attention
135
+ */
136
+ compute(query, keys, values) {
137
+ const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
138
+ return {
139
+ values: fromFloat32Array(raw),
140
+ raw
141
+ };
142
+ }
143
+ computeRaw(query, keys, values) {
144
+ return this.inner.compute(query, keys, values);
145
+ }
146
+ }
147
+ exports.FlashAttention = FlashAttention;
148
+ /**
149
+ * Hyperbolic attention in Poincare ball model
150
+ */
151
+ class HyperbolicAttention {
152
+ /**
153
+ * Create a new hyperbolic attention instance
154
+ *
155
+ * @param dim - Embedding dimension
156
+ * @param curvature - Hyperbolic curvature (typically 1.0)
157
+ */
158
+ constructor(dim, curvature = 1.0) {
159
+ const attention = getAttentionModule();
160
+ this.inner = new attention.HyperbolicAttention(dim, curvature);
161
+ this.dim = dim;
162
+ this.curvature = curvature;
163
+ }
164
+ /**
165
+ * Compute hyperbolic attention
166
+ */
167
+ compute(query, keys, values) {
168
+ const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
169
+ return {
170
+ values: fromFloat32Array(raw),
171
+ raw
172
+ };
173
+ }
174
+ computeRaw(query, keys, values) {
175
+ return this.inner.compute(query, keys, values);
176
+ }
177
+ }
178
+ exports.HyperbolicAttention = HyperbolicAttention;
179
+ /**
180
+ * Linear attention (Performer-style) with O(n) complexity
181
+ */
182
+ class LinearAttention {
183
+ /**
184
+ * Create a new linear attention instance
185
+ *
186
+ * @param dim - Embedding dimension
187
+ * @param numFeatures - Number of random features
188
+ */
189
+ constructor(dim, numFeatures) {
190
+ const attention = getAttentionModule();
191
+ this.inner = new attention.LinearAttention(dim, numFeatures);
192
+ this.dim = dim;
193
+ this.numFeatures = numFeatures;
194
+ }
195
+ /**
196
+ * Compute linear attention
197
+ */
198
+ compute(query, keys, values) {
199
+ const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
200
+ return {
201
+ values: fromFloat32Array(raw),
202
+ raw
203
+ };
204
+ }
205
+ computeRaw(query, keys, values) {
206
+ return this.inner.compute(query, keys, values);
207
+ }
208
+ }
209
+ exports.LinearAttention = LinearAttention;
210
+ /**
211
+ * Local-global attention (Longformer-style)
212
+ */
213
+ class LocalGlobalAttention {
214
+ /**
215
+ * Create a new local-global attention instance
216
+ *
217
+ * @param dim - Embedding dimension
218
+ * @param localWindow - Size of local attention window
219
+ * @param globalTokens - Number of global attention tokens
220
+ */
221
+ constructor(dim, localWindow, globalTokens) {
222
+ const attention = getAttentionModule();
223
+ this.inner = new attention.LocalGlobalAttention(dim, localWindow, globalTokens);
224
+ this.dim = dim;
225
+ this.localWindow = localWindow;
226
+ this.globalTokens = globalTokens;
227
+ }
228
+ /**
229
+ * Compute local-global attention
230
+ */
231
+ compute(query, keys, values) {
232
+ const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
233
+ return {
234
+ values: fromFloat32Array(raw),
235
+ raw
236
+ };
237
+ }
238
+ computeRaw(query, keys, values) {
239
+ return this.inner.compute(query, keys, values);
240
+ }
241
+ }
242
+ exports.LocalGlobalAttention = LocalGlobalAttention;
243
+ /**
244
+ * Mixture of Experts attention
245
+ */
246
+ class MoEAttention {
247
+ /**
248
+ * Create a new MoE attention instance
249
+ *
250
+ * @param config - MoE configuration
251
+ */
252
+ constructor(config) {
253
+ const attention = getAttentionModule();
254
+ this.inner = new attention.MoEAttention({
255
+ dim: config.dim,
256
+ num_experts: config.numExperts,
257
+ top_k: config.topK,
258
+ expert_capacity: config.expertCapacity ?? 1.25,
259
+ });
260
+ this.config = config;
261
+ }
262
+ /**
263
+ * Create with simple parameters
264
+ */
265
+ static simple(dim, numExperts, topK) {
266
+ return new MoEAttention({ dim, numExperts, topK });
267
+ }
268
+ /**
269
+ * Compute MoE attention
270
+ */
271
+ compute(query, keys, values) {
272
+ const raw = this.inner.compute(toFloat32Array(query), toFloat32Arrays(keys), toFloat32Arrays(values));
273
+ return {
274
+ values: fromFloat32Array(raw),
275
+ raw
276
+ };
277
+ }
278
+ computeRaw(query, keys, values) {
279
+ return this.inner.compute(query, keys, values);
280
+ }
281
+ }
282
+ exports.MoEAttention = MoEAttention;
283
+ // Hyperbolic math utilities
284
+ /**
285
+ * Project a vector into the Poincare ball
286
+ */
287
+ function projectToPoincareBall(vector, curvature = 1.0) {
288
+ const attention = getAttentionModule();
289
+ const result = attention.projectToPoincareBall(toFloat32Array(vector), curvature);
290
+ return fromFloat32Array(result);
291
+ }
292
+ /**
293
+ * Compute hyperbolic (Poincare) distance between two points
294
+ */
295
+ function poincareDistance(a, b, curvature = 1.0) {
296
+ const attention = getAttentionModule();
297
+ return attention.poincareDistance(toFloat32Array(a), toFloat32Array(b), curvature);
298
+ }
299
+ /**
300
+ * Mobius addition in hyperbolic space
301
+ */
302
+ function mobiusAddition(a, b, curvature = 1.0) {
303
+ const attention = getAttentionModule();
304
+ const result = attention.mobiusAddition(toFloat32Array(a), toFloat32Array(b), curvature);
305
+ return fromFloat32Array(result);
306
+ }
307
+ /**
308
+ * Exponential map from tangent space to hyperbolic space
309
+ */
310
+ function expMap(base, tangent, curvature = 1.0) {
311
+ const attention = getAttentionModule();
312
+ const result = attention.expMap(toFloat32Array(base), toFloat32Array(tangent), curvature);
313
+ return fromFloat32Array(result);
314
+ }
315
+ /**
316
+ * Logarithmic map from hyperbolic space to tangent space
317
+ */
318
+ function logMap(base, point, curvature = 1.0) {
319
+ const attention = getAttentionModule();
320
+ const result = attention.logMap(toFloat32Array(base), toFloat32Array(point), curvature);
321
+ return fromFloat32Array(result);
322
+ }
323
+ /**
324
+ * Check if attention module is available
325
+ */
326
+ function isAttentionAvailable() {
327
+ try {
328
+ getAttentionModule();
329
+ return true;
330
+ }
331
+ catch {
332
+ return false;
333
+ }
334
+ }
335
+ /**
336
+ * Get attention module version
337
+ */
338
+ function getAttentionVersion() {
339
+ try {
340
+ const attention = getAttentionModule();
341
+ return attention.version?.() ?? null;
342
+ }
343
+ catch {
344
+ return null;
345
+ }
346
+ }
347
+ exports.default = {
348
+ MultiHeadAttention,
349
+ FlashAttention,
350
+ HyperbolicAttention,
351
+ LinearAttention,
352
+ LocalGlobalAttention,
353
+ MoEAttention,
354
+ projectToPoincareBall,
355
+ poincareDistance,
356
+ mobiusAddition,
357
+ expMap,
358
+ logMap,
359
+ isAttentionAvailable,
360
+ getAttentionVersion,
361
+ };
@@ -0,0 +1,124 @@
1
+ /**
2
+ * GNN Wrapper - Safe wrapper around @ruvector/gnn with automatic array conversion
3
+ *
4
+ * This wrapper handles the array type conversion automatically, allowing users
5
+ * to pass either regular arrays or Float32Arrays.
6
+ *
7
+ * @ruvector/gnn requires regular JavaScript arrays, NOT Float32Array.
8
+ * This wrapper handles the conversion automatically.
9
+ */
10
+ /**
11
+ * Search result from differentiable search
12
+ */
13
+ export interface DifferentiableSearchResult {
14
+ /** Indices of top-k candidates */
15
+ indices: number[];
16
+ /** Soft weights for top-k candidates */
17
+ weights: number[];
18
+ }
19
+ /**
20
+ * Differentiable search using soft attention mechanism
21
+ *
22
+ * This wrapper automatically converts Float32Array inputs to regular arrays.
23
+ *
24
+ * @param query - Query vector (array or Float32Array)
25
+ * @param candidates - List of candidate vectors (arrays or Float32Arrays)
26
+ * @param k - Number of top results to return
27
+ * @param temperature - Temperature for softmax (lower = sharper, higher = smoother)
28
+ * @returns Search result with indices and soft weights
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * import { differentiableSearch } from 'ruvector/core/gnn-wrapper';
33
+ *
34
+ * // Works with regular arrays
35
+ * const result1 = differentiableSearch([1, 0, 0], [[1, 0, 0], [0, 1, 0]], 2, 1.0);
36
+ *
37
+ * // Also works with Float32Array
38
+ * const query = new Float32Array([1, 0, 0]);
39
+ * const candidates = [new Float32Array([1, 0, 0]), new Float32Array([0, 1, 0])];
40
+ * const result2 = differentiableSearch(query, candidates, 2, 1.0);
41
+ * ```
42
+ */
43
+ export declare function differentiableSearch(query: number[] | Float32Array | Float64Array, candidates: (number[] | Float32Array | Float64Array)[], k: number, temperature?: number): DifferentiableSearchResult;
44
+ /**
45
+ * GNN Layer for HNSW topology
46
+ */
47
+ export declare class RuvectorLayer {
48
+ private inner;
49
+ /**
50
+ * Create a new Ruvector GNN layer
51
+ *
52
+ * @param inputDim - Dimension of input node embeddings
53
+ * @param hiddenDim - Dimension of hidden representations
54
+ * @param heads - Number of attention heads
55
+ * @param dropout - Dropout rate (0.0 to 1.0)
56
+ */
57
+ constructor(inputDim: number, hiddenDim: number, heads: number, dropout?: number);
58
+ /**
59
+ * Forward pass through the GNN layer
60
+ *
61
+ * @param nodeEmbedding - Current node's embedding
62
+ * @param neighborEmbeddings - Embeddings of neighbor nodes
63
+ * @param edgeWeights - Weights of edges to neighbors
64
+ * @returns Updated node embedding
65
+ */
66
+ forward(nodeEmbedding: number[] | Float32Array, neighborEmbeddings: (number[] | Float32Array)[], edgeWeights: number[] | Float32Array): number[];
67
+ /**
68
+ * Serialize the layer to JSON
69
+ */
70
+ toJson(): string;
71
+ /**
72
+ * Deserialize the layer from JSON
73
+ */
74
+ static fromJson(json: string): RuvectorLayer;
75
+ }
76
+ /**
77
+ * Tensor compressor with adaptive level selection
78
+ */
79
+ export declare class TensorCompress {
80
+ private inner;
81
+ constructor();
82
+ /**
83
+ * Compress an embedding based on access frequency
84
+ *
85
+ * @param embedding - Input embedding vector
86
+ * @param accessFreq - Access frequency (0.0 to 1.0)
87
+ * @returns Compressed tensor as JSON string
88
+ */
89
+ compress(embedding: number[] | Float32Array, accessFreq: number): string;
90
+ /**
91
+ * Decompress a compressed tensor
92
+ *
93
+ * @param compressedJson - Compressed tensor JSON
94
+ * @returns Decompressed embedding
95
+ */
96
+ decompress(compressedJson: string): number[];
97
+ }
98
+ /**
99
+ * Hierarchical forward pass through GNN layers
100
+ *
101
+ * @param query - Query vector
102
+ * @param layerEmbeddings - Embeddings organized by layer
103
+ * @param gnnLayersJson - JSON array of serialized GNN layers
104
+ * @returns Final embedding after hierarchical processing
105
+ */
106
+ export declare function hierarchicalForward(query: number[] | Float32Array, layerEmbeddings: (number[] | Float32Array)[][], gnnLayersJson: string[]): number[];
107
+ /**
108
+ * Get compression level for a given access frequency
109
+ */
110
+ export declare function getCompressionLevel(accessFreq: number): string;
111
+ /**
112
+ * Check if GNN module is available
113
+ */
114
+ export declare function isGnnAvailable(): boolean;
115
+ declare const _default: {
116
+ differentiableSearch: typeof differentiableSearch;
117
+ RuvectorLayer: typeof RuvectorLayer;
118
+ TensorCompress: typeof TensorCompress;
119
+ hierarchicalForward: typeof hierarchicalForward;
120
+ getCompressionLevel: typeof getCompressionLevel;
121
+ isGnnAvailable: typeof isGnnAvailable;
122
+ };
123
+ export default _default;
124
+ //# sourceMappingURL=gnn-wrapper.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gnn-wrapper.d.ts","sourceRoot":"","sources":["../../src/core/gnn-wrapper.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAuCH;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC,kCAAkC;IAClC,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,wCAAwC;IACxC,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY,GAAG,YAAY,EAC7C,UAAU,EAAE,CAAC,MAAM,EAAE,GAAG,YAAY,GAAG,YAAY,CAAC,EAAE,EACtD,CAAC,EAAE,MAAM,EACT,WAAW,GAAE,MAAY,GACxB,0BAA0B,CAQ5B;AAED;;GAEG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,KAAK,CAAM;IAEnB;;;;;;;OAOG;gBACS,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,MAAY;IAKrF;;;;;;;OAOG;IACH,OAAO,CACL,aAAa,EAAE,MAAM,EAAE,GAAG,YAAY,EACtC,kBAAkB,EAAE,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,EAAE,EAC/C,WAAW,EAAE,MAAM,EAAE,GAAG,YAAY,GACnC,MAAM,EAAE;IAQX;;OAEG;IACH,MAAM,IAAI,MAAM;IAIhB;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa;CAM7C;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,KAAK,CAAM;;IAOnB;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,YAAY,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM;IAIxE;;;;;OAKG;IACH,UAAU,CAAC,cAAc,EAAE,MAAM,GAAG,MAAM,EAAE;CAG7C;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EAAE,GAAG,YAAY,EAC9B,eAAe,EAAE,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,EAAE,EAAE,EAC9C,aAAa,EAAE,MAAM,EAAE,GACtB,MAAM,EAAE,CAOV;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAG9D;AAED;;GAEG;AACH,wBAAgB,cAAc,IAAI,OAAO,CAOxC;;;;;;;;;AAED,wBAOE"}
@@ -0,0 +1,192 @@
1
+ "use strict";
2
+ /**
3
+ * GNN Wrapper - Safe wrapper around @ruvector/gnn with automatic array conversion
4
+ *
5
+ * This wrapper handles the array type conversion automatically, allowing users
6
+ * to pass either regular arrays or Float32Arrays.
7
+ *
8
+ * @ruvector/gnn requires regular JavaScript arrays, NOT Float32Array.
9
+ * This wrapper handles the conversion automatically.
10
+ */
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TensorCompress = exports.RuvectorLayer = void 0;
13
+ exports.differentiableSearch = differentiableSearch;
14
+ exports.hierarchicalForward = hierarchicalForward;
15
+ exports.getCompressionLevel = getCompressionLevel;
16
+ exports.isGnnAvailable = isGnnAvailable;
17
+ // Lazy load to avoid import errors if not installed
18
+ let gnnModule = null;
19
+ let loadError = null;
20
+ function getGnnModule() {
21
+ if (gnnModule)
22
+ return gnnModule;
23
+ if (loadError)
24
+ throw loadError;
25
+ try {
26
+ gnnModule = require('@ruvector/gnn');
27
+ return gnnModule;
28
+ }
29
+ catch (e) {
30
+ loadError = new Error(`@ruvector/gnn is not installed or failed to load: ${e.message}\n` +
31
+ `Install with: npm install @ruvector/gnn`);
32
+ throw loadError;
33
+ }
34
+ }
35
+ /**
36
+ * Convert any array-like input to a regular JavaScript array
37
+ */
38
+ function toArray(input) {
39
+ if (Array.isArray(input)) {
40
+ return input;
41
+ }
42
+ return Array.from(input);
43
+ }
44
+ /**
45
+ * Convert nested arrays/typed arrays to regular JavaScript arrays
46
+ */
47
+ function toNestedArray(input) {
48
+ return input.map(arr => toArray(arr));
49
+ }
50
+ /**
51
+ * Differentiable search using soft attention mechanism
52
+ *
53
+ * This wrapper automatically converts Float32Array inputs to regular arrays.
54
+ *
55
+ * @param query - Query vector (array or Float32Array)
56
+ * @param candidates - List of candidate vectors (arrays or Float32Arrays)
57
+ * @param k - Number of top results to return
58
+ * @param temperature - Temperature for softmax (lower = sharper, higher = smoother)
59
+ * @returns Search result with indices and soft weights
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * import { differentiableSearch } from 'ruvector/core/gnn-wrapper';
64
+ *
65
+ * // Works with regular arrays
66
+ * const result1 = differentiableSearch([1, 0, 0], [[1, 0, 0], [0, 1, 0]], 2, 1.0);
67
+ *
68
+ * // Also works with Float32Array
69
+ * const query = new Float32Array([1, 0, 0]);
70
+ * const candidates = [new Float32Array([1, 0, 0]), new Float32Array([0, 1, 0])];
71
+ * const result2 = differentiableSearch(query, candidates, 2, 1.0);
72
+ * ```
73
+ */
74
+ function differentiableSearch(query, candidates, k, temperature = 1.0) {
75
+ const gnn = getGnnModule();
76
+ // Convert to regular arrays (required by @ruvector/gnn)
77
+ const queryArray = toArray(query);
78
+ const candidatesArray = toNestedArray(candidates);
79
+ return gnn.differentiableSearch(queryArray, candidatesArray, k, temperature);
80
+ }
81
+ /**
82
+ * GNN Layer for HNSW topology
83
+ */
84
+ class RuvectorLayer {
85
+ /**
86
+ * Create a new Ruvector GNN layer
87
+ *
88
+ * @param inputDim - Dimension of input node embeddings
89
+ * @param hiddenDim - Dimension of hidden representations
90
+ * @param heads - Number of attention heads
91
+ * @param dropout - Dropout rate (0.0 to 1.0)
92
+ */
93
+ constructor(inputDim, hiddenDim, heads, dropout = 0.1) {
94
+ const gnn = getGnnModule();
95
+ this.inner = new gnn.RuvectorLayer(inputDim, hiddenDim, heads, dropout);
96
+ }
97
+ /**
98
+ * Forward pass through the GNN layer
99
+ *
100
+ * @param nodeEmbedding - Current node's embedding
101
+ * @param neighborEmbeddings - Embeddings of neighbor nodes
102
+ * @param edgeWeights - Weights of edges to neighbors
103
+ * @returns Updated node embedding
104
+ */
105
+ forward(nodeEmbedding, neighborEmbeddings, edgeWeights) {
106
+ return this.inner.forward(toArray(nodeEmbedding), toNestedArray(neighborEmbeddings), toArray(edgeWeights));
107
+ }
108
+ /**
109
+ * Serialize the layer to JSON
110
+ */
111
+ toJson() {
112
+ return this.inner.toJson();
113
+ }
114
+ /**
115
+ * Deserialize the layer from JSON
116
+ */
117
+ static fromJson(json) {
118
+ const gnn = getGnnModule();
119
+ const layer = new RuvectorLayer(1, 1, 1, 0); // Dummy constructor
120
+ layer.inner = gnn.RuvectorLayer.fromJson(json);
121
+ return layer;
122
+ }
123
+ }
124
+ exports.RuvectorLayer = RuvectorLayer;
125
+ /**
126
+ * Tensor compressor with adaptive level selection
127
+ */
128
+ class TensorCompress {
129
+ constructor() {
130
+ const gnn = getGnnModule();
131
+ this.inner = new gnn.TensorCompress();
132
+ }
133
+ /**
134
+ * Compress an embedding based on access frequency
135
+ *
136
+ * @param embedding - Input embedding vector
137
+ * @param accessFreq - Access frequency (0.0 to 1.0)
138
+ * @returns Compressed tensor as JSON string
139
+ */
140
+ compress(embedding, accessFreq) {
141
+ return this.inner.compress(toArray(embedding), accessFreq);
142
+ }
143
+ /**
144
+ * Decompress a compressed tensor
145
+ *
146
+ * @param compressedJson - Compressed tensor JSON
147
+ * @returns Decompressed embedding
148
+ */
149
+ decompress(compressedJson) {
150
+ return this.inner.decompress(compressedJson);
151
+ }
152
+ }
153
+ exports.TensorCompress = TensorCompress;
154
+ /**
155
+ * Hierarchical forward pass through GNN layers
156
+ *
157
+ * @param query - Query vector
158
+ * @param layerEmbeddings - Embeddings organized by layer
159
+ * @param gnnLayersJson - JSON array of serialized GNN layers
160
+ * @returns Final embedding after hierarchical processing
161
+ */
162
+ function hierarchicalForward(query, layerEmbeddings, gnnLayersJson) {
163
+ const gnn = getGnnModule();
164
+ return gnn.hierarchicalForward(toArray(query), layerEmbeddings.map(layer => toNestedArray(layer)), gnnLayersJson);
165
+ }
166
+ /**
167
+ * Get compression level for a given access frequency
168
+ */
169
+ function getCompressionLevel(accessFreq) {
170
+ const gnn = getGnnModule();
171
+ return gnn.getCompressionLevel(accessFreq);
172
+ }
173
+ /**
174
+ * Check if GNN module is available
175
+ */
176
+ function isGnnAvailable() {
177
+ try {
178
+ getGnnModule();
179
+ return true;
180
+ }
181
+ catch {
182
+ return false;
183
+ }
184
+ }
185
+ exports.default = {
186
+ differentiableSearch,
187
+ RuvectorLayer,
188
+ TensorCompress,
189
+ hierarchicalForward,
190
+ getCompressionLevel,
191
+ isGnnAvailable,
192
+ };