File size: 7,935 Bytes
40d7073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
 * IntelligenceEngine - Full RuVector Intelligence Stack
 *
 * Integrates all RuVector capabilities for self-learning hooks:
 * - VectorDB with HNSW for semantic memory (150x faster)
 * - SONA for continual learning (Micro-LoRA, EWC++)
 * - FastAgentDB for episode/trajectory storage
 * - Attention mechanisms for pattern recognition
 * - ReasoningBank for pattern clustering
 *
 * Replaces the simple Q-learning approach with real ML-powered intelligence.
 */
import { EpisodeSearchResult } from './agentdb-fast';
import { SonaConfig, LearnedPattern } from './sona-wrapper';
import { ParallelConfig, BatchEpisode } from './parallel-intelligence';
export interface MemoryEntry {
    id: string;
    content: string;
    type: string;
    embedding: number[];
    created: string;
    accessed: number;
    score?: number;
}
export interface AgentRoute {
    agent: string;
    confidence: number;
    reason: string;
    patterns?: LearnedPattern[];
    alternates?: Array<{
        agent: string;
        confidence: number;
    }>;
}
export interface LearningStats {
    totalMemories: number;
    memoryDimensions: number;
    totalEpisodes: number;
    totalTrajectories: number;
    avgReward: number;
    sonaEnabled: boolean;
    trajectoriesRecorded: number;
    patternsLearned: number;
    microLoraUpdates: number;
    baseLoraUpdates: number;
    ewcConsolidations: number;
    routingPatterns: number;
    errorPatterns: number;
    coEditPatterns: number;
    workerTriggers: number;
    attentionEnabled: boolean;
    onnxEnabled: boolean;
    parallelEnabled: boolean;
    parallelWorkers: number;
    parallelBusy: number;
    parallelQueued: number;
}
export interface IntelligenceConfig {
    /** Embedding dimension for vectors (default: 256, 384 for ONNX) */
    embeddingDim?: number;
    /** Maximum memories to store (default: 100000) */
    maxMemories?: number;
    /** Maximum episodes for trajectory storage (default: 50000) */
    maxEpisodes?: number;
    /** Enable SONA continual learning (default: true if available) */
    enableSona?: boolean;
    /** Enable attention mechanisms (default: true if available) */
    enableAttention?: boolean;
    /** Enable ONNX semantic embeddings (default: false, opt-in for quality) */
    enableOnnx?: boolean;
    /** SONA configuration */
    sonaConfig?: Partial<SonaConfig>;
    /** Storage path for persistence */
    storagePath?: string;
    /** Learning rate for pattern updates (default: 0.1) */
    learningRate?: number;
    /**
     * Enable parallel workers for batch operations
     * Auto-enabled for MCP servers, disabled for CLI hooks
     */
    parallelConfig?: Partial<ParallelConfig>;
}
/**
 * Full-stack intelligence engine using all RuVector capabilities
 */
