adaptive-memory-multi-model-router 1.9.0 → 1.9.2

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,332 +1,390 @@
1
1
  "use strict";
2
2
  /**
3
- * TMLPD Advanced Routing - RouteLLM Style
4
- *
5
- * Learned routing based on arXiv:2404.06035 (RouteLLM)
6
- * Balances cost-quality tradeoff with confidence-based model selection
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
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.MODEL_PROFILES = void 0;
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
- // Pre-configured model profiles
17
- exports.MODEL_PROFILES = {
18
- "openai/gpt-4o": {
19
- name: "openai/gpt-4o",
20
- provider: "openai",
21
- cost_per_1k_input: 2.50,
22
- cost_per_1k_output: 10.00,
23
- latency_ms: 2000,
24
- quality_score: 0.95,
25
- strengths: ["reasoning", "coding", "analysis"],
26
- context_window: 128000
27
- },
28
- "openai/gpt-4o-mini": {
29
- name: "openai/gpt-4o-mini",
30
- provider: "openai",
31
- cost_per_1k_input: 0.15,
32
- cost_per_1k_output: 0.60,
33
- latency_ms: 500,
34
- quality_score: 0.85,
35
- strengths: ["fast", "coding"],
36
- context_window: 128000
37
- },
38
- "anthropic/claude-3.5-sonnet": {
39
- name: "anthropic/claude-3.5-sonnet",
40
- provider: "anthropic",
41
- cost_per_1k_input: 3.00,
42
- cost_per_1k_output: 15.00,
43
- latency_ms: 2500,
44
- quality_score: 0.96,
45
- strengths: ["reasoning", "creative", "analysis"],
46
- context_window: 200000
47
- },
48
- "anthropic/claude-3-haiku": {
49
- name: "anthropic/claude-3-haiku",
50
- provider: "anthropic",
51
- cost_per_1k_input: 0.25,
52
- cost_per_1k_output: 1.25,
53
- latency_ms: 500,
54
- quality_score: 0.80,
55
- strengths: ["fast", "simple"],
56
- context_window: 200000
57
- },
58
- "google/gemini-2.0-flash": {
59
- name: "google/gemini-2.0-flash",
60
- provider: "google",
61
- cost_per_1k_input: 0.00, // Free
62
- cost_per_1k_output: 0.00,
63
- latency_ms: 800,
64
- quality_score: 0.88,
65
- strengths: ["fast", "multilingual"],
66
- context_window: 1000000
67
- },
68
- "google/gemini-1.5-pro": {
69
- name: "google/gemini-1.5-pro",
70
- provider: "google",
71
- cost_per_1k_input: 1.25,
72
- cost_per_1k_output: 5.00,
73
- latency_ms: 1500,
74
- quality_score: 0.92,
75
- strengths: ["reasoning", "long-context"],
76
- context_window: 2000000
77
- },
78
- "groq/llama-3.3-70b": {
79
- name: "groq/llama-3.3-70b",
80
- provider: "groq",
81
- cost_per_1k_input: 0.59,
82
- cost_per_1k_output: 0.79,
83
- latency_ms: 400,
84
- quality_score: 0.82,
85
- strengths: ["fast", "coding"],
86
- context_window: 128000
87
- },
88
- "cerebras/llama-3.3-70b": {
89
- name: "cerebras/llama-3.3-70b",
90
- provider: "cerebras",
91
- cost_per_1k_input: 0.60,
92
- cost_per_1k_output: 0.60,
93
- latency_ms: 350,
94
- quality_score: 0.82,
95
- strengths: ["fast", "budget"],
96
- context_window: 128000
97
- },
98
- "local/llama-3.3-70b": {
99
- name: "local/llama-3.3-70b",
100
- provider: "ollama",
101
- cost_per_1k_input: 0.00,
102
- cost_per_1k_output: 0.00,
103
- latency_ms: 100,
104
- quality_score: 0.75,
105
- strengths: ["privacy", "free"],
106
- context_window: 128000
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
+ };
107
76
  }
108
- };
109
- /**
110
- * Extract features from prompt for routing decision
111
- */
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
93
+ // ============================================================
94
+
112
95
  function extractQueryFeatures(prompt) {
113
- const lower = prompt.toLowerCase();
114
- // Code patterns
115
- const code_indicators = [
116
- "function", "class ", "def ", "import ", "const ", "let ",
117
- "python", "javascript", "typescript", "java", "cpp", "rust",
118
- "```", "=>", "->", "async", "await"
119
- ];
120
- const has_code = code_indicators.some(pattern => lower.includes(pattern));
121
- // Math patterns (expanded for unicode and common notation)
122
- const math_indicators = [
123
- "equation", "formula", "calculate", "sqrt", "^", "log",
124
- "sin", "cos", "tan", "integral", "derivative", "$", "math",
125
- "", "", "", "", "", "", "π", "θ", "β",
126
- "dx", "dy", "dz", "=", "solver", "compute"
127
- ];
128
- const has_math = math_indicators.some(pattern => prompt.includes(pattern));
129
- // Multilingual
130
- const lang_patterns = [
131
- /[\u4e00-\u9fff]/, // Chinese
132
- /[\u3040-\u309f\u30a0-\u30ff]/, // Japanese
133
- /[\uac00-\ud7af]/, // Korean
134
- /[а-яА-Я]/, // Russian
135
- /[áéíóúñ]/ // Spanish accented
136
- ];
137
- const is_multilingual = lang_patterns.some(pattern => pattern.test(prompt));
138
- // Creative writing
139
- const creative_indicators = [
140
- "write a", "story", "poem", "creative", "imagine",
141
- "describe", "explain in", "tell me", "narrative"
142
- ];
143
- const is_creative = creative_indicators.some(pattern => lower.includes(pattern));
144
- // Reasoning
145
- const reasoning_indicators = [
146
- "explain", "why", "because", "therefore", "thus",
147
- "analyze", "think", "consider", "reason", "logic"
148
- ];
149
- const requires_reasoning = reasoning_indicators.some(pattern => lower.includes(pattern));
150
- // Complexity estimation based on length and patterns
151
- const tokens = (0, tokenUtils_1.countTokens)(prompt, "gpt-4o");
152
- let complexity = 0.3;
153
- if (tokens > 1000)
154
- complexity += 0.2;
155
- if (has_code)
156
- complexity += 0.15;
157
- if (has_math)
158
- complexity += 0.2;
159
- if (requires_reasoning)
160
- complexity += 0.15;
161
- if (is_creative)
162
- complexity += 0.1;
163
- complexity = Math.min(1.0, complexity);
164
- return {
165
- complexity,
166
- length: tokens,
167
- has_code,
168
- has_math,
169
- is_multilingual,
170
- is_creative,
171
- requires_reasoning
172
- };
96
+ const lower = prompt.toLowerCase();
97
+
98
+ // Code patterns
99
+ const code_indicators = [
100
+ "function", "class ", "def ", "import ", "const ", "let ",
101
+ "python", "javascript", "typescript", "java", "cpp", "rust",
102
+ "```", "=>", "->", "async", "await"
103
+ ];
104
+ const has_code = code_indicators.some(pattern => lower.includes(pattern));
105
+
106
+ // Math patterns
107
+ const math_indicators = [
108
+ "equation", "formula", "calculate", "sqrt", "^", "log",
109
+ "sin", "cos", "tan", "integral", "derivative", "$", "math",
110
+ "∫", "∂", "∑", "∏", "√", "∞", "π", "θ", "β",
111
+ "dx", "dy", "dz", "=", "solver", "compute"
112
+ ];
113
+ const has_math = math_indicators.some(pattern => prompt.includes(pattern));
114
+
115
+ // Multilingual
116
+ const lang_patterns = [
117
+ /[\u4e00-\u9fff]/, // Chinese
118
+ /[\u3040-\u309f\u30a0-\u30ff]/, // Japanese
119
+ /[\uac00-\ud7af]/, // Korean
120
+ /[а-яА-Я]/, // Russian
121
+ /[áéíóúñ]/ // Spanish accented
122
+ ];
123
+ const is_multilingual = lang_patterns.some(pattern => pattern.test(prompt));
124
+
125
+ // Translation detection
126
+ const translation_indicators = ["translate", "translation", "translate to", "in french", "in spanish", "in japanese"];
127
+ const is_translation = translation_indicators.some(pattern => lower.includes(pattern));
128
+
129
+ // Creative writing
130
+ const creative_indicators = [
131
+ "write a", "story", "poem", "creative", "imagine",
132
+ "describe", "explain in", "tell me", "narrative", "joke"
133
+ ];
134
+ const is_creative = creative_indicators.some(pattern => lower.includes(pattern));
135
+
136
+ // Reasoning
137
+ const reasoning_indicators = [
138
+ "explain", "why", "because", "therefore", "thus",
139
+ "analyze", "think", "consider", "reason", "logic"
140
+ ];
141
+ const requires_reasoning = reasoning_indicators.some(pattern => lower.includes(pattern));
142
+
143
+ // Security/specialized
144
+ const security_indicators = ["security", "vulnerability", "inject", "exploit", "attack", "encryption", "auth"];
145
+ const is_security = security_indicators.some(pattern => lower.includes(pattern));
146
+
147
+ // DevOps
148
+ const devops_indicators = ["ci/cd", "docker", "kubernetes", "k8s", "deploy", "pipeline", "github action", "terraform"];
149
+ const is_devops = devops_indicators.some(pattern => lower.includes(pattern));
150
+
151
+ // Data/ML
152
+ const data_indicators = ["dataset", "pandas", "numpy", "training", "model", "neural", "transformer", "bert", "llm"];
153
+ const is_data = data_indicators.some(pattern => lower.includes(pattern));
154
+
155
+ // Complexity estimation
156
+ const tokens = tokenUtils_1.countTokens(prompt, "gpt-4o");
157
+ let complexity = 0.3;
158
+ if (tokens > 1000) complexity += 0.2;
159
+ if (has_code) complexity += 0.15;
160
+ if (has_math) complexity += 0.2;
161
+ if (requires_reasoning) complexity += 0.15;
162
+ if (is_creative) complexity += 0.1;
163
+ if (is_security) complexity += 0.1;
164
+ if (is_devops) complexity += 0.1;
165
+ if (is_data) complexity += 0.15;
166
+ complexity = Math.min(1.0, complexity);
167
+
168
+ return {
169
+ complexity,
170
+ length: tokens,
171
+ has_code,
172
+ has_math,
173
+ is_multilingual,
174
+ is_translation,
175
+ is_creative,
176
+ requires_reasoning,
177
+ is_security,
178
+ is_devops,
179
+ is_data,
180
+ };
173
181
  }
