adaptive-memory-multi-model-router 2.2.6 → 2.2.8
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 +146 -105
- package/assets/benchmark-results.png +0 -0
- package/assets/complexity-scoring-v2.png +0 -0
- package/assets/complexity-scoring.png +0 -0
- package/assets/cost-comparison-chart.png +0 -0
- package/assets/cost-comparison-v2.png +0 -0
- package/assets/feature-comparison-v2.png +0 -0
- package/assets/feature-comparison-v3.png +0 -0
- package/assets/provider-health-chart.png +0 -0
- package/assets/provider-health-v2.png +0 -0
- package/assets/routing-flow-v2.png +0 -0
- package/assets/routing-flow-v3.png +0 -0
- package/assets/routing-flow.png +0 -0
- package/assets/tier-distribution.png +0 -0
- package/benchmark-results.json +620 -46
- package/dist/analytics/costAnalytics.d.ts +0 -1
- package/dist/cache/semanticCache.d.ts +0 -41
- package/dist/cache/semanticCache.d.ts.map +1 -1
- package/dist/cache/semanticCache.js +0 -142
- package/dist/cache/semanticCache.js.map +1 -1
- package/dist/cli.js +478 -35
- package/dist/cost/costTracker.js +3 -0
- package/dist/index.d.ts +0 -16
- package/dist/index.js +64 -264
- package/dist/integrations/langchainAdapter.d.ts +0 -1
- package/dist/integrations/oauth.d.ts +0 -1
- package/dist/memory/autoFetch.d.ts +0 -1
- package/dist/memory/memoryTree.d.ts +0 -1
- package/dist/memory/obsidianVault.d.ts +0 -1
- package/dist/providers/providerConfig.d.ts +0 -1
- package/dist/providers/providerConfig.js +0 -2
- package/dist/providers/registry.js +128 -126
- package/dist/routing/advancedRouter.js +427 -310
- package/dist/sdk.js +100 -109
- package/dist/security/guardrails.d.ts +0 -1
- package/dist/server/dashboard.d.ts +0 -1
- package/dist/server/modelMapper.d.ts +0 -1
- package/dist/server/proxyServer.d.ts +0 -1
- package/package.json +96 -389
- package/scripts/run-mmlu-benchmark.js +176 -0
- package/scripts/run-provider-benchmark.js +244 -0
- package/src/cache/semanticCache.ts +0 -148
- package/src/index.ts +99 -0
- package/test/provider-test.js +70 -91
- package/test.js +41 -67
- package/tsconfig.json +5 -15
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/sdk.d.ts.map +0 -1
- package/dist/sdk.js.map +0 -1
- package/test.js.bak +0 -376
|
@@ -1,336 +1,453 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
3
|
+
* A3M Router - Generic Adaptive Routing (RouteLLM Style)
|
|
4
|
+
*
|
|
5
|
+
* Routes queries to the best available LLM based on:
|
|
6
|
+
* - Query features (code, math, creative, etc.)
|
|
7
|
+
* - Provider availability (checks API keys)
|
|
8
|
+
* - Cost optimization
|
|
9
|
+
* - Quality vs speed tradeoff
|
|
10
|
+
*
|
|
11
|
+
* All provider references are dynamically loaded from providerConfig.
|
|
12
|
+
* Users can add/remove providers via environment variables or config files.
|
|
7
13
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.extractQueryFeatures = extractQueryFeatures;
|
|
11
|
-
exports.routeQuery = routeQuery;
|
|
12
|
-
exports.routeBatch = routeBatch;
|
|
13
|
-
exports.recommendForTask = recommendForTask;
|
|
14
|
-
exports.updateModelProfile = updateModelProfile;
|
|
14
|
+
|
|
15
|
+
const { getAvailableProviders } = require("../providers/providerConfig");
|
|
15
16
|
const tokenUtils_1 = require("../utils/tokenUtils");
|
|
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
|
-
strengths
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
17
|
+
|
|
18
|
+
// ============================================================
|
|
19
|
+
// DYNAMIC MODEL PROFILES (built from available providers)
|
|
20
|
+
// ============================================================
|
|
21
|
+
|
|
22
|
+
function buildModelProfiles() {
|
|
23
|
+
const profiles = {};
|
|
24
|
+
const available = getAvailableProviders();
|
|
25
|
+
|
|
26
|
+
for (const [providerId, provider] of Object.entries(available)) {
|
|
27
|
+
for (const model of provider.models) {
|
|
28
|
+
const modelKey = model.includes('/') ? model : providerId + '/' + model;
|
|
29
|
+
const costPerKInput = provider.costPerK ? provider.costPerK.input : 0;
|
|
30
|
+
const costPerKOutput = provider.costPerK ? provider.costPerK.output : 0;
|
|
31
|
+
|
|
32
|
+
// Assign strengths based on model characteristics
|
|
33
|
+
const strengths = [];
|
|
34
|
+
if (provider.type === 'cli') {
|
|
35
|
+
strengths.push('free', 'local');
|
|
36
|
+
}
|
|
37
|
+
if (costPerKInput < 0.3) {
|
|
38
|
+
strengths.push('budget', 'fast');
|
|
39
|
+
} else if (costPerKInput > 2) {
|
|
40
|
+
strengths.push('premium', 'reasoning');
|
|
41
|
+
}
|
|
42
|
+
if (provider.name === 'Mistral' || provider.name === 'Groq' || provider.name === 'Cerebras') {
|
|
43
|
+
strengths.push('fast', 'coding');
|
|
44
|
+
}
|
|
45
|
+
if (provider.name === 'CommandCode') {
|
|
46
|
+
strengths.push('code-aware', 'context-rich');
|
|
47
|
+
}
|
|
48
|
+
if (provider.name === 'OpenCode') {
|
|
49
|
+
strengths.push('free', 'multi-model');
|
|
50
|
+
}
|
|
51
|
+
if (provider.name === 'Google') {
|
|
52
|
+
strengths.push('multilingual', 'long-context');
|
|
53
|
+
}
|
|
54
|
+
if (provider.name === 'OpenAI') {
|
|
55
|
+
strengths.push('reasoning', 'coding', 'analysis');
|
|
56
|
+
}
|
|
57
|
+
if (provider.name === 'Anthropic') {
|
|
58
|
+
strengths.push('reasoning', 'creative', 'analysis');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
profiles[modelKey] = {
|
|
62
|
+
name: modelKey,
|
|
63
|
+
provider: providerId,
|
|
64
|
+
providerName: provider.name,
|
|
65
|
+
cost_per_1k_input: costPerKInput,
|
|
66
|
+
cost_per_1k_output: costPerKOutput,
|
|
67
|
+
latency_ms: provider.type === 'cli' ? 5000 : (provider.priority * 200 + 300),
|
|
68
|
+
quality_score: strengths.includes('premium') ? 0.95 :
|
|
69
|
+
strengths.includes('reasoning') ? 0.90 :
|
|
70
|
+
strengths.includes('fast') ? 0.82 : 0.80,
|
|
71
|
+
strengths,
|
|
72
|
+
context_window: provider.maxTokens || 8192,
|
|
73
|
+
type: provider.type,
|
|
74
|
+
priority: provider.priority,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return profiles;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let MODEL_PROFILES = buildModelProfiles();
|
|
83
|
+
|
|
84
|
+
// Refresh profiles when providers change
|
|
85
|
+
function refreshModelProfiles() {
|
|
86
|
+
MODEL_PROFILES = buildModelProfiles();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
exports.MODEL_PROFILES = MODEL_PROFILES;
|
|
90
|
+
|
|
91
|
+
// ============================================================
|
|
92
|
+
// FEATURE EXTRACTION (v3 — multi-signal complexity scorer)
|
|
93
|
+
// ============================================================
|
|
94
|
+
|
|
95
|
+
function extractQueryFeatures(prompt) {
|
|
96
|
+
const lower = prompt.toLowerCase();
|
|
97
|
+
const words = prompt.split(/\s+/);
|
|
98
|
+
const wordCount = words.length;
|
|
99
|
+
|
|
100
|
+
// === SIGNAL 1: Domain Detection ===
|
|
101
|
+
// Professional domains that indicate expert-level queries
|
|
102
|
+
const domainSignals = {
|
|
103
|
+
legal: {
|
|
104
|
+
keywords: ['legal', 'law', 'contract', 'liability', 'litigation', 'patent', 'copyright',
|
|
105
|
+
'regulation', 'compliance', 'constitutional', 'statute', 'jurisdiction',
|
|
106
|
+
'court', 'ruling', 'precedent', 'attorney', 'amicus', 'sec ', 'fda ',
|
|
107
|
+
'gdpr', 'ccpa', 'cfpr', 'due diligence', 'merger', 'acquisition',
|
|
108
|
+
'10-k', 'sec filing', 'forensic', 'embezzlement', 'infringement'],
|
|
109
|
+
weight: 0.35
|
|
57
110
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
quality_score: 0.88,
|
|
65
|
-
strengths: ["fast", "multilingual"],
|
|
66
|
-
context_window: 1000000
|
|
111
|
+
medical: {
|
|
112
|
+
keywords: ['clinical', 'medical', 'pharmaceutical', 'oncology', 'drug', 'trial protocol',
|
|
113
|
+
'diagnosis', 'treatment', 'epidemiolog', 'genome', 'cohort study',
|
|
114
|
+
'biomarker', 'efficacy', 'pharmacoeconomic', 'biologic', 'vaccine',
|
|
115
|
+
'sepsis', 'ehr ', 'surgical', 'patient safety', 'fda approval'],
|
|
116
|
+
weight: 0.35
|
|
67
117
|
},
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
strengths: ["reasoning", "long-context"],
|
|
76
|
-
context_window: 2000000
|
|
118
|
+
finance: {
|
|
119
|
+
keywords: ['financial model', 'valuation', 'revenue', 'portfolio', 'derivative',
|
|
120
|
+
'hedge fund', 'series a', 'series b', 'startup valuation', 'sensitivity analysis',
|
|
121
|
+
'investment thesis', 'earnings', 'tax optimization', 'forensic accounting',
|
|
122
|
+
'multinational', 'jurisdiction', 'risk assessment', 'monte carlo',
|
|
123
|
+
'black-scholes', 'options pricing', 'credit risk'],
|
|
124
|
+
weight: 0.30
|
|
77
125
|
},
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
strengths: ["fast", "coding"],
|
|
86
|
-
context_window: 128000
|
|
126
|
+
security: {
|
|
127
|
+
keywords: ['security audit', 'penetration', 'vulnerability', 'exploit', 'zero-trust',
|
|
128
|
+
'threat model', 'incident response', 'malware', 'ransomware',
|
|
129
|
+
'authentication flow', 'cryptograph', 'encryption', 'timing attack',
|
|
130
|
+
'supply chain attack', 'owasp', 'compliance', 'risk assessment',
|
|
131
|
+
'mfa', 'zero-day', 'firewall', 'intrusion'],
|
|
132
|
+
weight: 0.30
|
|
87
133
|
},
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
strengths: ["fast", "budget"],
|
|
96
|
-
context_window: 128000
|
|
134
|
+
architecture: {
|
|
135
|
+
keywords: ['system design', 'microservice', 'distributed system', 'fault-tolerant',
|
|
136
|
+
'event-sourced', 'cqrs', 'consensus algorithm', 'real-time pipeline',
|
|
137
|
+
'high availability', 'multi-region', 'latency sla', 'kafka',
|
|
138
|
+
'event-driven', 'data warehouse', 'etl', 'streaming', '1m events',
|
|
139
|
+
'million events', 'scalab', 'infrastruct', 'deploy'],
|
|
140
|
+
weight: 0.25
|
|
97
141
|
},
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
strengths: ["privacy", "free"],
|
|
106
|
-
context_window: 128000
|
|
142
|
+
ml_research: {
|
|
143
|
+
keywords: ['neural network', 'transformer', 'backpropagation', 'gradient',
|
|
144
|
+
'reinforcement learning', 'rlhf', 'fine-tun', 'bert ', 'gpt ',
|
|
145
|
+
'attention mechanism', 'training pipeline', 'model monitoring',
|
|
146
|
+
'data drift', 'feature engine', 'deep learn', 'benchmark',
|
|
147
|
+
'ablation', 'sota', 'state of the art', 'paper', 'arxiv'],
|
|
148
|
+
weight: 0.25
|
|
107
149
|
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const
|
|
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
|
-
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
let domainScore = 0;
|
|
153
|
+
let detectedDomain = '';
|
|
154
|
+
for (const [domain, config] of Object.entries(domainSignals)) {
|
|
155
|
+
const matchCount = config.keywords.filter(kw => lower.includes(kw)).length;
|
|
156
|
+
if (matchCount > 0) {
|
|
157
|
+
const score = config.weight * Math.min(matchCount / 2, 1.5); // cap at 1.5x
|
|
158
|
+
if (score > domainScore) {
|
|
159
|
+
domainScore = score;
|
|
160
|
+
detectedDomain = domain;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// === SIGNAL 2: Task Complexity Indicators ===
|
|
166
|
+
const has_code = /function|class |def |import |const |let |python|javascript|typescript|java |cpp|rust|```|=>|->|async|await|sql|css|html|react|node|express|docker|kubernetes/i.test(prompt);
|
|
167
|
+
const has_math = /equation|formula|calculate|sqrt|\^|log|sin|cos|integral|derivative|math|∫|∂|∑|∏|√|∞|π|compute|theorem|proof|complexity|algorithm/i.test(prompt);
|
|
168
|
+
const requires_reasoning = /analyze|compare|contrast|evaluate|assess|implications|impact|consequence|why|because|therefore|reason|logic|argue|debate|critique|synthesize/i.test(prompt);
|
|
169
|
+
const is_creative = /write a|story|poem|creative|imagine|narrative|joke|compose|fiction/i.test(lower);
|
|
170
|
+
const is_translation = /translate|translation|in french|in spanish|in japanese|in chinese/i.test(lower);
|
|
171
|
+
const is_multilingual = /[\u4e00-\u9fff]|[\u3040-\u309f\u30a0-\u30ff]|[\uac00-\ud7af]|[а-яА-Я]/.test(prompt);
|
|
172
|
+
|
|
173
|
+
// === SIGNAL 3: Query Structure ===
|
|
174
|
+
// Longer, more structured queries = more complex
|
|
175
|
+
const avgWordLength = words.reduce((sum, w) => sum + w.length, 0) / Math.max(wordCount, 1);
|
|
176
|
+
const hasMultipleClauses = (prompt.match(/[,;:]/g) || []).length >= 2;
|
|
177
|
+
const hasQualifiers = /detailed|comprehensive|thorough|in-depth|extensive|step-by-step|systematic|formal|rigorous/i.test(prompt);
|
|
178
|
+
|
|
179
|
+
// === SIGNAL 4: Action Verb Intensity ===
|
|
180
|
+
// Expert verbs indicate higher cognitive demands
|
|
181
|
+
const expertVerbs = /design|architect|review|audit|investigate|diagnose|optimize|strategize|formulate|derive|prove|verify|validate/i;
|
|
182
|
+
const midVerbs = /analyze|evaluate|compare|assess|implement|create|build|develop|construct|derive|explain/i;
|
|
183
|
+
const simpleVerbs = /what is|who|when|where|how many|define|list|name|convert|translate|summarize briefly/i;
|
|
184
|
+
|
|
185
|
+
let verbScore = 0;
|
|
186
|
+
if (expertVerbs.test(lower)) verbScore = 0.20;
|
|
187
|
+
else if (midVerbs.test(lower)) verbScore = 0.10;
|
|
188
|
+
if (simpleVerbs.test(lower)) verbScore = -0.10; // deboost simple questions
|
|
189
|
+
|
|
190
|
+
// === SIGNAL 5: Specificity ===
|
|
191
|
+
// Specific details = more complex
|
|
192
|
+
const hasSpecifics = /\d+%|\$\d+|million|billion|specific|particular|given|according to|based on/i.test(prompt);
|
|
193
|
+
const hasMultiStep = /and then|first.*then|after that|next|finally|additionally|furthermore|moreover/i.test(prompt);
|
|
194
|
+
|
|
195
|
+
// === COMPLEXITY SCORING (weighted multi-signal) ===
|
|
196
|
+
let complexity = 0.15; // Base: simple query
|
|
197
|
+
|
|
198
|
+
// Domain signal (strongest predictor)
|
|
199
|
+
complexity += domainScore;
|
|
200
|
+
|
|
201
|
+
// Length signal (longer = harder, but diminishing)
|
|
202
|
+
if (wordCount > 5) complexity += 0.03;
|
|
203
|
+
if (wordCount > 10) complexity += 0.05;
|
|
204
|
+
if (wordCount > 15) complexity += 0.05;
|
|
205
|
+
if (wordCount > 20) complexity += 0.03;
|
|
206
|
+
|
|
207
|
+
// Feature signals
|
|
208
|
+
if (has_code) complexity += 0.10;
|
|
209
|
+
if (has_math) complexity += 0.12;
|
|
210
|
+
if (requires_reasoning) complexity += 0.08;
|
|
211
|
+
if (is_creative) complexity += 0.05;
|
|
212
|
+
if (is_translation) complexity += 0.02;
|
|
213
|
+
|
|
214
|
+
// Structure signals
|
|
215
|
+
if (hasQualifiers) complexity += 0.08;
|
|
216
|
+
if (hasMultipleClauses) complexity += 0.05;
|
|
217
|
+
if (hasSpecifics) complexity += 0.05;
|
|
218
|
+
if (hasMultiStep) complexity += 0.05;
|
|
219
|
+
|
|
220
|
+
// Verb intensity
|
|
221
|
+
complexity += verbScore;
|
|
222
|
+
|
|
223
|
+
// Long words = technical language
|
|
224
|
+
if (avgWordLength > 6) complexity += 0.05;
|
|
225
|
+
if (avgWordLength > 8) complexity += 0.05;
|
|
226
|
+
|
|
227
|
+
complexity = Math.max(0.10, Math.min(1.0, complexity));
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
complexity,
|
|
231
|
+
length: wordCount,
|
|
232
|
+
has_code,
|
|
233
|
+
has_math,
|
|
234
|
+
is_multilingual,
|
|
235
|
+
is_translation,
|
|
236
|
+
is_creative,
|
|
237
|
+
requires_reasoning,
|
|
238
|
+
is_security: /security|vulnerability|inject|exploit|attack|encryption|auth/i.test(lower),
|
|
239
|
+
is_devops: /ci\/cd|docker|kubernetes|k8s|deploy|pipeline|github action|terraform/i.test(lower),
|
|
240
|
+
is_data: /dataset|pandas|numpy|training|model|neural|transformer|bert|llm/i.test(lower),
|
|
241
|
+
detected_domain: detectedDomain,
|
|
242
|
+
domain_score: domainScore,
|
|
243
|
+
};
|
|
173
244
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
245
|
+
|
|
246
|
+
exports.extractQueryFeatures = extractQueryFeatures;
|
|
247
|
+
|
|
248
|
+
// ============================================================
|
|
249
|
+
// SCORING FUNCTIONS
|
|
250
|
+
// ============================================================
|
|
251
|
+
|
|
177
252
|
function scoreModelFit(model, features) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
if (features.complexity < 0.4 && model.latency_ms < 1000) {
|
|
197
|
-
score += 0.1;
|
|
198
|
-
}
|
|
199
|
-
return score;
|
|
253
|
+
let score = model.quality_score * 0.4; // Base quality
|
|
254
|
+
|
|
255
|
+
if (features.has_code && model.strengths.includes("coding")) score += 0.2;
|
|
256
|
+
if (features.requires_reasoning && model.strengths.includes("reasoning")) score += 0.2;
|
|
257
|
+
if (features.is_creative && model.strengths.includes("creative")) score += 0.15;
|
|
258
|
+
if (features.is_multilingual && model.strengths.includes("multilingual")) score += 0.15;
|
|
259
|
+
if (features.has_math && model.strengths.includes("analysis")) score += 0.15;
|
|
260
|
+
if (features.is_security && model.strengths.includes("reasoning")) score += 0.1;
|
|
261
|
+
if (features.is_data && model.strengths.includes("analysis")) score += 0.1;
|
|
262
|
+
|
|
263
|
+
// Free/local providers bonus for simple tasks
|
|
264
|
+
if (features.complexity < 0.4 && model.strengths.includes("free")) score += 0.15;
|
|
265
|
+
if (features.complexity < 0.4 && model.latency_ms < 1000) score += 0.1;
|
|
266
|
+
|
|
267
|
+
// Code-aware providers for code tasks
|
|
268
|
+
if (features.has_code && model.strengths.includes("code-aware")) score += 0.2;
|
|
269
|
+
|
|
270
|
+
return score;
|
|
200
271
|
}
|
|
201
|
-
|
|
202
|
-
* Cost efficiency score (inverse of normalized cost)
|
|
203
|
-
*/
|
|
272
|
+
|
|
204
273
|
function costEfficiency(model, features) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
// For complex tasks, deprioritize cost
|
|
211
|
-
return (1 - Math.min(avg_cost / 10, 1)) * 0.2;
|
|
274
|
+
const avg_cost = (model.cost_per_1k_input + model.cost_per_1k_output) / 2;
|
|
275
|
+
if (features.complexity < 0.5) {
|
|
276
|
+
return (1 - Math.min(avg_cost / 10, 1)) * 0.6;
|
|
277
|
+
}
|
|
278
|
+
return (1 - Math.min(avg_cost / 10, 1)) * 0.2;
|
|
212
279
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
280
|
+
|
|
281
|
+
// ============================================================
|
|
282
|
+
// ROUTING
|
|
283
|
+
// ============================================================
|
|
284
|
+
|
|
216
285
|
function routeQuery(prompt, available_models, budget_multiplier = 1.0) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
286
|
+
// Refresh profiles to ensure we have latest provider config
|
|
287
|
+
refreshModelProfiles();
|
|
288
|
+
|
|
289
|
+
const features = extractQueryFeatures(prompt);
|
|
290
|
+
const candidate_names = available_models || Object.keys(MODEL_PROFILES);
|
|
291
|
+
|
|
292
|
+
// Filter to available models
|
|
293
|
+
const candidates = candidate_names
|
|
294
|
+
.filter(name => MODEL_PROFILES[name])
|
|
295
|
+
.map(name => {
|
|
296
|
+
const profile = MODEL_PROFILES[name];
|
|
297
|
+
const quality = scoreModelFit(profile, features);
|
|
298
|
+
const cost = costEfficiency(profile, features);
|
|
299
|
+
return {
|
|
300
|
+
name,
|
|
301
|
+
profile,
|
|
302
|
+
quality_score: quality,
|
|
303
|
+
cost_score: cost,
|
|
304
|
+
total_score: quality + cost
|
|
305
|
+
};
|
|
232
306
|
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
candidates.sort((a, b) => {
|
|
236
|
-
const score_a = a.quality_score * complexity_bias + a.cost_score * (1 - complexity_bias);
|
|
237
|
-
const score_b = b.quality_score * complexity_bias + b.cost_score * (1 - complexity_bias);
|
|
238
|
-
return score_b - score_a;
|
|
239
|
-
});
|
|
240
|
-
const primary = candidates[0];
|
|
241
|
-
const secondary = candidates.slice(1, 3);
|
|
242
|
-
// Calculate confidence based on score gap
|
|
243
|
-
let confidence = 0.5;
|
|
244
|
-
if (candidates.length > 1) {
|
|
245
|
-
const gap = primary.total_score - candidates[1].total_score;
|
|
246
|
-
confidence = Math.min(0.95, 0.5 + gap * 2);
|
|
247
|
-
}
|
|
248
|
-
// Build reasoning
|
|
249
|
-
const reasons = [];
|
|
250
|
-
if (features.has_code)
|
|
251
|
-
reasons.push("code detected");
|
|
252
|
-
if (features.requires_reasoning)
|
|
253
|
-
reasons.push("reasoning needed");
|
|
254
|
-
if (features.complexity > 0.6)
|
|
255
|
-
reasons.push("high complexity");
|
|
256
|
-
if (features.is_multilingual)
|
|
257
|
-
reasons.push("multilingual");
|
|
258
|
-
const estimated_tokens = features.length * 1.5; // rough completion estimate
|
|
259
|
-
const estimated_cost = (0, tokenUtils_1.estimateCost)(features.length, estimated_tokens, primary.name);
|
|
307
|
+
|
|
308
|
+
if (candidates.length === 0) {
|
|
260
309
|
return {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
310
|
+
primary_model: null,
|
|
311
|
+
fallback_models: [],
|
|
312
|
+
confidence: 0,
|
|
313
|
+
reasoning: "No providers available - configure API keys in ~/.config/a3m-router/providers.json",
|
|
314
|
+
estimated_cost: 0,
|
|
315
|
+
estimated_latency_ms: 0,
|
|
267
316
|
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Sort by total score (quality vs cost tradeoff based on complexity)
|
|
320
|
+
const complexity_bias = features.complexity > 0.6 ? 0.7 : 0.3;
|
|
321
|
+
candidates.sort((a, b) => {
|
|
322
|
+
const score_a = a.quality_score * complexity_bias + a.cost_score * (1 - complexity_bias);
|
|
323
|
+
const score_b = b.quality_score * complexity_bias + b.cost_score * (1 - complexity_bias);
|
|
324
|
+
return score_b - score_a;
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const primary = candidates[0];
|
|
328
|
+
const secondary = candidates.slice(1, 3);
|
|
329
|
+
|
|
330
|
+
// Calculate confidence based on score gap
|
|
331
|
+
let confidence = 0.5;
|
|
332
|
+
if (candidates.length > 1) {
|
|
333
|
+
const gap = primary.total_score - candidates[1].total_score;
|
|
334
|
+
confidence = Math.min(0.95, 0.5 + gap * 2);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Build reasoning
|
|
338
|
+
const reasons = [];
|
|
339
|
+
if (features.has_code) reasons.push("code detected");
|
|
340
|
+
if (features.requires_reasoning) reasons.push("reasoning needed");
|
|
341
|
+
if (features.complexity > 0.6) reasons.push("high complexity");
|
|
342
|
+
if (features.is_multilingual) reasons.push("multilingual");
|
|
343
|
+
if (features.is_translation) reasons.push("translation");
|
|
344
|
+
if (primary.profile.strengths.includes("free")) reasons.push("free tier");
|
|
345
|
+
|
|
346
|
+
const estimated_tokens = features.length * 1.5;
|
|
347
|
+
const estimated_cost = tokenUtils_1.estimateCost(features.length, estimated_tokens, primary.name);
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
primary_model: primary.name,
|
|
351
|
+
fallback_models: secondary.map(c => c.name),
|
|
352
|
+
confidence,
|
|
353
|
+
reasoning: `Selected ${primary.profile.providerName || primary.profile.provider}/${primary.name} for ${reasons.join(", ") || "general query"}`,
|
|
354
|
+
estimated_cost: estimated_cost * budget_multiplier,
|
|
355
|
+
estimated_latency_ms: primary.profile.latency_ms,
|
|
356
|
+
features,
|
|
357
|
+
provider_type: primary.profile.type,
|
|
358
|
+
};
|
|
268
359
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
360
|
+
|
|
361
|
+
exports.routeQuery = routeQuery;
|
|
362
|
+
|
|
363
|
+
// ============================================================
|
|
364
|
+
// BATCH ROUTING
|
|
365
|
+
// ============================================================
|
|
366
|
+
|
|
367
|
+
function routeBatch(prompts, options = {}) {
|
|
368
|
+
const decisions = prompts.map(p => routeQuery(p));
|
|
369
|
+
|
|
370
|
+
if (options.same_model && decisions.length > 0) {
|
|
371
|
+
const primary_model = decisions[0].primary_model;
|
|
372
|
+
decisions.forEach(d => {
|
|
373
|
+
d.primary_model = primary_model;
|
|
374
|
+
d.fallback_models = decisions[0].fallback_models;
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (options.max_cost_per_prompt) {
|
|
379
|
+
decisions.forEach(d => {
|
|
380
|
+
if (d.estimated_cost > options.max_cost_per_prompt) {
|
|
381
|
+
const cheap = Object.entries(MODEL_PROFILES)
|
|
382
|
+
.find(([name, p]) => p.cost_per_1k_input < 0.5);
|
|
383
|
+
if (cheap) {
|
|
384
|
+
d.primary_model = cheap[0];
|
|
385
|
+
d.reasoning = `Budget-limited routing to ${cheap[1].providerName || cheap[1].provider}`;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return decisions;
|
|
296
392
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
393
|
+
|
|
394
|
+
exports.routeBatch = routeBatch;
|
|
395
|
+
|
|
396
|
+
// ============================================================
|
|
397
|
+
// TASK RECOMMENDATIONS
|
|
398
|
+
// ============================================================
|
|
399
|
+
|
|
300
400
|
function recommendForTask(task) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
401
|
+
refreshModelProfiles();
|
|
402
|
+
const features = extractQueryFeatures(task);
|
|
403
|
+
const decision = routeQuery(task);
|
|
404
|
+
return {
|
|
405
|
+
primary: decision.primary_model,
|
|
406
|
+
fallbacks: decision.fallback_models,
|
|
407
|
+
reason: decision.reasoning,
|
|
408
|
+
features,
|
|
409
|
+
};
|
|
309
410
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
411
|
+
|
|
412
|
+
exports.recommendForTask = recommendForTask;
|
|
413
|
+
|
|
414
|
+
// ============================================================
|
|
415
|
+
// ONLINE LEARNING - Update model profiles from feedback
|
|
416
|
+
// ============================================================
|
|
417
|
+
|
|
418
|
+
function updateModelProfile(model_name, actual_latency_ms, actual_cost, quality_rating) {
|
|
419
|
+
refreshModelProfiles();
|
|
420
|
+
const profile = MODEL_PROFILES[model_name];
|
|
421
|
+
if (!profile) return;
|
|
422
|
+
|
|
423
|
+
const alpha = 0.2; // Learning rate
|
|
424
|
+
profile.latency_ms = profile.latency_ms * (1 - alpha) + actual_latency_ms * alpha;
|
|
425
|
+
profile.quality_score = profile.quality_score * (1 - alpha) + quality_rating * alpha;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
exports.updateModelProfile = updateModelProfile;
|
|
429
|
+
|
|
430
|
+
// ============================================================
|
|
431
|
+
// PROVIDER HEALTH CHECK
|
|
432
|
+
// ============================================================
|
|
433
|
+
|
|
434
|
+
async function getProviderHealth() {
|
|
435
|
+
const { checkAllProviders } = require("../providers/providerConfig");
|
|
436
|
+
return checkAllProviders();
|
|
327
437
|
}
|
|
438
|
+
|
|
439
|
+
exports.getProviderHealth = getProviderHealth;
|
|
440
|
+
|
|
441
|
+
// ============================================================
|
|
442
|
+
// Default export
|
|
443
|
+
// ============================================================
|
|
444
|
+
|
|
328
445
|
exports.default = {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
446
|
+
extractQueryFeatures,
|
|
447
|
+
routeQuery,
|
|
448
|
+
routeBatch,
|
|
449
|
+
recommendForTask,
|
|
450
|
+
updateModelProfile,
|
|
451
|
+
getProviderHealth,
|
|
452
|
+
MODEL_PROFILES,
|
|
335
453
|
};
|
|
336
|
-
//# sourceMappingURL=advancedRouter.js.map
|