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.
@@ -1,301 +0,0 @@
1
- "use strict";
2
- /**
3
- * AgentDB Fast - High-performance in-process alternative to AgentDB CLI
4
- *
5
- * The AgentDB CLI has ~2.3s startup overhead due to npx initialization.
6
- * This module provides 50-200x faster operations by using in-process calls.
7
- *
8
- * Features:
9
- * - In-memory episode storage with LRU eviction
10
- * - Vector similarity search using @ruvector/core
11
- * - Compatible API with AgentDB's episode/trajectory interfaces
12
- */
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.FastAgentDB = void 0;
15
- exports.createFastAgentDB = createFastAgentDB;
16
- exports.getDefaultAgentDB = getDefaultAgentDB;
17
- // Lazy load ruvector core
18
- let coreModule = null;
19
- function getCoreModule() {
20
- if (coreModule)
21
- return coreModule;
22
- try {
23
- coreModule = require('@ruvector/core');
24
- return coreModule;
25
- }
26
- catch {
27
- // Fallback to ruvector if core not available
28
- try {
29
- coreModule = require('ruvector');
30
- return coreModule;
31
- }
32
- catch (e) {
33
- throw new Error(`Neither @ruvector/core nor ruvector is available: ${e.message}`);
34
- }
35
- }
36
- }
37
- /**
38
- * Fast in-memory AgentDB implementation
39
- */
40
- class FastAgentDB {
41
- /**
42
- * Create a new FastAgentDB instance
43
- *
44
- * @param dimensions - Vector dimensions for state embeddings
45
- * @param maxEpisodes - Maximum episodes to store (LRU eviction)
46
- */
47
- constructor(dimensions = 128, maxEpisodes = 100000) {
48
- this.episodes = new Map();
49
- this.trajectories = new Map();
50
- this.vectorDb = null;
51
- this.episodeOrder = []; // For LRU eviction
52
- this.dimensions = dimensions;
53
- this.maxEpisodes = maxEpisodes;
54
- }
55
- /**
56
- * Initialize the vector database
57
- */
58
- async initVectorDb() {
59
- if (this.vectorDb)
60
- return;
61
- try {
62
- const core = getCoreModule();
63
- this.vectorDb = new core.VectorDB({
64
- dimensions: this.dimensions,
65
- distanceMetric: 'Cosine',
66
- });
67
- }
68
- catch (e) {
69
- // Vector DB not available, use fallback similarity
70
- console.warn(`VectorDB not available, using fallback similarity: ${e.message}`);
71
- }
72
- }
73
- /**
74
- * Store an episode
75
- *
76
- * @param episode - Episode to store
77
- * @returns Episode ID
78
- */
79
- async storeEpisode(episode) {
80
- await this.initVectorDb();
81
- const id = episode.id ?? this.generateId();
82
- const fullEpisode = {
83
- ...episode,
84
- id,
85
- timestamp: episode.timestamp ?? Date.now(),
86
- };
87
- // LRU eviction if needed
88
- if (this.episodes.size >= this.maxEpisodes) {
89
- const oldestId = this.episodeOrder.shift();
90
- if (oldestId) {
91
- this.episodes.delete(oldestId);
92
- }
93
- }
94
- this.episodes.set(id, fullEpisode);
95
- this.episodeOrder.push(id);
96
- // Index in vector DB if available
97
- if (this.vectorDb && fullEpisode.state.length === this.dimensions) {
98
- try {
99
- await this.vectorDb.insert({
100
- id,
101
- vector: new Float32Array(fullEpisode.state),
102
- });
103
- }
104
- catch {
105
- // Ignore indexing errors
106
- }
107
- }
108
- return id;
109
- }
110
- /**
111
- * Store multiple episodes in batch
112
- */
113
- async storeEpisodes(episodes) {
114
- const ids = [];
115
- for (const episode of episodes) {
116
- const id = await this.storeEpisode(episode);
117
- ids.push(id);
118
- }
119
- return ids;
120
- }
121
- /**
122
- * Retrieve an episode by ID
123
- */
124
- async getEpisode(id) {
125
- const episode = this.episodes.get(id);
126
- if (episode) {
127
- // Update LRU order
128
- const idx = this.episodeOrder.indexOf(id);
129
- if (idx > -1) {
130
- this.episodeOrder.splice(idx, 1);
131
- this.episodeOrder.push(id);
132
- }
133
- }
134
- return episode ?? null;
135
- }
136
- /**
137
- * Search for similar episodes by state
138
- *
139
- * @param queryState - State vector to search for
140
- * @param k - Number of results to return
141
- * @returns Similar episodes sorted by similarity
142
- */
143
- async searchByState(queryState, k = 10) {
144
- await this.initVectorDb();
145
- const query = Array.isArray(queryState) ? queryState : Array.from(queryState);
146
- // Use vector DB if available
147
- if (this.vectorDb && query.length === this.dimensions) {
148
- try {
149
- const results = await this.vectorDb.search({
150
- vector: new Float32Array(query),
151
- k,
152
- });
153
- return results
154
- .map((r) => {
155
- const episode = this.episodes.get(r.id);
156
- if (!episode)
157
- return null;
158
- return {
159
- episode,
160
- similarity: 1 - r.score, // Convert distance to similarity
161
- };
162
- })
163
- .filter((r) => r !== null);
164
- }
165
- catch {
166
- // Fall through to fallback
167
- }
168
- }
169
- // Fallback: brute-force cosine similarity
170
- return this.fallbackSearch(query, k);
171
- }
172
- /**
173
- * Fallback similarity search using brute-force cosine similarity
174
- */
175
- fallbackSearch(query, k) {
176
- const results = [];
177
- for (const episode of this.episodes.values()) {
178
- if (episode.state.length !== query.length)
179
- continue;
180
- const similarity = this.cosineSimilarity(query, episode.state);
181
- results.push({ episode, similarity });
182
- }
183
- return results
184
- .sort((a, b) => b.similarity - a.similarity)
185
- .slice(0, k);
186
- }
187
- /**
188
- * Compute cosine similarity between two vectors
189
- */
190
- cosineSimilarity(a, b) {
191
- let dotProduct = 0;
192
- let normA = 0;
193
- let normB = 0;
194
- for (let i = 0; i < a.length; i++) {
195
- dotProduct += a[i] * b[i];
196
- normA += a[i] * a[i];
197
- normB += b[i] * b[i];
198
- }
199
- const denom = Math.sqrt(normA) * Math.sqrt(normB);
200
- return denom === 0 ? 0 : dotProduct / denom;
201
- }
202
- /**
203
- * Store a trajectory (sequence of episodes)
204
- */
205
- async storeTrajectory(episodes, metadata) {
206
- const trajectoryId = this.generateId();
207
- const storedEpisodes = [];
208
- let totalReward = 0;
209
- for (const episode of episodes) {
210
- const id = await this.storeEpisode(episode);
211
- const stored = await this.getEpisode(id);
212
- if (stored) {
213
- storedEpisodes.push(stored);
214
- totalReward += stored.reward;
215
- }
216
- }
217
- const trajectory = {
218
- id: trajectoryId,
219
- episodes: storedEpisodes,
220
- totalReward,
221
- metadata,
222
- };
223
- this.trajectories.set(trajectoryId, trajectory);
224
- return trajectoryId;
225
- }
226
- /**
227
- * Get a trajectory by ID
228
- */
229
- async getTrajectory(id) {
230
- return this.trajectories.get(id) ?? null;
231
- }
232
- /**
233
- * Get top trajectories by total reward
234
- */
235
- async getTopTrajectories(k = 10) {
236
- return Array.from(this.trajectories.values())
237
- .sort((a, b) => b.totalReward - a.totalReward)
238
- .slice(0, k);
239
- }
240
- /**
241
- * Sample random episodes (for experience replay)
242
- */
243
- async sampleEpisodes(n) {
244
- const allEpisodes = Array.from(this.episodes.values());
245
- const sampled = [];
246
- for (let i = 0; i < Math.min(n, allEpisodes.length); i++) {
247
- const idx = Math.floor(Math.random() * allEpisodes.length);
248
- sampled.push(allEpisodes[idx]);
249
- }
250
- return sampled;
251
- }
252
- /**
253
- * Get database statistics
254
- */
255
- getStats() {
256
- return {
257
- episodeCount: this.episodes.size,
258
- trajectoryCount: this.trajectories.size,
259
- dimensions: this.dimensions,
260
- maxEpisodes: this.maxEpisodes,
261
- vectorDbAvailable: this.vectorDb !== null,
262
- };
263
- }
264
- /**
265
- * Clear all data
266
- */
267
- clear() {
268
- this.episodes.clear();
269
- this.trajectories.clear();
270
- this.episodeOrder = [];
271
- }
272
- /**
273
- * Generate a unique ID
274
- */
275
- generateId() {
276
- return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
277
- }
278
- }
279
- exports.FastAgentDB = FastAgentDB;
280
- /**
281
- * Create a fast AgentDB instance
282
- */
283
- function createFastAgentDB(dimensions = 128, maxEpisodes = 100000) {
284
- return new FastAgentDB(dimensions, maxEpisodes);
285
- }
286
- // Singleton instance for convenience
287
- let defaultInstance = null;
288
- /**
289
- * Get the default FastAgentDB instance
290
- */
291
- function getDefaultAgentDB() {
292
- if (!defaultInstance) {
293
- defaultInstance = new FastAgentDB();
294
- }
295
- return defaultInstance;
296
- }
297
- exports.default = {
298
- FastAgentDB,
299
- createFastAgentDB,
300
- getDefaultAgentDB,
301
- };
@@ -1,257 +0,0 @@
1
- /**
2
- * IntelligenceEngine - Full RuVector Intelligence Stack
3
- *
4
- * Integrates all RuVector capabilities for self-learning hooks:
5
- * - VectorDB with HNSW for semantic memory (150x faster)
6
- * - SONA for continual learning (Micro-LoRA, EWC++)
7
- * - FastAgentDB for episode/trajectory storage
8
- * - Attention mechanisms for pattern recognition
9
- * - ReasoningBank for pattern clustering
10
- *
11
- * Replaces the simple Q-learning approach with real ML-powered intelligence.
12
- */
13
- import { EpisodeSearchResult } from './agentdb-fast';
14
- import { SonaConfig, LearnedPattern } from './sona-wrapper';
15
- import { ParallelConfig, BatchEpisode } from './parallel-intelligence';
16
- export interface MemoryEntry {
17
- id: string;
18
- content: string;
19
- type: string;
20
- embedding: number[];
21
- created: string;
22
- accessed: number;
23
- score?: number;
24
- }
25
- export interface AgentRoute {
26
- agent: string;
27
- confidence: number;
28
- reason: string;
29
- patterns?: LearnedPattern[];
30
- alternates?: Array<{
31
- agent: string;
32
- confidence: number;
33
- }>;
34
- }
35
- export interface LearningStats {
36
- totalMemories: number;
37
- memoryDimensions: number;
38
- totalEpisodes: number;
39
- totalTrajectories: number;
40
- avgReward: number;
41
- sonaEnabled: boolean;
42
- trajectoriesRecorded: number;
43
- patternsLearned: number;
44
- microLoraUpdates: number;
45
- baseLoraUpdates: number;
46
- ewcConsolidations: number;
47
- routingPatterns: number;
48
- errorPatterns: number;
49
- coEditPatterns: number;
50
- workerTriggers: number;
51
- attentionEnabled: boolean;
52
- onnxEnabled: boolean;
53
- parallelEnabled: boolean;
54
- parallelWorkers: number;
55
- parallelBusy: number;
56
- parallelQueued: number;
57
- }
58
- export interface IntelligenceConfig {
59
- /** Embedding dimension for vectors (default: 256, 384 for ONNX) */
60
- embeddingDim?: number;
61
- /** Maximum memories to store (default: 100000) */
62
- maxMemories?: number;
63
- /** Maximum episodes for trajectory storage (default: 50000) */
64
- maxEpisodes?: number;
65
- /** Enable SONA continual learning (default: true if available) */
66
- enableSona?: boolean;
67
- /** Enable attention mechanisms (default: true if available) */
68
- enableAttention?: boolean;
69
- /** Enable ONNX semantic embeddings (default: false, opt-in for quality) */
70
- enableOnnx?: boolean;
71
- /** SONA configuration */
72
- sonaConfig?: Partial<SonaConfig>;
73
- /** Storage path for persistence */
74
- storagePath?: string;
75
- /** Learning rate for pattern updates (default: 0.1) */
76
- learningRate?: number;
77
- /**
78
- * Enable parallel workers for batch operations
79
- * Auto-enabled for MCP servers, disabled for CLI hooks
80
- */
81
- parallelConfig?: Partial<ParallelConfig>;
82
- }
83
- /**
84
- * Full-stack intelligence engine using all RuVector capabilities
85
- */
86
- export declare class IntelligenceEngine {
87
- private config;
88
- private vectorDb;
89
- private agentDb;
90
- private sona;
91
- private attention;
92
- private onnxEmbedder;
93
- private onnxReady;
94
- private parallel;
95
- private memories;
96
- private routingPatterns;
97
- private errorPatterns;
98
- private coEditPatterns;
99
- private agentMappings;
100
- private workerTriggerMappings;
101
- private currentTrajectoryId;
102
- private sessionStart;
103
- private learningEnabled;
104
- private episodeBatchQueue;
105
- constructor(config?: IntelligenceConfig);
106
- private initOnnx;
107
- private initVectorDb;
108
- private initParallel;
109
- /**
110
- * Generate embedding using ONNX, attention, or hash (in order of preference)
111
- */
112
- embed(text: string): number[];
113
- /**
114
- * Async embedding with ONNX support (recommended for semantic quality)
115
- */
116
- embedAsync(text: string): Promise<number[]>;
117
- /**
118
- * Attention-based embedding using Flash or Multi-head attention
119
- */
120
- private attentionEmbed;
121
- /**
122
- * Improved hash-based embedding with positional encoding
123
- */
124
- private hashEmbed;
125
- private tokenize;
126
- private tokenEmbed;
127
- private meanPool;
128
- /**
129
- * Store content in vector memory (uses ONNX if available)
130
- */
131
- remember(content: string, type?: string): Promise<MemoryEntry>;
132
- /**
133
- * Semantic search of memories (uses ONNX if available)
134
- */
135
- recall(query: string, topK?: number): Promise<MemoryEntry[]>;
136
- private cosineSimilarity;
137
- /**
138
- * Route a task to the best agent using learned patterns
139
- */
140
- route(task: string, file?: string): Promise<AgentRoute>;
141
- private getExtension;
142
- private getState;
143
- private getAlternates;
144
- /**
145
- * Begin recording a trajectory (before edit/command)
146
- */
147
- beginTrajectory(context: string, file?: string): void;
148
- /**
149
- * Add a step to the current trajectory
150
- */
151
- addTrajectoryStep(activations: number[], reward: number): void;
152
- /**
153
- * End the current trajectory with a quality score
154
- */
155
- endTrajectory(success: boolean, quality?: number): void;
156
- /**
157
- * Set the agent route for current trajectory
158
- */
159
- setTrajectoryRoute(agent: string): void;
160
- /**
161
- * Record an episode for learning
162
- */
163
- recordEpisode(state: string, action: string, reward: number, nextState: string, done: boolean, metadata?: Record<string, any>): Promise<void>;
164
- /**
165
- * Queue episode for batch processing (3-4x faster with workers)
166
- */
167
- queueEpisode(episode: BatchEpisode): void;
168
- /**
169
- * Process queued episodes in parallel batch
170
- */
171
- flushEpisodeBatch(): Promise<number>;
172
- /**
173
- * Learn from similar past episodes
174
- */
175
- learnFromSimilar(state: string, k?: number): Promise<EpisodeSearchResult[]>;
176
- /**
177
- * Register worker trigger to agent mappings
178
- */
179
- registerWorkerTrigger(trigger: string, priority: string, agents: string[]): void;
180
- /**
181
- * Get agents for a worker trigger
182
- */
183
- getAgentsForTrigger(trigger: string): {
184
- priority: string;
185
- agents: string[];
186
- } | undefined;
187
- /**
188
- * Route a task using worker trigger patterns first, then fall back to regular routing
189
- */
190
- routeWithWorkers(task: string, file?: string): Promise<AgentRoute>;
191
- /**
192
- * Initialize default worker trigger mappings
193
- */
194
- initDefaultWorkerMappings(): void;
195
- /**
196
- * Record a co-edit pattern
197
- */
198
- recordCoEdit(file1: string, file2: string): void;
199
- /**
200
- * Get likely next files to edit
201
- */
202
- getLikelyNextFiles(file: string, topK?: number): Array<{
203
- file: string;
204
- count: number;
205
- }>;
206
- /**
207
- * Record an error pattern with fixes
208
- */
209
- recordErrorFix(errorPattern: string, fix: string): void;
210
- /**
211
- * Get suggested fixes for an error
212
- */
213
- getSuggestedFixes(error: string): string[];
214
- /**
215
- * Run background learning cycle
216
- */
217
- tick(): string | null;
218
- /**
219
- * Force immediate learning
220
- */
221
- forceLearn(): string | null;
222
- /**
223
- * Get comprehensive learning statistics
224
- */
225
- getStats(): LearningStats;
226
- /**
227
- * Export all data for persistence
228
- */
229
- export(): Record<string, any>;
230
- /**
231
- * Import data from persistence
232
- */
233
- import(data: Record<string, any>, merge?: boolean): void;
234
- /**
235
- * Clear all data
236
- */
237
- clear(): void;
238
- /** Legacy: patterns object */
239
- get patterns(): Record<string, Record<string, number>>;
240
- /** Legacy: file_sequences array */
241
- get file_sequences(): string[][];
242
- /** Legacy: errors object */
243
- get errors(): Record<string, string[]>;
244
- }
245
- /**
246
- * Create a new IntelligenceEngine with default settings
247
- */
248
- export declare function createIntelligenceEngine(config?: IntelligenceConfig): IntelligenceEngine;
249
- /**
250
- * Create a high-performance engine with all features enabled
251
- */
252
- export declare function createHighPerformanceEngine(): IntelligenceEngine;
253
- /**
254
- * Create a lightweight engine for fast startup
255
- */
256
- export declare function createLightweightEngine(): IntelligenceEngine;
257
- export default IntelligenceEngine;