174
- /**
175
- * Score model fit for query
176
- */
182
+
183
+ exports.extractQueryFeatures = extractQueryFeatures;
184
+
185
+ // ============================================================
186
+ // SCORING FUNCTIONS
187
+ // ============================================================
188
+
177
189
  function scoreModelFit(model, features) {
178
- let score = model.quality_score * 0.4; // Base quality
179
- // Strengths matching
180
- if (features.has_code && model.strengths.includes("coding")) {
181
- score += 0.2;
182
- }
183
- if (features.requires_reasoning && model.strengths.includes("reasoning")) {
184
- score += 0.2;
185
- }
186
- if (features.is_creative && model.strengths.includes("creative")) {
187
- score += 0.15;
188
- }
189
- if (features.is_multilingual && model.strengths.includes("multilingual")) {
190
- score += 0.15;
191
- }
192
- if (features.has_math && model.strengths.includes("analysis")) {
193
- score += 0.15;
194
- }
195
- // Speed bonus for simple tasks
196
- if (features.complexity < 0.4 && model.latency_ms < 1000) {
197
- score += 0.1;
198
- }
199
- return score;
190
+ let score = model.quality_score * 0.4; // Base quality
191
+
192
+ if (features.has_code && model.strengths.includes("coding")) score += 0.2;
193
+ if (features.requires_reasoning && model.strengths.includes("reasoning")) score += 0.2;
194
+ if (features.is_creative && model.strengths.includes("creative")) score += 0.15;
195
+ if (features.is_multilingual && model.strengths.includes("multilingual")) score += 0.15;
196
+ if (features.has_math && model.strengths.includes("analysis")) score += 0.15;
197
+ if (features.is_security && model.strengths.includes("reasoning")) score += 0.1;
198
+ if (features.is_data && model.strengths.includes("analysis")) score += 0.1;
199
+
200
+ // Free/local providers bonus for simple tasks
201
+ if (features.complexity < 0.4 && model.strengths.includes("free")) score += 0.15;
202
+ if (features.complexity < 0.4 && model.latency_ms < 1000) score += 0.1;
203
+
204
+ // Code-aware providers for code tasks
205
+ if (features.has_code && model.strengths.includes("code-aware")) score += 0.2;
206
+
207
+ return score;
200
208
  }
