adaptive-memory-multi-model-router 2.2.4 → 2.2.6

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.
Files changed (63) hide show
  1. package/README.md +17 -22
  2. package/README.md.bak +836 -0
  3. package/dist/analytics/costAnalytics.d.ts +1 -0
  4. package/dist/cache/cacheKeyGenerator.d.ts +67 -0
  5. package/dist/cache/cacheKeyGenerator.d.ts.map +1 -0
  6. package/dist/cache/cacheKeyGenerator.js +211 -0
  7. package/dist/cache/cacheKeyGenerator.js.map +1 -0
  8. package/dist/cache/semanticCache.d.ts +41 -0
  9. package/dist/cache/semanticCache.d.ts.map +1 -1
  10. package/dist/cache/semanticCache.js +142 -0
  11. package/dist/cache/semanticCache.js.map +1 -1
  12. package/dist/cli.js +35 -478
  13. package/dist/cost/costTracker.js +0 -3
  14. package/dist/cost/preCallCostEstimator.d.ts +114 -0
  15. package/dist/cost/preCallCostEstimator.d.ts.map +1 -0
  16. package/dist/cost/preCallCostEstimator.js +256 -0
  17. package/dist/cost/preCallCostEstimator.js.map +1 -0
  18. package/dist/index.d.ts +16 -0
  19. package/dist/index.d.ts.map +1 -1
  20. package/dist/index.js +264 -64
  21. package/dist/index.js.map +1 -1
  22. package/dist/inference/speculativeDecoding.d.ts +133 -0
  23. package/dist/inference/speculativeDecoding.d.ts.map +1 -0
  24. package/dist/inference/speculativeDecoding.js +276 -0
  25. package/dist/inference/speculativeDecoding.js.map +1 -0
  26. package/dist/integrations/langchainAdapter.d.ts +1 -0
  27. package/dist/integrations/oauth.d.ts +1 -0
  28. package/dist/memory/autoFetch.d.ts +1 -0
  29. package/dist/memory/memoryTree.d.ts +1 -0
  30. package/dist/memory/obsidianVault.d.ts +1 -0
  31. package/dist/providers/providerConfig.d.ts +1 -0
  32. package/dist/providers/providerConfig.js +2 -0
  33. package/dist/providers/providerHealth.d.ts +117 -0
  34. package/dist/providers/providerHealth.d.ts.map +1 -0
  35. package/dist/providers/providerHealth.js +309 -0
  36. package/dist/providers/providerHealth.js.map +1 -0
  37. package/dist/providers/registry.js +126 -128
  38. package/dist/routing/advancedRouter.js +310 -427
  39. package/dist/routing/difficultyClassifier.d.ts +79 -0
  40. package/dist/routing/difficultyClassifier.d.ts.map +1 -0
  41. package/dist/routing/difficultyClassifier.js +329 -0
  42. package/dist/routing/difficultyClassifier.js.map +1 -0
  43. package/dist/sdk.d.ts +125 -0
  44. package/dist/sdk.d.ts.map +1 -0
  45. package/dist/sdk.js +109 -100
  46. package/dist/sdk.js.map +1 -0
  47. package/dist/security/guardrails.d.ts +1 -0
  48. package/dist/server/dashboard.d.ts +1 -0
  49. package/dist/server/modelMapper.d.ts +1 -0
  50. package/dist/server/proxyServer.d.ts +1 -0
  51. package/package.json +106 -3
  52. package/src/cache/cacheKeyGenerator.ts +242 -0
  53. package/src/cache/semanticCache.ts +148 -0
  54. package/src/cost/preCallCostEstimator.ts +345 -0
  55. package/src/inference/speculativeDecoding.ts +373 -0
  56. package/src/providers/providerHealth.ts +397 -0
  57. package/src/routing/difficultyClassifier.ts +420 -0
  58. package/test/provider-test.js +2 -2
  59. package/test.js +7 -7
  60. package/test.js.bak +376 -0
  61. package/tsconfig.json +15 -5
  62. package/src/index.ts +0 -99
  63. package/src/skills/__tests__/skill_manager.test.ts +0 -328