export declare class IntelligenceEngine {
    private config;
    private vectorDb;
    private agentDb;
    private sona;
    private attention;
    private onnxEmbedder;
    private onnxReady;
    private parallel;
    private memories;
    private routingPatterns;
    private errorPatterns;
    private coEditPatterns;
    private agentMappings;
    private workerTriggerMappings;
    private currentTrajectoryId;
    private sessionStart;
    private learningEnabled;
    private episodeBatchQueue;
    constructor(config?: IntelligenceConfig);
    private initOnnx;
    private initVectorDb;
    private initParallel;
    /**
     * Generate embedding using ONNX, attention, or hash (in order of preference)
     */
    embed(text: string): number[];
    /**
     * Async embedding with ONNX support (recommended for semantic quality)
     */
    embedAsync(text: string): Promise<number[]>;
    /**
     * Attention-based embedding using Flash or Multi-head attention
     */
    private attentionEmbed;
    /**
     * Improved hash-based embedding with positional encoding
     */
    private hashEmbed;
    private tokenize;
    private tokenEmbed;
    private meanPool;
    /**
     * Store content in vector memory (uses ONNX if available)
     */
    remember(content: string, type?: string): Promise<MemoryEntry>;
    /**
     * Semantic search of memories (uses ONNX if available)
     */
    recall(query: string, topK?: number): Promise<MemoryEntry[]>;
    private cosineSimilarity;
    /**
     * Route a task to the best agent using learned patterns
     */
    route(task: string, file?: string): Promise<AgentRoute>;
    private getExtension;
    private getState;
    private getAlternates;
    /**
     * Begin recording a trajectory (before edit/command)
     */
    beginTrajectory(context: string, file?: string): void;
    /**
     * Add a step to the current trajectory
     */
    addTrajectoryStep(activations: number[], reward: number): void;
    /**
     * End the current trajectory with a quality score
     */
    endTrajectory(success: boolean, quality?: number): void;
    /**
     * Set the agent route for current trajectory
     */
    setTrajectoryRoute(agent: string): void;
    /**
     * Record an episode for learning
     */
    recordEpisode(state: string, action: string, reward: number, nextState: string, done: boolean, metadata?: Record<string, any>): Promise<void>;
    /**
     * Queue episode for batch processing (3-4x faster with workers)
     */
    queueEpisode(episode: BatchEpisode): void;
    /**
     * Process queued episodes in parallel batch
     */
    flushEpisodeBatch(): Promise<number>;
    /**
     * Learn from similar past episodes
     */
    learnFromSimilar(state: string, k?: number): Promise<EpisodeSearchResult[]>;
    /**
     * Register worker trigger to agent mappings
     */
    registerWorkerTrigger(trigger: string, priority: string, agents: string[]): void;
    /**
     * Get agents for a worker trigger
     */
    getAgentsForTrigger(trigger: string): {
        priority: string;
        agents: string[];
    } | undefined;
    /**
     * Route a task using worker trigger patterns first, then fall back to regular routing
     */
    routeWithWorkers(task: string, file?: string): Promise<AgentRoute>;
    /**
     * Initialize default worker trigger mappings
     */
    initDefaultWorkerMappings(): void;
    /**
     * Record a co-edit pattern
     */
    recordCoEdit(file1: string, file2: string): void;
    /**
     * Get likely next files to edit
     */
    getLikelyNextFiles(file: string, topK?: number): Array<{
        file: string;
        count: number;
    }>;
    /**
     * Record an error pattern with fixes
     */
    recordErrorFix(errorPattern: string, fix: string): void;
    /**
     * Get suggested fixes for an error
     */
    getSuggestedFixes(error: string): string[];
    /**
     * Run background learning cycle
     */
    tick(): string | null;
    /**
     * Force immediate learning
     */
    forceLearn(): string | null;
    /**
     * Get comprehensive learning statistics
     */
    getStats(): LearningStats;
    /**
     * Export all data for persistence
     */
    export(): Record<string, any>;
    /**
     * Import data from persistence
     */
    import(data: Record<string, any>, merge?: boolean): void;
    /**
     * Clear all data
     */
    clear(): void;
    /** Legacy: patterns object */
    get patterns(): Record<string, Record<string, number>>;
    /** Legacy: file_sequences array */
    get file_sequences(): string[][];
    /** Legacy: errors object */
    get errors(): Record<string, string[]>;
}
/**
 * Create a new IntelligenceEngine with default settings
 */
export declare function createIntelligenceEngine(config?: IntelligenceConfig): IntelligenceEngine;
/**
 * Create a high-performance engine with all features enabled
 */
export declare function createHighPerformanceEngine(): IntelligenceEngine;
/**
 * Create a lightweight engine for fast startup
 */
export declare function createLightweightEngine(): IntelligenceEngine;
export default IntelligenceEngine;
//# sourceMappingURL=intelligence-engine.d.ts.map