201
- /**
202
- * Cost efficiency score (inverse of normalized cost)
203
- */
209
+
204
210
  function costEfficiency(model, features) {
205
- const avg_cost = (model.cost_per_1k_input + model.cost_per_1k_output) / 2;
206
- // For simple tasks, prioritize cost efficiency
207
- if (features.complexity < 0.5) {
208
- return (1 - Math.min(avg_cost / 10, 1)) * 0.6;
209
- }
210
- // For complex tasks, deprioritize cost
211
- return (1 - Math.min(avg_cost / 10, 1)) * 0.2;
211
+ const avg_cost = (model.cost_per_1k_input + model.cost_per_1k_output) / 2;
212
+ if (features.complexity < 0.5) {
213
+ return (1 - Math.min(avg_cost / 10, 1)) * 0.6;
214
+ }
215
+ return (1 - Math.min(avg_cost / 10, 1)) * 0.2;
212
216
  }
213
- /**
214
- * RouteLLM-style learned routing decision
215
- */
217
+
218
+ // ============================================================
219
+ // ROUTING
220
+ // ============================================================
221
+
216
222
  function routeQuery(prompt, available_models, budget_multiplier = 1.0) {
217
- const features = extractQueryFeatures(prompt);
218
- const candidate_names = available_models || Object.keys(exports.MODEL_PROFILES);
219
- const candidates = candidate_names
220
- .filter(name => exports.MODEL_PROFILES[name])
221
- .map(name => {
222
- const profile = exports.MODEL_PROFILES[name];
223
- const quality = scoreModelFit(profile, features);
224
- const cost = costEfficiency(profile, features);
225
- return {
226
- name,
227
- profile,
228
- quality_score: quality,
229
- cost_score: cost,
230
- total_score: quality + cost
231
- };
232
- });
233
- // Sort by total score (quality vs cost tradeoff based on complexity)
234
- const complexity_bias = features.complexity > 0.6 ? 0.7 : 0.3; // High complexity = quality bias
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;
223
+ // Refresh profiles to ensure we have latest provider config
224
+ refreshModelProfiles();
225
+
226
+ const features = extractQueryFeatures(prompt);
227
+ const candidate_names = available_models || Object.keys(MODEL_PROFILES);
228
+
229
+ // Filter to available models
230
+ const candidates = candidate_names
231
+ .filter(name => MODEL_PROFILES[name])
232
+ .map(name => {
233
+ const profile = MODEL_PROFILES[name];
234
+ const quality = scoreModelFit(profile, features);
235
+ const cost = costEfficiency(profile, features);
236
+ return {
237
+ name,
238
+ profile,
239
+ quality_score: quality,
240
+ cost_score: cost,
241
+ total_score: quality + cost
242
+ };
239
243
  });
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);
244
+
245
+ if (candidates.length === 0) {
260
246
  return {
261
- primary_model: primary.name,
262
- fallback_models: secondary.map(c => c.name),
263
- confidence,
264
- reasoning: `Selected ${primary.profile.provider}/${primary.name.split("/")[1]} for ${reasons.join(", ") || "general query"}`,
265
- estimated_cost: estimated_cost * budget_multiplier,
266
- estimated_latency_ms: primary.profile.latency_ms
247
+ primary_model: null,
248
+ fallback_models: [],
249
+ confidence: 0,
250
+ reasoning: "No providers available - configure API keys in ~/.config/a3m-router/providers.json",
251
+ estimated_cost: 0,
252
+ estimated_latency_ms: 0,
267
253
  };
254
+ }
255
+
256
+ // Sort by total score (quality vs cost tradeoff based on complexity)
257
+ const complexity_bias = features.complexity > 0.6 ? 0.7 : 0.3;
258
+ candidates.sort((a, b) => {
259
+ const score_a = a.quality_score * complexity_bias + a.cost_score * (1 - complexity_bias);
260
+ const score_b = b.quality_score * complexity_bias + b.cost_score * (1 - complexity_bias);
261
+ return score_b - score_a;
262
+ });
263
+
264
+ const primary = candidates[0];
265
+ const secondary = candidates.slice(1, 3);
266
+
267
+ // Calculate confidence based on score gap
268
+ let confidence = 0.5;
269
+ if (candidates.length > 1) {
270
+ const gap = primary.total_score - candidates[1].total_score;
271
+ confidence = Math.min(0.95, 0.5 + gap * 2);
272
+ }
273
+
274
+ // Build reasoning
275
+ const reasons = [];
276
+ if (features.has_code) reasons.push("code detected");
277
+ if (features.requires_reasoning) reasons.push("reasoning needed");
278
+ if (features.complexity > 0.6) reasons.push("high complexity");
279
+ if (features.is_multilingual) reasons.push("multilingual");
280
+ if (features.is_translation) reasons.push("translation");
281
+ if (primary.profile.strengths.includes("free")) reasons.push("free tier");
282
+
283
+ const estimated_tokens = features.length * 1.5;
284
+ const estimated_cost = tokenUtils_1.estimateCost(features.length, estimated_tokens, primary.name);
285
+
286
+ return {
287
+ primary_model: primary.name,
288
+ fallback_models: secondary.map(c => c.name),
289
+ confidence,
290
+ reasoning: `Selected ${primary.profile.providerName || primary.profile.provider}/${primary.name} for ${reasons.join(", ") || "general query"}`,
291
+ estimated_cost: estimated_cost * budget_multiplier,
292
+ estimated_latency_ms: primary.profile.latency_ms,
293
+ features,
294
+ provider_type: primary.profile.type,
295
+ };
268
296
  }