@@ -1,453 +1,336 @@
1
1
  "use strict";
2
2
  /**
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.
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
13
7
  */
14
-
15
- const { getAvailableProviders } = require("../providers/providerConfig");
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;
16
15
  const tokenUtils_1 = require("../utils/tokenUtils");
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
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
110
27
  },
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
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
117
37
  },
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
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
125
47
  },
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
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
133
57
  },
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
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
141
67
  },
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
149
- }
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
- }
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
162
107
  }
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
- };
108
+ };
109
+ /**
110
+ * Extract features from prompt for routing decision
111
+ */
112
+ 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
+ };
244
173
  }
245
-
246
- exports.extractQueryFeatures = extractQueryFeatures;
247
-
248
- // ============================================================
249
- // SCORING FUNCTIONS
250
- // ============================================================
251
-
174
+ /**
175
+ * Score model fit for query
176
+ */
252
177
  function scoreModelFit(model, features) {
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;
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;
271
200
  }
272
-
201
+ /**
202
+ * Cost efficiency score (inverse of normalized cost)
203
+ */
273
204
  function costEfficiency(model, features) {
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;
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;
279
212
  }
280
-
281
- // ============================================================
282
- // ROUTING
283
- // ============================================================
284
-
213
+ /**
214
+ * RouteLLM-style learned routing decision
215
+ */
285
216
  function routeQuery(prompt, available_models, budget_multiplier = 1.0) {
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
- };
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
+ };
306
232
  });
307
-
308
- if (candidates.length === 0) {
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;
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);
309
260
  return {
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,
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
316
267
  };
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
- };
359
268
  }
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;
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;
392
296
  }
393
-
394
- exports.routeBatch = routeBatch;
395
-
396
- // ============================================================
397
- // TASK RECOMMENDATIONS
398
- // ============================================================
399
-
297
+ /**
298
+ * Get model recommendation for task type
299
+ */
400
300
  function recommendForTask(task) {
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
- };
410
- }
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;
301
+ const features = extractQueryFeatures(task);
302
+ const decision = routeQuery(task);
303
+ // Return object format for backward compatibility
304
+ return {
305
+ primary: decision.primary_model,
306
+ fallbacks: decision.fallback_models || [],
307
+ reason: decision.reasoning || 'routed based on task'
308
+ };
426
309
  }
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();
310
+ /**
311
+ * Update model profile from execution feedback (online learning)
312
+ */
313
+ function updateModelProfile(model_name, actual_latency_ms, actual_cost, quality_rating // 0-1
314
+ ) {
315
+ const profile = exports.MODEL_PROFILES[model_name];
316
+ if (!profile)
317
+ return;
318
+ // Exponential moving average update
319
+ const alpha = 0.2; // Learning rate
320
+ profile.latency_ms = profile.latency_ms * (1 - alpha) + actual_latency_ms * alpha;
321
+ profile.quality_score = profile.quality_score * (1 - alpha) + quality_rating * alpha;
322
+ // Adjust cost perception
323
+ const actual_cost_per_1k = actual_cost * 1000;
324
+ const current_avg_cost = (profile.cost_per_1k_input + profile.cost_per_1k_output) / 2;
325
+ // Keep stored costs as reference, but note actual in profile
326
+ console.log(`[RouteLLM] Updated ${model_name}: latency=${profile.latency_ms.toFixed(0)}ms, quality=${profile.quality_score.toFixed(2)}`);
437
327
  }
438
-
439
- exports.getProviderHealth = getProviderHealth;
440
-
441
- // ============================================================
442
- // Default export
443
- // ============================================================
444
-
445
328
  exports.default = {
446
- extractQueryFeatures,
447
- routeQuery,
448
- routeBatch,
449
- recommendForTask,
450
- updateModelProfile,
451
- getProviderHealth,
452
- MODEL_PROFILES,
329
+ extractQueryFeatures,
330
+ routeQuery,
331
+ routeBatch,
332
+ recommendForTask,
333
+ updateModelProfile,
334
+ MODEL_PROFILES: exports.MODEL_PROFILES
453
335
  };
336
+ //# sourceMappingURL=advancedRouter.js.map