@claude-flow/cli 3.0.0-alpha.108 → 3.0.0-alpha.109
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.
- package/README.md +21 -0
- package/dist/src/commands/hooks.d.ts.map +1 -1
- package/dist/src/commands/hooks.js +243 -1
- package/dist/src/commands/hooks.js.map +1 -1
- package/dist/src/mcp-tools/agent-tools.d.ts +1 -0
- package/dist/src/mcp-tools/agent-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/agent-tools.js +127 -4
- package/dist/src/mcp-tools/agent-tools.js.map +1 -1
- package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -1
- package/dist/src/mcp-tools/hooks-tools.js +41 -1
- package/dist/src/mcp-tools/hooks-tools.js.map +1 -1
- package/dist/src/ruvector/enhanced-model-router.d.ts +146 -0
- package/dist/src/ruvector/enhanced-model-router.d.ts.map +1 -0
- package/dist/src/ruvector/enhanced-model-router.js +517 -0
- package/dist/src/ruvector/enhanced-model-router.js.map +1 -0
- package/dist/src/ruvector/flash-attention.d.ts +35 -2
- package/dist/src/ruvector/flash-attention.d.ts.map +1 -1
- package/dist/src/ruvector/flash-attention.js +226 -9
- package/dist/src/ruvector/flash-attention.js.map +1 -1
- package/dist/src/ruvector/model-router.js +1 -1
- package/dist/src/ruvector/model-router.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced Model Router with Agent Booster AST Integration
|
|
3
|
+
*
|
|
4
|
+
* Implements ADR-026: 3-tier intelligent model routing:
|
|
5
|
+
* - Tier 1: Agent Booster (WASM) - <1ms, $0 for simple transforms
|
|
6
|
+
* - Tier 2: Haiku - ~500ms for low complexity
|
|
7
|
+
* - Tier 3: Sonnet/Opus - 2-5s for high complexity
|
|
8
|
+
*
|
|
9
|
+
* @module enhanced-model-router
|
|
10
|
+
*/
|
|
11
|
+
import { existsSync, readFileSync } from 'fs';
|
|
12
|
+
import { extname } from 'path';
|
|
13
|
+
import { getModelRouter } from './model-router.js';
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Intent Detection Patterns
|
|
16
|
+
// ============================================================================
|
|
17
|
+
/**
|
|
18
|
+
* Pattern definitions for Agent Booster intent detection
|
|
19
|
+
*/
|
|
20
|
+
const INTENT_PATTERNS = {
|
|
21
|
+
'var-to-const': {
|
|
22
|
+
patterns: [
|
|
23
|
+
/convert\s+var\s+to\s+const/i,
|
|
24
|
+
/change\s+var\s+to\s+const/i,
|
|
25
|
+
/change\s+var\s+declarations?\s+to\s+const/i,
|
|
26
|
+
/replace\s+var\s+with\s+const/i,
|
|
27
|
+
/var\s*(?:→|->|to)\s*const/i,
|
|
28
|
+
/use\s+const\s+instead\s+of\s+var/i,
|
|
29
|
+
],
|
|
30
|
+
weight: 1.0,
|
|
31
|
+
description: 'Convert var declarations to const/let',
|
|
32
|
+
},
|
|
33
|
+
'add-types': {
|
|
34
|
+
patterns: [
|
|
35
|
+
/add\s+type\s+annotations?/i,
|
|
36
|
+
/add\s+typescript\s+types?/i,
|
|
37
|
+
/type\s+this\s+function/i,
|
|
38
|
+
/add\s+types?\s+to/i,
|
|
39
|
+
/annotate\s+with\s+types?/i,
|
|
40
|
+
],
|
|
41
|
+
weight: 0.9,
|
|
42
|
+
description: 'Add TypeScript type annotations',
|
|
43
|
+
},
|
|
44
|
+
'add-error-handling': {
|
|
45
|
+
patterns: [
|
|
46
|
+
/add\s+error\s+handling/i,
|
|
47
|
+
/wrap\s+in\s+try\s*[/-]?\s*catch/i,
|
|
48
|
+
/add\s+try\s*[/-]?\s*catch/i,
|
|
49
|
+
/handle\s+errors?/i,
|
|
50
|
+
/add\s+exception\s+handling/i,
|
|
51
|
+
],
|
|
52
|
+
weight: 0.7, // Lower weight - often needs more context
|
|
53
|
+
description: 'Wrap code in try/catch blocks',
|
|
54
|
+
},
|
|
55
|
+
'async-await': {
|
|
56
|
+
patterns: [
|
|
57
|
+
/convert\s+to\s+async\s*[/-]?\s*await/i,
|
|
58
|
+
/convert\s+\w+\s+to\s+async/i,
|
|
59
|
+
/use\s+async\s*[/-]?\s*await/i,
|
|
60
|
+
/change\s+promises?\s+to\s+async/i,
|
|
61
|
+
/refactor\s+to\s+async/i,
|
|
62
|
+
/\.then\s*(?:→|->|to)\s*await/i,
|
|
63
|
+
/callback\s+to\s+async/i,
|
|
64
|
+
/callbacks?\s+to\s+async/i,
|
|
65
|
+
],
|
|
66
|
+
weight: 0.8,
|
|
67
|
+
description: 'Convert callbacks/promises to async/await',
|
|
68
|
+
},
|
|
69
|
+
'add-logging': {
|
|
70
|
+
patterns: [
|
|
71
|
+
/add\s+logging/i,
|
|
72
|
+
/add\s+console\.log/i,
|
|
73
|
+
/add\s+debug\s+logs?/i,
|
|
74
|
+
/log\s+this\s+function/i,
|
|
75
|
+
/add\s+trace\s+logging/i,
|
|
76
|
+
],
|
|
77
|
+
weight: 0.85,
|
|
78
|
+
description: 'Add console.log or logging statements',
|
|
79
|
+
},
|
|
80
|
+
'remove-console': {
|
|
81
|
+
patterns: [
|
|
82
|
+
/remove\s+console\.log/i,
|
|
83
|
+
/delete\s+console\s+statements?/i,
|
|
84
|
+
/strip\s+console/i,
|
|
85
|
+
/clean\s+up\s+console/i,
|
|
86
|
+
/clean\s+up\s+debug\s+logs?/i,
|
|
87
|
+
/remove\s+debug\s+logs?/i,
|
|
88
|
+
],
|
|
89
|
+
weight: 0.95,
|
|
90
|
+
description: 'Remove console.* calls',
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* File path extraction patterns
|
|
95
|
+
*/
|
|
96
|
+
const FILE_PATH_PATTERNS = [
|
|
97
|
+
/(?:in|from|to|file|path)\s+[`"']?([a-zA-Z0-9_./\\-]+\.[a-zA-Z]+)[`"']?/i,
|
|
98
|
+
/[`"']([a-zA-Z0-9_./\\-]+\.[a-zA-Z]+)[`"']/,
|
|
99
|
+
/(\S+\.[tj]sx?)\b/i,
|
|
100
|
+
/(\S+\.(?:js|ts|jsx|tsx|py|rb|go|rs|java|kt|swift|c|cpp|h))\b/i,
|
|
101
|
+
];
|
|
102
|
+
/**
|
|
103
|
+
* Language detection by extension
|
|
104
|
+
*/
|
|
105
|
+
/**
|
|
106
|
+
* High-complexity keywords that indicate Tier 3 (Opus) routing
|
|
107
|
+
* These tasks require deep reasoning and architectural understanding
|
|
108
|
+
*/
|
|
109
|
+
const TIER3_KEYWORDS = [
|
|
110
|
+
// Architecture & Design
|
|
111
|
+
/\b(microservices?|architecture|system\s+design|distributed)\b/i,
|
|
112
|
+
/\b(design|architect|plan)\s+(a|an|the|complex)\b/i,
|
|
113
|
+
/\b(design)\s+\w+\s+(schema|system|architecture)\b/i,
|
|
114
|
+
// Security
|
|
115
|
+
/\b(oauth2?|pkce|jwt|rbac|authentication\s+system|security\s+audit)\b/i,
|
|
116
|
+
/\b(refresh\s+token|token\s+rotation|role-based|permission|authorization)\b/i,
|
|
117
|
+
/\b(encryption|cryptograph|certificate|ssl|tls)\b/i,
|
|
118
|
+
/\b(end-to-end\s+encryption|key\s+rotation|secure\s+channel)\b/i,
|
|
119
|
+
// Distributed Systems
|
|
120
|
+
/\b(consensus|distributed|byzantine|raft|paxos)\b/i,
|
|
121
|
+
/\b(replication|sharding|partitioning|eventual\s+consistency)\b/i,
|
|
122
|
+
/\b(load\s+balanc|fault[- ]toleran|high\s+availability)\b/i,
|
|
123
|
+
/\b(message\s+queue|event\s+sourc|cqrs|saga)\b/i,
|
|
124
|
+
// Complex Algorithms
|
|
125
|
+
/\b(algorithm|machine\s+learning|neural|optimization)\b/i,
|
|
126
|
+
/\b(graph\s+algorithm|tree\s+traversal|dynamic\s+programming)\b/i,
|
|
127
|
+
// Database Design
|
|
128
|
+
/\b(schema\s+design|database\s+architect|data\s+model)\b/i,
|
|
129
|
+
/\b(database\s+schema|multi[- ]tenant)\b/i,
|
|
130
|
+
/\b(normalization|denormalization|index\s+strateg)\b/i,
|
|
131
|
+
// Performance Critical
|
|
132
|
+
/\b(performance\s+critical|low\s+latency|high\s+throughput)\b/i,
|
|
133
|
+
/\b(memory\s+optimi|cache\s+strateg|concurrent)\b/i,
|
|
134
|
+
];
|
|
135
|
+
const LANGUAGE_MAP = {
|
|
136
|
+
'.js': 'javascript',
|
|
137
|
+
'.jsx': 'javascript',
|
|
138
|
+
'.ts': 'typescript',
|
|
139
|
+
'.tsx': 'typescript',
|
|
140
|
+
'.py': 'python',
|
|
141
|
+
'.rb': 'ruby',
|
|
142
|
+
'.go': 'go',
|
|
143
|
+
'.rs': 'rust',
|
|
144
|
+
'.java': 'java',
|
|
145
|
+
'.kt': 'kotlin',
|
|
146
|
+
'.swift': 'swift',
|
|
147
|
+
'.c': 'c',
|
|
148
|
+
'.cpp': 'cpp',
|
|
149
|
+
'.h': 'c',
|
|
150
|
+
};
|
|
151
|
+
// ============================================================================
|
|
152
|
+
// Enhanced Model Router Implementation
|
|
153
|
+
// ============================================================================
|
|
154
|
+
/**
|
|
155
|
+
* Enhanced Model Router with Agent Booster AST integration
|
|
156
|
+
*
|
|
157
|
+
* Provides intelligent 3-tier routing:
|
|
158
|
+
* - Tier 1: Agent Booster for simple code transforms (352x faster, $0)
|
|
159
|
+
* - Tier 2: Haiku for low complexity tasks
|
|
160
|
+
* - Tier 3: Sonnet/Opus for complex reasoning tasks
|
|
161
|
+
*/
|
|
162
|
+
export class EnhancedModelRouter {
|
|
163
|
+
config;
|
|
164
|
+
tinyDancerRouter;
|
|
165
|
+
constructor(config) {
|
|
166
|
+
this.config = {
|
|
167
|
+
agentBoosterEnabled: true,
|
|
168
|
+
agentBoosterConfidenceThreshold: 0.7,
|
|
169
|
+
enabledIntents: [
|
|
170
|
+
'var-to-const',
|
|
171
|
+
'add-types',
|
|
172
|
+
'add-error-handling',
|
|
173
|
+
'async-await',
|
|
174
|
+
'add-logging',
|
|
175
|
+
'remove-console',
|
|
176
|
+
],
|
|
177
|
+
complexityThresholds: {
|
|
178
|
+
haiku: 0.3,
|
|
179
|
+
sonnet: 0.6,
|
|
180
|
+
opus: 1.0,
|
|
181
|
+
},
|
|
182
|
+
preferCost: false,
|
|
183
|
+
preferQuality: false,
|
|
184
|
+
...config,
|
|
185
|
+
};
|
|
186
|
+
this.tinyDancerRouter = getModelRouter();
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Detect code editing intent from task description
|
|
190
|
+
*/
|
|
191
|
+
detectIntent(task) {
|
|
192
|
+
const taskLower = task.toLowerCase();
|
|
193
|
+
let bestIntent = null;
|
|
194
|
+
let bestScore = 0;
|
|
195
|
+
for (const [intentType, config] of Object.entries(INTENT_PATTERNS)) {
|
|
196
|
+
if (!this.config.enabledIntents.includes(intentType)) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
for (const pattern of config.patterns) {
|
|
200
|
+
if (pattern.test(taskLower)) {
|
|
201
|
+
const score = config.weight;
|
|
202
|
+
if (score > bestScore) {
|
|
203
|
+
bestScore = score;
|
|
204
|
+
bestIntent = {
|
|
205
|
+
type: intentType,
|
|
206
|
+
confidence: score,
|
|
207
|
+
description: config.description,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Extract file path if intent found
|
|
214
|
+
if (bestIntent) {
|
|
215
|
+
const filePath = this.extractFilePath(task);
|
|
216
|
+
if (filePath) {
|
|
217
|
+
bestIntent.filePath = filePath;
|
|
218
|
+
bestIntent.language = this.detectLanguage(filePath);
|
|
219
|
+
// Boost confidence if file exists
|
|
220
|
+
if (existsSync(filePath)) {
|
|
221
|
+
bestIntent.confidence = Math.min(1.0, bestIntent.confidence + 0.1);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return bestIntent;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Extract file path from task description
|
|
229
|
+
*/
|
|
230
|
+
extractFilePath(task) {
|
|
231
|
+
for (const pattern of FILE_PATH_PATTERNS) {
|
|
232
|
+
const match = task.match(pattern);
|
|
233
|
+
if (match && match[1]) {
|
|
234
|
+
return match[1];
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Detect language from file extension
|
|
241
|
+
*/
|
|
242
|
+
detectLanguage(filePath) {
|
|
243
|
+
const ext = extname(filePath).toLowerCase();
|
|
244
|
+
return LANGUAGE_MAP[ext] || 'javascript';
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Check if task contains Tier 3 (Opus) keywords
|
|
248
|
+
*/
|
|
249
|
+
containsTier3Keywords(task) {
|
|
250
|
+
let count = 0;
|
|
251
|
+
for (const pattern of TIER3_KEYWORDS) {
|
|
252
|
+
if (pattern.test(task)) {
|
|
253
|
+
count++;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return { matches: count > 0, count };
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Route a task to the optimal tier and handler
|
|
260
|
+
*/
|
|
261
|
+
async route(task, context) {
|
|
262
|
+
// Step 1: Try Agent Booster intent detection
|
|
263
|
+
if (this.config.agentBoosterEnabled) {
|
|
264
|
+
const intent = this.detectIntent(task);
|
|
265
|
+
if (intent && intent.confidence >= this.config.agentBoosterConfidenceThreshold) {
|
|
266
|
+
return {
|
|
267
|
+
tier: 1,
|
|
268
|
+
handler: 'agent-booster',
|
|
269
|
+
confidence: intent.confidence,
|
|
270
|
+
reasoning: `Agent Booster can handle "${intent.type}" with ${(intent.confidence * 100).toFixed(0)}% confidence`,
|
|
271
|
+
agentBoosterIntent: intent,
|
|
272
|
+
canSkipLLM: true,
|
|
273
|
+
estimatedLatencyMs: 1,
|
|
274
|
+
estimatedCost: 0,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// Step 2: Check for Tier 3 keywords (architecture, security, distributed)
|
|
279
|
+
const tier3Check = this.containsTier3Keywords(task);
|
|
280
|
+
if (tier3Check.matches && tier3Check.count >= 2) {
|
|
281
|
+
// Strong signal for Opus - multiple complex keywords
|
|
282
|
+
return {
|
|
283
|
+
tier: 3,
|
|
284
|
+
handler: 'opus',
|
|
285
|
+
model: 'opus',
|
|
286
|
+
confidence: Math.min(0.95, 0.7 + tier3Check.count * 0.1),
|
|
287
|
+
complexity: 0.8 + tier3Check.count * 0.05,
|
|
288
|
+
reasoning: `High complexity task (${tier3Check.count} architectural keywords) - using opus`,
|
|
289
|
+
canSkipLLM: false,
|
|
290
|
+
estimatedLatencyMs: 5000,
|
|
291
|
+
estimatedCost: 0.015,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
// Step 3: AST complexity analysis (if file path provided)
|
|
295
|
+
let astComplexity;
|
|
296
|
+
const targetFile = context?.filePath || this.extractFilePath(task);
|
|
297
|
+
if (targetFile && existsSync(targetFile)) {
|
|
298
|
+
try {
|
|
299
|
+
astComplexity = await this.analyzeASTComplexity(targetFile);
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
// AST analysis not available, continue with text-based routing
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
// Step 4: Text-based complexity + tiny-dancer routing
|
|
306
|
+
const tinyDancerResult = await this.tinyDancerRouter.route(task);
|
|
307
|
+
// Step 5: Combine AST complexity with tiny-dancer result
|
|
308
|
+
// Also boost if single tier3 keyword found
|
|
309
|
+
let finalComplexity = astComplexity !== undefined
|
|
310
|
+
? (astComplexity + tinyDancerResult.complexity) / 2
|
|
311
|
+
: tinyDancerResult.complexity;
|
|
312
|
+
// Boost complexity if tier3 keywords found (even just one)
|
|
313
|
+
if (tier3Check.matches) {
|
|
314
|
+
finalComplexity = Math.min(1.0, finalComplexity + 0.25);
|
|
315
|
+
}
|
|
316
|
+
// Step 6: Determine tier based on complexity
|
|
317
|
+
const { haiku, sonnet } = this.config.complexityThresholds;
|
|
318
|
+
if (finalComplexity < haiku) {
|
|
319
|
+
return {
|
|
320
|
+
tier: 2,
|
|
321
|
+
handler: 'haiku',
|
|
322
|
+
model: 'haiku',
|
|
323
|
+
confidence: tinyDancerResult.confidence,
|
|
324
|
+
complexity: finalComplexity,
|
|
325
|
+
reasoning: `Low complexity (${(finalComplexity * 100).toFixed(0)}%) - using haiku`,
|
|
326
|
+
canSkipLLM: false,
|
|
327
|
+
estimatedLatencyMs: 500,
|
|
328
|
+
estimatedCost: 0.0002,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
if (finalComplexity < sonnet) {
|
|
332
|
+
return {
|
|
333
|
+
tier: 2,
|
|
334
|
+
handler: 'sonnet',
|
|
335
|
+
model: 'sonnet',
|
|
336
|
+
confidence: tinyDancerResult.confidence,
|
|
337
|
+
complexity: finalComplexity,
|
|
338
|
+
reasoning: `Medium complexity (${(finalComplexity * 100).toFixed(0)}%) - using sonnet`,
|
|
339
|
+
canSkipLLM: false,
|
|
340
|
+
estimatedLatencyMs: 2000,
|
|
341
|
+
estimatedCost: 0.003,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
tier: 3,
|
|
346
|
+
handler: 'opus',
|
|
347
|
+
model: 'opus',
|
|
348
|
+
confidence: tinyDancerResult.confidence,
|
|
349
|
+
complexity: finalComplexity,
|
|
350
|
+
reasoning: `High complexity (${(finalComplexity * 100).toFixed(0)}%) - using opus`,
|
|
351
|
+
canSkipLLM: false,
|
|
352
|
+
estimatedLatencyMs: 5000,
|
|
353
|
+
estimatedCost: 0.015,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Analyze AST complexity of a file
|
|
358
|
+
* Returns normalized complexity score (0-1)
|
|
359
|
+
*/
|
|
360
|
+
async analyzeASTComplexity(filePath) {
|
|
361
|
+
try {
|
|
362
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
363
|
+
const lines = content.split('\n');
|
|
364
|
+
// Simple heuristics for complexity
|
|
365
|
+
let complexity = 0;
|
|
366
|
+
// Line count contribution
|
|
367
|
+
complexity += Math.min(0.3, lines.length / 1000);
|
|
368
|
+
// Nesting depth estimation (count indentation)
|
|
369
|
+
const avgIndent = lines
|
|
370
|
+
.filter((l) => l.trim().length > 0)
|
|
371
|
+
.map((l) => l.match(/^(\s*)/)?.[1].length || 0)
|
|
372
|
+
.reduce((sum, indent) => sum + indent, 0) / Math.max(1, lines.length);
|
|
373
|
+
complexity += Math.min(0.2, avgIndent / 20);
|
|
374
|
+
// Control flow complexity (count keywords)
|
|
375
|
+
const controlFlowCount = (content.match(/\b(if|else|for|while|switch|case|try|catch|async|await)\b/g) || []).length;
|
|
376
|
+
complexity += Math.min(0.3, controlFlowCount / 100);
|
|
377
|
+
// Function/class count
|
|
378
|
+
const functionCount = (content.match(/\b(function|class|=>)\b/g) || []).length;
|
|
379
|
+
complexity += Math.min(0.2, functionCount / 50);
|
|
380
|
+
return Math.min(1, complexity);
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
return 0.5; // Default to medium complexity on error
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Execute task using the appropriate tier
|
|
388
|
+
* Returns the result and routing information
|
|
389
|
+
*/
|
|
390
|
+
async execute(task, context) {
|
|
391
|
+
const routeResult = await this.route(task, context);
|
|
392
|
+
if (routeResult.tier === 1 && routeResult.agentBoosterIntent) {
|
|
393
|
+
// Try to execute with Agent Booster
|
|
394
|
+
const abResult = await this.tryAgentBooster(routeResult.agentBoosterIntent, context);
|
|
395
|
+
if (abResult.success) {
|
|
396
|
+
return {
|
|
397
|
+
result: { applied: true, confidence: abResult.confidence },
|
|
398
|
+
routeResult,
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
// Agent Booster failed, fall back to LLM
|
|
402
|
+
routeResult.tier = 2;
|
|
403
|
+
routeResult.handler = 'sonnet';
|
|
404
|
+
routeResult.model = 'sonnet';
|
|
405
|
+
routeResult.canSkipLLM = false;
|
|
406
|
+
routeResult.reasoning += ' (Agent Booster fallback to LLM)';
|
|
407
|
+
}
|
|
408
|
+
// Return routing result - caller handles LLM invocation
|
|
409
|
+
return { result: routeResult.reasoning, routeResult };
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Try to apply edit using Agent Booster
|
|
413
|
+
*/
|
|
414
|
+
async tryAgentBooster(intent, context) {
|
|
415
|
+
try {
|
|
416
|
+
// Try to import and use agentic-flow's agent-booster
|
|
417
|
+
const { execSync } = await import('child_process');
|
|
418
|
+
const filePath = intent.filePath || context?.filePath;
|
|
419
|
+
if (!filePath || !existsSync(filePath)) {
|
|
420
|
+
return { success: false, confidence: 0 };
|
|
421
|
+
}
|
|
422
|
+
const originalCode = context?.originalCode || readFileSync(filePath, 'utf-8');
|
|
423
|
+
// Build agent-booster command based on intent
|
|
424
|
+
const intentToInstruction = {
|
|
425
|
+
'var-to-const': 'Convert all var declarations to const',
|
|
426
|
+
'add-types': 'Add TypeScript type annotations',
|
|
427
|
+
'add-error-handling': 'Wrap in try/catch blocks',
|
|
428
|
+
'async-await': 'Convert to async/await',
|
|
429
|
+
'add-logging': 'Add console.log statements',
|
|
430
|
+
'remove-console': 'Remove all console.* statements',
|
|
431
|
+
};
|
|
432
|
+
const instruction = intentToInstruction[intent.type];
|
|
433
|
+
const language = intent.language || 'javascript';
|
|
434
|
+
// Try using npx agent-booster
|
|
435
|
+
const cmd = `npx --yes agent-booster@0.2.2 apply --language ${language}`;
|
|
436
|
+
const result = execSync(cmd, {
|
|
437
|
+
encoding: 'utf-8',
|
|
438
|
+
input: JSON.stringify({
|
|
439
|
+
code: originalCode,
|
|
440
|
+
edit: instruction,
|
|
441
|
+
}),
|
|
442
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
443
|
+
timeout: 5000,
|
|
444
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
445
|
+
});
|
|
446
|
+
const parsed = JSON.parse(result);
|
|
447
|
+
if (parsed.confidence >= this.config.agentBoosterConfidenceThreshold) {
|
|
448
|
+
return {
|
|
449
|
+
success: true,
|
|
450
|
+
confidence: parsed.confidence,
|
|
451
|
+
output: parsed.output,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
return { success: false, confidence: parsed.confidence || 0 };
|
|
455
|
+
}
|
|
456
|
+
catch {
|
|
457
|
+
// Agent Booster not available or failed
|
|
458
|
+
return { success: false, confidence: 0 };
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Get router statistics
|
|
463
|
+
*/
|
|
464
|
+
getStats() {
|
|
465
|
+
return {
|
|
466
|
+
config: { ...this.config },
|
|
467
|
+
tinyDancerStats: this.tinyDancerRouter.getStats(),
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
// ============================================================================
|
|
472
|
+
// Singleton & Factory Functions
|
|
473
|
+
// ============================================================================
|
|
474
|
+
let enhancedRouterInstance = null;
|
|
475
|
+
/**
|
|
476
|
+
* Get or create the singleton EnhancedModelRouter instance
|
|
477
|
+
*/
|
|
478
|
+
export function getEnhancedModelRouter(config) {
|
|
479
|
+
if (!enhancedRouterInstance) {
|
|
480
|
+
enhancedRouterInstance = new EnhancedModelRouter(config);
|
|
481
|
+
}
|
|
482
|
+
return enhancedRouterInstance;
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Reset the singleton instance
|
|
486
|
+
*/
|
|
487
|
+
export function resetEnhancedModelRouter() {
|
|
488
|
+
enhancedRouterInstance = null;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Create a new EnhancedModelRouter instance (non-singleton)
|
|
492
|
+
*/
|
|
493
|
+
export function createEnhancedModelRouter(config) {
|
|
494
|
+
return new EnhancedModelRouter(config);
|
|
495
|
+
}
|
|
496
|
+
// ============================================================================
|
|
497
|
+
// Convenience Functions
|
|
498
|
+
// ============================================================================
|
|
499
|
+
/**
|
|
500
|
+
* Quick route function with enhanced routing
|
|
501
|
+
*/
|
|
502
|
+
export async function enhancedRouteToModel(task, context) {
|
|
503
|
+
const router = getEnhancedModelRouter();
|
|
504
|
+
return router.route(task, context);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Detect if a task can be handled by Agent Booster
|
|
508
|
+
*/
|
|
509
|
+
export function canUseAgentBooster(task) {
|
|
510
|
+
const router = getEnhancedModelRouter();
|
|
511
|
+
const intent = router.detectIntent(task);
|
|
512
|
+
if (intent && intent.confidence >= 0.7) {
|
|
513
|
+
return { canUse: true, intent };
|
|
514
|
+
}
|
|
515
|
+
return { canUse: false };
|
|
516
|
+
}
|
|
517
|
+
//# sourceMappingURL=enhanced-model-router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enhanced-model-router.js","sourceRoot":"","sources":["../../../src/ruvector/enhanced-model-router.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,IAAI,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EAAe,cAAc,EAAmC,MAAM,mBAAmB,CAAC;AA4DjG,+EAA+E;AAC/E,4BAA4B;AAC5B,+EAA+E;AAE/E;;GAEG;AACH,MAAM,eAAe,GAIhB;IACH,cAAc,EAAE;QACd,QAAQ,EAAE;YACR,6BAA6B;YAC7B,4BAA4B;YAC5B,4CAA4C;YAC5C,+BAA+B;YAC/B,4BAA4B;YAC5B,mCAAmC;SACpC;QACD,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,uCAAuC;KACrD;IACD,WAAW,EAAE;QACX,QAAQ,EAAE;YACR,4BAA4B;YAC5B,4BAA4B;YAC5B,yBAAyB;YACzB,oBAAoB;YACpB,2BAA2B;SAC5B;QACD,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,iCAAiC;KAC/C;IACD,oBAAoB,EAAE;QACpB,QAAQ,EAAE;YACR,yBAAyB;YACzB,kC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C,MAAM,CAAC,CAAC;AACzC,CAAC;AAED,+EAA+E;AAC/E,wBAAwB;AACxB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,IAAY,EACZ,OAA+B;IAE/B,MAAM,MAAM,GAAG,sBAAsB,EAAE,CAAC;IACxC,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAI7C,MAAM,MAAM,GAAG,sBAAsB,EAAE,CAAC;IACxC,MAAM,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;IAEzC,IAAI,MAAM,IAAI,MAAM,CAAC,UAAU,IAAI,GAAG,EAAE,CAAC;QACvC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;IAClC,CAAC;IAED,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AAC3B,CAAC"}
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* Created with love by ruv.io
|
|
16
16
|
*/
|
|
17
17
|
export interface FlashAttentionConfig {
|
|
18
|
-
/** Block size for tiling (64
|
|
18
|
+
/** Block size for tiling (32-64 optimal for CPU L1 cache) */
|
|
19
19
|
blockSize: number;
|
|
20
20
|
/** Number of dimensions in embedding vectors */
|
|
21
21
|
dimensions: number;
|
|
@@ -23,6 +23,8 @@ export interface FlashAttentionConfig {
|
|
|
23
23
|
temperature: number;
|
|
24
24
|
/** Enable numerical stability optimizations */
|
|
25
25
|
useStableMode: boolean;
|
|
26
|
+
/** Use optimized CPU path (default: true) */
|
|
27
|
+
useCPUOptimizations: boolean;
|
|
26
28
|
}
|
|
27
29
|
export interface AttentionResult {
|
|
28
30
|
/** Output vectors after attention */
|
|
@@ -54,6 +56,9 @@ export declare class FlashAttention {
|
|
|
54
56
|
private config;
|
|
55
57
|
private lastSpeedup;
|
|
56
58
|
private benchmarkHistory;
|
|
59
|
+
private scoreBuffer;
|
|
60
|
+
private expBuffer;
|
|
61
|
+
private accumBuffer;
|
|
57
62
|
constructor(config?: Partial<FlashAttentionConfig>);
|
|
58
63
|
/**
|
|
59
64
|
* Main attention computation using Flash Attention algorithm
|
|
@@ -64,6 +69,34 @@ export declare class FlashAttention {
|
|
|
64
69
|
* @returns Attention output [N x D]
|
|
65
70
|
*/
|
|
66
71
|
attention(queries: Float32Array[], keys: Float32Array[], values: Float32Array[]): AttentionResult;
|
|
72
|
+
/**
|
|
73
|
+
* CPU-optimized attention with aggressive optimizations
|
|
74
|
+
*
|
|
75
|
+
* Key optimizations:
|
|
76
|
+
* - Blocked score computation (better cache utilization)
|
|
77
|
+
* - Top-K sparse attention (only use most relevant keys)
|
|
78
|
+
* - Pre-allocated buffers to avoid GC pressure
|
|
79
|
+
* - 8x loop unrolling for dot products
|
|
80
|
+
* - Fused max-finding during score computation
|
|
81
|
+
*/
|
|
82
|
+
private cpuOptimizedAttention;
|
|
83
|
+
/**
|
|
84
|
+
* Partial dot product using only first N dimensions (for screening)
|
|
85
|
+
*/
|
|
86
|
+
private partialDotProduct;
|
|
87
|
+
/**
|
|
88
|
+
* Partial sort to get top-K elements (QuickSelect-like)
|
|
89
|
+
* Only ensures first K elements are the largest, not sorted
|
|
90
|
+
*/
|
|
91
|
+
private partialSort;
|
|
92
|
+
/**
|
|
93
|
+
* Swap two indices in array
|
|
94
|
+
*/
|
|
95
|
+
private swapIndices;
|
|
96
|
+
/**
|
|
97
|
+
* Fast dot product with 8x unrolling
|
|
98
|
+
*/
|
|
99
|
+
private fastDotProduct;
|
|
67
100
|
/**
|
|
68
101
|
* Block-wise attention computation (Flash Attention core algorithm)
|
|
69
102
|
*
|
|
@@ -85,7 +118,7 @@ export declare class FlashAttention {
|
|
|
85
118
|
*/
|
|
86
119
|
getSpeedup(): number;
|
|
87
120
|
/**
|
|
88
|
-
* Run benchmark comparing naive vs
|
|
121
|
+
* Run benchmark comparing naive vs CPU-optimized attention
|
|
89
122
|
*
|
|
90
123
|
* @param numVectors - Number of vectors to test
|
|
91
124
|
* @param dimensions - Dimensions per vector
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"flash-attention.d.ts","sourceRoot":"","sources":["../../../src/ruvector/flash-attention.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAMH,MAAM,WAAW,oBAAoB;IACnC,
|
|
1
|
+
{"version":3,"file":"flash-attention.d.ts","sourceRoot":"","sources":["../../../src/ruvector/flash-attention.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAMH,MAAM,WAAW,oBAAoB;IACnC,6DAA6D;IAC7D,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,UAAU,EAAE,MAAM,CAAC;IACnB,sCAAsC;IACtC,WAAW,EAAE,MAAM,CAAC;IACpB,+CAA+C;IAC/C,aAAa,EAAE,OAAO,CAAC;IACvB,6CAA6C;IAC7C,mBAAmB,EAAE,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,eAAe;IAC9B,qCAAqC;IACrC,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,kDAAkD;IAClD,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;IACzB,uCAAuC;IACvC,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,2CAA2C;IAC3C,WAAW,EAAE,MAAM,CAAC;IACpB,qCAAqC;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,oCAAoC;IACpC,UAAU,EAAE,MAAM,CAAC;IACnB,4BAA4B;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,8CAA8C;IAC9C,gBAAgB,EAAE,MAAM,CAAC;IACzB,8CAA8C;IAC9C,gBAAgB,EAAE,MAAM,CAAC;IACzB,8BAA8B;IAC9B,eAAe,EAAE,MAAM,CAAC;CACz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