@sparkleideas/ruvector-upstream 3.0.0-alpha.11

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,182 @@
1
+ /**
2
+ * WASM Module Registry
3
+ *
4
+ * Centralized registry for all RuVector WASM modules.
5
+ * Handles lazy loading, caching, and lifecycle management.
6
+ */
7
+
8
+ import type { WasmBridge, WasmModuleStatus } from './types.js';
9
+
10
+ /**
11
+ * Registry entry for a WASM module
12
+ */
13
+ interface RegistryEntry {
14
+ bridge: WasmBridge;
15
+ loadedAt?: Date;
16
+ lastUsed?: Date;
17
+ useCount: number;
18
+ }
19
+
20
+ /**
21
+ * WASM Module Registry
22
+ */
23
+ export class WasmRegistry {
24
+ private modules: Map<string, RegistryEntry> = new Map();
25
+ private initPromises: Map<string, Promise<void>> = new Map();
26
+
27
+ /**
28
+ * Register a WASM bridge
29
+ */
30
+ register(name: string, bridge: WasmBridge): void {
31
+ if (this.modules.has(name)) {
32
+ console.warn(`WASM module '${name}' already registered, skipping`);
33
+ return;
34
+ }
35
+
36
+ this.modules.set(name, {
37
+ bridge,
38
+ useCount: 0,
39
+ });
40
+ }
41
+
42
+ /**
43
+ * Get a WASM bridge by name
44
+ */
45
+ async get<T = unknown>(name: string): Promise<WasmBridge<T> | null> {
46
+ const entry = this.modules.get(name);
47
+ if (!entry) {
48
+ return null;
49
+ }
50
+
51
+ // Initialize if needed
52
+ if (!entry.bridge.isReady()) {
53
+ await this.ensureInitialized(name);
54
+ }
55
+
56
+ // Update usage stats
57
+ entry.lastUsed = new Date();
58
+ entry.useCount++;
59
+
60
+ return entry.bridge as WasmBridge<T>;
61
+ }
62
+
63
+ /**
64
+ * Ensure a module is initialized (with deduplication)
65
+ */
66
+ private async ensureInitialized(name: string): Promise<void> {
67
+ // Check if already initializing
68
+ const existing = this.initPromises.get(name);
69
+ if (existing) {
70
+ return existing;
71
+ }
72
+
73
+ const entry = this.modules.get(name);
74
+ if (!entry) {
75
+ throw new Error(`WASM module '${name}' not registered`);
76
+ }
77
+
78
+ // Start initialization
79
+ const promise = entry.bridge.init().then(() => {
80
+ entry.loadedAt = new Date();
81
+ this.initPromises.delete(name);
82
+ }).catch((error) => {
83
+ this.initPromises.delete(name);
84
+ throw error;
85
+ });
86
+
87
+ this.initPromises.set(name, promise);
88
+ return promise;
89
+ }
90
+
91
+ /**
92
+ * Check if a module is registered
93
+ */
94
+ has(name: string): boolean {
95
+ return this.modules.has(name);
96
+ }
97
+
98
+ /**
99
+ * Get module status
100
+ */
101
+ getStatus(name: string): WasmModuleStatus | null {
102
+ const entry = this.modules.get(name);
103
+ return entry?.bridge.status ?? null;
104
+ }
105
+
106
+ /**
107
+ * List all registered modules
108
+ */
109
+ list(): Array<{
110
+ name: string;
111
+ status: WasmModuleStatus;
112
+ version: string;
113
+ useCount: number;
114
+ }> {
115
+ return Array.from(this.modules.entries()).map(([name, entry]) => ({
116
+ name,
117
+ status: entry.bridge.status,
118
+ version: entry.bridge.version,
119
+ useCount: entry.useCount,
120
+ }));
121
+ }
122
+
123
+ /**
124
+ * Unload a module to free memory
125
+ */
126
+ async unload(name: string): Promise<void> {
127
+ const entry = this.modules.get(name);
128
+ if (!entry) {
129
+ return;
130
+ }
131
+
132
+ await entry.bridge.destroy();
133
+ }
134
+
135
+ /**
136
+ * Unload all modules
137
+ */
138
+ async unloadAll(): Promise<void> {
139
+ const promises = Array.from(this.modules.keys()).map(name => this.unload(name));
140
+ await Promise.all(promises);
141
+ }
142
+
143
+ /**
144
+ * Get registry statistics
145
+ */
146
+ getStats(): {
147
+ totalModules: number;
148
+ loadedModules: number;
149
+ totalUseCount: number;
150
+ memoryEstimateMb: number;
151
+ } {
152
+ let loadedCount = 0;
153
+ let totalUse = 0;
154
+
155
+ for (const entry of this.modules.values()) {
156
+ if (entry.bridge.status === 'ready') {
157
+ loadedCount++;
158
+ }
159
+ totalUse += entry.useCount;
160
+ }
161
+
162
+ return {
163
+ totalModules: this.modules.size,
164
+ loadedModules: loadedCount,
165
+ totalUseCount: totalUse,
166
+ memoryEstimateMb: loadedCount * 2, // Rough estimate: 2MB per loaded module
167
+ };
168
+ }
169
+ }
170
+
171
+ // Singleton instance
172
+ let registryInstance: WasmRegistry | null = null;
173
+
174
+ /**
175
+ * Get the global WASM registry instance
176
+ */
177
+ export function getWasmRegistry(): WasmRegistry {
178
+ if (!registryInstance) {
179
+ registryInstance = new WasmRegistry();
180
+ }
181
+ return registryInstance;
182
+ }
package/src/types.ts ADDED
@@ -0,0 +1,146 @@
1
+ /**
2
+ * RuVector WASM Types
3
+ */
4
+
5
+ import { z } from 'zod';
6
+
7
+ /**
8
+ * WASM module status
9
+ */
10
+ export type WasmModuleStatus = 'unloaded' | 'loading' | 'ready' | 'error';
11
+
12
+ /**
13
+ * Base WASM bridge interface
14
+ */
15
+ export interface WasmBridge<T = unknown> {
16
+ readonly name: string;
17
+ readonly version: string;
18
+ readonly status: WasmModuleStatus;
19
+
20
+ init(): Promise<void>;
21
+ destroy(): Promise<void>;
22
+ isReady(): boolean;
23
+ getModule(): T | null;
24
+ }
25
+
26
+ /**
27
+ * HNSW configuration
28
+ */
29
+ export const HnswConfigSchema = z.object({
30
+ dimensions: z.number().min(1).max(4096).default(384),
31
+ maxElements: z.number().min(100).max(10_000_000).default(100_000),
32
+ efConstruction: z.number().min(10).max(500).default(200),
33
+ M: z.number().min(4).max(64).default(16),
34
+ efSearch: z.number().min(10).max(500).default(100),
35
+ });
36
+
37
+ export type HnswConfig = z.infer<typeof HnswConfigSchema>;
38
+
39
+ /**
40
+ * Vector search result
41
+ */
42
+ export interface SearchResult {
43
+ id: string;
44
+ score: number;
45
+ vector?: Float32Array;
46
+ metadata?: Record<string, unknown>;
47
+ }
48
+
49
+ /**
50
+ * Flash attention configuration
51
+ */
52
+ export const AttentionConfigSchema = z.object({
53
+ headDim: z.number().min(16).max(256).default(64),
54
+ numHeads: z.number().min(1).max(128).default(8),
55
+ seqLength: z.number().min(1).max(32768).default(512),
56
+ causal: z.boolean().default(true),
57
+ dropout: z.number().min(0).max(1).default(0),
58
+ });
59
+
60
+ export type AttentionConfig = z.infer<typeof AttentionConfigSchema>;
61
+
62
+ /**
63
+ * GNN configuration
64
+ */
65
+ export const GnnConfigSchema = z.object({
66
+ inputDim: z.number().min(1).max(4096).default(384),
67
+ hiddenDim: z.number().min(16).max(2048).default(256),
68
+ outputDim: z.number().min(1).max(4096).default(128),
69
+ numLayers: z.number().min(1).max(10).default(3),
70
+ aggregation: z.enum(['mean', 'sum', 'max', 'attention']).default('mean'),
71
+ });
72
+
73
+ export type GnnConfig = z.infer<typeof GnnConfigSchema>;
74
+
75
+ /**
76
+ * Hyperbolic embedding configuration
77
+ */
78
+ export const HyperbolicConfigSchema = z.object({
79
+ dimensions: z.number().min(2).max(1024).default(64),
80
+ curvature: z.number().min(-10).max(-0.01).default(-1),
81
+ model: z.enum(['poincare', 'lorentz', 'klein']).default('poincare'),
82
+ });
83
+
84
+ export type HyperbolicConfig = z.infer<typeof HyperbolicConfigSchema>;
85
+
86
+ /**
87
+ * Reinforcement learning configuration
88
+ */
89
+ export const LearningConfigSchema = z.object({
90
+ algorithm: z.enum([
91
+ 'q-learning',
92
+ 'sarsa',
93
+ 'actor-critic',
94
+ 'ppo',
95
+ 'dqn',
96
+ 'decision-transformer',
97
+ ]).default('ppo'),
98
+ learningRate: z.number().min(0.00001).max(1).default(0.001),
99
+ gamma: z.number().min(0).max(1).default(0.99),
100
+ batchSize: z.number().min(1).max(1024).default(64),
101
+ });
102
+
103
+ export type LearningConfig = z.infer<typeof LearningConfigSchema>;
104
+
105
+ /**
106
+ * Quantum-inspired optimization configuration
107
+ */
108
+ export const ExoticConfigSchema = z.object({
109
+ algorithm: z.enum([
110
+ 'qaoa',
111
+ 'vqe',
112
+ 'grover',
113
+ 'quantum-annealing',
114
+ 'tensor-network',
115
+ ]).default('qaoa'),
116
+ numQubits: z.number().min(2).max(64).default(16),
117
+ depth: z.number().min(1).max(100).default(4),
118
+ shots: z.number().min(100).max(100000).default(1000),
119
+ });
120
+
121
+ export type ExoticConfig = z.infer<typeof ExoticConfigSchema>;
122
+
123
+ /**
124
+ * Cognitive kernel configuration
125
+ */
126
+ export const CognitiveConfigSchema = z.object({
127
+ workingMemorySize: z.number().min(3).max(15).default(7),
128
+ attentionSpan: z.number().min(1).max(100).default(10),
129
+ metaCognitionEnabled: z.boolean().default(true),
130
+ scaffoldingLevel: z.enum(['none', 'light', 'moderate', 'heavy']).default('light'),
131
+ });
132
+
133
+ export type CognitiveConfig = z.infer<typeof CognitiveConfigSchema>;
134
+
135
+ /**
136
+ * SONA (Self-Optimizing Neural Architecture) configuration
137
+ */
138
+ export const SonaConfigSchema = z.object({
139
+ mode: z.enum(['real-time', 'balanced', 'research', 'edge', 'batch']).default('balanced'),
140
+ loraRank: z.number().min(1).max(64).default(4),
141
+ learningRate: z.number().min(0.00001).max(0.1).default(0.001),
142
+ ewcLambda: z.number().min(0).max(10000).default(100),
143
+ batchSize: z.number().min(1).max(256).default(32),
144
+ });
145
+
146
+ export type SonaConfig = z.infer<typeof SonaConfigSchema>;