269
- /**
270
- * Batch routing for multiple prompts
271
- */
272
- function routeBatch(prompts, options) {
273
- const decisions = prompts.map(p => routeQuery(p));
274
- if (options?.same_model && decisions.length > 0) {
275
- // Use first decision's model for all (for batch consistency)
276
- const primary_model = decisions[0].primary_model;
277
- decisions.forEach(d => {
278
- d.primary_model = primary_model;
279
- d.fallback_models = decisions[0].fallback_models;
280
- });
281
- }
282
- if (options?.max_cost_per_prompt) {
283
- decisions.forEach(d => {
284
- if (d.estimated_cost > options.max_cost_per_prompt) {
285
- // Route to cheaper alternative
286
- const cheap = Object.entries(exports.MODEL_PROFILES)
287
- .find(([name, p]) => p.cost_per_1k_input < 0.5);
288
- if (cheap) {
289
- d.primary_model = cheap[0];
290
- d.reasoning = `Budget-limited routing to ${cheap[1].provider}`;
291
- }
292
- }
293
- });
294
- }
295
- return decisions;
297
+
298
+ exports.routeQuery = routeQuery;
299
+
300
+ // ============================================================
301
+ // BATCH ROUTING
302
+ // ============================================================
303
+
304
+ function routeBatch(prompts, options = {}) {
305
+ const decisions = prompts.map(p => routeQuery(p));
306
+
307
+ if (options.same_model && decisions.length > 0) {
308
+ const primary_model = decisions[0].primary_model;
309
+ decisions.forEach(d => {
310
+ d.primary_model = primary_model;
311
+ d.fallback_models = decisions[0].fallback_models;
312
+ });
313
+ }
314
+
315
+ if (options.max_cost_per_prompt) {
316
+ decisions.forEach(d => {
317
+ if (d.estimated_cost > options.max_cost_per_prompt) {
318
+ const cheap = Object.entries(MODEL_PROFILES)
319
+ .find(([name, p]) => p.cost_per_1k_input < 0.5);
320
+ if (cheap) {
321
+ d.primary_model = cheap[0];
322
+ d.reasoning = `Budget-limited routing to ${cheap[1].providerName || cheap[1].provider}`;
323
+ }
324
+ }
325
+ });
326
+ }
327
+
328
+ return decisions;
296
329
  }
297
- /**
298
- * Get model recommendation for task type
299
- */
330
+
331
+ exports.routeBatch = routeBatch;
332
+
333
+ // ============================================================
334
+ // TASK RECOMMENDATIONS
335
+ // ============================================================
336
+
300
337
  function recommendForTask(task) {
301
- const features = extractQueryFeatures(task);
302
- const decision = routeQuery(task);
303
- // Return primary + fallbacks
304
- return [decision.primary_model, ...decision.fallback_models];
338
+ refreshModelProfiles();
339
+ const features = extractQueryFeatures(task);
340
+ const decision = routeQuery(task);
341
+ return {
342
+ primary: decision.primary_model,
343
+ fallbacks: decision.fallback_models,
344
+ reason: decision.reasoning,
345
+ features,
346
+ };
305
347
  }
306
- /**
307
- * Update model profile from execution feedback (online learning)
308
- */
309
- function updateModelProfile(model_name, actual_latency_ms, actual_cost, quality_rating // 0-1
310
- ) {
311
- const profile = exports.MODEL_PROFILES[model_name];
312
- if (!profile)
313
- return;
314
- // Exponential moving average update
315
- const alpha = 0.2; // Learning rate
316
- profile.latency_ms = profile.latency_ms * (1 - alpha) + actual_latency_ms * alpha;
317
- profile.quality_score = profile.quality_score * (1 - alpha) + quality_rating * alpha;
318
- // Adjust cost perception
319
- const actual_cost_per_1k = actual_cost * 1000;
320
- const current_avg_cost = (profile.cost_per_1k_input + profile.cost_per_1k_output) / 2;
321
- // Keep stored costs as reference, but note actual in profile
322
- console.log(`[RouteLLM] Updated ${model_name}: latency=${profile.latency_ms.toFixed(0)}ms, quality=${profile.quality_score.toFixed(2)}`);
348
+
349
+ exports.recommendForTask = recommendForTask;
350
+
351
+ // ============================================================
352
+ // ONLINE LEARNING - Update model profiles from feedback
353
+ // ============================================================
354
+
355
+ function updateModelProfile(model_name, actual_latency_ms, actual_cost, quality_rating) {
356
+ refreshModelProfiles();
357
+ const profile = MODEL_PROFILES[model_name];
358
+ if (!profile) return;
359
+
360
+ const alpha = 0.2; // Learning rate
361
+ profile.latency_ms = profile.latency_ms * (1 - alpha) + actual_latency_ms * alpha;
362
+ profile.quality_score = profile.quality_score * (1 - alpha) + quality_rating * alpha;
363
+ }
364
+
365
+ exports.updateModelProfile = updateModelProfile;
366
+
367
+ // ============================================================
368
+ // PROVIDER HEALTH CHECK
369
+ // ============================================================
370
+
371
+ async function getProviderHealth() {
372
+ const { checkAllProviders } = require("../providers/providerConfig");
373
+ return checkAllProviders();
323
374
  }
375
+
376
+ exports.getProviderHealth = getProviderHealth;
377
+
378
+ // ============================================================
379
+ // Default export
380
+ // ============================================================
381
+
324
382
  exports.default = {
325
- extractQueryFeatures,
326
- routeQuery,
327
- routeBatch,
328
- recommendForTask,
329
- updateModelProfile,
330
- MODEL_PROFILES: exports.MODEL_PROFILES
383
+ extractQueryFeatures,
384
+ routeQuery,
385
+ routeBatch,
386
+ recommendForTask,
387
+ updateModelProfile,
388
+ getProviderHealth,
389
+ MODEL_PROFILES,
331
390
  };
332
- //# sourceMappingURL=advancedRouter.js.map