ruvector 0.2.16 → 0.2.18

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.
@@ -1,258 +0,0 @@
1
- "use strict";
2
- /**
3
- * SONA Wrapper - Self-Optimizing Neural Architecture
4
- *
5
- * Provides a safe, flexible interface to @ruvector/sona with:
6
- * - Automatic array type conversion (Array <-> Float64Array)
7
- * - Graceful handling when sona is not installed
8
- * - TypeScript types for all APIs
9
- *
10
- * SONA Features:
11
- * - Micro-LoRA: Ultra-fast rank-1/2 adaptations (~0.1ms)
12
- * - Base-LoRA: Deeper adaptations for complex patterns
13
- * - EWC++: Elastic Weight Consolidation to prevent catastrophic forgetting
14
- * - ReasoningBank: Pattern storage and retrieval
15
- * - Trajectory tracking: Record and learn from execution paths
16
- */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.Sona = exports.SonaEngine = void 0;
19
- exports.isSonaAvailable = isSonaAvailable;
20
- // ============================================================================
21
- // Helper Functions
22
- // ============================================================================
23
- /** Convert any array-like to regular Array (SONA expects number[]) */
24
- function toArray(input) {
25
- if (Array.isArray(input))
26
- return input;
27
- return Array.from(input);
28
- }
29
- // ============================================================================
30
- // Lazy Loading
31
- // ============================================================================
32
- let sonaModule = null;
33
- let sonaLoadError = null;
34
- function getSonaModule() {
35
- if (sonaModule)
36
- return sonaModule;
37
- if (sonaLoadError)
38
- throw sonaLoadError;
39
- try {
40
- sonaModule = require('@ruvector/sona');
41
- return sonaModule;
42
- }
43
- catch (e) {
44
- sonaLoadError = new Error(`@ruvector/sona is not installed. Install it with:\n` +
45
- ` npm install @ruvector/sona\n\n` +
46
- `Original error: ${e.message}`);
47
- throw sonaLoadError;
48
- }
49
- }
50
- /** Check if sona is available */
51
- function isSonaAvailable() {
52
- try {
53
- getSonaModule();
54
- return true;
55
- }
56
- catch {
57
- return false;
58
- }
59
- }
60
- // ============================================================================
61
- // SONA Engine Wrapper
62
- // ============================================================================
63
- /**
64
- * SONA Engine - Self-Optimizing Neural Architecture
65
- *
66
- * Provides runtime-adaptive learning with:
67
- * - Micro-LoRA for instant adaptations
68
- * - Base-LoRA for deeper learning
69
- * - EWC++ for preventing forgetting
70
- * - ReasoningBank for pattern storage
71
- *
72
- * @example
73
- * ```typescript
74
- * import { Sona } from 'ruvector';
75
- *
76
- * // Create engine with hidden dimension
77
- * const engine = new Sona.Engine(256);
78
- *
79
- * // Or with custom config
80
- * const engine = Sona.Engine.withConfig({
81
- * hiddenDim: 256,
82
- * microLoraRank: 2,
83
- * patternClusters: 100
84
- * });
85
- *
86
- * // Record a trajectory
87
- * const trajId = engine.beginTrajectory([0.1, 0.2, ...]);
88
- * engine.addStep(trajId, activations, attentionWeights, 0.8);
89
- * engine.endTrajectory(trajId, 0.9);
90
- *
91
- * // Apply learned adaptations
92
- * const adapted = engine.applyMicroLora(input);
93
- * ```
94
- */
95
- class SonaEngine {
96
- /**
97
- * Create a new SONA engine
98
- * @param hiddenDim Hidden dimension size (e.g., 256, 512, 768)
99
- */
100
- constructor(hiddenDim) {
101
- const mod = getSonaModule();
102
- this._native = new mod.SonaEngine(hiddenDim);
103
- }
104
- /**
105
- * Create engine with custom configuration
106
- * @param config SONA configuration options
107
- */
108
- static withConfig(config) {
109
- const mod = getSonaModule();
110
- const engine = new SonaEngine(config.hiddenDim);
111
- // Replace native with configured version
112
- engine._native = mod.SonaEngine.withConfig(config);
113
- return engine;
114
- }
115
- // -------------------------------------------------------------------------
116
- // Trajectory Recording
117
- // -------------------------------------------------------------------------
118
- /**
119
- * Begin recording a new trajectory
120
- * @param queryEmbedding Initial query embedding
121
- * @returns Trajectory ID for subsequent operations
122
- */
123
- beginTrajectory(queryEmbedding) {
124
- return this._native.beginTrajectory(toArray(queryEmbedding));
125
- }
126
- /**
127
- * Add a step to an active trajectory
128
- * @param trajectoryId Trajectory ID from beginTrajectory
129
- * @param activations Layer activations
130
- * @param attentionWeights Attention weights
131
- * @param reward Reward signal for this step (0.0 - 1.0)
132
- */
133
- addStep(trajectoryId, activations, attentionWeights, reward) {
134
- this._native.addTrajectoryStep(trajectoryId, toArray(activations), toArray(attentionWeights), reward);
135
- }
136
- /**
137
- * Alias for addStep for API compatibility
138
- */
139
- addTrajectoryStep(trajectoryId, activations, attentionWeights, reward) {
140
- this.addStep(trajectoryId, activations, attentionWeights, reward);
141
- }
142
- /**
143
- * Set the model route for a trajectory
144
- * @param trajectoryId Trajectory ID
145
- * @param route Model route identifier (e.g., "gpt-4", "claude-3")
146
- */
147
- setRoute(trajectoryId, route) {
148
- this._native.setTrajectoryRoute(trajectoryId, route);
149
- }
150
- /**
151
- * Add context to a trajectory
152
- * @param trajectoryId Trajectory ID
153
- * @param contextId Context identifier
154
- */
155
- addContext(trajectoryId, contextId) {
156
- this._native.addTrajectoryContext(trajectoryId, contextId);
157
- }
158
- /**
159
- * Complete a trajectory and submit for learning
160
- * @param trajectoryId Trajectory ID
161
- * @param quality Final quality score (0.0 - 1.0)
162
- */
163
- endTrajectory(trajectoryId, quality) {
164
- this._native.endTrajectory(trajectoryId, quality);
165
- }
166
- // -------------------------------------------------------------------------
167
- // LoRA Transformations
168
- // -------------------------------------------------------------------------
169
- /**
170
- * Apply micro-LoRA transformation (ultra-fast, ~0.1ms)
171
- * @param input Input vector
172
- * @returns Transformed output vector
173
- */
174
- applyMicroLora(input) {
175
- return this._native.applyMicroLora(toArray(input));
176
- }
177
- /**
178
- * Apply base-LoRA transformation to a specific layer
179
- * @param layerIdx Layer index
180
- * @param input Input vector
181
- * @returns Transformed output vector
182
- */
183
- applyBaseLora(layerIdx, input) {
184
- return this._native.applyBaseLora(layerIdx, toArray(input));
185
- }
186
- // -------------------------------------------------------------------------
187
- // Learning Control
188
- // -------------------------------------------------------------------------
189
- /**
190
- * Run background learning cycle if due
191
- * Call this periodically (e.g., every few seconds)
192
- * @returns Status message if learning occurred, null otherwise
193
- */
194
- tick() {
195
- return this._native.tick();
196
- }
197
- /**
198
- * Force immediate background learning cycle
199
- * @returns Status message with learning results
200
- */
201
- forceLearn() {
202
- return this._native.forceLearn();
203
- }
204
- /**
205
- * Flush pending instant loop updates
206
- */
207
- flush() {
208
- this._native.flush();
209
- }
210
- // -------------------------------------------------------------------------
211
- // Pattern Retrieval
212
- // -------------------------------------------------------------------------
213
- /**
214
- * Find similar learned patterns to a query
215
- * @param queryEmbedding Query embedding
216
- * @param k Number of patterns to return
217
- * @returns Array of similar patterns
218
- */
219
- findPatterns(queryEmbedding, k) {
220
- return this._native.findPatterns(toArray(queryEmbedding), k);
221
- }
222
- // -------------------------------------------------------------------------
223
- // Engine Control
224
- // -------------------------------------------------------------------------
225
- /**
226
- * Get engine statistics
227
- * @returns Statistics object
228
- */
229
- getStats() {
230
- const statsJson = this._native.getStats();
231
- return JSON.parse(statsJson);
232
- }
233
- /**
234
- * Enable or disable the engine
235
- * @param enabled Whether to enable
236
- */
237
- setEnabled(enabled) {
238
- this._native.setEnabled(enabled);
239
- }
240
- /**
241
- * Check if engine is enabled
242
- */
243
- isEnabled() {
244
- return this._native.isEnabled();
245
- }
246
- }
247
- exports.SonaEngine = SonaEngine;
248
- // ============================================================================
249
- // Convenience Exports
250
- // ============================================================================
251
- /**
252
- * SONA namespace with all exports
253
- */
254
- exports.Sona = {
255
- Engine: SonaEngine,
256
- isAvailable: isSonaAvailable,
257
- };
258
- exports.default = exports.Sona;
@@ -1,144 +0,0 @@
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
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });